blob: 7dc094b25f585e449be52f2e7c6ee2a597f7210d [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
17#include "bounds_check_elimination.h"
Aart Bikaab5b752015-09-23 11:18:57 -070018
19#include <limits>
20
21#include "base/arena_containers.h"
Aart Bik22af3be2015-09-10 12:50:58 -070022#include "induction_var_range.h"
Aart Bik4a342772015-11-30 10:17:46 -080023#include "side_effects_analysis.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070024#include "nodes.h"
Mingyao Yangf384f882014-10-22 16:08:18 -070025
26namespace art {
27
28class MonotonicValueRange;
29
30/**
31 * A value bound is represented as a pair of value and constant,
32 * e.g. array.length - 1.
33 */
34class ValueBound : public ValueObject {
35 public:
Mingyao Yang0304e182015-01-30 16:41:29 -080036 ValueBound(HInstruction* instruction, int32_t constant) {
Mingyao Yang64197522014-12-05 15:56:23 -080037 if (instruction != nullptr && instruction->IsIntConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -080038 // Normalize ValueBound with constant instruction.
39 int32_t instr_const = instruction->AsIntConstant()->GetValue();
Mingyao Yang8c8bad82015-02-09 18:13:26 -080040 if (!WouldAddOverflowOrUnderflow(instr_const, constant)) {
Mingyao Yang64197522014-12-05 15:56:23 -080041 instruction_ = nullptr;
42 constant_ = instr_const + constant;
43 return;
44 }
Mingyao Yangf384f882014-10-22 16:08:18 -070045 }
Mingyao Yang64197522014-12-05 15:56:23 -080046 instruction_ = instruction;
47 constant_ = constant;
48 }
49
Mingyao Yang8c8bad82015-02-09 18:13:26 -080050 // Return whether (left + right) overflows or underflows.
51 static bool WouldAddOverflowOrUnderflow(int32_t left, int32_t right) {
52 if (right == 0) {
53 return false;
54 }
Aart Bikaab5b752015-09-23 11:18:57 -070055 if ((right > 0) && (left <= (std::numeric_limits<int32_t>::max() - right))) {
Mingyao Yang8c8bad82015-02-09 18:13:26 -080056 // No overflow.
57 return false;
58 }
Aart Bikaab5b752015-09-23 11:18:57 -070059 if ((right < 0) && (left >= (std::numeric_limits<int32_t>::min() - right))) {
Mingyao Yang8c8bad82015-02-09 18:13:26 -080060 // No underflow.
61 return false;
62 }
63 return true;
64 }
65
Aart Bik1d239822016-02-09 14:26:34 -080066 // Return true if instruction can be expressed as "left_instruction + right_constant".
Mingyao Yang0304e182015-01-30 16:41:29 -080067 static bool IsAddOrSubAConstant(HInstruction* instruction,
Aart Bik1d239822016-02-09 14:26:34 -080068 /* out */ HInstruction** left_instruction,
69 /* out */ int32_t* right_constant) {
Aart Bikbf3f1cf2016-02-22 16:22:33 -080070 HInstruction* left_so_far = nullptr;
71 int32_t right_so_far = 0;
72 while (instruction->IsAdd() || instruction->IsSub()) {
Mingyao Yang0304e182015-01-30 16:41:29 -080073 HBinaryOperation* bin_op = instruction->AsBinaryOperation();
74 HInstruction* left = bin_op->GetLeft();
75 HInstruction* right = bin_op->GetRight();
76 if (right->IsIntConstant()) {
Aart Bikbf3f1cf2016-02-22 16:22:33 -080077 int32_t v = right->AsIntConstant()->GetValue();
78 int32_t c = instruction->IsAdd() ? v : -v;
79 if (!WouldAddOverflowOrUnderflow(right_so_far, c)) {
80 instruction = left;
81 left_so_far = left;
82 right_so_far += c;
83 continue;
84 }
Mingyao Yang0304e182015-01-30 16:41:29 -080085 }
Aart Bikbf3f1cf2016-02-22 16:22:33 -080086 break;
Mingyao Yang0304e182015-01-30 16:41:29 -080087 }
Aart Bikbf3f1cf2016-02-22 16:22:33 -080088 // Return result: either false and "null+0" or true and "instr+constant".
89 *left_instruction = left_so_far;
90 *right_constant = right_so_far;
91 return left_so_far != nullptr;
Mingyao Yang0304e182015-01-30 16:41:29 -080092 }
93
Aart Bik1d239822016-02-09 14:26:34 -080094 // Expresses any instruction as a value bound.
95 static ValueBound AsValueBound(HInstruction* instruction) {
96 if (instruction->IsIntConstant()) {
97 return ValueBound(nullptr, instruction->AsIntConstant()->GetValue());
98 }
99 HInstruction *left;
100 int32_t right;
101 if (IsAddOrSubAConstant(instruction, &left, &right)) {
102 return ValueBound(left, right);
103 }
104 return ValueBound(instruction, 0);
105 }
106
Mingyao Yang64197522014-12-05 15:56:23 -0800107 // Try to detect useful value bound format from an instruction, e.g.
108 // a constant or array length related value.
Aart Bik1d239822016-02-09 14:26:34 -0800109 static ValueBound DetectValueBoundFromValue(HInstruction* instruction, /* out */ bool* found) {
Mingyao Yang64197522014-12-05 15:56:23 -0800110 DCHECK(instruction != nullptr);
Mingyao Yangf384f882014-10-22 16:08:18 -0700111 if (instruction->IsIntConstant()) {
Mingyao Yang64197522014-12-05 15:56:23 -0800112 *found = true;
113 return ValueBound(nullptr, instruction->AsIntConstant()->GetValue());
Mingyao Yangf384f882014-10-22 16:08:18 -0700114 }
Mingyao Yang64197522014-12-05 15:56:23 -0800115
116 if (instruction->IsArrayLength()) {
117 *found = true;
118 return ValueBound(instruction, 0);
119 }
120 // Try to detect (array.length + c) format.
Mingyao Yang0304e182015-01-30 16:41:29 -0800121 HInstruction *left;
122 int32_t right;
123 if (IsAddOrSubAConstant(instruction, &left, &right)) {
124 if (left->IsArrayLength()) {
Mingyao Yang64197522014-12-05 15:56:23 -0800125 *found = true;
Mingyao Yang0304e182015-01-30 16:41:29 -0800126 return ValueBound(left, right);
Mingyao Yang64197522014-12-05 15:56:23 -0800127 }
128 }
129
130 // No useful bound detected.
131 *found = false;
132 return ValueBound::Max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700133 }
134
135 HInstruction* GetInstruction() const { return instruction_; }
Mingyao Yang0304e182015-01-30 16:41:29 -0800136 int32_t GetConstant() const { return constant_; }
Mingyao Yangf384f882014-10-22 16:08:18 -0700137
Mingyao Yang0304e182015-01-30 16:41:29 -0800138 bool IsRelatedToArrayLength() const {
139 // Some bounds are created with HNewArray* as the instruction instead
140 // of HArrayLength*. They are treated the same.
141 return (instruction_ != nullptr) &&
142 (instruction_->IsArrayLength() || instruction_->IsNewArray());
Mingyao Yangf384f882014-10-22 16:08:18 -0700143 }
144
145 bool IsConstant() const {
146 return instruction_ == nullptr;
147 }
148
Aart Bikaab5b752015-09-23 11:18:57 -0700149 static ValueBound Min() { return ValueBound(nullptr, std::numeric_limits<int32_t>::min()); }
150 static ValueBound Max() { return ValueBound(nullptr, std::numeric_limits<int32_t>::max()); }
Mingyao Yangf384f882014-10-22 16:08:18 -0700151
152 bool Equals(ValueBound bound) const {
153 return instruction_ == bound.instruction_ && constant_ == bound.constant_;
154 }
155
Aart Bik22af3be2015-09-10 12:50:58 -0700156 /*
157 * Hunt "under the hood" of array lengths (leading to array references),
158 * null checks (also leading to array references), and new arrays
159 * (leading to the actual length). This makes it more likely related
160 * instructions become actually comparable.
161 */
162 static HInstruction* HuntForDeclaration(HInstruction* instruction) {
163 while (instruction->IsArrayLength() ||
164 instruction->IsNullCheck() ||
165 instruction->IsNewArray()) {
166 instruction = instruction->InputAt(0);
Mingyao Yang0304e182015-01-30 16:41:29 -0800167 }
168 return instruction;
169 }
170
171 static bool Equal(HInstruction* instruction1, HInstruction* instruction2) {
172 if (instruction1 == instruction2) {
173 return true;
174 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800175 if (instruction1 == nullptr || instruction2 == nullptr) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700176 return false;
177 }
Aart Bik22af3be2015-09-10 12:50:58 -0700178 instruction1 = HuntForDeclaration(instruction1);
179 instruction2 = HuntForDeclaration(instruction2);
Mingyao Yang0304e182015-01-30 16:41:29 -0800180 return instruction1 == instruction2;
181 }
182
183 // Returns if it's certain this->bound >= `bound`.
184 bool GreaterThanOrEqualTo(ValueBound bound) const {
185 if (Equal(instruction_, bound.instruction_)) {
186 return constant_ >= bound.constant_;
187 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700188 // Not comparable. Just return false.
189 return false;
190 }
191
Mingyao Yang0304e182015-01-30 16:41:29 -0800192 // Returns if it's certain this->bound <= `bound`.
193 bool LessThanOrEqualTo(ValueBound bound) const {
194 if (Equal(instruction_, bound.instruction_)) {
195 return constant_ <= bound.constant_;
Mingyao Yangf384f882014-10-22 16:08:18 -0700196 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700197 // Not comparable. Just return false.
198 return false;
199 }
200
Aart Bik4a342772015-11-30 10:17:46 -0800201 // Returns if it's certain this->bound > `bound`.
202 bool GreaterThan(ValueBound bound) const {
203 if (Equal(instruction_, bound.instruction_)) {
204 return constant_ > bound.constant_;
205 }
206 // Not comparable. Just return false.
207 return false;
208 }
209
210 // Returns if it's certain this->bound < `bound`.
211 bool LessThan(ValueBound bound) const {
212 if (Equal(instruction_, bound.instruction_)) {
213 return constant_ < bound.constant_;
214 }
215 // Not comparable. Just return false.
216 return false;
217 }
218
Mingyao Yangf384f882014-10-22 16:08:18 -0700219 // Try to narrow lower bound. Returns the greatest of the two if possible.
220 // Pick one if they are not comparable.
221 static ValueBound NarrowLowerBound(ValueBound bound1, ValueBound bound2) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800222 if (bound1.GreaterThanOrEqualTo(bound2)) {
223 return bound1;
224 }
225 if (bound2.GreaterThanOrEqualTo(bound1)) {
226 return bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700227 }
228
229 // Not comparable. Just pick one. We may lose some info, but that's ok.
230 // Favor constant as lower bound.
231 return bound1.IsConstant() ? bound1 : bound2;
232 }
233
234 // Try to narrow upper bound. Returns the lowest of the two if possible.
235 // Pick one if they are not comparable.
236 static ValueBound NarrowUpperBound(ValueBound bound1, ValueBound bound2) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800237 if (bound1.LessThanOrEqualTo(bound2)) {
238 return bound1;
239 }
240 if (bound2.LessThanOrEqualTo(bound1)) {
241 return bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700242 }
243
244 // Not comparable. Just pick one. We may lose some info, but that's ok.
245 // Favor array length as upper bound.
Mingyao Yang0304e182015-01-30 16:41:29 -0800246 return bound1.IsRelatedToArrayLength() ? bound1 : bound2;
Mingyao Yangf384f882014-10-22 16:08:18 -0700247 }
248
Mingyao Yang0304e182015-01-30 16:41:29 -0800249 // Add a constant to a ValueBound.
250 // `overflow` or `underflow` will return whether the resulting bound may
251 // overflow or underflow an int.
Aart Bik1d239822016-02-09 14:26:34 -0800252 ValueBound Add(int32_t c, /* out */ bool* overflow, /* out */ bool* underflow) const {
Mingyao Yang0304e182015-01-30 16:41:29 -0800253 *overflow = *underflow = false;
Mingyao Yangf384f882014-10-22 16:08:18 -0700254 if (c == 0) {
255 return *this;
256 }
257
Mingyao Yang0304e182015-01-30 16:41:29 -0800258 int32_t new_constant;
Mingyao Yangf384f882014-10-22 16:08:18 -0700259 if (c > 0) {
Aart Bikaab5b752015-09-23 11:18:57 -0700260 if (constant_ > (std::numeric_limits<int32_t>::max() - c)) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800261 *overflow = true;
Mingyao Yang64197522014-12-05 15:56:23 -0800262 return Max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700263 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800264
265 new_constant = constant_ + c;
266 // (array.length + non-positive-constant) won't overflow an int.
267 if (IsConstant() || (IsRelatedToArrayLength() && new_constant <= 0)) {
268 return ValueBound(instruction_, new_constant);
269 }
270 // Be conservative.
271 *overflow = true;
272 return Max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700273 } else {
Aart Bikaab5b752015-09-23 11:18:57 -0700274 if (constant_ < (std::numeric_limits<int32_t>::min() - c)) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800275 *underflow = true;
276 return Min();
Mingyao Yangf384f882014-10-22 16:08:18 -0700277 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800278
279 new_constant = constant_ + c;
280 // Regardless of the value new_constant, (array.length+new_constant) will
281 // never underflow since array.length is no less than 0.
282 if (IsConstant() || IsRelatedToArrayLength()) {
283 return ValueBound(instruction_, new_constant);
284 }
285 // Be conservative.
286 *underflow = true;
287 return Min();
Mingyao Yangf384f882014-10-22 16:08:18 -0700288 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700289 }
290
291 private:
Mingyao Yangf384f882014-10-22 16:08:18 -0700292 HInstruction* instruction_;
Mingyao Yang0304e182015-01-30 16:41:29 -0800293 int32_t constant_;
Mingyao Yangf384f882014-10-22 16:08:18 -0700294};
295
296/**
297 * Represent a range of lower bound and upper bound, both being inclusive.
298 * Currently a ValueRange may be generated as a result of the following:
299 * comparisons related to array bounds, array bounds check, add/sub on top
Mingyao Yang0304e182015-01-30 16:41:29 -0800300 * of an existing value range, NewArray or a loop phi corresponding to an
Mingyao Yangf384f882014-10-22 16:08:18 -0700301 * incrementing/decrementing array index (MonotonicValueRange).
302 */
Vladimir Marko5233f932015-09-29 19:01:15 +0100303class ValueRange : public ArenaObject<kArenaAllocBoundsCheckElimination> {
Mingyao Yangf384f882014-10-22 16:08:18 -0700304 public:
305 ValueRange(ArenaAllocator* allocator, ValueBound lower, ValueBound upper)
306 : allocator_(allocator), lower_(lower), upper_(upper) {}
307
308 virtual ~ValueRange() {}
309
Mingyao Yang57e04752015-02-09 18:13:26 -0800310 virtual MonotonicValueRange* AsMonotonicValueRange() { return nullptr; }
311 bool IsMonotonicValueRange() {
Mingyao Yangf384f882014-10-22 16:08:18 -0700312 return AsMonotonicValueRange() != nullptr;
313 }
314
315 ArenaAllocator* GetAllocator() const { return allocator_; }
316 ValueBound GetLower() const { return lower_; }
317 ValueBound GetUpper() const { return upper_; }
318
Mingyao Yang3584bce2015-05-19 16:01:59 -0700319 bool IsConstantValueRange() { return lower_.IsConstant() && upper_.IsConstant(); }
320
Mingyao Yangf384f882014-10-22 16:08:18 -0700321 // If it's certain that this value range fits in other_range.
322 virtual bool FitsIn(ValueRange* other_range) const {
323 if (other_range == nullptr) {
324 return true;
325 }
326 DCHECK(!other_range->IsMonotonicValueRange());
Mingyao Yang0304e182015-01-30 16:41:29 -0800327 return lower_.GreaterThanOrEqualTo(other_range->lower_) &&
328 upper_.LessThanOrEqualTo(other_range->upper_);
Mingyao Yangf384f882014-10-22 16:08:18 -0700329 }
330
331 // Returns the intersection of this and range.
332 // If it's not possible to do intersection because some
333 // bounds are not comparable, it's ok to pick either bound.
334 virtual ValueRange* Narrow(ValueRange* range) {
335 if (range == nullptr) {
336 return this;
337 }
338
339 if (range->IsMonotonicValueRange()) {
340 return this;
341 }
342
343 return new (allocator_) ValueRange(
344 allocator_,
345 ValueBound::NarrowLowerBound(lower_, range->lower_),
346 ValueBound::NarrowUpperBound(upper_, range->upper_));
347 }
348
Mingyao Yang0304e182015-01-30 16:41:29 -0800349 // Shift a range by a constant.
350 ValueRange* Add(int32_t constant) const {
351 bool overflow, underflow;
352 ValueBound lower = lower_.Add(constant, &overflow, &underflow);
353 if (underflow) {
354 // Lower bound underflow will wrap around to positive values
355 // and invalidate the upper bound.
356 return nullptr;
Mingyao Yangf384f882014-10-22 16:08:18 -0700357 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800358 ValueBound upper = upper_.Add(constant, &overflow, &underflow);
359 if (overflow) {
360 // Upper bound overflow will wrap around to negative values
361 // and invalidate the lower bound.
362 return nullptr;
Mingyao Yangf384f882014-10-22 16:08:18 -0700363 }
364 return new (allocator_) ValueRange(allocator_, lower, upper);
365 }
366
Mingyao Yangf384f882014-10-22 16:08:18 -0700367 private:
368 ArenaAllocator* const allocator_;
369 const ValueBound lower_; // inclusive
370 const ValueBound upper_; // inclusive
371
372 DISALLOW_COPY_AND_ASSIGN(ValueRange);
373};
374
375/**
376 * A monotonically incrementing/decrementing value range, e.g.
377 * the variable i in "for (int i=0; i<array.length; i++)".
378 * Special care needs to be taken to account for overflow/underflow
379 * of such value ranges.
380 */
381class MonotonicValueRange : public ValueRange {
382 public:
Mingyao Yang64197522014-12-05 15:56:23 -0800383 MonotonicValueRange(ArenaAllocator* allocator,
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700384 HPhi* induction_variable,
Mingyao Yang64197522014-12-05 15:56:23 -0800385 HInstruction* initial,
Mingyao Yang0304e182015-01-30 16:41:29 -0800386 int32_t increment,
Mingyao Yang64197522014-12-05 15:56:23 -0800387 ValueBound bound)
Aart Bikaab5b752015-09-23 11:18:57 -0700388 // To be conservative, give it full range [Min(), Max()] in case it's
Mingyao Yang64197522014-12-05 15:56:23 -0800389 // used as a regular value range, due to possible overflow/underflow.
390 : ValueRange(allocator, ValueBound::Min(), ValueBound::Max()),
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700391 induction_variable_(induction_variable),
Mingyao Yang64197522014-12-05 15:56:23 -0800392 initial_(initial),
393 increment_(increment),
394 bound_(bound) {}
Mingyao Yangf384f882014-10-22 16:08:18 -0700395
396 virtual ~MonotonicValueRange() {}
397
Mingyao Yang57e04752015-02-09 18:13:26 -0800398 int32_t GetIncrement() const { return increment_; }
Mingyao Yang57e04752015-02-09 18:13:26 -0800399 ValueBound GetBound() const { return bound_; }
Mingyao Yang3584bce2015-05-19 16:01:59 -0700400 HBasicBlock* GetLoopHeader() const {
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700401 DCHECK(induction_variable_->GetBlock()->IsLoopHeader());
402 return induction_variable_->GetBlock();
403 }
Mingyao Yang57e04752015-02-09 18:13:26 -0800404
405 MonotonicValueRange* AsMonotonicValueRange() OVERRIDE { return this; }
Mingyao Yangf384f882014-10-22 16:08:18 -0700406
407 // If it's certain that this value range fits in other_range.
408 bool FitsIn(ValueRange* other_range) const OVERRIDE {
409 if (other_range == nullptr) {
410 return true;
411 }
412 DCHECK(!other_range->IsMonotonicValueRange());
413 return false;
414 }
415
416 // Try to narrow this MonotonicValueRange given another range.
417 // Ideally it will return a normal ValueRange. But due to
418 // possible overflow/underflow, that may not be possible.
419 ValueRange* Narrow(ValueRange* range) OVERRIDE {
420 if (range == nullptr) {
421 return this;
422 }
423 DCHECK(!range->IsMonotonicValueRange());
424
425 if (increment_ > 0) {
426 // Monotonically increasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800427 ValueBound lower = ValueBound::NarrowLowerBound(bound_, range->GetLower());
Aart Bikaab5b752015-09-23 11:18:57 -0700428 if (!lower.IsConstant() || lower.GetConstant() == std::numeric_limits<int32_t>::min()) {
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700429 // Lower bound isn't useful. Leave it to deoptimization.
430 return this;
431 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700432
Aart Bikaab5b752015-09-23 11:18:57 -0700433 // We currently conservatively assume max array length is Max().
434 // If we can make assumptions about the max array length, e.g. due to the max heap size,
Mingyao Yangf384f882014-10-22 16:08:18 -0700435 // divided by the element size (such as 4 bytes for each integer array), we can
436 // lower this number and rule out some possible overflows.
Aart Bikaab5b752015-09-23 11:18:57 -0700437 int32_t max_array_len = std::numeric_limits<int32_t>::max();
Mingyao Yangf384f882014-10-22 16:08:18 -0700438
Mingyao Yang0304e182015-01-30 16:41:29 -0800439 // max possible integer value of range's upper value.
Aart Bikaab5b752015-09-23 11:18:57 -0700440 int32_t upper = std::numeric_limits<int32_t>::max();
Mingyao Yang0304e182015-01-30 16:41:29 -0800441 // Try to lower upper.
442 ValueBound upper_bound = range->GetUpper();
443 if (upper_bound.IsConstant()) {
444 upper = upper_bound.GetConstant();
445 } else if (upper_bound.IsRelatedToArrayLength() && upper_bound.GetConstant() <= 0) {
446 // Normal case. e.g. <= array.length - 1.
447 upper = max_array_len + upper_bound.GetConstant();
Mingyao Yangf384f882014-10-22 16:08:18 -0700448 }
449
450 // If we can prove for the last number in sequence of initial_,
451 // initial_ + increment_, initial_ + 2 x increment_, ...
452 // that's <= upper, (last_num_in_sequence + increment_) doesn't trigger overflow,
453 // then this MonoticValueRange is narrowed to a normal value range.
454
455 // Be conservative first, assume last number in the sequence hits upper.
Mingyao Yang0304e182015-01-30 16:41:29 -0800456 int32_t last_num_in_sequence = upper;
Mingyao Yangf384f882014-10-22 16:08:18 -0700457 if (initial_->IsIntConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800458 int32_t initial_constant = initial_->AsIntConstant()->GetValue();
Mingyao Yangf384f882014-10-22 16:08:18 -0700459 if (upper <= initial_constant) {
460 last_num_in_sequence = upper;
461 } else {
Mingyao Yang0304e182015-01-30 16:41:29 -0800462 // Cast to int64_t for the substraction part to avoid int32_t overflow.
Mingyao Yangf384f882014-10-22 16:08:18 -0700463 last_num_in_sequence = initial_constant +
464 ((int64_t)upper - (int64_t)initial_constant) / increment_ * increment_;
465 }
466 }
Aart Bikaab5b752015-09-23 11:18:57 -0700467 if (last_num_in_sequence <= (std::numeric_limits<int32_t>::max() - increment_)) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700468 // No overflow. The sequence will be stopped by the upper bound test as expected.
469 return new (GetAllocator()) ValueRange(GetAllocator(), lower, range->GetUpper());
470 }
471
472 // There might be overflow. Give up narrowing.
473 return this;
474 } else {
475 DCHECK_NE(increment_, 0);
476 // Monotonically decreasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800477 ValueBound upper = ValueBound::NarrowUpperBound(bound_, range->GetUpper());
Aart Bikaab5b752015-09-23 11:18:57 -0700478 if ((!upper.IsConstant() || upper.GetConstant() == std::numeric_limits<int32_t>::max()) &&
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700479 !upper.IsRelatedToArrayLength()) {
480 // Upper bound isn't useful. Leave it to deoptimization.
481 return this;
482 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700483
484 // Need to take care of underflow. Try to prove underflow won't happen
Mingyao Yang0304e182015-01-30 16:41:29 -0800485 // for common cases.
Mingyao Yangf384f882014-10-22 16:08:18 -0700486 if (range->GetLower().IsConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800487 int32_t constant = range->GetLower().GetConstant();
Aart Bikaab5b752015-09-23 11:18:57 -0700488 if (constant >= (std::numeric_limits<int32_t>::min() - increment_)) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700489 return new (GetAllocator()) ValueRange(GetAllocator(), range->GetLower(), upper);
490 }
491 }
492
Mingyao Yang0304e182015-01-30 16:41:29 -0800493 // For non-constant lower bound, just assume might be underflow. Give up narrowing.
Mingyao Yangf384f882014-10-22 16:08:18 -0700494 return this;
495 }
496 }
497
498 private:
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700499 HPhi* const induction_variable_; // Induction variable for this monotonic value range.
500 HInstruction* const initial_; // Initial value.
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700501 const int32_t increment_; // Increment for each loop iteration.
502 const ValueBound bound_; // Additional value bound info for initial_.
Mingyao Yangf384f882014-10-22 16:08:18 -0700503
504 DISALLOW_COPY_AND_ASSIGN(MonotonicValueRange);
505};
506
507class BCEVisitor : public HGraphVisitor {
508 public:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700509 // The least number of bounds checks that should be eliminated by triggering
510 // the deoptimization technique.
511 static constexpr size_t kThresholdForAddingDeoptimize = 2;
512
Aart Bik1d239822016-02-09 14:26:34 -0800513 // Very large lengths are considered an anomaly. This is a threshold beyond which we don't
514 // bother to apply the deoptimization technique since it's likely, or sometimes certain,
515 // an AIOOBE will be thrown.
516 static constexpr uint32_t kMaxLengthForAddingDeoptimize =
Aart Bikaab5b752015-09-23 11:18:57 -0700517 std::numeric_limits<int32_t>::max() - 1024 * 1024;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700518
Mingyao Yang3584bce2015-05-19 16:01:59 -0700519 // Added blocks for loop body entry test.
520 bool IsAddedBlock(HBasicBlock* block) const {
521 return block->GetBlockId() >= initial_block_size_;
522 }
523
Aart Bik4a342772015-11-30 10:17:46 -0800524 BCEVisitor(HGraph* graph,
525 const SideEffectsAnalysis& side_effects,
526 HInductionVarAnalysis* induction_analysis)
Aart Bik22af3be2015-09-10 12:50:58 -0700527 : HGraphVisitor(graph),
Vladimir Marko5233f932015-09-29 19:01:15 +0100528 maps_(graph->GetBlocks().size(),
529 ArenaSafeMap<int, ValueRange*>(
530 std::less<int>(),
531 graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)),
532 graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)),
Aart Bik1d239822016-02-09 14:26:34 -0800533 first_index_bounds_check_map_(
Vladimir Marko5233f932015-09-29 19:01:15 +0100534 std::less<int>(),
535 graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)),
Aart Bik4a342772015-11-30 10:17:46 -0800536 early_exit_loop_(
537 std::less<uint32_t>(),
538 graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)),
539 taken_test_loop_(
540 std::less<uint32_t>(),
541 graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)),
542 finite_loop_(graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)),
Aart Bik1d239822016-02-09 14:26:34 -0800543 has_dom_based_dynamic_bce_(false),
Vladimir Markofa6b93c2015-09-15 10:15:55 +0100544 initial_block_size_(graph->GetBlocks().size()),
Aart Bik4a342772015-11-30 10:17:46 -0800545 side_effects_(side_effects),
Aart Bik22af3be2015-09-10 12:50:58 -0700546 induction_range_(induction_analysis) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700547
548 void VisitBasicBlock(HBasicBlock* block) OVERRIDE {
Mingyao Yang3584bce2015-05-19 16:01:59 -0700549 DCHECK(!IsAddedBlock(block));
Aart Bik1d239822016-02-09 14:26:34 -0800550 first_index_bounds_check_map_.clear();
Aart Bik1e677482016-11-01 14:23:58 -0700551 // Visit phis and instructions using a safe iterator. The iteration protects
552 // against deleting the current instruction during iteration. However, it
553 // must advance next_ if that instruction is deleted during iteration.
554 for (HInstruction* instruction = block->GetFirstPhi(); instruction != nullptr;) {
555 DCHECK(instruction->IsInBlock());
556 next_ = instruction->GetNext();
557 instruction->Accept(this);
558 instruction = next_;
559 }
560 for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) {
561 DCHECK(instruction->IsInBlock());
562 next_ = instruction->GetNext();
563 instruction->Accept(this);
564 instruction = next_;
565 }
Nicolas Geoffray93a18c52016-04-22 13:16:14 +0100566 // We should never deoptimize from an osr method, otherwise we might wrongly optimize
567 // code dominated by the deoptimization.
568 if (!GetGraph()->IsCompilingOsr()) {
569 AddComparesWithDeoptimization(block);
570 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700571 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700572
Aart Bik4a342772015-11-30 10:17:46 -0800573 void Finish() {
574 // Preserve SSA structure which may have been broken by adding one or more
575 // new taken-test structures (see TransformLoopForDeoptimizationIfNeeded()).
576 InsertPhiNodes();
577
578 // Clear the loop data structures.
579 early_exit_loop_.clear();
580 taken_test_loop_.clear();
581 finite_loop_.clear();
582 }
583
Mingyao Yangf384f882014-10-22 16:08:18 -0700584 private:
585 // Return the map of proven value ranges at the beginning of a basic block.
586 ArenaSafeMap<int, ValueRange*>* GetValueRangeMap(HBasicBlock* basic_block) {
Mingyao Yang3584bce2015-05-19 16:01:59 -0700587 if (IsAddedBlock(basic_block)) {
588 // Added blocks don't keep value ranges.
589 return nullptr;
590 }
Aart Bik1d239822016-02-09 14:26:34 -0800591 return &maps_[basic_block->GetBlockId()];
Mingyao Yangf384f882014-10-22 16:08:18 -0700592 }
593
594 // Traverse up the dominator tree to look for value range info.
595 ValueRange* LookupValueRange(HInstruction* instruction, HBasicBlock* basic_block) {
596 while (basic_block != nullptr) {
597 ArenaSafeMap<int, ValueRange*>* map = GetValueRangeMap(basic_block);
Mingyao Yang3584bce2015-05-19 16:01:59 -0700598 if (map != nullptr) {
599 if (map->find(instruction->GetId()) != map->end()) {
600 return map->Get(instruction->GetId());
601 }
602 } else {
603 DCHECK(IsAddedBlock(basic_block));
Mingyao Yangf384f882014-10-22 16:08:18 -0700604 }
605 basic_block = basic_block->GetDominator();
606 }
607 // Didn't find any.
608 return nullptr;
609 }
610
Aart Bik1d239822016-02-09 14:26:34 -0800611 // Helper method to assign a new range to an instruction in given basic block.
612 void AssignRange(HBasicBlock* basic_block, HInstruction* instruction, ValueRange* range) {
613 GetValueRangeMap(basic_block)->Overwrite(instruction->GetId(), range);
614 }
615
Mingyao Yang0304e182015-01-30 16:41:29 -0800616 // Narrow the value range of `instruction` at the end of `basic_block` with `range`,
617 // and push the narrowed value range to `successor`.
Mingyao Yangf384f882014-10-22 16:08:18 -0700618 void ApplyRangeFromComparison(HInstruction* instruction, HBasicBlock* basic_block,
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800619 HBasicBlock* successor, ValueRange* range) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700620 ValueRange* existing_range = LookupValueRange(instruction, basic_block);
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800621 if (existing_range == nullptr) {
622 if (range != nullptr) {
Aart Bik1d239822016-02-09 14:26:34 -0800623 AssignRange(successor, instruction, range);
Mingyao Yang8c8bad82015-02-09 18:13:26 -0800624 }
625 return;
626 }
627 if (existing_range->IsMonotonicValueRange()) {
628 DCHECK(instruction->IsLoopHeaderPhi());
629 // Make sure the comparison is in the loop header so each increment is
630 // checked with a comparison.
631 if (instruction->GetBlock() != basic_block) {
632 return;
633 }
634 }
Aart Bik1d239822016-02-09 14:26:34 -0800635 AssignRange(successor, instruction, existing_range->Narrow(range));
Mingyao Yangf384f882014-10-22 16:08:18 -0700636 }
637
Mingyao Yang57e04752015-02-09 18:13:26 -0800638 // Special case that we may simultaneously narrow two MonotonicValueRange's to
639 // regular value ranges.
640 void HandleIfBetweenTwoMonotonicValueRanges(HIf* instruction,
641 HInstruction* left,
642 HInstruction* right,
643 IfCondition cond,
644 MonotonicValueRange* left_range,
645 MonotonicValueRange* right_range) {
646 DCHECK(left->IsLoopHeaderPhi());
647 DCHECK(right->IsLoopHeaderPhi());
648 if (instruction->GetBlock() != left->GetBlock()) {
649 // Comparison needs to be in loop header to make sure it's done after each
650 // increment/decrement.
651 return;
652 }
653
654 // Handle common cases which also don't have overflow/underflow concerns.
655 if (left_range->GetIncrement() == 1 &&
656 left_range->GetBound().IsConstant() &&
657 right_range->GetIncrement() == -1 &&
658 right_range->GetBound().IsRelatedToArrayLength() &&
659 right_range->GetBound().GetConstant() < 0) {
Mingyao Yang57e04752015-02-09 18:13:26 -0800660 HBasicBlock* successor = nullptr;
661 int32_t left_compensation = 0;
662 int32_t right_compensation = 0;
663 if (cond == kCondLT) {
664 left_compensation = -1;
665 right_compensation = 1;
666 successor = instruction->IfTrueSuccessor();
667 } else if (cond == kCondLE) {
668 successor = instruction->IfTrueSuccessor();
669 } else if (cond == kCondGT) {
670 successor = instruction->IfFalseSuccessor();
671 } else if (cond == kCondGE) {
672 left_compensation = -1;
673 right_compensation = 1;
674 successor = instruction->IfFalseSuccessor();
675 } else {
676 // We don't handle '=='/'!=' test in case left and right can cross and
677 // miss each other.
678 return;
679 }
680
681 if (successor != nullptr) {
682 bool overflow;
683 bool underflow;
684 ValueRange* new_left_range = new (GetGraph()->GetArena()) ValueRange(
685 GetGraph()->GetArena(),
686 left_range->GetBound(),
687 right_range->GetBound().Add(left_compensation, &overflow, &underflow));
688 if (!overflow && !underflow) {
689 ApplyRangeFromComparison(left, instruction->GetBlock(), successor,
690 new_left_range);
691 }
692
693 ValueRange* new_right_range = new (GetGraph()->GetArena()) ValueRange(
694 GetGraph()->GetArena(),
695 left_range->GetBound().Add(right_compensation, &overflow, &underflow),
696 right_range->GetBound());
697 if (!overflow && !underflow) {
698 ApplyRangeFromComparison(right, instruction->GetBlock(), successor,
699 new_right_range);
700 }
701 }
702 }
703 }
704
Mingyao Yangf384f882014-10-22 16:08:18 -0700705 // Handle "if (left cmp_cond right)".
706 void HandleIf(HIf* instruction, HInstruction* left, HInstruction* right, IfCondition cond) {
707 HBasicBlock* block = instruction->GetBlock();
708
709 HBasicBlock* true_successor = instruction->IfTrueSuccessor();
710 // There should be no critical edge at this point.
Vladimir Marko60584552015-09-03 13:35:12 +0000711 DCHECK_EQ(true_successor->GetPredecessors().size(), 1u);
Mingyao Yangf384f882014-10-22 16:08:18 -0700712
713 HBasicBlock* false_successor = instruction->IfFalseSuccessor();
714 // There should be no critical edge at this point.
Vladimir Marko60584552015-09-03 13:35:12 +0000715 DCHECK_EQ(false_successor->GetPredecessors().size(), 1u);
Mingyao Yangf384f882014-10-22 16:08:18 -0700716
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700717 ValueRange* left_range = LookupValueRange(left, block);
718 MonotonicValueRange* left_monotonic_range = nullptr;
719 if (left_range != nullptr) {
720 left_monotonic_range = left_range->AsMonotonicValueRange();
721 if (left_monotonic_range != nullptr) {
Mingyao Yang3584bce2015-05-19 16:01:59 -0700722 HBasicBlock* loop_head = left_monotonic_range->GetLoopHeader();
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700723 if (instruction->GetBlock() != loop_head) {
724 // For monotonic value range, don't handle `instruction`
725 // if it's not defined in the loop header.
726 return;
727 }
728 }
729 }
730
Mingyao Yang64197522014-12-05 15:56:23 -0800731 bool found;
732 ValueBound bound = ValueBound::DetectValueBoundFromValue(right, &found);
Mingyao Yang0304e182015-01-30 16:41:29 -0800733 // Each comparison can establish a lower bound and an upper bound
734 // for the left hand side.
Mingyao Yangf384f882014-10-22 16:08:18 -0700735 ValueBound lower = bound;
736 ValueBound upper = bound;
737 if (!found) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800738 // No constant or array.length+c format bound found.
Mingyao Yangf384f882014-10-22 16:08:18 -0700739 // 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 -0800740 ValueRange* right_range = LookupValueRange(right, block);
741 if (right_range != nullptr) {
742 if (right_range->IsMonotonicValueRange()) {
Mingyao Yang57e04752015-02-09 18:13:26 -0800743 if (left_range != nullptr && left_range->IsMonotonicValueRange()) {
744 HandleIfBetweenTwoMonotonicValueRanges(instruction, left, right, cond,
745 left_range->AsMonotonicValueRange(),
746 right_range->AsMonotonicValueRange());
747 return;
748 }
749 }
750 lower = right_range->GetLower();
751 upper = right_range->GetUpper();
Mingyao Yangf384f882014-10-22 16:08:18 -0700752 } else {
753 lower = ValueBound::Min();
754 upper = ValueBound::Max();
755 }
756 }
757
Mingyao Yang0304e182015-01-30 16:41:29 -0800758 bool overflow, underflow;
Mingyao Yangf384f882014-10-22 16:08:18 -0700759 if (cond == kCondLT || cond == kCondLE) {
760 if (!upper.Equals(ValueBound::Max())) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800761 int32_t compensation = (cond == kCondLT) ? -1 : 0; // upper bound is inclusive
762 ValueBound new_upper = upper.Add(compensation, &overflow, &underflow);
763 if (overflow || underflow) {
764 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800765 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700766 ValueRange* new_range = new (GetGraph()->GetArena())
767 ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper);
768 ApplyRangeFromComparison(left, block, true_successor, new_range);
769 }
770
771 // array.length as a lower bound isn't considered useful.
Mingyao Yang0304e182015-01-30 16:41:29 -0800772 if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) {
773 int32_t compensation = (cond == kCondLE) ? 1 : 0; // lower bound is inclusive
774 ValueBound new_lower = lower.Add(compensation, &overflow, &underflow);
775 if (overflow || underflow) {
776 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800777 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700778 ValueRange* new_range = new (GetGraph()->GetArena())
779 ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max());
780 ApplyRangeFromComparison(left, block, false_successor, new_range);
781 }
782 } else if (cond == kCondGT || cond == kCondGE) {
783 // array.length as a lower bound isn't considered useful.
Mingyao Yang0304e182015-01-30 16:41:29 -0800784 if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) {
785 int32_t compensation = (cond == kCondGT) ? 1 : 0; // lower bound is inclusive
786 ValueBound new_lower = lower.Add(compensation, &overflow, &underflow);
787 if (overflow || underflow) {
788 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800789 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700790 ValueRange* new_range = new (GetGraph()->GetArena())
791 ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max());
792 ApplyRangeFromComparison(left, block, true_successor, new_range);
793 }
794
795 if (!upper.Equals(ValueBound::Max())) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800796 int32_t compensation = (cond == kCondGE) ? -1 : 0; // upper bound is inclusive
797 ValueBound new_upper = upper.Add(compensation, &overflow, &underflow);
798 if (overflow || underflow) {
799 return;
Mingyao Yang64197522014-12-05 15:56:23 -0800800 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700801 ValueRange* new_range = new (GetGraph()->GetArena())
802 ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper);
803 ApplyRangeFromComparison(left, block, false_successor, new_range);
804 }
Aart Bika2106892016-05-04 14:00:55 -0700805 } else if (cond == kCondNE || cond == kCondEQ) {
806 if (left->IsArrayLength() && lower.IsConstant() && upper.IsConstant()) {
807 // Special case:
808 // length == [c,d] yields [c, d] along true
809 // length != [c,d] yields [c, d] along false
810 if (!lower.Equals(ValueBound::Min()) || !upper.Equals(ValueBound::Max())) {
811 ValueRange* new_range = new (GetGraph()->GetArena())
812 ValueRange(GetGraph()->GetArena(), lower, upper);
813 ApplyRangeFromComparison(
814 left, block, cond == kCondEQ ? true_successor : false_successor, new_range);
815 }
816 // In addition:
817 // length == 0 yields [1, max] along false
818 // length != 0 yields [1, max] along true
819 if (lower.GetConstant() == 0 && upper.GetConstant() == 0) {
820 ValueRange* new_range = new (GetGraph()->GetArena())
821 ValueRange(GetGraph()->GetArena(), ValueBound(nullptr, 1), ValueBound::Max());
822 ApplyRangeFromComparison(
823 left, block, cond == kCondEQ ? false_successor : true_successor, new_range);
824 }
825 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700826 }
827 }
828
Aart Bik4a342772015-11-30 10:17:46 -0800829 void VisitBoundsCheck(HBoundsCheck* bounds_check) OVERRIDE {
Mingyao Yangf384f882014-10-22 16:08:18 -0700830 HBasicBlock* block = bounds_check->GetBlock();
831 HInstruction* index = bounds_check->InputAt(0);
832 HInstruction* array_length = bounds_check->InputAt(1);
Mingyao Yang3584bce2015-05-19 16:01:59 -0700833 DCHECK(array_length->IsIntConstant() ||
834 array_length->IsArrayLength() ||
835 array_length->IsPhi());
Aart Bik4a342772015-11-30 10:17:46 -0800836 bool try_dynamic_bce = true;
Aart Bik1d239822016-02-09 14:26:34 -0800837 // Analyze index range.
Mingyao Yang0304e182015-01-30 16:41:29 -0800838 if (!index->IsIntConstant()) {
Aart Bik1d239822016-02-09 14:26:34 -0800839 // Non-constant index.
Aart Bik22af3be2015-09-10 12:50:58 -0700840 ValueBound lower = ValueBound(nullptr, 0); // constant 0
841 ValueBound upper = ValueBound(array_length, -1); // array_length - 1
842 ValueRange array_range(GetGraph()->GetArena(), lower, upper);
Aart Bik1d239822016-02-09 14:26:34 -0800843 // Try index range obtained by dominator-based analysis.
Mingyao Yang0304e182015-01-30 16:41:29 -0800844 ValueRange* index_range = LookupValueRange(index, block);
Aart Bik22af3be2015-09-10 12:50:58 -0700845 if (index_range != nullptr && index_range->FitsIn(&array_range)) {
Aart Bik4a342772015-11-30 10:17:46 -0800846 ReplaceInstruction(bounds_check, index);
Aart Bik22af3be2015-09-10 12:50:58 -0700847 return;
848 }
Aart Bik1d239822016-02-09 14:26:34 -0800849 // Try index range obtained by induction variable analysis.
Aart Bik4a342772015-11-30 10:17:46 -0800850 // Disables dynamic bce if OOB is certain.
Aart Bik52be7e72016-06-23 11:20:41 -0700851 if (InductionRangeFitsIn(&array_range, bounds_check, &try_dynamic_bce)) {
Aart Bik4a342772015-11-30 10:17:46 -0800852 ReplaceInstruction(bounds_check, index);
Aart Bik22af3be2015-09-10 12:50:58 -0700853 return;
Mingyao Yangf384f882014-10-22 16:08:18 -0700854 }
Mingyao Yang0304e182015-01-30 16:41:29 -0800855 } else {
Aart Bik1d239822016-02-09 14:26:34 -0800856 // Constant index.
Mingyao Yang0304e182015-01-30 16:41:29 -0800857 int32_t constant = index->AsIntConstant()->GetValue();
858 if (constant < 0) {
859 // Will always throw exception.
860 return;
Aart Bik1d239822016-02-09 14:26:34 -0800861 } else if (array_length->IsIntConstant()) {
Mingyao Yang0304e182015-01-30 16:41:29 -0800862 if (constant < array_length->AsIntConstant()->GetValue()) {
Aart Bik4a342772015-11-30 10:17:46 -0800863 ReplaceInstruction(bounds_check, index);
Mingyao Yang0304e182015-01-30 16:41:29 -0800864 }
865 return;
866 }
Aart Bik1d239822016-02-09 14:26:34 -0800867 // Analyze array length range.
Mingyao Yang0304e182015-01-30 16:41:29 -0800868 DCHECK(array_length->IsArrayLength());
869 ValueRange* existing_range = LookupValueRange(array_length, block);
870 if (existing_range != nullptr) {
871 ValueBound lower = existing_range->GetLower();
872 DCHECK(lower.IsConstant());
873 if (constant < lower.GetConstant()) {
Aart Bik4a342772015-11-30 10:17:46 -0800874 ReplaceInstruction(bounds_check, index);
Mingyao Yang0304e182015-01-30 16:41:29 -0800875 return;
876 } else {
877 // Existing range isn't strong enough to eliminate the bounds check.
878 // Fall through to update the array_length range with info from this
879 // bounds check.
880 }
881 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700882 // Once we have an array access like 'array[5] = 1', we record array.length >= 6.
Mingyao Yang0304e182015-01-30 16:41:29 -0800883 // We currently don't do it for non-constant index since a valid array[i] can't prove
884 // a valid array[i-1] yet due to the lower bound side.
Aart Bikaab5b752015-09-23 11:18:57 -0700885 if (constant == std::numeric_limits<int32_t>::max()) {
886 // Max() as an index will definitely throw AIOOBE.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700887 return;
Aart Bik1d239822016-02-09 14:26:34 -0800888 } else {
889 ValueBound lower = ValueBound(nullptr, constant + 1);
890 ValueBound upper = ValueBound::Max();
891 ValueRange* range = new (GetGraph()->GetArena())
892 ValueRange(GetGraph()->GetArena(), lower, upper);
893 AssignRange(block, array_length, range);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700894 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700895 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700896
Aart Bik4a342772015-11-30 10:17:46 -0800897 // If static analysis fails, and OOB is not certain, try dynamic elimination.
898 if (try_dynamic_bce) {
Aart Bik1d239822016-02-09 14:26:34 -0800899 // Try loop-based dynamic elimination.
Aart Bik67def592016-07-14 17:19:43 -0700900 HLoopInformation* loop = bounds_check->GetBlock()->GetLoopInformation();
901 bool needs_finite_test = false;
902 bool needs_taken_test = false;
903 if (DynamicBCESeemsProfitable(loop, bounds_check->GetBlock()) &&
Aart Bik16d3a652016-09-09 10:33:50 -0700904 induction_range_.CanGenerateRange(
Aart Bik67def592016-07-14 17:19:43 -0700905 bounds_check, index, &needs_finite_test, &needs_taken_test) &&
906 CanHandleInfiniteLoop(loop, index, needs_finite_test) &&
907 // Do this test last, since it may generate code.
908 CanHandleLength(loop, array_length, needs_taken_test)) {
909 TransformLoopForDeoptimizationIfNeeded(loop, needs_taken_test);
910 TransformLoopForDynamicBCE(loop, bounds_check);
Aart Bik1d239822016-02-09 14:26:34 -0800911 return;
912 }
Aart Bik67def592016-07-14 17:19:43 -0700913 // Otherwise, prepare dominator-based dynamic elimination.
Aart Bik1d239822016-02-09 14:26:34 -0800914 if (first_index_bounds_check_map_.find(array_length->GetId()) ==
915 first_index_bounds_check_map_.end()) {
916 // Remember the first bounds check against each array_length. That bounds check
917 // instruction has an associated HEnvironment where we may add an HDeoptimize
918 // to eliminate subsequent bounds checks against the same array_length.
919 first_index_bounds_check_map_.Put(array_length->GetId(), bounds_check);
920 }
Aart Bik4a342772015-11-30 10:17:46 -0800921 }
Mingyao Yangf384f882014-10-22 16:08:18 -0700922 }
923
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100924 static bool HasSameInputAtBackEdges(HPhi* phi) {
925 DCHECK(phi->IsLoopHeaderPhi());
Vladimir Markoe9004912016-06-16 16:50:52 +0100926 HConstInputsRef inputs = phi->GetInputs();
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100927 // Start with input 1. Input 0 is from the incoming block.
Vladimir Markoe9004912016-06-16 16:50:52 +0100928 const HInstruction* input1 = inputs[1];
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100929 DCHECK(phi->GetBlock()->GetLoopInformation()->IsBackEdge(
Vladimir Markoec7802a2015-10-01 20:57:57 +0100930 *phi->GetBlock()->GetPredecessors()[1]));
Vladimir Marko372f10e2016-05-17 16:30:10 +0100931 for (size_t i = 2; i < inputs.size(); ++i) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100932 DCHECK(phi->GetBlock()->GetLoopInformation()->IsBackEdge(
Vladimir Markoec7802a2015-10-01 20:57:57 +0100933 *phi->GetBlock()->GetPredecessors()[i]));
Vladimir Marko372f10e2016-05-17 16:30:10 +0100934 if (input1 != inputs[i]) {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100935 return false;
936 }
937 }
938 return true;
939 }
940
Aart Bik4a342772015-11-30 10:17:46 -0800941 void VisitPhi(HPhi* phi) OVERRIDE {
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100942 if (phi->IsLoopHeaderPhi()
943 && (phi->GetType() == Primitive::kPrimInt)
944 && HasSameInputAtBackEdges(phi)) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700945 HInstruction* instruction = phi->InputAt(1);
Mingyao Yang0304e182015-01-30 16:41:29 -0800946 HInstruction *left;
947 int32_t increment;
948 if (ValueBound::IsAddOrSubAConstant(instruction, &left, &increment)) {
949 if (left == phi) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700950 HInstruction* initial_value = phi->InputAt(0);
951 ValueRange* range = nullptr;
Mingyao Yang64197522014-12-05 15:56:23 -0800952 if (increment == 0) {
Mingyao Yangf384f882014-10-22 16:08:18 -0700953 // Add constant 0. It's really a fixed value.
954 range = new (GetGraph()->GetArena()) ValueRange(
955 GetGraph()->GetArena(),
Mingyao Yang64197522014-12-05 15:56:23 -0800956 ValueBound(initial_value, 0),
957 ValueBound(initial_value, 0));
Mingyao Yangf384f882014-10-22 16:08:18 -0700958 } else {
959 // Monotonically increasing/decreasing.
Mingyao Yang64197522014-12-05 15:56:23 -0800960 bool found;
961 ValueBound bound = ValueBound::DetectValueBoundFromValue(
962 initial_value, &found);
963 if (!found) {
964 // No constant or array.length+c bound found.
965 // For i=j, we can still use j's upper bound as i's upper bound.
966 // Same for lower.
967 ValueRange* initial_range = LookupValueRange(initial_value, phi->GetBlock());
968 if (initial_range != nullptr) {
969 bound = increment > 0 ? initial_range->GetLower() :
970 initial_range->GetUpper();
971 } else {
972 bound = increment > 0 ? ValueBound::Min() : ValueBound::Max();
973 }
974 }
975 range = new (GetGraph()->GetArena()) MonotonicValueRange(
Mingyao Yangf384f882014-10-22 16:08:18 -0700976 GetGraph()->GetArena(),
Mingyao Yang206d6fd2015-04-13 16:46:28 -0700977 phi,
Mingyao Yangf384f882014-10-22 16:08:18 -0700978 initial_value,
Mingyao Yang64197522014-12-05 15:56:23 -0800979 increment,
980 bound);
Mingyao Yangf384f882014-10-22 16:08:18 -0700981 }
Aart Bik1d239822016-02-09 14:26:34 -0800982 AssignRange(phi->GetBlock(), phi, range);
Mingyao Yangf384f882014-10-22 16:08:18 -0700983 }
984 }
985 }
986 }
987
Aart Bik4a342772015-11-30 10:17:46 -0800988 void VisitIf(HIf* instruction) OVERRIDE {
Mingyao Yangf384f882014-10-22 16:08:18 -0700989 if (instruction->InputAt(0)->IsCondition()) {
990 HCondition* cond = instruction->InputAt(0)->AsCondition();
Aart Bika2106892016-05-04 14:00:55 -0700991 HandleIf(instruction, cond->GetLeft(), cond->GetRight(), cond->GetCondition());
Mingyao Yangf384f882014-10-22 16:08:18 -0700992 }
993 }
994
Aart Bik4a342772015-11-30 10:17:46 -0800995 void VisitAdd(HAdd* add) OVERRIDE {
Mingyao Yangf384f882014-10-22 16:08:18 -0700996 HInstruction* right = add->GetRight();
997 if (right->IsIntConstant()) {
998 ValueRange* left_range = LookupValueRange(add->GetLeft(), add->GetBlock());
999 if (left_range == nullptr) {
1000 return;
1001 }
1002 ValueRange* range = left_range->Add(right->AsIntConstant()->GetValue());
1003 if (range != nullptr) {
Aart Bik1d239822016-02-09 14:26:34 -08001004 AssignRange(add->GetBlock(), add, range);
Mingyao Yangf384f882014-10-22 16:08:18 -07001005 }
1006 }
1007 }
1008
Aart Bik4a342772015-11-30 10:17:46 -08001009 void VisitSub(HSub* sub) OVERRIDE {
Mingyao Yangf384f882014-10-22 16:08:18 -07001010 HInstruction* left = sub->GetLeft();
1011 HInstruction* right = sub->GetRight();
1012 if (right->IsIntConstant()) {
1013 ValueRange* left_range = LookupValueRange(left, sub->GetBlock());
1014 if (left_range == nullptr) {
1015 return;
1016 }
1017 ValueRange* range = left_range->Add(-right->AsIntConstant()->GetValue());
1018 if (range != nullptr) {
Aart Bik1d239822016-02-09 14:26:34 -08001019 AssignRange(sub->GetBlock(), sub, range);
Mingyao Yangf384f882014-10-22 16:08:18 -07001020 return;
1021 }
1022 }
1023
1024 // Here we are interested in the typical triangular case of nested loops,
1025 // such as the inner loop 'for (int j=0; j<array.length-i; j++)' where i
1026 // 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 -08001027
1028 // Try to handle (array.length - i) or (array.length + c - i) format.
1029 HInstruction* left_of_left; // left input of left.
1030 int32_t right_const = 0;
1031 if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &right_const)) {
1032 left = left_of_left;
1033 }
1034 // The value of left input of the sub equals (left + right_const).
1035
Mingyao Yangf384f882014-10-22 16:08:18 -07001036 if (left->IsArrayLength()) {
1037 HInstruction* array_length = left->AsArrayLength();
1038 ValueRange* right_range = LookupValueRange(right, sub->GetBlock());
1039 if (right_range != nullptr) {
1040 ValueBound lower = right_range->GetLower();
1041 ValueBound upper = right_range->GetUpper();
Mingyao Yang0304e182015-01-30 16:41:29 -08001042 if (lower.IsConstant() && upper.IsRelatedToArrayLength()) {
Mingyao Yangf384f882014-10-22 16:08:18 -07001043 HInstruction* upper_inst = upper.GetInstruction();
Mingyao Yang0304e182015-01-30 16:41:29 -08001044 // Make sure it's the same array.
1045 if (ValueBound::Equal(array_length, upper_inst)) {
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001046 int32_t c0 = right_const;
1047 int32_t c1 = lower.GetConstant();
1048 int32_t c2 = upper.GetConstant();
1049 // (array.length + c0 - v) where v is in [c1, array.length + c2]
1050 // gets [c0 - c2, array.length + c0 - c1] as its value range.
1051 if (!ValueBound::WouldAddOverflowOrUnderflow(c0, -c2) &&
1052 !ValueBound::WouldAddOverflowOrUnderflow(c0, -c1)) {
1053 if ((c0 - c1) <= 0) {
1054 // array.length + (c0 - c1) won't overflow/underflow.
1055 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1056 GetGraph()->GetArena(),
1057 ValueBound(nullptr, right_const - upper.GetConstant()),
1058 ValueBound(array_length, right_const - lower.GetConstant()));
Aart Bik1d239822016-02-09 14:26:34 -08001059 AssignRange(sub->GetBlock(), sub, range);
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001060 }
1061 }
Mingyao Yangf384f882014-10-22 16:08:18 -07001062 }
1063 }
1064 }
1065 }
1066 }
1067
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001068 void FindAndHandlePartialArrayLength(HBinaryOperation* instruction) {
1069 DCHECK(instruction->IsDiv() || instruction->IsShr() || instruction->IsUShr());
1070 HInstruction* right = instruction->GetRight();
1071 int32_t right_const;
1072 if (right->IsIntConstant()) {
1073 right_const = right->AsIntConstant()->GetValue();
1074 // Detect division by two or more.
1075 if ((instruction->IsDiv() && right_const <= 1) ||
1076 (instruction->IsShr() && right_const < 1) ||
1077 (instruction->IsUShr() && right_const < 1)) {
1078 return;
1079 }
1080 } else {
1081 return;
1082 }
1083
1084 // Try to handle array.length/2 or (array.length-1)/2 format.
1085 HInstruction* left = instruction->GetLeft();
1086 HInstruction* left_of_left; // left input of left.
1087 int32_t c = 0;
1088 if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &c)) {
1089 left = left_of_left;
1090 }
1091 // The value of left input of instruction equals (left + c).
1092
1093 // (array_length + 1) or smaller divided by two or more
Aart Bikaab5b752015-09-23 11:18:57 -07001094 // always generate a value in [Min(), array_length].
1095 // This is true even if array_length is Max().
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001096 if (left->IsArrayLength() && c <= 1) {
1097 if (instruction->IsUShr() && c < 0) {
1098 // Make sure for unsigned shift, left side is not negative.
1099 // e.g. if array_length is 2, ((array_length - 3) >>> 2) is way bigger
1100 // than array_length.
1101 return;
1102 }
1103 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1104 GetGraph()->GetArena(),
Aart Bikaab5b752015-09-23 11:18:57 -07001105 ValueBound(nullptr, std::numeric_limits<int32_t>::min()),
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001106 ValueBound(left, 0));
Aart Bik1d239822016-02-09 14:26:34 -08001107 AssignRange(instruction->GetBlock(), instruction, range);
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001108 }
1109 }
1110
Aart Bik4a342772015-11-30 10:17:46 -08001111 void VisitDiv(HDiv* div) OVERRIDE {
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001112 FindAndHandlePartialArrayLength(div);
1113 }
1114
Aart Bik4a342772015-11-30 10:17:46 -08001115 void VisitShr(HShr* shr) OVERRIDE {
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001116 FindAndHandlePartialArrayLength(shr);
1117 }
1118
Aart Bik4a342772015-11-30 10:17:46 -08001119 void VisitUShr(HUShr* ushr) OVERRIDE {
Mingyao Yang8c8bad82015-02-09 18:13:26 -08001120 FindAndHandlePartialArrayLength(ushr);
1121 }
1122
Aart Bik4a342772015-11-30 10:17:46 -08001123 void VisitAnd(HAnd* instruction) OVERRIDE {
Mingyao Yang4559f002015-02-27 14:43:53 -08001124 if (instruction->GetRight()->IsIntConstant()) {
1125 int32_t constant = instruction->GetRight()->AsIntConstant()->GetValue();
1126 if (constant > 0) {
1127 // constant serves as a mask so any number masked with it
1128 // gets a [0, constant] value range.
1129 ValueRange* range = new (GetGraph()->GetArena()) ValueRange(
1130 GetGraph()->GetArena(),
1131 ValueBound(nullptr, 0),
1132 ValueBound(nullptr, constant));
Aart Bik1d239822016-02-09 14:26:34 -08001133 AssignRange(instruction->GetBlock(), instruction, range);
Mingyao Yang4559f002015-02-27 14:43:53 -08001134 }
1135 }
1136 }
1137
Aart Bik4a342772015-11-30 10:17:46 -08001138 void VisitNewArray(HNewArray* new_array) OVERRIDE {
Mingyao Yang0304e182015-01-30 16:41:29 -08001139 HInstruction* len = new_array->InputAt(0);
1140 if (!len->IsIntConstant()) {
1141 HInstruction *left;
1142 int32_t right_const;
1143 if (ValueBound::IsAddOrSubAConstant(len, &left, &right_const)) {
1144 // (left + right_const) is used as size to new the array.
1145 // We record "-right_const <= left <= new_array - right_const";
1146 ValueBound lower = ValueBound(nullptr, -right_const);
1147 // We use new_array for the bound instead of new_array.length,
1148 // which isn't available as an instruction yet. new_array will
1149 // be treated the same as new_array.length when it's used in a ValueBound.
1150 ValueBound upper = ValueBound(new_array, -right_const);
1151 ValueRange* range = new (GetGraph()->GetArena())
1152 ValueRange(GetGraph()->GetArena(), lower, upper);
Nicolas Geoffraya09ff9c2015-06-24 10:38:27 +01001153 ValueRange* existing_range = LookupValueRange(left, new_array->GetBlock());
1154 if (existing_range != nullptr) {
1155 range = existing_range->Narrow(range);
1156 }
Aart Bik1d239822016-02-09 14:26:34 -08001157 AssignRange(new_array->GetBlock(), left, range);
Mingyao Yang0304e182015-01-30 16:41:29 -08001158 }
1159 }
1160 }
1161
Aart Bik4a342772015-11-30 10:17:46 -08001162 /**
1163 * After null/bounds checks are eliminated, some invariant array references
1164 * may be exposed underneath which can be hoisted out of the loop to the
1165 * preheader or, in combination with dynamic bce, the deoptimization block.
1166 *
1167 * for (int i = 0; i < n; i++) {
1168 * <-------+
1169 * for (int j = 0; j < n; j++) |
1170 * a[i][j] = 0; --a[i]--+
1171 * }
1172 *
Aart Bik1d239822016-02-09 14:26:34 -08001173 * Note: this optimization is no longer applied after dominator-based dynamic deoptimization
1174 * has occurred (see AddCompareWithDeoptimization()), since in those cases it would be
1175 * unsafe to hoist array references across their deoptimization instruction inside a loop.
Aart Bik4a342772015-11-30 10:17:46 -08001176 */
1177 void VisitArrayGet(HArrayGet* array_get) OVERRIDE {
Aart Bik1d239822016-02-09 14:26:34 -08001178 if (!has_dom_based_dynamic_bce_ && array_get->IsInLoop()) {
Aart Bik4a342772015-11-30 10:17:46 -08001179 HLoopInformation* loop = array_get->GetBlock()->GetLoopInformation();
Mingyao Yang4b467ed2015-11-19 17:04:22 -08001180 if (loop->IsDefinedOutOfTheLoop(array_get->InputAt(0)) &&
1181 loop->IsDefinedOutOfTheLoop(array_get->InputAt(1))) {
Aart Bik4a342772015-11-30 10:17:46 -08001182 SideEffects loop_effects = side_effects_.GetLoopEffects(loop->GetHeader());
1183 if (!array_get->GetSideEffects().MayDependOn(loop_effects)) {
Anton Shaminf89381f2016-05-16 16:44:13 +06001184 // We can hoist ArrayGet only if its execution is guaranteed on every iteration.
1185 // In other words only if array_get_bb dominates all back branches.
1186 if (loop->DominatesAllBackEdges(array_get->GetBlock())) {
1187 HoistToPreHeaderOrDeoptBlock(loop, array_get);
1188 }
Aart Bik4a342772015-11-30 10:17:46 -08001189 }
1190 }
1191 }
1192 }
1193
Aart Bik67def592016-07-14 17:19:43 -07001194 /** Performs dominator-based dynamic elimination on suitable set of bounds checks. */
Aart Bik1d239822016-02-09 14:26:34 -08001195 void AddCompareWithDeoptimization(HBasicBlock* block,
1196 HInstruction* array_length,
1197 HInstruction* base,
1198 int32_t min_c, int32_t max_c) {
1199 HBoundsCheck* bounds_check =
1200 first_index_bounds_check_map_.Get(array_length->GetId())->AsBoundsCheck();
1201 // Construct deoptimization on single or double bounds on range [base-min_c,base+max_c],
1202 // for example either for a[0]..a[3] just 3 or for a[base-1]..a[base+3] both base-1
1203 // and base+3, since we made the assumption any in between value may occur too.
Aart Bik67def592016-07-14 17:19:43 -07001204 // In code, using unsigned comparisons:
1205 // (1) constants only
1206 // if (max_c >= a.length) deoptimize;
1207 // (2) general case
1208 // if (base-min_c > base+max_c) deoptimize;
1209 // if (base+max_c >= a.length ) deoptimize;
Aart Bik1d239822016-02-09 14:26:34 -08001210 static_assert(kMaxLengthForAddingDeoptimize < std::numeric_limits<int32_t>::max(),
1211 "Incorrect max length may be subject to arithmetic wrap-around");
1212 HInstruction* upper = GetGraph()->GetIntConstant(max_c);
1213 if (base == nullptr) {
1214 DCHECK_GE(min_c, 0);
1215 } else {
1216 HInstruction* lower = new (GetGraph()->GetArena())
1217 HAdd(Primitive::kPrimInt, base, GetGraph()->GetIntConstant(min_c));
1218 upper = new (GetGraph()->GetArena()) HAdd(Primitive::kPrimInt, base, upper);
1219 block->InsertInstructionBefore(lower, bounds_check);
1220 block->InsertInstructionBefore(upper, bounds_check);
1221 InsertDeoptInBlock(bounds_check, new (GetGraph()->GetArena()) HAbove(lower, upper));
1222 }
1223 InsertDeoptInBlock(bounds_check, new (GetGraph()->GetArena()) HAboveOrEqual(upper, array_length));
1224 // Flag that this kind of deoptimization has occurred.
1225 has_dom_based_dynamic_bce_ = true;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001226 }
1227
Aart Bik67def592016-07-14 17:19:43 -07001228 /** Attempts dominator-based dynamic elimination on remaining candidates. */
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001229 void AddComparesWithDeoptimization(HBasicBlock* block) {
Vladimir Markoda571cb2016-02-15 17:54:56 +00001230 for (const auto& entry : first_index_bounds_check_map_) {
1231 HBoundsCheck* bounds_check = entry.second;
Aart Bik1d239822016-02-09 14:26:34 -08001232 HInstruction* index = bounds_check->InputAt(0);
Nicolas Geoffray8df886b2015-06-24 14:57:44 +01001233 HInstruction* array_length = bounds_check->InputAt(1);
1234 if (!array_length->IsArrayLength()) {
Aart Bik1d239822016-02-09 14:26:34 -08001235 continue; // disregard phis and constants
Nicolas Geoffray8df886b2015-06-24 14:57:44 +01001236 }
Aart Bik1ae88742016-03-14 14:11:26 -07001237 // Collect all bounds checks that are still there and that are related as "a[base + constant]"
Aart Bik1d239822016-02-09 14:26:34 -08001238 // for a base instruction (possibly absent) and various constants. Note that no attempt
1239 // is made to partition the set into matching subsets (viz. a[0], a[1] and a[base+1] and
1240 // a[base+2] are considered as one set).
1241 // TODO: would such a partitioning be worthwhile?
1242 ValueBound value = ValueBound::AsValueBound(index);
1243 HInstruction* base = value.GetInstruction();
1244 int32_t min_c = base == nullptr ? 0 : value.GetConstant();
1245 int32_t max_c = value.GetConstant();
1246 ArenaVector<HBoundsCheck*> candidates(
1247 GetGraph()->GetArena()->Adapter(kArenaAllocBoundsCheckElimination));
1248 ArenaVector<HBoundsCheck*> standby(
1249 GetGraph()->GetArena()->Adapter(kArenaAllocBoundsCheckElimination));
Vladimir Marko46817b82016-03-29 12:21:58 +01001250 for (const HUseListNode<HInstruction*>& use : array_length->GetUses()) {
Aart Bik1d239822016-02-09 14:26:34 -08001251 // Another bounds check in same or dominated block?
Vladimir Marko46817b82016-03-29 12:21:58 +01001252 HInstruction* user = use.GetUser();
Aart Bik1d239822016-02-09 14:26:34 -08001253 HBasicBlock* other_block = user->GetBlock();
1254 if (user->IsBoundsCheck() && block->Dominates(other_block)) {
1255 HBoundsCheck* other_bounds_check = user->AsBoundsCheck();
1256 HInstruction* other_index = other_bounds_check->InputAt(0);
1257 HInstruction* other_array_length = other_bounds_check->InputAt(1);
1258 ValueBound other_value = ValueBound::AsValueBound(other_index);
1259 if (array_length == other_array_length && base == other_value.GetInstruction()) {
Aart Bik1ae88742016-03-14 14:11:26 -07001260 // Reject certain OOB if BoundsCheck(l, l) occurs on considered subset.
1261 if (array_length == other_index) {
1262 candidates.clear();
1263 standby.clear();
1264 break;
1265 }
Aart Bik1d239822016-02-09 14:26:34 -08001266 // Since a subsequent dominated block could be under a conditional, only accept
1267 // the other bounds check if it is in same block or both blocks dominate the exit.
1268 // TODO: we could improve this by testing proper post-dominance, or even if this
1269 // constant is seen along *all* conditional paths that follow.
1270 HBasicBlock* exit = GetGraph()->GetExitBlock();
1271 if (block == user->GetBlock() ||
1272 (block->Dominates(exit) && other_block->Dominates(exit))) {
Aart Bik1ae88742016-03-14 14:11:26 -07001273 int32_t other_c = other_value.GetConstant();
Aart Bik1d239822016-02-09 14:26:34 -08001274 min_c = std::min(min_c, other_c);
1275 max_c = std::max(max_c, other_c);
1276 candidates.push_back(other_bounds_check);
1277 } else {
1278 // Add this candidate later only if it falls into the range.
1279 standby.push_back(other_bounds_check);
1280 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001281 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001282 }
1283 }
Aart Bik1d239822016-02-09 14:26:34 -08001284 // Add standby candidates that fall in selected range.
Vladimir Markoda571cb2016-02-15 17:54:56 +00001285 for (HBoundsCheck* other_bounds_check : standby) {
Aart Bik1d239822016-02-09 14:26:34 -08001286 HInstruction* other_index = other_bounds_check->InputAt(0);
1287 int32_t other_c = ValueBound::AsValueBound(other_index).GetConstant();
1288 if (min_c <= other_c && other_c <= max_c) {
1289 candidates.push_back(other_bounds_check);
1290 }
1291 }
Aart Bik67def592016-07-14 17:19:43 -07001292 // Perform dominator-based deoptimization if it seems profitable, where we eliminate
1293 // bounds checks and replace these with deopt checks that guard against any possible
1294 // OOB. Note that we reject cases where the distance min_c:max_c range gets close to
1295 // the maximum possible array length, since those cases are likely to always deopt
1296 // (such situations do not necessarily go OOB, though, since the array could be really
1297 // large, or the programmer could rely on arithmetic wrap-around from max to min).
Aart Bik1d239822016-02-09 14:26:34 -08001298 size_t threshold = kThresholdForAddingDeoptimize + (base == nullptr ? 0 : 1); // extra test?
1299 uint32_t distance = static_cast<uint32_t>(max_c) - static_cast<uint32_t>(min_c);
1300 if (candidates.size() >= threshold &&
1301 (base != nullptr || min_c >= 0) && // reject certain OOB
1302 distance <= kMaxLengthForAddingDeoptimize) { // reject likely/certain deopt
1303 AddCompareWithDeoptimization(block, array_length, base, min_c, max_c);
Aart Bik67def592016-07-14 17:19:43 -07001304 for (HBoundsCheck* other_bounds_check : candidates) {
Aart Bik1ae88742016-03-14 14:11:26 -07001305 // Only replace if still in the graph. This avoids visiting the same
1306 // bounds check twice if it occurred multiple times in the use list.
1307 if (other_bounds_check->IsInBlock()) {
1308 ReplaceInstruction(other_bounds_check, other_bounds_check->InputAt(0));
1309 }
Aart Bik1d239822016-02-09 14:26:34 -08001310 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001311 }
1312 }
1313 }
1314
Aart Bik4a342772015-11-30 10:17:46 -08001315 /**
1316 * Returns true if static range analysis based on induction variables can determine the bounds
1317 * check on the given array range is always satisfied with the computed index range. The output
1318 * parameter try_dynamic_bce is set to false if OOB is certain.
1319 */
1320 bool InductionRangeFitsIn(ValueRange* array_range,
Aart Bik52be7e72016-06-23 11:20:41 -07001321 HBoundsCheck* context,
Aart Bik4a342772015-11-30 10:17:46 -08001322 bool* try_dynamic_bce) {
1323 InductionVarRange::Value v1;
1324 InductionVarRange::Value v2;
1325 bool needs_finite_test = false;
Aart Bik52be7e72016-06-23 11:20:41 -07001326 HInstruction* index = context->InputAt(0);
1327 HInstruction* hint = ValueBound::HuntForDeclaration(context->InputAt(1));
1328 if (induction_range_.GetInductionRange(context, index, hint, &v1, &v2, &needs_finite_test)) {
1329 if (v1.is_known && (v1.a_constant == 0 || v1.a_constant == 1) &&
1330 v2.is_known && (v2.a_constant == 0 || v2.a_constant == 1)) {
1331 DCHECK(v1.a_constant == 1 || v1.instruction == nullptr);
1332 DCHECK(v2.a_constant == 1 || v2.instruction == nullptr);
1333 ValueRange index_range(GetGraph()->GetArena(),
1334 ValueBound(v1.instruction, v1.b_constant),
1335 ValueBound(v2.instruction, v2.b_constant));
1336 // If analysis reveals a certain OOB, disable dynamic BCE. Otherwise,
1337 // use analysis for static bce only if loop is finite.
1338 if (index_range.GetLower().LessThan(array_range->GetLower()) ||
1339 index_range.GetUpper().GreaterThan(array_range->GetUpper())) {
1340 *try_dynamic_bce = false;
1341 } else if (!needs_finite_test && index_range.FitsIn(array_range)) {
1342 return true;
Aart Bikb738d4f2015-12-03 11:23:35 -08001343 }
Aart Bik52be7e72016-06-23 11:20:41 -07001344 }
Aart Bik1fc3afb2016-02-02 13:26:16 -08001345 }
Aart Bik4a342772015-11-30 10:17:46 -08001346 return false;
1347 }
1348
1349 /**
Aart Bik67def592016-07-14 17:19:43 -07001350 * Performs loop-based dynamic elimination on a bounds check. In order to minimize the
1351 * number of eventually generated tests, related bounds checks with tests that can be
1352 * combined with tests for the given bounds check are collected first.
Aart Bik4a342772015-11-30 10:17:46 -08001353 */
Aart Bik67def592016-07-14 17:19:43 -07001354 void TransformLoopForDynamicBCE(HLoopInformation* loop, HBoundsCheck* bounds_check) {
1355 HInstruction* index = bounds_check->InputAt(0);
1356 HInstruction* array_length = bounds_check->InputAt(1);
1357 DCHECK(loop->IsDefinedOutOfTheLoop(array_length)); // pre-checked
1358 DCHECK(loop->DominatesAllBackEdges(bounds_check->GetBlock()));
1359 // Collect all bounds checks in the same loop that are related as "a[base + constant]"
1360 // for a base instruction (possibly absent) and various constants.
1361 ValueBound value = ValueBound::AsValueBound(index);
1362 HInstruction* base = value.GetInstruction();
1363 int32_t min_c = base == nullptr ? 0 : value.GetConstant();
1364 int32_t max_c = value.GetConstant();
1365 ArenaVector<HBoundsCheck*> candidates(
1366 GetGraph()->GetArena()->Adapter(kArenaAllocBoundsCheckElimination));
1367 ArenaVector<HBoundsCheck*> standby(
1368 GetGraph()->GetArena()->Adapter(kArenaAllocBoundsCheckElimination));
1369 for (const HUseListNode<HInstruction*>& use : array_length->GetUses()) {
1370 HInstruction* user = use.GetUser();
1371 if (user->IsBoundsCheck() && loop == user->GetBlock()->GetLoopInformation()) {
1372 HBoundsCheck* other_bounds_check = user->AsBoundsCheck();
1373 HInstruction* other_index = other_bounds_check->InputAt(0);
1374 HInstruction* other_array_length = other_bounds_check->InputAt(1);
1375 ValueBound other_value = ValueBound::AsValueBound(other_index);
1376 int32_t other_c = other_value.GetConstant();
1377 if (array_length == other_array_length && base == other_value.GetInstruction()) {
Aart Bik12a10602016-10-18 11:35:22 -07001378 // Ensure every candidate could be picked for code generation.
1379 bool b1 = false, b2 = false;
1380 if (!induction_range_.CanGenerateRange(other_bounds_check, other_index, &b1, &b2)) {
1381 continue;
1382 }
Aart Bik67def592016-07-14 17:19:43 -07001383 // Does the current basic block dominate all back edges? If not,
1384 // add this candidate later only if it falls into the range.
1385 if (!loop->DominatesAllBackEdges(user->GetBlock())) {
1386 standby.push_back(other_bounds_check);
1387 continue;
1388 }
1389 min_c = std::min(min_c, other_c);
1390 max_c = std::max(max_c, other_c);
1391 candidates.push_back(other_bounds_check);
1392 }
Aart Bik4a342772015-11-30 10:17:46 -08001393 }
Aart Bik4a342772015-11-30 10:17:46 -08001394 }
Aart Bik67def592016-07-14 17:19:43 -07001395 // Add standby candidates that fall in selected range.
1396 for (HBoundsCheck* other_bounds_check : standby) {
1397 HInstruction* other_index = other_bounds_check->InputAt(0);
1398 int32_t other_c = ValueBound::AsValueBound(other_index).GetConstant();
1399 if (min_c <= other_c && other_c <= max_c) {
1400 candidates.push_back(other_bounds_check);
1401 }
1402 }
1403 // Perform loop-based deoptimization if it seems profitable, where we eliminate bounds
1404 // checks and replace these with deopt checks that guard against any possible OOB.
1405 DCHECK_LT(0u, candidates.size());
1406 uint32_t distance = static_cast<uint32_t>(max_c) - static_cast<uint32_t>(min_c);
1407 if ((base != nullptr || min_c >= 0) && // reject certain OOB
1408 distance <= kMaxLengthForAddingDeoptimize) { // reject likely/certain deopt
1409 HBasicBlock* block = GetPreHeader(loop, bounds_check);
1410 HInstruction* min_lower = nullptr;
1411 HInstruction* min_upper = nullptr;
1412 HInstruction* max_lower = nullptr;
1413 HInstruction* max_upper = nullptr;
1414 // Iterate over all bounds checks.
1415 for (HBoundsCheck* other_bounds_check : candidates) {
1416 // Only handle if still in the graph. This avoids visiting the same
1417 // bounds check twice if it occurred multiple times in the use list.
1418 if (other_bounds_check->IsInBlock()) {
1419 HInstruction* other_index = other_bounds_check->InputAt(0);
1420 int32_t other_c = ValueBound::AsValueBound(other_index).GetConstant();
1421 // Generate code for either the maximum or minimum. Range analysis already was queried
1422 // whether code generation on the original and, thus, related bounds check was possible.
1423 // It handles either loop invariants (lower is not set) or unit strides.
1424 if (other_c == max_c) {
Aart Bik16d3a652016-09-09 10:33:50 -07001425 induction_range_.GenerateRange(
Aart Bik67def592016-07-14 17:19:43 -07001426 other_bounds_check, other_index, GetGraph(), block, &max_lower, &max_upper);
1427 } else if (other_c == min_c && base != nullptr) {
Aart Bik16d3a652016-09-09 10:33:50 -07001428 induction_range_.GenerateRange(
Aart Bik67def592016-07-14 17:19:43 -07001429 other_bounds_check, other_index, GetGraph(), block, &min_lower, &min_upper);
1430 }
1431 ReplaceInstruction(other_bounds_check, other_index);
1432 }
1433 }
1434 // In code, using unsigned comparisons:
1435 // (1) constants only
1436 // if (max_upper >= a.length ) deoptimize;
1437 // (2) two symbolic invariants
1438 // if (min_upper > max_upper) deoptimize; unless min_c == max_c
1439 // if (max_upper >= a.length ) deoptimize;
1440 // (3) general case, unit strides (where lower would exceed upper for arithmetic wrap-around)
1441 // if (min_lower > max_lower) deoptimize; unless min_c == max_c
1442 // if (max_lower > max_upper) deoptimize;
1443 // if (max_upper >= a.length ) deoptimize;
1444 if (base == nullptr) {
1445 // Constants only.
1446 DCHECK_GE(min_c, 0);
1447 DCHECK(min_lower == nullptr && min_upper == nullptr &&
1448 max_lower == nullptr && max_upper != nullptr);
1449 } else if (max_lower == nullptr) {
1450 // Two symbolic invariants.
1451 if (min_c != max_c) {
1452 DCHECK(min_lower == nullptr && min_upper != nullptr &&
1453 max_lower == nullptr && max_upper != nullptr);
1454 InsertDeoptInLoop(loop, block, new (GetGraph()->GetArena()) HAbove(min_upper, max_upper));
1455 } else {
1456 DCHECK(min_lower == nullptr && min_upper == nullptr &&
1457 max_lower == nullptr && max_upper != nullptr);
1458 }
1459 } else {
1460 // General case, unit strides.
1461 if (min_c != max_c) {
1462 DCHECK(min_lower != nullptr && min_upper != nullptr &&
1463 max_lower != nullptr && max_upper != nullptr);
1464 InsertDeoptInLoop(loop, block, new (GetGraph()->GetArena()) HAbove(min_lower, max_lower));
1465 } else {
1466 DCHECK(min_lower == nullptr && min_upper == nullptr &&
1467 max_lower != nullptr && max_upper != nullptr);
1468 }
1469 InsertDeoptInLoop(loop, block, new (GetGraph()->GetArena()) HAbove(max_lower, max_upper));
1470 }
1471 InsertDeoptInLoop(
1472 loop, block, new (GetGraph()->GetArena()) HAboveOrEqual(max_upper, array_length));
1473 } else {
1474 // TODO: if rejected, avoid doing this again for subsequent instructions in this set?
1475 }
Aart Bik4a342772015-11-30 10:17:46 -08001476 }
1477
1478 /**
1479 * Returns true if heuristics indicate that dynamic bce may be profitable.
1480 */
1481 bool DynamicBCESeemsProfitable(HLoopInformation* loop, HBasicBlock* block) {
1482 if (loop != nullptr) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001483 // The loop preheader of an irreducible loop does not dominate all the blocks in
1484 // the loop. We would need to find the common dominator of all blocks in the loop.
1485 if (loop->IsIrreducible()) {
1486 return false;
1487 }
Nicolas Geoffray93a18c52016-04-22 13:16:14 +01001488 // We should never deoptimize from an osr method, otherwise we might wrongly optimize
1489 // code dominated by the deoptimization.
1490 if (GetGraph()->IsCompilingOsr()) {
1491 return false;
1492 }
Aart Bik4a342772015-11-30 10:17:46 -08001493 // A try boundary preheader is hard to handle.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00001494 // TODO: remove this restriction.
Aart Bik4a342772015-11-30 10:17:46 -08001495 if (loop->GetPreHeader()->GetLastInstruction()->IsTryBoundary()) {
1496 return false;
1497 }
1498 // Does loop have early-exits? If so, the full range may not be covered by the loop
1499 // at runtime and testing the range may apply deoptimization unnecessarily.
1500 if (IsEarlyExitLoop(loop)) {
1501 return false;
1502 }
1503 // Does the current basic block dominate all back edges? If not,
1504 // don't apply dynamic bce to something that may not be executed.
Anton Shaminf89381f2016-05-16 16:44:13 +06001505 return loop->DominatesAllBackEdges(block);
Aart Bik4a342772015-11-30 10:17:46 -08001506 }
1507 return false;
1508 }
1509
1510 /**
1511 * Returns true if the loop has early exits, which implies it may not cover
1512 * the full range computed by range analysis based on induction variables.
1513 */
1514 bool IsEarlyExitLoop(HLoopInformation* loop) {
1515 const uint32_t loop_id = loop->GetHeader()->GetBlockId();
1516 // If loop has been analyzed earlier for early-exit, don't repeat the analysis.
1517 auto it = early_exit_loop_.find(loop_id);
1518 if (it != early_exit_loop_.end()) {
1519 return it->second;
1520 }
1521 // First time early-exit analysis for this loop. Since analysis requires scanning
1522 // the full loop-body, results of the analysis is stored for subsequent queries.
1523 HBlocksInLoopReversePostOrderIterator it_loop(*loop);
1524 for (it_loop.Advance(); !it_loop.Done(); it_loop.Advance()) {
1525 for (HBasicBlock* successor : it_loop.Current()->GetSuccessors()) {
1526 if (!loop->Contains(*successor)) {
1527 early_exit_loop_.Put(loop_id, true);
1528 return true;
1529 }
1530 }
1531 }
1532 early_exit_loop_.Put(loop_id, false);
1533 return false;
1534 }
1535
1536 /**
1537 * Returns true if the array length is already loop invariant, or can be made so
1538 * by handling the null check under the hood of the array length operation.
1539 */
1540 bool CanHandleLength(HLoopInformation* loop, HInstruction* length, bool needs_taken_test) {
Mingyao Yang4b467ed2015-11-19 17:04:22 -08001541 if (loop->IsDefinedOutOfTheLoop(length)) {
Aart Bik4a342772015-11-30 10:17:46 -08001542 return true;
1543 } else if (length->IsArrayLength() && length->GetBlock()->GetLoopInformation() == loop) {
1544 if (CanHandleNullCheck(loop, length->InputAt(0), needs_taken_test)) {
Aart Bik55b14df2016-01-12 14:12:47 -08001545 HoistToPreHeaderOrDeoptBlock(loop, length);
Aart Bik4a342772015-11-30 10:17:46 -08001546 return true;
1547 }
1548 }
1549 return false;
1550 }
1551
1552 /**
1553 * Returns true if the null check is already loop invariant, or can be made so
1554 * by generating a deoptimization test.
1555 */
1556 bool CanHandleNullCheck(HLoopInformation* loop, HInstruction* check, bool needs_taken_test) {
Mingyao Yang4b467ed2015-11-19 17:04:22 -08001557 if (loop->IsDefinedOutOfTheLoop(check)) {
Aart Bik4a342772015-11-30 10:17:46 -08001558 return true;
1559 } else if (check->IsNullCheck() && check->GetBlock()->GetLoopInformation() == loop) {
1560 HInstruction* array = check->InputAt(0);
Mingyao Yang4b467ed2015-11-19 17:04:22 -08001561 if (loop->IsDefinedOutOfTheLoop(array)) {
Aart Bik4a342772015-11-30 10:17:46 -08001562 // Generate: if (array == null) deoptimize;
Aart Bik55b14df2016-01-12 14:12:47 -08001563 TransformLoopForDeoptimizationIfNeeded(loop, needs_taken_test);
1564 HBasicBlock* block = GetPreHeader(loop, check);
Aart Bik4a342772015-11-30 10:17:46 -08001565 HInstruction* cond =
1566 new (GetGraph()->GetArena()) HEqual(array, GetGraph()->GetNullConstant());
Aart Bik1d239822016-02-09 14:26:34 -08001567 InsertDeoptInLoop(loop, block, cond);
Aart Bik4a342772015-11-30 10:17:46 -08001568 ReplaceInstruction(check, array);
1569 return true;
1570 }
1571 }
1572 return false;
1573 }
1574
1575 /**
1576 * Returns true if compiler can apply dynamic bce to loops that may be infinite
1577 * (e.g. for (int i = 0; i <= U; i++) with U = MAX_INT), which would invalidate
1578 * the range analysis evaluation code by "overshooting" the computed range.
1579 * Since deoptimization would be a bad choice, and there is no other version
1580 * of the loop to use, dynamic bce in such cases is only allowed if other tests
1581 * ensure the loop is finite.
1582 */
Aart Bik67def592016-07-14 17:19:43 -07001583 bool CanHandleInfiniteLoop(HLoopInformation* loop, HInstruction* index, bool needs_infinite_test) {
Aart Bik4a342772015-11-30 10:17:46 -08001584 if (needs_infinite_test) {
1585 // If we already forced the loop to be finite, allow directly.
1586 const uint32_t loop_id = loop->GetHeader()->GetBlockId();
1587 if (finite_loop_.find(loop_id) != finite_loop_.end()) {
1588 return true;
1589 }
1590 // Otherwise, allow dynamic bce if the index (which is necessarily an induction at
1591 // this point) is the direct loop index (viz. a[i]), since then the runtime tests
1592 // ensure upper bound cannot cause an infinite loop.
1593 HInstruction* control = loop->GetHeader()->GetLastInstruction();
1594 if (control->IsIf()) {
1595 HInstruction* if_expr = control->AsIf()->InputAt(0);
1596 if (if_expr->IsCondition()) {
1597 HCondition* condition = if_expr->AsCondition();
1598 if (index == condition->InputAt(0) ||
1599 index == condition->InputAt(1)) {
1600 finite_loop_.insert(loop_id);
1601 return true;
1602 }
1603 }
1604 }
1605 return false;
1606 }
1607 return true;
1608 }
1609
Aart Bik55b14df2016-01-12 14:12:47 -08001610 /**
1611 * Returns appropriate preheader for the loop, depending on whether the
1612 * instruction appears in the loop header or proper loop-body.
1613 */
1614 HBasicBlock* GetPreHeader(HLoopInformation* loop, HInstruction* instruction) {
1615 // Use preheader unless there is an earlier generated deoptimization block since
1616 // hoisted expressions may depend on and/or used by the deoptimization tests.
1617 HBasicBlock* header = loop->GetHeader();
1618 const uint32_t loop_id = header->GetBlockId();
1619 auto it = taken_test_loop_.find(loop_id);
1620 if (it != taken_test_loop_.end()) {
1621 HBasicBlock* block = it->second;
1622 // If always taken, keep it that way by returning the original preheader,
1623 // which can be found by following the predecessor of the true-block twice.
1624 if (instruction->GetBlock() == header) {
1625 return block->GetSinglePredecessor()->GetSinglePredecessor();
1626 }
1627 return block;
1628 }
1629 return loop->GetPreHeader();
1630 }
1631
Aart Bik1d239822016-02-09 14:26:34 -08001632 /** Inserts a deoptimization test in a loop preheader. */
1633 void InsertDeoptInLoop(HLoopInformation* loop, HBasicBlock* block, HInstruction* condition) {
Aart Bik4a342772015-11-30 10:17:46 -08001634 HInstruction* suspend = loop->GetSuspendCheck();
1635 block->InsertInstructionBefore(condition, block->GetLastInstruction());
1636 HDeoptimize* deoptimize =
1637 new (GetGraph()->GetArena()) HDeoptimize(condition, suspend->GetDexPc());
1638 block->InsertInstructionBefore(deoptimize, block->GetLastInstruction());
1639 if (suspend->HasEnvironment()) {
1640 deoptimize->CopyEnvironmentFromWithLoopPhiAdjustment(
1641 suspend->GetEnvironment(), loop->GetHeader());
1642 }
1643 }
1644
Aart Bik1d239822016-02-09 14:26:34 -08001645 /** Inserts a deoptimization test right before a bounds check. */
1646 void InsertDeoptInBlock(HBoundsCheck* bounds_check, HInstruction* condition) {
1647 HBasicBlock* block = bounds_check->GetBlock();
1648 block->InsertInstructionBefore(condition, bounds_check);
1649 HDeoptimize* deoptimize =
1650 new (GetGraph()->GetArena()) HDeoptimize(condition, bounds_check->GetDexPc());
1651 block->InsertInstructionBefore(deoptimize, bounds_check);
1652 deoptimize->CopyEnvironmentFrom(bounds_check->GetEnvironment());
1653 }
1654
Aart Bik4a342772015-11-30 10:17:46 -08001655 /** Hoists instruction out of the loop to preheader or deoptimization block. */
Aart Bik55b14df2016-01-12 14:12:47 -08001656 void HoistToPreHeaderOrDeoptBlock(HLoopInformation* loop, HInstruction* instruction) {
1657 HBasicBlock* block = GetPreHeader(loop, instruction);
Aart Bik4a342772015-11-30 10:17:46 -08001658 DCHECK(!instruction->HasEnvironment());
1659 instruction->MoveBefore(block->GetLastInstruction());
1660 }
1661
1662 /**
Aart Bik55b14df2016-01-12 14:12:47 -08001663 * Adds a new taken-test structure to a loop if needed and not already done.
Aart Bik4a342772015-11-30 10:17:46 -08001664 * The taken-test protects range analysis evaluation code to avoid any
1665 * deoptimization caused by incorrect trip-count evaluation in non-taken loops.
1666 *
Aart Bik4a342772015-11-30 10:17:46 -08001667 * old_preheader
1668 * |
1669 * if_block <- taken-test protects deoptimization block
1670 * / \
1671 * true_block false_block <- deoptimizations/invariants are placed in true_block
1672 * \ /
1673 * new_preheader <- may require phi nodes to preserve SSA structure
1674 * |
1675 * header
1676 *
1677 * For example, this loop:
1678 *
1679 * for (int i = lower; i < upper; i++) {
1680 * array[i] = 0;
1681 * }
1682 *
1683 * will be transformed to:
1684 *
1685 * if (lower < upper) {
1686 * if (array == null) deoptimize;
1687 * array_length = array.length;
1688 * if (lower > upper) deoptimize; // unsigned
1689 * if (upper >= array_length) deoptimize; // unsigned
1690 * } else {
1691 * array_length = 0;
1692 * }
1693 * for (int i = lower; i < upper; i++) {
1694 * // Loop without null check and bounds check, and any array.length replaced with array_length.
1695 * array[i] = 0;
1696 * }
1697 */
Aart Bik55b14df2016-01-12 14:12:47 -08001698 void TransformLoopForDeoptimizationIfNeeded(HLoopInformation* loop, bool needs_taken_test) {
1699 // Not needed (can use preheader) or already done (can reuse)?
Aart Bik4a342772015-11-30 10:17:46 -08001700 const uint32_t loop_id = loop->GetHeader()->GetBlockId();
Aart Bik55b14df2016-01-12 14:12:47 -08001701 if (!needs_taken_test || taken_test_loop_.find(loop_id) != taken_test_loop_.end()) {
1702 return;
Aart Bik4a342772015-11-30 10:17:46 -08001703 }
1704
1705 // Generate top test structure.
1706 HBasicBlock* header = loop->GetHeader();
1707 GetGraph()->TransformLoopHeaderForBCE(header);
1708 HBasicBlock* new_preheader = loop->GetPreHeader();
1709 HBasicBlock* if_block = new_preheader->GetDominator();
1710 HBasicBlock* true_block = if_block->GetSuccessors()[0]; // True successor.
1711 HBasicBlock* false_block = if_block->GetSuccessors()[1]; // False successor.
1712
1713 // Goto instructions.
1714 true_block->AddInstruction(new (GetGraph()->GetArena()) HGoto());
1715 false_block->AddInstruction(new (GetGraph()->GetArena()) HGoto());
1716 new_preheader->AddInstruction(new (GetGraph()->GetArena()) HGoto());
1717
1718 // Insert the taken-test to see if the loop body is entered. If the
1719 // loop isn't entered at all, it jumps around the deoptimization block.
1720 if_block->AddInstruction(new (GetGraph()->GetArena()) HGoto()); // placeholder
Aart Bik16d3a652016-09-09 10:33:50 -07001721 HInstruction* condition = induction_range_.GenerateTakenTest(
1722 header->GetLastInstruction(), GetGraph(), if_block);
Aart Bik4a342772015-11-30 10:17:46 -08001723 DCHECK(condition != nullptr);
1724 if_block->RemoveInstruction(if_block->GetLastInstruction());
1725 if_block->AddInstruction(new (GetGraph()->GetArena()) HIf(condition));
1726
1727 taken_test_loop_.Put(loop_id, true_block);
Aart Bik4a342772015-11-30 10:17:46 -08001728 }
1729
1730 /**
1731 * Inserts phi nodes that preserve SSA structure in generated top test structures.
1732 * All uses of instructions in the deoptimization block that reach the loop need
1733 * a phi node in the new loop preheader to fix the dominance relation.
1734 *
1735 * Example:
1736 * if_block
1737 * / \
1738 * x_0 = .. false_block
1739 * \ /
1740 * x_1 = phi(x_0, null) <- synthetic phi
1741 * |
Aart Bik55b14df2016-01-12 14:12:47 -08001742 * new_preheader
Aart Bik4a342772015-11-30 10:17:46 -08001743 */
1744 void InsertPhiNodes() {
1745 // Scan all new deoptimization blocks.
1746 for (auto it1 = taken_test_loop_.begin(); it1 != taken_test_loop_.end(); ++it1) {
1747 HBasicBlock* true_block = it1->second;
1748 HBasicBlock* new_preheader = true_block->GetSingleSuccessor();
1749 // Scan all instructions in a new deoptimization block.
1750 for (HInstructionIterator it(true_block->GetInstructions()); !it.Done(); it.Advance()) {
1751 HInstruction* instruction = it.Current();
1752 Primitive::Type type = instruction->GetType();
1753 HPhi* phi = nullptr;
1754 // Scan all uses of an instruction and replace each later use with a phi node.
Vladimir Marko46817b82016-03-29 12:21:58 +01001755 const HUseList<HInstruction*>& uses = instruction->GetUses();
1756 for (auto it2 = uses.begin(), end2 = uses.end(); it2 != end2; /* ++it2 below */) {
1757 HInstruction* user = it2->GetUser();
1758 size_t index = it2->GetIndex();
1759 // Increment `it2` now because `*it2` may disappear thanks to user->ReplaceInput().
1760 ++it2;
Aart Bik4a342772015-11-30 10:17:46 -08001761 if (user->GetBlock() != true_block) {
1762 if (phi == nullptr) {
1763 phi = NewPhi(new_preheader, instruction, type);
1764 }
Vladimir Marko46817b82016-03-29 12:21:58 +01001765 user->ReplaceInput(phi, index); // Removes the use node from the list.
Aart Bik4a342772015-11-30 10:17:46 -08001766 }
1767 }
1768 // Scan all environment uses of an instruction and replace each later use with a phi node.
Vladimir Marko46817b82016-03-29 12:21:58 +01001769 const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses();
1770 for (auto it2 = env_uses.begin(), end2 = env_uses.end(); it2 != end2; /* ++it2 below */) {
1771 HEnvironment* user = it2->GetUser();
1772 size_t index = it2->GetIndex();
1773 // Increment `it2` now because `*it2` may disappear thanks to user->RemoveAsUserOfInput().
1774 ++it2;
Aart Bik4a342772015-11-30 10:17:46 -08001775 if (user->GetHolder()->GetBlock() != true_block) {
1776 if (phi == nullptr) {
1777 phi = NewPhi(new_preheader, instruction, type);
1778 }
Vladimir Marko46817b82016-03-29 12:21:58 +01001779 user->RemoveAsUserOfInput(index);
1780 user->SetRawEnvAt(index, phi);
1781 phi->AddEnvUseAt(user, index);
Aart Bik4a342772015-11-30 10:17:46 -08001782 }
1783 }
1784 }
1785 }
1786 }
1787
1788 /**
1789 * Construct a phi(instruction, 0) in the new preheader to fix the dominance relation.
1790 * These are synthetic phi nodes without a virtual register.
1791 */
1792 HPhi* NewPhi(HBasicBlock* new_preheader,
1793 HInstruction* instruction,
1794 Primitive::Type type) {
1795 HGraph* graph = GetGraph();
1796 HInstruction* zero;
1797 switch (type) {
David Brazdil4833f5a2015-12-16 10:37:39 +00001798 case Primitive::kPrimNot: zero = graph->GetNullConstant(); break;
1799 case Primitive::kPrimFloat: zero = graph->GetFloatConstant(0); break;
1800 case Primitive::kPrimDouble: zero = graph->GetDoubleConstant(0); break;
Aart Bik4a342772015-11-30 10:17:46 -08001801 default: zero = graph->GetConstant(type, 0); break;
1802 }
1803 HPhi* phi = new (graph->GetArena())
1804 HPhi(graph->GetArena(), kNoRegNumber, /*number_of_inputs*/ 2, HPhi::ToPhiType(type));
1805 phi->SetRawInputAt(0, instruction);
1806 phi->SetRawInputAt(1, zero);
David Brazdil4833f5a2015-12-16 10:37:39 +00001807 if (type == Primitive::kPrimNot) {
1808 phi->SetReferenceTypeInfo(instruction->GetReferenceTypeInfo());
1809 }
Aart Bik4a342772015-11-30 10:17:46 -08001810 new_preheader->AddPhi(phi);
1811 return phi;
1812 }
1813
1814 /** Helper method to replace an instruction with another instruction. */
Aart Bik1e677482016-11-01 14:23:58 -07001815 void ReplaceInstruction(HInstruction* instruction, HInstruction* replacement) {
1816 // Safe iteration.
1817 if (instruction == next_) {
1818 next_ = next_->GetNext();
1819 }
1820 // Replace and remove.
Aart Bik4a342772015-11-30 10:17:46 -08001821 instruction->ReplaceWith(replacement);
1822 instruction->GetBlock()->RemoveInstruction(instruction);
1823 }
1824
1825 // A set of maps, one per basic block, from instruction to range.
Vladimir Marko5233f932015-09-29 19:01:15 +01001826 ArenaVector<ArenaSafeMap<int, ValueRange*>> maps_;
Mingyao Yangf384f882014-10-22 16:08:18 -07001827
Aart Bik1d239822016-02-09 14:26:34 -08001828 // Map an HArrayLength instruction's id to the first HBoundsCheck instruction
1829 // in a block that checks an index against that HArrayLength.
1830 ArenaSafeMap<int, HBoundsCheck*> first_index_bounds_check_map_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001831
Aart Bik4a342772015-11-30 10:17:46 -08001832 // Early-exit loop bookkeeping.
1833 ArenaSafeMap<uint32_t, bool> early_exit_loop_;
1834
1835 // Taken-test loop bookkeeping.
1836 ArenaSafeMap<uint32_t, HBasicBlock*> taken_test_loop_;
1837
1838 // Finite loop bookkeeping.
1839 ArenaSet<uint32_t> finite_loop_;
1840
Aart Bik1d239822016-02-09 14:26:34 -08001841 // Flag that denotes whether dominator-based dynamic elimination has occurred.
1842 bool has_dom_based_dynamic_bce_;
Aart Bik4a342772015-11-30 10:17:46 -08001843
Mingyao Yang3584bce2015-05-19 16:01:59 -07001844 // Initial number of blocks.
Vladimir Markofa6b93c2015-09-15 10:15:55 +01001845 uint32_t initial_block_size_;
Mingyao Yang3584bce2015-05-19 16:01:59 -07001846
Aart Bik4a342772015-11-30 10:17:46 -08001847 // Side effects.
1848 const SideEffectsAnalysis& side_effects_;
1849
Aart Bik22af3be2015-09-10 12:50:58 -07001850 // Range analysis based on induction variables.
1851 InductionVarRange induction_range_;
1852
Aart Bik1e677482016-11-01 14:23:58 -07001853 // Safe iteration.
1854 HInstruction* next_;
1855
Mingyao Yangf384f882014-10-22 16:08:18 -07001856 DISALLOW_COPY_AND_ASSIGN(BCEVisitor);
1857};
1858
1859void BoundsCheckElimination::Run() {
Mark Mendell1152c922015-04-24 17:06:35 -04001860 if (!graph_->HasBoundsChecks()) {
Mingyao Yange4335eb2015-03-02 15:14:13 -08001861 return;
1862 }
1863
Mingyao Yangf384f882014-10-22 16:08:18 -07001864 // Reverse post order guarantees a node's dominators are visited first.
1865 // We want to visit in the dominator-based order since if a value is known to
1866 // be bounded by a range at one instruction, it must be true that all uses of
1867 // that value dominated by that instruction fits in that range. Range of that
1868 // value can be narrowed further down in the dominator tree.
Aart Bik4a342772015-11-30 10:17:46 -08001869 BCEVisitor visitor(graph_, side_effects_, induction_analysis_);
Vladimir Marko2c45bc92016-10-25 16:54:12 +01001870 for (size_t i = 0, size = graph_->GetReversePostOrder().size(); i != size; ++i) {
1871 HBasicBlock* current = graph_->GetReversePostOrder()[i];
Mingyao Yang3584bce2015-05-19 16:01:59 -07001872 if (visitor.IsAddedBlock(current)) {
1873 // Skip added blocks. Their effects are already taken care of.
1874 continue;
1875 }
1876 visitor.VisitBasicBlock(current);
Aart Bikb6347b72016-02-29 13:56:44 -08001877 // Skip forward to the current block in case new basic blocks were inserted
1878 // (which always appear earlier in reverse post order) to avoid visiting the
1879 // same basic block twice.
Vladimir Marko2c45bc92016-10-25 16:54:12 +01001880 size_t new_size = graph_->GetReversePostOrder().size();
1881 DCHECK_GE(new_size, size);
1882 i += new_size - size;
1883 DCHECK_EQ(current, graph_->GetReversePostOrder()[i]);
1884 size = new_size;
Mingyao Yang3584bce2015-05-19 16:01:59 -07001885 }
Aart Bik4a342772015-11-30 10:17:46 -08001886
1887 // Perform cleanup.
1888 visitor.Finish();
Mingyao Yangf384f882014-10-22 16:08:18 -07001889}
1890
1891} // namespace art