blob: 900dabea0ed9edb845c54a9c1228aab972042273 [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 Yangbca381a2015-05-19 16:01:59 -0700129 static HInstruction* FromArrayLengthToArray(HInstruction* instruction) {
130 DCHECK(instruction->IsArrayLength() || instruction->IsNewArray());
131 if (instruction->IsArrayLength()) {
132 HInstruction* input = instruction->InputAt(0);
133 if (input->IsNullCheck()) {
134 input = input->AsNullCheck()->InputAt(0);
135 }
136 return input;
Mingyao Yang0304e182015-01-30 16:41:29 -0800137 }
138 return instruction;
139 }
140
141 static bool Equal(HInstruction* instruction1, HInstruction* instruction2) {
142 if (instruction1 == instruction2) {
143 return true;
144 }
145
146 if (instruction1 == nullptr || instruction2 == nullptr) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700147 return false;
148 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800149
150 // Some bounds are created with HNewArray* as the instruction instead
151 // of HArrayLength*. They are treated the same.
Mingyao Yangbca381a2015-05-19 16:01:59 -0700152 // HArrayLength with the same array input are considered equal also.
153 instruction1 = FromArrayLengthToArray(instruction1);
154 instruction2 = FromArrayLengthToArray(instruction2);
Mingyao Yang0304e182015-01-30 16:41:29 -0800155 return instruction1 == instruction2;
156 }
157
158 // Returns if it's certain this->bound >= `bound`.
159 bool GreaterThanOrEqualTo(ValueBound bound) const {
160 if (Equal(instruction_, bound.instruction_)) {
161 return constant_ >= bound.constant_;
162 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700163 // Not comparable. Just return false.
164 return false;
165 }
166
Mingyao Yang0304e182015-01-30 16:41:29 -0800167 // Returns if it's certain this->bound <= `bound`.
168 bool LessThanOrEqualTo(ValueBound bound) const {
169 if (Equal(instruction_, bound.instruction_)) {
170 return constant_ <= bound.constant_;
Mingyao Yangf384f882014-10-22 16:08:18 -0700171 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700172 // Not comparable. Just return false.
173 return false;
174 }
175
176 // Try to narrow lower bound. Returns the greatest of the two if possible.
177 // Pick one if they are not comparable.
178 static ValueBound NarrowLowerBound(ValueBound bound1, ValueBound bound2) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800179 if (bound1.GreaterThanOrEqualTo(bound2)) {
180 return bound1;
181 }
182 if (bound2.GreaterThanOrEqualTo(bound1)) {
183 return bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700184 }
185
186 // Not comparable. Just pick one. We may lose some info, but that's ok.
187 // Favor constant as lower bound.
188 return bound1.IsConstant() ? bound1 : bound2;
189 }
190
191 // Try to narrow upper bound. Returns the lowest of the two if possible.
192 // Pick one if they are not comparable.
193 static ValueBound NarrowUpperBound(ValueBound bound1, ValueBound bound2) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800194 if (bound1.LessThanOrEqualTo(bound2)) {
195 return bound1;
196 }
197 if (bound2.LessThanOrEqualTo(bound1)) {
198 return bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700199 }
200
201 // Not comparable. Just pick one. We may lose some info, but that's ok.
202 // Favor array length as upper bound.
Mingyao Yang0304e182015-01-30 16:41:29 -0800203 return bound1.IsRelatedToArrayLength() ? bound1 : bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700204 }
205
Mingyao Yang0304e182015-01-30 16:41:29 -0800206 // Add a constant to a ValueBound.
207 // `overflow` or `underflow` will return whether the resulting bound may
208 // overflow or underflow an int.
209 ValueBound Add(int32_t c, bool* overflow, bool* underflow) const {
210 *overflow = *underflow = false;
Mingyao Yangf384f882014-10-22 16:08:18 -0700211 if (c == 0) {
212 return *this;
213 }
214
Mingyao Yang0304e182015-01-30 16:41:29 -0800215 int32_t new_constant;
Mingyao Yangf384f882014-10-22 16:08:18 -0700216 if (c > 0) {
217 if (constant_ > INT_MAX - c) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800218 *overflow = true;
Mingyao Yang64197522014-12-05 15:56:23 -0800219 return Max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700220 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800221
222 new_constant = constant_ + c;
223 // (array.length + non-positive-constant) won't overflow an int.
224 if (IsConstant() || (IsRelatedToArrayLength() && new_constant <= 0)) {
225 return ValueBound(instruction_, new_constant);
226 }
227 // Be conservative.
228 *overflow = true;
229 return Max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700230 } else {
231 if (constant_ < INT_MIN - c) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800232 *underflow = true;
233 return Min();
Mingyao Yangf384f882014-10-22 16:08:18 -0700234 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800235
236 new_constant = constant_ + c;
237 // Regardless of the value new_constant, (array.length+new_constant) will
238 // never underflow since array.length is no less than 0.
239 if (IsConstant() || IsRelatedToArrayLength()) {
240 return ValueBound(instruction_, new_constant);
241 }
242 // Be conservative.
243 *underflow = true;
244 return Min();
Mingyao Yangf384f882014-10-22 16:08:18 -0700245 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700246 }
247
248 private:
Mingyao Yangf384f882014-10-22 16:08:18 -0700249 HInstruction* instruction_;
Mingyao Yang0304e182015-01-30 16:41:29 -0800250 int32_t constant_;
Mingyao Yangf384f882014-10-22 16:08:18 -0700251};
252
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700253// Collect array access data for a loop.
254// TODO: make it work for multiple arrays inside the loop.
255class ArrayAccessInsideLoopFinder : public ValueObject {
256 public:
257 explicit ArrayAccessInsideLoopFinder(HInstruction* induction_variable)
258 : induction_variable_(induction_variable),
259 found_array_length_(nullptr),
260 offset_low_(INT_MAX),
261 offset_high_(INT_MIN) {
262 Run();
263 }
264
265 HArrayLength* GetFoundArrayLength() const { return found_array_length_; }
266 bool HasFoundArrayLength() const { return found_array_length_ != nullptr; }
267 int32_t GetOffsetLow() const { return offset_low_; }
268 int32_t GetOffsetHigh() const { return offset_high_; }
269
Mingyao Yang9d750ef2015-04-26 18:15:30 -0700270 // Returns if `block` that is in loop_info may exit the loop, unless it's
271 // the loop header for loop_info.
272 static bool EarlyExit(HBasicBlock* block, HLoopInformation* loop_info) {
273 DCHECK(loop_info->Contains(*block));
274 if (block == loop_info->GetHeader()) {
275 // Loop header of loop_info. Exiting loop is normal.
276 return false;
277 }
Mingyao Yangbca381a2015-05-19 16:01:59 -0700278 const GrowableArray<HBasicBlock*>& successors = block->GetSuccessors();
Mingyao Yang9d750ef2015-04-26 18:15:30 -0700279 for (size_t i = 0; i < successors.Size(); i++) {
280 if (!loop_info->Contains(*successors.Get(i))) {
281 // One of the successors exits the loop.
282 return true;
283 }
284 }
285 return false;
286 }
287
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100288 static bool DominatesAllBackEdges(HBasicBlock* block, HLoopInformation* loop_info) {
289 for (size_t i = 0, e = loop_info->GetBackEdges().Size(); i < e; ++i) {
290 HBasicBlock* back_edge = loop_info->GetBackEdges().Get(i);
291 if (!block->Dominates(back_edge)) {
292 return false;
293 }
294 }
295 return true;
296 }
297
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700298 void Run() {
299 HLoopInformation* loop_info = induction_variable_->GetBlock()->GetLoopInformation();
Mingyao Yangbca381a2015-05-19 16:01:59 -0700300 HBlocksInLoopReversePostOrderIterator it_loop(*loop_info);
301 HBasicBlock* block = it_loop.Current();
302 DCHECK(block == induction_variable_->GetBlock());
303 // Skip loop header. Since narrowed value range of a MonotonicValueRange only
304 // applies to the loop body (after the test at the end of the loop header).
305 it_loop.Advance();
306 for (; !it_loop.Done(); it_loop.Advance()) {
307 block = it_loop.Current();
Mingyao Yang9d750ef2015-04-26 18:15:30 -0700308 DCHECK(block->IsInLoop());
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100309 if (!DominatesAllBackEdges(block, loop_info)) {
Mingyao Yang9d750ef2015-04-26 18:15:30 -0700310 // In order not to trigger deoptimization unnecessarily, make sure
311 // that all array accesses collected are really executed in the loop.
312 // For array accesses in a branch inside the loop, don't collect the
313 // access. The bounds check in that branch might not be eliminated.
314 continue;
315 }
316 if (EarlyExit(block, loop_info)) {
317 // If the loop body can exit loop (like break, return, etc.), it's not guaranteed
318 // that the loop will loop through the full monotonic value range from
319 // initial_ to end_. So adding deoptimization might be too aggressive and can
320 // trigger deoptimization unnecessarily even if the loop won't actually throw
Mingyao Yangbca381a2015-05-19 16:01:59 -0700321 // AIOOBE.
Mingyao Yang9d750ef2015-04-26 18:15:30 -0700322 found_array_length_ = nullptr;
323 return;
324 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700325 for (HInstruction* instruction = block->GetFirstInstruction();
326 instruction != nullptr;
327 instruction = instruction->GetNext()) {
Mingyao Yangbca381a2015-05-19 16:01:59 -0700328 if (!instruction->IsBoundsCheck()) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700329 continue;
330 }
331
Mingyao Yangbca381a2015-05-19 16:01:59 -0700332 HInstruction* length_value = instruction->InputAt(1);
333 if (length_value->IsIntConstant()) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700334 // TODO: may optimize for constant case.
335 continue;
336 }
337
Mingyao Yangbca381a2015-05-19 16:01:59 -0700338 if (length_value->IsPhi()) {
Nicolas Geoffray31fa4b52015-06-17 10:17:49 +0100339 // When adding deoptimizations in outer loops, we might create
340 // a phi for the array length, and update all uses of the
341 // length in the loop to that phi. Therefore, inner loops having
342 // bounds checks on the same array will use that phi.
343 // TODO: handle these cases.
Mingyao Yangbca381a2015-05-19 16:01:59 -0700344 continue;
345 }
346
347 DCHECK(length_value->IsArrayLength());
348 HArrayLength* array_length = length_value->AsArrayLength();
349
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700350 HInstruction* array = array_length->InputAt(0);
351 if (array->IsNullCheck()) {
352 array = array->AsNullCheck()->InputAt(0);
353 }
354 if (loop_info->Contains(*array->GetBlock())) {
355 // Array is defined inside the loop. Skip.
356 continue;
357 }
358
359 if (found_array_length_ != nullptr && found_array_length_ != array_length) {
360 // There is already access for another array recorded for the loop.
361 // TODO: handle multiple arrays.
362 continue;
363 }
364
Mingyao Yangbca381a2015-05-19 16:01:59 -0700365 HInstruction* index = instruction->AsBoundsCheck()->InputAt(0);
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700366 HInstruction* left = index;
367 int32_t right = 0;
368 if (left == induction_variable_ ||
369 (ValueBound::IsAddOrSubAConstant(index, &left, &right) &&
370 left == induction_variable_)) {
371 // For patterns like array[i] or array[i + 2].
372 if (right < offset_low_) {
373 offset_low_ = right;
374 }
375 if (right > offset_high_) {
376 offset_high_ = right;
377 }
378 } else {
379 // Access not in induction_variable/(induction_variable_ + constant)
380 // format. Skip.
381 continue;
382 }
383 // Record this array.
384 found_array_length_ = array_length;
385 }
386 }
387 }
388
389 private:
390 // The instruction that corresponds to a MonotonicValueRange.
391 HInstruction* induction_variable_;
392
Mingyao Yangbca381a2015-05-19 16:01:59 -0700393 // The array length of the array that's accessed inside the loop body.
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700394 HArrayLength* found_array_length_;
395
396 // The lowest and highest constant offsets relative to induction variable
397 // instruction_ in all array accesses.
398 // If array access are: array[i-1], array[i], array[i+1],
399 // offset_low_ is -1 and offset_high is 1.
400 int32_t offset_low_;
401 int32_t offset_high_;
402
403 DISALLOW_COPY_AND_ASSIGN(ArrayAccessInsideLoopFinder);
404};
405
Mingyao Yangf384f882014-10-22 16:08:18 -0700406/**
407 * Represent a range of lower bound and upper bound, both being inclusive.
408 * Currently a ValueRange may be generated as a result of the following:
409 * comparisons related to array bounds, array bounds check, add/sub on top
Mingyao Yang0304e182015-01-30 16:41:29 -0800410 * of an existing value range, NewArray or a loop phi corresponding to an
Mingyao Yangf384f882014-10-22 16:08:18 -0700411 * incrementing/decrementing array index (MonotonicValueRange).
412 */
413class ValueRange : public ArenaObject<kArenaAllocMisc> {
414 public:
415 ValueRange(ArenaAllocator* allocator, ValueBound lower, ValueBound upper)
416 : allocator_(allocator), lower_(lower), upper_(upper) {}
417
418 virtual ~ValueRange() {}
419
Mingyao Yang57e04752015-02-09 18:13:26 -0800420 virtual MonotonicValueRange* AsMonotonicValueRange() { return nullptr; }
421 bool IsMonotonicValueRange() {
Mingyao Yangf384f882014-10-22 16:08:18 -0700422 return AsMonotonicValueRange() != nullptr;
423 }
424
425 ArenaAllocator* GetAllocator() const { return allocator_; }
426 ValueBound GetLower() const { return lower_; }
427 ValueBound GetUpper() const { return upper_; }
428
Mingyao Yangbca381a2015-05-19 16:01:59 -0700429 bool IsConstantValueRange() { return lower_.IsConstant() && upper_.IsConstant(); }
430
Mingyao Yangf384f882014-10-22 16:08:18 -0700431 // If it's certain that this value range fits in other_range.
432 virtual bool FitsIn(ValueRange* other_range) const {
433 if (other_range == nullptr) {
434 return true;
435 }
436 DCHECK(!other_range->IsMonotonicValueRange());
Mingyao Yang0304e182015-01-30 16:41:29 -0800437 return lower_.GreaterThanOrEqualTo(other_range->lower_) &&
438 upper_.LessThanOrEqualTo(other_range->upper_);
Mingyao Yangf384f882014-10-22 16:08:18 -0700439 }
440
441 // Returns the intersection of this and range.
442 // If it's not possible to do intersection because some
443 // bounds are not comparable, it's ok to pick either bound.
444 virtual ValueRange* Narrow(ValueRange* range) {
445 if (range == nullptr) {
446 return this;
447 }
448
449 if (range->IsMonotonicValueRange()) {
450 return this;
451 }
452
453 return new (allocator_) ValueRange(
454 allocator_,
455 ValueBound::NarrowLowerBound(lower_, range->lower_),
456 ValueBound::NarrowUpperBound(upper_, range->upper_));
457 }
458
Mingyao Yang0304e182015-01-30 16:41:29 -0800459 // Shift a range by a constant.
460 ValueRange* Add(int32_t constant) const {
461 bool overflow, underflow;
462 ValueBound lower = lower_.Add(constant, &overflow, &underflow);
463 if (underflow) {
464 // Lower bound underflow will wrap around to positive values
465 // and invalidate the upper bound.
466 return nullptr;
Mingyao Yangf384f882014-10-22 16:08:18 -0700467 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800468 ValueBound upper = upper_.Add(constant, &overflow, &underflow);
469 if (overflow) {
470 // Upper bound overflow will wrap around to negative values
471 // and invalidate the lower bound.
472 return nullptr;
Mingyao Yangf384f882014-10-22 16:08:18 -0700473 }
474 return new (allocator_) ValueRange(allocator_, lower, upper);
475 }
476
Mingyao Yangf384f882014-10-22 16:08:18 -0700477 private:
478 ArenaAllocator* const allocator_;
479 const ValueBound lower_; // inclusive
480 const ValueBound upper_; // inclusive
481
482 DISALLOW_COPY_AND_ASSIGN(ValueRange);
483};
484
485/**
486 * A monotonically incrementing/decrementing value range, e.g.
487 * the variable i in "for (int i=0; i<array.length; i++)".
488 * Special care needs to be taken to account for overflow/underflow
489 * of such value ranges.
490 */
491class MonotonicValueRange : public ValueRange {
492 public:
Mingyao Yang64197522014-12-05 15:56:23 -0800493 MonotonicValueRange(ArenaAllocator* allocator,
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700494 HPhi* induction_variable,
Mingyao Yang64197522014-12-05 15:56:23 -0800495 HInstruction* initial,
Mingyao Yang0304e182015-01-30 16:41:29 -0800496 int32_t increment,
Mingyao Yang64197522014-12-05 15:56:23 -0800497 ValueBound bound)
498 // To be conservative, give it full range [INT_MIN, INT_MAX] in case it's
499 // used as a regular value range, due to possible overflow/underflow.
500 : ValueRange(allocator, ValueBound::Min(), ValueBound::Max()),
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700501 induction_variable_(induction_variable),
Mingyao Yang64197522014-12-05 15:56:23 -0800502 initial_(initial),
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700503 end_(nullptr),
504 inclusive_(false),
Mingyao Yang64197522014-12-05 15:56:23 -0800505 increment_(increment),
506 bound_(bound) {}
Mingyao Yangf384f882014-10-22 16:08:18 -0700507
508 virtual ~MonotonicValueRange() {}
509
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700510 HInstruction* GetInductionVariable() const { return induction_variable_; }
Mingyao Yang57e04752015-02-09 18:13:26 -0800511 int32_t GetIncrement() const { return increment_; }
Mingyao Yang57e04752015-02-09 18:13:26 -0800512 ValueBound GetBound() const { return bound_; }
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700513 void SetEnd(HInstruction* end) { end_ = end; }
514 void SetInclusive(bool inclusive) { inclusive_ = inclusive; }
Mingyao Yangbca381a2015-05-19 16:01:59 -0700515 HBasicBlock* GetLoopHeader() const {
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700516 DCHECK(induction_variable_->GetBlock()->IsLoopHeader());
517 return induction_variable_->GetBlock();
518 }
Mingyao Yang57e04752015-02-09 18:13:26 -0800519
520 MonotonicValueRange* AsMonotonicValueRange() OVERRIDE { return this; }
Mingyao Yangf384f882014-10-22 16:08:18 -0700521
Mingyao Yangbca381a2015-05-19 16:01:59 -0700522 HBasicBlock* GetLoopHeaderSuccesorInLoop() {
523 HBasicBlock* header = GetLoopHeader();
524 HInstruction* instruction = header->GetLastInstruction();
525 DCHECK(instruction->IsIf());
526 HIf* h_if = instruction->AsIf();
527 HLoopInformation* loop_info = header->GetLoopInformation();
528 bool true_successor_in_loop = loop_info->Contains(*h_if->IfTrueSuccessor());
529 bool false_successor_in_loop = loop_info->Contains(*h_if->IfFalseSuccessor());
530
531 // Just in case it's some strange loop structure.
532 if (true_successor_in_loop && false_successor_in_loop) {
533 return nullptr;
534 }
535 DCHECK(true_successor_in_loop || false_successor_in_loop);
536 return false_successor_in_loop ? h_if->IfFalseSuccessor() : h_if->IfTrueSuccessor();
537 }
538
Mingyao Yangf384f882014-10-22 16:08:18 -0700539 // If it's certain that this value range fits in other_range.
540 bool FitsIn(ValueRange* other_range) const OVERRIDE {
541 if (other_range == nullptr) {
542 return true;
543 }
544 DCHECK(!other_range->IsMonotonicValueRange());
545 return false;
546 }
547
548 // Try to narrow this MonotonicValueRange given another range.
549 // Ideally it will return a normal ValueRange. But due to
550 // possible overflow/underflow, that may not be possible.
551 ValueRange* Narrow(ValueRange* range) OVERRIDE {
552 if (range == nullptr) {
553 return this;
554 }
555 DCHECK(!range->IsMonotonicValueRange());
556
557 if (increment_ > 0) {
558 // Monotonically increasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800559 ValueBound lower = ValueBound::NarrowLowerBound(bound_, range->GetLower());
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700560 if (!lower.IsConstant() || lower.GetConstant() == INT_MIN) {
561 // Lower bound isn't useful. Leave it to deoptimization.
562 return this;
563 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700564
565 // We currently conservatively assume max array length is INT_MAX. If we can
566 // make assumptions about the max array length, e.g. due to the max heap size,
567 // divided by the element size (such as 4 bytes for each integer array), we can
568 // lower this number and rule out some possible overflows.
Mingyao Yang0304e182015-01-30 16:41:29 -0800569 int32_t max_array_len = INT_MAX;
Mingyao Yangf384f882014-10-22 16:08:18 -0700570
Mingyao Yang0304e182015-01-30 16:41:29 -0800571 // max possible integer value of range's upper value.
572 int32_t upper = INT_MAX;
573 // Try to lower upper.
574 ValueBound upper_bound = range->GetUpper();
575 if (upper_bound.IsConstant()) {
576 upper = upper_bound.GetConstant();
577 } else if (upper_bound.IsRelatedToArrayLength() && upper_bound.GetConstant() <= 0) {
578 // Normal case. e.g. <= array.length - 1.
579 upper = max_array_len + upper_bound.GetConstant();
Mingyao Yangf384f882014-10-22 16:08:18 -0700580 }
581
582 // If we can prove for the last number in sequence of initial_,
583 // initial_ + increment_, initial_ + 2 x increment_, ...
584 // that's <= upper, (last_num_in_sequence + increment_) doesn't trigger overflow,
585 // then this MonoticValueRange is narrowed to a normal value range.
586
587 // Be conservative first, assume last number in the sequence hits upper.
Mingyao Yang0304e182015-01-30 16:41:29 -0800588 int32_t last_num_in_sequence = upper;
Mingyao Yangf384f882014-10-22 16:08:18 -0700589 if (initial_->IsIntConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800590 int32_t initial_constant = initial_->AsIntConstant()->GetValue();
Mingyao Yangf384f882014-10-22 16:08:18 -0700591 if (upper <= initial_constant) {
592 last_num_in_sequence = upper;
593 } else {
Mingyao Yang0304e182015-01-30 16:41:29 -0800594 // Cast to int64_t for the substraction part to avoid int32_t overflow.
Mingyao Yangf384f882014-10-22 16:08:18 -0700595 last_num_in_sequence = initial_constant +
596 ((int64_t)upper - (int64_t)initial_constant) / increment_ * increment_;
597 }
598 }
599 if (last_num_in_sequence <= INT_MAX - increment_) {
600 // No overflow. The sequence will be stopped by the upper bound test as expected.
601 return new (GetAllocator()) ValueRange(GetAllocator(), lower, range->GetUpper());
602 }
603
604 // There might be overflow. Give up narrowing.
605 return this;
606 } else {
607 DCHECK_NE(increment_, 0);
608 // Monotonically decreasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800609 ValueBound upper = ValueBound::NarrowUpperBound(bound_, range->GetUpper());
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700610 if ((!upper.IsConstant() || upper.GetConstant() == INT_MAX) &&
611 !upper.IsRelatedToArrayLength()) {
612 // Upper bound isn't useful. Leave it to deoptimization.
613 return this;
614 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700615
616 // Need to take care of underflow. Try to prove underflow won't happen
Mingyao Yang0304e182015-01-30 16:41:29 -0800617 // for common cases.
Mingyao Yangf384f882014-10-22 16:08:18 -0700618 if (range->GetLower().IsConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800619 int32_t constant = range->GetLower().GetConstant();
Mingyao Yangf384f882014-10-22 16:08:18 -0700620 if (constant >= INT_MIN - increment_) {
621 return new (GetAllocator()) ValueRange(GetAllocator(), range->GetLower(), upper);
622 }
623 }
624
Mingyao Yang0304e182015-01-30 16:41:29 -0800625 // For non-constant lower bound, just assume might be underflow. Give up narrowing.
Mingyao Yangf384f882014-10-22 16:08:18 -0700626 return this;
627 }
628 }
629
Mingyao Yangbca381a2015-05-19 16:01:59 -0700630 // Try to add HDeoptimize's in the loop pre-header first to narrow this range.
631 // For example, this loop:
632 //
633 // for (int i = start; i < end; i++) {
634 // array[i - 1] = array[i] + array[i + 1];
635 // }
636 //
637 // will be transformed to:
638 //
639 // int array_length_in_loop_body_if_needed;
640 // if (start >= end) {
641 // array_length_in_loop_body_if_needed = 0;
642 // } else {
643 // if (start < 1) deoptimize();
644 // if (array == null) deoptimize();
645 // array_length = array.length;
646 // if (end > array_length - 1) deoptimize;
647 // array_length_in_loop_body_if_needed = array_length;
648 // }
649 // for (int i = start; i < end; i++) {
650 // // No more null check and bounds check.
651 // // array.length value is replaced with array_length_in_loop_body_if_needed
652 // // in the loop body.
653 // array[i - 1] = array[i] + array[i + 1];
654 // }
655 //
656 // We basically first go through the loop body and find those array accesses whose
657 // index is at a constant offset from the induction variable ('i' in the above example),
658 // and update offset_low and offset_high along the way. We then add the following
659 // deoptimizations in the loop pre-header (suppose end is not inclusive).
660 // if (start < -offset_low) deoptimize();
661 // if (end >= array.length - offset_high) deoptimize();
662 // It might be necessary to first hoist array.length (and the null check on it) out of
663 // the loop with another deoptimization.
664 //
665 // In order not to trigger deoptimization unnecessarily, we want to make a strong
666 // guarantee that no deoptimization is triggered if the loop body itself doesn't
667 // throw AIOOBE. (It's the same as saying if deoptimization is triggered, the loop
668 // body must throw AIOOBE).
669 // This is achieved by the following:
670 // 1) We only process loops that iterate through the full monotonic range from
671 // initial_ to end_. We do the following checks to make sure that's the case:
672 // a) The loop doesn't have early exit (via break, return, etc.)
673 // b) The increment_ is 1/-1. An increment of 2, for example, may skip end_.
674 // 2) We only collect array accesses of blocks in the loop body that dominate
675 // all loop back edges, these array accesses are guaranteed to happen
676 // at each loop iteration.
677 // With 1) and 2), if the loop body doesn't throw AIOOBE, collected array accesses
678 // when the induction variable is at initial_ and end_ must be in a legal range.
679 // Since the added deoptimizations are basically checking the induction variable
680 // at initial_ and end_ values, no deoptimization will be triggered either.
681 //
682 // A special case is the loop body isn't entered at all. In that case, we may still
683 // add deoptimization due to the analysis described above. In order not to trigger
684 // deoptimization, we do a test between initial_ and end_ first and skip over
685 // the added deoptimization.
686 ValueRange* NarrowWithDeoptimization() {
687 if (increment_ != 1 && increment_ != -1) {
688 // In order not to trigger deoptimization unnecessarily, we want to
689 // make sure the loop iterates through the full range from initial_ to
690 // end_ so that boundaries are covered by the loop. An increment of 2,
691 // for example, may skip end_.
692 return this;
693 }
694
695 if (end_ == nullptr) {
696 // No full info to add deoptimization.
697 return this;
698 }
699
700 HBasicBlock* header = induction_variable_->GetBlock();
701 DCHECK(header->IsLoopHeader());
702 HBasicBlock* pre_header = header->GetLoopInformation()->GetPreHeader();
703 if (!initial_->GetBlock()->Dominates(pre_header) ||
704 !end_->GetBlock()->Dominates(pre_header)) {
705 // Can't add a check in loop pre-header if the value isn't available there.
706 return this;
707 }
708
709 ArrayAccessInsideLoopFinder finder(induction_variable_);
710
711 if (!finder.HasFoundArrayLength()) {
712 // No array access was found inside the loop that can benefit
713 // from deoptimization.
714 return this;
715 }
716
717 if (!AddDeoptimization(finder)) {
718 return this;
719 }
720
721 // After added deoptimizations, induction variable fits in
722 // [-offset_low, array.length-1-offset_high], adjusted with collected offsets.
723 ValueBound lower = ValueBound(0, -finder.GetOffsetLow());
724 ValueBound upper = ValueBound(finder.GetFoundArrayLength(), -1 - finder.GetOffsetHigh());
725 // We've narrowed the range after added deoptimizations.
726 return new (GetAllocator()) ValueRange(GetAllocator(), lower, upper);
727 }
728
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700729 // Returns true if adding a (constant >= value) check for deoptimization
730 // is allowed and will benefit compiled code.
Mingyao Yangbca381a2015-05-19 16:01:59 -0700731 bool CanAddDeoptimizationConstant(HInstruction* value, int32_t constant, bool* is_proven) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700732 *is_proven = false;
Mingyao Yangbca381a2015-05-19 16:01:59 -0700733 HBasicBlock* header = induction_variable_->GetBlock();
734 DCHECK(header->IsLoopHeader());
735 HBasicBlock* pre_header = header->GetLoopInformation()->GetPreHeader();
736 DCHECK(value->GetBlock()->Dominates(pre_header));
737
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700738 // See if we can prove the relationship first.
739 if (value->IsIntConstant()) {
740 if (value->AsIntConstant()->GetValue() >= constant) {
741 // Already true.
742 *is_proven = true;
743 return true;
744 } else {
745 // May throw exception. Don't add deoptimization.
746 // Keep bounds checks in the loops.
747 return false;
748 }
749 }
750 // Can benefit from deoptimization.
751 return true;
752 }
753
Mingyao Yangbca381a2015-05-19 16:01:59 -0700754 // Try to filter out cases that the loop entry test will never be true.
755 bool LoopEntryTestUseful() {
756 if (initial_->IsIntConstant() && end_->IsIntConstant()) {
757 int32_t initial_val = initial_->AsIntConstant()->GetValue();
758 int32_t end_val = end_->AsIntConstant()->GetValue();
759 if (increment_ == 1) {
760 if (inclusive_) {
761 return initial_val > end_val;
762 } else {
763 return initial_val >= end_val;
764 }
765 } else {
Andreas Gampe38fad462015-06-10 18:33:26 -0700766 DCHECK_EQ(increment_, -1);
Mingyao Yangbca381a2015-05-19 16:01:59 -0700767 if (inclusive_) {
768 return initial_val < end_val;
769 } else {
770 return initial_val <= end_val;
771 }
772 }
773 }
774 return true;
775 }
776
777 // Returns the block for adding deoptimization.
778 HBasicBlock* TransformLoopForDeoptimizationIfNeeded() {
779 HBasicBlock* header = induction_variable_->GetBlock();
780 DCHECK(header->IsLoopHeader());
781 HBasicBlock* pre_header = header->GetLoopInformation()->GetPreHeader();
782 // Deoptimization is only added when both initial_ and end_ are defined
783 // before the loop.
784 DCHECK(initial_->GetBlock()->Dominates(pre_header));
785 DCHECK(end_->GetBlock()->Dominates(pre_header));
786
787 // If it can be proven the loop body is definitely entered (unless exception
788 // is thrown in the loop header for which triggering deoptimization is fine),
789 // there is no need for tranforming the loop. In that case, deoptimization
790 // will just be added in the loop pre-header.
791 if (!LoopEntryTestUseful()) {
792 return pre_header;
793 }
794
795 HGraph* graph = header->GetGraph();
796 graph->TransformLoopHeaderForBCE(header);
797 HBasicBlock* new_pre_header = header->GetDominator();
798 DCHECK(new_pre_header == header->GetLoopInformation()->GetPreHeader());
799 HBasicBlock* if_block = new_pre_header->GetDominator();
800 HBasicBlock* dummy_block = if_block->GetSuccessors().Get(0); // True successor.
801 HBasicBlock* deopt_block = if_block->GetSuccessors().Get(1); // False successor.
802
803 dummy_block->AddInstruction(new (graph->GetArena()) HGoto());
804 deopt_block->AddInstruction(new (graph->GetArena()) HGoto());
805 new_pre_header->AddInstruction(new (graph->GetArena()) HGoto());
806 return deopt_block;
807 }
808
809 // Adds a test between initial_ and end_ to see if the loop body is entered.
810 // If the loop body isn't entered at all, it jumps to the loop pre-header (after
811 // transformation) to avoid any deoptimization.
812 void AddLoopBodyEntryTest() {
813 HBasicBlock* header = induction_variable_->GetBlock();
814 DCHECK(header->IsLoopHeader());
815 HBasicBlock* pre_header = header->GetLoopInformation()->GetPreHeader();
816 HBasicBlock* if_block = pre_header->GetDominator();
817 HGraph* graph = header->GetGraph();
818
819 HCondition* cond;
820 if (increment_ == 1) {
821 if (inclusive_) {
822 cond = new (graph->GetArena()) HGreaterThan(initial_, end_);
823 } else {
824 cond = new (graph->GetArena()) HGreaterThanOrEqual(initial_, end_);
825 }
826 } else {
Andreas Gampe38fad462015-06-10 18:33:26 -0700827 DCHECK_EQ(increment_, -1);
Mingyao Yangbca381a2015-05-19 16:01:59 -0700828 if (inclusive_) {
829 cond = new (graph->GetArena()) HLessThan(initial_, end_);
830 } else {
831 cond = new (graph->GetArena()) HLessThanOrEqual(initial_, end_);
832 }
833 }
834 HIf* h_if = new (graph->GetArena()) HIf(cond);
835 if_block->AddInstruction(cond);
836 if_block->AddInstruction(h_if);
837 }
838
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700839 // Adds a check that (value >= constant), and HDeoptimize otherwise.
840 void AddDeoptimizationConstant(HInstruction* value,
Mingyao Yangbca381a2015-05-19 16:01:59 -0700841 int32_t constant,
842 HBasicBlock* deopt_block,
843 bool loop_entry_test_block_added) {
844 HBasicBlock* header = induction_variable_->GetBlock();
845 DCHECK(header->IsLoopHeader());
846 HBasicBlock* pre_header = header->GetDominator();
847 if (loop_entry_test_block_added) {
848 DCHECK(deopt_block->GetSuccessors().Get(0) == pre_header);
849 } else {
850 DCHECK(deopt_block == pre_header);
851 }
852 HGraph* graph = header->GetGraph();
853 HSuspendCheck* suspend_check = header->GetLoopInformation()->GetSuspendCheck();
854 if (loop_entry_test_block_added) {
855 DCHECK_EQ(deopt_block, header->GetDominator()->GetDominator()->GetSuccessors().Get(1));
856 }
857
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700858 HIntConstant* const_instr = graph->GetIntConstant(constant);
859 HCondition* cond = new (graph->GetArena()) HLessThan(value, const_instr);
860 HDeoptimize* deoptimize = new (graph->GetArena())
861 HDeoptimize(cond, suspend_check->GetDexPc());
Mingyao Yangbca381a2015-05-19 16:01:59 -0700862 deopt_block->InsertInstructionBefore(cond, deopt_block->GetLastInstruction());
863 deopt_block->InsertInstructionBefore(deoptimize, deopt_block->GetLastInstruction());
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700864 deoptimize->CopyEnvironmentFromWithLoopPhiAdjustment(
Mingyao Yangbca381a2015-05-19 16:01:59 -0700865 suspend_check->GetEnvironment(), header);
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700866 }
867
868 // Returns true if adding a (value <= array_length + offset) check for deoptimization
869 // is allowed and will benefit compiled code.
870 bool CanAddDeoptimizationArrayLength(HInstruction* value,
871 HArrayLength* array_length,
872 int32_t offset,
873 bool* is_proven) {
874 *is_proven = false;
Mingyao Yangbca381a2015-05-19 16:01:59 -0700875 HBasicBlock* header = induction_variable_->GetBlock();
876 DCHECK(header->IsLoopHeader());
877 HBasicBlock* pre_header = header->GetLoopInformation()->GetPreHeader();
878 DCHECK(value->GetBlock()->Dominates(pre_header));
879
880 if (array_length->GetBlock() == header) {
881 // array_length_in_loop_body_if_needed only has correct value when the loop
882 // body is entered. We bail out in this case. Usually array_length defined
883 // in the loop header is already hoisted by licm.
884 return false;
885 } else {
886 // array_length is defined either before the loop header already, or in
887 // the loop body since it's used in the loop body. If it's defined in the loop body,
888 // a phi array_length_in_loop_body_if_needed is used to replace it. In that case,
889 // all the uses of array_length must be dominated by its definition in the loop
890 // body. array_length_in_loop_body_if_needed is guaranteed to be the same as
891 // array_length once the loop body is entered so all the uses of the phi will
892 // use the correct value.
893 }
894
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700895 if (offset > 0) {
896 // There might be overflow issue.
897 // TODO: handle this, possibly with some distance relationship between
898 // offset_low and offset_high, or using another deoptimization to make
899 // sure (array_length + offset) doesn't overflow.
900 return false;
901 }
902
903 // See if we can prove the relationship first.
904 if (value == array_length) {
905 if (offset >= 0) {
906 // Already true.
907 *is_proven = true;
908 return true;
909 } else {
910 // May throw exception. Don't add deoptimization.
911 // Keep bounds checks in the loops.
912 return false;
913 }
914 }
915 // Can benefit from deoptimization.
916 return true;
917 }
918
919 // Adds a check that (value <= array_length + offset), and HDeoptimize otherwise.
920 void AddDeoptimizationArrayLength(HInstruction* value,
921 HArrayLength* array_length,
Mingyao Yangbca381a2015-05-19 16:01:59 -0700922 int32_t offset,
923 HBasicBlock* deopt_block,
924 bool loop_entry_test_block_added) {
925 HBasicBlock* header = induction_variable_->GetBlock();
926 DCHECK(header->IsLoopHeader());
927 HBasicBlock* pre_header = header->GetDominator();
928 if (loop_entry_test_block_added) {
929 DCHECK(deopt_block->GetSuccessors().Get(0) == pre_header);
930 } else {
931 DCHECK(deopt_block == pre_header);
932 }
933 HGraph* graph = header->GetGraph();
934 HSuspendCheck* suspend_check = header->GetLoopInformation()->GetSuspendCheck();
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700935
936 // We may need to hoist null-check and array_length out of loop first.
Mingyao Yangbca381a2015-05-19 16:01:59 -0700937 if (!array_length->GetBlock()->Dominates(deopt_block)) {
938 // array_length must be defined in the loop body.
939 DCHECK(header->GetLoopInformation()->Contains(*array_length->GetBlock()));
940 DCHECK(array_length->GetBlock() != header);
941
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700942 HInstruction* array = array_length->InputAt(0);
943 HNullCheck* null_check = array->AsNullCheck();
944 if (null_check != nullptr) {
945 array = null_check->InputAt(0);
946 }
Mingyao Yangbca381a2015-05-19 16:01:59 -0700947 // We've already made sure the array is defined before the loop when collecting
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700948 // array accesses for the loop.
Mingyao Yangbca381a2015-05-19 16:01:59 -0700949 DCHECK(array->GetBlock()->Dominates(deopt_block));
950 if (null_check != nullptr && !null_check->GetBlock()->Dominates(deopt_block)) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700951 // Hoist null check out of loop with a deoptimization.
952 HNullConstant* null_constant = graph->GetNullConstant();
953 HCondition* null_check_cond = new (graph->GetArena()) HEqual(array, null_constant);
954 // TODO: for one dex_pc, share the same deoptimization slow path.
955 HDeoptimize* null_check_deoptimize = new (graph->GetArena())
956 HDeoptimize(null_check_cond, suspend_check->GetDexPc());
Mingyao Yangbca381a2015-05-19 16:01:59 -0700957 deopt_block->InsertInstructionBefore(
958 null_check_cond, deopt_block->GetLastInstruction());
959 deopt_block->InsertInstructionBefore(
960 null_check_deoptimize, deopt_block->GetLastInstruction());
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700961 // Eliminate null check in the loop.
962 null_check->ReplaceWith(array);
963 null_check->GetBlock()->RemoveInstruction(null_check);
964 null_check_deoptimize->CopyEnvironmentFromWithLoopPhiAdjustment(
Mingyao Yangbca381a2015-05-19 16:01:59 -0700965 suspend_check->GetEnvironment(), header);
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700966 }
Mingyao Yangbca381a2015-05-19 16:01:59 -0700967
968 HArrayLength* new_array_length = new (graph->GetArena()) HArrayLength(array);
969 deopt_block->InsertInstructionBefore(new_array_length, deopt_block->GetLastInstruction());
970
971 if (loop_entry_test_block_added) {
972 // Replace array_length defined inside the loop body with a phi
973 // array_length_in_loop_body_if_needed. This is a synthetic phi so there is
974 // no vreg number for it.
975 HPhi* phi = new (graph->GetArena()) HPhi(
976 graph->GetArena(), kNoRegNumber, 2, Primitive::kPrimInt);
977 // Set to 0 if the loop body isn't entered.
978 phi->SetRawInputAt(0, graph->GetIntConstant(0));
979 // Set to array.length if the loop body is entered.
980 phi->SetRawInputAt(1, new_array_length);
981 pre_header->AddPhi(phi);
982 array_length->ReplaceWith(phi);
983 // Make sure phi is only used after the loop body is entered.
984 if (kIsDebugBuild) {
985 for (HUseIterator<HInstruction*> it(phi->GetUses());
986 !it.Done();
987 it.Advance()) {
988 HInstruction* user = it.Current()->GetUser();
989 DCHECK(GetLoopHeaderSuccesorInLoop()->Dominates(user->GetBlock()));
990 }
991 }
992 } else {
993 array_length->ReplaceWith(new_array_length);
994 }
995
996 array_length->GetBlock()->RemoveInstruction(array_length);
997 // Use new_array_length for deopt.
998 array_length = new_array_length;
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700999 }
1000
Mingyao Yangbca381a2015-05-19 16:01:59 -07001001 HInstruction* added = array_length;
1002 if (offset != 0) {
1003 HIntConstant* offset_instr = graph->GetIntConstant(offset);
1004 added = new (graph->GetArena()) HAdd(Primitive::kPrimInt, array_length, offset_instr);
1005 deopt_block->InsertInstructionBefore(added, deopt_block->GetLastInstruction());
1006 }
1007 HCondition* cond = new (graph->GetArena()) HGreaterThan(value, added);
1008 HDeoptimize* deopt = new (graph->GetArena()) HDeoptimize(cond, suspend_check->GetDexPc());
1009 deopt_block->InsertInstructionBefore(cond, deopt_block->GetLastInstruction());
1010 deopt_block->InsertInstructionBefore(deopt, deopt_block->GetLastInstruction());
1011 deopt->CopyEnvironmentFromWithLoopPhiAdjustment(suspend_check->GetEnvironment(), header);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001012 }
1013
Mingyao Yangbca381a2015-05-19 16:01:59 -07001014 // Adds deoptimizations in loop pre-header with the collected array access
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001015 // data so that value ranges can be established in loop body.
1016 // Returns true if deoptimizations are successfully added, or if it's proven
1017 // it's not necessary.
1018 bool AddDeoptimization(const ArrayAccessInsideLoopFinder& finder) {
1019 int32_t offset_low = finder.GetOffsetLow();
1020 int32_t offset_high = finder.GetOffsetHigh();
1021 HArrayLength* array_length = finder.GetFoundArrayLength();
1022
1023 HBasicBlock* pre_header =
1024 induction_variable_->GetBlock()->GetLoopInformation()->GetPreHeader();
1025 if (!initial_->GetBlock()->Dominates(pre_header) ||
1026 !end_->GetBlock()->Dominates(pre_header)) {
1027 // Can't move initial_ or end_ into pre_header for comparisons.
1028 return false;
1029 }
1030
Mingyao Yangbca381a2015-05-19 16:01:59 -07001031 HBasicBlock* deopt_block;
1032 bool loop_entry_test_block_added = false;
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001033 bool is_constant_proven, is_length_proven;
Mingyao Yangbca381a2015-05-19 16:01:59 -07001034
1035 HInstruction* const_comparing_instruction;
1036 int32_t const_compared_to;
1037 HInstruction* array_length_comparing_instruction;
1038 int32_t array_length_offset;
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001039 if (increment_ == 1) {
1040 // Increasing from initial_ to end_.
Mingyao Yangbca381a2015-05-19 16:01:59 -07001041 const_comparing_instruction = initial_;
1042 const_compared_to = -offset_low;
1043 array_length_comparing_instruction = end_;
1044 array_length_offset = inclusive_ ? -offset_high - 1 : -offset_high;
1045 } else {
1046 const_comparing_instruction = end_;
1047 const_compared_to = inclusive_ ? -offset_low : -offset_low - 1;
1048 array_length_comparing_instruction = initial_;
1049 array_length_offset = -offset_high - 1;
1050 }
1051
1052 if (CanAddDeoptimizationConstant(const_comparing_instruction,
1053 const_compared_to,
1054 &is_constant_proven) &&
1055 CanAddDeoptimizationArrayLength(array_length_comparing_instruction,
1056 array_length,
1057 array_length_offset,
1058 &is_length_proven)) {
1059 if (!is_constant_proven || !is_length_proven) {
1060 deopt_block = TransformLoopForDeoptimizationIfNeeded();
1061 loop_entry_test_block_added = (deopt_block != pre_header);
1062 if (loop_entry_test_block_added) {
1063 // Loop body may be entered.
1064 AddLoopBodyEntryTest();
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001065 }
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001066 }
Mingyao Yangbca381a2015-05-19 16:01:59 -07001067 if (!is_constant_proven) {
1068 AddDeoptimizationConstant(const_comparing_instruction,
1069 const_compared_to,
1070 deopt_block,
1071 loop_entry_test_block_added);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001072 }
Mingyao Yangbca381a2015-05-19 16:01:59 -07001073 if (!is_length_proven) {
1074 AddDeoptimizationArrayLength(array_length_comparing_instruction,
1075 array_length,
1076 array_length_offset,
1077 deopt_block,
1078 loop_entry_test_block_added);
1079 }
1080 return true;
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001081 }
1082 return false;
1083 }
1084
Mingyao Yangf384f882014-10-22 16:08:18 -07001085 private:
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001086 HPhi* const induction_variable_; // Induction variable for this monotonic value range.
1087 HInstruction* const initial_; // Initial value.
1088 HInstruction* end_; // End value.
1089 bool inclusive_; // Whether end value is inclusive.
1090 const int32_t increment_; // Increment for each loop iteration.
1091 const ValueBound bound_; // Additional value bound info for initial_.
Mingyao Yangf384f882014-10-22 16:08:18 -07001092
1093 DISALLOW_COPY_AND_ASSIGN(MonotonicValueRange);
1094};
1095
1096class BCEVisitor : public HGraphVisitor {
1097 public:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001098 // The least number of bounds checks that should be eliminated by triggering
1099 // the deoptimization technique.
1100 static constexpr size_t kThresholdForAddingDeoptimize = 2;
1101
1102 // Very large constant index is considered as an anomaly. This is a threshold
1103 // beyond which we don't bother to apply the deoptimization technique since
1104 // it's likely some AIOOBE will be thrown.
1105 static constexpr int32_t kMaxConstantForAddingDeoptimize = INT_MAX - 1024 * 1024;
1106
Mingyao Yangbca381a2015-05-19 16:01:59 -07001107 // Added blocks for loop body entry test.
1108 bool IsAddedBlock(HBasicBlock* block) const {
1109 return block->GetBlockId() >= initial_block_size_;
1110 }
1111
Andreas Gampe0418b5b2014-12-04 17:24:50 -08001112 explicit BCEVisitor(HGraph* graph)
Mingyao Yangbca381a2015-05-19 16:01:59 -07001113 : HGraphVisitor(graph), maps_(graph->GetBlocks().Size()),
1114 need_to_revisit_block_(false), initial_block_size_(graph->GetBlocks().Size()) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001115
1116 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Mingyao Yangbca381a2015-05-19 16:01:59 -07001117 DCHECK(!IsAddedBlock(block));
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001118 first_constant_index_bounds_check_map_.clear();
1119 HGraphVisitor::VisitBasicBlock(block);
1120 if (need_to_revisit_block_) {
1121 AddComparesWithDeoptimization(block);
1122 need_to_revisit_block_ = false;
1123 first_constant_index_bounds_check_map_.clear();
1124 GetValueRangeMap(block)->clear();
1125 HGraphVisitor::VisitBasicBlock(block);
1126 }
1127 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001128
1129 private:
1130 // Return the map of proven value ranges at the beginning of a basic block.
1131 ArenaSafeMap<int, ValueRange*>* GetValueRangeMap(HBasicBlock* basic_block) {
Mingyao Yangbca381a2015-05-19 16:01:59 -07001132 if (IsAddedBlock(basic_block)) {
1133 // Added blocks don't keep value ranges.
1134 return nullptr;
1135 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001136 int block_id = basic_block->GetBlockId();
1137 if (maps_.at(block_id) == nullptr) {
1138 std::unique_ptr<ArenaSafeMap<int, ValueRange*>> map(
1139 new ArenaSafeMap<int, ValueRange*>(
1140 std::less<int>(), GetGraph()->GetArena()->Adapter()));
1141 maps_.at(block_id) = std::move(map);
1142 }
1143 return maps_.at(block_id).get();
1144 }
1145
1146 // Traverse up the dominator tree to look for value range info.
1147 ValueRange* LookupValueRange(HInstruction* instruction, HBasicBlock* basic_block) {
1148 while (basic_block != nullptr) {
1149 ArenaSafeMap<int, ValueRange*>* map = GetValueRangeMap(basic_block);
Mingyao Yangbca381a2015-05-19 16:01:59 -07001150 if (map != nullptr) {
1151 if (map->find(instruction->GetId()) != map->end()) {
1152 return map->Get(instruction->GetId());
1153 }
1154 } else {
1155 DCHECK(IsAddedBlock(basic_block));
Mingyao Yangf384f882014-10-22 16:08:18 -07001156 }
1157 basic_block = basic_block->GetDominator();
1158 }
1159 // Didn't find any.
1160 return nullptr;
1161 }
1162
Mingyao Yang0304e182015-01-30 16:41:29 -08001163 // Narrow the value range of `instruction` at the end of `basic_block` with `range`,
1164 // and push the narrowed value range to `successor`.
Mingyao Yangf384f882014-10-22 16:08:18 -07001165 void ApplyRangeFromComparison(HInstruction* instruction, HBasicBlock* basic_block,
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001166 HBasicBlock* successor, ValueRange* range) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001167 ValueRange* existing_range = LookupValueRange(instruction, basic_block);
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001168 if (existing_range == nullptr) {
1169 if (range != nullptr) {
1170 GetValueRangeMap(successor)->Overwrite(instruction->GetId(), range);
1171 }
1172 return;
1173 }
1174 if (existing_range->IsMonotonicValueRange()) {
1175 DCHECK(instruction->IsLoopHeaderPhi());
1176 // Make sure the comparison is in the loop header so each increment is
1177 // checked with a comparison.
1178 if (instruction->GetBlock() != basic_block) {
1179 return;
1180 }
1181 }
1182 ValueRange* narrowed_range = existing_range->Narrow(range);
Mingyao Yangf384f882014-10-22 16:08:18 -07001183 if (narrowed_range != nullptr) {
1184 GetValueRangeMap(successor)->Overwrite(instruction->GetId(), narrowed_range);
1185 }
1186 }
1187
Mingyao Yang57e04752015-02-09 18:13:26 -08001188 // Special case that we may simultaneously narrow two MonotonicValueRange's to
1189 // regular value ranges.
1190 void HandleIfBetweenTwoMonotonicValueRanges(HIf* instruction,
1191 HInstruction* left,
1192 HInstruction* right,
1193 IfCondition cond,
1194 MonotonicValueRange* left_range,
1195 MonotonicValueRange* right_range) {
1196 DCHECK(left->IsLoopHeaderPhi());
1197 DCHECK(right->IsLoopHeaderPhi());
1198 if (instruction->GetBlock() != left->GetBlock()) {
1199 // Comparison needs to be in loop header to make sure it's done after each
1200 // increment/decrement.
1201 return;
1202 }
1203
1204 // Handle common cases which also don't have overflow/underflow concerns.
1205 if (left_range->GetIncrement() == 1 &&
1206 left_range->GetBound().IsConstant() &&
1207 right_range->GetIncrement() == -1 &&
1208 right_range->GetBound().IsRelatedToArrayLength() &&
1209 right_range->GetBound().GetConstant() < 0) {
Mingyao Yang57e04752015-02-09 18:13:26 -08001210 HBasicBlock* successor = nullptr;
1211 int32_t left_compensation = 0;
1212 int32_t right_compensation = 0;
1213 if (cond == kCondLT) {
1214 left_compensation = -1;
1215 right_compensation = 1;
1216 successor = instruction->IfTrueSuccessor();
1217 } else if (cond == kCondLE) {
1218 successor = instruction->IfTrueSuccessor();
1219 } else if (cond == kCondGT) {
1220 successor = instruction->IfFalseSuccessor();
1221 } else if (cond == kCondGE) {
1222 left_compensation = -1;
1223 right_compensation = 1;
1224 successor = instruction->IfFalseSuccessor();
1225 } else {
1226 // We don't handle '=='/'!=' test in case left and right can cross and
1227 // miss each other.
1228 return;
1229 }
1230
1231 if (successor != nullptr) {
1232 bool overflow;
1233 bool underflow;
1234 ValueRange* new_left_range = new (GetGraph()->GetArena()) ValueRange(
1235 GetGraph()->GetArena(),
1236 left_range->GetBound(),
1237 right_range->GetBound().Add(left_compensation, &overflow, &underflow));
1238 if (!overflow && !underflow) {
1239 ApplyRangeFromComparison(left, instruction->GetBlock(), successor,
1240 new_left_range);
1241 }
1242
1243 ValueRange* new_right_range = new (GetGraph()->GetArena()) ValueRange(
1244 GetGraph()->GetArena(),
1245 left_range->GetBound().Add(right_compensation, &overflow, &underflow),
1246 right_range->GetBound());
1247 if (!overflow && !underflow) {
1248 ApplyRangeFromComparison(right, instruction->GetBlock(), successor,
1249 new_right_range);
1250 }
1251 }
1252 }
1253 }
1254
Mingyao Yangf384f882014-10-22 16:08:18 -07001255 // Handle "if (left cmp_cond right)".
1256 void HandleIf(HIf* instruction, HInstruction* left, HInstruction* right, IfCondition cond) {
1257 HBasicBlock* block = instruction->GetBlock();
1258
1259 HBasicBlock* true_successor = instruction->IfTrueSuccessor();
1260 // There should be no critical edge at this point.
1261 DCHECK_EQ(true_successor->GetPredecessors().Size(), 1u);
1262
1263 HBasicBlock* false_successor = instruction->IfFalseSuccessor();
1264 // There should be no critical edge at this point.
1265 DCHECK_EQ(false_successor->GetPredecessors().Size(), 1u);
1266
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001267 ValueRange* left_range = LookupValueRange(left, block);
1268 MonotonicValueRange* left_monotonic_range = nullptr;
1269 if (left_range != nullptr) {
1270 left_monotonic_range = left_range->AsMonotonicValueRange();
1271 if (left_monotonic_range != nullptr) {
Mingyao Yangbca381a2015-05-19 16:01:59 -07001272 HBasicBlock* loop_head = left_monotonic_range->GetLoopHeader();
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001273 if (instruction->GetBlock() != loop_head) {
1274 // For monotonic value range, don't handle `instruction`
1275 // if it's not defined in the loop header.
1276 return;
1277 }
1278 }
1279 }
1280
Mingyao Yang64197522014-12-05 15:56:23 -08001281 bool found;
1282 ValueBound bound = ValueBound::DetectValueBoundFromValue(right, &found);
Mingyao Yang0304e182015-01-30 16:41:29 -08001283 // Each comparison can establish a lower bound and an upper bound
1284 // for the left hand side.
Mingyao Yangf384f882014-10-22 16:08:18 -07001285 ValueBound lower = bound;
1286 ValueBound upper = bound;
1287 if (!found) {
Mingyao Yang0304e182015-01-30 16:41:29 -08001288 // No constant or array.length+c format bound found.
Mingyao Yangf384f882014-10-22 16:08:18 -07001289 // 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 -08001290 ValueRange* right_range = LookupValueRange(right, block);
1291 if (right_range != nullptr) {
1292 if (right_range->IsMonotonicValueRange()) {
Mingyao Yang57e04752015-02-09 18:13:26 -08001293 if (left_range != nullptr && left_range->IsMonotonicValueRange()) {
1294 HandleIfBetweenTwoMonotonicValueRanges(instruction, left, right, cond,
1295 left_range->AsMonotonicValueRange(),
1296 right_range->AsMonotonicValueRange());
1297 return;
1298 }
1299 }
1300 lower = right_range->GetLower();
1301 upper = right_range->GetUpper();
Mingyao Yangf384f882014-10-22 16:08:18 -07001302 } else {
1303 lower = ValueBound::Min();
1304 upper = ValueBound::Max();
1305 }
1306 }
1307
Mingyao Yang0304e182015-01-30 16:41:29 -08001308 bool overflow, underflow;
Mingyao Yangf384f882014-10-22 16:08:18 -07001309 if (cond == kCondLT || cond == kCondLE) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001310 if (left_monotonic_range != nullptr) {
1311 // Update the info for monotonic value range.
1312 if (left_monotonic_range->GetInductionVariable() == left &&
1313 left_monotonic_range->GetIncrement() < 0 &&
Mingyao Yangbca381a2015-05-19 16:01:59 -07001314 block == left_monotonic_range->GetLoopHeader() &&
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001315 instruction->IfFalseSuccessor()->GetLoopInformation() == block->GetLoopInformation()) {
1316 left_monotonic_range->SetEnd(right);
1317 left_monotonic_range->SetInclusive(cond == kCondLT);
1318 }
1319 }
1320
Mingyao Yangf384f882014-10-22 16:08:18 -07001321 if (!upper.Equals(ValueBound::Max())) {
Mingyao Yang0304e182015-01-30 16:41:29 -08001322 int32_t compensation = (cond == kCondLT) ? -1 : 0; // upper bound is inclusive
1323 ValueBound new_upper = upper.Add(compensation, &overflow, &underflow);
1324 if (overflow || underflow) {
1325 return;
Mingyao Yang64197522014-12-05 15:56:23 -08001326 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001327 ValueRange* new_range = new (GetGraph()->GetArena())
1328 ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper);
1329 ApplyRangeFromComparison(left, block, true_successor, new_range);
1330 }
1331
1332 // array.length as a lower bound isn't considered useful.
Mingyao Yang0304e182015-01-30 16:41:29 -08001333 if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) {
1334 int32_t compensation = (cond == kCondLE) ? 1 : 0; // lower bound is inclusive
1335 ValueBound new_lower = lower.Add(compensation, &overflow, &underflow);
1336 if (overflow || underflow) {
1337 return;
Mingyao Yang64197522014-12-05 15:56:23 -08001338 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001339 ValueRange* new_range = new (GetGraph()->GetArena())
1340 ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max());
1341 ApplyRangeFromComparison(left, block, false_successor, new_range);
1342 }
1343 } else if (cond == kCondGT || cond == kCondGE) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001344 if (left_monotonic_range != nullptr) {
1345 // Update the info for monotonic value range.
1346 if (left_monotonic_range->GetInductionVariable() == left &&
1347 left_monotonic_range->GetIncrement() > 0 &&
Mingyao Yangbca381a2015-05-19 16:01:59 -07001348 block == left_monotonic_range->GetLoopHeader() &&
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001349 instruction->IfFalseSuccessor()->GetLoopInformation() == block->GetLoopInformation()) {
1350 left_monotonic_range->SetEnd(right);
1351 left_monotonic_range->SetInclusive(cond == kCondGT);
1352 }
1353 }
1354
Mingyao Yangf384f882014-10-22 16:08:18 -07001355 // array.length as a lower bound isn't considered useful.
Mingyao Yang0304e182015-01-30 16:41:29 -08001356 if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) {
1357 int32_t compensation = (cond == kCondGT) ? 1 : 0; // lower bound is inclusive
1358 ValueBound new_lower = lower.Add(compensation, &overflow, &underflow);
1359 if (overflow || underflow) {
1360 return;
Mingyao Yang64197522014-12-05 15:56:23 -08001361 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001362 ValueRange* new_range = new (GetGraph()->GetArena())
1363 ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max());
1364 ApplyRangeFromComparison(left, block, true_successor, new_range);
1365 }
1366
1367 if (!upper.Equals(ValueBound::Max())) {
Mingyao Yang0304e182015-01-30 16:41:29 -08001368 int32_t compensation = (cond == kCondGE) ? -1 : 0; // upper bound is inclusive
1369 ValueBound new_upper = upper.Add(compensation, &overflow, &underflow);
1370 if (overflow || underflow) {
1371 return;
Mingyao Yang64197522014-12-05 15:56:23 -08001372 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001373 ValueRange* new_range = new (GetGraph()->GetArena())
1374 ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper);
1375 ApplyRangeFromComparison(left, block, false_successor, new_range);
1376 }
1377 }
1378 }
1379
1380 void VisitBoundsCheck(HBoundsCheck* bounds_check) {
1381 HBasicBlock* block = bounds_check->GetBlock();
1382 HInstruction* index = bounds_check->InputAt(0);
1383 HInstruction* array_length = bounds_check->InputAt(1);
Mingyao Yangbca381a2015-05-19 16:01:59 -07001384 DCHECK(array_length->IsIntConstant() ||
1385 array_length->IsArrayLength() ||
1386 array_length->IsPhi());
1387
1388 if (array_length->IsPhi()) {
1389 // Input 1 of the phi contains the real array.length once the loop body is
1390 // entered. That value will be used for bound analysis. The graph is still
1391 // strickly in SSA form.
1392 array_length = array_length->AsPhi()->InputAt(1)->AsArrayLength();
1393 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001394
Mingyao Yang0304e182015-01-30 16:41:29 -08001395 if (!index->IsIntConstant()) {
1396 ValueRange* index_range = LookupValueRange(index, block);
1397 if (index_range != nullptr) {
1398 ValueBound lower = ValueBound(nullptr, 0); // constant 0
1399 ValueBound upper = ValueBound(array_length, -1); // array_length - 1
1400 ValueRange* array_range = new (GetGraph()->GetArena())
1401 ValueRange(GetGraph()->GetArena(), lower, upper);
1402 if (index_range->FitsIn(array_range)) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001403 ReplaceBoundsCheck(bounds_check, index);
1404 return;
1405 }
1406 }
Mingyao Yang0304e182015-01-30 16:41:29 -08001407 } else {
1408 int32_t constant = index->AsIntConstant()->GetValue();
1409 if (constant < 0) {
1410 // Will always throw exception.
1411 return;
1412 }
1413 if (array_length->IsIntConstant()) {
1414 if (constant < array_length->AsIntConstant()->GetValue()) {
1415 ReplaceBoundsCheck(bounds_check, index);
1416 }
1417 return;
1418 }
1419
1420 DCHECK(array_length->IsArrayLength());
1421 ValueRange* existing_range = LookupValueRange(array_length, block);
1422 if (existing_range != nullptr) {
1423 ValueBound lower = existing_range->GetLower();
1424 DCHECK(lower.IsConstant());
1425 if (constant < lower.GetConstant()) {
1426 ReplaceBoundsCheck(bounds_check, index);
1427 return;
1428 } else {
1429 // Existing range isn't strong enough to eliminate the bounds check.
1430 // Fall through to update the array_length range with info from this
1431 // bounds check.
1432 }
1433 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001434
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001435 if (first_constant_index_bounds_check_map_.find(array_length->GetId()) ==
1436 first_constant_index_bounds_check_map_.end()) {
1437 // Remember the first bounds check against array_length of a constant index.
1438 // That bounds check instruction has an associated HEnvironment where we
1439 // may add an HDeoptimize to eliminate bounds checks of constant indices
1440 // against array_length.
1441 first_constant_index_bounds_check_map_.Put(array_length->GetId(), bounds_check);
1442 } else {
1443 // We've seen it at least twice. It's beneficial to introduce a compare with
1444 // deoptimization fallback to eliminate the bounds checks.
1445 need_to_revisit_block_ = true;
1446 }
1447
Mingyao Yangf384f882014-10-22 16:08:18 -07001448 // Once we have an array access like 'array[5] = 1', we record array.length >= 6.
Mingyao Yang0304e182015-01-30 16:41:29 -08001449 // We currently don't do it for non-constant index since a valid array[i] can't prove
1450 // a valid array[i-1] yet due to the lower bound side.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001451 if (constant == INT_MAX) {
1452 // INT_MAX as an index will definitely throw AIOOBE.
1453 return;
1454 }
Mingyao Yang64197522014-12-05 15:56:23 -08001455 ValueBound lower = ValueBound(nullptr, constant + 1);
Mingyao Yangf384f882014-10-22 16:08:18 -07001456 ValueBound upper = ValueBound::Max();
1457 ValueRange* range = new (GetGraph()->GetArena())
1458 ValueRange(GetGraph()->GetArena(), lower, upper);
Mingyao Yang0304e182015-01-30 16:41:29 -08001459 GetValueRangeMap(block)->Overwrite(array_length->GetId(), range);
Mingyao Yangf384f882014-10-22 16:08:18 -07001460 }
1461 }
1462
1463 void ReplaceBoundsCheck(HInstruction* bounds_check, HInstruction* index) {
1464 bounds_check->ReplaceWith(index);
1465 bounds_check->GetBlock()->RemoveInstruction(bounds_check);
1466 }
1467
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001468 static bool HasSameInputAtBackEdges(HPhi* phi) {
1469 DCHECK(phi->IsLoopHeaderPhi());
1470 // Start with input 1. Input 0 is from the incoming block.
1471 HInstruction* input1 = phi->InputAt(1);
1472 DCHECK(phi->GetBlock()->GetLoopInformation()->IsBackEdge(
1473 *phi->GetBlock()->GetPredecessors().Get(1)));
1474 for (size_t i = 2, e = phi->InputCount(); i < e; ++i) {
1475 DCHECK(phi->GetBlock()->GetLoopInformation()->IsBackEdge(
1476 *phi->GetBlock()->GetPredecessors().Get(i)));
1477 if (input1 != phi->InputAt(i)) {
1478 return false;
1479 }
1480 }
1481 return true;
1482 }
1483
Mingyao Yangf384f882014-10-22 16:08:18 -07001484 void VisitPhi(HPhi* phi) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001485 if (phi->IsLoopHeaderPhi()
1486 && (phi->GetType() == Primitive::kPrimInt)
1487 && HasSameInputAtBackEdges(phi)) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001488 HInstruction* instruction = phi->InputAt(1);
Mingyao Yang0304e182015-01-30 16:41:29 -08001489 HInstruction *left;
1490 int32_t increment;
1491 if (ValueBound::IsAddOrSubAConstant(instruction, &left, &increment)) {
1492 if (left == phi) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001493 HInstruction* initial_value = phi->InputAt(0);
1494 ValueRange* range = nullptr;
Mingyao Yang64197522014-12-05 15:56:23 -08001495 if (increment == 0) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001496 // Add constant 0. It's really a fixed value.
1497 range = new (GetGraph()->GetArena()) ValueRange(
1498 GetGraph()->GetArena(),
Mingyao Yang64197522014-12-05 15:56:23 -08001499 ValueBound(initial_value, 0),
1500 ValueBound(initial_value, 0));
Mingyao Yangf384f882014-10-22 16:08:18 -07001501 } else {
1502 // Monotonically increasing/decreasing.
Mingyao Yang64197522014-12-05 15:56:23 -08001503 bool found;
1504 ValueBound bound = ValueBound::DetectValueBoundFromValue(
1505 initial_value, &found);
1506 if (!found) {
1507 // No constant or array.length+c bound found.
1508 // For i=j, we can still use j's upper bound as i's upper bound.
1509 // Same for lower.
1510 ValueRange* initial_range = LookupValueRange(initial_value, phi->GetBlock());
1511 if (initial_range != nullptr) {
1512 bound = increment > 0 ? initial_range->GetLower() :
1513 initial_range->GetUpper();
1514 } else {
1515 bound = increment > 0 ? ValueBound::Min() : ValueBound::Max();
1516 }
1517 }
1518 range = new (GetGraph()->GetArena()) MonotonicValueRange(
Mingyao Yangf384f882014-10-22 16:08:18 -07001519 GetGraph()->GetArena(),
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001520 phi,
Mingyao Yangf384f882014-10-22 16:08:18 -07001521 initial_value,
Mingyao Yang64197522014-12-05 15:56:23 -08001522 increment,
1523 bound);
Mingyao Yangf384f882014-10-22 16:08:18 -07001524 }
1525 GetValueRangeMap(phi->GetBlock())->Overwrite(phi->GetId(), range);
1526 }
1527 }
1528 }
1529 }
1530
1531 void VisitIf(HIf* instruction) {
1532 if (instruction->InputAt(0)->IsCondition()) {
1533 HCondition* cond = instruction->InputAt(0)->AsCondition();
1534 IfCondition cmp = cond->GetCondition();
1535 if (cmp == kCondGT || cmp == kCondGE ||
1536 cmp == kCondLT || cmp == kCondLE) {
1537 HInstruction* left = cond->GetLeft();
1538 HInstruction* right = cond->GetRight();
1539 HandleIf(instruction, left, right, cmp);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001540
1541 HBasicBlock* block = instruction->GetBlock();
1542 ValueRange* left_range = LookupValueRange(left, block);
1543 if (left_range == nullptr) {
1544 return;
1545 }
1546
1547 if (left_range->IsMonotonicValueRange() &&
Mingyao Yangbca381a2015-05-19 16:01:59 -07001548 block == left_range->AsMonotonicValueRange()->GetLoopHeader()) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001549 // The comparison is for an induction variable in the loop header.
1550 DCHECK(left == left_range->AsMonotonicValueRange()->GetInductionVariable());
Mingyao Yangbca381a2015-05-19 16:01:59 -07001551 HBasicBlock* loop_body_successor =
1552 left_range->AsMonotonicValueRange()->GetLoopHeaderSuccesorInLoop();
1553 if (loop_body_successor == nullptr) {
1554 // In case it's some strange loop structure.
1555 return;
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001556 }
1557 ValueRange* new_left_range = LookupValueRange(left, loop_body_successor);
Mingyao Yangbca381a2015-05-19 16:01:59 -07001558 if ((new_left_range == left_range) ||
1559 // Range narrowed with deoptimization is usually more useful than
1560 // a constant range.
1561 new_left_range->IsConstantValueRange()) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001562 // We are not successful in narrowing the monotonic value range to
1563 // a regular value range. Try using deoptimization.
1564 new_left_range = left_range->AsMonotonicValueRange()->
1565 NarrowWithDeoptimization();
1566 if (new_left_range != left_range) {
Mingyao Yangbca381a2015-05-19 16:01:59 -07001567 GetValueRangeMap(loop_body_successor)->Overwrite(left->GetId(), new_left_range);
Mingyao Yang206d6fd2015-04-13 16:46:28 -07001568 }
1569 }
1570 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001571 }
1572 }
1573 }
1574
1575 void VisitAdd(HAdd* add) {
1576 HInstruction* right = add->GetRight();
1577 if (right->IsIntConstant()) {
1578 ValueRange* left_range = LookupValueRange(add->GetLeft(), add->GetBlock());
1579 if (left_range == nullptr) {
1580 return;
1581 }
1582 ValueRange* range = left_range->Add(right->AsIntConstant()->GetValue());
1583 if (range != nullptr) {
1584 GetValueRangeMap(add->GetBlock())->Overwrite(add->GetId(), range);
1585 }
1586 }
1587 }
1588
1589 void VisitSub(HSub* sub) {
1590 HInstruction* left = sub->GetLeft();
1591 HInstruction* right = sub->GetRight();
1592 if (right->IsIntConstant()) {
1593 ValueRange* left_range = LookupValueRange(left, sub->GetBlock());
1594 if (left_range == nullptr) {
1595 return;
1596 }
1597 ValueRange* range = left_range->Add(-right->AsIntConstant()->GetValue());
1598 if (range != nullptr) {
1599 GetValueRangeMap(sub->GetBlock())->Overwrite(sub->GetId(), range);
1600 return;
1601 }
1602 }
1603
1604 // Here we are interested in the typical triangular case of nested loops,
1605 // such as the inner loop 'for (int j=0; j<array.length-i; j++)' where i
1606 // 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 -08001607
1608 // Try to handle (array.length - i) or (array.length + c - i) format.
1609 HInstruction* left_of_left; // left input of left.
1610 int32_t right_const = 0;
1611 if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &right_const)) {
1612 left = left_of_left;
1613 }
1614 // The value of left input of the sub equals (left + right_const).
1615
Mingyao Yangf384f882014-10-22 16:08:18 -07001616 if (left->IsArrayLength()) {
1617 HInstruction* array_length = left->AsArrayLength();
1618 ValueRange* right_range = LookupValueRange(right, sub->GetBlock());
1619 if (right_range != nullptr) {
1620 ValueBound lower = right_range->GetLower();
1621 ValueBound upper = right_range->GetUpper();
Mingyao Yang0304e182015-01-30 16:41:29 -08001622 if (lower.IsConstant() && upper.IsRelatedToArrayLength()) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001623 HInstruction* upper_inst = upper.GetInstruction();
Mingyao Yang0304e182015-01-30 16:41:29 -08001624 // Make sure it's the same array.
1625 if (ValueBound::Equal(array_length, upper_inst)) {
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001626 int32_t c0 = right_const;
1627 int32_t c1 = lower.GetConstant();
1628 int32_t c2 = upper.GetConstant();
1629 // (array.length + c0 - v) where v is in [c1, array.length + c2]
1630 // gets [c0 - c2, array.length + c0 - c1] as its value range.
1631 if (!ValueBound::WouldAddOverflowOrUnderflow(c0, -c2) &&
1632 !ValueBound::WouldAddOverflowOrUnderflow(c0, -c1)) {
1633 if ((c0 - c1) <= 0) {
1634 // array.length + (c0 - c1) won't overflow/underflow.
1635 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1636 GetGraph()->GetArena(),
1637 ValueBound(nullptr, right_const - upper.GetConstant()),
1638 ValueBound(array_length, right_const - lower.GetConstant()));
1639 GetValueRangeMap(sub->GetBlock())->Overwrite(sub->GetId(), range);
1640 }
1641 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001642 }
1643 }
1644 }
1645 }
1646 }
1647
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001648 void FindAndHandlePartialArrayLength(HBinaryOperation* instruction) {
1649 DCHECK(instruction->IsDiv() || instruction->IsShr() || instruction->IsUShr());
1650 HInstruction* right = instruction->GetRight();
1651 int32_t right_const;
1652 if (right->IsIntConstant()) {
1653 right_const = right->AsIntConstant()->GetValue();
1654 // Detect division by two or more.
1655 if ((instruction->IsDiv() && right_const <= 1) ||
1656 (instruction->IsShr() && right_const < 1) ||
1657 (instruction->IsUShr() && right_const < 1)) {
1658 return;
1659 }
1660 } else {
1661 return;
1662 }
1663
1664 // Try to handle array.length/2 or (array.length-1)/2 format.
1665 HInstruction* left = instruction->GetLeft();
1666 HInstruction* left_of_left; // left input of left.
1667 int32_t c = 0;
1668 if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &c)) {
1669 left = left_of_left;
1670 }
1671 // The value of left input of instruction equals (left + c).
1672
1673 // (array_length + 1) or smaller divided by two or more
1674 // always generate a value in [INT_MIN, array_length].
1675 // This is true even if array_length is INT_MAX.
1676 if (left->IsArrayLength() && c <= 1) {
1677 if (instruction->IsUShr() && c < 0) {
1678 // Make sure for unsigned shift, left side is not negative.
1679 // e.g. if array_length is 2, ((array_length - 3) >>> 2) is way bigger
1680 // than array_length.
1681 return;
1682 }
1683 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1684 GetGraph()->GetArena(),
1685 ValueBound(nullptr, INT_MIN),
1686 ValueBound(left, 0));
1687 GetValueRangeMap(instruction->GetBlock())->Overwrite(instruction->GetId(), range);
1688 }
1689 }
1690
1691 void VisitDiv(HDiv* div) {
1692 FindAndHandlePartialArrayLength(div);
1693 }
1694
1695 void VisitShr(HShr* shr) {
1696 FindAndHandlePartialArrayLength(shr);
1697 }
1698
1699 void VisitUShr(HUShr* ushr) {
1700 FindAndHandlePartialArrayLength(ushr);
1701 }
1702
Mingyao Yang4559f002015-02-27 14:43:53 -08001703 void VisitAnd(HAnd* instruction) {
1704 if (instruction->GetRight()->IsIntConstant()) {
1705 int32_t constant = instruction->GetRight()->AsIntConstant()->GetValue();
1706 if (constant > 0) {
1707 // constant serves as a mask so any number masked with it
1708 // gets a [0, constant] value range.
1709 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1710 GetGraph()->GetArena(),
1711 ValueBound(nullptr, 0),
1712 ValueBound(nullptr, constant));
1713 GetValueRangeMap(instruction->GetBlock())->Overwrite(instruction->GetId(), range);
1714 }
1715 }
1716 }
1717
Mingyao Yang0304e182015-01-30 16:41:29 -08001718 void VisitNewArray(HNewArray* new_array) {
1719 HInstruction* len = new_array->InputAt(0);
1720 if (!len->IsIntConstant()) {
1721 HInstruction *left;
1722 int32_t right_const;
1723 if (ValueBound::IsAddOrSubAConstant(len, &left, &right_const)) {
1724 // (left + right_const) is used as size to new the array.
1725 // We record "-right_const <= left <= new_array - right_const";
1726 ValueBound lower = ValueBound(nullptr, -right_const);
1727 // We use new_array for the bound instead of new_array.length,
1728 // which isn't available as an instruction yet. new_array will
1729 // be treated the same as new_array.length when it's used in a ValueBound.
1730 ValueBound upper = ValueBound(new_array, -right_const);
1731 ValueRange* range = new (GetGraph()->GetArena())
1732 ValueRange(GetGraph()->GetArena(), lower, upper);
1733 GetValueRangeMap(new_array->GetBlock())->Overwrite(left->GetId(), range);
1734 }
1735 }
1736 }
1737
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001738 void VisitDeoptimize(HDeoptimize* deoptimize) {
1739 // Right now it's only HLessThanOrEqual.
1740 DCHECK(deoptimize->InputAt(0)->IsLessThanOrEqual());
1741 HLessThanOrEqual* less_than_or_equal = deoptimize->InputAt(0)->AsLessThanOrEqual();
1742 HInstruction* instruction = less_than_or_equal->InputAt(0);
1743 if (instruction->IsArrayLength()) {
1744 HInstruction* constant = less_than_or_equal->InputAt(1);
1745 DCHECK(constant->IsIntConstant());
1746 DCHECK(constant->AsIntConstant()->GetValue() <= kMaxConstantForAddingDeoptimize);
1747 ValueBound lower = ValueBound(nullptr, constant->AsIntConstant()->GetValue() + 1);
1748 ValueRange* range = new (GetGraph()->GetArena())
1749 ValueRange(GetGraph()->GetArena(), lower, ValueBound::Max());
1750 GetValueRangeMap(deoptimize->GetBlock())->Overwrite(instruction->GetId(), range);
1751 }
1752 }
1753
1754 void AddCompareWithDeoptimization(HInstruction* array_length,
1755 HIntConstant* const_instr,
1756 HBasicBlock* block) {
1757 DCHECK(array_length->IsArrayLength());
1758 ValueRange* range = LookupValueRange(array_length, block);
1759 ValueBound lower_bound = range->GetLower();
1760 DCHECK(lower_bound.IsConstant());
1761 DCHECK(const_instr->GetValue() <= kMaxConstantForAddingDeoptimize);
Nicolas Geoffray7d4cc8c2015-06-20 23:49:01 +01001762 // Note that the lower bound of the array length may have been refined
1763 // through other instructions (such as `HNewArray(length - 4)`).
1764 DCHECK_LE(const_instr->GetValue() + 1, lower_bound.GetConstant());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001765
1766 // If array_length is less than lower_const, deoptimize.
1767 HBoundsCheck* bounds_check = first_constant_index_bounds_check_map_.Get(
1768 array_length->GetId())->AsBoundsCheck();
1769 HCondition* cond = new (GetGraph()->GetArena()) HLessThanOrEqual(array_length, const_instr);
1770 HDeoptimize* deoptimize = new (GetGraph()->GetArena())
1771 HDeoptimize(cond, bounds_check->GetDexPc());
1772 block->InsertInstructionBefore(cond, bounds_check);
1773 block->InsertInstructionBefore(deoptimize, bounds_check);
Nicolas Geoffray3dcd58c2015-04-03 11:02:38 +01001774 deoptimize->CopyEnvironmentFrom(bounds_check->GetEnvironment());
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001775 }
1776
1777 void AddComparesWithDeoptimization(HBasicBlock* block) {
1778 for (ArenaSafeMap<int, HBoundsCheck*>::iterator it =
1779 first_constant_index_bounds_check_map_.begin();
1780 it != first_constant_index_bounds_check_map_.end();
1781 ++it) {
1782 HBoundsCheck* bounds_check = it->second;
1783 HArrayLength* array_length = bounds_check->InputAt(1)->AsArrayLength();
1784 HIntConstant* lower_bound_const_instr = nullptr;
1785 int32_t lower_bound_const = INT_MIN;
1786 size_t counter = 0;
1787 // Count the constant indexing for which bounds checks haven't
1788 // been removed yet.
1789 for (HUseIterator<HInstruction*> it2(array_length->GetUses());
1790 !it2.Done();
1791 it2.Advance()) {
1792 HInstruction* user = it2.Current()->GetUser();
1793 if (user->GetBlock() == block &&
1794 user->IsBoundsCheck() &&
1795 user->AsBoundsCheck()->InputAt(0)->IsIntConstant()) {
1796 DCHECK_EQ(array_length, user->AsBoundsCheck()->InputAt(1));
1797 HIntConstant* const_instr = user->AsBoundsCheck()->InputAt(0)->AsIntConstant();
1798 if (const_instr->GetValue() > lower_bound_const) {
1799 lower_bound_const = const_instr->GetValue();
1800 lower_bound_const_instr = const_instr;
1801 }
1802 counter++;
1803 }
1804 }
1805 if (counter >= kThresholdForAddingDeoptimize &&
1806 lower_bound_const_instr->GetValue() <= kMaxConstantForAddingDeoptimize) {
1807 AddCompareWithDeoptimization(array_length, lower_bound_const_instr, block);
1808 }
1809 }
1810 }
1811
Mingyao Yangf384f882014-10-22 16:08:18 -07001812 std::vector<std::unique_ptr<ArenaSafeMap<int, ValueRange*>>> maps_;
1813
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001814 // Map an HArrayLength instruction's id to the first HBoundsCheck instruction in
1815 // a block that checks a constant index against that HArrayLength.
1816 SafeMap<int, HBoundsCheck*> first_constant_index_bounds_check_map_;
1817
1818 // For the block, there is at least one HArrayLength instruction for which there
1819 // is more than one bounds check instruction with constant indexing. And it's
1820 // beneficial to add a compare instruction that has deoptimization fallback and
1821 // eliminate those bounds checks.
1822 bool need_to_revisit_block_;
1823
Mingyao Yangbca381a2015-05-19 16:01:59 -07001824 // Initial number of blocks.
1825 int32_t initial_block_size_;
1826
Mingyao Yangf384f882014-10-22 16:08:18 -07001827 DISALLOW_COPY_AND_ASSIGN(BCEVisitor);
1828};
1829
1830void BoundsCheckElimination::Run() {
Mark Mendell1152c922015-04-24 17:06:35 -04001831 if (!graph_->HasBoundsChecks()) {
Mingyao Yange4335eb2015-03-02 15:14:13 -08001832 return;
1833 }
1834
Mingyao Yangf384f882014-10-22 16:08:18 -07001835 BCEVisitor visitor(graph_);
1836 // Reverse post order guarantees a node's dominators are visited first.
1837 // We want to visit in the dominator-based order since if a value is known to
1838 // be bounded by a range at one instruction, it must be true that all uses of
1839 // that value dominated by that instruction fits in that range. Range of that
1840 // value can be narrowed further down in the dominator tree.
1841 //
1842 // TODO: only visit blocks that dominate some array accesses.
Mingyao Yangbca381a2015-05-19 16:01:59 -07001843 HBasicBlock* last_visited_block = nullptr;
1844 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
1845 HBasicBlock* current = it.Current();
1846 if (current == last_visited_block) {
1847 // We may insert blocks into the reverse post order list when processing
1848 // a loop header. Don't process it again.
1849 DCHECK(current->IsLoopHeader());
1850 continue;
1851 }
1852 if (visitor.IsAddedBlock(current)) {
1853 // Skip added blocks. Their effects are already taken care of.
1854 continue;
1855 }
1856 visitor.VisitBasicBlock(current);
1857 last_visited_block = current;
1858 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001859}
1860
1861} // namespace art