Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 17 | #include "base/arena_containers.h" |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 18 | #include "bounds_check_elimination.h" |
| 19 | #include "nodes.h" |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | class MonotonicValueRange; |
| 24 | |
| 25 | /** |
| 26 | * A value bound is represented as a pair of value and constant, |
| 27 | * e.g. array.length - 1. |
| 28 | */ |
| 29 | class ValueBound : public ValueObject { |
| 30 | public: |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 31 | ValueBound(HInstruction* instruction, int32_t constant) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 32 | if (instruction != nullptr && instruction->IsIntConstant()) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 33 | // Normalize ValueBound with constant instruction. |
| 34 | int32_t instr_const = instruction->AsIntConstant()->GetValue(); |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 35 | if (!WouldAddOverflowOrUnderflow(instr_const, constant)) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 36 | instruction_ = nullptr; |
| 37 | constant_ = instr_const + constant; |
| 38 | return; |
| 39 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 40 | } |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 41 | instruction_ = instruction; |
| 42 | constant_ = constant; |
| 43 | } |
| 44 | |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 45 | // Return whether (left + right) overflows or underflows. |
| 46 | static bool WouldAddOverflowOrUnderflow(int32_t left, int32_t right) { |
| 47 | if (right == 0) { |
| 48 | return false; |
| 49 | } |
| 50 | if ((right > 0) && (left <= INT_MAX - right)) { |
| 51 | // No overflow. |
| 52 | return false; |
| 53 | } |
| 54 | if ((right < 0) && (left >= INT_MIN - right)) { |
| 55 | // No underflow. |
| 56 | return false; |
| 57 | } |
| 58 | return true; |
| 59 | } |
| 60 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 61 | static bool IsAddOrSubAConstant(HInstruction* instruction, |
| 62 | HInstruction** left_instruction, |
| 63 | int* right_constant) { |
| 64 | if (instruction->IsAdd() || instruction->IsSub()) { |
| 65 | HBinaryOperation* bin_op = instruction->AsBinaryOperation(); |
| 66 | HInstruction* left = bin_op->GetLeft(); |
| 67 | HInstruction* right = bin_op->GetRight(); |
| 68 | if (right->IsIntConstant()) { |
| 69 | *left_instruction = left; |
| 70 | int32_t c = right->AsIntConstant()->GetValue(); |
| 71 | *right_constant = instruction->IsAdd() ? c : -c; |
| 72 | return true; |
| 73 | } |
| 74 | } |
| 75 | *left_instruction = nullptr; |
| 76 | *right_constant = 0; |
| 77 | return false; |
| 78 | } |
| 79 | |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 80 | // Try to detect useful value bound format from an instruction, e.g. |
| 81 | // a constant or array length related value. |
| 82 | static ValueBound DetectValueBoundFromValue(HInstruction* instruction, bool* found) { |
| 83 | DCHECK(instruction != nullptr); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 84 | if (instruction->IsIntConstant()) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 85 | *found = true; |
| 86 | return ValueBound(nullptr, instruction->AsIntConstant()->GetValue()); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 87 | } |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 88 | |
| 89 | if (instruction->IsArrayLength()) { |
| 90 | *found = true; |
| 91 | return ValueBound(instruction, 0); |
| 92 | } |
| 93 | // Try to detect (array.length + c) format. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 94 | HInstruction *left; |
| 95 | int32_t right; |
| 96 | if (IsAddOrSubAConstant(instruction, &left, &right)) { |
| 97 | if (left->IsArrayLength()) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 98 | *found = true; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 99 | return ValueBound(left, right); |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | // No useful bound detected. |
| 104 | *found = false; |
| 105 | return ValueBound::Max(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | HInstruction* GetInstruction() const { return instruction_; } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 109 | int32_t GetConstant() const { return constant_; } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 110 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 111 | bool IsRelatedToArrayLength() const { |
| 112 | // Some bounds are created with HNewArray* as the instruction instead |
| 113 | // of HArrayLength*. They are treated the same. |
| 114 | return (instruction_ != nullptr) && |
| 115 | (instruction_->IsArrayLength() || instruction_->IsNewArray()); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool IsConstant() const { |
| 119 | return instruction_ == nullptr; |
| 120 | } |
| 121 | |
| 122 | static ValueBound Min() { return ValueBound(nullptr, INT_MIN); } |
| 123 | static ValueBound Max() { return ValueBound(nullptr, INT_MAX); } |
| 124 | |
| 125 | bool Equals(ValueBound bound) const { |
| 126 | return instruction_ == bound.instruction_ && constant_ == bound.constant_; |
| 127 | } |
| 128 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 129 | static HInstruction* FromArrayLengthToNewArrayIfPossible(HInstruction* instruction) { |
| 130 | // Null check on the NewArray should have been eliminated by instruction |
| 131 | // simplifier already. |
| 132 | if (instruction->IsArrayLength() && instruction->InputAt(0)->IsNewArray()) { |
| 133 | return instruction->InputAt(0)->AsNewArray(); |
| 134 | } |
| 135 | return instruction; |
| 136 | } |
| 137 | |
| 138 | static bool Equal(HInstruction* instruction1, HInstruction* instruction2) { |
| 139 | if (instruction1 == instruction2) { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | if (instruction1 == nullptr || instruction2 == nullptr) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 144 | return false; |
| 145 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 146 | |
| 147 | // Some bounds are created with HNewArray* as the instruction instead |
| 148 | // of HArrayLength*. They are treated the same. |
| 149 | instruction1 = FromArrayLengthToNewArrayIfPossible(instruction1); |
| 150 | instruction2 = FromArrayLengthToNewArrayIfPossible(instruction2); |
| 151 | return instruction1 == instruction2; |
| 152 | } |
| 153 | |
| 154 | // Returns if it's certain this->bound >= `bound`. |
| 155 | bool GreaterThanOrEqualTo(ValueBound bound) const { |
| 156 | if (Equal(instruction_, bound.instruction_)) { |
| 157 | return constant_ >= bound.constant_; |
| 158 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 159 | // Not comparable. Just return false. |
| 160 | return false; |
| 161 | } |
| 162 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 163 | // Returns if it's certain this->bound <= `bound`. |
| 164 | bool LessThanOrEqualTo(ValueBound bound) const { |
| 165 | if (Equal(instruction_, bound.instruction_)) { |
| 166 | return constant_ <= bound.constant_; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 167 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 168 | // Not comparable. Just return false. |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | // Try to narrow lower bound. Returns the greatest of the two if possible. |
| 173 | // Pick one if they are not comparable. |
| 174 | static ValueBound NarrowLowerBound(ValueBound bound1, ValueBound bound2) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 175 | if (bound1.GreaterThanOrEqualTo(bound2)) { |
| 176 | return bound1; |
| 177 | } |
| 178 | if (bound2.GreaterThanOrEqualTo(bound1)) { |
| 179 | return bound2; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | // Not comparable. Just pick one. We may lose some info, but that's ok. |
| 183 | // Favor constant as lower bound. |
| 184 | return bound1.IsConstant() ? bound1 : bound2; |
| 185 | } |
| 186 | |
| 187 | // Try to narrow upper bound. Returns the lowest of the two if possible. |
| 188 | // Pick one if they are not comparable. |
| 189 | static ValueBound NarrowUpperBound(ValueBound bound1, ValueBound bound2) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 190 | if (bound1.LessThanOrEqualTo(bound2)) { |
| 191 | return bound1; |
| 192 | } |
| 193 | if (bound2.LessThanOrEqualTo(bound1)) { |
| 194 | return bound2; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Not comparable. Just pick one. We may lose some info, but that's ok. |
| 198 | // Favor array length as upper bound. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 199 | return bound1.IsRelatedToArrayLength() ? bound1 : bound2; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 202 | // Add a constant to a ValueBound. |
| 203 | // `overflow` or `underflow` will return whether the resulting bound may |
| 204 | // overflow or underflow an int. |
| 205 | ValueBound Add(int32_t c, bool* overflow, bool* underflow) const { |
| 206 | *overflow = *underflow = false; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 207 | if (c == 0) { |
| 208 | return *this; |
| 209 | } |
| 210 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 211 | int32_t new_constant; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 212 | if (c > 0) { |
| 213 | if (constant_ > INT_MAX - c) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 214 | *overflow = true; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 215 | return Max(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 216 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 217 | |
| 218 | new_constant = constant_ + c; |
| 219 | // (array.length + non-positive-constant) won't overflow an int. |
| 220 | if (IsConstant() || (IsRelatedToArrayLength() && new_constant <= 0)) { |
| 221 | return ValueBound(instruction_, new_constant); |
| 222 | } |
| 223 | // Be conservative. |
| 224 | *overflow = true; |
| 225 | return Max(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 226 | } else { |
| 227 | if (constant_ < INT_MIN - c) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 228 | *underflow = true; |
| 229 | return Min(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 230 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 231 | |
| 232 | new_constant = constant_ + c; |
| 233 | // Regardless of the value new_constant, (array.length+new_constant) will |
| 234 | // never underflow since array.length is no less than 0. |
| 235 | if (IsConstant() || IsRelatedToArrayLength()) { |
| 236 | return ValueBound(instruction_, new_constant); |
| 237 | } |
| 238 | // Be conservative. |
| 239 | *underflow = true; |
| 240 | return Min(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 241 | } |
| 242 | return ValueBound(instruction_, new_constant); |
| 243 | } |
| 244 | |
| 245 | private: |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 246 | HInstruction* instruction_; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 247 | int32_t constant_; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | /** |
| 251 | * Represent a range of lower bound and upper bound, both being inclusive. |
| 252 | * Currently a ValueRange may be generated as a result of the following: |
| 253 | * comparisons related to array bounds, array bounds check, add/sub on top |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 254 | * of an existing value range, NewArray or a loop phi corresponding to an |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 255 | * incrementing/decrementing array index (MonotonicValueRange). |
| 256 | */ |
| 257 | class ValueRange : public ArenaObject<kArenaAllocMisc> { |
| 258 | public: |
| 259 | ValueRange(ArenaAllocator* allocator, ValueBound lower, ValueBound upper) |
| 260 | : allocator_(allocator), lower_(lower), upper_(upper) {} |
| 261 | |
| 262 | virtual ~ValueRange() {} |
| 263 | |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 264 | virtual MonotonicValueRange* AsMonotonicValueRange() { return nullptr; } |
| 265 | bool IsMonotonicValueRange() { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 266 | return AsMonotonicValueRange() != nullptr; |
| 267 | } |
| 268 | |
| 269 | ArenaAllocator* GetAllocator() const { return allocator_; } |
| 270 | ValueBound GetLower() const { return lower_; } |
| 271 | ValueBound GetUpper() const { return upper_; } |
| 272 | |
| 273 | // If it's certain that this value range fits in other_range. |
| 274 | virtual bool FitsIn(ValueRange* other_range) const { |
| 275 | if (other_range == nullptr) { |
| 276 | return true; |
| 277 | } |
| 278 | DCHECK(!other_range->IsMonotonicValueRange()); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 279 | return lower_.GreaterThanOrEqualTo(other_range->lower_) && |
| 280 | upper_.LessThanOrEqualTo(other_range->upper_); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Returns the intersection of this and range. |
| 284 | // If it's not possible to do intersection because some |
| 285 | // bounds are not comparable, it's ok to pick either bound. |
| 286 | virtual ValueRange* Narrow(ValueRange* range) { |
| 287 | if (range == nullptr) { |
| 288 | return this; |
| 289 | } |
| 290 | |
| 291 | if (range->IsMonotonicValueRange()) { |
| 292 | return this; |
| 293 | } |
| 294 | |
| 295 | return new (allocator_) ValueRange( |
| 296 | allocator_, |
| 297 | ValueBound::NarrowLowerBound(lower_, range->lower_), |
| 298 | ValueBound::NarrowUpperBound(upper_, range->upper_)); |
| 299 | } |
| 300 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 301 | // Shift a range by a constant. |
| 302 | ValueRange* Add(int32_t constant) const { |
| 303 | bool overflow, underflow; |
| 304 | ValueBound lower = lower_.Add(constant, &overflow, &underflow); |
| 305 | if (underflow) { |
| 306 | // Lower bound underflow will wrap around to positive values |
| 307 | // and invalidate the upper bound. |
| 308 | return nullptr; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 309 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 310 | ValueBound upper = upper_.Add(constant, &overflow, &underflow); |
| 311 | if (overflow) { |
| 312 | // Upper bound overflow will wrap around to negative values |
| 313 | // and invalidate the lower bound. |
| 314 | return nullptr; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 315 | } |
| 316 | return new (allocator_) ValueRange(allocator_, lower, upper); |
| 317 | } |
| 318 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 319 | private: |
| 320 | ArenaAllocator* const allocator_; |
| 321 | const ValueBound lower_; // inclusive |
| 322 | const ValueBound upper_; // inclusive |
| 323 | |
| 324 | DISALLOW_COPY_AND_ASSIGN(ValueRange); |
| 325 | }; |
| 326 | |
| 327 | /** |
| 328 | * A monotonically incrementing/decrementing value range, e.g. |
| 329 | * the variable i in "for (int i=0; i<array.length; i++)". |
| 330 | * Special care needs to be taken to account for overflow/underflow |
| 331 | * of such value ranges. |
| 332 | */ |
| 333 | class MonotonicValueRange : public ValueRange { |
| 334 | public: |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 335 | MonotonicValueRange(ArenaAllocator* allocator, |
| 336 | HInstruction* initial, |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 337 | int32_t increment, |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 338 | ValueBound bound) |
| 339 | // To be conservative, give it full range [INT_MIN, INT_MAX] in case it's |
| 340 | // used as a regular value range, due to possible overflow/underflow. |
| 341 | : ValueRange(allocator, ValueBound::Min(), ValueBound::Max()), |
| 342 | initial_(initial), |
| 343 | increment_(increment), |
| 344 | bound_(bound) {} |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 345 | |
| 346 | virtual ~MonotonicValueRange() {} |
| 347 | |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 348 | int32_t GetIncrement() const { return increment_; } |
| 349 | |
| 350 | ValueBound GetBound() const { return bound_; } |
| 351 | |
| 352 | MonotonicValueRange* AsMonotonicValueRange() OVERRIDE { return this; } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 353 | |
| 354 | // If it's certain that this value range fits in other_range. |
| 355 | bool FitsIn(ValueRange* other_range) const OVERRIDE { |
| 356 | if (other_range == nullptr) { |
| 357 | return true; |
| 358 | } |
| 359 | DCHECK(!other_range->IsMonotonicValueRange()); |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | // Try to narrow this MonotonicValueRange given another range. |
| 364 | // Ideally it will return a normal ValueRange. But due to |
| 365 | // possible overflow/underflow, that may not be possible. |
| 366 | ValueRange* Narrow(ValueRange* range) OVERRIDE { |
| 367 | if (range == nullptr) { |
| 368 | return this; |
| 369 | } |
| 370 | DCHECK(!range->IsMonotonicValueRange()); |
| 371 | |
| 372 | if (increment_ > 0) { |
| 373 | // Monotonically increasing. |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 374 | ValueBound lower = ValueBound::NarrowLowerBound(bound_, range->GetLower()); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 375 | |
| 376 | // We currently conservatively assume max array length is INT_MAX. If we can |
| 377 | // make assumptions about the max array length, e.g. due to the max heap size, |
| 378 | // divided by the element size (such as 4 bytes for each integer array), we can |
| 379 | // lower this number and rule out some possible overflows. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 380 | int32_t max_array_len = INT_MAX; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 381 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 382 | // max possible integer value of range's upper value. |
| 383 | int32_t upper = INT_MAX; |
| 384 | // Try to lower upper. |
| 385 | ValueBound upper_bound = range->GetUpper(); |
| 386 | if (upper_bound.IsConstant()) { |
| 387 | upper = upper_bound.GetConstant(); |
| 388 | } else if (upper_bound.IsRelatedToArrayLength() && upper_bound.GetConstant() <= 0) { |
| 389 | // Normal case. e.g. <= array.length - 1. |
| 390 | upper = max_array_len + upper_bound.GetConstant(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | // If we can prove for the last number in sequence of initial_, |
| 394 | // initial_ + increment_, initial_ + 2 x increment_, ... |
| 395 | // that's <= upper, (last_num_in_sequence + increment_) doesn't trigger overflow, |
| 396 | // then this MonoticValueRange is narrowed to a normal value range. |
| 397 | |
| 398 | // Be conservative first, assume last number in the sequence hits upper. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 399 | int32_t last_num_in_sequence = upper; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 400 | if (initial_->IsIntConstant()) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 401 | int32_t initial_constant = initial_->AsIntConstant()->GetValue(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 402 | if (upper <= initial_constant) { |
| 403 | last_num_in_sequence = upper; |
| 404 | } else { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 405 | // Cast to int64_t for the substraction part to avoid int32_t overflow. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 406 | last_num_in_sequence = initial_constant + |
| 407 | ((int64_t)upper - (int64_t)initial_constant) / increment_ * increment_; |
| 408 | } |
| 409 | } |
| 410 | if (last_num_in_sequence <= INT_MAX - increment_) { |
| 411 | // No overflow. The sequence will be stopped by the upper bound test as expected. |
| 412 | return new (GetAllocator()) ValueRange(GetAllocator(), lower, range->GetUpper()); |
| 413 | } |
| 414 | |
| 415 | // There might be overflow. Give up narrowing. |
| 416 | return this; |
| 417 | } else { |
| 418 | DCHECK_NE(increment_, 0); |
| 419 | // Monotonically decreasing. |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 420 | ValueBound upper = ValueBound::NarrowUpperBound(bound_, range->GetUpper()); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 421 | |
| 422 | // Need to take care of underflow. Try to prove underflow won't happen |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 423 | // for common cases. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 424 | if (range->GetLower().IsConstant()) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 425 | int32_t constant = range->GetLower().GetConstant(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 426 | if (constant >= INT_MIN - increment_) { |
| 427 | return new (GetAllocator()) ValueRange(GetAllocator(), range->GetLower(), upper); |
| 428 | } |
| 429 | } |
| 430 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 431 | // For non-constant lower bound, just assume might be underflow. Give up narrowing. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 432 | return this; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | private: |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 437 | HInstruction* const initial_; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 438 | const int32_t increment_; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 439 | ValueBound bound_; // Additional value bound info for initial_; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 440 | |
| 441 | DISALLOW_COPY_AND_ASSIGN(MonotonicValueRange); |
| 442 | }; |
| 443 | |
| 444 | class BCEVisitor : public HGraphVisitor { |
| 445 | public: |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame^] | 446 | // The least number of bounds checks that should be eliminated by triggering |
| 447 | // the deoptimization technique. |
| 448 | static constexpr size_t kThresholdForAddingDeoptimize = 2; |
| 449 | |
| 450 | // Very large constant index is considered as an anomaly. This is a threshold |
| 451 | // beyond which we don't bother to apply the deoptimization technique since |
| 452 | // it's likely some AIOOBE will be thrown. |
| 453 | static constexpr int32_t kMaxConstantForAddingDeoptimize = INT_MAX - 1024 * 1024; |
| 454 | |
Andreas Gampe | 0418b5b | 2014-12-04 17:24:50 -0800 | [diff] [blame] | 455 | explicit BCEVisitor(HGraph* graph) |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 456 | : HGraphVisitor(graph), |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame^] | 457 | maps_(graph->GetBlocks().Size()), |
| 458 | need_to_revisit_block_(false) {} |
| 459 | |
| 460 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
| 461 | first_constant_index_bounds_check_map_.clear(); |
| 462 | HGraphVisitor::VisitBasicBlock(block); |
| 463 | if (need_to_revisit_block_) { |
| 464 | AddComparesWithDeoptimization(block); |
| 465 | need_to_revisit_block_ = false; |
| 466 | first_constant_index_bounds_check_map_.clear(); |
| 467 | GetValueRangeMap(block)->clear(); |
| 468 | HGraphVisitor::VisitBasicBlock(block); |
| 469 | } |
| 470 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 471 | |
| 472 | private: |
| 473 | // Return the map of proven value ranges at the beginning of a basic block. |
| 474 | ArenaSafeMap<int, ValueRange*>* GetValueRangeMap(HBasicBlock* basic_block) { |
| 475 | int block_id = basic_block->GetBlockId(); |
| 476 | if (maps_.at(block_id) == nullptr) { |
| 477 | std::unique_ptr<ArenaSafeMap<int, ValueRange*>> map( |
| 478 | new ArenaSafeMap<int, ValueRange*>( |
| 479 | std::less<int>(), GetGraph()->GetArena()->Adapter())); |
| 480 | maps_.at(block_id) = std::move(map); |
| 481 | } |
| 482 | return maps_.at(block_id).get(); |
| 483 | } |
| 484 | |
| 485 | // Traverse up the dominator tree to look for value range info. |
| 486 | ValueRange* LookupValueRange(HInstruction* instruction, HBasicBlock* basic_block) { |
| 487 | while (basic_block != nullptr) { |
| 488 | ArenaSafeMap<int, ValueRange*>* map = GetValueRangeMap(basic_block); |
| 489 | if (map->find(instruction->GetId()) != map->end()) { |
| 490 | return map->Get(instruction->GetId()); |
| 491 | } |
| 492 | basic_block = basic_block->GetDominator(); |
| 493 | } |
| 494 | // Didn't find any. |
| 495 | return nullptr; |
| 496 | } |
| 497 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 498 | // Narrow the value range of `instruction` at the end of `basic_block` with `range`, |
| 499 | // and push the narrowed value range to `successor`. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 500 | void ApplyRangeFromComparison(HInstruction* instruction, HBasicBlock* basic_block, |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 501 | HBasicBlock* successor, ValueRange* range) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 502 | ValueRange* existing_range = LookupValueRange(instruction, basic_block); |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 503 | if (existing_range == nullptr) { |
| 504 | if (range != nullptr) { |
| 505 | GetValueRangeMap(successor)->Overwrite(instruction->GetId(), range); |
| 506 | } |
| 507 | return; |
| 508 | } |
| 509 | if (existing_range->IsMonotonicValueRange()) { |
| 510 | DCHECK(instruction->IsLoopHeaderPhi()); |
| 511 | // Make sure the comparison is in the loop header so each increment is |
| 512 | // checked with a comparison. |
| 513 | if (instruction->GetBlock() != basic_block) { |
| 514 | return; |
| 515 | } |
| 516 | } |
| 517 | ValueRange* narrowed_range = existing_range->Narrow(range); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 518 | if (narrowed_range != nullptr) { |
| 519 | GetValueRangeMap(successor)->Overwrite(instruction->GetId(), narrowed_range); |
| 520 | } |
| 521 | } |
| 522 | |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 523 | // Special case that we may simultaneously narrow two MonotonicValueRange's to |
| 524 | // regular value ranges. |
| 525 | void HandleIfBetweenTwoMonotonicValueRanges(HIf* instruction, |
| 526 | HInstruction* left, |
| 527 | HInstruction* right, |
| 528 | IfCondition cond, |
| 529 | MonotonicValueRange* left_range, |
| 530 | MonotonicValueRange* right_range) { |
| 531 | DCHECK(left->IsLoopHeaderPhi()); |
| 532 | DCHECK(right->IsLoopHeaderPhi()); |
| 533 | if (instruction->GetBlock() != left->GetBlock()) { |
| 534 | // Comparison needs to be in loop header to make sure it's done after each |
| 535 | // increment/decrement. |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | // Handle common cases which also don't have overflow/underflow concerns. |
| 540 | if (left_range->GetIncrement() == 1 && |
| 541 | left_range->GetBound().IsConstant() && |
| 542 | right_range->GetIncrement() == -1 && |
| 543 | right_range->GetBound().IsRelatedToArrayLength() && |
| 544 | right_range->GetBound().GetConstant() < 0) { |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 545 | HBasicBlock* successor = nullptr; |
| 546 | int32_t left_compensation = 0; |
| 547 | int32_t right_compensation = 0; |
| 548 | if (cond == kCondLT) { |
| 549 | left_compensation = -1; |
| 550 | right_compensation = 1; |
| 551 | successor = instruction->IfTrueSuccessor(); |
| 552 | } else if (cond == kCondLE) { |
| 553 | successor = instruction->IfTrueSuccessor(); |
| 554 | } else if (cond == kCondGT) { |
| 555 | successor = instruction->IfFalseSuccessor(); |
| 556 | } else if (cond == kCondGE) { |
| 557 | left_compensation = -1; |
| 558 | right_compensation = 1; |
| 559 | successor = instruction->IfFalseSuccessor(); |
| 560 | } else { |
| 561 | // We don't handle '=='/'!=' test in case left and right can cross and |
| 562 | // miss each other. |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | if (successor != nullptr) { |
| 567 | bool overflow; |
| 568 | bool underflow; |
| 569 | ValueRange* new_left_range = new (GetGraph()->GetArena()) ValueRange( |
| 570 | GetGraph()->GetArena(), |
| 571 | left_range->GetBound(), |
| 572 | right_range->GetBound().Add(left_compensation, &overflow, &underflow)); |
| 573 | if (!overflow && !underflow) { |
| 574 | ApplyRangeFromComparison(left, instruction->GetBlock(), successor, |
| 575 | new_left_range); |
| 576 | } |
| 577 | |
| 578 | ValueRange* new_right_range = new (GetGraph()->GetArena()) ValueRange( |
| 579 | GetGraph()->GetArena(), |
| 580 | left_range->GetBound().Add(right_compensation, &overflow, &underflow), |
| 581 | right_range->GetBound()); |
| 582 | if (!overflow && !underflow) { |
| 583 | ApplyRangeFromComparison(right, instruction->GetBlock(), successor, |
| 584 | new_right_range); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 590 | // Handle "if (left cmp_cond right)". |
| 591 | void HandleIf(HIf* instruction, HInstruction* left, HInstruction* right, IfCondition cond) { |
| 592 | HBasicBlock* block = instruction->GetBlock(); |
| 593 | |
| 594 | HBasicBlock* true_successor = instruction->IfTrueSuccessor(); |
| 595 | // There should be no critical edge at this point. |
| 596 | DCHECK_EQ(true_successor->GetPredecessors().Size(), 1u); |
| 597 | |
| 598 | HBasicBlock* false_successor = instruction->IfFalseSuccessor(); |
| 599 | // There should be no critical edge at this point. |
| 600 | DCHECK_EQ(false_successor->GetPredecessors().Size(), 1u); |
| 601 | |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 602 | bool found; |
| 603 | ValueBound bound = ValueBound::DetectValueBoundFromValue(right, &found); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 604 | // Each comparison can establish a lower bound and an upper bound |
| 605 | // for the left hand side. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 606 | ValueBound lower = bound; |
| 607 | ValueBound upper = bound; |
| 608 | if (!found) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 609 | // No constant or array.length+c format bound found. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 610 | // For i<j, we can still use j's upper bound as i's upper bound. Same for lower. |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 611 | ValueRange* right_range = LookupValueRange(right, block); |
| 612 | if (right_range != nullptr) { |
| 613 | if (right_range->IsMonotonicValueRange()) { |
| 614 | ValueRange* left_range = LookupValueRange(left, block); |
| 615 | if (left_range != nullptr && left_range->IsMonotonicValueRange()) { |
| 616 | HandleIfBetweenTwoMonotonicValueRanges(instruction, left, right, cond, |
| 617 | left_range->AsMonotonicValueRange(), |
| 618 | right_range->AsMonotonicValueRange()); |
| 619 | return; |
| 620 | } |
| 621 | } |
| 622 | lower = right_range->GetLower(); |
| 623 | upper = right_range->GetUpper(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 624 | } else { |
| 625 | lower = ValueBound::Min(); |
| 626 | upper = ValueBound::Max(); |
| 627 | } |
| 628 | } |
| 629 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 630 | bool overflow, underflow; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 631 | if (cond == kCondLT || cond == kCondLE) { |
| 632 | if (!upper.Equals(ValueBound::Max())) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 633 | int32_t compensation = (cond == kCondLT) ? -1 : 0; // upper bound is inclusive |
| 634 | ValueBound new_upper = upper.Add(compensation, &overflow, &underflow); |
| 635 | if (overflow || underflow) { |
| 636 | return; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 637 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 638 | ValueRange* new_range = new (GetGraph()->GetArena()) |
| 639 | ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper); |
| 640 | ApplyRangeFromComparison(left, block, true_successor, new_range); |
| 641 | } |
| 642 | |
| 643 | // array.length as a lower bound isn't considered useful. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 644 | if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) { |
| 645 | int32_t compensation = (cond == kCondLE) ? 1 : 0; // lower bound is inclusive |
| 646 | ValueBound new_lower = lower.Add(compensation, &overflow, &underflow); |
| 647 | if (overflow || underflow) { |
| 648 | return; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 649 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 650 | ValueRange* new_range = new (GetGraph()->GetArena()) |
| 651 | ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max()); |
| 652 | ApplyRangeFromComparison(left, block, false_successor, new_range); |
| 653 | } |
| 654 | } else if (cond == kCondGT || cond == kCondGE) { |
| 655 | // array.length as a lower bound isn't considered useful. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 656 | if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) { |
| 657 | int32_t compensation = (cond == kCondGT) ? 1 : 0; // lower bound is inclusive |
| 658 | ValueBound new_lower = lower.Add(compensation, &overflow, &underflow); |
| 659 | if (overflow || underflow) { |
| 660 | return; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 661 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 662 | ValueRange* new_range = new (GetGraph()->GetArena()) |
| 663 | ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max()); |
| 664 | ApplyRangeFromComparison(left, block, true_successor, new_range); |
| 665 | } |
| 666 | |
| 667 | if (!upper.Equals(ValueBound::Max())) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 668 | int32_t compensation = (cond == kCondGE) ? -1 : 0; // upper bound is inclusive |
| 669 | ValueBound new_upper = upper.Add(compensation, &overflow, &underflow); |
| 670 | if (overflow || underflow) { |
| 671 | return; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 672 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 673 | ValueRange* new_range = new (GetGraph()->GetArena()) |
| 674 | ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper); |
| 675 | ApplyRangeFromComparison(left, block, false_successor, new_range); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | void VisitBoundsCheck(HBoundsCheck* bounds_check) { |
| 681 | HBasicBlock* block = bounds_check->GetBlock(); |
| 682 | HInstruction* index = bounds_check->InputAt(0); |
| 683 | HInstruction* array_length = bounds_check->InputAt(1); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 684 | DCHECK(array_length->IsIntConstant() || array_length->IsArrayLength()); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 685 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 686 | if (!index->IsIntConstant()) { |
| 687 | ValueRange* index_range = LookupValueRange(index, block); |
| 688 | if (index_range != nullptr) { |
| 689 | ValueBound lower = ValueBound(nullptr, 0); // constant 0 |
| 690 | ValueBound upper = ValueBound(array_length, -1); // array_length - 1 |
| 691 | ValueRange* array_range = new (GetGraph()->GetArena()) |
| 692 | ValueRange(GetGraph()->GetArena(), lower, upper); |
| 693 | if (index_range->FitsIn(array_range)) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 694 | ReplaceBoundsCheck(bounds_check, index); |
| 695 | return; |
| 696 | } |
| 697 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 698 | } else { |
| 699 | int32_t constant = index->AsIntConstant()->GetValue(); |
| 700 | if (constant < 0) { |
| 701 | // Will always throw exception. |
| 702 | return; |
| 703 | } |
| 704 | if (array_length->IsIntConstant()) { |
| 705 | if (constant < array_length->AsIntConstant()->GetValue()) { |
| 706 | ReplaceBoundsCheck(bounds_check, index); |
| 707 | } |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | DCHECK(array_length->IsArrayLength()); |
| 712 | ValueRange* existing_range = LookupValueRange(array_length, block); |
| 713 | if (existing_range != nullptr) { |
| 714 | ValueBound lower = existing_range->GetLower(); |
| 715 | DCHECK(lower.IsConstant()); |
| 716 | if (constant < lower.GetConstant()) { |
| 717 | ReplaceBoundsCheck(bounds_check, index); |
| 718 | return; |
| 719 | } else { |
| 720 | // Existing range isn't strong enough to eliminate the bounds check. |
| 721 | // Fall through to update the array_length range with info from this |
| 722 | // bounds check. |
| 723 | } |
| 724 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 725 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame^] | 726 | if (first_constant_index_bounds_check_map_.find(array_length->GetId()) == |
| 727 | first_constant_index_bounds_check_map_.end()) { |
| 728 | // Remember the first bounds check against array_length of a constant index. |
| 729 | // That bounds check instruction has an associated HEnvironment where we |
| 730 | // may add an HDeoptimize to eliminate bounds checks of constant indices |
| 731 | // against array_length. |
| 732 | first_constant_index_bounds_check_map_.Put(array_length->GetId(), bounds_check); |
| 733 | } else { |
| 734 | // We've seen it at least twice. It's beneficial to introduce a compare with |
| 735 | // deoptimization fallback to eliminate the bounds checks. |
| 736 | need_to_revisit_block_ = true; |
| 737 | } |
| 738 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 739 | // Once we have an array access like 'array[5] = 1', we record array.length >= 6. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 740 | // We currently don't do it for non-constant index since a valid array[i] can't prove |
| 741 | // a valid array[i-1] yet due to the lower bound side. |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame^] | 742 | if (constant == INT_MAX) { |
| 743 | // INT_MAX as an index will definitely throw AIOOBE. |
| 744 | return; |
| 745 | } |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 746 | ValueBound lower = ValueBound(nullptr, constant + 1); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 747 | ValueBound upper = ValueBound::Max(); |
| 748 | ValueRange* range = new (GetGraph()->GetArena()) |
| 749 | ValueRange(GetGraph()->GetArena(), lower, upper); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 750 | GetValueRangeMap(block)->Overwrite(array_length->GetId(), range); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
| 754 | void ReplaceBoundsCheck(HInstruction* bounds_check, HInstruction* index) { |
| 755 | bounds_check->ReplaceWith(index); |
| 756 | bounds_check->GetBlock()->RemoveInstruction(bounds_check); |
| 757 | } |
| 758 | |
| 759 | void VisitPhi(HPhi* phi) { |
| 760 | if (phi->IsLoopHeaderPhi() && phi->GetType() == Primitive::kPrimInt) { |
Andreas Gampe | 0418b5b | 2014-12-04 17:24:50 -0800 | [diff] [blame] | 761 | DCHECK_EQ(phi->InputCount(), 2U); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 762 | HInstruction* instruction = phi->InputAt(1); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 763 | HInstruction *left; |
| 764 | int32_t increment; |
| 765 | if (ValueBound::IsAddOrSubAConstant(instruction, &left, &increment)) { |
| 766 | if (left == phi) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 767 | HInstruction* initial_value = phi->InputAt(0); |
| 768 | ValueRange* range = nullptr; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 769 | if (increment == 0) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 770 | // Add constant 0. It's really a fixed value. |
| 771 | range = new (GetGraph()->GetArena()) ValueRange( |
| 772 | GetGraph()->GetArena(), |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 773 | ValueBound(initial_value, 0), |
| 774 | ValueBound(initial_value, 0)); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 775 | } else { |
| 776 | // Monotonically increasing/decreasing. |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 777 | bool found; |
| 778 | ValueBound bound = ValueBound::DetectValueBoundFromValue( |
| 779 | initial_value, &found); |
| 780 | if (!found) { |
| 781 | // No constant or array.length+c bound found. |
| 782 | // For i=j, we can still use j's upper bound as i's upper bound. |
| 783 | // Same for lower. |
| 784 | ValueRange* initial_range = LookupValueRange(initial_value, phi->GetBlock()); |
| 785 | if (initial_range != nullptr) { |
| 786 | bound = increment > 0 ? initial_range->GetLower() : |
| 787 | initial_range->GetUpper(); |
| 788 | } else { |
| 789 | bound = increment > 0 ? ValueBound::Min() : ValueBound::Max(); |
| 790 | } |
| 791 | } |
| 792 | range = new (GetGraph()->GetArena()) MonotonicValueRange( |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 793 | GetGraph()->GetArena(), |
| 794 | initial_value, |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 795 | increment, |
| 796 | bound); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 797 | } |
| 798 | GetValueRangeMap(phi->GetBlock())->Overwrite(phi->GetId(), range); |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | void VisitIf(HIf* instruction) { |
| 805 | if (instruction->InputAt(0)->IsCondition()) { |
| 806 | HCondition* cond = instruction->InputAt(0)->AsCondition(); |
| 807 | IfCondition cmp = cond->GetCondition(); |
| 808 | if (cmp == kCondGT || cmp == kCondGE || |
| 809 | cmp == kCondLT || cmp == kCondLE) { |
| 810 | HInstruction* left = cond->GetLeft(); |
| 811 | HInstruction* right = cond->GetRight(); |
| 812 | HandleIf(instruction, left, right, cmp); |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | void VisitAdd(HAdd* add) { |
| 818 | HInstruction* right = add->GetRight(); |
| 819 | if (right->IsIntConstant()) { |
| 820 | ValueRange* left_range = LookupValueRange(add->GetLeft(), add->GetBlock()); |
| 821 | if (left_range == nullptr) { |
| 822 | return; |
| 823 | } |
| 824 | ValueRange* range = left_range->Add(right->AsIntConstant()->GetValue()); |
| 825 | if (range != nullptr) { |
| 826 | GetValueRangeMap(add->GetBlock())->Overwrite(add->GetId(), range); |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
| 831 | void VisitSub(HSub* sub) { |
| 832 | HInstruction* left = sub->GetLeft(); |
| 833 | HInstruction* right = sub->GetRight(); |
| 834 | if (right->IsIntConstant()) { |
| 835 | ValueRange* left_range = LookupValueRange(left, sub->GetBlock()); |
| 836 | if (left_range == nullptr) { |
| 837 | return; |
| 838 | } |
| 839 | ValueRange* range = left_range->Add(-right->AsIntConstant()->GetValue()); |
| 840 | if (range != nullptr) { |
| 841 | GetValueRangeMap(sub->GetBlock())->Overwrite(sub->GetId(), range); |
| 842 | return; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | // Here we are interested in the typical triangular case of nested loops, |
| 847 | // such as the inner loop 'for (int j=0; j<array.length-i; j++)' where i |
| 848 | // is the index for outer loop. In this case, we know j is bounded by array.length-1. |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 849 | |
| 850 | // Try to handle (array.length - i) or (array.length + c - i) format. |
| 851 | HInstruction* left_of_left; // left input of left. |
| 852 | int32_t right_const = 0; |
| 853 | if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &right_const)) { |
| 854 | left = left_of_left; |
| 855 | } |
| 856 | // The value of left input of the sub equals (left + right_const). |
| 857 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 858 | if (left->IsArrayLength()) { |
| 859 | HInstruction* array_length = left->AsArrayLength(); |
| 860 | ValueRange* right_range = LookupValueRange(right, sub->GetBlock()); |
| 861 | if (right_range != nullptr) { |
| 862 | ValueBound lower = right_range->GetLower(); |
| 863 | ValueBound upper = right_range->GetUpper(); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 864 | if (lower.IsConstant() && upper.IsRelatedToArrayLength()) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 865 | HInstruction* upper_inst = upper.GetInstruction(); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 866 | // Make sure it's the same array. |
| 867 | if (ValueBound::Equal(array_length, upper_inst)) { |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 868 | int32_t c0 = right_const; |
| 869 | int32_t c1 = lower.GetConstant(); |
| 870 | int32_t c2 = upper.GetConstant(); |
| 871 | // (array.length + c0 - v) where v is in [c1, array.length + c2] |
| 872 | // gets [c0 - c2, array.length + c0 - c1] as its value range. |
| 873 | if (!ValueBound::WouldAddOverflowOrUnderflow(c0, -c2) && |
| 874 | !ValueBound::WouldAddOverflowOrUnderflow(c0, -c1)) { |
| 875 | if ((c0 - c1) <= 0) { |
| 876 | // array.length + (c0 - c1) won't overflow/underflow. |
| 877 | ValueRange* range = new (GetGraph()->GetArena()) ValueRange( |
| 878 | GetGraph()->GetArena(), |
| 879 | ValueBound(nullptr, right_const - upper.GetConstant()), |
| 880 | ValueBound(array_length, right_const - lower.GetConstant())); |
| 881 | GetValueRangeMap(sub->GetBlock())->Overwrite(sub->GetId(), range); |
| 882 | } |
| 883 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 884 | } |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | } |
| 889 | |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 890 | void FindAndHandlePartialArrayLength(HBinaryOperation* instruction) { |
| 891 | DCHECK(instruction->IsDiv() || instruction->IsShr() || instruction->IsUShr()); |
| 892 | HInstruction* right = instruction->GetRight(); |
| 893 | int32_t right_const; |
| 894 | if (right->IsIntConstant()) { |
| 895 | right_const = right->AsIntConstant()->GetValue(); |
| 896 | // Detect division by two or more. |
| 897 | if ((instruction->IsDiv() && right_const <= 1) || |
| 898 | (instruction->IsShr() && right_const < 1) || |
| 899 | (instruction->IsUShr() && right_const < 1)) { |
| 900 | return; |
| 901 | } |
| 902 | } else { |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | // Try to handle array.length/2 or (array.length-1)/2 format. |
| 907 | HInstruction* left = instruction->GetLeft(); |
| 908 | HInstruction* left_of_left; // left input of left. |
| 909 | int32_t c = 0; |
| 910 | if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &c)) { |
| 911 | left = left_of_left; |
| 912 | } |
| 913 | // The value of left input of instruction equals (left + c). |
| 914 | |
| 915 | // (array_length + 1) or smaller divided by two or more |
| 916 | // always generate a value in [INT_MIN, array_length]. |
| 917 | // This is true even if array_length is INT_MAX. |
| 918 | if (left->IsArrayLength() && c <= 1) { |
| 919 | if (instruction->IsUShr() && c < 0) { |
| 920 | // Make sure for unsigned shift, left side is not negative. |
| 921 | // e.g. if array_length is 2, ((array_length - 3) >>> 2) is way bigger |
| 922 | // than array_length. |
| 923 | return; |
| 924 | } |
| 925 | ValueRange* range = new (GetGraph()->GetArena()) ValueRange( |
| 926 | GetGraph()->GetArena(), |
| 927 | ValueBound(nullptr, INT_MIN), |
| 928 | ValueBound(left, 0)); |
| 929 | GetValueRangeMap(instruction->GetBlock())->Overwrite(instruction->GetId(), range); |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | void VisitDiv(HDiv* div) { |
| 934 | FindAndHandlePartialArrayLength(div); |
| 935 | } |
| 936 | |
| 937 | void VisitShr(HShr* shr) { |
| 938 | FindAndHandlePartialArrayLength(shr); |
| 939 | } |
| 940 | |
| 941 | void VisitUShr(HUShr* ushr) { |
| 942 | FindAndHandlePartialArrayLength(ushr); |
| 943 | } |
| 944 | |
Mingyao Yang | 4559f00 | 2015-02-27 14:43:53 -0800 | [diff] [blame] | 945 | void VisitAnd(HAnd* instruction) { |
| 946 | if (instruction->GetRight()->IsIntConstant()) { |
| 947 | int32_t constant = instruction->GetRight()->AsIntConstant()->GetValue(); |
| 948 | if (constant > 0) { |
| 949 | // constant serves as a mask so any number masked with it |
| 950 | // gets a [0, constant] value range. |
| 951 | ValueRange* range = new (GetGraph()->GetArena()) ValueRange( |
| 952 | GetGraph()->GetArena(), |
| 953 | ValueBound(nullptr, 0), |
| 954 | ValueBound(nullptr, constant)); |
| 955 | GetValueRangeMap(instruction->GetBlock())->Overwrite(instruction->GetId(), range); |
| 956 | } |
| 957 | } |
| 958 | } |
| 959 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 960 | void VisitNewArray(HNewArray* new_array) { |
| 961 | HInstruction* len = new_array->InputAt(0); |
| 962 | if (!len->IsIntConstant()) { |
| 963 | HInstruction *left; |
| 964 | int32_t right_const; |
| 965 | if (ValueBound::IsAddOrSubAConstant(len, &left, &right_const)) { |
| 966 | // (left + right_const) is used as size to new the array. |
| 967 | // We record "-right_const <= left <= new_array - right_const"; |
| 968 | ValueBound lower = ValueBound(nullptr, -right_const); |
| 969 | // We use new_array for the bound instead of new_array.length, |
| 970 | // which isn't available as an instruction yet. new_array will |
| 971 | // be treated the same as new_array.length when it's used in a ValueBound. |
| 972 | ValueBound upper = ValueBound(new_array, -right_const); |
| 973 | ValueRange* range = new (GetGraph()->GetArena()) |
| 974 | ValueRange(GetGraph()->GetArena(), lower, upper); |
| 975 | GetValueRangeMap(new_array->GetBlock())->Overwrite(left->GetId(), range); |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame^] | 980 | void VisitDeoptimize(HDeoptimize* deoptimize) { |
| 981 | // Right now it's only HLessThanOrEqual. |
| 982 | DCHECK(deoptimize->InputAt(0)->IsLessThanOrEqual()); |
| 983 | HLessThanOrEqual* less_than_or_equal = deoptimize->InputAt(0)->AsLessThanOrEqual(); |
| 984 | HInstruction* instruction = less_than_or_equal->InputAt(0); |
| 985 | if (instruction->IsArrayLength()) { |
| 986 | HInstruction* constant = less_than_or_equal->InputAt(1); |
| 987 | DCHECK(constant->IsIntConstant()); |
| 988 | DCHECK(constant->AsIntConstant()->GetValue() <= kMaxConstantForAddingDeoptimize); |
| 989 | ValueBound lower = ValueBound(nullptr, constant->AsIntConstant()->GetValue() + 1); |
| 990 | ValueRange* range = new (GetGraph()->GetArena()) |
| 991 | ValueRange(GetGraph()->GetArena(), lower, ValueBound::Max()); |
| 992 | GetValueRangeMap(deoptimize->GetBlock())->Overwrite(instruction->GetId(), range); |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | void AddCompareWithDeoptimization(HInstruction* array_length, |
| 997 | HIntConstant* const_instr, |
| 998 | HBasicBlock* block) { |
| 999 | DCHECK(array_length->IsArrayLength()); |
| 1000 | ValueRange* range = LookupValueRange(array_length, block); |
| 1001 | ValueBound lower_bound = range->GetLower(); |
| 1002 | DCHECK(lower_bound.IsConstant()); |
| 1003 | DCHECK(const_instr->GetValue() <= kMaxConstantForAddingDeoptimize); |
| 1004 | DCHECK_EQ(lower_bound.GetConstant(), const_instr->GetValue() + 1); |
| 1005 | |
| 1006 | // If array_length is less than lower_const, deoptimize. |
| 1007 | HBoundsCheck* bounds_check = first_constant_index_bounds_check_map_.Get( |
| 1008 | array_length->GetId())->AsBoundsCheck(); |
| 1009 | HCondition* cond = new (GetGraph()->GetArena()) HLessThanOrEqual(array_length, const_instr); |
| 1010 | HDeoptimize* deoptimize = new (GetGraph()->GetArena()) |
| 1011 | HDeoptimize(cond, bounds_check->GetDexPc()); |
| 1012 | block->InsertInstructionBefore(cond, bounds_check); |
| 1013 | block->InsertInstructionBefore(deoptimize, bounds_check); |
| 1014 | deoptimize->SetEnvironment(bounds_check->GetEnvironment()); |
| 1015 | } |
| 1016 | |
| 1017 | void AddComparesWithDeoptimization(HBasicBlock* block) { |
| 1018 | for (ArenaSafeMap<int, HBoundsCheck*>::iterator it = |
| 1019 | first_constant_index_bounds_check_map_.begin(); |
| 1020 | it != first_constant_index_bounds_check_map_.end(); |
| 1021 | ++it) { |
| 1022 | HBoundsCheck* bounds_check = it->second; |
| 1023 | HArrayLength* array_length = bounds_check->InputAt(1)->AsArrayLength(); |
| 1024 | HIntConstant* lower_bound_const_instr = nullptr; |
| 1025 | int32_t lower_bound_const = INT_MIN; |
| 1026 | size_t counter = 0; |
| 1027 | // Count the constant indexing for which bounds checks haven't |
| 1028 | // been removed yet. |
| 1029 | for (HUseIterator<HInstruction*> it2(array_length->GetUses()); |
| 1030 | !it2.Done(); |
| 1031 | it2.Advance()) { |
| 1032 | HInstruction* user = it2.Current()->GetUser(); |
| 1033 | if (user->GetBlock() == block && |
| 1034 | user->IsBoundsCheck() && |
| 1035 | user->AsBoundsCheck()->InputAt(0)->IsIntConstant()) { |
| 1036 | DCHECK_EQ(array_length, user->AsBoundsCheck()->InputAt(1)); |
| 1037 | HIntConstant* const_instr = user->AsBoundsCheck()->InputAt(0)->AsIntConstant(); |
| 1038 | if (const_instr->GetValue() > lower_bound_const) { |
| 1039 | lower_bound_const = const_instr->GetValue(); |
| 1040 | lower_bound_const_instr = const_instr; |
| 1041 | } |
| 1042 | counter++; |
| 1043 | } |
| 1044 | } |
| 1045 | if (counter >= kThresholdForAddingDeoptimize && |
| 1046 | lower_bound_const_instr->GetValue() <= kMaxConstantForAddingDeoptimize) { |
| 1047 | AddCompareWithDeoptimization(array_length, lower_bound_const_instr, block); |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1052 | std::vector<std::unique_ptr<ArenaSafeMap<int, ValueRange*>>> maps_; |
| 1053 | |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame^] | 1054 | // Map an HArrayLength instruction's id to the first HBoundsCheck instruction in |
| 1055 | // a block that checks a constant index against that HArrayLength. |
| 1056 | SafeMap<int, HBoundsCheck*> first_constant_index_bounds_check_map_; |
| 1057 | |
| 1058 | // For the block, there is at least one HArrayLength instruction for which there |
| 1059 | // is more than one bounds check instruction with constant indexing. And it's |
| 1060 | // beneficial to add a compare instruction that has deoptimization fallback and |
| 1061 | // eliminate those bounds checks. |
| 1062 | bool need_to_revisit_block_; |
| 1063 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1064 | DISALLOW_COPY_AND_ASSIGN(BCEVisitor); |
| 1065 | }; |
| 1066 | |
| 1067 | void BoundsCheckElimination::Run() { |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 1068 | if (!graph_->HasArrayAccesses()) { |
| 1069 | return; |
| 1070 | } |
| 1071 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1072 | BCEVisitor visitor(graph_); |
| 1073 | // Reverse post order guarantees a node's dominators are visited first. |
| 1074 | // We want to visit in the dominator-based order since if a value is known to |
| 1075 | // be bounded by a range at one instruction, it must be true that all uses of |
| 1076 | // that value dominated by that instruction fits in that range. Range of that |
| 1077 | // value can be narrowed further down in the dominator tree. |
| 1078 | // |
| 1079 | // TODO: only visit blocks that dominate some array accesses. |
| 1080 | visitor.VisitReversePostOrder(); |
| 1081 | } |
| 1082 | |
| 1083 | } // namespace art |