blob: 0a310ca940e294e18027cc2ed40aaf05df6e9441 [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 Bik97412c922016-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 Bik97412c922016-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 Bik97412c922016-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 {
Aart Bik9abf8942016-10-14 09:49:42 -0700355 HInductionVarAnalysis::InductionInfo *trip =
356 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
Aart Bik6b69e0a2017-01-11 10:20:43 -0800357 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
Aart Bik1f8d51b2018-02-15 10:42:37 -0800358 IsConstant(trip->op_a, kExact, trip_count);
Aart Bik6b69e0a2017-01-11 10:20:43 -0800359 return true;
360 }
361 return false;
Aart Bik9abf8942016-10-14 09:49:42 -0700362}
363
Aart Bikfa762962017-04-07 11:33:37 -0700364bool InductionVarRange::IsUnitStride(HInstruction* context,
365 HInstruction* instruction,
Aart Bik37dc4df2017-06-28 14:08:00 -0700366 HGraph* graph,
Aart Bik8e02e3e2017-02-28 14:41:55 -0800367 /*out*/ HInstruction** offset) const {
368 HLoopInformation* loop = nullptr;
369 HInductionVarAnalysis::InductionInfo* info = nullptr;
370 HInductionVarAnalysis::InductionInfo* trip = nullptr;
Aart Bikfa762962017-04-07 11:33:37 -0700371 if (HasInductionInfo(context, instruction, &loop, &info, &trip)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800372 if (info->induction_class == HInductionVarAnalysis::kLinear &&
Aart Bik7adb6882017-03-07 13:28:51 -0800373 !HInductionVarAnalysis::IsNarrowingLinear(info)) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800374 int64_t stride_value = 0;
375 if (IsConstant(info->op_a, kExact, &stride_value) && stride_value == 1) {
376 int64_t off_value = 0;
Aart Bik37dc4df2017-06-28 14:08:00 -0700377 if (IsConstant(info->op_b, kExact, &off_value)) {
378 *offset = graph->GetConstant(info->op_b->type, off_value);
379 } else if (info->op_b->operation == HInductionVarAnalysis::kFetch) {
Aart Bik8e02e3e2017-02-28 14:41:55 -0800380 *offset = info->op_b->fetch;
Aart Bik37dc4df2017-06-28 14:08:00 -0700381 } else {
382 return false;
Aart Bik8e02e3e2017-02-28 14:41:55 -0800383 }
384 return true;
385 }
386 }
387 }
388 return false;
389}
390
391HInstruction* InductionVarRange::GenerateTripCount(HLoopInformation* loop,
392 HGraph* graph,
393 HBasicBlock* block) {
394 HInductionVarAnalysis::InductionInfo *trip =
395 induction_analysis_->LookupInfo(loop, GetLoopControl(loop));
396 if (trip != nullptr && !IsUnsafeTripCount(trip)) {
397 HInstruction* taken_test = nullptr;
398 HInstruction* trip_expr = nullptr;
399 if (IsBodyTripCount(trip)) {
400 if (!GenerateCode(trip->op_b, nullptr, graph, block, &taken_test, false, false)) {
401 return nullptr;
402 }
403 }
404 if (GenerateCode(trip->op_a, nullptr, graph, block, &trip_expr, false, false)) {
405 if (taken_test != nullptr) {
406 HInstruction* zero = graph->GetConstant(trip->type, 0);
Vladimir Markoca6fff82017-10-03 14:49:14 +0100407 ArenaAllocator* allocator = graph->GetAllocator();
408 trip_expr = Insert(block, new (allocator) HSelect(taken_test, trip_expr, zero, kNoDexPc));
Aart Bik8e02e3e2017-02-28 14:41:55 -0800409 }
410 return trip_expr;
411 }
412 }
413 return nullptr;
414}
415
Aart Bikd14c5952015-09-08 15:25:15 -0700416//
417// Private class methods.
418//
419
Aart Bik97412c922016-02-19 20:14:38 -0800420bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
421 ConstantRequest request,
Aart Bik52be7e72016-06-23 11:20:41 -0700422 /*out*/ int64_t* value) const {
Aart Bik97412c922016-02-19 20:14:38 -0800423 if (info != nullptr) {
424 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
425 // any of the three requests (kExact, kAtMost, and KAtLeast).
426 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
427 info->operation == HInductionVarAnalysis::kFetch) {
Aart Bikf3e61ee2017-04-12 17:09:20 -0700428 if (IsInt64AndGet(info->fetch, value)) {
Aart Bik97412c922016-02-19 20:14:38 -0800429 return true;
430 }
431 }
Aart Bik40fbf742016-10-31 11:02:50 -0700432 // Try range analysis on the invariant, only accept a proper range
433 // to avoid arithmetic wrap-around anomalies.
Aart Bik52be7e72016-06-23 11:20:41 -0700434 Value min_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ true);
435 Value max_val = GetVal(info, nullptr, /* in_body */ true, /* is_min */ false);
436 if (IsConstantValue(min_val) &&
437 IsConstantValue(max_val) && min_val.b_constant <= max_val.b_constant) {
438 if ((request == kExact && min_val.b_constant == max_val.b_constant) || request == kAtMost) {
439 *value = max_val.b_constant;
440 return true;
441 } else if (request == kAtLeast) {
442 *value = min_val.b_constant;
Aart Bik358af832016-02-24 14:17:53 -0800443 return true;
444 }
445 }
Aart Bik97412c922016-02-19 20:14:38 -0800446 }
447 return false;
448}
449
Aart Bik52be7e72016-06-23 11:20:41 -0700450bool InductionVarRange::HasInductionInfo(
451 HInstruction* context,
452 HInstruction* instruction,
453 /*out*/ HLoopInformation** loop,
454 /*out*/ HInductionVarAnalysis::InductionInfo** info,
455 /*out*/ HInductionVarAnalysis::InductionInfo** trip) const {
Aart Bikc071a012016-12-01 10:22:31 -0800456 DCHECK(context != nullptr);
457 DCHECK(context->GetBlock() != nullptr);
Aart Bik009cace2016-09-16 10:15:19 -0700458 HLoopInformation* lp = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
459 if (lp != nullptr) {
460 HInductionVarAnalysis::InductionInfo* i = induction_analysis_->LookupInfo(lp, instruction);
Aart Bik52be7e72016-06-23 11:20:41 -0700461 if (i != nullptr) {
Aart Bik009cace2016-09-16 10:15:19 -0700462 *loop = lp;
Aart Bik52be7e72016-06-23 11:20:41 -0700463 *info = i;
Aart Bik009cace2016-09-16 10:15:19 -0700464 *trip = induction_analysis_->LookupInfo(lp, GetLoopControl(lp));
Aart Bik52be7e72016-06-23 11:20:41 -0700465 return true;
466 }
467 }
468 return false;
469}
470
471bool InductionVarRange::IsWellBehavedTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
472 if (trip != nullptr) {
473 // Both bounds that define a trip-count are well-behaved if they either are not defined
474 // in any loop, or are contained in a proper interval. This allows finding the min/max
475 // of an expression by chasing outward.
476 InductionVarRange range(induction_analysis_);
477 HInductionVarAnalysis::InductionInfo* lower = trip->op_b->op_a;
478 HInductionVarAnalysis::InductionInfo* upper = trip->op_b->op_b;
479 int64_t not_used = 0;
480 return (!HasFetchInLoop(lower) || range.IsConstant(lower, kAtLeast, &not_used)) &&
481 (!HasFetchInLoop(upper) || range.IsConstant(upper, kAtLeast, &not_used));
482 }
483 return true;
484}
485
486bool InductionVarRange::HasFetchInLoop(HInductionVarAnalysis::InductionInfo* info) const {
487 if (info != nullptr) {
488 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
489 info->operation == HInductionVarAnalysis::kFetch) {
490 return info->fetch->GetBlock()->GetLoopInformation() != nullptr;
491 }
492 return HasFetchInLoop(info->op_a) || HasFetchInLoop(info->op_b);
493 }
494 return false;
495}
496
Aart Bik16d3a652016-09-09 10:33:50 -0700497bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info,
498 int64_t* stride_value) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700499 if (info != nullptr) {
500 if (info->induction_class == HInductionVarAnalysis::kLinear) {
Aart Bik16d3a652016-09-09 10:33:50 -0700501 return IsConstant(info->op_a, kExact, stride_value);
Aart Bikdf7822e2016-12-06 10:05:30 -0800502 } else if (info->induction_class == HInductionVarAnalysis::kPolynomial) {
503 return NeedsTripCount(info->op_a, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700504 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
Aart Bik16d3a652016-09-09 10:33:50 -0700505 return NeedsTripCount(info->op_b, stride_value);
Aart Bik389b3db2015-10-28 14:23:40 -0700506 }
Aart Bikd14c5952015-09-08 15:25:15 -0700507 }
Aart Bik389b3db2015-10-28 14:23:40 -0700508 return false;
509}
510
Aart Bik7d57d7f2015-12-09 14:39:48 -0800511bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700512 if (trip != nullptr) {
513 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
514 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
515 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
516 }
517 }
518 return false;
519}
520
Aart Bik7d57d7f2015-12-09 14:39:48 -0800521bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700522 if (trip != nullptr) {
523 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
524 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
525 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
526 }
527 }
528 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700529}
530
Aart Bik7d57d7f2015-12-09 14:39:48 -0800531InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
532 HInductionVarAnalysis::InductionInfo* trip,
533 bool in_body,
534 bool is_min) const {
Aart Bikc071a012016-12-01 10:22:31 -0800535 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800536 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kLinear);
Aart Bik52be7e72016-06-23 11:20:41 -0700537 // Detect common situation where an offset inside the trip-count cancels out during range
Aart Bik7d57d7f2015-12-09 14:39:48 -0800538 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
539 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
540 // with intermediate results that only incorporate single instructions.
541 if (trip != nullptr) {
542 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
Aart Bik52be7e72016-06-23 11:20:41 -0700543 if (trip_expr->type == info->type && trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800544 int64_t stride_value = 0;
545 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800546 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800547 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800548 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
549 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
550 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700551 trip->induction_class,
552 trip->operation,
553 trip_expr->op_a,
554 trip->op_b,
555 nullptr,
556 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800557 return GetVal(&cancelled_trip, trip, in_body, is_min);
558 }
559 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800560 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800561 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
562 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
563 HInductionVarAnalysis::InductionInfo neg(
564 HInductionVarAnalysis::kInvariant,
565 HInductionVarAnalysis::kNeg,
566 nullptr,
567 trip_expr->op_b,
Aart Bik0d345cf2016-03-16 10:49:38 -0700568 nullptr,
569 trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800570 HInductionVarAnalysis::InductionInfo cancelled_trip(
Aart Bik0d345cf2016-03-16 10:49:38 -0700571 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr, trip->type);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800572 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
573 }
574 }
575 }
576 }
577 }
578 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
579 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
580 GetVal(info->op_b, trip, in_body, is_min));
581}
582
Aart Bikdf7822e2016-12-06 10:05:30 -0800583InductionVarRange::Value InductionVarRange::GetPolynomial(HInductionVarAnalysis::InductionInfo* info,
584 HInductionVarAnalysis::InductionInfo* trip,
585 bool in_body,
586 bool is_min) const {
587 DCHECK(info != nullptr);
588 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
589 int64_t a = 0;
590 int64_t b = 0;
591 if (IsConstant(info->op_a->op_a, kExact, &a) && CanLongValueFitIntoInt(a) && a >= 0 &&
592 IsConstant(info->op_a->op_b, kExact, &b) && CanLongValueFitIntoInt(b) && b >= 0) {
Aart Bike6bd0272016-12-16 13:57:52 -0800593 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c with a,b >= 0 for
Aart Bikdf7822e2016-12-06 10:05:30 -0800594 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
595 Value c = GetVal(info->op_b, trip, in_body, is_min);
596 if (is_min) {
597 return c;
598 } else {
599 Value m = GetVal(trip, trip, in_body, is_min);
600 Value t = DivValue(MulValue(m, SubValue(m, Value(1))), Value(2));
601 Value x = MulValue(Value(a), t);
602 Value y = MulValue(Value(b), m);
603 return AddValue(AddValue(x, y), c);
604 }
605 }
606 return Value();
607}
608
Aart Bikc071a012016-12-01 10:22:31 -0800609InductionVarRange::Value InductionVarRange::GetGeometric(HInductionVarAnalysis::InductionInfo* info,
610 HInductionVarAnalysis::InductionInfo* trip,
611 bool in_body,
612 bool is_min) const {
613 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -0800614 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -0800615 int64_t a = 0;
616 int64_t f = 0;
617 if (IsConstant(info->op_a, kExact, &a) &&
618 CanLongValueFitIntoInt(a) &&
Aart Bikf3e61ee2017-04-12 17:09:20 -0700619 IsInt64AndGet(info->fetch, &f) && f >= 1) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800620 // Conservative bounds on a * f^-i + b with f >= 1 can be computed without
621 // trip count. Other forms would require a much more elaborate evaluation.
Aart Bikc071a012016-12-01 10:22:31 -0800622 const bool is_min_a = a >= 0 ? is_min : !is_min;
623 if (info->operation == HInductionVarAnalysis::kDiv) {
Aart Bikdf7822e2016-12-06 10:05:30 -0800624 Value b = GetVal(info->op_b, trip, in_body, is_min);
625 return is_min_a ? b : AddValue(Value(a), b);
Aart Bikc071a012016-12-01 10:22:31 -0800626 }
627 }
628 return Value();
629}
630
Aart Bikd14c5952015-09-08 15:25:15 -0700631InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700632 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700633 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800634 bool is_min) const {
Aart Bik40fbf742016-10-31 11:02:50 -0700635 // Special case when chasing constants: single instruction that denotes trip count in the
636 // loop-body is minimal 1 and maximal, with safe trip-count, max int,
637 if (chase_hint_ == nullptr && in_body && trip != nullptr && instruction == trip->op_a->fetch) {
Aart Bik52be7e72016-06-23 11:20:41 -0700638 if (is_min) {
639 return Value(1);
Aart Bikdf7822e2016-12-06 10:05:30 -0800640 } else if (!instruction->IsConstant() && !IsUnsafeTripCount(trip)) {
Aart Bik52be7e72016-06-23 11:20:41 -0700641 return Value(std::numeric_limits<int32_t>::max());
642 }
643 }
Aart Bik40fbf742016-10-31 11:02:50 -0700644 // Unless at a constant or hint, chase the instruction a bit deeper into the HIR tree, so that
645 // it becomes more likely range analysis will compare the same instructions as terminal nodes.
646 int64_t value;
Aart Bikf3e61ee2017-04-12 17:09:20 -0700647 if (IsInt64AndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
Aart Bik40fbf742016-10-31 11:02:50 -0700648 // Proper constant reveals best information.
649 return Value(static_cast<int32_t>(value));
650 } else if (instruction == chase_hint_) {
651 // At hint, fetch is represented by itself.
652 return Value(instruction, 1, 0);
653 } else if (instruction->IsAdd()) {
654 // Incorporate suitable constants in the chased value.
Aart Bikf3e61ee2017-04-12 17:09:20 -0700655 if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
Aart Bik97412c922016-02-19 20:14:38 -0800656 return AddValue(Value(static_cast<int32_t>(value)),
657 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
Aart Bikf3e61ee2017-04-12 17:09:20 -0700658 } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
Aart Bik97412c922016-02-19 20:14:38 -0800659 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
660 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700661 }
Aart Bik8e9090b2017-09-08 16:46:50 -0700662 } else if (instruction->IsSub()) {
663 // Incorporate suitable constants in the chased value.
664 if (IsInt64AndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
665 return SubValue(Value(static_cast<int32_t>(value)),
666 GetFetch(instruction->InputAt(1), trip, in_body, !is_min));
667 } else if (IsInt64AndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
668 return SubValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
669 Value(static_cast<int32_t>(value)));
670 }
Aart Bik52be7e72016-06-23 11:20:41 -0700671 } else if (instruction->IsArrayLength()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700672 // Exploit length properties when chasing constants or chase into a new array declaration.
Aart Bik52be7e72016-06-23 11:20:41 -0700673 if (chase_hint_ == nullptr) {
674 return is_min ? Value(0) : Value(std::numeric_limits<int32_t>::max());
675 } else if (instruction->InputAt(0)->IsNewArray()) {
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +0000676 return GetFetch(instruction->InputAt(0)->AsNewArray()->GetLength(), trip, in_body, is_min);
Aart Bik52be7e72016-06-23 11:20:41 -0700677 }
Aart Bik0d345cf2016-03-16 10:49:38 -0700678 } else if (instruction->IsTypeConversion()) {
Aart Bik40fbf742016-10-31 11:02:50 -0700679 // Since analysis is 32-bit (or narrower), chase beyond widening along the path.
Aart Bike6bd0272016-12-16 13:57:52 -0800680 // For example, this discovers the length in: for (long i = 0; i < a.length; i++);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100681 if (instruction->AsTypeConversion()->GetInputType() == DataType::Type::kInt32 &&
682 instruction->AsTypeConversion()->GetResultType() == DataType::Type::kInt64) {
Aart Bik0d345cf2016-03-16 10:49:38 -0700683 return GetFetch(instruction->InputAt(0), trip, in_body, is_min);
684 }
Aart Bik52be7e72016-06-23 11:20:41 -0700685 }
686 // Chase an invariant fetch that is defined by an outer loop if the trip-count used
687 // so far is well-behaved in both bounds and the next trip-count is safe.
688 // Example:
689 // for (int i = 0; i <= 100; i++) // safe
690 // for (int j = 0; j <= i; j++) // well-behaved
691 // j is in range [0, i ] (if i is chase hint)
692 // or in range [0, 100] (otherwise)
693 HLoopInformation* next_loop = nullptr;
694 HInductionVarAnalysis::InductionInfo* next_info = nullptr;
695 HInductionVarAnalysis::InductionInfo* next_trip = nullptr;
696 bool next_in_body = true; // inner loop is always in body of outer loop
697 if (HasInductionInfo(instruction, instruction, &next_loop, &next_info, &next_trip) &&
698 IsWellBehavedTripCount(trip) &&
699 !IsUnsafeTripCount(next_trip)) {
700 return GetVal(next_info, next_trip, next_in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700701 }
Aart Bik40fbf742016-10-31 11:02:50 -0700702 // Fetch is represented by itself.
Aart Bikd14c5952015-09-08 15:25:15 -0700703 return Value(instruction, 1, 0);
704}
705
Aart Bikcd26feb2015-09-23 17:50:50 -0700706InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
707 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700708 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800709 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700710 if (info != nullptr) {
711 switch (info->induction_class) {
712 case HInductionVarAnalysis::kInvariant:
713 // Invariants.
714 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700715 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700716 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
717 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700718 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700719 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
720 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700721 case HInductionVarAnalysis::kNeg: // second reversed!
722 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700723 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700724 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700725 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700726 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700727 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikdf7822e2016-12-06 10:05:30 -0800728 case HInductionVarAnalysis::kRem:
729 return GetRem(info->op_a, info->op_b);
Aart Bik7dc96932016-10-12 10:01:05 -0700730 case HInductionVarAnalysis::kXor:
731 return GetXor(info->op_a, info->op_b);
Aart Bikd14c5952015-09-08 15:25:15 -0700732 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700733 return GetFetch(info->fetch, trip, in_body, is_min);
734 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700735 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700736 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700737 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700738 }
739 FALLTHROUGH_INTENDED;
740 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700741 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700742 if (is_min) {
743 return Value(0);
744 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700745 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700746 }
747 break;
748 default:
749 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700750 }
751 break;
Aart Bik52be7e72016-06-23 11:20:41 -0700752 case HInductionVarAnalysis::kLinear:
Aart Bik0d345cf2016-03-16 10:49:38 -0700753 return CorrectForType(GetLinear(info, trip, in_body, is_min), info->type);
Aart Bikc071a012016-12-01 10:22:31 -0800754 case HInductionVarAnalysis::kPolynomial:
Aart Bikdf7822e2016-12-06 10:05:30 -0800755 return GetPolynomial(info, trip, in_body, is_min);
Aart Bikc071a012016-12-01 10:22:31 -0800756 case HInductionVarAnalysis::kGeometric:
757 return GetGeometric(info, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700758 case HInductionVarAnalysis::kWrapAround:
759 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700760 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
761 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700762 }
763 }
Aart Bikb3365e02015-09-21 14:45:05 -0700764 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700765}
766
767InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
768 HInductionVarAnalysis::InductionInfo* info2,
769 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700770 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800771 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700772 // Constant times range.
773 int64_t value = 0;
774 if (IsConstant(info1, kExact, &value)) {
775 return MulRangeAndConstant(value, info2, trip, in_body, is_min);
776 } else if (IsConstant(info2, kExact, &value)) {
777 return MulRangeAndConstant(value, info1, trip, in_body, is_min);
778 }
779 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700780 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
781 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
782 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
783 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800784 // Positive range vs. positive or negative range.
785 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
786 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
787 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
788 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
789 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700790 }
Aart Bik97412c922016-02-19 20:14:38 -0800791 }
792 // Negative range vs. positive or negative range.
793 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
794 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
795 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
796 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
797 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700798 }
799 }
Aart Bikb3365e02015-09-21 14:45:05 -0700800 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700801}
802
803InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
804 HInductionVarAnalysis::InductionInfo* info2,
805 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700806 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800807 bool is_min) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700808 // Range divided by constant.
809 int64_t value = 0;
810 if (IsConstant(info2, kExact, &value)) {
811 return DivRangeAndConstant(value, info1, trip, in_body, is_min);
812 }
813 // Interval ranges.
Aart Bik9401f532015-09-28 16:25:56 -0700814 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
815 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
816 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
817 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800818 // Positive range vs. positive or negative range.
819 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
820 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
821 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
822 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
823 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700824 }
Aart Bik97412c922016-02-19 20:14:38 -0800825 }
826 // Negative range vs. positive or negative range.
827 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
828 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
829 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
830 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
831 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700832 }
833 }
Aart Bikb3365e02015-09-21 14:45:05 -0700834 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700835}
836
Aart Bikdf7822e2016-12-06 10:05:30 -0800837InductionVarRange::Value InductionVarRange::GetRem(
838 HInductionVarAnalysis::InductionInfo* info1,
839 HInductionVarAnalysis::InductionInfo* info2) const {
840 int64_t v1 = 0;
841 int64_t v2 = 0;
842 // Only accept exact values.
843 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2) && v2 != 0) {
844 int64_t value = v1 % v2;
845 if (CanLongValueFitIntoInt(value)) {
846 return Value(static_cast<int32_t>(value));
847 }
848 }
849 return Value();
850}
851
Aart Bik7dc96932016-10-12 10:01:05 -0700852InductionVarRange::Value InductionVarRange::GetXor(
853 HInductionVarAnalysis::InductionInfo* info1,
854 HInductionVarAnalysis::InductionInfo* info2) const {
855 int64_t v1 = 0;
856 int64_t v2 = 0;
857 // Only accept exact values.
858 if (IsConstant(info1, kExact, &v1) && IsConstant(info2, kExact, &v2)) {
859 int64_t value = v1 ^ v2;
860 if (CanLongValueFitIntoInt(value)) {
861 return Value(static_cast<int32_t>(value));
862 }
863 }
864 return Value();
865}
866
Aart Bik52be7e72016-06-23 11:20:41 -0700867InductionVarRange::Value InductionVarRange::MulRangeAndConstant(
868 int64_t value,
869 HInductionVarAnalysis::InductionInfo* info,
870 HInductionVarAnalysis::InductionInfo* trip,
871 bool in_body,
872 bool is_min) const {
873 if (CanLongValueFitIntoInt(value)) {
874 Value c(static_cast<int32_t>(value));
875 return MulValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
876 }
877 return Value();
Aart Bik97412c922016-02-19 20:14:38 -0800878}
879
Aart Bik52be7e72016-06-23 11:20:41 -0700880InductionVarRange::Value InductionVarRange::DivRangeAndConstant(
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 DivValue(GetVal(info, trip, in_body, is_min == value >= 0), c);
889 }
890 return Value();
Aart Bik9401f532015-09-28 16:25:56 -0700891}
892
Aart Bik7d57d7f2015-12-09 14:39:48 -0800893InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700894 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bike6bd0272016-12-16 13:57:52 -0800895 int32_t b = v1.b_constant + v2.b_constant;
Aart Bikd14c5952015-09-08 15:25:15 -0700896 if (v1.a_constant == 0) {
897 return Value(v2.instruction, v2.a_constant, b);
898 } else if (v2.a_constant == 0) {
899 return Value(v1.instruction, v1.a_constant, b);
900 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
901 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
902 }
903 }
Aart Bikb3365e02015-09-21 14:45:05 -0700904 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700905}
906
Aart Bik7d57d7f2015-12-09 14:39:48 -0800907InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700908 if (v1.is_known && v2.is_known && IsSafeSub(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 && IsSafeSub(0, v2.a_constant)) {
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 && IsSafeSub(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::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700922 if (v1.is_known && v2.is_known) {
923 if (v1.a_constant == 0) {
924 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
925 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
926 }
927 } else if (v2.a_constant == 0) {
928 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
929 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
930 }
Aart Bikd14c5952015-09-08 15:25:15 -0700931 }
932 }
Aart Bikb3365e02015-09-21 14:45:05 -0700933 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700934}
935
Aart Bik7d57d7f2015-12-09 14:39:48 -0800936InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700937 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700938 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
939 return Value(v1.b_constant / v2.b_constant);
940 }
941 }
Aart Bikb3365e02015-09-21 14:45:05 -0700942 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700943}
944
Aart Bik7d57d7f2015-12-09 14:39:48 -0800945InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700946 if (v1.is_known && v2.is_known) {
947 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700948 return Value(v1.instruction, v1.a_constant,
949 is_min ? std::min(v1.b_constant, v2.b_constant)
950 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700951 }
Aart Bikd14c5952015-09-08 15:25:15 -0700952 }
Aart Bikb3365e02015-09-21 14:45:05 -0700953 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700954}
955
Aart Bik9abf8942016-10-14 09:49:42 -0700956bool InductionVarRange::GenerateRangeOrLastValue(HInstruction* context,
957 HInstruction* instruction,
958 bool is_last_value,
959 HGraph* graph,
960 HBasicBlock* block,
961 /*out*/HInstruction** lower,
962 /*out*/HInstruction** upper,
963 /*out*/HInstruction** taken_test,
964 /*out*/int64_t* stride_value,
965 /*out*/bool* needs_finite_test,
966 /*out*/bool* needs_taken_test) const {
Aart Bik52be7e72016-06-23 11:20:41 -0700967 HLoopInformation* loop = nullptr;
968 HInductionVarAnalysis::InductionInfo* info = nullptr;
969 HInductionVarAnalysis::InductionInfo* trip = nullptr;
970 if (!HasInductionInfo(context, instruction, &loop, &info, &trip) || trip == nullptr) {
971 return false; // codegen needs all information, including tripcount
Aart Bik97412c922016-02-19 20:14:38 -0800972 }
973 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
974 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
975 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
976 // code does not use the trip-count explicitly (since there could be an implicit relation
977 // between e.g. an invariant subscript and a not-taken condition).
Aart Bik52be7e72016-06-23 11:20:41 -0700978 bool in_body = context->GetBlock() != loop->GetHeader();
Aart Bik16d3a652016-09-09 10:33:50 -0700979 *stride_value = 0;
980 *needs_finite_test = NeedsTripCount(info, stride_value) && IsUnsafeTripCount(trip);
Aart Bik97412c922016-02-19 20:14:38 -0800981 *needs_taken_test = IsBodyTripCount(trip);
Aart Bik16d3a652016-09-09 10:33:50 -0700982 // Handle last value request.
983 if (is_last_value) {
Aart Bikc071a012016-12-01 10:22:31 -0800984 DCHECK(!in_body);
985 switch (info->induction_class) {
986 case HInductionVarAnalysis::kLinear:
987 if (*stride_value > 0) {
988 lower = nullptr;
989 } else {
990 upper = nullptr;
991 }
992 break;
Aart Bikdf7822e2016-12-06 10:05:30 -0800993 case HInductionVarAnalysis::kPolynomial:
994 return GenerateLastValuePolynomial(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -0800995 case HInductionVarAnalysis::kGeometric:
996 return GenerateLastValueGeometric(info, trip, graph, block, lower);
Aart Bikdf7822e2016-12-06 10:05:30 -0800997 case HInductionVarAnalysis::kWrapAround:
998 return GenerateLastValueWrapAround(info, trip, graph, block, lower);
Aart Bikc071a012016-12-01 10:22:31 -0800999 case HInductionVarAnalysis::kPeriodic:
1000 return GenerateLastValuePeriodic(info, trip, graph, block, lower, needs_taken_test);
1001 default:
1002 return false;
Aart Bik16d3a652016-09-09 10:33:50 -07001003 }
1004 }
Aart Bik97412c922016-02-19 20:14:38 -08001005 // Code generation for taken test: generate the code when requested or otherwise analyze
1006 // if code generation is feasible when taken test is needed.
1007 if (taken_test != nullptr) {
1008 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
1009 } else if (*needs_taken_test) {
1010 if (!GenerateCode(
1011 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
1012 return false;
1013 }
1014 }
1015 // Code generation for lower and upper.
1016 return
1017 // Success on lower if invariant (not set), or code can be generated.
1018 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
1019 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
1020 // And success on upper.
1021 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -07001022}
1023
Aart Bikdf7822e2016-12-06 10:05:30 -08001024bool InductionVarRange::GenerateLastValuePolynomial(HInductionVarAnalysis::InductionInfo* info,
1025 HInductionVarAnalysis::InductionInfo* trip,
1026 HGraph* graph,
1027 HBasicBlock* block,
1028 /*out*/HInstruction** result) const {
1029 DCHECK(info != nullptr);
1030 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPolynomial);
1031 // Detect known coefficients and trip count (always taken).
1032 int64_t a = 0;
1033 int64_t b = 0;
1034 int64_t m = 0;
Aart Bikd0a022d2016-12-13 11:22:31 -08001035 if (IsConstant(info->op_a->op_a, kExact, &a) &&
1036 IsConstant(info->op_a->op_b, kExact, &b) &&
Aart Bikdf7822e2016-12-06 10:05:30 -08001037 IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikd0a022d2016-12-13 11:22:31 -08001038 // Evaluate bounds on sum_i=0^m-1(a * i + b) + c for known
Aart Bikdf7822e2016-12-06 10:05:30 -08001039 // maximum index value m as a * (m * (m-1)) / 2 + b * m + c.
Aart Bike6bd0272016-12-16 13:57:52 -08001040 HInstruction* c = nullptr;
1041 if (GenerateCode(info->op_b, nullptr, graph, block, graph ? &c : nullptr, false, false)) {
Aart Bikdf7822e2016-12-06 10:05:30 -08001042 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001043 DataType::Type type = info->type;
Aart Bikdf7822e2016-12-06 10:05:30 -08001044 int64_t sum = a * ((m * (m - 1)) / 2) + b * m;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001045 if (type != DataType::Type::kInt64) {
Aart Bike6bd0272016-12-16 13:57:52 -08001046 sum = static_cast<int32_t>(sum); // okay to truncate
1047 }
1048 *result =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001049 Insert(block, new (graph->GetAllocator()) HAdd(type, graph->GetConstant(type, sum), c));
Aart Bikdf7822e2016-12-06 10:05:30 -08001050 }
1051 return true;
1052 }
1053 }
1054 return false;
1055}
1056
Aart Bikc071a012016-12-01 10:22:31 -08001057bool InductionVarRange::GenerateLastValueGeometric(HInductionVarAnalysis::InductionInfo* info,
1058 HInductionVarAnalysis::InductionInfo* trip,
1059 HGraph* graph,
1060 HBasicBlock* block,
1061 /*out*/HInstruction** result) const {
1062 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001063 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kGeometric);
Aart Bikc071a012016-12-01 10:22:31 -08001064 // Detect known base and trip count (always taken).
1065 int64_t f = 0;
Aart Bike6bd0272016-12-16 13:57:52 -08001066 int64_t m = 0;
Aart Bikf3e61ee2017-04-12 17:09:20 -07001067 if (IsInt64AndGet(info->fetch, &f) && f >= 1 && IsConstant(trip->op_a, kExact, &m) && m >= 1) {
Aart Bikc071a012016-12-01 10:22:31 -08001068 HInstruction* opa = nullptr;
1069 HInstruction* opb = nullptr;
1070 if (GenerateCode(info->op_a, nullptr, graph, block, &opa, false, false) &&
1071 GenerateCode(info->op_b, nullptr, graph, block, &opb, false, false)) {
Aart Bikc071a012016-12-01 10:22:31 -08001072 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001073 DataType::Type type = info->type;
Aart Bikd3ba6262017-01-30 14:37:12 -08001074 // Compute f ^ m for known maximum index value m.
1075 bool overflow = false;
1076 int64_t fpow = IntPow(f, m, &overflow);
1077 if (info->operation == HInductionVarAnalysis::kDiv) {
1078 // For division, any overflow truncates to zero.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001079 if (overflow || (type != DataType::Type::kInt64 && !CanLongValueFitIntoInt(fpow))) {
Aart Bikd3ba6262017-01-30 14:37:12 -08001080 fpow = 0;
1081 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001082 } else if (type != DataType::Type::kInt64) {
Aart Bikd3ba6262017-01-30 14:37:12 -08001083 // For multiplication, okay to truncate to required precision.
1084 DCHECK(info->operation == HInductionVarAnalysis::kMul);
1085 fpow = static_cast<int32_t>(fpow);
1086 }
1087 // Generate code.
Aart Bike6bd0272016-12-16 13:57:52 -08001088 if (fpow == 0) {
Aart Bikc071a012016-12-01 10:22:31 -08001089 // Special case: repeated mul/div always yields zero.
Aart Bike6bd0272016-12-16 13:57:52 -08001090 *result = graph->GetConstant(type, 0);
Aart Bikc071a012016-12-01 10:22:31 -08001091 } else {
Aart Bike6bd0272016-12-16 13:57:52 -08001092 // Last value: a * f ^ m + b or a * f ^ -m + b.
Aart Bike6bd0272016-12-16 13:57:52 -08001093 HInstruction* e = nullptr;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001094 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001095 if (info->operation == HInductionVarAnalysis::kMul) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001096 e = new (allocator) HMul(type, opa, graph->GetConstant(type, fpow));
Aart Bike6bd0272016-12-16 13:57:52 -08001097 } else {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001098 e = new (allocator) HDiv(type, opa, graph->GetConstant(type, fpow), kNoDexPc);
Aart Bike6bd0272016-12-16 13:57:52 -08001099 }
Vladimir Markoca6fff82017-10-03 14:49:14 +01001100 *result = Insert(block, new (allocator) HAdd(type, Insert(block, e), opb));
Aart Bikc071a012016-12-01 10:22:31 -08001101 }
1102 }
1103 return true;
1104 }
1105 }
1106 return false;
1107}
1108
Aart Bikdf7822e2016-12-06 10:05:30 -08001109bool InductionVarRange::GenerateLastValueWrapAround(HInductionVarAnalysis::InductionInfo* info,
1110 HInductionVarAnalysis::InductionInfo* trip,
1111 HGraph* graph,
1112 HBasicBlock* block,
1113 /*out*/HInstruction** result) const {
1114 DCHECK(info != nullptr);
1115 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kWrapAround);
1116 // Count depth.
1117 int32_t depth = 0;
1118 for (; info->induction_class == HInductionVarAnalysis::kWrapAround;
1119 info = info->op_b, ++depth) {}
1120 // Handle wrap(x, wrap(.., y)) if trip count reaches an invariant at end.
Aart Bike6bd0272016-12-16 13:57:52 -08001121 // TODO: generalize, but be careful to adjust the terminal.
1122 int64_t m = 0;
Aart Bikdf7822e2016-12-06 10:05:30 -08001123 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
Aart Bike6bd0272016-12-16 13:57:52 -08001124 IsConstant(trip->op_a, kExact, &m) && m >= depth) {
1125 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bikdf7822e2016-12-06 10:05:30 -08001126 }
1127 return false;
1128}
1129
Aart Bik9abf8942016-10-14 09:49:42 -07001130bool InductionVarRange::GenerateLastValuePeriodic(HInductionVarAnalysis::InductionInfo* info,
1131 HInductionVarAnalysis::InductionInfo* trip,
1132 HGraph* graph,
1133 HBasicBlock* block,
1134 /*out*/HInstruction** result,
1135 /*out*/bool* needs_taken_test) const {
Aart Bikc071a012016-12-01 10:22:31 -08001136 DCHECK(info != nullptr);
Aart Bikdf7822e2016-12-06 10:05:30 -08001137 DCHECK_EQ(info->induction_class, HInductionVarAnalysis::kPeriodic);
Aart Bik8523ea12017-06-01 15:45:09 -07001138 // Count period and detect all-invariants.
Aart Bike6bd0272016-12-16 13:57:52 -08001139 int64_t period = 1;
Aart Bik8523ea12017-06-01 15:45:09 -07001140 bool all_invariants = true;
1141 HInductionVarAnalysis::InductionInfo* p = info;
1142 for (; p->induction_class == HInductionVarAnalysis::kPeriodic; p = p->op_b, ++period) {
1143 DCHECK_EQ(p->op_a->induction_class, HInductionVarAnalysis::kInvariant);
1144 if (p->op_a->operation != HInductionVarAnalysis::kFetch) {
1145 all_invariants = false;
1146 }
1147 }
1148 DCHECK_EQ(p->induction_class, HInductionVarAnalysis::kInvariant);
1149 if (p->operation != HInductionVarAnalysis::kFetch) {
1150 all_invariants = false;
1151 }
1152 // Don't rely on FP arithmetic to be precise, unless the full period
1153 // consist of pre-computed expressions only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001154 if (info->type == DataType::Type::kFloat32 || info->type == DataType::Type::kFloat64) {
Aart Bik8523ea12017-06-01 15:45:09 -07001155 if (!all_invariants) {
1156 return false;
1157 }
1158 }
Aart Bike6bd0272016-12-16 13:57:52 -08001159 // Handle any periodic(x, periodic(.., y)) for known maximum index value m.
1160 int64_t m = 0;
1161 if (IsConstant(trip->op_a, kExact, &m) && m >= 1) {
1162 int64_t li = m % period;
1163 for (int64_t i = 0; i < li; info = info->op_b, i++) {}
1164 if (info->induction_class == HInductionVarAnalysis::kPeriodic) {
1165 info = info->op_a;
1166 }
1167 return GenerateCode(info, nullptr, graph, block, result, false, false);
Aart Bik9abf8942016-10-14 09:49:42 -07001168 }
Aart Bike6bd0272016-12-16 13:57:52 -08001169 // Handle periodic(x, y) using even/odd-select on trip count. Enter trip count expression
1170 // directly to obtain the maximum index value t even if taken test is needed.
1171 HInstruction* x = nullptr;
1172 HInstruction* y = nullptr;
1173 HInstruction* t = nullptr;
1174 if (period == 2 &&
1175 GenerateCode(info->op_a, nullptr, graph, block, graph ? &x : nullptr, false, false) &&
1176 GenerateCode(info->op_b, nullptr, graph, block, graph ? &y : nullptr, false, false) &&
1177 GenerateCode(trip->op_a, nullptr, graph, block, graph ? &t : nullptr, false, false)) {
1178 // During actual code generation (graph != nullptr), generate is_even ? x : y.
Aart Bik9abf8942016-10-14 09:49:42 -07001179 if (graph != nullptr) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001180 DataType::Type type = trip->type;
Vladimir Markoca6fff82017-10-03 14:49:14 +01001181 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001182 HInstruction* msk =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001183 Insert(block, new (allocator) HAnd(type, t, graph->GetConstant(type, 1)));
Aart Bike6bd0272016-12-16 13:57:52 -08001184 HInstruction* is_even =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001185 Insert(block, new (allocator) HEqual(msk, graph->GetConstant(type, 0), kNoDexPc));
1186 *result = Insert(block, new (graph->GetAllocator()) HSelect(is_even, x, y, kNoDexPc));
Aart Bik9abf8942016-10-14 09:49:42 -07001187 }
1188 // Guard select with taken test if needed.
1189 if (*needs_taken_test) {
Aart Bike6bd0272016-12-16 13:57:52 -08001190 HInstruction* is_taken = nullptr;
1191 if (GenerateCode(trip->op_b, nullptr, graph, block, graph ? &is_taken : nullptr, false, false)) {
1192 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001193 ArenaAllocator* allocator = graph->GetAllocator();
1194 *result = Insert(block, new (allocator) HSelect(is_taken, *result, x, kNoDexPc));
Aart Bike6bd0272016-12-16 13:57:52 -08001195 }
1196 *needs_taken_test = false; // taken care of
1197 } else {
Aart Bik9abf8942016-10-14 09:49:42 -07001198 return false;
Aart Bik9abf8942016-10-14 09:49:42 -07001199 }
Aart Bik9abf8942016-10-14 09:49:42 -07001200 }
1201 return true;
1202 }
1203 return false;
1204}
1205
Aart Bikaec3cce2015-10-14 17:44:55 -07001206bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
1207 HInductionVarAnalysis::InductionInfo* trip,
1208 HGraph* graph, // when set, code is generated
1209 HBasicBlock* block,
1210 /*out*/HInstruction** result,
1211 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -08001212 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -07001213 if (info != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001214 // If during codegen, the result is not needed (nullptr), simply return success.
1215 if (graph != nullptr && result == nullptr) {
1216 return true;
1217 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001218 // Handle current operation.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001219 DataType::Type type = info->type;
Aart Bikaec3cce2015-10-14 17:44:55 -07001220 HInstruction* opa = nullptr;
1221 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -07001222 switch (info->induction_class) {
1223 case HInductionVarAnalysis::kInvariant:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001224 // Invariants (note that since invariants only have other invariants as
1225 // sub expressions, viz. no induction, there is no need to adjust is_min).
Aart Bikaec3cce2015-10-14 17:44:55 -07001226 switch (info->operation) {
1227 case HInductionVarAnalysis::kAdd:
Aart Bik8e02e3e2017-02-28 14:41:55 -08001228 case HInductionVarAnalysis::kSub:
1229 case HInductionVarAnalysis::kMul:
1230 case HInductionVarAnalysis::kDiv:
1231 case HInductionVarAnalysis::kRem:
1232 case HInductionVarAnalysis::kXor:
Aart Bik389b3db2015-10-28 14:23:40 -07001233 case HInductionVarAnalysis::kLT:
1234 case HInductionVarAnalysis::kLE:
1235 case HInductionVarAnalysis::kGT:
1236 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -07001237 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
1238 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1239 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -07001240 HInstruction* operation = nullptr;
1241 switch (info->operation) {
1242 case HInductionVarAnalysis::kAdd:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001243 operation = new (graph->GetAllocator()) HAdd(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001244 case HInductionVarAnalysis::kSub:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001245 operation = new (graph->GetAllocator()) HSub(type, opa, opb); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001246 case HInductionVarAnalysis::kMul:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001247 operation = new (graph->GetAllocator()) HMul(type, opa, opb, kNoDexPc); break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001248 case HInductionVarAnalysis::kDiv:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001249 operation = new (graph->GetAllocator()) HDiv(type, opa, opb, kNoDexPc); break;
Aart Bikdf7822e2016-12-06 10:05:30 -08001250 case HInductionVarAnalysis::kRem:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001251 operation = new (graph->GetAllocator()) HRem(type, opa, opb, kNoDexPc); break;
Aart Bik9abf8942016-10-14 09:49:42 -07001252 case HInductionVarAnalysis::kXor:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001253 operation = new (graph->GetAllocator()) HXor(type, opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001254 case HInductionVarAnalysis::kLT:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001255 operation = new (graph->GetAllocator()) HLessThan(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001256 case HInductionVarAnalysis::kLE:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001257 operation = new (graph->GetAllocator()) HLessThanOrEqual(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001258 case HInductionVarAnalysis::kGT:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001259 operation = new (graph->GetAllocator()) HGreaterThan(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001260 case HInductionVarAnalysis::kGE:
Vladimir Markoca6fff82017-10-03 14:49:14 +01001261 operation = new (graph->GetAllocator()) HGreaterThanOrEqual(opa, opb); break;
Aart Bik389b3db2015-10-28 14:23:40 -07001262 default:
1263 LOG(FATAL) << "unknown operation";
1264 }
1265 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -07001266 }
1267 return true;
1268 }
1269 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001270 case HInductionVarAnalysis::kNeg:
Aart Bikaec3cce2015-10-14 17:44:55 -07001271 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
1272 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001273 *result = Insert(block, new (graph->GetAllocator()) HNeg(type, opb));
Aart Bikaec3cce2015-10-14 17:44:55 -07001274 }
1275 return true;
1276 }
1277 break;
1278 case HInductionVarAnalysis::kFetch:
Aart Bik0d345cf2016-03-16 10:49:38 -07001279 if (graph != nullptr) {
1280 *result = info->fetch; // already in HIR
Aart Bikaec3cce2015-10-14 17:44:55 -07001281 }
Aart Bik0d345cf2016-03-16 10:49:38 -07001282 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001283 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -07001284 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001285 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -07001286 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -07001287 }
1288 FALLTHROUGH_INTENDED;
1289 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -07001290 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -07001291 if (is_min) {
1292 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001293 *result = graph->GetConstant(type, 0);
Aart Bikaec3cce2015-10-14 17:44:55 -07001294 }
1295 return true;
1296 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -07001297 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -07001298 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001299 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001300 *result =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001301 Insert(block, new (allocator) HSub(type, opb, graph->GetConstant(type, 1)));
Aart Bikaec3cce2015-10-14 17:44:55 -07001302 }
1303 return true;
1304 }
1305 }
1306 break;
Aart Bik8e02e3e2017-02-28 14:41:55 -08001307 case HInductionVarAnalysis::kNop:
1308 LOG(FATAL) << "unexpected invariant nop";
1309 } // switch invariant operation
Aart Bikaec3cce2015-10-14 17:44:55 -07001310 break;
Aart Bik389b3db2015-10-28 14:23:40 -07001311 case HInductionVarAnalysis::kLinear: {
Aart Bik16d3a652016-09-09 10:33:50 -07001312 // Linear induction a * i + b, for normalized 0 <= i < TC. For ranges, this should
1313 // be restricted to a unit stride to avoid arithmetic wrap-around situations that
1314 // are harder to guard against. For a last value, requesting min/max based on any
Aart Bike6bd0272016-12-16 13:57:52 -08001315 // known stride yields right value. Always avoid any narrowing linear induction or
1316 // any type mismatch between the linear induction and the trip count expression.
1317 // TODO: careful runtime type conversions could generalize this latter restriction.
1318 if (!HInductionVarAnalysis::IsNarrowingLinear(info) && trip->type == type) {
1319 int64_t stride_value = 0;
1320 if (IsConstant(info->op_a, kExact, &stride_value) &&
1321 CanLongValueFitIntoInt(stride_value)) {
1322 const bool is_min_a = stride_value >= 0 ? is_min : !is_min;
1323 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
1324 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
1325 if (graph != nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001326 ArenaAllocator* allocator = graph->GetAllocator();
Aart Bike6bd0272016-12-16 13:57:52 -08001327 HInstruction* oper;
1328 if (stride_value == 1) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001329 oper = new (allocator) HAdd(type, opa, opb);
Aart Bike6bd0272016-12-16 13:57:52 -08001330 } else if (stride_value == -1) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01001331 oper = new (graph->GetAllocator()) HSub(type, opb, opa);
Aart Bike6bd0272016-12-16 13:57:52 -08001332 } else {
1333 HInstruction* mul =
Vladimir Markoca6fff82017-10-03 14:49:14 +01001334 new (allocator) HMul(type, graph->GetConstant(type, stride_value), opa);
1335 oper = new (allocator) HAdd(type, Insert(block, mul), opb);
Aart Bike6bd0272016-12-16 13:57:52 -08001336 }
1337 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -07001338 }
Aart Bike6bd0272016-12-16 13:57:52 -08001339 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -07001340 }
1341 }
1342 }
1343 break;
Aart Bik4a342772015-11-30 10:17:46 -08001344 }
Aart Bikc071a012016-12-01 10:22:31 -08001345 case HInductionVarAnalysis::kPolynomial:
1346 case HInductionVarAnalysis::kGeometric:
1347 break;
Aart Bik4a342772015-11-30 10:17:46 -08001348 case HInductionVarAnalysis::kWrapAround:
1349 case HInductionVarAnalysis::kPeriodic: {
1350 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
1351 // values are easy to test at runtime without complications of arithmetic wrap-around.
1352 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -08001353 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -08001354 if (graph != nullptr) {
Aart Bike6bd0272016-12-16 13:57:52 -08001355 *result = graph->GetConstant(type, extreme.b_constant);
Aart Bik4a342772015-11-30 10:17:46 -08001356 }
1357 return true;
1358 }
1359 break;
1360 }
Aart Bik8e02e3e2017-02-28 14:41:55 -08001361 } // switch induction class
Aart Bikaec3cce2015-10-14 17:44:55 -07001362 }
1363 return false;
1364}
1365
Aart Bik16d3a652016-09-09 10:33:50 -07001366void InductionVarRange::ReplaceInduction(HInductionVarAnalysis::InductionInfo* info,
1367 HInstruction* fetch,
1368 HInstruction* replacement) {
1369 if (info != nullptr) {
1370 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
1371 info->operation == HInductionVarAnalysis::kFetch &&
1372 info->fetch == fetch) {
1373 info->fetch = replacement;
1374 }
1375 ReplaceInduction(info->op_a, fetch, replacement);
1376 ReplaceInduction(info->op_b, fetch, replacement);
1377 }
1378}
1379
Aart Bikd14c5952015-09-08 15:25:15 -07001380} // namespace art