blob: 94afc71c3d3a97b6891cb55c4d25ce1a8a325e52 [file] [log] [blame]
Aart Bik30efb4e2015-07-30 12:14:31 -07001/*
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
25namespace art {
26
27/**
Aart Bike609b7c2015-08-27 13:46:58 -070028 * 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 Bik30efb4e2015-07-30 12:14:31 -070031 *
Aart Bike609b7c2015-08-27 13:46:58 -070032 * The analysis implementation is based on the paper by M. Gerlek et al.
Aart Bik30efb4e2015-07-30 12:14:31 -070033 * "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 */
36class HInductionVarAnalysis : public HOptimization {
37 public:
38 explicit HInductionVarAnalysis(HGraph* graph);
39
Aart Bik30efb4e2015-07-30 12:14:31 -070040 void Run() OVERRIDE;
41
Aart Bik30efb4e2015-07-30 12:14:31 -070042 static constexpr const char* kInductionPassName = "induction_var_analysis";
43
Wojciech Staszkiewicz5319d3c2016-08-01 17:48:59 -070044 private:
Aart Bik30efb4e2015-07-30 12:14:31 -070045 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 Bik30efb4e2015-07-30 12:14:31 -070052 kInvariant,
53 kLinear,
Aart Bikc071a012016-12-01 10:22:31 -080054 kPolynomial,
55 kGeometric,
Aart Bik30efb4e2015-07-30 12:14:31 -070056 kWrapAround,
Aart Bike609b7c2015-08-27 13:46:58 -070057 kPeriodic
Aart Bik30efb4e2015-07-30 12:14:31 -070058 };
59
60 enum InductionOp {
Aart Bikc071a012016-12-01 10:22:31 -080061 // Operations.
Aart Bik9401f532015-09-28 16:25:56 -070062 kNop,
Aart Bik30efb4e2015-07-30 12:14:31 -070063 kAdd,
64 kSub,
65 kNeg,
66 kMul,
67 kDiv,
Aart Bik7dc96932016-10-12 10:01:05 -070068 kXor,
Aart Bik9401f532015-09-28 16:25:56 -070069 kFetch,
Aart Bik22f05872015-10-27 15:56:28 -070070 // Trip-counts.
71 kTripCountInLoop, // valid in full loop; loop is finite
72 kTripCountInBody, // valid in body only; loop is finite
73 kTripCountInLoopUnsafe, // valid in full loop; loop may be infinite
74 kTripCountInBodyUnsafe, // valid in body only; loop may be infinite
75 // Comparisons for trip-count tests.
76 kLT,
77 kLE,
78 kGT,
79 kGE
Aart Bik30efb4e2015-07-30 12:14:31 -070080 };
81
82 /**
83 * Defines a detected induction as:
84 * (1) invariant:
Aart Bikc071a012016-12-01 10:22:31 -080085 * op: a + b, a - b, -b, a * b, a / b, a % b, a ^ b, fetch
Aart Bik30efb4e2015-07-30 12:14:31 -070086 * (2) linear:
87 * nop: a * i + b
Aart Bikc071a012016-12-01 10:22:31 -080088 * (3) polynomial: // TODO: coming soon
89 * nop: sum_i(a) + b, for linear a
90 * (4) geometric:
91 * op: a * fetch^i + b, a * fetch^-i + b, a mod_i fetch + b
92 * (5) wrap-around
Aart Bik30efb4e2015-07-30 12:14:31 -070093 * nop: a, then defined by b
Aart Bikc071a012016-12-01 10:22:31 -080094 * (6) periodic
Aart Bik30efb4e2015-07-30 12:14:31 -070095 * nop: a, then defined by b (repeated when exhausted)
Aart Bikc071a012016-12-01 10:22:31 -080096 * (7) trip-count:
Aart Bik22f05872015-10-27 15:56:28 -070097 * tc: defined by a, taken-test in b
Aart Bik30efb4e2015-07-30 12:14:31 -070098 */
Vladimir Marko5233f932015-09-29 19:01:15 +010099 struct InductionInfo : public ArenaObject<kArenaAllocInductionVarAnalysis> {
Aart Bik30efb4e2015-07-30 12:14:31 -0700100 InductionInfo(InductionClass ic,
101 InductionOp op,
102 InductionInfo* a,
103 InductionInfo* b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700104 HInstruction* f,
105 Primitive::Type t)
Aart Bik30efb4e2015-07-30 12:14:31 -0700106 : induction_class(ic),
107 operation(op),
108 op_a(a),
109 op_b(b),
Aart Bik0d345cf2016-03-16 10:49:38 -0700110 fetch(f),
111 type(t) {}
Aart Bik30efb4e2015-07-30 12:14:31 -0700112 InductionClass induction_class;
113 InductionOp operation;
114 InductionInfo* op_a;
115 InductionInfo* op_b;
116 HInstruction* fetch;
Aart Bik0d345cf2016-03-16 10:49:38 -0700117 Primitive::Type type; // precision of induction
Aart Bik30efb4e2015-07-30 12:14:31 -0700118 };
119
Aart Bike609b7c2015-08-27 13:46:58 -0700120 bool IsVisitedNode(HInstruction* instruction) const {
121 return map_.find(instruction) != map_.end();
Aart Bik30efb4e2015-07-30 12:14:31 -0700122 }
123
Aart Bik471a2032015-09-04 18:22:11 -0700124 InductionInfo* CreateInvariantOp(InductionOp op, InductionInfo* a, InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700125 DCHECK(((op != kNeg && a != nullptr) || (op == kNeg && a == nullptr)) && b != nullptr);
Aart Bik471a2032015-09-04 18:22:11 -0700126 return CreateSimplifiedInvariant(op, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700127 }
128
Aart Bik471a2032015-09-04 18:22:11 -0700129 InductionInfo* CreateInvariantFetch(HInstruction* f) {
Aart Bike609b7c2015-08-27 13:46:58 -0700130 DCHECK(f != nullptr);
Aart Bik0d345cf2016-03-16 10:49:38 -0700131 return new (graph_->GetArena())
132 InductionInfo(kInvariant, kFetch, nullptr, nullptr, f, f->GetType());
Aart Bike609b7c2015-08-27 13:46:58 -0700133 }
134
Aart Bik0d345cf2016-03-16 10:49:38 -0700135 InductionInfo* CreateTripCount(InductionOp op,
136 InductionInfo* a,
137 InductionInfo* b,
138 Primitive::Type type) {
Aart Bik52be7e72016-06-23 11:20:41 -0700139 DCHECK(a != nullptr && b != nullptr);
Aart Bik0d345cf2016-03-16 10:49:38 -0700140 return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr, type);
Aart Bik9401f532015-09-28 16:25:56 -0700141 }
142
Aart Bik0d345cf2016-03-16 10:49:38 -0700143 InductionInfo* CreateInduction(InductionClass ic,
Aart Bikc071a012016-12-01 10:22:31 -0800144 InductionOp op,
Aart Bik0d345cf2016-03-16 10:49:38 -0700145 InductionInfo* a,
146 InductionInfo* b,
Aart Bikc071a012016-12-01 10:22:31 -0800147 HInstruction* f,
Aart Bik0d345cf2016-03-16 10:49:38 -0700148 Primitive::Type type) {
Aart Bike609b7c2015-08-27 13:46:58 -0700149 DCHECK(a != nullptr && b != nullptr);
Aart Bikc071a012016-12-01 10:22:31 -0800150 return new (graph_->GetArena()) InductionInfo(ic, op, a, b, f, type);
Aart Bik30efb4e2015-07-30 12:14:31 -0700151 }
152
153 // Methods for analysis.
154 void VisitLoop(HLoopInformation* loop);
155 void VisitNode(HLoopInformation* loop, HInstruction* instruction);
156 uint32_t VisitDescendant(HLoopInformation* loop, HInstruction* instruction);
157 void ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction);
158 void ClassifyNonTrivial(HLoopInformation* loop);
Aart Bikf475bee2015-09-16 12:50:25 -0700159 InductionInfo* RotatePeriodicInduction(InductionInfo* induction, InductionInfo* last);
Aart Bik30efb4e2015-07-30 12:14:31 -0700160
161 // Transfer operations.
Aart Bikf475bee2015-09-16 12:50:25 -0700162 InductionInfo* TransferPhi(HLoopInformation* loop, HInstruction* phi, size_t input_index);
Aart Bik30efb4e2015-07-30 12:14:31 -0700163 InductionInfo* TransferAddSub(InductionInfo* a, InductionInfo* b, InductionOp op);
Aart Bik30efb4e2015-07-30 12:14:31 -0700164 InductionInfo* TransferNeg(InductionInfo* a);
Aart Bikc071a012016-12-01 10:22:31 -0800165 InductionInfo* TransferMul(InductionInfo* a, InductionInfo* b);
Aart Bik0d345cf2016-03-16 10:49:38 -0700166 InductionInfo* TransferCnv(InductionInfo* a, Primitive::Type from, Primitive::Type to);
Aart Bike609b7c2015-08-27 13:46:58 -0700167
168 // Solvers.
Aart Bikf475bee2015-09-16 12:50:25 -0700169 InductionInfo* SolvePhi(HInstruction* phi, size_t input_index);
170 InductionInfo* SolvePhiAllInputs(HLoopInformation* loop,
171 HInstruction* entry_phi,
172 HInstruction* phi);
Aart Bike609b7c2015-08-27 13:46:58 -0700173 InductionInfo* SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700174 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700175 HInstruction* instruction,
176 HInstruction* x,
177 HInstruction* y,
178 InductionOp op,
Aart Bik7dc96932016-10-12 10:01:05 -0700179 bool is_first_call); // possibly swaps x and y to try again
Aart Bikc071a012016-12-01 10:22:31 -0800180 InductionInfo* SolveGeo(HLoopInformation* loop,
181 HInstruction* entry_phi,
182 HInstruction* instruction,
183 HInstruction* x,
184 HInstruction* y,
185 InductionOp op);
Aart Bik7dc96932016-10-12 10:01:05 -0700186 InductionInfo* SolveXor(HLoopInformation* loop,
187 HInstruction* entry_phi,
188 HInstruction* instruction,
189 HInstruction* x,
Aart Bik639cc8c2016-10-18 13:03:31 -0700190 HInstruction* y);
191 InductionInfo* SolveTest(HLoopInformation* loop,
192 HInstruction* entry_phi,
193 HInstruction* instruction,
194 int64_t oppositive_value);
Aart Bik0d345cf2016-03-16 10:49:38 -0700195 InductionInfo* SolveCnv(HTypeConversion* conversion);
Aart Bik30efb4e2015-07-30 12:14:31 -0700196
Aart Bikd14c5952015-09-08 15:25:15 -0700197 // Trip count information.
198 void VisitControl(HLoopInformation* loop);
199 void VisitCondition(HLoopInformation* loop,
200 InductionInfo* a,
201 InductionInfo* b,
202 Primitive::Type type,
203 IfCondition cmp);
204 void VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700205 InductionInfo* lower_expr,
206 InductionInfo* upper_expr,
Aart Bikd14c5952015-09-08 15:25:15 -0700207 InductionInfo* stride,
Aart Bik9401f532015-09-28 16:25:56 -0700208 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700209 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700210 IfCondition cmp);
Aart Bik9401f532015-09-28 16:25:56 -0700211 bool IsTaken(InductionInfo* lower_expr, InductionInfo* upper_expr, IfCondition cmp);
212 bool IsFinite(InductionInfo* upper_expr,
213 int64_t stride_value,
214 Primitive::Type type,
215 IfCondition cmp);
Aart Bik0d345cf2016-03-16 10:49:38 -0700216 bool FitsNarrowerControl(InductionInfo* lower_expr,
217 InductionInfo* upper_expr,
218 int64_t stride_value,
219 Primitive::Type type,
220 IfCondition cmp);
Aart Bikd14c5952015-09-08 15:25:15 -0700221
Aart Bik30efb4e2015-07-30 12:14:31 -0700222 // Assign and lookup.
Aart Bik30efb4e2015-07-30 12:14:31 -0700223 void AssignInfo(HLoopInformation* loop, HInstruction* instruction, InductionInfo* info);
224 InductionInfo* LookupInfo(HLoopInformation* loop, HInstruction* instruction);
Aart Bikd14c5952015-09-08 15:25:15 -0700225 InductionInfo* CreateConstant(int64_t value, Primitive::Type type);
Aart Bik471a2032015-09-04 18:22:11 -0700226 InductionInfo* CreateSimplifiedInvariant(InductionOp op, InductionInfo* a, InductionInfo* b);
Aart Bikc071a012016-12-01 10:22:31 -0800227 HInstruction* GetMultConstantForShift(HLoopInformation* loop, HInstruction* instruction);
Aart Bikcc42be02016-10-20 16:14:16 -0700228 void AssignCycle(HPhi* phi);
229 ArenaSet<HInstruction*>* LookupCycle(HPhi* phi);
Aart Bik30efb4e2015-07-30 12:14:31 -0700230
Aart Bik7d57d7f2015-12-09 14:39:48 -0800231 // Constants.
Aart Bik97412c922016-02-19 20:14:38 -0800232 bool IsExact(InductionInfo* info, /*out*/ int64_t* value);
233 bool IsAtMost(InductionInfo* info, /*out*/ int64_t* value);
234 bool IsAtLeast(InductionInfo* info, /*out*/ int64_t* value);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800235
Aart Bike609b7c2015-08-27 13:46:58 -0700236 // Helpers.
237 static bool InductionEqual(InductionInfo* info1, InductionInfo* info2);
Aart Bikc071a012016-12-01 10:22:31 -0800238 static std::string FetchToString(HInstruction* fetch);
Aart Bike609b7c2015-08-27 13:46:58 -0700239 static std::string InductionToString(InductionInfo* info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700240
Aart Bike609b7c2015-08-27 13:46:58 -0700241 // TODO: fine tune the following data structures, only keep relevant data.
242
243 // Temporary book-keeping during the analysis.
Aart Bik30efb4e2015-07-30 12:14:31 -0700244 uint32_t global_depth_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700245 ArenaVector<HInstruction*> stack_;
Aart Bike609b7c2015-08-27 13:46:58 -0700246 ArenaSafeMap<HInstruction*, NodeInfo> map_;
Aart Bik7dc96932016-10-12 10:01:05 -0700247 ArenaVector<HInstruction*> scc_;
Aart Bike609b7c2015-08-27 13:46:58 -0700248 ArenaSafeMap<HInstruction*, InductionInfo*> cycle_;
Aart Bik0d345cf2016-03-16 10:49:38 -0700249 Primitive::Type type_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700250
Aart Bike609b7c2015-08-27 13:46:58 -0700251 /**
252 * Maintains the results of the analysis as a mapping from loops to a mapping from instructions
253 * to the induction information for that instruction in that loop.
254 */
255 ArenaSafeMap<HLoopInformation*, ArenaSafeMap<HInstruction*, InductionInfo*>> induction_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700256
Aart Bikcc42be02016-10-20 16:14:16 -0700257 /**
258 * Preserves induction cycle information for each loop-phi.
259 */
260 ArenaSafeMap<HPhi*, ArenaSet<HInstruction*>> cycles_;
261
Aart Bike609b7c2015-08-27 13:46:58 -0700262 friend class InductionVarAnalysisTest;
Aart Bikd14c5952015-09-08 15:25:15 -0700263 friend class InductionVarRange;
264 friend class InductionVarRangeTest;
Aart Bik30efb4e2015-07-30 12:14:31 -0700265
266 DISALLOW_COPY_AND_ASSIGN(HInductionVarAnalysis);
267};
268
269} // namespace art
270
271#endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_ANALYSIS_H_