Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_OPTIMIZING_INDUCTION_VAR_ANALYSIS_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_INDUCTION_VAR_ANALYSIS_H_ |
| 19 | |
| 20 | #include <string> |
| 21 | |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 22 | #include "base/arena_containers.h" |
| 23 | #include "base/array_ref.h" |
| 24 | #include "base/scoped_arena_containers.h" |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 25 | #include "nodes.h" |
| 26 | #include "optimization.h" |
| 27 | |
Vladimir Marko | 0a51605 | 2019-10-14 13:00:44 +0000 | [diff] [blame] | 28 | namespace art { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 31 | * Induction variable analysis. This class does not have a direct public API. |
| 32 | * Instead, the results of induction variable analysis can be queried through |
| 33 | * friend classes, such as InductionVarRange. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 34 | * |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 35 | * The analysis implementation is based on the paper by M. Gerlek et al. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 36 | * "Beyond Induction Variables: Detecting and Classifying Sequences Using a Demand-Driven SSA Form" |
| 37 | * (ACM Transactions on Programming Languages and Systems, Volume 17 Issue 1, Jan. 1995). |
| 38 | */ |
| 39 | class HInductionVarAnalysis : public HOptimization { |
| 40 | public: |
Aart Bik | 2ca10eb | 2017-11-15 15:17:53 -0800 | [diff] [blame] | 41 | explicit HInductionVarAnalysis(HGraph* graph, const char* name = kInductionPassName); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 42 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 43 | bool Run() override; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 44 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 45 | static constexpr const char* kInductionPassName = "induction_var_analysis"; |
| 46 | |
Wojciech Staszkiewicz | 5319d3c | 2016-08-01 17:48:59 -0700 | [diff] [blame] | 47 | private: |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 48 | struct NodeInfo; |
| 49 | struct StackEntry; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 50 | |
| 51 | enum InductionClass { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 52 | kInvariant, |
| 53 | kLinear, |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 54 | kPolynomial, |
| 55 | kGeometric, |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 56 | kWrapAround, |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 57 | kPeriodic |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | enum InductionOp { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 61 | // Operations. |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 62 | kNop, |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 63 | kAdd, |
| 64 | kSub, |
| 65 | kNeg, |
| 66 | kMul, |
| 67 | kDiv, |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 68 | kRem, |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 69 | kXor, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 70 | kFetch, |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 71 | // Trip-counts. |
| 72 | kTripCountInLoop, // valid in full loop; loop is finite |
| 73 | kTripCountInBody, // valid in body only; loop is finite |
| 74 | kTripCountInLoopUnsafe, // valid in full loop; loop may be infinite |
| 75 | kTripCountInBodyUnsafe, // valid in body only; loop may be infinite |
| 76 | // Comparisons for trip-count tests. |
| 77 | kLT, |
| 78 | kLE, |
| 79 | kGT, |
| 80 | kGE |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | /** |
| 84 | * Defines a detected induction as: |
| 85 | * (1) invariant: |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 86 | * op: a + b, a - b, -b, a * b, a / b, a % b, a ^ b, fetch |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 87 | * (2) linear: |
| 88 | * nop: a * i + b |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 89 | * (3) polynomial: |
| 90 | * nop: sum_lt(a) + b, for linear a |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 91 | * (4) geometric: |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 92 | * op: a * fetch^i + b, a * fetch^-i + b |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 93 | * (5) wrap-around |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 94 | * nop: a, then defined by b |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 95 | * (6) periodic |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 96 | * nop: a, then defined by b (repeated when exhausted) |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 97 | * (7) trip-count: |
Aart Bik | 22f0587 | 2015-10-27 15:56:28 -0700 | [diff] [blame] | 98 | * tc: defined by a, taken-test in b |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 99 | */ |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 100 | struct InductionInfo : public ArenaObject<kArenaAllocInductionVarAnalysis> { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 101 | InductionInfo(InductionClass ic, |
| 102 | InductionOp op, |
| 103 | InductionInfo* a, |
| 104 | InductionInfo* b, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 105 | HInstruction* f, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 106 | DataType::Type t) |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 107 | : induction_class(ic), |
| 108 | operation(op), |
| 109 | op_a(a), |
| 110 | op_b(b), |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 111 | fetch(f), |
| 112 | type(t) {} |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 113 | InductionClass induction_class; |
| 114 | InductionOp operation; |
| 115 | InductionInfo* op_a; |
| 116 | InductionInfo* op_b; |
| 117 | HInstruction* fetch; |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 118 | DataType::Type type; // precision of operation |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 121 | |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 122 | InductionInfo* CreateInvariantOp(const HBasicBlock* context, |
| 123 | const HLoopInformation* loop, |
| 124 | InductionOp op, |
| 125 | InductionInfo* a, |
| 126 | InductionInfo* b) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 127 | DCHECK(((op != kNeg && a != nullptr) || (op == kNeg && a == nullptr)) && b != nullptr); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 128 | return CreateSimplifiedInvariant(context, loop, op, a, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 131 | InductionInfo* CreateInvariantFetch(HInstruction* f) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 132 | DCHECK(f != nullptr); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 133 | return new (graph_->GetAllocator()) |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 134 | InductionInfo(kInvariant, kFetch, nullptr, nullptr, f, f->GetType()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 137 | InductionInfo* CreateTripCount(InductionOp op, |
| 138 | InductionInfo* a, |
| 139 | InductionInfo* b, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 140 | DataType::Type type) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 141 | DCHECK(a != nullptr && b != nullptr); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 142 | return new (graph_->GetAllocator()) InductionInfo(kInvariant, op, a, b, nullptr, type); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 145 | InductionInfo* CreateInduction(InductionClass ic, |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 146 | InductionOp op, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 147 | InductionInfo* a, |
| 148 | InductionInfo* b, |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 149 | HInstruction* f, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 150 | DataType::Type type) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 151 | DCHECK(a != nullptr && b != nullptr); |
Vladimir Marko | ca6fff8 | 2017-10-03 14:49:14 +0100 | [diff] [blame] | 152 | return new (graph_->GetAllocator()) InductionInfo(ic, op, a, b, f, type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // Methods for analysis. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 156 | void VisitLoop(const HLoopInformation* loop); |
| 157 | size_t TryVisitNodes(const HLoopInformation* loop, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 158 | HInstruction* start_instruction, |
| 159 | size_t global_depth, |
| 160 | /*inout*/ ScopedArenaSafeMap<HInstruction*, NodeInfo>* visited_instructions); |
| 161 | void ExtractScc(ArrayRef<const StackEntry> stack_tail, ScopedArenaVector<HInstruction*>* scc); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 162 | void ClassifyTrivial(const HLoopInformation* loop, HInstruction* instruction); |
| 163 | void ClassifyNonTrivial(const HLoopInformation* loop, ArrayRef<const StackEntry> stack_tail); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 164 | InductionInfo* RotatePeriodicInduction(InductionInfo* induction, |
| 165 | InductionInfo* last, |
| 166 | DataType::Type type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 167 | |
| 168 | // Transfer operations. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 169 | InductionInfo* TransferPhi(const HLoopInformation* loop, |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 170 | HInstruction* phi, |
| 171 | size_t input_index, |
| 172 | size_t adjust_input_size); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 173 | InductionInfo* TransferAddSub(const HBasicBlock* context, |
| 174 | const HLoopInformation* loop, |
| 175 | InductionInfo* a, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 176 | InductionInfo* b, |
| 177 | InductionOp op, |
| 178 | DataType::Type type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 179 | InductionInfo* TransferNeg(const HBasicBlock* context, |
| 180 | const HLoopInformation* loop, |
| 181 | InductionInfo* a, |
| 182 | DataType::Type type); |
| 183 | InductionInfo* TransferMul(const HBasicBlock* context, |
| 184 | const HLoopInformation* loop, |
| 185 | InductionInfo* a, |
| 186 | InductionInfo* b, |
| 187 | DataType::Type type); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 188 | InductionInfo* TransferConversion(InductionInfo* a, DataType::Type from, DataType::Type to); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 189 | |
| 190 | // Solvers. |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 191 | InductionInfo* SolvePhi(HInstruction* phi, |
| 192 | size_t input_index, |
| 193 | size_t adjust_input_size, |
| 194 | const ScopedArenaSafeMap<HInstruction*, InductionInfo*>& cycle); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 195 | InductionInfo* SolvePhiAllInputs(const HLoopInformation* loop, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 196 | HInstruction* entry_phi, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 197 | HInstruction* phi, |
| 198 | const ScopedArenaSafeMap<HInstruction*, InductionInfo*>& cycle, |
| 199 | DataType::Type type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 200 | InductionInfo* SolveAddSub(const HLoopInformation* loop, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 201 | HInstruction* entry_phi, |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 202 | HInstruction* instruction, |
| 203 | HInstruction* x, |
| 204 | HInstruction* y, |
| 205 | InductionOp op, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 206 | const ScopedArenaSafeMap<HInstruction*, InductionInfo*>& cycle, |
| 207 | DataType::Type type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 208 | InductionInfo* SolveOp(const HLoopInformation* loop, |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame] | 209 | HInstruction* entry_phi, |
| 210 | HInstruction* instruction, |
| 211 | HInstruction* x, |
| 212 | HInstruction* y, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 213 | InductionOp op, |
| 214 | DataType::Type type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 215 | InductionInfo* SolveTest(const HLoopInformation* loop, |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 216 | HInstruction* entry_phi, |
| 217 | HInstruction* instruction, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 218 | int64_t opposite_value, |
| 219 | DataType::Type type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 220 | InductionInfo* SolveConversion(const HLoopInformation* loop, |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 221 | HInstruction* entry_phi, |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 222 | HTypeConversion* conversion, |
| 223 | const ScopedArenaSafeMap<HInstruction*, InductionInfo*>& cycle, |
| 224 | /*inout*/ DataType::Type* type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 225 | |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 226 | // |
| 227 | // Loop trip count analysis methods. |
| 228 | // |
| 229 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 230 | // Trip count information. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 231 | void VisitControl(const HLoopInformation* loop); |
| 232 | void VisitCondition(const HBasicBlock* context, |
| 233 | const HLoopInformation* loop, |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 234 | HBasicBlock* body, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 235 | InductionInfo* a, |
| 236 | InductionInfo* b, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 237 | DataType::Type type, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 238 | IfCondition cmp); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 239 | void VisitTripCount(const HBasicBlock* context, |
| 240 | const HLoopInformation* loop, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 241 | InductionInfo* lower_expr, |
| 242 | InductionInfo* upper_expr, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 243 | InductionInfo* stride, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 244 | int64_t stride_value, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 245 | DataType::Type type, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 246 | IfCondition cmp); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 247 | bool IsTaken(const HBasicBlock* context, |
| 248 | const HLoopInformation* loop, |
| 249 | InductionInfo* lower_expr, |
| 250 | InductionInfo* upper_expr, |
| 251 | IfCondition cmp); |
| 252 | bool IsFinite(const HBasicBlock* context, |
| 253 | const HLoopInformation* loop, |
| 254 | InductionInfo* upper_expr, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 255 | int64_t stride_value, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 256 | DataType::Type type, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 257 | IfCondition cmp); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 258 | bool FitsNarrowerControl(const HBasicBlock* context, |
| 259 | const HLoopInformation* loop, |
| 260 | InductionInfo* lower_expr, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 261 | InductionInfo* upper_expr, |
| 262 | int64_t stride_value, |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 263 | DataType::Type type, |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 264 | IfCondition cmp); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 265 | bool RewriteBreakLoop(const HBasicBlock* context, |
| 266 | const HLoopInformation* loop, |
Aart Bik | ceb0693 | 2017-11-13 10:31:17 -0800 | [diff] [blame] | 267 | HBasicBlock* body, |
| 268 | int64_t stride_value, |
| 269 | DataType::Type type); |
| 270 | |
| 271 | // |
| 272 | // Helper methods. |
| 273 | // |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 274 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 275 | // Assign and lookup. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 276 | void AssignInfo(const HLoopInformation* loop, HInstruction* instruction, InductionInfo* info); |
| 277 | InductionInfo* LookupInfo(const HLoopInformation* loop, HInstruction* instruction); |
Vladimir Marko | 0ebe0d8 | 2017-09-21 22:50:39 +0100 | [diff] [blame] | 278 | InductionInfo* CreateConstant(int64_t value, DataType::Type type); |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 279 | InductionInfo* CreateSimplifiedInvariant(const HBasicBlock* context, |
| 280 | const HLoopInformation* loop, |
| 281 | InductionOp op, |
| 282 | InductionInfo* a, |
| 283 | InductionInfo* b); |
| 284 | HInstruction* GetShiftConstant(const HLoopInformation* loop, |
Aart Bik | d0a022d | 2016-12-13 11:22:31 -0800 | [diff] [blame] | 285 | HInstruction* instruction, |
| 286 | InductionInfo* initial); |
Vladimir Marko | de4d195 | 2022-03-07 09:29:40 +0000 | [diff] [blame] | 287 | void AssignCycle(HPhi* phi, ArrayRef<HInstruction* const> scc); |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 288 | ArenaSet<HInstruction*>* LookupCycle(HPhi* phi); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 289 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 290 | // Constants. |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 291 | bool IsExact(const HBasicBlock* context, |
| 292 | const HLoopInformation* loop, |
| 293 | InductionInfo* info, |
| 294 | /*out*/int64_t* value); |
| 295 | bool IsAtMost(const HBasicBlock* context, |
| 296 | const HLoopInformation* loop, |
| 297 | InductionInfo* info, |
| 298 | /*out*/int64_t* value); |
| 299 | bool IsAtLeast(const HBasicBlock* context, |
| 300 | const HLoopInformation* loop, |
| 301 | InductionInfo* info, |
| 302 | /*out*/int64_t* value); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 303 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 304 | // Helpers. |
Aart Bik | e6bd027 | 2016-12-16 13:57:52 -0800 | [diff] [blame] | 305 | static bool IsNarrowingLinear(InductionInfo* info); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 306 | static bool InductionEqual(InductionInfo* info1, InductionInfo* info2); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 307 | static std::string FetchToString(HInstruction* fetch); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 308 | static std::string InductionToString(InductionInfo* info); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 309 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 310 | /** |
| 311 | * Maintains the results of the analysis as a mapping from loops to a mapping from instructions |
| 312 | * to the induction information for that instruction in that loop. |
| 313 | */ |
Vladimir Marko | 8d100ba | 2022-03-04 10:13:10 +0000 | [diff] [blame] | 314 | ArenaSafeMap<const HLoopInformation*, ArenaSafeMap<HInstruction*, InductionInfo*>> induction_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 315 | |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 316 | /** |
| 317 | * Preserves induction cycle information for each loop-phi. |
| 318 | */ |
| 319 | ArenaSafeMap<HPhi*, ArenaSet<HInstruction*>> cycles_; |
| 320 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 321 | friend class InductionVarAnalysisTest; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 322 | friend class InductionVarRange; |
| 323 | friend class InductionVarRangeTest; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 324 | |
| 325 | DISALLOW_COPY_AND_ASSIGN(HInductionVarAnalysis); |
| 326 | }; |
| 327 | |
| 328 | } // namespace art |
| 329 | |
| 330 | #endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_ANALYSIS_H_ |