blob: 1cd65c1c66703f87a305ef604913d6106683a917 [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 Bik97412c92016-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 Bik97412c92016-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 Bik97412c92016-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 Bik97412c92016-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 Bik97412c92016-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 Bik8e02e3e2017-02-28 14:41:55 -0800380bool InductionVarRange::IsUnitStride(HInstruction* instruction,
381 /*out*/ HInstruction** offset) const {
382 HLoopInformation* loop = nullptr;
383 HInductionVarAnalysis::InductionInfo* info = nullptr;
384 HInductionVarAnalysis::InductionInfo* trip = nullptr;
385 if (HasInductionInfo(instruction, instruction, &loop, &info, &trip)) {
386 if (info->induction_class == HInductionVarAnalysis::kLinear &&
387 info->op_b->operation == HInductionVarAnalysis::kFetch) {
388 int64_t stride_value = 0;
389 if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) {
390 int64_t off_value = 0;
391 if (IsConstant(info->op_b, kExact, &off_value) && off_value == 0) {
392 *offset = nullptr;
393 } else {
394 *offset = info->op_b->fetch;
395 }
396 return true;
397 }
398 }
399 }
400 return false;
401}
402
403HInstruction* InductionVarRange::GenerateTripCount(HLoopInformation* loop,
404 HGraph* graph,
405 HBasicBlock* block) {
406 HInductionVarAnalysis::InductionInfo *trip =
407 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
408 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
409 HInstruction* taken_test = nullptr;
410 HInstruction* trip_expr = nullptr;
411 if (IsBodyTripCount(trip)) {
412 if (!GenerateCode(trip->op_b, nullptr, graph, block, &taken_test, false, false)) {
413 return nullptr;
414 }
415 }
416 if (GenerateCode(trip->op_a, nullptr, graph, block, &trip_expr, false, false)) {
417 if (taken_test != nullptr) {
418 HInstruction* zero = graph->GetConstant(trip->type, 0);
419 trip_expr = Insert(block, new (graph->GetArena()) HSelect(taken_test, trip_expr, zero, kNoDexPc));
420 }
421 return trip_expr;
422 }
423 }
424 return nullptr;
425}
426
Aart Bikd14c5952015-09-08 15:25:15 -0700427//
428// Private class methods.
429//
430
Aart Bik97412c92016-02-19 20:14:38 -0800431bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
432 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700433 /*out*/ int64_t* value) const {
Aart Bik97412c92016-02-19 20:14:38 -0800434 if (info != nullptr) {
435 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
436 // any of the three requests (kExact, kAtMost, and KAtLeast).
437 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
438 info->operation == HInductionVarAnalysis::kFetch) {
439 if (IsIntAndGet(info->fetch, value)) {
440 return true;
441 }
442 }
Aart Bik40fbf742016-10-31 11:02:50 -0700443 // Try range analysis on the invariant, only accept a proper range
444 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700445 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
446 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
447 if (IsConstantValue(min_val) &&
448 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
449 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
450 *value = max_val.b_constant;
451 return true;
452 } else if (request == kAtLeast) {
453 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800454 return true;
455 }
456 }
Aart Bik97412c92016-02-19 20:14:38 -0800457 }
458 return false;
459}
460
Aart Bik52be7e72016-06-23 11:20:41 -0700461bool InductionVarRange::HasInductionInfo(
462 HInstruction* context,
463 HInstruction* instruction,
464 /*out*/ HLoopInformation** loop,
465 /*out*/ HInductionVarAnalysis::InductionInfo** info,
466 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800467 DCHECK(context != nullptr);
468 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700469 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
470 if (lp != nullptr) {
471 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700472 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700473 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700474 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700475 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700476 return true;
477 }
478 }
479 return false;
480}
481
482bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
483 if (trip != nullptr) {
484 // Both bounds that define a trip-count are well-behaved if they either are not defined
485 // in any loop, or are contained in a proper interval. This allows finding the min/max
486 // of an expression by chasing outward.
487 InductionVarRange range(induction_analysis_);
488 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
489 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
490 int64_t not_used = 0;
491 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
492 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
493 }
494 return true;
495}
496
497bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
498 if (info != nullptr) {
499 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
500 info->operation == HInductionVarAnalysis::kFetch) {
501 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
502 }
503 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
504 }
505 return false;
506}
507
Aart Bik16d3a652016-09-09 10:33:50 -0700508bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
509 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700510 if (info != nullptr) {
511 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700512 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800513 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
514 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700515 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700516 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700517 }
Aart Bikd14c5952015-09-08 15:25:15 -0700518 }
Aart Bik389b3db2015-10-28 14:23:40 -0700519 return false;
520}
521
Aart Bik7d57d7f2015-12-09 14:39:48 -0800522bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700523 if (trip != nullptr) {
524 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
525 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
526 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
527 }
528 }
529 return false;
530}
531
Aart Bik7d57d7f2015-12-09 14:39:48 -0800532bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700533 if (trip != nullptr) {
534 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
535 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
536 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
537 }
538 }
539 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700540}
541
Aart Bik7d57d7f2015-12-09 14:39:48 -0800542InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
543 HInductionVarAnalysis::InductionInfo* trip,
544 bool in_body,
545 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800546 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800547 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700548 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800549 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
550 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
551 // with intermediate results that only incorporate single instructions.
552 if (trip != nullptr) {
553 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700554 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c92016-02-19 20:14:38 -0800555 int64_t stride_value = 0;
556 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800557 if (!is_min && stride_value == 1) {
Aart Bik97412c92016-02-19 20:14:38 -0800558 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800559 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
560 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
561 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700562 trip->induction_class,
563 trip->operation,
564 trip_expr->op_a,
565 trip->op_b,
566 nullptr,
567 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800568 return GetVal(&cancelled_trip, trip, in_body, is_min);
569 }
570 } else if (is_min && stride_value == -1) {
Aart Bik97412c92016-02-19 20:14:38 -0800571 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800572 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
573 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
574 HInductionVarAnalysis::InductionInfo neg(
575 HInductionVarAnalysis::kInvariant,
576 HInductionVarAnalysis::kNeg,
577 nullptr,
578 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700579 nullptr,
580 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800581 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700582 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800583 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
584 }
585 }
586 }
587 }
588 }
589 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
590 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
591 GetVal(info->op_b, trip, in_body, is_min));
592}
593
Aart Bikdf7822e2016-12-06 10:05:30 -0800594InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
595 HInductionVarAnalysis::InductionInfo* trip,
596 bool in_body,
597 bool is_min) const {
598 DCHECK(info != nullptr);
599 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
600 int64_t a = 0;
601 int64_t b = 0;
602 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
603 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800604 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800605 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
606 Value c = GetVal(info->op_b, trip, in_body, is_min);
607 if (is_min) {
608 return c;
609 } else {
610 Value m = GetVal(trip, trip, in_body, is_min);
611 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
612 Value x = MulValue(Value(a), t);
613 Value y = MulValue(Value(b), m);
614 return AddValue(AddValue(x, y), c);
615 }
616 }
617 return Value();
618}
619
Aart Bikc071a012016-12-01 10:22:31 -0800620InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
621 HInductionVarAnalysis::InductionInfo* trip,
622 bool in_body,
623 bool is_min) const {
624 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800625 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800626 int64_t a = 0;
627 int64_t f = 0;
628 if (IsConstant(info->op_a, kExact, &a) &&
629 CanLongValueFitIntoInt(a) &&
Aart Bikdf7822e2016-12-06 10:05:30 -0800630 IsIntAndGet(info->fetch, &f) && f >= 1) {
631 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
632 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800633 const bool is_min_a = a >= 0 ? is_min : !is_min;
634 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800635 Value b = GetVal(info->op_b, trip, in_body, is_min);
636 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800637 }
638 }
639 return Value();
640}
641
Aart Bikd14c5952015-09-08 15:25:15 -0700642InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700643 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700644 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800645 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700646 // Special case when chasing constants: single instruction that denotes trip count in the
647 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
648 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700649 if (is_min) {
650 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800651 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700652 return Value(std::numeric_limits<int32_t>::max());
653 }
654 }
Aart Bik40fbf742016-10-31 11:02:50 -0700655 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
656 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
657 int64_t value;
658 if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
659 // Proper constant reveals best information.
660 return Value(static_cast<int32_t>(value));
661 } else if (instruction == chase_hint_) {
662 // At hint, fetch is represented by itself.
663 return Value(instruction, 1, 0);
664 } else if (instruction->IsAdd()) {
665 // Incorporate suitable constants in the chased value.
Aart Bik97412c92016-02-19 20:14:38 -0800666 if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
667 return AddValue(Value(static_cast<int32_t>(value)),
668 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
669 } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
670 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
671 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700672 }
Aart Bik52be7e72016-06-23 11:20:41 -0700673 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700674 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700675 if (chase_hint_ == nullptr) {
676 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
677 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000678 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700679 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700680 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700681 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800682 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Aart Bik0d345cf2016-03-16 10:49:38 -0700683 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
684 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
685 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
686 }
Aart Bik52be7e72016-06-23 11:20:41 -0700687 }
688 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
689 // so far is well-behaved in both bounds and the next trip-count is safe.
690 // Example:
691 // for (int i = 0; i <= 100; i++) // safe
692 // for (int j = 0; j <= i; j++) // well-behaved
693 // j is in range [0, i ] (if i is chase hint)
694 // or in range [0, 100] (otherwise)
695 HLoopInformation* next_loop = nullptr;
696 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
697 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
698 bool next_in_body = true; // inner loop is always in body of outer loop
699 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
700 IsWellBehavedTripCount(trip) &&
701 !IsUnsafeTripCount(next_trip)) {
702 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700703 }
Aart Bik40fbf742016-10-31 11:02:50 -0700704 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700705 return Value(instruction, 1, 0);
706}
707
Aart Bikcd26feb2015-09-23 17:50:50 -0700708InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
709 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700710 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800711 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700712 if (info != nullptr) {
713 switch (info->induction_class) {
714 case HInductionVarAnalysis::kInvariant:
715 // Invariants.
716 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700717 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700718 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
719 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700720 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700721 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
722 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700723 case HInductionVarAnalysis::kNeg: // second reversed!
724 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700725 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700726 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700727 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700728 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700729 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800730 case HInductionVarAnalysis::kRem:
731 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700732 case HInductionVarAnalysis::kXor:
733 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700734 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700735 return GetFetch(info->fetch, trip, in_body, is_min);
736 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700737 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700738 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700739 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700740 }
741 FALLTHROUGH_INTENDED;
742 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700743 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700744 if (is_min) {
745 return Value(0);
746 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700747 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700748 }
749 break;
750 default:
751 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700752 }
753 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700754 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700755 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800756 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800757 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800758 case HInductionVarAnalysis::kGeometric:
759 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700760 case HInductionVarAnalysis::kWrapAround:
761 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700762 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
763 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700764 }
765 }
Aart Bikb3365e02015-09-21 14:45:05 -0700766 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700767}
768
769InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
770 HInductionVarAnalysis::InductionInfo* info2,
771 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700772 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800773 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700774 // Constant times range.
775 int64_t value = 0;
776 if (IsConstant(info1, kExact, &value)) {
777 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
778 } else if (IsConstant(info2, kExact, &value)) {
779 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
780 }
781 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700782 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
783 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
784 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
785 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c92016-02-19 20:14:38 -0800786 // Positive range vs. positive or negative range.
787 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
788 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
789 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
790 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
791 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700792 }
Aart Bik97412c92016-02-19 20:14:38 -0800793 }
794 // Negative range vs. positive or negative range.
795 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
796 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
797 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
798 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
799 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700800 }
801 }
Aart Bikb3365e02015-09-21 14:45:05 -0700802 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700803}
804
805InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
806 HInductionVarAnalysis::InductionInfo* info2,
807 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700808 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800809 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700810 // Range divided by constant.
811 int64_t value = 0;
812 if (IsConstant(info2, kExact, &value)) {
813 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
814 }
815 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700816 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
817 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
818 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
819 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c92016-02-19 20:14:38 -0800820 // Positive range vs. positive or negative range.
821 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
822 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
823 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
824 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
825 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700826 }
Aart Bik97412c92016-02-19 20:14:38 -0800827 }
828 // Negative range vs. positive or negative range.
829 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
830 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
831 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
832 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
833 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700834 }
835 }
Aart Bikb3365e02015-09-21 14:45:05 -0700836 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700837}
838
Aart Bikdf7822e2016-12-06 10:05:30 -0800839InductionVarRange::Value InductionVarRange::GetRem(
840 HInductionVarAnalysis::InductionInfo* info1,
841 HInductionVarAnalysis::InductionInfo* info2) const {
842 int64_t v1 = 0;
843 int64_t v2 = 0;
844 // Only accept exact values.
845 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
846 int64_t value = v1 % v2;
847 if (CanLongValueFitIntoInt(value)) {
848 return Value(static_cast<int32_t>(value));
849 }
850 }
851 return Value();
852}
853
Aart Bik7dc96932016-10-12 10:01:05 -0700854InductionVarRange::Value InductionVarRange::GetXor(
855 HInductionVarAnalysis::InductionInfo* info1,
856 HInductionVarAnalysis::InductionInfo* info2) const {
857 int64_t v1 = 0;
858 int64_t v2 = 0;
859 // Only accept exact values.
860 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
861 int64_t value = v1 ^ v2;
862 if (CanLongValueFitIntoInt(value)) {
863 return Value(static_cast<int32_t>(value));
864 }
865 }
866 return Value();
867}
868
Aart Bik52be7e72016-06-23 11:20:41 -0700869InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
870 int64_t value,
871 HInductionVarAnalysis::InductionInfo* info,
872 HInductionVarAnalysis::InductionInfo* trip,
873 bool in_body,
874 bool is_min) const {
875 if (CanLongValueFitIntoInt(value)) {
876 Value c(static_cast<int32_t>(value));
877 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
878 }
879 return Value();
Aart Bik97412c92016-02-19 20:14:38 -0800880}
881
Aart Bik52be7e72016-06-23 11:20:41 -0700882InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
883 int64_t value,
884 HInductionVarAnalysis::InductionInfo* info,
885 HInductionVarAnalysis::InductionInfo* trip,
886 bool in_body,
887 bool is_min) const {
888 if (CanLongValueFitIntoInt(value)) {
889 Value c(static_cast<int32_t>(value));
890 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
891 }
892 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700893}
894
Aart Bik7d57d7f2015-12-09 14:39:48 -0800895InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700896 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800897 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700898 if (v1.a_constant == 0) {
899 return Value(v2.instruction, v2.a_constant, b);
900 } else if (v2.a_constant == 0) {
901 return Value(v1.instruction, v1.a_constant, b);
902 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
903 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
904 }
905 }
Aart Bikb3365e02015-09-21 14:45:05 -0700906 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700907}
908
Aart Bik7d57d7f2015-12-09 14:39:48 -0800909InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700910 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800911 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700912 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
913 return Value(v2.instruction, -v2.a_constant, b);
914 } else if (v2.a_constant == 0) {
915 return Value(v1.instruction, v1.a_constant, b);
916 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
917 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
918 }
919 }
Aart Bikb3365e02015-09-21 14:45:05 -0700920 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700921}
922
Aart Bik7d57d7f2015-12-09 14:39:48 -0800923InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700924 if (v1.is_known && v2.is_known) {
925 if (v1.a_constant == 0) {
926 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
927 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
928 }
929 } else if (v2.a_constant == 0) {
930 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
931 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
932 }
Aart Bikd14c5952015-09-08 15:25:15 -0700933 }
934 }
Aart Bikb3365e02015-09-21 14:45:05 -0700935 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700936}
937
Aart Bik7d57d7f2015-12-09 14:39:48 -0800938InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700939 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700940 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
941 return Value(v1.b_constant / v2.b_constant);
942 }
943 }
Aart Bikb3365e02015-09-21 14:45:05 -0700944 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700945}
946
Aart Bik7d57d7f2015-12-09 14:39:48 -0800947InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700948 if (v1.is_known && v2.is_known) {
949 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700950 return Value(v1.instruction, v1.a_constant,
951 is_min ? std::min(v1.b_constant, v2.b_constant)
952 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700953 }
Aart Bikd14c5952015-09-08 15:25:15 -0700954 }
Aart Bikb3365e02015-09-21 14:45:05 -0700955 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700956}
957
Aart Bik9abf8942016-10-14 09:49:42 -0700958bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
959 HInstruction* instruction,
960 bool is_last_value,
961 HGraph* graph,
962 HBasicBlock* block,
963 /*out*/HInstruction** lower,
964 /*out*/HInstruction** upper,
965 /*out*/HInstruction** taken_test,
966 /*out*/int64_t* stride_value,
967 /*out*/bool* needs_finite_test,
968 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700969 HLoopInformation* loop = nullptr;
970 HInductionVarAnalysis::InductionInfo* info = nullptr;
971 HInductionVarAnalysis::InductionInfo* trip = nullptr;
972 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
973 return false; // codegen needs all information, including tripcount
Aart Bik97412c92016-02-19 20:14:38 -0800974 }
975 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
976 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
977 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
978 // code does not use the trip-count explicitly (since there could be an implicit relation
979 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700980 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700981 *stride_value = 0;
982 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c92016-02-19 20:14:38 -0800983 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700984 // Handle last value request.
985 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800986 DCHECK(!in_body);
987 switch (info->induction_class) {
988 case HInductionVarAnalysis::kLinear:
989 if (*stride_value > 0) {
990 lower = nullptr;
991 } else {
992 upper = nullptr;
993 }
994 break;
Aart Bikdf7822e2016-12-06 10:05:30 -0800995 case HInductionVarAnalysis::kPolynomial:
996 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -0800997 case HInductionVarAnalysis::kGeometric:
998 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -0800999 case HInductionVarAnalysis::kWrapAround:
1000 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001001 case HInductionVarAnalysis::kPeriodic:
1002 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1003 default:
1004 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001005 }
1006 }
Aart Bik97412c92016-02-19 20:14:38 -08001007 // Code generation for taken test: generate the code when requested or otherwise analyze
1008 // if code generation is feasible when taken test is needed.
1009 if (taken_test != nullptr) {
1010 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1011 } else if (*needs_taken_test) {
1012 if (!GenerateCode(
1013 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1014 return false;
1015 }
1016 }
1017 // Code generation for lower and upper.
1018 return
1019 // Success on lower if invariant (not set), or code can be generated.
1020 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1021 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1022 // And success on upper.
1023 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001024}
1025
Aart Bikdf7822e2016-12-06 10:05:30 -08001026bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1027 HInductionVarAnalysis::InductionInfo* trip,
1028 HGraph* graph,
1029 HBasicBlock* block,
1030 /*out*/HInstruction** result) const {
1031 DCHECK(info != nullptr);
1032 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1033 // Detect known coefficients and trip count (always taken).
1034 int64_t a = 0;
1035 int64_t b = 0;
1036 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001037 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1038 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001039 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001040 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001041 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001042 HInstruction* c = nullptr;
1043 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001044 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001045 Primitive::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001046 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Aart Bike6bd0272016-12-16 13:57:52 -08001047 if (type != Primitive::kPrimLong) {
1048 sum = static_cast<int32_t>(sum); // okay to truncate
1049 }
1050 *result =
1051 Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001052 }
1053 return true;
1054 }
1055 }
1056 return false;
1057}
1058
Aart Bikc071a012016-12-01 10:22:31 -08001059bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1060 HInductionVarAnalysis::InductionInfo* trip,
1061 HGraph* graph,
1062 HBasicBlock* block,
1063 /*out*/HInstruction** result) const {
1064 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001065 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001066 // Detect known base and trip count (always taken).
1067 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001068 int64_t m = 0;
1069 if (IsIntAndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001070 HInstruction* opa = nullptr;
1071 HInstruction* opb = nullptr;
1072 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1073 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001074 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001075 Primitive::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001076 // Compute f ^ m for known maximum index value m.
1077 bool overflow = false;
1078 int64_t fpow = IntPow(f, m, &overflow);
1079 if (info->operation == HInductionVarAnalysis::kDiv) {
1080 // For division, any overflow truncates to zero.
1081 if (overflow || (type != Primitive::kPrimLong && !CanLongValueFitIntoInt(fpow))) {
1082 fpow = 0;
1083 }
1084 } else if (type != Primitive::kPrimLong) {
1085 // For multiplication, okay to truncate to required precision.
1086 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1087 fpow = static_cast<int32_t>(fpow);
1088 }
1089 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001090 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001091 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001092 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001093 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001094 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001095 HInstruction* e = nullptr;
1096 if (info->operation == HInductionVarAnalysis::kMul) {
1097 e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow));
1098 } else {
1099 e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
1100 }
1101 *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001102 }
1103 }
1104 return true;
1105 }
1106 }
1107 return false;
1108}
1109
Aart Bikdf7822e2016-12-06 10:05:30 -08001110bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1111 HInductionVarAnalysis::InductionInfo* trip,
1112 HGraph* graph,
1113 HBasicBlock* block,
1114 /*out*/HInstruction** result) const {
1115 DCHECK(info != nullptr);
1116 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1117 // Count depth.
1118 int32_t depth = 0;
1119 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1120 info = info->op_b, ++depth) {}
1121 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001122 // TODO: generalize, but be careful to adjust the terminal.
1123 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001124 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001125 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1126 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001127 }
1128 return false;
1129}
1130
Aart Bik9abf8942016-10-14 09:49:42 -07001131bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1132 HInductionVarAnalysis::InductionInfo* trip,
1133 HGraph* graph,
1134 HBasicBlock* block,
1135 /*out*/HInstruction** result,
1136 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001137 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001138 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik9abf8942016-10-14 09:49:42 -07001139 // Count period.
Aart Bike6bd0272016-12-16 13:57:52 -08001140 int64_t period = 1;
Aart Bik9abf8942016-10-14 09:49:42 -07001141 for (HInductionVarAnalysis::InductionInfo* p = info;
1142 p->induction_class == HInductionVarAnalysis::kPeriodic;
1143 p = p->op_b, ++period) {}
Aart Bike6bd0272016-12-16 13:57:52 -08001144 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1145 int64_t m = 0;
1146 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1147 int64_t li = m % period;
1148 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1149 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1150 info = info->op_a;
1151 }
1152 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001153 }
Aart Bike6bd0272016-12-16 13:57:52 -08001154 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1155 // directly to obtain the maximum index value t even if taken test is needed.
1156 HInstruction* x = nullptr;
1157 HInstruction* y = nullptr;
1158 HInstruction* t = nullptr;
1159 if (period == 2 &&
1160 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1161 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1162 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1163 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001164 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001165 Primitive::Type type = trip->type;
1166 HInstruction* msk =
1167 Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1)));
1168 HInstruction* is_even =
1169 Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1170 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001171 }
1172 // Guard select with taken test if needed.
1173 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001174 HInstruction* is_taken = nullptr;
1175 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1176 if (graph != nullptr) {
1177 *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc));
1178 }
1179 *needs_taken_test = false; // taken care of
1180 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001181 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001182 }
Aart Bik9abf8942016-10-14 09:49:42 -07001183 }
1184 return true;
1185 }
1186 return false;
1187}
1188
Aart Bikaec3cce2015-10-14 17:44:55 -07001189bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1190 HInductionVarAnalysis::InductionInfo* trip,
1191 HGraph* graph, // when set, code is generated
1192 HBasicBlock* block,
1193 /*out*/HInstruction** result,
1194 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001195 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001196 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001197 // If during codegen, the result is not needed (nullptr), simply return success.
1198 if (graph != nullptr && result == nullptr) {
1199 return true;
1200 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001201 // Handle current operation.
Aart Bike6bd0272016-12-16 13:57:52 -08001202 Primitive::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001203 HInstruction* opa = nullptr;
1204 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001205 switch (info->induction_class) {
1206 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001207 // Invariants (note that since invariants only have other invariants as
1208 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001209 switch (info->operation) {
1210 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001211 case HInductionVarAnalysis::kSub:
1212 case HInductionVarAnalysis::kMul:
1213 case HInductionVarAnalysis::kDiv:
1214 case HInductionVarAnalysis::kRem:
1215 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001216 case HInductionVarAnalysis::kLT:
1217 case HInductionVarAnalysis::kLE:
1218 case HInductionVarAnalysis::kGT:
1219 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001220 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1221 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1222 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001223 HInstruction* operation = nullptr;
1224 switch (info->operation) {
1225 case HInductionVarAnalysis::kAdd:
1226 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001227 case HInductionVarAnalysis::kSub:
1228 operation = new (graph->GetArena()) HSub(type, opa, opb); break;
1229 case HInductionVarAnalysis::kMul:
1230 operation = new (graph->GetArena()) HMul(type, opa, opb, kNoDexPc); break;
1231 case HInductionVarAnalysis::kDiv:
1232 operation = new (graph->GetArena()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001233 case HInductionVarAnalysis::kRem:
1234 operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001235 case HInductionVarAnalysis::kXor:
1236 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001237 case HInductionVarAnalysis::kLT:
1238 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1239 case HInductionVarAnalysis::kLE:
1240 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1241 case HInductionVarAnalysis::kGT:
1242 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1243 case HInductionVarAnalysis::kGE:
1244 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1245 default:
1246 LOG(FATAL) << "unknown operation";
1247 }
1248 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001249 }
1250 return true;
1251 }
1252 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001253 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001254 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1255 if (graph != nullptr) {
1256 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1257 }
1258 return true;
1259 }
1260 break;
1261 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001262 if (graph != nullptr) {
1263 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001264 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001265 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001266 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001267 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001268 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001269 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001270 }
1271 FALLTHROUGH_INTENDED;
1272 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001273 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001274 if (is_min) {
1275 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001276 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001277 }
1278 return true;
1279 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001280 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001281 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001282 *result =
1283 Insert(block,
1284 new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001285 }
1286 return true;
1287 }
1288 }
1289 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001290 case HInductionVarAnalysis::kNop:
1291 LOG(FATAL) << "unexpected invariant nop";
1292 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001293 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001294 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001295 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1296 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1297 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001298 // known stride yields right value. Always avoid any narrowing linear induction or
1299 // any type mismatch between the linear induction and the trip count expression.
1300 // TODO: careful runtime type conversions could generalize this latter restriction.
1301 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1302 int64_t stride_value = 0;
1303 if (IsConstant(info->op_a, kExact, &stride_value) &&
1304 CanLongValueFitIntoInt(stride_value)) {
1305 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1306 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1307 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1308 if (graph != nullptr) {
1309 HInstruction* oper;
1310 if (stride_value == 1) {
1311 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1312 } else if (stride_value == -1) {
1313 oper = new (graph->GetArena()) HSub(type, opb, opa);
1314 } else {
1315 HInstruction* mul =
1316 new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa);
1317 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
1318 }
1319 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001320 }
Aart Bike6bd0272016-12-16 13:57:52 -08001321 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001322 }
1323 }
1324 }
1325 break;
Aart Bik4a342772015-11-30 10:17:46 -08001326 }
Aart Bikc071a012016-12-01 10:22:31 -08001327 case HInductionVarAnalysis::kPolynomial:
1328 case HInductionVarAnalysis::kGeometric:
1329 break;
Aart Bik4a342772015-11-30 10:17:46 -08001330 case HInductionVarAnalysis::kWrapAround:
1331 case HInductionVarAnalysis::kPeriodic: {
1332 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1333 // values are easy to test at runtime without complications of arithmetic wrap-around.
1334 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c92016-02-19 20:14:38 -08001335 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001336 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001337 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001338 }
1339 return true;
1340 }
1341 break;
1342 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001343 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001344 }
1345 return false;
1346}
1347
Aart Bik16d3a652016-09-09 10:33:50 -07001348void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1349 HInstruction* fetch,
1350 HInstruction* replacement) {
1351 if (info != nullptr) {
1352 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1353 info->operation == HInductionVarAnalysis::kFetch &&
1354 info->fetch == fetch) {
1355 info->fetch = replacement;
1356 }
1357 ReplaceInduction(info->op_a, fetch, replacement);
1358 ReplaceInduction(info->op_b, fetch, replacement);
1359 }
1360}
1361
Aart Bikd14c5952015-09-08 15:25:15 -07001362} // namespace art