blob: 191d3d128c7a21831507768e722142083924780c [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 Bik8e9090b2017-09-08 16:46:50 -0700673 } else if (instruction->IsSub()) {
674 // Incorporate suitable constants in the chased value.
675 if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
676 return SubValue(Value(static_cast<int32_t>(value)),
677 GetFetch(instruction->InputAt(1), trip, in_body, !is_min));
678 } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
679 return SubValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
680 Value(static_cast<int32_t>(value)));
681 }
Aart Bik52be7e72016-06-23 11:20:41 -0700682 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700683 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700684 if (chase_hint_ == nullptr) {
685 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
686 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000687 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700688 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700689 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700690 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800691 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Aart Bik0d345cf2016-03-16 10:49:38 -0700692 if (instruction->AsTypeConversion()->GetInputType() == Primitive::kPrimInt &&
693 instruction->AsTypeConversion()->GetResultType() == Primitive::kPrimLong) {
694 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
695 }
Aart Bik52be7e72016-06-23 11:20:41 -0700696 }
697 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
698 // so far is well-behaved in both bounds and the next trip-count is safe.
699 // Example:
700 // for (int i = 0; i <= 100; i++) // safe
701 // for (int j = 0; j <= i; j++) // well-behaved
702 // j is in range [0, i ] (if i is chase hint)
703 // or in range [0, 100] (otherwise)
704 HLoopInformation* next_loop = nullptr;
705 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
706 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
707 bool next_in_body = true; // inner loop is always in body of outer loop
708 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
709 IsWellBehavedTripCount(trip) &&
710 !IsUnsafeTripCount(next_trip)) {
711 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700712 }
Aart Bik40fbf742016-10-31 11:02:50 -0700713 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700714 return Value(instruction, 1, 0);
715}
716
Aart Bikcd26feb2015-09-23 17:50:50 -0700717InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
718 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700719 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800720 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700721 if (info != nullptr) {
722 switch (info->induction_class) {
723 case HInductionVarAnalysis::kInvariant:
724 // Invariants.
725 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700726 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700727 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
728 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700729 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700730 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
731 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700732 case HInductionVarAnalysis::kNeg: // second reversed!
733 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700734 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700735 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700736 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700737 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700738 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800739 case HInductionVarAnalysis::kRem:
740 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700741 case HInductionVarAnalysis::kXor:
742 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700743 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700744 return GetFetch(info->fetch, trip, in_body, is_min);
745 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700746 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700747 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700748 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700749 }
750 FALLTHROUGH_INTENDED;
751 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700752 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700753 if (is_min) {
754 return Value(0);
755 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700756 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700757 }
758 break;
759 default:
760 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700761 }
762 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700763 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700764 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800765 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800766 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800767 case HInductionVarAnalysis::kGeometric:
768 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700769 case HInductionVarAnalysis::kWrapAround:
770 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700771 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
772 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700773 }
774 }
Aart Bikb3365e02015-09-21 14:45:05 -0700775 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700776}
777
778InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
779 HInductionVarAnalysis::InductionInfo* info2,
780 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700781 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800782 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700783 // Constant times range.
784 int64_t value = 0;
785 if (IsConstant(info1, kExact, &value)) {
786 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
787 } else if (IsConstant(info2, kExact, &value)) {
788 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
789 }
790 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700791 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
792 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
793 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
794 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800795 // Positive range vs. positive or negative range.
796 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
797 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
798 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
799 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
800 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700801 }
Aart Bik97412c922016-02-19 20:14:38 -0800802 }
803 // Negative range vs. positive or negative range.
804 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
805 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
806 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
807 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
808 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700809 }
810 }
Aart Bikb3365e02015-09-21 14:45:05 -0700811 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700812}
813
814InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
815 HInductionVarAnalysis::InductionInfo* info2,
816 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700817 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800818 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700819 // Range divided by constant.
820 int64_t value = 0;
821 if (IsConstant(info2, kExact, &value)) {
822 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
823 }
824 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700825 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
826 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
827 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
828 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800829 // Positive range vs. positive or negative range.
830 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
831 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
832 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
833 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
834 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700835 }
Aart Bik97412c922016-02-19 20:14:38 -0800836 }
837 // Negative range vs. positive or negative range.
838 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
839 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
840 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
841 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
842 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700843 }
844 }
Aart Bikb3365e02015-09-21 14:45:05 -0700845 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700846}
847
Aart Bikdf7822e2016-12-06 10:05:30 -0800848InductionVarRange::Value InductionVarRange::GetRem(
849 HInductionVarAnalysis::InductionInfo* info1,
850 HInductionVarAnalysis::InductionInfo* info2) const {
851 int64_t v1 = 0;
852 int64_t v2 = 0;
853 // Only accept exact values.
854 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
855 int64_t value = v1 % v2;
856 if (CanLongValueFitIntoInt(value)) {
857 return Value(static_cast<int32_t>(value));
858 }
859 }
860 return Value();
861}
862
Aart Bik7dc96932016-10-12 10:01:05 -0700863InductionVarRange::Value InductionVarRange::GetXor(
864 HInductionVarAnalysis::InductionInfo* info1,
865 HInductionVarAnalysis::InductionInfo* info2) const {
866 int64_t v1 = 0;
867 int64_t v2 = 0;
868 // Only accept exact values.
869 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
870 int64_t value = v1 ^ v2;
871 if (CanLongValueFitIntoInt(value)) {
872 return Value(static_cast<int32_t>(value));
873 }
874 }
875 return Value();
876}
877
Aart Bik52be7e72016-06-23 11:20:41 -0700878InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
879 int64_t value,
880 HInductionVarAnalysis::InductionInfo* info,
881 HInductionVarAnalysis::InductionInfo* trip,
882 bool in_body,
883 bool is_min) const {
884 if (CanLongValueFitIntoInt(value)) {
885 Value c(static_cast<int32_t>(value));
886 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
887 }
888 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800889}
890
Aart Bik52be7e72016-06-23 11:20:41 -0700891InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
892 int64_t value,
893 HInductionVarAnalysis::InductionInfo* info,
894 HInductionVarAnalysis::InductionInfo* trip,
895 bool in_body,
896 bool is_min) const {
897 if (CanLongValueFitIntoInt(value)) {
898 Value c(static_cast<int32_t>(value));
899 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
900 }
901 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700902}
903
Aart Bik7d57d7f2015-12-09 14:39:48 -0800904InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700905 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800906 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700907 if (v1.a_constant == 0) {
908 return Value(v2.instruction, v2.a_constant, b);
909 } else if (v2.a_constant == 0) {
910 return Value(v1.instruction, v1.a_constant, b);
911 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
912 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
913 }
914 }
Aart Bikb3365e02015-09-21 14:45:05 -0700915 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700916}
917
Aart Bik7d57d7f2015-12-09 14:39:48 -0800918InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700919 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800920 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700921 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
922 return Value(v2.instruction, -v2.a_constant, b);
923 } else if (v2.a_constant == 0) {
924 return Value(v1.instruction, v1.a_constant, b);
925 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
926 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
927 }
928 }
Aart Bikb3365e02015-09-21 14:45:05 -0700929 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700930}
931
Aart Bik7d57d7f2015-12-09 14:39:48 -0800932InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700933 if (v1.is_known && v2.is_known) {
934 if (v1.a_constant == 0) {
935 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
936 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
937 }
938 } else if (v2.a_constant == 0) {
939 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
940 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
941 }
Aart Bikd14c5952015-09-08 15:25:15 -0700942 }
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::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700948 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700949 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
950 return Value(v1.b_constant / v2.b_constant);
951 }
952 }
Aart Bikb3365e02015-09-21 14:45:05 -0700953 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700954}
955
Aart Bik7d57d7f2015-12-09 14:39:48 -0800956InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700957 if (v1.is_known && v2.is_known) {
958 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700959 return Value(v1.instruction, v1.a_constant,
960 is_min ? std::min(v1.b_constant, v2.b_constant)
961 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700962 }
Aart Bikd14c5952015-09-08 15:25:15 -0700963 }
Aart Bikb3365e02015-09-21 14:45:05 -0700964 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700965}
966
Aart Bik9abf8942016-10-14 09:49:42 -0700967bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
968 HInstruction* instruction,
969 bool is_last_value,
970 HGraph* graph,
971 HBasicBlock* block,
972 /*out*/HInstruction** lower,
973 /*out*/HInstruction** upper,
974 /*out*/HInstruction** taken_test,
975 /*out*/int64_t* stride_value,
976 /*out*/bool* needs_finite_test,
977 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700978 HLoopInformation* loop = nullptr;
979 HInductionVarAnalysis::InductionInfo* info = nullptr;
980 HInductionVarAnalysis::InductionInfo* trip = nullptr;
981 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
982 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800983 }
984 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
985 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
986 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
987 // code does not use the trip-count explicitly (since there could be an implicit relation
988 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700989 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700990 *stride_value = 0;
991 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800992 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700993 // Handle last value request.
994 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800995 DCHECK(!in_body);
996 switch (info->induction_class) {
997 case HInductionVarAnalysis::kLinear:
998 if (*stride_value > 0) {
999 lower = nullptr;
1000 } else {
1001 upper = nullptr;
1002 }
1003 break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001004 case HInductionVarAnalysis::kPolynomial:
1005 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001006 case HInductionVarAnalysis::kGeometric:
1007 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -08001008 case HInductionVarAnalysis::kWrapAround:
1009 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001010 case HInductionVarAnalysis::kPeriodic:
1011 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1012 default:
1013 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001014 }
1015 }
Aart Bik97412c922016-02-19 20:14:38 -08001016 // Code generation for taken test: generate the code when requested or otherwise analyze
1017 // if code generation is feasible when taken test is needed.
1018 if (taken_test != nullptr) {
1019 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1020 } else if (*needs_taken_test) {
1021 if (!GenerateCode(
1022 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1023 return false;
1024 }
1025 }
1026 // Code generation for lower and upper.
1027 return
1028 // Success on lower if invariant (not set), or code can be generated.
1029 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1030 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1031 // And success on upper.
1032 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001033}
1034
Aart Bikdf7822e2016-12-06 10:05:30 -08001035bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1036 HInductionVarAnalysis::InductionInfo* trip,
1037 HGraph* graph,
1038 HBasicBlock* block,
1039 /*out*/HInstruction** result) const {
1040 DCHECK(info != nullptr);
1041 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1042 // Detect known coefficients and trip count (always taken).
1043 int64_t a = 0;
1044 int64_t b = 0;
1045 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001046 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1047 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001048 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001049 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001050 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001051 HInstruction* c = nullptr;
1052 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001053 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001054 Primitive::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001055 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Aart Bike6bd0272016-12-16 13:57:52 -08001056 if (type != Primitive::kPrimLong) {
1057 sum = static_cast<int32_t>(sum); // okay to truncate
1058 }
1059 *result =
1060 Insert(block, new (graph->GetArena()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001061 }
1062 return true;
1063 }
1064 }
1065 return false;
1066}
1067
Aart Bikc071a012016-12-01 10:22:31 -08001068bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1069 HInductionVarAnalysis::InductionInfo* trip,
1070 HGraph* graph,
1071 HBasicBlock* block,
1072 /*out*/HInstruction** result) const {
1073 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001074 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001075 // Detect known base and trip count (always taken).
1076 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001077 int64_t m = 0;
Aart Bikf3e61ee2017-04-12 17:09:20 -07001078 if (IsInt64AndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001079 HInstruction* opa = nullptr;
1080 HInstruction* opb = nullptr;
1081 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1082 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001083 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001084 Primitive::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001085 // Compute f ^ m for known maximum index value m.
1086 bool overflow = false;
1087 int64_t fpow = IntPow(f, m, &overflow);
1088 if (info->operation == HInductionVarAnalysis::kDiv) {
1089 // For division, any overflow truncates to zero.
1090 if (overflow || (type != Primitive::kPrimLong && !CanLongValueFitIntoInt(fpow))) {
1091 fpow = 0;
1092 }
1093 } else if (type != Primitive::kPrimLong) {
1094 // For multiplication, okay to truncate to required precision.
1095 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1096 fpow = static_cast<int32_t>(fpow);
1097 }
1098 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001099 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001100 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001101 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001102 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001103 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001104 HInstruction* e = nullptr;
1105 if (info->operation == HInductionVarAnalysis::kMul) {
1106 e = new (graph->GetArena()) HMul(type, opa, graph->GetConstant(type, fpow));
1107 } else {
1108 e = new (graph->GetArena()) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
1109 }
1110 *result = Insert(block, new (graph->GetArena()) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001111 }
1112 }
1113 return true;
1114 }
1115 }
1116 return false;
1117}
1118
Aart Bikdf7822e2016-12-06 10:05:30 -08001119bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1120 HInductionVarAnalysis::InductionInfo* trip,
1121 HGraph* graph,
1122 HBasicBlock* block,
1123 /*out*/HInstruction** result) const {
1124 DCHECK(info != nullptr);
1125 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1126 // Count depth.
1127 int32_t depth = 0;
1128 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1129 info = info->op_b, ++depth) {}
1130 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001131 // TODO: generalize, but be careful to adjust the terminal.
1132 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001133 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001134 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1135 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001136 }
1137 return false;
1138}
1139
Aart Bik9abf8942016-10-14 09:49:42 -07001140bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1141 HInductionVarAnalysis::InductionInfo* trip,
1142 HGraph* graph,
1143 HBasicBlock* block,
1144 /*out*/HInstruction** result,
1145 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001146 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001147 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik8523ea12017-06-01 15:45:09 -07001148 // Count period and detect all-invariants.
Aart Bike6bd0272016-12-16 13:57:52 -08001149 int64_t period = 1;
Aart Bik8523ea12017-06-01 15:45:09 -07001150 bool all_invariants = true;
1151 HInductionVarAnalysis::InductionInfo* p = info;
1152 for (; p->induction_class == HInductionVarAnalysis::kPeriodic; p = p->op_b, ++period) {
1153 DCHECK_EQ(p->op_a->induction_class, HInductionVarAnalysis::kInvariant);
1154 if (p->op_a->operation != HInductionVarAnalysis::kFetch) {
1155 all_invariants = false;
1156 }
1157 }
1158 DCHECK_EQ(p->induction_class, HInductionVarAnalysis::kInvariant);
1159 if (p->operation != HInductionVarAnalysis::kFetch) {
1160 all_invariants = false;
1161 }
1162 // Don't rely on FP arithmetic to be precise, unless the full period
1163 // consist of pre-computed expressions only.
1164 if (info->type == Primitive::kPrimFloat || info->type == Primitive::kPrimDouble) {
1165 if (!all_invariants) {
1166 return false;
1167 }
1168 }
Aart Bike6bd0272016-12-16 13:57:52 -08001169 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1170 int64_t m = 0;
1171 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1172 int64_t li = m % period;
1173 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1174 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1175 info = info->op_a;
1176 }
1177 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001178 }
Aart Bike6bd0272016-12-16 13:57:52 -08001179 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1180 // directly to obtain the maximum index value t even if taken test is needed.
1181 HInstruction* x = nullptr;
1182 HInstruction* y = nullptr;
1183 HInstruction* t = nullptr;
1184 if (period == 2 &&
1185 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1186 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1187 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1188 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001189 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001190 Primitive::Type type = trip->type;
1191 HInstruction* msk =
1192 Insert(block, new (graph->GetArena()) HAnd(type, t, graph->GetConstant(type, 1)));
1193 HInstruction* is_even =
1194 Insert(block, new (graph->GetArena()) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1195 *result = Insert(block, new (graph->GetArena()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001196 }
1197 // Guard select with taken test if needed.
1198 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001199 HInstruction* is_taken = nullptr;
1200 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1201 if (graph != nullptr) {
1202 *result = Insert(block, new (graph->GetArena()) HSelect(is_taken, *result, x, kNoDexPc));
1203 }
1204 *needs_taken_test = false; // taken care of
1205 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001206 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001207 }
Aart Bik9abf8942016-10-14 09:49:42 -07001208 }
1209 return true;
1210 }
1211 return false;
1212}
1213
Aart Bikaec3cce2015-10-14 17:44:55 -07001214bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1215 HInductionVarAnalysis::InductionInfo* trip,
1216 HGraph* graph, // when set, code is generated
1217 HBasicBlock* block,
1218 /*out*/HInstruction** result,
1219 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001220 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001221 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001222 // If during codegen, the result is not needed (nullptr), simply return success.
1223 if (graph != nullptr && result == nullptr) {
1224 return true;
1225 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001226 // Handle current operation.
Aart Bike6bd0272016-12-16 13:57:52 -08001227 Primitive::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001228 HInstruction* opa = nullptr;
1229 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001230 switch (info->induction_class) {
1231 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001232 // Invariants (note that since invariants only have other invariants as
1233 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001234 switch (info->operation) {
1235 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001236 case HInductionVarAnalysis::kSub:
1237 case HInductionVarAnalysis::kMul:
1238 case HInductionVarAnalysis::kDiv:
1239 case HInductionVarAnalysis::kRem:
1240 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001241 case HInductionVarAnalysis::kLT:
1242 case HInductionVarAnalysis::kLE:
1243 case HInductionVarAnalysis::kGT:
1244 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001245 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1246 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1247 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001248 HInstruction* operation = nullptr;
1249 switch (info->operation) {
1250 case HInductionVarAnalysis::kAdd:
1251 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001252 case HInductionVarAnalysis::kSub:
1253 operation = new (graph->GetArena()) HSub(type, opa, opb); break;
1254 case HInductionVarAnalysis::kMul:
1255 operation = new (graph->GetArena()) HMul(type, opa, opb, kNoDexPc); break;
1256 case HInductionVarAnalysis::kDiv:
1257 operation = new (graph->GetArena()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001258 case HInductionVarAnalysis::kRem:
1259 operation = new (graph->GetArena()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001260 case HInductionVarAnalysis::kXor:
1261 operation = new (graph->GetArena()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001262 case HInductionVarAnalysis::kLT:
1263 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
1264 case HInductionVarAnalysis::kLE:
1265 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
1266 case HInductionVarAnalysis::kGT:
1267 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
1268 case HInductionVarAnalysis::kGE:
1269 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
1270 default:
1271 LOG(FATAL) << "unknown operation";
1272 }
1273 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001274 }
1275 return true;
1276 }
1277 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001278 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001279 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1280 if (graph != nullptr) {
1281 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
1282 }
1283 return true;
1284 }
1285 break;
1286 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001287 if (graph != nullptr) {
1288 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001289 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001290 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001291 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001292 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001293 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001294 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001295 }
1296 FALLTHROUGH_INTENDED;
1297 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001298 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001299 if (is_min) {
1300 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001301 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001302 }
1303 return true;
1304 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001305 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001306 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001307 *result =
1308 Insert(block,
1309 new (graph->GetArena()) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001310 }
1311 return true;
1312 }
1313 }
1314 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001315 case HInductionVarAnalysis::kNop:
1316 LOG(FATAL) << "unexpected invariant nop";
1317 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001318 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001319 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001320 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1321 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1322 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001323 // known stride yields right value. Always avoid any narrowing linear induction or
1324 // any type mismatch between the linear induction and the trip count expression.
1325 // TODO: careful runtime type conversions could generalize this latter restriction.
1326 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1327 int64_t stride_value = 0;
1328 if (IsConstant(info->op_a, kExact, &stride_value) &&
1329 CanLongValueFitIntoInt(stride_value)) {
1330 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1331 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1332 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1333 if (graph != nullptr) {
1334 HInstruction* oper;
1335 if (stride_value == 1) {
1336 oper = new (graph->GetArena()) HAdd(type, opa, opb);
1337 } else if (stride_value == -1) {
1338 oper = new (graph->GetArena()) HSub(type, opb, opa);
1339 } else {
1340 HInstruction* mul =
1341 new (graph->GetArena()) HMul(type, graph->GetConstant(type, stride_value), opa);
1342 oper = new (graph->GetArena()) HAdd(type, Insert(block, mul), opb);
1343 }
1344 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001345 }
Aart Bike6bd0272016-12-16 13:57:52 -08001346 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001347 }
1348 }
1349 }
1350 break;
Aart Bik4a342772015-11-30 10:17:46 -08001351 }
Aart Bikc071a012016-12-01 10:22:31 -08001352 case HInductionVarAnalysis::kPolynomial:
1353 case HInductionVarAnalysis::kGeometric:
1354 break;
Aart Bik4a342772015-11-30 10:17:46 -08001355 case HInductionVarAnalysis::kWrapAround:
1356 case HInductionVarAnalysis::kPeriodic: {
1357 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1358 // values are easy to test at runtime without complications of arithmetic wrap-around.
1359 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001360 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001361 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001362 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001363 }
1364 return true;
1365 }
1366 break;
1367 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001368 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001369 }
1370 return false;
1371}
1372
Aart Bik16d3a652016-09-09 10:33:50 -07001373void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1374 HInstruction* fetch,
1375 HInstruction* replacement) {
1376 if (info != nullptr) {
1377 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1378 info->operation == HInductionVarAnalysis::kFetch &&
1379 info->fetch == fetch) {
1380 info->fetch = replacement;
1381 }
1382 ReplaceInduction(info->op_a, fetch, replacement);
1383 ReplaceInduction(info->op_b, fetch, replacement);
1384 }
1385}
1386
Aart Bikd14c5952015-09-08 15:25:15 -07001387} // namespace art