blob: 9faead50477723254244c1d75310c6a7947572e2 [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 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700242 }
243
244 private:
Mingyao Yangf384f882014-10-22 16:08:18 -0700245 HInstruction* instruction_;
Mingyao Yang0304e182015-01-30 16:41:29 -0800246 int32_t constant_;
Mingyao Yangf384f882014-10-22 16:08:18 -0700247};
248
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700249// Collect array access data for a loop.
250// TODO: make it work for multiple arrays inside the loop.
251class ArrayAccessInsideLoopFinder : public ValueObject {
252 public:
253 explicit ArrayAccessInsideLoopFinder(HInstruction* induction_variable)
254 : induction_variable_(induction_variable),
255 found_array_length_(nullptr),
256 offset_low_(INT_MAX),
257 offset_high_(INT_MIN) {
258 Run();
259 }
260
261 HArrayLength* GetFoundArrayLength() const { return found_array_length_; }
262 bool HasFoundArrayLength() const { return found_array_length_ != nullptr; }
263 int32_t GetOffsetLow() const { return offset_low_; }
264 int32_t GetOffsetHigh() const { return offset_high_; }
265
266 void Run() {
267 HLoopInformation* loop_info = induction_variable_->GetBlock()->GetLoopInformation();
268 // Must be simplified loop.
269 DCHECK_EQ(loop_info->GetBackEdges().Size(), 1U);
270 // In order not to trigger deoptimization unnecessarily, make sure
271 // that all array accesses collected are really executed in the loop.
272 // For array accesses in a branch inside the loop, don't collect the
273 // access. The bounds check in that branch might not be eliminated.
274 for (HBasicBlock* block = loop_info->GetBackEdges().Get(0);
275 block != loop_info->GetPreHeader();
276 block = block->GetDominator()) {
277 for (HInstruction* instruction = block->GetFirstInstruction();
278 instruction != nullptr;
279 instruction = instruction->GetNext()) {
280 if (!instruction->IsArrayGet() && !instruction->IsArraySet()) {
281 continue;
282 }
283 HInstruction* index = instruction->InputAt(1);
284 if (!index->IsBoundsCheck()) {
285 continue;
286 }
287
288 HArrayLength* array_length = index->InputAt(1)->AsArrayLength();
289 if (array_length == nullptr) {
290 DCHECK(index->InputAt(1)->IsIntConstant());
291 // TODO: may optimize for constant case.
292 continue;
293 }
294
295 HInstruction* array = array_length->InputAt(0);
296 if (array->IsNullCheck()) {
297 array = array->AsNullCheck()->InputAt(0);
298 }
299 if (loop_info->Contains(*array->GetBlock())) {
300 // Array is defined inside the loop. Skip.
301 continue;
302 }
303
304 if (found_array_length_ != nullptr && found_array_length_ != array_length) {
305 // There is already access for another array recorded for the loop.
306 // TODO: handle multiple arrays.
307 continue;
308 }
309
310 index = index->AsBoundsCheck()->InputAt(0);
311 HInstruction* left = index;
312 int32_t right = 0;
313 if (left == induction_variable_ ||
314 (ValueBound::IsAddOrSubAConstant(index, &left, &right) &&
315 left == induction_variable_)) {
316 // For patterns like array[i] or array[i + 2].
317 if (right < offset_low_) {
318 offset_low_ = right;
319 }
320 if (right > offset_high_) {
321 offset_high_ = right;
322 }
323 } else {
324 // Access not in induction_variable/(induction_variable_ + constant)
325 // format. Skip.
326 continue;
327 }
328 // Record this array.
329 found_array_length_ = array_length;
330 }
331 }
332 }
333
334 private:
335 // The instruction that corresponds to a MonotonicValueRange.
336 HInstruction* induction_variable_;
337
338 // The array length of the array that's accessed inside the loop.
339 HArrayLength* found_array_length_;
340
341 // The lowest and highest constant offsets relative to induction variable
342 // instruction_ in all array accesses.
343 // If array access are: array[i-1], array[i], array[i+1],
344 // offset_low_ is -1 and offset_high is 1.
345 int32_t offset_low_;
346 int32_t offset_high_;
347
348 DISALLOW_COPY_AND_ASSIGN(ArrayAccessInsideLoopFinder);
349};
350
Mingyao Yangf384f882014-10-22 16:08:18 -0700351/**
352 * Represent a range of lower bound and upper bound, both being inclusive.
353 * Currently a ValueRange may be generated as a result of the following:
354 * comparisons related to array bounds, array bounds check, add/sub on top
Mingyao Yang0304e182015-01-30 16:41:29 -0800355 * of an existing value range, NewArray or a loop phi corresponding to an
Mingyao Yangf384f882014-10-22 16:08:18 -0700356 * incrementing/decrementing array index (MonotonicValueRange).
357 */
358class ValueRange : public ArenaObject<kArenaAllocMisc> {
359 public:
360 ValueRange(ArenaAllocator* allocator, ValueBound lower, ValueBound upper)
361 : allocator_(allocator), lower_(lower), upper_(upper) {}
362
363 virtual ~ValueRange() {}
364
Mingyao Yang57e04752015-02-09 18:13:26 -0800365 virtual MonotonicValueRange* AsMonotonicValueRange() { return nullptr; }
366 bool IsMonotonicValueRange() {
Mingyao Yangf384f882014-10-22 16:08:18 -0700367 return AsMonotonicValueRange() != nullptr;
368 }
369
370 ArenaAllocator* GetAllocator() const { return allocator_; }
371 ValueBound GetLower() const { return lower_; }
372 ValueBound GetUpper() const { return upper_; }
373
374 // If it's certain that this value range fits in other_range.
375 virtual bool FitsIn(ValueRange* other_range) const {
376 if (other_range == nullptr) {
377 return true;
378 }
379 DCHECK(!other_range->IsMonotonicValueRange());
Mingyao Yang0304e182015-01-30 16:41:29 -0800380 return lower_.GreaterThanOrEqualTo(other_range->lower_) &&
381 upper_.LessThanOrEqualTo(other_range->upper_);
Mingyao Yangf384f882014-10-22 16:08:18 -0700382 }
383
384 // Returns the intersection of this and range.
385 // If it's not possible to do intersection because some
386 // bounds are not comparable, it's ok to pick either bound.
387 virtual ValueRange* Narrow(ValueRange* range) {
388 if (range == nullptr) {
389 return this;
390 }
391
392 if (range->IsMonotonicValueRange()) {
393 return this;
394 }
395
396 return new (allocator_) ValueRange(
397 allocator_,
398 ValueBound::NarrowLowerBound(lower_, range->lower_),
399 ValueBound::NarrowUpperBound(upper_, range->upper_));
400 }
401
Mingyao Yang0304e182015-01-30 16:41:29 -0800402 // Shift a range by a constant.
403 ValueRange* Add(int32_t constant) const {
404 bool overflow, underflow;
405 ValueBound lower = lower_.Add(constant, &overflow, &underflow);
406 if (underflow) {
407 // Lower bound underflow will wrap around to positive values
408 // and invalidate the upper bound.
409 return nullptr;
Mingyao Yangf384f882014-10-22 16:08:18 -0700410 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800411 ValueBound upper = upper_.Add(constant, &overflow, &underflow);
412 if (overflow) {
413 // Upper bound overflow will wrap around to negative values
414 // and invalidate the lower bound.
415 return nullptr;
Mingyao Yangf384f882014-10-22 16:08:18 -0700416 }
417 return new (allocator_) ValueRange(allocator_, lower, upper);
418 }
419
Mingyao Yangf384f882014-10-22 16:08:18 -0700420 private:
421 ArenaAllocator* const allocator_;
422 const ValueBound lower_; // inclusive
423 const ValueBound upper_; // inclusive
424
425 DISALLOW_COPY_AND_ASSIGN(ValueRange);
426};
427
428/**
429 * A monotonically incrementing/decrementing value range, e.g.
430 * the variable i in "for (int i=0; i<array.length; i++)".
431 * Special care needs to be taken to account for overflow/underflow
432 * of such value ranges.
433 */
434class MonotonicValueRange : public ValueRange {
435 public:
Mingyao Yang64197522014-12-05 15:56:23 -0800436 MonotonicValueRange(ArenaAllocator* allocator,
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700437 HPhi* induction_variable,
Mingyao Yang64197522014-12-05 15:56:23 -0800438 HInstruction* initial,
Mingyao Yang0304e182015-01-30 16:41:29 -0800439 int32_t increment,
Mingyao Yang64197522014-12-05 15:56:23 -0800440 ValueBound bound)
441 // To be conservative, give it full range [INT_MIN, INT_MAX] in case it's
442 // used as a regular value range, due to possible overflow/underflow.
443 : ValueRange(allocator, ValueBound::Min(), ValueBound::Max()),
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700444 induction_variable_(induction_variable),
Mingyao Yang64197522014-12-05 15:56:23 -0800445 initial_(initial),
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700446 end_(nullptr),
447 inclusive_(false),
Mingyao Yang64197522014-12-05 15:56:23 -0800448 increment_(increment),
449 bound_(bound) {}
Mingyao Yangf384f882014-10-22 16:08:18 -0700450
451 virtual ~MonotonicValueRange() {}
452
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700453 HInstruction* GetInductionVariable() const { return induction_variable_; }
Mingyao Yang57e04752015-02-09 18:13:26 -0800454 int32_t GetIncrement() const { return increment_; }
Mingyao Yang57e04752015-02-09 18:13:26 -0800455 ValueBound GetBound() const { return bound_; }
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700456 void SetEnd(HInstruction* end) { end_ = end; }
457 void SetInclusive(bool inclusive) { inclusive_ = inclusive; }
458 HBasicBlock* GetLoopHead() const {
459 DCHECK(induction_variable_->GetBlock()->IsLoopHeader());
460 return induction_variable_->GetBlock();
461 }
Mingyao Yang57e04752015-02-09 18:13:26 -0800462
463 MonotonicValueRange* AsMonotonicValueRange() OVERRIDE { return this; }
Mingyao Yangf384f882014-10-22 16:08:18 -0700464
465 // If it's certain that this value range fits in other_range.
466 bool FitsIn(ValueRange* other_range) const OVERRIDE {
467 if (other_range == nullptr) {
468 return true;
469 }
470 DCHECK(!other_range->IsMonotonicValueRange());
471 return false;
472 }
473
474 // Try to narrow this MonotonicValueRange given another range.
475 // Ideally it will return a normal ValueRange. But due to
476 // possible overflow/underflow, that may not be possible.
477 ValueRange* Narrow(ValueRange* range) OVERRIDE {
478 if (range == nullptr) {
479 return this;
480 }
481 DCHECK(!range->IsMonotonicValueRange());
482
483 if (increment_ > 0) {
484 // Monotonically increasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800485 ValueBound lower = ValueBound::NarrowLowerBound(bound_, range->GetLower());
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700486 if (!lower.IsConstant() || lower.GetConstant() == INT_MIN) {
487 // Lower bound isn't useful. Leave it to deoptimization.
488 return this;
489 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700490
491 // We currently conservatively assume max array length is INT_MAX. If we can
492 // make assumptions about the max array length, e.g. due to the max heap size,
493 // divided by the element size (such as 4 bytes for each integer array), we can
494 // lower this number and rule out some possible overflows.
Mingyao Yang0304e182015-01-30 16:41:29 -0800495 int32_t max_array_len = INT_MAX;
Mingyao Yangf384f882014-10-22 16:08:18 -0700496
Mingyao Yang0304e182015-01-30 16:41:29 -0800497 // max possible integer value of range's upper value.
498 int32_t upper = INT_MAX;
499 // Try to lower upper.
500 ValueBound upper_bound = range->GetUpper();
501 if (upper_bound.IsConstant()) {
502 upper = upper_bound.GetConstant();
503 } else if (upper_bound.IsRelatedToArrayLength() && upper_bound.GetConstant() <= 0) {
504 // Normal case. e.g. <= array.length - 1.
505 upper = max_array_len + upper_bound.GetConstant();
Mingyao Yangf384f882014-10-22 16:08:18 -0700506 }
507
508 // If we can prove for the last number in sequence of initial_,
509 // initial_ + increment_, initial_ + 2 x increment_, ...
510 // that's <= upper, (last_num_in_sequence + increment_) doesn't trigger overflow,
511 // then this MonoticValueRange is narrowed to a normal value range.
512
513 // Be conservative first, assume last number in the sequence hits upper.
Mingyao Yang0304e182015-01-30 16:41:29 -0800514 int32_t last_num_in_sequence = upper;
Mingyao Yangf384f882014-10-22 16:08:18 -0700515 if (initial_->IsIntConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800516 int32_t initial_constant = initial_->AsIntConstant()->GetValue();
Mingyao Yangf384f882014-10-22 16:08:18 -0700517 if (upper <= initial_constant) {
518 last_num_in_sequence = upper;
519 } else {
Mingyao Yang0304e182015-01-30 16:41:29 -0800520 // Cast to int64_t for the substraction part to avoid int32_t overflow.
Mingyao Yangf384f882014-10-22 16:08:18 -0700521 last_num_in_sequence = initial_constant +
522 ((int64_t)upper - (int64_t)initial_constant) / increment_ * increment_;
523 }
524 }
525 if (last_num_in_sequence <= INT_MAX - increment_) {
526 // No overflow. The sequence will be stopped by the upper bound test as expected.
527 return new (GetAllocator()) ValueRange(GetAllocator(), lower, range->GetUpper());
528 }
529
530 // There might be overflow. Give up narrowing.
531 return this;
532 } else {
533 DCHECK_NE(increment_, 0);
534 // Monotonically decreasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800535 ValueBound upper = ValueBound::NarrowUpperBound(bound_, range->GetUpper());
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700536 if ((!upper.IsConstant() || upper.GetConstant() == INT_MAX) &&
537 !upper.IsRelatedToArrayLength()) {
538 // Upper bound isn't useful. Leave it to deoptimization.
539 return this;
540 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700541
542 // Need to take care of underflow. Try to prove underflow won't happen
Mingyao Yang0304e182015-01-30 16:41:29 -0800543 // for common cases.
Mingyao Yangf384f882014-10-22 16:08:18 -0700544 if (range->GetLower().IsConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800545 int32_t constant = range->GetLower().GetConstant();
Mingyao Yangf384f882014-10-22 16:08:18 -0700546 if (constant >= INT_MIN - increment_) {
547 return new (GetAllocator()) ValueRange(GetAllocator(), range->GetLower(), upper);
548 }
549 }
550
Mingyao Yang0304e182015-01-30 16:41:29 -0800551 // For non-constant lower bound, just assume might be underflow. Give up narrowing.
Mingyao Yangf384f882014-10-22 16:08:18 -0700552 return this;
553 }
554 }
555
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700556 // Returns true if adding a (constant >= value) check for deoptimization
557 // is allowed and will benefit compiled code.
558 bool CanAddDeoptimizationConstant(HInstruction* value,
559 int32_t constant,
560 bool* is_proven) {
561 *is_proven = false;
562 // See if we can prove the relationship first.
563 if (value->IsIntConstant()) {
564 if (value->AsIntConstant()->GetValue() >= constant) {
565 // Already true.
566 *is_proven = true;
567 return true;
568 } else {
569 // May throw exception. Don't add deoptimization.
570 // Keep bounds checks in the loops.
571 return false;
572 }
573 }
574 // Can benefit from deoptimization.
575 return true;
576 }
577
578 // Adds a check that (value >= constant), and HDeoptimize otherwise.
579 void AddDeoptimizationConstant(HInstruction* value,
580 int32_t constant) {
581 HBasicBlock* block = induction_variable_->GetBlock();
582 DCHECK(block->IsLoopHeader());
583 HGraph* graph = block->GetGraph();
584 HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader();
585 HSuspendCheck* suspend_check = block->GetLoopInformation()->GetSuspendCheck();
586 HIntConstant* const_instr = graph->GetIntConstant(constant);
587 HCondition* cond = new (graph->GetArena()) HLessThan(value, const_instr);
588 HDeoptimize* deoptimize = new (graph->GetArena())
589 HDeoptimize(cond, suspend_check->GetDexPc());
590 pre_header->InsertInstructionBefore(cond, pre_header->GetLastInstruction());
591 pre_header->InsertInstructionBefore(deoptimize, pre_header->GetLastInstruction());
592 deoptimize->CopyEnvironmentFromWithLoopPhiAdjustment(
593 suspend_check->GetEnvironment(), block);
594 }
595
596 // Returns true if adding a (value <= array_length + offset) check for deoptimization
597 // is allowed and will benefit compiled code.
598 bool CanAddDeoptimizationArrayLength(HInstruction* value,
599 HArrayLength* array_length,
600 int32_t offset,
601 bool* is_proven) {
602 *is_proven = false;
603 if (offset > 0) {
604 // There might be overflow issue.
605 // TODO: handle this, possibly with some distance relationship between
606 // offset_low and offset_high, or using another deoptimization to make
607 // sure (array_length + offset) doesn't overflow.
608 return false;
609 }
610
611 // See if we can prove the relationship first.
612 if (value == array_length) {
613 if (offset >= 0) {
614 // Already true.
615 *is_proven = true;
616 return true;
617 } else {
618 // May throw exception. Don't add deoptimization.
619 // Keep bounds checks in the loops.
620 return false;
621 }
622 }
623 // Can benefit from deoptimization.
624 return true;
625 }
626
627 // Adds a check that (value <= array_length + offset), and HDeoptimize otherwise.
628 void AddDeoptimizationArrayLength(HInstruction* value,
629 HArrayLength* array_length,
630 int32_t offset) {
631 HBasicBlock* block = induction_variable_->GetBlock();
632 DCHECK(block->IsLoopHeader());
633 HGraph* graph = block->GetGraph();
634 HBasicBlock* pre_header = block->GetLoopInformation()->GetPreHeader();
635 HSuspendCheck* suspend_check = block->GetLoopInformation()->GetSuspendCheck();
636
637 // We may need to hoist null-check and array_length out of loop first.
638 if (!array_length->GetBlock()->Dominates(pre_header)) {
639 HInstruction* array = array_length->InputAt(0);
640 HNullCheck* null_check = array->AsNullCheck();
641 if (null_check != nullptr) {
642 array = null_check->InputAt(0);
643 }
644 // We've already made sure array is defined before the loop when collecting
645 // array accesses for the loop.
646 DCHECK(array->GetBlock()->Dominates(pre_header));
647 if (null_check != nullptr && !null_check->GetBlock()->Dominates(pre_header)) {
648 // Hoist null check out of loop with a deoptimization.
649 HNullConstant* null_constant = graph->GetNullConstant();
650 HCondition* null_check_cond = new (graph->GetArena()) HEqual(array, null_constant);
651 // TODO: for one dex_pc, share the same deoptimization slow path.
652 HDeoptimize* null_check_deoptimize = new (graph->GetArena())
653 HDeoptimize(null_check_cond, suspend_check->GetDexPc());
654 pre_header->InsertInstructionBefore(null_check_cond, pre_header->GetLastInstruction());
655 pre_header->InsertInstructionBefore(
656 null_check_deoptimize, pre_header->GetLastInstruction());
657 // Eliminate null check in the loop.
658 null_check->ReplaceWith(array);
659 null_check->GetBlock()->RemoveInstruction(null_check);
660 null_check_deoptimize->CopyEnvironmentFromWithLoopPhiAdjustment(
661 suspend_check->GetEnvironment(), block);
662 }
663 // Hoist array_length out of loop.
664 array_length->MoveBefore(pre_header->GetLastInstruction());
665 }
666
667 HIntConstant* offset_instr = graph->GetIntConstant(offset);
668 HAdd* add = new (graph->GetArena()) HAdd(Primitive::kPrimInt, array_length, offset_instr);
669 HCondition* cond = new (graph->GetArena()) HGreaterThan(value, add);
670 HDeoptimize* deoptimize = new (graph->GetArena())
671 HDeoptimize(cond, suspend_check->GetDexPc());
672 pre_header->InsertInstructionBefore(add, pre_header->GetLastInstruction());
673 pre_header->InsertInstructionBefore(cond, pre_header->GetLastInstruction());
674 pre_header->InsertInstructionBefore(deoptimize, pre_header->GetLastInstruction());
675 deoptimize->CopyEnvironmentFromWithLoopPhiAdjustment(
676 suspend_check->GetEnvironment(), block);
677 }
678
679 // Add deoptimizations in loop pre-header with the collected array access
680 // data so that value ranges can be established in loop body.
681 // Returns true if deoptimizations are successfully added, or if it's proven
682 // it's not necessary.
683 bool AddDeoptimization(const ArrayAccessInsideLoopFinder& finder) {
684 int32_t offset_low = finder.GetOffsetLow();
685 int32_t offset_high = finder.GetOffsetHigh();
686 HArrayLength* array_length = finder.GetFoundArrayLength();
687
688 HBasicBlock* pre_header =
689 induction_variable_->GetBlock()->GetLoopInformation()->GetPreHeader();
690 if (!initial_->GetBlock()->Dominates(pre_header) ||
691 !end_->GetBlock()->Dominates(pre_header)) {
692 // Can't move initial_ or end_ into pre_header for comparisons.
693 return false;
694 }
695
696 bool is_constant_proven, is_length_proven;
697 if (increment_ == 1) {
698 // Increasing from initial_ to end_.
699 int32_t offset = inclusive_ ? -offset_high - 1 : -offset_high;
700 if (CanAddDeoptimizationConstant(initial_, -offset_low, &is_constant_proven) &&
701 CanAddDeoptimizationArrayLength(end_, array_length, offset, &is_length_proven)) {
702 if (!is_constant_proven) {
703 AddDeoptimizationConstant(initial_, -offset_low);
704 }
705 if (!is_length_proven) {
706 AddDeoptimizationArrayLength(end_, array_length, offset);
707 }
708 return true;
709 }
710 } else if (increment_ == -1) {
711 // Decreasing from initial_ to end_.
712 int32_t constant = inclusive_ ? -offset_low : -offset_low - 1;
713 if (CanAddDeoptimizationConstant(end_, constant, &is_constant_proven) &&
714 CanAddDeoptimizationArrayLength(
715 initial_, array_length, -offset_high - 1, &is_length_proven)) {
716 if (!is_constant_proven) {
717 AddDeoptimizationConstant(end_, constant);
718 }
719 if (!is_length_proven) {
720 AddDeoptimizationArrayLength(initial_, array_length, -offset_high - 1);
721 }
722 return true;
723 }
724 }
725 return false;
726 }
727
728 // Try to add HDeoptimize's in the loop pre-header first to narrow this range.
729 ValueRange* NarrowWithDeoptimization() {
730 if (increment_ != 1 && increment_ != -1) {
731 // TODO: possibly handle overflow/underflow issues with deoptimization.
732 return this;
733 }
734
735 if (end_ == nullptr) {
736 // No full info to add deoptimization.
737 return this;
738 }
739
740 ArrayAccessInsideLoopFinder finder(induction_variable_);
741
742 if (!finder.HasFoundArrayLength()) {
743 // No array access inside the loop.
744 return this;
745 }
746
747 if (!AddDeoptimization(finder)) {
748 return this;
749 }
750
751 // After added deoptimizations, induction variable fits in
752 // [-offset_low, array.length-1-offset_high], adjusted with collected offsets.
753 ValueBound lower = ValueBound(0, -finder.GetOffsetLow());
754 ValueBound upper = ValueBound(finder.GetFoundArrayLength(), -1 - finder.GetOffsetHigh());
755 // We've narrowed the range after added deoptimizations.
756 return new (GetAllocator()) ValueRange(GetAllocator(), lower, upper);
757 }
758
Mingyao Yangf384f882014-10-22 16:08:18 -0700759 private:
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700760 HPhi* const induction_variable_; // Induction variable for this monotonic value range.
761 HInstruction* const initial_; // Initial value.
762 HInstruction* end_; // End value.
763 bool inclusive_; // Whether end value is inclusive.
764 const int32_t increment_; // Increment for each loop iteration.
765 const ValueBound bound_; // Additional value bound info for initial_.
Mingyao Yangf384f882014-10-22 16:08:18 -0700766
767 DISALLOW_COPY_AND_ASSIGN(MonotonicValueRange);
768};
769
770class BCEVisitor : public HGraphVisitor {
771 public:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700772 // The least number of bounds checks that should be eliminated by triggering
773 // the deoptimization technique.
774 static constexpr size_t kThresholdForAddingDeoptimize = 2;
775
776 // Very large constant index is considered as an anomaly. This is a threshold
777 // beyond which we don't bother to apply the deoptimization technique since
778 // it's likely some AIOOBE will be thrown.
779 static constexpr int32_t kMaxConstantForAddingDeoptimize = INT_MAX - 1024 * 1024;
780
Andreas Gampe0418b5b2014-12-04 17:24:50 -0800781 explicit BCEVisitor(HGraph* graph)
Mingyao Yangf384f882014-10-22 16:08:18 -0700782 : HGraphVisitor(graph),
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700783 maps_(graph->GetBlocks().Size()),
784 need_to_revisit_block_(false) {}
785
786 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
787 first_constant_index_bounds_check_map_.clear();
788 HGraphVisitor::VisitBasicBlock(block);
789 if (need_to_revisit_block_) {
790 AddComparesWithDeoptimization(block);
791 need_to_revisit_block_ = false;
792 first_constant_index_bounds_check_map_.clear();
793 GetValueRangeMap(block)->clear();
794 HGraphVisitor::VisitBasicBlock(block);
795 }
796 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700797
798 private:
799 // Return the map of proven value ranges at the beginning of a basic block.
800 ArenaSafeMap<int, ValueRange*>* GetValueRangeMap(HBasicBlock* basic_block) {
801 int block_id = basic_block->GetBlockId();
802 if (maps_.at(block_id) == nullptr) {
803 std::unique_ptr<ArenaSafeMap<int, ValueRange*>> map(
804 new ArenaSafeMap<int, ValueRange*>(
805 std::less<int>(), GetGraph()->GetArena()->Adapter()));
806 maps_.at(block_id) = std::move(map);
807 }
808 return maps_.at(block_id).get();
809 }
810
811 // Traverse up the dominator tree to look for value range info.
812 ValueRange* LookupValueRange(HInstruction* instruction, HBasicBlock* basic_block) {
813 while (basic_block != nullptr) {
814 ArenaSafeMap<int, ValueRange*>* map = GetValueRangeMap(basic_block);
815 if (map->find(instruction->GetId()) != map->end()) {
816 return map->Get(instruction->GetId());
817 }
818 basic_block = basic_block->GetDominator();
819 }
820 // Didn't find any.
821 return nullptr;
822 }
823
Mingyao Yang0304e182015-01-30 16:41:29 -0800824 // Narrow the value range of `instruction` at the end of `basic_block` with `range`,
825 // and push the narrowed value range to `successor`.
Mingyao Yangf384f882014-10-22 16:08:18 -0700826 void ApplyRangeFromComparison(HInstruction* instruction, HBasicBlock* basic_block,
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800827 HBasicBlock* successor, ValueRange* range) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700828 ValueRange* existing_range = LookupValueRange(instruction, basic_block);
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800829 if (existing_range == nullptr) {
830 if (range != nullptr) {
831 GetValueRangeMap(successor)->Overwrite(instruction->GetId(), range);
832 }
833 return;
834 }
835 if (existing_range->IsMonotonicValueRange()) {
836 DCHECK(instruction->IsLoopHeaderPhi());
837 // Make sure the comparison is in the loop header so each increment is
838 // checked with a comparison.
839 if (instruction->GetBlock() != basic_block) {
840 return;
841 }
842 }
843 ValueRange* narrowed_range = existing_range->Narrow(range);
Mingyao Yangf384f882014-10-22 16:08:18 -0700844 if (narrowed_range != nullptr) {
845 GetValueRangeMap(successor)->Overwrite(instruction->GetId(), narrowed_range);
846 }
847 }
848
Mingyao Yang57e04752015-02-09 18:13:26 -0800849 // Special case that we may simultaneously narrow two MonotonicValueRange's to
850 // regular value ranges.
851 void HandleIfBetweenTwoMonotonicValueRanges(HIf* instruction,
852 HInstruction* left,
853 HInstruction* right,
854 IfCondition cond,
855 MonotonicValueRange* left_range,
856 MonotonicValueRange* right_range) {
857 DCHECK(left->IsLoopHeaderPhi());
858 DCHECK(right->IsLoopHeaderPhi());
859 if (instruction->GetBlock() != left->GetBlock()) {
860 // Comparison needs to be in loop header to make sure it's done after each
861 // increment/decrement.
862 return;
863 }
864
865 // Handle common cases which also don't have overflow/underflow concerns.
866 if (left_range->GetIncrement() == 1 &&
867 left_range->GetBound().IsConstant() &&
868 right_range->GetIncrement() == -1 &&
869 right_range->GetBound().IsRelatedToArrayLength() &&
870 right_range->GetBound().GetConstant() < 0) {
Mingyao Yang57e04752015-02-09 18:13:26 -0800871 HBasicBlock* successor = nullptr;
872 int32_t left_compensation = 0;
873 int32_t right_compensation = 0;
874 if (cond == kCondLT) {
875 left_compensation = -1;
876 right_compensation = 1;
877 successor = instruction->IfTrueSuccessor();
878 } else if (cond == kCondLE) {
879 successor = instruction->IfTrueSuccessor();
880 } else if (cond == kCondGT) {
881 successor = instruction->IfFalseSuccessor();
882 } else if (cond == kCondGE) {
883 left_compensation = -1;
884 right_compensation = 1;
885 successor = instruction->IfFalseSuccessor();
886 } else {
887 // We don't handle '=='/'!=' test in case left and right can cross and
888 // miss each other.
889 return;
890 }
891
892 if (successor != nullptr) {
893 bool overflow;
894 bool underflow;
895 ValueRange* new_left_range = new (GetGraph()->GetArena()) ValueRange(
896 GetGraph()->GetArena(),
897 left_range->GetBound(),
898 right_range->GetBound().Add(left_compensation, &overflow, &underflow));
899 if (!overflow && !underflow) {
900 ApplyRangeFromComparison(left, instruction->GetBlock(), successor,
901 new_left_range);
902 }
903
904 ValueRange* new_right_range = new (GetGraph()->GetArena()) ValueRange(
905 GetGraph()->GetArena(),
906 left_range->GetBound().Add(right_compensation, &overflow, &underflow),
907 right_range->GetBound());
908 if (!overflow && !underflow) {
909 ApplyRangeFromComparison(right, instruction->GetBlock(), successor,
910 new_right_range);
911 }
912 }
913 }
914 }
915
Mingyao Yangf384f882014-10-22 16:08:18 -0700916 // Handle "if (left cmp_cond right)".
917 void HandleIf(HIf* instruction, HInstruction* left, HInstruction* right, IfCondition cond) {
918 HBasicBlock* block = instruction->GetBlock();
919
920 HBasicBlock* true_successor = instruction->IfTrueSuccessor();
921 // There should be no critical edge at this point.
922 DCHECK_EQ(true_successor->GetPredecessors().Size(), 1u);
923
924 HBasicBlock* false_successor = instruction->IfFalseSuccessor();
925 // There should be no critical edge at this point.
926 DCHECK_EQ(false_successor->GetPredecessors().Size(), 1u);
927
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700928 ValueRange* left_range = LookupValueRange(left, block);
929 MonotonicValueRange* left_monotonic_range = nullptr;
930 if (left_range != nullptr) {
931 left_monotonic_range = left_range->AsMonotonicValueRange();
932 if (left_monotonic_range != nullptr) {
933 HBasicBlock* loop_head = left_monotonic_range->GetLoopHead();
934 if (instruction->GetBlock() != loop_head) {
935 // For monotonic value range, don't handle `instruction`
936 // if it's not defined in the loop header.
937 return;
938 }
939 }
940 }
941
Mingyao Yang64197522014-12-05 15:56:23 -0800942 bool found;
943 ValueBound bound = ValueBound::DetectValueBoundFromValue(right, &found);
Mingyao Yang0304e182015-01-30 16:41:29 -0800944 // Each comparison can establish a lower bound and an upper bound
945 // for the left hand side.
Mingyao Yangf384f882014-10-22 16:08:18 -0700946 ValueBound lower = bound;
947 ValueBound upper = bound;
948 if (!found) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800949 // No constant or array.length+c format bound found.
Mingyao Yangf384f882014-10-22 16:08:18 -0700950 // For i<j, we can still use j's upper bound as i's upper bound. Same for lower.
Mingyao Yang57e04752015-02-09 18:13:26 -0800951 ValueRange* right_range = LookupValueRange(right, block);
952 if (right_range != nullptr) {
953 if (right_range->IsMonotonicValueRange()) {
Mingyao Yang57e04752015-02-09 18:13:26 -0800954 if (left_range != nullptr && left_range->IsMonotonicValueRange()) {
955 HandleIfBetweenTwoMonotonicValueRanges(instruction, left, right, cond,
956 left_range->AsMonotonicValueRange(),
957 right_range->AsMonotonicValueRange());
958 return;
959 }
960 }
961 lower = right_range->GetLower();
962 upper = right_range->GetUpper();
Mingyao Yangf384f882014-10-22 16:08:18 -0700963 } else {
964 lower = ValueBound::Min();
965 upper = ValueBound::Max();
966 }
967 }
968
Mingyao Yang0304e182015-01-30 16:41:29 -0800969 bool overflow, underflow;
Mingyao Yangf384f882014-10-22 16:08:18 -0700970 if (cond == kCondLT || cond == kCondLE) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700971 if (left_monotonic_range != nullptr) {
972 // Update the info for monotonic value range.
973 if (left_monotonic_range->GetInductionVariable() == left &&
974 left_monotonic_range->GetIncrement() < 0 &&
975 block == left_monotonic_range->GetLoopHead() &&
976 instruction->IfFalseSuccessor()->GetLoopInformation() == block->GetLoopInformation()) {
977 left_monotonic_range->SetEnd(right);
978 left_monotonic_range->SetInclusive(cond == kCondLT);
979 }
980 }
981
Mingyao Yangf384f882014-10-22 16:08:18 -0700982 if (!upper.Equals(ValueBound::Max())) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800983 int32_t compensation = (cond == kCondLT) ? -1 : 0; // upper bound is inclusive
984 ValueBound new_upper = upper.Add(compensation, &overflow, &underflow);
985 if (overflow || underflow) {
986 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800987 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700988 ValueRange* new_range = new (GetGraph()->GetArena())
989 ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper);
990 ApplyRangeFromComparison(left, block, true_successor, new_range);
991 }
992
993 // array.length as a lower bound isn't considered useful.
Mingyao Yang0304e182015-01-30 16:41:29 -0800994 if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) {
995 int32_t compensation = (cond == kCondLE) ? 1 : 0; // lower bound is inclusive
996 ValueBound new_lower = lower.Add(compensation, &overflow, &underflow);
997 if (overflow || underflow) {
998 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800999 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001000 ValueRange* new_range = new (GetGraph()->GetArena())
1001 ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max());
1002 ApplyRangeFromComparison(left, block, false_successor, new_range);
1003 }
1004 } else if (cond == kCondGT || cond == kCondGE) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001005 if (left_monotonic_range != nullptr) {
1006 // Update the info for monotonic value range.
1007 if (left_monotonic_range->GetInductionVariable() == left &&
1008 left_monotonic_range->GetIncrement() > 0 &&
1009 block == left_monotonic_range->GetLoopHead() &&
1010 instruction->IfFalseSuccessor()->GetLoopInformation() == block->GetLoopInformation()) {
1011 left_monotonic_range->SetEnd(right);
1012 left_monotonic_range->SetInclusive(cond == kCondGT);
1013 }
1014 }
1015
Mingyao Yangf384f882014-10-22 16:08:18 -07001016 // array.length as a lower bound isn't considered useful.
Mingyao Yang0304e182015-01-30 16:41:29 -08001017 if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) {
1018 int32_t compensation = (cond == kCondGT) ? 1 : 0; // lower bound is inclusive
1019 ValueBound new_lower = lower.Add(compensation, &overflow, &underflow);
1020 if (overflow || underflow) {
1021 return;
Mingyao Yang64197522014-12-05 15:56:23 -08001022 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001023 ValueRange* new_range = new (GetGraph()->GetArena())
1024 ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max());
1025 ApplyRangeFromComparison(left, block, true_successor, new_range);
1026 }
1027
1028 if (!upper.Equals(ValueBound::Max())) {
Mingyao Yang0304e182015-01-30 16:41:29 -08001029 int32_t compensation = (cond == kCondGE) ? -1 : 0; // upper bound is inclusive
1030 ValueBound new_upper = upper.Add(compensation, &overflow, &underflow);
1031 if (overflow || underflow) {
1032 return;
Mingyao Yang64197522014-12-05 15:56:23 -08001033 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001034 ValueRange* new_range = new (GetGraph()->GetArena())
1035 ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper);
1036 ApplyRangeFromComparison(left, block, false_successor, new_range);
1037 }
1038 }
1039 }
1040
1041 void VisitBoundsCheck(HBoundsCheck* bounds_check) {
1042 HBasicBlock* block = bounds_check->GetBlock();
1043 HInstruction* index = bounds_check->InputAt(0);
1044 HInstruction* array_length = bounds_check->InputAt(1);
Mingyao Yang0304e182015-01-30 16:41:29 -08001045 DCHECK(array_length->IsIntConstant() || array_length->IsArrayLength());
Mingyao Yangf384f882014-10-22 16:08:18 -07001046
Mingyao Yang0304e182015-01-30 16:41:29 -08001047 if (!index->IsIntConstant()) {
1048 ValueRange* index_range = LookupValueRange(index, block);
1049 if (index_range != nullptr) {
1050 ValueBound lower = ValueBound(nullptr, 0); // constant 0
1051 ValueBound upper = ValueBound(array_length, -1); // array_length - 1
1052 ValueRange* array_range = new (GetGraph()->GetArena())
1053 ValueRange(GetGraph()->GetArena(), lower, upper);
1054 if (index_range->FitsIn(array_range)) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001055 ReplaceBoundsCheck(bounds_check, index);
1056 return;
1057 }
1058 }
Mingyao Yang0304e182015-01-30 16:41:29 -08001059 } else {
1060 int32_t constant = index->AsIntConstant()->GetValue();
1061 if (constant < 0) {
1062 // Will always throw exception.
1063 return;
1064 }
1065 if (array_length->IsIntConstant()) {
1066 if (constant < array_length->AsIntConstant()->GetValue()) {
1067 ReplaceBoundsCheck(bounds_check, index);
1068 }
1069 return;
1070 }
1071
1072 DCHECK(array_length->IsArrayLength());
1073 ValueRange* existing_range = LookupValueRange(array_length, block);
1074 if (existing_range != nullptr) {
1075 ValueBound lower = existing_range->GetLower();
1076 DCHECK(lower.IsConstant());
1077 if (constant < lower.GetConstant()) {
1078 ReplaceBoundsCheck(bounds_check, index);
1079 return;
1080 } else {
1081 // Existing range isn't strong enough to eliminate the bounds check.
1082 // Fall through to update the array_length range with info from this
1083 // bounds check.
1084 }
1085 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001086
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001087 if (first_constant_index_bounds_check_map_.find(array_length->GetId()) ==
1088 first_constant_index_bounds_check_map_.end()) {
1089 // Remember the first bounds check against array_length of a constant index.
1090 // That bounds check instruction has an associated HEnvironment where we
1091 // may add an HDeoptimize to eliminate bounds checks of constant indices
1092 // against array_length.
1093 first_constant_index_bounds_check_map_.Put(array_length->GetId(), bounds_check);
1094 } else {
1095 // We've seen it at least twice. It's beneficial to introduce a compare with
1096 // deoptimization fallback to eliminate the bounds checks.
1097 need_to_revisit_block_ = true;
1098 }
1099
Mingyao Yangf384f882014-10-22 16:08:18 -07001100 // Once we have an array access like 'array[5] = 1', we record array.length >= 6.
Mingyao Yang0304e182015-01-30 16:41:29 -08001101 // We currently don't do it for non-constant index since a valid array[i] can't prove
1102 // a valid array[i-1] yet due to the lower bound side.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001103 if (constant == INT_MAX) {
1104 // INT_MAX as an index will definitely throw AIOOBE.
1105 return;
1106 }
Mingyao Yang64197522014-12-05 15:56:23 -08001107 ValueBound lower = ValueBound(nullptr, constant + 1);
Mingyao Yangf384f882014-10-22 16:08:18 -07001108 ValueBound upper = ValueBound::Max();
1109 ValueRange* range = new (GetGraph()->GetArena())
1110 ValueRange(GetGraph()->GetArena(), lower, upper);
Mingyao Yang0304e182015-01-30 16:41:29 -08001111 GetValueRangeMap(block)->Overwrite(array_length->GetId(), range);
Mingyao Yangf384f882014-10-22 16:08:18 -07001112 }
1113 }
1114
1115 void ReplaceBoundsCheck(HInstruction* bounds_check, HInstruction* index) {
1116 bounds_check->ReplaceWith(index);
1117 bounds_check->GetBlock()->RemoveInstruction(bounds_check);
1118 }
1119
1120 void VisitPhi(HPhi* phi) {
1121 if (phi->IsLoopHeaderPhi() && phi->GetType() == Primitive::kPrimInt) {
Andreas Gampe0418b5b2014-12-04 17:24:50 -08001122 DCHECK_EQ(phi->InputCount(), 2U);
Mingyao Yangf384f882014-10-22 16:08:18 -07001123 HInstruction* instruction = phi->InputAt(1);
Mingyao Yang0304e182015-01-30 16:41:29 -08001124 HInstruction *left;
1125 int32_t increment;
1126 if (ValueBound::IsAddOrSubAConstant(instruction, &left, &increment)) {
1127 if (left == phi) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001128 HInstruction* initial_value = phi->InputAt(0);
1129 ValueRange* range = nullptr;
Mingyao Yang64197522014-12-05 15:56:23 -08001130 if (increment == 0) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001131 // Add constant 0. It's really a fixed value.
1132 range = new (GetGraph()->GetArena()) ValueRange(
1133 GetGraph()->GetArena(),
Mingyao Yang64197522014-12-05 15:56:23 -08001134 ValueBound(initial_value, 0),
1135 ValueBound(initial_value, 0));
Mingyao Yangf384f882014-10-22 16:08:18 -07001136 } else {
1137 // Monotonically increasing/decreasing.
Mingyao Yang64197522014-12-05 15:56:23 -08001138 bool found;
1139 ValueBound bound = ValueBound::DetectValueBoundFromValue(
1140 initial_value, &found);
1141 if (!found) {
1142 // No constant or array.length+c bound found.
1143 // For i=j, we can still use j's upper bound as i's upper bound.
1144 // Same for lower.
1145 ValueRange* initial_range = LookupValueRange(initial_value, phi->GetBlock());
1146 if (initial_range != nullptr) {
1147 bound = increment > 0 ? initial_range->GetLower() :
1148 initial_range->GetUpper();
1149 } else {
1150 bound = increment > 0 ? ValueBound::Min() : ValueBound::Max();
1151 }
1152 }
1153 range = new (GetGraph()->GetArena()) MonotonicValueRange(
Mingyao Yangf384f882014-10-22 16:08:18 -07001154 GetGraph()->GetArena(),
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001155 phi,
Mingyao Yangf384f882014-10-22 16:08:18 -07001156 initial_value,
Mingyao Yang64197522014-12-05 15:56:23 -08001157 increment,
1158 bound);
Mingyao Yangf384f882014-10-22 16:08:18 -07001159 }
1160 GetValueRangeMap(phi->GetBlock())->Overwrite(phi->GetId(), range);
1161 }
1162 }
1163 }
1164 }
1165
1166 void VisitIf(HIf* instruction) {
1167 if (instruction->InputAt(0)->IsCondition()) {
1168 HCondition* cond = instruction->InputAt(0)->AsCondition();
1169 IfCondition cmp = cond->GetCondition();
1170 if (cmp == kCondGT || cmp == kCondGE ||
1171 cmp == kCondLT || cmp == kCondLE) {
1172 HInstruction* left = cond->GetLeft();
1173 HInstruction* right = cond->GetRight();
1174 HandleIf(instruction, left, right, cmp);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001175
1176 HBasicBlock* block = instruction->GetBlock();
1177 ValueRange* left_range = LookupValueRange(left, block);
1178 if (left_range == nullptr) {
1179 return;
1180 }
1181
1182 if (left_range->IsMonotonicValueRange() &&
1183 block == left_range->AsMonotonicValueRange()->GetLoopHead()) {
1184 // The comparison is for an induction variable in the loop header.
1185 DCHECK(left == left_range->AsMonotonicValueRange()->GetInductionVariable());
1186 HBasicBlock* loop_body_successor;
1187 if (UNLIKELY(block->GetLoopInformation() !=
1188 instruction->IfFalseSuccessor()->GetLoopInformation())) {
1189 loop_body_successor = instruction->IfTrueSuccessor();
1190 } else {
1191 loop_body_successor = instruction->IfFalseSuccessor();
1192 }
1193 ValueRange* new_left_range = LookupValueRange(left, loop_body_successor);
1194 if (new_left_range == left_range) {
1195 // We are not successful in narrowing the monotonic value range to
1196 // a regular value range. Try using deoptimization.
1197 new_left_range = left_range->AsMonotonicValueRange()->
1198 NarrowWithDeoptimization();
1199 if (new_left_range != left_range) {
1200 GetValueRangeMap(instruction->IfFalseSuccessor())->
1201 Overwrite(left->GetId(), new_left_range);
1202 }
1203 }
1204 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001205 }
1206 }
1207 }
1208
1209 void VisitAdd(HAdd* add) {
1210 HInstruction* right = add->GetRight();
1211 if (right->IsIntConstant()) {
1212 ValueRange* left_range = LookupValueRange(add->GetLeft(), add->GetBlock());
1213 if (left_range == nullptr) {
1214 return;
1215 }
1216 ValueRange* range = left_range->Add(right->AsIntConstant()->GetValue());
1217 if (range != nullptr) {
1218 GetValueRangeMap(add->GetBlock())->Overwrite(add->GetId(), range);
1219 }
1220 }
1221 }
1222
1223 void VisitSub(HSub* sub) {
1224 HInstruction* left = sub->GetLeft();
1225 HInstruction* right = sub->GetRight();
1226 if (right->IsIntConstant()) {
1227 ValueRange* left_range = LookupValueRange(left, sub->GetBlock());
1228 if (left_range == nullptr) {
1229 return;
1230 }
1231 ValueRange* range = left_range->Add(-right->AsIntConstant()->GetValue());
1232 if (range != nullptr) {
1233 GetValueRangeMap(sub->GetBlock())->Overwrite(sub->GetId(), range);
1234 return;
1235 }
1236 }
1237
1238 // Here we are interested in the typical triangular case of nested loops,
1239 // such as the inner loop 'for (int j=0; j<array.length-i; j++)' where i
1240 // 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 -08001241
1242 // Try to handle (array.length - i) or (array.length + c - i) format.
1243 HInstruction* left_of_left; // left input of left.
1244 int32_t right_const = 0;
1245 if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &right_const)) {
1246 left = left_of_left;
1247 }
1248 // The value of left input of the sub equals (left + right_const).
1249
Mingyao Yangf384f882014-10-22 16:08:18 -07001250 if (left->IsArrayLength()) {
1251 HInstruction* array_length = left->AsArrayLength();
1252 ValueRange* right_range = LookupValueRange(right, sub->GetBlock());
1253 if (right_range != nullptr) {
1254 ValueBound lower = right_range->GetLower();
1255 ValueBound upper = right_range->GetUpper();
Mingyao Yang0304e182015-01-30 16:41:29 -08001256 if (lower.IsConstant() && upper.IsRelatedToArrayLength()) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001257 HInstruction* upper_inst = upper.GetInstruction();
Mingyao Yang0304e182015-01-30 16:41:29 -08001258 // Make sure it's the same array.
1259 if (ValueBound::Equal(array_length, upper_inst)) {
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001260 int32_t c0 = right_const;
1261 int32_t c1 = lower.GetConstant();
1262 int32_t c2 = upper.GetConstant();
1263 // (array.length + c0 - v) where v is in [c1, array.length + c2]
1264 // gets [c0 - c2, array.length + c0 - c1] as its value range.
1265 if (!ValueBound::WouldAddOverflowOrUnderflow(c0, -c2) &&
1266 !ValueBound::WouldAddOverflowOrUnderflow(c0, -c1)) {
1267 if ((c0 - c1) <= 0) {
1268 // array.length + (c0 - c1) won't overflow/underflow.
1269 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1270 GetGraph()->GetArena(),
1271 ValueBound(nullptr, right_const - upper.GetConstant()),
1272 ValueBound(array_length, right_const - lower.GetConstant()));
1273 GetValueRangeMap(sub->GetBlock())->Overwrite(sub->GetId(), range);
1274 }
1275 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001276 }
1277 }
1278 }
1279 }
1280 }
1281
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001282 void FindAndHandlePartialArrayLength(HBinaryOperation* instruction) {
1283 DCHECK(instruction->IsDiv() || instruction->IsShr() || instruction->IsUShr());
1284 HInstruction* right = instruction->GetRight();
1285 int32_t right_const;
1286 if (right->IsIntConstant()) {
1287 right_const = right->AsIntConstant()->GetValue();
1288 // Detect division by two or more.
1289 if ((instruction->IsDiv() && right_const <= 1) ||
1290 (instruction->IsShr() && right_const < 1) ||
1291 (instruction->IsUShr() && right_const < 1)) {
1292 return;
1293 }
1294 } else {
1295 return;
1296 }
1297
1298 // Try to handle array.length/2 or (array.length-1)/2 format.
1299 HInstruction* left = instruction->GetLeft();
1300 HInstruction* left_of_left; // left input of left.
1301 int32_t c = 0;
1302 if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &c)) {
1303 left = left_of_left;
1304 }
1305 // The value of left input of instruction equals (left + c).
1306
1307 // (array_length + 1) or smaller divided by two or more
1308 // always generate a value in [INT_MIN, array_length].
1309 // This is true even if array_length is INT_MAX.
1310 if (left->IsArrayLength() && c <= 1) {
1311 if (instruction->IsUShr() && c < 0) {
1312 // Make sure for unsigned shift, left side is not negative.
1313 // e.g. if array_length is 2, ((array_length - 3) >>> 2) is way bigger
1314 // than array_length.
1315 return;
1316 }
1317 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1318 GetGraph()->GetArena(),
1319 ValueBound(nullptr, INT_MIN),
1320 ValueBound(left, 0));
1321 GetValueRangeMap(instruction->GetBlock())->Overwrite(instruction->GetId(), range);
1322 }
1323 }
1324
1325 void VisitDiv(HDiv* div) {
1326 FindAndHandlePartialArrayLength(div);
1327 }
1328
1329 void VisitShr(HShr* shr) {
1330 FindAndHandlePartialArrayLength(shr);
1331 }
1332
1333 void VisitUShr(HUShr* ushr) {
1334 FindAndHandlePartialArrayLength(ushr);
1335 }
1336
Mingyao Yang4559f002015-02-27 14:43:53 -08001337 void VisitAnd(HAnd* instruction) {
1338 if (instruction->GetRight()->IsIntConstant()) {
1339 int32_t constant = instruction->GetRight()->AsIntConstant()->GetValue();
1340 if (constant > 0) {
1341 // constant serves as a mask so any number masked with it
1342 // gets a [0, constant] value range.
1343 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1344 GetGraph()->GetArena(),
1345 ValueBound(nullptr, 0),
1346 ValueBound(nullptr, constant));
1347 GetValueRangeMap(instruction->GetBlock())->Overwrite(instruction->GetId(), range);
1348 }
1349 }
1350 }
1351
Mingyao Yang0304e182015-01-30 16:41:29 -08001352 void VisitNewArray(HNewArray* new_array) {
1353 HInstruction* len = new_array->InputAt(0);
1354 if (!len->IsIntConstant()) {
1355 HInstruction *left;
1356 int32_t right_const;
1357 if (ValueBound::IsAddOrSubAConstant(len, &left, &right_const)) {
1358 // (left + right_const) is used as size to new the array.
1359 // We record "-right_const <= left <= new_array - right_const";
1360 ValueBound lower = ValueBound(nullptr, -right_const);
1361 // We use new_array for the bound instead of new_array.length,
1362 // which isn't available as an instruction yet. new_array will
1363 // be treated the same as new_array.length when it's used in a ValueBound.
1364 ValueBound upper = ValueBound(new_array, -right_const);
1365 ValueRange* range = new (GetGraph()->GetArena())
1366 ValueRange(GetGraph()->GetArena(), lower, upper);
1367 GetValueRangeMap(new_array->GetBlock())->Overwrite(left->GetId(), range);
1368 }
1369 }
1370 }
1371
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001372 void VisitDeoptimize(HDeoptimize* deoptimize) {
1373 // Right now it's only HLessThanOrEqual.
1374 DCHECK(deoptimize->InputAt(0)->IsLessThanOrEqual());
1375 HLessThanOrEqual* less_than_or_equal = deoptimize->InputAt(0)->AsLessThanOrEqual();
1376 HInstruction* instruction = less_than_or_equal->InputAt(0);
1377 if (instruction->IsArrayLength()) {
1378 HInstruction* constant = less_than_or_equal->InputAt(1);
1379 DCHECK(constant->IsIntConstant());
1380 DCHECK(constant->AsIntConstant()->GetValue() <= kMaxConstantForAddingDeoptimize);
1381 ValueBound lower = ValueBound(nullptr, constant->AsIntConstant()->GetValue() + 1);
1382 ValueRange* range = new (GetGraph()->GetArena())
1383 ValueRange(GetGraph()->GetArena(), lower, ValueBound::Max());
1384 GetValueRangeMap(deoptimize->GetBlock())->Overwrite(instruction->GetId(), range);
1385 }
1386 }
1387
1388 void AddCompareWithDeoptimization(HInstruction* array_length,
1389 HIntConstant* const_instr,
1390 HBasicBlock* block) {
1391 DCHECK(array_length->IsArrayLength());
1392 ValueRange* range = LookupValueRange(array_length, block);
1393 ValueBound lower_bound = range->GetLower();
1394 DCHECK(lower_bound.IsConstant());
1395 DCHECK(const_instr->GetValue() <= kMaxConstantForAddingDeoptimize);
1396 DCHECK_EQ(lower_bound.GetConstant(), const_instr->GetValue() + 1);
1397
1398 // If array_length is less than lower_const, deoptimize.
1399 HBoundsCheck* bounds_check = first_constant_index_bounds_check_map_.Get(
1400 array_length->GetId())->AsBoundsCheck();
1401 HCondition* cond = new (GetGraph()->GetArena()) HLessThanOrEqual(array_length, const_instr);
1402 HDeoptimize* deoptimize = new (GetGraph()->GetArena())
1403 HDeoptimize(cond, bounds_check->GetDexPc());
1404 block->InsertInstructionBefore(cond, bounds_check);
1405 block->InsertInstructionBefore(deoptimize, bounds_check);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001406 deoptimize->CopyEnvironmentFrom(bounds_check->GetEnvironment());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001407 }
1408
1409 void AddComparesWithDeoptimization(HBasicBlock* block) {
1410 for (ArenaSafeMap<int, HBoundsCheck*>::iterator it =
1411 first_constant_index_bounds_check_map_.begin();
1412 it != first_constant_index_bounds_check_map_.end();
1413 ++it) {
1414 HBoundsCheck* bounds_check = it->second;
1415 HArrayLength* array_length = bounds_check->InputAt(1)->AsArrayLength();
1416 HIntConstant* lower_bound_const_instr = nullptr;
1417 int32_t lower_bound_const = INT_MIN;
1418 size_t counter = 0;
1419 // Count the constant indexing for which bounds checks haven't
1420 // been removed yet.
1421 for (HUseIterator<HInstruction*> it2(array_length->GetUses());
1422 !it2.Done();
1423 it2.Advance()) {
1424 HInstruction* user = it2.Current()->GetUser();
1425 if (user->GetBlock() == block &&
1426 user->IsBoundsCheck() &&
1427 user->AsBoundsCheck()->InputAt(0)->IsIntConstant()) {
1428 DCHECK_EQ(array_length, user->AsBoundsCheck()->InputAt(1));
1429 HIntConstant* const_instr = user->AsBoundsCheck()->InputAt(0)->AsIntConstant();
1430 if (const_instr->GetValue() > lower_bound_const) {
1431 lower_bound_const = const_instr->GetValue();
1432 lower_bound_const_instr = const_instr;
1433 }
1434 counter++;
1435 }
1436 }
1437 if (counter >= kThresholdForAddingDeoptimize &&
1438 lower_bound_const_instr->GetValue() <= kMaxConstantForAddingDeoptimize) {
1439 AddCompareWithDeoptimization(array_length, lower_bound_const_instr, block);
1440 }
1441 }
1442 }
1443
Mingyao Yangf384f882014-10-22 16:08:18 -07001444 std::vector<std::unique_ptr<ArenaSafeMap<int, ValueRange*>>> maps_;
1445
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001446 // Map an HArrayLength instruction's id to the first HBoundsCheck instruction in
1447 // a block that checks a constant index against that HArrayLength.
1448 SafeMap<int, HBoundsCheck*> first_constant_index_bounds_check_map_;
1449
1450 // For the block, there is at least one HArrayLength instruction for which there
1451 // is more than one bounds check instruction with constant indexing. And it's
1452 // beneficial to add a compare instruction that has deoptimization fallback and
1453 // eliminate those bounds checks.
1454 bool need_to_revisit_block_;
1455
Mingyao Yangf384f882014-10-22 16:08:18 -07001456 DISALLOW_COPY_AND_ASSIGN(BCEVisitor);
1457};
1458
1459void BoundsCheckElimination::Run() {
Mingyao Yange4335eb2015-03-02 15:14:13 -08001460 if (!graph_->HasArrayAccesses()) {
1461 return;
1462 }
1463
Mingyao Yangf384f882014-10-22 16:08:18 -07001464 BCEVisitor visitor(graph_);
1465 // Reverse post order guarantees a node's dominators are visited first.
1466 // We want to visit in the dominator-based order since if a value is known to
1467 // be bounded by a range at one instruction, it must be true that all uses of
1468 // that value dominated by that instruction fits in that range. Range of that
1469 // value can be narrowed further down in the dominator tree.
1470 //
1471 // TODO: only visit blocks that dominate some array accesses.
1472 visitor.VisitReversePostOrder();
1473}
1474
1475} // namespace art