blob: 1c8674d5222868452206ed574883f29893d912bc [file] [log] [blame]
Aart Bikd14c5952015-09-08 15:25:15 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Aart Bikd14c5952015-09-08 15:25:15 -070017#include "induction_var_range.h"
18
Aart Bikcd26feb2015-09-23 17:50:50 -070019#include <limits>
20
Aart Bikd14c5952015-09-08 15:25:15 -070021namespace art {
22
Aart Bikb3365e02015-09-21 14:45:05 -070023/** Returns true if 64-bit constant fits in 32-bit constant. */
24static bool CanLongValueFitIntoInt(int64_t c) {
Aart Bikcd26feb2015-09-23 17:50:50 -070025 return std::numeric_limits<int32_t>::min() <= c && c <= std::numeric_limits<int32_t>::max();
Aart Bikd14c5952015-09-08 15:25:15 -070026}
27
Aart Bikb3365e02015-09-21 14:45:05 -070028/** Returns true if 32-bit addition can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070029static bool IsSafeAdd(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070030 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) + static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070031}
32
Aart Bikb3365e02015-09-21 14:45:05 -070033/** Returns true if 32-bit subtraction can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070034static bool IsSafeSub(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070035 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) - static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070036}
37
Aart Bikb3365e02015-09-21 14:45:05 -070038/** Returns true if 32-bit multiplication can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070039static bool IsSafeMul(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070040 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) * static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070041}
42
Aart Bikb3365e02015-09-21 14:45:05 -070043/** Returns true if 32-bit division can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070044static bool IsSafeDiv(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070045 return c2 != 0 && CanLongValueFitIntoInt(static_cast<int64_t>(c1) / static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070046}
47
Aart Bik97412c922016-02-19 20:14:38 -080048/** Returns true for 32/64-bit constant instruction. */
49static bool IsIntAndGet(HInstruction* instruction, int64_t* value) {
Aart Bikd14c5952015-09-08 15:25:15 -070050 if (instruction->IsIntConstant()) {
Aart Bikb3365e02015-09-21 14:45:05 -070051 *value = instruction->AsIntConstant()->GetValue();
52 return true;
Aart Bikd14c5952015-09-08 15:25:15 -070053 } else if (instruction->IsLongConstant()) {
Aart Bik97412c922016-02-19 20:14:38 -080054 *value = instruction->AsLongConstant()->GetValue();
55 return true;
Aart Bikd14c5952015-09-08 15:25:15 -070056 }
57 return false;
58}
59
Aart Bikb603a5c2017-03-06 18:29:39 -080060/** Computes a * b for a,b > 0 (at least until first overflow happens). */
61static int64_t SafeMul(int64_t a, int64_t b, /*out*/ bool* overflow) {
62 if (a > 0 && b > 0 && a > (std::numeric_limits<int64_t>::max() / b)) {
63 *overflow = true;
64 }
65 return a * b;
66}
67
68/** Returns b^e for b,e > 0. Sets overflow if arithmetic wrap-around occurred. */
Aart Bikd3ba6262017-01-30 14:37:12 -080069static int64_t IntPow(int64_t b, int64_t e, /*out*/ bool* overflow) {
Aart Bikb603a5c2017-03-06 18:29:39 -080070 DCHECK_LT(0, b);
71 DCHECK_LT(0, e);
Aart Bikc071a012016-12-01 10:22:31 -080072 int64_t pow = 1;
73 while (e) {
74 if (e & 1) {
Aart Bikb603a5c2017-03-06 18:29:39 -080075 pow = SafeMul(pow, b, overflow);
Aart Bikc071a012016-12-01 10:22:31 -080076 }
77 e >>= 1;
Aart Bikb603a5c2017-03-06 18:29:39 -080078 if (e) {
79 b = SafeMul(b, b, overflow);
80 }
Aart Bikc071a012016-12-01 10:22:31 -080081 }
82 return pow;
83}
84
Aart Bikb3365e02015-09-21 14:45:05 -070085/**
Aart Bik40fbf742016-10-31 11:02:50 -070086 * Detects an instruction that is >= 0. As long as the value is carried by
87 * a single instruction, arithmetic wrap-around cannot occur.
Aart Bikb3365e02015-09-21 14:45:05 -070088 */
Aart Bik40fbf742016-10-31 11:02:50 -070089static bool IsGEZero(HInstruction* instruction) {
90 DCHECK(instruction != nullptr);
91 if (instruction->IsArrayLength()) {
92 return true;
93 } else if (instruction->IsInvokeStaticOrDirect()) {
94 switch (instruction->AsInvoke()->GetIntrinsic()) {
95 case Intrinsics::kMathMinIntInt:
96 case Intrinsics::kMathMinLongLong:
97 // Instruction MIN(>=0, >=0) is >= 0.
98 return IsGEZero(instruction->InputAt(0)) &&
99 IsGEZero(instruction->InputAt(1));
100 case Intrinsics::kMathAbsInt:
101 case Intrinsics::kMathAbsLong:
102 // Instruction ABS(x) is >= 0.
103 return true;
104 default:
105 break;
106 }
107 }
108 int64_t value = -1;
109 return IsIntAndGet(instruction, &value) && value >= 0;
110}
111
112/** Hunts "under the hood" for a suitable instruction at the hint. */
113static bool IsMaxAtHint(
114 HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) {
115 if (instruction->IsInvokeStaticOrDirect()) {
116 switch (instruction->AsInvoke()->GetIntrinsic()) {
117 case Intrinsics::kMathMinIntInt:
118 case Intrinsics::kMathMinLongLong:
119 // For MIN(x, y), return most suitable x or y as maximum.
120 return IsMaxAtHint(instruction->InputAt(0), hint, suitable) ||
121 IsMaxAtHint(instruction->InputAt(1), hint, suitable);
122 default:
123 break;
124 }
125 } else {
126 *suitable = instruction;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000127 return HuntForDeclaration(instruction) == hint;
Aart Bik40fbf742016-10-31 11:02:50 -0700128 }
129 return false;
130}
131
132/** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */
133static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) {
134 if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) {
135 // If a == 1, instruction >= 0 and b <= 0, just return the constant b.
136 // No arithmetic wrap-around can occur.
137 if (IsGEZero(v.instruction)) {
138 return InductionVarRange::Value(v.b_constant);
139 }
Aart Bikb3365e02015-09-21 14:45:05 -0700140 }
141 return v;
142}
143
Aart Bik40fbf742016-10-31 11:02:50 -0700144/** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */
145static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) {
146 if (v.is_known && v.a_constant >= 1) {
147 // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as
148 // length + b because length >= 0 is true.
149 int64_t value;
150 if (v.instruction->IsDiv() &&
151 v.instruction->InputAt(0)->IsArrayLength() &&
152 IsIntAndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
153 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
154 }
155 // If a == 1, the most suitable one suffices as maximum value.
156 HInstruction* suitable = nullptr;
157 if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) {
158 return InductionVarRange::Value(suitable, 1, v.b_constant);
159 }
160 }
161 return v;
162}
163
164/** Tests for a constant value. */
Aart Bik52be7e72016-06-23 11:20:41 -0700165static bool IsConstantValue(InductionVarRange::Value v) {
166 return v.is_known && v.a_constant == 0;
167}
168
169/** Corrects a value for type to account for arithmetic wrap-around in lower precision. */
Aart Bik0d345cf2016-03-16 10:49:38 -0700170static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) {
171 switch (type) {
172 case Primitive::kPrimShort:
173 case Primitive::kPrimChar:
174 case Primitive::kPrimByte: {
175 // Constants within range only.
176 // TODO: maybe some room for improvement, like allowing widening conversions
Aart Bike6bd0272016-12-16 13:57:52 -0800177 int32_t min = Primitive::MinValueOfIntegralType(type);
178 int32_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik52be7e72016-06-23 11:20:41 -0700179 return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max)
Aart Bik0d345cf2016-03-16 10:49:38 -0700180 ? v
181 : InductionVarRange::Value();
182 }
183 default:
Aart Bik0d345cf2016-03-16 10:49:38 -0700184 return v;
185 }
186}
187
Aart Bik40fbf742016-10-31 11:02:50 -0700188/** Inserts an instruction. */
Aart Bik389b3db2015-10-28 14:23:40 -0700189static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
190 DCHECK(block != nullptr);
191 DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId();
Aart Bikaec3cce2015-10-14 17:44:55 -0700192 DCHECK(instruction != nullptr);
Aart Bik389b3db2015-10-28 14:23:40 -0700193 block->InsertInstructionBefore(instruction, block->GetLastInstruction());
Aart Bikaec3cce2015-10-14 17:44:55 -0700194 return instruction;
195}
196
Aart Bik40fbf742016-10-31 11:02:50 -0700197/** Obtains loop's control instruction. */
Aart Bik009cace2016-09-16 10:15:19 -0700198static HInstruction* GetLoopControl(HLoopInformation* loop) {
199 DCHECK(loop != nullptr);
200 return loop->GetHeader()->GetLastInstruction();
201}
202
Aart Bikd14c5952015-09-08 15:25:15 -0700203//
204// Public class methods.
205//
206
207InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis)
Aart Bik52be7e72016-06-23 11:20:41 -0700208 : induction_analysis_(induction_analysis),
209 chase_hint_(nullptr) {
Aart Bikb3365e02015-09-21 14:45:05 -0700210 DCHECK(induction_analysis != nullptr);
Aart Bikd14c5952015-09-08 15:25:15 -0700211}
212
Aart Bik1fc3afb2016-02-02 13:26:16 -0800213bool InductionVarRange::GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -0700214 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -0700215 HInstruction* chase_hint,
Aart Bik389b3db2015-10-28 14:23:40 -0700216 /*out*/Value* min_val,
217 /*out*/Value* max_val,
218 /*out*/bool* needs_finite_test) {
Aart Bik52be7e72016-06-23 11:20:41 -0700219 HLoopInformation* loop = nullptr;
220 HInductionVarAnalysis::InductionInfo* info = nullptr;
221 HInductionVarAnalysis::InductionInfo* trip = nullptr;
222 if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) {
223 return false;
Aart Bik97412c922016-02-19 20:14:38 -0800224 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700225 // Type int or lower (this is not too restrictive since intended clients, like
226 // bounds check elimination, will have truncated higher precision induction
227 // at their use point already).
228 switch (info->type) {
229 case Primitive::kPrimInt:
230 case Primitive::kPrimShort:
231 case Primitive::kPrimChar:
232 case Primitive::kPrimByte:
233 break;
234 default:
235 return false;
236 }
Aart Bik97412c922016-02-19 20:14:38 -0800237 // Find range.
Aart Bik52be7e72016-06-23 11:20:41 -0700238 chase_hint_ = chase_hint;
239 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700240 int64_t stride_value = 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700241 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
242 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint);
Aart Bik16d3a652016-09-09 10:33:50 -0700243 *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip);
Aart Bik40fbf742016-10-31 11:02:50 -0700244 chase_hint_ = nullptr;
245 // Retry chasing constants for wrap-around (merge sensitive).
246 if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) {
247 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
248 }
Aart Bik97412c922016-02-19 20:14:38 -0800249 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700250}
251
Aart Bik16d3a652016-09-09 10:33:50 -0700252bool InductionVarRange::CanGenerateRange(HInstruction* context,
253 HInstruction* instruction,
254 /*out*/bool* needs_finite_test,
255 /*out*/bool* needs_taken_test) {
256 bool is_last_value = false;
257 int64_t stride_value = 0;
Aart Bik9abf8942016-10-14 09:49:42 -0700258 return GenerateRangeOrLastValue(context,
259 instruction,
260 is_last_value,
261 nullptr,
262 nullptr,
263 nullptr,
264 nullptr,
265 nullptr, // nothing generated yet
266 &stride_value,
267 needs_finite_test,
268 needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700269 && (stride_value == -1 ||
270 stride_value == 0 ||
Aart Bik40fbf742016-10-31 11:02:50 -0700271 stride_value == 1); // avoid arithmetic wrap-around anomalies.
Aart Bikaec3cce2015-10-14 17:44:55 -0700272}
273
Aart Bik16d3a652016-09-09 10:33:50 -0700274void InductionVarRange::GenerateRange(HInstruction* context,
275 HInstruction* instruction,
276 HGraph* graph,
277 HBasicBlock* block,
278 /*out*/HInstruction** lower,
279 /*out*/HInstruction** upper) {
280 bool is_last_value = false;
Aart Bik009cace2016-09-16 10:15:19 -0700281 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700282 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700283 if (!GenerateRangeOrLastValue(context,
284 instruction,
285 is_last_value,
286 graph,
287 block,
288 lower,
289 upper,
290 nullptr,
291 &stride_value,
292 &b1,
293 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700294 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
Aart Bik389b3db2015-10-28 14:23:40 -0700295 }
296}
297
Aart Bik16d3a652016-09-09 10:33:50 -0700298HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context,
299 HGraph* graph,
300 HBasicBlock* block) {
301 HInstruction* taken_test = nullptr;
302 bool is_last_value = false;
303 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700304 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700305 if (!GenerateRangeOrLastValue(context,
306 context,
307 is_last_value,
308 graph,
309 block,
310 nullptr,
311 nullptr,
312 &taken_test,
313 &stride_value,
314 &b1,
315 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700316 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
317 }
318 return taken_test;
319}
320
321bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) {
322 bool is_last_value = true;
323 int64_t stride_value = 0;
324 bool needs_finite_test = false;
325 bool needs_taken_test = false;
Aart Bik9abf8942016-10-14 09:49:42 -0700326 return GenerateRangeOrLastValue(instruction,
327 instruction,
328 is_last_value,
329 nullptr,
330 nullptr,
331 nullptr,
332 nullptr,
333 nullptr, // nothing generated yet
334 &stride_value,
335 &needs_finite_test,
336 &needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700337 && !needs_finite_test && !needs_taken_test;
338}
339
340HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction,
341 HGraph* graph,
342 HBasicBlock* block) {
343 HInstruction* last_value = nullptr;
344 bool is_last_value = true;
345 int64_t stride_value = 0;
346 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700347 if (!GenerateRangeOrLastValue(instruction,
348 instruction,
349 is_last_value,
350 graph,
351 block,
352 &last_value,
353 &last_value,
354 nullptr,
355 &stride_value,
356 &b1,
357 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700358 LOG(FATAL) << "Failed precondition: CanGenerateLastValue()";
359 }
360 return last_value;
361}
362
363void InductionVarRange::Replace(HInstruction* instruction,
364 HInstruction* fetch,
365 HInstruction* replacement) {
366 for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
367 lp != nullptr;
368 lp = lp->GetPreHeader()->GetLoopInformation()) {
Aart Bik009cace2016-09-16 10:15:19 -0700369 // Update instruction's information.
Aart Bik16d3a652016-09-09 10:33:50 -0700370 ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement);
Aart Bik009cace2016-09-16 10:15:19 -0700371 // Update loop's trip-count information.
372 ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement);
Aart Bik389b3db2015-10-28 14:23:40 -0700373 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700374}
375
Aart Bik6b69e0a2017-01-11 10:20:43 -0800376bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const {
Aart Bik9abf8942016-10-14 09:49:42 -0700377 HInductionVarAnalysis::InductionInfo *trip =
378 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
Aart Bik6b69e0a2017-01-11 10:20:43 -0800379 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
380 IsConstant(trip->op_a, kExact, tc);
381 return true;
382 }
383 return false;
Aart Bik9abf8942016-10-14 09:49:42 -0700384}
385
Aart Bikfa762962017-04-07 11:33:37 -0700386bool InductionVarRange::IsUnitStride(HInstruction* context,
387 HInstruction* instruction,
Aart Bik8e02e3e2017-02-28 14:41:55 -0800388 /*out*/ HInstruction** offset) const {
389 HLoopInformation* loop = nullptr;
390 HInductionVarAnalysis::InductionInfo* info = nullptr;
391 HInductionVarAnalysis::InductionInfo* trip = nullptr;
Aart Bikfa762962017-04-07 11:33:37 -0700392 if (HasInductionInfo(context, instruction, &loop, &info, &trip)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800393 if (info->induction_class == HInductionVarAnalysis::kLinear &&
Aart Bik7adb6882017-03-07 13:28:51 -0800394 info->op_b->operation == HInductionVarAnalysis::kFetch &&
395 !HInductionVarAnalysis::IsNarrowingLinear(info)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800396 int64_t stride_value = 0;
397 if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) {
398 int64_t off_value = 0;
399 if (IsConstant(info->op_b, kExact, &off_value) && off_value == 0) {
400 *offset = nullptr;
401 } else {
402 *offset = info->op_b->fetch;
403 }
404 return true;
405 }
406 }
407 }
408 return false;
409}
410
411HInstruction* InductionVarRange::GenerateTripCount(HLoopInformation* loop,
412 HGraph* graph,
413 HBasicBlock* block) {
414 HInductionVarAnalysis::InductionInfo *trip =
415 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
416 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
417 HInstruction* taken_test = nullptr;
418 HInstruction* trip_expr = nullptr;
419 if (IsBodyTripCount(trip)) {
420 if (!GenerateCode(trip->op_b, nullptr, graph, block, &taken_test, false, false)) {
421 return nullptr;
422 }
423 }
424 if (GenerateCode(trip->op_a, nullptr, graph, block, &trip_expr, false, false)) {
425 if (taken_test != nullptr) {
426 HInstruction* zero = graph->GetConstant(trip->type, 0);
427 trip_expr = Insert(block, new (graph->GetArena()) HSelect(taken_test, trip_expr, zero, kNoDexPc));
428 }
429 return trip_expr;
430 }
431 }
432 return nullptr;
433}
434
Aart Bikd14c5952015-09-08 15:25:15 -0700435//
436// Private class methods.
437//
438
Aart Bik97412c922016-02-19 20:14:38 -0800439bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
440 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700441 /*out*/ int64_t* value) const {
Aart Bik97412c922016-02-19 20:14:38 -0800442 if (info != nullptr) {
443 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
444 // any of the three requests (kExact, kAtMost, and KAtLeast).
445 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
446 info->operation == HInductionVarAnalysis::kFetch) {
447 if (IsIntAndGet(info->fetch, value)) {
448 return true;
449 }
450 }
Aart Bik40fbf742016-10-31 11:02:50 -0700451 // Try range analysis on the invariant, only accept a proper range
452 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700453 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
454 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
455 if (IsConstantValue(min_val) &&
456 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
457 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
458 *value = max_val.b_constant;
459 return true;
460 } else if (request == kAtLeast) {
461 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800462 return true;
463 }
464 }
Aart Bik97412c922016-02-19 20:14:38 -0800465 }
466 return false;
467}
468
Aart Bik52be7e72016-06-23 11:20:41 -0700469bool InductionVarRange::HasInductionInfo(
470 HInstruction* context,
471 HInstruction* instruction,
472 /*out*/ HLoopInformation** loop,
473 /*out*/ HInductionVarAnalysis::InductionInfo** info,
474 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800475 DCHECK(context != nullptr);
476 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700477 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
478 if (lp != nullptr) {
479 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700480 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700481 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700482 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700483 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700484 return true;
485 }
486 }
487 return false;
488}
489
490bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
491 if (trip != nullptr) {
492 // Both bounds that define a trip-count are well-behaved if they either are not defined
493 // in any loop, or are contained in a proper interval. This allows finding the min/max
494 // of an expression by chasing outward.
495 InductionVarRange range(induction_analysis_);
496 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
497 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
498 int64_t not_used = 0;
499 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
500 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
501 }
502 return true;
503}
504
505bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
506 if (info != nullptr) {
507 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
508 info->operation == HInductionVarAnalysis::kFetch) {
509 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
510 }
511 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
512 }
513 return false;
514}
515
Aart Bik16d3a652016-09-09 10:33:50 -0700516bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
517 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700518 if (info != nullptr) {
519 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700520 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800521 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
522 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700523 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700524 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700525 }
Aart Bikd14c5952015-09-08 15:25:15 -0700526 }
Aart Bik389b3db2015-10-28 14:23:40 -0700527 return false;
528}
529
Aart Bik7d57d7f2015-12-09 14:39:48 -0800530bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700531 if (trip != nullptr) {
532 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
533 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
534 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
535 }
536 }
537 return false;
538}
539
Aart Bik7d57d7f2015-12-09 14:39:48 -0800540bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700541 if (trip != nullptr) {
542 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
543 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
544 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
545 }
546 }
547 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700548}
549
Aart Bik7d57d7f2015-12-09 14:39:48 -0800550InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
551 HInductionVarAnalysis::InductionInfo* trip,
552 bool in_body,
553 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800554 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800555 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700556 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800557 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
558 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
559 // with intermediate results that only incorporate single instructions.
560 if (trip != nullptr) {
561 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700562 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800563 int64_t stride_value = 0;
564 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800565 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800566 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800567 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
568 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
569 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700570 trip->induction_class,
571 trip->operation,
572 trip_expr->op_a,
573 trip->op_b,
574 nullptr,
575 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800576 return GetVal(&cancelled_trip, trip, in_body, is_min);
577 }
578 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800579 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800580 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
581 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
582 HInductionVarAnalysis::InductionInfo neg(
583 HInductionVarAnalysis::kInvariant,
584 HInductionVarAnalysis::kNeg,
585 nullptr,
586 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700587 nullptr,
588 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800589 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700590 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800591 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
592 }
593 }
594 }
595 }
596 }
597 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
598 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
599 GetVal(info->op_b, trip, in_body, is_min));
600}
601
Aart Bikdf7822e2016-12-06 10:05:30 -0800602InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
603 HInductionVarAnalysis::InductionInfo* trip,
604 bool in_body,
605 bool is_min) const {
606 DCHECK(info != nullptr);
607 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
608 int64_t a = 0;
609 int64_t b = 0;
610 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
611 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800612 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800613 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
614 Value c = GetVal(info->op_b, trip, in_body, is_min);
615 if (is_min) {
616 return c;
617 } else {
618 Value m = GetVal(trip, trip, in_body, is_min);
619 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
620 Value x = MulValue(Value(a), t);
621 Value y = MulValue(Value(b), m);
622 return AddValue(AddValue(x, y), c);
623 }
624 }
625 return Value();
626}
627
Aart Bikc071a012016-12-01 10:22:31 -0800628InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
629 HInductionVarAnalysis::InductionInfo* trip,
630 bool in_body,
631 bool is_min) const {
632 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800633 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800634 int64_t a = 0;
635 int64_t f = 0;
636 if (IsConstant(info->op_a, kExact, &a) &&
637 CanLongValueFitIntoInt(a) &&
Aart Bikdf7822e2016-12-06 10:05:30 -0800638 IsIntAndGet(info->fetch, &f) && f >= 1) {
639 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
640 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800641 const bool is_min_a = a >= 0 ? is_min : !is_min;
642 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800643 Value b = GetVal(info->op_b, trip, in_body, is_min);
644 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800645 }
646 }
647 return Value();
648}
649
Aart Bikd14c5952015-09-08 15:25:15 -0700650InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700651 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700652 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800653 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700654 // Special case when chasing constants: single instruction that denotes trip count in the
655 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
656 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700657 if (is_min) {
658 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800659 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700660 return Value(std::numeric_limits<int32_t>::max());
661 }
662 }
Aart Bik40fbf742016-10-31 11:02:50 -0700663 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
664 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
665 int64_t value;
666 if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
667 // Proper constant reveals best information.
668 return Value(static_cast<int32_t>(value));
669 } else if (instruction == chase_hint_) {
670 // At hint, fetch is represented by itself.
671 return Value(instruction, 1, 0);
672 } else if (instruction->IsAdd()) {
673 // Incorporate suitable constants in the chased value.
Aart Bik97412c922016-02-19 20:14:38 -0800674 if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
675 return AddValue(Value(static_cast<int32_t>(value)),
676 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
677 } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
678 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
679 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700680 }
Aart Bik52be7e72016-06-23 11:20:41 -0700681 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700682 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700683 if (chase_hint_ == nullptr) {
684 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
685 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000686 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700687 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700688 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700689 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800690 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Aart Bik0d345cf2016-03-16 10:49:38 -0700691 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
692 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
693 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
694 }
Aart Bik52be7e72016-06-23 11:20:41 -0700695 }
696 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
697 // so far is well-behaved in both bounds and the next trip-count is safe.
698 // Example:
699 // for (int i = 0; i <= 100; i++) // safe
700 // for (int j = 0; j <= i; j++) // well-behaved
701 // j is in range [0, i ] (if i is chase hint)
702 // or in range [0, 100] (otherwise)
703 HLoopInformation* next_loop = nullptr;
704 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
705 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
706 bool next_in_body = true; // inner loop is always in body of outer loop
707 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
708 IsWellBehavedTripCount(trip) &&
709 !IsUnsafeTripCount(next_trip)) {
710 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700711 }
Aart Bik40fbf742016-10-31 11:02:50 -0700712 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700713 return Value(instruction, 1, 0);
714}
715
Aart Bikcd26feb2015-09-23 17:50:50 -0700716InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
717 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700718 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800719 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700720 if (info != nullptr) {
721 switch (info->induction_class) {
722 case HInductionVarAnalysis::kInvariant:
723 // Invariants.
724 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700725 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700726 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
727 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700728 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700729 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
730 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700731 case HInductionVarAnalysis::kNeg: // second reversed!
732 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700733 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700734 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700735 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700736 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700737 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800738 case HInductionVarAnalysis::kRem:
739 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700740 case HInductionVarAnalysis::kXor:
741 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700742 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700743 return GetFetch(info->fetch, trip, in_body, is_min);
744 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700745 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700746 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700747 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700748 }
749 FALLTHROUGH_INTENDED;
750 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700751 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700752 if (is_min) {
753 return Value(0);
754 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700755 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700756 }
757 break;
758 default:
759 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700760 }
761 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700762 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700763 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800764 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800765 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800766 case HInductionVarAnalysis::kGeometric:
767 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700768 case HInductionVarAnalysis::kWrapAround:
769 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700770 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
771 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700772 }
773 }
Aart Bikb3365e02015-09-21 14:45:05 -0700774 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700775}
776
777InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
778 HInductionVarAnalysis::InductionInfo* info2,
779 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700780 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800781 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700782 // Constant times range.
783 int64_t value = 0;
784 if (IsConstant(info1, kExact, &value)) {
785 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
786 } else if (IsConstant(info2, kExact, &value)) {
787 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
788 }
789 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700790 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
791 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
792 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
793 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800794 // Positive range vs. positive or negative range.
795 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
796 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
797 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
798 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
799 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700800 }
Aart Bik97412c922016-02-19 20:14:38 -0800801 }
802 // Negative range vs. positive or negative range.
803 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
804 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
805 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
806 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
807 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700808 }
809 }
Aart Bikb3365e02015-09-21 14:45:05 -0700810 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700811}
812
813InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
814 HInductionVarAnalysis::InductionInfo* info2,
815 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700816 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800817 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700818 // Range divided by constant.
819 int64_t value = 0;
820 if (IsConstant(info2, kExact, &value)) {
821 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
822 }
823 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700824 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
825 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
826 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
827 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800828 // Positive range vs. positive or negative range.
829 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
830 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
831 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
832 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
833 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700834 }
Aart Bik97412c922016-02-19 20:14:38 -0800835 }
836 // Negative range vs. positive or negative range.
837 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
838 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
839 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
840 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
841 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700842 }
843 }
Aart Bikb3365e02015-09-21 14:45:05 -0700844 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700845}
846
Aart Bikdf7822e2016-12-06 10:05:30 -0800847InductionVarRange::Value InductionVarRange::GetRem(
848 HInductionVarAnalysis::InductionInfo* info1,
849 HInductionVarAnalysis::InductionInfo* info2) const {
850 int64_t v1 = 0;
851 int64_t v2 = 0;
852 // Only accept exact values.
853 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
854 int64_t value = v1 % v2;
855 if (CanLongValueFitIntoInt(value)) {
856 return Value(static_cast<int32_t>(value));
857 }
858 }
859 return Value();
860}
861
Aart Bik7dc96932016-10-12 10:01:05 -0700862InductionVarRange::Value InductionVarRange::GetXor(
863 HInductionVarAnalysis::InductionInfo* info1,
864 HInductionVarAnalysis::InductionInfo* info2) const {
865 int64_t v1 = 0;
866 int64_t v2 = 0;
867 // Only accept exact values.
868 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
869 int64_t value = v1 ^ v2;
870 if (CanLongValueFitIntoInt(value)) {
871 return Value(static_cast<int32_t>(value));
872 }
873 }
874 return Value();
875}
876
Aart Bik52be7e72016-06-23 11:20:41 -0700877InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
878 int64_t value,
879 HInductionVarAnalysis::InductionInfo* info,
880 HInductionVarAnalysis::InductionInfo* trip,
881 bool in_body,
882 bool is_min) const {
883 if (CanLongValueFitIntoInt(value)) {
884 Value c(static_cast<int32_t>(value));
885 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
886 }
887 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800888}
889
Aart Bik52be7e72016-06-23 11:20:41 -0700890InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
891 int64_t value,
892 HInductionVarAnalysis::InductionInfo* info,
893 HInductionVarAnalysis::InductionInfo* trip,
894 bool in_body,
895 bool is_min) const {
896 if (CanLongValueFitIntoInt(value)) {
897 Value c(static_cast<int32_t>(value));
898 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
899 }
900 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700901}
902
Aart Bik7d57d7f2015-12-09 14:39:48 -0800903InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700904 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800905 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700906 if (v1.a_constant == 0) {
907 return Value(v2.instruction, v2.a_constant, b);
908 } else if (v2.a_constant == 0) {
909 return Value(v1.instruction, v1.a_constant, b);
910 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
911 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
912 }
913 }
Aart Bikb3365e02015-09-21 14:45:05 -0700914 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700915}
916
Aart Bik7d57d7f2015-12-09 14:39:48 -0800917InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700918 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800919 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700920 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
921 return Value(v2.instruction, -v2.a_constant, b);
922 } else if (v2.a_constant == 0) {
923 return Value(v1.instruction, v1.a_constant, b);
924 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
925 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
926 }
927 }
Aart Bikb3365e02015-09-21 14:45:05 -0700928 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700929}
930
Aart Bik7d57d7f2015-12-09 14:39:48 -0800931InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700932 if (v1.is_known && v2.is_known) {
933 if (v1.a_constant == 0) {
934 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
935 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
936 }
937 } else if (v2.a_constant == 0) {
938 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
939 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
940 }
Aart Bikd14c5952015-09-08 15:25:15 -0700941 }
942 }
Aart Bikb3365e02015-09-21 14:45:05 -0700943 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700944}
945
Aart Bik7d57d7f2015-12-09 14:39:48 -0800946InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700947 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700948 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
949 return Value(v1.b_constant / v2.b_constant);
950 }
951 }
Aart Bikb3365e02015-09-21 14:45:05 -0700952 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700953}
954
Aart Bik7d57d7f2015-12-09 14:39:48 -0800955InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700956 if (v1.is_known && v2.is_known) {
957 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700958 return Value(v1.instruction, v1.a_constant,
959 is_min ? std::min(v1.b_constant, v2.b_constant)
960 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700961 }
Aart Bikd14c5952015-09-08 15:25:15 -0700962 }
Aart Bikb3365e02015-09-21 14:45:05 -0700963 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700964}
965
Aart Bik9abf8942016-10-14 09:49:42 -0700966bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
967 HInstruction* instruction,
968 bool is_last_value,
969 HGraph* graph,
970 HBasicBlock* block,
971 /*out*/HInstruction** lower,
972 /*out*/HInstruction** upper,
973 /*out*/HInstruction** taken_test,
974 /*out*/int64_t* stride_value,
975 /*out*/bool* needs_finite_test,
976 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700977 HLoopInformation* loop = nullptr;
978 HInductionVarAnalysis::InductionInfo* info = nullptr;
979 HInductionVarAnalysis::InductionInfo* trip = nullptr;
980 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
981 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800982 }
983 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
984 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
985 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
986 // code does not use the trip-count explicitly (since there could be an implicit relation
987 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700988 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700989 *stride_value = 0;
990 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800991 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700992 // Handle last value request.
993 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800994 DCHECK(!in_body);
995 switch (info->induction_class) {
996 case HInductionVarAnalysis::kLinear:
997 if (*stride_value > 0) {
998 lower = nullptr;
999 } else {
1000 upper = nullptr;
1001 }
1002 break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001003 case HInductionVarAnalysis::kPolynomial:
1004 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001005 case HInductionVarAnalysis::kGeometric:
1006 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -08001007 case HInductionVarAnalysis::kWrapAround:
1008 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001009 case HInductionVarAnalysis::kPeriodic:
1010 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1011 default:
1012 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001013 }
1014 }
Aart Bik97412c922016-02-19 20:14:38 -08001015 // Code generation for taken test: generate the code when requested or otherwise analyze
1016 // if code generation is feasible when taken test is needed.
1017 if (taken_test != nullptr) {
1018 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1019 } else if (*needs_taken_test) {
1020 if (!GenerateCode(
1021 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1022 return false;
1023 }
1024 }
1025 // Code generation for lower and upper.
1026 return
1027 // Success on lower if invariant (not set), or code can be generated.
1028 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1029 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1030 // And success on upper.
1031 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001032}
1033
Aart Bikdf7822e2016-12-06 10:05:30 -08001034bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1035 HInductionVarAnalysis::InductionInfo* trip,
1036 HGraph* graph,
1037 HBasicBlock* block,
1038 /*out*/HInstruction** result) const {
1039 DCHECK(info != nullptr);
1040 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1041 // Detect known coefficients and trip count (always taken).
1042 int64_t a = 0;
1043 int64_t b = 0;
1044 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001045 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1046 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001047 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001048 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001049 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001050 HInstruction* c = nullptr;
1051 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001052 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001053 Primitive::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001054 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Aart Bike6bd0272016-12-16 13:57:52 -08001055 if (type != Primitive::kPrimLong) {
1056 sum = static_cast<int32_t>(sum); // okay to truncate
1057 }
1058 *result =
1059 Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001060 }
1061 return true;
1062 }
1063 }
1064 return false;
1065}
1066
Aart Bikc071a012016-12-01 10:22:31 -08001067bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1068 HInductionVarAnalysis::InductionInfo* trip,
1069 HGraph* graph,
1070 HBasicBlock* block,
1071 /*out*/HInstruction** result) const {
1072 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001073 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001074 // Detect known base and trip count (always taken).
1075 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001076 int64_t m = 0;
1077 if (IsIntAndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001078 HInstruction* opa = nullptr;
1079 HInstruction* opb = nullptr;
1080 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1081 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001082 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001083 Primitive::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001084 // Compute f ^ m for known maximum index value m.
1085 bool overflow = false;
1086 int64_t fpow = IntPow(f, m, &overflow);
1087 if (info->operation == HInductionVarAnalysis::kDiv) {
1088 // For division, any overflow truncates to zero.
1089 if (overflow || (type != Primitive::kPrimLong && !CanLongValueFitIntoInt(fpow))) {
1090 fpow = 0;
1091 }
1092 } else if (type != Primitive::kPrimLong) {
1093 // For multiplication, okay to truncate to required precision.
1094 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1095 fpow = static_cast<int32_t>(fpow);
1096 }
1097 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001098 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001099 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001100 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001101 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001102 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001103 HInstruction* e = nullptr;
1104 if (info->operation == HInductionVarAnalysis::kMul) {
1105 e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow));
1106 } else {
1107 e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
1108 }
1109 *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001110 }
1111 }
1112 return true;
1113 }
1114 }
1115 return false;
1116}
1117
Aart Bikdf7822e2016-12-06 10:05:30 -08001118bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1119 HInductionVarAnalysis::InductionInfo* trip,
1120 HGraph* graph,
1121 HBasicBlock* block,
1122 /*out*/HInstruction** result) const {
1123 DCHECK(info != nullptr);
1124 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1125 // Count depth.
1126 int32_t depth = 0;
1127 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1128 info = info->op_b, ++depth) {}
1129 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001130 // TODO: generalize, but be careful to adjust the terminal.
1131 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001132 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001133 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1134 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001135 }
1136 return false;
1137}
1138
Aart Bik9abf8942016-10-14 09:49:42 -07001139bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1140 HInductionVarAnalysis::InductionInfo* trip,
1141 HGraph* graph,
1142 HBasicBlock* block,
1143 /*out*/HInstruction** result,
1144 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001145 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001146 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik9abf8942016-10-14 09:49:42 -07001147 // Count period.
Aart Bike6bd0272016-12-16 13:57:52 -08001148 int64_t period = 1;
Aart Bik9abf8942016-10-14 09:49:42 -07001149 for (HInductionVarAnalysis::InductionInfo* p = info;
1150 p->induction_class == HInductionVarAnalysis::kPeriodic;
1151 p = p->op_b, ++period) {}
Aart Bike6bd0272016-12-16 13:57:52 -08001152 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1153 int64_t m = 0;
1154 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1155 int64_t li = m % period;
1156 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1157 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1158 info = info->op_a;
1159 }
1160 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001161 }
Aart Bike6bd0272016-12-16 13:57:52 -08001162 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1163 // directly to obtain the maximum index value t even if taken test is needed.
1164 HInstruction* x = nullptr;
1165 HInstruction* y = nullptr;
1166 HInstruction* t = nullptr;
1167 if (period == 2 &&
1168 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1169 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1170 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1171 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001172 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001173 Primitive::Type type = trip->type;
1174 HInstruction* msk =
1175 Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1)));
1176 HInstruction* is_even =
1177 Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1178 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001179 }
1180 // Guard select with taken test if needed.
1181 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001182 HInstruction* is_taken = nullptr;
1183 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1184 if (graph != nullptr) {
1185 *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc));
1186 }
1187 *needs_taken_test = false; // taken care of
1188 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001189 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001190 }
Aart Bik9abf8942016-10-14 09:49:42 -07001191 }
1192 return true;
1193 }
1194 return false;
1195}
1196
Aart Bikaec3cce2015-10-14 17:44:55 -07001197bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1198 HInductionVarAnalysis::InductionInfo* trip,
1199 HGraph* graph, // when set, code is generated
1200 HBasicBlock* block,
1201 /*out*/HInstruction** result,
1202 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001203 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001204 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001205 // If during codegen, the result is not needed (nullptr), simply return success.
1206 if (graph != nullptr && result == nullptr) {
1207 return true;
1208 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001209 // Handle current operation.
Aart Bike6bd0272016-12-16 13:57:52 -08001210 Primitive::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001211 HInstruction* opa = nullptr;
1212 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001213 switch (info->induction_class) {
1214 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001215 // Invariants (note that since invariants only have other invariants as
1216 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001217 switch (info->operation) {
1218 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001219 case HInductionVarAnalysis::kSub:
1220 case HInductionVarAnalysis::kMul:
1221 case HInductionVarAnalysis::kDiv:
1222 case HInductionVarAnalysis::kRem:
1223 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001224 case HInductionVarAnalysis::kLT:
1225 case HInductionVarAnalysis::kLE:
1226 case HInductionVarAnalysis::kGT:
1227 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001228 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1229 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1230 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001231 HInstruction* operation = nullptr;
1232 switch (info->operation) {
1233 case HInductionVarAnalysis::kAdd:
1234 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001235 case HInductionVarAnalysis::kSub:
1236 operation = new (graph->GetArena()) HSub(type, opa, opb); break;
1237 case HInductionVarAnalysis::kMul:
1238 operation = new (graph->GetArena()) HMul(type, opa, opb, kNoDexPc); break;
1239 case HInductionVarAnalysis::kDiv:
1240 operation = new (graph->GetArena()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001241 case HInductionVarAnalysis::kRem:
1242 operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001243 case HInductionVarAnalysis::kXor:
1244 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001245 case HInductionVarAnalysis::kLT:
1246 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1247 case HInductionVarAnalysis::kLE:
1248 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1249 case HInductionVarAnalysis::kGT:
1250 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1251 case HInductionVarAnalysis::kGE:
1252 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1253 default:
1254 LOG(FATAL) << "unknown operation";
1255 }
1256 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001257 }
1258 return true;
1259 }
1260 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001261 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001262 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1263 if (graph != nullptr) {
1264 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1265 }
1266 return true;
1267 }
1268 break;
1269 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001270 if (graph != nullptr) {
1271 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001272 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001273 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001274 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001275 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001276 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001277 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001278 }
1279 FALLTHROUGH_INTENDED;
1280 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001281 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001282 if (is_min) {
1283 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001284 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001285 }
1286 return true;
1287 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001288 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001289 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001290 *result =
1291 Insert(block,
1292 new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001293 }
1294 return true;
1295 }
1296 }
1297 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001298 case HInductionVarAnalysis::kNop:
1299 LOG(FATAL) << "unexpected invariant nop";
1300 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001301 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001302 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001303 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1304 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1305 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001306 // known stride yields right value. Always avoid any narrowing linear induction or
1307 // any type mismatch between the linear induction and the trip count expression.
1308 // TODO: careful runtime type conversions could generalize this latter restriction.
1309 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1310 int64_t stride_value = 0;
1311 if (IsConstant(info->op_a, kExact, &stride_value) &&
1312 CanLongValueFitIntoInt(stride_value)) {
1313 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1314 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1315 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1316 if (graph != nullptr) {
1317 HInstruction* oper;
1318 if (stride_value == 1) {
1319 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1320 } else if (stride_value == -1) {
1321 oper = new (graph->GetArena()) HSub(type, opb, opa);
1322 } else {
1323 HInstruction* mul =
1324 new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa);
1325 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
1326 }
1327 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001328 }
Aart Bike6bd0272016-12-16 13:57:52 -08001329 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001330 }
1331 }
1332 }
1333 break;
Aart Bik4a342772015-11-30 10:17:46 -08001334 }
Aart Bikc071a012016-12-01 10:22:31 -08001335 case HInductionVarAnalysis::kPolynomial:
1336 case HInductionVarAnalysis::kGeometric:
1337 break;
Aart Bik4a342772015-11-30 10:17:46 -08001338 case HInductionVarAnalysis::kWrapAround:
1339 case HInductionVarAnalysis::kPeriodic: {
1340 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1341 // values are easy to test at runtime without complications of arithmetic wrap-around.
1342 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001343 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001344 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001345 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001346 }
1347 return true;
1348 }
1349 break;
1350 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001351 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001352 }
1353 return false;
1354}
1355
Aart Bik16d3a652016-09-09 10:33:50 -07001356void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1357 HInstruction* fetch,
1358 HInstruction* replacement) {
1359 if (info != nullptr) {
1360 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1361 info->operation == HInductionVarAnalysis::kFetch &&
1362 info->fetch == fetch) {
1363 info->fetch = replacement;
1364 }
1365 ReplaceInduction(info->op_a, fetch, replacement);
1366 ReplaceInduction(info->op_b, fetch, replacement);
1367 }
1368}
1369
Aart Bikd14c5952015-09-08 15:25:15 -07001370} // namespace art