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