blob: 190a0db65c626fc0034ccf65e7363a1a3a2b0a85 [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 {
59 kNop, // no-operation: a true induction
60 kAdd,
61 kSub,
62 kNeg,
63 kMul,
64 kDiv,
65 kFetch
66 };
67
68 /**
69 * Defines a detected induction as:
70 * (1) invariant:
71 * operation: a + b, a - b, -b, a * b, a / b
Aart Bike609b7c2015-08-27 13:46:58 -070072 * or:
Aart Bik30efb4e2015-07-30 12:14:31 -070073 * fetch: fetch from HIR
74 * (2) linear:
75 * nop: a * i + b
76 * (3) wrap-around
77 * nop: a, then defined by b
78 * (4) periodic
79 * nop: a, then defined by b (repeated when exhausted)
Aart Bik30efb4e2015-07-30 12:14:31 -070080 */
81 struct InductionInfo : public ArenaObject<kArenaAllocMisc> {
82 InductionInfo(InductionClass ic,
83 InductionOp op,
84 InductionInfo* a,
85 InductionInfo* b,
86 HInstruction* f)
87 : induction_class(ic),
88 operation(op),
89 op_a(a),
90 op_b(b),
91 fetch(f) {}
92 InductionClass induction_class;
93 InductionOp operation;
94 InductionInfo* op_a;
95 InductionInfo* op_b;
96 HInstruction* fetch;
97 };
98
Aart Bike609b7c2015-08-27 13:46:58 -070099 bool IsVisitedNode(HInstruction* instruction) const {
100 return map_.find(instruction) != map_.end();
Aart Bik30efb4e2015-07-30 12:14:31 -0700101 }
102
Aart Bik471a2032015-09-04 18:22:11 -0700103 InductionInfo* CreateInvariantOp(InductionOp op, InductionInfo* a, InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700104 DCHECK(((op != kNeg && a != nullptr) || (op == kNeg && a == nullptr)) && b != nullptr);
Aart Bik471a2032015-09-04 18:22:11 -0700105 return CreateSimplifiedInvariant(op, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700106 }
107
Aart Bik471a2032015-09-04 18:22:11 -0700108 InductionInfo* CreateInvariantFetch(HInstruction* f) {
Aart Bike609b7c2015-08-27 13:46:58 -0700109 DCHECK(f != nullptr);
110 return new (graph_->GetArena()) InductionInfo(kInvariant, kFetch, nullptr, nullptr, f);
111 }
112
Aart Bik471a2032015-09-04 18:22:11 -0700113 InductionInfo* CreateInduction(InductionClass ic, InductionInfo* a, InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700114 DCHECK(a != nullptr && b != nullptr);
115 return new (graph_->GetArena()) InductionInfo(ic, kNop, a, b, nullptr);
Aart Bik30efb4e2015-07-30 12:14:31 -0700116 }
117
118 // Methods for analysis.
119 void VisitLoop(HLoopInformation* loop);
120 void VisitNode(HLoopInformation* loop, HInstruction* instruction);
121 uint32_t VisitDescendant(HLoopInformation* loop, HInstruction* instruction);
122 void ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction);
123 void ClassifyNonTrivial(HLoopInformation* loop);
Aart Bikf475bee2015-09-16 12:50:25 -0700124 InductionInfo* RotatePeriodicInduction(InductionInfo* induction, InductionInfo* last);
Aart Bik30efb4e2015-07-30 12:14:31 -0700125
126 // Transfer operations.
Aart Bikf475bee2015-09-16 12:50:25 -0700127 InductionInfo* TransferPhi(HLoopInformation* loop, HInstruction* phi, size_t input_index);
Aart Bik30efb4e2015-07-30 12:14:31 -0700128 InductionInfo* TransferAddSub(InductionInfo* a, InductionInfo* b, InductionOp op);
129 InductionInfo* TransferMul(InductionInfo* a, InductionInfo* b);
Aart Bikd14c5952015-09-08 15:25:15 -0700130 InductionInfo* TransferShl(InductionInfo* a, InductionInfo* b, Primitive::Type type);
Aart Bik30efb4e2015-07-30 12:14:31 -0700131 InductionInfo* TransferNeg(InductionInfo* a);
Aart Bike609b7c2015-08-27 13:46:58 -0700132
133 // Solvers.
Aart Bikf475bee2015-09-16 12:50:25 -0700134 InductionInfo* SolvePhi(HInstruction* phi, size_t input_index);
135 InductionInfo* SolvePhiAllInputs(HLoopInformation* loop,
136 HInstruction* entry_phi,
137 HInstruction* phi);
Aart Bike609b7c2015-08-27 13:46:58 -0700138 InductionInfo* SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700139 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700140 HInstruction* instruction,
141 HInstruction* x,
142 HInstruction* y,
143 InductionOp op,
144 bool is_first_call);
Aart Bik30efb4e2015-07-30 12:14:31 -0700145
Aart Bikd14c5952015-09-08 15:25:15 -0700146 // Trip count information.
147 void VisitControl(HLoopInformation* loop);
148 void VisitCondition(HLoopInformation* loop,
149 InductionInfo* a,
150 InductionInfo* b,
151 Primitive::Type type,
152 IfCondition cmp);
153 void VisitTripCount(HLoopInformation* loop,
154 InductionInfo* lo_val,
155 InductionInfo* hi_val,
156 InductionInfo* stride,
157 int32_t stride_value,
158 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700159 IfCondition cmp);
Aart Bikd14c5952015-09-08 15:25:15 -0700160
Aart Bik30efb4e2015-07-30 12:14:31 -0700161 // Assign and lookup.
Aart Bik30efb4e2015-07-30 12:14:31 -0700162 void AssignInfo(HLoopInformation* loop, HInstruction* instruction, InductionInfo* info);
163 InductionInfo* LookupInfo(HLoopInformation* loop, HInstruction* instruction);
Aart Bikd14c5952015-09-08 15:25:15 -0700164 InductionInfo* CreateConstant(int64_t value, Primitive::Type type);
Aart Bik471a2032015-09-04 18:22:11 -0700165 InductionInfo* CreateSimplifiedInvariant(InductionOp op, InductionInfo* a, InductionInfo* b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700166
Aart Bike609b7c2015-08-27 13:46:58 -0700167 // Helpers.
168 static bool InductionEqual(InductionInfo* info1, InductionInfo* info2);
Aart Bik471a2032015-09-04 18:22:11 -0700169 static bool IsIntAndGet(InductionInfo* info, int64_t* value);
Aart Bike609b7c2015-08-27 13:46:58 -0700170 static std::string InductionToString(InductionInfo* info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700171
Aart Bike609b7c2015-08-27 13:46:58 -0700172 // TODO: fine tune the following data structures, only keep relevant data.
173
174 // Temporary book-keeping during the analysis.
Aart Bik30efb4e2015-07-30 12:14:31 -0700175 uint32_t global_depth_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700176 ArenaVector<HInstruction*> stack_;
177 ArenaVector<HInstruction*> scc_;
Aart Bike609b7c2015-08-27 13:46:58 -0700178 ArenaSafeMap<HInstruction*, NodeInfo> map_;
179 ArenaSafeMap<HInstruction*, InductionInfo*> cycle_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700180
Aart Bike609b7c2015-08-27 13:46:58 -0700181 /**
182 * Maintains the results of the analysis as a mapping from loops to a mapping from instructions
183 * to the induction information for that instruction in that loop.
184 */
185 ArenaSafeMap<HLoopInformation*, ArenaSafeMap<HInstruction*, InductionInfo*>> induction_;
Aart Bik30efb4e2015-07-30 12:14:31 -0700186
Aart Bike609b7c2015-08-27 13:46:58 -0700187 friend class InductionVarAnalysisTest;
Aart Bikd14c5952015-09-08 15:25:15 -0700188 friend class InductionVarRange;
189 friend class InductionVarRangeTest;
Aart Bik30efb4e2015-07-30 12:14:31 -0700190
191 DISALLOW_COPY_AND_ASSIGN(HInductionVarAnalysis);
192};
193
194} // namespace art
195
196#endif // ART_COMPILER_OPTIMIZING_INDUCTION_VAR_ANALYSIS_H_