blob: 19e6cbd314806b93d25058474efcb98721eca3eb [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#include "induction_var_analysis.h"
Aart Bik22af3be2015-09-10 12:50:58 -070018#include "induction_var_range.h"
Aart Bik30efb4e2015-07-30 12:14:31 -070019
20namespace art {
21
22/**
Aart Bik22af3be2015-09-10 12:50:58 -070023 * Since graph traversal may enter a SCC at any position, an initial representation may be rotated,
24 * along dependences, viz. any of (a, b, c, d), (d, a, b, c) (c, d, a, b), (b, c, d, a) assuming
25 * a chain of dependences (mutual independent items may occur in arbitrary order). For proper
26 * classification, the lexicographically first entry-phi is rotated to the front.
27 */
28static void RotateEntryPhiFirst(HLoopInformation* loop,
29 ArenaVector<HInstruction*>* scc,
30 ArenaVector<HInstruction*>* new_scc) {
31 // Find very first entry-phi.
32 const HInstructionList& phis = loop->GetHeader()->GetPhis();
33 HInstruction* phi = nullptr;
34 size_t phi_pos = -1;
35 const size_t size = scc->size();
36 for (size_t i = 0; i < size; i++) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010037 HInstruction* other = (*scc)[i];
Aart Bikf475bee2015-09-16 12:50:25 -070038 if (other->IsLoopHeaderPhi() && (phi == nullptr || phis.FoundBefore(other, phi))) {
39 phi = other;
Aart Bik22af3be2015-09-10 12:50:58 -070040 phi_pos = i;
41 }
42 }
43
44 // If found, bring that entry-phi to front.
45 if (phi != nullptr) {
46 new_scc->clear();
47 for (size_t i = 0; i < size; i++) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010048 new_scc->push_back((*scc)[phi_pos]);
Aart Bik22af3be2015-09-10 12:50:58 -070049 if (++phi_pos >= size) phi_pos = 0;
50 }
51 DCHECK_EQ(size, new_scc->size());
52 scc->swap(*new_scc);
53 }
54}
55
Aart Bik30efb4e2015-07-30 12:14:31 -070056//
57// Class methods.
58//
59
60HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph)
61 : HOptimization(graph, kInductionPassName),
62 global_depth_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +010063 stack_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
64 scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
65 map_(std::less<HInstruction*>(),
66 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
67 cycle_(std::less<HInstruction*>(),
68 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
69 induction_(std::less<HLoopInformation*>(),
70 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) {
Aart Bik30efb4e2015-07-30 12:14:31 -070071}
72
73void HInductionVarAnalysis::Run() {
Aart Bik7d57d7f2015-12-09 14:39:48 -080074 // Detects sequence variables (generalized induction variables) during an outer to inner
75 // traversal of all loops using Gerlek's algorithm. The order is important to enable
76 // range analysis on outer loop while visiting inner loops.
77 for (HReversePostOrderIterator it_graph(*graph_); !it_graph.Done(); it_graph.Advance()) {
Aart Bik30efb4e2015-07-30 12:14:31 -070078 HBasicBlock* graph_block = it_graph.Current();
79 if (graph_block->IsLoopHeader()) {
80 VisitLoop(graph_block->GetLoopInformation());
81 }
82 }
83}
84
85void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) {
86 // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's
87 // algorithm. Due to the descendant-first nature, classification happens "on-demand".
88 global_depth_ = 0;
Aart Bike609b7c2015-08-27 13:46:58 -070089 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -070090 map_.clear();
91
92 for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) {
93 HBasicBlock* loop_block = it_loop.Current();
Aart Bike609b7c2015-08-27 13:46:58 -070094 DCHECK(loop_block->IsInLoop());
Aart Bik30efb4e2015-07-30 12:14:31 -070095 if (loop_block->GetLoopInformation() != loop) {
96 continue; // Inner loops already visited.
97 }
98 // Visit phi-operations and instructions.
99 for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) {
100 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700101 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700102 VisitNode(loop, instruction);
103 }
104 }
105 for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) {
106 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700107 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700108 VisitNode(loop, instruction);
109 }
110 }
111 }
112
Aart Bike609b7c2015-08-27 13:46:58 -0700113 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700114 map_.clear();
Aart Bikd14c5952015-09-08 15:25:15 -0700115
116 // Determine the loop's trip count.
117 VisitControl(loop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700118}
119
120void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700121 const uint32_t d1 = ++global_depth_;
Aart Bike609b7c2015-08-27 13:46:58 -0700122 map_.Put(instruction, NodeInfo(d1));
Aart Bik30efb4e2015-07-30 12:14:31 -0700123 stack_.push_back(instruction);
124
125 // Visit all descendants.
126 uint32_t low = d1;
127 for (size_t i = 0, count = instruction->InputCount(); i < count; ++i) {
128 low = std::min(low, VisitDescendant(loop, instruction->InputAt(i)));
129 }
130
131 // Lower or found SCC?
132 if (low < d1) {
Aart Bike609b7c2015-08-27 13:46:58 -0700133 map_.find(instruction)->second.depth = low;
Aart Bik30efb4e2015-07-30 12:14:31 -0700134 } else {
135 scc_.clear();
136 cycle_.clear();
137
138 // Pop the stack to build the SCC for classification.
139 while (!stack_.empty()) {
140 HInstruction* x = stack_.back();
141 scc_.push_back(x);
142 stack_.pop_back();
Aart Bike609b7c2015-08-27 13:46:58 -0700143 map_.find(x)->second.done = true;
Aart Bik30efb4e2015-07-30 12:14:31 -0700144 if (x == instruction) {
145 break;
146 }
147 }
148
149 // Classify the SCC.
Aart Bikf475bee2015-09-16 12:50:25 -0700150 if (scc_.size() == 1 && !scc_[0]->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700151 ClassifyTrivial(loop, scc_[0]);
152 } else {
153 ClassifyNonTrivial(loop);
154 }
155
156 scc_.clear();
157 cycle_.clear();
158 }
159}
160
161uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) {
162 // If the definition is either outside the loop (loop invariant entry value)
163 // or assigned in inner loop (inner exit value), the traversal stops.
164 HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation();
165 if (otherLoop != loop) {
166 return global_depth_;
167 }
168
169 // Inspect descendant node.
Aart Bike609b7c2015-08-27 13:46:58 -0700170 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700171 VisitNode(loop, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700172 return map_.find(instruction)->second.depth;
Aart Bik30efb4e2015-07-30 12:14:31 -0700173 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700174 auto it = map_.find(instruction);
Aart Bik30efb4e2015-07-30 12:14:31 -0700175 return it->second.done ? global_depth_ : it->second.depth;
176 }
177}
178
179void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) {
180 InductionInfo* info = nullptr;
181 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700182 info = TransferPhi(loop, instruction, /* input_index */ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700183 } else if (instruction->IsAdd()) {
184 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
185 LookupInfo(loop, instruction->InputAt(1)), kAdd);
186 } else if (instruction->IsSub()) {
187 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
188 LookupInfo(loop, instruction->InputAt(1)), kSub);
189 } else if (instruction->IsMul()) {
190 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
191 LookupInfo(loop, instruction->InputAt(1)));
Aart Bike609b7c2015-08-27 13:46:58 -0700192 } else if (instruction->IsShl()) {
193 info = TransferShl(LookupInfo(loop, instruction->InputAt(0)),
194 LookupInfo(loop, instruction->InputAt(1)),
195 instruction->InputAt(0)->GetType());
Aart Bik30efb4e2015-07-30 12:14:31 -0700196 } else if (instruction->IsNeg()) {
197 info = TransferNeg(LookupInfo(loop, instruction->InputAt(0)));
Aart Bike609b7c2015-08-27 13:46:58 -0700198 } else if (instruction->IsBoundsCheck()) {
199 info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through.
200 } else if (instruction->IsTypeConversion()) {
201 HTypeConversion* conversion = instruction->AsTypeConversion();
202 // TODO: accept different conversion scenarios.
203 if (conversion->GetResultType() == conversion->GetInputType()) {
204 info = LookupInfo(loop, conversion->GetInput());
205 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700206 }
207
208 // Successfully classified?
209 if (info != nullptr) {
210 AssignInfo(loop, instruction, info);
211 }
212}
213
214void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) {
215 const size_t size = scc_.size();
Aart Bike609b7c2015-08-27 13:46:58 -0700216 DCHECK_GE(size, 1u);
Aart Bik22af3be2015-09-10 12:50:58 -0700217
218 // Rotate proper entry-phi to front.
219 if (size > 1) {
Vladimir Marko5233f932015-09-29 19:01:15 +0100220 ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700221 RotateEntryPhiFirst(loop, &scc_, &other);
222 }
223
Aart Bikf475bee2015-09-16 12:50:25 -0700224 // Analyze from entry-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700225 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700226 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700227 return;
228 }
Aart Bikf475bee2015-09-16 12:50:25 -0700229
230 // External link should be loop invariant.
231 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700232 if (initial == nullptr || initial->induction_class != kInvariant) {
233 return;
234 }
235
Aart Bikf475bee2015-09-16 12:50:25 -0700236 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700237 if (size == 1) {
Aart Bikf475bee2015-09-16 12:50:25 -0700238 InductionInfo* update = TransferPhi(loop, phi, /* input_index */ 1);
Aart Bik30efb4e2015-07-30 12:14:31 -0700239 if (update != nullptr) {
Aart Bik471a2032015-09-04 18:22:11 -0700240 AssignInfo(loop, phi, CreateInduction(kWrapAround, initial, update));
Aart Bik30efb4e2015-07-30 12:14:31 -0700241 }
242 return;
243 }
244
245 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700246 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700247 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700248 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700249 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700250 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700251 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700252 } else if (instruction->IsAdd()) {
253 update = SolveAddSub(
254 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
255 } else if (instruction->IsSub()) {
256 update = SolveAddSub(
257 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bik30efb4e2015-07-30 12:14:31 -0700258 }
259 if (update == nullptr) {
260 return;
261 }
Aart Bike609b7c2015-08-27 13:46:58 -0700262 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700263 }
264
Aart Bikf475bee2015-09-16 12:50:25 -0700265 // Success if all internal links received the same temporary meaning.
266 InductionInfo* induction = SolvePhi(phi, /* input_index */ 1);
267 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700268 switch (induction->induction_class) {
269 case kInvariant:
Aart Bik22af3be2015-09-10 12:50:58 -0700270 // Classify first phi and then the rest of the cycle "on-demand".
271 // Statements are scanned in order.
Aart Bik471a2032015-09-04 18:22:11 -0700272 AssignInfo(loop, phi, CreateInduction(kLinear, induction, initial));
Aart Bik22af3be2015-09-10 12:50:58 -0700273 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700274 ClassifyTrivial(loop, scc_[i]);
275 }
276 break;
277 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700278 // Classify all elements in the cycle with the found periodic induction while
279 // rotating each first element to the end. Lastly, phi is classified.
280 // Statements are scanned in reverse order.
281 for (size_t i = size - 1; i >= 1; i--) {
282 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700283 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
284 }
285 AssignInfo(loop, phi, induction);
286 break;
287 default:
288 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700289 }
290 }
291}
292
Aart Bike609b7c2015-08-27 13:46:58 -0700293HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
294 InductionInfo* induction,
295 InductionInfo* last) {
296 // Rotates a periodic induction of the form
297 // (a, b, c, d, e)
298 // into
299 // (b, c, d, e, a)
300 // in preparation of assigning this to the previous variable in the sequence.
301 if (induction->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700302 return CreateInduction(kPeriodic, induction, last);
Aart Bike609b7c2015-08-27 13:46:58 -0700303 }
Aart Bik471a2032015-09-04 18:22:11 -0700304 return CreateInduction(kPeriodic, induction->op_a, RotatePeriodicInduction(induction->op_b, last));
Aart Bike609b7c2015-08-27 13:46:58 -0700305}
306
Aart Bikf475bee2015-09-16 12:50:25 -0700307HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
308 HInstruction* phi,
309 size_t input_index) {
310 // Match all phi inputs from input_index onwards exactly.
311 const size_t count = phi->InputCount();
312 DCHECK_LT(input_index, count);
313 InductionInfo* a = LookupInfo(loop, phi->InputAt(input_index));
314 for (size_t i = input_index + 1; i < count; i++) {
315 InductionInfo* b = LookupInfo(loop, phi->InputAt(i));
316 if (!InductionEqual(a, b)) {
317 return nullptr;
318 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700319 }
Aart Bikf475bee2015-09-16 12:50:25 -0700320 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700321}
322
323HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
324 InductionInfo* b,
325 InductionOp op) {
Aart Bike609b7c2015-08-27 13:46:58 -0700326 // Transfer over an addition or subtraction: any invariant, linear, wrap-around, or periodic
327 // can be combined with an invariant to yield a similar result. Even two linear inputs can
328 // be combined. All other combinations fail, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700329 if (a != nullptr && b != nullptr) {
330 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700331 return CreateInvariantOp(op, a, b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700332 } else if (a->induction_class == kLinear && b->induction_class == kLinear) {
Aart Bik471a2032015-09-04 18:22:11 -0700333 return CreateInduction(
Aart Bike609b7c2015-08-27 13:46:58 -0700334 kLinear, TransferAddSub(a->op_a, b->op_a, op), TransferAddSub(a->op_b, b->op_b, op));
335 } else if (a->induction_class == kInvariant) {
336 InductionInfo* new_a = b->op_a;
337 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
338 if (b->induction_class != kLinear) {
339 DCHECK(b->induction_class == kWrapAround || b->induction_class == kPeriodic);
340 new_a = TransferAddSub(a, new_a, op);
341 } else if (op == kSub) { // Negation required.
342 new_a = TransferNeg(new_a);
343 }
Aart Bik471a2032015-09-04 18:22:11 -0700344 return CreateInduction(b->induction_class, new_a, new_b);
Aart Bike609b7c2015-08-27 13:46:58 -0700345 } else if (b->induction_class == kInvariant) {
346 InductionInfo* new_a = a->op_a;
347 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
348 if (a->induction_class != kLinear) {
349 DCHECK(a->induction_class == kWrapAround || a->induction_class == kPeriodic);
350 new_a = TransferAddSub(new_a, b, op);
351 }
Aart Bik471a2032015-09-04 18:22:11 -0700352 return CreateInduction(a->induction_class, new_a, new_b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700353 }
354 }
355 return nullptr;
356}
357
358HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
359 InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700360 // Transfer over a multiplication: any invariant, linear, wrap-around, or periodic
361 // can be multiplied with an invariant to yield a similar but multiplied result.
362 // Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700363 if (a != nullptr && b != nullptr) {
364 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700365 return CreateInvariantOp(kMul, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700366 } else if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700367 return CreateInduction(b->induction_class, TransferMul(a, b->op_a), TransferMul(a, b->op_b));
Aart Bike609b7c2015-08-27 13:46:58 -0700368 } else if (b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700369 return CreateInduction(a->induction_class, TransferMul(a->op_a, b), TransferMul(a->op_b, b));
Aart Bike609b7c2015-08-27 13:46:58 -0700370 }
371 }
372 return nullptr;
373}
374
375HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferShl(InductionInfo* a,
376 InductionInfo* b,
Aart Bikd14c5952015-09-08 15:25:15 -0700377 Primitive::Type type) {
Aart Bike609b7c2015-08-27 13:46:58 -0700378 // Transfer over a shift left: treat shift by restricted constant as equivalent multiplication.
Aart Bik471a2032015-09-04 18:22:11 -0700379 int64_t value = -1;
380 if (a != nullptr && IsIntAndGet(b, &value)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700381 // Obtain the constant needed for the multiplication. This yields an existing instruction
382 // if the constants is already there. Otherwise, this has a side effect on the HIR.
383 // The restriction on the shift factor avoids generating a negative constant
384 // (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that generalization
385 // for shift factors outside [0,32) and [0,64) ranges is done by earlier simplification.
Aart Bikd14c5952015-09-08 15:25:15 -0700386 if ((type == Primitive::kPrimInt && 0 <= value && value < 31) ||
387 (type == Primitive::kPrimLong && 0 <= value && value < 63)) {
388 return TransferMul(a, CreateConstant(1 << value, type));
Aart Bik30efb4e2015-07-30 12:14:31 -0700389 }
390 }
391 return nullptr;
392}
393
394HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
Aart Bike609b7c2015-08-27 13:46:58 -0700395 // Transfer over a unary negation: an invariant, linear, wrap-around, or periodic input
396 // yields a similar but negated induction as result.
Aart Bik30efb4e2015-07-30 12:14:31 -0700397 if (a != nullptr) {
398 if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700399 return CreateInvariantOp(kNeg, nullptr, a);
Aart Bik30efb4e2015-07-30 12:14:31 -0700400 }
Aart Bik471a2032015-09-04 18:22:11 -0700401 return CreateInduction(a->induction_class, TransferNeg(a->op_a), TransferNeg(a->op_b));
Aart Bik30efb4e2015-07-30 12:14:31 -0700402 }
403 return nullptr;
404}
405
Aart Bikf475bee2015-09-16 12:50:25 -0700406HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
407 size_t input_index) {
408 // Match all phi inputs from input_index onwards exactly.
409 const size_t count = phi->InputCount();
410 DCHECK_LT(input_index, count);
411 auto ita = cycle_.find(phi->InputAt(input_index));
Aart Bik30efb4e2015-07-30 12:14:31 -0700412 if (ita != cycle_.end()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700413 for (size_t i = input_index + 1; i < count; i++) {
414 auto itb = cycle_.find(phi->InputAt(i));
415 if (itb == cycle_.end() ||
416 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700417 return nullptr;
418 }
419 }
Aart Bikf475bee2015-09-16 12:50:25 -0700420 return ita->second;
421 }
422 return nullptr;
423}
424
425HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
426 HLoopInformation* loop,
427 HInstruction* entry_phi,
428 HInstruction* phi) {
429 // Match all phi inputs.
430 InductionInfo* match = SolvePhi(phi, /* input_index */ 0);
431 if (match != nullptr) {
432 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700433 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700434
Aart Bikf475bee2015-09-16 12:50:25 -0700435 // Otherwise, try to solve for a periodic seeded from phi onward.
436 // Only tight multi-statement cycles are considered in order to
437 // simplify rotating the periodic during the final classification.
438 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
439 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700440 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700441 if (phi->InputAt(1) == entry_phi) {
442 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik471a2032015-09-04 18:22:11 -0700443 return CreateInduction(kPeriodic, a, initial);
Aart Bike609b7c2015-08-27 13:46:58 -0700444 }
Aart Bikf475bee2015-09-16 12:50:25 -0700445 InductionInfo* b = SolvePhi(phi, /* input_index */ 1);
446 if (b != nullptr && b->induction_class == kPeriodic) {
447 return CreateInduction(kPeriodic, a, b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700448 }
449 }
450 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700451 return nullptr;
452}
453
Aart Bike609b7c2015-08-27 13:46:58 -0700454HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700455 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700456 HInstruction* instruction,
457 HInstruction* x,
458 HInstruction* y,
459 InductionOp op,
460 bool is_first_call) {
461 // Solve within a cycle over an addition or subtraction: adding or subtracting an
462 // invariant value, seeded from phi, keeps adding to the stride of the induction.
463 InductionInfo* b = LookupInfo(loop, y);
464 if (b != nullptr && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700465 if (x == entry_phi) {
Aart Bik471a2032015-09-04 18:22:11 -0700466 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700467 }
468 auto it = cycle_.find(x);
469 if (it != cycle_.end()) {
470 InductionInfo* a = it->second;
471 if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700472 return CreateInvariantOp(op, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700473 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700474 }
475 }
Aart Bike609b7c2015-08-27 13:46:58 -0700476
477 // Try some alternatives before failing.
478 if (op == kAdd) {
479 // Try the other way around for an addition if considered for first time.
480 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700481 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700482 }
483 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700484 // Solve within a tight cycle that is formed by exactly two instructions,
485 // one phi and one update, for a periodic idiom of the form k = c - k;
486 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700487 InductionInfo* a = LookupInfo(loop, x);
488 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700489 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik471a2032015-09-04 18:22:11 -0700490 return CreateInduction(kPeriodic, CreateInvariantOp(kSub, a, initial), initial);
Aart Bike609b7c2015-08-27 13:46:58 -0700491 }
492 }
493 }
494
Aart Bik30efb4e2015-07-30 12:14:31 -0700495 return nullptr;
496}
497
Aart Bikd14c5952015-09-08 15:25:15 -0700498void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
499 HInstruction* control = loop->GetHeader()->GetLastInstruction();
500 if (control->IsIf()) {
501 HIf* ifs = control->AsIf();
502 HBasicBlock* if_true = ifs->IfTrueSuccessor();
503 HBasicBlock* if_false = ifs->IfFalseSuccessor();
504 HInstruction* if_expr = ifs->InputAt(0);
505 // Determine if loop has following structure in header.
506 // loop-header: ....
507 // if (condition) goto X
508 if (if_expr->IsCondition()) {
509 HCondition* condition = if_expr->AsCondition();
510 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
511 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
512 Primitive::Type type = condition->InputAt(0)->GetType();
513 // Determine if the loop control uses integral arithmetic and an if-exit (X outside) or an
514 // if-iterate (X inside), always expressed as if-iterate when passing into VisitCondition().
515 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
516 // Loop control is not 32/64-bit integral.
517 } else if (a == nullptr || b == nullptr) {
518 // Loop control is not a sequence.
519 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
520 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
521 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
522 VisitCondition(loop, a, b, type, condition->GetCondition());
523 }
524 }
525 }
526}
527
528void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
529 InductionInfo* a,
530 InductionInfo* b,
531 Primitive::Type type,
532 IfCondition cmp) {
533 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700534 // Swap condition if induction is at right-hand-side (e.g. U > i is same as i < U).
Aart Bikd14c5952015-09-08 15:25:15 -0700535 switch (cmp) {
536 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
537 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
538 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
539 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700540 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700541 default: break;
542 }
543 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700544 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700545 InductionInfo* lower_expr = a->op_b;
546 InductionInfo* upper_expr = b;
Aart Bikd14c5952015-09-08 15:25:15 -0700547 InductionInfo* stride = a->op_a;
Aart Bik9401f532015-09-28 16:25:56 -0700548 int64_t stride_value = 0;
549 if (!IsIntAndGet(stride, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700550 return;
551 }
Aart Bik9401f532015-09-28 16:25:56 -0700552 // Rewrite condition i != U into i < U or i > U if end condition is reached exactly.
553 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLT)) ||
554 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGT)))) {
555 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700556 }
Aart Bikf475bee2015-09-16 12:50:25 -0700557 // Normalize a linear loop control with a nonzero stride:
558 // stride > 0, either i < U or i <= U
559 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700560 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
561 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik9401f532015-09-28 16:25:56 -0700562 VisitTripCount(loop, lower_expr, upper_expr, stride, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700563 }
Aart Bikd14c5952015-09-08 15:25:15 -0700564 }
565}
566
567void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700568 InductionInfo* lower_expr,
569 InductionInfo* upper_expr,
Aart Bikd14c5952015-09-08 15:25:15 -0700570 InductionInfo* stride,
Aart Bik9401f532015-09-28 16:25:56 -0700571 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700572 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700573 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700574 // Any loop of the general form:
575 //
576 // for (i = L; i <= U; i += S) // S > 0
577 // or for (i = L; i >= U; i += S) // S < 0
578 // .. i ..
579 //
580 // can be normalized into:
581 //
582 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
583 // .. L + S * n ..
584 //
Aart Bik9401f532015-09-28 16:25:56 -0700585 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700586 //
Aart Bik9401f532015-09-28 16:25:56 -0700587 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
588 // an unsigned entity, for example, as in the following loop that uses the full range:
589 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
590 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
Aart Bik22f05872015-10-27 15:56:28 -0700591 // for (int i = 12; i < U; i++) // TC = 0 when U < 12
Aart Bik9401f532015-09-28 16:25:56 -0700592 // If this cannot be determined at compile-time, the TC is only valid within the
Aart Bik22f05872015-10-27 15:56:28 -0700593 // loop-body proper, not the loop-header unless enforced with an explicit taken-test.
Aart Bik9401f532015-09-28 16:25:56 -0700594 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
595 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
596 // If this cannot be determined at compile-time, the TC is only valid when enforced
Aart Bik22f05872015-10-27 15:56:28 -0700597 // with an explicit finite-test.
Aart Bik9401f532015-09-28 16:25:56 -0700598 // (4) For loops which early-exits, the TC forms an upper bound, as in:
599 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
Aart Bik22f05872015-10-27 15:56:28 -0700600 InductionInfo* trip_count = upper_expr;
Aart Bik9401f532015-09-28 16:25:56 -0700601 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
602 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
603 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700604 if (!cancels) {
605 // Convert exclusive integral inequality into inclusive integral inequality,
606 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700607 if (cmp == kCondLT) {
Aart Bik22f05872015-10-27 15:56:28 -0700608 trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700609 } else if (cmp == kCondGT) {
Aart Bik22f05872015-10-27 15:56:28 -0700610 trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700611 }
612 // Compensate for stride.
Aart Bik22f05872015-10-27 15:56:28 -0700613 trip_count = CreateInvariantOp(kAdd, trip_count, stride);
Aart Bikd14c5952015-09-08 15:25:15 -0700614 }
Aart Bik22f05872015-10-27 15:56:28 -0700615 trip_count = CreateInvariantOp(kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride);
Aart Bikd14c5952015-09-08 15:25:15 -0700616 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700617 // should be aware that the expression is only valid under the conditions listed above.
Aart Bik22f05872015-10-27 15:56:28 -0700618 InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests
Aart Bik9401f532015-09-28 16:25:56 -0700619 if (is_taken && is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700620 tcKind = kTripCountInLoop; // needs neither test
Aart Bik9401f532015-09-28 16:25:56 -0700621 } else if (is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700622 tcKind = kTripCountInBody; // needs taken-test
Aart Bik9401f532015-09-28 16:25:56 -0700623 } else if (is_taken) {
Aart Bik22f05872015-10-27 15:56:28 -0700624 tcKind = kTripCountInLoopUnsafe; // needs finite-test
Aart Bik9401f532015-09-28 16:25:56 -0700625 }
Aart Bik22f05872015-10-27 15:56:28 -0700626 InductionOp op = kNop;
627 switch (cmp) {
628 case kCondLT: op = kLT; break;
629 case kCondLE: op = kLE; break;
630 case kCondGT: op = kGT; break;
631 case kCondGE: op = kGE; break;
632 default: LOG(FATAL) << "CONDITION UNREACHABLE";
633 }
634 InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr);
635 AssignInfo(loop,
636 loop->GetHeader()->GetLastInstruction(),
637 CreateTripCount(tcKind, trip_count, taken_test));
Aart Bik9401f532015-09-28 16:25:56 -0700638}
639
640bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
641 InductionInfo* upper_expr,
642 IfCondition cmp) {
643 int64_t lower_value;
644 int64_t upper_value;
645 if (IsIntAndGet(lower_expr, &lower_value) && IsIntAndGet(upper_expr, &upper_value)) {
646 switch (cmp) {
647 case kCondLT: return lower_value < upper_value;
648 case kCondLE: return lower_value <= upper_value;
649 case kCondGT: return lower_value > upper_value;
650 case kCondGE: return lower_value >= upper_value;
Aart Bike9f37602015-10-09 11:15:55 -0700651 default: LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700652 }
653 }
654 return false; // not certain, may be untaken
655}
656
657bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
658 int64_t stride_value,
659 Primitive::Type type,
660 IfCondition cmp) {
661 const int64_t min = type == Primitive::kPrimInt
662 ? std::numeric_limits<int32_t>::min()
663 : std::numeric_limits<int64_t>::min();
664 const int64_t max = type == Primitive::kPrimInt
665 ? std::numeric_limits<int32_t>::max()
666 : std::numeric_limits<int64_t>::max();
667 // Some rules under which it is certain at compile-time that the loop is finite.
668 int64_t value;
669 switch (cmp) {
670 case kCondLT:
671 return stride_value == 1 ||
672 (IsIntAndGet(upper_expr, &value) && value <= (max - stride_value + 1));
673 case kCondLE:
674 return (IsIntAndGet(upper_expr, &value) && value <= (max - stride_value));
675 case kCondGT:
676 return stride_value == -1 ||
677 (IsIntAndGet(upper_expr, &value) && value >= (min - stride_value - 1));
678 case kCondGE:
679 return (IsIntAndGet(upper_expr, &value) && value >= (min - stride_value));
Aart Bike9f37602015-10-09 11:15:55 -0700680 default:
681 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700682 }
683 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700684}
685
Aart Bik30efb4e2015-07-30 12:14:31 -0700686void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
687 HInstruction* instruction,
688 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700689 auto it = induction_.find(loop);
690 if (it == induction_.end()) {
691 it = induction_.Put(loop,
692 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100693 std::less<HInstruction*>(),
694 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700695 }
696 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700697}
698
Aart Bike609b7c2015-08-27 13:46:58 -0700699HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
700 HInstruction* instruction) {
701 auto it = induction_.find(loop);
702 if (it != induction_.end()) {
703 auto loop_it = it->second.find(instruction);
704 if (loop_it != it->second.end()) {
705 return loop_it->second;
706 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700707 }
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800708 if (loop->IsDefinedOutOfTheLoop(instruction)) {
709 DCHECK(instruction->GetBlock()->Dominates(loop->GetPreHeader()));
Aart Bik471a2032015-09-04 18:22:11 -0700710 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700711 AssignInfo(loop, instruction, info);
712 return info;
713 }
714 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -0700715}
716
Aart Bikd14c5952015-09-08 15:25:15 -0700717HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
718 Primitive::Type type) {
719 if (type == Primitive::kPrimInt) {
720 return CreateInvariantFetch(graph_->GetIntConstant(value));
721 }
722 DCHECK_EQ(type, Primitive::kPrimLong);
723 return CreateInvariantFetch(graph_->GetLongConstant(value));
724}
725
Aart Bik471a2032015-09-04 18:22:11 -0700726HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
727 InductionOp op,
728 InductionInfo* a,
729 InductionInfo* b) {
730 // Perform some light-weight simplifications during construction of a new invariant.
731 // This often safes memory and yields a more concise representation of the induction.
732 // More exhaustive simplifications are done by later phases once induction nodes are
733 // translated back into HIR code (e.g. by loop optimizations or BCE).
734 int64_t value = -1;
735 if (IsIntAndGet(a, &value)) {
736 if (value == 0) {
737 // Simplify 0 + b = b, 0 * b = 0.
738 if (op == kAdd) {
739 return b;
740 } else if (op == kMul) {
741 return a;
742 }
Aart Bikd14c5952015-09-08 15:25:15 -0700743 } else if (op == kMul) {
744 // Simplify 1 * b = b, -1 * b = -b
745 if (value == 1) {
746 return b;
747 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800748 return CreateSimplifiedInvariant(kNeg, nullptr, b);
Aart Bikd14c5952015-09-08 15:25:15 -0700749 }
Aart Bik471a2032015-09-04 18:22:11 -0700750 }
751 }
752 if (IsIntAndGet(b, &value)) {
753 if (value == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700754 // Simplify a + 0 = a, a - 0 = a, a * 0 = 0, -0 = 0.
Aart Bik471a2032015-09-04 18:22:11 -0700755 if (op == kAdd || op == kSub) {
756 return a;
757 } else if (op == kMul || op == kNeg) {
758 return b;
759 }
Aart Bikd14c5952015-09-08 15:25:15 -0700760 } else if (op == kMul || op == kDiv) {
761 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
762 if (value == 1) {
763 return a;
764 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800765 return CreateSimplifiedInvariant(kNeg, nullptr, a);
Aart Bikd14c5952015-09-08 15:25:15 -0700766 }
Aart Bik471a2032015-09-04 18:22:11 -0700767 }
768 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -0700769 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
770 if (op == kAdd) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800771 return CreateSimplifiedInvariant(kSub, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700772 } else if (op == kSub) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800773 return CreateSimplifiedInvariant(kAdd, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700774 } else if (op == kNeg) {
775 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -0700776 }
Aart Bik7d57d7f2015-12-09 14:39:48 -0800777 } else if (b->operation == kSub) {
778 // Simplify - (a - b) = b - a.
779 if (op == kNeg) {
780 return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a);
781 }
Aart Bik471a2032015-09-04 18:22:11 -0700782 }
783 return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr);
784}
785
Aart Bik7d57d7f2015-12-09 14:39:48 -0800786bool HInductionVarAnalysis::IsIntAndGet(InductionInfo* info, int64_t* value) {
787 if (info != nullptr && info->induction_class == kInvariant) {
788 // A direct constant fetch.
789 if (info->operation == kFetch) {
790 DCHECK(info->fetch);
791 if (info->fetch->IsIntConstant()) {
792 *value = info->fetch->AsIntConstant()->GetValue();
793 return true;
794 } else if (info->fetch->IsLongConstant()) {
795 *value = info->fetch->AsLongConstant()->GetValue();
796 return true;
797 }
798 }
799 // Use range analysis to resolve compound values.
800 InductionVarRange range(this);
801 int32_t min_val = 0;
802 int32_t max_val = 0;
803 if (range.IsConstantRange(info, &min_val, &max_val) && min_val == max_val) {
804 *value = min_val;
805 return true;
806 }
807 }
808 return false;
809}
810
Aart Bik30efb4e2015-07-30 12:14:31 -0700811bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
812 InductionInfo* info2) {
813 // Test structural equality only, without accounting for simplifications.
814 if (info1 != nullptr && info2 != nullptr) {
815 return
816 info1->induction_class == info2->induction_class &&
817 info1->operation == info2->operation &&
818 info1->fetch == info2->fetch &&
819 InductionEqual(info1->op_a, info2->op_a) &&
820 InductionEqual(info1->op_b, info2->op_b);
821 }
822 // Otherwise only two nullptrs are considered equal.
823 return info1 == info2;
824}
825
826std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
827 if (info != nullptr) {
828 if (info->induction_class == kInvariant) {
829 std::string inv = "(";
830 inv += InductionToString(info->op_a);
831 switch (info->operation) {
Aart Bik22f05872015-10-27 15:56:28 -0700832 case kNop: inv += " @ "; break;
833 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700834 case kSub:
Aart Bik22f05872015-10-27 15:56:28 -0700835 case kNeg: inv += " - "; break;
836 case kMul: inv += " * "; break;
837 case kDiv: inv += " / "; break;
838 case kLT: inv += " < "; break;
839 case kLE: inv += " <= "; break;
840 case kGT: inv += " > "; break;
841 case kGE: inv += " >= "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700842 case kFetch:
Aart Bike609b7c2015-08-27 13:46:58 -0700843 DCHECK(info->fetch);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800844 if (info->fetch->IsIntConstant()) {
845 inv += std::to_string(info->fetch->AsIntConstant()->GetValue());
846 } else if (info->fetch->IsLongConstant()) {
847 inv += std::to_string(info->fetch->AsLongConstant()->GetValue());
Aart Bik471a2032015-09-04 18:22:11 -0700848 } else {
849 inv += std::to_string(info->fetch->GetId()) + ":" + info->fetch->DebugName();
850 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700851 break;
Aart Bik22f05872015-10-27 15:56:28 -0700852 case kTripCountInLoop: inv += " (TC-loop) "; break;
853 case kTripCountInBody: inv += " (TC-body) "; break;
854 case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break;
855 case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700856 }
857 inv += InductionToString(info->op_b);
858 return inv + ")";
859 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700860 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700861 if (info->induction_class == kLinear) {
862 return "(" + InductionToString(info->op_a) + " * i + " +
863 InductionToString(info->op_b) + ")";
864 } else if (info->induction_class == kWrapAround) {
865 return "wrap(" + InductionToString(info->op_a) + ", " +
866 InductionToString(info->op_b) + ")";
867 } else if (info->induction_class == kPeriodic) {
868 return "periodic(" + InductionToString(info->op_a) + ", " +
869 InductionToString(info->op_b) + ")";
870 }
871 }
872 }
873 return "";
874}
875
876} // namespace art