blob: 55eca2316a173ad50888315803bcc5f336cd9a53 [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;
Aart Bik1f8d51b2018-02-15 10:42:37 -080081 } else if (instruction->IsMin()) {
82 // Instruction MIN(>=0, >=0) is >= 0.
83 return IsGEZero(instruction->InputAt(0)) &&
84 IsGEZero(instruction->InputAt(1));
Aart Bik3dad3412018-02-28 12:01:46 -080085 } else if (instruction->IsAbs()) {
86 // Instruction ABS(>=0) is >= 0.
87 // NOTE: ABS(minint) = minint prevents assuming
88 // >= 0 without looking at the argument.
89 return IsGEZero(instruction->InputAt(0));
Aart Bik40fbf742016-10-31 11:02:50 -070090 }
91 int64_t value = -1;
Aart Bikf3e61ee2017-04-12 17:09:20 -070092 return IsInt64AndGet(instruction, &value) && value >= 0;
Aart Bik40fbf742016-10-31 11:02:50 -070093}
94
95/** Hunts "under the hood" for a suitable instruction at the hint. */
96static bool IsMaxAtHint(
97 HInstruction* instruction, HInstruction* hint, /*out*/HInstruction** suitable) {
Aart Bik1f8d51b2018-02-15 10:42:37 -080098 if (instruction->IsMin()) {
99 // For MIN(x, y), return most suitable x or y as maximum.
100 return IsMaxAtHint(instruction->InputAt(0), hint, suitable) ||
101 IsMaxAtHint(instruction->InputAt(1), hint, suitable);
Aart Bik40fbf742016-10-31 11:02:50 -0700102 } else {
103 *suitable = instruction;
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000104 return HuntForDeclaration(instruction) == hint;
Aart Bik40fbf742016-10-31 11:02:50 -0700105 }
Aart Bik40fbf742016-10-31 11:02:50 -0700106}
107
108/** Post-analysis simplification of a minimum value that makes the bound more useful to clients. */
109static InductionVarRange::Value SimplifyMin(InductionVarRange::Value v) {
110 if (v.is_known && v.a_constant == 1 && v.b_constant <= 0) {
111 // If a == 1, instruction >= 0 and b <= 0, just return the constant b.
112 // No arithmetic wrap-around can occur.
113 if (IsGEZero(v.instruction)) {
114 return InductionVarRange::Value(v.b_constant);
115 }
Aart Bikb3365e02015-09-21 14:45:05 -0700116 }
117 return v;
118}
119
Aart Bik40fbf742016-10-31 11:02:50 -0700120/** Post-analysis simplification of a maximum value that makes the bound more useful to clients. */
121static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v, HInstruction* hint) {
122 if (v.is_known && v.a_constant >= 1) {
123 // An upper bound a * (length / a) + b, where a >= 1, can be conservatively rewritten as
124 // length + b because length >= 0 is true.
125 int64_t value;
126 if (v.instruction->IsDiv() &&
127 v.instruction->InputAt(0)->IsArrayLength() &&
Aart Bikf3e61ee2017-04-12 17:09:20 -0700128 IsInt64AndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
Aart Bik40fbf742016-10-31 11:02:50 -0700129 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
130 }
131 // If a == 1, the most suitable one suffices as maximum value.
132 HInstruction* suitable = nullptr;
133 if (v.a_constant == 1 && IsMaxAtHint(v.instruction, hint, &suitable)) {
134 return InductionVarRange::Value(suitable, 1, v.b_constant);
135 }
136 }
137 return v;
138}
139
140/** Tests for a constant value. */
Aart Bik52be7e72016-06-23 11:20:41 -0700141static bool IsConstantValue(InductionVarRange::Value v) {
142 return v.is_known && v.a_constant == 0;
143}
144
145/** Corrects a value for type to account for arithmetic wrap-around in lower precision. */
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100146static InductionVarRange::Value CorrectForType(InductionVarRange::Value v, DataType::Type type) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700147 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100148 case DataType::Type::kUint8:
149 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100150 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100151 case DataType::Type::kInt16: {
Aart Bik0d345cf2016-03-16 10:49:38 -0700152 // Constants within range only.
153 // TODO: maybe some room for improvement, like allowing widening conversions
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154 int32_t min = DataType::MinValueOfIntegralType(type);
155 int32_t max = DataType::MaxValueOfIntegralType(type);
Aart Bik52be7e72016-06-23 11:20:41 -0700156 return (IsConstantValue(v) && min <= v.b_constant && v.b_constant <= max)
Aart Bik0d345cf2016-03-16 10:49:38 -0700157 ? v
158 : InductionVarRange::Value();
159 }
160 default:
Aart Bik0d345cf2016-03-16 10:49:38 -0700161 return v;
162 }
163}
164
Aart Bik40fbf742016-10-31 11:02:50 -0700165/** Inserts an instruction. */
Aart Bik389b3db2015-10-28 14:23:40 -0700166static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
167 DCHECK(block != nullptr);
168 DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId();
Aart Bikaec3cce2015-10-14 17:44:55 -0700169 DCHECK(instruction != nullptr);
Aart Bik389b3db2015-10-28 14:23:40 -0700170 block->InsertInstructionBefore(instruction, block->GetLastInstruction());
Aart Bikaec3cce2015-10-14 17:44:55 -0700171 return instruction;
172}
173
Aart Bik40fbf742016-10-31 11:02:50 -0700174/** Obtains loop's control instruction. */
Aart Bik009cace2016-09-16 10:15:19 -0700175static HInstruction* GetLoopControl(HLoopInformation* loop) {
176 DCHECK(loop != nullptr);
177 return loop->GetHeader()->GetLastInstruction();
178}
179
Aart Bikd14c5952015-09-08 15:25:15 -0700180//
181// Public class methods.
182//
183
184InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis)
Aart Bik52be7e72016-06-23 11:20:41 -0700185 : induction_analysis_(induction_analysis),
186 chase_hint_(nullptr) {
Aart Bikb3365e02015-09-21 14:45:05 -0700187 DCHECK(induction_analysis != nullptr);
Aart Bikd14c5952015-09-08 15:25:15 -0700188}
189
Aart Bik1fc3afb2016-02-02 13:26:16 -0800190bool InductionVarRange::GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -0700191 HInstruction* instruction,
Aart Bik52be7e72016-06-23 11:20:41 -0700192 HInstruction* chase_hint,
Aart Bik389b3db2015-10-28 14:23:40 -0700193 /*out*/Value* min_val,
194 /*out*/Value* max_val,
195 /*out*/bool* needs_finite_test) {
Aart Bik52be7e72016-06-23 11:20:41 -0700196 HLoopInformation* loop = nullptr;
197 HInductionVarAnalysis::InductionInfo* info = nullptr;
198 HInductionVarAnalysis::InductionInfo* trip = nullptr;
199 if (!HasInductionInfo(context, instruction, &loop, &info, &trip)) {
200 return false;
Aart Bik97412c92016-02-19 20:14:38 -0800201 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700202 // Type int or lower (this is not too restrictive since intended clients, like
203 // bounds check elimination, will have truncated higher precision induction
204 // at their use point already).
205 switch (info->type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100206 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100207 case DataType::Type::kInt8:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100208 case DataType::Type::kUint16:
209 case DataType::Type::kInt16:
210 case DataType::Type::kInt32:
Aart Bik0d345cf2016-03-16 10:49:38 -0700211 break;
212 default:
213 return false;
214 }
Aart Bik97412c92016-02-19 20:14:38 -0800215 // Find range.
Aart Bik52be7e72016-06-23 11:20:41 -0700216 chase_hint_ = chase_hint;
217 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700218 int64_t stride_value = 0;
Aart Bik40fbf742016-10-31 11:02:50 -0700219 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
220 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false), chase_hint);
Aart Bik16d3a652016-09-09 10:33:50 -0700221 *needs_finite_test = NeedsTripCount(info, &stride_value) && IsUnsafeTripCount(trip);
Aart Bik40fbf742016-10-31 11:02:50 -0700222 chase_hint_ = nullptr;
223 // Retry chasing constants for wrap-around (merge sensitive).
224 if (!min_val->is_known && info->induction_class == HInductionVarAnalysis::kWrapAround) {
225 *min_val = SimplifyMin(GetVal(info, trip, in_body, /* is_min */ true));
226 }
Aart Bik97412c92016-02-19 20:14:38 -0800227 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700228}
229
Aart Bik16d3a652016-09-09 10:33:50 -0700230bool InductionVarRange::CanGenerateRange(HInstruction* context,
231 HInstruction* instruction,
232 /*out*/bool* needs_finite_test,
233 /*out*/bool* needs_taken_test) {
234 bool is_last_value = false;
235 int64_t stride_value = 0;
Aart Bik9abf8942016-10-14 09:49:42 -0700236 return GenerateRangeOrLastValue(context,
237 instruction,
238 is_last_value,
239 nullptr,
240 nullptr,
241 nullptr,
242 nullptr,
243 nullptr, // nothing generated yet
244 &stride_value,
245 needs_finite_test,
246 needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700247 && (stride_value == -1 ||
248 stride_value == 0 ||
Aart Bik40fbf742016-10-31 11:02:50 -0700249 stride_value == 1); // avoid arithmetic wrap-around anomalies.
Aart Bikaec3cce2015-10-14 17:44:55 -0700250}
251
Aart Bik16d3a652016-09-09 10:33:50 -0700252void InductionVarRange::GenerateRange(HInstruction* context,
253 HInstruction* instruction,
254 HGraph* graph,
255 HBasicBlock* block,
256 /*out*/HInstruction** lower,
257 /*out*/HInstruction** upper) {
258 bool is_last_value = false;
Aart Bik009cace2016-09-16 10:15:19 -0700259 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700260 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700261 if (!GenerateRangeOrLastValue(context,
262 instruction,
263 is_last_value,
264 graph,
265 block,
266 lower,
267 upper,
268 nullptr,
269 &stride_value,
270 &b1,
271 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700272 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
Aart Bik389b3db2015-10-28 14:23:40 -0700273 }
274}
275
Aart Bik16d3a652016-09-09 10:33:50 -0700276HInstruction* InductionVarRange::GenerateTakenTest(HInstruction* context,
277 HGraph* graph,
278 HBasicBlock* block) {
279 HInstruction* taken_test = nullptr;
280 bool is_last_value = false;
281 int64_t stride_value = 0;
Aart Bik389b3db2015-10-28 14:23:40 -0700282 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700283 if (!GenerateRangeOrLastValue(context,
284 context,
285 is_last_value,
286 graph,
287 block,
288 nullptr,
289 nullptr,
290 &taken_test,
291 &stride_value,
292 &b1,
293 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700294 LOG(FATAL) << "Failed precondition: CanGenerateRange()";
295 }
296 return taken_test;
297}
298
299bool InductionVarRange::CanGenerateLastValue(HInstruction* instruction) {
300 bool is_last_value = true;
301 int64_t stride_value = 0;
302 bool needs_finite_test = false;
303 bool needs_taken_test = false;
Aart Bik9abf8942016-10-14 09:49:42 -0700304 return GenerateRangeOrLastValue(instruction,
305 instruction,
306 is_last_value,
307 nullptr,
308 nullptr,
309 nullptr,
310 nullptr,
311 nullptr, // nothing generated yet
312 &stride_value,
313 &needs_finite_test,
314 &needs_taken_test)
Aart Bik16d3a652016-09-09 10:33:50 -0700315 && !needs_finite_test && !needs_taken_test;
316}
317
318HInstruction* InductionVarRange::GenerateLastValue(HInstruction* instruction,
319 HGraph* graph,
320 HBasicBlock* block) {
321 HInstruction* last_value = nullptr;
322 bool is_last_value = true;
323 int64_t stride_value = 0;
324 bool b1, b2; // unused
Aart Bik9abf8942016-10-14 09:49:42 -0700325 if (!GenerateRangeOrLastValue(instruction,
326 instruction,
327 is_last_value,
328 graph,
329 block,
330 &last_value,
331 &last_value,
332 nullptr,
333 &stride_value,
334 &b1,
335 &b2)) {
Aart Bik16d3a652016-09-09 10:33:50 -0700336 LOG(FATAL) << "Failed precondition: CanGenerateLastValue()";
337 }
338 return last_value;
339}
340
341void InductionVarRange::Replace(HInstruction* instruction,
342 HInstruction* fetch,
343 HInstruction* replacement) {
344 for (HLoopInformation* lp = instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
345 lp != nullptr;
346 lp = lp->GetPreHeader()->GetLoopInformation()) {
Aart Bik009cace2016-09-16 10:15:19 -0700347 // Update instruction's information.
Aart Bik16d3a652016-09-09 10:33:50 -0700348 ReplaceInduction(induction_analysis_->LookupInfo(lp, instruction), fetch, replacement);
Aart Bik009cace2016-09-16 10:15:19 -0700349 // Update loop's trip-count information.
350 ReplaceInduction(induction_analysis_->LookupInfo(lp, GetLoopControl(lp)), fetch, replacement);
Aart Bik389b3db2015-10-28 14:23:40 -0700351 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700352}
353
Aart Bik1f8d51b2018-02-15 10:42:37 -0800354bool InductionVarRange::IsFinite(HLoopInformation* loop, /*out*/ int64_t* trip_count) const {
Artem Serov121f2032017-10-23 19:19:06 +0100355 bool is_constant_unused = false;
356 return CheckForFiniteAndConstantProps(loop, &is_constant_unused, trip_count);
357}
358
359bool InductionVarRange::HasKnownTripCount(HLoopInformation* loop,
360 /*out*/ int64_t* trip_count) const {
361 bool is_constant = false;
362 CheckForFiniteAndConstantProps(loop, &is_constant, trip_count);
363 return is_constant;
Aart Bik9abf8942016-10-14 09:49:42 -0700364}
365
Aart Bikfa762962017-04-07 11:33:37 -0700366bool InductionVarRange::IsUnitStride(HInstruction* context,
367 HInstruction* instruction,
Aart Bik37dc4df2017-06-28 14:08:00 -0700368 HGraph* graph,
Aart Bik8e02e3e2017-02-28 14:41:55 -0800369 /*out*/ HInstruction** offset) const {
370 HLoopInformation* loop = nullptr;
371 HInductionVarAnalysis::InductionInfo* info = nullptr;
372 HInductionVarAnalysis::InductionInfo* trip = nullptr;
Aart Bikfa762962017-04-07 11:33:37 -0700373 if (HasInductionInfo(context, instruction, &loop, &info, &trip)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800374 if (info->induction_class == HInductionVarAnalysis::kLinear &&
Aart Bik7adb6882017-03-07 13:28:51 -0800375 !HInductionVarAnalysis::IsNarrowingLinear(info)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800376 int64_t stride_value = 0;
377 if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) {
378 int64_t off_value = 0;
Aart Bik37dc4df2017-06-28 14:08:00 -0700379 if (IsConstant(info->op_b, kExact, &off_value)) {
380 *offset = graph->GetConstant(info->op_b->type, off_value);
381 } else if (info->op_b->operation == HInductionVarAnalysis::kFetch) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800382 *offset = info->op_b->fetch;
Aart Bik37dc4df2017-06-28 14:08:00 -0700383 } else {
384 return false;
Aart Bik8e02e3e2017-02-28 14:41:55 -0800385 }
386 return true;
387 }
388 }
389 }
390 return false;
391}
392
393HInstruction* InductionVarRange::GenerateTripCount(HLoopInformation* loop,
394 HGraph* graph,
395 HBasicBlock* block) {
396 HInductionVarAnalysis::InductionInfo *trip =
397 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
398 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
399 HInstruction* taken_test = nullptr;
400 HInstruction* trip_expr = nullptr;
401 if (IsBodyTripCount(trip)) {
402 if (!GenerateCode(trip->op_b, nullptr, graph, block, &taken_test, false, false)) {
403 return nullptr;
404 }
405 }
406 if (GenerateCode(trip->op_a, nullptr, graph, block, &trip_expr, false, false)) {
407 if (taken_test != nullptr) {
408 HInstruction* zero = graph->GetConstant(trip->type, 0);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100409 ArenaAllocator* allocator = graph->GetAllocator();
410 trip_expr = Insert(block, new (allocator) HSelect(taken_test, trip_expr, zero, kNoDexPc));
Aart Bik8e02e3e2017-02-28 14:41:55 -0800411 }
412 return trip_expr;
413 }
414 }
415 return nullptr;
416}
417
Aart Bikd14c5952015-09-08 15:25:15 -0700418//
419// Private class methods.
420//
421
Artem Serov121f2032017-10-23 19:19:06 +0100422bool InductionVarRange::CheckForFiniteAndConstantProps(HLoopInformation* loop,
423 /*out*/ bool* is_constant,
424 /*out*/ int64_t* trip_count) const {
425 HInductionVarAnalysis::InductionInfo *trip =
426 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
427 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
428 *is_constant = IsConstant(trip->op_a, kExact, trip_count);
429 return true;
430 }
431 return false;
432}
433
Aart Bik97412c92016-02-19 20:14:38 -0800434bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
435 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700436 /*out*/ int64_t* value) const {
Aart Bik97412c92016-02-19 20:14:38 -0800437 if (info != nullptr) {
438 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
439 // any of the three requests (kExact, kAtMost, and KAtLeast).
440 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
441 info->operation == HInductionVarAnalysis::kFetch) {
Aart Bikf3e61ee2017-04-12 17:09:20 -0700442 if (IsInt64AndGet(info->fetch, value)) {
Aart Bik97412c92016-02-19 20:14:38 -0800443 return true;
444 }
445 }
Aart Bik40fbf742016-10-31 11:02:50 -0700446 // Try range analysis on the invariant, only accept a proper range
447 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700448 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
449 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
450 if (IsConstantValue(min_val) &&
451 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
452 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
453 *value = max_val.b_constant;
454 return true;
455 } else if (request == kAtLeast) {
456 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800457 return true;
458 }
459 }
Aart Bik97412c92016-02-19 20:14:38 -0800460 }
461 return false;
462}
463
Aart Bik52be7e72016-06-23 11:20:41 -0700464bool InductionVarRange::HasInductionInfo(
465 HInstruction* context,
466 HInstruction* instruction,
467 /*out*/ HLoopInformation** loop,
468 /*out*/ HInductionVarAnalysis::InductionInfo** info,
469 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800470 DCHECK(context != nullptr);
471 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700472 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
473 if (lp != nullptr) {
474 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700475 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700476 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700477 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700478 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700479 return true;
480 }
481 }
482 return false;
483}
484
485bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
486 if (trip != nullptr) {
487 // Both bounds that define a trip-count are well-behaved if they either are not defined
488 // in any loop, or are contained in a proper interval. This allows finding the min/max
489 // of an expression by chasing outward.
490 InductionVarRange range(induction_analysis_);
491 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
492 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
493 int64_t not_used = 0;
494 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
495 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
496 }
497 return true;
498}
499
500bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
501 if (info != nullptr) {
502 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
503 info->operation == HInductionVarAnalysis::kFetch) {
504 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
505 }
506 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
507 }
508 return false;
509}
510
Aart Bik16d3a652016-09-09 10:33:50 -0700511bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
512 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700513 if (info != nullptr) {
514 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700515 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800516 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
517 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700518 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700519 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700520 }
Aart Bikd14c5952015-09-08 15:25:15 -0700521 }
Aart Bik389b3db2015-10-28 14:23:40 -0700522 return false;
523}
524
Aart Bik7d57d7f2015-12-09 14:39:48 -0800525bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700526 if (trip != nullptr) {
527 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
528 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
529 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
530 }
531 }
532 return false;
533}
534
Aart Bik7d57d7f2015-12-09 14:39:48 -0800535bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700536 if (trip != nullptr) {
537 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
538 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
539 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
540 }
541 }
542 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700543}
544
Aart Bik7d57d7f2015-12-09 14:39:48 -0800545InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
546 HInductionVarAnalysis::InductionInfo* trip,
547 bool in_body,
548 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800549 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800550 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700551 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800552 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
553 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
554 // with intermediate results that only incorporate single instructions.
555 if (trip != nullptr) {
556 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700557 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c92016-02-19 20:14:38 -0800558 int64_t stride_value = 0;
559 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800560 if (!is_min && stride_value == 1) {
Aart Bik97412c92016-02-19 20:14:38 -0800561 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800562 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
563 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
564 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700565 trip->induction_class,
566 trip->operation,
567 trip_expr->op_a,
568 trip->op_b,
569 nullptr,
570 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800571 return GetVal(&cancelled_trip, trip, in_body, is_min);
572 }
573 } else if (is_min && stride_value == -1) {
Aart Bik97412c92016-02-19 20:14:38 -0800574 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800575 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
576 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
577 HInductionVarAnalysis::InductionInfo neg(
578 HInductionVarAnalysis::kInvariant,
579 HInductionVarAnalysis::kNeg,
580 nullptr,
581 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700582 nullptr,
583 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800584 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700585 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800586 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
587 }
588 }
589 }
590 }
591 }
592 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
593 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
594 GetVal(info->op_b, trip, in_body, is_min));
595}
596
Aart Bikdf7822e2016-12-06 10:05:30 -0800597InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
598 HInductionVarAnalysis::InductionInfo* trip,
599 bool in_body,
600 bool is_min) const {
601 DCHECK(info != nullptr);
602 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
603 int64_t a = 0;
604 int64_t b = 0;
605 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
606 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800607 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800608 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
609 Value c = GetVal(info->op_b, trip, in_body, is_min);
610 if (is_min) {
611 return c;
612 } else {
613 Value m = GetVal(trip, trip, in_body, is_min);
614 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
615 Value x = MulValue(Value(a), t);
616 Value y = MulValue(Value(b), m);
617 return AddValue(AddValue(x, y), c);
618 }
619 }
620 return Value();
621}
622
Aart Bikc071a012016-12-01 10:22:31 -0800623InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
624 HInductionVarAnalysis::InductionInfo* trip,
625 bool in_body,
626 bool is_min) const {
627 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800628 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800629 int64_t a = 0;
630 int64_t f = 0;
631 if (IsConstant(info->op_a, kExact, &a) &&
632 CanLongValueFitIntoInt(a) &&
Aart Bikf3e61ee2017-04-12 17:09:20 -0700633 IsInt64AndGet(info->fetch, &f) && f >= 1) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800634 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
635 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800636 const bool is_min_a = a >= 0 ? is_min : !is_min;
637 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800638 Value b = GetVal(info->op_b, trip, in_body, is_min);
639 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800640 }
641 }
642 return Value();
643}
644
Aart Bikd14c5952015-09-08 15:25:15 -0700645InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700646 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700647 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800648 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700649 // Special case when chasing constants: single instruction that denotes trip count in the
650 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
651 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700652 if (is_min) {
653 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800654 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700655 return Value(std::numeric_limits<int32_t>::max());
656 }
657 }
Aart Bik40fbf742016-10-31 11:02:50 -0700658 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
659 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
660 int64_t value;
Aart Bikf3e61ee2017-04-12 17:09:20 -0700661 if (IsInt64AndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
Aart Bik40fbf742016-10-31 11:02:50 -0700662 // Proper constant reveals best information.
663 return Value(static_cast<int32_t>(value));
664 } else if (instruction == chase_hint_) {
665 // At hint, fetch is represented by itself.
666 return Value(instruction, 1, 0);
667 } else if (instruction->IsAdd()) {
668 // Incorporate suitable constants in the chased value.
Aart Bikf3e61ee2017-04-12 17:09:20 -0700669 if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
Aart Bik97412c92016-02-19 20:14:38 -0800670 return AddValue(Value(static_cast<int32_t>(value)),
671 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
Aart Bikf3e61ee2017-04-12 17:09:20 -0700672 } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
Aart Bik97412c92016-02-19 20:14:38 -0800673 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
674 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700675 }
Aart Bik8e9090b2017-09-08 16:46:50 -0700676 } else if (instruction->IsSub()) {
677 // Incorporate suitable constants in the chased value.
678 if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
679 return SubValue(Value(static_cast<int32_t>(value)),
680 GetFetch(instruction->InputAt(1), trip, in_body, !is_min));
681 } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
682 return SubValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
683 Value(static_cast<int32_t>(value)));
684 }
Aart Bik52be7e72016-06-23 11:20:41 -0700685 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700686 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700687 if (chase_hint_ == nullptr) {
688 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
689 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000690 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700691 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700692 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700693 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800694 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100695 if (instruction->AsTypeConversion()->GetInputType() == DataType::Type::kInt32 &&
696 instruction->AsTypeConversion()->GetResultType() == DataType::Type::kInt64) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700697 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
698 }
Aart Bik52be7e72016-06-23 11:20:41 -0700699 }
700 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
701 // so far is well-behaved in both bounds and the next trip-count is safe.
702 // Example:
703 // for (int i = 0; i <= 100; i++) // safe
704 // for (int j = 0; j <= i; j++) // well-behaved
705 // j is in range [0, i ] (if i is chase hint)
706 // or in range [0, 100] (otherwise)
707 HLoopInformation* next_loop = nullptr;
708 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
709 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
710 bool next_in_body = true; // inner loop is always in body of outer loop
711 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
712 IsWellBehavedTripCount(trip) &&
713 !IsUnsafeTripCount(next_trip)) {
714 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700715 }
Aart Bik40fbf742016-10-31 11:02:50 -0700716 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700717 return Value(instruction, 1, 0);
718}
719
Aart Bikcd26feb2015-09-23 17:50:50 -0700720InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
721 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700722 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800723 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700724 if (info != nullptr) {
725 switch (info->induction_class) {
726 case HInductionVarAnalysis::kInvariant:
727 // Invariants.
728 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700729 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700730 return AddValue(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::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700733 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
734 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700735 case HInductionVarAnalysis::kNeg: // second reversed!
736 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700737 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700738 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700739 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700740 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700741 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800742 case HInductionVarAnalysis::kRem:
743 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700744 case HInductionVarAnalysis::kXor:
745 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700746 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700747 return GetFetch(info->fetch, trip, in_body, is_min);
748 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700749 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700750 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700751 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700752 }
753 FALLTHROUGH_INTENDED;
754 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700755 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700756 if (is_min) {
757 return Value(0);
758 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700759 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700760 }
761 break;
762 default:
763 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700764 }
765 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700766 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700767 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800768 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800769 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800770 case HInductionVarAnalysis::kGeometric:
771 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700772 case HInductionVarAnalysis::kWrapAround:
773 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700774 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
775 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700776 }
777 }
Aart Bikb3365e02015-09-21 14:45:05 -0700778 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700779}
780
781InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
782 HInductionVarAnalysis::InductionInfo* info2,
783 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700784 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800785 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700786 // Constant times range.
787 int64_t value = 0;
788 if (IsConstant(info1, kExact, &value)) {
789 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
790 } else if (IsConstant(info2, kExact, &value)) {
791 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
792 }
793 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700794 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
795 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
796 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
797 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c92016-02-19 20:14:38 -0800798 // Positive range vs. positive or negative range.
799 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
800 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
801 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
802 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
803 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700804 }
Aart Bik97412c92016-02-19 20:14:38 -0800805 }
806 // Negative range vs. positive or negative range.
807 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
808 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
809 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
810 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
811 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700812 }
813 }
Aart Bikb3365e02015-09-21 14:45:05 -0700814 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700815}
816
817InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
818 HInductionVarAnalysis::InductionInfo* info2,
819 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700820 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800821 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700822 // Range divided by constant.
823 int64_t value = 0;
824 if (IsConstant(info2, kExact, &value)) {
825 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
826 }
827 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700828 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
829 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
830 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
831 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c92016-02-19 20:14:38 -0800832 // Positive range vs. positive or negative range.
833 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
834 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
835 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
836 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
837 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700838 }
Aart Bik97412c92016-02-19 20:14:38 -0800839 }
840 // Negative range vs. positive or negative range.
841 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
842 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
843 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
844 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
845 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700846 }
847 }
Aart Bikb3365e02015-09-21 14:45:05 -0700848 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700849}
850
Aart Bikdf7822e2016-12-06 10:05:30 -0800851InductionVarRange::Value InductionVarRange::GetRem(
852 HInductionVarAnalysis::InductionInfo* info1,
853 HInductionVarAnalysis::InductionInfo* info2) const {
854 int64_t v1 = 0;
855 int64_t v2 = 0;
856 // Only accept exact values.
857 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
858 int64_t value = v1 % v2;
859 if (CanLongValueFitIntoInt(value)) {
860 return Value(static_cast<int32_t>(value));
861 }
862 }
863 return Value();
864}
865
Aart Bik7dc96932016-10-12 10:01:05 -0700866InductionVarRange::Value InductionVarRange::GetXor(
867 HInductionVarAnalysis::InductionInfo* info1,
868 HInductionVarAnalysis::InductionInfo* info2) const {
869 int64_t v1 = 0;
870 int64_t v2 = 0;
871 // Only accept exact values.
872 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
873 int64_t value = v1 ^ v2;
874 if (CanLongValueFitIntoInt(value)) {
875 return Value(static_cast<int32_t>(value));
876 }
877 }
878 return Value();
879}
880
Aart Bik52be7e72016-06-23 11:20:41 -0700881InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
882 int64_t value,
883 HInductionVarAnalysis::InductionInfo* info,
884 HInductionVarAnalysis::InductionInfo* trip,
885 bool in_body,
886 bool is_min) const {
887 if (CanLongValueFitIntoInt(value)) {
888 Value c(static_cast<int32_t>(value));
889 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
890 }
891 return Value();
Aart Bik97412c92016-02-19 20:14:38 -0800892}
893
Aart Bik52be7e72016-06-23 11:20:41 -0700894InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
895 int64_t value,
896 HInductionVarAnalysis::InductionInfo* info,
897 HInductionVarAnalysis::InductionInfo* trip,
898 bool in_body,
899 bool is_min) const {
900 if (CanLongValueFitIntoInt(value)) {
901 Value c(static_cast<int32_t>(value));
902 return DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
903 }
904 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700905}
906
Aart Bik7d57d7f2015-12-09 14:39:48 -0800907InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700908 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800909 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700910 if (v1.a_constant == 0) {
911 return Value(v2.instruction, v2.a_constant, b);
912 } else if (v2.a_constant == 0) {
913 return Value(v1.instruction, v1.a_constant, b);
914 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
915 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
916 }
917 }
Aart Bikb3365e02015-09-21 14:45:05 -0700918 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700919}
920
Aart Bik7d57d7f2015-12-09 14:39:48 -0800921InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700922 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800923 int32_t b = v1.b_constant - v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700924 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
925 return Value(v2.instruction, -v2.a_constant, b);
926 } else if (v2.a_constant == 0) {
927 return Value(v1.instruction, v1.a_constant, b);
928 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
929 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
930 }
931 }
Aart Bikb3365e02015-09-21 14:45:05 -0700932 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700933}
934
Aart Bik7d57d7f2015-12-09 14:39:48 -0800935InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700936 if (v1.is_known && v2.is_known) {
937 if (v1.a_constant == 0) {
938 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
939 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
940 }
941 } else if (v2.a_constant == 0) {
942 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
943 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
944 }
Aart Bikd14c5952015-09-08 15:25:15 -0700945 }
946 }
Aart Bikb3365e02015-09-21 14:45:05 -0700947 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700948}
949
Aart Bik7d57d7f2015-12-09 14:39:48 -0800950InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700951 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700952 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
953 return Value(v1.b_constant / v2.b_constant);
954 }
955 }
Aart Bikb3365e02015-09-21 14:45:05 -0700956 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700957}
958
Aart Bik7d57d7f2015-12-09 14:39:48 -0800959InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700960 if (v1.is_known && v2.is_known) {
961 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700962 return Value(v1.instruction, v1.a_constant,
963 is_min ? std::min(v1.b_constant, v2.b_constant)
964 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700965 }
Aart Bikd14c5952015-09-08 15:25:15 -0700966 }
Aart Bikb3365e02015-09-21 14:45:05 -0700967 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700968}
969
Aart Bik9abf8942016-10-14 09:49:42 -0700970bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
971 HInstruction* instruction,
972 bool is_last_value,
973 HGraph* graph,
974 HBasicBlock* block,
975 /*out*/HInstruction** lower,
976 /*out*/HInstruction** upper,
977 /*out*/HInstruction** taken_test,
978 /*out*/int64_t* stride_value,
979 /*out*/bool* needs_finite_test,
980 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700981 HLoopInformation* loop = nullptr;
982 HInductionVarAnalysis::InductionInfo* info = nullptr;
983 HInductionVarAnalysis::InductionInfo* trip = nullptr;
984 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
985 return false; // codegen needs all information, including tripcount
Aart Bik97412c92016-02-19 20:14:38 -0800986 }
987 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
988 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
989 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
990 // code does not use the trip-count explicitly (since there could be an implicit relation
991 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700992 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700993 *stride_value = 0;
994 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c92016-02-19 20:14:38 -0800995 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700996 // Handle last value request.
997 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800998 DCHECK(!in_body);
999 switch (info->induction_class) {
1000 case HInductionVarAnalysis::kLinear:
1001 if (*stride_value > 0) {
1002 lower = nullptr;
1003 } else {
1004 upper = nullptr;
1005 }
1006 break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001007 case HInductionVarAnalysis::kPolynomial:
1008 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001009 case HInductionVarAnalysis::kGeometric:
1010 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -08001011 case HInductionVarAnalysis::kWrapAround:
1012 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -08001013 case HInductionVarAnalysis::kPeriodic:
1014 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1015 default:
1016 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001017 }
1018 }
Aart Bik97412c92016-02-19 20:14:38 -08001019 // Code generation for taken test: generate the code when requested or otherwise analyze
1020 // if code generation is feasible when taken test is needed.
1021 if (taken_test != nullptr) {
1022 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1023 } else if (*needs_taken_test) {
1024 if (!GenerateCode(
1025 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1026 return false;
1027 }
1028 }
1029 // Code generation for lower and upper.
1030 return
1031 // Success on lower if invariant (not set), or code can be generated.
1032 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1033 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1034 // And success on upper.
1035 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001036}
1037
Aart Bikdf7822e2016-12-06 10:05:30 -08001038bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1039 HInductionVarAnalysis::InductionInfo* trip,
1040 HGraph* graph,
1041 HBasicBlock* block,
1042 /*out*/HInstruction** result) const {
1043 DCHECK(info != nullptr);
1044 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1045 // Detect known coefficients and trip count (always taken).
1046 int64_t a = 0;
1047 int64_t b = 0;
1048 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001049 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1050 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001051 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001052 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001053 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001054 HInstruction* c = nullptr;
1055 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001056 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001057 DataType::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001058 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001059 if (type != DataType::Type::kInt64) {
Aart Bike6bd0272016-12-16 13:57:52 -08001060 sum = static_cast<int32_t>(sum); // okay to truncate
1061 }
1062 *result =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001063 Insert(block, new (graph->GetAllocator()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001064 }
1065 return true;
1066 }
1067 }
1068 return false;
1069}
1070
Aart Bikc071a012016-12-01 10:22:31 -08001071bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1072 HInductionVarAnalysis::InductionInfo* trip,
1073 HGraph* graph,
1074 HBasicBlock* block,
1075 /*out*/HInstruction** result) const {
1076 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001077 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001078 // Detect known base and trip count (always taken).
1079 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001080 int64_t m = 0;
Aart Bikf3e61ee2017-04-12 17:09:20 -07001081 if (IsInt64AndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001082 HInstruction* opa = nullptr;
1083 HInstruction* opb = nullptr;
1084 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1085 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001086 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001087 DataType::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001088 // Compute f ^ m for known maximum index value m.
1089 bool overflow = false;
1090 int64_t fpow = IntPow(f, m, &overflow);
1091 if (info->operation == HInductionVarAnalysis::kDiv) {
1092 // For division, any overflow truncates to zero.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001093 if (overflow || (type != DataType::Type::kInt64 && !CanLongValueFitIntoInt(fpow))) {
Aart Bikd3ba6262017-01-30 14:37:12 -08001094 fpow = 0;
1095 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001096 } else if (type != DataType::Type::kInt64) {
Aart Bikd3ba6262017-01-30 14:37:12 -08001097 // For multiplication, okay to truncate to required precision.
1098 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1099 fpow = static_cast<int32_t>(fpow);
1100 }
1101 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001102 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001103 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001104 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001105 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001106 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001107 HInstruction* e = nullptr;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001108 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001109 if (info->operation == HInductionVarAnalysis::kMul) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001110 e = new (allocator) HMul(type, opa, graph->GetConstant(type, fpow));
Aart Bike6bd0272016-12-16 13:57:52 -08001111 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001112 e = new (allocator) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
Aart Bike6bd0272016-12-16 13:57:52 -08001113 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001114 *result = Insert(block, new (allocator) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001115 }
1116 }
1117 return true;
1118 }
1119 }
1120 return false;
1121}
1122
Aart Bikdf7822e2016-12-06 10:05:30 -08001123bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1124 HInductionVarAnalysis::InductionInfo* trip,
1125 HGraph* graph,
1126 HBasicBlock* block,
1127 /*out*/HInstruction** result) const {
1128 DCHECK(info != nullptr);
1129 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1130 // Count depth.
1131 int32_t depth = 0;
1132 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1133 info = info->op_b, ++depth) {}
1134 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001135 // TODO: generalize, but be careful to adjust the terminal.
1136 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001137 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001138 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1139 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001140 }
1141 return false;
1142}
1143
Aart Bik9abf8942016-10-14 09:49:42 -07001144bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1145 HInductionVarAnalysis::InductionInfo* trip,
1146 HGraph* graph,
1147 HBasicBlock* block,
1148 /*out*/HInstruction** result,
1149 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001150 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001151 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik8523ea12017-06-01 15:45:09 -07001152 // Count period and detect all-invariants.
Aart Bike6bd0272016-12-16 13:57:52 -08001153 int64_t period = 1;
Aart Bik8523ea12017-06-01 15:45:09 -07001154 bool all_invariants = true;
1155 HInductionVarAnalysis::InductionInfo* p = info;
1156 for (; p->induction_class == HInductionVarAnalysis::kPeriodic; p = p->op_b, ++period) {
1157 DCHECK_EQ(p->op_a->induction_class, HInductionVarAnalysis::kInvariant);
1158 if (p->op_a->operation != HInductionVarAnalysis::kFetch) {
1159 all_invariants = false;
1160 }
1161 }
1162 DCHECK_EQ(p->induction_class, HInductionVarAnalysis::kInvariant);
1163 if (p->operation != HInductionVarAnalysis::kFetch) {
1164 all_invariants = false;
1165 }
1166 // Don't rely on FP arithmetic to be precise, unless the full period
1167 // consist of pre-computed expressions only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001168 if (info->type == DataType::Type::kFloat32 || info->type == DataType::Type::kFloat64) {
Aart Bik8523ea12017-06-01 15:45:09 -07001169 if (!all_invariants) {
1170 return false;
1171 }
1172 }
Aart Bike6bd0272016-12-16 13:57:52 -08001173 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1174 int64_t m = 0;
1175 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1176 int64_t li = m % period;
1177 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1178 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1179 info = info->op_a;
1180 }
1181 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001182 }
Aart Bike6bd0272016-12-16 13:57:52 -08001183 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1184 // directly to obtain the maximum index value t even if taken test is needed.
1185 HInstruction* x = nullptr;
1186 HInstruction* y = nullptr;
1187 HInstruction* t = nullptr;
1188 if (period == 2 &&
1189 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1190 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1191 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1192 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001193 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001194 DataType::Type type = trip->type;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001195 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001196 HInstruction* msk =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001197 Insert(block, new (allocator) HAnd(type, t, graph->GetConstant(type, 1)));
Aart Bike6bd0272016-12-16 13:57:52 -08001198 HInstruction* is_even =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001199 Insert(block, new (allocator) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1200 *result = Insert(block, new (graph->GetAllocator()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001201 }
1202 // Guard select with taken test if needed.
1203 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001204 HInstruction* is_taken = nullptr;
1205 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1206 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001207 ArenaAllocator* allocator = graph->GetAllocator();
1208 *result = Insert(block, new (allocator) HSelect(is_taken, *result, x, kNoDexPc));
Aart Bike6bd0272016-12-16 13:57:52 -08001209 }
1210 *needs_taken_test = false; // taken care of
1211 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001212 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001213 }
Aart Bik9abf8942016-10-14 09:49:42 -07001214 }
1215 return true;
1216 }
1217 return false;
1218}
1219
Aart Bikaec3cce2015-10-14 17:44:55 -07001220bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1221 HInductionVarAnalysis::InductionInfo* trip,
1222 HGraph* graph, // when set, code is generated
1223 HBasicBlock* block,
1224 /*out*/HInstruction** result,
1225 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001226 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001227 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001228 // If during codegen, the result is not needed (nullptr), simply return success.
1229 if (graph != nullptr && result == nullptr) {
1230 return true;
1231 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001232 // Handle current operation.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001233 DataType::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001234 HInstruction* opa = nullptr;
1235 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001236 switch (info->induction_class) {
1237 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001238 // Invariants (note that since invariants only have other invariants as
1239 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001240 switch (info->operation) {
1241 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001242 case HInductionVarAnalysis::kSub:
1243 case HInductionVarAnalysis::kMul:
1244 case HInductionVarAnalysis::kDiv:
1245 case HInductionVarAnalysis::kRem:
1246 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001247 case HInductionVarAnalysis::kLT:
1248 case HInductionVarAnalysis::kLE:
1249 case HInductionVarAnalysis::kGT:
1250 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001251 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1252 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1253 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001254 HInstruction* operation = nullptr;
1255 switch (info->operation) {
1256 case HInductionVarAnalysis::kAdd:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001257 operation = new (graph->GetAllocator()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001258 case HInductionVarAnalysis::kSub:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001259 operation = new (graph->GetAllocator()) HSub(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001260 case HInductionVarAnalysis::kMul:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001261 operation = new (graph->GetAllocator()) HMul(type, opa, opb, kNoDexPc); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001262 case HInductionVarAnalysis::kDiv:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001263 operation = new (graph->GetAllocator()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001264 case HInductionVarAnalysis::kRem:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001265 operation = new (graph->GetAllocator()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001266 case HInductionVarAnalysis::kXor:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001267 operation = new (graph->GetAllocator()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001268 case HInductionVarAnalysis::kLT:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001269 operation = new (graph->GetAllocator()) HLessThan(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001270 case HInductionVarAnalysis::kLE:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001271 operation = new (graph->GetAllocator()) HLessThanOrEqual(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001272 case HInductionVarAnalysis::kGT:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001273 operation = new (graph->GetAllocator()) HGreaterThan(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001274 case HInductionVarAnalysis::kGE:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001275 operation = new (graph->GetAllocator()) HGreaterThanOrEqual(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001276 default:
1277 LOG(FATAL) << "unknown operation";
1278 }
1279 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001280 }
1281 return true;
1282 }
1283 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001284 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001285 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1286 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001287 *result = Insert(block, new (graph->GetAllocator()) HNeg(type, opb));
Aart Bikaec3cce2015-10-14 17:44:55 -07001288 }
1289 return true;
1290 }
1291 break;
1292 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001293 if (graph != nullptr) {
1294 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001295 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001296 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001297 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001298 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001299 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001300 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001301 }
1302 FALLTHROUGH_INTENDED;
1303 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001304 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001305 if (is_min) {
1306 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001307 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001308 }
1309 return true;
1310 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001311 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001312 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001313 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001314 *result =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001315 Insert(block, new (allocator) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001316 }
1317 return true;
1318 }
1319 }
1320 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001321 case HInductionVarAnalysis::kNop:
1322 LOG(FATAL) << "unexpected invariant nop";
1323 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001324 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001325 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001326 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1327 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1328 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001329 // known stride yields right value. Always avoid any narrowing linear induction or
1330 // any type mismatch between the linear induction and the trip count expression.
1331 // TODO: careful runtime type conversions could generalize this latter restriction.
1332 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1333 int64_t stride_value = 0;
1334 if (IsConstant(info->op_a, kExact, &stride_value) &&
1335 CanLongValueFitIntoInt(stride_value)) {
1336 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1337 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1338 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1339 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001340 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001341 HInstruction* oper;
1342 if (stride_value == 1) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001343 oper = new (allocator) HAdd(type, opa, opb);
Aart Bike6bd0272016-12-16 13:57:52 -08001344 } else if (stride_value == -1) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001345 oper = new (graph->GetAllocator()) HSub(type, opb, opa);
Aart Bike6bd0272016-12-16 13:57:52 -08001346 } else {
1347 HInstruction* mul =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001348 new (allocator) HMul(type, graph->GetConstant(type, stride_value), opa);
1349 oper = new (allocator) HAdd(type, Insert(block, mul), opb);
Aart Bike6bd0272016-12-16 13:57:52 -08001350 }
1351 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001352 }
Aart Bike6bd0272016-12-16 13:57:52 -08001353 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001354 }
1355 }
1356 }
1357 break;
Aart Bik4a342772015-11-30 10:17:46 -08001358 }
Aart Bikc071a012016-12-01 10:22:31 -08001359 case HInductionVarAnalysis::kPolynomial:
1360 case HInductionVarAnalysis::kGeometric:
1361 break;
Aart Bik4a342772015-11-30 10:17:46 -08001362 case HInductionVarAnalysis::kWrapAround:
1363 case HInductionVarAnalysis::kPeriodic: {
1364 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1365 // values are easy to test at runtime without complications of arithmetic wrap-around.
1366 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c92016-02-19 20:14:38 -08001367 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001368 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001369 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001370 }
1371 return true;
1372 }
1373 break;
1374 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001375 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001376 }
1377 return false;
1378}
1379
Aart Bik16d3a652016-09-09 10:33:50 -07001380void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1381 HInstruction* fetch,
1382 HInstruction* replacement) {
1383 if (info != nullptr) {
1384 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1385 info->operation == HInductionVarAnalysis::kFetch &&
1386 info->fetch == fetch) {
1387 info->fetch = replacement;
1388 }
1389 ReplaceInduction(info->op_a, fetch, replacement);
1390 ReplaceInduction(info->op_b, fetch, replacement);
1391 }
1392}
1393
Aart Bikd14c5952015-09-08 15:25:15 -07001394} // namespace art