blob: cf0f3493fd38867953a0b3309f36622dee7127e6 [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/**
23 * Returns true if instruction is invariant within the given loop.
24 */
25static bool IsLoopInvariant(HLoopInformation* loop, HInstruction* instruction) {
26 HLoopInformation* other_loop = instruction->GetBlock()->GetLoopInformation();
27 if (other_loop != loop) {
28 // If instruction does not occur in same loop, it is invariant
29 // if it appears in an outer loop (including no loop at all).
30 return other_loop == nullptr || loop->IsIn(*other_loop);
31 }
32 return false;
33}
34
35/**
Aart Bik22af3be2015-09-10 12:50:58 -070036 * Since graph traversal may enter a SCC at any position, an initial representation may be rotated,
37 * along dependences, viz. any of (a, b, c, d), (d, a, b, c) (c, d, a, b), (b, c, d, a) assuming
38 * a chain of dependences (mutual independent items may occur in arbitrary order). For proper
39 * classification, the lexicographically first entry-phi is rotated to the front.
40 */
41static void RotateEntryPhiFirst(HLoopInformation* loop,
42 ArenaVector<HInstruction*>* scc,
43 ArenaVector<HInstruction*>* new_scc) {
44 // Find very first entry-phi.
45 const HInstructionList& phis = loop->GetHeader()->GetPhis();
46 HInstruction* phi = nullptr;
47 size_t phi_pos = -1;
48 const size_t size = scc->size();
49 for (size_t i = 0; i < size; i++) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010050 HInstruction* other = (*scc)[i];
Aart Bikf475bee2015-09-16 12:50:25 -070051 if (other->IsLoopHeaderPhi() && (phi == nullptr || phis.FoundBefore(other, phi))) {
52 phi = other;
Aart Bik22af3be2015-09-10 12:50:58 -070053 phi_pos = i;
54 }
55 }
56
57 // If found, bring that entry-phi to front.
58 if (phi != nullptr) {
59 new_scc->clear();
60 for (size_t i = 0; i < size; i++) {
Vladimir Markoec7802a2015-10-01 20:57:57 +010061 new_scc->push_back((*scc)[phi_pos]);
Aart Bik22af3be2015-09-10 12:50:58 -070062 if (++phi_pos >= size) phi_pos = 0;
63 }
64 DCHECK_EQ(size, new_scc->size());
65 scc->swap(*new_scc);
66 }
67}
68
Aart Bik30efb4e2015-07-30 12:14:31 -070069//
70// Class methods.
71//
72
73HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph)
74 : HOptimization(graph, kInductionPassName),
75 global_depth_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +010076 stack_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
77 scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
78 map_(std::less<HInstruction*>(),
79 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
80 cycle_(std::less<HInstruction*>(),
81 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
82 induction_(std::less<HLoopInformation*>(),
83 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) {
Aart Bik30efb4e2015-07-30 12:14:31 -070084}
85
86void HInductionVarAnalysis::Run() {
Aart Bike609b7c2015-08-27 13:46:58 -070087 // Detects sequence variables (generalized induction variables) during an inner-loop-first
88 // traversal of all loops using Gerlek's algorithm. The order is only relevant if outer
89 // loops would use induction information of inner loops (not currently done).
Aart Bik30efb4e2015-07-30 12:14:31 -070090 for (HPostOrderIterator it_graph(*graph_); !it_graph.Done(); it_graph.Advance()) {
91 HBasicBlock* graph_block = it_graph.Current();
92 if (graph_block->IsLoopHeader()) {
93 VisitLoop(graph_block->GetLoopInformation());
94 }
95 }
96}
97
98void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) {
99 // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's
100 // algorithm. Due to the descendant-first nature, classification happens "on-demand".
101 global_depth_ = 0;
Aart Bike609b7c2015-08-27 13:46:58 -0700102 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700103 map_.clear();
104
105 for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) {
106 HBasicBlock* loop_block = it_loop.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700107 DCHECK(loop_block->IsInLoop());
Aart Bik30efb4e2015-07-30 12:14:31 -0700108 if (loop_block->GetLoopInformation() != loop) {
109 continue; // Inner loops already visited.
110 }
111 // Visit phi-operations and instructions.
112 for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) {
113 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700114 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700115 VisitNode(loop, instruction);
116 }
117 }
118 for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) {
119 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700120 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700121 VisitNode(loop, instruction);
122 }
123 }
124 }
125
Aart Bike609b7c2015-08-27 13:46:58 -0700126 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700127 map_.clear();
Aart Bikd14c5952015-09-08 15:25:15 -0700128
129 // Determine the loop's trip count.
130 VisitControl(loop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700131}
132
133void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700134 const uint32_t d1 = ++global_depth_;
Aart Bike609b7c2015-08-27 13:46:58 -0700135 map_.Put(instruction, NodeInfo(d1));
Aart Bik30efb4e2015-07-30 12:14:31 -0700136 stack_.push_back(instruction);
137
138 // Visit all descendants.
139 uint32_t low = d1;
140 for (size_t i = 0, count = instruction->InputCount(); i < count; ++i) {
141 low = std::min(low, VisitDescendant(loop, instruction->InputAt(i)));
142 }
143
144 // Lower or found SCC?
145 if (low < d1) {
Aart Bike609b7c2015-08-27 13:46:58 -0700146 map_.find(instruction)->second.depth = low;
Aart Bik30efb4e2015-07-30 12:14:31 -0700147 } else {
148 scc_.clear();
149 cycle_.clear();
150
151 // Pop the stack to build the SCC for classification.
152 while (!stack_.empty()) {
153 HInstruction* x = stack_.back();
154 scc_.push_back(x);
155 stack_.pop_back();
Aart Bike609b7c2015-08-27 13:46:58 -0700156 map_.find(x)->second.done = true;
Aart Bik30efb4e2015-07-30 12:14:31 -0700157 if (x == instruction) {
158 break;
159 }
160 }
161
162 // Classify the SCC.
Aart Bikf475bee2015-09-16 12:50:25 -0700163 if (scc_.size() == 1 && !scc_[0]->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700164 ClassifyTrivial(loop, scc_[0]);
165 } else {
166 ClassifyNonTrivial(loop);
167 }
168
169 scc_.clear();
170 cycle_.clear();
171 }
172}
173
174uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) {
175 // If the definition is either outside the loop (loop invariant entry value)
176 // or assigned in inner loop (inner exit value), the traversal stops.
177 HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation();
178 if (otherLoop != loop) {
179 return global_depth_;
180 }
181
182 // Inspect descendant node.
Aart Bike609b7c2015-08-27 13:46:58 -0700183 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700184 VisitNode(loop, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700185 return map_.find(instruction)->second.depth;
Aart Bik30efb4e2015-07-30 12:14:31 -0700186 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700187 auto it = map_.find(instruction);
Aart Bik30efb4e2015-07-30 12:14:31 -0700188 return it->second.done ? global_depth_ : it->second.depth;
189 }
190}
191
192void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) {
193 InductionInfo* info = nullptr;
194 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700195 info = TransferPhi(loop, instruction, /* input_index */ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700196 } else if (instruction->IsAdd()) {
197 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
198 LookupInfo(loop, instruction->InputAt(1)), kAdd);
199 } else if (instruction->IsSub()) {
200 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
201 LookupInfo(loop, instruction->InputAt(1)), kSub);
202 } else if (instruction->IsMul()) {
203 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
204 LookupInfo(loop, instruction->InputAt(1)));
Aart Bike609b7c2015-08-27 13:46:58 -0700205 } else if (instruction->IsShl()) {
206 info = TransferShl(LookupInfo(loop, instruction->InputAt(0)),
207 LookupInfo(loop, instruction->InputAt(1)),
208 instruction->InputAt(0)->GetType());
Aart Bik30efb4e2015-07-30 12:14:31 -0700209 } else if (instruction->IsNeg()) {
210 info = TransferNeg(LookupInfo(loop, instruction->InputAt(0)));
Aart Bike609b7c2015-08-27 13:46:58 -0700211 } else if (instruction->IsBoundsCheck()) {
212 info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through.
213 } else if (instruction->IsTypeConversion()) {
214 HTypeConversion* conversion = instruction->AsTypeConversion();
215 // TODO: accept different conversion scenarios.
216 if (conversion->GetResultType() == conversion->GetInputType()) {
217 info = LookupInfo(loop, conversion->GetInput());
218 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700219 }
220
221 // Successfully classified?
222 if (info != nullptr) {
223 AssignInfo(loop, instruction, info);
224 }
225}
226
227void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) {
228 const size_t size = scc_.size();
Aart Bike609b7c2015-08-27 13:46:58 -0700229 DCHECK_GE(size, 1u);
Aart Bik22af3be2015-09-10 12:50:58 -0700230
231 // Rotate proper entry-phi to front.
232 if (size > 1) {
Vladimir Marko5233f932015-09-29 19:01:15 +0100233 ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700234 RotateEntryPhiFirst(loop, &scc_, &other);
235 }
236
Aart Bikf475bee2015-09-16 12:50:25 -0700237 // Analyze from entry-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700238 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700239 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700240 return;
241 }
Aart Bikf475bee2015-09-16 12:50:25 -0700242
243 // External link should be loop invariant.
244 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700245 if (initial == nullptr || initial->induction_class != kInvariant) {
246 return;
247 }
248
Aart Bikf475bee2015-09-16 12:50:25 -0700249 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700250 if (size == 1) {
Aart Bikf475bee2015-09-16 12:50:25 -0700251 InductionInfo* update = TransferPhi(loop, phi, /* input_index */ 1);
Aart Bik30efb4e2015-07-30 12:14:31 -0700252 if (update != nullptr) {
Aart Bik471a2032015-09-04 18:22:11 -0700253 AssignInfo(loop, phi, CreateInduction(kWrapAround, initial, update));
Aart Bik30efb4e2015-07-30 12:14:31 -0700254 }
255 return;
256 }
257
258 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700259 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700260 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700261 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700262 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700263 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700264 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700265 } else if (instruction->IsAdd()) {
266 update = SolveAddSub(
267 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
268 } else if (instruction->IsSub()) {
269 update = SolveAddSub(
270 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bik30efb4e2015-07-30 12:14:31 -0700271 }
272 if (update == nullptr) {
273 return;
274 }
Aart Bike609b7c2015-08-27 13:46:58 -0700275 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700276 }
277
Aart Bikf475bee2015-09-16 12:50:25 -0700278 // Success if all internal links received the same temporary meaning.
279 InductionInfo* induction = SolvePhi(phi, /* input_index */ 1);
280 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700281 switch (induction->induction_class) {
282 case kInvariant:
Aart Bik22af3be2015-09-10 12:50:58 -0700283 // Classify first phi and then the rest of the cycle "on-demand".
284 // Statements are scanned in order.
Aart Bik471a2032015-09-04 18:22:11 -0700285 AssignInfo(loop, phi, CreateInduction(kLinear, induction, initial));
Aart Bik22af3be2015-09-10 12:50:58 -0700286 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700287 ClassifyTrivial(loop, scc_[i]);
288 }
289 break;
290 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700291 // Classify all elements in the cycle with the found periodic induction while
292 // rotating each first element to the end. Lastly, phi is classified.
293 // Statements are scanned in reverse order.
294 for (size_t i = size - 1; i >= 1; i--) {
295 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700296 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
297 }
298 AssignInfo(loop, phi, induction);
299 break;
300 default:
301 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700302 }
303 }
304}
305
Aart Bike609b7c2015-08-27 13:46:58 -0700306HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
307 InductionInfo* induction,
308 InductionInfo* last) {
309 // Rotates a periodic induction of the form
310 // (a, b, c, d, e)
311 // into
312 // (b, c, d, e, a)
313 // in preparation of assigning this to the previous variable in the sequence.
314 if (induction->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700315 return CreateInduction(kPeriodic, induction, last);
Aart Bike609b7c2015-08-27 13:46:58 -0700316 }
Aart Bik471a2032015-09-04 18:22:11 -0700317 return CreateInduction(kPeriodic, induction->op_a, RotatePeriodicInduction(induction->op_b, last));
Aart Bike609b7c2015-08-27 13:46:58 -0700318}
319
Aart Bikf475bee2015-09-16 12:50:25 -0700320HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
321 HInstruction* phi,
322 size_t input_index) {
323 // Match all phi inputs from input_index onwards exactly.
324 const size_t count = phi->InputCount();
325 DCHECK_LT(input_index, count);
326 InductionInfo* a = LookupInfo(loop, phi->InputAt(input_index));
327 for (size_t i = input_index + 1; i < count; i++) {
328 InductionInfo* b = LookupInfo(loop, phi->InputAt(i));
329 if (!InductionEqual(a, b)) {
330 return nullptr;
331 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700332 }
Aart Bikf475bee2015-09-16 12:50:25 -0700333 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700334}
335
336HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
337 InductionInfo* b,
338 InductionOp op) {
Aart Bike609b7c2015-08-27 13:46:58 -0700339 // Transfer over an addition or subtraction: any invariant, linear, wrap-around, or periodic
340 // can be combined with an invariant to yield a similar result. Even two linear inputs can
341 // be combined. All other combinations fail, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700342 if (a != nullptr && b != nullptr) {
343 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700344 return CreateInvariantOp(op, a, b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700345 } else if (a->induction_class == kLinear && b->induction_class == kLinear) {
Aart Bik471a2032015-09-04 18:22:11 -0700346 return CreateInduction(
Aart Bike609b7c2015-08-27 13:46:58 -0700347 kLinear, TransferAddSub(a->op_a, b->op_a, op), TransferAddSub(a->op_b, b->op_b, op));
348 } else if (a->induction_class == kInvariant) {
349 InductionInfo* new_a = b->op_a;
350 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
351 if (b->induction_class != kLinear) {
352 DCHECK(b->induction_class == kWrapAround || b->induction_class == kPeriodic);
353 new_a = TransferAddSub(a, new_a, op);
354 } else if (op == kSub) { // Negation required.
355 new_a = TransferNeg(new_a);
356 }
Aart Bik471a2032015-09-04 18:22:11 -0700357 return CreateInduction(b->induction_class, new_a, new_b);
Aart Bike609b7c2015-08-27 13:46:58 -0700358 } else if (b->induction_class == kInvariant) {
359 InductionInfo* new_a = a->op_a;
360 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
361 if (a->induction_class != kLinear) {
362 DCHECK(a->induction_class == kWrapAround || a->induction_class == kPeriodic);
363 new_a = TransferAddSub(new_a, b, op);
364 }
Aart Bik471a2032015-09-04 18:22:11 -0700365 return CreateInduction(a->induction_class, new_a, new_b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700366 }
367 }
368 return nullptr;
369}
370
371HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
372 InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700373 // Transfer over a multiplication: any invariant, linear, wrap-around, or periodic
374 // can be multiplied with an invariant to yield a similar but multiplied result.
375 // Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700376 if (a != nullptr && b != nullptr) {
377 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700378 return CreateInvariantOp(kMul, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700379 } else if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700380 return CreateInduction(b->induction_class, TransferMul(a, b->op_a), TransferMul(a, b->op_b));
Aart Bike609b7c2015-08-27 13:46:58 -0700381 } else if (b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700382 return CreateInduction(a->induction_class, TransferMul(a->op_a, b), TransferMul(a->op_b, b));
Aart Bike609b7c2015-08-27 13:46:58 -0700383 }
384 }
385 return nullptr;
386}
387
388HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferShl(InductionInfo* a,
389 InductionInfo* b,
Aart Bikd14c5952015-09-08 15:25:15 -0700390 Primitive::Type type) {
Aart Bike609b7c2015-08-27 13:46:58 -0700391 // Transfer over a shift left: treat shift by restricted constant as equivalent multiplication.
Aart Bik471a2032015-09-04 18:22:11 -0700392 int64_t value = -1;
393 if (a != nullptr && IsIntAndGet(b, &value)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700394 // Obtain the constant needed for the multiplication. This yields an existing instruction
395 // if the constants is already there. Otherwise, this has a side effect on the HIR.
396 // The restriction on the shift factor avoids generating a negative constant
397 // (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that generalization
398 // for shift factors outside [0,32) and [0,64) ranges is done by earlier simplification.
Aart Bikd14c5952015-09-08 15:25:15 -0700399 if ((type == Primitive::kPrimInt && 0 <= value && value < 31) ||
400 (type == Primitive::kPrimLong && 0 <= value && value < 63)) {
401 return TransferMul(a, CreateConstant(1 << value, type));
Aart Bik30efb4e2015-07-30 12:14:31 -0700402 }
403 }
404 return nullptr;
405}
406
407HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
Aart Bike609b7c2015-08-27 13:46:58 -0700408 // Transfer over a unary negation: an invariant, linear, wrap-around, or periodic input
409 // yields a similar but negated induction as result.
Aart Bik30efb4e2015-07-30 12:14:31 -0700410 if (a != nullptr) {
411 if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700412 return CreateInvariantOp(kNeg, nullptr, a);
Aart Bik30efb4e2015-07-30 12:14:31 -0700413 }
Aart Bik471a2032015-09-04 18:22:11 -0700414 return CreateInduction(a->induction_class, TransferNeg(a->op_a), TransferNeg(a->op_b));
Aart Bik30efb4e2015-07-30 12:14:31 -0700415 }
416 return nullptr;
417}
418
Aart Bikf475bee2015-09-16 12:50:25 -0700419HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
420 size_t input_index) {
421 // Match all phi inputs from input_index onwards exactly.
422 const size_t count = phi->InputCount();
423 DCHECK_LT(input_index, count);
424 auto ita = cycle_.find(phi->InputAt(input_index));
Aart Bik30efb4e2015-07-30 12:14:31 -0700425 if (ita != cycle_.end()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700426 for (size_t i = input_index + 1; i < count; i++) {
427 auto itb = cycle_.find(phi->InputAt(i));
428 if (itb == cycle_.end() ||
429 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700430 return nullptr;
431 }
432 }
Aart Bikf475bee2015-09-16 12:50:25 -0700433 return ita->second;
434 }
435 return nullptr;
436}
437
438HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
439 HLoopInformation* loop,
440 HInstruction* entry_phi,
441 HInstruction* phi) {
442 // Match all phi inputs.
443 InductionInfo* match = SolvePhi(phi, /* input_index */ 0);
444 if (match != nullptr) {
445 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700446 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700447
Aart Bikf475bee2015-09-16 12:50:25 -0700448 // Otherwise, try to solve for a periodic seeded from phi onward.
449 // Only tight multi-statement cycles are considered in order to
450 // simplify rotating the periodic during the final classification.
451 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
452 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700453 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700454 if (phi->InputAt(1) == entry_phi) {
455 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik471a2032015-09-04 18:22:11 -0700456 return CreateInduction(kPeriodic, a, initial);
Aart Bike609b7c2015-08-27 13:46:58 -0700457 }
Aart Bikf475bee2015-09-16 12:50:25 -0700458 InductionInfo* b = SolvePhi(phi, /* input_index */ 1);
459 if (b != nullptr && b->induction_class == kPeriodic) {
460 return CreateInduction(kPeriodic, a, b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700461 }
462 }
463 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700464 return nullptr;
465}
466
Aart Bike609b7c2015-08-27 13:46:58 -0700467HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700468 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700469 HInstruction* instruction,
470 HInstruction* x,
471 HInstruction* y,
472 InductionOp op,
473 bool is_first_call) {
474 // Solve within a cycle over an addition or subtraction: adding or subtracting an
475 // invariant value, seeded from phi, keeps adding to the stride of the induction.
476 InductionInfo* b = LookupInfo(loop, y);
477 if (b != nullptr && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700478 if (x == entry_phi) {
Aart Bik471a2032015-09-04 18:22:11 -0700479 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700480 }
481 auto it = cycle_.find(x);
482 if (it != cycle_.end()) {
483 InductionInfo* a = it->second;
484 if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700485 return CreateInvariantOp(op, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700486 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700487 }
488 }
Aart Bike609b7c2015-08-27 13:46:58 -0700489
490 // Try some alternatives before failing.
491 if (op == kAdd) {
492 // Try the other way around for an addition if considered for first time.
493 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700494 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700495 }
496 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700497 // Solve within a tight cycle that is formed by exactly two instructions,
498 // one phi and one update, for a periodic idiom of the form k = c - k;
499 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700500 InductionInfo* a = LookupInfo(loop, x);
501 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700502 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik471a2032015-09-04 18:22:11 -0700503 return CreateInduction(kPeriodic, CreateInvariantOp(kSub, a, initial), initial);
Aart Bike609b7c2015-08-27 13:46:58 -0700504 }
505 }
506 }
507
Aart Bik30efb4e2015-07-30 12:14:31 -0700508 return nullptr;
509}
510
Aart Bikd14c5952015-09-08 15:25:15 -0700511void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
512 HInstruction* control = loop->GetHeader()->GetLastInstruction();
513 if (control->IsIf()) {
514 HIf* ifs = control->AsIf();
515 HBasicBlock* if_true = ifs->IfTrueSuccessor();
516 HBasicBlock* if_false = ifs->IfFalseSuccessor();
517 HInstruction* if_expr = ifs->InputAt(0);
518 // Determine if loop has following structure in header.
519 // loop-header: ....
520 // if (condition) goto X
521 if (if_expr->IsCondition()) {
522 HCondition* condition = if_expr->AsCondition();
523 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
524 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
525 Primitive::Type type = condition->InputAt(0)->GetType();
526 // Determine if the loop control uses integral arithmetic and an if-exit (X outside) or an
527 // if-iterate (X inside), always expressed as if-iterate when passing into VisitCondition().
528 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
529 // Loop control is not 32/64-bit integral.
530 } else if (a == nullptr || b == nullptr) {
531 // Loop control is not a sequence.
532 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
533 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
534 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
535 VisitCondition(loop, a, b, type, condition->GetCondition());
536 }
537 }
538 }
539}
540
541void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
542 InductionInfo* a,
543 InductionInfo* b,
544 Primitive::Type type,
545 IfCondition cmp) {
546 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700547 // 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 -0700548 switch (cmp) {
549 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
550 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
551 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
552 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700553 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700554 default: break;
555 }
556 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700557 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700558 InductionInfo* lower_expr = a->op_b;
559 InductionInfo* upper_expr = b;
Aart Bikd14c5952015-09-08 15:25:15 -0700560 InductionInfo* stride = a->op_a;
Aart Bik9401f532015-09-28 16:25:56 -0700561 int64_t stride_value = 0;
562 if (!IsIntAndGet(stride, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700563 return;
564 }
Aart Bik9401f532015-09-28 16:25:56 -0700565 // Rewrite condition i != U into i < U or i > U if end condition is reached exactly.
566 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLT)) ||
567 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGT)))) {
568 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700569 }
Aart Bikf475bee2015-09-16 12:50:25 -0700570 // Normalize a linear loop control with a nonzero stride:
571 // stride > 0, either i < U or i <= U
572 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700573 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
574 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik9401f532015-09-28 16:25:56 -0700575 VisitTripCount(loop, lower_expr, upper_expr, stride, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700576 }
Aart Bikd14c5952015-09-08 15:25:15 -0700577 }
578}
579
580void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700581 InductionInfo* lower_expr,
582 InductionInfo* upper_expr,
Aart Bikd14c5952015-09-08 15:25:15 -0700583 InductionInfo* stride,
Aart Bik9401f532015-09-28 16:25:56 -0700584 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700585 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700586 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700587 // Any loop of the general form:
588 //
589 // for (i = L; i <= U; i += S) // S > 0
590 // or for (i = L; i >= U; i += S) // S < 0
591 // .. i ..
592 //
593 // can be normalized into:
594 //
595 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
596 // .. L + S * n ..
597 //
Aart Bik9401f532015-09-28 16:25:56 -0700598 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700599 //
Aart Bik9401f532015-09-28 16:25:56 -0700600 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
601 // an unsigned entity, for example, as in the following loop that uses the full range:
602 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
603 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
604 // for (int i = 12; i < U; i++) // TC = 0 when U >= 12
605 // If this cannot be determined at compile-time, the TC is only valid within the
606 // loop-body proper, not the loop-header unless enforced with an explicit condition.
607 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
608 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
609 // If this cannot be determined at compile-time, the TC is only valid when enforced
610 // with an explicit condition.
611 // (4) For loops which early-exits, the TC forms an upper bound, as in:
612 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
613 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
614 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
615 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700616 if (!cancels) {
617 // Convert exclusive integral inequality into inclusive integral inequality,
618 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700619 if (cmp == kCondLT) {
Aart Bik9401f532015-09-28 16:25:56 -0700620 upper_expr = CreateInvariantOp(kSub, upper_expr, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700621 } else if (cmp == kCondGT) {
Aart Bik9401f532015-09-28 16:25:56 -0700622 upper_expr = CreateInvariantOp(kAdd, upper_expr, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700623 }
624 // Compensate for stride.
Aart Bik9401f532015-09-28 16:25:56 -0700625 upper_expr = CreateInvariantOp(kAdd, upper_expr, stride);
Aart Bikd14c5952015-09-08 15:25:15 -0700626 }
Aart Bik9401f532015-09-28 16:25:56 -0700627 InductionInfo* trip_count
628 = CreateInvariantOp(kDiv, CreateInvariantOp(kSub, upper_expr, lower_expr), stride);
Aart Bikd14c5952015-09-08 15:25:15 -0700629 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700630 // should be aware that the expression is only valid under the conditions listed above.
631 InductionOp tcKind = kTripCountInBodyUnsafe;
632 if (is_taken && is_finite) {
633 tcKind = kTripCountInLoop;
634 } else if (is_finite) {
635 tcKind = kTripCountInBody;
636 } else if (is_taken) {
637 tcKind = kTripCountInLoopUnsafe;
638 }
639 AssignInfo(loop, loop->GetHeader()->GetLastInstruction(), CreateTripCount(tcKind, trip_count));
640}
641
642bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
643 InductionInfo* upper_expr,
644 IfCondition cmp) {
645 int64_t lower_value;
646 int64_t upper_value;
647 if (IsIntAndGet(lower_expr, &lower_value) && IsIntAndGet(upper_expr, &upper_value)) {
648 switch (cmp) {
649 case kCondLT: return lower_value < upper_value;
650 case kCondLE: return lower_value <= upper_value;
651 case kCondGT: return lower_value > upper_value;
652 case kCondGE: return lower_value >= upper_value;
653 case kCondEQ:
654 case kCondNE: LOG(FATAL) << "CONDITION UNREACHABLE";
655 }
656 }
657 return false; // not certain, may be untaken
658}
659
660bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
661 int64_t stride_value,
662 Primitive::Type type,
663 IfCondition cmp) {
664 const int64_t min = type == Primitive::kPrimInt
665 ? std::numeric_limits<int32_t>::min()
666 : std::numeric_limits<int64_t>::min();
667 const int64_t max = type == Primitive::kPrimInt
668 ? std::numeric_limits<int32_t>::max()
669 : std::numeric_limits<int64_t>::max();
670 // Some rules under which it is certain at compile-time that the loop is finite.
671 int64_t value;
672 switch (cmp) {
673 case kCondLT:
674 return stride_value == 1 ||
675 (IsIntAndGet(upper_expr, &value) && value <= (max - stride_value + 1));
676 case kCondLE:
677 return (IsIntAndGet(upper_expr, &value) && value <= (max - stride_value));
678 case kCondGT:
679 return stride_value == -1 ||
680 (IsIntAndGet(upper_expr, &value) && value >= (min - stride_value - 1));
681 case kCondGE:
682 return (IsIntAndGet(upper_expr, &value) && value >= (min - stride_value));
683 case kCondEQ:
684 case kCondNE: LOG(FATAL) << "CONDITION UNREACHABLE";
685 }
686 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700687}
688
Aart Bik30efb4e2015-07-30 12:14:31 -0700689void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
690 HInstruction* instruction,
691 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700692 auto it = induction_.find(loop);
693 if (it == induction_.end()) {
694 it = induction_.Put(loop,
695 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100696 std::less<HInstruction*>(),
697 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700698 }
699 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700700}
701
Aart Bike609b7c2015-08-27 13:46:58 -0700702HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
703 HInstruction* instruction) {
704 auto it = induction_.find(loop);
705 if (it != induction_.end()) {
706 auto loop_it = it->second.find(instruction);
707 if (loop_it != it->second.end()) {
708 return loop_it->second;
709 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700710 }
Aart Bike609b7c2015-08-27 13:46:58 -0700711 if (IsLoopInvariant(loop, instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -0700712 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700713 AssignInfo(loop, instruction, info);
714 return info;
715 }
716 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -0700717}
718
Aart Bikd14c5952015-09-08 15:25:15 -0700719HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
720 Primitive::Type type) {
721 if (type == Primitive::kPrimInt) {
722 return CreateInvariantFetch(graph_->GetIntConstant(value));
723 }
724 DCHECK_EQ(type, Primitive::kPrimLong);
725 return CreateInvariantFetch(graph_->GetLongConstant(value));
726}
727
Aart Bik471a2032015-09-04 18:22:11 -0700728HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
729 InductionOp op,
730 InductionInfo* a,
731 InductionInfo* b) {
732 // Perform some light-weight simplifications during construction of a new invariant.
733 // This often safes memory and yields a more concise representation of the induction.
734 // More exhaustive simplifications are done by later phases once induction nodes are
735 // translated back into HIR code (e.g. by loop optimizations or BCE).
736 int64_t value = -1;
737 if (IsIntAndGet(a, &value)) {
738 if (value == 0) {
739 // Simplify 0 + b = b, 0 * b = 0.
740 if (op == kAdd) {
741 return b;
742 } else if (op == kMul) {
743 return a;
744 }
Aart Bikd14c5952015-09-08 15:25:15 -0700745 } else if (op == kMul) {
746 // Simplify 1 * b = b, -1 * b = -b
747 if (value == 1) {
748 return b;
749 } else if (value == -1) {
750 op = kNeg;
751 a = nullptr;
752 }
Aart Bik471a2032015-09-04 18:22:11 -0700753 }
754 }
755 if (IsIntAndGet(b, &value)) {
756 if (value == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700757 // Simplify a + 0 = a, a - 0 = a, a * 0 = 0, -0 = 0.
Aart Bik471a2032015-09-04 18:22:11 -0700758 if (op == kAdd || op == kSub) {
759 return a;
760 } else if (op == kMul || op == kNeg) {
761 return b;
762 }
Aart Bikd14c5952015-09-08 15:25:15 -0700763 } else if (op == kMul || op == kDiv) {
764 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
765 if (value == 1) {
766 return a;
767 } else if (value == -1) {
768 op = kNeg;
769 b = a;
770 a = nullptr;
771 }
Aart Bik471a2032015-09-04 18:22:11 -0700772 }
773 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -0700774 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
775 if (op == kAdd) {
776 op = kSub;
777 b = b->op_b;
778 } else if (op == kSub) {
779 op = kAdd;
780 b = b->op_b;
781 } else if (op == kNeg) {
782 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -0700783 }
784 }
785 return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr);
786}
787
Aart Bik30efb4e2015-07-30 12:14:31 -0700788bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
789 InductionInfo* info2) {
790 // Test structural equality only, without accounting for simplifications.
791 if (info1 != nullptr && info2 != nullptr) {
792 return
793 info1->induction_class == info2->induction_class &&
794 info1->operation == info2->operation &&
795 info1->fetch == info2->fetch &&
796 InductionEqual(info1->op_a, info2->op_a) &&
797 InductionEqual(info1->op_b, info2->op_b);
798 }
799 // Otherwise only two nullptrs are considered equal.
800 return info1 == info2;
801}
802
Aart Bik471a2032015-09-04 18:22:11 -0700803bool HInductionVarAnalysis::IsIntAndGet(InductionInfo* info, int64_t* value) {
Aart Bik9401f532015-09-28 16:25:56 -0700804 if (info != nullptr && info->induction_class == kInvariant) {
805 // A direct constant fetch.
806 if (info->operation == kFetch) {
807 DCHECK(info->fetch);
808 if (info->fetch->IsIntConstant()) {
809 *value = info->fetch->AsIntConstant()->GetValue();
810 return true;
811 } else if (info->fetch->IsLongConstant()) {
812 *value = info->fetch->AsLongConstant()->GetValue();
813 return true;
814 }
815 }
816 // Use range analysis to resolve compound values.
817 int32_t range_value;
818 if (InductionVarRange::GetConstant(info, &range_value)) {
819 *value = range_value;
Aart Bik471a2032015-09-04 18:22:11 -0700820 return true;
821 }
822 }
823 return false;
824}
825
Aart Bik30efb4e2015-07-30 12:14:31 -0700826std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
827 if (info != nullptr) {
828 if (info->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700829 int64_t value = -1;
Aart Bik30efb4e2015-07-30 12:14:31 -0700830 std::string inv = "(";
831 inv += InductionToString(info->op_a);
832 switch (info->operation) {
Aart Bike609b7c2015-08-27 13:46:58 -0700833 case kNop: inv += " @ "; break;
834 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700835 case kSub:
Aart Bike609b7c2015-08-27 13:46:58 -0700836 case kNeg: inv += " - "; break;
837 case kMul: inv += " * "; break;
838 case kDiv: inv += " / "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700839 case kFetch:
Aart Bike609b7c2015-08-27 13:46:58 -0700840 DCHECK(info->fetch);
Aart Bik471a2032015-09-04 18:22:11 -0700841 if (IsIntAndGet(info, &value)) {
842 inv += std::to_string(value);
843 } else {
844 inv += std::to_string(info->fetch->GetId()) + ":" + info->fetch->DebugName();
845 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700846 break;
Aart Bik9401f532015-09-28 16:25:56 -0700847 case kTripCountInLoop: inv += "TC-loop:"; break;
848 case kTripCountInBody: inv += "TC-body:"; break;
849 case kTripCountInLoopUnsafe: inv += "TC-loop-unsafe:"; break;
850 case kTripCountInBodyUnsafe: inv += "TC-body-unsafe:"; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700851 }
852 inv += InductionToString(info->op_b);
853 return inv + ")";
854 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700855 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700856 if (info->induction_class == kLinear) {
857 return "(" + InductionToString(info->op_a) + " * i + " +
858 InductionToString(info->op_b) + ")";
859 } else if (info->induction_class == kWrapAround) {
860 return "wrap(" + InductionToString(info->op_a) + ", " +
861 InductionToString(info->op_b) + ")";
862 } else if (info->induction_class == kPeriodic) {
863 return "periodic(" + InductionToString(info->op_a) + ", " +
864 InductionToString(info->op_b) + ")";
865 }
866 }
867 }
868 return "";
869}
870
871} // namespace art