blob: c240c67e79d0df68c3cacc11bd12e2686152fae5 [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
Aart Bikcc42be02016-10-20 16:14:16 -070026 * classification, the lexicographically first loop-phi is rotated to the front.
Aart Bik22af3be2015-09-10 12:50:58 -070027 */
28static void RotateEntryPhiFirst(HLoopInformation* loop,
29 ArenaVector<HInstruction*>* scc,
30 ArenaVector<HInstruction*>* new_scc) {
Aart Bikcc42be02016-10-20 16:14:16 -070031 // Find very first loop-phi.
Aart Bik22af3be2015-09-10 12:50:58 -070032 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
Aart Bikcc42be02016-10-20 16:14:16 -070044 // If found, bring that loop-phi to front.
Aart Bik22af3be2015-09-10 12:50:58 -070045 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 Bik0d345cf2016-03-16 10:49:38 -070056/**
57 * Returns true if the from/to types denote a narrowing, integral conversion (precision loss).
58 */
59static bool IsNarrowingIntegralConversion(Primitive::Type from, Primitive::Type to) {
60 switch (from) {
61 case Primitive::kPrimLong:
62 return to == Primitive::kPrimByte || to == Primitive::kPrimShort
63 || to == Primitive::kPrimChar || to == Primitive::kPrimInt;
64 case Primitive::kPrimInt:
65 return to == Primitive::kPrimByte || to == Primitive::kPrimShort
66 || to == Primitive::kPrimChar;
67 case Primitive::kPrimChar:
68 case Primitive::kPrimShort:
69 return to == Primitive::kPrimByte;
70 default:
71 return false;
72 }
73}
74
75/**
76 * Returns narrowest data type.
77 */
78static Primitive::Type Narrowest(Primitive::Type type1, Primitive::Type type2) {
79 return Primitive::ComponentSize(type1) <= Primitive::ComponentSize(type2) ? type1 : type2;
80}
81
Aart Bik30efb4e2015-07-30 12:14:31 -070082//
83// Class methods.
84//
85
86HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph)
87 : HOptimization(graph, kInductionPassName),
88 global_depth_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +010089 stack_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko5233f932015-09-29 19:01:15 +010090 map_(std::less<HInstruction*>(),
91 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Aart Bik7dc96932016-10-12 10:01:05 -070092 scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko5233f932015-09-29 19:01:15 +010093 cycle_(std::less<HInstruction*>(),
94 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Aart Bik7dc96932016-10-12 10:01:05 -070095 type_(Primitive::kPrimVoid),
Vladimir Marko5233f932015-09-29 19:01:15 +010096 induction_(std::less<HLoopInformation*>(),
Aart Bikcc42be02016-10-20 16:14:16 -070097 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
98 cycles_(std::less<HPhi*>(),
99 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700100}
101
102void HInductionVarAnalysis::Run() {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800103 // Detects sequence variables (generalized induction variables) during an outer to inner
104 // traversal of all loops using Gerlek's algorithm. The order is important to enable
105 // range analysis on outer loop while visiting inner loops.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100106 for (HBasicBlock* graph_block : graph_->GetReversePostOrder()) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000107 // Don't analyze irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000108 if (graph_block->IsLoopHeader() && !graph_block->GetLoopInformation()->IsIrreducible()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700109 VisitLoop(graph_block->GetLoopInformation());
110 }
111 }
112}
113
114void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) {
115 // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's
116 // algorithm. Due to the descendant-first nature, classification happens "on-demand".
117 global_depth_ = 0;
Aart Bike609b7c2015-08-27 13:46:58 -0700118 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700119 map_.clear();
120
121 for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) {
122 HBasicBlock* loop_block = it_loop.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700123 DCHECK(loop_block->IsInLoop());
Aart Bik30efb4e2015-07-30 12:14:31 -0700124 if (loop_block->GetLoopInformation() != loop) {
Aart Bik7dc96932016-10-12 10:01:05 -0700125 continue; // Inner loops visited later.
Aart Bik30efb4e2015-07-30 12:14:31 -0700126 }
127 // Visit phi-operations and instructions.
128 for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) {
129 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700130 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700131 VisitNode(loop, instruction);
132 }
133 }
134 for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) {
135 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700136 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700137 VisitNode(loop, instruction);
138 }
139 }
140 }
141
Aart Bike609b7c2015-08-27 13:46:58 -0700142 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700143 map_.clear();
Aart Bikd14c5952015-09-08 15:25:15 -0700144
Aart Bik78296912016-03-25 13:14:53 -0700145 // Determine the loop's trip-count.
Aart Bikd14c5952015-09-08 15:25:15 -0700146 VisitControl(loop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700147}
148
149void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700150 const uint32_t d1 = ++global_depth_;
Aart Bike609b7c2015-08-27 13:46:58 -0700151 map_.Put(instruction, NodeInfo(d1));
Aart Bik30efb4e2015-07-30 12:14:31 -0700152 stack_.push_back(instruction);
153
154 // Visit all descendants.
155 uint32_t low = d1;
Vladimir Marko372f10e2016-05-17 16:30:10 +0100156 for (HInstruction* input : instruction->GetInputs()) {
157 low = std::min(low, VisitDescendant(loop, input));
Aart Bik30efb4e2015-07-30 12:14:31 -0700158 }
159
160 // Lower or found SCC?
161 if (low < d1) {
Aart Bike609b7c2015-08-27 13:46:58 -0700162 map_.find(instruction)->second.depth = low;
Aart Bik30efb4e2015-07-30 12:14:31 -0700163 } else {
164 scc_.clear();
165 cycle_.clear();
166
167 // Pop the stack to build the SCC for classification.
168 while (!stack_.empty()) {
169 HInstruction* x = stack_.back();
170 scc_.push_back(x);
171 stack_.pop_back();
Aart Bike609b7c2015-08-27 13:46:58 -0700172 map_.find(x)->second.done = true;
Aart Bik30efb4e2015-07-30 12:14:31 -0700173 if (x == instruction) {
174 break;
175 }
176 }
177
Aart Bik0d345cf2016-03-16 10:49:38 -0700178 // Type of induction.
179 type_ = scc_[0]->GetType();
180
Aart Bik30efb4e2015-07-30 12:14:31 -0700181 // Classify the SCC.
Aart Bikf475bee2015-09-16 12:50:25 -0700182 if (scc_.size() == 1 && !scc_[0]->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700183 ClassifyTrivial(loop, scc_[0]);
184 } else {
185 ClassifyNonTrivial(loop);
186 }
187
188 scc_.clear();
189 cycle_.clear();
190 }
191}
192
193uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) {
194 // If the definition is either outside the loop (loop invariant entry value)
195 // or assigned in inner loop (inner exit value), the traversal stops.
196 HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation();
197 if (otherLoop != loop) {
198 return global_depth_;
199 }
200
201 // Inspect descendant node.
Aart Bike609b7c2015-08-27 13:46:58 -0700202 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700203 VisitNode(loop, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700204 return map_.find(instruction)->second.depth;
Aart Bik30efb4e2015-07-30 12:14:31 -0700205 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700206 auto it = map_.find(instruction);
Aart Bik30efb4e2015-07-30 12:14:31 -0700207 return it->second.done ? global_depth_ : it->second.depth;
208 }
209}
210
211void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) {
212 InductionInfo* info = nullptr;
213 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700214 info = TransferPhi(loop, instruction, /* input_index */ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700215 } else if (instruction->IsAdd()) {
216 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
217 LookupInfo(loop, instruction->InputAt(1)), kAdd);
218 } else if (instruction->IsSub()) {
219 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
220 LookupInfo(loop, instruction->InputAt(1)), kSub);
Aart Bikc071a012016-12-01 10:22:31 -0800221 } else if (instruction->IsNeg()) {
222 info = TransferNeg(LookupInfo(loop, instruction->InputAt(0)));
Aart Bik30efb4e2015-07-30 12:14:31 -0700223 } else if (instruction->IsMul()) {
224 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
225 LookupInfo(loop, instruction->InputAt(1)));
Aart Bike609b7c2015-08-27 13:46:58 -0700226 } else if (instruction->IsShl()) {
Aart Bikc071a012016-12-01 10:22:31 -0800227 HInstruction* mulc = GetMultConstantForShift(loop, instruction);
228 if (mulc != nullptr) {
229 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
230 LookupInfo(loop, mulc));
231 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700232 } else if (instruction->IsTypeConversion()) {
233 info = TransferCnv(LookupInfo(loop, instruction->InputAt(0)),
234 instruction->AsTypeConversion()->GetInputType(),
235 instruction->AsTypeConversion()->GetResultType());
Aart Bike609b7c2015-08-27 13:46:58 -0700236 } else if (instruction->IsBoundsCheck()) {
237 info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through.
Aart Bik30efb4e2015-07-30 12:14:31 -0700238 }
239
240 // Successfully classified?
241 if (info != nullptr) {
242 AssignInfo(loop, instruction, info);
243 }
244}
245
246void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) {
247 const size_t size = scc_.size();
Aart Bike609b7c2015-08-27 13:46:58 -0700248 DCHECK_GE(size, 1u);
Aart Bik22af3be2015-09-10 12:50:58 -0700249
Aart Bikcc42be02016-10-20 16:14:16 -0700250 // Rotate proper loop-phi to front.
Aart Bik22af3be2015-09-10 12:50:58 -0700251 if (size > 1) {
Vladimir Marko5233f932015-09-29 19:01:15 +0100252 ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700253 RotateEntryPhiFirst(loop, &scc_, &other);
254 }
255
Aart Bikcc42be02016-10-20 16:14:16 -0700256 // Analyze from loop-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700257 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700258 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700259 return;
260 }
Aart Bikf475bee2015-09-16 12:50:25 -0700261
262 // External link should be loop invariant.
263 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700264 if (initial == nullptr || initial->induction_class != kInvariant) {
265 return;
266 }
267
Aart Bikcc42be02016-10-20 16:14:16 -0700268 // Store interesting cycle.
269 AssignCycle(phi->AsPhi());
270
Aart Bikf475bee2015-09-16 12:50:25 -0700271 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700272 if (size == 1) {
Aart Bikf475bee2015-09-16 12:50:25 -0700273 InductionInfo* update = TransferPhi(loop, phi, /* input_index */ 1);
Aart Bik30efb4e2015-07-30 12:14:31 -0700274 if (update != nullptr) {
Aart Bikc071a012016-12-01 10:22:31 -0800275 AssignInfo(loop, phi, CreateInduction(kWrapAround,
276 kNop,
277 initial,
278 update,
279 /*fetch*/ nullptr,
280 type_));
Aart Bik30efb4e2015-07-30 12:14:31 -0700281 }
282 return;
283 }
284
285 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700286 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700287 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700288 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700289 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700290 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700291 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700292 } else if (instruction->IsAdd()) {
293 update = SolveAddSub(
294 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
295 } else if (instruction->IsSub()) {
296 update = SolveAddSub(
297 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bikc071a012016-12-01 10:22:31 -0800298 } else if (instruction->IsMul()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800299 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800300 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kMul);
301 } else if (instruction->IsDiv()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800302 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800303 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kDiv);
304 } else if (instruction->IsRem()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800305 update = SolveOp(
306 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kRem);
Aart Bikc071a012016-12-01 10:22:31 -0800307 } else if (instruction->IsShl()) {
308 HInstruction* mulc = GetMultConstantForShift(loop, instruction);
309 if (mulc != nullptr) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800310 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), mulc, kMul);
Aart Bikc071a012016-12-01 10:22:31 -0800311 }
Aart Bik7dc96932016-10-12 10:01:05 -0700312 } else if (instruction->IsXor()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800313 update = SolveOp(
314 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700315 } else if (instruction->IsEqual()) {
316 update = SolveTest(loop, phi, instruction, 0);
317 } else if (instruction->IsNotEqual()) {
318 update = SolveTest(loop, phi, instruction, 1);
Aart Bik0d345cf2016-03-16 10:49:38 -0700319 } else if (instruction->IsTypeConversion()) {
320 update = SolveCnv(instruction->AsTypeConversion());
Aart Bik30efb4e2015-07-30 12:14:31 -0700321 }
322 if (update == nullptr) {
323 return;
324 }
Aart Bike609b7c2015-08-27 13:46:58 -0700325 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700326 }
327
Aart Bikf475bee2015-09-16 12:50:25 -0700328 // Success if all internal links received the same temporary meaning.
329 InductionInfo* induction = SolvePhi(phi, /* input_index */ 1);
330 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700331 switch (induction->induction_class) {
332 case kInvariant:
Aart Bikc071a012016-12-01 10:22:31 -0800333 // Construct combined stride of the linear induction.
334 induction = CreateInduction(kLinear, kNop, induction, initial, /*fetch*/ nullptr, type_);
335 FALLTHROUGH_INTENDED;
336 case kPolynomial:
337 case kGeometric:
Aart Bikdf7822e2016-12-06 10:05:30 -0800338 case kWrapAround:
Aart Bik22af3be2015-09-10 12:50:58 -0700339 // Classify first phi and then the rest of the cycle "on-demand".
340 // Statements are scanned in order.
Aart Bikc071a012016-12-01 10:22:31 -0800341 AssignInfo(loop, phi, induction);
Aart Bik22af3be2015-09-10 12:50:58 -0700342 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700343 ClassifyTrivial(loop, scc_[i]);
344 }
345 break;
346 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700347 // Classify all elements in the cycle with the found periodic induction while
348 // rotating each first element to the end. Lastly, phi is classified.
349 // Statements are scanned in reverse order.
350 for (size_t i = size - 1; i >= 1; i--) {
351 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700352 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
353 }
354 AssignInfo(loop, phi, induction);
355 break;
356 default:
357 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700358 }
359 }
360}
361
Aart Bike609b7c2015-08-27 13:46:58 -0700362HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
363 InductionInfo* induction,
364 InductionInfo* last) {
365 // Rotates a periodic induction of the form
366 // (a, b, c, d, e)
367 // into
368 // (b, c, d, e, a)
369 // in preparation of assigning this to the previous variable in the sequence.
370 if (induction->induction_class == kInvariant) {
Aart Bikc071a012016-12-01 10:22:31 -0800371 return CreateInduction(kPeriodic,
372 kNop,
373 induction,
374 last,
375 /*fetch*/ nullptr,
376 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700377 }
Aart Bikc071a012016-12-01 10:22:31 -0800378 return CreateInduction(kPeriodic,
379 kNop,
380 induction->op_a,
381 RotatePeriodicInduction(induction->op_b, last),
382 /*fetch*/ nullptr,
383 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700384}
385
Aart Bikf475bee2015-09-16 12:50:25 -0700386HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
387 HInstruction* phi,
388 size_t input_index) {
389 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100390 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100391 DCHECK_LT(input_index, inputs.size());
392 InductionInfo* a = LookupInfo(loop, inputs[input_index]);
393 for (size_t i = input_index + 1; i < inputs.size(); i++) {
394 InductionInfo* b = LookupInfo(loop, inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700395 if (!InductionEqual(a, b)) {
396 return nullptr;
397 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700398 }
Aart Bikf475bee2015-09-16 12:50:25 -0700399 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700400}
401
402HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
403 InductionInfo* b,
404 InductionOp op) {
Aart Bikc071a012016-12-01 10:22:31 -0800405 // Transfer over an addition or subtraction: any invariant, linear, polynomial, geometric,
406 // wrap-around, or periodic can be combined with an invariant to yield a similar result.
Aart Bikdf7822e2016-12-06 10:05:30 -0800407 // Two linear or two polynomial inputs can be combined too. Other combinations fail.
Aart Bik30efb4e2015-07-30 12:14:31 -0700408 if (a != nullptr && b != nullptr) {
Aart Bikcc42be02016-10-20 16:14:16 -0700409 type_ = Narrowest(type_, Narrowest(a->type, b->type));
Aart Bik30efb4e2015-07-30 12:14:31 -0700410 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700411 return CreateInvariantOp(op, a, b);
Aart Bikdf7822e2016-12-06 10:05:30 -0800412 } else if ((a->induction_class == kLinear && b->induction_class == kLinear) ||
413 (a->induction_class == kPolynomial && b->induction_class == kPolynomial)) {
414 return CreateInduction(a->induction_class,
415 a->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700416 TransferAddSub(a->op_a, b->op_a, op),
417 TransferAddSub(a->op_b, b->op_b, op),
Aart Bikc071a012016-12-01 10:22:31 -0800418 /*fetch*/ nullptr,
Aart Bik0d345cf2016-03-16 10:49:38 -0700419 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700420 } else if (a->induction_class == kInvariant) {
421 InductionInfo* new_a = b->op_a;
422 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800423 if (b->induction_class == kWrapAround || b->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700424 new_a = TransferAddSub(a, new_a, op);
425 } else if (op == kSub) { // Negation required.
426 new_a = TransferNeg(new_a);
427 }
Aart Bikc071a012016-12-01 10:22:31 -0800428 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700429 } else if (b->induction_class == kInvariant) {
430 InductionInfo* new_a = a->op_a;
431 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800432 if (a->induction_class == kWrapAround || a->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700433 new_a = TransferAddSub(new_a, b, op);
434 }
Aart Bikc071a012016-12-01 10:22:31 -0800435 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
436 }
437 }
438 return nullptr;
439}
440
441HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
442 // Transfer over a unary negation: an invariant, linear, polynomial, geometric (mul),
443 // wrap-around, or periodic input yields a similar but negated induction as result.
444 if (a != nullptr) {
445 type_ = Narrowest(type_, a->type);
446 if (a->induction_class == kInvariant) {
447 return CreateInvariantOp(kNeg, nullptr, a);
448 } else if (a->induction_class != kGeometric || a->operation == kMul) {
449 return CreateInduction(a->induction_class,
450 a->operation,
451 TransferNeg(a->op_a),
452 TransferNeg(a->op_b),
453 a->fetch,
454 type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700455 }
456 }
457 return nullptr;
458}
459
460HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
461 InductionInfo* b) {
Aart Bikc071a012016-12-01 10:22:31 -0800462 // Transfer over a multiplication: any invariant, linear, polynomial, geometric (mul),
463 // wrap-around, or periodic can be multiplied with an invariant to yield a similar
464 // but multiplied result. Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700465 if (a != nullptr && b != nullptr) {
Aart Bikcc42be02016-10-20 16:14:16 -0700466 type_ = Narrowest(type_, Narrowest(a->type, b->type));
Aart Bik30efb4e2015-07-30 12:14:31 -0700467 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700468 return CreateInvariantOp(kMul, a, b);
Aart Bikc071a012016-12-01 10:22:31 -0800469 } else if (a->induction_class == kInvariant && (b->induction_class != kGeometric ||
470 b->operation == kMul)) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700471 return CreateInduction(b->induction_class,
Aart Bikc071a012016-12-01 10:22:31 -0800472 b->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700473 TransferMul(a, b->op_a),
474 TransferMul(a, b->op_b),
Aart Bikc071a012016-12-01 10:22:31 -0800475 b->fetch,
Aart Bik0d345cf2016-03-16 10:49:38 -0700476 type_);
Aart Bikc071a012016-12-01 10:22:31 -0800477 } else if (b->induction_class == kInvariant && (a->induction_class != kGeometric ||
478 a->operation == kMul)) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700479 return CreateInduction(a->induction_class,
Aart Bikc071a012016-12-01 10:22:31 -0800480 a->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700481 TransferMul(a->op_a, b),
482 TransferMul(a->op_b, b),
Aart Bikc071a012016-12-01 10:22:31 -0800483 a->fetch,
Aart Bik0d345cf2016-03-16 10:49:38 -0700484 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700485 }
486 }
487 return nullptr;
488}
489
Aart Bik0d345cf2016-03-16 10:49:38 -0700490HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferCnv(InductionInfo* a,
491 Primitive::Type from,
492 Primitive::Type to) {
493 if (a != nullptr) {
Aart Bikc071a012016-12-01 10:22:31 -0800494 // Allow narrowing conversion on linear induction in certain cases.
Aart Bik0d345cf2016-03-16 10:49:38 -0700495 if (IsNarrowingIntegralConversion(from, to)) {
496 if (a->induction_class == kLinear) {
497 if (a->type == to || (a->type == from && IsNarrowingIntegralConversion(from, to))) {
Aart Bikc071a012016-12-01 10:22:31 -0800498 return CreateInduction(kLinear, kNop, a->op_a, a->op_b, /*fetch*/ nullptr, to);
Aart Bik0d345cf2016-03-16 10:49:38 -0700499 }
500 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700501 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700502 }
503 return nullptr;
504}
505
Aart Bikf475bee2015-09-16 12:50:25 -0700506HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
507 size_t input_index) {
508 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100509 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100510 DCHECK_LT(input_index, inputs.size());
511 auto ita = cycle_.find(inputs[input_index]);
Aart Bik30efb4e2015-07-30 12:14:31 -0700512 if (ita != cycle_.end()) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100513 for (size_t i = input_index + 1; i < inputs.size(); i++) {
514 auto itb = cycle_.find(inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700515 if (itb == cycle_.end() ||
516 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700517 return nullptr;
518 }
519 }
Aart Bikf475bee2015-09-16 12:50:25 -0700520 return ita->second;
521 }
522 return nullptr;
523}
524
525HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
526 HLoopInformation* loop,
527 HInstruction* entry_phi,
528 HInstruction* phi) {
529 // Match all phi inputs.
530 InductionInfo* match = SolvePhi(phi, /* input_index */ 0);
531 if (match != nullptr) {
532 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700533 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700534
Aart Bikf475bee2015-09-16 12:50:25 -0700535 // Otherwise, try to solve for a periodic seeded from phi onward.
536 // Only tight multi-statement cycles are considered in order to
537 // simplify rotating the periodic during the final classification.
538 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
539 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700540 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700541 if (phi->InputAt(1) == entry_phi) {
542 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800543 return CreateInduction(kPeriodic, kNop, a, initial, /*fetch*/ nullptr, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700544 }
Aart Bikf475bee2015-09-16 12:50:25 -0700545 InductionInfo* b = SolvePhi(phi, /* input_index */ 1);
546 if (b != nullptr && b->induction_class == kPeriodic) {
Aart Bikc071a012016-12-01 10:22:31 -0800547 return CreateInduction(kPeriodic, kNop, a, b, /*fetch*/ nullptr, type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700548 }
549 }
550 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700551 return nullptr;
552}
553
Aart Bike609b7c2015-08-27 13:46:58 -0700554HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700555 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700556 HInstruction* instruction,
557 HInstruction* x,
558 HInstruction* y,
559 InductionOp op,
560 bool is_first_call) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800561 // Solve within a cycle over an addition or subtraction.
Aart Bike609b7c2015-08-27 13:46:58 -0700562 InductionInfo* b = LookupInfo(loop, y);
Aart Bikdf7822e2016-12-06 10:05:30 -0800563 if (b != nullptr) {
564 if (b->induction_class == kInvariant) {
565 // Adding or subtracting an invariant value, seeded from phi,
566 // keeps adding to the stride of the linear induction.
567 if (x == entry_phi) {
568 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
569 }
570 auto it = cycle_.find(x);
571 if (it != cycle_.end()) {
572 InductionInfo* a = it->second;
573 if (a->induction_class == kInvariant) {
574 return CreateInvariantOp(op, a, b);
575 }
576 }
577 } else if (op == kAdd && b->induction_class == kLinear) {
578 // Solve within a tight cycle that adds a term that is already classified as a linear
579 // induction for a polynomial induction k = k + i (represented as sum over linear terms).
580 if (x == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
581 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
582 return CreateInduction(kPolynomial,
583 kNop,
584 b,
585 initial,
586 /*fetch*/ nullptr,
587 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700588 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700589 }
590 }
Aart Bike609b7c2015-08-27 13:46:58 -0700591
592 // Try some alternatives before failing.
593 if (op == kAdd) {
594 // Try the other way around for an addition if considered for first time.
595 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700596 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700597 }
598 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700599 // Solve within a tight cycle that is formed by exactly two instructions,
Aart Bikc071a012016-12-01 10:22:31 -0800600 // one phi and one update, for a periodic idiom of the form k = c - k.
Aart Bikf475bee2015-09-16 12:50:25 -0700601 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700602 InductionInfo* a = LookupInfo(loop, x);
603 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700604 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800605 return CreateInduction(kPeriodic,
606 kNop,
607 CreateInvariantOp(kSub, a, initial),
608 initial,
609 /*fetch*/ nullptr,
610 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700611 }
612 }
613 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700614 return nullptr;
615}
616
Aart Bikdf7822e2016-12-06 10:05:30 -0800617HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveOp(HLoopInformation* loop,
Aart Bikc071a012016-12-01 10:22:31 -0800618 HInstruction* entry_phi,
619 HInstruction* instruction,
620 HInstruction* x,
621 HInstruction* y,
622 InductionOp op) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800623 // Solve within a tight cycle for a binary operation k = k op c or, for some op, k = c op k.
Aart Bik639cc8c2016-10-18 13:03:31 -0700624 if (entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800625 InductionInfo* c = nullptr;
Aart Bik639cc8c2016-10-18 13:03:31 -0700626 InductionInfo* b = LookupInfo(loop, y);
627 if (b != nullptr && b->induction_class == kInvariant && entry_phi == x) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800628 c = b;
629 } else if (op != kDiv && op != kRem) {
630 InductionInfo* a = LookupInfo(loop, x);
631 if (a != nullptr && a->induction_class == kInvariant && entry_phi == y) {
632 c = a;
633 }
634 }
635 // Found suitable operand left or right?
636 if (c != nullptr) {
637 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
638 switch (op) {
639 case kMul:
640 case kDiv:
641 // Restrict base of geometric induction to direct fetch.
642 if (c->operation == kFetch) {
643 return CreateInduction(kGeometric,
644 op,
645 initial,
646 CreateConstant(0, type_),
647 c->fetch,
648 type_);
649 };
650 break;
651 case kRem:
652 // Idiomatic MOD wrap-around induction.
653 return CreateInduction(kWrapAround,
654 kNop,
655 initial,
656 CreateInvariantOp(kRem, initial, c),
657 /*fetch*/ nullptr,
658 type_);
659 case kXor:
660 // Idiomatic XOR periodic induction.
661 return CreateInduction(kPeriodic,
662 kNop,
663 CreateInvariantOp(kXor, initial, c),
664 initial,
665 /*fetch*/ nullptr,
666 type_);
667 default:
668 CHECK(false) << op;
669 break;
670 }
Aart Bik7dc96932016-10-12 10:01:05 -0700671 }
672 }
Aart Bik639cc8c2016-10-18 13:03:31 -0700673 return nullptr;
674}
675
676HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveTest(HLoopInformation* loop,
677 HInstruction* entry_phi,
678 HInstruction* instruction,
679 int64_t opposite_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800680 // Detect hidden XOR construction in x = (x == false) or x = (x != true).
Aart Bik639cc8c2016-10-18 13:03:31 -0700681 int64_t value = -1;
682 HInstruction* x = instruction->InputAt(0);
683 HInstruction* y = instruction->InputAt(1);
684 if (IsExact(LookupInfo(loop, x), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800685 return SolveOp(loop, entry_phi, instruction, graph_->GetIntConstant(1), y, kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700686 } else if (IsExact(LookupInfo(loop, y), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800687 return SolveOp(loop, entry_phi, instruction, x, graph_->GetIntConstant(1), kXor);
Aart Bik7dc96932016-10-12 10:01:05 -0700688 }
689 return nullptr;
690}
691
Aart Bik0d345cf2016-03-16 10:49:38 -0700692HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveCnv(HTypeConversion* conversion) {
693 Primitive::Type from = conversion->GetInputType();
694 Primitive::Type to = conversion->GetResultType();
695 // A narrowing conversion is allowed within the cycle of a linear induction, provided that the
696 // narrowest encountered type is recorded with the induction to account for the precision loss.
697 if (IsNarrowingIntegralConversion(from, to)) {
698 auto it = cycle_.find(conversion->GetInput());
699 if (it != cycle_.end() && it->second->induction_class == kInvariant) {
700 type_ = Narrowest(type_, to);
701 return it->second;
702 }
703 }
704 return nullptr;
705}
706
Aart Bikd14c5952015-09-08 15:25:15 -0700707void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
708 HInstruction* control = loop->GetHeader()->GetLastInstruction();
709 if (control->IsIf()) {
710 HIf* ifs = control->AsIf();
711 HBasicBlock* if_true = ifs->IfTrueSuccessor();
712 HBasicBlock* if_false = ifs->IfFalseSuccessor();
713 HInstruction* if_expr = ifs->InputAt(0);
714 // Determine if loop has following structure in header.
715 // loop-header: ....
716 // if (condition) goto X
717 if (if_expr->IsCondition()) {
718 HCondition* condition = if_expr->AsCondition();
719 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
720 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
721 Primitive::Type type = condition->InputAt(0)->GetType();
Aart Bik0d345cf2016-03-16 10:49:38 -0700722 // Determine if the loop control uses a known sequence on an if-exit (X outside) or on
723 // an if-iterate (X inside), expressed as if-iterate when passed into VisitCondition().
724 if (a == nullptr || b == nullptr) {
725 return; // Loop control is not a sequence.
Aart Bikd14c5952015-09-08 15:25:15 -0700726 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
727 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
728 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
729 VisitCondition(loop, a, b, type, condition->GetCondition());
730 }
731 }
732 }
733}
734
735void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
736 InductionInfo* a,
737 InductionInfo* b,
738 Primitive::Type type,
739 IfCondition cmp) {
740 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700741 // 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 -0700742 switch (cmp) {
743 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
744 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
745 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
746 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700747 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700748 default: break;
749 }
750 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700751 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700752 InductionInfo* lower_expr = a->op_b;
753 InductionInfo* upper_expr = b;
Aart Bik97412c922016-02-19 20:14:38 -0800754 InductionInfo* stride_expr = a->op_a;
755 // Constant stride?
Aart Bik9401f532015-09-28 16:25:56 -0700756 int64_t stride_value = 0;
Aart Bik97412c922016-02-19 20:14:38 -0800757 if (!IsExact(stride_expr, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700758 return;
759 }
Aart Bik358af832016-02-24 14:17:53 -0800760 // Rewrite condition i != U into strict end condition i < U or i > U if this end condition
761 // is reached exactly (tested by verifying if the loop has a unit stride and the non-strict
762 // condition would be always taken).
763 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLE)) ||
764 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGE)))) {
Aart Bik9401f532015-09-28 16:25:56 -0700765 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700766 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700767 // Only accept integral condition. A mismatch between the type of condition and the induction
768 // is only allowed if the, necessarily narrower, induction range fits the narrower control.
769 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
770 return; // not integral
771 } else if (type != a->type &&
772 !FitsNarrowerControl(lower_expr, upper_expr, stride_value, a->type, cmp)) {
773 return; // mismatched type
774 }
Aart Bikf475bee2015-09-16 12:50:25 -0700775 // Normalize a linear loop control with a nonzero stride:
776 // stride > 0, either i < U or i <= U
777 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700778 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
779 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik97412c922016-02-19 20:14:38 -0800780 VisitTripCount(loop, lower_expr, upper_expr, stride_expr, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700781 }
Aart Bikd14c5952015-09-08 15:25:15 -0700782 }
783}
784
785void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700786 InductionInfo* lower_expr,
787 InductionInfo* upper_expr,
Aart Bik97412c922016-02-19 20:14:38 -0800788 InductionInfo* stride_expr,
Aart Bik9401f532015-09-28 16:25:56 -0700789 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700790 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700791 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700792 // Any loop of the general form:
793 //
794 // for (i = L; i <= U; i += S) // S > 0
795 // or for (i = L; i >= U; i += S) // S < 0
796 // .. i ..
797 //
798 // can be normalized into:
799 //
800 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
801 // .. L + S * n ..
802 //
Aart Bik9401f532015-09-28 16:25:56 -0700803 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700804 //
Aart Bik9401f532015-09-28 16:25:56 -0700805 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
806 // an unsigned entity, for example, as in the following loop that uses the full range:
807 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
808 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
Aart Bikd5cc6832016-06-22 16:34:46 -0700809 // for (int i = 12; i < U; i++) // TC = 0 when U <= 12
Aart Bik9401f532015-09-28 16:25:56 -0700810 // If this cannot be determined at compile-time, the TC is only valid within the
Aart Bik22f05872015-10-27 15:56:28 -0700811 // loop-body proper, not the loop-header unless enforced with an explicit taken-test.
Aart Bik9401f532015-09-28 16:25:56 -0700812 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
813 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
814 // If this cannot be determined at compile-time, the TC is only valid when enforced
Aart Bik22f05872015-10-27 15:56:28 -0700815 // with an explicit finite-test.
Aart Bik9401f532015-09-28 16:25:56 -0700816 // (4) For loops which early-exits, the TC forms an upper bound, as in:
817 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
Aart Bik22f05872015-10-27 15:56:28 -0700818 InductionInfo* trip_count = upper_expr;
Aart Bik9401f532015-09-28 16:25:56 -0700819 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
820 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
821 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700822 if (!cancels) {
823 // Convert exclusive integral inequality into inclusive integral inequality,
824 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700825 if (cmp == kCondLT) {
Aart Bik22f05872015-10-27 15:56:28 -0700826 trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700827 } else if (cmp == kCondGT) {
Aart Bik22f05872015-10-27 15:56:28 -0700828 trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700829 }
830 // Compensate for stride.
Aart Bik97412c922016-02-19 20:14:38 -0800831 trip_count = CreateInvariantOp(kAdd, trip_count, stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700832 }
Aart Bik97412c922016-02-19 20:14:38 -0800833 trip_count = CreateInvariantOp(
834 kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700835 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700836 // should be aware that the expression is only valid under the conditions listed above.
Aart Bik22f05872015-10-27 15:56:28 -0700837 InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests
Aart Bik9401f532015-09-28 16:25:56 -0700838 if (is_taken && is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700839 tcKind = kTripCountInLoop; // needs neither test
Aart Bik9401f532015-09-28 16:25:56 -0700840 } else if (is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700841 tcKind = kTripCountInBody; // needs taken-test
Aart Bik9401f532015-09-28 16:25:56 -0700842 } else if (is_taken) {
Aart Bik22f05872015-10-27 15:56:28 -0700843 tcKind = kTripCountInLoopUnsafe; // needs finite-test
Aart Bik9401f532015-09-28 16:25:56 -0700844 }
Aart Bik22f05872015-10-27 15:56:28 -0700845 InductionOp op = kNop;
846 switch (cmp) {
847 case kCondLT: op = kLT; break;
848 case kCondLE: op = kLE; break;
849 case kCondGT: op = kGT; break;
850 case kCondGE: op = kGE; break;
851 default: LOG(FATAL) << "CONDITION UNREACHABLE";
852 }
Aart Bik009cace2016-09-16 10:15:19 -0700853 // Associate trip count with control instruction, rather than the condition (even
854 // though it's its use) since former provides a convenient use-free placeholder.
855 HInstruction* control = loop->GetHeader()->GetLastInstruction();
Aart Bik22f05872015-10-27 15:56:28 -0700856 InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr);
Aart Bik009cace2016-09-16 10:15:19 -0700857 DCHECK(control->IsIf());
858 AssignInfo(loop, control, CreateTripCount(tcKind, trip_count, taken_test, type));
Aart Bik9401f532015-09-28 16:25:56 -0700859}
860
861bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
862 InductionInfo* upper_expr,
863 IfCondition cmp) {
864 int64_t lower_value;
865 int64_t upper_value;
Aart Bik97412c922016-02-19 20:14:38 -0800866 switch (cmp) {
867 case kCondLT:
868 return IsAtMost(lower_expr, &lower_value)
869 && IsAtLeast(upper_expr, &upper_value)
870 && lower_value < upper_value;
871 case kCondLE:
872 return IsAtMost(lower_expr, &lower_value)
873 && IsAtLeast(upper_expr, &upper_value)
874 && lower_value <= upper_value;
875 case kCondGT:
876 return IsAtLeast(lower_expr, &lower_value)
877 && IsAtMost(upper_expr, &upper_value)
878 && lower_value > upper_value;
879 case kCondGE:
880 return IsAtLeast(lower_expr, &lower_value)
881 && IsAtMost(upper_expr, &upper_value)
882 && lower_value >= upper_value;
883 default:
884 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700885 }
886 return false; // not certain, may be untaken
887}
888
889bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
890 int64_t stride_value,
891 Primitive::Type type,
892 IfCondition cmp) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700893 const int64_t min = Primitive::MinValueOfIntegralType(type);
894 const int64_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik9401f532015-09-28 16:25:56 -0700895 // Some rules under which it is certain at compile-time that the loop is finite.
896 int64_t value;
897 switch (cmp) {
898 case kCondLT:
899 return stride_value == 1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800900 (IsAtMost(upper_expr, &value) && value <= (max - stride_value + 1));
Aart Bik9401f532015-09-28 16:25:56 -0700901 case kCondLE:
Aart Bik97412c922016-02-19 20:14:38 -0800902 return (IsAtMost(upper_expr, &value) && value <= (max - stride_value));
Aart Bik9401f532015-09-28 16:25:56 -0700903 case kCondGT:
904 return stride_value == -1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800905 (IsAtLeast(upper_expr, &value) && value >= (min - stride_value - 1));
Aart Bik9401f532015-09-28 16:25:56 -0700906 case kCondGE:
Aart Bik97412c922016-02-19 20:14:38 -0800907 return (IsAtLeast(upper_expr, &value) && value >= (min - stride_value));
Aart Bike9f37602015-10-09 11:15:55 -0700908 default:
909 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700910 }
911 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700912}
913
Aart Bik0d345cf2016-03-16 10:49:38 -0700914bool HInductionVarAnalysis::FitsNarrowerControl(InductionInfo* lower_expr,
915 InductionInfo* upper_expr,
916 int64_t stride_value,
917 Primitive::Type type,
918 IfCondition cmp) {
919 int64_t min = Primitive::MinValueOfIntegralType(type);
920 int64_t max = Primitive::MaxValueOfIntegralType(type);
921 // Inclusive test need one extra.
922 if (stride_value != 1 && stride_value != -1) {
923 return false; // non-unit stride
924 } else if (cmp == kCondLE) {
925 max--;
926 } else if (cmp == kCondGE) {
927 min++;
928 }
929 // Do both bounds fit the range?
Vladimir Marko0e2f2ff2016-03-22 12:31:54 +0000930 // Note: The `value` is initialized to please valgrind - the compiler can reorder
931 // the return value check with the `value` check, b/27651442 .
932 int64_t value = 0;
Aart Bik0d345cf2016-03-16 10:49:38 -0700933 return IsAtLeast(lower_expr, &value) && value >= min &&
934 IsAtMost(lower_expr, &value) && value <= max &&
935 IsAtLeast(upper_expr, &value) && value >= min &&
936 IsAtMost(upper_expr, &value) && value <= max;
937}
938
Aart Bik30efb4e2015-07-30 12:14:31 -0700939void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
940 HInstruction* instruction,
941 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700942 auto it = induction_.find(loop);
943 if (it == induction_.end()) {
944 it = induction_.Put(loop,
945 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100946 std::less<HInstruction*>(),
947 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700948 }
949 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700950}
951
Aart Bike609b7c2015-08-27 13:46:58 -0700952HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
953 HInstruction* instruction) {
954 auto it = induction_.find(loop);
955 if (it != induction_.end()) {
956 auto loop_it = it->second.find(instruction);
957 if (loop_it != it->second.end()) {
958 return loop_it->second;
959 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700960 }
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800961 if (loop->IsDefinedOutOfTheLoop(instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -0700962 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700963 AssignInfo(loop, instruction, info);
964 return info;
965 }
966 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -0700967}
968
Aart Bikd14c5952015-09-08 15:25:15 -0700969HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
970 Primitive::Type type) {
Aart Bikc071a012016-12-01 10:22:31 -0800971 HInstruction* constant;
972 switch (type) {
973 case Primitive::kPrimDouble: constant = graph_->GetDoubleConstant(value); break;
974 case Primitive::kPrimFloat: constant = graph_->GetFloatConstant(value); break;
975 case Primitive::kPrimLong: constant = graph_->GetLongConstant(value); break;
976 default: constant = graph_->GetIntConstant(value); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700977 }
Aart Bikc071a012016-12-01 10:22:31 -0800978 return CreateInvariantFetch(constant);
Aart Bikd14c5952015-09-08 15:25:15 -0700979}
980
Aart Bik471a2032015-09-04 18:22:11 -0700981HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
982 InductionOp op,
983 InductionInfo* a,
984 InductionInfo* b) {
985 // Perform some light-weight simplifications during construction of a new invariant.
986 // This often safes memory and yields a more concise representation of the induction.
987 // More exhaustive simplifications are done by later phases once induction nodes are
988 // translated back into HIR code (e.g. by loop optimizations or BCE).
989 int64_t value = -1;
Aart Bik97412c922016-02-19 20:14:38 -0800990 if (IsExact(a, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -0700991 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -0700992 // Simplify 0 + b = b, 0 ^ b = b, 0 * b = 0.
993 if (op == kAdd || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -0700994 return b;
995 } else if (op == kMul) {
996 return a;
997 }
Aart Bikd14c5952015-09-08 15:25:15 -0700998 } else if (op == kMul) {
999 // Simplify 1 * b = b, -1 * b = -b
1000 if (value == 1) {
1001 return b;
1002 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001003 return CreateSimplifiedInvariant(kNeg, nullptr, b);
Aart Bikd14c5952015-09-08 15:25:15 -07001004 }
Aart Bik471a2032015-09-04 18:22:11 -07001005 }
1006 }
Aart Bik97412c922016-02-19 20:14:38 -08001007 if (IsExact(b, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001008 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001009 // Simplify a + 0 = a, a - 0 = a, a ^ 0 = a, a * 0 = 0, -0 = 0.
1010 if (op == kAdd || op == kSub || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001011 return a;
1012 } else if (op == kMul || op == kNeg) {
1013 return b;
1014 }
Aart Bikd14c5952015-09-08 15:25:15 -07001015 } else if (op == kMul || op == kDiv) {
1016 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
1017 if (value == 1) {
1018 return a;
1019 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001020 return CreateSimplifiedInvariant(kNeg, nullptr, a);
Aart Bikd14c5952015-09-08 15:25:15 -07001021 }
Aart Bik471a2032015-09-04 18:22:11 -07001022 }
1023 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -07001024 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
1025 if (op == kAdd) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001026 return CreateSimplifiedInvariant(kSub, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001027 } else if (op == kSub) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001028 return CreateSimplifiedInvariant(kAdd, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001029 } else if (op == kNeg) {
1030 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -07001031 }
Aart Bik7d57d7f2015-12-09 14:39:48 -08001032 } else if (b->operation == kSub) {
1033 // Simplify - (a - b) = b - a.
1034 if (op == kNeg) {
1035 return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a);
1036 }
Aart Bik471a2032015-09-04 18:22:11 -07001037 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001038 return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr, b->type);
Aart Bik471a2032015-09-04 18:22:11 -07001039}
1040
Aart Bikc071a012016-12-01 10:22:31 -08001041HInstruction* HInductionVarAnalysis::GetMultConstantForShift(HLoopInformation* loop,
1042 HInstruction* instruction) {
1043 // Obtain the constant needed to treat shift as equivalent multiplication. This yields an
Aart Bikdf7822e2016-12-06 10:05:30 -08001044 // existing instruction if the constant is already there. Otherwise, this has a side effect
Aart Bikc071a012016-12-01 10:22:31 -08001045 // on the HIR. The restriction on the shift factor avoids generating a negative constant
1046 // (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that generalization for
1047 // shift factors outside [0,32) and [0,64) ranges is done by earlier simplification.
1048 InductionInfo* b = LookupInfo(loop, instruction->InputAt(1));
1049 int64_t value = -1;
1050 if (IsExact(b, &value)) {
1051 Primitive::Type type = instruction->InputAt(0)->GetType();
1052 if (type == Primitive::kPrimInt && 0 <= value && value < 31) {
1053 return graph_->GetIntConstant(1 << value);
1054 }
1055 if (type == Primitive::kPrimLong && 0 <= value && value < 63) {
1056 return graph_->GetLongConstant(1L << value);
1057 }
1058 }
1059 return nullptr;
1060}
Aart Bikcc42be02016-10-20 16:14:16 -07001061
1062void HInductionVarAnalysis::AssignCycle(HPhi* phi) {
1063 ArenaSet<HInstruction*>* set = &cycles_.Put(phi, ArenaSet<HInstruction*>(
1064 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)))->second;
1065 for (HInstruction* i : scc_) {
1066 set->insert(i);
1067 }
1068}
1069
1070ArenaSet<HInstruction*>* HInductionVarAnalysis::LookupCycle(HPhi* phi) {
1071 auto it = cycles_.find(phi);
1072 if (it != cycles_.end()) {
1073 return &it->second;
1074 }
1075 return nullptr;
1076}
1077
Aart Bik97412c922016-02-19 20:14:38 -08001078bool HInductionVarAnalysis::IsExact(InductionInfo* info, int64_t* value) {
1079 return InductionVarRange(this).IsConstant(info, InductionVarRange::kExact, value);
1080}
1081
1082bool HInductionVarAnalysis::IsAtMost(InductionInfo* info, int64_t* value) {
1083 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtMost, value);
1084}
1085
1086bool HInductionVarAnalysis::IsAtLeast(InductionInfo* info, int64_t* value) {
1087 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtLeast, value);
Aart Bik7d57d7f2015-12-09 14:39:48 -08001088}
1089
Aart Bik30efb4e2015-07-30 12:14:31 -07001090bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
1091 InductionInfo* info2) {
1092 // Test structural equality only, without accounting for simplifications.
1093 if (info1 != nullptr && info2 != nullptr) {
1094 return
1095 info1->induction_class == info2->induction_class &&
1096 info1->operation == info2->operation &&
1097 info1->fetch == info2->fetch &&
Aart Bik78296912016-03-25 13:14:53 -07001098 info1->type == info2->type &&
Aart Bik30efb4e2015-07-30 12:14:31 -07001099 InductionEqual(info1->op_a, info2->op_a) &&
1100 InductionEqual(info1->op_b, info2->op_b);
1101 }
1102 // Otherwise only two nullptrs are considered equal.
1103 return info1 == info2;
1104}
1105
Aart Bikc071a012016-12-01 10:22:31 -08001106std::string HInductionVarAnalysis::FetchToString(HInstruction* fetch) {
1107 DCHECK(fetch != nullptr);
1108 if (fetch->IsIntConstant()) {
1109 return std::to_string(fetch->AsIntConstant()->GetValue());
1110 } else if (fetch->IsLongConstant()) {
1111 return std::to_string(fetch->AsLongConstant()->GetValue());
1112 }
1113 return std::to_string(fetch->GetId()) + ":" + fetch->DebugName();
1114}
1115
Aart Bik30efb4e2015-07-30 12:14:31 -07001116std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
1117 if (info != nullptr) {
1118 if (info->induction_class == kInvariant) {
1119 std::string inv = "(";
1120 inv += InductionToString(info->op_a);
1121 switch (info->operation) {
Aart Bik22f05872015-10-27 15:56:28 -07001122 case kNop: inv += " @ "; break;
1123 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001124 case kSub:
Aart Bik22f05872015-10-27 15:56:28 -07001125 case kNeg: inv += " - "; break;
1126 case kMul: inv += " * "; break;
1127 case kDiv: inv += " / "; break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001128 case kRem: inv += " % "; break;
Aart Bik7dc96932016-10-12 10:01:05 -07001129 case kXor: inv += " ^ "; break;
Aart Bik22f05872015-10-27 15:56:28 -07001130 case kLT: inv += " < "; break;
1131 case kLE: inv += " <= "; break;
1132 case kGT: inv += " > "; break;
1133 case kGE: inv += " >= "; break;
Aart Bikc071a012016-12-01 10:22:31 -08001134 case kFetch: inv += FetchToString(info->fetch); break;
Aart Bik22f05872015-10-27 15:56:28 -07001135 case kTripCountInLoop: inv += " (TC-loop) "; break;
1136 case kTripCountInBody: inv += " (TC-body) "; break;
1137 case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break;
1138 case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001139 }
1140 inv += InductionToString(info->op_b);
Aart Bik0d345cf2016-03-16 10:49:38 -07001141 inv += ")";
1142 return inv;
Aart Bik30efb4e2015-07-30 12:14:31 -07001143 } else {
Aart Bik30efb4e2015-07-30 12:14:31 -07001144 if (info->induction_class == kLinear) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001145 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001146 return "(" + InductionToString(info->op_a) + " * i + " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001147 InductionToString(info->op_b) + "):" +
1148 Primitive::PrettyDescriptor(info->type);
Aart Bikc071a012016-12-01 10:22:31 -08001149 } else if (info->induction_class == kPolynomial) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001150 DCHECK(info->operation == kNop);
1151 return "poly(sum_lt(" + InductionToString(info->op_a) + ") + " +
Aart Bikc071a012016-12-01 10:22:31 -08001152 InductionToString(info->op_b) + "):" +
1153 Primitive::PrettyDescriptor(info->type);
1154 } else if (info->induction_class == kGeometric) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001155 DCHECK(info->operation == kMul || info->operation == kDiv);
Aart Bikc071a012016-12-01 10:22:31 -08001156 DCHECK(info->fetch != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001157 return "geo(" + InductionToString(info->op_a) + " * " +
1158 FetchToString(info->fetch) +
1159 (info->operation == kMul ? " ^ i + " : " ^ -i + ") +
1160 InductionToString(info->op_b) + "):" +
1161 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001162 } else if (info->induction_class == kWrapAround) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001163 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001164 return "wrap(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001165 InductionToString(info->op_b) + "):" +
1166 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001167 } else if (info->induction_class == kPeriodic) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001168 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001169 return "periodic(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001170 InductionToString(info->op_b) + "):" +
1171 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001172 }
1173 }
1174 }
1175 return "";
1176}
1177
1178} // namespace art