blob: 52c729d5f5fd4ebba8d8605fb8146244b9eef12b [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/**
23 * Returns true if instruction is invariant within the given loop.
24 */
25static bool IsLoopInvariant(HLoopInformation* loop, HInstruction* instruction) {
26 HLoopInformation* other_loop = instruction->GetBlock()->GetLoopInformation();
27 if (other_loop != loop) {
28 // If instruction does not occur in same loop, it is invariant
29 // if it appears in an outer loop (including no loop at all).
30 return other_loop == nullptr || loop->IsIn(*other_loop);
31 }
32 return false;
33}
34
35/**
Aart Bik22af3be2015-09-10 12:50:58 -070036 * Since graph traversal may enter a SCC at any position, an initial representation may be rotated,
37 * along dependences, viz. any of (a, b, c, d), (d, a, b, c) (c, d, a, b), (b, c, d, a) assuming
38 * a chain of dependences (mutual independent items may occur in arbitrary order). For proper
39 * classification, the lexicographically first entry-phi is rotated to the front.
40 */
41static void RotateEntryPhiFirst(HLoopInformation* loop,
42 ArenaVector<HInstruction*>* scc,
43 ArenaVector<HInstruction*>* new_scc) {
44 // Find very first entry-phi.
45 const HInstructionList& phis = loop->GetHeader()->GetPhis();
46 HInstruction* phi = nullptr;
47 size_t phi_pos = -1;
48 const size_t size = scc->size();
49 for (size_t i = 0; i < size; i++) {
Aart Bikf475bee2015-09-16 12:50:25 -070050 HInstruction* other = scc->at(i);
51 if (other->IsLoopHeaderPhi() && (phi == nullptr || phis.FoundBefore(other, phi))) {
52 phi = other;
Aart Bik22af3be2015-09-10 12:50:58 -070053 phi_pos = i;
54 }
55 }
56
57 // If found, bring that entry-phi to front.
58 if (phi != nullptr) {
59 new_scc->clear();
60 for (size_t i = 0; i < size; i++) {
61 DCHECK_LT(phi_pos, size);
62 new_scc->push_back(scc->at(phi_pos));
63 if (++phi_pos >= size) phi_pos = 0;
64 }
65 DCHECK_EQ(size, new_scc->size());
66 scc->swap(*new_scc);
67 }
68}
69
Aart Bik30efb4e2015-07-30 12:14:31 -070070//
71// Class methods.
72//
73
74HInductionVarAnalysis::HInductionVarAnalysis(HGraph* graph)
75 : HOptimization(graph, kInductionPassName),
76 global_depth_(0),
Vladimir Marko5233f932015-09-29 19:01:15 +010077 stack_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
78 scc_(graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
79 map_(std::less<HInstruction*>(),
80 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
81 cycle_(std::less<HInstruction*>(),
82 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)),
83 induction_(std::less<HLoopInformation*>(),
84 graph->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)) {
Aart Bik30efb4e2015-07-30 12:14:31 -070085}
86
87void HInductionVarAnalysis::Run() {
Aart Bike609b7c2015-08-27 13:46:58 -070088 // Detects sequence variables (generalized induction variables) during an inner-loop-first
89 // traversal of all loops using Gerlek's algorithm. The order is only relevant if outer
90 // loops would use induction information of inner loops (not currently done).
Aart Bik30efb4e2015-07-30 12:14:31 -070091 for (HPostOrderIterator it_graph(*graph_); !it_graph.Done(); it_graph.Advance()) {
92 HBasicBlock* graph_block = it_graph.Current();
93 if (graph_block->IsLoopHeader()) {
94 VisitLoop(graph_block->GetLoopInformation());
95 }
96 }
97}
98
99void HInductionVarAnalysis::VisitLoop(HLoopInformation* loop) {
100 // Find strongly connected components (SSCs) in the SSA graph of this loop using Tarjan's
101 // algorithm. Due to the descendant-first nature, classification happens "on-demand".
102 global_depth_ = 0;
Aart Bike609b7c2015-08-27 13:46:58 -0700103 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700104 map_.clear();
105
106 for (HBlocksInLoopIterator it_loop(*loop); !it_loop.Done(); it_loop.Advance()) {
107 HBasicBlock* loop_block = it_loop.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700108 DCHECK(loop_block->IsInLoop());
Aart Bik30efb4e2015-07-30 12:14:31 -0700109 if (loop_block->GetLoopInformation() != loop) {
110 continue; // Inner loops already visited.
111 }
112 // Visit phi-operations and instructions.
113 for (HInstructionIterator it(loop_block->GetPhis()); !it.Done(); it.Advance()) {
114 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700115 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700116 VisitNode(loop, instruction);
117 }
118 }
119 for (HInstructionIterator it(loop_block->GetInstructions()); !it.Done(); it.Advance()) {
120 HInstruction* instruction = it.Current();
Aart Bike609b7c2015-08-27 13:46:58 -0700121 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700122 VisitNode(loop, instruction);
123 }
124 }
125 }
126
Aart Bike609b7c2015-08-27 13:46:58 -0700127 DCHECK(stack_.empty());
Aart Bik30efb4e2015-07-30 12:14:31 -0700128 map_.clear();
Aart Bikd14c5952015-09-08 15:25:15 -0700129
130 // Determine the loop's trip count.
131 VisitControl(loop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700132}
133
134void HInductionVarAnalysis::VisitNode(HLoopInformation* loop, HInstruction* instruction) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700135 const uint32_t d1 = ++global_depth_;
Aart Bike609b7c2015-08-27 13:46:58 -0700136 map_.Put(instruction, NodeInfo(d1));
Aart Bik30efb4e2015-07-30 12:14:31 -0700137 stack_.push_back(instruction);
138
139 // Visit all descendants.
140 uint32_t low = d1;
141 for (size_t i = 0, count = instruction->InputCount(); i < count; ++i) {
142 low = std::min(low, VisitDescendant(loop, instruction->InputAt(i)));
143 }
144
145 // Lower or found SCC?
146 if (low < d1) {
Aart Bike609b7c2015-08-27 13:46:58 -0700147 map_.find(instruction)->second.depth = low;
Aart Bik30efb4e2015-07-30 12:14:31 -0700148 } else {
149 scc_.clear();
150 cycle_.clear();
151
152 // Pop the stack to build the SCC for classification.
153 while (!stack_.empty()) {
154 HInstruction* x = stack_.back();
155 scc_.push_back(x);
156 stack_.pop_back();
Aart Bike609b7c2015-08-27 13:46:58 -0700157 map_.find(x)->second.done = true;
Aart Bik30efb4e2015-07-30 12:14:31 -0700158 if (x == instruction) {
159 break;
160 }
161 }
162
163 // Classify the SCC.
Aart Bikf475bee2015-09-16 12:50:25 -0700164 if (scc_.size() == 1 && !scc_[0]->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700165 ClassifyTrivial(loop, scc_[0]);
166 } else {
167 ClassifyNonTrivial(loop);
168 }
169
170 scc_.clear();
171 cycle_.clear();
172 }
173}
174
175uint32_t HInductionVarAnalysis::VisitDescendant(HLoopInformation* loop, HInstruction* instruction) {
176 // If the definition is either outside the loop (loop invariant entry value)
177 // or assigned in inner loop (inner exit value), the traversal stops.
178 HLoopInformation* otherLoop = instruction->GetBlock()->GetLoopInformation();
179 if (otherLoop != loop) {
180 return global_depth_;
181 }
182
183 // Inspect descendant node.
Aart Bike609b7c2015-08-27 13:46:58 -0700184 if (!IsVisitedNode(instruction)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700185 VisitNode(loop, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700186 return map_.find(instruction)->second.depth;
Aart Bik30efb4e2015-07-30 12:14:31 -0700187 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700188 auto it = map_.find(instruction);
Aart Bik30efb4e2015-07-30 12:14:31 -0700189 return it->second.done ? global_depth_ : it->second.depth;
190 }
191}
192
193void HInductionVarAnalysis::ClassifyTrivial(HLoopInformation* loop, HInstruction* instruction) {
194 InductionInfo* info = nullptr;
195 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700196 info = TransferPhi(loop, instruction, /* input_index */ 0);
Aart Bik30efb4e2015-07-30 12:14:31 -0700197 } else if (instruction->IsAdd()) {
198 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
199 LookupInfo(loop, instruction->InputAt(1)), kAdd);
200 } else if (instruction->IsSub()) {
201 info = TransferAddSub(LookupInfo(loop, instruction->InputAt(0)),
202 LookupInfo(loop, instruction->InputAt(1)), kSub);
203 } else if (instruction->IsMul()) {
204 info = TransferMul(LookupInfo(loop, instruction->InputAt(0)),
205 LookupInfo(loop, instruction->InputAt(1)));
Aart Bike609b7c2015-08-27 13:46:58 -0700206 } else if (instruction->IsShl()) {
207 info = TransferShl(LookupInfo(loop, instruction->InputAt(0)),
208 LookupInfo(loop, instruction->InputAt(1)),
209 instruction->InputAt(0)->GetType());
Aart Bik30efb4e2015-07-30 12:14:31 -0700210 } else if (instruction->IsNeg()) {
211 info = TransferNeg(LookupInfo(loop, instruction->InputAt(0)));
Aart Bike609b7c2015-08-27 13:46:58 -0700212 } else if (instruction->IsBoundsCheck()) {
213 info = LookupInfo(loop, instruction->InputAt(0)); // Pass-through.
214 } else if (instruction->IsTypeConversion()) {
215 HTypeConversion* conversion = instruction->AsTypeConversion();
216 // TODO: accept different conversion scenarios.
217 if (conversion->GetResultType() == conversion->GetInputType()) {
218 info = LookupInfo(loop, conversion->GetInput());
219 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700220 }
221
222 // Successfully classified?
223 if (info != nullptr) {
224 AssignInfo(loop, instruction, info);
225 }
226}
227
228void HInductionVarAnalysis::ClassifyNonTrivial(HLoopInformation* loop) {
229 const size_t size = scc_.size();
Aart Bike609b7c2015-08-27 13:46:58 -0700230 DCHECK_GE(size, 1u);
Aart Bik22af3be2015-09-10 12:50:58 -0700231
232 // Rotate proper entry-phi to front.
233 if (size > 1) {
Vladimir Marko5233f932015-09-29 19:01:15 +0100234 ArenaVector<HInstruction*> other(graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis));
Aart Bik22af3be2015-09-10 12:50:58 -0700235 RotateEntryPhiFirst(loop, &scc_, &other);
236 }
237
Aart Bikf475bee2015-09-16 12:50:25 -0700238 // Analyze from entry-phi onwards.
Aart Bik22af3be2015-09-10 12:50:58 -0700239 HInstruction* phi = scc_[0];
Aart Bikf475bee2015-09-16 12:50:25 -0700240 if (!phi->IsLoopHeaderPhi()) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700241 return;
242 }
Aart Bikf475bee2015-09-16 12:50:25 -0700243
244 // External link should be loop invariant.
245 InductionInfo* initial = LookupInfo(loop, phi->InputAt(0));
Aart Bik30efb4e2015-07-30 12:14:31 -0700246 if (initial == nullptr || initial->induction_class != kInvariant) {
247 return;
248 }
249
Aart Bikf475bee2015-09-16 12:50:25 -0700250 // Singleton is wrap-around induction if all internal links have the same meaning.
Aart Bik30efb4e2015-07-30 12:14:31 -0700251 if (size == 1) {
Aart Bikf475bee2015-09-16 12:50:25 -0700252 InductionInfo* update = TransferPhi(loop, phi, /* input_index */ 1);
Aart Bik30efb4e2015-07-30 12:14:31 -0700253 if (update != nullptr) {
Aart Bik471a2032015-09-04 18:22:11 -0700254 AssignInfo(loop, phi, CreateInduction(kWrapAround, initial, update));
Aart Bik30efb4e2015-07-30 12:14:31 -0700255 }
256 return;
257 }
258
259 // Inspect remainder of the cycle that resides in scc_. The cycle_ mapping assigns
Aart Bike609b7c2015-08-27 13:46:58 -0700260 // temporary meaning to its nodes, seeded from the phi instruction and back.
Aart Bik22af3be2015-09-10 12:50:58 -0700261 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700262 HInstruction* instruction = scc_[i];
Aart Bik30efb4e2015-07-30 12:14:31 -0700263 InductionInfo* update = nullptr;
Aart Bike609b7c2015-08-27 13:46:58 -0700264 if (instruction->IsPhi()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700265 update = SolvePhiAllInputs(loop, phi, instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700266 } else if (instruction->IsAdd()) {
267 update = SolveAddSub(
268 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kAdd, true);
269 } else if (instruction->IsSub()) {
270 update = SolveAddSub(
271 loop, phi, instruction, instruction->InputAt(0), instruction->InputAt(1), kSub, true);
Aart Bik30efb4e2015-07-30 12:14:31 -0700272 }
273 if (update == nullptr) {
274 return;
275 }
Aart Bike609b7c2015-08-27 13:46:58 -0700276 cycle_.Put(instruction, update);
Aart Bik30efb4e2015-07-30 12:14:31 -0700277 }
278
Aart Bikf475bee2015-09-16 12:50:25 -0700279 // Success if all internal links received the same temporary meaning.
280 InductionInfo* induction = SolvePhi(phi, /* input_index */ 1);
281 if (induction != nullptr) {
Aart Bike609b7c2015-08-27 13:46:58 -0700282 switch (induction->induction_class) {
283 case kInvariant:
Aart Bik22af3be2015-09-10 12:50:58 -0700284 // Classify first phi and then the rest of the cycle "on-demand".
285 // Statements are scanned in order.
Aart Bik471a2032015-09-04 18:22:11 -0700286 AssignInfo(loop, phi, CreateInduction(kLinear, induction, initial));
Aart Bik22af3be2015-09-10 12:50:58 -0700287 for (size_t i = 1; i < size; i++) {
Aart Bike609b7c2015-08-27 13:46:58 -0700288 ClassifyTrivial(loop, scc_[i]);
289 }
290 break;
291 case kPeriodic:
Aart Bik22af3be2015-09-10 12:50:58 -0700292 // Classify all elements in the cycle with the found periodic induction while
293 // rotating each first element to the end. Lastly, phi is classified.
294 // Statements are scanned in reverse order.
295 for (size_t i = size - 1; i >= 1; i--) {
296 AssignInfo(loop, scc_[i], induction);
Aart Bike609b7c2015-08-27 13:46:58 -0700297 induction = RotatePeriodicInduction(induction->op_b, induction->op_a);
298 }
299 AssignInfo(loop, phi, induction);
300 break;
301 default:
302 break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700303 }
304 }
305}
306
Aart Bike609b7c2015-08-27 13:46:58 -0700307HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::RotatePeriodicInduction(
308 InductionInfo* induction,
309 InductionInfo* last) {
310 // Rotates a periodic induction of the form
311 // (a, b, c, d, e)
312 // into
313 // (b, c, d, e, a)
314 // in preparation of assigning this to the previous variable in the sequence.
315 if (induction->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700316 return CreateInduction(kPeriodic, induction, last);
Aart Bike609b7c2015-08-27 13:46:58 -0700317 }
Aart Bik471a2032015-09-04 18:22:11 -0700318 return CreateInduction(kPeriodic, induction->op_a, RotatePeriodicInduction(induction->op_b, last));
Aart Bike609b7c2015-08-27 13:46:58 -0700319}
320
Aart Bikf475bee2015-09-16 12:50:25 -0700321HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferPhi(HLoopInformation* loop,
322 HInstruction* phi,
323 size_t input_index) {
324 // Match all phi inputs from input_index onwards exactly.
325 const size_t count = phi->InputCount();
326 DCHECK_LT(input_index, count);
327 InductionInfo* a = LookupInfo(loop, phi->InputAt(input_index));
328 for (size_t i = input_index + 1; i < count; i++) {
329 InductionInfo* b = LookupInfo(loop, phi->InputAt(i));
330 if (!InductionEqual(a, b)) {
331 return nullptr;
332 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700333 }
Aart Bikf475bee2015-09-16 12:50:25 -0700334 return a;
Aart Bik30efb4e2015-07-30 12:14:31 -0700335}
336
337HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferAddSub(InductionInfo* a,
338 InductionInfo* b,
339 InductionOp op) {
Aart Bike609b7c2015-08-27 13:46:58 -0700340 // Transfer over an addition or subtraction: any invariant, linear, wrap-around, or periodic
341 // can be combined with an invariant to yield a similar result. Even two linear inputs can
342 // be combined. All other combinations fail, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700343 if (a != nullptr && b != nullptr) {
344 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700345 return CreateInvariantOp(op, a, b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700346 } else if (a->induction_class == kLinear && b->induction_class == kLinear) {
Aart Bik471a2032015-09-04 18:22:11 -0700347 return CreateInduction(
Aart Bike609b7c2015-08-27 13:46:58 -0700348 kLinear, TransferAddSub(a->op_a, b->op_a, op), TransferAddSub(a->op_b, b->op_b, op));
349 } else if (a->induction_class == kInvariant) {
350 InductionInfo* new_a = b->op_a;
351 InductionInfo* new_b = TransferAddSub(a, b->op_b, op);
352 if (b->induction_class != kLinear) {
353 DCHECK(b->induction_class == kWrapAround || b->induction_class == kPeriodic);
354 new_a = TransferAddSub(a, new_a, op);
355 } else if (op == kSub) { // Negation required.
356 new_a = TransferNeg(new_a);
357 }
Aart Bik471a2032015-09-04 18:22:11 -0700358 return CreateInduction(b->induction_class, new_a, new_b);
Aart Bike609b7c2015-08-27 13:46:58 -0700359 } else if (b->induction_class == kInvariant) {
360 InductionInfo* new_a = a->op_a;
361 InductionInfo* new_b = TransferAddSub(a->op_b, b, op);
362 if (a->induction_class != kLinear) {
363 DCHECK(a->induction_class == kWrapAround || a->induction_class == kPeriodic);
364 new_a = TransferAddSub(new_a, b, op);
365 }
Aart Bik471a2032015-09-04 18:22:11 -0700366 return CreateInduction(a->induction_class, new_a, new_b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700367 }
368 }
369 return nullptr;
370}
371
372HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferMul(InductionInfo* a,
373 InductionInfo* b) {
Aart Bike609b7c2015-08-27 13:46:58 -0700374 // Transfer over a multiplication: any invariant, linear, wrap-around, or periodic
375 // can be multiplied with an invariant to yield a similar but multiplied result.
376 // Two non-invariant inputs cannot be multiplied, however.
Aart Bik30efb4e2015-07-30 12:14:31 -0700377 if (a != nullptr && b != nullptr) {
378 if (a->induction_class == kInvariant && b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700379 return CreateInvariantOp(kMul, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700380 } else if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700381 return CreateInduction(b->induction_class, TransferMul(a, b->op_a), TransferMul(a, b->op_b));
Aart Bike609b7c2015-08-27 13:46:58 -0700382 } else if (b->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700383 return CreateInduction(a->induction_class, TransferMul(a->op_a, b), TransferMul(a->op_b, b));
Aart Bike609b7c2015-08-27 13:46:58 -0700384 }
385 }
386 return nullptr;
387}
388
389HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferShl(InductionInfo* a,
390 InductionInfo* b,
Aart Bikd14c5952015-09-08 15:25:15 -0700391 Primitive::Type type) {
Aart Bike609b7c2015-08-27 13:46:58 -0700392 // Transfer over a shift left: treat shift by restricted constant as equivalent multiplication.
Aart Bik471a2032015-09-04 18:22:11 -0700393 int64_t value = -1;
394 if (a != nullptr && IsIntAndGet(b, &value)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700395 // Obtain the constant needed for the multiplication. This yields an existing instruction
396 // if the constants is already there. Otherwise, this has a side effect on the HIR.
397 // The restriction on the shift factor avoids generating a negative constant
398 // (viz. 1 << 31 and 1L << 63 set the sign bit). The code assumes that generalization
399 // for shift factors outside [0,32) and [0,64) ranges is done by earlier simplification.
Aart Bikd14c5952015-09-08 15:25:15 -0700400 if ((type == Primitive::kPrimInt && 0 <= value && value < 31) ||
401 (type == Primitive::kPrimLong && 0 <= value && value < 63)) {
402 return TransferMul(a, CreateConstant(1 << value, type));
Aart Bik30efb4e2015-07-30 12:14:31 -0700403 }
404 }
405 return nullptr;
406}
407
408HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::TransferNeg(InductionInfo* a) {
Aart Bike609b7c2015-08-27 13:46:58 -0700409 // Transfer over a unary negation: an invariant, linear, wrap-around, or periodic input
410 // yields a similar but negated induction as result.
Aart Bik30efb4e2015-07-30 12:14:31 -0700411 if (a != nullptr) {
412 if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700413 return CreateInvariantOp(kNeg, nullptr, a);
Aart Bik30efb4e2015-07-30 12:14:31 -0700414 }
Aart Bik471a2032015-09-04 18:22:11 -0700415 return CreateInduction(a->induction_class, TransferNeg(a->op_a), TransferNeg(a->op_b));
Aart Bik30efb4e2015-07-30 12:14:31 -0700416 }
417 return nullptr;
418}
419
Aart Bikf475bee2015-09-16 12:50:25 -0700420HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhi(HInstruction* phi,
421 size_t input_index) {
422 // Match all phi inputs from input_index onwards exactly.
423 const size_t count = phi->InputCount();
424 DCHECK_LT(input_index, count);
425 auto ita = cycle_.find(phi->InputAt(input_index));
Aart Bik30efb4e2015-07-30 12:14:31 -0700426 if (ita != cycle_.end()) {
Aart Bikf475bee2015-09-16 12:50:25 -0700427 for (size_t i = input_index + 1; i < count; i++) {
428 auto itb = cycle_.find(phi->InputAt(i));
429 if (itb == cycle_.end() ||
430 !HInductionVarAnalysis::InductionEqual(ita->second, itb->second)) {
Aart Bik30efb4e2015-07-30 12:14:31 -0700431 return nullptr;
432 }
433 }
Aart Bikf475bee2015-09-16 12:50:25 -0700434 return ita->second;
435 }
436 return nullptr;
437}
438
439HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolvePhiAllInputs(
440 HLoopInformation* loop,
441 HInstruction* entry_phi,
442 HInstruction* phi) {
443 // Match all phi inputs.
444 InductionInfo* match = SolvePhi(phi, /* input_index */ 0);
445 if (match != nullptr) {
446 return match;
Aart Bik30efb4e2015-07-30 12:14:31 -0700447 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700448
Aart Bikf475bee2015-09-16 12:50:25 -0700449 // Otherwise, try to solve for a periodic seeded from phi onward.
450 // Only tight multi-statement cycles are considered in order to
451 // simplify rotating the periodic during the final classification.
452 if (phi->IsLoopHeaderPhi() && phi->InputCount() == 2) {
453 InductionInfo* a = LookupInfo(loop, phi->InputAt(0));
Aart Bike609b7c2015-08-27 13:46:58 -0700454 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700455 if (phi->InputAt(1) == entry_phi) {
456 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik471a2032015-09-04 18:22:11 -0700457 return CreateInduction(kPeriodic, a, initial);
Aart Bike609b7c2015-08-27 13:46:58 -0700458 }
Aart Bikf475bee2015-09-16 12:50:25 -0700459 InductionInfo* b = SolvePhi(phi, /* input_index */ 1);
460 if (b != nullptr && b->induction_class == kPeriodic) {
461 return CreateInduction(kPeriodic, a, b);
Aart Bik30efb4e2015-07-30 12:14:31 -0700462 }
463 }
464 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700465 return nullptr;
466}
467
Aart Bike609b7c2015-08-27 13:46:58 -0700468HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::SolveAddSub(HLoopInformation* loop,
Aart Bikf475bee2015-09-16 12:50:25 -0700469 HInstruction* entry_phi,
Aart Bike609b7c2015-08-27 13:46:58 -0700470 HInstruction* instruction,
471 HInstruction* x,
472 HInstruction* y,
473 InductionOp op,
474 bool is_first_call) {
475 // Solve within a cycle over an addition or subtraction: adding or subtracting an
476 // invariant value, seeded from phi, keeps adding to the stride of the induction.
477 InductionInfo* b = LookupInfo(loop, y);
478 if (b != nullptr && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700479 if (x == entry_phi) {
Aart Bik471a2032015-09-04 18:22:11 -0700480 return (op == kAdd) ? b : CreateInvariantOp(kNeg, nullptr, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700481 }
482 auto it = cycle_.find(x);
483 if (it != cycle_.end()) {
484 InductionInfo* a = it->second;
485 if (a->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700486 return CreateInvariantOp(op, a, b);
Aart Bike609b7c2015-08-27 13:46:58 -0700487 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700488 }
489 }
Aart Bike609b7c2015-08-27 13:46:58 -0700490
491 // Try some alternatives before failing.
492 if (op == kAdd) {
493 // Try the other way around for an addition if considered for first time.
494 if (is_first_call) {
Aart Bikf475bee2015-09-16 12:50:25 -0700495 return SolveAddSub(loop, entry_phi, instruction, y, x, op, false);
Aart Bike609b7c2015-08-27 13:46:58 -0700496 }
497 } else if (op == kSub) {
Aart Bikf475bee2015-09-16 12:50:25 -0700498 // Solve within a tight cycle that is formed by exactly two instructions,
499 // one phi and one update, for a periodic idiom of the form k = c - k;
500 if (y == entry_phi && entry_phi->InputCount() == 2 && instruction == entry_phi->InputAt(1)) {
Aart Bike609b7c2015-08-27 13:46:58 -0700501 InductionInfo* a = LookupInfo(loop, x);
502 if (a != nullptr && a->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700503 InductionInfo* initial = LookupInfo(loop, entry_phi->InputAt(0));
Aart Bik471a2032015-09-04 18:22:11 -0700504 return CreateInduction(kPeriodic, CreateInvariantOp(kSub, a, initial), initial);
Aart Bike609b7c2015-08-27 13:46:58 -0700505 }
506 }
507 }
508
Aart Bik30efb4e2015-07-30 12:14:31 -0700509 return nullptr;
510}
511
Aart Bikd14c5952015-09-08 15:25:15 -0700512void HInductionVarAnalysis::VisitControl(HLoopInformation* loop) {
513 HInstruction* control = loop->GetHeader()->GetLastInstruction();
514 if (control->IsIf()) {
515 HIf* ifs = control->AsIf();
516 HBasicBlock* if_true = ifs->IfTrueSuccessor();
517 HBasicBlock* if_false = ifs->IfFalseSuccessor();
518 HInstruction* if_expr = ifs->InputAt(0);
519 // Determine if loop has following structure in header.
520 // loop-header: ....
521 // if (condition) goto X
522 if (if_expr->IsCondition()) {
523 HCondition* condition = if_expr->AsCondition();
524 InductionInfo* a = LookupInfo(loop, condition->InputAt(0));
525 InductionInfo* b = LookupInfo(loop, condition->InputAt(1));
526 Primitive::Type type = condition->InputAt(0)->GetType();
527 // Determine if the loop control uses integral arithmetic and an if-exit (X outside) or an
528 // if-iterate (X inside), always expressed as if-iterate when passing into VisitCondition().
529 if (type != Primitive::kPrimInt && type != Primitive::kPrimLong) {
530 // Loop control is not 32/64-bit integral.
531 } else if (a == nullptr || b == nullptr) {
532 // Loop control is not a sequence.
533 } else if (if_true->GetLoopInformation() != loop && if_false->GetLoopInformation() == loop) {
534 VisitCondition(loop, a, b, type, condition->GetOppositeCondition());
535 } else if (if_true->GetLoopInformation() == loop && if_false->GetLoopInformation() != loop) {
536 VisitCondition(loop, a, b, type, condition->GetCondition());
537 }
538 }
539 }
540}
541
542void HInductionVarAnalysis::VisitCondition(HLoopInformation* loop,
543 InductionInfo* a,
544 InductionInfo* b,
545 Primitive::Type type,
546 IfCondition cmp) {
547 if (a->induction_class == kInvariant && b->induction_class == kLinear) {
Aart Bikf475bee2015-09-16 12:50:25 -0700548 // 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 -0700549 switch (cmp) {
550 case kCondLT: VisitCondition(loop, b, a, type, kCondGT); break;
551 case kCondLE: VisitCondition(loop, b, a, type, kCondGE); break;
552 case kCondGT: VisitCondition(loop, b, a, type, kCondLT); break;
553 case kCondGE: VisitCondition(loop, b, a, type, kCondLE); break;
Aart Bikf475bee2015-09-16 12:50:25 -0700554 case kCondNE: VisitCondition(loop, b, a, type, kCondNE); break;
Aart Bikd14c5952015-09-08 15:25:15 -0700555 default: break;
556 }
557 } else if (a->induction_class == kLinear && b->induction_class == kInvariant) {
Aart Bikf475bee2015-09-16 12:50:25 -0700558 // Analyze condition with induction at left-hand-side (e.g. i < U).
Aart Bikd14c5952015-09-08 15:25:15 -0700559 InductionInfo* stride = a->op_a;
560 InductionInfo* lo_val = a->op_b;
561 InductionInfo* hi_val = b;
Aart Bikf475bee2015-09-16 12:50:25 -0700562 // Analyze stride (may be compound).
Aart Bikcd26feb2015-09-23 17:50:50 -0700563 InductionVarRange::Value v1 = InductionVarRange::GetVal(stride, nullptr, /* is_min */ true);
564 InductionVarRange::Value v2 = InductionVarRange::GetVal(stride, nullptr, /* is_min */ false);
Aart Bikf475bee2015-09-16 12:50:25 -0700565 if (v1.a_constant != 0 || v2.a_constant != 0 || v1.b_constant != v2.b_constant) {
566 return;
567 }
568 // Rewrite safe condition i != U with unit stride into i < U or i > U
569 // (unit stride guarantees that the end condition is always reached).
570 const int32_t stride_value = v1.b_constant;
571 int64_t lo_value = 0;
572 int64_t hi_value = 0;
573 if (cmp == kCondNE && IsIntAndGet(lo_val, &lo_value) && IsIntAndGet(hi_val, &hi_value)) {
574 if ((stride_value == +1 && lo_value < hi_value) ||
575 (stride_value == -1 && lo_value > hi_value)) {
576 cmp = stride_value > 0 ? kCondLT : kCondGT;
Aart Bikd14c5952015-09-08 15:25:15 -0700577 }
578 }
Aart Bikf475bee2015-09-16 12:50:25 -0700579 // Normalize a linear loop control with a nonzero stride:
580 // stride > 0, either i < U or i <= U
581 // stride < 0, either i > U or i >= U
582 //
583 // TODO: construct conditions for constant/symbolic safety of trip-count
584 //
585 if ((stride_value > 0 && (cmp == kCondLT || cmp == kCondLE)) ||
586 (stride_value < 0 && (cmp == kCondGT || cmp == kCondGE))) {
587 VisitTripCount(loop, lo_val, hi_val, stride, stride_value, type, cmp);
588 }
Aart Bikd14c5952015-09-08 15:25:15 -0700589 }
590}
591
592void HInductionVarAnalysis::VisitTripCount(HLoopInformation* loop,
593 InductionInfo* lo_val,
594 InductionInfo* hi_val,
595 InductionInfo* stride,
596 int32_t stride_value,
597 Primitive::Type type,
Aart Bikf475bee2015-09-16 12:50:25 -0700598 IfCondition cmp) {
Aart Bikd14c5952015-09-08 15:25:15 -0700599 // Any loop of the general form:
600 //
601 // for (i = L; i <= U; i += S) // S > 0
602 // or for (i = L; i >= U; i += S) // S < 0
603 // .. i ..
604 //
605 // can be normalized into:
606 //
607 // for (n = 0; n < TC; n++) // where TC = (U + S - L) / S
608 // .. L + S * n ..
609 //
Aart Bikf475bee2015-09-16 12:50:25 -0700610 // NOTE: The TC (trip-count) expression is only valid when safe. Otherwise TC is 0
611 // (or possibly infinite). Also, the expression assumes the loop does not have
612 // early-exits. Otherwise, TC is an upper bound.
Aart Bikd14c5952015-09-08 15:25:15 -0700613 //
Aart Bikf475bee2015-09-16 12:50:25 -0700614 bool cancels = (cmp == kCondLT || cmp == kCondGT) && std::abs(stride_value) == 1;
Aart Bikd14c5952015-09-08 15:25:15 -0700615 if (!cancels) {
616 // Convert exclusive integral inequality into inclusive integral inequality,
617 // viz. condition i < U is i <= U - 1 and condition i > U is i >= U + 1.
Aart Bikf475bee2015-09-16 12:50:25 -0700618 if (cmp == kCondLT) {
619 hi_val = CreateInvariantOp(kSub, hi_val, CreateConstant(1, type));
620 } else if (cmp == kCondGT) {
621 hi_val = CreateInvariantOp(kAdd, hi_val, CreateConstant(1, type));
Aart Bikd14c5952015-09-08 15:25:15 -0700622 }
623 // Compensate for stride.
624 hi_val = CreateInvariantOp(kAdd, hi_val, stride);
625 }
626
627 // Assign the trip-count expression to the loop control. Clients that use the information
Aart Bikf475bee2015-09-16 12:50:25 -0700628 // should be aware that the expression is only valid in the loop-body proper (when symbolically
629 // safe), and not yet in the loop-header (unless constant safe). If the loop has any early exits,
630 // the trip-count forms a conservative upper bound on the number of loop iterations.
Aart Bikd14c5952015-09-08 15:25:15 -0700631 InductionInfo* trip_count =
632 CreateInvariantOp(kDiv, CreateInvariantOp(kSub, hi_val, lo_val), stride);
633 AssignInfo(loop, loop->GetHeader()->GetLastInstruction(), trip_count);
634}
635
Aart Bik30efb4e2015-07-30 12:14:31 -0700636void HInductionVarAnalysis::AssignInfo(HLoopInformation* loop,
637 HInstruction* instruction,
638 InductionInfo* info) {
Aart Bike609b7c2015-08-27 13:46:58 -0700639 auto it = induction_.find(loop);
640 if (it == induction_.end()) {
641 it = induction_.Put(loop,
642 ArenaSafeMap<HInstruction*, InductionInfo*>(
Vladimir Marko5233f932015-09-29 19:01:15 +0100643 std::less<HInstruction*>(),
644 graph_->GetArena()->Adapter(kArenaAllocInductionVarAnalysis)));
Aart Bike609b7c2015-08-27 13:46:58 -0700645 }
646 it->second.Put(instruction, info);
Aart Bik30efb4e2015-07-30 12:14:31 -0700647}
648
Aart Bike609b7c2015-08-27 13:46:58 -0700649HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::LookupInfo(HLoopInformation* loop,
650 HInstruction* instruction) {
651 auto it = induction_.find(loop);
652 if (it != induction_.end()) {
653 auto loop_it = it->second.find(instruction);
654 if (loop_it != it->second.end()) {
655 return loop_it->second;
656 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700657 }
Aart Bike609b7c2015-08-27 13:46:58 -0700658 if (IsLoopInvariant(loop, instruction)) {
Aart Bik471a2032015-09-04 18:22:11 -0700659 InductionInfo* info = CreateInvariantFetch(instruction);
Aart Bike609b7c2015-08-27 13:46:58 -0700660 AssignInfo(loop, instruction, info);
661 return info;
662 }
663 return nullptr;
Aart Bik30efb4e2015-07-30 12:14:31 -0700664}
665
Aart Bikd14c5952015-09-08 15:25:15 -0700666HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateConstant(int64_t value,
667 Primitive::Type type) {
668 if (type == Primitive::kPrimInt) {
669 return CreateInvariantFetch(graph_->GetIntConstant(value));
670 }
671 DCHECK_EQ(type, Primitive::kPrimLong);
672 return CreateInvariantFetch(graph_->GetLongConstant(value));
673}
674
Aart Bik471a2032015-09-04 18:22:11 -0700675HInductionVarAnalysis::InductionInfo* HInductionVarAnalysis::CreateSimplifiedInvariant(
676 InductionOp op,
677 InductionInfo* a,
678 InductionInfo* b) {
679 // Perform some light-weight simplifications during construction of a new invariant.
680 // This often safes memory and yields a more concise representation of the induction.
681 // More exhaustive simplifications are done by later phases once induction nodes are
682 // translated back into HIR code (e.g. by loop optimizations or BCE).
683 int64_t value = -1;
684 if (IsIntAndGet(a, &value)) {
685 if (value == 0) {
686 // Simplify 0 + b = b, 0 * b = 0.
687 if (op == kAdd) {
688 return b;
689 } else if (op == kMul) {
690 return a;
691 }
Aart Bikd14c5952015-09-08 15:25:15 -0700692 } else if (op == kMul) {
693 // Simplify 1 * b = b, -1 * b = -b
694 if (value == 1) {
695 return b;
696 } else if (value == -1) {
697 op = kNeg;
698 a = nullptr;
699 }
Aart Bik471a2032015-09-04 18:22:11 -0700700 }
701 }
702 if (IsIntAndGet(b, &value)) {
703 if (value == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700704 // Simplify a + 0 = a, a - 0 = a, a * 0 = 0, -0 = 0.
Aart Bik471a2032015-09-04 18:22:11 -0700705 if (op == kAdd || op == kSub) {
706 return a;
707 } else if (op == kMul || op == kNeg) {
708 return b;
709 }
Aart Bikd14c5952015-09-08 15:25:15 -0700710 } else if (op == kMul || op == kDiv) {
711 // Simplify a * 1 = a, a / 1 = a, a * -1 = -a, a / -1 = -a
712 if (value == 1) {
713 return a;
714 } else if (value == -1) {
715 op = kNeg;
716 b = a;
717 a = nullptr;
718 }
Aart Bik471a2032015-09-04 18:22:11 -0700719 }
720 } else if (b->operation == kNeg) {
Aart Bikd14c5952015-09-08 15:25:15 -0700721 // Simplify a + (-b) = a - b, a - (-b) = a + b, -(-b) = b.
722 if (op == kAdd) {
723 op = kSub;
724 b = b->op_b;
725 } else if (op == kSub) {
726 op = kAdd;
727 b = b->op_b;
728 } else if (op == kNeg) {
729 return b->op_b;
Aart Bik471a2032015-09-04 18:22:11 -0700730 }
731 }
732 return new (graph_->GetArena()) InductionInfo(kInvariant, op, a, b, nullptr);
733}
734
Aart Bik30efb4e2015-07-30 12:14:31 -0700735bool HInductionVarAnalysis::InductionEqual(InductionInfo* info1,
736 InductionInfo* info2) {
737 // Test structural equality only, without accounting for simplifications.
738 if (info1 != nullptr && info2 != nullptr) {
739 return
740 info1->induction_class == info2->induction_class &&
741 info1->operation == info2->operation &&
742 info1->fetch == info2->fetch &&
743 InductionEqual(info1->op_a, info2->op_a) &&
744 InductionEqual(info1->op_b, info2->op_b);
745 }
746 // Otherwise only two nullptrs are considered equal.
747 return info1 == info2;
748}
749
Aart Bik471a2032015-09-04 18:22:11 -0700750bool HInductionVarAnalysis::IsIntAndGet(InductionInfo* info, int64_t* value) {
751 if (info != nullptr && info->induction_class == kInvariant && info->operation == kFetch) {
752 DCHECK(info->fetch);
753 if (info->fetch->IsIntConstant()) {
754 *value = info->fetch->AsIntConstant()->GetValue();
755 return true;
756 } else if (info->fetch->IsLongConstant()) {
757 *value = info->fetch->AsLongConstant()->GetValue();
758 return true;
759 }
760 }
761 return false;
762}
763
Aart Bik30efb4e2015-07-30 12:14:31 -0700764std::string HInductionVarAnalysis::InductionToString(InductionInfo* info) {
765 if (info != nullptr) {
766 if (info->induction_class == kInvariant) {
Aart Bik471a2032015-09-04 18:22:11 -0700767 int64_t value = -1;
Aart Bik30efb4e2015-07-30 12:14:31 -0700768 std::string inv = "(";
769 inv += InductionToString(info->op_a);
770 switch (info->operation) {
Aart Bike609b7c2015-08-27 13:46:58 -0700771 case kNop: inv += " @ "; break;
772 case kAdd: inv += " + "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700773 case kSub:
Aart Bike609b7c2015-08-27 13:46:58 -0700774 case kNeg: inv += " - "; break;
775 case kMul: inv += " * "; break;
776 case kDiv: inv += " / "; break;
Aart Bik30efb4e2015-07-30 12:14:31 -0700777 case kFetch:
Aart Bike609b7c2015-08-27 13:46:58 -0700778 DCHECK(info->fetch);
Aart Bik471a2032015-09-04 18:22:11 -0700779 if (IsIntAndGet(info, &value)) {
780 inv += std::to_string(value);
781 } else {
782 inv += std::to_string(info->fetch->GetId()) + ":" + info->fetch->DebugName();
783 }
Aart Bik30efb4e2015-07-30 12:14:31 -0700784 break;
785 }
786 inv += InductionToString(info->op_b);
787 return inv + ")";
788 } else {
Aart Bike609b7c2015-08-27 13:46:58 -0700789 DCHECK(info->operation == kNop);
Aart Bik30efb4e2015-07-30 12:14:31 -0700790 if (info->induction_class == kLinear) {
791 return "(" + InductionToString(info->op_a) + " * i + " +
792 InductionToString(info->op_b) + ")";
793 } else if (info->induction_class == kWrapAround) {
794 return "wrap(" + InductionToString(info->op_a) + ", " +
795 InductionToString(info->op_b) + ")";
796 } else if (info->induction_class == kPeriodic) {
797 return "periodic(" + InductionToString(info->op_a) + ", " +
798 InductionToString(info->op_b) + ")";
799 }
800 }
801 }
802 return "";
803}
804
805} // namespace art