blob: 5539413aadd10ac9dd4ac4526a640d0c54b92bb3 [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 Bikd3ba6262017-01-30 14:37:12 -080060/** Returns b^e for b,e >= 1. Sets overflow if arithmetic wrap-around occurred. */
61static int64_t IntPow(int64_t b, int64_t e, /*out*/ bool* overflow) {
Aart Bikc071a012016-12-01 10:22:31 -080062 DCHECK_GE(b, 1);
63 DCHECK_GE(e, 1);
64 int64_t pow = 1;
65 while (e) {
66 if (e & 1) {
Aart Bikd3ba6262017-01-30 14:37:12 -080067 int64_t oldpow = pow;
Aart Bikc071a012016-12-01 10:22:31 -080068 pow *= b;
Aart Bikd3ba6262017-01-30 14:37:12 -080069 if (pow < oldpow) {
70 *overflow = true;
71 }
Aart Bikc071a012016-12-01 10:22:31 -080072 }
73 e >>= 1;
74 b *= b;
75 }
76 return pow;
77}
78
Aart Bikb3365e02015-09-21 14:45:05 -070079/**
Aart Bik40fbf742016-10-31 11:02:50 -070080 * Detects an instruction that is >= 0. As long as the value is carried by
81 * a single instruction, arithmetic wrap-around cannot occur.
Aart Bikb3365e02015-09-21 14:45:05 -070082 */
Aart Bik40fbf742016-10-31 11:02:50 -070083static bool IsGEZero(HInstruction* instruction) {
84 DCHECK(instruction != nullptr);
85 if (instruction->IsArrayLength()) {
86 return true;
87 } else if (instruction->IsInvokeStaticOrDirect()) {
88 switch (instruction->AsInvoke()->GetIntrinsic()) {
89 case Intrinsics::kMathMinIntInt:
90 case Intrinsics::kMathMinLongLong:
91 // Instruction MIN(>=0, >=0) is >= 0.
92 return IsGEZero(instruction->InputAt(0)) &&
93 IsGEZero(instruction->InputAt(1));
94 case Intrinsics::kMathAbsInt:
95 case Intrinsics::kMathAbsLong:
96 // Instruction ABS(x) is >= 0.
97 return true;
98 default:
99 break;
100 }
101 }
102 int64_t value = -1;
103 return IsIntAndGet(instruction, &value) && value >= 0;
104}
105
106/** Hunts "under the hood" for a suitable instruction at the hint. */
107static bool IsMaxAtHint(
108 HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) {
109 if (instruction->IsInvokeStaticOrDirect()) {
110 switch (instruction->AsInvoke()->GetIntrinsic()) {
111 case Intrinsics::kMathMinIntInt:
112 case Intrinsics::kMathMinLongLong:
113 // For MIN(x, y), return most suitable x or y as maximum.
114 return IsMaxAtHint(instruction->InputAt(0), hint, suitable) ||
115 IsMaxAtHint(instruction->InputAt(1), hint, suitable);
116 default:
117 break;
118 }
119 } else {
120 *suitable = instruction;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000121 return HuntForDeclaration(instruction) == hint;
Aart Bik40fbf742016-10-31 11:02:50 -0700122 }
123 return false;
124}
125
126/** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */
127static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) {
128 if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) {
129 // If a == 1, instruction >= 0 and b <= 0, just return the constant b.
130 // No arithmetic wrap-around can occur.
131 if (IsGEZero(v.instruction)) {
132 return InductionVarRange::Value(v.b_constant);
133 }
Aart Bikb3365e02015-09-21 14:45:05 -0700134 }
135 return v;
136}
137
Aart Bik40fbf742016-10-31 11:02:50 -0700138/** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */
139static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) {
140 if (v.is_known && v.a_constant >= 1) {
141 // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as
142 // length + b because length >= 0 is true.
143 int64_t value;
144 if (v.instruction->IsDiv() &&
145 v.instruction->InputAt(0)->IsArrayLength() &&
146 IsIntAndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
147 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
148 }
149 // If a == 1, the most suitable one suffices as maximum value.
150 HInstruction* suitable = nullptr;
151 if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) {
152 return InductionVarRange::Value(suitable, 1, v.b_constant);
153 }
154 }
155 return v;
156}
157
158/** Tests for a constant value. */
Aart Bik52be7e72016-06-23 11:20:41 -0700159static bool IsConstantValue(InductionVarRange::Value v) {
160 return v.is_known && v.a_constant == 0;
161}
162
163/** Corrects a value for type to account for arithmetic wrap-around in lower precision. */
Aart Bik0d345cf2016-03-16 10:49:38 -0700164static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) {
165 switch (type) {
166 case Primitive::kPrimShort:
167 case Primitive::kPrimChar:
168 case Primitive::kPrimByte: {
169 // Constants within range only.
170 // TODO: maybe some room for improvement, like allowing widening conversions
Aart Bike6bd0272016-12-16 13:57:52 -0800171 int32_t min = Primitive::MinValueOfIntegralType(type);
172 int32_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik52be7e72016-06-23 11:20:41 -0700173 return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max)
Aart Bik0d345cf2016-03-16 10:49:38 -0700174 ? v
175 : InductionVarRange::Value();
176 }
177 default:
Aart Bik0d345cf2016-03-16 10:49:38 -0700178 return v;
179 }
180}
181
Aart Bik40fbf742016-10-31 11:02:50 -0700182/** Inserts an instruction. */
Aart Bik389b3db2015-10-28 14:23:40 -0700183static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
184 DCHECK(block != nullptr);
185 DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId();
Aart Bikaec3cce2015-10-14 17:44:55 -0700186 DCHECK(instruction != nullptr);
Aart Bik389b3db2015-10-28 14:23:40 -0700187 block->InsertInstructionBefore(instruction, block->GetLastInstruction());
Aart Bikaec3cce2015-10-14 17:44:55 -0700188 return instruction;
189}
190
Aart Bik40fbf742016-10-31 11:02:50 -0700191/** Obtains loop's control instruction. */
Aart Bik009cace2016-09-16 10:15:19 -0700192static HInstruction* GetLoopControl(HLoopInformation* loop) {
193 DCHECK(loop != nullptr);
194 return loop->GetHeader()->GetLastInstruction();
195}
196
Aart Bikd14c5952015-09-08 15:25:15 -0700197//
198// Public class methods.
199//
200
201InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis)
Aart Bik52be7e72016-06-23 11:20:41 -0700202 : induction_analysis_(induction_analysis),
203 chase_hint_(nullptr) {
Aart Bikb3365e02015-09-21 14:45:05 -0700204 DCHECK(induction_analysis != nullptr);
Aart Bikd14c5952015-09-08 15:25:15 -0700205}
206
Aart Bik1fc3afb2016-02-02 13:26:16 -0800207bool InductionVarRange::GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -0700208 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -0700209 HInstruction* chase_hint,
Aart Bik389b3db2015-10-28 14:23:40 -0700210 /*out*/Value* min_val,
211 /*out*/Value* max_val,
212 /*out*/bool* needs_finite_test) {
Aart Bik52be7e72016-06-23 11:20:41 -0700213 HLoopInformation* loop = nullptr;
214 HInductionVarAnalysis::InductionInfo* info = nullptr;
215 HInductionVarAnalysis::InductionInfo* trip = nullptr;
216 if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) {
217 return false;
Aart Bik97412c922016-02-19 20:14:38 -0800218 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700219 // Type int or lower (this is not too restrictive since intended clients, like
220 // bounds check elimination, will have truncated higher precision induction
221 // at their use point already).
222 switch (info->type) {
223 case Primitive::kPrimInt:
224 case Primitive::kPrimShort:
225 case Primitive::kPrimChar:
226 case Primitive::kPrimByte:
227 break;
228 default:
229 return false;
230 }
Aart Bik97412c922016-02-19 20:14:38 -0800231 // Find range.
Aart Bik52be7e72016-06-23 11:20:41 -0700232 chase_hint_ = chase_hint;
233 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700234 int64_t stride_value = 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700235 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
236 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint);
Aart Bik16d3a652016-09-09 10:33:50 -0700237 *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip);
Aart Bik40fbf742016-10-31 11:02:50 -0700238 chase_hint_ = nullptr;
239 // Retry chasing constants for wrap-around (merge sensitive).
240 if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) {
241 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
242 }
Aart Bik97412c922016-02-19 20:14:38 -0800243 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700244}
245
Aart Bik16d3a652016-09-09 10:33:50 -0700246bool InductionVarRange::CanGenerateRange(HInstruction* context,
247 HInstruction* instruction,
248 /*out*/bool* needs_finite_test,
249 /*out*/bool* needs_taken_test) {
250 bool is_last_value = false;
251 int64_t stride_value = 0;
Aart Bik9abf8942016-10-14 09:49:42 -0700252 return GenerateRangeOrLastValue(context,
253 instruction,
254 is_last_value,
255 nullptr,
256 nullptr,
257 nullptr,
258 nullptr,
259 nullptr, // nothing generated yet
260 &stride_value,
261 needs_finite_test,
262 needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700263 && (stride_value == -1 ||
264 stride_value == 0 ||
Aart Bik40fbf742016-10-31 11:02:50 -0700265 stride_value == 1); // avoid arithmetic wrap-around anomalies.
Aart Bikaec3cce2015-10-14 17:44:55 -0700266}
267
Aart Bik16d3a652016-09-09 10:33:50 -0700268void InductionVarRange::GenerateRange(HInstruction* context,
269 HInstruction* instruction,
270 HGraph* graph,
271 HBasicBlock* block,
272 /*out*/HInstruction** lower,
273 /*out*/HInstruction** upper) {
274 bool is_last_value = false;
Aart Bik009cace2016-09-16 10:15:19 -0700275 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700276 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700277 if (!GenerateRangeOrLastValue(context,
278 instruction,
279 is_last_value,
280 graph,
281 block,
282 lower,
283 upper,
284 nullptr,
285 &stride_value,
286 &b1,
287 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700288 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
Aart Bik389b3db2015-10-28 14:23:40 -0700289 }
290}
291
Aart Bik16d3a652016-09-09 10:33:50 -0700292HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context,
293 HGraph* graph,
294 HBasicBlock* block) {
295 HInstruction* taken_test = nullptr;
296 bool is_last_value = false;
297 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700298 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700299 if (!GenerateRangeOrLastValue(context,
300 context,
301 is_last_value,
302 graph,
303 block,
304 nullptr,
305 nullptr,
306 &taken_test,
307 &stride_value,
308 &b1,
309 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700310 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
311 }
312 return taken_test;
313}
314
315bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) {
316 bool is_last_value = true;
317 int64_t stride_value = 0;
318 bool needs_finite_test = false;
319 bool needs_taken_test = false;
Aart Bik9abf8942016-10-14 09:49:42 -0700320 return GenerateRangeOrLastValue(instruction,
321 instruction,
322 is_last_value,
323 nullptr,
324 nullptr,
325 nullptr,
326 nullptr,
327 nullptr, // nothing generated yet
328 &stride_value,
329 &needs_finite_test,
330 &needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700331 && !needs_finite_test && !needs_taken_test;
332}
333
334HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction,
335 HGraph* graph,
336 HBasicBlock* block) {
337 HInstruction* last_value = nullptr;
338 bool is_last_value = true;
339 int64_t stride_value = 0;
340 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700341 if (!GenerateRangeOrLastValue(instruction,
342 instruction,
343 is_last_value,
344 graph,
345 block,
346 &last_value,
347 &last_value,
348 nullptr,
349 &stride_value,
350 &b1,
351 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700352 LOG(FATAL) << "Failed precondition: CanGenerateLastValue()";
353 }
354 return last_value;
355}
356
357void InductionVarRange::Replace(HInstruction* instruction,
358 HInstruction* fetch,
359 HInstruction* replacement) {
360 for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
361 lp != nullptr;
362 lp = lp->GetPreHeader()->GetLoopInformation()) {
Aart Bik009cace2016-09-16 10:15:19 -0700363 // Update instruction's information.
Aart Bik16d3a652016-09-09 10:33:50 -0700364 ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement);
Aart Bik009cace2016-09-16 10:15:19 -0700365 // Update loop's trip-count information.
366 ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement);
Aart Bik389b3db2015-10-28 14:23:40 -0700367 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700368}
369
Aart Bik6b69e0a2017-01-11 10:20:43 -0800370bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const {
Aart Bik9abf8942016-10-14 09:49:42 -0700371 HInductionVarAnalysis::InductionInfo *trip =
372 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
Aart Bik6b69e0a2017-01-11 10:20:43 -0800373 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
374 IsConstant(trip->op_a, kExact, tc);
375 return true;
376 }
377 return false;
Aart Bik9abf8942016-10-14 09:49:42 -0700378}
379
Aart Bikd14c5952015-09-08 15:25:15 -0700380//
381// Private class methods.
382//
383
Aart Bik97412c922016-02-19 20:14:38 -0800384bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
385 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700386 /*out*/ int64_t* value) const {
Aart Bik97412c922016-02-19 20:14:38 -0800387 if (info != nullptr) {
388 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
389 // any of the three requests (kExact, kAtMost, and KAtLeast).
390 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
391 info->operation == HInductionVarAnalysis::kFetch) {
392 if (IsIntAndGet(info->fetch, value)) {
393 return true;
394 }
395 }
Aart Bik40fbf742016-10-31 11:02:50 -0700396 // Try range analysis on the invariant, only accept a proper range
397 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700398 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
399 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
400 if (IsConstantValue(min_val) &&
401 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
402 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
403 *value = max_val.b_constant;
404 return true;
405 } else if (request == kAtLeast) {
406 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800407 return true;
408 }
409 }
Aart Bik97412c922016-02-19 20:14:38 -0800410 }
411 return false;
412}
413
Aart Bik52be7e72016-06-23 11:20:41 -0700414bool InductionVarRange::HasInductionInfo(
415 HInstruction* context,
416 HInstruction* instruction,
417 /*out*/ HLoopInformation** loop,
418 /*out*/ HInductionVarAnalysis::InductionInfo** info,
419 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800420 DCHECK(context != nullptr);
421 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700422 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
423 if (lp != nullptr) {
424 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700425 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700426 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700427 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700428 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700429 return true;
430 }
431 }
432 return false;
433}
434
435bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
436 if (trip != nullptr) {
437 // Both bounds that define a trip-count are well-behaved if they either are not defined
438 // in any loop, or are contained in a proper interval. This allows finding the min/max
439 // of an expression by chasing outward.
440 InductionVarRange range(induction_analysis_);
441 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
442 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
443 int64_t not_used = 0;
444 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
445 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
446 }
447 return true;
448}
449
450bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
451 if (info != nullptr) {
452 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
453 info->operation == HInductionVarAnalysis::kFetch) {
454 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
455 }
456 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
457 }
458 return false;
459}
460
Aart Bik16d3a652016-09-09 10:33:50 -0700461bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
462 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700463 if (info != nullptr) {
464 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700465 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800466 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
467 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700468 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700469 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700470 }
Aart Bikd14c5952015-09-08 15:25:15 -0700471 }
Aart Bik389b3db2015-10-28 14:23:40 -0700472 return false;
473}
474
Aart Bik7d57d7f2015-12-09 14:39:48 -0800475bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700476 if (trip != nullptr) {
477 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
478 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
479 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
480 }
481 }
482 return false;
483}
484
Aart Bik7d57d7f2015-12-09 14:39:48 -0800485bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700486 if (trip != nullptr) {
487 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
488 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
489 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
490 }
491 }
492 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700493}
494
Aart Bik7d57d7f2015-12-09 14:39:48 -0800495InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
496 HInductionVarAnalysis::InductionInfo* trip,
497 bool in_body,
498 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800499 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800500 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700501 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800502 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
503 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
504 // with intermediate results that only incorporate single instructions.
505 if (trip != nullptr) {
506 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700507 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800508 int64_t stride_value = 0;
509 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800510 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800511 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800512 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
513 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
514 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700515 trip->induction_class,
516 trip->operation,
517 trip_expr->op_a,
518 trip->op_b,
519 nullptr,
520 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800521 return GetVal(&cancelled_trip, trip, in_body, is_min);
522 }
523 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800524 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800525 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
526 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
527 HInductionVarAnalysis::InductionInfo neg(
528 HInductionVarAnalysis::kInvariant,
529 HInductionVarAnalysis::kNeg,
530 nullptr,
531 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700532 nullptr,
533 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800534 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700535 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800536 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
537 }
538 }
539 }
540 }
541 }
542 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
543 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
544 GetVal(info->op_b, trip, in_body, is_min));
545}
546
Aart Bikdf7822e2016-12-06 10:05:30 -0800547InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
548 HInductionVarAnalysis::InductionInfo* trip,
549 bool in_body,
550 bool is_min) const {
551 DCHECK(info != nullptr);
552 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
553 int64_t a = 0;
554 int64_t b = 0;
555 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
556 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800557 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800558 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
559 Value c = GetVal(info->op_b, trip, in_body, is_min);
560 if (is_min) {
561 return c;
562 } else {
563 Value m = GetVal(trip, trip, in_body, is_min);
564 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
565 Value x = MulValue(Value(a), t);
566 Value y = MulValue(Value(b), m);
567 return AddValue(AddValue(x, y), c);
568 }
569 }
570 return Value();
571}
572
Aart Bikc071a012016-12-01 10:22:31 -0800573InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
574 HInductionVarAnalysis::InductionInfo* trip,
575 bool in_body,
576 bool is_min) const {
577 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800578 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800579 int64_t a = 0;
580 int64_t f = 0;
581 if (IsConstant(info->op_a, kExact, &a) &&
582 CanLongValueFitIntoInt(a) &&
Aart Bikdf7822e2016-12-06 10:05:30 -0800583 IsIntAndGet(info->fetch, &f) && f >= 1) {
584 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
585 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800586 const bool is_min_a = a >= 0 ? is_min : !is_min;
587 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800588 Value b = GetVal(info->op_b, trip, in_body, is_min);
589 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800590 }
591 }
592 return Value();
593}
594
Aart Bikd14c5952015-09-08 15:25:15 -0700595InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700596 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700597 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800598 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700599 // Special case when chasing constants: single instruction that denotes trip count in the
600 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
601 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700602 if (is_min) {
603 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800604 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700605 return Value(std::numeric_limits<int32_t>::max());
606 }
607 }
Aart Bik40fbf742016-10-31 11:02:50 -0700608 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
609 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
610 int64_t value;
611 if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
612 // Proper constant reveals best information.
613 return Value(static_cast<int32_t>(value));
614 } else if (instruction == chase_hint_) {
615 // At hint, fetch is represented by itself.
616 return Value(instruction, 1, 0);
617 } else if (instruction->IsAdd()) {
618 // Incorporate suitable constants in the chased value.
Aart Bik97412c922016-02-19 20:14:38 -0800619 if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
620 return AddValue(Value(static_cast<int32_t>(value)),
621 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
622 } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
623 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
624 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700625 }
Aart Bik52be7e72016-06-23 11:20:41 -0700626 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700627 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700628 if (chase_hint_ == nullptr) {
629 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
630 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000631 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700632 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700633 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700634 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800635 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Aart Bik0d345cf2016-03-16 10:49:38 -0700636 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
637 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
638 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
639 }
Aart Bik52be7e72016-06-23 11:20:41 -0700640 }
641 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
642 // so far is well-behaved in both bounds and the next trip-count is safe.
643 // Example:
644 // for (int i = 0; i <= 100; i++) // safe
645 // for (int j = 0; j <= i; j++) // well-behaved
646 // j is in range [0, i ] (if i is chase hint)
647 // or in range [0, 100] (otherwise)
648 HLoopInformation* next_loop = nullptr;
649 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
650 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
651 bool next_in_body = true; // inner loop is always in body of outer loop
652 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
653 IsWellBehavedTripCount(trip) &&
654 !IsUnsafeTripCount(next_trip)) {
655 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700656 }
Aart Bik40fbf742016-10-31 11:02:50 -0700657 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700658 return Value(instruction, 1, 0);
659}
660
Aart Bikcd26feb2015-09-23 17:50:50 -0700661InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
662 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700663 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800664 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700665 if (info != nullptr) {
666 switch (info->induction_class) {
667 case HInductionVarAnalysis::kInvariant:
668 // Invariants.
669 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700670 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700671 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
672 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700673 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700674 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
675 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700676 case HInductionVarAnalysis::kNeg: // second reversed!
677 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700678 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700679 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700680 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700681 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700682 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800683 case HInductionVarAnalysis::kRem:
684 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700685 case HInductionVarAnalysis::kXor:
686 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700687 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700688 return GetFetch(info->fetch, trip, in_body, is_min);
689 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700690 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700691 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700692 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700693 }
694 FALLTHROUGH_INTENDED;
695 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700696 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700697 if (is_min) {
698 return Value(0);
699 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700700 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700701 }
702 break;
703 default:
704 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700705 }
706 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700707 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700708 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800709 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800710 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800711 case HInductionVarAnalysis::kGeometric:
712 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700713 case HInductionVarAnalysis::kWrapAround:
714 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700715 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
716 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700717 }
718 }
Aart Bikb3365e02015-09-21 14:45:05 -0700719 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700720}
721
722InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
723 HInductionVarAnalysis::InductionInfo* info2,
724 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700725 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800726 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700727 // Constant times range.
728 int64_t value = 0;
729 if (IsConstant(info1, kExact, &value)) {
730 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
731 } else if (IsConstant(info2, kExact, &value)) {
732 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
733 }
734 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700735 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
736 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
737 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
738 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800739 // Positive range vs. positive or negative range.
740 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
741 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
742 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
743 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
744 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700745 }
Aart Bik97412c922016-02-19 20:14:38 -0800746 }
747 // Negative range vs. positive or negative range.
748 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
749 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
750 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
751 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
752 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700753 }
754 }
Aart Bikb3365e02015-09-21 14:45:05 -0700755 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700756}
757
758InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
759 HInductionVarAnalysis::InductionInfo* info2,
760 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700761 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800762 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700763 // Range divided by constant.
764 int64_t value = 0;
765 if (IsConstant(info2, kExact, &value)) {
766 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
767 }
768 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700769 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
770 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
771 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
772 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800773 // Positive range vs. positive or negative range.
774 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
775 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
776 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
777 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
778 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700779 }
Aart Bik97412c922016-02-19 20:14:38 -0800780 }
781 // Negative range vs. positive or negative range.
782 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
783 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
784 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
785 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
786 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700787 }
788 }
Aart Bikb3365e02015-09-21 14:45:05 -0700789 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700790}
791
Aart Bikdf7822e2016-12-06 10:05:30 -0800792InductionVarRange::Value InductionVarRange::GetRem(
793 HInductionVarAnalysis::InductionInfo* info1,
794 HInductionVarAnalysis::InductionInfo* info2) const {
795 int64_t v1 = 0;
796 int64_t v2 = 0;
797 // Only accept exact values.
798 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
799 int64_t value = v1 % v2;
800 if (CanLongValueFitIntoInt(value)) {
801 return Value(static_cast<int32_t>(value));
802 }
803 }
804 return Value();
805}
806
Aart Bik7dc96932016-10-12 10:01:05 -0700807InductionVarRange::Value InductionVarRange::GetXor(
808 HInductionVarAnalysis::InductionInfo* info1,
809 HInductionVarAnalysis::InductionInfo* info2) const {
810 int64_t v1 = 0;
811 int64_t v2 = 0;
812 // Only accept exact values.
813 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
814 int64_t value = v1 ^ v2;
815 if (CanLongValueFitIntoInt(value)) {
816 return Value(static_cast<int32_t>(value));
817 }
818 }
819 return Value();
820}
821
Aart Bik52be7e72016-06-23 11:20:41 -0700822InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
823 int64_t value,
824 HInductionVarAnalysis::InductionInfo* info,
825 HInductionVarAnalysis::InductionInfo* trip,
826 bool in_body,
827 bool is_min) const {
828 if (CanLongValueFitIntoInt(value)) {
829 Value c(static_cast<int32_t>(value));
830 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
831 }
832 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800833}
834
Aart Bik52be7e72016-06-23 11:20:41 -0700835InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
836 int64_t value,
837 HInductionVarAnalysis::InductionInfo* info,
838 HInductionVarAnalysis::InductionInfo* trip,
839 bool in_body,
840 bool is_min) const {
841 if (CanLongValueFitIntoInt(value)) {
842 Value c(static_cast<int32_t>(value));
843 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
844 }
845 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700846}
847
Aart Bik7d57d7f2015-12-09 14:39:48 -0800848InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700849 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800850 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700851 if (v1.a_constant == 0) {
852 return Value(v2.instruction, v2.a_constant, b);
853 } else if (v2.a_constant == 0) {
854 return Value(v1.instruction, v1.a_constant, b);
855 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
856 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
857 }
858 }
Aart Bikb3365e02015-09-21 14:45:05 -0700859 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700860}
861
Aart Bik7d57d7f2015-12-09 14:39:48 -0800862InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700863 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800864 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700865 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
866 return Value(v2.instruction, -v2.a_constant, b);
867 } else if (v2.a_constant == 0) {
868 return Value(v1.instruction, v1.a_constant, b);
869 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
870 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
871 }
872 }
Aart Bikb3365e02015-09-21 14:45:05 -0700873 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700874}
875
Aart Bik7d57d7f2015-12-09 14:39:48 -0800876InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700877 if (v1.is_known && v2.is_known) {
878 if (v1.a_constant == 0) {
879 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
880 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
881 }
882 } else if (v2.a_constant == 0) {
883 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
884 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
885 }
Aart Bikd14c5952015-09-08 15:25:15 -0700886 }
887 }
Aart Bikb3365e02015-09-21 14:45:05 -0700888 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700889}
890
Aart Bik7d57d7f2015-12-09 14:39:48 -0800891InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700892 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700893 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
894 return Value(v1.b_constant / v2.b_constant);
895 }
896 }
Aart Bikb3365e02015-09-21 14:45:05 -0700897 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700898}
899
Aart Bik7d57d7f2015-12-09 14:39:48 -0800900InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700901 if (v1.is_known && v2.is_known) {
902 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700903 return Value(v1.instruction, v1.a_constant,
904 is_min ? std::min(v1.b_constant, v2.b_constant)
905 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700906 }
Aart Bikd14c5952015-09-08 15:25:15 -0700907 }
Aart Bikb3365e02015-09-21 14:45:05 -0700908 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700909}
910
Aart Bik9abf8942016-10-14 09:49:42 -0700911bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
912 HInstruction* instruction,
913 bool is_last_value,
914 HGraph* graph,
915 HBasicBlock* block,
916 /*out*/HInstruction** lower,
917 /*out*/HInstruction** upper,
918 /*out*/HInstruction** taken_test,
919 /*out*/int64_t* stride_value,
920 /*out*/bool* needs_finite_test,
921 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700922 HLoopInformation* loop = nullptr;
923 HInductionVarAnalysis::InductionInfo* info = nullptr;
924 HInductionVarAnalysis::InductionInfo* trip = nullptr;
925 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
926 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800927 }
928 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
929 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
930 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
931 // code does not use the trip-count explicitly (since there could be an implicit relation
932 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700933 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700934 *stride_value = 0;
935 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800936 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700937 // Handle last value request.
938 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800939 DCHECK(!in_body);
940 switch (info->induction_class) {
941 case HInductionVarAnalysis::kLinear:
942 if (*stride_value > 0) {
943 lower = nullptr;
944 } else {
945 upper = nullptr;
946 }
947 break;
Aart Bikdf7822e2016-12-06 10:05:30 -0800948 case HInductionVarAnalysis::kPolynomial:
949 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -0800950 case HInductionVarAnalysis::kGeometric:
951 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -0800952 case HInductionVarAnalysis::kWrapAround:
953 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -0800954 case HInductionVarAnalysis::kPeriodic:
955 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
956 default:
957 return false;
Aart Bik16d3a652016-09-09 10:33:50 -0700958 }
959 }
Aart Bik97412c922016-02-19 20:14:38 -0800960 // Code generation for taken test: generate the code when requested or otherwise analyze
961 // if code generation is feasible when taken test is needed.
962 if (taken_test != nullptr) {
963 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
964 } else if (*needs_taken_test) {
965 if (!GenerateCode(
966 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
967 return false;
968 }
969 }
970 // Code generation for lower and upper.
971 return
972 // Success on lower if invariant (not set), or code can be generated.
973 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
974 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
975 // And success on upper.
976 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -0700977}
978
Aart Bikdf7822e2016-12-06 10:05:30 -0800979bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
980 HInductionVarAnalysis::InductionInfo* trip,
981 HGraph* graph,
982 HBasicBlock* block,
983 /*out*/HInstruction** result) const {
984 DCHECK(info != nullptr);
985 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
986 // Detect known coefficients and trip count (always taken).
987 int64_t a = 0;
988 int64_t b = 0;
989 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -0800990 if (IsConstant(info->op_a->op_a, kExact, &a) &&
991 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -0800992 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800993 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -0800994 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -0800995 HInstruction* c = nullptr;
996 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800997 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800998 Primitive::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -0800999 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Aart Bike6bd0272016-12-16 13:57:52 -08001000 if (type != Primitive::kPrimLong) {
1001 sum = static_cast<int32_t>(sum); // okay to truncate
1002 }
1003 *result =
1004 Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001005 }
1006 return true;
1007 }
1008 }
1009 return false;
1010}
1011
Aart Bikc071a012016-12-01 10:22:31 -08001012bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1013 HInductionVarAnalysis::InductionInfo* trip,
1014 HGraph* graph,
1015 HBasicBlock* block,
1016 /*out*/HInstruction** result) const {
1017 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001018 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001019 // Detect known base and trip count (always taken).
1020 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001021 int64_t m = 0;
1022 if (IsIntAndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001023 HInstruction* opa = nullptr;
1024 HInstruction* opb = nullptr;
1025 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1026 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001027 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001028 Primitive::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001029 // Compute f ^ m for known maximum index value m.
1030 bool overflow = false;
1031 int64_t fpow = IntPow(f, m, &overflow);
1032 if (info->operation == HInductionVarAnalysis::kDiv) {
1033 // For division, any overflow truncates to zero.
1034 if (overflow || (type != Primitive::kPrimLong && !CanLongValueFitIntoInt(fpow))) {
1035 fpow = 0;
1036 }
1037 } else if (type != Primitive::kPrimLong) {
1038 // For multiplication, okay to truncate to required precision.
1039 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1040 fpow = static_cast<int32_t>(fpow);
1041 }
1042 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001043 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001044 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001045 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001046 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001047 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001048 HInstruction* e = nullptr;
1049 if (info->operation == HInductionVarAnalysis::kMul) {
1050 e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow));
1051 } else {
1052 e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
1053 }
1054 *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001055 }
1056 }
1057 return true;
1058 }
1059 }
1060 return false;
1061}
1062
Aart Bikdf7822e2016-12-06 10:05:30 -08001063bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1064 HInductionVarAnalysis::InductionInfo* trip,
1065 HGraph* graph,
1066 HBasicBlock* block,
1067 /*out*/HInstruction** result) const {
1068 DCHECK(info != nullptr);
1069 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1070 // Count depth.
1071 int32_t depth = 0;
1072 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1073 info = info->op_b, ++depth) {}
1074 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001075 // TODO: generalize, but be careful to adjust the terminal.
1076 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001077 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001078 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1079 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001080 }
1081 return false;
1082}
1083
Aart Bik9abf8942016-10-14 09:49:42 -07001084bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1085 HInductionVarAnalysis::InductionInfo* trip,
1086 HGraph* graph,
1087 HBasicBlock* block,
1088 /*out*/HInstruction** result,
1089 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001090 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001091 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik9abf8942016-10-14 09:49:42 -07001092 // Count period.
Aart Bike6bd0272016-12-16 13:57:52 -08001093 int64_t period = 1;
Aart Bik9abf8942016-10-14 09:49:42 -07001094 for (HInductionVarAnalysis::InductionInfo* p = info;
1095 p->induction_class == HInductionVarAnalysis::kPeriodic;
1096 p = p->op_b, ++period) {}
Aart Bike6bd0272016-12-16 13:57:52 -08001097 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1098 int64_t m = 0;
1099 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1100 int64_t li = m % period;
1101 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1102 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1103 info = info->op_a;
1104 }
1105 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001106 }
Aart Bike6bd0272016-12-16 13:57:52 -08001107 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1108 // directly to obtain the maximum index value t even if taken test is needed.
1109 HInstruction* x = nullptr;
1110 HInstruction* y = nullptr;
1111 HInstruction* t = nullptr;
1112 if (period == 2 &&
1113 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1114 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1115 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1116 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001117 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001118 Primitive::Type type = trip->type;
1119 HInstruction* msk =
1120 Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1)));
1121 HInstruction* is_even =
1122 Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1123 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001124 }
1125 // Guard select with taken test if needed.
1126 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001127 HInstruction* is_taken = nullptr;
1128 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1129 if (graph != nullptr) {
1130 *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc));
1131 }
1132 *needs_taken_test = false; // taken care of
1133 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001134 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001135 }
Aart Bik9abf8942016-10-14 09:49:42 -07001136 }
1137 return true;
1138 }
1139 return false;
1140}
1141
Aart Bikaec3cce2015-10-14 17:44:55 -07001142bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1143 HInductionVarAnalysis::InductionInfo* trip,
1144 HGraph* graph, // when set, code is generated
1145 HBasicBlock* block,
1146 /*out*/HInstruction** result,
1147 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001148 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001149 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001150 // If during codegen, the result is not needed (nullptr), simply return success.
1151 if (graph != nullptr && result == nullptr) {
1152 return true;
1153 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001154 // Handle current operation.
Aart Bike6bd0272016-12-16 13:57:52 -08001155 Primitive::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001156 HInstruction* opa = nullptr;
1157 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001158 switch (info->induction_class) {
1159 case HInductionVarAnalysis::kInvariant:
Aart Bik40fbf742016-10-31 11:02:50 -07001160 // Invariants (note that even though is_min does not impact code generation for
1161 // invariants, some effort is made to keep this parameter consistent).
Aart Bikaec3cce2015-10-14 17:44:55 -07001162 switch (info->operation) {
1163 case HInductionVarAnalysis::kAdd:
Aart Bikdf7822e2016-12-06 10:05:30 -08001164 case HInductionVarAnalysis::kRem: // no proper is_min for second arg
Aart Bik40fbf742016-10-31 11:02:50 -07001165 case HInductionVarAnalysis::kXor: // no proper is_min for second arg
Aart Bik389b3db2015-10-28 14:23:40 -07001166 case HInductionVarAnalysis::kLT:
1167 case HInductionVarAnalysis::kLE:
1168 case HInductionVarAnalysis::kGT:
1169 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001170 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1171 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1172 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001173 HInstruction* operation = nullptr;
1174 switch (info->operation) {
1175 case HInductionVarAnalysis::kAdd:
1176 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001177 case HInductionVarAnalysis::kRem:
1178 operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001179 case HInductionVarAnalysis::kXor:
1180 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001181 case HInductionVarAnalysis::kLT:
1182 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1183 case HInductionVarAnalysis::kLE:
1184 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1185 case HInductionVarAnalysis::kGT:
1186 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1187 case HInductionVarAnalysis::kGE:
1188 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1189 default:
1190 LOG(FATAL) << "unknown operation";
1191 }
1192 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001193 }
1194 return true;
1195 }
1196 break;
1197 case HInductionVarAnalysis::kSub: // second reversed!
1198 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1199 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1200 if (graph != nullptr) {
1201 *result = Insert(block, new (graph->GetArena()) HSub(type, opa, opb));
1202 }
1203 return true;
1204 }
1205 break;
1206 case HInductionVarAnalysis::kNeg: // reversed!
1207 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1208 if (graph != nullptr) {
1209 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1210 }
1211 return true;
1212 }
1213 break;
1214 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001215 if (graph != nullptr) {
1216 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001217 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001218 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001219 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001220 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001221 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001222 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001223 }
1224 FALLTHROUGH_INTENDED;
1225 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001226 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001227 if (is_min) {
1228 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001229 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001230 }
1231 return true;
1232 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001233 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001234 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001235 *result =
1236 Insert(block,
1237 new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001238 }
1239 return true;
1240 }
1241 }
1242 break;
1243 default:
1244 break;
1245 }
1246 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001247 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001248 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1249 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1250 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001251 // known stride yields right value. Always avoid any narrowing linear induction or
1252 // any type mismatch between the linear induction and the trip count expression.
1253 // TODO: careful runtime type conversions could generalize this latter restriction.
1254 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1255 int64_t stride_value = 0;
1256 if (IsConstant(info->op_a, kExact, &stride_value) &&
1257 CanLongValueFitIntoInt(stride_value)) {
1258 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1259 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1260 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1261 if (graph != nullptr) {
1262 HInstruction* oper;
1263 if (stride_value == 1) {
1264 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1265 } else if (stride_value == -1) {
1266 oper = new (graph->GetArena()) HSub(type, opb, opa);
1267 } else {
1268 HInstruction* mul =
1269 new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa);
1270 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
1271 }
1272 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001273 }
Aart Bike6bd0272016-12-16 13:57:52 -08001274 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001275 }
1276 }
1277 }
1278 break;
Aart Bik4a342772015-11-30 10:17:46 -08001279 }
Aart Bikc071a012016-12-01 10:22:31 -08001280 case HInductionVarAnalysis::kPolynomial:
1281 case HInductionVarAnalysis::kGeometric:
1282 break;
Aart Bik4a342772015-11-30 10:17:46 -08001283 case HInductionVarAnalysis::kWrapAround:
1284 case HInductionVarAnalysis::kPeriodic: {
1285 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1286 // values are easy to test at runtime without complications of arithmetic wrap-around.
1287 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001288 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001289 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001290 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001291 }
1292 return true;
1293 }
1294 break;
1295 }
Aart Bikaec3cce2015-10-14 17:44:55 -07001296 }
1297 }
1298 return false;
1299}
1300
Aart Bik16d3a652016-09-09 10:33:50 -07001301void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1302 HInstruction* fetch,
1303 HInstruction* replacement) {
1304 if (info != nullptr) {
1305 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1306 info->operation == HInductionVarAnalysis::kFetch &&
1307 info->fetch == fetch) {
1308 info->fetch = replacement;
1309 }
1310 ReplaceInduction(info->op_a, fetch, replacement);
1311 ReplaceInduction(info->op_b, fetch, replacement);
1312 }
1313}
1314
Aart Bikd14c5952015-09-08 15:25:15 -07001315} // namespace art