blob: 5456b1e9bf83d39a380542d8b0ce01d14ac00503 [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 Bik471a2032015-09-04 18:22:11 -0700434 return CreateInvariantOp(op, a, b);
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)) {
437 return CreateInduction(a->induction_class,
438 a->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700439 TransferAddSub(a->op_a, b->op_a, op),
440 TransferAddSub(a->op_b, b->op_b, op),
Aart Bikc071a012016-12-01 10:22:31 -0800441 /*fetch*/ nullptr,
Aart Bik0d345cf2016-03-16 10:49:38 -0700442 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700443 } else if (a->induction_class == kInvariant) {
444 InductionInfo* new_a = b->op_a;
445 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800446 if (b->induction_class == kWrapAround || b->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700447 new_a = TransferAddSub(a, new_a, op);
448 } else if (op == kSub) { // Negation required.
449 new_a = TransferNeg(new_a);
450 }
Aart Bikc071a012016-12-01 10:22:31 -0800451 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700452 } else if (b->induction_class == kInvariant) {
453 InductionInfo* new_a = a->op_a;
454 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800455 if (a->induction_class == kWrapAround || a->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700456 new_a = TransferAddSub(new_a, b, op);
457 }
Aart Bikc071a012016-12-01 10:22:31 -0800458 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
459 }
460 }
461 return nullptr;
462}
463
464HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
465 // Transfer over a unary negation: an invariant, linear, polynomial, geometric (mul),
466 // wrap-around, or periodic input yields a similar but negated induction as result.
467 if (a != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800468 if (IsNarrowingLinear(a)) {
469 return nullptr; // no transfer
470 } else if (a->induction_class == kInvariant) {
Aart Bikc071a012016-12-01 10:22:31 -0800471 return CreateInvariantOp(kNeg, nullptr, a);
472 } else if (a->induction_class != kGeometric || a->operation == kMul) {
473 return CreateInduction(a->induction_class,
474 a->operation,
475 TransferNeg(a->op_a),
476 TransferNeg(a->op_b),
477 a->fetch,
478 type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700479 }
480 }
481 return nullptr;
482}
483
484HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
485 InductionInfo* b) {
Aart Bikc071a012016-12-01 10:22:31 -0800486 // Transfer over a multiplication: any invariant, linear, polynomial, geometric (mul),
487 // wrap-around, or periodic can be multiplied with an invariant to yield a similar
488 // but multiplied result. Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700489 if (a != nullptr && b != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800490 if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) {
491 return nullptr; // no transfer
492 } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700493 return CreateInvariantOp(kMul, a, b);
Aart Bikc071a012016-12-01 10:22:31 -0800494 } else if (a->induction_class == kInvariant && (b->induction_class != kGeometric ||
495 b->operation == kMul)) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700496 return CreateInduction(b->induction_class,
Aart Bikc071a012016-12-01 10:22:31 -0800497 b->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700498 TransferMul(a, b->op_a),
499 TransferMul(a, b->op_b),
Aart Bikc071a012016-12-01 10:22:31 -0800500 b->fetch,
Aart Bik0d345cf2016-03-16 10:49:38 -0700501 type_);
Aart Bikc071a012016-12-01 10:22:31 -0800502 } else if (b->induction_class == kInvariant && (a->induction_class != kGeometric ||
503 a->operation == kMul)) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700504 return CreateInduction(a->induction_class,
Aart Bikc071a012016-12-01 10:22:31 -0800505 a->operation,
Aart Bik0d345cf2016-03-16 10:49:38 -0700506 TransferMul(a->op_a, b),
507 TransferMul(a->op_b, b),
Aart Bikc071a012016-12-01 10:22:31 -0800508 a->fetch,
Aart Bik0d345cf2016-03-16 10:49:38 -0700509 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700510 }
511 }
512 return nullptr;
513}
514
Aart Bike6bd0272016-12-16 13:57:52 -0800515HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferConversion(
516 InductionInfo* a,
517 Primitive::Type from,
518 Primitive::Type to) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700519 if (a != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800520 // Allow narrowing conversion on linear induction in certain cases:
521 // induction is already at narrow type, or can be made narrower.
522 if (IsNarrowingIntegralConversion(from, to) &&
523 a->induction_class == kLinear &&
524 (a->type == to || IsNarrowingIntegralConversion(a->type, to))) {
525 return CreateInduction(kLinear, kNop, a->op_a, a->op_b, /*fetch*/ nullptr, to);
Aart Bik0d345cf2016-03-16 10:49:38 -0700526 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700527 }
528 return nullptr;
529}
530
Aart Bikf475bee2015-09-16 12:50:25 -0700531HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800532 size_t input_index,
533 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700534 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100535 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100536 DCHECK_LT(input_index, inputs.size());
537 auto ita = cycle_.find(inputs[input_index]);
Aart Bik30efb4e2015-07-30 12:14:31 -0700538 if (ita != cycle_.end()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800539 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100540 auto itb = cycle_.find(inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700541 if (itb == cycle_.end() ||
542 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700543 return nullptr;
544 }
545 }
Aart Bikf475bee2015-09-16 12:50:25 -0700546 return ita->second;
547 }
548 return nullptr;
549}
550
551HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
552 HLoopInformation* loop,
553 HInstruction* entry_phi,
554 HInstruction* phi) {
555 // Match all phi inputs.
Aart Bikd0a022d2016-12-13 11:22:31 -0800556 InductionInfo* match = SolvePhi(phi, /*input_index*/ 0, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700557 if (match != nullptr) {
558 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700559 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700560
Aart Bikf475bee2015-09-16 12:50:25 -0700561 // Otherwise, try to solve for a periodic seeded from phi onward.
562 // Only tight multi-statement cycles are considered in order to
563 // simplify rotating the periodic during the final classification.
564 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
565 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700566 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700567 if (phi->InputAt(1) == entry_phi) {
568 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800569 return CreateInduction(kPeriodic, kNop, a, initial, /*fetch*/ nullptr, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700570 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800571 InductionInfo* b = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700572 if (b != nullptr && b->induction_class == kPeriodic) {
Aart Bikc071a012016-12-01 10:22:31 -0800573 return CreateInduction(kPeriodic, kNop, a, b, /*fetch*/ nullptr, type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700574 }
575 }
576 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700577 return nullptr;
578}
579
Aart Bike609b7c2015-08-27 13:46:58 -0700580HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700581 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700582 HInstruction* instruction,
583 HInstruction* x,
584 HInstruction* y,
585 InductionOp op,
586 bool is_first_call) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800587 // Solve within a cycle over an addition or subtraction.
Aart Bike609b7c2015-08-27 13:46:58 -0700588 InductionInfo* b = LookupInfo(loop, y);
Aart Bikdf7822e2016-12-06 10:05:30 -0800589 if (b != nullptr) {
590 if (b->induction_class == kInvariant) {
591 // Adding or subtracting an invariant value, seeded from phi,
592 // keeps adding to the stride of the linear induction.
593 if (x == entry_phi) {
594 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
595 }
596 auto it = cycle_.find(x);
597 if (it != cycle_.end()) {
598 InductionInfo* a = it->second;
599 if (a->induction_class == kInvariant) {
600 return CreateInvariantOp(op, a, b);
601 }
602 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800603 } else if (b->induction_class == kLinear) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800604 // Solve within a tight cycle that adds a term that is already classified as a linear
605 // induction for a polynomial induction k = k + i (represented as sum over linear terms).
606 if (x == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
607 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
608 return CreateInduction(kPolynomial,
609 kNop,
Aart Bikd0a022d2016-12-13 11:22:31 -0800610 op == kAdd ? b : TransferNeg(b),
Aart Bikdf7822e2016-12-06 10:05:30 -0800611 initial,
612 /*fetch*/ nullptr,
613 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700614 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700615 }
616 }
Aart Bike609b7c2015-08-27 13:46:58 -0700617
618 // Try some alternatives before failing.
619 if (op == kAdd) {
620 // Try the other way around for an addition if considered for first time.
621 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700622 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700623 }
624 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700625 // Solve within a tight cycle that is formed by exactly two instructions,
Aart Bikc071a012016-12-01 10:22:31 -0800626 // one phi and one update, for a periodic idiom of the form k = c - k.
Aart Bikf475bee2015-09-16 12:50:25 -0700627 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700628 InductionInfo* a = LookupInfo(loop, x);
629 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700630 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800631 return CreateInduction(kPeriodic,
632 kNop,
633 CreateInvariantOp(kSub, a, initial),
634 initial,
635 /*fetch*/ nullptr,
636 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700637 }
638 }
639 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700640 return nullptr;
641}
642
Aart Bikdf7822e2016-12-06 10:05:30 -0800643HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveOp(HLoopInformation* loop,
Aart Bikc071a012016-12-01 10:22:31 -0800644 HInstruction* entry_phi,
645 HInstruction* instruction,
646 HInstruction* x,
647 HInstruction* y,
648 InductionOp op) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800649 // 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 -0700650 if (entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800651 InductionInfo* c = nullptr;
Aart Bik639cc8c2016-10-18 13:03:31 -0700652 InductionInfo* b = LookupInfo(loop, y);
653 if (b != nullptr && b->induction_class == kInvariant && entry_phi == x) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800654 c = b;
655 } else if (op != kDiv && op != kRem) {
656 InductionInfo* a = LookupInfo(loop, x);
657 if (a != nullptr && a->induction_class == kInvariant && entry_phi == y) {
658 c = a;
659 }
660 }
661 // Found suitable operand left or right?
662 if (c != nullptr) {
663 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
664 switch (op) {
665 case kMul:
666 case kDiv:
667 // Restrict base of geometric induction to direct fetch.
668 if (c->operation == kFetch) {
669 return CreateInduction(kGeometric,
670 op,
671 initial,
672 CreateConstant(0, type_),
673 c->fetch,
674 type_);
675 };
676 break;
677 case kRem:
678 // Idiomatic MOD wrap-around induction.
679 return CreateInduction(kWrapAround,
680 kNop,
681 initial,
682 CreateInvariantOp(kRem, initial, c),
683 /*fetch*/ nullptr,
684 type_);
685 case kXor:
686 // Idiomatic XOR periodic induction.
687 return CreateInduction(kPeriodic,
688 kNop,
689 CreateInvariantOp(kXor, initial, c),
690 initial,
691 /*fetch*/ nullptr,
692 type_);
693 default:
694 CHECK(false) << op;
695 break;
696 }
Aart Bik7dc96932016-10-12 10:01:05 -0700697 }
698 }
Aart Bik639cc8c2016-10-18 13:03:31 -0700699 return nullptr;
700}
701
702HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveTest(HLoopInformation* loop,
703 HInstruction* entry_phi,
704 HInstruction* instruction,
705 int64_t opposite_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800706 // Detect hidden XOR construction in x = (x == false) or x = (x != true).
Aart Bik639cc8c2016-10-18 13:03:31 -0700707 int64_t value = -1;
708 HInstruction* x = instruction->InputAt(0);
709 HInstruction* y = instruction->InputAt(1);
710 if (IsExact(LookupInfo(loop, x), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800711 return SolveOp(loop, entry_phi, instruction, graph_->GetIntConstant(1), y, kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700712 } else if (IsExact(LookupInfo(loop, y), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800713 return SolveOp(loop, entry_phi, instruction, x, graph_->GetIntConstant(1), kXor);
Aart Bik7dc96932016-10-12 10:01:05 -0700714 }
715 return nullptr;
716}
717
Aart Bike6bd0272016-12-16 13:57:52 -0800718HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveConversion(
719 HLoopInformation* loop,
720 HInstruction* entry_phi,
721 HTypeConversion* conversion) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700722 Primitive::Type from = conversion->GetInputType();
723 Primitive::Type to = conversion->GetResultType();
Aart Bike6bd0272016-12-16 13:57:52 -0800724 // A narrowing conversion is allowed as *last* operation of the cycle of a linear induction
725 // with an initial value that fits the type, provided that the narrowest encountered type is
726 // recorded with the induction to account for the precision loss. The narrower induction does
727 // *not* transfer to any wider operations, however, since these may yield out-of-type values
728 if (entry_phi->InputCount() == 2 && conversion == entry_phi->InputAt(1)) {
729 int64_t min = Primitive::MinValueOfIntegralType(to);
730 int64_t max = Primitive::MaxValueOfIntegralType(to);
731 int64_t value = 0;
732 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
733 if (IsNarrowingIntegralConversion(from, to) &&
734 IsAtLeast(initial, &value) && value >= min &&
735 IsAtMost(initial, &value) && value <= max) {
736 auto it = cycle_.find(conversion->GetInput());
737 if (it != cycle_.end() && it->second->induction_class == kInvariant) {
738 type_ = to;
739 return it->second;
740 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700741 }
742 }
743 return nullptr;
744}
745
Aart Bikd14c5952015-09-08 15:25:15 -0700746void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
747 HInstruction* control = loop->GetHeader()->GetLastInstruction();
748 if (control->IsIf()) {
749 HIf* ifs = control->AsIf();
750 HBasicBlock* if_true = ifs->IfTrueSuccessor();
751 HBasicBlock* if_false = ifs->IfFalseSuccessor();
752 HInstruction* if_expr = ifs->InputAt(0);
753 // Determine if loop has following structure in header.
754 // loop-header: ....
755 // if (condition) goto X
756 if (if_expr->IsCondition()) {
757 HCondition* condition = if_expr->AsCondition();
758 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
759 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
Aart Bike6bd0272016-12-16 13:57:52 -0800760 Primitive::Type type = ImplicitConversion(condition->InputAt(0)->GetType());
Aart Bik0d345cf2016-03-16 10:49:38 -0700761 // Determine if the loop control uses a known sequence on an if-exit (X outside) or on
762 // an if-iterate (X inside), expressed as if-iterate when passed into VisitCondition().
763 if (a == nullptr || b == nullptr) {
764 return; // Loop control is not a sequence.
Aart Bikd14c5952015-09-08 15:25:15 -0700765 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
766 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
767 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
768 VisitCondition(loop, a, b, type, condition->GetCondition());
769 }
770 }
771 }
772}
773
774void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
775 InductionInfo* a,
776 InductionInfo* b,
777 Primitive::Type type,
778 IfCondition cmp) {
779 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700780 // 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 -0700781 switch (cmp) {
782 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
783 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
784 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
785 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700786 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700787 default: break;
788 }
789 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700790 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700791 InductionInfo* lower_expr = a->op_b;
792 InductionInfo* upper_expr = b;
Aart Bik97412c922016-02-19 20:14:38 -0800793 InductionInfo* stride_expr = a->op_a;
794 // Constant stride?
Aart Bik9401f532015-09-28 16:25:56 -0700795 int64_t stride_value = 0;
Aart Bik97412c922016-02-19 20:14:38 -0800796 if (!IsExact(stride_expr, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700797 return;
798 }
Aart Bik358af832016-02-24 14:17:53 -0800799 // Rewrite condition i != U into strict end condition i < U or i > U if this end condition
800 // is reached exactly (tested by verifying if the loop has a unit stride and the non-strict
801 // condition would be always taken).
802 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLE)) ||
803 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGE)))) {
Aart Bik9401f532015-09-28 16:25:56 -0700804 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700805 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700806 // Only accept integral condition. A mismatch between the type of condition and the induction
807 // is only allowed if the, necessarily narrower, induction range fits the narrower control.
808 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
809 return; // not integral
810 } else if (type != a->type &&
811 !FitsNarrowerControl(lower_expr, upper_expr, stride_value, a->type, cmp)) {
812 return; // mismatched type
813 }
Aart Bikf475bee2015-09-16 12:50:25 -0700814 // Normalize a linear loop control with a nonzero stride:
815 // stride > 0, either i < U or i <= U
816 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700817 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
818 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik97412c922016-02-19 20:14:38 -0800819 VisitTripCount(loop, lower_expr, upper_expr, stride_expr, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700820 }
Aart Bikd14c5952015-09-08 15:25:15 -0700821 }
822}
823
824void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700825 InductionInfo* lower_expr,
826 InductionInfo* upper_expr,
Aart Bik97412c922016-02-19 20:14:38 -0800827 InductionInfo* stride_expr,
Aart Bik9401f532015-09-28 16:25:56 -0700828 int64_t stride_value,
Aart Bikd14c5952015-09-08 15:25:15 -0700829 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700830 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700831 // Any loop of the general form:
832 //
833 // for (i = L; i <= U; i += S) // S > 0
834 // or for (i = L; i >= U; i += S) // S < 0
835 // .. i ..
836 //
837 // can be normalized into:
838 //
839 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
840 // .. L + S * n ..
841 //
Aart Bik9401f532015-09-28 16:25:56 -0700842 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700843 //
Aart Bik9401f532015-09-28 16:25:56 -0700844 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
845 // an unsigned entity, for example, as in the following loop that uses the full range:
846 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
847 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
Aart Bikd5cc6832016-06-22 16:34:46 -0700848 // for (int i = 12; i < U; i++) // TC = 0 when U <= 12
Aart Bik9401f532015-09-28 16:25:56 -0700849 // If this cannot be determined at compile-time, the TC is only valid within the
Aart Bik22f05872015-10-27 15:56:28 -0700850 // loop-body proper, not the loop-header unless enforced with an explicit taken-test.
Aart Bik9401f532015-09-28 16:25:56 -0700851 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
852 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
853 // If this cannot be determined at compile-time, the TC is only valid when enforced
Aart Bik22f05872015-10-27 15:56:28 -0700854 // with an explicit finite-test.
Aart Bik9401f532015-09-28 16:25:56 -0700855 // (4) For loops which early-exits, the TC forms an upper bound, as in:
856 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
Aart Bik22f05872015-10-27 15:56:28 -0700857 InductionInfo* trip_count = upper_expr;
Aart Bik9401f532015-09-28 16:25:56 -0700858 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
859 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
860 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700861 if (!cancels) {
862 // Convert exclusive integral inequality into inclusive integral inequality,
863 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700864 if (cmp == kCondLT) {
Aart Bik22f05872015-10-27 15:56:28 -0700865 trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700866 } else if (cmp == kCondGT) {
Aart Bik22f05872015-10-27 15:56:28 -0700867 trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700868 }
869 // Compensate for stride.
Aart Bik97412c922016-02-19 20:14:38 -0800870 trip_count = CreateInvariantOp(kAdd, trip_count, stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700871 }
Aart Bik97412c922016-02-19 20:14:38 -0800872 trip_count = CreateInvariantOp(
873 kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700874 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700875 // should be aware that the expression is only valid under the conditions listed above.
Aart Bik22f05872015-10-27 15:56:28 -0700876 InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests
Aart Bik9401f532015-09-28 16:25:56 -0700877 if (is_taken && is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700878 tcKind = kTripCountInLoop; // needs neither test
Aart Bik9401f532015-09-28 16:25:56 -0700879 } else if (is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700880 tcKind = kTripCountInBody; // needs taken-test
Aart Bik9401f532015-09-28 16:25:56 -0700881 } else if (is_taken) {
Aart Bik22f05872015-10-27 15:56:28 -0700882 tcKind = kTripCountInLoopUnsafe; // needs finite-test
Aart Bik9401f532015-09-28 16:25:56 -0700883 }
Aart Bik22f05872015-10-27 15:56:28 -0700884 InductionOp op = kNop;
885 switch (cmp) {
886 case kCondLT: op = kLT; break;
887 case kCondLE: op = kLE; break;
888 case kCondGT: op = kGT; break;
889 case kCondGE: op = kGE; break;
890 default: LOG(FATAL) << "CONDITION UNREACHABLE";
891 }
Aart Bik009cace2016-09-16 10:15:19 -0700892 // Associate trip count with control instruction, rather than the condition (even
893 // though it's its use) since former provides a convenient use-free placeholder.
894 HInstruction* control = loop->GetHeader()->GetLastInstruction();
Aart Bik22f05872015-10-27 15:56:28 -0700895 InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr);
Aart Bik009cace2016-09-16 10:15:19 -0700896 DCHECK(control->IsIf());
897 AssignInfo(loop, control, CreateTripCount(tcKind, trip_count, taken_test, type));
Aart Bik9401f532015-09-28 16:25:56 -0700898}
899
900bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
901 InductionInfo* upper_expr,
902 IfCondition cmp) {
903 int64_t lower_value;
904 int64_t upper_value;
Aart Bik97412c922016-02-19 20:14:38 -0800905 switch (cmp) {
906 case kCondLT:
907 return IsAtMost(lower_expr, &lower_value)
908 && IsAtLeast(upper_expr, &upper_value)
909 && lower_value < upper_value;
910 case kCondLE:
911 return IsAtMost(lower_expr, &lower_value)
912 && IsAtLeast(upper_expr, &upper_value)
913 && lower_value <= upper_value;
914 case kCondGT:
915 return IsAtLeast(lower_expr, &lower_value)
916 && IsAtMost(upper_expr, &upper_value)
917 && lower_value > upper_value;
918 case kCondGE:
919 return IsAtLeast(lower_expr, &lower_value)
920 && IsAtMost(upper_expr, &upper_value)
921 && lower_value >= upper_value;
922 default:
923 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700924 }
925 return false; // not certain, may be untaken
926}
927
928bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
929 int64_t stride_value,
930 Primitive::Type type,
931 IfCondition cmp) {
Aart Bike6bd0272016-12-16 13:57:52 -0800932 int64_t min = Primitive::MinValueOfIntegralType(type);
933 int64_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik9401f532015-09-28 16:25:56 -0700934 // Some rules under which it is certain at compile-time that the loop is finite.
935 int64_t value;
936 switch (cmp) {
937 case kCondLT:
938 return stride_value == 1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800939 (IsAtMost(upper_expr, &value) && value <= (max - stride_value + 1));
Aart Bik9401f532015-09-28 16:25:56 -0700940 case kCondLE:
Aart Bik97412c922016-02-19 20:14:38 -0800941 return (IsAtMost(upper_expr, &value) && value <= (max - stride_value));
Aart Bik9401f532015-09-28 16:25:56 -0700942 case kCondGT:
943 return stride_value == -1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800944 (IsAtLeast(upper_expr, &value) && value >= (min - stride_value - 1));
Aart Bik9401f532015-09-28 16:25:56 -0700945 case kCondGE:
Aart Bik97412c922016-02-19 20:14:38 -0800946 return (IsAtLeast(upper_expr, &value) && value >= (min - stride_value));
Aart Bike9f37602015-10-09 11:15:55 -0700947 default:
948 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700949 }
950 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700951}
952
Aart Bik0d345cf2016-03-16 10:49:38 -0700953bool HInductionVarAnalysis::FitsNarrowerControl(InductionInfo* lower_expr,
954 InductionInfo* upper_expr,
955 int64_t stride_value,
956 Primitive::Type type,
957 IfCondition cmp) {
958 int64_t min = Primitive::MinValueOfIntegralType(type);
959 int64_t max = Primitive::MaxValueOfIntegralType(type);
960 // Inclusive test need one extra.
961 if (stride_value != 1 && stride_value != -1) {
962 return false; // non-unit stride
963 } else if (cmp == kCondLE) {
964 max--;
965 } else if (cmp == kCondGE) {
966 min++;
967 }
968 // Do both bounds fit the range?
Vladimir Marko0e2f2ff2016-03-22 12:31:54 +0000969 int64_t value = 0;
Aart Bik0d345cf2016-03-16 10:49:38 -0700970 return IsAtLeast(lower_expr, &value) && value >= min &&
971 IsAtMost(lower_expr, &value) && value <= max &&
972 IsAtLeast(upper_expr, &value) && value >= min &&
973 IsAtMost(upper_expr, &value) && value <= max;
974}
975
Aart Bik30efb4e2015-07-30 12:14:31 -0700976void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
977 HInstruction* instruction,
978 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700979 auto it = induction_.find(loop);
980 if (it == induction_.end()) {
981 it = induction_.Put(loop,
982 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100983 std::less<HInstruction*>(),
984 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700985 }
986 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700987}
988
Aart Bike609b7c2015-08-27 13:46:58 -0700989HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
990 HInstruction* instruction) {
991 auto it = induction_.find(loop);
992 if (it != induction_.end()) {
993 auto loop_it = it->second.find(instruction);
994 if (loop_it != it->second.end()) {
995 return loop_it->second;
996 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700997 }
Mingyao Yang4b467ed2015-11-19 17:04:22 -0800998 if (loop->IsDefinedOutOfTheLoop(instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -0700999 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -07001000 AssignInfo(loop, instruction, info);
1001 return info;
1002 }
1003 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -07001004}
1005
Aart Bikd14c5952015-09-08 15:25:15 -07001006HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
1007 Primitive::Type type) {
Aart Bikc071a012016-12-01 10:22:31 -08001008 HInstruction* constant;
1009 switch (type) {
1010 case Primitive::kPrimDouble: constant = graph_->GetDoubleConstant(value); break;
1011 case Primitive::kPrimFloat: constant = graph_->GetFloatConstant(value); break;
1012 case Primitive::kPrimLong: constant = graph_->GetLongConstant(value); break;
1013 default: constant = graph_->GetIntConstant(value); break;
Aart Bikd14c5952015-09-08 15:25:15 -07001014 }
Aart Bikc071a012016-12-01 10:22:31 -08001015 return CreateInvariantFetch(constant);
Aart Bikd14c5952015-09-08 15:25:15 -07001016}
1017
Aart Bik471a2032015-09-04 18:22:11 -07001018HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
1019 InductionOp op,
1020 InductionInfo* a,
1021 InductionInfo* b) {
1022 // Perform some light-weight simplifications during construction of a new invariant.
1023 // This often safes memory and yields a more concise representation of the induction.
1024 // More exhaustive simplifications are done by later phases once induction nodes are
1025 // translated back into HIR code (e.g. by loop optimizations or BCE).
1026 int64_t value = -1;
Aart Bik97412c922016-02-19 20:14:38 -08001027 if (IsExact(a, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001028 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001029 // Simplify 0 + b = b, 0 ^ b = b, 0 * b = 0.
1030 if (op == kAdd || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001031 return b;
1032 } else if (op == kMul) {
1033 return a;
1034 }
Aart Bikd14c5952015-09-08 15:25:15 -07001035 } else if (op == kMul) {
1036 // Simplify 1 * b = b, -1 * b = -b
1037 if (value == 1) {
1038 return b;
1039 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001040 return CreateSimplifiedInvariant(kNeg, nullptr, b);
Aart Bikd14c5952015-09-08 15:25:15 -07001041 }
Aart Bik471a2032015-09-04 18:22:11 -07001042 }
1043 }
Aart Bik97412c922016-02-19 20:14:38 -08001044 if (IsExact(b, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001045 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001046 // Simplify a + 0 = a, a - 0 = a, a ^ 0 = a, a * 0 = 0, -0 = 0.
1047 if (op == kAdd || op == kSub || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001048 return a;
1049 } else if (op == kMul || op == kNeg) {
1050 return b;
1051 }
Aart Bikd14c5952015-09-08 15:25:15 -07001052 } else if (op == kMul || op == kDiv) {
1053 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
1054 if (value == 1) {
1055 return a;
1056 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001057 return CreateSimplifiedInvariant(kNeg, nullptr, a);
Aart Bikd14c5952015-09-08 15:25:15 -07001058 }
Aart Bik471a2032015-09-04 18:22:11 -07001059 }
1060 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -07001061 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
1062 if (op == kAdd) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001063 return CreateSimplifiedInvariant(kSub, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001064 } else if (op == kSub) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001065 return CreateSimplifiedInvariant(kAdd, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001066 } else if (op == kNeg) {
1067 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -07001068 }
Aart Bik7d57d7f2015-12-09 14:39:48 -08001069 } else if (b->operation == kSub) {
1070 // Simplify - (a - b) = b - a.
1071 if (op == kNeg) {
1072 return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a);
1073 }
Aart Bik471a2032015-09-04 18:22:11 -07001074 }
Aart Bike6bd0272016-12-16 13:57:52 -08001075 return new (graph_->GetArena()) InductionInfo(
1076 kInvariant, op, a, b, nullptr, ImplicitConversion(b->type));
Aart Bik471a2032015-09-04 18:22:11 -07001077}
1078
Aart Bikd0a022d2016-12-13 11:22:31 -08001079HInstruction* HInductionVarAnalysis::GetShiftConstant(HLoopInformation* loop,
1080 HInstruction* instruction,
1081 InductionInfo* initial) {
1082 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
1083 // Shift-rights are only the same as division for non-negative initial inputs.
1084 // Otherwise we would round incorrectly.
1085 if (initial != nullptr) {
1086 int64_t value = -1;
1087 if (!IsAtLeast(initial, &value) || value < 0) {
1088 return nullptr;
1089 }
1090 }
1091 // Obtain the constant needed to treat shift as equivalent multiplication or division.
1092 // This yields an existing instruction if the constant is already there. Otherwise, this
1093 // has a side effect on the HIR. The restriction on the shift factor avoids generating a
1094 // negative constant (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that
1095 // generalization for shift factors outside [0,32) and [0,64) ranges is done earlier.
Aart Bikc071a012016-12-01 10:22:31 -08001096 InductionInfo* b = LookupInfo(loop, instruction->InputAt(1));
1097 int64_t value = -1;
1098 if (IsExact(b, &value)) {
1099 Primitive::Type type = instruction->InputAt(0)->GetType();
1100 if (type == Primitive::kPrimInt && 0 <= value && value < 31) {
1101 return graph_->GetIntConstant(1 << value);
1102 }
1103 if (type == Primitive::kPrimLong && 0 <= value && value < 63) {
1104 return graph_->GetLongConstant(1L << value);
1105 }
1106 }
1107 return nullptr;
1108}
Aart Bikcc42be02016-10-20 16:14:16 -07001109
1110void HInductionVarAnalysis::AssignCycle(HPhi* phi) {
1111 ArenaSet<HInstruction*>* set = &cycles_.Put(phi, ArenaSet<HInstruction*>(
1112 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)))->second;
1113 for (HInstruction* i : scc_) {
1114 set->insert(i);
1115 }
1116}
1117
1118ArenaSet<HInstruction*>* HInductionVarAnalysis::LookupCycle(HPhi* phi) {
1119 auto it = cycles_.find(phi);
1120 if (it != cycles_.end()) {
1121 return &it->second;
1122 }
1123 return nullptr;
1124}
1125
Aart Bik97412c922016-02-19 20:14:38 -08001126bool HInductionVarAnalysis::IsExact(InductionInfo* info, int64_t* value) {
1127 return InductionVarRange(this).IsConstant(info, InductionVarRange::kExact, value);
1128}
1129
1130bool HInductionVarAnalysis::IsAtMost(InductionInfo* info, int64_t* value) {
1131 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtMost, value);
1132}
1133
1134bool HInductionVarAnalysis::IsAtLeast(InductionInfo* info, int64_t* value) {
1135 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtLeast, value);
Aart Bik7d57d7f2015-12-09 14:39:48 -08001136}
1137
Aart Bike6bd0272016-12-16 13:57:52 -08001138bool HInductionVarAnalysis::IsNarrowingLinear(InductionInfo* info) {
1139 return info != nullptr &&
1140 info->induction_class == kLinear &&
1141 (info->type == Primitive::kPrimByte ||
1142 info->type == Primitive::kPrimShort ||
1143 info->type == Primitive::kPrimChar ||
1144 (info->type == Primitive::kPrimInt && (info->op_a->type == Primitive::kPrimLong ||
1145 info->op_b->type == Primitive::kPrimLong)));
1146}
1147
Aart Bik30efb4e2015-07-30 12:14:31 -07001148bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
1149 InductionInfo* info2) {
1150 // Test structural equality only, without accounting for simplifications.
1151 if (info1 != nullptr && info2 != nullptr) {
1152 return
1153 info1->induction_class == info2->induction_class &&
1154 info1->operation == info2->operation &&
1155 info1->fetch == info2->fetch &&
Aart Bik78296912016-03-25 13:14:53 -07001156 info1->type == info2->type &&
Aart Bik30efb4e2015-07-30 12:14:31 -07001157 InductionEqual(info1->op_a, info2->op_a) &&
1158 InductionEqual(info1->op_b, info2->op_b);
1159 }
1160 // Otherwise only two nullptrs are considered equal.
1161 return info1 == info2;
1162}
1163
Aart Bikc071a012016-12-01 10:22:31 -08001164std::string HInductionVarAnalysis::FetchToString(HInstruction* fetch) {
1165 DCHECK(fetch != nullptr);
1166 if (fetch->IsIntConstant()) {
1167 return std::to_string(fetch->AsIntConstant()->GetValue());
1168 } else if (fetch->IsLongConstant()) {
1169 return std::to_string(fetch->AsLongConstant()->GetValue());
1170 }
1171 return std::to_string(fetch->GetId()) + ":" + fetch->DebugName();
1172}
1173
Aart Bik30efb4e2015-07-30 12:14:31 -07001174std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
1175 if (info != nullptr) {
1176 if (info->induction_class == kInvariant) {
1177 std::string inv = "(";
1178 inv += InductionToString(info->op_a);
1179 switch (info->operation) {
Aart Bik22f05872015-10-27 15:56:28 -07001180 case kNop: inv += " @ "; break;
1181 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001182 case kSub:
Aart Bik22f05872015-10-27 15:56:28 -07001183 case kNeg: inv += " - "; break;
1184 case kMul: inv += " * "; break;
1185 case kDiv: inv += " / "; break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001186 case kRem: inv += " % "; break;
Aart Bik7dc96932016-10-12 10:01:05 -07001187 case kXor: inv += " ^ "; break;
Aart Bik22f05872015-10-27 15:56:28 -07001188 case kLT: inv += " < "; break;
1189 case kLE: inv += " <= "; break;
1190 case kGT: inv += " > "; break;
1191 case kGE: inv += " >= "; break;
Aart Bikc071a012016-12-01 10:22:31 -08001192 case kFetch: inv += FetchToString(info->fetch); break;
Aart Bik22f05872015-10-27 15:56:28 -07001193 case kTripCountInLoop: inv += " (TC-loop) "; break;
1194 case kTripCountInBody: inv += " (TC-body) "; break;
1195 case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break;
1196 case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001197 }
1198 inv += InductionToString(info->op_b);
Aart Bik0d345cf2016-03-16 10:49:38 -07001199 inv += ")";
1200 return inv;
Aart Bik30efb4e2015-07-30 12:14:31 -07001201 } else {
Aart Bik30efb4e2015-07-30 12:14:31 -07001202 if (info->induction_class == kLinear) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001203 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001204 return "(" + InductionToString(info->op_a) + " * i + " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001205 InductionToString(info->op_b) + "):" +
1206 Primitive::PrettyDescriptor(info->type);
Aart Bikc071a012016-12-01 10:22:31 -08001207 } else if (info->induction_class == kPolynomial) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001208 DCHECK(info->operation == kNop);
1209 return "poly(sum_lt(" + InductionToString(info->op_a) + ") + " +
Aart Bikc071a012016-12-01 10:22:31 -08001210 InductionToString(info->op_b) + "):" +
1211 Primitive::PrettyDescriptor(info->type);
1212 } else if (info->induction_class == kGeometric) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001213 DCHECK(info->operation == kMul || info->operation == kDiv);
Aart Bikc071a012016-12-01 10:22:31 -08001214 DCHECK(info->fetch != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001215 return "geo(" + InductionToString(info->op_a) + " * " +
1216 FetchToString(info->fetch) +
1217 (info->operation == kMul ? " ^ i + " : " ^ -i + ") +
1218 InductionToString(info->op_b) + "):" +
1219 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001220 } else if (info->induction_class == kWrapAround) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001221 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001222 return "wrap(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001223 InductionToString(info->op_b) + "):" +
1224 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001225 } else if (info->induction_class == kPeriodic) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001226 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001227 return "periodic(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001228 InductionToString(info->op_b) + "):" +
1229 Primitive::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001230 }
1231 }
1232 }
1233 return "";
1234}
1235
1236} // namespace art