blob: ad29ba56aba75dd838f841afd5e9d6a284a56612 [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
Aart Bik2ca10eb2017-11-15 15:17:53 -0800100HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph, const char* name)
101 : HOptimization(graph, name),
Aart Bik30efb4e2015-07-30 12:14:31 -0700102 global_depth_(0),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100103 stack_(graph->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100104 map_(std::less<HInstruction*>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100105 graph->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)),
106 scc_(graph->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)),
Vladimir Marko5233f932015-09-29 19:01:15 +0100107 cycle_(std::less<HInstruction*>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100108 graph->GetAllocator()->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*>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100111 graph->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)),
Aart Bikcc42be02016-10-20 16:14:16 -0700112 cycles_(std::less<HPhi*>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100113 graph->GetAllocator()->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 Markoca6fff82017-10-03 14:49:14 +0100268 ArenaVector<HInstruction*> other(
269 graph_->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700270 RotateEntryPhiFirst(loop, &scc_, &other);
271 }
272
Aart Bikcc42be02016-10-20 16:14:16 -0700273 // Analyze from loop-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700274 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700275 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700276 return;
277 }
Aart Bikf475bee2015-09-16 12:50:25 -0700278
279 // External link should be loop invariant.
280 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700281 if (initial == nullptr || initial->induction_class != kInvariant) {
282 return;
283 }
284
Aart Bike6bd0272016-12-16 13:57:52 -0800285 // Store interesting cycle in each loop phi.
286 for (size_t i = 0; i < size; i++) {
287 if (scc_[i]->IsLoopHeaderPhi()) {
288 AssignCycle(scc_[i]->AsPhi());
289 }
290 }
Aart Bikcc42be02016-10-20 16:14:16 -0700291
Aart Bikf475bee2015-09-16 12:50:25 -0700292 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700293 if (size == 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800294 InductionInfo* update = TransferPhi(loop, phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700295 if (update != nullptr) {
Aart Bikc071a012016-12-01 10:22:31 -0800296 AssignInfo(loop, phi, CreateInduction(kWrapAround,
297 kNop,
298 initial,
299 update,
300 /*fetch*/ nullptr,
301 type_));
Aart Bik30efb4e2015-07-30 12:14:31 -0700302 }
303 return;
304 }
305
306 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700307 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700308 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700309 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700310 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700311 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700312 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700313 } else if (instruction->IsAdd()) {
314 update = SolveAddSub(
315 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
316 } else if (instruction->IsSub()) {
317 update = SolveAddSub(
318 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bikc071a012016-12-01 10:22:31 -0800319 } else if (instruction->IsMul()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800320 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800321 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kMul);
322 } else if (instruction->IsDiv()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800323 update = SolveOp(
Aart Bikc071a012016-12-01 10:22:31 -0800324 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kDiv);
325 } else if (instruction->IsRem()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800326 update = SolveOp(
327 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kRem);
Aart Bikc071a012016-12-01 10:22:31 -0800328 } else if (instruction->IsShl()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800329 HInstruction* mulc = GetShiftConstant(loop, instruction, /*initial*/ nullptr);
Aart Bikc071a012016-12-01 10:22:31 -0800330 if (mulc != nullptr) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800331 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), mulc, kMul);
Aart Bikc071a012016-12-01 10:22:31 -0800332 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800333 } else if (instruction->IsShr() || instruction->IsUShr()) {
334 HInstruction* divc = GetShiftConstant(loop, instruction, initial);
335 if (divc != nullptr) {
336 update = SolveOp(loop, phi, instruction, instruction->InputAt(0), divc, kDiv);
337 }
Aart Bik7dc96932016-10-12 10:01:05 -0700338 } else if (instruction->IsXor()) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800339 update = SolveOp(
340 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700341 } else if (instruction->IsEqual()) {
342 update = SolveTest(loop, phi, instruction, 0);
343 } else if (instruction->IsNotEqual()) {
344 update = SolveTest(loop, phi, instruction, 1);
Aart Bikd0a022d2016-12-13 11:22:31 -0800345 } else if (instruction->IsSelect()) {
346 update = SolvePhi(instruction, /*input_index*/ 0, /*adjust_input_size*/ 1); // acts like Phi
Aart Bik0d345cf2016-03-16 10:49:38 -0700347 } else if (instruction->IsTypeConversion()) {
Aart Bike6bd0272016-12-16 13:57:52 -0800348 update = SolveConversion(loop, phi, instruction->AsTypeConversion());
Aart Bik30efb4e2015-07-30 12:14:31 -0700349 }
350 if (update == nullptr) {
351 return;
352 }
Aart Bike609b7c2015-08-27 13:46:58 -0700353 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700354 }
355
Aart Bikf475bee2015-09-16 12:50:25 -0700356 // Success if all internal links received the same temporary meaning.
Aart Bikd0a022d2016-12-13 11:22:31 -0800357 InductionInfo* induction = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700358 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700359 switch (induction->induction_class) {
360 case kInvariant:
Aart Bikc071a012016-12-01 10:22:31 -0800361 // Construct combined stride of the linear induction.
362 induction = CreateInduction(kLinear, kNop, induction, initial, /*fetch*/ nullptr, type_);
363 FALLTHROUGH_INTENDED;
364 case kPolynomial:
365 case kGeometric:
Aart Bikdf7822e2016-12-06 10:05:30 -0800366 case kWrapAround:
Aart Bik22af3be2015-09-10 12:50:58 -0700367 // Classify first phi and then the rest of the cycle "on-demand".
368 // Statements are scanned in order.
Aart Bikc071a012016-12-01 10:22:31 -0800369 AssignInfo(loop, phi, induction);
Aart Bik22af3be2015-09-10 12:50:58 -0700370 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700371 ClassifyTrivial(loop, scc_[i]);
372 }
373 break;
374 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700375 // Classify all elements in the cycle with the found periodic induction while
376 // rotating each first element to the end. Lastly, phi is classified.
377 // Statements are scanned in reverse order.
378 for (size_t i = size - 1; i >= 1; i--) {
379 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700380 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
381 }
382 AssignInfo(loop, phi, induction);
383 break;
384 default:
385 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700386 }
387 }
388}
389
Aart Bike609b7c2015-08-27 13:46:58 -0700390HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
391 InductionInfo* induction,
392 InductionInfo* last) {
393 // Rotates a periodic induction of the form
394 // (a, b, c, d, e)
395 // into
396 // (b, c, d, e, a)
397 // in preparation of assigning this to the previous variable in the sequence.
398 if (induction->induction_class == kInvariant) {
Aart Bikc071a012016-12-01 10:22:31 -0800399 return CreateInduction(kPeriodic,
400 kNop,
401 induction,
402 last,
403 /*fetch*/ nullptr,
404 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700405 }
Aart Bikc071a012016-12-01 10:22:31 -0800406 return CreateInduction(kPeriodic,
407 kNop,
408 induction->op_a,
409 RotatePeriodicInduction(induction->op_b, last),
410 /*fetch*/ nullptr,
411 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700412}
413
Aart Bikf475bee2015-09-16 12:50:25 -0700414HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
415 HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800416 size_t input_index,
417 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700418 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100419 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100420 DCHECK_LT(input_index, inputs.size());
421 InductionInfo* a = LookupInfo(loop, inputs[input_index]);
Aart Bikd0a022d2016-12-13 11:22:31 -0800422 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100423 InductionInfo* b = LookupInfo(loop, inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700424 if (!InductionEqual(a, b)) {
425 return nullptr;
426 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700427 }
Aart Bikf475bee2015-09-16 12:50:25 -0700428 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700429}
430
431HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
432 InductionInfo* b,
433 InductionOp op) {
Aart Bikc071a012016-12-01 10:22:31 -0800434 // Transfer over an addition or subtraction: any invariant, linear, polynomial, geometric,
435 // wrap-around, or periodic can be combined with an invariant to yield a similar result.
Aart Bikdf7822e2016-12-06 10:05:30 -0800436 // Two linear or two polynomial inputs can be combined too. Other combinations fail.
Aart Bik30efb4e2015-07-30 12:14:31 -0700437 if (a != nullptr && b != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800438 if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) {
439 return nullptr; // no transfer
440 } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800441 return CreateInvariantOp(op, a, b); // direct invariant
Aart Bikdf7822e2016-12-06 10:05:30 -0800442 } else if ((a->induction_class == kLinear && b->induction_class == kLinear) ||
443 (a->induction_class == kPolynomial && b->induction_class == kPolynomial)) {
Aart Bik74da5292016-12-20 11:13:03 -0800444 // Rule induc(a, b) + induc(a', b') -> induc(a + a', b + b').
445 InductionInfo* new_a = TransferAddSub(a->op_a, b->op_a, op);
446 InductionInfo* new_b = TransferAddSub(a->op_b, b->op_b, op);
447 if (new_a != nullptr && new_b != nullptr) {
448 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
449 }
Aart Bike609b7c2015-08-27 13:46:58 -0700450 } else if (a->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800451 // Rule a + induc(a', b') -> induc(a', a + b') or induc(a + a', a + b').
Aart Bike609b7c2015-08-27 13:46:58 -0700452 InductionInfo* new_a = b->op_a;
453 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800454 if (b->induction_class == kWrapAround || b->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700455 new_a = TransferAddSub(a, new_a, op);
456 } else if (op == kSub) { // Negation required.
457 new_a = TransferNeg(new_a);
458 }
Aart Bik74da5292016-12-20 11:13:03 -0800459 if (new_a != nullptr && new_b != nullptr) {
460 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
461 }
Aart Bike609b7c2015-08-27 13:46:58 -0700462 } else if (b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800463 // Rule induc(a, b) + b' -> induc(a, b + b') or induc(a + b', b + b').
Aart Bike609b7c2015-08-27 13:46:58 -0700464 InductionInfo* new_a = a->op_a;
465 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
Aart Bikc071a012016-12-01 10:22:31 -0800466 if (a->induction_class == kWrapAround || a->induction_class == kPeriodic) {
Aart Bike609b7c2015-08-27 13:46:58 -0700467 new_a = TransferAddSub(new_a, b, op);
468 }
Aart Bik74da5292016-12-20 11:13:03 -0800469 if (new_a != nullptr && new_b != nullptr) {
470 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
471 }
Aart Bikc071a012016-12-01 10:22:31 -0800472 }
473 }
474 return nullptr;
475}
476
477HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
478 // Transfer over a unary negation: an invariant, linear, polynomial, geometric (mul),
479 // wrap-around, or periodic input yields a similar but negated induction as result.
480 if (a != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800481 if (IsNarrowingLinear(a)) {
482 return nullptr; // no transfer
483 } else if (a->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800484 return CreateInvariantOp(kNeg, nullptr, a); // direct invariant
Aart Bikc071a012016-12-01 10:22:31 -0800485 } else if (a->induction_class != kGeometric || a->operation == kMul) {
Aart Bik74da5292016-12-20 11:13:03 -0800486 // Rule - induc(a, b) -> induc(-a, -b).
487 InductionInfo* new_a = TransferNeg(a->op_a);
488 InductionInfo* new_b = TransferNeg(a->op_b);
489 if (new_a != nullptr && new_b != nullptr) {
490 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
491 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700492 }
493 }
494 return nullptr;
495}
496
497HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
498 InductionInfo* b) {
Aart Bikc071a012016-12-01 10:22:31 -0800499 // Transfer over a multiplication: any invariant, linear, polynomial, geometric (mul),
500 // wrap-around, or periodic can be multiplied with an invariant to yield a similar
501 // but multiplied result. Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700502 if (a != nullptr && b != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800503 if (IsNarrowingLinear(a) || IsNarrowingLinear(b)) {
504 return nullptr; // no transfer
505 } else if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik74da5292016-12-20 11:13:03 -0800506 return CreateInvariantOp(kMul, a, b); // direct invariant
Aart Bikc071a012016-12-01 10:22:31 -0800507 } else if (a->induction_class == kInvariant && (b->induction_class != kGeometric ||
508 b->operation == kMul)) {
Aart Bik74da5292016-12-20 11:13:03 -0800509 // Rule a * induc(a', b') -> induc(a * a', b * b').
510 InductionInfo* new_a = TransferMul(a, b->op_a);
511 InductionInfo* new_b = TransferMul(a, b->op_b);
512 if (new_a != nullptr && new_b != nullptr) {
513 return CreateInduction(b->induction_class, b->operation, new_a, new_b, b->fetch, type_);
514 }
Aart Bikc071a012016-12-01 10:22:31 -0800515 } else if (b->induction_class == kInvariant && (a->induction_class != kGeometric ||
516 a->operation == kMul)) {
Aart Bik74da5292016-12-20 11:13:03 -0800517 // Rule induc(a, b) * b' -> induc(a * b', b * b').
518 InductionInfo* new_a = TransferMul(a->op_a, b);
519 InductionInfo* new_b = TransferMul(a->op_b, b);
520 if (new_a != nullptr && new_b != nullptr) {
521 return CreateInduction(a->induction_class, a->operation, new_a, new_b, a->fetch, type_);
522 }
Aart Bike609b7c2015-08-27 13:46:58 -0700523 }
524 }
525 return nullptr;
526}
527
Aart Bike6bd0272016-12-16 13:57:52 -0800528HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferConversion(
529 InductionInfo* a,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 DataType::Type from,
531 DataType::Type to) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700532 if (a != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800533 // Allow narrowing conversion on linear induction in certain cases:
534 // induction is already at narrow type, or can be made narrower.
535 if (IsNarrowingIntegralConversion(from, to) &&
536 a->induction_class == kLinear &&
537 (a->type == to || IsNarrowingIntegralConversion(a->type, to))) {
Aart Bik74da5292016-12-20 11:13:03 -0800538 return CreateInduction(kLinear, kNop, a->op_a, a->op_b, a->fetch, to);
Aart Bik0d345cf2016-03-16 10:49:38 -0700539 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700540 }
541 return nullptr;
542}
543
Aart Bikf475bee2015-09-16 12:50:25 -0700544HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
Aart Bikd0a022d2016-12-13 11:22:31 -0800545 size_t input_index,
546 size_t adjust_input_size) {
Aart Bikf475bee2015-09-16 12:50:25 -0700547 // Match all phi inputs from input_index onwards exactly.
Vladimir Markoe9004912016-06-16 16:50:52 +0100548 HInputsRef inputs = phi->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100549 DCHECK_LT(input_index, inputs.size());
550 auto ita = cycle_.find(inputs[input_index]);
Aart Bik30efb4e2015-07-30 12:14:31 -0700551 if (ita != cycle_.end()) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800552 for (size_t i = input_index + 1, n = inputs.size() - adjust_input_size; i < n; i++) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100553 auto itb = cycle_.find(inputs[i]);
Aart Bikf475bee2015-09-16 12:50:25 -0700554 if (itb == cycle_.end() ||
555 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700556 return nullptr;
557 }
558 }
Aart Bikf475bee2015-09-16 12:50:25 -0700559 return ita->second;
560 }
561 return nullptr;
562}
563
564HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
565 HLoopInformation* loop,
566 HInstruction* entry_phi,
567 HInstruction* phi) {
568 // Match all phi inputs.
Aart Bikd0a022d2016-12-13 11:22:31 -0800569 InductionInfo* match = SolvePhi(phi, /*input_index*/ 0, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700570 if (match != nullptr) {
571 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700572 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700573
Aart Bikf475bee2015-09-16 12:50:25 -0700574 // Otherwise, try to solve for a periodic seeded from phi onward.
575 // Only tight multi-statement cycles are considered in order to
576 // simplify rotating the periodic during the final classification.
577 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
578 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700579 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700580 if (phi->InputAt(1) == entry_phi) {
581 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800582 return CreateInduction(kPeriodic, kNop, a, initial, /*fetch*/ nullptr, type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700583 }
Aart Bikd0a022d2016-12-13 11:22:31 -0800584 InductionInfo* b = SolvePhi(phi, /*input_index*/ 1, /*adjust_input_size*/ 0);
Aart Bikf475bee2015-09-16 12:50:25 -0700585 if (b != nullptr && b->induction_class == kPeriodic) {
Aart Bikc071a012016-12-01 10:22:31 -0800586 return CreateInduction(kPeriodic, kNop, a, b, /*fetch*/ nullptr, type_);
Aart Bik30efb4e2015-07-30 12:14:31 -0700587 }
588 }
589 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700590 return nullptr;
591}
592
Aart Bike609b7c2015-08-27 13:46:58 -0700593HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700594 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700595 HInstruction* instruction,
596 HInstruction* x,
597 HInstruction* y,
598 InductionOp op,
599 bool is_first_call) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800600 // Solve within a cycle over an addition or subtraction.
Aart Bike609b7c2015-08-27 13:46:58 -0700601 InductionInfo* b = LookupInfo(loop, y);
Aart Bikdf7822e2016-12-06 10:05:30 -0800602 if (b != nullptr) {
603 if (b->induction_class == kInvariant) {
604 // Adding or subtracting an invariant value, seeded from phi,
605 // keeps adding to the stride of the linear induction.
606 if (x == entry_phi) {
607 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
608 }
609 auto it = cycle_.find(x);
610 if (it != cycle_.end()) {
611 InductionInfo* a = it->second;
612 if (a->induction_class == kInvariant) {
613 return CreateInvariantOp(op, a, b);
614 }
615 }
Aart Bik74da5292016-12-20 11:13:03 -0800616 } else if (b->induction_class == kLinear && b->type == type_) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800617 // Solve within a tight cycle that adds a term that is already classified as a linear
618 // induction for a polynomial induction k = k + i (represented as sum over linear terms).
619 if (x == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
620 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik74da5292016-12-20 11:13:03 -0800621 InductionInfo* new_a = op == kAdd ? b : TransferNeg(b);
622 if (new_a != nullptr) {
623 return CreateInduction(kPolynomial, kNop, new_a, initial, /*fetch*/ nullptr, type_);
624 }
Aart Bike609b7c2015-08-27 13:46:58 -0700625 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700626 }
627 }
Aart Bike609b7c2015-08-27 13:46:58 -0700628
629 // Try some alternatives before failing.
630 if (op == kAdd) {
631 // Try the other way around for an addition if considered for first time.
632 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700633 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700634 }
635 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700636 // Solve within a tight cycle that is formed by exactly two instructions,
Aart Bikc071a012016-12-01 10:22:31 -0800637 // one phi and one update, for a periodic idiom of the form k = c - k.
Aart Bikf475bee2015-09-16 12:50:25 -0700638 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700639 InductionInfo* a = LookupInfo(loop, x);
640 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700641 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bikc071a012016-12-01 10:22:31 -0800642 return CreateInduction(kPeriodic,
643 kNop,
644 CreateInvariantOp(kSub, a, initial),
645 initial,
646 /*fetch*/ nullptr,
647 type_);
Aart Bike609b7c2015-08-27 13:46:58 -0700648 }
649 }
650 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700651 return nullptr;
652}
653
Aart Bikdf7822e2016-12-06 10:05:30 -0800654HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveOp(HLoopInformation* loop,
Aart Bikc071a012016-12-01 10:22:31 -0800655 HInstruction* entry_phi,
656 HInstruction* instruction,
657 HInstruction* x,
658 HInstruction* y,
659 InductionOp op) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800660 // 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 -0700661 if (entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800662 InductionInfo* c = nullptr;
Aart Bik639cc8c2016-10-18 13:03:31 -0700663 InductionInfo* b = LookupInfo(loop, y);
664 if (b != nullptr && b->induction_class == kInvariant && entry_phi == x) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800665 c = b;
666 } else if (op != kDiv && op != kRem) {
667 InductionInfo* a = LookupInfo(loop, x);
668 if (a != nullptr && a->induction_class == kInvariant && entry_phi == y) {
669 c = a;
670 }
671 }
672 // Found suitable operand left or right?
673 if (c != nullptr) {
674 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
675 switch (op) {
676 case kMul:
677 case kDiv:
678 // Restrict base of geometric induction to direct fetch.
679 if (c->operation == kFetch) {
680 return CreateInduction(kGeometric,
681 op,
682 initial,
683 CreateConstant(0, type_),
684 c->fetch,
685 type_);
Igor Murashkin2ffb7032017-11-08 13:35:21 -0800686 }
Aart Bikdf7822e2016-12-06 10:05:30 -0800687 break;
688 case kRem:
689 // Idiomatic MOD wrap-around induction.
690 return CreateInduction(kWrapAround,
691 kNop,
692 initial,
693 CreateInvariantOp(kRem, initial, c),
694 /*fetch*/ nullptr,
695 type_);
696 case kXor:
697 // Idiomatic XOR periodic induction.
698 return CreateInduction(kPeriodic,
699 kNop,
700 CreateInvariantOp(kXor, initial, c),
701 initial,
702 /*fetch*/ nullptr,
703 type_);
704 default:
Andreas Gampef45d61c2017-06-07 10:29:33 -0700705 LOG(FATAL) << op;
706 UNREACHABLE();
Aart Bikdf7822e2016-12-06 10:05:30 -0800707 }
Aart Bik7dc96932016-10-12 10:01:05 -0700708 }
709 }
Aart Bik639cc8c2016-10-18 13:03:31 -0700710 return nullptr;
711}
712
713HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveTest(HLoopInformation* loop,
714 HInstruction* entry_phi,
715 HInstruction* instruction,
716 int64_t opposite_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800717 // Detect hidden XOR construction in x = (x == false) or x = (x != true).
Aart Bik639cc8c2016-10-18 13:03:31 -0700718 int64_t value = -1;
719 HInstruction* x = instruction->InputAt(0);
720 HInstruction* y = instruction->InputAt(1);
721 if (IsExact(LookupInfo(loop, x), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800722 return SolveOp(loop, entry_phi, instruction, graph_->GetIntConstant(1), y, kXor);
Aart Bik639cc8c2016-10-18 13:03:31 -0700723 } else if (IsExact(LookupInfo(loop, y), &value) && value == opposite_value) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800724 return SolveOp(loop, entry_phi, instruction, x, graph_->GetIntConstant(1), kXor);
Aart Bik7dc96932016-10-12 10:01:05 -0700725 }
726 return nullptr;
727}
728
Aart Bike6bd0272016-12-16 13:57:52 -0800729HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveConversion(
730 HLoopInformation* loop,
731 HInstruction* entry_phi,
732 HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100733 DataType::Type from = conversion->GetInputType();
734 DataType::Type to = conversion->GetResultType();
Aart Bike6bd0272016-12-16 13:57:52 -0800735 // A narrowing conversion is allowed as *last* operation of the cycle of a linear induction
736 // with an initial value that fits the type, provided that the narrowest encountered type is
737 // recorded with the induction to account for the precision loss. The narrower induction does
738 // *not* transfer to any wider operations, however, since these may yield out-of-type values
739 if (entry_phi->InputCount() == 2 && conversion == entry_phi->InputAt(1)) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100740 int64_t min = DataType::MinValueOfIntegralType(to);
741 int64_t max = DataType::MaxValueOfIntegralType(to);
Aart Bike6bd0272016-12-16 13:57:52 -0800742 int64_t value = 0;
743 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
744 if (IsNarrowingIntegralConversion(from, to) &&
745 IsAtLeast(initial, &value) && value >= min &&
746 IsAtMost(initial, &value) && value <= max) {
747 auto it = cycle_.find(conversion->GetInput());
748 if (it != cycle_.end() && it->second->induction_class == kInvariant) {
749 type_ = to;
750 return it->second;
751 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700752 }
753 }
754 return nullptr;
755}
756
Aart Bikd14c5952015-09-08 15:25:15 -0700757void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
758 HInstruction* control = loop->GetHeader()->GetLastInstruction();
759 if (control->IsIf()) {
760 HIf* ifs = control->AsIf();
761 HBasicBlock* if_true = ifs->IfTrueSuccessor();
762 HBasicBlock* if_false = ifs->IfFalseSuccessor();
763 HInstruction* if_expr = ifs->InputAt(0);
764 // Determine if loop has following structure in header.
765 // loop-header: ....
766 // if (condition) goto X
767 if (if_expr->IsCondition()) {
768 HCondition* condition = if_expr->AsCondition();
769 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
770 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100771 DataType::Type type = ImplicitConversion(condition->InputAt(0)->GetType());
Aart Bik0d345cf2016-03-16 10:49:38 -0700772 // Determine if the loop control uses a known sequence on an if-exit (X outside) or on
773 // an if-iterate (X inside), expressed as if-iterate when passed into VisitCondition().
774 if (a == nullptr || b == nullptr) {
775 return; // Loop control is not a sequence.
Aart Bikd14c5952015-09-08 15:25:15 -0700776 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
777 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
778 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
779 VisitCondition(loop, a, b, type, condition->GetCondition());
780 }
781 }
782 }
783}
784
785void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
786 InductionInfo* a,
787 InductionInfo* b,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100788 DataType::Type type,
Aart Bikd14c5952015-09-08 15:25:15 -0700789 IfCondition cmp) {
790 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700791 // 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 -0700792 switch (cmp) {
793 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
794 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
795 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
796 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700797 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700798 default: break;
799 }
800 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700801 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bik9401f532015-09-28 16:25:56 -0700802 InductionInfo* lower_expr = a->op_b;
803 InductionInfo* upper_expr = b;
Aart Bik97412c922016-02-19 20:14:38 -0800804 InductionInfo* stride_expr = a->op_a;
805 // Constant stride?
Aart Bik9401f532015-09-28 16:25:56 -0700806 int64_t stride_value = 0;
Aart Bik97412c922016-02-19 20:14:38 -0800807 if (!IsExact(stride_expr, &stride_value)) {
Aart Bikf475bee2015-09-16 12:50:25 -0700808 return;
809 }
Aart Bik358af832016-02-24 14:17:53 -0800810 // Rewrite condition i != U into strict end condition i < U or i > U if this end condition
811 // is reached exactly (tested by verifying if the loop has a unit stride and the non-strict
812 // condition would be always taken).
813 if (cmp == kCondNE && ((stride_value == +1 && IsTaken(lower_expr, upper_expr, kCondLE)) ||
814 (stride_value == -1 && IsTaken(lower_expr, upper_expr, kCondGE)))) {
Aart Bik9401f532015-09-28 16:25:56 -0700815 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700816 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700817 // Only accept integral condition. A mismatch between the type of condition and the induction
818 // is only allowed if the, necessarily narrower, induction range fits the narrower control.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100819 if (type != DataType::Type::kInt32 && type != DataType::Type::kInt64) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700820 return; // not integral
821 } else if (type != a->type &&
822 !FitsNarrowerControl(lower_expr, upper_expr, stride_value, a->type, cmp)) {
823 return; // mismatched type
824 }
Aart Bikf475bee2015-09-16 12:50:25 -0700825 // Normalize a linear loop control with a nonzero stride:
826 // stride > 0, either i < U or i <= U
827 // stride < 0, either i > U or i >= U
Aart Bikf475bee2015-09-16 12:50:25 -0700828 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
829 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
Aart Bik97412c922016-02-19 20:14:38 -0800830 VisitTripCount(loop, lower_expr, upper_expr, stride_expr, stride_value, type, cmp);
Aart Bikf475bee2015-09-16 12:50:25 -0700831 }
Aart Bikd14c5952015-09-08 15:25:15 -0700832 }
833}
834
835void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
Aart Bik9401f532015-09-28 16:25:56 -0700836 InductionInfo* lower_expr,
837 InductionInfo* upper_expr,
Aart Bik97412c922016-02-19 20:14:38 -0800838 InductionInfo* stride_expr,
Aart Bik9401f532015-09-28 16:25:56 -0700839 int64_t stride_value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100840 DataType::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700841 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700842 // Any loop of the general form:
843 //
844 // for (i = L; i <= U; i += S) // S > 0
845 // or for (i = L; i >= U; i += S) // S < 0
846 // .. i ..
847 //
848 // can be normalized into:
849 //
850 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
851 // .. L + S * n ..
852 //
Aart Bik9401f532015-09-28 16:25:56 -0700853 // taking the following into consideration:
Aart Bikd14c5952015-09-08 15:25:15 -0700854 //
Aart Bik9401f532015-09-28 16:25:56 -0700855 // (1) Using the same precision, the TC (trip-count) expression should be interpreted as
856 // an unsigned entity, for example, as in the following loop that uses the full range:
857 // for (int i = INT_MIN; i < INT_MAX; i++) // TC = UINT_MAX
858 // (2) The TC is only valid if the loop is taken, otherwise TC = 0, as in:
Aart Bikd5cc6832016-06-22 16:34:46 -0700859 // for (int i = 12; i < U; i++) // TC = 0 when U <= 12
Aart Bik9401f532015-09-28 16:25:56 -0700860 // If this cannot be determined at compile-time, the TC is only valid within the
Aart Bik22f05872015-10-27 15:56:28 -0700861 // loop-body proper, not the loop-header unless enforced with an explicit taken-test.
Aart Bik9401f532015-09-28 16:25:56 -0700862 // (3) The TC is only valid if the loop is finite, otherwise TC has no value, as in:
863 // for (int i = 0; i <= U; i++) // TC = Inf when U = INT_MAX
864 // If this cannot be determined at compile-time, the TC is only valid when enforced
Aart Bik22f05872015-10-27 15:56:28 -0700865 // with an explicit finite-test.
Aart Bik9401f532015-09-28 16:25:56 -0700866 // (4) For loops which early-exits, the TC forms an upper bound, as in:
867 // for (int i = 0; i < 10 && ....; i++) // TC <= 10
Aart Bik22f05872015-10-27 15:56:28 -0700868 InductionInfo* trip_count = upper_expr;
Aart Bik9401f532015-09-28 16:25:56 -0700869 const bool is_taken = IsTaken(lower_expr, upper_expr, cmp);
870 const bool is_finite = IsFinite(upper_expr, stride_value, type, cmp);
871 const bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700872 if (!cancels) {
873 // Convert exclusive integral inequality into inclusive integral inequality,
874 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700875 if (cmp == kCondLT) {
Aart Bik22f05872015-10-27 15:56:28 -0700876 trip_count = CreateInvariantOp(kSub, trip_count, CreateConstant(1, type));
Aart Bikf475bee2015-09-16 12:50:25 -0700877 } else if (cmp == kCondGT) {
Aart Bik22f05872015-10-27 15:56:28 -0700878 trip_count = CreateInvariantOp(kAdd, trip_count, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700879 }
880 // Compensate for stride.
Aart Bik97412c922016-02-19 20:14:38 -0800881 trip_count = CreateInvariantOp(kAdd, trip_count, stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700882 }
Aart Bik97412c922016-02-19 20:14:38 -0800883 trip_count = CreateInvariantOp(
884 kDiv, CreateInvariantOp(kSub, trip_count, lower_expr), stride_expr);
Aart Bikd14c5952015-09-08 15:25:15 -0700885 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bik9401f532015-09-28 16:25:56 -0700886 // should be aware that the expression is only valid under the conditions listed above.
Aart Bik22f05872015-10-27 15:56:28 -0700887 InductionOp tcKind = kTripCountInBodyUnsafe; // needs both tests
Aart Bik9401f532015-09-28 16:25:56 -0700888 if (is_taken && is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700889 tcKind = kTripCountInLoop; // needs neither test
Aart Bik9401f532015-09-28 16:25:56 -0700890 } else if (is_finite) {
Aart Bik22f05872015-10-27 15:56:28 -0700891 tcKind = kTripCountInBody; // needs taken-test
Aart Bik9401f532015-09-28 16:25:56 -0700892 } else if (is_taken) {
Aart Bik22f05872015-10-27 15:56:28 -0700893 tcKind = kTripCountInLoopUnsafe; // needs finite-test
Aart Bik9401f532015-09-28 16:25:56 -0700894 }
Aart Bik22f05872015-10-27 15:56:28 -0700895 InductionOp op = kNop;
896 switch (cmp) {
897 case kCondLT: op = kLT; break;
898 case kCondLE: op = kLE; break;
899 case kCondGT: op = kGT; break;
900 case kCondGE: op = kGE; break;
901 default: LOG(FATAL) << "CONDITION UNREACHABLE";
902 }
Aart Bik009cace2016-09-16 10:15:19 -0700903 // Associate trip count with control instruction, rather than the condition (even
904 // though it's its use) since former provides a convenient use-free placeholder.
905 HInstruction* control = loop->GetHeader()->GetLastInstruction();
Aart Bik22f05872015-10-27 15:56:28 -0700906 InductionInfo* taken_test = CreateInvariantOp(op, lower_expr, upper_expr);
Aart Bik009cace2016-09-16 10:15:19 -0700907 DCHECK(control->IsIf());
908 AssignInfo(loop, control, CreateTripCount(tcKind, trip_count, taken_test, type));
Aart Bik9401f532015-09-28 16:25:56 -0700909}
910
911bool HInductionVarAnalysis::IsTaken(InductionInfo* lower_expr,
912 InductionInfo* upper_expr,
913 IfCondition cmp) {
914 int64_t lower_value;
915 int64_t upper_value;
Aart Bik97412c922016-02-19 20:14:38 -0800916 switch (cmp) {
917 case kCondLT:
918 return IsAtMost(lower_expr, &lower_value)
919 && IsAtLeast(upper_expr, &upper_value)
920 && lower_value < upper_value;
921 case kCondLE:
922 return IsAtMost(lower_expr, &lower_value)
923 && IsAtLeast(upper_expr, &upper_value)
924 && lower_value <= upper_value;
925 case kCondGT:
926 return IsAtLeast(lower_expr, &lower_value)
927 && IsAtMost(upper_expr, &upper_value)
928 && lower_value > upper_value;
929 case kCondGE:
930 return IsAtLeast(lower_expr, &lower_value)
931 && IsAtMost(upper_expr, &upper_value)
932 && lower_value >= upper_value;
933 default:
934 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700935 }
936 return false; // not certain, may be untaken
937}
938
939bool HInductionVarAnalysis::IsFinite(InductionInfo* upper_expr,
940 int64_t stride_value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100941 DataType::Type type,
Aart Bik9401f532015-09-28 16:25:56 -0700942 IfCondition cmp) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100943 int64_t min = DataType::MinValueOfIntegralType(type);
944 int64_t max = DataType::MaxValueOfIntegralType(type);
Aart Bik9401f532015-09-28 16:25:56 -0700945 // Some rules under which it is certain at compile-time that the loop is finite.
946 int64_t value;
947 switch (cmp) {
948 case kCondLT:
949 return stride_value == 1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800950 (IsAtMost(upper_expr, &value) && value <= (max - stride_value + 1));
Aart Bik9401f532015-09-28 16:25:56 -0700951 case kCondLE:
Aart Bik97412c922016-02-19 20:14:38 -0800952 return (IsAtMost(upper_expr, &value) && value <= (max - stride_value));
Aart Bik9401f532015-09-28 16:25:56 -0700953 case kCondGT:
954 return stride_value == -1 ||
Aart Bik97412c922016-02-19 20:14:38 -0800955 (IsAtLeast(upper_expr, &value) && value >= (min - stride_value - 1));
Aart Bik9401f532015-09-28 16:25:56 -0700956 case kCondGE:
Aart Bik97412c922016-02-19 20:14:38 -0800957 return (IsAtLeast(upper_expr, &value) && value >= (min - stride_value));
Aart Bike9f37602015-10-09 11:15:55 -0700958 default:
959 LOG(FATAL) << "CONDITION UNREACHABLE";
Aart Bik9401f532015-09-28 16:25:56 -0700960 }
961 return false; // not certain, may be infinite
Aart Bikd14c5952015-09-08 15:25:15 -0700962}
963
Aart Bik0d345cf2016-03-16 10:49:38 -0700964bool HInductionVarAnalysis::FitsNarrowerControl(InductionInfo* lower_expr,
965 InductionInfo* upper_expr,
966 int64_t stride_value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100967 DataType::Type type,
Aart Bik0d345cf2016-03-16 10:49:38 -0700968 IfCondition cmp) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100969 int64_t min = DataType::MinValueOfIntegralType(type);
970 int64_t max = DataType::MaxValueOfIntegralType(type);
Aart Bik0d345cf2016-03-16 10:49:38 -0700971 // Inclusive test need one extra.
972 if (stride_value != 1 && stride_value != -1) {
973 return false; // non-unit stride
974 } else if (cmp == kCondLE) {
975 max--;
976 } else if (cmp == kCondGE) {
977 min++;
978 }
979 // Do both bounds fit the range?
Vladimir Marko0e2f2ff2016-03-22 12:31:54 +0000980 int64_t value = 0;
Aart Bik0d345cf2016-03-16 10:49:38 -0700981 return IsAtLeast(lower_expr, &value) && value >= min &&
982 IsAtMost(lower_expr, &value) && value <= max &&
983 IsAtLeast(upper_expr, &value) && value >= min &&
984 IsAtMost(upper_expr, &value) && value <= max;
985}
986
Aart Bik30efb4e2015-07-30 12:14:31 -0700987void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
988 HInstruction* instruction,
989 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700990 auto it = induction_.find(loop);
991 if (it == induction_.end()) {
992 it = induction_.Put(loop,
993 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100994 std::less<HInstruction*>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +0100995 graph_->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700996 }
997 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700998}
999
Aart Bike609b7c2015-08-27 13:46:58 -07001000HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
1001 HInstruction* instruction) {
1002 auto it = induction_.find(loop);
1003 if (it != induction_.end()) {
1004 auto loop_it = it->second.find(instruction);
1005 if (loop_it != it->second.end()) {
1006 return loop_it->second;
1007 }
Aart Bik30efb4e2015-07-30 12:14:31 -07001008 }
Mingyao Yang4b467ed2015-11-19 17:04:22 -08001009 if (loop->IsDefinedOutOfTheLoop(instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -07001010 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -07001011 AssignInfo(loop, instruction, info);
1012 return info;
1013 }
1014 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -07001015}
1016
Aart Bikd14c5952015-09-08 15:25:15 -07001017HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001018 DataType::Type type) {
Aart Bikc071a012016-12-01 10:22:31 -08001019 HInstruction* constant;
1020 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001021 case DataType::Type::kFloat64: constant = graph_->GetDoubleConstant(value); break;
1022 case DataType::Type::kFloat32: constant = graph_->GetFloatConstant(value); break;
1023 case DataType::Type::kInt64: constant = graph_->GetLongConstant(value); break;
1024 default: constant = graph_->GetIntConstant(value); break;
Aart Bikd14c5952015-09-08 15:25:15 -07001025 }
Aart Bikc071a012016-12-01 10:22:31 -08001026 return CreateInvariantFetch(constant);
Aart Bikd14c5952015-09-08 15:25:15 -07001027}
1028
Aart Bik471a2032015-09-04 18:22:11 -07001029HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
1030 InductionOp op,
1031 InductionInfo* a,
1032 InductionInfo* b) {
1033 // Perform some light-weight simplifications during construction of a new invariant.
1034 // This often safes memory and yields a more concise representation of the induction.
1035 // More exhaustive simplifications are done by later phases once induction nodes are
1036 // translated back into HIR code (e.g. by loop optimizations or BCE).
1037 int64_t value = -1;
Aart Bik97412c922016-02-19 20:14:38 -08001038 if (IsExact(a, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001039 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001040 // Simplify 0 + b = b, 0 ^ b = b, 0 * b = 0.
1041 if (op == kAdd || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001042 return b;
1043 } else if (op == kMul) {
1044 return a;
1045 }
Aart Bikd14c5952015-09-08 15:25:15 -07001046 } else if (op == kMul) {
1047 // Simplify 1 * b = b, -1 * b = -b
1048 if (value == 1) {
1049 return b;
1050 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001051 return CreateSimplifiedInvariant(kNeg, nullptr, b);
Aart Bikd14c5952015-09-08 15:25:15 -07001052 }
Aart Bik471a2032015-09-04 18:22:11 -07001053 }
1054 }
Aart Bik97412c922016-02-19 20:14:38 -08001055 if (IsExact(b, &value)) {
Aart Bik471a2032015-09-04 18:22:11 -07001056 if (value == 0) {
Aart Bik7dc96932016-10-12 10:01:05 -07001057 // Simplify a + 0 = a, a - 0 = a, a ^ 0 = a, a * 0 = 0, -0 = 0.
1058 if (op == kAdd || op == kSub || op == kXor) {
Aart Bik471a2032015-09-04 18:22:11 -07001059 return a;
1060 } else if (op == kMul || op == kNeg) {
1061 return b;
1062 }
Aart Bikd14c5952015-09-08 15:25:15 -07001063 } else if (op == kMul || op == kDiv) {
1064 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
1065 if (value == 1) {
1066 return a;
1067 } else if (value == -1) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001068 return CreateSimplifiedInvariant(kNeg, nullptr, a);
Aart Bikd14c5952015-09-08 15:25:15 -07001069 }
Aart Bik471a2032015-09-04 18:22:11 -07001070 }
1071 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -07001072 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
1073 if (op == kAdd) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001074 return CreateSimplifiedInvariant(kSub, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001075 } else if (op == kSub) {
Aart Bik7d57d7f2015-12-09 14:39:48 -08001076 return CreateSimplifiedInvariant(kAdd, a, b->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -07001077 } else if (op == kNeg) {
1078 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -07001079 }
Aart Bik7d57d7f2015-12-09 14:39:48 -08001080 } else if (b->operation == kSub) {
1081 // Simplify - (a - b) = b - a.
1082 if (op == kNeg) {
1083 return CreateSimplifiedInvariant(kSub, b->op_b, b->op_a);
1084 }
Aart Bik471a2032015-09-04 18:22:11 -07001085 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001086 return new (graph_->GetAllocator()) InductionInfo(
Aart Bike6bd0272016-12-16 13:57:52 -08001087 kInvariant, op, a, b, nullptr, ImplicitConversion(b->type));
Aart Bik471a2032015-09-04 18:22:11 -07001088}
1089
Aart Bikd0a022d2016-12-13 11:22:31 -08001090HInstruction* HInductionVarAnalysis::GetShiftConstant(HLoopInformation* loop,
1091 HInstruction* instruction,
1092 InductionInfo* initial) {
1093 DCHECK(instruction->IsShl() || instruction->IsShr() || instruction->IsUShr());
1094 // Shift-rights are only the same as division for non-negative initial inputs.
1095 // Otherwise we would round incorrectly.
1096 if (initial != nullptr) {
1097 int64_t value = -1;
1098 if (!IsAtLeast(initial, &value) || value < 0) {
1099 return nullptr;
1100 }
1101 }
1102 // Obtain the constant needed to treat shift as equivalent multiplication or division.
1103 // This yields an existing instruction if the constant is already there. Otherwise, this
1104 // has a side effect on the HIR. The restriction on the shift factor avoids generating a
1105 // negative constant (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that
1106 // generalization for shift factors outside [0,32) and [0,64) ranges is done earlier.
Aart Bikc071a012016-12-01 10:22:31 -08001107 InductionInfo* b = LookupInfo(loop, instruction->InputAt(1));
1108 int64_t value = -1;
1109 if (IsExact(b, &value)) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001110 DataType::Type type = instruction->InputAt(0)->GetType();
1111 if (type == DataType::Type::kInt32 && 0 <= value && value < 31) {
Aart Bikc071a012016-12-01 10:22:31 -08001112 return graph_->GetIntConstant(1 << value);
1113 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001114 if (type == DataType::Type::kInt64 && 0 <= value && value < 63) {
Aart Bikc071a012016-12-01 10:22:31 -08001115 return graph_->GetLongConstant(1L << value);
1116 }
1117 }
1118 return nullptr;
1119}
Aart Bikcc42be02016-10-20 16:14:16 -07001120
1121void HInductionVarAnalysis::AssignCycle(HPhi* phi) {
1122 ArenaSet<HInstruction*>* set = &cycles_.Put(phi, ArenaSet<HInstruction*>(
Vladimir Markoca6fff82017-10-03 14:49:14 +01001123 graph_->GetAllocator()->Adapter(kArenaAllocInductionVarAnalysis)))->second;
Aart Bikcc42be02016-10-20 16:14:16 -07001124 for (HInstruction* i : scc_) {
1125 set->insert(i);
1126 }
1127}
1128
1129ArenaSet<HInstruction*>* HInductionVarAnalysis::LookupCycle(HPhi* phi) {
1130 auto it = cycles_.find(phi);
1131 if (it != cycles_.end()) {
1132 return &it->second;
1133 }
1134 return nullptr;
1135}
1136
Aart Bik97412c922016-02-19 20:14:38 -08001137bool HInductionVarAnalysis::IsExact(InductionInfo* info, int64_t* value) {
1138 return InductionVarRange(this).IsConstant(info, InductionVarRange::kExact, value);
1139}
1140
1141bool HInductionVarAnalysis::IsAtMost(InductionInfo* info, int64_t* value) {
1142 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtMost, value);
1143}
1144
1145bool HInductionVarAnalysis::IsAtLeast(InductionInfo* info, int64_t* value) {
1146 return InductionVarRange(this).IsConstant(info, InductionVarRange::kAtLeast, value);
Aart Bik7d57d7f2015-12-09 14:39:48 -08001147}
1148
Aart Bike6bd0272016-12-16 13:57:52 -08001149bool HInductionVarAnalysis::IsNarrowingLinear(InductionInfo* info) {
1150 return info != nullptr &&
1151 info->induction_class == kLinear &&
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001152 (info->type == DataType::Type::kUint8 ||
1153 info->type == DataType::Type::kInt8 ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001154 info->type == DataType::Type::kUint16 ||
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001155 info->type == DataType::Type::kInt16 ||
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001156 (info->type == DataType::Type::kInt32 && (info->op_a->type == DataType::Type::kInt64 ||
1157 info->op_b->type == DataType::Type::kInt64)));
Aart Bike6bd0272016-12-16 13:57:52 -08001158}
1159
Aart Bik30efb4e2015-07-30 12:14:31 -07001160bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
1161 InductionInfo* info2) {
1162 // Test structural equality only, without accounting for simplifications.
1163 if (info1 != nullptr && info2 != nullptr) {
1164 return
1165 info1->induction_class == info2->induction_class &&
1166 info1->operation == info2->operation &&
1167 info1->fetch == info2->fetch &&
Aart Bik78296912016-03-25 13:14:53 -07001168 info1->type == info2->type &&
Aart Bik30efb4e2015-07-30 12:14:31 -07001169 InductionEqual(info1->op_a, info2->op_a) &&
1170 InductionEqual(info1->op_b, info2->op_b);
1171 }
1172 // Otherwise only two nullptrs are considered equal.
1173 return info1 == info2;
1174}
1175
Aart Bikc071a012016-12-01 10:22:31 -08001176std::string HInductionVarAnalysis::FetchToString(HInstruction* fetch) {
1177 DCHECK(fetch != nullptr);
1178 if (fetch->IsIntConstant()) {
1179 return std::to_string(fetch->AsIntConstant()->GetValue());
1180 } else if (fetch->IsLongConstant()) {
1181 return std::to_string(fetch->AsLongConstant()->GetValue());
1182 }
1183 return std::to_string(fetch->GetId()) + ":" + fetch->DebugName();
1184}
1185
Aart Bik30efb4e2015-07-30 12:14:31 -07001186std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
1187 if (info != nullptr) {
1188 if (info->induction_class == kInvariant) {
1189 std::string inv = "(";
1190 inv += InductionToString(info->op_a);
1191 switch (info->operation) {
Aart Bik22f05872015-10-27 15:56:28 -07001192 case kNop: inv += " @ "; break;
1193 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001194 case kSub:
Aart Bik22f05872015-10-27 15:56:28 -07001195 case kNeg: inv += " - "; break;
1196 case kMul: inv += " * "; break;
1197 case kDiv: inv += " / "; break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001198 case kRem: inv += " % "; break;
Aart Bik7dc96932016-10-12 10:01:05 -07001199 case kXor: inv += " ^ "; break;
Aart Bik22f05872015-10-27 15:56:28 -07001200 case kLT: inv += " < "; break;
1201 case kLE: inv += " <= "; break;
1202 case kGT: inv += " > "; break;
1203 case kGE: inv += " >= "; break;
Aart Bikc071a012016-12-01 10:22:31 -08001204 case kFetch: inv += FetchToString(info->fetch); break;
Aart Bik22f05872015-10-27 15:56:28 -07001205 case kTripCountInLoop: inv += " (TC-loop) "; break;
1206 case kTripCountInBody: inv += " (TC-body) "; break;
1207 case kTripCountInLoopUnsafe: inv += " (TC-loop-unsafe) "; break;
1208 case kTripCountInBodyUnsafe: inv += " (TC-body-unsafe) "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -07001209 }
1210 inv += InductionToString(info->op_b);
Aart Bik0d345cf2016-03-16 10:49:38 -07001211 inv += ")";
1212 return inv;
Aart Bik30efb4e2015-07-30 12:14:31 -07001213 } else {
Aart Bik30efb4e2015-07-30 12:14:31 -07001214 if (info->induction_class == kLinear) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001215 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001216 return "(" + InductionToString(info->op_a) + " * i + " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001217 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001218 DataType::PrettyDescriptor(info->type);
Aart Bikc071a012016-12-01 10:22:31 -08001219 } else if (info->induction_class == kPolynomial) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001220 DCHECK(info->operation == kNop);
1221 return "poly(sum_lt(" + InductionToString(info->op_a) + ") + " +
Aart Bikc071a012016-12-01 10:22:31 -08001222 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001223 DataType::PrettyDescriptor(info->type);
Aart Bikc071a012016-12-01 10:22:31 -08001224 } else if (info->induction_class == kGeometric) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001225 DCHECK(info->operation == kMul || info->operation == kDiv);
Aart Bikc071a012016-12-01 10:22:31 -08001226 DCHECK(info->fetch != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001227 return "geo(" + InductionToString(info->op_a) + " * " +
1228 FetchToString(info->fetch) +
1229 (info->operation == kMul ? " ^ i + " : " ^ -i + ") +
1230 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001231 DataType::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001232 } else if (info->induction_class == kWrapAround) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001233 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001234 return "wrap(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001235 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001236 DataType::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001237 } else if (info->induction_class == kPeriodic) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001238 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -07001239 return "periodic(" + InductionToString(info->op_a) + ", " +
Aart Bik0d345cf2016-03-16 10:49:38 -07001240 InductionToString(info->op_b) + "):" +
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001241 DataType::PrettyDescriptor(info->type);
Aart Bik30efb4e2015-07-30 12:14:31 -07001242 }
1243 }
1244 }
1245 return "";
1246}
1247
1248} // namespace art