blob: e5123deed68955955e5e9d304110fa2ee4e67487 [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++) {
Aart Bikf475bee2015-09-16 12:50:25 -070050 HInstruction* other = scc->at(i);
51 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++) {
61 DCHECK_LT(phi_pos, size);
62 new_scc->push_back(scc->at(phi_pos));
63 if (++phi_pos >= size) phi_pos = 0;
64 }
65 DCHECK_EQ(size, new_scc->size());
66 scc->swap(*new_scc);
67 }
68}
69
Aart Bik30efb4e2015-07-30 12:14:31 -070070//
71// Class methods.
72//
73
74HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph)
75 : HOptimization(graph, kInductionPassName),
76 global_depth_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +010077 stack_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
78 scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
79 map_(std::less<HInstruction*>(),
80 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
81 cycle_(std::less<HInstruction*>(),
82 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
83 induction_(std::less<HLoopInformation*>(),
84 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) {
Aart Bik30efb4e2015-07-30 12:14:31 -070085}
86
87void HInductionVarAnalysis::Run() {
Aart Bike609b7c2015-08-27 13:46:58 -070088 // Detects sequence variables (generalized induction variables) during an inner-loop-first
89 // traversal of all loops using Gerlek's algorithm. The order is only relevant if outer
90 // loops would use induction information of inner loops (not currently done).
Aart Bik30efb4e2015-07-30 12:14:31 -070091 for (HPostOrderIterator it_graph(*graph_); !it_graph.Done(); it_graph.Advance()) {
92 HBasicBlock* graph_block = it_graph.Current();
93 if (graph_block->IsLoopHeader()) {
94 VisitLoop(graph_block->GetLoopInformation());
95 }
96 }
97}
98
99void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) {
100 // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's
101 // algorithm. Due to the descendant-first nature, classification happens "on-demand".
102 global_depth_ = 0;
Aart Bike609b7c2015-08-27 13:46:58 -0700103 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700104 map_.clear();
105
106 for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) {
107 HBasicBlock* loop_block = it_loop.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700108 DCHECK(loop_block->IsInLoop());
Aart Bik30efb4e2015-07-30 12:14:31 -0700109 if (loop_block->GetLoopInformation() != loop) {
110 continue; // Inner loops already visited.
111 }
112 // Visit phi-operations and instructions.
113 for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) {
114 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700115 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700116 VisitNode(loop, instruction);
117 }
118 }
119 for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) {
120 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700121 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700122 VisitNode(loop, instruction);
123 }
124 }
125 }
126
Aart Bike609b7c2015-08-27 13:46:58 -0700127 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700128 map_.clear();
Aart Bikd14c5952015-09-08 15:25:15 -0700129
130 // Determine the loop's trip count.
131 VisitControl(loop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700132}
133
134void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700135 const uint32_t d1 = ++global_depth_;
Aart Bike609b7c2015-08-27 13:46:58 -0700136 map_.Put(instruction, NodeInfo(d1));
Aart Bik30efb4e2015-07-30 12:14:31 -0700137 stack_.push_back(instruction);
138
139 // Visit all descendants.
140 uint32_t low = d1;
141 for (size_t i = 0, count = instruction->InputCount(); i < count; ++i) {
142 low = std::min(low, VisitDescendant(loop, instruction->InputAt(i)));
143 }
144
145 // Lower or found SCC?
146 if (low < d1) {
Aart Bike609b7c2015-08-27 13:46:58 -0700147 map_.find(instruction)->second.depth = low;
Aart Bik30efb4e2015-07-30 12:14:31 -0700148 } else {
149 scc_.clear();
150 cycle_.clear();
151
152 // Pop the stack to build the SCC for classification.
153 while (!stack_.empty()) {
154 HInstruction* x = stack_.back();
155 scc_.push_back(x);
156 stack_.pop_back();
Aart Bike609b7c2015-08-27 13:46:58 -0700157 map_.find(x)->second.done = true;
Aart Bik30efb4e2015-07-30 12:14:31 -0700158 if (x == instruction) {
159 break;
160 }
161 }
162
163 // Classify the SCC.
Aart Bikf475bee2015-09-16 12:50:25 -0700164 if (scc_.size() == 1 && !scc_[0]->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700165 ClassifyTrivial(loop, scc_[0]);
166 } else {
167 ClassifyNonTrivial(loop);
168 }
169
170 scc_.clear();
171 cycle_.clear();
172 }
173}
174
175uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) {
176 // If the definition is either outside the loop (loop invariant entry value)
177 // or assigned in inner loop (inner exit value), the traversal stops.
178 HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation();
179 if (otherLoop != loop) {
180 return global_depth_;
181 }
182
183 // Inspect descendant node.
Aart Bike609b7c2015-08-27 13:46:58 -0700184 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700185 VisitNode(loop, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700186 return map_.find(instruction)->second.depth;
Aart Bik30efb4e2015-07-30 12:14:31 -0700187 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700188 auto it = map_.find(instruction);
Aart Bik30efb4e2015-07-30 12:14:31 -0700189 return it->second.done ? global_depth_ : it->second.depth;
190 }
191}
192
193void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) {
194 InductionInfo* info = nullptr;
195 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700196 info = TransferPhi(loop, instruction, /* input_index */ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700197 } else if (instruction->IsAdd()) {
198 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
199 LookupInfo(loop, instruction->InputAt(1)), kAdd);
200 } else if (instruction->IsSub()) {
201 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
202 LookupInfo(loop, instruction->InputAt(1)), kSub);
203 } else if (instruction->IsMul()) {
204 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
205 LookupInfo(loop, instruction->InputAt(1)));
Aart Bike609b7c2015-08-27 13:46:58 -0700206 } else if (instruction->IsShl()) {
207 info = TransferShl(LookupInfo(loop, instruction->InputAt(0)),
208 LookupInfo(loop, instruction->InputAt(1)),
209 instruction->InputAt(0)->GetType());
Aart Bik30efb4e2015-07-30 12:14:31 -0700210 } else if (instruction->IsNeg()) {
211 info = TransferNeg(LookupInfo(loop, instruction->InputAt(0)));
Aart Bike609b7c2015-08-27 13:46:58 -0700212 } else if (instruction->IsBoundsCheck()) {
213 info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through.
214 } else if (instruction->IsTypeConversion()) {
215 HTypeConversion* conversion = instruction->AsTypeConversion();
216 // TODO: accept different conversion scenarios.
217 if (conversion->GetResultType() == conversion->GetInputType()) {
218 info = LookupInfo(loop, conversion->GetInput());
219 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700220 }
221
222 // Successfully classified?
223 if (info != nullptr) {
224 AssignInfo(loop, instruction, info);
225 }
226}
227
228void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) {
229 const size_t size = scc_.size();
Aart Bike609b7c2015-08-27 13:46:58 -0700230 DCHECK_GE(size, 1u);
Aart Bik22af3be2015-09-10 12:50:58 -0700231
232 // Rotate proper entry-phi to front.
233 if (size > 1) {
Vladimir Marko5233f932015-09-29 19:01:15 +0100234 ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700235 RotateEntryPhiFirst(loop, &scc_, &other);
236 }
237
Aart Bikf475bee2015-09-16 12:50:25 -0700238 // Analyze from entry-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700239 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700240 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700241 return;
242 }
Aart Bikf475bee2015-09-16 12:50:25 -0700243
244 // External link should be loop invariant.
245 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700246 if (initial == nullptr || initial->induction_class != kInvariant) {
247 return;
248 }
249
Aart Bikf475bee2015-09-16 12:50:25 -0700250 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700251 if (size == 1) {
Aart Bikf475bee2015-09-16 12:50:25 -0700252 InductionInfo* update = TransferPhi(loop, phi, /* input_index */ 1);
Aart Bik30efb4e2015-07-30 12:14:31 -0700253 if (update != nullptr) {
Aart Bik471a2032015-09-04 18:22:11 -0700254 AssignInfo(loop, phi, CreateInduction(kWrapAround, initial, update));
Aart Bik30efb4e2015-07-30 12:14:31 -0700255 }
256 return;
257 }
258
259 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700260 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700261 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700262 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700263 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700264 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700265 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700266 } else if (instruction->IsAdd()) {
267 update = SolveAddSub(
268 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
269 } else if (instruction->IsSub()) {
270 update = SolveAddSub(
271 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bik30efb4e2015-07-30 12:14:31 -0700272 }
273 if (update == nullptr) {
274 return;
275 }
Aart Bike609b7c2015-08-27 13:46:58 -0700276 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700277 }
278
Aart Bikf475bee2015-09-16 12:50:25 -0700279 // Success if all internal links received the same temporary meaning.
280 InductionInfo* induction = SolvePhi(phi, /* input_index */ 1);
281 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700282 switch (induction->induction_class) {
283 case kInvariant:
Aart Bik22af3be2015-09-10 12:50:58 -0700284 // Classify first phi and then the rest of the cycle "on-demand".
285 // Statements are scanned in order.
Aart Bik471a2032015-09-04 18:22:11 -0700286 AssignInfo(loop, phi, CreateInduction(kLinear, induction, initial));
Aart Bik22af3be2015-09-10 12:50:58 -0700287 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700288 ClassifyTrivial(loop, scc_[i]);
289 }
290 break;
291 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700292 // Classify all elements in the cycle with the found periodic induction while
293 // rotating each first element to the end. Lastly, phi is classified.
294 // Statements are scanned in reverse order.
295 for (size_t i = size - 1; i >= 1; i--) {
296 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700297 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
298 }
299 AssignInfo(loop, phi, induction);
300 break;
301 default:
302 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700303 }
304 }
305}
306
Aart Bike609b7c2015-08-27 13:46:58 -0700307HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
308 InductionInfo* induction,
309 InductionInfo* last) {
310 // Rotates a periodic induction of the form
311 // (a, b, c, d, e)
312 // into
313 // (b, c, d, e, a)
314 // in preparation of assigning this to the previous variable in the sequence.
315 if (induction->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700316 return CreateInduction(kPeriodic, induction, last);
Aart Bike609b7c2015-08-27 13:46:58 -0700317 }
Aart Bik471a2032015-09-04 18:22:11 -0700318 return CreateInduction(kPeriodic, induction->op_a, RotatePeriodicInduction(induction->op_b, last));
Aart Bike609b7c2015-08-27 13:46:58 -0700319}
320
Aart Bikf475bee2015-09-16 12:50:25 -0700321HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
322 HInstruction* phi,
323 size_t input_index) {
324 // Match all phi inputs from input_index onwards exactly.
325 const size_t count = phi->InputCount();
326 DCHECK_LT(input_index, count);
327 InductionInfo* a = LookupInfo(loop, phi->InputAt(input_index));
328 for (size_t i = input_index + 1; i < count; i++) {
329 InductionInfo* b = LookupInfo(loop, phi->InputAt(i));
330 if (!InductionEqual(a, b)) {
331 return nullptr;
332 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700333 }
Aart Bikf475bee2015-09-16 12:50:25 -0700334 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700335}
336
337HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
338 InductionInfo* b,
339 InductionOp op) {
Aart Bike609b7c2015-08-27 13:46:58 -0700340 // Transfer over an addition or subtraction: any invariant, linear, wrap-around, or periodic
341 // can be combined with an invariant to yield a similar result. Even two linear inputs can
342 // be combined. All other combinations fail, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700343 if (a != nullptr && b != nullptr) {
344 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700345 return CreateInvariantOp(op, a, b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700346 } else if (a->induction_class == kLinear && b->induction_class == kLinear) {
Aart Bik471a2032015-09-04 18:22:11 -0700347 return CreateInduction(
Aart Bike609b7c2015-08-27 13:46:58 -0700348 kLinear, TransferAddSub(a->op_a, b->op_a, op), TransferAddSub(a->op_b, b->op_b, op));
349 } else if (a->induction_class == kInvariant) {
350 InductionInfo* new_a = b->op_a;
351 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
352 if (b->induction_class != kLinear) {
353 DCHECK(b->induction_class == kWrapAround || b->induction_class == kPeriodic);
354 new_a = TransferAddSub(a, new_a, op);
355 } else if (op == kSub) { // Negation required.
356 new_a = TransferNeg(new_a);
357 }
Aart Bik471a2032015-09-04 18:22:11 -0700358 return CreateInduction(b->induction_class, new_a, new_b);
Aart Bike609b7c2015-08-27 13:46:58 -0700359 } else if (b->induction_class == kInvariant) {
360 InductionInfo* new_a = a->op_a;
361 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
362 if (a->induction_class != kLinear) {
363 DCHECK(a->induction_class == kWrapAround || a->induction_class == kPeriodic);
364 new_a = TransferAddSub(new_a, b, op);
365 }
Aart Bik471a2032015-09-04 18:22:11 -0700366 return CreateInduction(a->induction_class, new_a, new_b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700367 }
368 }
369 return nullptr;
370}
371
372HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
373 InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700374 // Transfer over a multiplication: any invariant, linear, wrap-around, or periodic
375 // can be multiplied with an invariant to yield a similar but multiplied result.
376 // Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700377 if (a != nullptr && b != nullptr) {
378 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700379 return CreateInvariantOp(kMul, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700380 } else if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700381 return CreateInduction(b->induction_class, TransferMul(a, b->op_a), TransferMul(a, b->op_b));
Aart Bike609b7c2015-08-27 13:46:58 -0700382 } else if (b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700383 return CreateInduction(a->induction_class, TransferMul(a->op_a, b), TransferMul(a->op_b, b));
Aart Bike609b7c2015-08-27 13:46:58 -0700384 }
385 }
386 return nullptr;
387}
388
389HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferShl(InductionInfo* a,
390 InductionInfo* b,
Aart Bikd14c5952015-09-08 15:25:15 -0700391 Primitive::Type type) {
Aart Bike609b7c2015-08-27 13:46:58 -0700392 // Transfer over a shift left: treat shift by restricted constant as equivalent multiplication.
Aart Bik471a2032015-09-04 18:22:11 -0700393 int64_t value = -1;
394 if (a != nullptr && IsIntAndGet(b, &value)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700395 // Obtain the constant needed for the multiplication. This yields an existing instruction
396 // if the constants is already there. Otherwise, this has a side effect on the HIR.
397 // The restriction on the shift factor avoids generating a negative constant
398 // (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that generalization
399 // for shift factors outside [0,32) and [0,64) ranges is done by earlier simplification.
Aart Bikd14c5952015-09-08 15:25:15 -0700400 if ((type == Primitive::kPrimInt && 0 <= value && value < 31) ||
401 (type == Primitive::kPrimLong && 0 <= value && value < 63)) {
402 return TransferMul(a, CreateConstant(1 << value, type));
Aart Bik30efb4e2015-07-30 12:14:31 -0700403 }
404 }
405 return nullptr;
406}
407
408HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
Aart Bike609b7c2015-08-27 13:46:58 -0700409 // Transfer over a unary negation: an invariant, linear, wrap-around, or periodic input
410 // yields a similar but negated induction as result.
Aart Bik30efb4e2015-07-30 12:14:31 -0700411 if (a != nullptr) {
412 if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700413 return CreateInvariantOp(kNeg, nullptr, a);
Aart Bik30efb4e2015-07-30 12:14:31 -0700414 }
Aart Bik471a2032015-09-04 18:22:11 -0700415 return CreateInduction(a->induction_class, TransferNeg(a->op_a), TransferNeg(a->op_b));
Aart Bik30efb4e2015-07-30 12:14:31 -0700416 }
417 return nullptr;
418}
419
Aart Bikf475bee2015-09-16 12:50:25 -0700420HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
421 size_t input_index) {
422 // Match all phi inputs from input_index onwards exactly.
423 const size_t count = phi->InputCount();
424 DCHECK_LT(input_index, count);
425 auto ita = cycle_.find(phi->InputAt(input_index));
Aart Bik30efb4e2015-07-30 12:14:31 -0700426 if (ita != cycle_.end()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700427 for (size_t i = input_index + 1; i < count; i++) {
428 auto itb = cycle_.find(phi->InputAt(i));
429 if (itb == cycle_.end() ||
430 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700431 return nullptr;
432 }
433 }
Aart Bikf475bee2015-09-16 12:50:25 -0700434 return ita->second;
435 }
436 return nullptr;
437}
438
439HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
440 HLoopInformation* loop,
441 HInstruction* entry_phi,
442 HInstruction* phi) {
443 // Match all phi inputs.
444 InductionInfo* match = SolvePhi(phi, /* input_index */ 0);
445 if (match != nullptr) {
446 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700447 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700448
Aart Bikf475bee2015-09-16 12:50:25 -0700449 // Otherwise, try to solve for a periodic seeded from phi onward.
450 // Only tight multi-statement cycles are considered in order to
451 // simplify rotating the periodic during the final classification.
452 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
453 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700454 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700455 if (phi->InputAt(1) == entry_phi) {
456 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik471a2032015-09-04 18:22:11 -0700457 return CreateInduction(kPeriodic, a, initial);
Aart Bike609b7c2015-08-27 13:46:58 -0700458 }
Aart Bikf475bee2015-09-16 12:50:25 -0700459 InductionInfo* b = SolvePhi(phi, /* input_index */ 1);
460 if (b != nullptr && b->induction_class == kPeriodic) {
461 return CreateInduction(kPeriodic, a, b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700462 }
463 }
464 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700465 return nullptr;
466}
467
Aart Bike609b7c2015-08-27 13:46:58 -0700468HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700469 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700470 HInstruction* instruction,
471 HInstruction* x,
472 HInstruction* y,
473 InductionOp op,
474 bool is_first_call) {
475 // Solve within a cycle over an addition or subtraction: adding or subtracting an
476 // invariant value, seeded from phi, keeps adding to the stride of the induction.
477 InductionInfo* b = LookupInfo(loop, y);
478 if (b != nullptr && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700479 if (x == entry_phi) {
Aart Bik471a2032015-09-04 18:22:11 -0700480 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700481 }
482 auto it = cycle_.find(x);
483 if (it != cycle_.end()) {
484 InductionInfo* a = it->second;
485 if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700486 return CreateInvariantOp(op, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700487 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700488 }
489 }
Aart Bike609b7c2015-08-27 13:46:58 -0700490
491 // Try some alternatives before failing.
492 if (op == kAdd) {
493 // Try the other way around for an addition if considered for first time.
494 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700495 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700496 }
497 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700498 // Solve within a tight cycle that is formed by exactly two instructions,
499 // one phi and one update, for a periodic idiom of the form k = c - k;
500 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700501 InductionInfo* a = LookupInfo(loop, x);
502 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700503 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik471a2032015-09-04 18:22:11 -0700504 return CreateInduction(kPeriodic, CreateInvariantOp(kSub, a, initial), initial);
Aart Bike609b7c2015-08-27 13:46:58 -0700505 }
506 }
507 }
508
Aart Bik30efb4e2015-07-30 12:14:31 -0700509 return nullptr;
510}
511
Aart Bikd14c5952015-09-08 15:25:15 -0700512void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
513 HInstruction* control = loop->GetHeader()->GetLastInstruction();
514 if (control->IsIf()) {
515 HIf* ifs = control->AsIf();
516 HBasicBlock* if_true = ifs->IfTrueSuccessor();
517 HBasicBlock* if_false = ifs->IfFalseSuccessor();
518 HInstruction* if_expr = ifs->InputAt(0);
519 // Determine if loop has following structure in header.
520 // loop-header: ....
521 // if (condition) goto X
522 if (if_expr->IsCondition()) {
523 HCondition* condition = if_expr->AsCondition();
524 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
525 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
526 Primitive::Type type = condition->InputAt(0)->GetType();
527 // Determine if the loop control uses integral arithmetic and an if-exit (X outside) or an
528 // if-iterate (X inside), always expressed as if-iterate when passing into VisitCondition().
529 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
530 // Loop control is not 32/64-bit integral.
531 } else if (a == nullptr || b == nullptr) {
532 // Loop control is not a sequence.
533 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
534 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
535 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
536 VisitCondition(loop, a, b, type, condition->GetCondition());
537 }
538 }
539 }
540}
541
542void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
543 InductionInfo* a,
544 InductionInfo* b,
545 Primitive::Type type,
546 IfCondition cmp) {
547 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700548 // 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 -0700549 switch (cmp) {
550 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
551 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
552 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
553 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700554 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700555 default: break;
556 }
557 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700558 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700559 InductionInfo* lower_expr = a->op_b;
560 InductionInfo* upper_expr = b;
Aart Bikd14c5952015-09-08 15:25:15 -0700561 InductionInfo* stride = a->op_a;
Aart Bik9401f532015-09-28 16:25:56 -0700562 int64_t stride_value = 0;
563 if (!IsIntAndGet(stride, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700564 return;
565 }
Aart Bik9401f532015-09-28 16:25:56 -0700566 // Rewrite condition i != U into i < U or i > U if end condition is reached exactly.
567 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLT)) ||
568 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGT)))) {
569 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700570 }
Aart Bikf475bee2015-09-16 12:50:25 -0700571 // Normalize a linear loop control with a nonzero stride:
572 // stride > 0, either i < U or i <= U
573 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700574 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
575 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik9401f532015-09-28 16:25:56 -0700576 VisitTripCount(loop, lower_expr, upper_expr, stride, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700577 }
Aart Bikd14c5952015-09-08 15:25:15 -0700578 }
579}
580
581void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700582 InductionInfo* lower_expr,
583 InductionInfo* upper_expr,
Aart Bikd14c5952015-09-08 15:25:15 -0700584 InductionInfo* stride,
Aart Bik9401f532015-09-28 16:25:56 -0700585 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700586 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700587 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700588 // Any loop of the general form:
589 //
590 // for (i = L; i <= U; i += S) // S > 0
591 // or for (i = L; i >= U; i += S) // S < 0
592 // .. i ..
593 //
594 // can be normalized into:
595 //
596 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
597 // .. L + S * n ..
598 //
Aart Bik9401f532015-09-28 16:25:56 -0700599 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700600 //
Aart Bik9401f532015-09-28 16:25:56 -0700601 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
602 // an unsigned entity, for example, as in the following loop that uses the full range:
603 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
604 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
605 // for (int i = 12; i < U; i++) // TC = 0 when U >= 12
606 // If this cannot be determined at compile-time, the TC is only valid within the
607 // loop-body proper, not the loop-header unless enforced with an explicit condition.
608 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
609 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
610 // If this cannot be determined at compile-time, the TC is only valid when enforced
611 // with an explicit condition.
612 // (4) For loops which early-exits, the TC forms an upper bound, as in:
613 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
614 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
615 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
616 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700617 if (!cancels) {
618 // Convert exclusive integral inequality into inclusive integral inequality,
619 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700620 if (cmp == kCondLT) {
Aart Bik9401f532015-09-28 16:25:56 -0700621 upper_expr = CreateInvariantOp(kSub, upper_expr, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700622 } else if (cmp == kCondGT) {
Aart Bik9401f532015-09-28 16:25:56 -0700623 upper_expr = CreateInvariantOp(kAdd, upper_expr, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700624 }
625 // Compensate for stride.
Aart Bik9401f532015-09-28 16:25:56 -0700626 upper_expr = CreateInvariantOp(kAdd, upper_expr, stride);
Aart Bikd14c5952015-09-08 15:25:15 -0700627 }
Aart Bik9401f532015-09-28 16:25:56 -0700628 InductionInfo* trip_count
629 = CreateInvariantOp(kDiv, CreateInvariantOp(kSub, upper_expr, lower_expr), stride);
Aart Bikd14c5952015-09-08 15:25:15 -0700630 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700631 // should be aware that the expression is only valid under the conditions listed above.
632 InductionOp tcKind = kTripCountInBodyUnsafe;
633 if (is_taken && is_finite) {
634 tcKind = kTripCountInLoop;
635 } else if (is_finite) {
636 tcKind = kTripCountInBody;
637 } else if (is_taken) {
638 tcKind = kTripCountInLoopUnsafe;
639 }
640 AssignInfo(loop, loop->GetHeader()->GetLastInstruction(), CreateTripCount(tcKind, trip_count));
641}
642
643bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
644 InductionInfo* upper_expr,
645 IfCondition cmp) {
646 int64_t lower_value;
647 int64_t upper_value;
648 if (IsIntAndGet(lower_expr, &lower_value) && IsIntAndGet(upper_expr, &upper_value)) {
649 switch (cmp) {
650 case kCondLT: return lower_value < upper_value;
651 case kCondLE: return lower_value <= upper_value;
652 case kCondGT: return lower_value > upper_value;
653 case kCondGE: return lower_value >= upper_value;
654 case kCondEQ:
655 case kCondNE: LOG(FATAL) << "CONDITION UNREACHABLE";
656 }
657 }
658 return false; // not certain, may be untaken
659}
660
661bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
662 int64_t stride_value,
663 Primitive::Type type,
664 IfCondition cmp) {
665 const int64_t min = type == Primitive::kPrimInt
666 ? std::numeric_limits<int32_t>::min()
667 : std::numeric_limits<int64_t>::min();
668 const int64_t max = type == Primitive::kPrimInt
669 ? std::numeric_limits<int32_t>::max()
670 : std::numeric_limits<int64_t>::max();
671 // Some rules under which it is certain at compile-time that the loop is finite.
672 int64_t value;
673 switch (cmp) {
674 case kCondLT:
675 return stride_value == 1 ||
676 (IsIntAndGet(upper_expr, &value) && value <= (max - stride_value + 1));
677 case kCondLE:
678 return (IsIntAndGet(upper_expr, &value) && value <= (max - stride_value));
679 case kCondGT:
680 return stride_value == -1 ||
681 (IsIntAndGet(upper_expr, &value) && value >= (min - stride_value - 1));
682 case kCondGE:
683 return (IsIntAndGet(upper_expr, &value) && value >= (min - stride_value));
684 case kCondEQ:
685 case kCondNE: LOG(FATAL) << "CONDITION UNREACHABLE";
686 }
687 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700688}
689
Aart Bik30efb4e2015-07-30 12:14:31 -0700690void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
691 HInstruction* instruction,
692 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700693 auto it = induction_.find(loop);
694 if (it == induction_.end()) {
695 it = induction_.Put(loop,
696 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100697 std::less<HInstruction*>(),
698 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700699 }
700 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700701}
702
Aart Bike609b7c2015-08-27 13:46:58 -0700703HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
704 HInstruction* instruction) {
705 auto it = induction_.find(loop);
706 if (it != induction_.end()) {
707 auto loop_it = it->second.find(instruction);
708 if (loop_it != it->second.end()) {
709 return loop_it->second;
710 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700711 }
Aart Bike609b7c2015-08-27 13:46:58 -0700712 if (IsLoopInvariant(loop, instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -0700713 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700714 AssignInfo(loop, instruction, info);
715 return info;
716 }
717 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -0700718}
719
Aart Bikd14c5952015-09-08 15:25:15 -0700720HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
721 Primitive::Type type) {
722 if (type == Primitive::kPrimInt) {
723 return CreateInvariantFetch(graph_->GetIntConstant(value));
724 }
725 DCHECK_EQ(type, Primitive::kPrimLong);
726 return CreateInvariantFetch(graph_->GetLongConstant(value));
727}
728
Aart Bik471a2032015-09-04 18:22:11 -0700729HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
730 InductionOp op,
731 InductionInfo* a,
732 InductionInfo* b) {
733 // Perform some light-weight simplifications during construction of a new invariant.
734 // This often safes memory and yields a more concise representation of the induction.
735 // More exhaustive simplifications are done by later phases once induction nodes are
736 // translated back into HIR code (e.g. by loop optimizations or BCE).
737 int64_t value = -1;
738 if (IsIntAndGet(a, &value)) {
739 if (value == 0) {
740 // Simplify 0 + b = b, 0 * b = 0.
741 if (op == kAdd) {
742 return b;
743 } else if (op == kMul) {
744 return a;
745 }
Aart Bikd14c5952015-09-08 15:25:15 -0700746 } else if (op == kMul) {
747 // Simplify 1 * b = b, -1 * b = -b
748 if (value == 1) {
749 return b;
750 } else if (value == -1) {
751 op = kNeg;
752 a = nullptr;
753 }
Aart Bik471a2032015-09-04 18:22:11 -0700754 }
755 }
756 if (IsIntAndGet(b, &value)) {
757 if (value == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700758 // Simplify a + 0 = a, a - 0 = a, a * 0 = 0, -0 = 0.
Aart Bik471a2032015-09-04 18:22:11 -0700759 if (op == kAdd || op == kSub) {
760 return a;
761 } else if (op == kMul || op == kNeg) {
762 return b;
763 }
Aart Bikd14c5952015-09-08 15:25:15 -0700764 } else if (op == kMul || op == kDiv) {
765 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
766 if (value == 1) {
767 return a;
768 } else if (value == -1) {
769 op = kNeg;
770 b = a;
771 a = nullptr;
772 }
Aart Bik471a2032015-09-04 18:22:11 -0700773 }
774 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -0700775 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
776 if (op == kAdd) {
777 op = kSub;
778 b = b->op_b;
779 } else if (op == kSub) {
780 op = kAdd;
781 b = b->op_b;
782 } else if (op == kNeg) {
783 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -0700784 }
785 }
786 return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr);
787}
788
Aart Bik30efb4e2015-07-30 12:14:31 -0700789bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
790 InductionInfo* info2) {
791 // Test structural equality only, without accounting for simplifications.
792 if (info1 != nullptr && info2 != nullptr) {
793 return
794 info1->induction_class == info2->induction_class &&
795 info1->operation == info2->operation &&
796 info1->fetch == info2->fetch &&
797 InductionEqual(info1->op_a, info2->op_a) &&
798 InductionEqual(info1->op_b, info2->op_b);
799 }
800 // Otherwise only two nullptrs are considered equal.
801 return info1 == info2;
802}
803
Aart Bik471a2032015-09-04 18:22:11 -0700804bool HInductionVarAnalysis::IsIntAndGet(InductionInfo* info, int64_t* value) {
Aart Bik9401f532015-09-28 16:25:56 -0700805 if (info != nullptr && info->induction_class == kInvariant) {
806 // A direct constant fetch.
807 if (info->operation == kFetch) {
808 DCHECK(info->fetch);
809 if (info->fetch->IsIntConstant()) {
810 *value = info->fetch->AsIntConstant()->GetValue();
811 return true;
812 } else if (info->fetch->IsLongConstant()) {
813 *value = info->fetch->AsLongConstant()->GetValue();
814 return true;
815 }
816 }
817 // Use range analysis to resolve compound values.
818 int32_t range_value;
819 if (InductionVarRange::GetConstant(info, &range_value)) {
820 *value = range_value;
Aart Bik471a2032015-09-04 18:22:11 -0700821 return true;
822 }
823 }
824 return false;
825}
826
Aart Bik30efb4e2015-07-30 12:14:31 -0700827std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
828 if (info != nullptr) {
829 if (info->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700830 int64_t value = -1;
Aart Bik30efb4e2015-07-30 12:14:31 -0700831 std::string inv = "(";
832 inv += InductionToString(info->op_a);
833 switch (info->operation) {
Aart Bike609b7c2015-08-27 13:46:58 -0700834 case kNop: inv += " @ "; break;
835 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700836 case kSub:
Aart Bike609b7c2015-08-27 13:46:58 -0700837 case kNeg: inv += " - "; break;
838 case kMul: inv += " * "; break;
839 case kDiv: inv += " / "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700840 case kFetch:
Aart Bike609b7c2015-08-27 13:46:58 -0700841 DCHECK(info->fetch);
Aart Bik471a2032015-09-04 18:22:11 -0700842 if (IsIntAndGet(info, &value)) {
843 inv += std::to_string(value);
844 } else {
845 inv += std::to_string(info->fetch->GetId()) + ":" + info->fetch->DebugName();
846 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700847 break;
Aart Bik9401f532015-09-28 16:25:56 -0700848 case kTripCountInLoop: inv += "TC-loop:"; break;
849 case kTripCountInBody: inv += "TC-body:"; break;
850 case kTripCountInLoopUnsafe: inv += "TC-loop-unsafe:"; break;
851 case kTripCountInBodyUnsafe: inv += "TC-body-unsafe:"; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700852 }
853 inv += InductionToString(info->op_b);
854 return inv + ")";
855 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700856 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700857 if (info->induction_class == kLinear) {
858 return "(" + InductionToString(info->op_a) + " * i + " +
859 InductionToString(info->op_b) + ")";
860 } else if (info->induction_class == kWrapAround) {
861 return "wrap(" + InductionToString(info->op_a) + ", " +
862 InductionToString(info->op_b) + ")";
863 } else if (info->induction_class == kPeriodic) {
864 return "periodic(" + InductionToString(info->op_a) + ", " +
865 InductionToString(info->op_b) + ")";
866 }
867 }
868 }
869 return "";
870}
871
872} // namespace art