blob: d6513c8e346bd828bf1d5a096158a3ef8e2126ed [file] [log] [blame]
Aart Bikd14c5952015-09-08 15:25:15 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Aart Bikd14c5952015-09-08 15:25:15 -070017#include "induction_var_range.h"
18
Aart Bikcd26feb2015-09-23 17:50:50 -070019#include <limits>
20
Aart Bikd14c5952015-09-08 15:25:15 -070021namespace art {
22
Aart Bikb3365e02015-09-21 14:45:05 -070023/** Returns true if 64-bit constant fits in 32-bit constant. */
24static bool CanLongValueFitIntoInt(int64_t c) {
Aart Bikcd26feb2015-09-23 17:50:50 -070025 return std::numeric_limits<int32_t>::min() <= c && c <= std::numeric_limits<int32_t>::max();
Aart Bikd14c5952015-09-08 15:25:15 -070026}
27
Aart Bikb3365e02015-09-21 14:45:05 -070028/** Returns true if 32-bit addition can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070029static bool IsSafeAdd(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070030 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) + static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070031}
32
Aart Bikb3365e02015-09-21 14:45:05 -070033/** Returns true if 32-bit subtraction can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070034static bool IsSafeSub(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070035 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) - static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070036}
37
Aart Bikb3365e02015-09-21 14:45:05 -070038/** Returns true if 32-bit multiplication can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070039static bool IsSafeMul(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070040 return CanLongValueFitIntoInt(static_cast<int64_t>(c1) * static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070041}
42
Aart Bikb3365e02015-09-21 14:45:05 -070043/** Returns true if 32-bit division can be done safely. */
Aart Bikd14c5952015-09-08 15:25:15 -070044static bool IsSafeDiv(int32_t c1, int32_t c2) {
Aart Bikb3365e02015-09-21 14:45:05 -070045 return c2 != 0 && CanLongValueFitIntoInt(static_cast<int64_t>(c1) / static_cast<int64_t>(c2));
Aart Bikd14c5952015-09-08 15:25:15 -070046}
47
Aart Bik97412c922016-02-19 20:14:38 -080048/** Returns true for 32/64-bit constant instruction. */
49static bool IsIntAndGet(HInstruction* instruction, int64_t* value) {
Aart Bikd14c5952015-09-08 15:25:15 -070050 if (instruction->IsIntConstant()) {
Aart Bikb3365e02015-09-21 14:45:05 -070051 *value = instruction->AsIntConstant()->GetValue();
52 return true;
Aart Bikd14c5952015-09-08 15:25:15 -070053 } else if (instruction->IsLongConstant()) {
Aart Bik97412c922016-02-19 20:14:38 -080054 *value = instruction->AsLongConstant()->GetValue();
55 return true;
Aart Bikd14c5952015-09-08 15:25:15 -070056 }
57 return false;
58}
59
Aart Bikb603a5c2017-03-06 18:29:39 -080060/** Computes a * b for a,b > 0 (at least until first overflow happens). */
61static int64_t SafeMul(int64_t a, int64_t b, /*out*/ bool* overflow) {
62 if (a > 0 && b > 0 && a > (std::numeric_limits<int64_t>::max() / b)) {
63 *overflow = true;
64 }
65 return a * b;
66}
67
68/** Returns b^e for b,e > 0. Sets overflow if arithmetic wrap-around occurred. */
Aart Bikd3ba6262017-01-30 14:37:12 -080069static int64_t IntPow(int64_t b, int64_t e, /*out*/ bool* overflow) {
Aart Bikb603a5c2017-03-06 18:29:39 -080070 DCHECK_LT(0, b);
71 DCHECK_LT(0, e);
Aart Bikc071a012016-12-01 10:22:31 -080072 int64_t pow = 1;
73 while (e) {
74 if (e & 1) {
Aart Bikb603a5c2017-03-06 18:29:39 -080075 pow = SafeMul(pow, b, overflow);
Aart Bikc071a012016-12-01 10:22:31 -080076 }
77 e >>= 1;
Aart Bikb603a5c2017-03-06 18:29:39 -080078 if (e) {
79 b = SafeMul(b, b, overflow);
80 }
Aart Bikc071a012016-12-01 10:22:31 -080081 }
82 return pow;
83}
84
Aart Bikb3365e02015-09-21 14:45:05 -070085/**
Aart Bik40fbf742016-10-31 11:02:50 -070086 * Detects an instruction that is >= 0. As long as the value is carried by
87 * a single instruction, arithmetic wrap-around cannot occur.
Aart Bikb3365e02015-09-21 14:45:05 -070088 */
Aart Bik40fbf742016-10-31 11:02:50 -070089static bool IsGEZero(HInstruction* instruction) {
90 DCHECK(instruction != nullptr);
91 if (instruction->IsArrayLength()) {
92 return true;
93 } else if (instruction->IsInvokeStaticOrDirect()) {
94 switch (instruction->AsInvoke()->GetIntrinsic()) {
95 case Intrinsics::kMathMinIntInt:
96 case Intrinsics::kMathMinLongLong:
97 // Instruction MIN(>=0, >=0) is >= 0.
98 return IsGEZero(instruction->InputAt(0)) &&
99 IsGEZero(instruction->InputAt(1));
100 case Intrinsics::kMathAbsInt:
101 case Intrinsics::kMathAbsLong:
102 // Instruction ABS(x) is >= 0.
103 return true;
104 default:
105 break;
106 }
107 }
108 int64_t value = -1;
109 return IsIntAndGet(instruction, &value) && value >= 0;
110}
111
112/** Hunts "under the hood" for a suitable instruction at the hint. */
113static bool IsMaxAtHint(
114 HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) {
115 if (instruction->IsInvokeStaticOrDirect()) {
116 switch (instruction->AsInvoke()->GetIntrinsic()) {
117 case Intrinsics::kMathMinIntInt:
118 case Intrinsics::kMathMinLongLong:
119 // For MIN(x, y), return most suitable x or y as maximum.
120 return IsMaxAtHint(instruction->InputAt(0), hint, suitable) ||
121 IsMaxAtHint(instruction->InputAt(1), hint, suitable);
122 default:
123 break;
124 }
125 } else {
126 *suitable = instruction;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000127 return HuntForDeclaration(instruction) == hint;
Aart Bik40fbf742016-10-31 11:02:50 -0700128 }
129 return false;
130}
131
132/** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */
133static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) {
134 if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) {
135 // If a == 1, instruction >= 0 and b <= 0, just return the constant b.
136 // No arithmetic wrap-around can occur.
137 if (IsGEZero(v.instruction)) {
138 return InductionVarRange::Value(v.b_constant);
139 }
Aart Bikb3365e02015-09-21 14:45:05 -0700140 }
141 return v;
142}
143
Aart Bik40fbf742016-10-31 11:02:50 -0700144/** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */
145static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) {
146 if (v.is_known && v.a_constant >= 1) {
147 // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as
148 // length + b because length >= 0 is true.
149 int64_t value;
150 if (v.instruction->IsDiv() &&
151 v.instruction->InputAt(0)->IsArrayLength() &&
152 IsIntAndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
153 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
154 }
155 // If a == 1, the most suitable one suffices as maximum value.
156 HInstruction* suitable = nullptr;
157 if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) {
158 return InductionVarRange::Value(suitable, 1, v.b_constant);
159 }
160 }
161 return v;
162}
163
164/** Tests for a constant value. */
Aart Bik52be7e72016-06-23 11:20:41 -0700165static bool IsConstantValue(InductionVarRange::Value v) {
166 return v.is_known && v.a_constant == 0;
167}
168
169/** Corrects a value for type to account for arithmetic wrap-around in lower precision. */
Aart Bik0d345cf2016-03-16 10:49:38 -0700170static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) {
171 switch (type) {
172 case Primitive::kPrimShort:
173 case Primitive::kPrimChar:
174 case Primitive::kPrimByte: {
175 // Constants within range only.
176 // TODO: maybe some room for improvement, like allowing widening conversions
Aart Bike6bd0272016-12-16 13:57:52 -0800177 int32_t min = Primitive::MinValueOfIntegralType(type);
178 int32_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik52be7e72016-06-23 11:20:41 -0700179 return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max)
Aart Bik0d345cf2016-03-16 10:49:38 -0700180 ? v
181 : InductionVarRange::Value();
182 }
183 default:
Aart Bik0d345cf2016-03-16 10:49:38 -0700184 return v;
185 }
186}
187
Aart Bik40fbf742016-10-31 11:02:50 -0700188/** Inserts an instruction. */
Aart Bik389b3db2015-10-28 14:23:40 -0700189static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
190 DCHECK(block != nullptr);
191 DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId();
Aart Bikaec3cce2015-10-14 17:44:55 -0700192 DCHECK(instruction != nullptr);
Aart Bik389b3db2015-10-28 14:23:40 -0700193 block->InsertInstructionBefore(instruction, block->GetLastInstruction());
Aart Bikaec3cce2015-10-14 17:44:55 -0700194 return instruction;
195}
196
Aart Bik40fbf742016-10-31 11:02:50 -0700197/** Obtains loop's control instruction. */
Aart Bik009cace2016-09-16 10:15:19 -0700198static HInstruction* GetLoopControl(HLoopInformation* loop) {
199 DCHECK(loop != nullptr);
200 return loop->GetHeader()->GetLastInstruction();
201}
202
Aart Bikd14c5952015-09-08 15:25:15 -0700203//
204// Public class methods.
205//
206
207InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis)
Aart Bik52be7e72016-06-23 11:20:41 -0700208 : induction_analysis_(induction_analysis),
209 chase_hint_(nullptr) {
Aart Bikb3365e02015-09-21 14:45:05 -0700210 DCHECK(induction_analysis != nullptr);
Aart Bikd14c5952015-09-08 15:25:15 -0700211}
212
Aart Bik1fc3afb2016-02-02 13:26:16 -0800213bool InductionVarRange::GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -0700214 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -0700215 HInstruction* chase_hint,
Aart Bik389b3db2015-10-28 14:23:40 -0700216 /*out*/Value* min_val,
217 /*out*/Value* max_val,
218 /*out*/bool* needs_finite_test) {
Aart Bik52be7e72016-06-23 11:20:41 -0700219 HLoopInformation* loop = nullptr;
220 HInductionVarAnalysis::InductionInfo* info = nullptr;
221 HInductionVarAnalysis::InductionInfo* trip = nullptr;
222 if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) {
223 return false;
Aart Bik97412c922016-02-19 20:14:38 -0800224 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700225 // Type int or lower (this is not too restrictive since intended clients, like
226 // bounds check elimination, will have truncated higher precision induction
227 // at their use point already).
228 switch (info->type) {
229 case Primitive::kPrimInt:
230 case Primitive::kPrimShort:
231 case Primitive::kPrimChar:
232 case Primitive::kPrimByte:
233 break;
234 default:
235 return false;
236 }
Aart Bik97412c922016-02-19 20:14:38 -0800237 // Find range.
Aart Bik52be7e72016-06-23 11:20:41 -0700238 chase_hint_ = chase_hint;
239 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700240 int64_t stride_value = 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700241 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
242 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint);
Aart Bik16d3a652016-09-09 10:33:50 -0700243 *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip);
Aart Bik40fbf742016-10-31 11:02:50 -0700244 chase_hint_ = nullptr;
245 // Retry chasing constants for wrap-around (merge sensitive).
246 if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) {
247 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
248 }
Aart Bik97412c922016-02-19 20:14:38 -0800249 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700250}
251
Aart Bik16d3a652016-09-09 10:33:50 -0700252bool InductionVarRange::CanGenerateRange(HInstruction* context,
253 HInstruction* instruction,
254 /*out*/bool* needs_finite_test,
255 /*out*/bool* needs_taken_test) {
256 bool is_last_value = false;
257 int64_t stride_value = 0;
Aart Bik9abf8942016-10-14 09:49:42 -0700258 return GenerateRangeOrLastValue(context,
259 instruction,
260 is_last_value,
261 nullptr,
262 nullptr,
263 nullptr,
264 nullptr,
265 nullptr, // nothing generated yet
266 &stride_value,
267 needs_finite_test,
268 needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700269 && (stride_value == -1 ||
270 stride_value == 0 ||
Aart Bik40fbf742016-10-31 11:02:50 -0700271 stride_value == 1); // avoid arithmetic wrap-around anomalies.
Aart Bikaec3cce2015-10-14 17:44:55 -0700272}
273
Aart Bik16d3a652016-09-09 10:33:50 -0700274void InductionVarRange::GenerateRange(HInstruction* context,
275 HInstruction* instruction,
276 HGraph* graph,
277 HBasicBlock* block,
278 /*out*/HInstruction** lower,
279 /*out*/HInstruction** upper) {
280 bool is_last_value = false;
Aart Bik009cace2016-09-16 10:15:19 -0700281 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700282 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700283 if (!GenerateRangeOrLastValue(context,
284 instruction,
285 is_last_value,
286 graph,
287 block,
288 lower,
289 upper,
290 nullptr,
291 &stride_value,
292 &b1,
293 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700294 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
Aart Bik389b3db2015-10-28 14:23:40 -0700295 }
296}
297
Aart Bik16d3a652016-09-09 10:33:50 -0700298HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context,
299 HGraph* graph,
300 HBasicBlock* block) {
301 HInstruction* taken_test = nullptr;
302 bool is_last_value = false;
303 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700304 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700305 if (!GenerateRangeOrLastValue(context,
306 context,
307 is_last_value,
308 graph,
309 block,
310 nullptr,
311 nullptr,
312 &taken_test,
313 &stride_value,
314 &b1,
315 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700316 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
317 }
318 return taken_test;
319}
320
321bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) {
322 bool is_last_value = true;
323 int64_t stride_value = 0;
324 bool needs_finite_test = false;
325 bool needs_taken_test = false;
Aart Bik9abf8942016-10-14 09:49:42 -0700326 return GenerateRangeOrLastValue(instruction,
327 instruction,
328 is_last_value,
329 nullptr,
330 nullptr,
331 nullptr,
332 nullptr,
333 nullptr, // nothing generated yet
334 &stride_value,
335 &needs_finite_test,
336 &needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700337 && !needs_finite_test && !needs_taken_test;
338}
339
340HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction,
341 HGraph* graph,
342 HBasicBlock* block) {
343 HInstruction* last_value = nullptr;
344 bool is_last_value = true;
345 int64_t stride_value = 0;
346 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700347 if (!GenerateRangeOrLastValue(instruction,
348 instruction,
349 is_last_value,
350 graph,
351 block,
352 &last_value,
353 &last_value,
354 nullptr,
355 &stride_value,
356 &b1,
357 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700358 LOG(FATAL) << "Failed precondition: CanGenerateLastValue()";
359 }
360 return last_value;
361}
362
363void InductionVarRange::Replace(HInstruction* instruction,
364 HInstruction* fetch,
365 HInstruction* replacement) {
366 for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
367 lp != nullptr;
368 lp = lp->GetPreHeader()->GetLoopInformation()) {
Aart Bik009cace2016-09-16 10:15:19 -0700369 // Update instruction's information.
Aart Bik16d3a652016-09-09 10:33:50 -0700370 ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement);
Aart Bik009cace2016-09-16 10:15:19 -0700371 // Update loop's trip-count information.
372 ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement);
Aart Bik389b3db2015-10-28 14:23:40 -0700373 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700374}
375
Aart Bik6b69e0a2017-01-11 10:20:43 -0800376bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const {
Aart Bik9abf8942016-10-14 09:49:42 -0700377 HInductionVarAnalysis::InductionInfo *trip =
378 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
Aart Bik6b69e0a2017-01-11 10:20:43 -0800379 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
380 IsConstant(trip->op_a, kExact, tc);
381 return true;
382 }
383 return false;
Aart Bik9abf8942016-10-14 09:49:42 -0700384}
385
Aart Bik8e02e3e2017-02-28 14:41:55 -0800386bool InductionVarRange::IsUnitStride(HInstruction* instruction,
387 /*out*/ HInstruction** offset) const {
388 HLoopInformation* loop = nullptr;
389 HInductionVarAnalysis::InductionInfo* info = nullptr;
390 HInductionVarAnalysis::InductionInfo* trip = nullptr;
391 if (HasInductionInfo(instruction, instruction, &loop, &info, &trip)) {
392 if (info->induction_class == HInductionVarAnalysis::kLinear &&
Aart Bik7adb6882017-03-07 13:28:51 -0800393 info->op_b->operation == HInductionVarAnalysis::kFetch &&
394 !HInductionVarAnalysis::IsNarrowingLinear(info)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800395 int64_t stride_value = 0;
396 if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) {
397 int64_t off_value = 0;
398 if (IsConstant(info->op_b, kExact, &off_value) && off_value == 0) {
399 *offset = nullptr;
400 } else {
401 *offset = info->op_b->fetch;
402 }
403 return true;
404 }
405 }
406 }
407 return false;
408}
409
410HInstruction* InductionVarRange::GenerateTripCount(HLoopInformation* loop,
411 HGraph* graph,
412 HBasicBlock* block) {
413 HInductionVarAnalysis::InductionInfo *trip =
414 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
415 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
416 HInstruction* taken_test = nullptr;
417 HInstruction* trip_expr = nullptr;
418 if (IsBodyTripCount(trip)) {
419 if (!GenerateCode(trip->op_b, nullptr, graph, block, &taken_test, false, false)) {
420 return nullptr;
421 }
422 }
423 if (GenerateCode(trip->op_a, nullptr, graph, block, &trip_expr, false, false)) {
424 if (taken_test != nullptr) {
425 HInstruction* zero = graph->GetConstant(trip->type, 0);
426 trip_expr = Insert(block, new (graph->GetArena()) HSelect(taken_test, trip_expr, zero, kNoDexPc));
427 }
428 return trip_expr;
429 }
430 }
431 return nullptr;
432}
433
Aart Bikd14c5952015-09-08 15:25:15 -0700434//
435// Private class methods.
436//
437
Aart Bik97412c922016-02-19 20:14:38 -0800438bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
439 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700440 /*out*/ int64_t* value) const {
Aart Bik97412c922016-02-19 20:14:38 -0800441 if (info != nullptr) {
442 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
443 // any of the three requests (kExact, kAtMost, and KAtLeast).
444 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
445 info->operation == HInductionVarAnalysis::kFetch) {
446 if (IsIntAndGet(info->fetch, value)) {
447 return true;
448 }
449 }
Aart Bik40fbf742016-10-31 11:02:50 -0700450 // Try range analysis on the invariant, only accept a proper range
451 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700452 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
453 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
454 if (IsConstantValue(min_val) &&
455 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
456 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
457 *value = max_val.b_constant;
458 return true;
459 } else if (request == kAtLeast) {
460 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800461 return true;
462 }
463 }
Aart Bik97412c922016-02-19 20:14:38 -0800464 }
465 return false;
466}
467
Aart Bik52be7e72016-06-23 11:20:41 -0700468bool InductionVarRange::HasInductionInfo(
469 HInstruction* context,
470 HInstruction* instruction,
471 /*out*/ HLoopInformation** loop,
472 /*out*/ HInductionVarAnalysis::InductionInfo** info,
473 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800474 DCHECK(context != nullptr);
475 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700476 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
477 if (lp != nullptr) {
478 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700479 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700480 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700481 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700482 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700483 return true;
484 }
485 }
486 return false;
487}
488
489bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
490 if (trip != nullptr) {
491 // Both bounds that define a trip-count are well-behaved if they either are not defined
492 // in any loop, or are contained in a proper interval. This allows finding the min/max
493 // of an expression by chasing outward.
494 InductionVarRange range(induction_analysis_);
495 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
496 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
497 int64_t not_used = 0;
498 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
499 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
500 }
501 return true;
502}
503
504bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
505 if (info != nullptr) {
506 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
507 info->operation == HInductionVarAnalysis::kFetch) {
508 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
509 }
510 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
511 }
512 return false;
513}
514
Aart Bik16d3a652016-09-09 10:33:50 -0700515bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
516 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700517 if (info != nullptr) {
518 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700519 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800520 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
521 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700522 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700523 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700524 }
Aart Bikd14c5952015-09-08 15:25:15 -0700525 }
Aart Bik389b3db2015-10-28 14:23:40 -0700526 return false;
527}
528
Aart Bik7d57d7f2015-12-09 14:39:48 -0800529bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700530 if (trip != nullptr) {
531 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
532 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
533 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
534 }
535 }
536 return false;
537}
538
Aart Bik7d57d7f2015-12-09 14:39:48 -0800539bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700540 if (trip != nullptr) {
541 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
542 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
543 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
544 }
545 }
546 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700547}
548
Aart Bik7d57d7f2015-12-09 14:39:48 -0800549InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
550 HInductionVarAnalysis::InductionInfo* trip,
551 bool in_body,
552 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800553 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800554 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700555 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800556 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
557 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
558 // with intermediate results that only incorporate single instructions.
559 if (trip != nullptr) {
560 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700561 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800562 int64_t stride_value = 0;
563 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800564 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800565 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800566 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
567 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
568 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700569 trip->induction_class,
570 trip->operation,
571 trip_expr->op_a,
572 trip->op_b,
573 nullptr,
574 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800575 return GetVal(&cancelled_trip, trip, in_body, is_min);
576 }
577 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800578 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800579 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
580 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
581 HInductionVarAnalysis::InductionInfo neg(
582 HInductionVarAnalysis::kInvariant,
583 HInductionVarAnalysis::kNeg,
584 nullptr,
585 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700586 nullptr,
587 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800588 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700589 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800590 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
591 }
592 }
593 }
594 }
595 }
596 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
597 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
598 GetVal(info->op_b, trip, in_body, is_min));
599}
600
Aart Bikdf7822e2016-12-06 10:05:30 -0800601InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
602 HInductionVarAnalysis::InductionInfo* trip,
603 bool in_body,
604 bool is_min) const {
605 DCHECK(info != nullptr);
606 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
607 int64_t a = 0;
608 int64_t b = 0;
609 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
610 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800611 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800612 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
613 Value c = GetVal(info->op_b, trip, in_body, is_min);
614 if (is_min) {
615 return c;
616 } else {
617 Value m = GetVal(trip, trip, in_body, is_min);
618 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
619 Value x = MulValue(Value(a), t);
620 Value y = MulValue(Value(b), m);
621 return AddValue(AddValue(x, y), c);
622 }
623 }
624 return Value();
625}
626
Aart Bikc071a012016-12-01 10:22:31 -0800627InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
628 HInductionVarAnalysis::InductionInfo* trip,
629 bool in_body,
630 bool is_min) const {
631 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800632 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800633 int64_t a = 0;
634 int64_t f = 0;
635 if (IsConstant(info->op_a, kExact, &a) &&
636 CanLongValueFitIntoInt(a) &&
Aart Bikdf7822e2016-12-06 10:05:30 -0800637 IsIntAndGet(info->fetch, &f) && f >= 1) {
638 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
639 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800640 const bool is_min_a = a >= 0 ? is_min : !is_min;
641 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800642 Value b = GetVal(info->op_b, trip, in_body, is_min);
643 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800644 }
645 }
646 return Value();
647}
648
Aart Bikd14c5952015-09-08 15:25:15 -0700649InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700650 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700651 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800652 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700653 // Special case when chasing constants: single instruction that denotes trip count in the
654 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
655 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700656 if (is_min) {
657 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800658 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700659 return Value(std::numeric_limits<int32_t>::max());
660 }
661 }
Aart Bik40fbf742016-10-31 11:02:50 -0700662 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
663 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
664 int64_t value;
665 if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
666 // Proper constant reveals best information.
667 return Value(static_cast<int32_t>(value));
668 } else if (instruction == chase_hint_) {
669 // At hint, fetch is represented by itself.
670 return Value(instruction, 1, 0);
671 } else if (instruction->IsAdd()) {
672 // Incorporate suitable constants in the chased value.
Aart Bik97412c922016-02-19 20:14:38 -0800673 if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
674 return AddValue(Value(static_cast<int32_t>(value)),
675 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
676 } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
677 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
678 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700679 }
Aart Bik52be7e72016-06-23 11:20:41 -0700680 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700681 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700682 if (chase_hint_ == nullptr) {
683 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
684 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000685 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700686 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700687 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700688 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800689 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Aart Bik0d345cf2016-03-16 10:49:38 -0700690 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
691 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
692 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
693 }
Aart Bik52be7e72016-06-23 11:20:41 -0700694 }
695 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
696 // so far is well-behaved in both bounds and the next trip-count is safe.
697 // Example:
698 // for (int i = 0; i <= 100; i++) // safe
699 // for (int j = 0; j <= i; j++) // well-behaved
700 // j is in range [0, i ] (if i is chase hint)
701 // or in range [0, 100] (otherwise)
702 HLoopInformation* next_loop = nullptr;
703 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
704 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
705 bool next_in_body = true; // inner loop is always in body of outer loop
706 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
707 IsWellBehavedTripCount(trip) &&
708 !IsUnsafeTripCount(next_trip)) {
709 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700710 }
Aart Bik40fbf742016-10-31 11:02:50 -0700711 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700712 return Value(instruction, 1, 0);
713}
714
Aart Bikcd26feb2015-09-23 17:50:50 -0700715InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
716 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700717 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800718 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700719 if (info != nullptr) {
720 switch (info->induction_class) {
721 case HInductionVarAnalysis::kInvariant:
722 // Invariants.
723 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700724 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700725 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
726 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700727 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700728 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
729 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700730 case HInductionVarAnalysis::kNeg: // second reversed!
731 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700732 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700733 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700734 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700735 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700736 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800737 case HInductionVarAnalysis::kRem:
738 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700739 case HInductionVarAnalysis::kXor:
740 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700741 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700742 return GetFetch(info->fetch, trip, in_body, is_min);
743 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700744 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700745 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700746 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700747 }
748 FALLTHROUGH_INTENDED;
749 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700750 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700751 if (is_min) {
752 return Value(0);
753 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700754 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700755 }
756 break;
757 default:
758 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700759 }
760 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700761 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700762 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800763 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800764 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800765 case HInductionVarAnalysis::kGeometric:
766 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700767 case HInductionVarAnalysis::kWrapAround:
768 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700769 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
770 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700771 }
772 }
Aart Bikb3365e02015-09-21 14:45:05 -0700773 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700774}
775
776InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
777 HInductionVarAnalysis::InductionInfo* info2,
778 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700779 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800780 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700781 // Constant times range.
782 int64_t value = 0;
783 if (IsConstant(info1, kExact, &value)) {
784 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
785 } else if (IsConstant(info2, kExact, &value)) {
786 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
787 }
788 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700789 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
790 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
791 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
792 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800793 // Positive range vs. positive or negative range.
794 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
795 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
796 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
797 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
798 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700799 }
Aart Bik97412c922016-02-19 20:14:38 -0800800 }
801 // Negative range vs. positive or negative range.
802 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
803 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
804 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
805 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
806 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700807 }
808 }
Aart Bikb3365e02015-09-21 14:45:05 -0700809 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700810}
811
812InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
813 HInductionVarAnalysis::InductionInfo* info2,
814 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700815 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800816 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700817 // Range divided by constant.
818 int64_t value = 0;
819 if (IsConstant(info2, kExact, &value)) {
820 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
821 }
822 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700823 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
824 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
825 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
826 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800827 // Positive range vs. positive or negative range.
828 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
829 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
830 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
831 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
832 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700833 }
Aart Bik97412c922016-02-19 20:14:38 -0800834 }
835 // Negative range vs. positive or negative range.
836 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
837 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
838 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
839 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
840 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700841 }
842 }
Aart Bikb3365e02015-09-21 14:45:05 -0700843 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700844}
845
Aart Bikdf7822e2016-12-06 10:05:30 -0800846InductionVarRange::Value InductionVarRange::GetRem(
847 HInductionVarAnalysis::InductionInfo* info1,
848 HInductionVarAnalysis::InductionInfo* info2) const {
849 int64_t v1 = 0;
850 int64_t v2 = 0;
851 // Only accept exact values.
852 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
853 int64_t value = v1 % v2;
854 if (CanLongValueFitIntoInt(value)) {
855 return Value(static_cast<int32_t>(value));
856 }
857 }
858 return Value();
859}
860
Aart Bik7dc96932016-10-12 10:01:05 -0700861InductionVarRange::Value InductionVarRange::GetXor(
862 HInductionVarAnalysis::InductionInfo* info1,
863 HInductionVarAnalysis::InductionInfo* info2) const {
864 int64_t v1 = 0;
865 int64_t v2 = 0;
866 // Only accept exact values.
867 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
868 int64_t value = v1 ^ v2;
869 if (CanLongValueFitIntoInt(value)) {
870 return Value(static_cast<int32_t>(value));
871 }
872 }
873 return Value();
874}
875
Aart Bik52be7e72016-06-23 11:20:41 -0700876InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
877 int64_t value,
878 HInductionVarAnalysis::InductionInfo* info,
879 HInductionVarAnalysis::InductionInfo* trip,
880 bool in_body,
881 bool is_min) const {
882 if (CanLongValueFitIntoInt(value)) {
883 Value c(static_cast<int32_t>(value));
884 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
885 }
886 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800887}
888
Aart Bik52be7e72016-06-23 11:20:41 -0700889InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
890 int64_t value,
891 HInductionVarAnalysis::InductionInfo* info,
892 HInductionVarAnalysis::InductionInfo* trip,
893 bool in_body,
894 bool is_min) const {
895 if (CanLongValueFitIntoInt(value)) {
896 Value c(static_cast<int32_t>(value));
897 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
898 }
899 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700900}
901
Aart Bik7d57d7f2015-12-09 14:39:48 -0800902InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700903 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800904 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700905 if (v1.a_constant == 0) {
906 return Value(v2.instruction, v2.a_constant, b);
907 } else if (v2.a_constant == 0) {
908 return Value(v1.instruction, v1.a_constant, b);
909 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
910 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
911 }
912 }
Aart Bikb3365e02015-09-21 14:45:05 -0700913 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700914}
915
Aart Bik7d57d7f2015-12-09 14:39:48 -0800916InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700917 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800918 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700919 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
920 return Value(v2.instruction, -v2.a_constant, b);
921 } else if (v2.a_constant == 0) {
922 return Value(v1.instruction, v1.a_constant, b);
923 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
924 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
925 }
926 }
Aart Bikb3365e02015-09-21 14:45:05 -0700927 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700928}
929
Aart Bik7d57d7f2015-12-09 14:39:48 -0800930InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700931 if (v1.is_known && v2.is_known) {
932 if (v1.a_constant == 0) {
933 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
934 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
935 }
936 } else if (v2.a_constant == 0) {
937 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
938 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
939 }
Aart Bikd14c5952015-09-08 15:25:15 -0700940 }
941 }
Aart Bikb3365e02015-09-21 14:45:05 -0700942 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700943}
944
Aart Bik7d57d7f2015-12-09 14:39:48 -0800945InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700946 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700947 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
948 return Value(v1.b_constant / v2.b_constant);
949 }
950 }
Aart Bikb3365e02015-09-21 14:45:05 -0700951 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700952}
953
Aart Bik7d57d7f2015-12-09 14:39:48 -0800954InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700955 if (v1.is_known && v2.is_known) {
956 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700957 return Value(v1.instruction, v1.a_constant,
958 is_min ? std::min(v1.b_constant, v2.b_constant)
959 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700960 }
Aart Bikd14c5952015-09-08 15:25:15 -0700961 }
Aart Bikb3365e02015-09-21 14:45:05 -0700962 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700963}
964
Aart Bik9abf8942016-10-14 09:49:42 -0700965bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
966 HInstruction* instruction,
967 bool is_last_value,
968 HGraph* graph,
969 HBasicBlock* block,
970 /*out*/HInstruction** lower,
971 /*out*/HInstruction** upper,
972 /*out*/HInstruction** taken_test,
973 /*out*/int64_t* stride_value,
974 /*out*/bool* needs_finite_test,
975 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700976 HLoopInformation* loop = nullptr;
977 HInductionVarAnalysis::InductionInfo* info = nullptr;
978 HInductionVarAnalysis::InductionInfo* trip = nullptr;
979 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
980 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800981 }
982 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
983 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
984 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
985 // code does not use the trip-count explicitly (since there could be an implicit relation
986 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700987 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700988 *stride_value = 0;
989 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800990 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700991 // Handle last value request.
992 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800993 DCHECK(!in_body);
994 switch (info->induction_class) {
995 case HInductionVarAnalysis::kLinear:
996 if (*stride_value > 0) {
997 lower = nullptr;
998 } else {
999 upper = nullptr;
1000 }
1001 break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001002 case HInductionVarAnalysis::kPolynomial:
1003 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001004 case HInductionVarAnalysis::kGeometric:
1005 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -08001006 case HInductionVarAnalysis::kWrapAround:
1007 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001008 case HInductionVarAnalysis::kPeriodic:
1009 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1010 default:
1011 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001012 }
1013 }
Aart Bik97412c922016-02-19 20:14:38 -08001014 // Code generation for taken test: generate the code when requested or otherwise analyze
1015 // if code generation is feasible when taken test is needed.
1016 if (taken_test != nullptr) {
1017 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1018 } else if (*needs_taken_test) {
1019 if (!GenerateCode(
1020 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1021 return false;
1022 }
1023 }
1024 // Code generation for lower and upper.
1025 return
1026 // Success on lower if invariant (not set), or code can be generated.
1027 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1028 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1029 // And success on upper.
1030 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001031}
1032
Aart Bikdf7822e2016-12-06 10:05:30 -08001033bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1034 HInductionVarAnalysis::InductionInfo* trip,
1035 HGraph* graph,
1036 HBasicBlock* block,
1037 /*out*/HInstruction** result) const {
1038 DCHECK(info != nullptr);
1039 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1040 // Detect known coefficients and trip count (always taken).
1041 int64_t a = 0;
1042 int64_t b = 0;
1043 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001044 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1045 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001046 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001047 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001048 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001049 HInstruction* c = nullptr;
1050 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001051 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001052 Primitive::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001053 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Aart Bike6bd0272016-12-16 13:57:52 -08001054 if (type != Primitive::kPrimLong) {
1055 sum = static_cast<int32_t>(sum); // okay to truncate
1056 }
1057 *result =
1058 Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001059 }
1060 return true;
1061 }
1062 }
1063 return false;
1064}
1065
Aart Bikc071a012016-12-01 10:22:31 -08001066bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1067 HInductionVarAnalysis::InductionInfo* trip,
1068 HGraph* graph,
1069 HBasicBlock* block,
1070 /*out*/HInstruction** result) const {
1071 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001072 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001073 // Detect known base and trip count (always taken).
1074 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001075 int64_t m = 0;
1076 if (IsIntAndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001077 HInstruction* opa = nullptr;
1078 HInstruction* opb = nullptr;
1079 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1080 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001081 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001082 Primitive::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001083 // Compute f ^ m for known maximum index value m.
1084 bool overflow = false;
1085 int64_t fpow = IntPow(f, m, &overflow);
1086 if (info->operation == HInductionVarAnalysis::kDiv) {
1087 // For division, any overflow truncates to zero.
1088 if (overflow || (type != Primitive::kPrimLong && !CanLongValueFitIntoInt(fpow))) {
1089 fpow = 0;
1090 }
1091 } else if (type != Primitive::kPrimLong) {
1092 // For multiplication, okay to truncate to required precision.
1093 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1094 fpow = static_cast<int32_t>(fpow);
1095 }
1096 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001097 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001098 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001099 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001100 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001101 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001102 HInstruction* e = nullptr;
1103 if (info->operation == HInductionVarAnalysis::kMul) {
1104 e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow));
1105 } else {
1106 e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
1107 }
1108 *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001109 }
1110 }
1111 return true;
1112 }
1113 }
1114 return false;
1115}
1116
Aart Bikdf7822e2016-12-06 10:05:30 -08001117bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1118 HInductionVarAnalysis::InductionInfo* trip,
1119 HGraph* graph,
1120 HBasicBlock* block,
1121 /*out*/HInstruction** result) const {
1122 DCHECK(info != nullptr);
1123 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1124 // Count depth.
1125 int32_t depth = 0;
1126 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1127 info = info->op_b, ++depth) {}
1128 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001129 // TODO: generalize, but be careful to adjust the terminal.
1130 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001131 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001132 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1133 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001134 }
1135 return false;
1136}
1137
Aart Bik9abf8942016-10-14 09:49:42 -07001138bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1139 HInductionVarAnalysis::InductionInfo* trip,
1140 HGraph* graph,
1141 HBasicBlock* block,
1142 /*out*/HInstruction** result,
1143 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001144 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001145 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik9abf8942016-10-14 09:49:42 -07001146 // Count period.
Aart Bike6bd0272016-12-16 13:57:52 -08001147 int64_t period = 1;
Aart Bik9abf8942016-10-14 09:49:42 -07001148 for (HInductionVarAnalysis::InductionInfo* p = info;
1149 p->induction_class == HInductionVarAnalysis::kPeriodic;
1150 p = p->op_b, ++period) {}
Aart Bike6bd0272016-12-16 13:57:52 -08001151 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1152 int64_t m = 0;
1153 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1154 int64_t li = m % period;
1155 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1156 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1157 info = info->op_a;
1158 }
1159 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001160 }
Aart Bike6bd0272016-12-16 13:57:52 -08001161 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1162 // directly to obtain the maximum index value t even if taken test is needed.
1163 HInstruction* x = nullptr;
1164 HInstruction* y = nullptr;
1165 HInstruction* t = nullptr;
1166 if (period == 2 &&
1167 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1168 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1169 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1170 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001171 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001172 Primitive::Type type = trip->type;
1173 HInstruction* msk =
1174 Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1)));
1175 HInstruction* is_even =
1176 Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1177 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001178 }
1179 // Guard select with taken test if needed.
1180 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001181 HInstruction* is_taken = nullptr;
1182 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1183 if (graph != nullptr) {
1184 *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc));
1185 }
1186 *needs_taken_test = false; // taken care of
1187 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001188 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001189 }
Aart Bik9abf8942016-10-14 09:49:42 -07001190 }
1191 return true;
1192 }
1193 return false;
1194}
1195
Aart Bikaec3cce2015-10-14 17:44:55 -07001196bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1197 HInductionVarAnalysis::InductionInfo* trip,
1198 HGraph* graph, // when set, code is generated
1199 HBasicBlock* block,
1200 /*out*/HInstruction** result,
1201 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001202 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001203 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001204 // If during codegen, the result is not needed (nullptr), simply return success.
1205 if (graph != nullptr && result == nullptr) {
1206 return true;
1207 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001208 // Handle current operation.
Aart Bike6bd0272016-12-16 13:57:52 -08001209 Primitive::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001210 HInstruction* opa = nullptr;
1211 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001212 switch (info->induction_class) {
1213 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001214 // Invariants (note that since invariants only have other invariants as
1215 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001216 switch (info->operation) {
1217 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001218 case HInductionVarAnalysis::kSub:
1219 case HInductionVarAnalysis::kMul:
1220 case HInductionVarAnalysis::kDiv:
1221 case HInductionVarAnalysis::kRem:
1222 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001223 case HInductionVarAnalysis::kLT:
1224 case HInductionVarAnalysis::kLE:
1225 case HInductionVarAnalysis::kGT:
1226 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001227 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1228 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1229 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001230 HInstruction* operation = nullptr;
1231 switch (info->operation) {
1232 case HInductionVarAnalysis::kAdd:
1233 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001234 case HInductionVarAnalysis::kSub:
1235 operation = new (graph->GetArena()) HSub(type, opa, opb); break;
1236 case HInductionVarAnalysis::kMul:
1237 operation = new (graph->GetArena()) HMul(type, opa, opb, kNoDexPc); break;
1238 case HInductionVarAnalysis::kDiv:
1239 operation = new (graph->GetArena()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001240 case HInductionVarAnalysis::kRem:
1241 operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001242 case HInductionVarAnalysis::kXor:
1243 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001244 case HInductionVarAnalysis::kLT:
1245 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1246 case HInductionVarAnalysis::kLE:
1247 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1248 case HInductionVarAnalysis::kGT:
1249 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1250 case HInductionVarAnalysis::kGE:
1251 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1252 default:
1253 LOG(FATAL) << "unknown operation";
1254 }
1255 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001256 }
1257 return true;
1258 }
1259 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001260 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001261 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1262 if (graph != nullptr) {
1263 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1264 }
1265 return true;
1266 }
1267 break;
1268 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001269 if (graph != nullptr) {
1270 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001271 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001272 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001273 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001274 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001275 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001276 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001277 }
1278 FALLTHROUGH_INTENDED;
1279 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001280 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001281 if (is_min) {
1282 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001283 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001284 }
1285 return true;
1286 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001287 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001288 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001289 *result =
1290 Insert(block,
1291 new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001292 }
1293 return true;
1294 }
1295 }
1296 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001297 case HInductionVarAnalysis::kNop:
1298 LOG(FATAL) << "unexpected invariant nop";
1299 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001300 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001301 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001302 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1303 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1304 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001305 // known stride yields right value. Always avoid any narrowing linear induction or
1306 // any type mismatch between the linear induction and the trip count expression.
1307 // TODO: careful runtime type conversions could generalize this latter restriction.
1308 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1309 int64_t stride_value = 0;
1310 if (IsConstant(info->op_a, kExact, &stride_value) &&
1311 CanLongValueFitIntoInt(stride_value)) {
1312 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1313 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1314 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1315 if (graph != nullptr) {
1316 HInstruction* oper;
1317 if (stride_value == 1) {
1318 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1319 } else if (stride_value == -1) {
1320 oper = new (graph->GetArena()) HSub(type, opb, opa);
1321 } else {
1322 HInstruction* mul =
1323 new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa);
1324 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
1325 }
1326 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001327 }
Aart Bike6bd0272016-12-16 13:57:52 -08001328 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001329 }
1330 }
1331 }
1332 break;
Aart Bik4a342772015-11-30 10:17:46 -08001333 }
Aart Bikc071a012016-12-01 10:22:31 -08001334 case HInductionVarAnalysis::kPolynomial:
1335 case HInductionVarAnalysis::kGeometric:
1336 break;
Aart Bik4a342772015-11-30 10:17:46 -08001337 case HInductionVarAnalysis::kWrapAround:
1338 case HInductionVarAnalysis::kPeriodic: {
1339 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1340 // values are easy to test at runtime without complications of arithmetic wrap-around.
1341 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001342 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001343 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001344 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001345 }
1346 return true;
1347 }
1348 break;
1349 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001350 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001351 }
1352 return false;
1353}
1354
Aart Bik16d3a652016-09-09 10:33:50 -07001355void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1356 HInstruction* fetch,
1357 HInstruction* replacement) {
1358 if (info != nullptr) {
1359 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1360 info->operation == HInductionVarAnalysis::kFetch &&
1361 info->fetch == fetch) {
1362 info->fetch = replacement;
1363 }
1364 ReplaceInduction(info->op_a, fetch, replacement);
1365 ReplaceInduction(info->op_b, fetch, replacement);
1366 }
1367}
1368
Aart Bikd14c5952015-09-08 15:25:15 -07001369} // namespace art