blob: b21bc09cbdc63fd87ab1a31b7cfeac473cb03904 [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 Bikd0a022d2016-12-13 11:22:31 -0800214 info = TransferPhi(loop, instruction, /*input_index*/ 0, /*adjust_input_size*/ 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 Bikd0a022d2016-12-13 11:22:31 -0800227 HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr);
Aart Bikc071a012016-12-01 10:22:31 -0800228 if (mulc != nullptr) {
229 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
230 LookupInfo(loop, mulc));
231 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800232 } else if (instruction->IsSelect()) {
233 info = TransferPhi(loop, instruction, /*input_index*/ 0, /*adjust_input_size*/ 1);
Aart Bik0d345cf2016-03-16 10:49:38 -0700234 } else if (instruction->IsTypeConversion()) {
235 info = TransferCnv(LookupInfo(loop, instruction->InputAt(0)),
236 instruction->AsTypeConversion()->GetInputType(),
237 instruction->AsTypeConversion()->GetResultType());
Aart Bike609b7c2015-08-27 13:46:58 -0700238 } else if (instruction->IsBoundsCheck()) {
239 info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through.
Aart Bik30efb4e2015-07-30 12:14:31 -0700240 }
241
242 // Successfully classified?
243 if (info != nullptr) {
244 AssignInfo(loop, instruction, info);
245 }
246}
247
248void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) {
249 const size_t size = scc_.size();
Aart Bike609b7c2015-08-27 13:46:58 -0700250 DCHECK_GE(size, 1u);
Aart Bik22af3be2015-09-10 12:50:58 -0700251
Aart Bikcc42be02016-10-20 16:14:16 -0700252 // Rotate proper loop-phi to front.
Aart Bik22af3be2015-09-10 12:50:58 -0700253 if (size > 1) {
Vladimir Marko5233f932015-09-29 19:01:15 +0100254 ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700255 RotateEntryPhiFirst(loop, &scc_, &other);
256 }
257
Aart Bikcc42be02016-10-20 16:14:16 -0700258 // Analyze from loop-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700259 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700260 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700261 return;
262 }
Aart Bikf475bee2015-09-16 12:50:25 -0700263
264 // External link should be loop invariant.
265 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700266 if (initial == nullptr || initial->induction_class != kInvariant) {
267 return;
268 }
269
Aart Bikcc42be02016-10-20 16:14:16 -0700270 // Store interesting cycle.
271 AssignCycle(phi->AsPhi());
272
Aart Bikf475bee2015-09-16 12:50:25 -0700273 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700274 if (size == 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800275 InductionInfo* update = TransferPhi(loop, phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700276 if (update != nullptr) {
Aart Bikc071a012016-12-01 10:22:31 -0800277 AssignInfo(loop, phi, CreateInduction(kWrapAround,
278 kNop,
279 initial,
280 update,
281 /*fetch*/ nullptr,
282 type_));
Aart Bik30efb4e2015-07-30 12:14:31 -0700283 }
284 return;
285 }
286
287 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700288 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700289 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700290 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700291 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700292 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700293 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700294 } else if (instruction->IsAdd()) {
295 update = SolveAddSub(
296 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
297 } else if (instruction->IsSub()) {
298 update = SolveAddSub(
299 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bikc071a012016-12-01 10:22:31 -0800300 } else if (instruction->IsMul()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800301 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800302 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kMul);
303 } else if (instruction->IsDiv()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800304 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800305 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kDiv);
306 } else if (instruction->IsRem()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800307 update = SolveOp(
308 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kRem);
Aart Bikc071a012016-12-01 10:22:31 -0800309 } else if (instruction->IsShl()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800310 HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr);
Aart Bikc071a012016-12-01 10:22:31 -0800311 if (mulc != nullptr) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800312 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), mulc, kMul);
Aart Bikc071a012016-12-01 10:22:31 -0800313 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800314 } else if (instruction->IsShr() || instruction->IsUShr()) {
315 HInstruction* divc = GetShiftConstant(loop, instruction, initial);
316 if (divc != nullptr) {
317 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), divc, kDiv);
318 }
Aart Bik7dc96932016-10-12 10:01:05 -0700319 } else if (instruction->IsXor()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800320 update = SolveOp(
321 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700322 } else if (instruction->IsEqual()) {
323 update = SolveTest(loop, phi, instruction, 0);
324 } else if (instruction->IsNotEqual()) {
325 update = SolveTest(loop, phi, instruction, 1);
Aart Bikd0a022d2016-12-13 11:22:31 -0800326 } else if (instruction->IsSelect()) {
327 update = SolvePhi(instruction, /*input_index*/ 0, /*adjust_input_size*/ 1); // acts like Phi
Aart Bik0d345cf2016-03-16 10:49:38 -0700328 } else if (instruction->IsTypeConversion()) {
329 update = SolveCnv(instruction->AsTypeConversion());
Aart Bik30efb4e2015-07-30 12:14:31 -0700330 }
331 if (update == nullptr) {
332 return;
333 }
Aart Bike609b7c2015-08-27 13:46:58 -0700334 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700335 }
336
Aart Bikf475bee2015-09-16 12:50:25 -0700337 // Success if all internal links received the same temporary meaning.
Aart Bikd0a022d2016-12-13 11:22:31 -0800338 InductionInfo* induction = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700339 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700340 switch (induction->induction_class) {
341 case kInvariant:
Aart Bikc071a012016-12-01 10:22:31 -0800342 // Construct combined stride of the linear induction.
343 induction = CreateInduction(kLinear, kNop, induction, initial, /*fetch*/ nullptr, type_);
344 FALLTHROUGH_INTENDED;
345 case kPolynomial:
346 case kGeometric:
Aart Bikdf7822e2016-12-06 10:05:30 -0800347 case kWrapAround:
Aart Bik22af3be2015-09-10 12:50:58 -0700348 // Classify first phi and then the rest of the cycle "on-demand".
349 // Statements are scanned in order.
Aart Bikc071a012016-12-01 10:22:31 -0800350 AssignInfo(loop, phi, induction);
Aart Bik22af3be2015-09-10 12:50:58 -0700351 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700352 ClassifyTrivial(loop, scc_[i]);
353 }
354 break;
355 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700356 // Classify all elements in the cycle with the found periodic induction while
357 // rotating each first element to the end. Lastly, phi is classified.
358 // Statements are scanned in reverse order.
359 for (size_t i = size - 1; i >= 1; i--) {
360 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700361 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
362 }
363 AssignInfo(loop, phi, induction);
364 break;
365 default:
366 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700367 }
368 }
369}
370
Aart Bike609b7c2015-08-27 13:46:58 -0700371HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
372 InductionInfo* induction,
373 InductionInfo* last) {
374 // Rotates a periodic induction of the form
375 // (a, b, c, d, e)
376 // into
377 // (b, c, d, e, a)
378 // in preparation of assigning this to the previous variable in the sequence.
379 if (induction->induction_class == kInvariant) {
Aart Bikc071a012016-12-01 10:22:31 -0800380 return CreateInduction(kPeriodic,
381 kNop,
382 induction,
383 last,
384 /*fetch*/ nullptr,
385 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700386 }
Aart Bikc071a012016-12-01 10:22:31 -0800387 return CreateInduction(kPeriodic,
388 kNop,
389 induction->op_a,
390 RotatePeriodicInduction(induction->op_b, last),
391 /*fetch*/ nullptr,
392 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700393}
394
Aart Bikf475bee2015-09-16 12:50:25 -0700395HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
396 HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800397 size_t input_index,
398 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700399 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100400 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100401 DCHECK_LT(input_index, inputs.size());
402 InductionInfo* a = LookupInfo(loop, inputs[input_index]);
Aart Bikd0a022d2016-12-13 11:22:31 -0800403 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100404 InductionInfo* b = LookupInfo(loop, inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700405 if (!InductionEqual(a, b)) {
406 return nullptr;
407 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700408 }
Aart Bikf475bee2015-09-16 12:50:25 -0700409 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700410}
411
412HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
413 InductionInfo* b,
414 InductionOp op) {
Aart Bikc071a012016-12-01 10:22:31 -0800415 // Transfer over an addition or subtraction: any invariant, linear, polynomial, geometric,
416 // wrap-around, or periodic can be combined with an invariant to yield a similar result.
Aart Bikdf7822e2016-12-06 10:05:30 -0800417 // Two linear or two polynomial inputs can be combined too. Other combinations fail.
Aart Bik30efb4e2015-07-30 12:14:31 -0700418 if (a != nullptr && b != nullptr) {
Aart Bikcc42be02016-10-20 16:14:16 -0700419 type_ = Narrowest(type_, Narrowest(a->type, b->type));
Aart Bik30efb4e2015-07-30 12:14:31 -0700420 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700421 return CreateInvariantOp(op, a, b);
Aart Bikdf7822e2016-12-06 10:05:30 -0800422 } else if ((a->induction_class == kLinear && b->induction_class == kLinear) ||
423 (a->induction_class == kPolynomial && b->induction_class == kPolynomial)) {
424 return CreateInduction(a->induction_class,
425 a->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700426 TransferAddSub(a->op_a, b->op_a, op),
427 TransferAddSub(a->op_b, b->op_b, op),
Aart Bikc071a012016-12-01 10:22:31 -0800428 /*fetch*/ nullptr,
Aart Bik0d345cf2016-03-16 10:49:38 -0700429 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700430 } else if (a->induction_class == kInvariant) {
431 InductionInfo* new_a = b->op_a;
432 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800433 if (b->induction_class == kWrapAround || b->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700434 new_a = TransferAddSub(a, new_a, op);
435 } else if (op == kSub) { // Negation required.
436 new_a = TransferNeg(new_a);
437 }
Aart Bikc071a012016-12-01 10:22:31 -0800438 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700439 } else if (b->induction_class == kInvariant) {
440 InductionInfo* new_a = a->op_a;
441 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800442 if (a->induction_class == kWrapAround || a->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700443 new_a = TransferAddSub(new_a, b, op);
444 }
Aart Bikc071a012016-12-01 10:22:31 -0800445 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
446 }
447 }
448 return nullptr;
449}
450
451HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
452 // Transfer over a unary negation: an invariant, linear, polynomial, geometric (mul),
453 // wrap-around, or periodic input yields a similar but negated induction as result.
454 if (a != nullptr) {
455 type_ = Narrowest(type_, a->type);
456 if (a->induction_class == kInvariant) {
457 return CreateInvariantOp(kNeg, nullptr, a);
458 } else if (a->induction_class != kGeometric || a->operation == kMul) {
459 return CreateInduction(a->induction_class,
460 a->operation,
461 TransferNeg(a->op_a),
462 TransferNeg(a->op_b),
463 a->fetch,
464 type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700465 }
466 }
467 return nullptr;
468}
469
470HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
471 InductionInfo* b) {
Aart Bikc071a012016-12-01 10:22:31 -0800472 // Transfer over a multiplication: any invariant, linear, polynomial, geometric (mul),
473 // wrap-around, or periodic can be multiplied with an invariant to yield a similar
474 // but multiplied result. Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700475 if (a != nullptr && b != nullptr) {
Aart Bikcc42be02016-10-20 16:14:16 -0700476 type_ = Narrowest(type_, Narrowest(a->type, b->type));
Aart Bik30efb4e2015-07-30 12:14:31 -0700477 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700478 return CreateInvariantOp(kMul, a, b);
Aart Bikc071a012016-12-01 10:22:31 -0800479 } else if (a->induction_class == kInvariant && (b->induction_class != kGeometric ||
480 b->operation == kMul)) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700481 return CreateInduction(b->induction_class,
Aart Bikc071a012016-12-01 10:22:31 -0800482 b->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700483 TransferMul(a, b->op_a),
484 TransferMul(a, b->op_b),
Aart Bikc071a012016-12-01 10:22:31 -0800485 b->fetch,
Aart Bik0d345cf2016-03-16 10:49:38 -0700486 type_);
Aart Bikc071a012016-12-01 10:22:31 -0800487 } else if (b->induction_class == kInvariant && (a->induction_class != kGeometric ||
488 a->operation == kMul)) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700489 return CreateInduction(a->induction_class,
Aart Bikc071a012016-12-01 10:22:31 -0800490 a->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700491 TransferMul(a->op_a, b),
492 TransferMul(a->op_b, b),
Aart Bikc071a012016-12-01 10:22:31 -0800493 a->fetch,
Aart Bik0d345cf2016-03-16 10:49:38 -0700494 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700495 }
496 }
497 return nullptr;
498}
499
Aart Bik0d345cf2016-03-16 10:49:38 -0700500HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferCnv(InductionInfo* a,
501 Primitive::Type from,
502 Primitive::Type to) {
503 if (a != nullptr) {
Aart Bikc071a012016-12-01 10:22:31 -0800504 // Allow narrowing conversion on linear induction in certain cases.
Aart Bik0d345cf2016-03-16 10:49:38 -0700505 if (IsNarrowingIntegralConversion(from, to)) {
506 if (a->induction_class == kLinear) {
507 if (a->type == to || (a->type == from && IsNarrowingIntegralConversion(from, to))) {
Aart Bikc071a012016-12-01 10:22:31 -0800508 return CreateInduction(kLinear, kNop, a->op_a, a->op_b, /*fetch*/ nullptr, to);
Aart Bik0d345cf2016-03-16 10:49:38 -0700509 }
510 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700511 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700512 }
513 return nullptr;
514}
515
Aart Bikf475bee2015-09-16 12:50:25 -0700516HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800517 size_t input_index,
518 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700519 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100520 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100521 DCHECK_LT(input_index, inputs.size());
522 auto ita = cycle_.find(inputs[input_index]);
Aart Bik30efb4e2015-07-30 12:14:31 -0700523 if (ita != cycle_.end()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800524 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100525 auto itb = cycle_.find(inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700526 if (itb == cycle_.end() ||
527 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700528 return nullptr;
529 }
530 }
Aart Bikf475bee2015-09-16 12:50:25 -0700531 return ita->second;
532 }
533 return nullptr;
534}
535
536HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
537 HLoopInformation* loop,
538 HInstruction* entry_phi,
539 HInstruction* phi) {
540 // Match all phi inputs.
Aart Bikd0a022d2016-12-13 11:22:31 -0800541 InductionInfo* match = SolvePhi(phi, /*input_index*/ 0, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700542 if (match != nullptr) {
543 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700544 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700545
Aart Bikf475bee2015-09-16 12:50:25 -0700546 // Otherwise, try to solve for a periodic seeded from phi onward.
547 // Only tight multi-statement cycles are considered in order to
548 // simplify rotating the periodic during the final classification.
549 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
550 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700551 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700552 if (phi->InputAt(1) == entry_phi) {
553 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800554 return CreateInduction(kPeriodic, kNop, a, initial, /*fetch*/ nullptr, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700555 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800556 InductionInfo* b = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700557 if (b != nullptr && b->induction_class == kPeriodic) {
Aart Bikc071a012016-12-01 10:22:31 -0800558 return CreateInduction(kPeriodic, kNop, a, b, /*fetch*/ nullptr, type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700559 }
560 }
561 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700562 return nullptr;
563}
564
Aart Bike609b7c2015-08-27 13:46:58 -0700565HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700566 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700567 HInstruction* instruction,
568 HInstruction* x,
569 HInstruction* y,
570 InductionOp op,
571 bool is_first_call) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800572 // Solve within a cycle over an addition or subtraction.
Aart Bike609b7c2015-08-27 13:46:58 -0700573 InductionInfo* b = LookupInfo(loop, y);
Aart Bikdf7822e2016-12-06 10:05:30 -0800574 if (b != nullptr) {
575 if (b->induction_class == kInvariant) {
576 // Adding or subtracting an invariant value, seeded from phi,
577 // keeps adding to the stride of the linear induction.
578 if (x == entry_phi) {
579 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
580 }
581 auto it = cycle_.find(x);
582 if (it != cycle_.end()) {
583 InductionInfo* a = it->second;
584 if (a->induction_class == kInvariant) {
585 return CreateInvariantOp(op, a, b);
586 }
587 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800588 } else if (b->induction_class == kLinear) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800589 // Solve within a tight cycle that adds a term that is already classified as a linear
590 // induction for a polynomial induction k = k + i (represented as sum over linear terms).
591 if (x == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
592 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
593 return CreateInduction(kPolynomial,
594 kNop,
Aart Bikd0a022d2016-12-13 11:22:31 -0800595 op == kAdd ? b : TransferNeg(b),
Aart Bikdf7822e2016-12-06 10:05:30 -0800596 initial,
597 /*fetch*/ nullptr,
598 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700599 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700600 }
601 }
Aart Bike609b7c2015-08-27 13:46:58 -0700602
603 // Try some alternatives before failing.
604 if (op == kAdd) {
605 // Try the other way around for an addition if considered for first time.
606 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700607 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700608 }
609 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700610 // Solve within a tight cycle that is formed by exactly two instructions,
Aart Bikc071a012016-12-01 10:22:31 -0800611 // one phi and one update, for a periodic idiom of the form k = c - k.
Aart Bikf475bee2015-09-16 12:50:25 -0700612 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700613 InductionInfo* a = LookupInfo(loop, x);
614 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700615 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800616 return CreateInduction(kPeriodic,
617 kNop,
618 CreateInvariantOp(kSub, a, initial),
619 initial,
620 /*fetch*/ nullptr,
621 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700622 }
623 }
624 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700625 return nullptr;
626}
627
Aart Bikdf7822e2016-12-06 10:05:30 -0800628HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveOp(HLoopInformation* loop,
Aart Bikc071a012016-12-01 10:22:31 -0800629 HInstruction* entry_phi,
630 HInstruction* instruction,
631 HInstruction* x,
632 HInstruction* y,
633 InductionOp op) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800634 // 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 -0700635 if (entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800636 InductionInfo* c = nullptr;
Aart Bik639cc8c2016-10-18 13:03:31 -0700637 InductionInfo* b = LookupInfo(loop, y);
638 if (b != nullptr && b->induction_class == kInvariant && entry_phi == x) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800639 c = b;
640 } else if (op != kDiv && op != kRem) {
641 InductionInfo* a = LookupInfo(loop, x);
642 if (a != nullptr && a->induction_class == kInvariant && entry_phi == y) {
643 c = a;
644 }
645 }
646 // Found suitable operand left or right?
647 if (c != nullptr) {
648 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
649 switch (op) {
650 case kMul:
651 case kDiv:
652 // Restrict base of geometric induction to direct fetch.
653 if (c->operation == kFetch) {
654 return CreateInduction(kGeometric,
655 op,
656 initial,
657 CreateConstant(0, type_),
658 c->fetch,
659 type_);
660 };
661 break;
662 case kRem:
663 // Idiomatic MOD wrap-around induction.
664 return CreateInduction(kWrapAround,
665 kNop,
666 initial,
667 CreateInvariantOp(kRem, initial, c),
668 /*fetch*/ nullptr,
669 type_);
670 case kXor:
671 // Idiomatic XOR periodic induction.
672 return CreateInduction(kPeriodic,
673 kNop,
674 CreateInvariantOp(kXor, initial, c),
675 initial,
676 /*fetch*/ nullptr,
677 type_);
678 default:
679 CHECK(false) << op;
680 break;
681 }
Aart Bik7dc96932016-10-12 10:01:05 -0700682 }
683 }
Aart Bik639cc8c2016-10-18 13:03:31 -0700684 return nullptr;
685}
686
687HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveTest(HLoopInformation* loop,
688 HInstruction* entry_phi,
689 HInstruction* instruction,
690 int64_t opposite_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800691 // Detect hidden XOR construction in x = (x == false) or x = (x != true).
Aart Bik639cc8c2016-10-18 13:03:31 -0700692 int64_t value = -1;
693 HInstruction* x = instruction->InputAt(0);
694 HInstruction* y = instruction->InputAt(1);
695 if (IsExact(LookupInfo(loop, x), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800696 return SolveOp(loop, entry_phi, instruction, graph_->GetIntConstant(1), y, kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700697 } else if (IsExact(LookupInfo(loop, y), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800698 return SolveOp(loop, entry_phi, instruction, x, graph_->GetIntConstant(1), kXor);
Aart Bik7dc96932016-10-12 10:01:05 -0700699 }
700 return nullptr;
701}
702
Aart Bik0d345cf2016-03-16 10:49:38 -0700703HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveCnv(HTypeConversion* conversion) {
704 Primitive::Type from = conversion->GetInputType();
705 Primitive::Type to = conversion->GetResultType();
706 // A narrowing conversion is allowed within the cycle of a linear induction, provided that the
707 // narrowest encountered type is recorded with the induction to account for the precision loss.
708 if (IsNarrowingIntegralConversion(from, to)) {
709 auto it = cycle_.find(conversion->GetInput());
710 if (it != cycle_.end() && it->second->induction_class == kInvariant) {
711 type_ = Narrowest(type_, to);
712 return it->second;
713 }
714 }
715 return nullptr;
716}
717
Aart Bikd14c5952015-09-08 15:25:15 -0700718void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
719 HInstruction* control = loop->GetHeader()->GetLastInstruction();
720 if (control->IsIf()) {
721 HIf* ifs = control->AsIf();
722 HBasicBlock* if_true = ifs->IfTrueSuccessor();
723 HBasicBlock* if_false = ifs->IfFalseSuccessor();
724 HInstruction* if_expr = ifs->InputAt(0);
725 // Determine if loop has following structure in header.
726 // loop-header: ....
727 // if (condition) goto X
728 if (if_expr->IsCondition()) {
729 HCondition* condition = if_expr->AsCondition();
730 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
731 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
732 Primitive::Type type = condition->InputAt(0)->GetType();
Aart Bik0d345cf2016-03-16 10:49:38 -0700733 // Determine if the loop control uses a known sequence on an if-exit (X outside) or on
734 // an if-iterate (X inside), expressed as if-iterate when passed into VisitCondition().
735 if (a == nullptr || b == nullptr) {
736 return; // Loop control is not a sequence.
Aart Bikd14c5952015-09-08 15:25:15 -0700737 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
738 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
739 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
740 VisitCondition(loop, a, b, type, condition->GetCondition());
741 }
742 }
743 }
744}
745
746void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
747 InductionInfo* a,
748 InductionInfo* b,
749 Primitive::Type type,
750 IfCondition cmp) {
751 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700752 // 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 -0700753 switch (cmp) {
754 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
755 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
756 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
757 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700758 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700759 default: break;
760 }
761 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700762 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700763 InductionInfo* lower_expr = a->op_b;
764 InductionInfo* upper_expr = b;
Aart Bik97412c922016-02-19 20:14:38 -0800765 InductionInfo* stride_expr = a->op_a;
766 // Constant stride?
Aart Bik9401f532015-09-28 16:25:56 -0700767 int64_t stride_value = 0;
Aart Bik97412c922016-02-19 20:14:38 -0800768 if (!IsExact(stride_expr, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700769 return;
770 }
Aart Bik358af832016-02-24 14:17:53 -0800771 // Rewrite condition i != U into strict end condition i < U or i > U if this end condition
772 // is reached exactly (tested by verifying if the loop has a unit stride and the non-strict
773 // condition would be always taken).
774 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLE)) ||
775 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGE)))) {
Aart Bik9401f532015-09-28 16:25:56 -0700776 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700777 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700778 // Only accept integral condition. A mismatch between the type of condition and the induction
779 // is only allowed if the, necessarily narrower, induction range fits the narrower control.
780 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
781 return; // not integral
782 } else if (type != a->type &&
783 !FitsNarrowerControl(lower_expr, upper_expr, stride_value, a->type, cmp)) {
784 return; // mismatched type
785 }
Aart Bikf475bee2015-09-16 12:50:25 -0700786 // Normalize a linear loop control with a nonzero stride:
787 // stride > 0, either i < U or i <= U
788 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700789 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
790 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik97412c922016-02-19 20:14:38 -0800791 VisitTripCount(loop, lower_expr, upper_expr, stride_expr, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700792 }
Aart Bikd14c5952015-09-08 15:25:15 -0700793 }
794}
795
796void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700797 InductionInfo* lower_expr,
798 InductionInfo* upper_expr,
Aart Bik97412c922016-02-19 20:14:38 -0800799 InductionInfo* stride_expr,
Aart Bik9401f532015-09-28 16:25:56 -0700800 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700801 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700802 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700803 // Any loop of the general form:
804 //
805 // for (i = L; i <= U; i += S) // S > 0
806 // or for (i = L; i >= U; i += S) // S < 0
807 // .. i ..
808 //
809 // can be normalized into:
810 //
811 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
812 // .. L + S * n ..
813 //
Aart Bik9401f532015-09-28 16:25:56 -0700814 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700815 //
Aart Bik9401f532015-09-28 16:25:56 -0700816 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
817 // an unsigned entity, for example, as in the following loop that uses the full range:
818 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
819 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
Aart Bikd5cc6832016-06-22 16:34:46 -0700820 // for (int i = 12; i < U; i++) // TC = 0 when U <= 12
Aart Bik9401f532015-09-28 16:25:56 -0700821 // If this cannot be determined at compile-time, the TC is only valid within the
Aart Bik22f05872015-10-27 15:56:28 -0700822 // loop-body proper, not the loop-header unless enforced with an explicit taken-test.
Aart Bik9401f532015-09-28 16:25:56 -0700823 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
824 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
825 // If this cannot be determined at compile-time, the TC is only valid when enforced
Aart Bik22f05872015-10-27 15:56:28 -0700826 // with an explicit finite-test.
Aart Bik9401f532015-09-28 16:25:56 -0700827 // (4) For loops which early-exits, the TC forms an upper bound, as in:
828 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
Aart Bik22f05872015-10-27 15:56:28 -0700829 InductionInfo* trip_count = upper_expr;
Aart Bik9401f532015-09-28 16:25:56 -0700830 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
831 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
832 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700833 if (!cancels) {
834 // Convert exclusive integral inequality into inclusive integral inequality,
835 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700836 if (cmp == kCondLT) {
Aart Bik22f05872015-10-27 15:56:28 -0700837 trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700838 } else if (cmp == kCondGT) {
Aart Bik22f05872015-10-27 15:56:28 -0700839 trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700840 }
841 // Compensate for stride.
Aart Bik97412c922016-02-19 20:14:38 -0800842 trip_count = CreateInvariantOp(kAdd, trip_count, stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700843 }
Aart Bik97412c922016-02-19 20:14:38 -0800844 trip_count = CreateInvariantOp(
845 kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700846 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700847 // should be aware that the expression is only valid under the conditions listed above.
Aart Bik22f05872015-10-27 15:56:28 -0700848 InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests
Aart Bik9401f532015-09-28 16:25:56 -0700849 if (is_taken && is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700850 tcKind = kTripCountInLoop; // needs neither test
Aart Bik9401f532015-09-28 16:25:56 -0700851 } else if (is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700852 tcKind = kTripCountInBody; // needs taken-test
Aart Bik9401f532015-09-28 16:25:56 -0700853 } else if (is_taken) {
Aart Bik22f05872015-10-27 15:56:28 -0700854 tcKind = kTripCountInLoopUnsafe; // needs finite-test
Aart Bik9401f532015-09-28 16:25:56 -0700855 }
Aart Bik22f05872015-10-27 15:56:28 -0700856 InductionOp op = kNop;
857 switch (cmp) {
858 case kCondLT: op = kLT; break;
859 case kCondLE: op = kLE; break;
860 case kCondGT: op = kGT; break;
861 case kCondGE: op = kGE; break;
862 default: LOG(FATAL) << "CONDITION UNREACHABLE";
863 }
Aart Bik009cace2016-09-16 10:15:19 -0700864 // Associate trip count with control instruction, rather than the condition (even
865 // though it's its use) since former provides a convenient use-free placeholder.
866 HInstruction* control = loop->GetHeader()->GetLastInstruction();
Aart Bik22f05872015-10-27 15:56:28 -0700867 InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr);
Aart Bik009cace2016-09-16 10:15:19 -0700868 DCHECK(control->IsIf());
869 AssignInfo(loop, control, CreateTripCount(tcKind, trip_count, taken_test, type));
Aart Bik9401f532015-09-28 16:25:56 -0700870}
871
872bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
873 InductionInfo* upper_expr,
874 IfCondition cmp) {
875 int64_t lower_value;
876 int64_t upper_value;
Aart Bik97412c922016-02-19 20:14:38 -0800877 switch (cmp) {
878 case kCondLT:
879 return IsAtMost(lower_expr, &lower_value)
880 && IsAtLeast(upper_expr, &upper_value)
881 && lower_value < upper_value;
882 case kCondLE:
883 return IsAtMost(lower_expr, &lower_value)
884 && IsAtLeast(upper_expr, &upper_value)
885 && lower_value <= upper_value;
886 case kCondGT:
887 return IsAtLeast(lower_expr, &lower_value)
888 && IsAtMost(upper_expr, &upper_value)
889 && lower_value > upper_value;
890 case kCondGE:
891 return IsAtLeast(lower_expr, &lower_value)
892 && IsAtMost(upper_expr, &upper_value)
893 && lower_value >= upper_value;
894 default:
895 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700896 }
897 return false; // not certain, may be untaken
898}
899
900bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
901 int64_t stride_value,
902 Primitive::Type type,
903 IfCondition cmp) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700904 const int64_t min = Primitive::MinValueOfIntegralType(type);
905 const int64_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik9401f532015-09-28 16:25:56 -0700906 // Some rules under which it is certain at compile-time that the loop is finite.
907 int64_t value;
908 switch (cmp) {
909 case kCondLT:
910 return stride_value == 1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800911 (IsAtMost(upper_expr, &value) && value <= (max - stride_value + 1));
Aart Bik9401f532015-09-28 16:25:56 -0700912 case kCondLE:
Aart Bik97412c922016-02-19 20:14:38 -0800913 return (IsAtMost(upper_expr, &value) && value <= (max - stride_value));
Aart Bik9401f532015-09-28 16:25:56 -0700914 case kCondGT:
915 return stride_value == -1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800916 (IsAtLeast(upper_expr, &value) && value >= (min - stride_value - 1));
Aart Bik9401f532015-09-28 16:25:56 -0700917 case kCondGE:
Aart Bik97412c922016-02-19 20:14:38 -0800918 return (IsAtLeast(upper_expr, &value) && value >= (min - stride_value));
Aart Bike9f37602015-10-09 11:15:55 -0700919 default:
920 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700921 }
922 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700923}
924
Aart Bik0d345cf2016-03-16 10:49:38 -0700925bool HInductionVarAnalysis::FitsNarrowerControl(InductionInfo* lower_expr,
926 InductionInfo* upper_expr,
927 int64_t stride_value,
928 Primitive::Type type,
929 IfCondition cmp) {
930 int64_t min = Primitive::MinValueOfIntegralType(type);
931 int64_t max = Primitive::MaxValueOfIntegralType(type);
932 // Inclusive test need one extra.
933 if (stride_value != 1 && stride_value != -1) {
934 return false; // non-unit stride
935 } else if (cmp == kCondLE) {
936 max--;
937 } else if (cmp == kCondGE) {
938 min++;
939 }
940 // Do both bounds fit the range?
Vladimir Marko0e2f2ff2016-03-22 12:31:54 +0000941 // Note: The `value` is initialized to please valgrind - the compiler can reorder
942 // the return value check with the `value` check, b/27651442 .
943 int64_t value = 0;
Aart Bik0d345cf2016-03-16 10:49:38 -0700944 return IsAtLeast(lower_expr, &value) && value >= min &&
945 IsAtMost(lower_expr, &value) && value <= max &&
946 IsAtLeast(upper_expr, &value) && value >= min &&
947 IsAtMost(upper_expr, &value) && value <= max;
948}
949
Aart Bik30efb4e2015-07-30 12:14:31 -0700950void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
951 HInstruction* instruction,
952 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700953 auto it = induction_.find(loop);
954 if (it == induction_.end()) {
955 it = induction_.Put(loop,
956 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100957 std::less<HInstruction*>(),
958 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700959 }
960 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700961}
962
Aart Bike609b7c2015-08-27 13:46:58 -0700963HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
964 HInstruction* instruction) {
965 auto it = induction_.find(loop);
966 if (it != induction_.end()) {
967 auto loop_it = it->second.find(instruction);
968 if (loop_it != it->second.end()) {
969 return loop_it->second;
970 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700971 }
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800972 if (loop->IsDefinedOutOfTheLoop(instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -0700973 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700974 AssignInfo(loop, instruction, info);
975 return info;
976 }
977 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -0700978}
979
Aart Bikd14c5952015-09-08 15:25:15 -0700980HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
981 Primitive::Type type) {
Aart Bikc071a012016-12-01 10:22:31 -0800982 HInstruction* constant;
983 switch (type) {
984 case Primitive::kPrimDouble: constant = graph_->GetDoubleConstant(value); break;
985 case Primitive::kPrimFloat: constant = graph_->GetFloatConstant(value); break;
986 case Primitive::kPrimLong: constant = graph_->GetLongConstant(value); break;
987 default: constant = graph_->GetIntConstant(value); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700988 }
Aart Bikc071a012016-12-01 10:22:31 -0800989 return CreateInvariantFetch(constant);
Aart Bikd14c5952015-09-08 15:25:15 -0700990}
991
Aart Bik471a2032015-09-04 18:22:11 -0700992HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
993 InductionOp op,
994 InductionInfo* a,
995 InductionInfo* b) {
996 // Perform some light-weight simplifications during construction of a new invariant.
997 // This often safes memory and yields a more concise representation of the induction.
998 // More exhaustive simplifications are done by later phases once induction nodes are
999 // translated back into HIR code (e.g. by loop optimizations or BCE).
1000 int64_t value = -1;
Aart Bik97412c922016-02-19 20:14:38 -08001001 if (IsExact(a, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001002 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001003 // Simplify 0 + b = b, 0 ^ b = b, 0 * b = 0.
1004 if (op == kAdd || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001005 return b;
1006 } else if (op == kMul) {
1007 return a;
1008 }
Aart Bikd14c5952015-09-08 15:25:15 -07001009 } else if (op == kMul) {
1010 // Simplify 1 * b = b, -1 * b = -b
1011 if (value == 1) {
1012 return b;
1013 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001014 return CreateSimplifiedInvariant(kNeg, nullptr, b);
Aart Bikd14c5952015-09-08 15:25:15 -07001015 }
Aart Bik471a2032015-09-04 18:22:11 -07001016 }
1017 }
Aart Bik97412c922016-02-19 20:14:38 -08001018 if (IsExact(b, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001019 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001020 // Simplify a + 0 = a, a - 0 = a, a ^ 0 = a, a * 0 = 0, -0 = 0.
1021 if (op == kAdd || op == kSub || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001022 return a;
1023 } else if (op == kMul || op == kNeg) {
1024 return b;
1025 }
Aart Bikd14c5952015-09-08 15:25:15 -07001026 } else if (op == kMul || op == kDiv) {
1027 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
1028 if (value == 1) {
1029 return a;
1030 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001031 return CreateSimplifiedInvariant(kNeg, nullptr, a);
Aart Bikd14c5952015-09-08 15:25:15 -07001032 }
Aart Bik471a2032015-09-04 18:22:11 -07001033 }
1034 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -07001035 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
1036 if (op == kAdd) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001037 return CreateSimplifiedInvariant(kSub, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001038 } else if (op == kSub) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001039 return CreateSimplifiedInvariant(kAdd, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001040 } else if (op == kNeg) {
1041 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -07001042 }
Aart Bik7d57d7f2015-12-09 14:39:48 -08001043 } else if (b->operation == kSub) {
1044 // Simplify - (a - b) = b - a.
1045 if (op == kNeg) {
1046 return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a);
1047 }
Aart Bik471a2032015-09-04 18:22:11 -07001048 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001049 return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr, b->type);
Aart Bik471a2032015-09-04 18:22:11 -07001050}
1051
Aart Bikd0a022d2016-12-13 11:22:31 -08001052HInstruction* HInductionVarAnalysis::GetShiftConstant(HLoopInformation* loop,
1053 HInstruction* instruction,
1054 InductionInfo* initial) {
1055 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
1056 // Shift-rights are only the same as division for non-negative initial inputs.
1057 // Otherwise we would round incorrectly.
1058 if (initial != nullptr) {
1059 int64_t value = -1;
1060 if (!IsAtLeast(initial, &value) || value < 0) {
1061 return nullptr;
1062 }
1063 }
1064 // Obtain the constant needed to treat shift as equivalent multiplication or division.
1065 // This yields an existing instruction if the constant is already there. Otherwise, this
1066 // has a side effect on the HIR. The restriction on the shift factor avoids generating a
1067 // negative constant (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that
1068 // generalization for shift factors outside [0,32) and [0,64) ranges is done earlier.
Aart Bikc071a012016-12-01 10:22:31 -08001069 InductionInfo* b = LookupInfo(loop, instruction->InputAt(1));
1070 int64_t value = -1;
1071 if (IsExact(b, &value)) {
1072 Primitive::Type type = instruction->InputAt(0)->GetType();
1073 if (type == Primitive::kPrimInt && 0 <= value && value < 31) {
1074 return graph_->GetIntConstant(1 << value);
1075 }
1076 if (type == Primitive::kPrimLong && 0 <= value && value < 63) {
1077 return graph_->GetLongConstant(1L << value);
1078 }
1079 }
1080 return nullptr;
1081}
Aart Bikcc42be02016-10-20 16:14:16 -07001082
1083void HInductionVarAnalysis::AssignCycle(HPhi* phi) {
1084 ArenaSet<HInstruction*>* set = &cycles_.Put(phi, ArenaSet<HInstruction*>(
1085 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)))->second;
1086 for (HInstruction* i : scc_) {
1087 set->insert(i);
1088 }
1089}
1090
1091ArenaSet<HInstruction*>* HInductionVarAnalysis::LookupCycle(HPhi* phi) {
1092 auto it = cycles_.find(phi);
1093 if (it != cycles_.end()) {
1094 return &it->second;
1095 }
1096 return nullptr;
1097}
1098
Aart Bik97412c922016-02-19 20:14:38 -08001099bool HInductionVarAnalysis::IsExact(InductionInfo* info, int64_t* value) {
1100 return InductionVarRange(this).IsConstant(info, InductionVarRange::kExact, value);
1101}
1102
1103bool HInductionVarAnalysis::IsAtMost(InductionInfo* info, int64_t* value) {
1104 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtMost, value);
1105}
1106
1107bool HInductionVarAnalysis::IsAtLeast(InductionInfo* info, int64_t* value) {
1108 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtLeast, value);
Aart Bik7d57d7f2015-12-09 14:39:48 -08001109}
1110
Aart Bik30efb4e2015-07-30 12:14:31 -07001111bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
1112 InductionInfo* info2) {
1113 // Test structural equality only, without accounting for simplifications.
1114 if (info1 != nullptr && info2 != nullptr) {
1115 return
1116 info1->induction_class == info2->induction_class &&
1117 info1->operation == info2->operation &&
1118 info1->fetch == info2->fetch &&
Aart Bik78296912016-03-25 13:14:53 -07001119 info1->type == info2->type &&
Aart Bik30efb4e2015-07-30 12:14:31 -07001120 InductionEqual(info1->op_a, info2->op_a) &&
1121 InductionEqual(info1->op_b, info2->op_b);
1122 }
1123 // Otherwise only two nullptrs are considered equal.
1124 return info1 == info2;
1125}
1126
Aart Bikc071a012016-12-01 10:22:31 -08001127std::string HInductionVarAnalysis::FetchToString(HInstruction* fetch) {
1128 DCHECK(fetch != nullptr);
1129 if (fetch->IsIntConstant()) {
1130 return std::to_string(fetch->AsIntConstant()->GetValue());
1131 } else if (fetch->IsLongConstant()) {
1132 return std::to_string(fetch->AsLongConstant()->GetValue());
1133 }
1134 return std::to_string(fetch->GetId()) + ":" + fetch->DebugName();
1135}
1136
Aart Bik30efb4e2015-07-30 12:14:31 -07001137std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
1138 if (info != nullptr) {
1139 if (info->induction_class == kInvariant) {
1140 std::string inv = "(";
1141 inv += InductionToString(info->op_a);
1142 switch (info->operation) {
Aart Bik22f05872015-10-27 15:56:28 -07001143 case kNop: inv += " @ "; break;
1144 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001145 case kSub:
Aart Bik22f05872015-10-27 15:56:28 -07001146 case kNeg: inv += " - "; break;
1147 case kMul: inv += " * "; break;
1148 case kDiv: inv += " / "; break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001149 case kRem: inv += " % "; break;
Aart Bik7dc96932016-10-12 10:01:05 -07001150 case kXor: inv += " ^ "; break;
Aart Bik22f05872015-10-27 15:56:28 -07001151 case kLT: inv += " < "; break;
1152 case kLE: inv += " <= "; break;
1153 case kGT: inv += " > "; break;
1154 case kGE: inv += " >= "; break;
Aart Bikc071a012016-12-01 10:22:31 -08001155 case kFetch: inv += FetchToString(info->fetch); break;
Aart Bik22f05872015-10-27 15:56:28 -07001156 case kTripCountInLoop: inv += " (TC-loop) "; break;
1157 case kTripCountInBody: inv += " (TC-body) "; break;
1158 case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break;
1159 case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001160 }
1161 inv += InductionToString(info->op_b);
Aart Bik0d345cf2016-03-16 10:49:38 -07001162 inv += ")";
1163 return inv;
Aart Bik30efb4e2015-07-30 12:14:31 -07001164 } else {
Aart Bik30efb4e2015-07-30 12:14:31 -07001165 if (info->induction_class == kLinear) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001166 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001167 return "(" + InductionToString(info->op_a) + " * i + " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001168 InductionToString(info->op_b) + "):" +
1169 Primitive::PrettyDescriptor(info->type);
Aart Bikc071a012016-12-01 10:22:31 -08001170 } else if (info->induction_class == kPolynomial) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001171 DCHECK(info->operation == kNop);
1172 return "poly(sum_lt(" + InductionToString(info->op_a) + ") + " +
Aart Bikc071a012016-12-01 10:22:31 -08001173 InductionToString(info->op_b) + "):" +
1174 Primitive::PrettyDescriptor(info->type);
1175 } else if (info->induction_class == kGeometric) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001176 DCHECK(info->operation == kMul || info->operation == kDiv);
Aart Bikc071a012016-12-01 10:22:31 -08001177 DCHECK(info->fetch != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001178 return "geo(" + InductionToString(info->op_a) + " * " +
1179 FetchToString(info->fetch) +
1180 (info->operation == kMul ? " ^ i + " : " ^ -i + ") +
1181 InductionToString(info->op_b) + "):" +
1182 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001183 } else if (info->induction_class == kWrapAround) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001184 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001185 return "wrap(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001186 InductionToString(info->op_b) + "):" +
1187 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001188 } else if (info->induction_class == kPeriodic) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001189 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001190 return "periodic(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001191 InductionToString(info->op_b) + "):" +
1192 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001193 }
1194 }
1195 }
1196 return "";
1197}
1198
1199} // namespace art