blob: eab17aad314a906aa6a209063fa27fe0610b6a62 [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 */
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010059static bool IsNarrowingIntegralConversion(DataType::Type from, DataType::Type to) {
Aart Bik0d345cf2016-03-16 10:49:38 -070060 switch (from) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010061 case DataType::Type::kInt64:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010062 return to == DataType::Type::kUint8 ||
63 to == DataType::Type::kInt8 ||
64 to == DataType::Type::kUint16 ||
65 to == DataType::Type::kInt16 ||
66 to == DataType::Type::kInt32;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kInt32:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010068 return to == DataType::Type::kUint8 ||
69 to == DataType::Type::kInt8 ||
70 to == DataType::Type::kUint16 ||
71 to == DataType::Type::kInt16;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010072 case DataType::Type::kUint16:
73 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010074 return to == DataType::Type::kUint8 || to == DataType::Type::kInt8;
Aart Bik0d345cf2016-03-16 10:49:38 -070075 default:
76 return false;
77 }
78}
79
80/**
Aart Bike6bd0272016-12-16 13:57:52 -080081 * Returns result of implicit widening type conversion done in HIR.
Aart Bik0d345cf2016-03-16 10:49:38 -070082 */
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083static DataType::Type ImplicitConversion(DataType::Type type) {
Aart Bike6bd0272016-12-16 13:57:52 -080084 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010086 case DataType::Type::kUint8:
87 case DataType::Type::kInt8:
88 case DataType::Type::kUint16:
89 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 return DataType::Type::kInt32;
Aart Bike6bd0272016-12-16 13:57:52 -080091 default:
92 return type;
93 }
Aart Bik0d345cf2016-03-16 10:49:38 -070094}
95
Aart Bik30efb4e2015-07-30 12:14:31 -070096//
97// Class methods.
98//
99
100HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph)
101 : HOptimization(graph, kInductionPassName),
102 global_depth_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +0100103 stack_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100104 map_(std::less<HInstruction*>(),
105 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Aart Bik7dc96932016-10-12 10:01:05 -0700106 scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100107 cycle_(std::less<HInstruction*>(),
108 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100109 type_(DataType::Type::kVoid),
Vladimir Marko5233f932015-09-29 19:01:15 +0100110 induction_(std::less<HLoopInformation*>(),
Aart Bikcc42be02016-10-20 16:14:16 -0700111 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
112 cycles_(std::less<HPhi*>(),
113 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700114}
115
116void HInductionVarAnalysis::Run() {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800117 // Detects sequence variables (generalized induction variables) during an outer to inner
118 // traversal of all loops using Gerlek's algorithm. The order is important to enable
119 // range analysis on outer loop while visiting inner loops.
Vladimir Marko2c45bc92016-10-25 16:54:12 +0100120 for (HBasicBlock* graph_block : graph_->GetReversePostOrder()) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000121 // Don't analyze irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000122 if (graph_block->IsLoopHeader() && !graph_block->GetLoopInformation()->IsIrreducible()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700123 VisitLoop(graph_block->GetLoopInformation());
124 }
125 }
126}
127
128void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) {
129 // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's
130 // algorithm. Due to the descendant-first nature, classification happens "on-demand".
131 global_depth_ = 0;
Aart Bike609b7c2015-08-27 13:46:58 -0700132 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700133 map_.clear();
134
135 for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) {
136 HBasicBlock* loop_block = it_loop.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700137 DCHECK(loop_block->IsInLoop());
Aart Bik30efb4e2015-07-30 12:14:31 -0700138 if (loop_block->GetLoopInformation() != loop) {
Aart Bik7dc96932016-10-12 10:01:05 -0700139 continue; // Inner loops visited later.
Aart Bik30efb4e2015-07-30 12:14:31 -0700140 }
141 // Visit phi-operations and instructions.
142 for (HInstructionIterator it(loop_block->GetPhis()); !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 for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) {
149 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700150 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700151 VisitNode(loop, instruction);
152 }
153 }
154 }
155
Aart Bike609b7c2015-08-27 13:46:58 -0700156 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700157 map_.clear();
Aart Bikd14c5952015-09-08 15:25:15 -0700158
Aart Bik78296912016-03-25 13:14:53 -0700159 // Determine the loop's trip-count.
Aart Bikd14c5952015-09-08 15:25:15 -0700160 VisitControl(loop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700161}
162
163void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700164 const uint32_t d1 = ++global_depth_;
Aart Bike609b7c2015-08-27 13:46:58 -0700165 map_.Put(instruction, NodeInfo(d1));
Aart Bik30efb4e2015-07-30 12:14:31 -0700166 stack_.push_back(instruction);
167
168 // Visit all descendants.
169 uint32_t low = d1;
Vladimir Marko372f10e2016-05-17 16:30:10 +0100170 for (HInstruction* input : instruction->GetInputs()) {
171 low = std::min(low, VisitDescendant(loop, input));
Aart Bik30efb4e2015-07-30 12:14:31 -0700172 }
173
174 // Lower or found SCC?
175 if (low < d1) {
Aart Bike609b7c2015-08-27 13:46:58 -0700176 map_.find(instruction)->second.depth = low;
Aart Bik30efb4e2015-07-30 12:14:31 -0700177 } else {
178 scc_.clear();
179 cycle_.clear();
180
181 // Pop the stack to build the SCC for classification.
182 while (!stack_.empty()) {
183 HInstruction* x = stack_.back();
184 scc_.push_back(x);
185 stack_.pop_back();
Aart Bike609b7c2015-08-27 13:46:58 -0700186 map_.find(x)->second.done = true;
Aart Bik30efb4e2015-07-30 12:14:31 -0700187 if (x == instruction) {
188 break;
189 }
190 }
191
Aart Bik0d345cf2016-03-16 10:49:38 -0700192 // Type of induction.
193 type_ = scc_[0]->GetType();
194
Aart Bik30efb4e2015-07-30 12:14:31 -0700195 // Classify the SCC.
Aart Bikf475bee2015-09-16 12:50:25 -0700196 if (scc_.size() == 1 && !scc_[0]->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700197 ClassifyTrivial(loop, scc_[0]);
198 } else {
199 ClassifyNonTrivial(loop);
200 }
201
202 scc_.clear();
203 cycle_.clear();
204 }
205}
206
207uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) {
208 // If the definition is either outside the loop (loop invariant entry value)
209 // or assigned in inner loop (inner exit value), the traversal stops.
210 HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation();
211 if (otherLoop != loop) {
212 return global_depth_;
213 }
214
215 // Inspect descendant node.
Aart Bike609b7c2015-08-27 13:46:58 -0700216 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700217 VisitNode(loop, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700218 return map_.find(instruction)->second.depth;
Aart Bik30efb4e2015-07-30 12:14:31 -0700219 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700220 auto it = map_.find(instruction);
Aart Bik30efb4e2015-07-30 12:14:31 -0700221 return it->second.done ? global_depth_ : it->second.depth;
222 }
223}
224
225void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) {
226 InductionInfo* info = nullptr;
227 if (instruction->IsPhi()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800228 info = TransferPhi(loop, instruction, /*input_index*/ 0, /*adjust_input_size*/ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700229 } else if (instruction->IsAdd()) {
230 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
231 LookupInfo(loop, instruction->InputAt(1)), kAdd);
232 } else if (instruction->IsSub()) {
233 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
234 LookupInfo(loop, instruction->InputAt(1)), kSub);
Aart Bikc071a012016-12-01 10:22:31 -0800235 } else if (instruction->IsNeg()) {
236 info = TransferNeg(LookupInfo(loop, instruction->InputAt(0)));
Aart Bik30efb4e2015-07-30 12:14:31 -0700237 } else if (instruction->IsMul()) {
238 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
239 LookupInfo(loop, instruction->InputAt(1)));
Aart Bike609b7c2015-08-27 13:46:58 -0700240 } else if (instruction->IsShl()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800241 HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr);
Aart Bikc071a012016-12-01 10:22:31 -0800242 if (mulc != nullptr) {
243 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
244 LookupInfo(loop, mulc));
245 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800246 } else if (instruction->IsSelect()) {
247 info = TransferPhi(loop, instruction, /*input_index*/ 0, /*adjust_input_size*/ 1);
Aart Bik0d345cf2016-03-16 10:49:38 -0700248 } else if (instruction->IsTypeConversion()) {
Aart Bike6bd0272016-12-16 13:57:52 -0800249 info = TransferConversion(LookupInfo(loop, instruction->InputAt(0)),
250 instruction->AsTypeConversion()->GetInputType(),
251 instruction->AsTypeConversion()->GetResultType());
Aart Bike609b7c2015-08-27 13:46:58 -0700252 } else if (instruction->IsBoundsCheck()) {
253 info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through.
Aart Bik30efb4e2015-07-30 12:14:31 -0700254 }
255
256 // Successfully classified?
257 if (info != nullptr) {
258 AssignInfo(loop, instruction, info);
259 }
260}
261
262void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) {
263 const size_t size = scc_.size();
Aart Bike609b7c2015-08-27 13:46:58 -0700264 DCHECK_GE(size, 1u);
Aart Bik22af3be2015-09-10 12:50:58 -0700265
Aart Bikcc42be02016-10-20 16:14:16 -0700266 // Rotate proper loop-phi to front.
Aart Bik22af3be2015-09-10 12:50:58 -0700267 if (size > 1) {
Vladimir Marko5233f932015-09-29 19:01:15 +0100268 ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700269 RotateEntryPhiFirst(loop, &scc_, &other);
270 }
271
Aart Bikcc42be02016-10-20 16:14:16 -0700272 // Analyze from loop-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700273 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700274 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700275 return;
276 }
Aart Bikf475bee2015-09-16 12:50:25 -0700277
278 // External link should be loop invariant.
279 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700280 if (initial == nullptr || initial->induction_class != kInvariant) {
281 return;
282 }
283
Aart Bike6bd0272016-12-16 13:57:52 -0800284 // Store interesting cycle in each loop phi.
285 for (size_t i = 0; i < size; i++) {
286 if (scc_[i]->IsLoopHeaderPhi()) {
287 AssignCycle(scc_[i]->AsPhi());
288 }
289 }
Aart Bikcc42be02016-10-20 16:14:16 -0700290
Aart Bikf475bee2015-09-16 12:50:25 -0700291 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700292 if (size == 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800293 InductionInfo* update = TransferPhi(loop, phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700294 if (update != nullptr) {
Aart Bikc071a012016-12-01 10:22:31 -0800295 AssignInfo(loop, phi, CreateInduction(kWrapAround,
296 kNop,
297 initial,
298 update,
299 /*fetch*/ nullptr,
300 type_));
Aart Bik30efb4e2015-07-30 12:14:31 -0700301 }
302 return;
303 }
304
305 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700306 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700307 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700308 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700309 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700310 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700311 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700312 } else if (instruction->IsAdd()) {
313 update = SolveAddSub(
314 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
315 } else if (instruction->IsSub()) {
316 update = SolveAddSub(
317 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bikc071a012016-12-01 10:22:31 -0800318 } else if (instruction->IsMul()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800319 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800320 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kMul);
321 } else if (instruction->IsDiv()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800322 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800323 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kDiv);
324 } else if (instruction->IsRem()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800325 update = SolveOp(
326 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kRem);
Aart Bikc071a012016-12-01 10:22:31 -0800327 } else if (instruction->IsShl()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800328 HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr);
Aart Bikc071a012016-12-01 10:22:31 -0800329 if (mulc != nullptr) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800330 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), mulc, kMul);
Aart Bikc071a012016-12-01 10:22:31 -0800331 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800332 } else if (instruction->IsShr() || instruction->IsUShr()) {
333 HInstruction* divc = GetShiftConstant(loop, instruction, initial);
334 if (divc != nullptr) {
335 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), divc, kDiv);
336 }
Aart Bik7dc96932016-10-12 10:01:05 -0700337 } else if (instruction->IsXor()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800338 update = SolveOp(
339 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700340 } else if (instruction->IsEqual()) {
341 update = SolveTest(loop, phi, instruction, 0);
342 } else if (instruction->IsNotEqual()) {
343 update = SolveTest(loop, phi, instruction, 1);
Aart Bikd0a022d2016-12-13 11:22:31 -0800344 } else if (instruction->IsSelect()) {
345 update = SolvePhi(instruction, /*input_index*/ 0, /*adjust_input_size*/ 1); // acts like Phi
Aart Bik0d345cf2016-03-16 10:49:38 -0700346 } else if (instruction->IsTypeConversion()) {
Aart Bike6bd0272016-12-16 13:57:52 -0800347 update = SolveConversion(loop, phi, instruction->AsTypeConversion());
Aart Bik30efb4e2015-07-30 12:14:31 -0700348 }
349 if (update == nullptr) {
350 return;
351 }
Aart Bike609b7c2015-08-27 13:46:58 -0700352 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700353 }
354
Aart Bikf475bee2015-09-16 12:50:25 -0700355 // Success if all internal links received the same temporary meaning.
Aart Bikd0a022d2016-12-13 11:22:31 -0800356 InductionInfo* induction = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700357 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700358 switch (induction->induction_class) {
359 case kInvariant:
Aart Bikc071a012016-12-01 10:22:31 -0800360 // Construct combined stride of the linear induction.
361 induction = CreateInduction(kLinear, kNop, induction, initial, /*fetch*/ nullptr, type_);
362 FALLTHROUGH_INTENDED;
363 case kPolynomial:
364 case kGeometric:
Aart Bikdf7822e2016-12-06 10:05:30 -0800365 case kWrapAround:
Aart Bik22af3be2015-09-10 12:50:58 -0700366 // Classify first phi and then the rest of the cycle "on-demand".
367 // Statements are scanned in order.
Aart Bikc071a012016-12-01 10:22:31 -0800368 AssignInfo(loop, phi, induction);
Aart Bik22af3be2015-09-10 12:50:58 -0700369 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700370 ClassifyTrivial(loop, scc_[i]);
371 }
372 break;
373 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700374 // Classify all elements in the cycle with the found periodic induction while
375 // rotating each first element to the end. Lastly, phi is classified.
376 // Statements are scanned in reverse order.
377 for (size_t i = size - 1; i >= 1; i--) {
378 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700379 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
380 }
381 AssignInfo(loop, phi, induction);
382 break;
383 default:
384 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700385 }
386 }
387}
388
Aart Bike609b7c2015-08-27 13:46:58 -0700389HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
390 InductionInfo* induction,
391 InductionInfo* last) {
392 // Rotates a periodic induction of the form
393 // (a, b, c, d, e)
394 // into
395 // (b, c, d, e, a)
396 // in preparation of assigning this to the previous variable in the sequence.
397 if (induction->induction_class == kInvariant) {
Aart Bikc071a012016-12-01 10:22:31 -0800398 return CreateInduction(kPeriodic,
399 kNop,
400 induction,
401 last,
402 /*fetch*/ nullptr,
403 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700404 }
Aart Bikc071a012016-12-01 10:22:31 -0800405 return CreateInduction(kPeriodic,
406 kNop,
407 induction->op_a,
408 RotatePeriodicInduction(induction->op_b, last),
409 /*fetch*/ nullptr,
410 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700411}
412
Aart Bikf475bee2015-09-16 12:50:25 -0700413HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
414 HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800415 size_t input_index,
416 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700417 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100418 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100419 DCHECK_LT(input_index, inputs.size());
420 InductionInfo* a = LookupInfo(loop, inputs[input_index]);
Aart Bikd0a022d2016-12-13 11:22:31 -0800421 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100422 InductionInfo* b = LookupInfo(loop, inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700423 if (!InductionEqual(a, b)) {
424 return nullptr;
425 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700426 }
Aart Bikf475bee2015-09-16 12:50:25 -0700427 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700428}
429
430HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
431 InductionInfo* b,
432 InductionOp op) {
Aart Bikc071a012016-12-01 10:22:31 -0800433 // Transfer over an addition or subtraction: any invariant, linear, polynomial, geometric,
434 // wrap-around, or periodic can be combined with an invariant to yield a similar result.
Aart Bikdf7822e2016-12-06 10:05:30 -0800435 // Two linear or two polynomial inputs can be combined too. Other combinations fail.
Aart Bik30efb4e2015-07-30 12:14:31 -0700436 if (a != nullptr && b != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800437 if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) {
438 return nullptr; // no transfer
439 } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800440 return CreateInvariantOp(op, a, b); // direct invariant
Aart Bikdf7822e2016-12-06 10:05:30 -0800441 } else if ((a->induction_class == kLinear && b->induction_class == kLinear) ||
442 (a->induction_class == kPolynomial && b->induction_class == kPolynomial)) {
Aart Bik74da5292016-12-20 11:13:03 -0800443 // Rule induc(a, b) + induc(a', b') -> induc(a + a', b + b').
444 InductionInfo* new_a = TransferAddSub(a->op_a, b->op_a, op);
445 InductionInfo* new_b = TransferAddSub(a->op_b, b->op_b, op);
446 if (new_a != nullptr && new_b != nullptr) {
447 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
448 }
Aart Bike609b7c2015-08-27 13:46:58 -0700449 } else if (a->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800450 // Rule a + induc(a', b') -> induc(a', a + b') or induc(a + a', a + b').
Aart Bike609b7c2015-08-27 13:46:58 -0700451 InductionInfo* new_a = b->op_a;
452 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800453 if (b->induction_class == kWrapAround || b->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700454 new_a = TransferAddSub(a, new_a, op);
455 } else if (op == kSub) { // Negation required.
456 new_a = TransferNeg(new_a);
457 }
Aart Bik74da5292016-12-20 11:13:03 -0800458 if (new_a != nullptr && new_b != nullptr) {
459 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
460 }
Aart Bike609b7c2015-08-27 13:46:58 -0700461 } else if (b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800462 // Rule induc(a, b) + b' -> induc(a, b + b') or induc(a + b', b + b').
Aart Bike609b7c2015-08-27 13:46:58 -0700463 InductionInfo* new_a = a->op_a;
464 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800465 if (a->induction_class == kWrapAround || a->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700466 new_a = TransferAddSub(new_a, b, op);
467 }
Aart Bik74da5292016-12-20 11:13:03 -0800468 if (new_a != nullptr && new_b != nullptr) {
469 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
470 }
Aart Bikc071a012016-12-01 10:22:31 -0800471 }
472 }
473 return nullptr;
474}
475
476HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
477 // Transfer over a unary negation: an invariant, linear, polynomial, geometric (mul),
478 // wrap-around, or periodic input yields a similar but negated induction as result.
479 if (a != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800480 if (IsNarrowingLinear(a)) {
481 return nullptr; // no transfer
482 } else if (a->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800483 return CreateInvariantOp(kNeg, nullptr, a); // direct invariant
Aart Bikc071a012016-12-01 10:22:31 -0800484 } else if (a->induction_class != kGeometric || a->operation == kMul) {
Aart Bik74da5292016-12-20 11:13:03 -0800485 // Rule - induc(a, b) -> induc(-a, -b).
486 InductionInfo* new_a = TransferNeg(a->op_a);
487 InductionInfo* new_b = TransferNeg(a->op_b);
488 if (new_a != nullptr && new_b != nullptr) {
489 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
490 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700491 }
492 }
493 return nullptr;
494}
495
496HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
497 InductionInfo* b) {
Aart Bikc071a012016-12-01 10:22:31 -0800498 // Transfer over a multiplication: any invariant, linear, polynomial, geometric (mul),
499 // wrap-around, or periodic can be multiplied with an invariant to yield a similar
500 // but multiplied result. Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700501 if (a != nullptr && b != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800502 if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) {
503 return nullptr; // no transfer
504 } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800505 return CreateInvariantOp(kMul, a, b); // direct invariant
Aart Bikc071a012016-12-01 10:22:31 -0800506 } else if (a->induction_class == kInvariant && (b->induction_class != kGeometric ||
507 b->operation == kMul)) {
Aart Bik74da5292016-12-20 11:13:03 -0800508 // Rule a * induc(a', b') -> induc(a * a', b * b').
509 InductionInfo* new_a = TransferMul(a, b->op_a);
510 InductionInfo* new_b = TransferMul(a, b->op_b);
511 if (new_a != nullptr && new_b != nullptr) {
512 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
513 }
Aart Bikc071a012016-12-01 10:22:31 -0800514 } else if (b->induction_class == kInvariant && (a->induction_class != kGeometric ||
515 a->operation == kMul)) {
Aart Bik74da5292016-12-20 11:13:03 -0800516 // Rule induc(a, b) * b' -> induc(a * b', b * b').
517 InductionInfo* new_a = TransferMul(a->op_a, b);
518 InductionInfo* new_b = TransferMul(a->op_b, b);
519 if (new_a != nullptr && new_b != nullptr) {
520 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
521 }
Aart Bike609b7c2015-08-27 13:46:58 -0700522 }
523 }
524 return nullptr;
525}
526
Aart Bike6bd0272016-12-16 13:57:52 -0800527HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferConversion(
528 InductionInfo* a,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100529 DataType::Type from,
530 DataType::Type to) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700531 if (a != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800532 // Allow narrowing conversion on linear induction in certain cases:
533 // induction is already at narrow type, or can be made narrower.
534 if (IsNarrowingIntegralConversion(from, to) &&
535 a->induction_class == kLinear &&
536 (a->type == to || IsNarrowingIntegralConversion(a->type, to))) {
Aart Bik74da5292016-12-20 11:13:03 -0800537 return CreateInduction(kLinear, kNop, a->op_a, a->op_b, a->fetch, to);
Aart Bik0d345cf2016-03-16 10:49:38 -0700538 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700539 }
540 return nullptr;
541}
542
Aart Bikf475bee2015-09-16 12:50:25 -0700543HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800544 size_t input_index,
545 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700546 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100547 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100548 DCHECK_LT(input_index, inputs.size());
549 auto ita = cycle_.find(inputs[input_index]);
Aart Bik30efb4e2015-07-30 12:14:31 -0700550 if (ita != cycle_.end()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800551 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100552 auto itb = cycle_.find(inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700553 if (itb == cycle_.end() ||
554 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700555 return nullptr;
556 }
557 }
Aart Bikf475bee2015-09-16 12:50:25 -0700558 return ita->second;
559 }
560 return nullptr;
561}
562
563HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
564 HLoopInformation* loop,
565 HInstruction* entry_phi,
566 HInstruction* phi) {
567 // Match all phi inputs.
Aart Bikd0a022d2016-12-13 11:22:31 -0800568 InductionInfo* match = SolvePhi(phi, /*input_index*/ 0, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700569 if (match != nullptr) {
570 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700571 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700572
Aart Bikf475bee2015-09-16 12:50:25 -0700573 // Otherwise, try to solve for a periodic seeded from phi onward.
574 // Only tight multi-statement cycles are considered in order to
575 // simplify rotating the periodic during the final classification.
576 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
577 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700578 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700579 if (phi->InputAt(1) == entry_phi) {
580 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800581 return CreateInduction(kPeriodic, kNop, a, initial, /*fetch*/ nullptr, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700582 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800583 InductionInfo* b = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700584 if (b != nullptr && b->induction_class == kPeriodic) {
Aart Bikc071a012016-12-01 10:22:31 -0800585 return CreateInduction(kPeriodic, kNop, a, b, /*fetch*/ nullptr, type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700586 }
587 }
588 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700589 return nullptr;
590}
591
Aart Bike609b7c2015-08-27 13:46:58 -0700592HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700593 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700594 HInstruction* instruction,
595 HInstruction* x,
596 HInstruction* y,
597 InductionOp op,
598 bool is_first_call) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800599 // Solve within a cycle over an addition or subtraction.
Aart Bike609b7c2015-08-27 13:46:58 -0700600 InductionInfo* b = LookupInfo(loop, y);
Aart Bikdf7822e2016-12-06 10:05:30 -0800601 if (b != nullptr) {
602 if (b->induction_class == kInvariant) {
603 // Adding or subtracting an invariant value, seeded from phi,
604 // keeps adding to the stride of the linear induction.
605 if (x == entry_phi) {
606 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
607 }
608 auto it = cycle_.find(x);
609 if (it != cycle_.end()) {
610 InductionInfo* a = it->second;
611 if (a->induction_class == kInvariant) {
612 return CreateInvariantOp(op, a, b);
613 }
614 }
Aart Bik74da5292016-12-20 11:13:03 -0800615 } else if (b->induction_class == kLinear && b->type == type_) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800616 // Solve within a tight cycle that adds a term that is already classified as a linear
617 // induction for a polynomial induction k = k + i (represented as sum over linear terms).
618 if (x == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
619 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik74da5292016-12-20 11:13:03 -0800620 InductionInfo* new_a = op == kAdd ? b : TransferNeg(b);
621 if (new_a != nullptr) {
622 return CreateInduction(kPolynomial, kNop, new_a, initial, /*fetch*/ nullptr, type_);
623 }
Aart Bike609b7c2015-08-27 13:46:58 -0700624 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700625 }
626 }
Aart Bike609b7c2015-08-27 13:46:58 -0700627
628 // Try some alternatives before failing.
629 if (op == kAdd) {
630 // Try the other way around for an addition if considered for first time.
631 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700632 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700633 }
634 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700635 // Solve within a tight cycle that is formed by exactly two instructions,
Aart Bikc071a012016-12-01 10:22:31 -0800636 // one phi and one update, for a periodic idiom of the form k = c - k.
Aart Bikf475bee2015-09-16 12:50:25 -0700637 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700638 InductionInfo* a = LookupInfo(loop, x);
639 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700640 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800641 return CreateInduction(kPeriodic,
642 kNop,
643 CreateInvariantOp(kSub, a, initial),
644 initial,
645 /*fetch*/ nullptr,
646 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700647 }
648 }
649 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700650 return nullptr;
651}
652
Aart Bikdf7822e2016-12-06 10:05:30 -0800653HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveOp(HLoopInformation* loop,
Aart Bikc071a012016-12-01 10:22:31 -0800654 HInstruction* entry_phi,
655 HInstruction* instruction,
656 HInstruction* x,
657 HInstruction* y,
658 InductionOp op) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800659 // 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 -0700660 if (entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800661 InductionInfo* c = nullptr;
Aart Bik639cc8c2016-10-18 13:03:31 -0700662 InductionInfo* b = LookupInfo(loop, y);
663 if (b != nullptr && b->induction_class == kInvariant && entry_phi == x) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800664 c = b;
665 } else if (op != kDiv && op != kRem) {
666 InductionInfo* a = LookupInfo(loop, x);
667 if (a != nullptr && a->induction_class == kInvariant && entry_phi == y) {
668 c = a;
669 }
670 }
671 // Found suitable operand left or right?
672 if (c != nullptr) {
673 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
674 switch (op) {
675 case kMul:
676 case kDiv:
677 // Restrict base of geometric induction to direct fetch.
678 if (c->operation == kFetch) {
679 return CreateInduction(kGeometric,
680 op,
681 initial,
682 CreateConstant(0, type_),
683 c->fetch,
684 type_);
685 };
686 break;
687 case kRem:
688 // Idiomatic MOD wrap-around induction.
689 return CreateInduction(kWrapAround,
690 kNop,
691 initial,
692 CreateInvariantOp(kRem, initial, c),
693 /*fetch*/ nullptr,
694 type_);
695 case kXor:
696 // Idiomatic XOR periodic induction.
697 return CreateInduction(kPeriodic,
698 kNop,
699 CreateInvariantOp(kXor, initial, c),
700 initial,
701 /*fetch*/ nullptr,
702 type_);
703 default:
Andreas Gampef45d61c2017-06-07 10:29:33 -0700704 LOG(FATAL) << op;
705 UNREACHABLE();
Aart Bikdf7822e2016-12-06 10:05:30 -0800706 }
Aart Bik7dc96932016-10-12 10:01:05 -0700707 }
708 }
Aart Bik639cc8c2016-10-18 13:03:31 -0700709 return nullptr;
710}
711
712HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveTest(HLoopInformation* loop,
713 HInstruction* entry_phi,
714 HInstruction* instruction,
715 int64_t opposite_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800716 // Detect hidden XOR construction in x = (x == false) or x = (x != true).
Aart Bik639cc8c2016-10-18 13:03:31 -0700717 int64_t value = -1;
718 HInstruction* x = instruction->InputAt(0);
719 HInstruction* y = instruction->InputAt(1);
720 if (IsExact(LookupInfo(loop, x), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800721 return SolveOp(loop, entry_phi, instruction, graph_->GetIntConstant(1), y, kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700722 } else if (IsExact(LookupInfo(loop, y), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800723 return SolveOp(loop, entry_phi, instruction, x, graph_->GetIntConstant(1), kXor);
Aart Bik7dc96932016-10-12 10:01:05 -0700724 }
725 return nullptr;
726}
727
Aart Bike6bd0272016-12-16 13:57:52 -0800728HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveConversion(
729 HLoopInformation* loop,
730 HInstruction* entry_phi,
731 HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100732 DataType::Type from = conversion->GetInputType();
733 DataType::Type to = conversion->GetResultType();
Aart Bike6bd0272016-12-16 13:57:52 -0800734 // A narrowing conversion is allowed as *last* operation of the cycle of a linear induction
735 // with an initial value that fits the type, provided that the narrowest encountered type is
736 // recorded with the induction to account for the precision loss. The narrower induction does
737 // *not* transfer to any wider operations, however, since these may yield out-of-type values
738 if (entry_phi->InputCount() == 2 && conversion == entry_phi->InputAt(1)) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100739 int64_t min = DataType::MinValueOfIntegralType(to);
740 int64_t max = DataType::MaxValueOfIntegralType(to);
Aart Bike6bd0272016-12-16 13:57:52 -0800741 int64_t value = 0;
742 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
743 if (IsNarrowingIntegralConversion(from, to) &&
744 IsAtLeast(initial, &value) && value >= min &&
745 IsAtMost(initial, &value) && value <= max) {
746 auto it = cycle_.find(conversion->GetInput());
747 if (it != cycle_.end() && it->second->induction_class == kInvariant) {
748 type_ = to;
749 return it->second;
750 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700751 }
752 }
753 return nullptr;
754}
755
Aart Bikd14c5952015-09-08 15:25:15 -0700756void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
757 HInstruction* control = loop->GetHeader()->GetLastInstruction();
758 if (control->IsIf()) {
759 HIf* ifs = control->AsIf();
760 HBasicBlock* if_true = ifs->IfTrueSuccessor();
761 HBasicBlock* if_false = ifs->IfFalseSuccessor();
762 HInstruction* if_expr = ifs->InputAt(0);
763 // Determine if loop has following structure in header.
764 // loop-header: ....
765 // if (condition) goto X
766 if (if_expr->IsCondition()) {
767 HCondition* condition = if_expr->AsCondition();
768 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
769 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100770 DataType::Type type = ImplicitConversion(condition->InputAt(0)->GetType());
Aart Bik0d345cf2016-03-16 10:49:38 -0700771 // Determine if the loop control uses a known sequence on an if-exit (X outside) or on
772 // an if-iterate (X inside), expressed as if-iterate when passed into VisitCondition().
773 if (a == nullptr || b == nullptr) {
774 return; // Loop control is not a sequence.
Aart Bikd14c5952015-09-08 15:25:15 -0700775 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
776 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
777 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
778 VisitCondition(loop, a, b, type, condition->GetCondition());
779 }
780 }
781 }
782}
783
784void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
785 InductionInfo* a,
786 InductionInfo* b,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100787 DataType::Type type,
Aart Bikd14c5952015-09-08 15:25:15 -0700788 IfCondition cmp) {
789 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700790 // 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 -0700791 switch (cmp) {
792 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
793 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
794 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
795 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700796 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700797 default: break;
798 }
799 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700800 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700801 InductionInfo* lower_expr = a->op_b;
802 InductionInfo* upper_expr = b;
Aart Bik97412c92016-02-19 20:14:38 -0800803 InductionInfo* stride_expr = a->op_a;
804 // Constant stride?
Aart Bik9401f532015-09-28 16:25:56 -0700805 int64_t stride_value = 0;
Aart Bik97412c92016-02-19 20:14:38 -0800806 if (!IsExact(stride_expr, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700807 return;
808 }
Aart Bik358af832016-02-24 14:17:53 -0800809 // Rewrite condition i != U into strict end condition i < U or i > U if this end condition
810 // is reached exactly (tested by verifying if the loop has a unit stride and the non-strict
811 // condition would be always taken).
812 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLE)) ||
813 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGE)))) {
Aart Bik9401f532015-09-28 16:25:56 -0700814 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700815 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700816 // Only accept integral condition. A mismatch between the type of condition and the induction
817 // is only allowed if the, necessarily narrower, induction range fits the narrower control.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100818 if (type != DataType::Type::kInt32 && type != DataType::Type::kInt64) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700819 return; // not integral
820 } else if (type != a->type &&
821 !FitsNarrowerControl(lower_expr, upper_expr, stride_value, a->type, cmp)) {
822 return; // mismatched type
823 }
Aart Bikf475bee2015-09-16 12:50:25 -0700824 // Normalize a linear loop control with a nonzero stride:
825 // stride > 0, either i < U or i <= U
826 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700827 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
828 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik97412c92016-02-19 20:14:38 -0800829 VisitTripCount(loop, lower_expr, upper_expr, stride_expr, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700830 }
Aart Bikd14c5952015-09-08 15:25:15 -0700831 }
832}
833
834void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700835 InductionInfo* lower_expr,
836 InductionInfo* upper_expr,
Aart Bik97412c92016-02-19 20:14:38 -0800837 InductionInfo* stride_expr,
Aart Bik9401f532015-09-28 16:25:56 -0700838 int64_t stride_value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100839 DataType::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700840 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700841 // Any loop of the general form:
842 //
843 // for (i = L; i <= U; i += S) // S > 0
844 // or for (i = L; i >= U; i += S) // S < 0
845 // .. i ..
846 //
847 // can be normalized into:
848 //
849 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
850 // .. L + S * n ..
851 //
Aart Bik9401f532015-09-28 16:25:56 -0700852 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700853 //
Aart Bik9401f532015-09-28 16:25:56 -0700854 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
855 // an unsigned entity, for example, as in the following loop that uses the full range:
856 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
857 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
Aart Bikd5cc6832016-06-22 16:34:46 -0700858 // for (int i = 12; i < U; i++) // TC = 0 when U <= 12
Aart Bik9401f532015-09-28 16:25:56 -0700859 // If this cannot be determined at compile-time, the TC is only valid within the
Aart Bik22f05872015-10-27 15:56:28 -0700860 // loop-body proper, not the loop-header unless enforced with an explicit taken-test.
Aart Bik9401f532015-09-28 16:25:56 -0700861 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
862 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
863 // If this cannot be determined at compile-time, the TC is only valid when enforced
Aart Bik22f05872015-10-27 15:56:28 -0700864 // with an explicit finite-test.
Aart Bik9401f532015-09-28 16:25:56 -0700865 // (4) For loops which early-exits, the TC forms an upper bound, as in:
866 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
Aart Bik22f05872015-10-27 15:56:28 -0700867 InductionInfo* trip_count = upper_expr;
Aart Bik9401f532015-09-28 16:25:56 -0700868 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
869 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
870 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700871 if (!cancels) {
872 // Convert exclusive integral inequality into inclusive integral inequality,
873 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700874 if (cmp == kCondLT) {
Aart Bik22f05872015-10-27 15:56:28 -0700875 trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700876 } else if (cmp == kCondGT) {
Aart Bik22f05872015-10-27 15:56:28 -0700877 trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700878 }
879 // Compensate for stride.
Aart Bik97412c92016-02-19 20:14:38 -0800880 trip_count = CreateInvariantOp(kAdd, trip_count, stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700881 }
Aart Bik97412c92016-02-19 20:14:38 -0800882 trip_count = CreateInvariantOp(
883 kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700884 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700885 // should be aware that the expression is only valid under the conditions listed above.
Aart Bik22f05872015-10-27 15:56:28 -0700886 InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests
Aart Bik9401f532015-09-28 16:25:56 -0700887 if (is_taken && is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700888 tcKind = kTripCountInLoop; // needs neither test
Aart Bik9401f532015-09-28 16:25:56 -0700889 } else if (is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700890 tcKind = kTripCountInBody; // needs taken-test
Aart Bik9401f532015-09-28 16:25:56 -0700891 } else if (is_taken) {
Aart Bik22f05872015-10-27 15:56:28 -0700892 tcKind = kTripCountInLoopUnsafe; // needs finite-test
Aart Bik9401f532015-09-28 16:25:56 -0700893 }
Aart Bik22f05872015-10-27 15:56:28 -0700894 InductionOp op = kNop;
895 switch (cmp) {
896 case kCondLT: op = kLT; break;
897 case kCondLE: op = kLE; break;
898 case kCondGT: op = kGT; break;
899 case kCondGE: op = kGE; break;
900 default: LOG(FATAL) << "CONDITION UNREACHABLE";
901 }
Aart Bik009cace2016-09-16 10:15:19 -0700902 // Associate trip count with control instruction, rather than the condition (even
903 // though it's its use) since former provides a convenient use-free placeholder.
904 HInstruction* control = loop->GetHeader()->GetLastInstruction();
Aart Bik22f05872015-10-27 15:56:28 -0700905 InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr);
Aart Bik009cace2016-09-16 10:15:19 -0700906 DCHECK(control->IsIf());
907 AssignInfo(loop, control, CreateTripCount(tcKind, trip_count, taken_test, type));
Aart Bik9401f532015-09-28 16:25:56 -0700908}
909
910bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
911 InductionInfo* upper_expr,
912 IfCondition cmp) {
913 int64_t lower_value;
914 int64_t upper_value;
Aart Bik97412c92016-02-19 20:14:38 -0800915 switch (cmp) {
916 case kCondLT:
917 return IsAtMost(lower_expr, &lower_value)
918 && IsAtLeast(upper_expr, &upper_value)
919 && lower_value < upper_value;
920 case kCondLE:
921 return IsAtMost(lower_expr, &lower_value)
922 && IsAtLeast(upper_expr, &upper_value)
923 && lower_value <= upper_value;
924 case kCondGT:
925 return IsAtLeast(lower_expr, &lower_value)
926 && IsAtMost(upper_expr, &upper_value)
927 && lower_value > upper_value;
928 case kCondGE:
929 return IsAtLeast(lower_expr, &lower_value)
930 && IsAtMost(upper_expr, &upper_value)
931 && lower_value >= upper_value;
932 default:
933 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700934 }
935 return false; // not certain, may be untaken
936}
937
938bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
939 int64_t stride_value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100940 DataType::Type type,
Aart Bik9401f532015-09-28 16:25:56 -0700941 IfCondition cmp) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100942 int64_t min = DataType::MinValueOfIntegralType(type);
943 int64_t max = DataType::MaxValueOfIntegralType(type);
Aart Bik9401f532015-09-28 16:25:56 -0700944 // Some rules under which it is certain at compile-time that the loop is finite.
945 int64_t value;
946 switch (cmp) {
947 case kCondLT:
948 return stride_value == 1 ||
Aart Bik97412c92016-02-19 20:14:38 -0800949 (IsAtMost(upper_expr, &value) && value <= (max - stride_value + 1));
Aart Bik9401f532015-09-28 16:25:56 -0700950 case kCondLE:
Aart Bik97412c92016-02-19 20:14:38 -0800951 return (IsAtMost(upper_expr, &value) && value <= (max - stride_value));
Aart Bik9401f532015-09-28 16:25:56 -0700952 case kCondGT:
953 return stride_value == -1 ||
Aart Bik97412c92016-02-19 20:14:38 -0800954 (IsAtLeast(upper_expr, &value) && value >= (min - stride_value - 1));
Aart Bik9401f532015-09-28 16:25:56 -0700955 case kCondGE:
Aart Bik97412c92016-02-19 20:14:38 -0800956 return (IsAtLeast(upper_expr, &value) && value >= (min - stride_value));
Aart Bike9f37602015-10-09 11:15:55 -0700957 default:
958 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700959 }
960 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700961}
962
Aart Bik0d345cf2016-03-16 10:49:38 -0700963bool HInductionVarAnalysis::FitsNarrowerControl(InductionInfo* lower_expr,
964 InductionInfo* upper_expr,
965 int64_t stride_value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100966 DataType::Type type,
Aart Bik0d345cf2016-03-16 10:49:38 -0700967 IfCondition cmp) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100968 int64_t min = DataType::MinValueOfIntegralType(type);
969 int64_t max = DataType::MaxValueOfIntegralType(type);
Aart Bik0d345cf2016-03-16 10:49:38 -0700970 // Inclusive test need one extra.
971 if (stride_value != 1 && stride_value != -1) {
972 return false; // non-unit stride
973 } else if (cmp == kCondLE) {
974 max--;
975 } else if (cmp == kCondGE) {
976 min++;
977 }
978 // Do both bounds fit the range?
Vladimir Marko0e2f2ff2016-03-22 12:31:54 +0000979 int64_t value = 0;
Aart Bik0d345cf2016-03-16 10:49:38 -0700980 return IsAtLeast(lower_expr, &value) && value >= min &&
981 IsAtMost(lower_expr, &value) && value <= max &&
982 IsAtLeast(upper_expr, &value) && value >= min &&
983 IsAtMost(upper_expr, &value) && value <= max;
984}
985
Aart Bik30efb4e2015-07-30 12:14:31 -0700986void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
987 HInstruction* instruction,
988 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700989 auto it = induction_.find(loop);
990 if (it == induction_.end()) {
991 it = induction_.Put(loop,
992 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100993 std::less<HInstruction*>(),
994 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700995 }
996 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700997}
998
Aart Bike609b7c2015-08-27 13:46:58 -0700999HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
1000 HInstruction* instruction) {
1001 auto it = induction_.find(loop);
1002 if (it != induction_.end()) {
1003 auto loop_it = it->second.find(instruction);
1004 if (loop_it != it->second.end()) {
1005 return loop_it->second;
1006 }
Aart Bik30efb4e2015-07-30 12:14:31 -07001007 }
Mingyao Yang4b467ed2015-11-19 17:04:22 -08001008 if (loop->IsDefinedOutOfTheLoop(instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -07001009 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -07001010 AssignInfo(loop, instruction, info);
1011 return info;
1012 }
1013 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -07001014}
1015
Aart Bikd14c5952015-09-08 15:25:15 -07001016HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001017 DataType::Type type) {
Aart Bikc071a012016-12-01 10:22:31 -08001018 HInstruction* constant;
1019 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001020 case DataType::Type::kFloat64: constant = graph_->GetDoubleConstant(value); break;
1021 case DataType::Type::kFloat32: constant = graph_->GetFloatConstant(value); break;
1022 case DataType::Type::kInt64: constant = graph_->GetLongConstant(value); break;
1023 default: constant = graph_->GetIntConstant(value); break;
Aart Bikd14c5952015-09-08 15:25:15 -07001024 }
Aart Bikc071a012016-12-01 10:22:31 -08001025 return CreateInvariantFetch(constant);
Aart Bikd14c5952015-09-08 15:25:15 -07001026}
1027
Aart Bik471a2032015-09-04 18:22:11 -07001028HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
1029 InductionOp op,
1030 InductionInfo* a,
1031 InductionInfo* b) {
1032 // Perform some light-weight simplifications during construction of a new invariant.
1033 // This often safes memory and yields a more concise representation of the induction.
1034 // More exhaustive simplifications are done by later phases once induction nodes are
1035 // translated back into HIR code (e.g. by loop optimizations or BCE).
1036 int64_t value = -1;
Aart Bik97412c92016-02-19 20:14:38 -08001037 if (IsExact(a, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001038 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001039 // Simplify 0 + b = b, 0 ^ b = b, 0 * b = 0.
1040 if (op == kAdd || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001041 return b;
1042 } else if (op == kMul) {
1043 return a;
1044 }
Aart Bikd14c5952015-09-08 15:25:15 -07001045 } else if (op == kMul) {
1046 // Simplify 1 * b = b, -1 * b = -b
1047 if (value == 1) {
1048 return b;
1049 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001050 return CreateSimplifiedInvariant(kNeg, nullptr, b);
Aart Bikd14c5952015-09-08 15:25:15 -07001051 }
Aart Bik471a2032015-09-04 18:22:11 -07001052 }
1053 }
Aart Bik97412c92016-02-19 20:14:38 -08001054 if (IsExact(b, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001055 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001056 // Simplify a + 0 = a, a - 0 = a, a ^ 0 = a, a * 0 = 0, -0 = 0.
1057 if (op == kAdd || op == kSub || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001058 return a;
1059 } else if (op == kMul || op == kNeg) {
1060 return b;
1061 }
Aart Bikd14c5952015-09-08 15:25:15 -07001062 } else if (op == kMul || op == kDiv) {
1063 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
1064 if (value == 1) {
1065 return a;
1066 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001067 return CreateSimplifiedInvariant(kNeg, nullptr, a);
Aart Bikd14c5952015-09-08 15:25:15 -07001068 }
Aart Bik471a2032015-09-04 18:22:11 -07001069 }
1070 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -07001071 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
1072 if (op == kAdd) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001073 return CreateSimplifiedInvariant(kSub, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001074 } else if (op == kSub) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001075 return CreateSimplifiedInvariant(kAdd, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001076 } else if (op == kNeg) {
1077 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -07001078 }
Aart Bik7d57d7f2015-12-09 14:39:48 -08001079 } else if (b->operation == kSub) {
1080 // Simplify - (a - b) = b - a.
1081 if (op == kNeg) {
1082 return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a);
1083 }
Aart Bik471a2032015-09-04 18:22:11 -07001084 }
Aart Bike6bd0272016-12-16 13:57:52 -08001085 return new (graph_->GetArena()) InductionInfo(
1086 kInvariant, op, a, b, nullptr, ImplicitConversion(b->type));
Aart Bik471a2032015-09-04 18:22:11 -07001087}
1088
Aart Bikd0a022d2016-12-13 11:22:31 -08001089HInstruction* HInductionVarAnalysis::GetShiftConstant(HLoopInformation* loop,
1090 HInstruction* instruction,
1091 InductionInfo* initial) {
1092 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
1093 // Shift-rights are only the same as division for non-negative initial inputs.
1094 // Otherwise we would round incorrectly.
1095 if (initial != nullptr) {
1096 int64_t value = -1;
1097 if (!IsAtLeast(initial, &value) || value < 0) {
1098 return nullptr;
1099 }
1100 }
1101 // Obtain the constant needed to treat shift as equivalent multiplication or division.
1102 // This yields an existing instruction if the constant is already there. Otherwise, this
1103 // has a side effect on the HIR. The restriction on the shift factor avoids generating a
1104 // negative constant (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that
1105 // generalization for shift factors outside [0,32) and [0,64) ranges is done earlier.
Aart Bikc071a012016-12-01 10:22:31 -08001106 InductionInfo* b = LookupInfo(loop, instruction->InputAt(1));
1107 int64_t value = -1;
1108 if (IsExact(b, &value)) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001109 DataType::Type type = instruction->InputAt(0)->GetType();
1110 if (type == DataType::Type::kInt32 && 0 <= value && value < 31) {
Aart Bikc071a012016-12-01 10:22:31 -08001111 return graph_->GetIntConstant(1 << value);
1112 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001113 if (type == DataType::Type::kInt64 && 0 <= value && value < 63) {
Aart Bikc071a012016-12-01 10:22:31 -08001114 return graph_->GetLongConstant(1L << value);
1115 }
1116 }
1117 return nullptr;
1118}
Aart Bikcc42be02016-10-20 16:14:16 -07001119
1120void HInductionVarAnalysis::AssignCycle(HPhi* phi) {
1121 ArenaSet<HInstruction*>* set = &cycles_.Put(phi, ArenaSet<HInstruction*>(
1122 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)))->second;
1123 for (HInstruction* i : scc_) {
1124 set->insert(i);
1125 }
1126}
1127
1128ArenaSet<HInstruction*>* HInductionVarAnalysis::LookupCycle(HPhi* phi) {
1129 auto it = cycles_.find(phi);
1130 if (it != cycles_.end()) {
1131 return &it->second;
1132 }
1133 return nullptr;
1134}
1135
Aart Bik97412c92016-02-19 20:14:38 -08001136bool HInductionVarAnalysis::IsExact(InductionInfo* info, int64_t* value) {
1137 return InductionVarRange(this).IsConstant(info, InductionVarRange::kExact, value);
1138}
1139
1140bool HInductionVarAnalysis::IsAtMost(InductionInfo* info, int64_t* value) {
1141 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtMost, value);
1142}
1143
1144bool HInductionVarAnalysis::IsAtLeast(InductionInfo* info, int64_t* value) {
1145 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtLeast, value);
Aart Bik7d57d7f2015-12-09 14:39:48 -08001146}
1147
Aart Bike6bd0272016-12-16 13:57:52 -08001148bool HInductionVarAnalysis::IsNarrowingLinear(InductionInfo* info) {
1149 return info != nullptr &&
1150 info->induction_class == kLinear &&
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001151 (info->type == DataType::Type::kUint8 ||
1152 info->type == DataType::Type::kInt8 ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001153 info->type == DataType::Type::kUint16 ||
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001154 info->type == DataType::Type::kInt16 ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001155 (info->type == DataType::Type::kInt32 && (info->op_a->type == DataType::Type::kInt64 ||
1156 info->op_b->type == DataType::Type::kInt64)));
Aart Bike6bd0272016-12-16 13:57:52 -08001157}
1158
Aart Bik30efb4e2015-07-30 12:14:31 -07001159bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
1160 InductionInfo* info2) {
1161 // Test structural equality only, without accounting for simplifications.
1162 if (info1 != nullptr && info2 != nullptr) {
1163 return
1164 info1->induction_class == info2->induction_class &&
1165 info1->operation == info2->operation &&
1166 info1->fetch == info2->fetch &&
Aart Bik78296912016-03-25 13:14:53 -07001167 info1->type == info2->type &&
Aart Bik30efb4e2015-07-30 12:14:31 -07001168 InductionEqual(info1->op_a, info2->op_a) &&
1169 InductionEqual(info1->op_b, info2->op_b);
1170 }
1171 // Otherwise only two nullptrs are considered equal.
1172 return info1 == info2;
1173}
1174
Aart Bikc071a012016-12-01 10:22:31 -08001175std::string HInductionVarAnalysis::FetchToString(HInstruction* fetch) {
1176 DCHECK(fetch != nullptr);
1177 if (fetch->IsIntConstant()) {
1178 return std::to_string(fetch->AsIntConstant()->GetValue());
1179 } else if (fetch->IsLongConstant()) {
1180 return std::to_string(fetch->AsLongConstant()->GetValue());
1181 }
1182 return std::to_string(fetch->GetId()) + ":" + fetch->DebugName();
1183}
1184
Aart Bik30efb4e2015-07-30 12:14:31 -07001185std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
1186 if (info != nullptr) {
1187 if (info->induction_class == kInvariant) {
1188 std::string inv = "(";
1189 inv += InductionToString(info->op_a);
1190 switch (info->operation) {
Aart Bik22f05872015-10-27 15:56:28 -07001191 case kNop: inv += " @ "; break;
1192 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001193 case kSub:
Aart Bik22f05872015-10-27 15:56:28 -07001194 case kNeg: inv += " - "; break;
1195 case kMul: inv += " * "; break;
1196 case kDiv: inv += " / "; break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001197 case kRem: inv += " % "; break;
Aart Bik7dc96932016-10-12 10:01:05 -07001198 case kXor: inv += " ^ "; break;
Aart Bik22f05872015-10-27 15:56:28 -07001199 case kLT: inv += " < "; break;
1200 case kLE: inv += " <= "; break;
1201 case kGT: inv += " > "; break;
1202 case kGE: inv += " >= "; break;
Aart Bikc071a012016-12-01 10:22:31 -08001203 case kFetch: inv += FetchToString(info->fetch); break;
Aart Bik22f05872015-10-27 15:56:28 -07001204 case kTripCountInLoop: inv += " (TC-loop) "; break;
1205 case kTripCountInBody: inv += " (TC-body) "; break;
1206 case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break;
1207 case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001208 }
1209 inv += InductionToString(info->op_b);
Aart Bik0d345cf2016-03-16 10:49:38 -07001210 inv += ")";
1211 return inv;
Aart Bik30efb4e2015-07-30 12:14:31 -07001212 } else {
Aart Bik30efb4e2015-07-30 12:14:31 -07001213 if (info->induction_class == kLinear) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001214 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001215 return "(" + InductionToString(info->op_a) + " * i + " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001216 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001217 DataType::PrettyDescriptor(info->type);
Aart Bikc071a012016-12-01 10:22:31 -08001218 } else if (info->induction_class == kPolynomial) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001219 DCHECK(info->operation == kNop);
1220 return "poly(sum_lt(" + InductionToString(info->op_a) + ") + " +
Aart Bikc071a012016-12-01 10:22:31 -08001221 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001222 DataType::PrettyDescriptor(info->type);
Aart Bikc071a012016-12-01 10:22:31 -08001223 } else if (info->induction_class == kGeometric) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001224 DCHECK(info->operation == kMul || info->operation == kDiv);
Aart Bikc071a012016-12-01 10:22:31 -08001225 DCHECK(info->fetch != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001226 return "geo(" + InductionToString(info->op_a) + " * " +
1227 FetchToString(info->fetch) +
1228 (info->operation == kMul ? " ^ i + " : " ^ -i + ") +
1229 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001230 DataType::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001231 } else if (info->induction_class == kWrapAround) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001232 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001233 return "wrap(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001234 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001235 DataType::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001236 } else if (info->induction_class == kPeriodic) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001237 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001238 return "periodic(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001239 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001240 DataType::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001241 }
1242 }
1243 }
1244 return "";
1245}
1246
1247} // namespace art