blob: d5c4c2fa69b12031936376c816e3696cea69979e [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
Aart Bike6bd0272016-12-16 13:57:52 -0800172 int32_t min = Primitive::MinValueOfIntegralType(type);
173 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 Bikdf7822e2016-12-06 10:05:30 -0800463 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
464 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700465 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700466 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700467 }
Aart Bikd14c5952015-09-08 15:25:15 -0700468 }
Aart Bik389b3db2015-10-28 14:23:40 -0700469 return false;
470}
471
Aart Bik7d57d7f2015-12-09 14:39:48 -0800472bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700473 if (trip != nullptr) {
474 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
475 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
476 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
477 }
478 }
479 return false;
480}
481
Aart Bik7d57d7f2015-12-09 14:39:48 -0800482bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700483 if (trip != nullptr) {
484 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
485 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
486 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
487 }
488 }
489 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700490}
491
Aart Bik7d57d7f2015-12-09 14:39:48 -0800492InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
493 HInductionVarAnalysis::InductionInfo* trip,
494 bool in_body,
495 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800496 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800497 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700498 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800499 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
500 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
501 // with intermediate results that only incorporate single instructions.
502 if (trip != nullptr) {
503 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700504 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800505 int64_t stride_value = 0;
506 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800507 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800508 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800509 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
510 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
511 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700512 trip->induction_class,
513 trip->operation,
514 trip_expr->op_a,
515 trip->op_b,
516 nullptr,
517 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800518 return GetVal(&cancelled_trip, trip, in_body, is_min);
519 }
520 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800521 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800522 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
523 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
524 HInductionVarAnalysis::InductionInfo neg(
525 HInductionVarAnalysis::kInvariant,
526 HInductionVarAnalysis::kNeg,
527 nullptr,
528 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700529 nullptr,
530 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800531 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700532 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800533 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
534 }
535 }
536 }
537 }
538 }
539 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
540 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
541 GetVal(info->op_b, trip, in_body, is_min));
542}
543
Aart Bikdf7822e2016-12-06 10:05:30 -0800544InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
545 HInductionVarAnalysis::InductionInfo* trip,
546 bool in_body,
547 bool is_min) const {
548 DCHECK(info != nullptr);
549 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
550 int64_t a = 0;
551 int64_t b = 0;
552 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
553 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800554 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800555 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
556 Value c = GetVal(info->op_b, trip, in_body, is_min);
557 if (is_min) {
558 return c;
559 } else {
560 Value m = GetVal(trip, trip, in_body, is_min);
561 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
562 Value x = MulValue(Value(a), t);
563 Value y = MulValue(Value(b), m);
564 return AddValue(AddValue(x, y), c);
565 }
566 }
567 return Value();
568}
569
Aart Bikc071a012016-12-01 10:22:31 -0800570InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
571 HInductionVarAnalysis::InductionInfo* trip,
572 bool in_body,
573 bool is_min) const {
574 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800575 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800576 int64_t a = 0;
577 int64_t f = 0;
578 if (IsConstant(info->op_a, kExact, &a) &&
579 CanLongValueFitIntoInt(a) &&
Aart Bikdf7822e2016-12-06 10:05:30 -0800580 IsIntAndGet(info->fetch, &f) && f >= 1) {
581 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
582 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800583 const bool is_min_a = a >= 0 ? is_min : !is_min;
584 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800585 Value b = GetVal(info->op_b, trip, in_body, is_min);
586 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800587 }
588 }
589 return Value();
590}
591
Aart Bikd14c5952015-09-08 15:25:15 -0700592InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700593 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700594 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800595 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700596 // Special case when chasing constants: single instruction that denotes trip count in the
597 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
598 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700599 if (is_min) {
600 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800601 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700602 return Value(std::numeric_limits<int32_t>::max());
603 }
604 }
Aart Bik40fbf742016-10-31 11:02:50 -0700605 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
606 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
607 int64_t value;
608 if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
609 // Proper constant reveals best information.
610 return Value(static_cast<int32_t>(value));
611 } else if (instruction == chase_hint_) {
612 // At hint, fetch is represented by itself.
613 return Value(instruction, 1, 0);
614 } else if (instruction->IsAdd()) {
615 // Incorporate suitable constants in the chased value.
Aart Bik97412c922016-02-19 20:14:38 -0800616 if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
617 return AddValue(Value(static_cast<int32_t>(value)),
618 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
619 } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
620 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
621 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700622 }
Aart Bik52be7e72016-06-23 11:20:41 -0700623 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700624 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700625 if (chase_hint_ == nullptr) {
626 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
627 } else if (instruction->InputAt(0)->IsNewArray()) {
628 return GetFetch(instruction->InputAt(0)->InputAt(0), trip, in_body, is_min);
629 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700630 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700631 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800632 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Aart Bik0d345cf2016-03-16 10:49:38 -0700633 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
634 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
635 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
636 }
Aart Bik52be7e72016-06-23 11:20:41 -0700637 }
638 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
639 // so far is well-behaved in both bounds and the next trip-count is safe.
640 // Example:
641 // for (int i = 0; i <= 100; i++) // safe
642 // for (int j = 0; j <= i; j++) // well-behaved
643 // j is in range [0, i ] (if i is chase hint)
644 // or in range [0, 100] (otherwise)
645 HLoopInformation* next_loop = nullptr;
646 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
647 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
648 bool next_in_body = true; // inner loop is always in body of outer loop
649 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
650 IsWellBehavedTripCount(trip) &&
651 !IsUnsafeTripCount(next_trip)) {
652 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700653 }
Aart Bik40fbf742016-10-31 11:02:50 -0700654 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700655 return Value(instruction, 1, 0);
656}
657
Aart Bikcd26feb2015-09-23 17:50:50 -0700658InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
659 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700660 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800661 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700662 if (info != nullptr) {
663 switch (info->induction_class) {
664 case HInductionVarAnalysis::kInvariant:
665 // Invariants.
666 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700667 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700668 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
669 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700670 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700671 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
672 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700673 case HInductionVarAnalysis::kNeg: // second reversed!
674 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700675 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700676 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700677 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700678 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700679 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800680 case HInductionVarAnalysis::kRem:
681 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700682 case HInductionVarAnalysis::kXor:
683 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700684 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700685 return GetFetch(info->fetch, trip, in_body, is_min);
686 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700687 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700688 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700689 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700690 }
691 FALLTHROUGH_INTENDED;
692 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700693 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700694 if (is_min) {
695 return Value(0);
696 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700697 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700698 }
699 break;
700 default:
701 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700702 }
703 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700704 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700705 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800706 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800707 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800708 case HInductionVarAnalysis::kGeometric:
709 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700710 case HInductionVarAnalysis::kWrapAround:
711 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700712 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
713 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700714 }
715 }
Aart Bikb3365e02015-09-21 14:45:05 -0700716 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700717}
718
719InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
720 HInductionVarAnalysis::InductionInfo* info2,
721 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700722 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800723 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700724 // Constant times range.
725 int64_t value = 0;
726 if (IsConstant(info1, kExact, &value)) {
727 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
728 } else if (IsConstant(info2, kExact, &value)) {
729 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
730 }
731 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700732 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
733 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
734 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
735 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800736 // Positive range vs. positive or negative range.
737 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
738 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
739 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
740 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
741 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700742 }
Aart Bik97412c922016-02-19 20:14:38 -0800743 }
744 // Negative range vs. positive or negative range.
745 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
746 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
747 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
748 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
749 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700750 }
751 }
Aart Bikb3365e02015-09-21 14:45:05 -0700752 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700753}
754
755InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
756 HInductionVarAnalysis::InductionInfo* info2,
757 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700758 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800759 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700760 // Range divided by constant.
761 int64_t value = 0;
762 if (IsConstant(info2, kExact, &value)) {
763 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
764 }
765 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700766 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
767 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
768 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
769 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800770 // Positive range vs. positive or negative range.
771 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
772 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
773 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
774 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
775 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700776 }
Aart Bik97412c922016-02-19 20:14:38 -0800777 }
778 // Negative range vs. positive or negative range.
779 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
780 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
781 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
782 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
783 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700784 }
785 }
Aart Bikb3365e02015-09-21 14:45:05 -0700786 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700787}
788
Aart Bikdf7822e2016-12-06 10:05:30 -0800789InductionVarRange::Value InductionVarRange::GetRem(
790 HInductionVarAnalysis::InductionInfo* info1,
791 HInductionVarAnalysis::InductionInfo* info2) const {
792 int64_t v1 = 0;
793 int64_t v2 = 0;
794 // Only accept exact values.
795 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
796 int64_t value = v1 % v2;
797 if (CanLongValueFitIntoInt(value)) {
798 return Value(static_cast<int32_t>(value));
799 }
800 }
801 return Value();
802}
803
Aart Bik7dc96932016-10-12 10:01:05 -0700804InductionVarRange::Value InductionVarRange::GetXor(
805 HInductionVarAnalysis::InductionInfo* info1,
806 HInductionVarAnalysis::InductionInfo* info2) const {
807 int64_t v1 = 0;
808 int64_t v2 = 0;
809 // Only accept exact values.
810 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
811 int64_t value = v1 ^ v2;
812 if (CanLongValueFitIntoInt(value)) {
813 return Value(static_cast<int32_t>(value));
814 }
815 }
816 return Value();
817}
818
Aart Bik52be7e72016-06-23 11:20:41 -0700819InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
820 int64_t value,
821 HInductionVarAnalysis::InductionInfo* info,
822 HInductionVarAnalysis::InductionInfo* trip,
823 bool in_body,
824 bool is_min) const {
825 if (CanLongValueFitIntoInt(value)) {
826 Value c(static_cast<int32_t>(value));
827 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
828 }
829 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800830}
831
Aart Bik52be7e72016-06-23 11:20:41 -0700832InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
833 int64_t value,
834 HInductionVarAnalysis::InductionInfo* info,
835 HInductionVarAnalysis::InductionInfo* trip,
836 bool in_body,
837 bool is_min) const {
838 if (CanLongValueFitIntoInt(value)) {
839 Value c(static_cast<int32_t>(value));
840 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
841 }
842 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700843}
844
Aart Bik7d57d7f2015-12-09 14:39:48 -0800845InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700846 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800847 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700848 if (v1.a_constant == 0) {
849 return Value(v2.instruction, v2.a_constant, b);
850 } else if (v2.a_constant == 0) {
851 return Value(v1.instruction, v1.a_constant, b);
852 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
853 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
854 }
855 }
Aart Bikb3365e02015-09-21 14:45:05 -0700856 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700857}
858
Aart Bik7d57d7f2015-12-09 14:39:48 -0800859InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700860 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800861 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700862 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
863 return Value(v2.instruction, -v2.a_constant, b);
864 } else if (v2.a_constant == 0) {
865 return Value(v1.instruction, v1.a_constant, b);
866 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
867 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
868 }
869 }
Aart Bikb3365e02015-09-21 14:45:05 -0700870 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700871}
872
Aart Bik7d57d7f2015-12-09 14:39:48 -0800873InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700874 if (v1.is_known && v2.is_known) {
875 if (v1.a_constant == 0) {
876 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
877 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
878 }
879 } else if (v2.a_constant == 0) {
880 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
881 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
882 }
Aart Bikd14c5952015-09-08 15:25:15 -0700883 }
884 }
Aart Bikb3365e02015-09-21 14:45:05 -0700885 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700886}
887
Aart Bik7d57d7f2015-12-09 14:39:48 -0800888InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700889 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700890 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
891 return Value(v1.b_constant / v2.b_constant);
892 }
893 }
Aart Bikb3365e02015-09-21 14:45:05 -0700894 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700895}
896
Aart Bik7d57d7f2015-12-09 14:39:48 -0800897InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700898 if (v1.is_known && v2.is_known) {
899 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700900 return Value(v1.instruction, v1.a_constant,
901 is_min ? std::min(v1.b_constant, v2.b_constant)
902 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700903 }
Aart Bikd14c5952015-09-08 15:25:15 -0700904 }
Aart Bikb3365e02015-09-21 14:45:05 -0700905 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700906}
907
Aart Bik9abf8942016-10-14 09:49:42 -0700908bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
909 HInstruction* instruction,
910 bool is_last_value,
911 HGraph* graph,
912 HBasicBlock* block,
913 /*out*/HInstruction** lower,
914 /*out*/HInstruction** upper,
915 /*out*/HInstruction** taken_test,
916 /*out*/int64_t* stride_value,
917 /*out*/bool* needs_finite_test,
918 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700919 HLoopInformation* loop = nullptr;
920 HInductionVarAnalysis::InductionInfo* info = nullptr;
921 HInductionVarAnalysis::InductionInfo* trip = nullptr;
922 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
923 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800924 }
925 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
926 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
927 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
928 // code does not use the trip-count explicitly (since there could be an implicit relation
929 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700930 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700931 *stride_value = 0;
932 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800933 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700934 // Handle last value request.
935 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800936 DCHECK(!in_body);
937 switch (info->induction_class) {
938 case HInductionVarAnalysis::kLinear:
939 if (*stride_value > 0) {
940 lower = nullptr;
941 } else {
942 upper = nullptr;
943 }
944 break;
Aart Bikdf7822e2016-12-06 10:05:30 -0800945 case HInductionVarAnalysis::kPolynomial:
946 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -0800947 case HInductionVarAnalysis::kGeometric:
948 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -0800949 case HInductionVarAnalysis::kWrapAround:
950 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -0800951 case HInductionVarAnalysis::kPeriodic:
952 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
953 default:
954 return false;
Aart Bik16d3a652016-09-09 10:33:50 -0700955 }
956 }
Aart Bik97412c922016-02-19 20:14:38 -0800957 // Code generation for taken test: generate the code when requested or otherwise analyze
958 // if code generation is feasible when taken test is needed.
959 if (taken_test != nullptr) {
960 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
961 } else if (*needs_taken_test) {
962 if (!GenerateCode(
963 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
964 return false;
965 }
966 }
967 // Code generation for lower and upper.
968 return
969 // Success on lower if invariant (not set), or code can be generated.
970 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
971 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
972 // And success on upper.
973 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -0700974}
975
Aart Bikdf7822e2016-12-06 10:05:30 -0800976bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
977 HInductionVarAnalysis::InductionInfo* trip,
978 HGraph* graph,
979 HBasicBlock* block,
980 /*out*/HInstruction** result) const {
981 DCHECK(info != nullptr);
982 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
983 // Detect known coefficients and trip count (always taken).
984 int64_t a = 0;
985 int64_t b = 0;
986 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -0800987 if (IsConstant(info->op_a->op_a, kExact, &a) &&
988 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -0800989 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -0800990 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -0800991 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -0800992 HInstruction* c = nullptr;
993 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800994 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -0800995 Primitive::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -0800996 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Aart Bike6bd0272016-12-16 13:57:52 -0800997 if (type != Primitive::kPrimLong) {
998 sum = static_cast<int32_t>(sum); // okay to truncate
999 }
1000 *result =
1001 Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001002 }
1003 return true;
1004 }
1005 }
1006 return false;
1007}
1008
Aart Bikc071a012016-12-01 10:22:31 -08001009bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1010 HInductionVarAnalysis::InductionInfo* trip,
1011 HGraph* graph,
1012 HBasicBlock* block,
1013 /*out*/HInstruction** result) const {
1014 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001015 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001016 // Detect known base and trip count (always taken).
1017 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001018 int64_t m = 0;
1019 if (IsIntAndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001020 HInstruction* opa = nullptr;
1021 HInstruction* opb = nullptr;
1022 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1023 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bike6bd0272016-12-16 13:57:52 -08001024 // Compute f ^ m for known maximum index value m.
1025 int64_t fpow = IntPow(f, m);
Aart Bikc071a012016-12-01 10:22:31 -08001026 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001027 DCHECK(info->operation == HInductionVarAnalysis::kMul ||
1028 info->operation == HInductionVarAnalysis::kDiv);
1029 Primitive::Type type = info->type;
1030 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001031 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001032 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001033 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001034 // Last value: a * f ^ m + b or a * f ^ -m + b.
1035 if (type != Primitive::kPrimLong) {
1036 fpow = static_cast<int32_t>(fpow); // okay to truncate
1037 }
1038 HInstruction* e = nullptr;
1039 if (info->operation == HInductionVarAnalysis::kMul) {
1040 e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow));
1041 } else {
1042 e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
1043 }
1044 *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001045 }
1046 }
1047 return true;
1048 }
1049 }
1050 return false;
1051}
1052
Aart Bikdf7822e2016-12-06 10:05:30 -08001053bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1054 HInductionVarAnalysis::InductionInfo* trip,
1055 HGraph* graph,
1056 HBasicBlock* block,
1057 /*out*/HInstruction** result) const {
1058 DCHECK(info != nullptr);
1059 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1060 // Count depth.
1061 int32_t depth = 0;
1062 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1063 info = info->op_b, ++depth) {}
1064 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001065 // TODO: generalize, but be careful to adjust the terminal.
1066 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001067 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001068 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1069 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001070 }
1071 return false;
1072}
1073
Aart Bik9abf8942016-10-14 09:49:42 -07001074bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1075 HInductionVarAnalysis::InductionInfo* trip,
1076 HGraph* graph,
1077 HBasicBlock* block,
1078 /*out*/HInstruction** result,
1079 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001080 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001081 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik9abf8942016-10-14 09:49:42 -07001082 // Count period.
Aart Bike6bd0272016-12-16 13:57:52 -08001083 int64_t period = 1;
Aart Bik9abf8942016-10-14 09:49:42 -07001084 for (HInductionVarAnalysis::InductionInfo* p = info;
1085 p->induction_class == HInductionVarAnalysis::kPeriodic;
1086 p = p->op_b, ++period) {}
Aart Bike6bd0272016-12-16 13:57:52 -08001087 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1088 int64_t m = 0;
1089 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1090 int64_t li = m % period;
1091 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1092 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1093 info = info->op_a;
1094 }
1095 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001096 }
Aart Bike6bd0272016-12-16 13:57:52 -08001097 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1098 // directly to obtain the maximum index value t even if taken test is needed.
1099 HInstruction* x = nullptr;
1100 HInstruction* y = nullptr;
1101 HInstruction* t = nullptr;
1102 if (period == 2 &&
1103 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1104 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1105 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1106 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001107 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001108 Primitive::Type type = trip->type;
1109 HInstruction* msk =
1110 Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1)));
1111 HInstruction* is_even =
1112 Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1113 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001114 }
1115 // Guard select with taken test if needed.
1116 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001117 HInstruction* is_taken = nullptr;
1118 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1119 if (graph != nullptr) {
1120 *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc));
1121 }
1122 *needs_taken_test = false; // taken care of
1123 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001124 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001125 }
Aart Bik9abf8942016-10-14 09:49:42 -07001126 }
1127 return true;
1128 }
1129 return false;
1130}
1131
Aart Bikaec3cce2015-10-14 17:44:55 -07001132bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1133 HInductionVarAnalysis::InductionInfo* trip,
1134 HGraph* graph, // when set, code is generated
1135 HBasicBlock* block,
1136 /*out*/HInstruction** result,
1137 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001138 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001139 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001140 // If during codegen, the result is not needed (nullptr), simply return success.
1141 if (graph != nullptr && result == nullptr) {
1142 return true;
1143 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001144 // Handle current operation.
Aart Bike6bd0272016-12-16 13:57:52 -08001145 Primitive::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001146 HInstruction* opa = nullptr;
1147 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001148 switch (info->induction_class) {
1149 case HInductionVarAnalysis::kInvariant:
Aart Bik40fbf742016-10-31 11:02:50 -07001150 // Invariants (note that even though is_min does not impact code generation for
1151 // invariants, some effort is made to keep this parameter consistent).
Aart Bikaec3cce2015-10-14 17:44:55 -07001152 switch (info->operation) {
1153 case HInductionVarAnalysis::kAdd:
Aart Bikdf7822e2016-12-06 10:05:30 -08001154 case HInductionVarAnalysis::kRem: // no proper is_min for second arg
Aart Bik40fbf742016-10-31 11:02:50 -07001155 case HInductionVarAnalysis::kXor: // no proper is_min for second arg
Aart Bik389b3db2015-10-28 14:23:40 -07001156 case HInductionVarAnalysis::kLT:
1157 case HInductionVarAnalysis::kLE:
1158 case HInductionVarAnalysis::kGT:
1159 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001160 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1161 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1162 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001163 HInstruction* operation = nullptr;
1164 switch (info->operation) {
1165 case HInductionVarAnalysis::kAdd:
1166 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001167 case HInductionVarAnalysis::kRem:
1168 operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001169 case HInductionVarAnalysis::kXor:
1170 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001171 case HInductionVarAnalysis::kLT:
1172 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1173 case HInductionVarAnalysis::kLE:
1174 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1175 case HInductionVarAnalysis::kGT:
1176 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1177 case HInductionVarAnalysis::kGE:
1178 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1179 default:
1180 LOG(FATAL) << "unknown operation";
1181 }
1182 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001183 }
1184 return true;
1185 }
1186 break;
1187 case HInductionVarAnalysis::kSub: // second reversed!
1188 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1189 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1190 if (graph != nullptr) {
1191 *result = Insert(block, new (graph->GetArena()) HSub(type, opa, opb));
1192 }
1193 return true;
1194 }
1195 break;
1196 case HInductionVarAnalysis::kNeg: // reversed!
1197 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1198 if (graph != nullptr) {
1199 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1200 }
1201 return true;
1202 }
1203 break;
1204 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001205 if (graph != nullptr) {
1206 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001207 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001208 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001209 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001210 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001211 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001212 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001213 }
1214 FALLTHROUGH_INTENDED;
1215 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001216 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001217 if (is_min) {
1218 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001219 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001220 }
1221 return true;
1222 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001223 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001224 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001225 *result =
1226 Insert(block,
1227 new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001228 }
1229 return true;
1230 }
1231 }
1232 break;
1233 default:
1234 break;
1235 }
1236 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001237 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001238 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1239 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1240 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001241 // known stride yields right value. Always avoid any narrowing linear induction or
1242 // any type mismatch between the linear induction and the trip count expression.
1243 // TODO: careful runtime type conversions could generalize this latter restriction.
1244 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1245 int64_t stride_value = 0;
1246 if (IsConstant(info->op_a, kExact, &stride_value) &&
1247 CanLongValueFitIntoInt(stride_value)) {
1248 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1249 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1250 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1251 if (graph != nullptr) {
1252 HInstruction* oper;
1253 if (stride_value == 1) {
1254 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1255 } else if (stride_value == -1) {
1256 oper = new (graph->GetArena()) HSub(type, opb, opa);
1257 } else {
1258 HInstruction* mul =
1259 new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa);
1260 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
1261 }
1262 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001263 }
Aart Bike6bd0272016-12-16 13:57:52 -08001264 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001265 }
1266 }
1267 }
1268 break;
Aart Bik4a342772015-11-30 10:17:46 -08001269 }
Aart Bikc071a012016-12-01 10:22:31 -08001270 case HInductionVarAnalysis::kPolynomial:
1271 case HInductionVarAnalysis::kGeometric:
1272 break;
Aart Bik4a342772015-11-30 10:17:46 -08001273 case HInductionVarAnalysis::kWrapAround:
1274 case HInductionVarAnalysis::kPeriodic: {
1275 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1276 // values are easy to test at runtime without complications of arithmetic wrap-around.
1277 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001278 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001279 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001280 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001281 }
1282 return true;
1283 }
1284 break;
1285 }
Aart Bikaec3cce2015-10-14 17:44:55 -07001286 }
1287 }
1288 return false;
1289}
1290
Aart Bik16d3a652016-09-09 10:33:50 -07001291void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1292 HInstruction* fetch,
1293 HInstruction* replacement) {
1294 if (info != nullptr) {
1295 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1296 info->operation == HInductionVarAnalysis::kFetch &&
1297 info->fetch == fetch) {
1298 info->fetch = replacement;
1299 }
1300 ReplaceInduction(info->op_a, fetch, replacement);
1301 ReplaceInduction(info->op_b, fetch, replacement);
1302 }
1303}
1304
Aart Bikd14c5952015-09-08 15:25:15 -07001305} // namespace art