blob: 84d5d82568bec8aa2bfe8cb5f3323db162bd392c [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
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 Bik30efb4e2015-07-30 12:14:31 -070052 kInvariant,
53 kLinear,
54 kWrapAround,
Aart Bike609b7c2015-08-27 13:46:58 -070055 kPeriodic
Aart Bik30efb4e2015-07-30 12:14:31 -070056 };
57
58 enum InductionOp {
Aart Bik9401f532015-09-28 16:25:56 -070059 // No-operation: a true induction.
60 kNop,
61 // Various invariant operations.
Aart Bik30efb4e2015-07-30 12:14:31 -070062 kAdd,
63 kSub,
64 kNeg,
65 kMul,
66 kDiv,
Aart Bik9401f532015-09-28 16:25:56 -070067 kFetch,
Aart Bik22f05872015-10-27 15:56:28 -070068 // Trip-counts.
69 kTripCountInLoop, // valid in full loop; loop is finite
70 kTripCountInBody, // valid in body only; loop is finite
71 kTripCountInLoopUnsafe, // valid in full loop; loop may be infinite
72 kTripCountInBodyUnsafe, // valid in body only; loop may be infinite
73 // Comparisons for trip-count tests.
74 kLT,
75 kLE,
76 kGT,
77 kGE
Aart Bik30efb4e2015-07-30 12:14:31 -070078 };
79
80 /**
81 * Defines a detected induction as:
82 * (1) invariant:
83 * operation: a + b, a - b, -b, a * b, a / b
Aart Bike609b7c2015-08-27 13:46:58 -070084 * or:
Aart Bik30efb4e2015-07-30 12:14:31 -070085 * fetch: fetch from HIR
86 * (2) linear:
87 * nop: a * i + b
88 * (3) wrap-around
89 * nop: a, then defined by b
90 * (4) periodic
91 * nop: a, then defined by b (repeated when exhausted)
Aart Bik9401f532015-09-28 16:25:56 -070092 * (5) trip-count:
Aart Bik22f05872015-10-27 15:56:28 -070093 * tc: defined by a, taken-test in b
Aart Bik30efb4e2015-07-30 12:14:31 -070094 */
Vladimir Marko5233f932015-09-29 19:01:15 +010095 struct InductionInfo : public ArenaObject<kArenaAllocInductionVarAnalysis> {
Aart Bik30efb4e2015-07-30 12:14:31 -070096 InductionInfo(InductionClass ic,
97 InductionOp op,
98 InductionInfo* a,
99 InductionInfo* b,
100 HInstruction* f)
101 : induction_class(ic),
102 operation(op),
103 op_a(a),
104 op_b(b),
105 fetch(f) {}
106 InductionClass induction_class;
107 InductionOp operation;
108 InductionInfo* op_a;
109 InductionInfo* op_b;
110 HInstruction* fetch;
111 };
112
Aart Bike609b7c2015-08-27 13:46:58 -0700113 bool IsVisitedNode(HInstruction* instruction) const {
114 return map_.find(instruction) != map_.end();
Aart Bik30efb4e2015-07-30 12:14:31 -0700115 }
116
Aart Bik471a2032015-09-04 18:22:11 -0700117 InductionInfo* CreateInvariantOp(InductionOp op, InductionInfo* a, InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700118 DCHECK(((op != kNeg && a != nullptr) || (op == kNeg && a == nullptr)) && b != nullptr);
Aart Bik471a2032015-09-04 18:22:11 -0700119 return CreateSimplifiedInvariant(op, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700120 }
121
Aart Bik471a2032015-09-04 18:22:11 -0700122 InductionInfo* CreateInvariantFetch(HInstruction* f) {
Aart Bike609b7c2015-08-27 13:46:58 -0700123 DCHECK(f != nullptr);
124 return new (graph_->GetArena()) InductionInfo(kInvariant, kFetch, nullptr, nullptr, f);
125 }
126
Aart Bik22f05872015-10-27 15:56:28 -0700127 InductionInfo* CreateTripCount(InductionOp op, InductionInfo* a, InductionInfo* b) {
128 DCHECK(a != nullptr);
129 return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr);
Aart Bik9401f532015-09-28 16:25:56 -0700130 }
131
Aart Bik471a2032015-09-04 18:22:11 -0700132 InductionInfo* CreateInduction(InductionClass ic, InductionInfo* a, InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700133 DCHECK(a != nullptr && b != nullptr);
134 return new (graph_->GetArena()) InductionInfo(ic, kNop, a, b, nullptr);
Aart Bik30efb4e2015-07-30 12:14:31 -0700135 }
136
137 // Methods for analysis.
138 void VisitLoop(HLoopInformation* loop);
139 void VisitNode(HLoopInformation* loop, HInstruction* instruction);
140 uint32_t VisitDescendant(HLoopInformation* loop, HInstruction* instruction);
141 void ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction);
142 void ClassifyNonTrivial(HLoopInformation* loop);
Aart Bikf475bee2015-09-16 12:50:25 -0700143 InductionInfo* RotatePeriodicInduction(InductionInfo* induction, InductionInfo* last);
Aart Bik30efb4e2015-07-30 12:14:31 -0700144
145 // Transfer operations.
Aart Bikf475bee2015-09-16 12:50:25 -0700146 InductionInfo* TransferPhi(HLoopInformation* loop, HInstruction* phi, size_t input_index);
Aart Bik30efb4e2015-07-30 12:14:31 -0700147 InductionInfo* TransferAddSub(InductionInfo* a, InductionInfo* b, InductionOp op);
148 InductionInfo* TransferMul(InductionInfo* a, InductionInfo* b);
Aart Bikd14c5952015-09-08 15:25:15 -0700149 InductionInfo* TransferShl(InductionInfo* a, InductionInfo* b, Primitive::Type type);
Aart Bik30efb4e2015-07-30 12:14:31 -0700150 InductionInfo* TransferNeg(InductionInfo* a);
Aart Bike609b7c2015-08-27 13:46:58 -0700151
152 // Solvers.
Aart Bikf475bee2015-09-16 12:50:25 -0700153 InductionInfo* SolvePhi(HInstruction* phi, size_t input_index);
154 InductionInfo* SolvePhiAllInputs(HLoopInformation* loop,
155 HInstruction* entry_phi,
156 HInstruction* phi);
Aart Bike609b7c2015-08-27 13:46:58 -0700157 InductionInfo* SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700158 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700159 HInstruction* instruction,
160 HInstruction* x,
161 HInstruction* y,
162 InductionOp op,
163 bool is_first_call);
Aart Bik30efb4e2015-07-30 12:14:31 -0700164
Aart Bikd14c5952015-09-08 15:25:15 -0700165 // Trip count information.
166 void VisitControl(HLoopInformation* loop);
167 void VisitCondition(HLoopInformation* loop,
168 InductionInfo* a,
169 InductionInfo* b,
170 Primitive::Type type,
171 IfCondition cmp);
172 void VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700173 InductionInfo* lower_expr,
174 InductionInfo* upper_expr,
Aart Bikd14c5952015-09-08 15:25:15 -0700175 InductionInfo* stride,
Aart Bik9401f532015-09-28 16:25:56 -0700176 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700177 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700178 IfCondition cmp);
Aart Bik9401f532015-09-28 16:25:56 -0700179 bool IsTaken(InductionInfo* lower_expr, InductionInfo* upper_expr, IfCondition cmp);
180 bool IsFinite(InductionInfo* upper_expr,
181 int64_t stride_value,
182 Primitive::Type type,
183 IfCondition cmp);
Aart Bikd14c5952015-09-08 15:25:15 -0700184
Aart Bik30efb4e2015-07-30 12:14:31 -0700185 // Assign and lookup.
Aart Bik30efb4e2015-07-30 12:14:31 -0700186 void AssignInfo(HLoopInformation* loop, HInstruction* instruction, InductionInfo* info);
187 InductionInfo* LookupInfo(HLoopInformation* loop, HInstruction* instruction);
Aart Bikd14c5952015-09-08 15:25:15 -0700188 InductionInfo* CreateConstant(int64_t value, Primitive::Type type);
Aart Bik471a2032015-09-04 18:22:11 -0700189 InductionInfo* CreateSimplifiedInvariant(InductionOp op, InductionInfo* a, InductionInfo* b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700190
Aart Bik7d57d7f2015-12-09 14:39:48 -0800191 // Constants.
192 bool IsIntAndGet(InductionInfo* info, int64_t* value);
193
Aart Bike609b7c2015-08-27 13:46:58 -0700194 // Helpers.
195 static bool InductionEqual(InductionInfo* info1, InductionInfo* info2);
196 static std::string InductionToString(InductionInfo* info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700197
Aart Bike609b7c2015-08-27 13:46:58 -0700198 // TODO: fine tune the following data structures, only keep relevant data.
199
200 // Temporary book-keeping during the analysis.
Aart Bik30efb4e2015-07-30 12:14:31 -0700201 uint32_t global_depth_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700202 ArenaVector<HInstruction*> stack_;
203 ArenaVector<HInstruction*> scc_;
Aart Bike609b7c2015-08-27 13:46:58 -0700204 ArenaSafeMap<HInstruction*, NodeInfo> map_;
205 ArenaSafeMap<HInstruction*, InductionInfo*> cycle_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700206
Aart Bike609b7c2015-08-27 13:46:58 -0700207 /**
208 * Maintains the results of the analysis as a mapping from loops to a mapping from instructions
209 * to the induction information for that instruction in that loop.
210 */
211 ArenaSafeMap<HLoopInformation*, ArenaSafeMap<HInstruction*, InductionInfo*>> induction_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700212
Aart Bike609b7c2015-08-27 13:46:58 -0700213 friend class InductionVarAnalysisTest;
Aart Bikd14c5952015-09-08 15:25:15 -0700214 friend class InductionVarRange;
215 friend class InductionVarRangeTest;
Aart Bik30efb4e2015-07-30 12:14:31 -0700216
217 DISALLOW_COPY_AND_ASSIGN(HInductionVarAnalysis);
218};
219
220} // namespace art
221
222#endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_ANALYSIS_H_