blob: 089340e71566c17d14999e48b2843a13ae4d6c6c [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 Bikb603a5c2017-03-06 18:29:39 -080048/** Computes a * b for a,b > 0 (at least until first overflow happens). */
49static int64_t SafeMul(int64_t a, int64_t b, /*out*/ bool* overflow) {
50 if (a > 0 && b > 0 && a > (std::numeric_limits<int64_t>::max() / b)) {
51 *overflow = true;
52 }
53 return a * b;
54}
55
56/** Returns b^e for b,e > 0. Sets overflow if arithmetic wrap-around occurred. */
Aart Bikd3ba6262017-01-30 14:37:12 -080057static int64_t IntPow(int64_t b, int64_t e, /*out*/ bool* overflow) {
Aart Bikb603a5c2017-03-06 18:29:39 -080058 DCHECK_LT(0, b);
59 DCHECK_LT(0, e);
Aart Bikc071a012016-12-01 10:22:31 -080060 int64_t pow = 1;
61 while (e) {
62 if (e & 1) {
Aart Bikb603a5c2017-03-06 18:29:39 -080063 pow = SafeMul(pow, b, overflow);
Aart Bikc071a012016-12-01 10:22:31 -080064 }
65 e >>= 1;
Aart Bikb603a5c2017-03-06 18:29:39 -080066 if (e) {
67 b = SafeMul(b, b, overflow);
68 }
Aart Bikc071a012016-12-01 10:22:31 -080069 }
70 return pow;
71}
72
Aart Bikb3365e02015-09-21 14:45:05 -070073/**
Aart Bik40fbf742016-10-31 11:02:50 -070074 * Detects an instruction that is >= 0. As long as the value is carried by
75 * a single instruction, arithmetic wrap-around cannot occur.
Aart Bikb3365e02015-09-21 14:45:05 -070076 */
Aart Bik40fbf742016-10-31 11:02:50 -070077static bool IsGEZero(HInstruction* instruction) {
78 DCHECK(instruction != nullptr);
79 if (instruction->IsArrayLength()) {
80 return true;
81 } else if (instruction->IsInvokeStaticOrDirect()) {
82 switch (instruction->AsInvoke()->GetIntrinsic()) {
83 case Intrinsics::kMathMinIntInt:
84 case Intrinsics::kMathMinLongLong:
85 // Instruction MIN(>=0, >=0) is >= 0.
86 return IsGEZero(instruction->InputAt(0)) &&
87 IsGEZero(instruction->InputAt(1));
88 case Intrinsics::kMathAbsInt:
89 case Intrinsics::kMathAbsLong:
Aart Bik7f56ff42017-08-30 10:20:47 -070090 // Instruction ABS(>=0) is >= 0.
91 // NOTE: ABS(minint) = minint prevents assuming
92 // >= 0 without looking at the argument.
93 return IsGEZero(instruction->InputAt(0));
Aart Bik40fbf742016-10-31 11:02:50 -070094 default:
95 break;
96 }
97 }
98 int64_t value = -1;
Aart Bikf3e61ee2017-04-12 17:09:20 -070099 return IsInt64AndGet(instruction, &value) && value >= 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700100}
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;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000117 return HuntForDeclaration(instruction) == hint;
Aart Bik40fbf742016-10-31 11:02:50 -0700118 }
119 return false;
120}
121
122/** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */
123static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) {
124 if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) {
125 // If a == 1, instruction >= 0 and b <= 0, just return the constant b.
126 // No arithmetic wrap-around can occur.
127 if (IsGEZero(v.instruction)) {
128 return InductionVarRange::Value(v.b_constant);
129 }
Aart Bikb3365e02015-09-21 14:45:05 -0700130 }
131 return v;
132}
133
Aart Bik40fbf742016-10-31 11:02:50 -0700134/** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */
135static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) {
136 if (v.is_known && v.a_constant >= 1) {
137 // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as
138 // length + b because length >= 0 is true.
139 int64_t value;
140 if (v.instruction->IsDiv() &&
141 v.instruction->InputAt(0)->IsArrayLength() &&
Aart Bikf3e61ee2017-04-12 17:09:20 -0700142 IsInt64AndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
Aart Bik40fbf742016-10-31 11:02:50 -0700143 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
144 }
145 // If a == 1, the most suitable one suffices as maximum value.
146 HInstruction* suitable = nullptr;
147 if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) {
148 return InductionVarRange::Value(suitable, 1, v.b_constant);
149 }
150 }
151 return v;
152}
153
154/** Tests for a constant value. */
Aart Bik52be7e72016-06-23 11:20:41 -0700155static bool IsConstantValue(InductionVarRange::Value v) {
156 return v.is_known && v.a_constant == 0;
157}
158
159/** Corrects a value for type to account for arithmetic wrap-around in lower precision. */
Aart Bik0d345cf2016-03-16 10:49:38 -0700160static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, Primitive::Type type) {
161 switch (type) {
162 case Primitive::kPrimShort:
163 case Primitive::kPrimChar:
164 case Primitive::kPrimByte: {
165 // Constants within range only.
166 // TODO: maybe some room for improvement, like allowing widening conversions
Aart Bike6bd0272016-12-16 13:57:52 -0800167 int32_t min = Primitive::MinValueOfIntegralType(type);
168 int32_t max = Primitive::MaxValueOfIntegralType(type);
Aart Bik52be7e72016-06-23 11:20:41 -0700169 return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max)
Aart Bik0d345cf2016-03-16 10:49:38 -0700170 ? v
171 : InductionVarRange::Value();
172 }
173 default:
Aart Bik0d345cf2016-03-16 10:49:38 -0700174 return v;
175 }
176}
177
Aart Bik40fbf742016-10-31 11:02:50 -0700178/** Inserts an instruction. */
Aart Bik389b3db2015-10-28 14:23:40 -0700179static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
180 DCHECK(block != nullptr);
181 DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId();
Aart Bikaec3cce2015-10-14 17:44:55 -0700182 DCHECK(instruction != nullptr);
Aart Bik389b3db2015-10-28 14:23:40 -0700183 block->InsertInstructionBefore(instruction, block->GetLastInstruction());
Aart Bikaec3cce2015-10-14 17:44:55 -0700184 return instruction;
185}
186
Aart Bik40fbf742016-10-31 11:02:50 -0700187/** Obtains loop's control instruction. */
Aart Bik009cace2016-09-16 10:15:19 -0700188static HInstruction* GetLoopControl(HLoopInformation* loop) {
189 DCHECK(loop != nullptr);
190 return loop->GetHeader()->GetLastInstruction();
191}
192
Aart Bikd14c5952015-09-08 15:25:15 -0700193//
194// Public class methods.
195//
196
197InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis)
Aart Bik52be7e72016-06-23 11:20:41 -0700198 : induction_analysis_(induction_analysis),
199 chase_hint_(nullptr) {
Aart Bikb3365e02015-09-21 14:45:05 -0700200 DCHECK(induction_analysis != nullptr);
Aart Bikd14c5952015-09-08 15:25:15 -0700201}
202
Aart Bik1fc3afb2016-02-02 13:26:16 -0800203bool InductionVarRange::GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -0700204 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -0700205 HInstruction* chase_hint,
Aart Bik389b3db2015-10-28 14:23:40 -0700206 /*out*/Value* min_val,
207 /*out*/Value* max_val,
208 /*out*/bool* needs_finite_test) {
Aart Bik52be7e72016-06-23 11:20:41 -0700209 HLoopInformation* loop = nullptr;
210 HInductionVarAnalysis::InductionInfo* info = nullptr;
211 HInductionVarAnalysis::InductionInfo* trip = nullptr;
212 if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) {
213 return false;
Aart Bik97412c922016-02-19 20:14:38 -0800214 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700215 // Type int or lower (this is not too restrictive since intended clients, like
216 // bounds check elimination, will have truncated higher precision induction
217 // at their use point already).
218 switch (info->type) {
219 case Primitive::kPrimInt:
220 case Primitive::kPrimShort:
221 case Primitive::kPrimChar:
222 case Primitive::kPrimByte:
223 break;
224 default:
225 return false;
226 }
Aart Bik97412c922016-02-19 20:14:38 -0800227 // Find range.
Aart Bik52be7e72016-06-23 11:20:41 -0700228 chase_hint_ = chase_hint;
229 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700230 int64_t stride_value = 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700231 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
232 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint);
Aart Bik16d3a652016-09-09 10:33:50 -0700233 *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip);
Aart Bik40fbf742016-10-31 11:02:50 -0700234 chase_hint_ = nullptr;
235 // Retry chasing constants for wrap-around (merge sensitive).
236 if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) {
237 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
238 }
Aart Bik97412c922016-02-19 20:14:38 -0800239 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700240}
241
Aart Bik16d3a652016-09-09 10:33:50 -0700242bool InductionVarRange::CanGenerateRange(HInstruction* context,
243 HInstruction* instruction,
244 /*out*/bool* needs_finite_test,
245 /*out*/bool* needs_taken_test) {
246 bool is_last_value = false;
247 int64_t stride_value = 0;
Aart Bik9abf8942016-10-14 09:49:42 -0700248 return GenerateRangeOrLastValue(context,
249 instruction,
250 is_last_value,
251 nullptr,
252 nullptr,
253 nullptr,
254 nullptr,
255 nullptr, // nothing generated yet
256 &stride_value,
257 needs_finite_test,
258 needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700259 && (stride_value == -1 ||
260 stride_value == 0 ||
Aart Bik40fbf742016-10-31 11:02:50 -0700261 stride_value == 1); // avoid arithmetic wrap-around anomalies.
Aart Bikaec3cce2015-10-14 17:44:55 -0700262}
263
Aart Bik16d3a652016-09-09 10:33:50 -0700264void InductionVarRange::GenerateRange(HInstruction* context,
265 HInstruction* instruction,
266 HGraph* graph,
267 HBasicBlock* block,
268 /*out*/HInstruction** lower,
269 /*out*/HInstruction** upper) {
270 bool is_last_value = false;
Aart Bik009cace2016-09-16 10:15:19 -0700271 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700272 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700273 if (!GenerateRangeOrLastValue(context,
274 instruction,
275 is_last_value,
276 graph,
277 block,
278 lower,
279 upper,
280 nullptr,
281 &stride_value,
282 &b1,
283 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700284 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
Aart Bik389b3db2015-10-28 14:23:40 -0700285 }
286}
287
Aart Bik16d3a652016-09-09 10:33:50 -0700288HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context,
289 HGraph* graph,
290 HBasicBlock* block) {
291 HInstruction* taken_test = nullptr;
292 bool is_last_value = false;
293 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700294 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700295 if (!GenerateRangeOrLastValue(context,
296 context,
297 is_last_value,
298 graph,
299 block,
300 nullptr,
301 nullptr,
302 &taken_test,
303 &stride_value,
304 &b1,
305 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700306 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
307 }
308 return taken_test;
309}
310
311bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) {
312 bool is_last_value = true;
313 int64_t stride_value = 0;
314 bool needs_finite_test = false;
315 bool needs_taken_test = false;
Aart Bik9abf8942016-10-14 09:49:42 -0700316 return GenerateRangeOrLastValue(instruction,
317 instruction,
318 is_last_value,
319 nullptr,
320 nullptr,
321 nullptr,
322 nullptr,
323 nullptr, // nothing generated yet
324 &stride_value,
325 &needs_finite_test,
326 &needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700327 && !needs_finite_test && !needs_taken_test;
328}
329
330HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction,
331 HGraph* graph,
332 HBasicBlock* block) {
333 HInstruction* last_value = nullptr;
334 bool is_last_value = true;
335 int64_t stride_value = 0;
336 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700337 if (!GenerateRangeOrLastValue(instruction,
338 instruction,
339 is_last_value,
340 graph,
341 block,
342 &last_value,
343 &last_value,
344 nullptr,
345 &stride_value,
346 &b1,
347 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700348 LOG(FATAL) << "Failed precondition: CanGenerateLastValue()";
349 }
350 return last_value;
351}
352
353void InductionVarRange::Replace(HInstruction* instruction,
354 HInstruction* fetch,
355 HInstruction* replacement) {
356 for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
357 lp != nullptr;
358 lp = lp->GetPreHeader()->GetLoopInformation()) {
Aart Bik009cace2016-09-16 10:15:19 -0700359 // Update instruction's information.
Aart Bik16d3a652016-09-09 10:33:50 -0700360 ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement);
Aart Bik009cace2016-09-16 10:15:19 -0700361 // Update loop's trip-count information.
362 ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement);
Aart Bik389b3db2015-10-28 14:23:40 -0700363 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700364}
365
Aart Bik6b69e0a2017-01-11 10:20:43 -0800366bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const {
Aart Bik9abf8942016-10-14 09:49:42 -0700367 HInductionVarAnalysis::InductionInfo *trip =
368 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
Aart Bik6b69e0a2017-01-11 10:20:43 -0800369 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
370 IsConstant(trip->op_a, kExact, tc);
371 return true;
372 }
373 return false;
Aart Bik9abf8942016-10-14 09:49:42 -0700374}
375
Aart Bikfa762962017-04-07 11:33:37 -0700376bool InductionVarRange::IsUnitStride(HInstruction* context,
377 HInstruction* instruction,
Aart Bik37dc4df2017-06-28 14:08:00 -0700378 HGraph* graph,
Aart Bik8e02e3e2017-02-28 14:41:55 -0800379 /*out*/ HInstruction** offset) const {
380 HLoopInformation* loop = nullptr;
381 HInductionVarAnalysis::InductionInfo* info = nullptr;
382 HInductionVarAnalysis::InductionInfo* trip = nullptr;
Aart Bikfa762962017-04-07 11:33:37 -0700383 if (HasInductionInfo(context, instruction, &loop, &info, &trip)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800384 if (info->induction_class == HInductionVarAnalysis::kLinear &&
Aart Bik7adb6882017-03-07 13:28:51 -0800385 !HInductionVarAnalysis::IsNarrowingLinear(info)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800386 int64_t stride_value = 0;
387 if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) {
388 int64_t off_value = 0;
Aart Bik37dc4df2017-06-28 14:08:00 -0700389 if (IsConstant(info->op_b, kExact, &off_value)) {
390 *offset = graph->GetConstant(info->op_b->type, off_value);
391 } else if (info->op_b->operation == HInductionVarAnalysis::kFetch) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800392 *offset = info->op_b->fetch;
Aart Bik37dc4df2017-06-28 14:08:00 -0700393 } else {
394 return false;
Aart Bik8e02e3e2017-02-28 14:41:55 -0800395 }
396 return true;
397 }
398 }
399 }
400 return false;
401}
402
403HInstruction* InductionVarRange::GenerateTripCount(HLoopInformation* loop,
404 HGraph* graph,
405 HBasicBlock* block) {
406 HInductionVarAnalysis::InductionInfo *trip =
407 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
408 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
409 HInstruction* taken_test = nullptr;
410 HInstruction* trip_expr = nullptr;
411 if (IsBodyTripCount(trip)) {
412 if (!GenerateCode(trip->op_b, nullptr, graph, block, &taken_test, false, false)) {
413 return nullptr;
414 }
415 }
416 if (GenerateCode(trip->op_a, nullptr, graph, block, &trip_expr, false, false)) {
417 if (taken_test != nullptr) {
418 HInstruction* zero = graph->GetConstant(trip->type, 0);
419 trip_expr = Insert(block, new (graph->GetArena()) HSelect(taken_test, trip_expr, zero, kNoDexPc));
420 }
421 return trip_expr;
422 }
423 }
424 return nullptr;
425}
426
Aart Bikd14c5952015-09-08 15:25:15 -0700427//
428// Private class methods.
429//
430
Aart Bik97412c922016-02-19 20:14:38 -0800431bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
432 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700433 /*out*/ int64_t* value) const {
Aart Bik97412c922016-02-19 20:14:38 -0800434 if (info != nullptr) {
435 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
436 // any of the three requests (kExact, kAtMost, and KAtLeast).
437 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
438 info->operation == HInductionVarAnalysis::kFetch) {
Aart Bikf3e61ee2017-04-12 17:09:20 -0700439 if (IsInt64AndGet(info->fetch, value)) {
Aart Bik97412c922016-02-19 20:14:38 -0800440 return true;
441 }
442 }
Aart Bik40fbf742016-10-31 11:02:50 -0700443 // Try range analysis on the invariant, only accept a proper range
444 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700445 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
446 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
447 if (IsConstantValue(min_val) &&
448 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
449 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
450 *value = max_val.b_constant;
451 return true;
452 } else if (request == kAtLeast) {
453 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800454 return true;
455 }
456 }
Aart Bik97412c922016-02-19 20:14:38 -0800457 }
458 return false;
459}
460
Aart Bik52be7e72016-06-23 11:20:41 -0700461bool InductionVarRange::HasInductionInfo(
462 HInstruction* context,
463 HInstruction* instruction,
464 /*out*/ HLoopInformation** loop,
465 /*out*/ HInductionVarAnalysis::InductionInfo** info,
466 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800467 DCHECK(context != nullptr);
468 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700469 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
470 if (lp != nullptr) {
471 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700472 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700473 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700474 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700475 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700476 return true;
477 }
478 }
479 return false;
480}
481
482bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
483 if (trip != nullptr) {
484 // Both bounds that define a trip-count are well-behaved if they either are not defined
485 // in any loop, or are contained in a proper interval. This allows finding the min/max
486 // of an expression by chasing outward.
487 InductionVarRange range(induction_analysis_);
488 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
489 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
490 int64_t not_used = 0;
491 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
492 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
493 }
494 return true;
495}
496
497bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
498 if (info != nullptr) {
499 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
500 info->operation == HInductionVarAnalysis::kFetch) {
501 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
502 }
503 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
504 }
505 return false;
506}
507
Aart Bik16d3a652016-09-09 10:33:50 -0700508bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
509 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700510 if (info != nullptr) {
511 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700512 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800513 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
514 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700515 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700516 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700517 }
Aart Bikd14c5952015-09-08 15:25:15 -0700518 }
Aart Bik389b3db2015-10-28 14:23:40 -0700519 return false;
520}
521
Aart Bik7d57d7f2015-12-09 14:39:48 -0800522bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700523 if (trip != nullptr) {
524 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
525 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
526 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
527 }
528 }
529 return false;
530}
531
Aart Bik7d57d7f2015-12-09 14:39:48 -0800532bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700533 if (trip != nullptr) {
534 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
535 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
536 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
537 }
538 }
539 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700540}
541
Aart Bik7d57d7f2015-12-09 14:39:48 -0800542InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
543 HInductionVarAnalysis::InductionInfo* trip,
544 bool in_body,
545 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800546 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800547 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700548 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800549 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
550 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
551 // with intermediate results that only incorporate single instructions.
552 if (trip != nullptr) {
553 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700554 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800555 int64_t stride_value = 0;
556 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800557 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800558 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800559 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
560 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
561 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700562 trip->induction_class,
563 trip->operation,
564 trip_expr->op_a,
565 trip->op_b,
566 nullptr,
567 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800568 return GetVal(&cancelled_trip, trip, in_body, is_min);
569 }
570 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800571 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800572 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
573 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
574 HInductionVarAnalysis::InductionInfo neg(
575 HInductionVarAnalysis::kInvariant,
576 HInductionVarAnalysis::kNeg,
577 nullptr,
578 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700579 nullptr,
580 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800581 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700582 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800583 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
584 }
585 }
586 }
587 }
588 }
589 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
590 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
591 GetVal(info->op_b, trip, in_body, is_min));
592}
593
Aart Bikdf7822e2016-12-06 10:05:30 -0800594InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
595 HInductionVarAnalysis::InductionInfo* trip,
596 bool in_body,
597 bool is_min) const {
598 DCHECK(info != nullptr);
599 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
600 int64_t a = 0;
601 int64_t b = 0;
602 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
603 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800604 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800605 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
606 Value c = GetVal(info->op_b, trip, in_body, is_min);
607 if (is_min) {
608 return c;
609 } else {
610 Value m = GetVal(trip, trip, in_body, is_min);
611 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
612 Value x = MulValue(Value(a), t);
613 Value y = MulValue(Value(b), m);
614 return AddValue(AddValue(x, y), c);
615 }
616 }
617 return Value();
618}
619
Aart Bikc071a012016-12-01 10:22:31 -0800620InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
621 HInductionVarAnalysis::InductionInfo* trip,
622 bool in_body,
623 bool is_min) const {
624 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800625 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800626 int64_t a = 0;
627 int64_t f = 0;
628 if (IsConstant(info->op_a, kExact, &a) &&
629 CanLongValueFitIntoInt(a) &&
Aart Bikf3e61ee2017-04-12 17:09:20 -0700630 IsInt64AndGet(info->fetch, &f) && f >= 1) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800631 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
632 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800633 const bool is_min_a = a >= 0 ? is_min : !is_min;
634 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800635 Value b = GetVal(info->op_b, trip, in_body, is_min);
636 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800637 }
638 }
639 return Value();
640}
641
Aart Bikd14c5952015-09-08 15:25:15 -0700642InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700643 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700644 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800645 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700646 // Special case when chasing constants: single instruction that denotes trip count in the
647 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
648 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700649 if (is_min) {
650 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800651 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700652 return Value(std::numeric_limits<int32_t>::max());
653 }
654 }
Aart Bik40fbf742016-10-31 11:02:50 -0700655 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
656 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
657 int64_t value;
Aart Bikf3e61ee2017-04-12 17:09:20 -0700658 if (IsInt64AndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
Aart Bik40fbf742016-10-31 11:02:50 -0700659 // Proper constant reveals best information.
660 return Value(static_cast<int32_t>(value));
661 } else if (instruction == chase_hint_) {
662 // At hint, fetch is represented by itself.
663 return Value(instruction, 1, 0);
664 } else if (instruction->IsAdd()) {
665 // Incorporate suitable constants in the chased value.
Aart Bikf3e61ee2017-04-12 17:09:20 -0700666 if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
Aart Bik97412c922016-02-19 20:14:38 -0800667 return AddValue(Value(static_cast<int32_t>(value)),
668 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
Aart Bikf3e61ee2017-04-12 17:09:20 -0700669 } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
Aart Bik97412c922016-02-19 20:14:38 -0800670 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
671 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700672 }
Aart Bik52be7e72016-06-23 11:20:41 -0700673 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700674 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700675 if (chase_hint_ == nullptr) {
676 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
677 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000678 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700679 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700680 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700681 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800682 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Aart Bik0d345cf2016-03-16 10:49:38 -0700683 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
684 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
685 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
686 }
Aart Bik52be7e72016-06-23 11:20:41 -0700687 }
688 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
689 // so far is well-behaved in both bounds and the next trip-count is safe.
690 // Example:
691 // for (int i = 0; i <= 100; i++) // safe
692 // for (int j = 0; j <= i; j++) // well-behaved
693 // j is in range [0, i ] (if i is chase hint)
694 // or in range [0, 100] (otherwise)
695 HLoopInformation* next_loop = nullptr;
696 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
697 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
698 bool next_in_body = true; // inner loop is always in body of outer loop
699 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
700 IsWellBehavedTripCount(trip) &&
701 !IsUnsafeTripCount(next_trip)) {
702 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700703 }
Aart Bik40fbf742016-10-31 11:02:50 -0700704 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700705 return Value(instruction, 1, 0);
706}
707
Aart Bikcd26feb2015-09-23 17:50:50 -0700708InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
709 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700710 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800711 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700712 if (info != nullptr) {
713 switch (info->induction_class) {
714 case HInductionVarAnalysis::kInvariant:
715 // Invariants.
716 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700717 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700718 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
719 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700720 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700721 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
722 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700723 case HInductionVarAnalysis::kNeg: // second reversed!
724 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700725 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700726 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700727 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700728 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700729 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800730 case HInductionVarAnalysis::kRem:
731 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700732 case HInductionVarAnalysis::kXor:
733 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700734 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700735 return GetFetch(info->fetch, trip, in_body, is_min);
736 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700737 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700738 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700739 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700740 }
741 FALLTHROUGH_INTENDED;
742 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700743 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700744 if (is_min) {
745 return Value(0);
746 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700747 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700748 }
749 break;
750 default:
751 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700752 }
753 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700754 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700755 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800756 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800757 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800758 case HInductionVarAnalysis::kGeometric:
759 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700760 case HInductionVarAnalysis::kWrapAround:
761 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700762 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
763 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700764 }
765 }
Aart Bikb3365e02015-09-21 14:45:05 -0700766 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700767}
768
769InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
770 HInductionVarAnalysis::InductionInfo* info2,
771 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700772 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800773 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700774 // Constant times range.
775 int64_t value = 0;
776 if (IsConstant(info1, kExact, &value)) {
777 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
778 } else if (IsConstant(info2, kExact, &value)) {
779 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
780 }
781 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700782 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
783 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
784 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
785 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800786 // Positive range vs. positive or negative range.
787 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
788 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
789 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
790 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
791 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700792 }
Aart Bik97412c922016-02-19 20:14:38 -0800793 }
794 // Negative range vs. positive or negative range.
795 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
796 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
797 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
798 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
799 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700800 }
801 }
Aart Bikb3365e02015-09-21 14:45:05 -0700802 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700803}
804
805InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
806 HInductionVarAnalysis::InductionInfo* info2,
807 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700808 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800809 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700810 // Range divided by constant.
811 int64_t value = 0;
812 if (IsConstant(info2, kExact, &value)) {
813 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
814 }
815 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700816 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
817 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
818 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
819 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800820 // Positive range vs. positive or negative range.
821 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
822 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
823 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
824 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
825 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700826 }
Aart Bik97412c922016-02-19 20:14:38 -0800827 }
828 // Negative range vs. positive or negative range.
829 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
830 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
831 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
832 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
833 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700834 }
835 }
Aart Bikb3365e02015-09-21 14:45:05 -0700836 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700837}
838
Aart Bikdf7822e2016-12-06 10:05:30 -0800839InductionVarRange::Value InductionVarRange::GetRem(
840 HInductionVarAnalysis::InductionInfo* info1,
841 HInductionVarAnalysis::InductionInfo* info2) const {
842 int64_t v1 = 0;
843 int64_t v2 = 0;
844 // Only accept exact values.
845 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
846 int64_t value = v1 % v2;
847 if (CanLongValueFitIntoInt(value)) {
848 return Value(static_cast<int32_t>(value));
849 }
850 }
851 return Value();
852}
853
Aart Bik7dc96932016-10-12 10:01:05 -0700854InductionVarRange::Value InductionVarRange::GetXor(
855 HInductionVarAnalysis::InductionInfo* info1,
856 HInductionVarAnalysis::InductionInfo* info2) const {
857 int64_t v1 = 0;
858 int64_t v2 = 0;
859 // Only accept exact values.
860 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
861 int64_t value = v1 ^ v2;
862 if (CanLongValueFitIntoInt(value)) {
863 return Value(static_cast<int32_t>(value));
864 }
865 }
866 return Value();
867}
868
Aart Bik52be7e72016-06-23 11:20:41 -0700869InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
870 int64_t value,
871 HInductionVarAnalysis::InductionInfo* info,
872 HInductionVarAnalysis::InductionInfo* trip,
873 bool in_body,
874 bool is_min) const {
875 if (CanLongValueFitIntoInt(value)) {
876 Value c(static_cast<int32_t>(value));
877 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
878 }
879 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800880}
881
Aart Bik52be7e72016-06-23 11:20:41 -0700882InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
883 int64_t value,
884 HInductionVarAnalysis::InductionInfo* info,
885 HInductionVarAnalysis::InductionInfo* trip,
886 bool in_body,
887 bool is_min) const {
888 if (CanLongValueFitIntoInt(value)) {
889 Value c(static_cast<int32_t>(value));
890 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
891 }
892 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700893}
894
Aart Bik7d57d7f2015-12-09 14:39:48 -0800895InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700896 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800897 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700898 if (v1.a_constant == 0) {
899 return Value(v2.instruction, v2.a_constant, b);
900 } else if (v2.a_constant == 0) {
901 return Value(v1.instruction, v1.a_constant, b);
902 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
903 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
904 }
905 }
Aart Bikb3365e02015-09-21 14:45:05 -0700906 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700907}
908
Aart Bik7d57d7f2015-12-09 14:39:48 -0800909InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700910 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800911 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700912 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
913 return Value(v2.instruction, -v2.a_constant, b);
914 } else if (v2.a_constant == 0) {
915 return Value(v1.instruction, v1.a_constant, b);
916 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
917 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
918 }
919 }
Aart Bikb3365e02015-09-21 14:45:05 -0700920 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700921}
922
Aart Bik7d57d7f2015-12-09 14:39:48 -0800923InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700924 if (v1.is_known && v2.is_known) {
925 if (v1.a_constant == 0) {
926 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
927 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
928 }
929 } else if (v2.a_constant == 0) {
930 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
931 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
932 }
Aart Bikd14c5952015-09-08 15:25:15 -0700933 }
934 }
Aart Bikb3365e02015-09-21 14:45:05 -0700935 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700936}
937
Aart Bik7d57d7f2015-12-09 14:39:48 -0800938InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700939 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700940 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
941 return Value(v1.b_constant / v2.b_constant);
942 }
943 }
Aart Bikb3365e02015-09-21 14:45:05 -0700944 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700945}
946
Aart Bik7d57d7f2015-12-09 14:39:48 -0800947InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700948 if (v1.is_known && v2.is_known) {
949 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700950 return Value(v1.instruction, v1.a_constant,
951 is_min ? std::min(v1.b_constant, v2.b_constant)
952 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700953 }
Aart Bikd14c5952015-09-08 15:25:15 -0700954 }
Aart Bikb3365e02015-09-21 14:45:05 -0700955 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700956}
957
Aart Bik9abf8942016-10-14 09:49:42 -0700958bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
959 HInstruction* instruction,
960 bool is_last_value,
961 HGraph* graph,
962 HBasicBlock* block,
963 /*out*/HInstruction** lower,
964 /*out*/HInstruction** upper,
965 /*out*/HInstruction** taken_test,
966 /*out*/int64_t* stride_value,
967 /*out*/bool* needs_finite_test,
968 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700969 HLoopInformation* loop = nullptr;
970 HInductionVarAnalysis::InductionInfo* info = nullptr;
971 HInductionVarAnalysis::InductionInfo* trip = nullptr;
972 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
973 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800974 }
975 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
976 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
977 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
978 // code does not use the trip-count explicitly (since there could be an implicit relation
979 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700980 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700981 *stride_value = 0;
982 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800983 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700984 // Handle last value request.
985 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800986 DCHECK(!in_body);
987 switch (info->induction_class) {
988 case HInductionVarAnalysis::kLinear:
989 if (*stride_value > 0) {
990 lower = nullptr;
991 } else {
992 upper = nullptr;
993 }
994 break;
Aart Bikdf7822e2016-12-06 10:05:30 -0800995 case HInductionVarAnalysis::kPolynomial:
996 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -0800997 case HInductionVarAnalysis::kGeometric:
998 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -0800999 case HInductionVarAnalysis::kWrapAround:
1000 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001001 case HInductionVarAnalysis::kPeriodic:
1002 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1003 default:
1004 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001005 }
1006 }
Aart Bik97412c922016-02-19 20:14:38 -08001007 // Code generation for taken test: generate the code when requested or otherwise analyze
1008 // if code generation is feasible when taken test is needed.
1009 if (taken_test != nullptr) {
1010 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1011 } else if (*needs_taken_test) {
1012 if (!GenerateCode(
1013 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1014 return false;
1015 }
1016 }
1017 // Code generation for lower and upper.
1018 return
1019 // Success on lower if invariant (not set), or code can be generated.
1020 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1021 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1022 // And success on upper.
1023 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001024}
1025
Aart Bikdf7822e2016-12-06 10:05:30 -08001026bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1027 HInductionVarAnalysis::InductionInfo* trip,
1028 HGraph* graph,
1029 HBasicBlock* block,
1030 /*out*/HInstruction** result) const {
1031 DCHECK(info != nullptr);
1032 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1033 // Detect known coefficients and trip count (always taken).
1034 int64_t a = 0;
1035 int64_t b = 0;
1036 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001037 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1038 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001039 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001040 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001041 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001042 HInstruction* c = nullptr;
1043 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001044 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001045 Primitive::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001046 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Aart Bike6bd0272016-12-16 13:57:52 -08001047 if (type != Primitive::kPrimLong) {
1048 sum = static_cast<int32_t>(sum); // okay to truncate
1049 }
1050 *result =
1051 Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001052 }
1053 return true;
1054 }
1055 }
1056 return false;
1057}
1058
Aart Bikc071a012016-12-01 10:22:31 -08001059bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1060 HInductionVarAnalysis::InductionInfo* trip,
1061 HGraph* graph,
1062 HBasicBlock* block,
1063 /*out*/HInstruction** result) const {
1064 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001065 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001066 // Detect known base and trip count (always taken).
1067 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001068 int64_t m = 0;
Aart Bikf3e61ee2017-04-12 17:09:20 -07001069 if (IsInt64AndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001070 HInstruction* opa = nullptr;
1071 HInstruction* opb = nullptr;
1072 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1073 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001074 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001075 Primitive::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001076 // Compute f ^ m for known maximum index value m.
1077 bool overflow = false;
1078 int64_t fpow = IntPow(f, m, &overflow);
1079 if (info->operation == HInductionVarAnalysis::kDiv) {
1080 // For division, any overflow truncates to zero.
1081 if (overflow || (type != Primitive::kPrimLong && !CanLongValueFitIntoInt(fpow))) {
1082 fpow = 0;
1083 }
1084 } else if (type != Primitive::kPrimLong) {
1085 // For multiplication, okay to truncate to required precision.
1086 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1087 fpow = static_cast<int32_t>(fpow);
1088 }
1089 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001090 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001091 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001092 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001093 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001094 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001095 HInstruction* e = nullptr;
1096 if (info->operation == HInductionVarAnalysis::kMul) {
1097 e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow));
1098 } else {
1099 e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
1100 }
1101 *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001102 }
1103 }
1104 return true;
1105 }
1106 }
1107 return false;
1108}
1109
Aart Bikdf7822e2016-12-06 10:05:30 -08001110bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1111 HInductionVarAnalysis::InductionInfo* trip,
1112 HGraph* graph,
1113 HBasicBlock* block,
1114 /*out*/HInstruction** result) const {
1115 DCHECK(info != nullptr);
1116 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1117 // Count depth.
1118 int32_t depth = 0;
1119 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1120 info = info->op_b, ++depth) {}
1121 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001122 // TODO: generalize, but be careful to adjust the terminal.
1123 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001124 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001125 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1126 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001127 }
1128 return false;
1129}
1130
Aart Bik9abf8942016-10-14 09:49:42 -07001131bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1132 HInductionVarAnalysis::InductionInfo* trip,
1133 HGraph* graph,
1134 HBasicBlock* block,
1135 /*out*/HInstruction** result,
1136 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001137 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001138 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik8523ea12017-06-01 15:45:09 -07001139 // Count period and detect all-invariants.
Aart Bike6bd0272016-12-16 13:57:52 -08001140 int64_t period = 1;
Aart Bik8523ea12017-06-01 15:45:09 -07001141 bool all_invariants = true;
1142 HInductionVarAnalysis::InductionInfo* p = info;
1143 for (; p->induction_class == HInductionVarAnalysis::kPeriodic; p = p->op_b, ++period) {
1144 DCHECK_EQ(p->op_a->induction_class, HInductionVarAnalysis::kInvariant);
1145 if (p->op_a->operation != HInductionVarAnalysis::kFetch) {
1146 all_invariants = false;
1147 }
1148 }
1149 DCHECK_EQ(p->induction_class, HInductionVarAnalysis::kInvariant);
1150 if (p->operation != HInductionVarAnalysis::kFetch) {
1151 all_invariants = false;
1152 }
1153 // Don't rely on FP arithmetic to be precise, unless the full period
1154 // consist of pre-computed expressions only.
1155 if (info->type == Primitive::kPrimFloat || info->type == Primitive::kPrimDouble) {
1156 if (!all_invariants) {
1157 return false;
1158 }
1159 }
Aart Bike6bd0272016-12-16 13:57:52 -08001160 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1161 int64_t m = 0;
1162 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1163 int64_t li = m % period;
1164 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1165 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1166 info = info->op_a;
1167 }
1168 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001169 }
Aart Bike6bd0272016-12-16 13:57:52 -08001170 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1171 // directly to obtain the maximum index value t even if taken test is needed.
1172 HInstruction* x = nullptr;
1173 HInstruction* y = nullptr;
1174 HInstruction* t = nullptr;
1175 if (period == 2 &&
1176 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1177 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1178 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1179 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001180 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001181 Primitive::Type type = trip->type;
1182 HInstruction* msk =
1183 Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1)));
1184 HInstruction* is_even =
1185 Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1186 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001187 }
1188 // Guard select with taken test if needed.
1189 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001190 HInstruction* is_taken = nullptr;
1191 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1192 if (graph != nullptr) {
1193 *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc));
1194 }
1195 *needs_taken_test = false; // taken care of
1196 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001197 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001198 }
Aart Bik9abf8942016-10-14 09:49:42 -07001199 }
1200 return true;
1201 }
1202 return false;
1203}
1204
Aart Bikaec3cce2015-10-14 17:44:55 -07001205bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1206 HInductionVarAnalysis::InductionInfo* trip,
1207 HGraph* graph, // when set, code is generated
1208 HBasicBlock* block,
1209 /*out*/HInstruction** result,
1210 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001211 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001212 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001213 // If during codegen, the result is not needed (nullptr), simply return success.
1214 if (graph != nullptr && result == nullptr) {
1215 return true;
1216 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001217 // Handle current operation.
Aart Bike6bd0272016-12-16 13:57:52 -08001218 Primitive::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001219 HInstruction* opa = nullptr;
1220 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001221 switch (info->induction_class) {
1222 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001223 // Invariants (note that since invariants only have other invariants as
1224 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001225 switch (info->operation) {
1226 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001227 case HInductionVarAnalysis::kSub:
1228 case HInductionVarAnalysis::kMul:
1229 case HInductionVarAnalysis::kDiv:
1230 case HInductionVarAnalysis::kRem:
1231 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001232 case HInductionVarAnalysis::kLT:
1233 case HInductionVarAnalysis::kLE:
1234 case HInductionVarAnalysis::kGT:
1235 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001236 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1237 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1238 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001239 HInstruction* operation = nullptr;
1240 switch (info->operation) {
1241 case HInductionVarAnalysis::kAdd:
1242 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001243 case HInductionVarAnalysis::kSub:
1244 operation = new (graph->GetArena()) HSub(type, opa, opb); break;
1245 case HInductionVarAnalysis::kMul:
1246 operation = new (graph->GetArena()) HMul(type, opa, opb, kNoDexPc); break;
1247 case HInductionVarAnalysis::kDiv:
1248 operation = new (graph->GetArena()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001249 case HInductionVarAnalysis::kRem:
1250 operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001251 case HInductionVarAnalysis::kXor:
1252 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001253 case HInductionVarAnalysis::kLT:
1254 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1255 case HInductionVarAnalysis::kLE:
1256 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1257 case HInductionVarAnalysis::kGT:
1258 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1259 case HInductionVarAnalysis::kGE:
1260 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1261 default:
1262 LOG(FATAL) << "unknown operation";
1263 }
1264 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001265 }
1266 return true;
1267 }
1268 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001269 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001270 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1271 if (graph != nullptr) {
1272 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1273 }
1274 return true;
1275 }
1276 break;
1277 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001278 if (graph != nullptr) {
1279 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001280 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001281 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001282 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001283 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001284 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001285 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001286 }
1287 FALLTHROUGH_INTENDED;
1288 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001289 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001290 if (is_min) {
1291 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001292 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001293 }
1294 return true;
1295 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001296 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001297 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001298 *result =
1299 Insert(block,
1300 new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001301 }
1302 return true;
1303 }
1304 }
1305 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001306 case HInductionVarAnalysis::kNop:
1307 LOG(FATAL) << "unexpected invariant nop";
1308 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001309 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001310 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001311 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1312 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1313 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001314 // known stride yields right value. Always avoid any narrowing linear induction or
1315 // any type mismatch between the linear induction and the trip count expression.
1316 // TODO: careful runtime type conversions could generalize this latter restriction.
1317 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1318 int64_t stride_value = 0;
1319 if (IsConstant(info->op_a, kExact, &stride_value) &&
1320 CanLongValueFitIntoInt(stride_value)) {
1321 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1322 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1323 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1324 if (graph != nullptr) {
1325 HInstruction* oper;
1326 if (stride_value == 1) {
1327 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1328 } else if (stride_value == -1) {
1329 oper = new (graph->GetArena()) HSub(type, opb, opa);
1330 } else {
1331 HInstruction* mul =
1332 new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa);
1333 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
1334 }
1335 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001336 }
Aart Bike6bd0272016-12-16 13:57:52 -08001337 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001338 }
1339 }
1340 }
1341 break;
Aart Bik4a342772015-11-30 10:17:46 -08001342 }
Aart Bikc071a012016-12-01 10:22:31 -08001343 case HInductionVarAnalysis::kPolynomial:
1344 case HInductionVarAnalysis::kGeometric:
1345 break;
Aart Bik4a342772015-11-30 10:17:46 -08001346 case HInductionVarAnalysis::kWrapAround:
1347 case HInductionVarAnalysis::kPeriodic: {
1348 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1349 // values are easy to test at runtime without complications of arithmetic wrap-around.
1350 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001351 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001352 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001353 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001354 }
1355 return true;
1356 }
1357 break;
1358 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001359 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001360 }
1361 return false;
1362}
1363
Aart Bik16d3a652016-09-09 10:33:50 -07001364void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1365 HInstruction* fetch,
1366 HInstruction* replacement) {
1367 if (info != nullptr) {
1368 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1369 info->operation == HInductionVarAnalysis::kFetch &&
1370 info->fetch == fetch) {
1371 info->fetch = replacement;
1372 }
1373 ReplaceInduction(info->op_a, fetch, replacement);
1374 ReplaceInduction(info->op_b, fetch, replacement);
1375 }
1376}
1377
Aart Bikd14c5952015-09-08 15:25:15 -07001378} // namespace art