blob: 88473f02e51fce00ec4efd1bc244b465268e738f [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/**
Aart Bike6bd0272016-12-16 13:57:52 -080076 * Returns result of implicit widening type conversion done in HIR.
Aart Bik0d345cf2016-03-16 10:49:38 -070077 */
Aart Bike6bd0272016-12-16 13:57:52 -080078static Primitive::Type ImplicitConversion(Primitive::Type type) {
79 switch (type) {
80 case Primitive::kPrimShort:
81 case Primitive::kPrimChar:
82 case Primitive::kPrimByte:
83 case Primitive::kPrimBoolean:
84 return Primitive::kPrimInt;
85 default:
86 return type;
87 }
Aart Bik0d345cf2016-03-16 10:49:38 -070088}
89
Aart Bik30efb4e2015-07-30 12:14:31 -070090//
91// Class methods.
92//
93
94HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph)
95 : HOptimization(graph, kInductionPassName),
96 global_depth_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +010097 stack_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko5233f932015-09-29 19:01:15 +010098 map_(std::less<HInstruction*>(),
99 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Aart Bik7dc96932016-10-12 10:01:05 -0700100 scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100101 cycle_(std::less<HInstruction*>(),
102 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Aart Bik7dc96932016-10-12 10:01:05 -0700103 type_(Primitive::kPrimVoid),
Vladimir Marko5233f932015-09-29 19:01:15 +0100104 induction_(std::less<HLoopInformation*>(),
Aart Bikcc42be02016-10-20 16:14:16 -0700105 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
106 cycles_(std::less<HPhi*>(),
107 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700108}
109
110void HInductionVarAnalysis::Run() {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800111 // Detects sequence variables (generalized induction variables) during an outer to inner
112 // traversal of all loops using Gerlek's algorithm. The order is important to enable
113 // range analysis on outer loop while visiting inner loops.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100114 for (HBasicBlock* graph_block : graph_->GetReversePostOrder()) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000115 // Don't analyze irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000116 if (graph_block->IsLoopHeader() && !graph_block->GetLoopInformation()->IsIrreducible()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700117 VisitLoop(graph_block->GetLoopInformation());
118 }
119 }
120}
121
122void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) {
123 // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's
124 // algorithm. Due to the descendant-first nature, classification happens "on-demand".
125 global_depth_ = 0;
Aart Bike609b7c2015-08-27 13:46:58 -0700126 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700127 map_.clear();
128
129 for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) {
130 HBasicBlock* loop_block = it_loop.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700131 DCHECK(loop_block->IsInLoop());
Aart Bik30efb4e2015-07-30 12:14:31 -0700132 if (loop_block->GetLoopInformation() != loop) {
Aart Bik7dc96932016-10-12 10:01:05 -0700133 continue; // Inner loops visited later.
Aart Bik30efb4e2015-07-30 12:14:31 -0700134 }
135 // Visit phi-operations and instructions.
136 for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) {
137 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700138 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700139 VisitNode(loop, instruction);
140 }
141 }
142 for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) {
143 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700144 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700145 VisitNode(loop, instruction);
146 }
147 }
148 }
149
Aart Bike609b7c2015-08-27 13:46:58 -0700150 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700151 map_.clear();
Aart Bikd14c5952015-09-08 15:25:15 -0700152
Aart Bik78296912016-03-25 13:14:53 -0700153 // Determine the loop's trip-count.
Aart Bikd14c5952015-09-08 15:25:15 -0700154 VisitControl(loop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700155}
156
157void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700158 const uint32_t d1 = ++global_depth_;
Aart Bike609b7c2015-08-27 13:46:58 -0700159 map_.Put(instruction, NodeInfo(d1));
Aart Bik30efb4e2015-07-30 12:14:31 -0700160 stack_.push_back(instruction);
161
162 // Visit all descendants.
163 uint32_t low = d1;
Vladimir Marko372f10e2016-05-17 16:30:10 +0100164 for (HInstruction* input : instruction->GetInputs()) {
165 low = std::min(low, VisitDescendant(loop, input));
Aart Bik30efb4e2015-07-30 12:14:31 -0700166 }
167
168 // Lower or found SCC?
169 if (low < d1) {
Aart Bike609b7c2015-08-27 13:46:58 -0700170 map_.find(instruction)->second.depth = low;
Aart Bik30efb4e2015-07-30 12:14:31 -0700171 } else {
172 scc_.clear();
173 cycle_.clear();
174
175 // Pop the stack to build the SCC for classification.
176 while (!stack_.empty()) {
177 HInstruction* x = stack_.back();
178 scc_.push_back(x);
179 stack_.pop_back();
Aart Bike609b7c2015-08-27 13:46:58 -0700180 map_.find(x)->second.done = true;
Aart Bik30efb4e2015-07-30 12:14:31 -0700181 if (x == instruction) {
182 break;
183 }
184 }
185
Aart Bik0d345cf2016-03-16 10:49:38 -0700186 // Type of induction.
187 type_ = scc_[0]->GetType();
188
Aart Bik30efb4e2015-07-30 12:14:31 -0700189 // Classify the SCC.
Aart Bikf475bee2015-09-16 12:50:25 -0700190 if (scc_.size() == 1 && !scc_[0]->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700191 ClassifyTrivial(loop, scc_[0]);
192 } else {
193 ClassifyNonTrivial(loop);
194 }
195
196 scc_.clear();
197 cycle_.clear();
198 }
199}
200
201uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) {
202 // If the definition is either outside the loop (loop invariant entry value)
203 // or assigned in inner loop (inner exit value), the traversal stops.
204 HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation();
205 if (otherLoop != loop) {
206 return global_depth_;
207 }
208
209 // Inspect descendant node.
Aart Bike609b7c2015-08-27 13:46:58 -0700210 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700211 VisitNode(loop, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700212 return map_.find(instruction)->second.depth;
Aart Bik30efb4e2015-07-30 12:14:31 -0700213 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700214 auto it = map_.find(instruction);
Aart Bik30efb4e2015-07-30 12:14:31 -0700215 return it->second.done ? global_depth_ : it->second.depth;
216 }
217}
218
219void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) {
220 InductionInfo* info = nullptr;
221 if (instruction->IsPhi()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800222 info = TransferPhi(loop, instruction, /*input_index*/ 0, /*adjust_input_size*/ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700223 } else if (instruction->IsAdd()) {
224 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
225 LookupInfo(loop, instruction->InputAt(1)), kAdd);
226 } else if (instruction->IsSub()) {
227 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
228 LookupInfo(loop, instruction->InputAt(1)), kSub);
Aart Bikc071a012016-12-01 10:22:31 -0800229 } else if (instruction->IsNeg()) {
230 info = TransferNeg(LookupInfo(loop, instruction->InputAt(0)));
Aart Bik30efb4e2015-07-30 12:14:31 -0700231 } else if (instruction->IsMul()) {
232 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
233 LookupInfo(loop, instruction->InputAt(1)));
Aart Bike609b7c2015-08-27 13:46:58 -0700234 } else if (instruction->IsShl()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800235 HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr);
Aart Bikc071a012016-12-01 10:22:31 -0800236 if (mulc != nullptr) {
237 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
238 LookupInfo(loop, mulc));
239 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800240 } else if (instruction->IsSelect()) {
241 info = TransferPhi(loop, instruction, /*input_index*/ 0, /*adjust_input_size*/ 1);
Aart Bik0d345cf2016-03-16 10:49:38 -0700242 } else if (instruction->IsTypeConversion()) {
Aart Bike6bd0272016-12-16 13:57:52 -0800243 info = TransferConversion(LookupInfo(loop, instruction->InputAt(0)),
244 instruction->AsTypeConversion()->GetInputType(),
245 instruction->AsTypeConversion()->GetResultType());
Aart Bike609b7c2015-08-27 13:46:58 -0700246 } else if (instruction->IsBoundsCheck()) {
247 info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through.
Aart Bik30efb4e2015-07-30 12:14:31 -0700248 }
249
250 // Successfully classified?
251 if (info != nullptr) {
252 AssignInfo(loop, instruction, info);
253 }
254}
255
256void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) {
257 const size_t size = scc_.size();
Aart Bike609b7c2015-08-27 13:46:58 -0700258 DCHECK_GE(size, 1u);
Aart Bik22af3be2015-09-10 12:50:58 -0700259
Aart Bikcc42be02016-10-20 16:14:16 -0700260 // Rotate proper loop-phi to front.
Aart Bik22af3be2015-09-10 12:50:58 -0700261 if (size > 1) {
Vladimir Marko5233f932015-09-29 19:01:15 +0100262 ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700263 RotateEntryPhiFirst(loop, &scc_, &other);
264 }
265
Aart Bikcc42be02016-10-20 16:14:16 -0700266 // Analyze from loop-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700267 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700268 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700269 return;
270 }
Aart Bikf475bee2015-09-16 12:50:25 -0700271
272 // External link should be loop invariant.
273 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700274 if (initial == nullptr || initial->induction_class != kInvariant) {
275 return;
276 }
277
Aart Bike6bd0272016-12-16 13:57:52 -0800278 // Store interesting cycle in each loop phi.
279 for (size_t i = 0; i < size; i++) {
280 if (scc_[i]->IsLoopHeaderPhi()) {
281 AssignCycle(scc_[i]->AsPhi());
282 }
283 }
Aart Bikcc42be02016-10-20 16:14:16 -0700284
Aart Bikf475bee2015-09-16 12:50:25 -0700285 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700286 if (size == 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800287 InductionInfo* update = TransferPhi(loop, phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700288 if (update != nullptr) {
Aart Bikc071a012016-12-01 10:22:31 -0800289 AssignInfo(loop, phi, CreateInduction(kWrapAround,
290 kNop,
291 initial,
292 update,
293 /*fetch*/ nullptr,
294 type_));
Aart Bik30efb4e2015-07-30 12:14:31 -0700295 }
296 return;
297 }
298
299 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700300 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700301 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700302 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700303 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700304 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700305 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700306 } else if (instruction->IsAdd()) {
307 update = SolveAddSub(
308 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
309 } else if (instruction->IsSub()) {
310 update = SolveAddSub(
311 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bikc071a012016-12-01 10:22:31 -0800312 } else if (instruction->IsMul()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800313 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800314 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kMul);
315 } else if (instruction->IsDiv()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800316 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800317 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kDiv);
318 } else if (instruction->IsRem()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800319 update = SolveOp(
320 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kRem);
Aart Bikc071a012016-12-01 10:22:31 -0800321 } else if (instruction->IsShl()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800322 HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr);
Aart Bikc071a012016-12-01 10:22:31 -0800323 if (mulc != nullptr) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800324 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), mulc, kMul);
Aart Bikc071a012016-12-01 10:22:31 -0800325 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800326 } else if (instruction->IsShr() || instruction->IsUShr()) {
327 HInstruction* divc = GetShiftConstant(loop, instruction, initial);
328 if (divc != nullptr) {
329 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), divc, kDiv);
330 }
Aart Bik7dc96932016-10-12 10:01:05 -0700331 } else if (instruction->IsXor()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800332 update = SolveOp(
333 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700334 } else if (instruction->IsEqual()) {
335 update = SolveTest(loop, phi, instruction, 0);
336 } else if (instruction->IsNotEqual()) {
337 update = SolveTest(loop, phi, instruction, 1);
Aart Bikd0a022d2016-12-13 11:22:31 -0800338 } else if (instruction->IsSelect()) {
339 update = SolvePhi(instruction, /*input_index*/ 0, /*adjust_input_size*/ 1); // acts like Phi
Aart Bik0d345cf2016-03-16 10:49:38 -0700340 } else if (instruction->IsTypeConversion()) {
Aart Bike6bd0272016-12-16 13:57:52 -0800341 update = SolveConversion(loop, phi, instruction->AsTypeConversion());
Aart Bik30efb4e2015-07-30 12:14:31 -0700342 }
343 if (update == nullptr) {
344 return;
345 }
Aart Bike609b7c2015-08-27 13:46:58 -0700346 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700347 }
348
Aart Bikf475bee2015-09-16 12:50:25 -0700349 // Success if all internal links received the same temporary meaning.
Aart Bikd0a022d2016-12-13 11:22:31 -0800350 InductionInfo* induction = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700351 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700352 switch (induction->induction_class) {
353 case kInvariant:
Aart Bikc071a012016-12-01 10:22:31 -0800354 // Construct combined stride of the linear induction.
355 induction = CreateInduction(kLinear, kNop, induction, initial, /*fetch*/ nullptr, type_);
356 FALLTHROUGH_INTENDED;
357 case kPolynomial:
358 case kGeometric:
Aart Bikdf7822e2016-12-06 10:05:30 -0800359 case kWrapAround:
Aart Bik22af3be2015-09-10 12:50:58 -0700360 // Classify first phi and then the rest of the cycle "on-demand".
361 // Statements are scanned in order.
Aart Bikc071a012016-12-01 10:22:31 -0800362 AssignInfo(loop, phi, induction);
Aart Bik22af3be2015-09-10 12:50:58 -0700363 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700364 ClassifyTrivial(loop, scc_[i]);
365 }
366 break;
367 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700368 // Classify all elements in the cycle with the found periodic induction while
369 // rotating each first element to the end. Lastly, phi is classified.
370 // Statements are scanned in reverse order.
371 for (size_t i = size - 1; i >= 1; i--) {
372 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700373 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
374 }
375 AssignInfo(loop, phi, induction);
376 break;
377 default:
378 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700379 }
380 }
381}
382
Aart Bike609b7c2015-08-27 13:46:58 -0700383HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
384 InductionInfo* induction,
385 InductionInfo* last) {
386 // Rotates a periodic induction of the form
387 // (a, b, c, d, e)
388 // into
389 // (b, c, d, e, a)
390 // in preparation of assigning this to the previous variable in the sequence.
391 if (induction->induction_class == kInvariant) {
Aart Bikc071a012016-12-01 10:22:31 -0800392 return CreateInduction(kPeriodic,
393 kNop,
394 induction,
395 last,
396 /*fetch*/ nullptr,
397 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700398 }
Aart Bikc071a012016-12-01 10:22:31 -0800399 return CreateInduction(kPeriodic,
400 kNop,
401 induction->op_a,
402 RotatePeriodicInduction(induction->op_b, last),
403 /*fetch*/ nullptr,
404 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700405}
406
Aart Bikf475bee2015-09-16 12:50:25 -0700407HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
408 HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800409 size_t input_index,
410 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700411 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100412 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100413 DCHECK_LT(input_index, inputs.size());
414 InductionInfo* a = LookupInfo(loop, inputs[input_index]);
Aart Bikd0a022d2016-12-13 11:22:31 -0800415 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100416 InductionInfo* b = LookupInfo(loop, inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700417 if (!InductionEqual(a, b)) {
418 return nullptr;
419 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700420 }
Aart Bikf475bee2015-09-16 12:50:25 -0700421 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700422}
423
424HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
425 InductionInfo* b,
426 InductionOp op) {
Aart Bikc071a012016-12-01 10:22:31 -0800427 // Transfer over an addition or subtraction: any invariant, linear, polynomial, geometric,
428 // wrap-around, or periodic can be combined with an invariant to yield a similar result.
Aart Bikdf7822e2016-12-06 10:05:30 -0800429 // Two linear or two polynomial inputs can be combined too. Other combinations fail.
Aart Bik30efb4e2015-07-30 12:14:31 -0700430 if (a != nullptr && b != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800431 if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) {
432 return nullptr; // no transfer
433 } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800434 return CreateInvariantOp(op, a, b); // direct invariant
Aart Bikdf7822e2016-12-06 10:05:30 -0800435 } else if ((a->induction_class == kLinear && b->induction_class == kLinear) ||
436 (a->induction_class == kPolynomial && b->induction_class == kPolynomial)) {
Aart Bik74da5292016-12-20 11:13:03 -0800437 // Rule induc(a, b) + induc(a', b') -> induc(a + a', b + b').
438 InductionInfo* new_a = TransferAddSub(a->op_a, b->op_a, op);
439 InductionInfo* new_b = TransferAddSub(a->op_b, b->op_b, op);
440 if (new_a != nullptr && new_b != nullptr) {
441 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
442 }
Aart Bike609b7c2015-08-27 13:46:58 -0700443 } else if (a->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800444 // Rule a + induc(a', b') -> induc(a', a + b') or induc(a + a', a + b').
Aart Bike609b7c2015-08-27 13:46:58 -0700445 InductionInfo* new_a = b->op_a;
446 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800447 if (b->induction_class == kWrapAround || b->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700448 new_a = TransferAddSub(a, new_a, op);
449 } else if (op == kSub) { // Negation required.
450 new_a = TransferNeg(new_a);
451 }
Aart Bik74da5292016-12-20 11:13:03 -0800452 if (new_a != nullptr && new_b != nullptr) {
453 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
454 }
Aart Bike609b7c2015-08-27 13:46:58 -0700455 } else if (b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800456 // Rule induc(a, b) + b' -> induc(a, b + b') or induc(a + b', b + b').
Aart Bike609b7c2015-08-27 13:46:58 -0700457 InductionInfo* new_a = a->op_a;
458 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800459 if (a->induction_class == kWrapAround || a->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700460 new_a = TransferAddSub(new_a, b, op);
461 }
Aart Bik74da5292016-12-20 11:13:03 -0800462 if (new_a != nullptr && new_b != nullptr) {
463 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
464 }
Aart Bikc071a012016-12-01 10:22:31 -0800465 }
466 }
467 return nullptr;
468}
469
470HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
471 // Transfer over a unary negation: an invariant, linear, polynomial, geometric (mul),
472 // wrap-around, or periodic input yields a similar but negated induction as result.
473 if (a != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800474 if (IsNarrowingLinear(a)) {
475 return nullptr; // no transfer
476 } else if (a->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800477 return CreateInvariantOp(kNeg, nullptr, a); // direct invariant
Aart Bikc071a012016-12-01 10:22:31 -0800478 } else if (a->induction_class != kGeometric || a->operation == kMul) {
Aart Bik74da5292016-12-20 11:13:03 -0800479 // Rule - induc(a, b) -> induc(-a, -b).
480 InductionInfo* new_a = TransferNeg(a->op_a);
481 InductionInfo* new_b = TransferNeg(a->op_b);
482 if (new_a != nullptr && new_b != nullptr) {
483 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
484 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700485 }
486 }
487 return nullptr;
488}
489
490HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
491 InductionInfo* b) {
Aart Bikc071a012016-12-01 10:22:31 -0800492 // Transfer over a multiplication: any invariant, linear, polynomial, geometric (mul),
493 // wrap-around, or periodic can be multiplied with an invariant to yield a similar
494 // but multiplied result. Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700495 if (a != nullptr && b != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800496 if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) {
497 return nullptr; // no transfer
498 } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800499 return CreateInvariantOp(kMul, a, b); // direct invariant
Aart Bikc071a012016-12-01 10:22:31 -0800500 } else if (a->induction_class == kInvariant && (b->induction_class != kGeometric ||
501 b->operation == kMul)) {
Aart Bik74da5292016-12-20 11:13:03 -0800502 // Rule a * induc(a', b') -> induc(a * a', b * b').
503 InductionInfo* new_a = TransferMul(a, b->op_a);
504 InductionInfo* new_b = TransferMul(a, b->op_b);
505 if (new_a != nullptr && new_b != nullptr) {
506 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
507 }
Aart Bikc071a012016-12-01 10:22:31 -0800508 } else if (b->induction_class == kInvariant && (a->induction_class != kGeometric ||
509 a->operation == kMul)) {
Aart Bik74da5292016-12-20 11:13:03 -0800510 // Rule induc(a, b) * b' -> induc(a * b', b * b').
511 InductionInfo* new_a = TransferMul(a->op_a, b);
512 InductionInfo* new_b = TransferMul(a->op_b, b);
513 if (new_a != nullptr && new_b != nullptr) {
514 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
515 }
Aart Bike609b7c2015-08-27 13:46:58 -0700516 }
517 }
518 return nullptr;
519}
520
Aart Bike6bd0272016-12-16 13:57:52 -0800521HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferConversion(
522 InductionInfo* a,
523 Primitive::Type from,
524 Primitive::Type to) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700525 if (a != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800526 // Allow narrowing conversion on linear induction in certain cases:
527 // induction is already at narrow type, or can be made narrower.
528 if (IsNarrowingIntegralConversion(from, to) &&
529 a->induction_class == kLinear &&
530 (a->type == to || IsNarrowingIntegralConversion(a->type, to))) {
Aart Bik74da5292016-12-20 11:13:03 -0800531 return CreateInduction(kLinear, kNop, a->op_a, a->op_b, a->fetch, to);
Aart Bik0d345cf2016-03-16 10:49:38 -0700532 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700533 }
534 return nullptr;
535}
536
Aart Bikf475bee2015-09-16 12:50:25 -0700537HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800538 size_t input_index,
539 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700540 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100541 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100542 DCHECK_LT(input_index, inputs.size());
543 auto ita = cycle_.find(inputs[input_index]);
Aart Bik30efb4e2015-07-30 12:14:31 -0700544 if (ita != cycle_.end()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800545 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100546 auto itb = cycle_.find(inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700547 if (itb == cycle_.end() ||
548 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700549 return nullptr;
550 }
551 }
Aart Bikf475bee2015-09-16 12:50:25 -0700552 return ita->second;
553 }
554 return nullptr;
555}
556
557HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
558 HLoopInformation* loop,
559 HInstruction* entry_phi,
560 HInstruction* phi) {
561 // Match all phi inputs.
Aart Bikd0a022d2016-12-13 11:22:31 -0800562 InductionInfo* match = SolvePhi(phi, /*input_index*/ 0, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700563 if (match != nullptr) {
564 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700565 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700566
Aart Bikf475bee2015-09-16 12:50:25 -0700567 // Otherwise, try to solve for a periodic seeded from phi onward.
568 // Only tight multi-statement cycles are considered in order to
569 // simplify rotating the periodic during the final classification.
570 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
571 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700572 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700573 if (phi->InputAt(1) == entry_phi) {
574 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800575 return CreateInduction(kPeriodic, kNop, a, initial, /*fetch*/ nullptr, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700576 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800577 InductionInfo* b = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700578 if (b != nullptr && b->induction_class == kPeriodic) {
Aart Bikc071a012016-12-01 10:22:31 -0800579 return CreateInduction(kPeriodic, kNop, a, b, /*fetch*/ nullptr, type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700580 }
581 }
582 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700583 return nullptr;
584}
585
Aart Bike609b7c2015-08-27 13:46:58 -0700586HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700587 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700588 HInstruction* instruction,
589 HInstruction* x,
590 HInstruction* y,
591 InductionOp op,
592 bool is_first_call) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800593 // Solve within a cycle over an addition or subtraction.
Aart Bike609b7c2015-08-27 13:46:58 -0700594 InductionInfo* b = LookupInfo(loop, y);
Aart Bikdf7822e2016-12-06 10:05:30 -0800595 if (b != nullptr) {
596 if (b->induction_class == kInvariant) {
597 // Adding or subtracting an invariant value, seeded from phi,
598 // keeps adding to the stride of the linear induction.
599 if (x == entry_phi) {
600 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
601 }
602 auto it = cycle_.find(x);
603 if (it != cycle_.end()) {
604 InductionInfo* a = it->second;
605 if (a->induction_class == kInvariant) {
606 return CreateInvariantOp(op, a, b);
607 }
608 }
Aart Bik74da5292016-12-20 11:13:03 -0800609 } else if (b->induction_class == kLinear && b->type == type_) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800610 // Solve within a tight cycle that adds a term that is already classified as a linear
611 // induction for a polynomial induction k = k + i (represented as sum over linear terms).
612 if (x == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
613 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik74da5292016-12-20 11:13:03 -0800614 InductionInfo* new_a = op == kAdd ? b : TransferNeg(b);
615 if (new_a != nullptr) {
616 return CreateInduction(kPolynomial, kNop, new_a, initial, /*fetch*/ nullptr, type_);
617 }
Aart Bike609b7c2015-08-27 13:46:58 -0700618 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700619 }
620 }
Aart Bike609b7c2015-08-27 13:46:58 -0700621
622 // Try some alternatives before failing.
623 if (op == kAdd) {
624 // Try the other way around for an addition if considered for first time.
625 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700626 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700627 }
628 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700629 // Solve within a tight cycle that is formed by exactly two instructions,
Aart Bikc071a012016-12-01 10:22:31 -0800630 // one phi and one update, for a periodic idiom of the form k = c - k.
Aart Bikf475bee2015-09-16 12:50:25 -0700631 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700632 InductionInfo* a = LookupInfo(loop, x);
633 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700634 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800635 return CreateInduction(kPeriodic,
636 kNop,
637 CreateInvariantOp(kSub, a, initial),
638 initial,
639 /*fetch*/ nullptr,
640 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700641 }
642 }
643 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700644 return nullptr;
645}
646
Aart Bikdf7822e2016-12-06 10:05:30 -0800647HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveOp(HLoopInformation* loop,
Aart Bikc071a012016-12-01 10:22:31 -0800648 HInstruction* entry_phi,
649 HInstruction* instruction,
650 HInstruction* x,
651 HInstruction* y,
652 InductionOp op) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800653 // 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 -0700654 if (entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800655 InductionInfo* c = nullptr;
Aart Bik639cc8c2016-10-18 13:03:31 -0700656 InductionInfo* b = LookupInfo(loop, y);
657 if (b != nullptr && b->induction_class == kInvariant && entry_phi == x) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800658 c = b;
659 } else if (op != kDiv && op != kRem) {
660 InductionInfo* a = LookupInfo(loop, x);
661 if (a != nullptr && a->induction_class == kInvariant && entry_phi == y) {
662 c = a;
663 }
664 }
665 // Found suitable operand left or right?
666 if (c != nullptr) {
667 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
668 switch (op) {
669 case kMul:
670 case kDiv:
671 // Restrict base of geometric induction to direct fetch.
672 if (c->operation == kFetch) {
673 return CreateInduction(kGeometric,
674 op,
675 initial,
676 CreateConstant(0, type_),
677 c->fetch,
678 type_);
679 };
680 break;
681 case kRem:
682 // Idiomatic MOD wrap-around induction.
683 return CreateInduction(kWrapAround,
684 kNop,
685 initial,
686 CreateInvariantOp(kRem, initial, c),
687 /*fetch*/ nullptr,
688 type_);
689 case kXor:
690 // Idiomatic XOR periodic induction.
691 return CreateInduction(kPeriodic,
692 kNop,
693 CreateInvariantOp(kXor, initial, c),
694 initial,
695 /*fetch*/ nullptr,
696 type_);
697 default:
698 CHECK(false) << op;
699 break;
700 }
Aart Bik7dc96932016-10-12 10:01:05 -0700701 }
702 }
Aart Bik639cc8c2016-10-18 13:03:31 -0700703 return nullptr;
704}
705
706HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveTest(HLoopInformation* loop,
707 HInstruction* entry_phi,
708 HInstruction* instruction,
709 int64_t opposite_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800710 // Detect hidden XOR construction in x = (x == false) or x = (x != true).
Aart Bik639cc8c2016-10-18 13:03:31 -0700711 int64_t value = -1;
712 HInstruction* x = instruction->InputAt(0);
713 HInstruction* y = instruction->InputAt(1);
714 if (IsExact(LookupInfo(loop, x), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800715 return SolveOp(loop, entry_phi, instruction, graph_->GetIntConstant(1), y, kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700716 } else if (IsExact(LookupInfo(loop, y), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800717 return SolveOp(loop, entry_phi, instruction, x, graph_->GetIntConstant(1), kXor);
Aart Bik7dc96932016-10-12 10:01:05 -0700718 }
719 return nullptr;
720}
721
Aart Bike6bd0272016-12-16 13:57:52 -0800722HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveConversion(
723 HLoopInformation* loop,
724 HInstruction* entry_phi,
725 HTypeConversion* conversion) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700726 Primitive::Type from = conversion->GetInputType();
727 Primitive::Type to = conversion->GetResultType();
Aart Bike6bd0272016-12-16 13:57:52 -0800728 // A narrowing conversion is allowed as *last* operation of the cycle of a linear induction
729 // with an initial value that fits the type, provided that the narrowest encountered type is
730 // recorded with the induction to account for the precision loss. The narrower induction does
731 // *not* transfer to any wider operations, however, since these may yield out-of-type values
732 if (entry_phi->InputCount() == 2 && conversion == entry_phi->InputAt(1)) {
733 int64_t min = Primitive::MinValueOfIntegralType(to);
734 int64_t max = Primitive::MaxValueOfIntegralType(to);
735 int64_t value = 0;
736 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
737 if (IsNarrowingIntegralConversion(from, to) &&
738 IsAtLeast(initial, &value) && value >= min &&
739 IsAtMost(initial, &value) && value <= max) {
740 auto it = cycle_.find(conversion->GetInput());
741 if (it != cycle_.end() && it->second->induction_class == kInvariant) {
742 type_ = to;
743 return it->second;
744 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700745 }
746 }
747 return nullptr;
748}
749
Aart Bikd14c5952015-09-08 15:25:15 -0700750void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
751 HInstruction* control = loop->GetHeader()->GetLastInstruction();
752 if (control->IsIf()) {
753 HIf* ifs = control->AsIf();
754 HBasicBlock* if_true = ifs->IfTrueSuccessor();
755 HBasicBlock* if_false = ifs->IfFalseSuccessor();
756 HInstruction* if_expr = ifs->InputAt(0);
757 // Determine if loop has following structure in header.
758 // loop-header: ....
759 // if (condition) goto X
760 if (if_expr->IsCondition()) {
761 HCondition* condition = if_expr->AsCondition();
762 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
763 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
Aart Bike6bd0272016-12-16 13:57:52 -0800764 Primitive::Type type = ImplicitConversion(condition->InputAt(0)->GetType());
Aart Bik0d345cf2016-03-16 10:49:38 -0700765 // Determine if the loop control uses a known sequence on an if-exit (X outside) or on
766 // an if-iterate (X inside), expressed as if-iterate when passed into VisitCondition().
767 if (a == nullptr || b == nullptr) {
768 return; // Loop control is not a sequence.
Aart Bikd14c5952015-09-08 15:25:15 -0700769 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
770 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
771 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
772 VisitCondition(loop, a, b, type, condition->GetCondition());
773 }
774 }
775 }
776}
777
778void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
779 InductionInfo* a,
780 InductionInfo* b,
781 Primitive::Type type,
782 IfCondition cmp) {
783 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700784 // 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 -0700785 switch (cmp) {
786 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
787 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
788 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
789 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700790 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700791 default: break;
792 }
793 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700794 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700795 InductionInfo* lower_expr = a->op_b;
796 InductionInfo* upper_expr = b;
Aart Bik97412c922016-02-19 20:14:38 -0800797 InductionInfo* stride_expr = a->op_a;
798 // Constant stride?
Aart Bik9401f532015-09-28 16:25:56 -0700799 int64_t stride_value = 0;
Aart Bik97412c922016-02-19 20:14:38 -0800800 if (!IsExact(stride_expr, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700801 return;
802 }
Aart Bik358af832016-02-24 14:17:53 -0800803 // Rewrite condition i != U into strict end condition i < U or i > U if this end condition
804 // is reached exactly (tested by verifying if the loop has a unit stride and the non-strict
805 // condition would be always taken).
806 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLE)) ||
807 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGE)))) {
Aart Bik9401f532015-09-28 16:25:56 -0700808 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700809 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700810 // Only accept integral condition. A mismatch between the type of condition and the induction
811 // is only allowed if the, necessarily narrower, induction range fits the narrower control.
812 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
813 return; // not integral
814 } else if (type != a->type &&
815 !FitsNarrowerControl(lower_expr, upper_expr, stride_value, a->type, cmp)) {
816 return; // mismatched type
817 }
Aart Bikf475bee2015-09-16 12:50:25 -0700818 // Normalize a linear loop control with a nonzero stride:
819 // stride > 0, either i < U or i <= U
820 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700821 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
822 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik97412c922016-02-19 20:14:38 -0800823 VisitTripCount(loop, lower_expr, upper_expr, stride_expr, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700824 }
Aart Bikd14c5952015-09-08 15:25:15 -0700825 }
826}
827
828void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700829 InductionInfo* lower_expr,
830 InductionInfo* upper_expr,
Aart Bik97412c922016-02-19 20:14:38 -0800831 InductionInfo* stride_expr,
Aart Bik9401f532015-09-28 16:25:56 -0700832 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700833 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700834 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700835 // Any loop of the general form:
836 //
837 // for (i = L; i <= U; i += S) // S > 0
838 // or for (i = L; i >= U; i += S) // S < 0
839 // .. i ..
840 //
841 // can be normalized into:
842 //
843 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
844 // .. L + S * n ..
845 //
Aart Bik9401f532015-09-28 16:25:56 -0700846 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700847 //
Aart Bik9401f532015-09-28 16:25:56 -0700848 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
849 // an unsigned entity, for example, as in the following loop that uses the full range:
850 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
851 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
Aart Bikd5cc6832016-06-22 16:34:46 -0700852 // for (int i = 12; i < U; i++) // TC = 0 when U <= 12
Aart Bik9401f532015-09-28 16:25:56 -0700853 // If this cannot be determined at compile-time, the TC is only valid within the
Aart Bik22f05872015-10-27 15:56:28 -0700854 // loop-body proper, not the loop-header unless enforced with an explicit taken-test.
Aart Bik9401f532015-09-28 16:25:56 -0700855 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
856 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
857 // If this cannot be determined at compile-time, the TC is only valid when enforced
Aart Bik22f05872015-10-27 15:56:28 -0700858 // with an explicit finite-test.
Aart Bik9401f532015-09-28 16:25:56 -0700859 // (4) For loops which early-exits, the TC forms an upper bound, as in:
860 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
Aart Bik22f05872015-10-27 15:56:28 -0700861 InductionInfo* trip_count = upper_expr;
Aart Bik9401f532015-09-28 16:25:56 -0700862 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
863 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
864 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700865 if (!cancels) {
866 // Convert exclusive integral inequality into inclusive integral inequality,
867 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700868 if (cmp == kCondLT) {
Aart Bik22f05872015-10-27 15:56:28 -0700869 trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700870 } else if (cmp == kCondGT) {
Aart Bik22f05872015-10-27 15:56:28 -0700871 trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700872 }
873 // Compensate for stride.
Aart Bik97412c922016-02-19 20:14:38 -0800874 trip_count = CreateInvariantOp(kAdd, trip_count, stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700875 }
Aart Bik97412c922016-02-19 20:14:38 -0800876 trip_count = CreateInvariantOp(
877 kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700878 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700879 // should be aware that the expression is only valid under the conditions listed above.
Aart Bik22f05872015-10-27 15:56:28 -0700880 InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests
Aart Bik9401f532015-09-28 16:25:56 -0700881 if (is_taken && is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700882 tcKind = kTripCountInLoop; // needs neither test
Aart Bik9401f532015-09-28 16:25:56 -0700883 } else if (is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700884 tcKind = kTripCountInBody; // needs taken-test
Aart Bik9401f532015-09-28 16:25:56 -0700885 } else if (is_taken) {
Aart Bik22f05872015-10-27 15:56:28 -0700886 tcKind = kTripCountInLoopUnsafe; // needs finite-test
Aart Bik9401f532015-09-28 16:25:56 -0700887 }
Aart Bik22f05872015-10-27 15:56:28 -0700888 InductionOp op = kNop;
889 switch (cmp) {
890 case kCondLT: op = kLT; break;
891 case kCondLE: op = kLE; break;
892 case kCondGT: op = kGT; break;
893 case kCondGE: op = kGE; break;
894 default: LOG(FATAL) << "CONDITION UNREACHABLE";
895 }
Aart Bik009cace2016-09-16 10:15:19 -0700896 // Associate trip count with control instruction, rather than the condition (even
897 // though it's its use) since former provides a convenient use-free placeholder.
898 HInstruction* control = loop->GetHeader()->GetLastInstruction();
Aart Bik22f05872015-10-27 15:56:28 -0700899 InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr);
Aart Bik009cace2016-09-16 10:15:19 -0700900 DCHECK(control->IsIf());
901 AssignInfo(loop, control, CreateTripCount(tcKind, trip_count, taken_test, type));
Aart Bik9401f532015-09-28 16:25:56 -0700902}
903
904bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
905 InductionInfo* upper_expr,
906 IfCondition cmp) {
907 int64_t lower_value;
908 int64_t upper_value;
Aart Bik97412c922016-02-19 20:14:38 -0800909 switch (cmp) {
910 case kCondLT:
911 return IsAtMost(lower_expr, &lower_value)
912 && IsAtLeast(upper_expr, &upper_value)
913 && lower_value < upper_value;
914 case kCondLE:
915 return IsAtMost(lower_expr, &lower_value)
916 && IsAtLeast(upper_expr, &upper_value)
917 && lower_value <= upper_value;
918 case kCondGT:
919 return IsAtLeast(lower_expr, &lower_value)
920 && IsAtMost(upper_expr, &upper_value)
921 && lower_value > upper_value;
922 case kCondGE:
923 return IsAtLeast(lower_expr, &lower_value)
924 && IsAtMost(upper_expr, &upper_value)
925 && lower_value >= upper_value;
926 default:
927 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700928 }
929 return false; // not certain, may be untaken
930}
931
932bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
933 int64_t stride_value,
934 Primitive::Type type,
935 IfCondition cmp) {
Aart Bike6bd0272016-12-16 13:57:52 -0800936 int64_t min = Primitive::MinValueOfIntegralType(type);
937 int64_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik9401f532015-09-28 16:25:56 -0700938 // Some rules under which it is certain at compile-time that the loop is finite.
939 int64_t value;
940 switch (cmp) {
941 case kCondLT:
942 return stride_value == 1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800943 (IsAtMost(upper_expr, &value) && value <= (max - stride_value + 1));
Aart Bik9401f532015-09-28 16:25:56 -0700944 case kCondLE:
Aart Bik97412c922016-02-19 20:14:38 -0800945 return (IsAtMost(upper_expr, &value) && value <= (max - stride_value));
Aart Bik9401f532015-09-28 16:25:56 -0700946 case kCondGT:
947 return stride_value == -1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800948 (IsAtLeast(upper_expr, &value) && value >= (min - stride_value - 1));
Aart Bik9401f532015-09-28 16:25:56 -0700949 case kCondGE:
Aart Bik97412c922016-02-19 20:14:38 -0800950 return (IsAtLeast(upper_expr, &value) && value >= (min - stride_value));
Aart Bike9f37602015-10-09 11:15:55 -0700951 default:
952 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700953 }
954 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700955}
956
Aart Bik0d345cf2016-03-16 10:49:38 -0700957bool HInductionVarAnalysis::FitsNarrowerControl(InductionInfo* lower_expr,
958 InductionInfo* upper_expr,
959 int64_t stride_value,
960 Primitive::Type type,
961 IfCondition cmp) {
962 int64_t min = Primitive::MinValueOfIntegralType(type);
963 int64_t max = Primitive::MaxValueOfIntegralType(type);
964 // Inclusive test need one extra.
965 if (stride_value != 1 && stride_value != -1) {
966 return false; // non-unit stride
967 } else if (cmp == kCondLE) {
968 max--;
969 } else if (cmp == kCondGE) {
970 min++;
971 }
972 // Do both bounds fit the range?
Vladimir Marko0e2f2ff2016-03-22 12:31:54 +0000973 int64_t value = 0;
Aart Bik0d345cf2016-03-16 10:49:38 -0700974 return IsAtLeast(lower_expr, &value) && value >= min &&
975 IsAtMost(lower_expr, &value) && value <= max &&
976 IsAtLeast(upper_expr, &value) && value >= min &&
977 IsAtMost(upper_expr, &value) && value <= max;
978}
979
Aart Bik30efb4e2015-07-30 12:14:31 -0700980void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
981 HInstruction* instruction,
982 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700983 auto it = induction_.find(loop);
984 if (it == induction_.end()) {
985 it = induction_.Put(loop,
986 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100987 std::less<HInstruction*>(),
988 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700989 }
990 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700991}
992
Aart Bike609b7c2015-08-27 13:46:58 -0700993HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
994 HInstruction* instruction) {
995 auto it = induction_.find(loop);
996 if (it != induction_.end()) {
997 auto loop_it = it->second.find(instruction);
998 if (loop_it != it->second.end()) {
999 return loop_it->second;
1000 }
Aart Bik30efb4e2015-07-30 12:14:31 -07001001 }
Mingyao Yang4b467ed2015-11-19 17:04:22 -08001002 if (loop->IsDefinedOutOfTheLoop(instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -07001003 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -07001004 AssignInfo(loop, instruction, info);
1005 return info;
1006 }
1007 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -07001008}
1009
Aart Bikd14c5952015-09-08 15:25:15 -07001010HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
1011 Primitive::Type type) {
Aart Bikc071a012016-12-01 10:22:31 -08001012 HInstruction* constant;
1013 switch (type) {
1014 case Primitive::kPrimDouble: constant = graph_->GetDoubleConstant(value); break;
1015 case Primitive::kPrimFloat: constant = graph_->GetFloatConstant(value); break;
1016 case Primitive::kPrimLong: constant = graph_->GetLongConstant(value); break;
1017 default: constant = graph_->GetIntConstant(value); break;
Aart Bikd14c5952015-09-08 15:25:15 -07001018 }
Aart Bikc071a012016-12-01 10:22:31 -08001019 return CreateInvariantFetch(constant);
Aart Bikd14c5952015-09-08 15:25:15 -07001020}
1021
Aart Bik471a2032015-09-04 18:22:11 -07001022HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
1023 InductionOp op,
1024 InductionInfo* a,
1025 InductionInfo* b) {
1026 // Perform some light-weight simplifications during construction of a new invariant.
1027 // This often safes memory and yields a more concise representation of the induction.
1028 // More exhaustive simplifications are done by later phases once induction nodes are
1029 // translated back into HIR code (e.g. by loop optimizations or BCE).
1030 int64_t value = -1;
Aart Bik97412c922016-02-19 20:14:38 -08001031 if (IsExact(a, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001032 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001033 // Simplify 0 + b = b, 0 ^ b = b, 0 * b = 0.
1034 if (op == kAdd || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001035 return b;
1036 } else if (op == kMul) {
1037 return a;
1038 }
Aart Bikd14c5952015-09-08 15:25:15 -07001039 } else if (op == kMul) {
1040 // Simplify 1 * b = b, -1 * b = -b
1041 if (value == 1) {
1042 return b;
1043 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001044 return CreateSimplifiedInvariant(kNeg, nullptr, b);
Aart Bikd14c5952015-09-08 15:25:15 -07001045 }
Aart Bik471a2032015-09-04 18:22:11 -07001046 }
1047 }
Aart Bik97412c922016-02-19 20:14:38 -08001048 if (IsExact(b, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001049 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001050 // Simplify a + 0 = a, a - 0 = a, a ^ 0 = a, a * 0 = 0, -0 = 0.
1051 if (op == kAdd || op == kSub || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001052 return a;
1053 } else if (op == kMul || op == kNeg) {
1054 return b;
1055 }
Aart Bikd14c5952015-09-08 15:25:15 -07001056 } else if (op == kMul || op == kDiv) {
1057 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
1058 if (value == 1) {
1059 return a;
1060 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001061 return CreateSimplifiedInvariant(kNeg, nullptr, a);
Aart Bikd14c5952015-09-08 15:25:15 -07001062 }
Aart Bik471a2032015-09-04 18:22:11 -07001063 }
1064 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -07001065 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
1066 if (op == kAdd) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001067 return CreateSimplifiedInvariant(kSub, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001068 } else if (op == kSub) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001069 return CreateSimplifiedInvariant(kAdd, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001070 } else if (op == kNeg) {
1071 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -07001072 }
Aart Bik7d57d7f2015-12-09 14:39:48 -08001073 } else if (b->operation == kSub) {
1074 // Simplify - (a - b) = b - a.
1075 if (op == kNeg) {
1076 return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a);
1077 }
Aart Bik471a2032015-09-04 18:22:11 -07001078 }
Aart Bike6bd0272016-12-16 13:57:52 -08001079 return new (graph_->GetArena()) InductionInfo(
1080 kInvariant, op, a, b, nullptr, ImplicitConversion(b->type));
Aart Bik471a2032015-09-04 18:22:11 -07001081}
1082
Aart Bikd0a022d2016-12-13 11:22:31 -08001083HInstruction* HInductionVarAnalysis::GetShiftConstant(HLoopInformation* loop,
1084 HInstruction* instruction,
1085 InductionInfo* initial) {
1086 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
1087 // Shift-rights are only the same as division for non-negative initial inputs.
1088 // Otherwise we would round incorrectly.
1089 if (initial != nullptr) {
1090 int64_t value = -1;
1091 if (!IsAtLeast(initial, &value) || value < 0) {
1092 return nullptr;
1093 }
1094 }
1095 // Obtain the constant needed to treat shift as equivalent multiplication or division.
1096 // This yields an existing instruction if the constant is already there. Otherwise, this
1097 // has a side effect on the HIR. The restriction on the shift factor avoids generating a
1098 // negative constant (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that
1099 // generalization for shift factors outside [0,32) and [0,64) ranges is done earlier.
Aart Bikc071a012016-12-01 10:22:31 -08001100 InductionInfo* b = LookupInfo(loop, instruction->InputAt(1));
1101 int64_t value = -1;
1102 if (IsExact(b, &value)) {
1103 Primitive::Type type = instruction->InputAt(0)->GetType();
1104 if (type == Primitive::kPrimInt && 0 <= value && value < 31) {
1105 return graph_->GetIntConstant(1 << value);
1106 }
1107 if (type == Primitive::kPrimLong && 0 <= value && value < 63) {
1108 return graph_->GetLongConstant(1L << value);
1109 }
1110 }
1111 return nullptr;
1112}
Aart Bikcc42be02016-10-20 16:14:16 -07001113
1114void HInductionVarAnalysis::AssignCycle(HPhi* phi) {
1115 ArenaSet<HInstruction*>* set = &cycles_.Put(phi, ArenaSet<HInstruction*>(
1116 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)))->second;
1117 for (HInstruction* i : scc_) {
1118 set->insert(i);
1119 }
1120}
1121
1122ArenaSet<HInstruction*>* HInductionVarAnalysis::LookupCycle(HPhi* phi) {
1123 auto it = cycles_.find(phi);
1124 if (it != cycles_.end()) {
1125 return &it->second;
1126 }
1127 return nullptr;
1128}
1129
Aart Bik97412c922016-02-19 20:14:38 -08001130bool HInductionVarAnalysis::IsExact(InductionInfo* info, int64_t* value) {
1131 return InductionVarRange(this).IsConstant(info, InductionVarRange::kExact, value);
1132}
1133
1134bool HInductionVarAnalysis::IsAtMost(InductionInfo* info, int64_t* value) {
1135 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtMost, value);
1136}
1137
1138bool HInductionVarAnalysis::IsAtLeast(InductionInfo* info, int64_t* value) {
1139 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtLeast, value);
Aart Bik7d57d7f2015-12-09 14:39:48 -08001140}
1141
Aart Bike6bd0272016-12-16 13:57:52 -08001142bool HInductionVarAnalysis::IsNarrowingLinear(InductionInfo* info) {
1143 return info != nullptr &&
1144 info->induction_class == kLinear &&
1145 (info->type == Primitive::kPrimByte ||
1146 info->type == Primitive::kPrimShort ||
1147 info->type == Primitive::kPrimChar ||
1148 (info->type == Primitive::kPrimInt && (info->op_a->type == Primitive::kPrimLong ||
1149 info->op_b->type == Primitive::kPrimLong)));
1150}
1151
Aart Bik30efb4e2015-07-30 12:14:31 -07001152bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
1153 InductionInfo* info2) {
1154 // Test structural equality only, without accounting for simplifications.
1155 if (info1 != nullptr && info2 != nullptr) {
1156 return
1157 info1->induction_class == info2->induction_class &&
1158 info1->operation == info2->operation &&
1159 info1->fetch == info2->fetch &&
Aart Bik78296912016-03-25 13:14:53 -07001160 info1->type == info2->type &&
Aart Bik30efb4e2015-07-30 12:14:31 -07001161 InductionEqual(info1->op_a, info2->op_a) &&
1162 InductionEqual(info1->op_b, info2->op_b);
1163 }
1164 // Otherwise only two nullptrs are considered equal.
1165 return info1 == info2;
1166}
1167
Aart Bikc071a012016-12-01 10:22:31 -08001168std::string HInductionVarAnalysis::FetchToString(HInstruction* fetch) {
1169 DCHECK(fetch != nullptr);
1170 if (fetch->IsIntConstant()) {
1171 return std::to_string(fetch->AsIntConstant()->GetValue());
1172 } else if (fetch->IsLongConstant()) {
1173 return std::to_string(fetch->AsLongConstant()->GetValue());
1174 }
1175 return std::to_string(fetch->GetId()) + ":" + fetch->DebugName();
1176}
1177
Aart Bik30efb4e2015-07-30 12:14:31 -07001178std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
1179 if (info != nullptr) {
1180 if (info->induction_class == kInvariant) {
1181 std::string inv = "(";
1182 inv += InductionToString(info->op_a);
1183 switch (info->operation) {
Aart Bik22f05872015-10-27 15:56:28 -07001184 case kNop: inv += " @ "; break;
1185 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001186 case kSub:
Aart Bik22f05872015-10-27 15:56:28 -07001187 case kNeg: inv += " - "; break;
1188 case kMul: inv += " * "; break;
1189 case kDiv: inv += " / "; break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001190 case kRem: inv += " % "; break;
Aart Bik7dc96932016-10-12 10:01:05 -07001191 case kXor: inv += " ^ "; break;
Aart Bik22f05872015-10-27 15:56:28 -07001192 case kLT: inv += " < "; break;
1193 case kLE: inv += " <= "; break;
1194 case kGT: inv += " > "; break;
1195 case kGE: inv += " >= "; break;
Aart Bikc071a012016-12-01 10:22:31 -08001196 case kFetch: inv += FetchToString(info->fetch); break;
Aart Bik22f05872015-10-27 15:56:28 -07001197 case kTripCountInLoop: inv += " (TC-loop) "; break;
1198 case kTripCountInBody: inv += " (TC-body) "; break;
1199 case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break;
1200 case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001201 }
1202 inv += InductionToString(info->op_b);
Aart Bik0d345cf2016-03-16 10:49:38 -07001203 inv += ")";
1204 return inv;
Aart Bik30efb4e2015-07-30 12:14:31 -07001205 } else {
Aart Bik30efb4e2015-07-30 12:14:31 -07001206 if (info->induction_class == kLinear) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001207 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001208 return "(" + InductionToString(info->op_a) + " * i + " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001209 InductionToString(info->op_b) + "):" +
1210 Primitive::PrettyDescriptor(info->type);
Aart Bikc071a012016-12-01 10:22:31 -08001211 } else if (info->induction_class == kPolynomial) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001212 DCHECK(info->operation == kNop);
1213 return "poly(sum_lt(" + InductionToString(info->op_a) + ") + " +
Aart Bikc071a012016-12-01 10:22:31 -08001214 InductionToString(info->op_b) + "):" +
1215 Primitive::PrettyDescriptor(info->type);
1216 } else if (info->induction_class == kGeometric) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001217 DCHECK(info->operation == kMul || info->operation == kDiv);
Aart Bikc071a012016-12-01 10:22:31 -08001218 DCHECK(info->fetch != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001219 return "geo(" + InductionToString(info->op_a) + " * " +
1220 FetchToString(info->fetch) +
1221 (info->operation == kMul ? " ^ i + " : " ^ -i + ") +
1222 InductionToString(info->op_b) + "):" +
1223 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001224 } else if (info->induction_class == kWrapAround) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001225 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001226 return "wrap(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001227 InductionToString(info->op_b) + "):" +
1228 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001229 } else if (info->induction_class == kPeriodic) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001230 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001231 return "periodic(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001232 InductionToString(info->op_b) + "):" +
1233 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001234 }
1235 }
1236 }
1237 return "";
1238}
1239
1240} // namespace art