blob: 811a3bdf0c491ba72a1c3932563c3a93041eed4d [file] [log] [blame]
Mingyao Yangf384f882014-10-22 16:08:18 -07001/*
2 * Copyright (C) 2014 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
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_containers.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070018#include "bounds_check_elimination.h"
19#include "nodes.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070020
21namespace art {
22
23class MonotonicValueRange;
24
25/**
26 * A value bound is represented as a pair of value and constant,
27 * e.g. array.length - 1.
28 */
29class ValueBound : public ValueObject {
30 public:
Mingyao Yang0304e182015-01-30 16:41:29 -080031 ValueBound(HInstruction* instruction, int32_t constant) {
Mingyao Yang64197522014-12-05 15:56:23 -080032 if (instruction != nullptr && instruction->IsIntConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -080033 // Normalize ValueBound with constant instruction.
34 int32_t instr_const = instruction->AsIntConstant()->GetValue();
Mingyao Yang8c8bad82015-02-09 18:13:26 -080035 if (!WouldAddOverflowOrUnderflow(instr_const, constant)) {
Mingyao Yang64197522014-12-05 15:56:23 -080036 instruction_ = nullptr;
37 constant_ = instr_const + constant;
38 return;
39 }
Mingyao Yangf384f882014-10-22 16:08:18 -070040 }
Mingyao Yang64197522014-12-05 15:56:23 -080041 instruction_ = instruction;
42 constant_ = constant;
43 }
44
Mingyao Yang8c8bad82015-02-09 18:13:26 -080045 // Return whether (left + right) overflows or underflows.
46 static bool WouldAddOverflowOrUnderflow(int32_t left, int32_t right) {
47 if (right == 0) {
48 return false;
49 }
50 if ((right > 0) && (left <= INT_MAX - right)) {
51 // No overflow.
52 return false;
53 }
54 if ((right < 0) && (left >= INT_MIN - right)) {
55 // No underflow.
56 return false;
57 }
58 return true;
59 }
60
Mingyao Yang0304e182015-01-30 16:41:29 -080061 static bool IsAddOrSubAConstant(HInstruction* instruction,
62 HInstruction** left_instruction,
63 int* right_constant) {
64 if (instruction->IsAdd() || instruction->IsSub()) {
65 HBinaryOperation* bin_op = instruction->AsBinaryOperation();
66 HInstruction* left = bin_op->GetLeft();
67 HInstruction* right = bin_op->GetRight();
68 if (right->IsIntConstant()) {
69 *left_instruction = left;
70 int32_t c = right->AsIntConstant()->GetValue();
71 *right_constant = instruction->IsAdd() ? c : -c;
72 return true;
73 }
74 }
75 *left_instruction = nullptr;
76 *right_constant = 0;
77 return false;
78 }
79
Mingyao Yang64197522014-12-05 15:56:23 -080080 // Try to detect useful value bound format from an instruction, e.g.
81 // a constant or array length related value.
82 static ValueBound DetectValueBoundFromValue(HInstruction* instruction, bool* found) {
83 DCHECK(instruction != nullptr);
Mingyao Yangf384f882014-10-22 16:08:18 -070084 if (instruction->IsIntConstant()) {
Mingyao Yang64197522014-12-05 15:56:23 -080085 *found = true;
86 return ValueBound(nullptr, instruction->AsIntConstant()->GetValue());
Mingyao Yangf384f882014-10-22 16:08:18 -070087 }
Mingyao Yang64197522014-12-05 15:56:23 -080088
89 if (instruction->IsArrayLength()) {
90 *found = true;
91 return ValueBound(instruction, 0);
92 }
93 // Try to detect (array.length + c) format.
Mingyao Yang0304e182015-01-30 16:41:29 -080094 HInstruction *left;
95 int32_t right;
96 if (IsAddOrSubAConstant(instruction, &left, &right)) {
97 if (left->IsArrayLength()) {
Mingyao Yang64197522014-12-05 15:56:23 -080098 *found = true;
Mingyao Yang0304e182015-01-30 16:41:29 -080099 return ValueBound(left, right);
Mingyao Yang64197522014-12-05 15:56:23 -0800100 }
101 }
102
103 // No useful bound detected.
104 *found = false;
105 return ValueBound::Max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700106 }
107
108 HInstruction* GetInstruction() const { return instruction_; }
Mingyao Yang0304e182015-01-30 16:41:29 -0800109 int32_t GetConstant() const { return constant_; }
Mingyao Yangf384f882014-10-22 16:08:18 -0700110
Mingyao Yang0304e182015-01-30 16:41:29 -0800111 bool IsRelatedToArrayLength() const {
112 // Some bounds are created with HNewArray* as the instruction instead
113 // of HArrayLength*. They are treated the same.
114 return (instruction_ != nullptr) &&
115 (instruction_->IsArrayLength() || instruction_->IsNewArray());
Mingyao Yangf384f882014-10-22 16:08:18 -0700116 }
117
118 bool IsConstant() const {
119 return instruction_ == nullptr;
120 }
121
122 static ValueBound Min() { return ValueBound(nullptr, INT_MIN); }
123 static ValueBound Max() { return ValueBound(nullptr, INT_MAX); }
124
125 bool Equals(ValueBound bound) const {
126 return instruction_ == bound.instruction_ && constant_ == bound.constant_;
127 }
128
Mingyao Yang0304e182015-01-30 16:41:29 -0800129 static HInstruction* FromArrayLengthToNewArrayIfPossible(HInstruction* instruction) {
130 // Null check on the NewArray should have been eliminated by instruction
131 // simplifier already.
132 if (instruction->IsArrayLength() && instruction->InputAt(0)->IsNewArray()) {
133 return instruction->InputAt(0)->AsNewArray();
134 }
135 return instruction;
136 }
137
138 static bool Equal(HInstruction* instruction1, HInstruction* instruction2) {
139 if (instruction1 == instruction2) {
140 return true;
141 }
142
143 if (instruction1 == nullptr || instruction2 == nullptr) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700144 return false;
145 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800146
147 // Some bounds are created with HNewArray* as the instruction instead
148 // of HArrayLength*. They are treated the same.
149 instruction1 = FromArrayLengthToNewArrayIfPossible(instruction1);
150 instruction2 = FromArrayLengthToNewArrayIfPossible(instruction2);
151 return instruction1 == instruction2;
152 }
153
154 // Returns if it's certain this->bound >= `bound`.
155 bool GreaterThanOrEqualTo(ValueBound bound) const {
156 if (Equal(instruction_, bound.instruction_)) {
157 return constant_ >= bound.constant_;
158 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700159 // Not comparable. Just return false.
160 return false;
161 }
162
Mingyao Yang0304e182015-01-30 16:41:29 -0800163 // Returns if it's certain this->bound <= `bound`.
164 bool LessThanOrEqualTo(ValueBound bound) const {
165 if (Equal(instruction_, bound.instruction_)) {
166 return constant_ <= bound.constant_;
Mingyao Yangf384f882014-10-22 16:08:18 -0700167 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700168 // Not comparable. Just return false.
169 return false;
170 }
171
172 // Try to narrow lower bound. Returns the greatest of the two if possible.
173 // Pick one if they are not comparable.
174 static ValueBound NarrowLowerBound(ValueBound bound1, ValueBound bound2) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800175 if (bound1.GreaterThanOrEqualTo(bound2)) {
176 return bound1;
177 }
178 if (bound2.GreaterThanOrEqualTo(bound1)) {
179 return bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700180 }
181
182 // Not comparable. Just pick one. We may lose some info, but that's ok.
183 // Favor constant as lower bound.
184 return bound1.IsConstant() ? bound1 : bound2;
185 }
186
187 // Try to narrow upper bound. Returns the lowest of the two if possible.
188 // Pick one if they are not comparable.
189 static ValueBound NarrowUpperBound(ValueBound bound1, ValueBound bound2) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800190 if (bound1.LessThanOrEqualTo(bound2)) {
191 return bound1;
192 }
193 if (bound2.LessThanOrEqualTo(bound1)) {
194 return bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700195 }
196
197 // Not comparable. Just pick one. We may lose some info, but that's ok.
198 // Favor array length as upper bound.
Mingyao Yang0304e182015-01-30 16:41:29 -0800199 return bound1.IsRelatedToArrayLength() ? bound1 : bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700200 }
201
Mingyao Yang0304e182015-01-30 16:41:29 -0800202 // Add a constant to a ValueBound.
203 // `overflow` or `underflow` will return whether the resulting bound may
204 // overflow or underflow an int.
205 ValueBound Add(int32_t c, bool* overflow, bool* underflow) const {
206 *overflow = *underflow = false;
Mingyao Yangf384f882014-10-22 16:08:18 -0700207 if (c == 0) {
208 return *this;
209 }
210
Mingyao Yang0304e182015-01-30 16:41:29 -0800211 int32_t new_constant;
Mingyao Yangf384f882014-10-22 16:08:18 -0700212 if (c > 0) {
213 if (constant_ > INT_MAX - c) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800214 *overflow = true;
Mingyao Yang64197522014-12-05 15:56:23 -0800215 return Max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700216 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800217
218 new_constant = constant_ + c;
219 // (array.length + non-positive-constant) won't overflow an int.
220 if (IsConstant() || (IsRelatedToArrayLength() && new_constant <= 0)) {
221 return ValueBound(instruction_, new_constant);
222 }
223 // Be conservative.
224 *overflow = true;
225 return Max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700226 } else {
227 if (constant_ < INT_MIN - c) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800228 *underflow = true;
229 return Min();
Mingyao Yangf384f882014-10-22 16:08:18 -0700230 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800231
232 new_constant = constant_ + c;
233 // Regardless of the value new_constant, (array.length+new_constant) will
234 // never underflow since array.length is no less than 0.
235 if (IsConstant() || IsRelatedToArrayLength()) {
236 return ValueBound(instruction_, new_constant);
237 }
238 // Be conservative.
239 *underflow = true;
240 return Min();
Mingyao Yangf384f882014-10-22 16:08:18 -0700241 }
242 return ValueBound(instruction_, new_constant);
243 }
244
245 private:
Mingyao Yangf384f882014-10-22 16:08:18 -0700246 HInstruction* instruction_;
Mingyao Yang0304e182015-01-30 16:41:29 -0800247 int32_t constant_;
Mingyao Yangf384f882014-10-22 16:08:18 -0700248};
249
250/**
251 * Represent a range of lower bound and upper bound, both being inclusive.
252 * Currently a ValueRange may be generated as a result of the following:
253 * comparisons related to array bounds, array bounds check, add/sub on top
Mingyao Yang0304e182015-01-30 16:41:29 -0800254 * of an existing value range, NewArray or a loop phi corresponding to an
Mingyao Yangf384f882014-10-22 16:08:18 -0700255 * incrementing/decrementing array index (MonotonicValueRange).
256 */
257class ValueRange : public ArenaObject<kArenaAllocMisc> {
258 public:
259 ValueRange(ArenaAllocator* allocator, ValueBound lower, ValueBound upper)
260 : allocator_(allocator), lower_(lower), upper_(upper) {}
261
262 virtual ~ValueRange() {}
263
264 virtual const MonotonicValueRange* AsMonotonicValueRange() const { return nullptr; }
265 bool IsMonotonicValueRange() const {
266 return AsMonotonicValueRange() != nullptr;
267 }
268
269 ArenaAllocator* GetAllocator() const { return allocator_; }
270 ValueBound GetLower() const { return lower_; }
271 ValueBound GetUpper() const { return upper_; }
272
273 // If it's certain that this value range fits in other_range.
274 virtual bool FitsIn(ValueRange* other_range) const {
275 if (other_range == nullptr) {
276 return true;
277 }
278 DCHECK(!other_range->IsMonotonicValueRange());
Mingyao Yang0304e182015-01-30 16:41:29 -0800279 return lower_.GreaterThanOrEqualTo(other_range->lower_) &&
280 upper_.LessThanOrEqualTo(other_range->upper_);
Mingyao Yangf384f882014-10-22 16:08:18 -0700281 }
282
283 // Returns the intersection of this and range.
284 // If it's not possible to do intersection because some
285 // bounds are not comparable, it's ok to pick either bound.
286 virtual ValueRange* Narrow(ValueRange* range) {
287 if (range == nullptr) {
288 return this;
289 }
290
291 if (range->IsMonotonicValueRange()) {
292 return this;
293 }
294
295 return new (allocator_) ValueRange(
296 allocator_,
297 ValueBound::NarrowLowerBound(lower_, range->lower_),
298 ValueBound::NarrowUpperBound(upper_, range->upper_));
299 }
300
Mingyao Yang0304e182015-01-30 16:41:29 -0800301 // Shift a range by a constant.
302 ValueRange* Add(int32_t constant) const {
303 bool overflow, underflow;
304 ValueBound lower = lower_.Add(constant, &overflow, &underflow);
305 if (underflow) {
306 // Lower bound underflow will wrap around to positive values
307 // and invalidate the upper bound.
308 return nullptr;
Mingyao Yangf384f882014-10-22 16:08:18 -0700309 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800310 ValueBound upper = upper_.Add(constant, &overflow, &underflow);
311 if (overflow) {
312 // Upper bound overflow will wrap around to negative values
313 // and invalidate the lower bound.
314 return nullptr;
Mingyao Yangf384f882014-10-22 16:08:18 -0700315 }
316 return new (allocator_) ValueRange(allocator_, lower, upper);
317 }
318
Mingyao Yangf384f882014-10-22 16:08:18 -0700319 private:
320 ArenaAllocator* const allocator_;
321 const ValueBound lower_; // inclusive
322 const ValueBound upper_; // inclusive
323
324 DISALLOW_COPY_AND_ASSIGN(ValueRange);
325};
326
327/**
328 * A monotonically incrementing/decrementing value range, e.g.
329 * the variable i in "for (int i=0; i<array.length; i++)".
330 * Special care needs to be taken to account for overflow/underflow
331 * of such value ranges.
332 */
333class MonotonicValueRange : public ValueRange {
334 public:
Mingyao Yang64197522014-12-05 15:56:23 -0800335 MonotonicValueRange(ArenaAllocator* allocator,
336 HInstruction* initial,
Mingyao Yang0304e182015-01-30 16:41:29 -0800337 int32_t increment,
Mingyao Yang64197522014-12-05 15:56:23 -0800338 ValueBound bound)
339 // To be conservative, give it full range [INT_MIN, INT_MAX] in case it's
340 // used as a regular value range, due to possible overflow/underflow.
341 : ValueRange(allocator, ValueBound::Min(), ValueBound::Max()),
342 initial_(initial),
343 increment_(increment),
344 bound_(bound) {}
Mingyao Yangf384f882014-10-22 16:08:18 -0700345
346 virtual ~MonotonicValueRange() {}
347
348 const MonotonicValueRange* AsMonotonicValueRange() const OVERRIDE { return this; }
349
350 // If it's certain that this value range fits in other_range.
351 bool FitsIn(ValueRange* other_range) const OVERRIDE {
352 if (other_range == nullptr) {
353 return true;
354 }
355 DCHECK(!other_range->IsMonotonicValueRange());
356 return false;
357 }
358
359 // Try to narrow this MonotonicValueRange given another range.
360 // Ideally it will return a normal ValueRange. But due to
361 // possible overflow/underflow, that may not be possible.
362 ValueRange* Narrow(ValueRange* range) OVERRIDE {
363 if (range == nullptr) {
364 return this;
365 }
366 DCHECK(!range->IsMonotonicValueRange());
367
368 if (increment_ > 0) {
369 // Monotonically increasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800370 ValueBound lower = ValueBound::NarrowLowerBound(bound_, range->GetLower());
Mingyao Yangf384f882014-10-22 16:08:18 -0700371
372 // We currently conservatively assume max array length is INT_MAX. If we can
373 // make assumptions about the max array length, e.g. due to the max heap size,
374 // divided by the element size (such as 4 bytes for each integer array), we can
375 // lower this number and rule out some possible overflows.
Mingyao Yang0304e182015-01-30 16:41:29 -0800376 int32_t max_array_len = INT_MAX;
Mingyao Yangf384f882014-10-22 16:08:18 -0700377
Mingyao Yang0304e182015-01-30 16:41:29 -0800378 // max possible integer value of range's upper value.
379 int32_t upper = INT_MAX;
380 // Try to lower upper.
381 ValueBound upper_bound = range->GetUpper();
382 if (upper_bound.IsConstant()) {
383 upper = upper_bound.GetConstant();
384 } else if (upper_bound.IsRelatedToArrayLength() && upper_bound.GetConstant() <= 0) {
385 // Normal case. e.g. <= array.length - 1.
386 upper = max_array_len + upper_bound.GetConstant();
Mingyao Yangf384f882014-10-22 16:08:18 -0700387 }
388
389 // If we can prove for the last number in sequence of initial_,
390 // initial_ + increment_, initial_ + 2 x increment_, ...
391 // that's <= upper, (last_num_in_sequence + increment_) doesn't trigger overflow,
392 // then this MonoticValueRange is narrowed to a normal value range.
393
394 // Be conservative first, assume last number in the sequence hits upper.
Mingyao Yang0304e182015-01-30 16:41:29 -0800395 int32_t last_num_in_sequence = upper;
Mingyao Yangf384f882014-10-22 16:08:18 -0700396 if (initial_->IsIntConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800397 int32_t initial_constant = initial_->AsIntConstant()->GetValue();
Mingyao Yangf384f882014-10-22 16:08:18 -0700398 if (upper <= initial_constant) {
399 last_num_in_sequence = upper;
400 } else {
Mingyao Yang0304e182015-01-30 16:41:29 -0800401 // Cast to int64_t for the substraction part to avoid int32_t overflow.
Mingyao Yangf384f882014-10-22 16:08:18 -0700402 last_num_in_sequence = initial_constant +
403 ((int64_t)upper - (int64_t)initial_constant) / increment_ * increment_;
404 }
405 }
406 if (last_num_in_sequence <= INT_MAX - increment_) {
407 // No overflow. The sequence will be stopped by the upper bound test as expected.
408 return new (GetAllocator()) ValueRange(GetAllocator(), lower, range->GetUpper());
409 }
410
411 // There might be overflow. Give up narrowing.
412 return this;
413 } else {
414 DCHECK_NE(increment_, 0);
415 // Monotonically decreasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800416 ValueBound upper = ValueBound::NarrowUpperBound(bound_, range->GetUpper());
Mingyao Yangf384f882014-10-22 16:08:18 -0700417
418 // Need to take care of underflow. Try to prove underflow won't happen
Mingyao Yang0304e182015-01-30 16:41:29 -0800419 // for common cases.
Mingyao Yangf384f882014-10-22 16:08:18 -0700420 if (range->GetLower().IsConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800421 int32_t constant = range->GetLower().GetConstant();
Mingyao Yangf384f882014-10-22 16:08:18 -0700422 if (constant >= INT_MIN - increment_) {
423 return new (GetAllocator()) ValueRange(GetAllocator(), range->GetLower(), upper);
424 }
425 }
426
Mingyao Yang0304e182015-01-30 16:41:29 -0800427 // For non-constant lower bound, just assume might be underflow. Give up narrowing.
Mingyao Yangf384f882014-10-22 16:08:18 -0700428 return this;
429 }
430 }
431
432 private:
Mingyao Yangf384f882014-10-22 16:08:18 -0700433 HInstruction* const initial_;
Mingyao Yang0304e182015-01-30 16:41:29 -0800434 const int32_t increment_;
Mingyao Yang64197522014-12-05 15:56:23 -0800435 ValueBound bound_; // Additional value bound info for initial_;
Mingyao Yangf384f882014-10-22 16:08:18 -0700436
437 DISALLOW_COPY_AND_ASSIGN(MonotonicValueRange);
438};
439
440class BCEVisitor : public HGraphVisitor {
441 public:
Andreas Gampe0418b5b2014-12-04 17:24:50 -0800442 explicit BCEVisitor(HGraph* graph)
Mingyao Yangf384f882014-10-22 16:08:18 -0700443 : HGraphVisitor(graph),
444 maps_(graph->GetBlocks().Size()) {}
445
446 private:
447 // Return the map of proven value ranges at the beginning of a basic block.
448 ArenaSafeMap<int, ValueRange*>* GetValueRangeMap(HBasicBlock* basic_block) {
449 int block_id = basic_block->GetBlockId();
450 if (maps_.at(block_id) == nullptr) {
451 std::unique_ptr<ArenaSafeMap<int, ValueRange*>> map(
452 new ArenaSafeMap<int, ValueRange*>(
453 std::less<int>(), GetGraph()->GetArena()->Adapter()));
454 maps_.at(block_id) = std::move(map);
455 }
456 return maps_.at(block_id).get();
457 }
458
459 // Traverse up the dominator tree to look for value range info.
460 ValueRange* LookupValueRange(HInstruction* instruction, HBasicBlock* basic_block) {
461 while (basic_block != nullptr) {
462 ArenaSafeMap<int, ValueRange*>* map = GetValueRangeMap(basic_block);
463 if (map->find(instruction->GetId()) != map->end()) {
464 return map->Get(instruction->GetId());
465 }
466 basic_block = basic_block->GetDominator();
467 }
468 // Didn't find any.
469 return nullptr;
470 }
471
Mingyao Yang0304e182015-01-30 16:41:29 -0800472 // Narrow the value range of `instruction` at the end of `basic_block` with `range`,
473 // and push the narrowed value range to `successor`.
Mingyao Yangf384f882014-10-22 16:08:18 -0700474 void ApplyRangeFromComparison(HInstruction* instruction, HBasicBlock* basic_block,
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800475 HBasicBlock* successor, ValueRange* range) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700476 ValueRange* existing_range = LookupValueRange(instruction, basic_block);
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800477 if (existing_range == nullptr) {
478 if (range != nullptr) {
479 GetValueRangeMap(successor)->Overwrite(instruction->GetId(), range);
480 }
481 return;
482 }
483 if (existing_range->IsMonotonicValueRange()) {
484 DCHECK(instruction->IsLoopHeaderPhi());
485 // Make sure the comparison is in the loop header so each increment is
486 // checked with a comparison.
487 if (instruction->GetBlock() != basic_block) {
488 return;
489 }
490 }
491 ValueRange* narrowed_range = existing_range->Narrow(range);
Mingyao Yangf384f882014-10-22 16:08:18 -0700492 if (narrowed_range != nullptr) {
493 GetValueRangeMap(successor)->Overwrite(instruction->GetId(), narrowed_range);
494 }
495 }
496
497 // Handle "if (left cmp_cond right)".
498 void HandleIf(HIf* instruction, HInstruction* left, HInstruction* right, IfCondition cond) {
499 HBasicBlock* block = instruction->GetBlock();
500
501 HBasicBlock* true_successor = instruction->IfTrueSuccessor();
502 // There should be no critical edge at this point.
503 DCHECK_EQ(true_successor->GetPredecessors().Size(), 1u);
504
505 HBasicBlock* false_successor = instruction->IfFalseSuccessor();
506 // There should be no critical edge at this point.
507 DCHECK_EQ(false_successor->GetPredecessors().Size(), 1u);
508
Mingyao Yang64197522014-12-05 15:56:23 -0800509 bool found;
510 ValueBound bound = ValueBound::DetectValueBoundFromValue(right, &found);
Mingyao Yang0304e182015-01-30 16:41:29 -0800511 // Each comparison can establish a lower bound and an upper bound
512 // for the left hand side.
Mingyao Yangf384f882014-10-22 16:08:18 -0700513 ValueBound lower = bound;
514 ValueBound upper = bound;
515 if (!found) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800516 // No constant or array.length+c format bound found.
Mingyao Yangf384f882014-10-22 16:08:18 -0700517 // For i<j, we can still use j's upper bound as i's upper bound. Same for lower.
518 ValueRange* range = LookupValueRange(right, block);
519 if (range != nullptr) {
520 lower = range->GetLower();
521 upper = range->GetUpper();
522 } else {
523 lower = ValueBound::Min();
524 upper = ValueBound::Max();
525 }
526 }
527
Mingyao Yang0304e182015-01-30 16:41:29 -0800528 bool overflow, underflow;
Mingyao Yangf384f882014-10-22 16:08:18 -0700529 if (cond == kCondLT || cond == kCondLE) {
530 if (!upper.Equals(ValueBound::Max())) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800531 int32_t compensation = (cond == kCondLT) ? -1 : 0; // upper bound is inclusive
532 ValueBound new_upper = upper.Add(compensation, &overflow, &underflow);
533 if (overflow || underflow) {
534 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800535 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700536 ValueRange* new_range = new (GetGraph()->GetArena())
537 ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper);
538 ApplyRangeFromComparison(left, block, true_successor, new_range);
539 }
540
541 // array.length as a lower bound isn't considered useful.
Mingyao Yang0304e182015-01-30 16:41:29 -0800542 if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) {
543 int32_t compensation = (cond == kCondLE) ? 1 : 0; // lower bound is inclusive
544 ValueBound new_lower = lower.Add(compensation, &overflow, &underflow);
545 if (overflow || underflow) {
546 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800547 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700548 ValueRange* new_range = new (GetGraph()->GetArena())
549 ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max());
550 ApplyRangeFromComparison(left, block, false_successor, new_range);
551 }
552 } else if (cond == kCondGT || cond == kCondGE) {
553 // array.length as a lower bound isn't considered useful.
Mingyao Yang0304e182015-01-30 16:41:29 -0800554 if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) {
555 int32_t compensation = (cond == kCondGT) ? 1 : 0; // lower bound is inclusive
556 ValueBound new_lower = lower.Add(compensation, &overflow, &underflow);
557 if (overflow || underflow) {
558 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800559 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700560 ValueRange* new_range = new (GetGraph()->GetArena())
561 ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max());
562 ApplyRangeFromComparison(left, block, true_successor, new_range);
563 }
564
565 if (!upper.Equals(ValueBound::Max())) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800566 int32_t compensation = (cond == kCondGE) ? -1 : 0; // upper bound is inclusive
567 ValueBound new_upper = upper.Add(compensation, &overflow, &underflow);
568 if (overflow || underflow) {
569 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800570 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700571 ValueRange* new_range = new (GetGraph()->GetArena())
572 ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper);
573 ApplyRangeFromComparison(left, block, false_successor, new_range);
574 }
575 }
576 }
577
578 void VisitBoundsCheck(HBoundsCheck* bounds_check) {
579 HBasicBlock* block = bounds_check->GetBlock();
580 HInstruction* index = bounds_check->InputAt(0);
581 HInstruction* array_length = bounds_check->InputAt(1);
Mingyao Yang0304e182015-01-30 16:41:29 -0800582 DCHECK(array_length->IsIntConstant() || array_length->IsArrayLength());
Mingyao Yangf384f882014-10-22 16:08:18 -0700583
Mingyao Yang0304e182015-01-30 16:41:29 -0800584 if (!index->IsIntConstant()) {
585 ValueRange* index_range = LookupValueRange(index, block);
586 if (index_range != nullptr) {
587 ValueBound lower = ValueBound(nullptr, 0); // constant 0
588 ValueBound upper = ValueBound(array_length, -1); // array_length - 1
589 ValueRange* array_range = new (GetGraph()->GetArena())
590 ValueRange(GetGraph()->GetArena(), lower, upper);
591 if (index_range->FitsIn(array_range)) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700592 ReplaceBoundsCheck(bounds_check, index);
593 return;
594 }
595 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800596 } else {
597 int32_t constant = index->AsIntConstant()->GetValue();
598 if (constant < 0) {
599 // Will always throw exception.
600 return;
601 }
602 if (array_length->IsIntConstant()) {
603 if (constant < array_length->AsIntConstant()->GetValue()) {
604 ReplaceBoundsCheck(bounds_check, index);
605 }
606 return;
607 }
608
609 DCHECK(array_length->IsArrayLength());
610 ValueRange* existing_range = LookupValueRange(array_length, block);
611 if (existing_range != nullptr) {
612 ValueBound lower = existing_range->GetLower();
613 DCHECK(lower.IsConstant());
614 if (constant < lower.GetConstant()) {
615 ReplaceBoundsCheck(bounds_check, index);
616 return;
617 } else {
618 // Existing range isn't strong enough to eliminate the bounds check.
619 // Fall through to update the array_length range with info from this
620 // bounds check.
621 }
622 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700623
624 // Once we have an array access like 'array[5] = 1', we record array.length >= 6.
Mingyao Yang0304e182015-01-30 16:41:29 -0800625 // We currently don't do it for non-constant index since a valid array[i] can't prove
626 // a valid array[i-1] yet due to the lower bound side.
Mingyao Yang64197522014-12-05 15:56:23 -0800627 ValueBound lower = ValueBound(nullptr, constant + 1);
Mingyao Yangf384f882014-10-22 16:08:18 -0700628 ValueBound upper = ValueBound::Max();
629 ValueRange* range = new (GetGraph()->GetArena())
630 ValueRange(GetGraph()->GetArena(), lower, upper);
Mingyao Yang0304e182015-01-30 16:41:29 -0800631 GetValueRangeMap(block)->Overwrite(array_length->GetId(), range);
Mingyao Yangf384f882014-10-22 16:08:18 -0700632 }
633 }
634
635 void ReplaceBoundsCheck(HInstruction* bounds_check, HInstruction* index) {
636 bounds_check->ReplaceWith(index);
637 bounds_check->GetBlock()->RemoveInstruction(bounds_check);
638 }
639
640 void VisitPhi(HPhi* phi) {
641 if (phi->IsLoopHeaderPhi() && phi->GetType() == Primitive::kPrimInt) {
Andreas Gampe0418b5b2014-12-04 17:24:50 -0800642 DCHECK_EQ(phi->InputCount(), 2U);
Mingyao Yangf384f882014-10-22 16:08:18 -0700643 HInstruction* instruction = phi->InputAt(1);
Mingyao Yang0304e182015-01-30 16:41:29 -0800644 HInstruction *left;
645 int32_t increment;
646 if (ValueBound::IsAddOrSubAConstant(instruction, &left, &increment)) {
647 if (left == phi) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700648 HInstruction* initial_value = phi->InputAt(0);
649 ValueRange* range = nullptr;
Mingyao Yang64197522014-12-05 15:56:23 -0800650 if (increment == 0) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700651 // Add constant 0. It's really a fixed value.
652 range = new (GetGraph()->GetArena()) ValueRange(
653 GetGraph()->GetArena(),
Mingyao Yang64197522014-12-05 15:56:23 -0800654 ValueBound(initial_value, 0),
655 ValueBound(initial_value, 0));
Mingyao Yangf384f882014-10-22 16:08:18 -0700656 } else {
657 // Monotonically increasing/decreasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800658 bool found;
659 ValueBound bound = ValueBound::DetectValueBoundFromValue(
660 initial_value, &found);
661 if (!found) {
662 // No constant or array.length+c bound found.
663 // For i=j, we can still use j's upper bound as i's upper bound.
664 // Same for lower.
665 ValueRange* initial_range = LookupValueRange(initial_value, phi->GetBlock());
666 if (initial_range != nullptr) {
667 bound = increment > 0 ? initial_range->GetLower() :
668 initial_range->GetUpper();
669 } else {
670 bound = increment > 0 ? ValueBound::Min() : ValueBound::Max();
671 }
672 }
673 range = new (GetGraph()->GetArena()) MonotonicValueRange(
Mingyao Yangf384f882014-10-22 16:08:18 -0700674 GetGraph()->GetArena(),
675 initial_value,
Mingyao Yang64197522014-12-05 15:56:23 -0800676 increment,
677 bound);
Mingyao Yangf384f882014-10-22 16:08:18 -0700678 }
679 GetValueRangeMap(phi->GetBlock())->Overwrite(phi->GetId(), range);
680 }
681 }
682 }
683 }
684
685 void VisitIf(HIf* instruction) {
686 if (instruction->InputAt(0)->IsCondition()) {
687 HCondition* cond = instruction->InputAt(0)->AsCondition();
688 IfCondition cmp = cond->GetCondition();
689 if (cmp == kCondGT || cmp == kCondGE ||
690 cmp == kCondLT || cmp == kCondLE) {
691 HInstruction* left = cond->GetLeft();
692 HInstruction* right = cond->GetRight();
693 HandleIf(instruction, left, right, cmp);
694 }
695 }
696 }
697
698 void VisitAdd(HAdd* add) {
699 HInstruction* right = add->GetRight();
700 if (right->IsIntConstant()) {
701 ValueRange* left_range = LookupValueRange(add->GetLeft(), add->GetBlock());
702 if (left_range == nullptr) {
703 return;
704 }
705 ValueRange* range = left_range->Add(right->AsIntConstant()->GetValue());
706 if (range != nullptr) {
707 GetValueRangeMap(add->GetBlock())->Overwrite(add->GetId(), range);
708 }
709 }
710 }
711
712 void VisitSub(HSub* sub) {
713 HInstruction* left = sub->GetLeft();
714 HInstruction* right = sub->GetRight();
715 if (right->IsIntConstant()) {
716 ValueRange* left_range = LookupValueRange(left, sub->GetBlock());
717 if (left_range == nullptr) {
718 return;
719 }
720 ValueRange* range = left_range->Add(-right->AsIntConstant()->GetValue());
721 if (range != nullptr) {
722 GetValueRangeMap(sub->GetBlock())->Overwrite(sub->GetId(), range);
723 return;
724 }
725 }
726
727 // Here we are interested in the typical triangular case of nested loops,
728 // such as the inner loop 'for (int j=0; j<array.length-i; j++)' where i
729 // is the index for outer loop. In this case, we know j is bounded by array.length-1.
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800730
731 // Try to handle (array.length - i) or (array.length + c - i) format.
732 HInstruction* left_of_left; // left input of left.
733 int32_t right_const = 0;
734 if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &right_const)) {
735 left = left_of_left;
736 }
737 // The value of left input of the sub equals (left + right_const).
738
Mingyao Yangf384f882014-10-22 16:08:18 -0700739 if (left->IsArrayLength()) {
740 HInstruction* array_length = left->AsArrayLength();
741 ValueRange* right_range = LookupValueRange(right, sub->GetBlock());
742 if (right_range != nullptr) {
743 ValueBound lower = right_range->GetLower();
744 ValueBound upper = right_range->GetUpper();
Mingyao Yang0304e182015-01-30 16:41:29 -0800745 if (lower.IsConstant() && upper.IsRelatedToArrayLength()) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700746 HInstruction* upper_inst = upper.GetInstruction();
Mingyao Yang0304e182015-01-30 16:41:29 -0800747 // Make sure it's the same array.
748 if (ValueBound::Equal(array_length, upper_inst)) {
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800749 int32_t c0 = right_const;
750 int32_t c1 = lower.GetConstant();
751 int32_t c2 = upper.GetConstant();
752 // (array.length + c0 - v) where v is in [c1, array.length + c2]
753 // gets [c0 - c2, array.length + c0 - c1] as its value range.
754 if (!ValueBound::WouldAddOverflowOrUnderflow(c0, -c2) &&
755 !ValueBound::WouldAddOverflowOrUnderflow(c0, -c1)) {
756 if ((c0 - c1) <= 0) {
757 // array.length + (c0 - c1) won't overflow/underflow.
758 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
759 GetGraph()->GetArena(),
760 ValueBound(nullptr, right_const - upper.GetConstant()),
761 ValueBound(array_length, right_const - lower.GetConstant()));
762 GetValueRangeMap(sub->GetBlock())->Overwrite(sub->GetId(), range);
763 }
764 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700765 }
766 }
767 }
768 }
769 }
770
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800771 void FindAndHandlePartialArrayLength(HBinaryOperation* instruction) {
772 DCHECK(instruction->IsDiv() || instruction->IsShr() || instruction->IsUShr());
773 HInstruction* right = instruction->GetRight();
774 int32_t right_const;
775 if (right->IsIntConstant()) {
776 right_const = right->AsIntConstant()->GetValue();
777 // Detect division by two or more.
778 if ((instruction->IsDiv() && right_const <= 1) ||
779 (instruction->IsShr() && right_const < 1) ||
780 (instruction->IsUShr() && right_const < 1)) {
781 return;
782 }
783 } else {
784 return;
785 }
786
787 // Try to handle array.length/2 or (array.length-1)/2 format.
788 HInstruction* left = instruction->GetLeft();
789 HInstruction* left_of_left; // left input of left.
790 int32_t c = 0;
791 if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &c)) {
792 left = left_of_left;
793 }
794 // The value of left input of instruction equals (left + c).
795
796 // (array_length + 1) or smaller divided by two or more
797 // always generate a value in [INT_MIN, array_length].
798 // This is true even if array_length is INT_MAX.
799 if (left->IsArrayLength() && c <= 1) {
800 if (instruction->IsUShr() && c < 0) {
801 // Make sure for unsigned shift, left side is not negative.
802 // e.g. if array_length is 2, ((array_length - 3) >>> 2) is way bigger
803 // than array_length.
804 return;
805 }
806 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
807 GetGraph()->GetArena(),
808 ValueBound(nullptr, INT_MIN),
809 ValueBound(left, 0));
810 GetValueRangeMap(instruction->GetBlock())->Overwrite(instruction->GetId(), range);
811 }
812 }
813
814 void VisitDiv(HDiv* div) {
815 FindAndHandlePartialArrayLength(div);
816 }
817
818 void VisitShr(HShr* shr) {
819 FindAndHandlePartialArrayLength(shr);
820 }
821
822 void VisitUShr(HUShr* ushr) {
823 FindAndHandlePartialArrayLength(ushr);
824 }
825
Mingyao Yang0304e182015-01-30 16:41:29 -0800826 void VisitNewArray(HNewArray* new_array) {
827 HInstruction* len = new_array->InputAt(0);
828 if (!len->IsIntConstant()) {
829 HInstruction *left;
830 int32_t right_const;
831 if (ValueBound::IsAddOrSubAConstant(len, &left, &right_const)) {
832 // (left + right_const) is used as size to new the array.
833 // We record "-right_const <= left <= new_array - right_const";
834 ValueBound lower = ValueBound(nullptr, -right_const);
835 // We use new_array for the bound instead of new_array.length,
836 // which isn't available as an instruction yet. new_array will
837 // be treated the same as new_array.length when it's used in a ValueBound.
838 ValueBound upper = ValueBound(new_array, -right_const);
839 ValueRange* range = new (GetGraph()->GetArena())
840 ValueRange(GetGraph()->GetArena(), lower, upper);
841 GetValueRangeMap(new_array->GetBlock())->Overwrite(left->GetId(), range);
842 }
843 }
844 }
845
Mingyao Yangf384f882014-10-22 16:08:18 -0700846 std::vector<std::unique_ptr<ArenaSafeMap<int, ValueRange*>>> maps_;
847
848 DISALLOW_COPY_AND_ASSIGN(BCEVisitor);
849};
850
851void BoundsCheckElimination::Run() {
852 BCEVisitor visitor(graph_);
853 // Reverse post order guarantees a node's dominators are visited first.
854 // We want to visit in the dominator-based order since if a value is known to
855 // be bounded by a range at one instruction, it must be true that all uses of
856 // that value dominated by that instruction fits in that range. Range of that
857 // value can be narrowed further down in the dominator tree.
858 //
859 // TODO: only visit blocks that dominate some array accesses.
860 visitor.VisitReversePostOrder();
861}
862
863} // namespace art