blob: b162696a42814c0fed82616d9117564cb065307b [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 Bik97412c922016-02-19 20:14:38 -080048/** Returns true for 32/64-bit constant instruction. */
49static bool IsIntAndGet(HInstruction* instruction, int64_t* value) {
Aart Bikd14c5952015-09-08 15:25:15 -070050 if (instruction->IsIntConstant()) {
Aart Bikb3365e02015-09-21 14:45:05 -070051 *value = instruction->AsIntConstant()->GetValue();
52 return true;
Aart Bikd14c5952015-09-08 15:25:15 -070053 } else if (instruction->IsLongConstant()) {
Aart Bik97412c922016-02-19 20:14:38 -080054 *value = instruction->AsLongConstant()->GetValue();
55 return true;
Aart Bikd14c5952015-09-08 15:25:15 -070056 }
57 return false;
58}
59
Aart Bikb3365e02015-09-21 14:45:05 -070060/**
61 * An upper bound a * (length / a) + b, where a > 0, can be conservatively rewritten as length + b
62 * because length >= 0 is true. This makes it more likely the bound is useful to clients.
63 */
64static InductionVarRange::Value SimplifyMax(InductionVarRange::Value v) {
Aart Bik97412c922016-02-19 20:14:38 -080065 int64_t value;
66 if (v.is_known &&
67 v.a_constant > 1 &&
Aart Bikb3365e02015-09-21 14:45:05 -070068 v.instruction->IsDiv() &&
69 v.instruction->InputAt(0)->IsArrayLength() &&
70 IsIntAndGet(v.instruction->InputAt(1), &value) && v.a_constant == value) {
71 return InductionVarRange::Value(v.instruction->InputAt(0), 1, v.b_constant);
72 }
73 return v;
74}
75
Aart Bik97412c922016-02-19 20:14:38 -080076/** Helper method to test for a constant value. */
77static bool IsConstantValue(InductionVarRange::Value v) {
78 return v.is_known && v.a_constant == 0;
79}
80
81/** Helper method to test for same constant value. */
82static bool IsSameConstantValue(InductionVarRange::Value v1, InductionVarRange::Value v2) {
83 return IsConstantValue(v1) && IsConstantValue(v2) && v1.b_constant == v2.b_constant;
84}
85
Aart Bik389b3db2015-10-28 14:23:40 -070086/** Helper method to insert an instruction. */
87static HInstruction* Insert(HBasicBlock* block, HInstruction* instruction) {
88 DCHECK(block != nullptr);
89 DCHECK(block->GetLastInstruction() != nullptr) << block->GetBlockId();
Aart Bikaec3cce2015-10-14 17:44:55 -070090 DCHECK(instruction != nullptr);
Aart Bik389b3db2015-10-28 14:23:40 -070091 block->InsertInstructionBefore(instruction, block->GetLastInstruction());
Aart Bikaec3cce2015-10-14 17:44:55 -070092 return instruction;
93}
94
Aart Bikd14c5952015-09-08 15:25:15 -070095//
96// Public class methods.
97//
98
99InductionVarRange::InductionVarRange(HInductionVarAnalysis* induction_analysis)
100 : induction_analysis_(induction_analysis) {
Aart Bikb3365e02015-09-21 14:45:05 -0700101 DCHECK(induction_analysis != nullptr);
Aart Bikd14c5952015-09-08 15:25:15 -0700102}
103
Aart Bik1fc3afb2016-02-02 13:26:16 -0800104bool InductionVarRange::GetInductionRange(HInstruction* context,
Aart Bik389b3db2015-10-28 14:23:40 -0700105 HInstruction* instruction,
106 /*out*/Value* min_val,
107 /*out*/Value* max_val,
108 /*out*/bool* needs_finite_test) {
109 HLoopInformation* loop = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
Aart Bik97412c922016-02-19 20:14:38 -0800110 if (loop == nullptr) {
111 return false; // no loop
Aart Bik389b3db2015-10-28 14:23:40 -0700112 }
Aart Bik97412c922016-02-19 20:14:38 -0800113 HInductionVarAnalysis::InductionInfo* info = induction_analysis_->LookupInfo(loop, instruction);
114 if (info == nullptr) {
115 return false; // no induction information
116 }
117 // Set up loop information.
118 HBasicBlock* header = loop->GetHeader();
119 bool in_body = context->GetBlock() != header;
120 HInductionVarAnalysis::InductionInfo* trip =
121 induction_analysis_->LookupInfo(loop, header->GetLastInstruction());
122 // Find range.
123 *min_val = GetVal(info, trip, in_body, /* is_min */ true);
124 *max_val = SimplifyMax(GetVal(info, trip, in_body, /* is_min */ false));
125 *needs_finite_test = NeedsTripCount(info) && IsUnsafeTripCount(trip);
126 return true;
Aart Bikd14c5952015-09-08 15:25:15 -0700127}
128
Aart Bik97412c922016-02-19 20:14:38 -0800129bool InductionVarRange::RefineOuter(/*in-out*/ Value* min_val,
130 /*in-out*/ Value* max_val) const {
131 Value v1_min = RefineOuter(*min_val, /* is_min */ true);
132 Value v2_max = RefineOuter(*max_val, /* is_min */ false);
133 // The refined range is safe if both sides refine the same instruction. Otherwise, since two
134 // different ranges are combined, the new refined range is safe to pass back to the client if
135 // the extremes of the computed ranges ensure no arithmetic wrap-around anomalies occur.
136 if (min_val->instruction != max_val->instruction) {
137 Value v1_max = RefineOuter(*min_val, /* is_min */ false);
138 Value v2_min = RefineOuter(*max_val, /* is_min */ true);
139 if (!IsConstantValue(v1_max) ||
140 !IsConstantValue(v2_min) ||
141 v1_max.b_constant > v2_min.b_constant) {
142 return false;
143 }
144 }
145 // Did something change?
146 if (v1_min.instruction != min_val->instruction || v2_max.instruction != max_val->instruction) {
147 *min_val = v1_min;
148 *max_val = v2_max;
Aart Bikb738d4f2015-12-03 11:23:35 -0800149 return true;
150 }
151 return false;
152}
153
Aart Bikaec3cce2015-10-14 17:44:55 -0700154bool InductionVarRange::CanGenerateCode(HInstruction* context,
155 HInstruction* instruction,
Aart Bik389b3db2015-10-28 14:23:40 -0700156 /*out*/bool* needs_finite_test,
157 /*out*/bool* needs_taken_test) {
158 return GenerateCode(context,
159 instruction,
160 nullptr, nullptr, nullptr, nullptr, nullptr, // nothing generated yet
161 needs_finite_test,
162 needs_taken_test);
Aart Bikaec3cce2015-10-14 17:44:55 -0700163}
164
Aart Bik389b3db2015-10-28 14:23:40 -0700165void InductionVarRange::GenerateRangeCode(HInstruction* context,
166 HInstruction* instruction,
167 HGraph* graph,
168 HBasicBlock* block,
169 /*out*/HInstruction** lower,
170 /*out*/HInstruction** upper) {
171 bool b1, b2; // unused
172 if (!GenerateCode(context, instruction, graph, block, lower, upper, nullptr, &b1, &b2)) {
173 LOG(FATAL) << "Failed precondition: GenerateCode()";
174 }
175}
176
177void InductionVarRange::GenerateTakenTest(HInstruction* context,
178 HGraph* graph,
179 HBasicBlock* block,
180 /*out*/HInstruction** taken_test) {
181 bool b1, b2; // unused
182 if (!GenerateCode(context, context, graph, block, nullptr, nullptr, taken_test, &b1, &b2)) {
183 LOG(FATAL) << "Failed precondition: GenerateCode()";
184 }
Aart Bikaec3cce2015-10-14 17:44:55 -0700185}
186
Aart Bikd14c5952015-09-08 15:25:15 -0700187//
188// Private class methods.
189//
190
Aart Bik97412c922016-02-19 20:14:38 -0800191bool InductionVarRange::IsConstant(HInductionVarAnalysis::InductionInfo* info,
192 ConstantRequest request,
193 /*out*/ int64_t *value) const {
194 if (info != nullptr) {
195 // A direct 32-bit or 64-bit constant fetch. This immediately satisfies
196 // any of the three requests (kExact, kAtMost, and KAtLeast).
197 if (info->induction_class == HInductionVarAnalysis::kInvariant &&
198 info->operation == HInductionVarAnalysis::kFetch) {
199 if (IsIntAndGet(info->fetch, value)) {
200 return true;
201 }
202 }
203 // Try range analysis while traversing outward on loops.
204 bool in_body = true; // no known trip count
205 Value v_min = GetVal(info, nullptr, in_body, /* is_min */ true);
206 Value v_max = GetVal(info, nullptr, in_body, /* is_min */ false);
207 do {
208 // Make sure *both* extremes are known to avoid arithmetic wrap-around anomalies.
209 if (IsConstantValue(v_min) && IsConstantValue(v_max) && v_min.b_constant <= v_max.b_constant) {
210 if ((request == kExact && v_min.b_constant == v_max.b_constant) || request == kAtMost) {
211 *value = v_max.b_constant;
212 return true;
213 } else if (request == kAtLeast) {
214 *value = v_min.b_constant;
215 return true;
216 }
217 }
218 } while (RefineOuter(&v_min, &v_max));
219 }
220 return false;
221}
222
Aart Bik7d57d7f2015-12-09 14:39:48 -0800223bool InductionVarRange::NeedsTripCount(HInductionVarAnalysis::InductionInfo* info) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700224 if (info != nullptr) {
225 if (info->induction_class == HInductionVarAnalysis::kLinear) {
226 return true;
227 } else if (info->induction_class == HInductionVarAnalysis::kWrapAround) {
228 return NeedsTripCount(info->op_b);
229 }
Aart Bikd14c5952015-09-08 15:25:15 -0700230 }
Aart Bik389b3db2015-10-28 14:23:40 -0700231 return false;
232}
233
Aart Bik7d57d7f2015-12-09 14:39:48 -0800234bool InductionVarRange::IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700235 if (trip != nullptr) {
236 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
237 return trip->operation == HInductionVarAnalysis::kTripCountInBody ||
238 trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe;
239 }
240 }
241 return false;
242}
243
Aart Bik7d57d7f2015-12-09 14:39:48 -0800244bool InductionVarRange::IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) const {
Aart Bik389b3db2015-10-28 14:23:40 -0700245 if (trip != nullptr) {
246 if (trip->induction_class == HInductionVarAnalysis::kInvariant) {
247 return trip->operation == HInductionVarAnalysis::kTripCountInBodyUnsafe ||
248 trip->operation == HInductionVarAnalysis::kTripCountInLoopUnsafe;
249 }
250 }
251 return false;
Aart Bikd14c5952015-09-08 15:25:15 -0700252}
253
Aart Bik7d57d7f2015-12-09 14:39:48 -0800254InductionVarRange::Value InductionVarRange::GetLinear(HInductionVarAnalysis::InductionInfo* info,
255 HInductionVarAnalysis::InductionInfo* trip,
256 bool in_body,
257 bool is_min) const {
258 // Detect common situation where an offset inside the trip count cancels out during range
259 // analysis (finding max a * (TC - 1) + OFFSET for a == 1 and TC = UPPER - OFFSET or finding
260 // min a * (TC - 1) + OFFSET for a == -1 and TC = OFFSET - UPPER) to avoid losing information
261 // with intermediate results that only incorporate single instructions.
262 if (trip != nullptr) {
263 HInductionVarAnalysis::InductionInfo* trip_expr = trip->op_a;
264 if (trip_expr->operation == HInductionVarAnalysis::kSub) {
Aart Bik97412c922016-02-19 20:14:38 -0800265 int64_t stride_value = 0;
266 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik7d57d7f2015-12-09 14:39:48 -0800267 if (!is_min && stride_value == 1) {
Aart Bik97412c922016-02-19 20:14:38 -0800268 // Test original trip's negative operand (trip_expr->op_b) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800269 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_b, info->op_b)) {
270 // Analyze cancelled trip with just the positive operand (trip_expr->op_a).
271 HInductionVarAnalysis::InductionInfo cancelled_trip(
272 trip->induction_class, trip->operation, trip_expr->op_a, trip->op_b, nullptr);
273 return GetVal(&cancelled_trip, trip, in_body, is_min);
274 }
275 } else if (is_min && stride_value == -1) {
Aart Bik97412c922016-02-19 20:14:38 -0800276 // Test original trip's positive operand (trip_expr->op_a) against offset of induction.
Aart Bik7d57d7f2015-12-09 14:39:48 -0800277 if (HInductionVarAnalysis::InductionEqual(trip_expr->op_a, info->op_b)) {
278 // Analyze cancelled trip with just the negative operand (trip_expr->op_b).
279 HInductionVarAnalysis::InductionInfo neg(
280 HInductionVarAnalysis::kInvariant,
281 HInductionVarAnalysis::kNeg,
282 nullptr,
283 trip_expr->op_b,
284 nullptr);
285 HInductionVarAnalysis::InductionInfo cancelled_trip(
286 trip->induction_class, trip->operation, &neg, trip->op_b, nullptr);
287 return SubValue(Value(0), GetVal(&cancelled_trip, trip, in_body, !is_min));
288 }
289 }
290 }
291 }
292 }
293 // General rule of linear induction a * i + b, for normalized 0 <= i < TC.
294 return AddValue(GetMul(info->op_a, trip, trip, in_body, is_min),
295 GetVal(info->op_b, trip, in_body, is_min));
296}
297
Aart Bikd14c5952015-09-08 15:25:15 -0700298InductionVarRange::Value InductionVarRange::GetFetch(HInstruction* instruction,
Aart Bik22af3be2015-09-10 12:50:58 -0700299 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700300 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800301 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700302 // Detect constants and chase the fetch a bit deeper into the HIR tree, so that it becomes
303 // more likely range analysis will compare the same instructions as terminal nodes.
Aart Bik97412c922016-02-19 20:14:38 -0800304 int64_t value;
305 if (IsIntAndGet(instruction, &value) && CanLongValueFitIntoInt(value)) {
306 return Value(static_cast<int32_t>(value));
Aart Bikd14c5952015-09-08 15:25:15 -0700307 } else if (instruction->IsAdd()) {
Aart Bik97412c922016-02-19 20:14:38 -0800308 if (IsIntAndGet(instruction->InputAt(0), &value) && CanLongValueFitIntoInt(value)) {
309 return AddValue(Value(static_cast<int32_t>(value)),
310 GetFetch(instruction->InputAt(1), trip, in_body, is_min));
311 } else if (IsIntAndGet(instruction->InputAt(1), &value) && CanLongValueFitIntoInt(value)) {
312 return AddValue(GetFetch(instruction->InputAt(0), trip, in_body, is_min),
313 Value(static_cast<int32_t>(value)));
Aart Bik22af3be2015-09-10 12:50:58 -0700314 }
Aart Bikb738d4f2015-12-03 11:23:35 -0800315 } else if (instruction->IsArrayLength() && instruction->InputAt(0)->IsNewArray()) {
316 return GetFetch(instruction->InputAt(0)->InputAt(0), trip, in_body, is_min);
Aart Bikb3365e02015-09-21 14:45:05 -0700317 } else if (is_min) {
Aart Bik9401f532015-09-28 16:25:56 -0700318 // Special case for finding minimum: minimum of trip-count in loop-body is 1.
Aart Bik22f05872015-10-27 15:56:28 -0700319 if (trip != nullptr && in_body && instruction == trip->op_a->fetch) {
Aart Bik22af3be2015-09-10 12:50:58 -0700320 return Value(1);
Aart Bikd14c5952015-09-08 15:25:15 -0700321 }
322 }
323 return Value(instruction, 1, 0);
324}
325
Aart Bikcd26feb2015-09-23 17:50:50 -0700326InductionVarRange::Value InductionVarRange::GetVal(HInductionVarAnalysis::InductionInfo* info,
327 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700328 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800329 bool is_min) const {
Aart Bikd14c5952015-09-08 15:25:15 -0700330 if (info != nullptr) {
331 switch (info->induction_class) {
332 case HInductionVarAnalysis::kInvariant:
333 // Invariants.
334 switch (info->operation) {
Aart Bikd14c5952015-09-08 15:25:15 -0700335 case HInductionVarAnalysis::kAdd:
Aart Bik9401f532015-09-28 16:25:56 -0700336 return AddValue(GetVal(info->op_a, trip, in_body, is_min),
337 GetVal(info->op_b, trip, in_body, is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700338 case HInductionVarAnalysis::kSub: // second reversed!
Aart Bik9401f532015-09-28 16:25:56 -0700339 return SubValue(GetVal(info->op_a, trip, in_body, is_min),
340 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikcd26feb2015-09-23 17:50:50 -0700341 case HInductionVarAnalysis::kNeg: // second reversed!
342 return SubValue(Value(0),
Aart Bik9401f532015-09-28 16:25:56 -0700343 GetVal(info->op_b, trip, in_body, !is_min));
Aart Bikd14c5952015-09-08 15:25:15 -0700344 case HInductionVarAnalysis::kMul:
Aart Bik9401f532015-09-28 16:25:56 -0700345 return GetMul(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700346 case HInductionVarAnalysis::kDiv:
Aart Bik9401f532015-09-28 16:25:56 -0700347 return GetDiv(info->op_a, info->op_b, trip, in_body, is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700348 case HInductionVarAnalysis::kFetch:
Aart Bik9401f532015-09-28 16:25:56 -0700349 return GetFetch(info->fetch, trip, in_body, is_min);
350 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700351 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700352 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700353 return GetVal(info->op_a, trip, in_body, is_min);
Aart Bik9401f532015-09-28 16:25:56 -0700354 }
355 FALLTHROUGH_INTENDED;
356 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700357 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700358 if (is_min) {
359 return Value(0);
360 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700361 return SubValue(GetVal(info->op_a, trip, in_body, is_min), Value(1));
Aart Bik9401f532015-09-28 16:25:56 -0700362 }
363 break;
364 default:
365 break;
Aart Bikd14c5952015-09-08 15:25:15 -0700366 }
367 break;
Aart Bik7d57d7f2015-12-09 14:39:48 -0800368 case HInductionVarAnalysis::kLinear: {
369 return GetLinear(info, trip, in_body, is_min);
370 }
Aart Bikd14c5952015-09-08 15:25:15 -0700371 case HInductionVarAnalysis::kWrapAround:
372 case HInductionVarAnalysis::kPeriodic:
Aart Bik9401f532015-09-28 16:25:56 -0700373 return MergeVal(GetVal(info->op_a, trip, in_body, is_min),
374 GetVal(info->op_b, trip, in_body, is_min), is_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700375 }
376 }
Aart Bikb3365e02015-09-21 14:45:05 -0700377 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700378}
379
380InductionVarRange::Value InductionVarRange::GetMul(HInductionVarAnalysis::InductionInfo* info1,
381 HInductionVarAnalysis::InductionInfo* info2,
382 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700383 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800384 bool is_min) const {
Aart Bik9401f532015-09-28 16:25:56 -0700385 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
386 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
387 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
388 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800389 // Try to refine first operand.
390 if (!IsConstantValue(v1_min) && !IsConstantValue(v1_max)) {
391 RefineOuter(&v1_min, &v1_max);
Aart Bik7d57d7f2015-12-09 14:39:48 -0800392 }
Aart Bik97412c922016-02-19 20:14:38 -0800393 // Constant times range.
394 if (IsSameConstantValue(v1_min, v1_max)) {
395 return MulRangeAndConstant(v2_min, v2_max, v1_min, is_min);
396 } else if (IsSameConstantValue(v2_min, v2_max)) {
397 return MulRangeAndConstant(v1_min, v1_max, v2_min, is_min);
398 }
399 // Positive range vs. positive or negative range.
400 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
401 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
402 return is_min ? MulValue(v1_min, v2_min) : MulValue(v1_max, v2_max);
403 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
404 return is_min ? MulValue(v1_max, v2_min) : MulValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700405 }
Aart Bik97412c922016-02-19 20:14:38 -0800406 }
407 // Negative range vs. positive or negative range.
408 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
409 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
410 return is_min ? MulValue(v1_min, v2_max) : MulValue(v1_max, v2_min);
411 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
412 return is_min ? MulValue(v1_max, v2_max) : MulValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700413 }
414 }
Aart Bikb3365e02015-09-21 14:45:05 -0700415 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700416}
417
418InductionVarRange::Value InductionVarRange::GetDiv(HInductionVarAnalysis::InductionInfo* info1,
419 HInductionVarAnalysis::InductionInfo* info2,
420 HInductionVarAnalysis::InductionInfo* trip,
Aart Bik9401f532015-09-28 16:25:56 -0700421 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800422 bool is_min) const {
Aart Bik9401f532015-09-28 16:25:56 -0700423 Value v1_min = GetVal(info1, trip, in_body, /* is_min */ true);
424 Value v1_max = GetVal(info1, trip, in_body, /* is_min */ false);
425 Value v2_min = GetVal(info2, trip, in_body, /* is_min */ true);
426 Value v2_max = GetVal(info2, trip, in_body, /* is_min */ false);
Aart Bik97412c922016-02-19 20:14:38 -0800427 // Range divided by constant.
428 if (IsSameConstantValue(v2_min, v2_max)) {
429 return DivRangeAndConstant(v1_min, v1_max, v2_min, is_min);
430 }
431 // Positive range vs. positive or negative range.
432 if (IsConstantValue(v1_min) && v1_min.b_constant >= 0) {
433 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
434 return is_min ? DivValue(v1_min, v2_max) : DivValue(v1_max, v2_min);
435 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
436 return is_min ? DivValue(v1_max, v2_max) : DivValue(v1_min, v2_min);
Aart Bikd14c5952015-09-08 15:25:15 -0700437 }
Aart Bik97412c922016-02-19 20:14:38 -0800438 }
439 // Negative range vs. positive or negative range.
440 if (IsConstantValue(v1_max) && v1_max.b_constant <= 0) {
441 if (IsConstantValue(v2_min) && v2_min.b_constant >= 0) {
442 return is_min ? DivValue(v1_min, v2_min) : DivValue(v1_max, v2_max);
443 } else if (IsConstantValue(v2_max) && v2_max.b_constant <= 0) {
444 return is_min ? DivValue(v1_max, v2_min) : DivValue(v1_min, v2_max);
Aart Bikd14c5952015-09-08 15:25:15 -0700445 }
446 }
Aart Bikb3365e02015-09-21 14:45:05 -0700447 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700448}
449
Aart Bik97412c922016-02-19 20:14:38 -0800450InductionVarRange::Value InductionVarRange::MulRangeAndConstant(Value v_min,
451 Value v_max,
452 Value c,
453 bool is_min) const {
454 return is_min == (c.b_constant >= 0) ? MulValue(v_min, c) : MulValue(v_max, c);
455}
456
457InductionVarRange::Value InductionVarRange::DivRangeAndConstant(Value v_min,
458 Value v_max,
459 Value c,
460 bool is_min) const {
461 return is_min == (c.b_constant >= 0) ? DivValue(v_min, c) : DivValue(v_max, c);
Aart Bik9401f532015-09-28 16:25:56 -0700462}
463
Aart Bik7d57d7f2015-12-09 14:39:48 -0800464InductionVarRange::Value InductionVarRange::AddValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700465 if (v1.is_known && v2.is_known && IsSafeAdd(v1.b_constant, v2.b_constant)) {
Aart Bikd14c5952015-09-08 15:25:15 -0700466 const int32_t b = v1.b_constant + v2.b_constant;
467 if (v1.a_constant == 0) {
468 return Value(v2.instruction, v2.a_constant, b);
469 } else if (v2.a_constant == 0) {
470 return Value(v1.instruction, v1.a_constant, b);
471 } else if (v1.instruction == v2.instruction && IsSafeAdd(v1.a_constant, v2.a_constant)) {
472 return Value(v1.instruction, v1.a_constant + v2.a_constant, b);
473 }
474 }
Aart Bikb3365e02015-09-21 14:45:05 -0700475 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700476}
477
Aart Bik7d57d7f2015-12-09 14:39:48 -0800478InductionVarRange::Value InductionVarRange::SubValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700479 if (v1.is_known && v2.is_known && IsSafeSub(v1.b_constant, v2.b_constant)) {
Aart Bikd14c5952015-09-08 15:25:15 -0700480 const int32_t b = v1.b_constant - v2.b_constant;
481 if (v1.a_constant == 0 && IsSafeSub(0, v2.a_constant)) {
482 return Value(v2.instruction, -v2.a_constant, b);
483 } else if (v2.a_constant == 0) {
484 return Value(v1.instruction, v1.a_constant, b);
485 } else if (v1.instruction == v2.instruction && IsSafeSub(v1.a_constant, v2.a_constant)) {
486 return Value(v1.instruction, v1.a_constant - v2.a_constant, b);
487 }
488 }
Aart Bikb3365e02015-09-21 14:45:05 -0700489 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700490}
491
Aart Bik7d57d7f2015-12-09 14:39:48 -0800492InductionVarRange::Value InductionVarRange::MulValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700493 if (v1.is_known && v2.is_known) {
494 if (v1.a_constant == 0) {
495 if (IsSafeMul(v1.b_constant, v2.a_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
496 return Value(v2.instruction, v1.b_constant * v2.a_constant, v1.b_constant * v2.b_constant);
497 }
498 } else if (v2.a_constant == 0) {
499 if (IsSafeMul(v1.a_constant, v2.b_constant) && IsSafeMul(v1.b_constant, v2.b_constant)) {
500 return Value(v1.instruction, v1.a_constant * v2.b_constant, v1.b_constant * v2.b_constant);
501 }
Aart Bikd14c5952015-09-08 15:25:15 -0700502 }
503 }
Aart Bikb3365e02015-09-21 14:45:05 -0700504 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700505}
506
Aart Bik7d57d7f2015-12-09 14:39:48 -0800507InductionVarRange::Value InductionVarRange::DivValue(Value v1, Value v2) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700508 if (v1.is_known && v2.is_known && v1.a_constant == 0 && v2.a_constant == 0) {
Aart Bikd14c5952015-09-08 15:25:15 -0700509 if (IsSafeDiv(v1.b_constant, v2.b_constant)) {
510 return Value(v1.b_constant / v2.b_constant);
511 }
512 }
Aart Bikb3365e02015-09-21 14:45:05 -0700513 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700514}
515
Aart Bik7d57d7f2015-12-09 14:39:48 -0800516InductionVarRange::Value InductionVarRange::MergeVal(Value v1, Value v2, bool is_min) const {
Aart Bikb3365e02015-09-21 14:45:05 -0700517 if (v1.is_known && v2.is_known) {
518 if (v1.instruction == v2.instruction && v1.a_constant == v2.a_constant) {
Aart Bikcd26feb2015-09-23 17:50:50 -0700519 return Value(v1.instruction, v1.a_constant,
520 is_min ? std::min(v1.b_constant, v2.b_constant)
521 : std::max(v1.b_constant, v2.b_constant));
Aart Bikb3365e02015-09-21 14:45:05 -0700522 }
Aart Bikd14c5952015-09-08 15:25:15 -0700523 }
Aart Bikb3365e02015-09-21 14:45:05 -0700524 return Value();
Aart Bikd14c5952015-09-08 15:25:15 -0700525}
526
Aart Bik7d57d7f2015-12-09 14:39:48 -0800527InductionVarRange::Value InductionVarRange::RefineOuter(Value v, bool is_min) const {
Aart Bik97412c922016-02-19 20:14:38 -0800528 if (v.instruction == nullptr) {
529 return v; // nothing to refine
Aart Bikb738d4f2015-12-03 11:23:35 -0800530 }
Aart Bik97412c922016-02-19 20:14:38 -0800531 HLoopInformation* loop =
532 v.instruction->GetBlock()->GetLoopInformation(); // closest enveloping loop
533 if (loop == nullptr) {
534 return v; // no loop
535 }
536 HInductionVarAnalysis::InductionInfo* info = induction_analysis_->LookupInfo(loop, v.instruction);
537 if (info == nullptr) {
538 return v; // no induction information
539 }
540 // Set up loop information.
541 HBasicBlock* header = loop->GetHeader();
542 bool in_body = true; // inner always in more outer
543 HInductionVarAnalysis::InductionInfo* trip =
544 induction_analysis_->LookupInfo(loop, header->GetLastInstruction());
545 // Try to refine "a x instruction + b" with outer loop range information on instruction.
546 return AddValue(MulValue(Value(v.a_constant), GetVal(info, trip, in_body, is_min)), Value(v.b_constant));
Aart Bikb738d4f2015-12-03 11:23:35 -0800547}
548
Aart Bikaec3cce2015-10-14 17:44:55 -0700549bool InductionVarRange::GenerateCode(HInstruction* context,
550 HInstruction* instruction,
551 HGraph* graph,
552 HBasicBlock* block,
553 /*out*/HInstruction** lower,
554 /*out*/HInstruction** upper,
Aart Bik389b3db2015-10-28 14:23:40 -0700555 /*out*/HInstruction** taken_test,
556 /*out*/bool* needs_finite_test,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800557 /*out*/bool* needs_taken_test) const {
Aart Bikaec3cce2015-10-14 17:44:55 -0700558 HLoopInformation* loop = context->GetBlock()->GetLoopInformation(); // closest enveloping loop
Aart Bik97412c922016-02-19 20:14:38 -0800559 if (loop == nullptr) {
560 return false; // no loop
Aart Bikaec3cce2015-10-14 17:44:55 -0700561 }
Aart Bik97412c922016-02-19 20:14:38 -0800562 HInductionVarAnalysis::InductionInfo* info = induction_analysis_->LookupInfo(loop, instruction);
563 if (info == nullptr) {
564 return false; // no induction information
565 }
566 // Set up loop information.
567 HBasicBlock* header = loop->GetHeader();
568 bool in_body = context->GetBlock() != header;
569 HInductionVarAnalysis::InductionInfo* trip =
570 induction_analysis_->LookupInfo(loop, header->GetLastInstruction());
571 if (trip == nullptr) {
572 return false; // codegen relies on trip count
573 }
574 // Determine what tests are needed. A finite test is needed if the evaluation code uses the
575 // trip-count and the loop maybe unsafe (because in such cases, the index could "overshoot"
576 // the computed range). A taken test is needed for any unknown trip-count, even if evaluation
577 // code does not use the trip-count explicitly (since there could be an implicit relation
578 // between e.g. an invariant subscript and a not-taken condition).
579 *needs_finite_test = NeedsTripCount(info) && IsUnsafeTripCount(trip);
580 *needs_taken_test = IsBodyTripCount(trip);
581 // Code generation for taken test: generate the code when requested or otherwise analyze
582 // if code generation is feasible when taken test is needed.
583 if (taken_test != nullptr) {
584 return GenerateCode(trip->op_b, nullptr, graph, block, taken_test, in_body, /* is_min */ false);
585 } else if (*needs_taken_test) {
586 if (!GenerateCode(
587 trip->op_b, nullptr, nullptr, nullptr, nullptr, in_body, /* is_min */ false)) {
588 return false;
589 }
590 }
591 // Code generation for lower and upper.
592 return
593 // Success on lower if invariant (not set), or code can be generated.
594 ((info->induction_class == HInductionVarAnalysis::kInvariant) ||
595 GenerateCode(info, trip, graph, block, lower, in_body, /* is_min */ true)) &&
596 // And success on upper.
597 GenerateCode(info, trip, graph, block, upper, in_body, /* is_min */ false);
Aart Bikaec3cce2015-10-14 17:44:55 -0700598}
599
600bool InductionVarRange::GenerateCode(HInductionVarAnalysis::InductionInfo* info,
601 HInductionVarAnalysis::InductionInfo* trip,
602 HGraph* graph, // when set, code is generated
603 HBasicBlock* block,
604 /*out*/HInstruction** result,
605 bool in_body,
Aart Bik7d57d7f2015-12-09 14:39:48 -0800606 bool is_min) const {
Aart Bikaec3cce2015-10-14 17:44:55 -0700607 if (info != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -0700608 // Handle current operation.
Aart Bikaec3cce2015-10-14 17:44:55 -0700609 Primitive::Type type = Primitive::kPrimInt;
610 HInstruction* opa = nullptr;
611 HInstruction* opb = nullptr;
Aart Bikaec3cce2015-10-14 17:44:55 -0700612 switch (info->induction_class) {
613 case HInductionVarAnalysis::kInvariant:
614 // Invariants.
615 switch (info->operation) {
616 case HInductionVarAnalysis::kAdd:
Aart Bik389b3db2015-10-28 14:23:40 -0700617 case HInductionVarAnalysis::kLT:
618 case HInductionVarAnalysis::kLE:
619 case HInductionVarAnalysis::kGT:
620 case HInductionVarAnalysis::kGE:
Aart Bikaec3cce2015-10-14 17:44:55 -0700621 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
622 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
623 if (graph != nullptr) {
Aart Bik389b3db2015-10-28 14:23:40 -0700624 HInstruction* operation = nullptr;
625 switch (info->operation) {
626 case HInductionVarAnalysis::kAdd:
627 operation = new (graph->GetArena()) HAdd(type, opa, opb); break;
628 case HInductionVarAnalysis::kLT:
629 operation = new (graph->GetArena()) HLessThan(opa, opb); break;
630 case HInductionVarAnalysis::kLE:
631 operation = new (graph->GetArena()) HLessThanOrEqual(opa, opb); break;
632 case HInductionVarAnalysis::kGT:
633 operation = new (graph->GetArena()) HGreaterThan(opa, opb); break;
634 case HInductionVarAnalysis::kGE:
635 operation = new (graph->GetArena()) HGreaterThanOrEqual(opa, opb); break;
636 default:
637 LOG(FATAL) << "unknown operation";
638 }
639 *result = Insert(block, operation);
Aart Bikaec3cce2015-10-14 17:44:55 -0700640 }
641 return true;
642 }
643 break;
644 case HInductionVarAnalysis::kSub: // second reversed!
645 if (GenerateCode(info->op_a, trip, graph, block, &opa, in_body, is_min) &&
646 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
647 if (graph != nullptr) {
648 *result = Insert(block, new (graph->GetArena()) HSub(type, opa, opb));
649 }
650 return true;
651 }
652 break;
653 case HInductionVarAnalysis::kNeg: // reversed!
654 if (GenerateCode(info->op_b, trip, graph, block, &opb, in_body, !is_min)) {
655 if (graph != nullptr) {
656 *result = Insert(block, new (graph->GetArena()) HNeg(type, opb));
657 }
658 return true;
659 }
660 break;
661 case HInductionVarAnalysis::kFetch:
Aart Bik4a342772015-11-30 10:17:46 -0800662 if (info->fetch->GetType() == type) {
663 if (graph != nullptr) {
664 *result = info->fetch; // already in HIR
665 }
666 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -0700667 }
Aart Bik4a342772015-11-30 10:17:46 -0800668 break;
Aart Bikaec3cce2015-10-14 17:44:55 -0700669 case HInductionVarAnalysis::kTripCountInLoop:
Aart Bik389b3db2015-10-28 14:23:40 -0700670 case HInductionVarAnalysis::kTripCountInLoopUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700671 if (!in_body && !is_min) { // one extra!
Aart Bik22f05872015-10-27 15:56:28 -0700672 return GenerateCode(info->op_a, trip, graph, block, result, in_body, is_min);
Aart Bikaec3cce2015-10-14 17:44:55 -0700673 }
674 FALLTHROUGH_INTENDED;
675 case HInductionVarAnalysis::kTripCountInBody:
Aart Bik389b3db2015-10-28 14:23:40 -0700676 case HInductionVarAnalysis::kTripCountInBodyUnsafe:
Aart Bikaec3cce2015-10-14 17:44:55 -0700677 if (is_min) {
678 if (graph != nullptr) {
679 *result = graph->GetIntConstant(0);
680 }
681 return true;
682 } else if (in_body) {
Aart Bik22f05872015-10-27 15:56:28 -0700683 if (GenerateCode(info->op_a, trip, graph, block, &opb, in_body, is_min)) {
Aart Bikaec3cce2015-10-14 17:44:55 -0700684 if (graph != nullptr) {
685 *result = Insert(block,
686 new (graph->GetArena())
687 HSub(type, opb, graph->GetIntConstant(1)));
688 }
689 return true;
690 }
691 }
692 break;
693 default:
694 break;
695 }
696 break;
Aart Bik389b3db2015-10-28 14:23:40 -0700697 case HInductionVarAnalysis::kLinear: {
Aart Bik4a342772015-11-30 10:17:46 -0800698 // Linear induction a * i + b, for normalized 0 <= i < TC. Restrict to unit stride only
699 // to avoid arithmetic wrap-around situations that are hard to guard against.
Aart Bik97412c922016-02-19 20:14:38 -0800700 int64_t stride_value = 0;
701 if (IsConstant(info->op_a, kExact, &stride_value)) {
Aart Bik4a342772015-11-30 10:17:46 -0800702 if (stride_value == 1 || stride_value == -1) {
703 const bool is_min_a = stride_value == 1 ? is_min : !is_min;
704 if (GenerateCode(trip, trip, graph, block, &opa, in_body, is_min_a) &&
705 GenerateCode(info->op_b, trip, graph, block, &opb, in_body, is_min)) {
706 if (graph != nullptr) {
707 HInstruction* oper;
708 if (stride_value == 1) {
709 oper = new (graph->GetArena()) HAdd(type, opa, opb);
710 } else {
711 oper = new (graph->GetArena()) HSub(type, opb, opa);
Aart Bik389b3db2015-10-28 14:23:40 -0700712 }
Aart Bik4a342772015-11-30 10:17:46 -0800713 *result = Insert(block, oper);
Aart Bikaec3cce2015-10-14 17:44:55 -0700714 }
Aart Bik4a342772015-11-30 10:17:46 -0800715 return true;
Aart Bikaec3cce2015-10-14 17:44:55 -0700716 }
717 }
718 }
719 break;
Aart Bik4a342772015-11-30 10:17:46 -0800720 }
721 case HInductionVarAnalysis::kWrapAround:
722 case HInductionVarAnalysis::kPeriodic: {
723 // Wrap-around and periodic inductions are restricted to constants only, so that extreme
724 // values are easy to test at runtime without complications of arithmetic wrap-around.
725 Value extreme = GetVal(info, trip, in_body, is_min);
Aart Bik97412c922016-02-19 20:14:38 -0800726 if (IsConstantValue(extreme)) {
Aart Bik4a342772015-11-30 10:17:46 -0800727 if (graph != nullptr) {
728 *result = graph->GetIntConstant(extreme.b_constant);
729 }
730 return true;
731 }
732 break;
733 }
Aart Bik389b3db2015-10-28 14:23:40 -0700734 default:
Aart Bikaec3cce2015-10-14 17:44:55 -0700735 break;
736 }
737 }
738 return false;
739}
740
Aart Bikd14c5952015-09-08 15:25:15 -0700741} // namespace art