blob: d699d01ecbbe9076195de421fadb179e3634362c [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));
Aart Bik40fbf742016-10-31 11:02:50 -070088 default:
89 break;
90 }
Aart Bik3dad3412018-02-28 12:01:46 -080091 } else if (instruction->IsAbs()) {
92 // Instruction ABS(>=0) is >= 0.
93 // NOTE: ABS(minint) = minint prevents assuming
94 // >= 0 without looking at the argument.
95 return IsGEZero(instruction->InputAt(0));
Aart Bik40fbf742016-10-31 11:02:50 -070096 }
97 int64_t value = -1;
Aart Bikf3e61ee2017-04-12 17:09:20 -070098 return IsInt64AndGet(instruction, &value) && value >= 0;
Aart Bik40fbf742016-10-31 11:02:50 -070099}
100
101/** Hunts "under the hood" for a suitable instruction at the hint. */
102static bool IsMaxAtHint(
103 HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) {
104 if (instruction->IsInvokeStaticOrDirect()) {
105 switch (instruction->AsInvoke()->GetIntrinsic()) {
106 case Intrinsics::kMathMinIntInt:
107 case Intrinsics::kMathMinLongLong:
108 // For MIN(x, y), return most suitable x or y as maximum.
109 return IsMaxAtHint(instruction->InputAt(0), hint, suitable) ||
110 IsMaxAtHint(instruction->InputAt(1), hint, suitable);
111 default:
112 break;
113 }
114 } else {
115 *suitable = instruction;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000116 return HuntForDeclaration(instruction) == hint;
Aart Bik40fbf742016-10-31 11:02:50 -0700117 }
118 return false;
119}
120
121/** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */
122static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) {
123 if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) {
124 // If a == 1, instruction >= 0 and b <= 0, just return the constant b.
125 // No arithmetic wrap-around can occur.
126 if (IsGEZero(v.instruction)) {
127 return InductionVarRange::Value(v.b_constant);
128 }
Aart Bikb3365e02015-09-21 14:45:05 -0700129 }
130 return v;
131}
132
Aart Bik40fbf742016-10-31 11:02:50 -0700133/** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */
134static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) {
135 if (v.is_known && v.a_constant >= 1) {
136 // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as
137 // length + b because length >= 0 is true.
138 int64_t value;
139 if (v.instruction->IsDiv() &&
140 v.instruction->InputAt(0)->IsArrayLength() &&
Aart Bikf3e61ee2017-04-12 17:09:20 -0700141 IsInt64AndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
Aart Bik40fbf742016-10-31 11:02:50 -0700142 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
143 }
144 // If a == 1, the most suitable one suffices as maximum value.
145 HInstruction* suitable = nullptr;
146 if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) {
147 return InductionVarRange::Value(suitable, 1, v.b_constant);
148 }
149 }
150 return v;
151}
152
153/** Tests for a constant value. */
Aart Bik52be7e72016-06-23 11:20:41 -0700154static bool IsConstantValue(InductionVarRange::Value v) {
155 return v.is_known && v.a_constant == 0;
156}
157
158/** Corrects a value for type to account for arithmetic wrap-around in lower precision. */
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100159static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, DataType::Type type) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700160 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100161 case DataType::Type::kUint8:
162 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100163 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100164 case DataType::Type::kInt16: {
Aart Bik0d345cf2016-03-16 10:49:38 -0700165 // Constants within range only.
166 // TODO: maybe some room for improvement, like allowing widening conversions
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100167 int32_t min = DataType::MinValueOfIntegralType(type);
168 int32_t max = DataType::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 Bik97412c92016-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) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100219 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100220 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100221 case DataType::Type::kUint16:
222 case DataType::Type::kInt16:
223 case DataType::Type::kInt32:
Aart Bik0d345cf2016-03-16 10:49:38 -0700224 break;
225 default:
226 return false;
227 }
Aart Bik97412c92016-02-19 20:14:38 -0800228 // Find range.
Aart Bik52be7e72016-06-23 11:20:41 -0700229 chase_hint_ = chase_hint;
230 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700231 int64_t stride_value = 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700232 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
233 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint);
Aart Bik16d3a652016-09-09 10:33:50 -0700234 *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip);
Aart Bik40fbf742016-10-31 11:02:50 -0700235 chase_hint_ = nullptr;
236 // Retry chasing constants for wrap-around (merge sensitive).
237 if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) {
238 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
239 }
Aart Bik97412c92016-02-19 20:14:38 -0800240 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700241}
242
Aart Bik16d3a652016-09-09 10:33:50 -0700243bool InductionVarRange::CanGenerateRange(HInstruction* context,
244 HInstruction* instruction,
245 /*out*/bool* needs_finite_test,
246 /*out*/bool* needs_taken_test) {
247 bool is_last_value = false;
248 int64_t stride_value = 0;
Aart Bik9abf8942016-10-14 09:49:42 -0700249 return GenerateRangeOrLastValue(context,
250 instruction,
251 is_last_value,
252 nullptr,
253 nullptr,
254 nullptr,
255 nullptr,
256 nullptr, // nothing generated yet
257 &stride_value,
258 needs_finite_test,
259 needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700260 && (stride_value == -1 ||
261 stride_value == 0 ||
Aart Bik40fbf742016-10-31 11:02:50 -0700262 stride_value == 1); // avoid arithmetic wrap-around anomalies.
Aart Bikaec3cce2015-10-14 17:44:55 -0700263}
264
Aart Bik16d3a652016-09-09 10:33:50 -0700265void InductionVarRange::GenerateRange(HInstruction* context,
266 HInstruction* instruction,
267 HGraph* graph,
268 HBasicBlock* block,
269 /*out*/HInstruction** lower,
270 /*out*/HInstruction** upper) {
271 bool is_last_value = false;
Aart Bik009cace2016-09-16 10:15:19 -0700272 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700273 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700274 if (!GenerateRangeOrLastValue(context,
275 instruction,
276 is_last_value,
277 graph,
278 block,
279 lower,
280 upper,
281 nullptr,
282 &stride_value,
283 &b1,
284 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700285 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
Aart Bik389b3db2015-10-28 14:23:40 -0700286 }
287}
288
Aart Bik16d3a652016-09-09 10:33:50 -0700289HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context,
290 HGraph* graph,
291 HBasicBlock* block) {
292 HInstruction* taken_test = nullptr;
293 bool is_last_value = false;
294 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700295 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700296 if (!GenerateRangeOrLastValue(context,
297 context,
298 is_last_value,
299 graph,
300 block,
301 nullptr,
302 nullptr,
303 &taken_test,
304 &stride_value,
305 &b1,
306 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700307 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
308 }
309 return taken_test;
310}
311
312bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) {
313 bool is_last_value = true;
314 int64_t stride_value = 0;
315 bool needs_finite_test = false;
316 bool needs_taken_test = false;
Aart Bik9abf8942016-10-14 09:49:42 -0700317 return GenerateRangeOrLastValue(instruction,
318 instruction,
319 is_last_value,
320 nullptr,
321 nullptr,
322 nullptr,
323 nullptr,
324 nullptr, // nothing generated yet
325 &stride_value,
326 &needs_finite_test,
327 &needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700328 && !needs_finite_test && !needs_taken_test;
329}
330
331HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction,
332 HGraph* graph,
333 HBasicBlock* block) {
334 HInstruction* last_value = nullptr;
335 bool is_last_value = true;
336 int64_t stride_value = 0;
337 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700338 if (!GenerateRangeOrLastValue(instruction,
339 instruction,
340 is_last_value,
341 graph,
342 block,
343 &last_value,
344 &last_value,
345 nullptr,
346 &stride_value,
347 &b1,
348 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700349 LOG(FATAL) << "Failed precondition: CanGenerateLastValue()";
350 }
351 return last_value;
352}
353
354void InductionVarRange::Replace(HInstruction* instruction,
355 HInstruction* fetch,
356 HInstruction* replacement) {
357 for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
358 lp != nullptr;
359 lp = lp->GetPreHeader()->GetLoopInformation()) {
Aart Bik009cace2016-09-16 10:15:19 -0700360 // Update instruction's information.
Aart Bik16d3a652016-09-09 10:33:50 -0700361 ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement);
Aart Bik009cace2016-09-16 10:15:19 -0700362 // Update loop's trip-count information.
363 ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement);
Aart Bik389b3db2015-10-28 14:23:40 -0700364 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700365}
366
Aart Bik6b69e0a2017-01-11 10:20:43 -0800367bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* tc) const {
Aart Bik9abf8942016-10-14 09:49:42 -0700368 HInductionVarAnalysis::InductionInfo *trip =
369 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
Aart Bik6b69e0a2017-01-11 10:20:43 -0800370 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
371 IsConstant(trip->op_a, kExact, tc);
372 return true;
373 }
374 return false;
Aart Bik9abf8942016-10-14 09:49:42 -0700375}
376
Aart Bikfa762962017-04-07 11:33:37 -0700377bool InductionVarRange::IsUnitStride(HInstruction* context,
378 HInstruction* instruction,
Aart Bik37dc4df2017-06-28 14:08:00 -0700379 HGraph* graph,
Aart Bik8e02e3e2017-02-28 14:41:55 -0800380 /*out*/ HInstruction** offset) const {
381 HLoopInformation* loop = nullptr;
382 HInductionVarAnalysis::InductionInfo* info = nullptr;
383 HInductionVarAnalysis::InductionInfo* trip = nullptr;
Aart Bikfa762962017-04-07 11:33:37 -0700384 if (HasInductionInfo(context, instruction, &loop, &info, &trip)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800385 if (info->induction_class == HInductionVarAnalysis::kLinear &&
Aart Bik7adb6882017-03-07 13:28:51 -0800386 !HInductionVarAnalysis::IsNarrowingLinear(info)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800387 int64_t stride_value = 0;
388 if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) {
389 int64_t off_value = 0;
Aart Bik37dc4df2017-06-28 14:08:00 -0700390 if (IsConstant(info->op_b, kExact, &off_value)) {
391 *offset = graph->GetConstant(info->op_b->type, off_value);
392 } else if (info->op_b->operation == HInductionVarAnalysis::kFetch) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800393 *offset = info->op_b->fetch;
Aart Bik37dc4df2017-06-28 14:08:00 -0700394 } else {
395 return false;
Aart Bik8e02e3e2017-02-28 14:41:55 -0800396 }
397 return true;
398 }
399 }
400 }
401 return false;
402}
403
404HInstruction* InductionVarRange::GenerateTripCount(HLoopInformation* loop,
405 HGraph* graph,
406 HBasicBlock* block) {
407 HInductionVarAnalysis::InductionInfo *trip =
408 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
409 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
410 HInstruction* taken_test = nullptr;
411 HInstruction* trip_expr = nullptr;
412 if (IsBodyTripCount(trip)) {
413 if (!GenerateCode(trip->op_b, nullptr, graph, block, &taken_test, false, false)) {
414 return nullptr;
415 }
416 }
417 if (GenerateCode(trip->op_a, nullptr, graph, block, &trip_expr, false, false)) {
418 if (taken_test != nullptr) {
419 HInstruction* zero = graph->GetConstant(trip->type, 0);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100420 ArenaAllocator* allocator = graph->GetAllocator();
421 trip_expr = Insert(block, new (allocator) HSelect(taken_test, trip_expr, zero, kNoDexPc));
Aart Bik8e02e3e2017-02-28 14:41:55 -0800422 }
423 return trip_expr;
424 }
425 }
426 return nullptr;
427}
428
Aart Bikd14c5952015-09-08 15:25:15 -0700429//
430// Private class methods.
431//
432
Aart Bik97412c92016-02-19 20:14:38 -0800433bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
434 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700435 /*out*/ int64_t* value) const {
Aart Bik97412c92016-02-19 20:14:38 -0800436 if (info != nullptr) {
437 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
438 // any of the three requests (kExact, kAtMost, and KAtLeast).
439 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
440 info->operation == HInductionVarAnalysis::kFetch) {
Aart Bikf3e61ee2017-04-12 17:09:20 -0700441 if (IsInt64AndGet(info->fetch, value)) {
Aart Bik97412c92016-02-19 20:14:38 -0800442 return true;
443 }
444 }
Aart Bik40fbf742016-10-31 11:02:50 -0700445 // Try range analysis on the invariant, only accept a proper range
446 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700447 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
448 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
449 if (IsConstantValue(min_val) &&
450 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
451 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
452 *value = max_val.b_constant;
453 return true;
454 } else if (request == kAtLeast) {
455 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800456 return true;
457 }
458 }
Aart Bik97412c92016-02-19 20:14:38 -0800459 }
460 return false;
461}
462
Aart Bik52be7e72016-06-23 11:20:41 -0700463bool InductionVarRange::HasInductionInfo(
464 HInstruction* context,
465 HInstruction* instruction,
466 /*out*/ HLoopInformation** loop,
467 /*out*/ HInductionVarAnalysis::InductionInfo** info,
468 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800469 DCHECK(context != nullptr);
470 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700471 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
472 if (lp != nullptr) {
473 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700474 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700475 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700476 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700477 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700478 return true;
479 }
480 }
481 return false;
482}
483
484bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
485 if (trip != nullptr) {
486 // Both bounds that define a trip-count are well-behaved if they either are not defined
487 // in any loop, or are contained in a proper interval. This allows finding the min/max
488 // of an expression by chasing outward.
489 InductionVarRange range(induction_analysis_);
490 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
491 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
492 int64_t not_used = 0;
493 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
494 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
495 }
496 return true;
497}
498
499bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
500 if (info != nullptr) {
501 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
502 info->operation == HInductionVarAnalysis::kFetch) {
503 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
504 }
505 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
506 }
507 return false;
508}
509
Aart Bik16d3a652016-09-09 10:33:50 -0700510bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
511 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700512 if (info != nullptr) {
513 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700514 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800515 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
516 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700517 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700518 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700519 }
Aart Bikd14c5952015-09-08 15:25:15 -0700520 }
Aart Bik389b3db2015-10-28 14:23:40 -0700521 return false;
522}
523
Aart Bik7d57d7f2015-12-09 14:39:48 -0800524bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700525 if (trip != nullptr) {
526 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
527 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
528 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
529 }
530 }
531 return false;
532}
533
Aart Bik7d57d7f2015-12-09 14:39:48 -0800534bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700535 if (trip != nullptr) {
536 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
537 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
538 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
539 }
540 }
541 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700542}
543
Aart Bik7d57d7f2015-12-09 14:39:48 -0800544InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
545 HInductionVarAnalysis::InductionInfo* trip,
546 bool in_body,
547 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800548 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800549 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700550 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800551 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
552 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
553 // with intermediate results that only incorporate single instructions.
554 if (trip != nullptr) {
555 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700556 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c92016-02-19 20:14:38 -0800557 int64_t stride_value = 0;
558 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800559 if (!is_min && stride_value == 1) {
Aart Bik97412c92016-02-19 20:14:38 -0800560 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800561 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
562 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
563 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700564 trip->induction_class,
565 trip->operation,
566 trip_expr->op_a,
567 trip->op_b,
568 nullptr,
569 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800570 return GetVal(&cancelled_trip, trip, in_body, is_min);
571 }
572 } else if (is_min && stride_value == -1) {
Aart Bik97412c92016-02-19 20:14:38 -0800573 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800574 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
575 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
576 HInductionVarAnalysis::InductionInfo neg(
577 HInductionVarAnalysis::kInvariant,
578 HInductionVarAnalysis::kNeg,
579 nullptr,
580 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700581 nullptr,
582 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800583 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700584 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800585 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
586 }
587 }
588 }
589 }
590 }
591 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
592 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
593 GetVal(info->op_b, trip, in_body, is_min));
594}
595
Aart Bikdf7822e2016-12-06 10:05:30 -0800596InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
597 HInductionVarAnalysis::InductionInfo* trip,
598 bool in_body,
599 bool is_min) const {
600 DCHECK(info != nullptr);
601 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
602 int64_t a = 0;
603 int64_t b = 0;
604 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
605 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800606 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800607 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
608 Value c = GetVal(info->op_b, trip, in_body, is_min);
609 if (is_min) {
610 return c;
611 } else {
612 Value m = GetVal(trip, trip, in_body, is_min);
613 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
614 Value x = MulValue(Value(a), t);
615 Value y = MulValue(Value(b), m);
616 return AddValue(AddValue(x, y), c);
617 }
618 }
619 return Value();
620}
621
Aart Bikc071a012016-12-01 10:22:31 -0800622InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
623 HInductionVarAnalysis::InductionInfo* trip,
624 bool in_body,
625 bool is_min) const {
626 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800627 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800628 int64_t a = 0;
629 int64_t f = 0;
630 if (IsConstant(info->op_a, kExact, &a) &&
631 CanLongValueFitIntoInt(a) &&
Aart Bikf3e61ee2017-04-12 17:09:20 -0700632 IsInt64AndGet(info->fetch, &f) && f >= 1) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800633 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
634 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800635 const bool is_min_a = a >= 0 ? is_min : !is_min;
636 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800637 Value b = GetVal(info->op_b, trip, in_body, is_min);
638 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800639 }
640 }
641 return Value();
642}
643
Aart Bikd14c5952015-09-08 15:25:15 -0700644InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700645 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700646 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800647 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700648 // Special case when chasing constants: single instruction that denotes trip count in the
649 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
650 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700651 if (is_min) {
652 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800653 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700654 return Value(std::numeric_limits<int32_t>::max());
655 }
656 }
Aart Bik40fbf742016-10-31 11:02:50 -0700657 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
658 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
659 int64_t value;
Aart Bikf3e61ee2017-04-12 17:09:20 -0700660 if (IsInt64AndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
Aart Bik40fbf742016-10-31 11:02:50 -0700661 // Proper constant reveals best information.
662 return Value(static_cast<int32_t>(value));
663 } else if (instruction == chase_hint_) {
664 // At hint, fetch is represented by itself.
665 return Value(instruction, 1, 0);
666 } else if (instruction->IsAdd()) {
667 // Incorporate suitable constants in the chased value.
Aart Bikf3e61ee2017-04-12 17:09:20 -0700668 if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
Aart Bik97412c92016-02-19 20:14:38 -0800669 return AddValue(Value(static_cast<int32_t>(value)),
670 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
Aart Bikf3e61ee2017-04-12 17:09:20 -0700671 } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
Aart Bik97412c92016-02-19 20:14:38 -0800672 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
673 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700674 }
Aart Bik8e9090b2017-09-08 16:46:50 -0700675 } else if (instruction->IsSub()) {
676 // Incorporate suitable constants in the chased value.
677 if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
678 return SubValue(Value(static_cast<int32_t>(value)),
679 GetFetch(instruction->InputAt(1), trip, in_body, !is_min));
680 } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
681 return SubValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
682 Value(static_cast<int32_t>(value)));
683 }
Aart Bik52be7e72016-06-23 11:20:41 -0700684 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700685 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700686 if (chase_hint_ == nullptr) {
687 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
688 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000689 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700690 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700691 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700692 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800693 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100694 if (instruction->AsTypeConversion()->GetInputType() == DataType::Type::kInt32 &&
695 instruction->AsTypeConversion()->GetResultType() == DataType::Type::kInt64) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700696 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
697 }
Aart Bik52be7e72016-06-23 11:20:41 -0700698 }
699 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
700 // so far is well-behaved in both bounds and the next trip-count is safe.
701 // Example:
702 // for (int i = 0; i <= 100; i++) // safe
703 // for (int j = 0; j <= i; j++) // well-behaved
704 // j is in range [0, i ] (if i is chase hint)
705 // or in range [0, 100] (otherwise)
706 HLoopInformation* next_loop = nullptr;
707 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
708 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
709 bool next_in_body = true; // inner loop is always in body of outer loop
710 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
711 IsWellBehavedTripCount(trip) &&
712 !IsUnsafeTripCount(next_trip)) {
713 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700714 }
Aart Bik40fbf742016-10-31 11:02:50 -0700715 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700716 return Value(instruction, 1, 0);
717}
718
Aart Bikcd26feb2015-09-23 17:50:50 -0700719InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
720 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700721 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800722 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700723 if (info != nullptr) {
724 switch (info->induction_class) {
725 case HInductionVarAnalysis::kInvariant:
726 // Invariants.
727 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700728 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700729 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
730 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700731 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700732 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
733 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700734 case HInductionVarAnalysis::kNeg: // second reversed!
735 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700736 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700737 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700738 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700739 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700740 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800741 case HInductionVarAnalysis::kRem:
742 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700743 case HInductionVarAnalysis::kXor:
744 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700745 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700746 return GetFetch(info->fetch, trip, in_body, is_min);
747 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700748 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700749 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700750 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700751 }
752 FALLTHROUGH_INTENDED;
753 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700754 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700755 if (is_min) {
756 return Value(0);
757 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700758 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700759 }
760 break;
761 default:
762 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700763 }
764 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700765 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700766 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800767 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800768 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800769 case HInductionVarAnalysis::kGeometric:
770 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700771 case HInductionVarAnalysis::kWrapAround:
772 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700773 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
774 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700775 }
776 }
Aart Bikb3365e02015-09-21 14:45:05 -0700777 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700778}
779
780InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
781 HInductionVarAnalysis::InductionInfo* info2,
782 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700783 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800784 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700785 // Constant times range.
786 int64_t value = 0;
787 if (IsConstant(info1, kExact, &value)) {
788 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
789 } else if (IsConstant(info2, kExact, &value)) {
790 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
791 }
792 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700793 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
794 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
795 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
796 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c92016-02-19 20:14:38 -0800797 // Positive range vs. positive or negative range.
798 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
799 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
800 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
801 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
802 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700803 }
Aart Bik97412c92016-02-19 20:14:38 -0800804 }
805 // Negative range vs. positive or negative range.
806 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
807 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
808 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
809 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
810 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700811 }
812 }
Aart Bikb3365e02015-09-21 14:45:05 -0700813 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700814}
815
816InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
817 HInductionVarAnalysis::InductionInfo* info2,
818 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700819 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800820 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700821 // Range divided by constant.
822 int64_t value = 0;
823 if (IsConstant(info2, kExact, &value)) {
824 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
825 }
826 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700827 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
828 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
829 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
830 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c92016-02-19 20:14:38 -0800831 // Positive range vs. positive or negative range.
832 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
833 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
834 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
835 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
836 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700837 }
Aart Bik97412c92016-02-19 20:14:38 -0800838 }
839 // Negative range vs. positive or negative range.
840 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
841 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
842 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
843 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
844 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700845 }
846 }
Aart Bikb3365e02015-09-21 14:45:05 -0700847 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700848}
849
Aart Bikdf7822e2016-12-06 10:05:30 -0800850InductionVarRange::Value InductionVarRange::GetRem(
851 HInductionVarAnalysis::InductionInfo* info1,
852 HInductionVarAnalysis::InductionInfo* info2) const {
853 int64_t v1 = 0;
854 int64_t v2 = 0;
855 // Only accept exact values.
856 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
857 int64_t value = v1 % v2;
858 if (CanLongValueFitIntoInt(value)) {
859 return Value(static_cast<int32_t>(value));
860 }
861 }
862 return Value();
863}
864
Aart Bik7dc96932016-10-12 10:01:05 -0700865InductionVarRange::Value InductionVarRange::GetXor(
866 HInductionVarAnalysis::InductionInfo* info1,
867 HInductionVarAnalysis::InductionInfo* info2) const {
868 int64_t v1 = 0;
869 int64_t v2 = 0;
870 // Only accept exact values.
871 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
872 int64_t value = v1 ^ v2;
873 if (CanLongValueFitIntoInt(value)) {
874 return Value(static_cast<int32_t>(value));
875 }
876 }
877 return Value();
878}
879
Aart Bik52be7e72016-06-23 11:20:41 -0700880InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
881 int64_t value,
882 HInductionVarAnalysis::InductionInfo* info,
883 HInductionVarAnalysis::InductionInfo* trip,
884 bool in_body,
885 bool is_min) const {
886 if (CanLongValueFitIntoInt(value)) {
887 Value c(static_cast<int32_t>(value));
888 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
889 }
890 return Value();
Aart Bik97412c92016-02-19 20:14:38 -0800891}
892
Aart Bik52be7e72016-06-23 11:20:41 -0700893InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
894 int64_t value,
895 HInductionVarAnalysis::InductionInfo* info,
896 HInductionVarAnalysis::InductionInfo* trip,
897 bool in_body,
898 bool is_min) const {
899 if (CanLongValueFitIntoInt(value)) {
900 Value c(static_cast<int32_t>(value));
901 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
902 }
903 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700904}
905
Aart Bik7d57d7f2015-12-09 14:39:48 -0800906InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700907 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800908 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700909 if (v1.a_constant == 0) {
910 return Value(v2.instruction, v2.a_constant, b);
911 } else if (v2.a_constant == 0) {
912 return Value(v1.instruction, v1.a_constant, b);
913 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
914 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
915 }
916 }
Aart Bikb3365e02015-09-21 14:45:05 -0700917 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700918}
919
Aart Bik7d57d7f2015-12-09 14:39:48 -0800920InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700921 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800922 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700923 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
924 return Value(v2.instruction, -v2.a_constant, b);
925 } else if (v2.a_constant == 0) {
926 return Value(v1.instruction, v1.a_constant, b);
927 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
928 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
929 }
930 }
Aart Bikb3365e02015-09-21 14:45:05 -0700931 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700932}
933
Aart Bik7d57d7f2015-12-09 14:39:48 -0800934InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700935 if (v1.is_known && v2.is_known) {
936 if (v1.a_constant == 0) {
937 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
938 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
939 }
940 } else if (v2.a_constant == 0) {
941 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
942 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
943 }
Aart Bikd14c5952015-09-08 15:25:15 -0700944 }
945 }
Aart Bikb3365e02015-09-21 14:45:05 -0700946 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700947}
948
Aart Bik7d57d7f2015-12-09 14:39:48 -0800949InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700950 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700951 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
952 return Value(v1.b_constant / v2.b_constant);
953 }
954 }
Aart Bikb3365e02015-09-21 14:45:05 -0700955 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700956}
957
Aart Bik7d57d7f2015-12-09 14:39:48 -0800958InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700959 if (v1.is_known && v2.is_known) {
960 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700961 return Value(v1.instruction, v1.a_constant,
962 is_min ? std::min(v1.b_constant, v2.b_constant)
963 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700964 }
Aart Bikd14c5952015-09-08 15:25:15 -0700965 }
Aart Bikb3365e02015-09-21 14:45:05 -0700966 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700967}
968
Aart Bik9abf8942016-10-14 09:49:42 -0700969bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
970 HInstruction* instruction,
971 bool is_last_value,
972 HGraph* graph,
973 HBasicBlock* block,
974 /*out*/HInstruction** lower,
975 /*out*/HInstruction** upper,
976 /*out*/HInstruction** taken_test,
977 /*out*/int64_t* stride_value,
978 /*out*/bool* needs_finite_test,
979 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700980 HLoopInformation* loop = nullptr;
981 HInductionVarAnalysis::InductionInfo* info = nullptr;
982 HInductionVarAnalysis::InductionInfo* trip = nullptr;
983 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
984 return false; // codegen needs all information, including tripcount
Aart Bik97412c92016-02-19 20:14:38 -0800985 }
986 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
987 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
988 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
989 // code does not use the trip-count explicitly (since there could be an implicit relation
990 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700991 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700992 *stride_value = 0;
993 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c92016-02-19 20:14:38 -0800994 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700995 // Handle last value request.
996 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800997 DCHECK(!in_body);
998 switch (info->induction_class) {
999 case HInductionVarAnalysis::kLinear:
1000 if (*stride_value > 0) {
1001 lower = nullptr;
1002 } else {
1003 upper = nullptr;
1004 }
1005 break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001006 case HInductionVarAnalysis::kPolynomial:
1007 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001008 case HInductionVarAnalysis::kGeometric:
1009 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -08001010 case HInductionVarAnalysis::kWrapAround:
1011 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001012 case HInductionVarAnalysis::kPeriodic:
1013 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1014 default:
1015 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001016 }
1017 }
Aart Bik97412c92016-02-19 20:14:38 -08001018 // Code generation for taken test: generate the code when requested or otherwise analyze
1019 // if code generation is feasible when taken test is needed.
1020 if (taken_test != nullptr) {
1021 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1022 } else if (*needs_taken_test) {
1023 if (!GenerateCode(
1024 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1025 return false;
1026 }
1027 }
1028 // Code generation for lower and upper.
1029 return
1030 // Success on lower if invariant (not set), or code can be generated.
1031 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1032 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1033 // And success on upper.
1034 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001035}
1036
Aart Bikdf7822e2016-12-06 10:05:30 -08001037bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1038 HInductionVarAnalysis::InductionInfo* trip,
1039 HGraph* graph,
1040 HBasicBlock* block,
1041 /*out*/HInstruction** result) const {
1042 DCHECK(info != nullptr);
1043 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1044 // Detect known coefficients and trip count (always taken).
1045 int64_t a = 0;
1046 int64_t b = 0;
1047 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001048 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1049 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001050 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001051 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001052 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001053 HInstruction* c = nullptr;
1054 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001055 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001056 DataType::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001057 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001058 if (type != DataType::Type::kInt64) {
Aart Bike6bd0272016-12-16 13:57:52 -08001059 sum = static_cast<int32_t>(sum); // okay to truncate
1060 }
1061 *result =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001062 Insert(block, new (graph->GetAllocator()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001063 }
1064 return true;
1065 }
1066 }
1067 return false;
1068}
1069
Aart Bikc071a012016-12-01 10:22:31 -08001070bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1071 HInductionVarAnalysis::InductionInfo* trip,
1072 HGraph* graph,
1073 HBasicBlock* block,
1074 /*out*/HInstruction** result) const {
1075 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001076 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001077 // Detect known base and trip count (always taken).
1078 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001079 int64_t m = 0;
Aart Bikf3e61ee2017-04-12 17:09:20 -07001080 if (IsInt64AndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001081 HInstruction* opa = nullptr;
1082 HInstruction* opb = nullptr;
1083 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1084 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001085 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001086 DataType::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001087 // Compute f ^ m for known maximum index value m.
1088 bool overflow = false;
1089 int64_t fpow = IntPow(f, m, &overflow);
1090 if (info->operation == HInductionVarAnalysis::kDiv) {
1091 // For division, any overflow truncates to zero.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001092 if (overflow || (type != DataType::Type::kInt64 && !CanLongValueFitIntoInt(fpow))) {
Aart Bikd3ba6262017-01-30 14:37:12 -08001093 fpow = 0;
1094 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001095 } else if (type != DataType::Type::kInt64) {
Aart Bikd3ba6262017-01-30 14:37:12 -08001096 // For multiplication, okay to truncate to required precision.
1097 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1098 fpow = static_cast<int32_t>(fpow);
1099 }
1100 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001101 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001102 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001103 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001104 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001105 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001106 HInstruction* e = nullptr;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001107 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001108 if (info->operation == HInductionVarAnalysis::kMul) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001109 e = new (allocator) HMul(type, opa, graph->GetConstant(type, fpow));
Aart Bike6bd0272016-12-16 13:57:52 -08001110 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001111 e = new (allocator) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
Aart Bike6bd0272016-12-16 13:57:52 -08001112 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001113 *result = Insert(block, new (allocator) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001114 }
1115 }
1116 return true;
1117 }
1118 }
1119 return false;
1120}
1121
Aart Bikdf7822e2016-12-06 10:05:30 -08001122bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1123 HInductionVarAnalysis::InductionInfo* trip,
1124 HGraph* graph,
1125 HBasicBlock* block,
1126 /*out*/HInstruction** result) const {
1127 DCHECK(info != nullptr);
1128 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1129 // Count depth.
1130 int32_t depth = 0;
1131 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1132 info = info->op_b, ++depth) {}
1133 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001134 // TODO: generalize, but be careful to adjust the terminal.
1135 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001136 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001137 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1138 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001139 }
1140 return false;
1141}
1142
Aart Bik9abf8942016-10-14 09:49:42 -07001143bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1144 HInductionVarAnalysis::InductionInfo* trip,
1145 HGraph* graph,
1146 HBasicBlock* block,
1147 /*out*/HInstruction** result,
1148 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001149 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001150 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik8523ea12017-06-01 15:45:09 -07001151 // Count period and detect all-invariants.
Aart Bike6bd0272016-12-16 13:57:52 -08001152 int64_t period = 1;
Aart Bik8523ea12017-06-01 15:45:09 -07001153 bool all_invariants = true;
1154 HInductionVarAnalysis::InductionInfo* p = info;
1155 for (; p->induction_class == HInductionVarAnalysis::kPeriodic; p = p->op_b, ++period) {
1156 DCHECK_EQ(p->op_a->induction_class, HInductionVarAnalysis::kInvariant);
1157 if (p->op_a->operation != HInductionVarAnalysis::kFetch) {
1158 all_invariants = false;
1159 }
1160 }
1161 DCHECK_EQ(p->induction_class, HInductionVarAnalysis::kInvariant);
1162 if (p->operation != HInductionVarAnalysis::kFetch) {
1163 all_invariants = false;
1164 }
1165 // Don't rely on FP arithmetic to be precise, unless the full period
1166 // consist of pre-computed expressions only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001167 if (info->type == DataType::Type::kFloat32 || info->type == DataType::Type::kFloat64) {
Aart Bik8523ea12017-06-01 15:45:09 -07001168 if (!all_invariants) {
1169 return false;
1170 }
1171 }
Aart Bike6bd0272016-12-16 13:57:52 -08001172 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1173 int64_t m = 0;
1174 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1175 int64_t li = m % period;
1176 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1177 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1178 info = info->op_a;
1179 }
1180 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001181 }
Aart Bike6bd0272016-12-16 13:57:52 -08001182 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1183 // directly to obtain the maximum index value t even if taken test is needed.
1184 HInstruction* x = nullptr;
1185 HInstruction* y = nullptr;
1186 HInstruction* t = nullptr;
1187 if (period == 2 &&
1188 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1189 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1190 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1191 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001192 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001193 DataType::Type type = trip->type;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001194 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001195 HInstruction* msk =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001196 Insert(block, new (allocator) HAnd(type, t, graph->GetConstant(type, 1)));
Aart Bike6bd0272016-12-16 13:57:52 -08001197 HInstruction* is_even =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001198 Insert(block, new (allocator) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1199 *result = Insert(block, new (graph->GetAllocator()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001200 }
1201 // Guard select with taken test if needed.
1202 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001203 HInstruction* is_taken = nullptr;
1204 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1205 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001206 ArenaAllocator* allocator = graph->GetAllocator();
1207 *result = Insert(block, new (allocator) HSelect(is_taken, *result, x, kNoDexPc));
Aart Bike6bd0272016-12-16 13:57:52 -08001208 }
1209 *needs_taken_test = false; // taken care of
1210 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001211 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001212 }
Aart Bik9abf8942016-10-14 09:49:42 -07001213 }
1214 return true;
1215 }
1216 return false;
1217}
1218
Aart Bikaec3cce2015-10-14 17:44:55 -07001219bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1220 HInductionVarAnalysis::InductionInfo* trip,
1221 HGraph* graph, // when set, code is generated
1222 HBasicBlock* block,
1223 /*out*/HInstruction** result,
1224 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001225 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001226 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001227 // If during codegen, the result is not needed (nullptr), simply return success.
1228 if (graph != nullptr && result == nullptr) {
1229 return true;
1230 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001231 // Handle current operation.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001232 DataType::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001233 HInstruction* opa = nullptr;
1234 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001235 switch (info->induction_class) {
1236 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001237 // Invariants (note that since invariants only have other invariants as
1238 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001239 switch (info->operation) {
1240 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001241 case HInductionVarAnalysis::kSub:
1242 case HInductionVarAnalysis::kMul:
1243 case HInductionVarAnalysis::kDiv:
1244 case HInductionVarAnalysis::kRem:
1245 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001246 case HInductionVarAnalysis::kLT:
1247 case HInductionVarAnalysis::kLE:
1248 case HInductionVarAnalysis::kGT:
1249 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001250 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1251 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1252 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001253 HInstruction* operation = nullptr;
1254 switch (info->operation) {
1255 case HInductionVarAnalysis::kAdd:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001256 operation = new (graph->GetAllocator()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001257 case HInductionVarAnalysis::kSub:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001258 operation = new (graph->GetAllocator()) HSub(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001259 case HInductionVarAnalysis::kMul:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001260 operation = new (graph->GetAllocator()) HMul(type, opa, opb, kNoDexPc); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001261 case HInductionVarAnalysis::kDiv:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001262 operation = new (graph->GetAllocator()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001263 case HInductionVarAnalysis::kRem:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001264 operation = new (graph->GetAllocator()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001265 case HInductionVarAnalysis::kXor:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001266 operation = new (graph->GetAllocator()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001267 case HInductionVarAnalysis::kLT:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001268 operation = new (graph->GetAllocator()) HLessThan(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001269 case HInductionVarAnalysis::kLE:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001270 operation = new (graph->GetAllocator()) HLessThanOrEqual(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001271 case HInductionVarAnalysis::kGT:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001272 operation = new (graph->GetAllocator()) HGreaterThan(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001273 case HInductionVarAnalysis::kGE:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001274 operation = new (graph->GetAllocator()) HGreaterThanOrEqual(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001275 default:
1276 LOG(FATAL) << "unknown operation";
1277 }
1278 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001279 }
1280 return true;
1281 }
1282 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001283 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001284 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1285 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001286 *result = Insert(block, new (graph->GetAllocator()) HNeg(type, opb));
Aart Bikaec3cce2015-10-14 17:44:55 -07001287 }
1288 return true;
1289 }
1290 break;
1291 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001292 if (graph != nullptr) {
1293 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001294 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001295 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001296 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001297 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001298 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001299 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001300 }
1301 FALLTHROUGH_INTENDED;
1302 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001303 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001304 if (is_min) {
1305 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001306 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001307 }
1308 return true;
1309 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001310 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001311 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001312 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001313 *result =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001314 Insert(block, new (allocator) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001315 }
1316 return true;
1317 }
1318 }
1319 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001320 case HInductionVarAnalysis::kNop:
1321 LOG(FATAL) << "unexpected invariant nop";
1322 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001323 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001324 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001325 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1326 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1327 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001328 // known stride yields right value. Always avoid any narrowing linear induction or
1329 // any type mismatch between the linear induction and the trip count expression.
1330 // TODO: careful runtime type conversions could generalize this latter restriction.
1331 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1332 int64_t stride_value = 0;
1333 if (IsConstant(info->op_a, kExact, &stride_value) &&
1334 CanLongValueFitIntoInt(stride_value)) {
1335 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1336 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1337 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1338 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001339 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001340 HInstruction* oper;
1341 if (stride_value == 1) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001342 oper = new (allocator) HAdd(type, opa, opb);
Aart Bike6bd0272016-12-16 13:57:52 -08001343 } else if (stride_value == -1) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001344 oper = new (graph->GetAllocator()) HSub(type, opb, opa);
Aart Bike6bd0272016-12-16 13:57:52 -08001345 } else {
1346 HInstruction* mul =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001347 new (allocator) HMul(type, graph->GetConstant(type, stride_value), opa);
1348 oper = new (allocator) HAdd(type, Insert(block, mul), opb);
Aart Bike6bd0272016-12-16 13:57:52 -08001349 }
1350 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001351 }
Aart Bike6bd0272016-12-16 13:57:52 -08001352 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001353 }
1354 }
1355 }
1356 break;
Aart Bik4a342772015-11-30 10:17:46 -08001357 }
Aart Bikc071a012016-12-01 10:22:31 -08001358 case HInductionVarAnalysis::kPolynomial:
1359 case HInductionVarAnalysis::kGeometric:
1360 break;
Aart Bik4a342772015-11-30 10:17:46 -08001361 case HInductionVarAnalysis::kWrapAround:
1362 case HInductionVarAnalysis::kPeriodic: {
1363 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1364 // values are easy to test at runtime without complications of arithmetic wrap-around.
1365 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c92016-02-19 20:14:38 -08001366 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001367 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001368 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001369 }
1370 return true;
1371 }
1372 break;
1373 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001374 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001375 }
1376 return false;
1377}
1378
Aart Bik16d3a652016-09-09 10:33:50 -07001379void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1380 HInstruction* fetch,
1381 HInstruction* replacement) {
1382 if (info != nullptr) {
1383 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1384 info->operation == HInductionVarAnalysis::kFetch &&
1385 info->fetch == fetch) {
1386 info->fetch = replacement;
1387 }
1388 ReplaceInduction(info->op_a, fetch, replacement);
1389 ReplaceInduction(info->op_b, fetch, replacement);
1390 }
1391}
1392
Aart Bikd14c5952015-09-08 15:25:15 -07001393} // namespace art