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 | |
| 22 | #include "nodes.h" |
| 23 | #include "optimization.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | /** |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 28 | * Induction variable analysis. This class does not have a direct public API. |
| 29 | * Instead, the results of induction variable analysis can be queried through |
| 30 | * friend classes, such as InductionVarRange. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 31 | * |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 32 | * 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] | 33 | * "Beyond Induction Variables: Detecting and Classifying Sequences Using a Demand-Driven SSA Form" |
| 34 | * (ACM Transactions on Programming Languages and Systems, Volume 17 Issue 1, Jan. 1995). |
| 35 | */ |
| 36 | class HInductionVarAnalysis : public HOptimization { |
| 37 | public: |
| 38 | explicit HInductionVarAnalysis(HGraph* graph); |
| 39 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 40 | void Run() OVERRIDE; |
| 41 | |
| 42 | private: |
| 43 | static constexpr const char* kInductionPassName = "induction_var_analysis"; |
| 44 | |
| 45 | struct NodeInfo { |
| 46 | explicit NodeInfo(uint32_t d) : depth(d), done(false) {} |
| 47 | uint32_t depth; |
| 48 | bool done; |
| 49 | }; |
| 50 | |
| 51 | enum InductionClass { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 52 | kInvariant, |
| 53 | kLinear, |
| 54 | kWrapAround, |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 55 | kPeriodic |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | enum InductionOp { |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame^] | 59 | // No-operation: a true induction. |
| 60 | kNop, |
| 61 | // Various invariant operations. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 62 | kAdd, |
| 63 | kSub, |
| 64 | kNeg, |
| 65 | kMul, |
| 66 | kDiv, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame^] | 67 | kFetch, |
| 68 | // Trip counts (valid in full loop or only body proper; unsafe implies loop may be infinite). |
| 69 | kTripCountInLoop, |
| 70 | kTripCountInBody, |
| 71 | kTripCountInLoopUnsafe, |
| 72 | kTripCountInBodyUnsafe |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * Defines a detected induction as: |
| 77 | * (1) invariant: |
| 78 | * operation: a + b, a - b, -b, a * b, a / b |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 79 | * or: |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 80 | * fetch: fetch from HIR |
| 81 | * (2) linear: |
| 82 | * nop: a * i + b |
| 83 | * (3) wrap-around |
| 84 | * nop: a, then defined by b |
| 85 | * (4) periodic |
| 86 | * nop: a, then defined by b (repeated when exhausted) |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame^] | 87 | * (5) trip-count: |
| 88 | * tc: defined by b |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 89 | */ |
| 90 | struct InductionInfo : public ArenaObject<kArenaAllocMisc> { |
| 91 | InductionInfo(InductionClass ic, |
| 92 | InductionOp op, |
| 93 | InductionInfo* a, |
| 94 | InductionInfo* b, |
| 95 | HInstruction* f) |
| 96 | : induction_class(ic), |
| 97 | operation(op), |
| 98 | op_a(a), |
| 99 | op_b(b), |
| 100 | fetch(f) {} |
| 101 | InductionClass induction_class; |
| 102 | InductionOp operation; |
| 103 | InductionInfo* op_a; |
| 104 | InductionInfo* op_b; |
| 105 | HInstruction* fetch; |
| 106 | }; |
| 107 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 108 | bool IsVisitedNode(HInstruction* instruction) const { |
| 109 | return map_.find(instruction) != map_.end(); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 112 | InductionInfo* CreateInvariantOp(InductionOp op, InductionInfo* a, InductionInfo* b) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 113 | DCHECK(((op != kNeg && a != nullptr) || (op == kNeg && a == nullptr)) && b != nullptr); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 114 | return CreateSimplifiedInvariant(op, a, b); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 117 | InductionInfo* CreateInvariantFetch(HInstruction* f) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 118 | DCHECK(f != nullptr); |
| 119 | return new (graph_->GetArena()) InductionInfo(kInvariant, kFetch, nullptr, nullptr, f); |
| 120 | } |
| 121 | |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame^] | 122 | InductionInfo* CreateTripCount(InductionOp op, InductionInfo* b) { |
| 123 | return new (graph_->GetArena()) InductionInfo(kInvariant, op, nullptr, b, nullptr); |
| 124 | } |
| 125 | |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 126 | InductionInfo* CreateInduction(InductionClass ic, InductionInfo* a, InductionInfo* b) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 127 | DCHECK(a != nullptr && b != nullptr); |
| 128 | return new (graph_->GetArena()) InductionInfo(ic, kNop, a, b, nullptr); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // Methods for analysis. |
| 132 | void VisitLoop(HLoopInformation* loop); |
| 133 | void VisitNode(HLoopInformation* loop, HInstruction* instruction); |
| 134 | uint32_t VisitDescendant(HLoopInformation* loop, HInstruction* instruction); |
| 135 | void ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction); |
| 136 | void ClassifyNonTrivial(HLoopInformation* loop); |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 137 | InductionInfo* RotatePeriodicInduction(InductionInfo* induction, InductionInfo* last); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 138 | |
| 139 | // Transfer operations. |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 140 | InductionInfo* TransferPhi(HLoopInformation* loop, HInstruction* phi, size_t input_index); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 141 | InductionInfo* TransferAddSub(InductionInfo* a, InductionInfo* b, InductionOp op); |
| 142 | InductionInfo* TransferMul(InductionInfo* a, InductionInfo* b); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 143 | InductionInfo* TransferShl(InductionInfo* a, InductionInfo* b, Primitive::Type type); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 144 | InductionInfo* TransferNeg(InductionInfo* a); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 145 | |
| 146 | // Solvers. |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 147 | InductionInfo* SolvePhi(HInstruction* phi, size_t input_index); |
| 148 | InductionInfo* SolvePhiAllInputs(HLoopInformation* loop, |
| 149 | HInstruction* entry_phi, |
| 150 | HInstruction* phi); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 151 | InductionInfo* SolveAddSub(HLoopInformation* loop, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 152 | HInstruction* entry_phi, |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 153 | HInstruction* instruction, |
| 154 | HInstruction* x, |
| 155 | HInstruction* y, |
| 156 | InductionOp op, |
| 157 | bool is_first_call); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 158 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 159 | // Trip count information. |
| 160 | void VisitControl(HLoopInformation* loop); |
| 161 | void VisitCondition(HLoopInformation* loop, |
| 162 | InductionInfo* a, |
| 163 | InductionInfo* b, |
| 164 | Primitive::Type type, |
| 165 | IfCondition cmp); |
| 166 | void VisitTripCount(HLoopInformation* loop, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame^] | 167 | InductionInfo* lower_expr, |
| 168 | InductionInfo* upper_expr, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 169 | InductionInfo* stride, |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame^] | 170 | int64_t stride_value, |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 171 | Primitive::Type type, |
Aart Bik | f475bee | 2015-09-16 12:50:25 -0700 | [diff] [blame] | 172 | IfCondition cmp); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame^] | 173 | bool IsTaken(InductionInfo* lower_expr, InductionInfo* upper_expr, IfCondition cmp); |
| 174 | bool IsFinite(InductionInfo* upper_expr, |
| 175 | int64_t stride_value, |
| 176 | Primitive::Type type, |
| 177 | IfCondition cmp); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 178 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 179 | // Assign and lookup. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 180 | void AssignInfo(HLoopInformation* loop, HInstruction* instruction, InductionInfo* info); |
| 181 | InductionInfo* LookupInfo(HLoopInformation* loop, HInstruction* instruction); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 182 | InductionInfo* CreateConstant(int64_t value, Primitive::Type type); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 183 | InductionInfo* CreateSimplifiedInvariant(InductionOp op, InductionInfo* a, InductionInfo* b); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 184 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 185 | // Helpers. |
| 186 | static bool InductionEqual(InductionInfo* info1, InductionInfo* info2); |
Aart Bik | 471a203 | 2015-09-04 18:22:11 -0700 | [diff] [blame] | 187 | static bool IsIntAndGet(InductionInfo* info, int64_t* value); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 188 | static std::string InductionToString(InductionInfo* info); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 189 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 190 | // TODO: fine tune the following data structures, only keep relevant data. |
| 191 | |
| 192 | // Temporary book-keeping during the analysis. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 193 | uint32_t global_depth_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 194 | ArenaVector<HInstruction*> stack_; |
| 195 | ArenaVector<HInstruction*> scc_; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 196 | ArenaSafeMap<HInstruction*, NodeInfo> map_; |
| 197 | ArenaSafeMap<HInstruction*, InductionInfo*> cycle_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 198 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 199 | /** |
| 200 | * Maintains the results of the analysis as a mapping from loops to a mapping from instructions |
| 201 | * to the induction information for that instruction in that loop. |
| 202 | */ |
| 203 | ArenaSafeMap<HLoopInformation*, ArenaSafeMap<HInstruction*, InductionInfo*>> induction_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 204 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 205 | friend class InductionVarAnalysisTest; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 206 | friend class InductionVarRange; |
| 207 | friend class InductionVarRangeTest; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 208 | |
| 209 | DISALLOW_COPY_AND_ASSIGN(HInductionVarAnalysis); |
| 210 | }; |
| 211 | |
| 212 | } // namespace art |
| 213 | |
| 214 | #endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_ANALYSIS_H_ |