blob: 75619a3c01108d7fbd4e2cd137f28d6a2c8870c3 [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 Bikc071a012016-12-01 10:22:31 -080060/** Returns b^e for b,e >= 1. */
61static int64_t IntPow(int64_t b, int64_t e) {
62 DCHECK_GE(b, 1);
63 DCHECK_GE(e, 1);
64 int64_t pow = 1;
65 while (e) {
66 if (e & 1) {
67 pow *= b;
68 }
69 e >>= 1;
70 b *= b;
71 }
72 return pow;
73}
74
Aart Bikb3365e02015-09-21 14:45:05 -070075/**
Aart Bik40fbf742016-10-31 11:02:50 -070076 * Detects an instruction that is >= 0. As long as the value is carried by
77 * a single instruction, arithmetic wrap-around cannot occur.
Aart Bikb3365e02015-09-21 14:45:05 -070078 */
Aart Bik40fbf742016-10-31 11:02:50 -070079static bool IsGEZero(HInstruction* instruction) {
80 DCHECK(instruction != nullptr);
81 if (instruction->IsArrayLength()) {
82 return true;
83 } else if (instruction->IsInvokeStaticOrDirect()) {
84 switch (instruction->AsInvoke()->GetIntrinsic()) {
85 case Intrinsics::kMathMinIntInt:
86 case Intrinsics::kMathMinLongLong:
87 // Instruction MIN(>=0, >=0) is >= 0.
88 return IsGEZero(instruction->InputAt(0)) &&
89 IsGEZero(instruction->InputAt(1));
90 case Intrinsics::kMathAbsInt:
91 case Intrinsics::kMathAbsLong:
92 // Instruction ABS(x) is >= 0.
93 return true;
94 default:
95 break;
96 }
97 }
98 int64_t value = -1;
99 return IsIntAndGet(instruction, &value) && value >= 0;
100}
101
102/** Hunts "under the hood" for a suitable instruction at the hint. */
103static bool IsMaxAtHint(
104 HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) {
105 if (instruction->IsInvokeStaticOrDirect()) {
106 switch (instruction->AsInvoke()->GetIntrinsic()) {
107 case Intrinsics::kMathMinIntInt:
108 case Intrinsics::kMathMinLongLong:
109 // For MIN(x, y), return most suitable x or y as maximum.
110 return IsMaxAtHint(instruction->InputAt(0), hint, suitable) ||
111 IsMaxAtHint(instruction->InputAt(1), hint, suitable);
112 default:
113 break;
114 }
115 } else {
116 *suitable = instruction;
117 while (instruction->IsArrayLength() ||
118 instruction->IsNullCheck() ||
119 instruction->IsNewArray()) {
120 instruction = instruction->InputAt(0);
121 }
122 return instruction == hint;
123 }
124 return false;
125}
126
127/** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */
128static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) {
129 if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) {
130 // If a == 1, instruction >= 0 and b <= 0, just return the constant b.
131 // No arithmetic wrap-around can occur.
132 if (IsGEZero(v.instruction)) {
133 return InductionVarRange::Value(v.b_constant);
134 }
Aart Bikb3365e02015-09-21 14:45:05 -0700135 }
136 return v;
137}
138
Aart Bik40fbf742016-10-31 11:02:50 -0700139/** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */
140static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) {
141 if (v.is_known && v.a_constant >= 1) {
142 // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as
143 // length + b because length >= 0 is true.
144 int64_t value;
145 if (v.instruction->IsDiv() &&
146 v.instruction->InputAt(0)->IsArrayLength() &&
147 IsIntAndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
148 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
149 }
150 // If a == 1, the most suitable one suffices as maximum value.
151 HInstruction* suitable = nullptr;
152 if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) {
153 return InductionVarRange::Value(suitable, 1, v.b_constant);
154 }
155 }
156 return v;
157}
158
159/** Tests for a constant value. */
Aart Bik52be7e72016-06-23 11:20:41 -0700160static bool IsConstantValue(InductionVarRange::Value v) {
161 return v.is_known && v.a_constant == 0;
162}
163
164/** Corrects a value for type to account for arithmetic wrap-around in lower precision. */
Aart Bik0d345cf2016-03-16 10:49:38 -0700165static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) {
166 switch (type) {
167 case Primitive::kPrimShort:
168 case Primitive::kPrimChar:
169 case Primitive::kPrimByte: {
170 // Constants within range only.
171 // TODO: maybe some room for improvement, like allowing widening conversions
172 const int32_t min = Primitive::MinValueOfIntegralType(type);
173 const int32_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik52be7e72016-06-23 11:20:41 -0700174 return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max)
Aart Bik0d345cf2016-03-16 10:49:38 -0700175 ? v
176 : InductionVarRange::Value();
177 }
178 default:
Aart Bik0d345cf2016-03-16 10:49:38 -0700179 return v;
180 }
181}
182
Aart Bik40fbf742016-10-31 11:02:50 -0700183/** Inserts an instruction. */
Aart Bik389b3db2015-10-28 14:23:40 -0700184static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
185 DCHECK(block != nullptr);
186 DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId();
Aart Bikaec3cce2015-10-14 17:44:55 -0700187 DCHECK(instruction != nullptr);
Aart Bik389b3db2015-10-28 14:23:40 -0700188 block->InsertInstructionBefore(instruction, block->GetLastInstruction());
Aart Bikaec3cce2015-10-14 17:44:55 -0700189 return instruction;
190}
191
Aart Bik40fbf742016-10-31 11:02:50 -0700192/** Obtains loop's control instruction. */
Aart Bik009cace2016-09-16 10:15:19 -0700193static HInstruction* GetLoopControl(HLoopInformation* loop) {
194 DCHECK(loop != nullptr);
195 return loop->GetHeader()->GetLastInstruction();
196}
197
Aart Bikd14c5952015-09-08 15:25:15 -0700198//
199// Public class methods.
200//
201
202InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis)
Aart Bik52be7e72016-06-23 11:20:41 -0700203 : induction_analysis_(induction_analysis),
204 chase_hint_(nullptr) {
Aart Bikb3365e02015-09-21 14:45:05 -0700205 DCHECK(induction_analysis != nullptr);
Aart Bikd14c5952015-09-08 15:25:15 -0700206}
207
Aart Bik1fc3afb2016-02-02 13:26:16 -0800208bool InductionVarRange::GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -0700209 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -0700210 HInstruction* chase_hint,
Aart Bik389b3db2015-10-28 14:23:40 -0700211 /*out*/Value* min_val,
212 /*out*/Value* max_val,
213 /*out*/bool* needs_finite_test) {
Aart Bik52be7e72016-06-23 11:20:41 -0700214 HLoopInformation* loop = nullptr;
215 HInductionVarAnalysis::InductionInfo* info = nullptr;
216 HInductionVarAnalysis::InductionInfo* trip = nullptr;
217 if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) {
218 return false;
Aart Bik97412c922016-02-19 20:14:38 -0800219 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700220 // Type int or lower (this is not too restrictive since intended clients, like
221 // bounds check elimination, will have truncated higher precision induction
222 // at their use point already).
223 switch (info->type) {
224 case Primitive::kPrimInt:
225 case Primitive::kPrimShort:
226 case Primitive::kPrimChar:
227 case Primitive::kPrimByte:
228 break;
229 default:
230 return false;
231 }
Aart Bik97412c922016-02-19 20:14:38 -0800232 // Find range.
Aart Bik52be7e72016-06-23 11:20:41 -0700233 chase_hint_ = chase_hint;
234 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700235 int64_t stride_value = 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700236 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
237 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint);
Aart Bik16d3a652016-09-09 10:33:50 -0700238 *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip);
Aart Bik40fbf742016-10-31 11:02:50 -0700239 chase_hint_ = nullptr;
240 // Retry chasing constants for wrap-around (merge sensitive).
241 if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) {
242 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
243 }
Aart Bik97412c922016-02-19 20:14:38 -0800244 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700245}
246
Aart Bik16d3a652016-09-09 10:33:50 -0700247bool InductionVarRange::CanGenerateRange(HInstruction* context,
248 HInstruction* instruction,
249 /*out*/bool* needs_finite_test,
250 /*out*/bool* needs_taken_test) {
251 bool is_last_value = false;
252 int64_t stride_value = 0;
Aart Bik9abf8942016-10-14 09:49:42 -0700253 return GenerateRangeOrLastValue(context,
254 instruction,
255 is_last_value,
256 nullptr,
257 nullptr,
258 nullptr,
259 nullptr,
260 nullptr, // nothing generated yet
261 &stride_value,
262 needs_finite_test,
263 needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700264 && (stride_value == -1 ||
265 stride_value == 0 ||
Aart Bik40fbf742016-10-31 11:02:50 -0700266 stride_value == 1); // avoid arithmetic wrap-around anomalies.
Aart Bikaec3cce2015-10-14 17:44:55 -0700267}
268
Aart Bik16d3a652016-09-09 10:33:50 -0700269void InductionVarRange::GenerateRange(HInstruction* context,
270 HInstruction* instruction,
271 HGraph* graph,
272 HBasicBlock* block,
273 /*out*/HInstruction** lower,
274 /*out*/HInstruction** upper) {
275 bool is_last_value = false;
Aart Bik009cace2016-09-16 10:15:19 -0700276 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700277 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700278 if (!GenerateRangeOrLastValue(context,
279 instruction,
280 is_last_value,
281 graph,
282 block,
283 lower,
284 upper,
285 nullptr,
286 &stride_value,
287 &b1,
288 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700289 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
Aart Bik389b3db2015-10-28 14:23:40 -0700290 }
291}
292
Aart Bik16d3a652016-09-09 10:33:50 -0700293HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context,
294 HGraph* graph,
295 HBasicBlock* block) {
296 HInstruction* taken_test = nullptr;
297 bool is_last_value = false;
298 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700299 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700300 if (!GenerateRangeOrLastValue(context,
301 context,
302 is_last_value,
303 graph,
304 block,
305 nullptr,
306 nullptr,
307 &taken_test,
308 &stride_value,
309 &b1,
310 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700311 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
312 }
313 return taken_test;
314}
315
316bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) {
317 bool is_last_value = true;
318 int64_t stride_value = 0;
319 bool needs_finite_test = false;
320 bool needs_taken_test = false;
Aart Bik9abf8942016-10-14 09:49:42 -0700321 return GenerateRangeOrLastValue(instruction,
322 instruction,
323 is_last_value,
324 nullptr,
325 nullptr,
326 nullptr,
327 nullptr,
328 nullptr, // nothing generated yet
329 &stride_value,
330 &needs_finite_test,
331 &needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700332 && !needs_finite_test && !needs_taken_test;
333}
334
335HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction,
336 HGraph* graph,
337 HBasicBlock* block) {
338 HInstruction* last_value = nullptr;
339 bool is_last_value = true;
340 int64_t stride_value = 0;
341 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700342 if (!GenerateRangeOrLastValue(instruction,
343 instruction,
344 is_last_value,
345 graph,
346 block,
347 &last_value,
348 &last_value,
349 nullptr,
350 &stride_value,
351 &b1,
352 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700353 LOG(FATAL) << "Failed precondition: CanGenerateLastValue()";
354 }
355 return last_value;
356}
357
358void InductionVarRange::Replace(HInstruction* instruction,
359 HInstruction* fetch,
360 HInstruction* replacement) {
361 for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
362 lp != nullptr;
363 lp = lp->GetPreHeader()->GetLoopInformation()) {
Aart Bik009cace2016-09-16 10:15:19 -0700364 // Update instruction's information.
Aart Bik16d3a652016-09-09 10:33:50 -0700365 ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement);
Aart Bik009cace2016-09-16 10:15:19 -0700366 // Update loop's trip-count information.
367 ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement);
Aart Bik389b3db2015-10-28 14:23:40 -0700368 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700369}
370
Aart Bik9abf8942016-10-14 09:49:42 -0700371bool InductionVarRange::IsFinite(HLoopInformation* loop) const {
372 HInductionVarAnalysis::InductionInfo *trip =
373 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
374 return trip != nullptr && !IsUnsafeTripCount(trip);
375}
376
Aart Bikd14c5952015-09-08 15:25:15 -0700377//
378// Private class methods.
379//
380
Aart Bik97412c922016-02-19 20:14:38 -0800381bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
382 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700383 /*out*/ int64_t* value) const {
Aart Bik97412c922016-02-19 20:14:38 -0800384 if (info != nullptr) {
385 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
386 // any of the three requests (kExact, kAtMost, and KAtLeast).
387 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
388 info->operation == HInductionVarAnalysis::kFetch) {
389 if (IsIntAndGet(info->fetch, value)) {
390 return true;
391 }
392 }
Aart Bik40fbf742016-10-31 11:02:50 -0700393 // Try range analysis on the invariant, only accept a proper range
394 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700395 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
396 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
397 if (IsConstantValue(min_val) &&
398 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
399 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
400 *value = max_val.b_constant;
401 return true;
402 } else if (request == kAtLeast) {
403 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800404 return true;
405 }
406 }
Aart Bik97412c922016-02-19 20:14:38 -0800407 }
408 return false;
409}
410
Aart Bik52be7e72016-06-23 11:20:41 -0700411bool InductionVarRange::HasInductionInfo(
412 HInstruction* context,
413 HInstruction* instruction,
414 /*out*/ HLoopInformation** loop,
415 /*out*/ HInductionVarAnalysis::InductionInfo** info,
416 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800417 DCHECK(context != nullptr);
418 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700419 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
420 if (lp != nullptr) {
421 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700422 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700423 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700424 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700425 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700426 return true;
427 }
428 }
429 return false;
430}
431
432bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
433 if (trip != nullptr) {
434 // Both bounds that define a trip-count are well-behaved if they either are not defined
435 // in any loop, or are contained in a proper interval. This allows finding the min/max
436 // of an expression by chasing outward.
437 InductionVarRange range(induction_analysis_);
438 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
439 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
440 int64_t not_used = 0;
441 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
442 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
443 }
444 return true;
445}
446
447bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
448 if (info != nullptr) {
449 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
450 info->operation == HInductionVarAnalysis::kFetch) {
451 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
452 }
453 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
454 }
455 return false;
456}
457
Aart Bik16d3a652016-09-09 10:33:50 -0700458bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
459 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700460 if (info != nullptr) {
461 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700462 return IsConstant(info->op_a, kExact, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700463 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700464 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700465 }
Aart Bikd14c5952015-09-08 15:25:15 -0700466 }
Aart Bik389b3db2015-10-28 14:23:40 -0700467 return false;
468}
469
Aart Bik7d57d7f2015-12-09 14:39:48 -0800470bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700471 if (trip != nullptr) {
472 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
473 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
474 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
475 }
476 }
477 return false;
478}
479
Aart Bik7d57d7f2015-12-09 14:39:48 -0800480bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700481 if (trip != nullptr) {
482 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
483 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
484 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
485 }
486 }
487 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700488}
489
Aart Bik7d57d7f2015-12-09 14:39:48 -0800490InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
491 HInductionVarAnalysis::InductionInfo* trip,
492 bool in_body,
493 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800494 DCHECK(info != nullptr);
495 DCHECK(info->induction_class == HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700496 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800497 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
498 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
499 // with intermediate results that only incorporate single instructions.
500 if (trip != nullptr) {
501 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700502 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800503 int64_t stride_value = 0;
504 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800505 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800506 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800507 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
508 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
509 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700510 trip->induction_class,
511 trip->operation,
512 trip_expr->op_a,
513 trip->op_b,
514 nullptr,
515 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800516 return GetVal(&cancelled_trip, trip, in_body, is_min);
517 }
518 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800519 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800520 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
521 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
522 HInductionVarAnalysis::InductionInfo neg(
523 HInductionVarAnalysis::kInvariant,
524 HInductionVarAnalysis::kNeg,
525 nullptr,
526 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700527 nullptr,
528 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800529 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700530 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800531 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
532 }
533 }
534 }
535 }
536 }
537 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
538 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
539 GetVal(info->op_b, trip, in_body, is_min));
540}
541
Aart Bikc071a012016-12-01 10:22:31 -0800542InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
543 HInductionVarAnalysis::InductionInfo* trip,
544 bool in_body,
545 bool is_min) const {
546 DCHECK(info != nullptr);
547 DCHECK(info->induction_class == HInductionVarAnalysis::kGeometric);
548 int64_t a = 0;
549 int64_t f = 0;
550 if (IsConstant(info->op_a, kExact, &a) &&
551 CanLongValueFitIntoInt(a) &&
552 IsIntAndGet(info->fetch, &f) &&
553 CanLongValueFitIntoInt(f) && f >= 1) {
554 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without trip count.
555 // Same for mod. All other forms would require a much more elaborate evaluation.
556 const bool is_min_a = a >= 0 ? is_min : !is_min;
557 if (info->operation == HInductionVarAnalysis::kDiv) {
558 return AddValue(Value(is_min_a ? 0 : a), GetVal(info->op_b, trip, in_body, is_min));
559 } else if (info->operation == HInductionVarAnalysis::kNop) {
560 return AddValue(Value(is_min_a ? (a % f) : a), GetVal(info->op_b, trip, in_body, is_min));
561 }
562 }
563 return Value();
564}
565
Aart Bikd14c5952015-09-08 15:25:15 -0700566InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700567 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700568 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800569 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700570 // Special case when chasing constants: single instruction that denotes trip count in the
571 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
572 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700573 if (is_min) {
574 return Value(1);
Aart Bik40fbf742016-10-31 11:02:50 -0700575 } else if (!IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700576 return Value(std::numeric_limits<int32_t>::max());
577 }
578 }
Aart Bik40fbf742016-10-31 11:02:50 -0700579 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
580 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
581 int64_t value;
582 if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
583 // Proper constant reveals best information.
584 return Value(static_cast<int32_t>(value));
585 } else if (instruction == chase_hint_) {
586 // At hint, fetch is represented by itself.
587 return Value(instruction, 1, 0);
588 } else if (instruction->IsAdd()) {
589 // Incorporate suitable constants in the chased value.
Aart Bik97412c922016-02-19 20:14:38 -0800590 if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
591 return AddValue(Value(static_cast<int32_t>(value)),
592 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
593 } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
594 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
595 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700596 }
Aart Bik52be7e72016-06-23 11:20:41 -0700597 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700598 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700599 if (chase_hint_ == nullptr) {
600 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
601 } else if (instruction->InputAt(0)->IsNewArray()) {
602 return GetFetch(instruction->InputAt(0)->InputAt(0), trip, in_body, is_min);
603 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700604 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700605 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bik0d345cf2016-03-16 10:49:38 -0700606 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
607 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
608 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
609 }
Aart Bik52be7e72016-06-23 11:20:41 -0700610 }
611 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
612 // so far is well-behaved in both bounds and the next trip-count is safe.
613 // Example:
614 // for (int i = 0; i <= 100; i++) // safe
615 // for (int j = 0; j <= i; j++) // well-behaved
616 // j is in range [0, i ] (if i is chase hint)
617 // or in range [0, 100] (otherwise)
618 HLoopInformation* next_loop = nullptr;
619 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
620 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
621 bool next_in_body = true; // inner loop is always in body of outer loop
622 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
623 IsWellBehavedTripCount(trip) &&
624 !IsUnsafeTripCount(next_trip)) {
625 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700626 }
Aart Bik40fbf742016-10-31 11:02:50 -0700627 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700628 return Value(instruction, 1, 0);
629}
630
Aart Bikcd26feb2015-09-23 17:50:50 -0700631InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
632 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700633 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800634 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700635 if (info != nullptr) {
636 switch (info->induction_class) {
637 case HInductionVarAnalysis::kInvariant:
638 // Invariants.
639 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700640 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700641 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
642 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700643 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700644 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
645 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700646 case HInductionVarAnalysis::kNeg: // second reversed!
647 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700648 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700649 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700650 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700651 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700652 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bik7dc96932016-10-12 10:01:05 -0700653 case HInductionVarAnalysis::kXor:
654 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700655 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700656 return GetFetch(info->fetch, trip, in_body, is_min);
657 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700658 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700659 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700660 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700661 }
662 FALLTHROUGH_INTENDED;
663 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700664 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700665 if (is_min) {
666 return Value(0);
667 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700668 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700669 }
670 break;
671 default:
672 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700673 }
674 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700675 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700676 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800677 case HInductionVarAnalysis::kPolynomial:
678 break;
679 case HInductionVarAnalysis::kGeometric:
680 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700681 case HInductionVarAnalysis::kWrapAround:
682 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700683 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
684 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700685 }
686 }
Aart Bikb3365e02015-09-21 14:45:05 -0700687 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700688}
689
690InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
691 HInductionVarAnalysis::InductionInfo* info2,
692 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700693 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800694 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700695 // Constant times range.
696 int64_t value = 0;
697 if (IsConstant(info1, kExact, &value)) {
698 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
699 } else if (IsConstant(info2, kExact, &value)) {
700 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
701 }
702 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700703 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
704 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
705 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
706 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800707 // Positive range vs. positive or negative range.
708 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
709 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
710 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
711 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
712 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700713 }
Aart Bik97412c922016-02-19 20:14:38 -0800714 }
715 // Negative range vs. positive or negative range.
716 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
717 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
718 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
719 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
720 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700721 }
722 }
Aart Bikb3365e02015-09-21 14:45:05 -0700723 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700724}
725
726InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
727 HInductionVarAnalysis::InductionInfo* info2,
728 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700729 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800730 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700731 // Range divided by constant.
732 int64_t value = 0;
733 if (IsConstant(info2, kExact, &value)) {
734 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
735 }
736 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700737 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
738 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
739 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
740 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800741 // Positive range vs. positive or negative range.
742 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
743 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
744 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
745 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
746 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700747 }
Aart Bik97412c922016-02-19 20:14:38 -0800748 }
749 // Negative range vs. positive or negative range.
750 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
751 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
752 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
753 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
754 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700755 }
756 }
Aart Bikb3365e02015-09-21 14:45:05 -0700757 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700758}
759
Aart Bik7dc96932016-10-12 10:01:05 -0700760InductionVarRange::Value InductionVarRange::GetXor(
761 HInductionVarAnalysis::InductionInfo* info1,
762 HInductionVarAnalysis::InductionInfo* info2) const {
763 int64_t v1 = 0;
764 int64_t v2 = 0;
765 // Only accept exact values.
766 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
767 int64_t value = v1 ^ v2;
768 if (CanLongValueFitIntoInt(value)) {
769 return Value(static_cast<int32_t>(value));
770 }
771 }
772 return Value();
773}
774
Aart Bik52be7e72016-06-23 11:20:41 -0700775InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
776 int64_t value,
777 HInductionVarAnalysis::InductionInfo* info,
778 HInductionVarAnalysis::InductionInfo* trip,
779 bool in_body,
780 bool is_min) const {
781 if (CanLongValueFitIntoInt(value)) {
782 Value c(static_cast<int32_t>(value));
783 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
784 }
785 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800786}
787
Aart Bik52be7e72016-06-23 11:20:41 -0700788InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
789 int64_t value,
790 HInductionVarAnalysis::InductionInfo* info,
791 HInductionVarAnalysis::InductionInfo* trip,
792 bool in_body,
793 bool is_min) const {
794 if (CanLongValueFitIntoInt(value)) {
795 Value c(static_cast<int32_t>(value));
796 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
797 }
798 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700799}
800
Aart Bik7d57d7f2015-12-09 14:39:48 -0800801InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700802 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bikd14c5952015-09-08 15:25:15 -0700803 const int32_t b = v1.b_constant + v2.b_constant;
804 if (v1.a_constant == 0) {
805 return Value(v2.instruction, v2.a_constant, b);
806 } else if (v2.a_constant == 0) {
807 return Value(v1.instruction, v1.a_constant, b);
808 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
809 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
810 }
811 }
Aart Bikb3365e02015-09-21 14:45:05 -0700812 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700813}
814
Aart Bik7d57d7f2015-12-09 14:39:48 -0800815InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700816 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bikd14c5952015-09-08 15:25:15 -0700817 const int32_t b = v1.b_constant - v2.b_constant;
818 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
819 return Value(v2.instruction, -v2.a_constant, b);
820 } else if (v2.a_constant == 0) {
821 return Value(v1.instruction, v1.a_constant, b);
822 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
823 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
824 }
825 }
Aart Bikb3365e02015-09-21 14:45:05 -0700826 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700827}
828
Aart Bik7d57d7f2015-12-09 14:39:48 -0800829InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700830 if (v1.is_known && v2.is_known) {
831 if (v1.a_constant == 0) {
832 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
833 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
834 }
835 } else if (v2.a_constant == 0) {
836 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
837 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
838 }
Aart Bikd14c5952015-09-08 15:25:15 -0700839 }
840 }
Aart Bikb3365e02015-09-21 14:45:05 -0700841 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700842}
843
Aart Bik7d57d7f2015-12-09 14:39:48 -0800844InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700845 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700846 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
847 return Value(v1.b_constant / v2.b_constant);
848 }
849 }
Aart Bikb3365e02015-09-21 14:45:05 -0700850 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700851}
852
Aart Bik7d57d7f2015-12-09 14:39:48 -0800853InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700854 if (v1.is_known && v2.is_known) {
855 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700856 return Value(v1.instruction, v1.a_constant,
857 is_min ? std::min(v1.b_constant, v2.b_constant)
858 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700859 }
Aart Bikd14c5952015-09-08 15:25:15 -0700860 }
Aart Bikb3365e02015-09-21 14:45:05 -0700861 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700862}
863
Aart Bik9abf8942016-10-14 09:49:42 -0700864bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
865 HInstruction* instruction,
866 bool is_last_value,
867 HGraph* graph,
868 HBasicBlock* block,
869 /*out*/HInstruction** lower,
870 /*out*/HInstruction** upper,
871 /*out*/HInstruction** taken_test,
872 /*out*/int64_t* stride_value,
873 /*out*/bool* needs_finite_test,
874 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700875 HLoopInformation* loop = nullptr;
876 HInductionVarAnalysis::InductionInfo* info = nullptr;
877 HInductionVarAnalysis::InductionInfo* trip = nullptr;
878 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
879 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800880 }
881 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
882 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
883 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
884 // code does not use the trip-count explicitly (since there could be an implicit relation
885 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700886 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700887 *stride_value = 0;
888 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800889 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700890 // Handle last value request.
891 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800892 DCHECK(!in_body);
893 switch (info->induction_class) {
894 case HInductionVarAnalysis::kLinear:
895 if (*stride_value > 0) {
896 lower = nullptr;
897 } else {
898 upper = nullptr;
899 }
900 break;
901 case HInductionVarAnalysis::kGeometric:
902 return GenerateLastValueGeometric(info, trip, graph, block, lower);
903 case HInductionVarAnalysis::kPeriodic:
904 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
905 default:
906 return false;
Aart Bik16d3a652016-09-09 10:33:50 -0700907 }
908 }
Aart Bik97412c922016-02-19 20:14:38 -0800909 // Code generation for taken test: generate the code when requested or otherwise analyze
910 // if code generation is feasible when taken test is needed.
911 if (taken_test != nullptr) {
912 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
913 } else if (*needs_taken_test) {
914 if (!GenerateCode(
915 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
916 return false;
917 }
918 }
919 // Code generation for lower and upper.
920 return
921 // Success on lower if invariant (not set), or code can be generated.
922 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
923 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
924 // And success on upper.
925 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -0700926}
927
Aart Bikc071a012016-12-01 10:22:31 -0800928bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
929 HInductionVarAnalysis::InductionInfo* trip,
930 HGraph* graph,
931 HBasicBlock* block,
932 /*out*/HInstruction** result) const {
933 DCHECK(info != nullptr);
934 DCHECK(info->induction_class == HInductionVarAnalysis::kGeometric);
935 // Detect known base and trip count (always taken).
936 int64_t f = 0;
937 int64_t t = 0;
938 if (IsIntAndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &t) && t >= 1) {
939 HInstruction* opa = nullptr;
940 HInstruction* opb = nullptr;
941 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
942 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
943 // Generate a % f + b.
944 if (info->operation == HInductionVarAnalysis::kNop) {
945 if (graph != nullptr) {
946 HInstruction* rem =
947 Insert(block, new (graph->GetArena()) HRem(info->type, opa, info->fetch, kNoDexPc));
948 *result = Insert(block, new (graph->GetArena()) HAdd(info->type, rem, opb));
949 }
950 return true;
951 }
952 // Compute f ^ t.
953 int64_t fpowt = IntPow(f, t);
954 if (graph != nullptr) {
955 DCHECK(info->type == Primitive::kPrimInt); // due to codegen, generalize?
956 if (fpowt == 0) {
957 // Special case: repeated mul/div always yields zero.
958 *result = graph->GetIntConstant(0);
959 } else if (info->operation == HInductionVarAnalysis::kMul) {
960 // Last value multiplication: a * f ^ t + b.
961 HInstruction* mul = Insert(block,
962 new (graph->GetArena()) HMul(info->type,
963 opa,
964 graph->GetIntConstant(fpowt)));
965 *result = Insert(block, new (graph->GetArena()) HAdd(info->type, mul, opb));
966 } else {
967 // Last value multiplication: a * f ^ -t + b.
968 DCHECK_EQ(info->operation, HInductionVarAnalysis::kDiv);
969 HInstruction* div = Insert(block,
970 new (graph->GetArena()) HDiv(info->type,
971 opa,
972 graph->GetIntConstant(fpowt),
973 kNoDexPc));
974 *result = Insert(block, new (graph->GetArena()) HAdd(info->type, div, opb));
975 }
976 }
977 return true;
978 }
979 }
980 return false;
981}
982
Aart Bik9abf8942016-10-14 09:49:42 -0700983bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
984 HInductionVarAnalysis::InductionInfo* trip,
985 HGraph* graph,
986 HBasicBlock* block,
987 /*out*/HInstruction** result,
988 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -0800989 DCHECK(info != nullptr);
Aart Bik9abf8942016-10-14 09:49:42 -0700990 DCHECK(info->induction_class == HInductionVarAnalysis::kPeriodic);
991 // Count period.
992 int32_t period = 1;
993 for (HInductionVarAnalysis::InductionInfo* p = info;
994 p->induction_class == HInductionVarAnalysis::kPeriodic;
995 p = p->op_b, ++period) {}
996 // Handle periodic(x, y) case for restricted types.
997 if (period != 2 ||
998 trip->op_a->type != Primitive::kPrimInt ||
999 (info->type != Primitive::kPrimInt && info->type != Primitive::kPrimBoolean)) {
1000 return false; // TODO: easy to generalize
1001 }
1002 HInstruction* x_instr = nullptr;
1003 HInstruction* y_instr = nullptr;
1004 HInstruction* trip_expr = nullptr;
1005 if (GenerateCode(info->op_a, nullptr, graph, block, graph ? &x_instr : nullptr, false, false) &&
1006 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y_instr : nullptr, false, false) &&
1007 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &trip_expr : nullptr, false, false)) {
1008 // During actual code generation (graph != nullptr),
1009 // generate is_even ? x : y select instruction.
1010 if (graph != nullptr) {
1011 HInstruction* is_even = Insert(block, new (graph->GetArena()) HEqual(
1012 Insert(block, new (graph->GetArena()) HAnd(
1013 Primitive::kPrimInt, trip_expr, graph->GetIntConstant(1))),
1014 graph->GetIntConstant(0), kNoDexPc));
1015 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x_instr, y_instr, kNoDexPc));
1016 }
1017 // Guard select with taken test if needed.
1018 if (*needs_taken_test) {
1019 HInstruction* taken_test = nullptr;
1020 if (!GenerateCode(
1021 trip->op_b, nullptr, graph, block, graph ? &taken_test : nullptr, false, false)) {
1022 return false;
1023 } else if (graph != nullptr) {
1024 *result = Insert(block,
1025 new (graph->GetArena()) HSelect(taken_test, *result, x_instr, kNoDexPc));
1026 }
1027 *needs_taken_test = false; // taken care of
1028 }
1029 return true;
1030 }
1031 return false;
1032}
1033
Aart Bikaec3cce2015-10-14 17:44:55 -07001034bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1035 HInductionVarAnalysis::InductionInfo* trip,
1036 HGraph* graph, // when set, code is generated
1037 HBasicBlock* block,
1038 /*out*/HInstruction** result,
1039 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001040 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001041 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001042 // If during codegen, the result is not needed (nullptr), simply return success.
1043 if (graph != nullptr && result == nullptr) {
1044 return true;
1045 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001046 // Verify type safety.
Aart Bikc071a012016-12-01 10:22:31 -08001047 // TODO: generalize
Aart Bikaec3cce2015-10-14 17:44:55 -07001048 Primitive::Type type = Primitive::kPrimInt;
Aart Bik639cc8c2016-10-18 13:03:31 -07001049 if (info->type != Primitive::kPrimInt && info->type != Primitive::kPrimBoolean) {
Aart Bik0d345cf2016-03-16 10:49:38 -07001050 return false;
1051 }
1052 // Handle current operation.
Aart Bikaec3cce2015-10-14 17:44:55 -07001053 HInstruction* opa = nullptr;
1054 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001055 switch (info->induction_class) {
1056 case HInductionVarAnalysis::kInvariant:
Aart Bik40fbf742016-10-31 11:02:50 -07001057 // Invariants (note that even though is_min does not impact code generation for
1058 // invariants, some effort is made to keep this parameter consistent).
Aart Bikaec3cce2015-10-14 17:44:55 -07001059 switch (info->operation) {
1060 case HInductionVarAnalysis::kAdd:
Aart Bik40fbf742016-10-31 11:02:50 -07001061 case HInductionVarAnalysis::kXor: // no proper is_min for second arg
Aart Bik389b3db2015-10-28 14:23:40 -07001062 case HInductionVarAnalysis::kLT:
1063 case HInductionVarAnalysis::kLE:
1064 case HInductionVarAnalysis::kGT:
1065 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001066 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1067 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1068 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001069 HInstruction* operation = nullptr;
1070 switch (info->operation) {
1071 case HInductionVarAnalysis::kAdd:
1072 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001073 case HInductionVarAnalysis::kXor:
1074 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001075 case HInductionVarAnalysis::kLT:
1076 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1077 case HInductionVarAnalysis::kLE:
1078 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1079 case HInductionVarAnalysis::kGT:
1080 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1081 case HInductionVarAnalysis::kGE:
1082 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1083 default:
1084 LOG(FATAL) << "unknown operation";
1085 }
1086 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001087 }
1088 return true;
1089 }
1090 break;
1091 case HInductionVarAnalysis::kSub: // second reversed!
1092 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1093 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1094 if (graph != nullptr) {
1095 *result = Insert(block, new (graph->GetArena()) HSub(type, opa, opb));
1096 }
1097 return true;
1098 }
1099 break;
1100 case HInductionVarAnalysis::kNeg: // reversed!
1101 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1102 if (graph != nullptr) {
1103 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1104 }
1105 return true;
1106 }
1107 break;
1108 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001109 if (graph != nullptr) {
1110 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001111 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001112 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001113 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001114 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001115 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001116 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001117 }
1118 FALLTHROUGH_INTENDED;
1119 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001120 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001121 if (is_min) {
1122 if (graph != nullptr) {
1123 *result = graph->GetIntConstant(0);
1124 }
1125 return true;
1126 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001127 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001128 if (graph != nullptr) {
1129 *result = Insert(block,
1130 new (graph->GetArena())
1131 HSub(type, opb, graph->GetIntConstant(1)));
1132 }
1133 return true;
1134 }
1135 }
1136 break;
1137 default:
1138 break;
1139 }
1140 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001141 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001142 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1143 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1144 // are harder to guard against. For a last value, requesting min/max based on any
1145 // stride yields right value.
Aart Bik97412c922016-02-19 20:14:38 -08001146 int64_t stride_value = 0;
1147 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik16d3a652016-09-09 10:33:50 -07001148 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1149 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1150 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1151 if (graph != nullptr) {
1152 HInstruction* oper;
1153 if (stride_value == 1) {
1154 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1155 } else if (stride_value == -1) {
1156 oper = new (graph->GetArena()) HSub(type, opb, opa);
1157 } else {
Aart Bik009cace2016-09-16 10:15:19 -07001158 HInstruction* mul = new (graph->GetArena()) HMul(
1159 type, graph->GetIntConstant(stride_value), opa);
Aart Bik16d3a652016-09-09 10:33:50 -07001160 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
Aart Bikaec3cce2015-10-14 17:44:55 -07001161 }
Aart Bik16d3a652016-09-09 10:33:50 -07001162 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001163 }
Aart Bik16d3a652016-09-09 10:33:50 -07001164 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001165 }
1166 }
1167 break;
Aart Bik4a342772015-11-30 10:17:46 -08001168 }
Aart Bikc071a012016-12-01 10:22:31 -08001169 case HInductionVarAnalysis::kPolynomial:
1170 case HInductionVarAnalysis::kGeometric:
1171 break;
Aart Bik4a342772015-11-30 10:17:46 -08001172 case HInductionVarAnalysis::kWrapAround:
1173 case HInductionVarAnalysis::kPeriodic: {
1174 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1175 // values are easy to test at runtime without complications of arithmetic wrap-around.
1176 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001177 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001178 if (graph != nullptr) {
1179 *result = graph->GetIntConstant(extreme.b_constant);
1180 }
1181 return true;
1182 }
1183 break;
1184 }
Aart Bikaec3cce2015-10-14 17:44:55 -07001185 }
1186 }
1187 return false;
1188}
1189
Aart Bik16d3a652016-09-09 10:33:50 -07001190void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1191 HInstruction* fetch,
1192 HInstruction* replacement) {
1193 if (info != nullptr) {
1194 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1195 info->operation == HInductionVarAnalysis::kFetch &&
1196 info->fetch == fetch) {
1197 info->fetch = replacement;
1198 }
1199 ReplaceInduction(info->op_a, fetch, replacement);
1200 ReplaceInduction(info->op_b, fetch, replacement);
1201 }
1202}
1203
Aart Bikd14c5952015-09-08 15:25:15 -07001204} // namespace art