blob: 3dfe17647cd007fc46e29469aab903607d524dda [file] [log] [blame]
Aart Bikd14c5952015-09-08 15:25:15 -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
Aart Bikd14c5952015-09-08 15:25:15 -070017#include "induction_var_range.h"
18
Aart Bikcd26feb2015-09-23 17:50:50 -070019#include <limits>
20
Aart Bikd14c5952015-09-08 15:25:15 -070021namespace art {
22
Aart Bikb3365e02015-09-21 14:45:05 -070023/** Returns true if 64-bit constant fits in 32-bit constant. */
24static bool CanLongValueFitIntoInt(int64_t c) {
Aart Bikcd26feb2015-09-23 17:50:50 -070025 return std::numeric_limits<int32_t>::min() <= c && c <= std::numeric_limits<int32_t>::max();
Aart Bikd14c5952015-09-08 15:25:15 -070026}
27
Aart Bikb3365e02015-09-21 14:45:05 -070028/** Returns true if 32-bit addition can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070029static bool IsSafeAdd(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070030 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) + static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070031}
32
Aart Bikb3365e02015-09-21 14:45:05 -070033/** Returns true if 32-bit subtraction can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070034static bool IsSafeSub(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070035 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) - static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070036}
37
Aart Bikb3365e02015-09-21 14:45:05 -070038/** Returns true if 32-bit multiplication can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070039static bool IsSafeMul(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070040 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) * static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070041}
42
Aart Bikb3365e02015-09-21 14:45:05 -070043/** Returns true if 32-bit division can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070044static bool IsSafeDiv(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070045 return c2 != 0 && CanLongValueFitIntoInt(static_cast<int64_t>(c1) / static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070046}
47
Aart Bik97412c922016-02-19 20:14:38 -080048/** Returns true for 32/64-bit constant instruction. */
49static bool IsIntAndGet(HInstruction* instruction, int64_t* value) {
Aart Bikd14c5952015-09-08 15:25:15 -070050 if (instruction->IsIntConstant()) {
Aart Bikb3365e02015-09-21 14:45:05 -070051 *value = instruction->AsIntConstant()->GetValue();
52 return true;
Aart Bikd14c5952015-09-08 15:25:15 -070053 } else if (instruction->IsLongConstant()) {
Aart Bik97412c922016-02-19 20:14:38 -080054 *value = instruction->AsLongConstant()->GetValue();
55 return true;
Aart Bikd14c5952015-09-08 15:25:15 -070056 }
57 return false;
58}
59
Aart Bikb603a5c2017-03-06 18:29:39 -080060/** Computes a * b for a,b > 0 (at least until first overflow happens). */
61static int64_t SafeMul(int64_t a, int64_t b, /*out*/ bool* overflow) {
62 if (a > 0 && b > 0 && a > (std::numeric_limits<int64_t>::max() / b)) {
63 *overflow = true;
64 }
65 return a * b;
66}
67
68/** Returns b^e for b,e > 0. Sets overflow if arithmetic wrap-around occurred. */
Aart Bikd3ba6262017-01-30 14:37:12 -080069static int64_t IntPow(int64_t b, int64_t e, /*out*/ bool* overflow) {
Aart Bikb603a5c2017-03-06 18:29:39 -080070 DCHECK_LT(0, b);
71 DCHECK_LT(0, e);
Aart Bikc071a012016-12-01 10:22:31 -080072 int64_t pow = 1;
73 while (e) {
74 if (e & 1) {
Aart Bikb603a5c2017-03-06 18:29:39 -080075 pow = SafeMul(pow, b, overflow);
Aart Bikc071a012016-12-01 10:22:31 -080076 }
77 e >>= 1;
Aart Bikb603a5c2017-03-06 18:29:39 -080078 if (e) {
79 b = SafeMul(b, b, overflow);
80 }
Aart Bikc071a012016-12-01 10:22:31 -080081 }
82 return pow;
83}
84
Aart Bikb3365e02015-09-21 14:45:05 -070085/**
Aart Bik40fbf742016-10-31 11:02:50 -070086 * Detects an instruction that is >= 0. As long as the value is carried by
87 * a single instruction, arithmetic wrap-around cannot occur.
Aart Bikb3365e02015-09-21 14:45:05 -070088 */
Aart Bik40fbf742016-10-31 11:02:50 -070089static bool IsGEZero(HInstruction* instruction) {
90 DCHECK(instruction != nullptr);
91 if (instruction->IsArrayLength()) {
92 return true;
93 } else if (instruction->IsInvokeStaticOrDirect()) {
94 switch (instruction->AsInvoke()->GetIntrinsic()) {
95 case Intrinsics::kMathMinIntInt:
96 case Intrinsics::kMathMinLongLong:
97 // Instruction MIN(>=0, >=0) is >= 0.
98 return IsGEZero(instruction->InputAt(0)) &&
99 IsGEZero(instruction->InputAt(1));
100 case Intrinsics::kMathAbsInt:
101 case Intrinsics::kMathAbsLong:
102 // Instruction ABS(x) is >= 0.
103 return true;
104 default:
105 break;
106 }
107 }
108 int64_t value = -1;
109 return IsIntAndGet(instruction, &value) && value >= 0;
110}
111
112/** Hunts "under the hood" for a suitable instruction at the hint. */
113static bool IsMaxAtHint(
114 HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) {
115 if (instruction->IsInvokeStaticOrDirect()) {
116 switch (instruction->AsInvoke()->GetIntrinsic()) {
117 case Intrinsics::kMathMinIntInt:
118 case Intrinsics::kMathMinLongLong:
119 // For MIN(x, y), return most suitable x or y as maximum.
120 return IsMaxAtHint(instruction->InputAt(0), hint, suitable) ||
121 IsMaxAtHint(instruction->InputAt(1), hint, suitable);
122 default:
123 break;
124 }
125 } else {
126 *suitable = instruction;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000127 return HuntForDeclaration(instruction) == hint;
Aart Bik40fbf742016-10-31 11:02:50 -0700128 }
129 return false;
130}
131
132/** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */
133static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) {
134 if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) {
135 // If a == 1, instruction >= 0 and b <= 0, just return the constant b.
136 // No arithmetic wrap-around can occur.
137 if (IsGEZero(v.instruction)) {
138 return InductionVarRange::Value(v.b_constant);
139 }
Aart Bikb3365e02015-09-21 14:45:05 -0700140 }
141 return v;
142}
143
Aart Bik40fbf742016-10-31 11:02:50 -0700144/** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */
145static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) {
146 if (v.is_known && v.a_constant >= 1) {
147 // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as
148 // length + b because length >= 0 is true.
149 int64_t value;
150 if (v.instruction->IsDiv() &&
151 v.instruction->InputAt(0)->IsArrayLength() &&
152 IsIntAndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
153 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
154 }
155 // If a == 1, the most suitable one suffices as maximum value.
156 HInstruction* suitable = nullptr;
157 if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) {
158 return InductionVarRange::Value(suitable, 1, v.b_constant);
159 }
160 }
161 return v;
162}
163
164/** Tests for a constant value. */
Aart Bik52be7e72016-06-23 11:20:41 -0700165static bool IsConstantValue(InductionVarRange::Value v) {
166 return v.is_known && v.a_constant == 0;
167}
168
169/** Corrects a value for type to account for arithmetic wrap-around in lower precision. */
Aart Bik0d345cf2016-03-16 10:49:38 -0700170static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) {
171 switch (type) {
172 case Primitive::kPrimShort:
173 case Primitive::kPrimChar:
174 case Primitive::kPrimByte: {
175 // Constants within range only.
176 // TODO: maybe some room for improvement, like allowing widening conversions
Aart Bike6bd0272016-12-16 13:57:52 -0800177 int32_t min = Primitive::MinValueOfIntegralType(type);
178 int32_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik52be7e72016-06-23 11:20:41 -0700179 return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max)
Aart Bik0d345cf2016-03-16 10:49:38 -0700180 ? v
181 : InductionVarRange::Value();
182 }
183 default:
Aart Bik0d345cf2016-03-16 10:49:38 -0700184 return v;
185 }
186}
187
Aart Bik40fbf742016-10-31 11:02:50 -0700188/** Inserts an instruction. */
Aart Bik389b3db2015-10-28 14:23:40 -0700189static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
190 DCHECK(block != nullptr);
191 DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId();
Aart Bikaec3cce2015-10-14 17:44:55 -0700192 DCHECK(instruction != nullptr);
Aart Bik389b3db2015-10-28 14:23:40 -0700193 block->InsertInstructionBefore(instruction, block->GetLastInstruction());
Aart Bikaec3cce2015-10-14 17:44:55 -0700194 return instruction;
195}
196
Aart Bik40fbf742016-10-31 11:02:50 -0700197/** Obtains loop's control instruction. */
Aart Bik009cace2016-09-16 10:15:19 -0700198static HInstruction* GetLoopControl(HLoopInformation* loop) {
199 DCHECK(loop != nullptr);
200 return loop->GetHeader()->GetLastInstruction();
201}
202
Aart Bikd14c5952015-09-08 15:25:15 -0700203//
204// Public class methods.
205//
206
207InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis)
Aart Bik52be7e72016-06-23 11:20:41 -0700208 : induction_analysis_(induction_analysis),
209 chase_hint_(nullptr) {
Aart Bikb3365e02015-09-21 14:45:05 -0700210 DCHECK(induction_analysis != nullptr);
Aart Bikd14c5952015-09-08 15:25:15 -0700211}
212
Aart Bik1fc3afb2016-02-02 13:26:16 -0800213bool InductionVarRange::GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -0700214 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -0700215 HInstruction* chase_hint,
Aart Bik389b3db2015-10-28 14:23:40 -0700216 /*out*/Value* min_val,
217 /*out*/Value* max_val,
218 /*out*/bool* needs_finite_test) {
Aart Bik52be7e72016-06-23 11:20:41 -0700219 HLoopInformation* loop = nullptr;
220 HInductionVarAnalysis::InductionInfo* info = nullptr;
221 HInductionVarAnalysis::InductionInfo* trip = nullptr;
222 if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) {
223 return false;
Aart Bik97412c922016-02-19 20:14:38 -0800224 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700225 // Type int or lower (this is not too restrictive since intended clients, like
226 // bounds check elimination, will have truncated higher precision induction
227 // at their use point already).
228 switch (info->type) {
229 case Primitive::kPrimInt:
230 case Primitive::kPrimShort:
231 case Primitive::kPrimChar:
232 case Primitive::kPrimByte:
233 break;
234 default:
235 return false;
236 }
Aart Bik97412c922016-02-19 20:14:38 -0800237 // Find range.
Aart Bik52be7e72016-06-23 11:20:41 -0700238 chase_hint_ = chase_hint;
239 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700240 int64_t stride_value = 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700241 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
242 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint);
Aart Bik16d3a652016-09-09 10:33:50 -0700243 *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip);
Aart Bik40fbf742016-10-31 11:02:50 -0700244 chase_hint_ = nullptr;
245 // Retry chasing constants for wrap-around (merge sensitive).
246 if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) {
247 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
248 }
Aart Bik97412c922016-02-19 20:14:38 -0800249 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700250}
251
Aart Bik16d3a652016-09-09 10:33:50 -0700252bool InductionVarRange::CanGenerateRange(HInstruction* context,
253 HInstruction* instruction,
254 /*out*/bool* needs_finite_test,
255 /*out*/bool* needs_taken_test) {
256 bool is_last_value = false;
257 int64_t stride_value = 0;
Aart Bik9abf8942016-10-14 09:49:42 -0700258 return GenerateRangeOrLastValue(context,
259 instruction,
260 is_last_value,
261 nullptr,
262 nullptr,
263 nullptr,
264 nullptr,
265 nullptr, // nothing generated yet
266 &stride_value,
267 needs_finite_test,
268 needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700269 && (stride_value == -1 ||
270 stride_value == 0 ||
Aart Bik40fbf742016-10-31 11:02:50 -0700271 stride_value == 1); // avoid arithmetic wrap-around anomalies.
Aart Bikaec3cce2015-10-14 17:44:55 -0700272}
273
Aart Bik16d3a652016-09-09 10:33:50 -0700274void InductionVarRange::GenerateRange(HInstruction* context,
275 HInstruction* instruction,
276 HGraph* graph,
277 HBasicBlock* block,
278 /*out*/HInstruction** lower,
279 /*out*/HInstruction** upper) {
280 bool is_last_value = false;
Aart Bik009cace2016-09-16 10:15:19 -0700281 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700282 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700283 if (!GenerateRangeOrLastValue(context,
284 instruction,
285 is_last_value,
286 graph,
287 block,
288 lower,
289 upper,
290 nullptr,
291 &stride_value,
292 &b1,
293 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700294 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
Aart Bik389b3db2015-10-28 14:23:40 -0700295 }
296}
297
Aart Bik16d3a652016-09-09 10:33:50 -0700298HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context,
299 HGraph* graph,
300 HBasicBlock* block) {
301 HInstruction* taken_test = nullptr;
302 bool is_last_value = false;
303 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700304 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700305 if (!GenerateRangeOrLastValue(context,
306 context,
307 is_last_value,
308 graph,
309 block,
310 nullptr,
311 nullptr,
312 &taken_test,
313 &stride_value,
314 &b1,
315 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700316 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
317 }
318 return taken_test;
319}
320
321bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) {
322 bool is_last_value = true;
323 int64_t stride_value = 0;
324 bool needs_finite_test = false;
325 bool needs_taken_test = false;
Aart Bik9abf8942016-10-14 09:49:42 -0700326 return GenerateRangeOrLastValue(instruction,
327 instruction,
328 is_last_value,
329 nullptr,
330 nullptr,
331 nullptr,
332 nullptr,
333 nullptr, // nothing generated yet
334 &stride_value,
335 &needs_finite_test,
336 &needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700337 && !needs_finite_test && !needs_taken_test;
338}
339
340HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction,
341 HGraph* graph,
342 HBasicBlock* block) {
343 HInstruction* last_value = nullptr;
344 bool is_last_value = true;
345 int64_t stride_value = 0;
346 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700347 if (!GenerateRangeOrLastValue(instruction,
348 instruction,
349 is_last_value,
350 graph,
351 block,
352 &last_value,
353 &last_value,
354 nullptr,
355 &stride_value,
356 &b1,
357 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700358 LOG(FATAL) << "Failed precondition: CanGenerateLastValue()";
359 }
360 return last_value;
361}
362
363void InductionVarRange::Replace(HInstruction* instruction,
364 HInstruction* fetch,
365 HInstruction* replacement) {
366 for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
367 lp != nullptr;
368 lp = lp->GetPreHeader()->GetLoopInformation()) {
Aart Bik009cace2016-09-16 10:15:19 -0700369 // Update instruction's information.
Aart Bik16d3a652016-09-09 10:33:50 -0700370 ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement);
Aart Bik009cace2016-09-16 10:15:19 -0700371 // Update loop's trip-count information.
372 ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement);
Aart Bik389b3db2015-10-28 14:23:40 -0700373 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700374}
375
Aart Bik6b69e0a2017-01-11 10:20:43 -0800376bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const {
Aart Bik9abf8942016-10-14 09:49:42 -0700377 HInductionVarAnalysis::InductionInfo *trip =
378 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
Aart Bik6b69e0a2017-01-11 10:20:43 -0800379 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
380 IsConstant(trip->op_a, kExact, tc);
381 return true;
382 }
383 return false;
Aart Bik9abf8942016-10-14 09:49:42 -0700384}
385
Aart Bik8e02e3e2017-02-28 14:41:55 -0800386bool InductionVarRange::IsUnitStride(HInstruction* instruction,
387 /*out*/ HInstruction** offset) const {
388 HLoopInformation* loop = nullptr;
389 HInductionVarAnalysis::InductionInfo* info = nullptr;
390 HInductionVarAnalysis::InductionInfo* trip = nullptr;
391 if (HasInductionInfo(instruction, instruction, &loop, &info, &trip)) {
392 if (info->induction_class == HInductionVarAnalysis::kLinear &&
393 info->op_b->operation == HInductionVarAnalysis::kFetch) {
394 int64_t stride_value = 0;
395 if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) {
396 int64_t off_value = 0;
397 if (IsConstant(info->op_b, kExact, &off_value) && off_value == 0) {
398 *offset = nullptr;
399 } else {
400 *offset = info->op_b->fetch;
401 }
402 return true;
403 }
404 }
405 }
406 return false;
407}
408
409HInstruction* InductionVarRange::GenerateTripCount(HLoopInformation* loop,
410 HGraph* graph,
411 HBasicBlock* block) {
412 HInductionVarAnalysis::InductionInfo *trip =
413 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
414 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
415 HInstruction* taken_test = nullptr;
416 HInstruction* trip_expr = nullptr;
417 if (IsBodyTripCount(trip)) {
418 if (!GenerateCode(trip->op_b, nullptr, graph, block, &taken_test, false, false)) {
419 return nullptr;
420 }
421 }
422 if (GenerateCode(trip->op_a, nullptr, graph, block, &trip_expr, false, false)) {
423 if (taken_test != nullptr) {
424 HInstruction* zero = graph->GetConstant(trip->type, 0);
425 trip_expr = Insert(block, new (graph->GetArena()) HSelect(taken_test, trip_expr, zero, kNoDexPc));
426 }
427 return trip_expr;
428 }
429 }
430 return nullptr;
431}
432
Aart Bikd14c5952015-09-08 15:25:15 -0700433//
434// Private class methods.
435//
436
Aart Bik97412c922016-02-19 20:14:38 -0800437bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
438 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700439 /*out*/ int64_t* value) const {
Aart Bik97412c922016-02-19 20:14:38 -0800440 if (info != nullptr) {
441 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
442 // any of the three requests (kExact, kAtMost, and KAtLeast).
443 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
444 info->operation == HInductionVarAnalysis::kFetch) {
445 if (IsIntAndGet(info->fetch, value)) {
446 return true;
447 }
448 }
Aart Bik40fbf742016-10-31 11:02:50 -0700449 // Try range analysis on the invariant, only accept a proper range
450 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700451 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
452 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
453 if (IsConstantValue(min_val) &&
454 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
455 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
456 *value = max_val.b_constant;
457 return true;
458 } else if (request == kAtLeast) {
459 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800460 return true;
461 }
462 }
Aart Bik97412c922016-02-19 20:14:38 -0800463 }
464 return false;
465}
466
Aart Bik52be7e72016-06-23 11:20:41 -0700467bool InductionVarRange::HasInductionInfo(
468 HInstruction* context,
469 HInstruction* instruction,
470 /*out*/ HLoopInformation** loop,
471 /*out*/ HInductionVarAnalysis::InductionInfo** info,
472 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800473 DCHECK(context != nullptr);
474 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700475 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
476 if (lp != nullptr) {
477 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700478 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700479 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700480 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700481 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700482 return true;
483 }
484 }
485 return false;
486}
487
488bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
489 if (trip != nullptr) {
490 // Both bounds that define a trip-count are well-behaved if they either are not defined
491 // in any loop, or are contained in a proper interval. This allows finding the min/max
492 // of an expression by chasing outward.
493 InductionVarRange range(induction_analysis_);
494 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
495 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
496 int64_t not_used = 0;
497 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
498 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
499 }
500 return true;
501}
502
503bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
504 if (info != nullptr) {
505 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
506 info->operation == HInductionVarAnalysis::kFetch) {
507 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
508 }
509 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
510 }
511 return false;
512}
513
Aart Bik16d3a652016-09-09 10:33:50 -0700514bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
515 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700516 if (info != nullptr) {
517 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700518 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800519 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
520 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700521 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700522 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700523 }
Aart Bikd14c5952015-09-08 15:25:15 -0700524 }
Aart Bik389b3db2015-10-28 14:23:40 -0700525 return false;
526}
527
Aart Bik7d57d7f2015-12-09 14:39:48 -0800528bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700529 if (trip != nullptr) {
530 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
531 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
532 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
533 }
534 }
535 return false;
536}
537
Aart Bik7d57d7f2015-12-09 14:39:48 -0800538bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700539 if (trip != nullptr) {
540 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
541 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
542 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
543 }
544 }
545 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700546}
547
Aart Bik7d57d7f2015-12-09 14:39:48 -0800548InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
549 HInductionVarAnalysis::InductionInfo* trip,
550 bool in_body,
551 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800552 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800553 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700554 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800555 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
556 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
557 // with intermediate results that only incorporate single instructions.
558 if (trip != nullptr) {
559 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700560 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800561 int64_t stride_value = 0;
562 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800563 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800564 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800565 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
566 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
567 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700568 trip->induction_class,
569 trip->operation,
570 trip_expr->op_a,
571 trip->op_b,
572 nullptr,
573 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800574 return GetVal(&cancelled_trip, trip, in_body, is_min);
575 }
576 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800577 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800578 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
579 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
580 HInductionVarAnalysis::InductionInfo neg(
581 HInductionVarAnalysis::kInvariant,
582 HInductionVarAnalysis::kNeg,
583 nullptr,
584 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700585 nullptr,
586 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800587 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700588 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800589 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
590 }
591 }
592 }
593 }
594 }
595 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
596 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
597 GetVal(info->op_b, trip, in_body, is_min));
598}
599
Aart Bikdf7822e2016-12-06 10:05:30 -0800600InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
601 HInductionVarAnalysis::InductionInfo* trip,
602 bool in_body,
603 bool is_min) const {
604 DCHECK(info != nullptr);
605 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
606 int64_t a = 0;
607 int64_t b = 0;
608 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
609 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800610 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800611 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
612 Value c = GetVal(info->op_b, trip, in_body, is_min);
613 if (is_min) {
614 return c;
615 } else {
616 Value m = GetVal(trip, trip, in_body, is_min);
617 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
618 Value x = MulValue(Value(a), t);
619 Value y = MulValue(Value(b), m);
620 return AddValue(AddValue(x, y), c);
621 }
622 }
623 return Value();
624}
625
Aart Bikc071a012016-12-01 10:22:31 -0800626InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
627 HInductionVarAnalysis::InductionInfo* trip,
628 bool in_body,
629 bool is_min) const {
630 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800631 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800632 int64_t a = 0;
633 int64_t f = 0;
634 if (IsConstant(info->op_a, kExact, &a) &&
635 CanLongValueFitIntoInt(a) &&
Aart Bikdf7822e2016-12-06 10:05:30 -0800636 IsIntAndGet(info->fetch, &f) && f >= 1) {
637 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
638 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800639 const bool is_min_a = a >= 0 ? is_min : !is_min;
640 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800641 Value b = GetVal(info->op_b, trip, in_body, is_min);
642 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800643 }
644 }
645 return Value();
646}
647
Aart Bikd14c5952015-09-08 15:25:15 -0700648InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700649 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700650 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800651 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700652 // Special case when chasing constants: single instruction that denotes trip count in the
653 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
654 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700655 if (is_min) {
656 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800657 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700658 return Value(std::numeric_limits<int32_t>::max());
659 }
660 }
Aart Bik40fbf742016-10-31 11:02:50 -0700661 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
662 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
663 int64_t value;
664 if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
665 // Proper constant reveals best information.
666 return Value(static_cast<int32_t>(value));
667 } else if (instruction == chase_hint_) {
668 // At hint, fetch is represented by itself.
669 return Value(instruction, 1, 0);
670 } else if (instruction->IsAdd()) {
671 // Incorporate suitable constants in the chased value.
Aart Bik97412c922016-02-19 20:14:38 -0800672 if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
673 return AddValue(Value(static_cast<int32_t>(value)),
674 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
675 } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
676 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
677 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700678 }
Aart Bik52be7e72016-06-23 11:20:41 -0700679 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700680 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700681 if (chase_hint_ == nullptr) {
682 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
683 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000684 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700685 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700686 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700687 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800688 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Aart Bik0d345cf2016-03-16 10:49:38 -0700689 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
690 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
691 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
692 }
Aart Bik52be7e72016-06-23 11:20:41 -0700693 }
694 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
695 // so far is well-behaved in both bounds and the next trip-count is safe.
696 // Example:
697 // for (int i = 0; i <= 100; i++) // safe
698 // for (int j = 0; j <= i; j++) // well-behaved
699 // j is in range [0, i ] (if i is chase hint)
700 // or in range [0, 100] (otherwise)
701 HLoopInformation* next_loop = nullptr;
702 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
703 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
704 bool next_in_body = true; // inner loop is always in body of outer loop
705 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
706 IsWellBehavedTripCount(trip) &&
707 !IsUnsafeTripCount(next_trip)) {
708 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700709 }
Aart Bik40fbf742016-10-31 11:02:50 -0700710 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700711 return Value(instruction, 1, 0);
712}
713
Aart Bikcd26feb2015-09-23 17:50:50 -0700714InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
715 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700716 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800717 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700718 if (info != nullptr) {
719 switch (info->induction_class) {
720 case HInductionVarAnalysis::kInvariant:
721 // Invariants.
722 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700723 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700724 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
725 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700726 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700727 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
728 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700729 case HInductionVarAnalysis::kNeg: // second reversed!
730 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700731 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700732 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700733 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700734 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700735 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800736 case HInductionVarAnalysis::kRem:
737 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700738 case HInductionVarAnalysis::kXor:
739 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700740 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700741 return GetFetch(info->fetch, trip, in_body, is_min);
742 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700743 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700744 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700745 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700746 }
747 FALLTHROUGH_INTENDED;
748 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700749 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700750 if (is_min) {
751 return Value(0);
752 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700753 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700754 }
755 break;
756 default:
757 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700758 }
759 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700760 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700761 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800762 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800763 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800764 case HInductionVarAnalysis::kGeometric:
765 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700766 case HInductionVarAnalysis::kWrapAround:
767 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700768 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
769 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700770 }
771 }
Aart Bikb3365e02015-09-21 14:45:05 -0700772 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700773}
774
775InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
776 HInductionVarAnalysis::InductionInfo* info2,
777 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700778 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800779 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700780 // Constant times range.
781 int64_t value = 0;
782 if (IsConstant(info1, kExact, &value)) {
783 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
784 } else if (IsConstant(info2, kExact, &value)) {
785 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
786 }
787 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700788 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
789 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
790 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
791 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800792 // Positive range vs. positive or negative range.
793 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
794 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
795 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
796 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
797 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700798 }
Aart Bik97412c922016-02-19 20:14:38 -0800799 }
800 // Negative range vs. positive or negative range.
801 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
802 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
803 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
804 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
805 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700806 }
807 }
Aart Bikb3365e02015-09-21 14:45:05 -0700808 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700809}
810
811InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
812 HInductionVarAnalysis::InductionInfo* info2,
813 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700814 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800815 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700816 // Range divided by constant.
817 int64_t value = 0;
818 if (IsConstant(info2, kExact, &value)) {
819 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
820 }
821 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700822 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
823 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
824 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
825 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800826 // Positive range vs. positive or negative range.
827 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
828 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
829 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
830 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
831 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700832 }
Aart Bik97412c922016-02-19 20:14:38 -0800833 }
834 // Negative range vs. positive or negative range.
835 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
836 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
837 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
838 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
839 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700840 }
841 }
Aart Bikb3365e02015-09-21 14:45:05 -0700842 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700843}
844
Aart Bikdf7822e2016-12-06 10:05:30 -0800845InductionVarRange::Value InductionVarRange::GetRem(
846 HInductionVarAnalysis::InductionInfo* info1,
847 HInductionVarAnalysis::InductionInfo* info2) const {
848 int64_t v1 = 0;
849 int64_t v2 = 0;
850 // Only accept exact values.
851 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
852 int64_t value = v1 % v2;
853 if (CanLongValueFitIntoInt(value)) {
854 return Value(static_cast<int32_t>(value));
855 }
856 }
857 return Value();
858}
859
Aart Bik7dc96932016-10-12 10:01:05 -0700860InductionVarRange::Value InductionVarRange::GetXor(
861 HInductionVarAnalysis::InductionInfo* info1,
862 HInductionVarAnalysis::InductionInfo* info2) const {
863 int64_t v1 = 0;
864 int64_t v2 = 0;
865 // Only accept exact values.
866 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
867 int64_t value = v1 ^ v2;
868 if (CanLongValueFitIntoInt(value)) {
869 return Value(static_cast<int32_t>(value));
870 }
871 }
872 return Value();
873}
874
Aart Bik52be7e72016-06-23 11:20:41 -0700875InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
876 int64_t value,
877 HInductionVarAnalysis::InductionInfo* info,
878 HInductionVarAnalysis::InductionInfo* trip,
879 bool in_body,
880 bool is_min) const {
881 if (CanLongValueFitIntoInt(value)) {
882 Value c(static_cast<int32_t>(value));
883 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
884 }
885 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800886}
887
Aart Bik52be7e72016-06-23 11:20:41 -0700888InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
889 int64_t value,
890 HInductionVarAnalysis::InductionInfo* info,
891 HInductionVarAnalysis::InductionInfo* trip,
892 bool in_body,
893 bool is_min) const {
894 if (CanLongValueFitIntoInt(value)) {
895 Value c(static_cast<int32_t>(value));
896 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
897 }
898 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700899}
900
Aart Bik7d57d7f2015-12-09 14:39:48 -0800901InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700902 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800903 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700904 if (v1.a_constant == 0) {
905 return Value(v2.instruction, v2.a_constant, b);
906 } else if (v2.a_constant == 0) {
907 return Value(v1.instruction, v1.a_constant, b);
908 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
909 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
910 }
911 }
Aart Bikb3365e02015-09-21 14:45:05 -0700912 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700913}
914
Aart Bik7d57d7f2015-12-09 14:39:48 -0800915InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700916 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800917 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700918 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
919 return Value(v2.instruction, -v2.a_constant, b);
920 } else if (v2.a_constant == 0) {
921 return Value(v1.instruction, v1.a_constant, b);
922 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
923 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
924 }
925 }
Aart Bikb3365e02015-09-21 14:45:05 -0700926 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700927}
928
Aart Bik7d57d7f2015-12-09 14:39:48 -0800929InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700930 if (v1.is_known && v2.is_known) {
931 if (v1.a_constant == 0) {
932 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
933 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
934 }
935 } else if (v2.a_constant == 0) {
936 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
937 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
938 }
Aart Bikd14c5952015-09-08 15:25:15 -0700939 }
940 }
Aart Bikb3365e02015-09-21 14:45:05 -0700941 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700942}
943
Aart Bik7d57d7f2015-12-09 14:39:48 -0800944InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700945 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700946 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
947 return Value(v1.b_constant / v2.b_constant);
948 }
949 }
Aart Bikb3365e02015-09-21 14:45:05 -0700950 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700951}
952
Aart Bik7d57d7f2015-12-09 14:39:48 -0800953InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700954 if (v1.is_known && v2.is_known) {
955 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700956 return Value(v1.instruction, v1.a_constant,
957 is_min ? std::min(v1.b_constant, v2.b_constant)
958 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700959 }
Aart Bikd14c5952015-09-08 15:25:15 -0700960 }
Aart Bikb3365e02015-09-21 14:45:05 -0700961 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700962}
963
Aart Bik9abf8942016-10-14 09:49:42 -0700964bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
965 HInstruction* instruction,
966 bool is_last_value,
967 HGraph* graph,
968 HBasicBlock* block,
969 /*out*/HInstruction** lower,
970 /*out*/HInstruction** upper,
971 /*out*/HInstruction** taken_test,
972 /*out*/int64_t* stride_value,
973 /*out*/bool* needs_finite_test,
974 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700975 HLoopInformation* loop = nullptr;
976 HInductionVarAnalysis::InductionInfo* info = nullptr;
977 HInductionVarAnalysis::InductionInfo* trip = nullptr;
978 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
979 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800980 }
981 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
982 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
983 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
984 // code does not use the trip-count explicitly (since there could be an implicit relation
985 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700986 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700987 *stride_value = 0;
988 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800989 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700990 // Handle last value request.
991 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800992 DCHECK(!in_body);
993 switch (info->induction_class) {
994 case HInductionVarAnalysis::kLinear:
995 if (*stride_value > 0) {
996 lower = nullptr;
997 } else {
998 upper = nullptr;
999 }
1000 break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001001 case HInductionVarAnalysis::kPolynomial:
1002 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001003 case HInductionVarAnalysis::kGeometric:
1004 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -08001005 case HInductionVarAnalysis::kWrapAround:
1006 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001007 case HInductionVarAnalysis::kPeriodic:
1008 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1009 default:
1010 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001011 }
1012 }
Aart Bik97412c922016-02-19 20:14:38 -08001013 // Code generation for taken test: generate the code when requested or otherwise analyze
1014 // if code generation is feasible when taken test is needed.
1015 if (taken_test != nullptr) {
1016 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1017 } else if (*needs_taken_test) {
1018 if (!GenerateCode(
1019 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1020 return false;
1021 }
1022 }
1023 // Code generation for lower and upper.
1024 return
1025 // Success on lower if invariant (not set), or code can be generated.
1026 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1027 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1028 // And success on upper.
1029 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001030}
1031
Aart Bikdf7822e2016-12-06 10:05:30 -08001032bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1033 HInductionVarAnalysis::InductionInfo* trip,
1034 HGraph* graph,
1035 HBasicBlock* block,
1036 /*out*/HInstruction** result) const {
1037 DCHECK(info != nullptr);
1038 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1039 // Detect known coefficients and trip count (always taken).
1040 int64_t a = 0;
1041 int64_t b = 0;
1042 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001043 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1044 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001045 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001046 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001047 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001048 HInstruction* c = nullptr;
1049 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001050 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001051 Primitive::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001052 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Aart Bike6bd0272016-12-16 13:57:52 -08001053 if (type != Primitive::kPrimLong) {
1054 sum = static_cast<int32_t>(sum); // okay to truncate
1055 }
1056 *result =
1057 Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001058 }
1059 return true;
1060 }
1061 }
1062 return false;
1063}
1064
Aart Bikc071a012016-12-01 10:22:31 -08001065bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1066 HInductionVarAnalysis::InductionInfo* trip,
1067 HGraph* graph,
1068 HBasicBlock* block,
1069 /*out*/HInstruction** result) const {
1070 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001071 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001072 // Detect known base and trip count (always taken).
1073 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001074 int64_t m = 0;
1075 if (IsIntAndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001076 HInstruction* opa = nullptr;
1077 HInstruction* opb = nullptr;
1078 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1079 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001080 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001081 Primitive::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001082 // Compute f ^ m for known maximum index value m.
1083 bool overflow = false;
1084 int64_t fpow = IntPow(f, m, &overflow);
1085 if (info->operation == HInductionVarAnalysis::kDiv) {
1086 // For division, any overflow truncates to zero.
1087 if (overflow || (type != Primitive::kPrimLong && !CanLongValueFitIntoInt(fpow))) {
1088 fpow = 0;
1089 }
1090 } else if (type != Primitive::kPrimLong) {
1091 // For multiplication, okay to truncate to required precision.
1092 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1093 fpow = static_cast<int32_t>(fpow);
1094 }
1095 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001096 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001097 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001098 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001099 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001100 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001101 HInstruction* e = nullptr;
1102 if (info->operation == HInductionVarAnalysis::kMul) {
1103 e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow));
1104 } else {
1105 e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
1106 }
1107 *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001108 }
1109 }
1110 return true;
1111 }
1112 }
1113 return false;
1114}
1115
Aart Bikdf7822e2016-12-06 10:05:30 -08001116bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1117 HInductionVarAnalysis::InductionInfo* trip,
1118 HGraph* graph,
1119 HBasicBlock* block,
1120 /*out*/HInstruction** result) const {
1121 DCHECK(info != nullptr);
1122 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1123 // Count depth.
1124 int32_t depth = 0;
1125 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1126 info = info->op_b, ++depth) {}
1127 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001128 // TODO: generalize, but be careful to adjust the terminal.
1129 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001130 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001131 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1132 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001133 }
1134 return false;
1135}
1136
Aart Bik9abf8942016-10-14 09:49:42 -07001137bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1138 HInductionVarAnalysis::InductionInfo* trip,
1139 HGraph* graph,
1140 HBasicBlock* block,
1141 /*out*/HInstruction** result,
1142 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001143 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001144 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik9abf8942016-10-14 09:49:42 -07001145 // Count period.
Aart Bike6bd0272016-12-16 13:57:52 -08001146 int64_t period = 1;
Aart Bik9abf8942016-10-14 09:49:42 -07001147 for (HInductionVarAnalysis::InductionInfo* p = info;
1148 p->induction_class == HInductionVarAnalysis::kPeriodic;
1149 p = p->op_b, ++period) {}
Aart Bike6bd0272016-12-16 13:57:52 -08001150 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1151 int64_t m = 0;
1152 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1153 int64_t li = m % period;
1154 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1155 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1156 info = info->op_a;
1157 }
1158 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001159 }
Aart Bike6bd0272016-12-16 13:57:52 -08001160 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1161 // directly to obtain the maximum index value t even if taken test is needed.
1162 HInstruction* x = nullptr;
1163 HInstruction* y = nullptr;
1164 HInstruction* t = nullptr;
1165 if (period == 2 &&
1166 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1167 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1168 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1169 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001170 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001171 Primitive::Type type = trip->type;
1172 HInstruction* msk =
1173 Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1)));
1174 HInstruction* is_even =
1175 Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1176 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001177 }
1178 // Guard select with taken test if needed.
1179 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001180 HInstruction* is_taken = nullptr;
1181 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1182 if (graph != nullptr) {
1183 *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc));
1184 }
1185 *needs_taken_test = false; // taken care of
1186 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001187 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001188 }
Aart Bik9abf8942016-10-14 09:49:42 -07001189 }
1190 return true;
1191 }
1192 return false;
1193}
1194
Aart Bikaec3cce2015-10-14 17:44:55 -07001195bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1196 HInductionVarAnalysis::InductionInfo* trip,
1197 HGraph* graph, // when set, code is generated
1198 HBasicBlock* block,
1199 /*out*/HInstruction** result,
1200 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001201 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001202 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001203 // If during codegen, the result is not needed (nullptr), simply return success.
1204 if (graph != nullptr && result == nullptr) {
1205 return true;
1206 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001207 // Handle current operation.
Aart Bike6bd0272016-12-16 13:57:52 -08001208 Primitive::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001209 HInstruction* opa = nullptr;
1210 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001211 switch (info->induction_class) {
1212 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001213 // Invariants (note that since invariants only have other invariants as
1214 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001215 switch (info->operation) {
1216 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001217 case HInductionVarAnalysis::kSub:
1218 case HInductionVarAnalysis::kMul:
1219 case HInductionVarAnalysis::kDiv:
1220 case HInductionVarAnalysis::kRem:
1221 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001222 case HInductionVarAnalysis::kLT:
1223 case HInductionVarAnalysis::kLE:
1224 case HInductionVarAnalysis::kGT:
1225 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001226 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1227 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1228 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001229 HInstruction* operation = nullptr;
1230 switch (info->operation) {
1231 case HInductionVarAnalysis::kAdd:
1232 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001233 case HInductionVarAnalysis::kSub:
1234 operation = new (graph->GetArena()) HSub(type, opa, opb); break;
1235 case HInductionVarAnalysis::kMul:
1236 operation = new (graph->GetArena()) HMul(type, opa, opb, kNoDexPc); break;
1237 case HInductionVarAnalysis::kDiv:
1238 operation = new (graph->GetArena()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001239 case HInductionVarAnalysis::kRem:
1240 operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001241 case HInductionVarAnalysis::kXor:
1242 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001243 case HInductionVarAnalysis::kLT:
1244 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1245 case HInductionVarAnalysis::kLE:
1246 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1247 case HInductionVarAnalysis::kGT:
1248 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1249 case HInductionVarAnalysis::kGE:
1250 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1251 default:
1252 LOG(FATAL) << "unknown operation";
1253 }
1254 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001255 }
1256 return true;
1257 }
1258 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001259 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001260 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1261 if (graph != nullptr) {
1262 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1263 }
1264 return true;
1265 }
1266 break;
1267 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001268 if (graph != nullptr) {
1269 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001270 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001271 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001272 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001273 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001274 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001275 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001276 }
1277 FALLTHROUGH_INTENDED;
1278 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001279 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001280 if (is_min) {
1281 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001282 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001283 }
1284 return true;
1285 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001286 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001287 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001288 *result =
1289 Insert(block,
1290 new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001291 }
1292 return true;
1293 }
1294 }
1295 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001296 case HInductionVarAnalysis::kNop:
1297 LOG(FATAL) << "unexpected invariant nop";
1298 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001299 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001300 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001301 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1302 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1303 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001304 // known stride yields right value. Always avoid any narrowing linear induction or
1305 // any type mismatch between the linear induction and the trip count expression.
1306 // TODO: careful runtime type conversions could generalize this latter restriction.
1307 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1308 int64_t stride_value = 0;
1309 if (IsConstant(info->op_a, kExact, &stride_value) &&
1310 CanLongValueFitIntoInt(stride_value)) {
1311 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1312 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1313 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1314 if (graph != nullptr) {
1315 HInstruction* oper;
1316 if (stride_value == 1) {
1317 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1318 } else if (stride_value == -1) {
1319 oper = new (graph->GetArena()) HSub(type, opb, opa);
1320 } else {
1321 HInstruction* mul =
1322 new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa);
1323 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
1324 }
1325 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001326 }
Aart Bike6bd0272016-12-16 13:57:52 -08001327 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001328 }
1329 }
1330 }
1331 break;
Aart Bik4a342772015-11-30 10:17:46 -08001332 }
Aart Bikc071a012016-12-01 10:22:31 -08001333 case HInductionVarAnalysis::kPolynomial:
1334 case HInductionVarAnalysis::kGeometric:
1335 break;
Aart Bik4a342772015-11-30 10:17:46 -08001336 case HInductionVarAnalysis::kWrapAround:
1337 case HInductionVarAnalysis::kPeriodic: {
1338 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1339 // values are easy to test at runtime without complications of arithmetic wrap-around.
1340 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001341 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001342 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001343 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001344 }
1345 return true;
1346 }
1347 break;
1348 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001349 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001350 }
1351 return false;
1352}
1353
Aart Bik16d3a652016-09-09 10:33:50 -07001354void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1355 HInstruction* fetch,
1356 HInstruction* replacement) {
1357 if (info != nullptr) {
1358 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1359 info->operation == HInductionVarAnalysis::kFetch &&
1360 info->fetch == fetch) {
1361 info->fetch = replacement;
1362 }
1363 ReplaceInduction(info->op_a, fetch, replacement);
1364 ReplaceInduction(info->op_b, fetch, replacement);
1365 }
1366}
1367
Aart Bikd14c5952015-09-08 15:25:15 -07001368} // namespace art