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 | |
| 17 | #include "bounds_check_elimination.h" |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 18 | |
| 19 | #include <limits> |
| 20 | |
| 21 | #include "base/arena_containers.h" |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 22 | #include "induction_var_range.h" |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 23 | #include "side_effects_analysis.h" |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 24 | #include "nodes.h" |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class MonotonicValueRange; |
| 29 | |
| 30 | /** |
| 31 | * A value bound is represented as a pair of value and constant, |
| 32 | * e.g. array.length - 1. |
| 33 | */ |
| 34 | class ValueBound : public ValueObject { |
| 35 | public: |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 36 | ValueBound(HInstruction* instruction, int32_t constant) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 37 | if (instruction != nullptr && instruction->IsIntConstant()) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 38 | // Normalize ValueBound with constant instruction. |
| 39 | int32_t instr_const = instruction->AsIntConstant()->GetValue(); |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 40 | if (!WouldAddOverflowOrUnderflow(instr_const, constant)) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 41 | instruction_ = nullptr; |
| 42 | constant_ = instr_const + constant; |
| 43 | return; |
| 44 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 45 | } |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 46 | instruction_ = instruction; |
| 47 | constant_ = constant; |
| 48 | } |
| 49 | |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 50 | // 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 Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 55 | if ((right > 0) && (left <= (std::numeric_limits<int32_t>::max() - right))) { |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 56 | // No overflow. |
| 57 | return false; |
| 58 | } |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 59 | if ((right < 0) && (left >= (std::numeric_limits<int32_t>::min() - right))) { |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 60 | // No underflow. |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 66 | // Return true if instruction can be expressed as "left_instruction + right_constant". |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 67 | static bool IsAddOrSubAConstant(HInstruction* instruction, |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 68 | /* out */ HInstruction** left_instruction, |
| 69 | /* out */ int32_t* right_constant) { |
Aart Bik | bf3f1cf | 2016-02-22 16:22:33 -0800 | [diff] [blame] | 70 | HInstruction* left_so_far = nullptr; |
| 71 | int32_t right_so_far = 0; |
| 72 | while (instruction->IsAdd() || instruction->IsSub()) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 73 | HBinaryOperation* bin_op = instruction->AsBinaryOperation(); |
| 74 | HInstruction* left = bin_op->GetLeft(); |
| 75 | HInstruction* right = bin_op->GetRight(); |
| 76 | if (right->IsIntConstant()) { |
Aart Bik | bf3f1cf | 2016-02-22 16:22:33 -0800 | [diff] [blame] | 77 | 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 85 | } |
Aart Bik | bf3f1cf | 2016-02-22 16:22:33 -0800 | [diff] [blame] | 86 | break; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 87 | } |
Aart Bik | bf3f1cf | 2016-02-22 16:22:33 -0800 | [diff] [blame] | 88 | // 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 94 | // 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 Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 107 | // Try to detect useful value bound format from an instruction, e.g. |
| 108 | // a constant or array length related value. |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 109 | static ValueBound DetectValueBoundFromValue(HInstruction* instruction, /* out */ bool* found) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 110 | DCHECK(instruction != nullptr); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 111 | if (instruction->IsIntConstant()) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 112 | *found = true; |
| 113 | return ValueBound(nullptr, instruction->AsIntConstant()->GetValue()); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 114 | } |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 115 | |
| 116 | if (instruction->IsArrayLength()) { |
| 117 | *found = true; |
| 118 | return ValueBound(instruction, 0); |
| 119 | } |
| 120 | // Try to detect (array.length + c) format. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 121 | HInstruction *left; |
| 122 | int32_t right; |
| 123 | if (IsAddOrSubAConstant(instruction, &left, &right)) { |
| 124 | if (left->IsArrayLength()) { |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 125 | *found = true; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 126 | return ValueBound(left, right); |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | // No useful bound detected. |
| 131 | *found = false; |
| 132 | return ValueBound::Max(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | HInstruction* GetInstruction() const { return instruction_; } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 136 | int32_t GetConstant() const { return constant_; } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 137 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 138 | 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | bool IsConstant() const { |
| 146 | return instruction_ == nullptr; |
| 147 | } |
| 148 | |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 149 | 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 151 | |
| 152 | bool Equals(ValueBound bound) const { |
| 153 | return instruction_ == bound.instruction_ && constant_ == bound.constant_; |
| 154 | } |
| 155 | |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 156 | /* |
| 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 167 | } |
| 168 | return instruction; |
| 169 | } |
| 170 | |
| 171 | static bool Equal(HInstruction* instruction1, HInstruction* instruction2) { |
| 172 | if (instruction1 == instruction2) { |
| 173 | return true; |
| 174 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 175 | if (instruction1 == nullptr || instruction2 == nullptr) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 176 | return false; |
| 177 | } |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 178 | instruction1 = HuntForDeclaration(instruction1); |
| 179 | instruction2 = HuntForDeclaration(instruction2); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 180 | 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 188 | // Not comparable. Just return false. |
| 189 | return false; |
| 190 | } |
| 191 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 192 | // 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 196 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 197 | // Not comparable. Just return false. |
| 198 | return false; |
| 199 | } |
| 200 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 201 | // 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 219 | // 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 222 | if (bound1.GreaterThanOrEqualTo(bound2)) { |
| 223 | return bound1; |
| 224 | } |
| 225 | if (bound2.GreaterThanOrEqualTo(bound1)) { |
| 226 | return bound2; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 227 | } |
| 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 237 | if (bound1.LessThanOrEqualTo(bound2)) { |
| 238 | return bound1; |
| 239 | } |
| 240 | if (bound2.LessThanOrEqualTo(bound1)) { |
| 241 | return bound2; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | // Not comparable. Just pick one. We may lose some info, but that's ok. |
| 245 | // Favor array length as upper bound. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 246 | return bound1.IsRelatedToArrayLength() ? bound1 : bound2; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 249 | // Add a constant to a ValueBound. |
| 250 | // `overflow` or `underflow` will return whether the resulting bound may |
| 251 | // overflow or underflow an int. |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 252 | ValueBound Add(int32_t c, /* out */ bool* overflow, /* out */ bool* underflow) const { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 253 | *overflow = *underflow = false; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 254 | if (c == 0) { |
| 255 | return *this; |
| 256 | } |
| 257 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 258 | int32_t new_constant; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 259 | if (c > 0) { |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 260 | if (constant_ > (std::numeric_limits<int32_t>::max() - c)) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 261 | *overflow = true; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 262 | return Max(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 263 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 264 | |
| 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 273 | } else { |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 274 | if (constant_ < (std::numeric_limits<int32_t>::min() - c)) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 275 | *underflow = true; |
| 276 | return Min(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 277 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 278 | |
| 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 288 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | private: |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 292 | HInstruction* instruction_; |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 293 | int32_t constant_; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 294 | }; |
| 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 300 | * 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] | 301 | * incrementing/decrementing array index (MonotonicValueRange). |
| 302 | */ |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 303 | class ValueRange : public ArenaObject<kArenaAllocBoundsCheckElimination> { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 304 | public: |
| 305 | ValueRange(ArenaAllocator* allocator, ValueBound lower, ValueBound upper) |
| 306 | : allocator_(allocator), lower_(lower), upper_(upper) {} |
| 307 | |
| 308 | virtual ~ValueRange() {} |
| 309 | |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 310 | virtual MonotonicValueRange* AsMonotonicValueRange() { return nullptr; } |
| 311 | bool IsMonotonicValueRange() { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 312 | 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 Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 319 | bool IsConstantValueRange() { return lower_.IsConstant() && upper_.IsConstant(); } |
| 320 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 321 | // 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 327 | return lower_.GreaterThanOrEqualTo(other_range->lower_) && |
| 328 | upper_.LessThanOrEqualTo(other_range->upper_); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 329 | } |
| 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 349 | // 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 357 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 358 | 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 363 | } |
| 364 | return new (allocator_) ValueRange(allocator_, lower, upper); |
| 365 | } |
| 366 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 367 | 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 | */ |
| 381 | class MonotonicValueRange : public ValueRange { |
| 382 | public: |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 383 | MonotonicValueRange(ArenaAllocator* allocator, |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 384 | HPhi* induction_variable, |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 385 | HInstruction* initial, |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 386 | int32_t increment, |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 387 | ValueBound bound) |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 388 | // To be conservative, give it full range [Min(), Max()] in case it's |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 389 | // used as a regular value range, due to possible overflow/underflow. |
| 390 | : ValueRange(allocator, ValueBound::Min(), ValueBound::Max()), |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 391 | induction_variable_(induction_variable), |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 392 | initial_(initial), |
| 393 | increment_(increment), |
| 394 | bound_(bound) {} |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 395 | |
| 396 | virtual ~MonotonicValueRange() {} |
| 397 | |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 398 | int32_t GetIncrement() const { return increment_; } |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 399 | ValueBound GetBound() const { return bound_; } |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 400 | HBasicBlock* GetLoopHeader() const { |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 401 | DCHECK(induction_variable_->GetBlock()->IsLoopHeader()); |
| 402 | return induction_variable_->GetBlock(); |
| 403 | } |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 404 | |
| 405 | MonotonicValueRange* AsMonotonicValueRange() OVERRIDE { return this; } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 406 | |
| 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 Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 427 | ValueBound lower = ValueBound::NarrowLowerBound(bound_, range->GetLower()); |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 428 | if (!lower.IsConstant() || lower.GetConstant() == std::numeric_limits<int32_t>::min()) { |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 429 | // Lower bound isn't useful. Leave it to deoptimization. |
| 430 | return this; |
| 431 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 432 | |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 433 | // 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 435 | // 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 Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 437 | int32_t max_array_len = std::numeric_limits<int32_t>::max(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 438 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 439 | // max possible integer value of range's upper value. |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 440 | int32_t upper = std::numeric_limits<int32_t>::max(); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 441 | // 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 Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 448 | } |
| 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 Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 456 | int32_t last_num_in_sequence = upper; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 457 | if (initial_->IsIntConstant()) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 458 | int32_t initial_constant = initial_->AsIntConstant()->GetValue(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 459 | if (upper <= initial_constant) { |
| 460 | last_num_in_sequence = upper; |
| 461 | } else { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 462 | // 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] | 463 | last_num_in_sequence = initial_constant + |
| 464 | ((int64_t)upper - (int64_t)initial_constant) / increment_ * increment_; |
| 465 | } |
| 466 | } |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 467 | if (last_num_in_sequence <= (std::numeric_limits<int32_t>::max() - increment_)) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 468 | // 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 Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 477 | ValueBound upper = ValueBound::NarrowUpperBound(bound_, range->GetUpper()); |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 478 | if ((!upper.IsConstant() || upper.GetConstant() == std::numeric_limits<int32_t>::max()) && |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 479 | !upper.IsRelatedToArrayLength()) { |
| 480 | // Upper bound isn't useful. Leave it to deoptimization. |
| 481 | return this; |
| 482 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 483 | |
| 484 | // 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] | 485 | // for common cases. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 486 | if (range->GetLower().IsConstant()) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 487 | int32_t constant = range->GetLower().GetConstant(); |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 488 | if (constant >= (std::numeric_limits<int32_t>::min() - increment_)) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 489 | return new (GetAllocator()) ValueRange(GetAllocator(), range->GetLower(), upper); |
| 490 | } |
| 491 | } |
| 492 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 493 | // 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] | 494 | return this; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | private: |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 499 | HPhi* const induction_variable_; // Induction variable for this monotonic value range. |
| 500 | HInstruction* const initial_; // Initial value. |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 501 | const int32_t increment_; // Increment for each loop iteration. |
| 502 | const ValueBound bound_; // Additional value bound info for initial_. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 503 | |
| 504 | DISALLOW_COPY_AND_ASSIGN(MonotonicValueRange); |
| 505 | }; |
| 506 | |
| 507 | class BCEVisitor : public HGraphVisitor { |
| 508 | public: |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 509 | // 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 Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 513 | // 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 Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 517 | std::numeric_limits<int32_t>::max() - 1024 * 1024; |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 518 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 519 | // Added blocks for loop body entry test. |
| 520 | bool IsAddedBlock(HBasicBlock* block) const { |
| 521 | return block->GetBlockId() >= initial_block_size_; |
| 522 | } |
| 523 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 524 | BCEVisitor(HGraph* graph, |
| 525 | const SideEffectsAnalysis& side_effects, |
| 526 | HInductionVarAnalysis* induction_analysis) |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 527 | : HGraphVisitor(graph), |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 528 | maps_(graph->GetBlocks().size(), |
| 529 | ArenaSafeMap<int, ValueRange*>( |
| 530 | std::less<int>(), |
| 531 | graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)), |
| 532 | graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)), |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 533 | first_index_bounds_check_map_( |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 534 | std::less<int>(), |
| 535 | graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)), |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 536 | dynamic_bce_standby_( |
| 537 | graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)), |
Vladimir Marko | b75878e | 2016-03-14 13:56:02 +0000 | [diff] [blame] | 538 | record_dynamic_bce_standby_(true), |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 539 | early_exit_loop_( |
| 540 | std::less<uint32_t>(), |
| 541 | graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)), |
| 542 | taken_test_loop_( |
| 543 | std::less<uint32_t>(), |
| 544 | graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)), |
| 545 | finite_loop_(graph->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)), |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 546 | has_dom_based_dynamic_bce_(false), |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 547 | initial_block_size_(graph->GetBlocks().size()), |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 548 | side_effects_(side_effects), |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 549 | induction_range_(induction_analysis) {} |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 550 | |
| 551 | void VisitBasicBlock(HBasicBlock* block) OVERRIDE { |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 552 | DCHECK(!IsAddedBlock(block)); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 553 | first_index_bounds_check_map_.clear(); |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 554 | HGraphVisitor::VisitBasicBlock(block); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 555 | AddComparesWithDeoptimization(block); |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 556 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 557 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 558 | void Finish() { |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 559 | // Retry dynamic bce candidates on standby that are still in the graph. |
Vladimir Marko | b75878e | 2016-03-14 13:56:02 +0000 | [diff] [blame] | 560 | record_dynamic_bce_standby_ = false; |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 561 | for (HBoundsCheck* bounds_check : dynamic_bce_standby_) { |
| 562 | if (bounds_check->IsInBlock()) { |
| 563 | TryDynamicBCE(bounds_check); |
| 564 | } |
| 565 | } |
| 566 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 567 | // Preserve SSA structure which may have been broken by adding one or more |
| 568 | // new taken-test structures (see TransformLoopForDeoptimizationIfNeeded()). |
| 569 | InsertPhiNodes(); |
| 570 | |
| 571 | // Clear the loop data structures. |
| 572 | early_exit_loop_.clear(); |
| 573 | taken_test_loop_.clear(); |
| 574 | finite_loop_.clear(); |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 575 | dynamic_bce_standby_.clear(); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 576 | } |
| 577 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 578 | private: |
| 579 | // Return the map of proven value ranges at the beginning of a basic block. |
| 580 | ArenaSafeMap<int, ValueRange*>* GetValueRangeMap(HBasicBlock* basic_block) { |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 581 | if (IsAddedBlock(basic_block)) { |
| 582 | // Added blocks don't keep value ranges. |
| 583 | return nullptr; |
| 584 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 585 | return &maps_[basic_block->GetBlockId()]; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // Traverse up the dominator tree to look for value range info. |
| 589 | ValueRange* LookupValueRange(HInstruction* instruction, HBasicBlock* basic_block) { |
| 590 | while (basic_block != nullptr) { |
| 591 | ArenaSafeMap<int, ValueRange*>* map = GetValueRangeMap(basic_block); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 592 | if (map != nullptr) { |
| 593 | if (map->find(instruction->GetId()) != map->end()) { |
| 594 | return map->Get(instruction->GetId()); |
| 595 | } |
| 596 | } else { |
| 597 | DCHECK(IsAddedBlock(basic_block)); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 598 | } |
| 599 | basic_block = basic_block->GetDominator(); |
| 600 | } |
| 601 | // Didn't find any. |
| 602 | return nullptr; |
| 603 | } |
| 604 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 605 | // Helper method to assign a new range to an instruction in given basic block. |
| 606 | void AssignRange(HBasicBlock* basic_block, HInstruction* instruction, ValueRange* range) { |
| 607 | GetValueRangeMap(basic_block)->Overwrite(instruction->GetId(), range); |
| 608 | } |
| 609 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 610 | // Narrow the value range of `instruction` at the end of `basic_block` with `range`, |
| 611 | // and push the narrowed value range to `successor`. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 612 | void ApplyRangeFromComparison(HInstruction* instruction, HBasicBlock* basic_block, |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 613 | HBasicBlock* successor, ValueRange* range) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 614 | ValueRange* existing_range = LookupValueRange(instruction, basic_block); |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 615 | if (existing_range == nullptr) { |
| 616 | if (range != nullptr) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 617 | AssignRange(successor, instruction, range); |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 618 | } |
| 619 | return; |
| 620 | } |
| 621 | if (existing_range->IsMonotonicValueRange()) { |
| 622 | DCHECK(instruction->IsLoopHeaderPhi()); |
| 623 | // Make sure the comparison is in the loop header so each increment is |
| 624 | // checked with a comparison. |
| 625 | if (instruction->GetBlock() != basic_block) { |
| 626 | return; |
| 627 | } |
| 628 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 629 | AssignRange(successor, instruction, existing_range->Narrow(range)); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 632 | // Special case that we may simultaneously narrow two MonotonicValueRange's to |
| 633 | // regular value ranges. |
| 634 | void HandleIfBetweenTwoMonotonicValueRanges(HIf* instruction, |
| 635 | HInstruction* left, |
| 636 | HInstruction* right, |
| 637 | IfCondition cond, |
| 638 | MonotonicValueRange* left_range, |
| 639 | MonotonicValueRange* right_range) { |
| 640 | DCHECK(left->IsLoopHeaderPhi()); |
| 641 | DCHECK(right->IsLoopHeaderPhi()); |
| 642 | if (instruction->GetBlock() != left->GetBlock()) { |
| 643 | // Comparison needs to be in loop header to make sure it's done after each |
| 644 | // increment/decrement. |
| 645 | return; |
| 646 | } |
| 647 | |
| 648 | // Handle common cases which also don't have overflow/underflow concerns. |
| 649 | if (left_range->GetIncrement() == 1 && |
| 650 | left_range->GetBound().IsConstant() && |
| 651 | right_range->GetIncrement() == -1 && |
| 652 | right_range->GetBound().IsRelatedToArrayLength() && |
| 653 | right_range->GetBound().GetConstant() < 0) { |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 654 | HBasicBlock* successor = nullptr; |
| 655 | int32_t left_compensation = 0; |
| 656 | int32_t right_compensation = 0; |
| 657 | if (cond == kCondLT) { |
| 658 | left_compensation = -1; |
| 659 | right_compensation = 1; |
| 660 | successor = instruction->IfTrueSuccessor(); |
| 661 | } else if (cond == kCondLE) { |
| 662 | successor = instruction->IfTrueSuccessor(); |
| 663 | } else if (cond == kCondGT) { |
| 664 | successor = instruction->IfFalseSuccessor(); |
| 665 | } else if (cond == kCondGE) { |
| 666 | left_compensation = -1; |
| 667 | right_compensation = 1; |
| 668 | successor = instruction->IfFalseSuccessor(); |
| 669 | } else { |
| 670 | // We don't handle '=='/'!=' test in case left and right can cross and |
| 671 | // miss each other. |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | if (successor != nullptr) { |
| 676 | bool overflow; |
| 677 | bool underflow; |
| 678 | ValueRange* new_left_range = new (GetGraph()->GetArena()) ValueRange( |
| 679 | GetGraph()->GetArena(), |
| 680 | left_range->GetBound(), |
| 681 | right_range->GetBound().Add(left_compensation, &overflow, &underflow)); |
| 682 | if (!overflow && !underflow) { |
| 683 | ApplyRangeFromComparison(left, instruction->GetBlock(), successor, |
| 684 | new_left_range); |
| 685 | } |
| 686 | |
| 687 | ValueRange* new_right_range = new (GetGraph()->GetArena()) ValueRange( |
| 688 | GetGraph()->GetArena(), |
| 689 | left_range->GetBound().Add(right_compensation, &overflow, &underflow), |
| 690 | right_range->GetBound()); |
| 691 | if (!overflow && !underflow) { |
| 692 | ApplyRangeFromComparison(right, instruction->GetBlock(), successor, |
| 693 | new_right_range); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 699 | // Handle "if (left cmp_cond right)". |
| 700 | void HandleIf(HIf* instruction, HInstruction* left, HInstruction* right, IfCondition cond) { |
| 701 | HBasicBlock* block = instruction->GetBlock(); |
| 702 | |
| 703 | HBasicBlock* true_successor = instruction->IfTrueSuccessor(); |
| 704 | // There should be no critical edge at this point. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 705 | DCHECK_EQ(true_successor->GetPredecessors().size(), 1u); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 706 | |
| 707 | HBasicBlock* false_successor = instruction->IfFalseSuccessor(); |
| 708 | // There should be no critical edge at this point. |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 709 | DCHECK_EQ(false_successor->GetPredecessors().size(), 1u); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 710 | |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 711 | ValueRange* left_range = LookupValueRange(left, block); |
| 712 | MonotonicValueRange* left_monotonic_range = nullptr; |
| 713 | if (left_range != nullptr) { |
| 714 | left_monotonic_range = left_range->AsMonotonicValueRange(); |
| 715 | if (left_monotonic_range != nullptr) { |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 716 | HBasicBlock* loop_head = left_monotonic_range->GetLoopHeader(); |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 717 | if (instruction->GetBlock() != loop_head) { |
| 718 | // For monotonic value range, don't handle `instruction` |
| 719 | // if it's not defined in the loop header. |
| 720 | return; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 725 | bool found; |
| 726 | ValueBound bound = ValueBound::DetectValueBoundFromValue(right, &found); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 727 | // Each comparison can establish a lower bound and an upper bound |
| 728 | // for the left hand side. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 729 | ValueBound lower = bound; |
| 730 | ValueBound upper = bound; |
| 731 | if (!found) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 732 | // No constant or array.length+c format bound found. |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 733 | // 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] | 734 | ValueRange* right_range = LookupValueRange(right, block); |
| 735 | if (right_range != nullptr) { |
| 736 | if (right_range->IsMonotonicValueRange()) { |
Mingyao Yang | 57e0475 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 737 | if (left_range != nullptr && left_range->IsMonotonicValueRange()) { |
| 738 | HandleIfBetweenTwoMonotonicValueRanges(instruction, left, right, cond, |
| 739 | left_range->AsMonotonicValueRange(), |
| 740 | right_range->AsMonotonicValueRange()); |
| 741 | return; |
| 742 | } |
| 743 | } |
| 744 | lower = right_range->GetLower(); |
| 745 | upper = right_range->GetUpper(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 746 | } else { |
| 747 | lower = ValueBound::Min(); |
| 748 | upper = ValueBound::Max(); |
| 749 | } |
| 750 | } |
| 751 | |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 752 | bool overflow, underflow; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 753 | if (cond == kCondLT || cond == kCondLE) { |
| 754 | if (!upper.Equals(ValueBound::Max())) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 755 | int32_t compensation = (cond == kCondLT) ? -1 : 0; // upper bound is inclusive |
| 756 | ValueBound new_upper = upper.Add(compensation, &overflow, &underflow); |
| 757 | if (overflow || underflow) { |
| 758 | return; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 759 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 760 | ValueRange* new_range = new (GetGraph()->GetArena()) |
| 761 | ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper); |
| 762 | ApplyRangeFromComparison(left, block, true_successor, new_range); |
| 763 | } |
| 764 | |
| 765 | // array.length as a lower bound isn't considered useful. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 766 | if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) { |
| 767 | int32_t compensation = (cond == kCondLE) ? 1 : 0; // lower bound is inclusive |
| 768 | ValueBound new_lower = lower.Add(compensation, &overflow, &underflow); |
| 769 | if (overflow || underflow) { |
| 770 | return; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 771 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 772 | ValueRange* new_range = new (GetGraph()->GetArena()) |
| 773 | ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max()); |
| 774 | ApplyRangeFromComparison(left, block, false_successor, new_range); |
| 775 | } |
| 776 | } else if (cond == kCondGT || cond == kCondGE) { |
| 777 | // array.length as a lower bound isn't considered useful. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 778 | if (!lower.Equals(ValueBound::Min()) && !lower.IsRelatedToArrayLength()) { |
| 779 | int32_t compensation = (cond == kCondGT) ? 1 : 0; // lower bound is inclusive |
| 780 | ValueBound new_lower = lower.Add(compensation, &overflow, &underflow); |
| 781 | if (overflow || underflow) { |
| 782 | return; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 783 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 784 | ValueRange* new_range = new (GetGraph()->GetArena()) |
| 785 | ValueRange(GetGraph()->GetArena(), new_lower, ValueBound::Max()); |
| 786 | ApplyRangeFromComparison(left, block, true_successor, new_range); |
| 787 | } |
| 788 | |
| 789 | if (!upper.Equals(ValueBound::Max())) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 790 | int32_t compensation = (cond == kCondGE) ? -1 : 0; // upper bound is inclusive |
| 791 | ValueBound new_upper = upper.Add(compensation, &overflow, &underflow); |
| 792 | if (overflow || underflow) { |
| 793 | return; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 794 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 795 | ValueRange* new_range = new (GetGraph()->GetArena()) |
| 796 | ValueRange(GetGraph()->GetArena(), ValueBound::Min(), new_upper); |
| 797 | ApplyRangeFromComparison(left, block, false_successor, new_range); |
| 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 802 | void VisitBoundsCheck(HBoundsCheck* bounds_check) OVERRIDE { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 803 | HBasicBlock* block = bounds_check->GetBlock(); |
| 804 | HInstruction* index = bounds_check->InputAt(0); |
| 805 | HInstruction* array_length = bounds_check->InputAt(1); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 806 | DCHECK(array_length->IsIntConstant() || |
| 807 | array_length->IsArrayLength() || |
| 808 | array_length->IsPhi()); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 809 | bool try_dynamic_bce = true; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 810 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 811 | // Analyze index range. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 812 | if (!index->IsIntConstant()) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 813 | // Non-constant index. |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 814 | ValueBound lower = ValueBound(nullptr, 0); // constant 0 |
| 815 | ValueBound upper = ValueBound(array_length, -1); // array_length - 1 |
| 816 | ValueRange array_range(GetGraph()->GetArena(), lower, upper); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 817 | // Try index range obtained by dominator-based analysis. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 818 | ValueRange* index_range = LookupValueRange(index, block); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 819 | if (index_range != nullptr && index_range->FitsIn(&array_range)) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 820 | ReplaceInstruction(bounds_check, index); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 821 | return; |
| 822 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 823 | // Try index range obtained by induction variable analysis. |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 824 | // Disables dynamic bce if OOB is certain. |
| 825 | if (InductionRangeFitsIn(&array_range, bounds_check, index, &try_dynamic_bce)) { |
| 826 | ReplaceInstruction(bounds_check, index); |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 827 | return; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 828 | } |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 829 | } else { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 830 | // Constant index. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 831 | int32_t constant = index->AsIntConstant()->GetValue(); |
| 832 | if (constant < 0) { |
| 833 | // Will always throw exception. |
| 834 | return; |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 835 | } else if (array_length->IsIntConstant()) { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 836 | if (constant < array_length->AsIntConstant()->GetValue()) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 837 | ReplaceInstruction(bounds_check, index); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 838 | } |
| 839 | return; |
| 840 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 841 | // Analyze array length range. |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 842 | DCHECK(array_length->IsArrayLength()); |
| 843 | ValueRange* existing_range = LookupValueRange(array_length, block); |
| 844 | if (existing_range != nullptr) { |
| 845 | ValueBound lower = existing_range->GetLower(); |
| 846 | DCHECK(lower.IsConstant()); |
| 847 | if (constant < lower.GetConstant()) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 848 | ReplaceInstruction(bounds_check, index); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 849 | return; |
| 850 | } else { |
| 851 | // Existing range isn't strong enough to eliminate the bounds check. |
| 852 | // Fall through to update the array_length range with info from this |
| 853 | // bounds check. |
| 854 | } |
| 855 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 856 | // 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] | 857 | // We currently don't do it for non-constant index since a valid array[i] can't prove |
| 858 | // a valid array[i-1] yet due to the lower bound side. |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 859 | if (constant == std::numeric_limits<int32_t>::max()) { |
| 860 | // Max() as an index will definitely throw AIOOBE. |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 861 | return; |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 862 | } else { |
| 863 | ValueBound lower = ValueBound(nullptr, constant + 1); |
| 864 | ValueBound upper = ValueBound::Max(); |
| 865 | ValueRange* range = new (GetGraph()->GetArena()) |
| 866 | ValueRange(GetGraph()->GetArena(), lower, upper); |
| 867 | AssignRange(block, array_length, range); |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 868 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 869 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 870 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 871 | // If static analysis fails, and OOB is not certain, try dynamic elimination. |
| 872 | if (try_dynamic_bce) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 873 | // Try loop-based dynamic elimination. |
| 874 | if (TryDynamicBCE(bounds_check)) { |
| 875 | return; |
| 876 | } |
| 877 | // Prepare dominator-based dynamic elimination. |
| 878 | if (first_index_bounds_check_map_.find(array_length->GetId()) == |
| 879 | first_index_bounds_check_map_.end()) { |
| 880 | // Remember the first bounds check against each array_length. That bounds check |
| 881 | // instruction has an associated HEnvironment where we may add an HDeoptimize |
| 882 | // to eliminate subsequent bounds checks against the same array_length. |
| 883 | first_index_bounds_check_map_.Put(array_length->GetId(), bounds_check); |
| 884 | } |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 885 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 888 | static bool HasSameInputAtBackEdges(HPhi* phi) { |
| 889 | DCHECK(phi->IsLoopHeaderPhi()); |
| 890 | // Start with input 1. Input 0 is from the incoming block. |
| 891 | HInstruction* input1 = phi->InputAt(1); |
| 892 | DCHECK(phi->GetBlock()->GetLoopInformation()->IsBackEdge( |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 893 | *phi->GetBlock()->GetPredecessors()[1])); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 894 | for (size_t i = 2, e = phi->InputCount(); i < e; ++i) { |
| 895 | DCHECK(phi->GetBlock()->GetLoopInformation()->IsBackEdge( |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 896 | *phi->GetBlock()->GetPredecessors()[i])); |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 897 | if (input1 != phi->InputAt(i)) { |
| 898 | return false; |
| 899 | } |
| 900 | } |
| 901 | return true; |
| 902 | } |
| 903 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 904 | void VisitPhi(HPhi* phi) OVERRIDE { |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame] | 905 | if (phi->IsLoopHeaderPhi() |
| 906 | && (phi->GetType() == Primitive::kPrimInt) |
| 907 | && HasSameInputAtBackEdges(phi)) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 908 | HInstruction* instruction = phi->InputAt(1); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 909 | HInstruction *left; |
| 910 | int32_t increment; |
| 911 | if (ValueBound::IsAddOrSubAConstant(instruction, &left, &increment)) { |
| 912 | if (left == phi) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 913 | HInstruction* initial_value = phi->InputAt(0); |
| 914 | ValueRange* range = nullptr; |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 915 | if (increment == 0) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 916 | // Add constant 0. It's really a fixed value. |
| 917 | range = new (GetGraph()->GetArena()) ValueRange( |
| 918 | GetGraph()->GetArena(), |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 919 | ValueBound(initial_value, 0), |
| 920 | ValueBound(initial_value, 0)); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 921 | } else { |
| 922 | // Monotonically increasing/decreasing. |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 923 | bool found; |
| 924 | ValueBound bound = ValueBound::DetectValueBoundFromValue( |
| 925 | initial_value, &found); |
| 926 | if (!found) { |
| 927 | // No constant or array.length+c bound found. |
| 928 | // For i=j, we can still use j's upper bound as i's upper bound. |
| 929 | // Same for lower. |
| 930 | ValueRange* initial_range = LookupValueRange(initial_value, phi->GetBlock()); |
| 931 | if (initial_range != nullptr) { |
| 932 | bound = increment > 0 ? initial_range->GetLower() : |
| 933 | initial_range->GetUpper(); |
| 934 | } else { |
| 935 | bound = increment > 0 ? ValueBound::Min() : ValueBound::Max(); |
| 936 | } |
| 937 | } |
| 938 | range = new (GetGraph()->GetArena()) MonotonicValueRange( |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 939 | GetGraph()->GetArena(), |
Mingyao Yang | 206d6fd | 2015-04-13 16:46:28 -0700 | [diff] [blame] | 940 | phi, |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 941 | initial_value, |
Mingyao Yang | 6419752 | 2014-12-05 15:56:23 -0800 | [diff] [blame] | 942 | increment, |
| 943 | bound); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 944 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 945 | AssignRange(phi->GetBlock(), phi, range); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 946 | } |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 951 | void VisitIf(HIf* instruction) OVERRIDE { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 952 | if (instruction->InputAt(0)->IsCondition()) { |
| 953 | HCondition* cond = instruction->InputAt(0)->AsCondition(); |
| 954 | IfCondition cmp = cond->GetCondition(); |
| 955 | if (cmp == kCondGT || cmp == kCondGE || |
| 956 | cmp == kCondLT || cmp == kCondLE) { |
| 957 | HInstruction* left = cond->GetLeft(); |
| 958 | HInstruction* right = cond->GetRight(); |
| 959 | HandleIf(instruction, left, right, cmp); |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 964 | void VisitAdd(HAdd* add) OVERRIDE { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 965 | HInstruction* right = add->GetRight(); |
| 966 | if (right->IsIntConstant()) { |
| 967 | ValueRange* left_range = LookupValueRange(add->GetLeft(), add->GetBlock()); |
| 968 | if (left_range == nullptr) { |
| 969 | return; |
| 970 | } |
| 971 | ValueRange* range = left_range->Add(right->AsIntConstant()->GetValue()); |
| 972 | if (range != nullptr) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 973 | AssignRange(add->GetBlock(), add, range); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 974 | } |
| 975 | } |
| 976 | } |
| 977 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 978 | void VisitSub(HSub* sub) OVERRIDE { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 979 | HInstruction* left = sub->GetLeft(); |
| 980 | HInstruction* right = sub->GetRight(); |
| 981 | if (right->IsIntConstant()) { |
| 982 | ValueRange* left_range = LookupValueRange(left, sub->GetBlock()); |
| 983 | if (left_range == nullptr) { |
| 984 | return; |
| 985 | } |
| 986 | ValueRange* range = left_range->Add(-right->AsIntConstant()->GetValue()); |
| 987 | if (range != nullptr) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 988 | AssignRange(sub->GetBlock(), sub, range); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 989 | return; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | // Here we are interested in the typical triangular case of nested loops, |
| 994 | // such as the inner loop 'for (int j=0; j<array.length-i; j++)' where i |
| 995 | // 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] | 996 | |
| 997 | // Try to handle (array.length - i) or (array.length + c - i) format. |
| 998 | HInstruction* left_of_left; // left input of left. |
| 999 | int32_t right_const = 0; |
| 1000 | if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &right_const)) { |
| 1001 | left = left_of_left; |
| 1002 | } |
| 1003 | // The value of left input of the sub equals (left + right_const). |
| 1004 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1005 | if (left->IsArrayLength()) { |
| 1006 | HInstruction* array_length = left->AsArrayLength(); |
| 1007 | ValueRange* right_range = LookupValueRange(right, sub->GetBlock()); |
| 1008 | if (right_range != nullptr) { |
| 1009 | ValueBound lower = right_range->GetLower(); |
| 1010 | ValueBound upper = right_range->GetUpper(); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 1011 | if (lower.IsConstant() && upper.IsRelatedToArrayLength()) { |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1012 | HInstruction* upper_inst = upper.GetInstruction(); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 1013 | // Make sure it's the same array. |
| 1014 | if (ValueBound::Equal(array_length, upper_inst)) { |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1015 | int32_t c0 = right_const; |
| 1016 | int32_t c1 = lower.GetConstant(); |
| 1017 | int32_t c2 = upper.GetConstant(); |
| 1018 | // (array.length + c0 - v) where v is in [c1, array.length + c2] |
| 1019 | // gets [c0 - c2, array.length + c0 - c1] as its value range. |
| 1020 | if (!ValueBound::WouldAddOverflowOrUnderflow(c0, -c2) && |
| 1021 | !ValueBound::WouldAddOverflowOrUnderflow(c0, -c1)) { |
| 1022 | if ((c0 - c1) <= 0) { |
| 1023 | // array.length + (c0 - c1) won't overflow/underflow. |
| 1024 | ValueRange* range = new (GetGraph()->GetArena()) ValueRange( |
| 1025 | GetGraph()->GetArena(), |
| 1026 | ValueBound(nullptr, right_const - upper.GetConstant()), |
| 1027 | ValueBound(array_length, right_const - lower.GetConstant())); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1028 | AssignRange(sub->GetBlock(), sub, range); |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1029 | } |
| 1030 | } |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1037 | void FindAndHandlePartialArrayLength(HBinaryOperation* instruction) { |
| 1038 | DCHECK(instruction->IsDiv() || instruction->IsShr() || instruction->IsUShr()); |
| 1039 | HInstruction* right = instruction->GetRight(); |
| 1040 | int32_t right_const; |
| 1041 | if (right->IsIntConstant()) { |
| 1042 | right_const = right->AsIntConstant()->GetValue(); |
| 1043 | // Detect division by two or more. |
| 1044 | if ((instruction->IsDiv() && right_const <= 1) || |
| 1045 | (instruction->IsShr() && right_const < 1) || |
| 1046 | (instruction->IsUShr() && right_const < 1)) { |
| 1047 | return; |
| 1048 | } |
| 1049 | } else { |
| 1050 | return; |
| 1051 | } |
| 1052 | |
| 1053 | // Try to handle array.length/2 or (array.length-1)/2 format. |
| 1054 | HInstruction* left = instruction->GetLeft(); |
| 1055 | HInstruction* left_of_left; // left input of left. |
| 1056 | int32_t c = 0; |
| 1057 | if (ValueBound::IsAddOrSubAConstant(left, &left_of_left, &c)) { |
| 1058 | left = left_of_left; |
| 1059 | } |
| 1060 | // The value of left input of instruction equals (left + c). |
| 1061 | |
| 1062 | // (array_length + 1) or smaller divided by two or more |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 1063 | // always generate a value in [Min(), array_length]. |
| 1064 | // This is true even if array_length is Max(). |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1065 | if (left->IsArrayLength() && c <= 1) { |
| 1066 | if (instruction->IsUShr() && c < 0) { |
| 1067 | // Make sure for unsigned shift, left side is not negative. |
| 1068 | // e.g. if array_length is 2, ((array_length - 3) >>> 2) is way bigger |
| 1069 | // than array_length. |
| 1070 | return; |
| 1071 | } |
| 1072 | ValueRange* range = new (GetGraph()->GetArena()) ValueRange( |
| 1073 | GetGraph()->GetArena(), |
Aart Bik | aab5b75 | 2015-09-23 11:18:57 -0700 | [diff] [blame] | 1074 | ValueBound(nullptr, std::numeric_limits<int32_t>::min()), |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1075 | ValueBound(left, 0)); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1076 | AssignRange(instruction->GetBlock(), instruction, range); |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1080 | void VisitDiv(HDiv* div) OVERRIDE { |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1081 | FindAndHandlePartialArrayLength(div); |
| 1082 | } |
| 1083 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1084 | void VisitShr(HShr* shr) OVERRIDE { |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1085 | FindAndHandlePartialArrayLength(shr); |
| 1086 | } |
| 1087 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1088 | void VisitUShr(HUShr* ushr) OVERRIDE { |
Mingyao Yang | 8c8bad8 | 2015-02-09 18:13:26 -0800 | [diff] [blame] | 1089 | FindAndHandlePartialArrayLength(ushr); |
| 1090 | } |
| 1091 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1092 | void VisitAnd(HAnd* instruction) OVERRIDE { |
Mingyao Yang | 4559f00 | 2015-02-27 14:43:53 -0800 | [diff] [blame] | 1093 | if (instruction->GetRight()->IsIntConstant()) { |
| 1094 | int32_t constant = instruction->GetRight()->AsIntConstant()->GetValue(); |
| 1095 | if (constant > 0) { |
| 1096 | // constant serves as a mask so any number masked with it |
| 1097 | // gets a [0, constant] value range. |
| 1098 | ValueRange* range = new (GetGraph()->GetArena()) ValueRange( |
| 1099 | GetGraph()->GetArena(), |
| 1100 | ValueBound(nullptr, 0), |
| 1101 | ValueBound(nullptr, constant)); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1102 | AssignRange(instruction->GetBlock(), instruction, range); |
Mingyao Yang | 4559f00 | 2015-02-27 14:43:53 -0800 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1107 | void VisitNewArray(HNewArray* new_array) OVERRIDE { |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 1108 | HInstruction* len = new_array->InputAt(0); |
| 1109 | if (!len->IsIntConstant()) { |
| 1110 | HInstruction *left; |
| 1111 | int32_t right_const; |
| 1112 | if (ValueBound::IsAddOrSubAConstant(len, &left, &right_const)) { |
| 1113 | // (left + right_const) is used as size to new the array. |
| 1114 | // We record "-right_const <= left <= new_array - right_const"; |
| 1115 | ValueBound lower = ValueBound(nullptr, -right_const); |
| 1116 | // We use new_array for the bound instead of new_array.length, |
| 1117 | // which isn't available as an instruction yet. new_array will |
| 1118 | // be treated the same as new_array.length when it's used in a ValueBound. |
| 1119 | ValueBound upper = ValueBound(new_array, -right_const); |
| 1120 | ValueRange* range = new (GetGraph()->GetArena()) |
| 1121 | ValueRange(GetGraph()->GetArena(), lower, upper); |
Nicolas Geoffray | a09ff9c | 2015-06-24 10:38:27 +0100 | [diff] [blame] | 1122 | ValueRange* existing_range = LookupValueRange(left, new_array->GetBlock()); |
| 1123 | if (existing_range != nullptr) { |
| 1124 | range = existing_range->Narrow(range); |
| 1125 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1126 | AssignRange(new_array->GetBlock(), left, range); |
Mingyao Yang | 0304e18 | 2015-01-30 16:41:29 -0800 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1131 | /** |
| 1132 | * After null/bounds checks are eliminated, some invariant array references |
| 1133 | * may be exposed underneath which can be hoisted out of the loop to the |
| 1134 | * preheader or, in combination with dynamic bce, the deoptimization block. |
| 1135 | * |
| 1136 | * for (int i = 0; i < n; i++) { |
| 1137 | * <-------+ |
| 1138 | * for (int j = 0; j < n; j++) | |
| 1139 | * a[i][j] = 0; --a[i]--+ |
| 1140 | * } |
| 1141 | * |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1142 | * Note: this optimization is no longer applied after dominator-based dynamic deoptimization |
| 1143 | * has occurred (see AddCompareWithDeoptimization()), since in those cases it would be |
| 1144 | * unsafe to hoist array references across their deoptimization instruction inside a loop. |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1145 | */ |
| 1146 | void VisitArrayGet(HArrayGet* array_get) OVERRIDE { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1147 | if (!has_dom_based_dynamic_bce_ && array_get->IsInLoop()) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1148 | HLoopInformation* loop = array_get->GetBlock()->GetLoopInformation(); |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 1149 | if (loop->IsDefinedOutOfTheLoop(array_get->InputAt(0)) && |
| 1150 | loop->IsDefinedOutOfTheLoop(array_get->InputAt(1))) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1151 | SideEffects loop_effects = side_effects_.GetLoopEffects(loop->GetHeader()); |
| 1152 | if (!array_get->GetSideEffects().MayDependOn(loop_effects)) { |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1153 | HoistToPreHeaderOrDeoptBlock(loop, array_get); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1159 | // Perform dominator-based dynamic elimination on suitable set of bounds checks. |
| 1160 | void AddCompareWithDeoptimization(HBasicBlock* block, |
| 1161 | HInstruction* array_length, |
| 1162 | HInstruction* base, |
| 1163 | int32_t min_c, int32_t max_c) { |
| 1164 | HBoundsCheck* bounds_check = |
| 1165 | first_index_bounds_check_map_.Get(array_length->GetId())->AsBoundsCheck(); |
| 1166 | // Construct deoptimization on single or double bounds on range [base-min_c,base+max_c], |
| 1167 | // for example either for a[0]..a[3] just 3 or for a[base-1]..a[base+3] both base-1 |
| 1168 | // and base+3, since we made the assumption any in between value may occur too. |
| 1169 | static_assert(kMaxLengthForAddingDeoptimize < std::numeric_limits<int32_t>::max(), |
| 1170 | "Incorrect max length may be subject to arithmetic wrap-around"); |
| 1171 | HInstruction* upper = GetGraph()->GetIntConstant(max_c); |
| 1172 | if (base == nullptr) { |
| 1173 | DCHECK_GE(min_c, 0); |
| 1174 | } else { |
| 1175 | HInstruction* lower = new (GetGraph()->GetArena()) |
| 1176 | HAdd(Primitive::kPrimInt, base, GetGraph()->GetIntConstant(min_c)); |
| 1177 | upper = new (GetGraph()->GetArena()) HAdd(Primitive::kPrimInt, base, upper); |
| 1178 | block->InsertInstructionBefore(lower, bounds_check); |
| 1179 | block->InsertInstructionBefore(upper, bounds_check); |
| 1180 | InsertDeoptInBlock(bounds_check, new (GetGraph()->GetArena()) HAbove(lower, upper)); |
| 1181 | } |
| 1182 | InsertDeoptInBlock(bounds_check, new (GetGraph()->GetArena()) HAboveOrEqual(upper, array_length)); |
| 1183 | // Flag that this kind of deoptimization has occurred. |
| 1184 | has_dom_based_dynamic_bce_ = true; |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1185 | } |
| 1186 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1187 | // Attempt dominator-based dynamic elimination on remaining candidates. |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1188 | void AddComparesWithDeoptimization(HBasicBlock* block) { |
Vladimir Marko | da571cb | 2016-02-15 17:54:56 +0000 | [diff] [blame] | 1189 | for (const auto& entry : first_index_bounds_check_map_) { |
| 1190 | HBoundsCheck* bounds_check = entry.second; |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1191 | HInstruction* index = bounds_check->InputAt(0); |
Nicolas Geoffray | 8df886b | 2015-06-24 14:57:44 +0100 | [diff] [blame] | 1192 | HInstruction* array_length = bounds_check->InputAt(1); |
| 1193 | if (!array_length->IsArrayLength()) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1194 | continue; // disregard phis and constants |
Nicolas Geoffray | 8df886b | 2015-06-24 14:57:44 +0100 | [diff] [blame] | 1195 | } |
Aart Bik | 1ae8874 | 2016-03-14 14:11:26 -0700 | [diff] [blame] | 1196 | // Collect all bounds checks that are still there and that are related as "a[base + constant]" |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1197 | // for a base instruction (possibly absent) and various constants. Note that no attempt |
| 1198 | // is made to partition the set into matching subsets (viz. a[0], a[1] and a[base+1] and |
| 1199 | // a[base+2] are considered as one set). |
| 1200 | // TODO: would such a partitioning be worthwhile? |
| 1201 | ValueBound value = ValueBound::AsValueBound(index); |
| 1202 | HInstruction* base = value.GetInstruction(); |
| 1203 | int32_t min_c = base == nullptr ? 0 : value.GetConstant(); |
| 1204 | int32_t max_c = value.GetConstant(); |
| 1205 | ArenaVector<HBoundsCheck*> candidates( |
| 1206 | GetGraph()->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)); |
| 1207 | ArenaVector<HBoundsCheck*> standby( |
| 1208 | GetGraph()->GetArena()->Adapter(kArenaAllocBoundsCheckElimination)); |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 1209 | for (const HUseListNode<HInstruction*>& use : array_length->GetUses()) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1210 | // Another bounds check in same or dominated block? |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 1211 | HInstruction* user = use.GetUser(); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1212 | HBasicBlock* other_block = user->GetBlock(); |
| 1213 | if (user->IsBoundsCheck() && block->Dominates(other_block)) { |
| 1214 | HBoundsCheck* other_bounds_check = user->AsBoundsCheck(); |
| 1215 | HInstruction* other_index = other_bounds_check->InputAt(0); |
| 1216 | HInstruction* other_array_length = other_bounds_check->InputAt(1); |
| 1217 | ValueBound other_value = ValueBound::AsValueBound(other_index); |
| 1218 | if (array_length == other_array_length && base == other_value.GetInstruction()) { |
Aart Bik | 1ae8874 | 2016-03-14 14:11:26 -0700 | [diff] [blame] | 1219 | // Reject certain OOB if BoundsCheck(l, l) occurs on considered subset. |
| 1220 | if (array_length == other_index) { |
| 1221 | candidates.clear(); |
| 1222 | standby.clear(); |
| 1223 | break; |
| 1224 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1225 | // Since a subsequent dominated block could be under a conditional, only accept |
| 1226 | // the other bounds check if it is in same block or both blocks dominate the exit. |
| 1227 | // TODO: we could improve this by testing proper post-dominance, or even if this |
| 1228 | // constant is seen along *all* conditional paths that follow. |
| 1229 | HBasicBlock* exit = GetGraph()->GetExitBlock(); |
| 1230 | if (block == user->GetBlock() || |
| 1231 | (block->Dominates(exit) && other_block->Dominates(exit))) { |
Aart Bik | 1ae8874 | 2016-03-14 14:11:26 -0700 | [diff] [blame] | 1232 | int32_t other_c = other_value.GetConstant(); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1233 | min_c = std::min(min_c, other_c); |
| 1234 | max_c = std::max(max_c, other_c); |
| 1235 | candidates.push_back(other_bounds_check); |
| 1236 | } else { |
| 1237 | // Add this candidate later only if it falls into the range. |
| 1238 | standby.push_back(other_bounds_check); |
| 1239 | } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1240 | } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1241 | } |
| 1242 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1243 | // Add standby candidates that fall in selected range. |
Vladimir Marko | da571cb | 2016-02-15 17:54:56 +0000 | [diff] [blame] | 1244 | for (HBoundsCheck* other_bounds_check : standby) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1245 | HInstruction* other_index = other_bounds_check->InputAt(0); |
| 1246 | int32_t other_c = ValueBound::AsValueBound(other_index).GetConstant(); |
| 1247 | if (min_c <= other_c && other_c <= max_c) { |
| 1248 | candidates.push_back(other_bounds_check); |
| 1249 | } |
| 1250 | } |
| 1251 | // Perform dominator-based deoptimization if it seems profitable. Note that we reject cases |
| 1252 | // where the distance min_c:max_c range gets close to the maximum possible array length, |
| 1253 | // since those cases are likely to always deopt (such situations do not necessarily go |
| 1254 | // OOB, though, since the programmer could rely on wrap-around from max to min). |
| 1255 | size_t threshold = kThresholdForAddingDeoptimize + (base == nullptr ? 0 : 1); // extra test? |
| 1256 | uint32_t distance = static_cast<uint32_t>(max_c) - static_cast<uint32_t>(min_c); |
| 1257 | if (candidates.size() >= threshold && |
| 1258 | (base != nullptr || min_c >= 0) && // reject certain OOB |
| 1259 | distance <= kMaxLengthForAddingDeoptimize) { // reject likely/certain deopt |
| 1260 | AddCompareWithDeoptimization(block, array_length, base, min_c, max_c); |
Vladimir Marko | da571cb | 2016-02-15 17:54:56 +0000 | [diff] [blame] | 1261 | for (HInstruction* other_bounds_check : candidates) { |
Aart Bik | 1ae8874 | 2016-03-14 14:11:26 -0700 | [diff] [blame] | 1262 | // Only replace if still in the graph. This avoids visiting the same |
| 1263 | // bounds check twice if it occurred multiple times in the use list. |
| 1264 | if (other_bounds_check->IsInBlock()) { |
| 1265 | ReplaceInstruction(other_bounds_check, other_bounds_check->InputAt(0)); |
| 1266 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1267 | } |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1272 | /** |
| 1273 | * Returns true if static range analysis based on induction variables can determine the bounds |
| 1274 | * check on the given array range is always satisfied with the computed index range. The output |
| 1275 | * parameter try_dynamic_bce is set to false if OOB is certain. |
| 1276 | */ |
| 1277 | bool InductionRangeFitsIn(ValueRange* array_range, |
| 1278 | HInstruction* context, |
| 1279 | HInstruction* index, |
| 1280 | bool* try_dynamic_bce) { |
| 1281 | InductionVarRange::Value v1; |
| 1282 | InductionVarRange::Value v2; |
| 1283 | bool needs_finite_test = false; |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 1284 | if (induction_range_.GetInductionRange(context, index, &v1, &v2, &needs_finite_test)) { |
| 1285 | do { |
| 1286 | if (v1.is_known && (v1.a_constant == 0 || v1.a_constant == 1) && |
| 1287 | v2.is_known && (v2.a_constant == 0 || v2.a_constant == 1)) { |
| 1288 | DCHECK(v1.a_constant == 1 || v1.instruction == nullptr); |
| 1289 | DCHECK(v2.a_constant == 1 || v2.instruction == nullptr); |
| 1290 | ValueRange index_range(GetGraph()->GetArena(), |
| 1291 | ValueBound(v1.instruction, v1.b_constant), |
| 1292 | ValueBound(v2.instruction, v2.b_constant)); |
| 1293 | // If analysis reveals a certain OOB, disable dynamic BCE. |
| 1294 | if (index_range.GetLower().LessThan(array_range->GetLower()) || |
| 1295 | index_range.GetUpper().GreaterThan(array_range->GetUpper())) { |
| 1296 | *try_dynamic_bce = false; |
| 1297 | return false; |
| 1298 | } |
| 1299 | // Use analysis for static bce only if loop is finite. |
| 1300 | if (!needs_finite_test && index_range.FitsIn(array_range)) { |
| 1301 | return true; |
| 1302 | } |
Aart Bik | b738d4f | 2015-12-03 11:23:35 -0800 | [diff] [blame] | 1303 | } |
Aart Bik | 1fc3afb | 2016-02-02 13:26:16 -0800 | [diff] [blame] | 1304 | } while (induction_range_.RefineOuter(&v1, &v2)); |
| 1305 | } |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1306 | return false; |
| 1307 | } |
| 1308 | |
| 1309 | /** |
| 1310 | * When the compiler fails to remove a bounds check statically, we try to remove the bounds |
| 1311 | * check dynamically by adding runtime tests that trigger a deoptimization in case bounds |
| 1312 | * will go out of range (we want to be rather certain of that given the slowdown of |
| 1313 | * deoptimization). If no deoptimization occurs, the loop is executed with all corresponding |
| 1314 | * bounds checks and related null checks removed. |
| 1315 | */ |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1316 | bool TryDynamicBCE(HBoundsCheck* instruction) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1317 | HLoopInformation* loop = instruction->GetBlock()->GetLoopInformation(); |
| 1318 | HInstruction* index = instruction->InputAt(0); |
| 1319 | HInstruction* length = instruction->InputAt(1); |
| 1320 | // If dynamic bounds check elimination seems profitable and is possible, then proceed. |
| 1321 | bool needs_finite_test = false; |
| 1322 | bool needs_taken_test = false; |
| 1323 | if (DynamicBCESeemsProfitable(loop, instruction->GetBlock()) && |
| 1324 | induction_range_.CanGenerateCode( |
| 1325 | instruction, index, &needs_finite_test, &needs_taken_test) && |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 1326 | CanHandleInfiniteLoop(loop, instruction, index, needs_finite_test) && |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1327 | CanHandleLength(loop, length, needs_taken_test)) { // do this test last (may code gen) |
| 1328 | HInstruction* lower = nullptr; |
| 1329 | HInstruction* upper = nullptr; |
| 1330 | // Generate the following unsigned comparisons |
| 1331 | // if (lower > upper) deoptimize; |
| 1332 | // if (upper >= length) deoptimize; |
| 1333 | // or, for a non-induction index, just the unsigned comparison on its 'upper' value |
| 1334 | // if (upper >= length) deoptimize; |
| 1335 | // as runtime test. By restricting dynamic bce to unit strides (with a maximum of 32-bit |
| 1336 | // iterations) and by not combining access (e.g. a[i], a[i-3], a[i+5] etc.), these tests |
| 1337 | // correctly guard against any possible OOB (including arithmetic wrap-around cases). |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1338 | TransformLoopForDeoptimizationIfNeeded(loop, needs_taken_test); |
| 1339 | HBasicBlock* block = GetPreHeader(loop, instruction); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1340 | induction_range_.GenerateRangeCode(instruction, index, GetGraph(), block, &lower, &upper); |
| 1341 | if (lower != nullptr) { |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1342 | InsertDeoptInLoop(loop, block, new (GetGraph()->GetArena()) HAbove(lower, upper)); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1343 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1344 | InsertDeoptInLoop(loop, block, new (GetGraph()->GetArena()) HAboveOrEqual(upper, length)); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1345 | ReplaceInstruction(instruction, index); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1346 | return true; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1347 | } |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1348 | return false; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1349 | } |
| 1350 | |
| 1351 | /** |
| 1352 | * Returns true if heuristics indicate that dynamic bce may be profitable. |
| 1353 | */ |
| 1354 | bool DynamicBCESeemsProfitable(HLoopInformation* loop, HBasicBlock* block) { |
| 1355 | if (loop != nullptr) { |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 1356 | // The loop preheader of an irreducible loop does not dominate all the blocks in |
| 1357 | // the loop. We would need to find the common dominator of all blocks in the loop. |
| 1358 | if (loop->IsIrreducible()) { |
| 1359 | return false; |
| 1360 | } |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1361 | // A try boundary preheader is hard to handle. |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 1362 | // TODO: remove this restriction. |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1363 | if (loop->GetPreHeader()->GetLastInstruction()->IsTryBoundary()) { |
| 1364 | return false; |
| 1365 | } |
| 1366 | // Does loop have early-exits? If so, the full range may not be covered by the loop |
| 1367 | // at runtime and testing the range may apply deoptimization unnecessarily. |
| 1368 | if (IsEarlyExitLoop(loop)) { |
| 1369 | return false; |
| 1370 | } |
| 1371 | // Does the current basic block dominate all back edges? If not, |
| 1372 | // don't apply dynamic bce to something that may not be executed. |
| 1373 | for (HBasicBlock* back_edge : loop->GetBackEdges()) { |
| 1374 | if (!block->Dominates(back_edge)) { |
| 1375 | return false; |
| 1376 | } |
| 1377 | } |
| 1378 | // Success! |
| 1379 | return true; |
| 1380 | } |
| 1381 | return false; |
| 1382 | } |
| 1383 | |
| 1384 | /** |
| 1385 | * Returns true if the loop has early exits, which implies it may not cover |
| 1386 | * the full range computed by range analysis based on induction variables. |
| 1387 | */ |
| 1388 | bool IsEarlyExitLoop(HLoopInformation* loop) { |
| 1389 | const uint32_t loop_id = loop->GetHeader()->GetBlockId(); |
| 1390 | // If loop has been analyzed earlier for early-exit, don't repeat the analysis. |
| 1391 | auto it = early_exit_loop_.find(loop_id); |
| 1392 | if (it != early_exit_loop_.end()) { |
| 1393 | return it->second; |
| 1394 | } |
| 1395 | // First time early-exit analysis for this loop. Since analysis requires scanning |
| 1396 | // the full loop-body, results of the analysis is stored for subsequent queries. |
| 1397 | HBlocksInLoopReversePostOrderIterator it_loop(*loop); |
| 1398 | for (it_loop.Advance(); !it_loop.Done(); it_loop.Advance()) { |
| 1399 | for (HBasicBlock* successor : it_loop.Current()->GetSuccessors()) { |
| 1400 | if (!loop->Contains(*successor)) { |
| 1401 | early_exit_loop_.Put(loop_id, true); |
| 1402 | return true; |
| 1403 | } |
| 1404 | } |
| 1405 | } |
| 1406 | early_exit_loop_.Put(loop_id, false); |
| 1407 | return false; |
| 1408 | } |
| 1409 | |
| 1410 | /** |
| 1411 | * Returns true if the array length is already loop invariant, or can be made so |
| 1412 | * by handling the null check under the hood of the array length operation. |
| 1413 | */ |
| 1414 | bool CanHandleLength(HLoopInformation* loop, HInstruction* length, bool needs_taken_test) { |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 1415 | if (loop->IsDefinedOutOfTheLoop(length)) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1416 | return true; |
| 1417 | } else if (length->IsArrayLength() && length->GetBlock()->GetLoopInformation() == loop) { |
| 1418 | if (CanHandleNullCheck(loop, length->InputAt(0), needs_taken_test)) { |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1419 | HoistToPreHeaderOrDeoptBlock(loop, length); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1420 | return true; |
| 1421 | } |
| 1422 | } |
| 1423 | return false; |
| 1424 | } |
| 1425 | |
| 1426 | /** |
| 1427 | * Returns true if the null check is already loop invariant, or can be made so |
| 1428 | * by generating a deoptimization test. |
| 1429 | */ |
| 1430 | bool CanHandleNullCheck(HLoopInformation* loop, HInstruction* check, bool needs_taken_test) { |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 1431 | if (loop->IsDefinedOutOfTheLoop(check)) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1432 | return true; |
| 1433 | } else if (check->IsNullCheck() && check->GetBlock()->GetLoopInformation() == loop) { |
| 1434 | HInstruction* array = check->InputAt(0); |
Mingyao Yang | 4b467ed | 2015-11-19 17:04:22 -0800 | [diff] [blame] | 1435 | if (loop->IsDefinedOutOfTheLoop(array)) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1436 | // Generate: if (array == null) deoptimize; |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1437 | TransformLoopForDeoptimizationIfNeeded(loop, needs_taken_test); |
| 1438 | HBasicBlock* block = GetPreHeader(loop, check); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1439 | HInstruction* cond = |
| 1440 | new (GetGraph()->GetArena()) HEqual(array, GetGraph()->GetNullConstant()); |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1441 | InsertDeoptInLoop(loop, block, cond); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1442 | ReplaceInstruction(check, array); |
| 1443 | return true; |
| 1444 | } |
| 1445 | } |
| 1446 | return false; |
| 1447 | } |
| 1448 | |
| 1449 | /** |
| 1450 | * Returns true if compiler can apply dynamic bce to loops that may be infinite |
| 1451 | * (e.g. for (int i = 0; i <= U; i++) with U = MAX_INT), which would invalidate |
| 1452 | * the range analysis evaluation code by "overshooting" the computed range. |
| 1453 | * Since deoptimization would be a bad choice, and there is no other version |
| 1454 | * of the loop to use, dynamic bce in such cases is only allowed if other tests |
| 1455 | * ensure the loop is finite. |
| 1456 | */ |
| 1457 | bool CanHandleInfiniteLoop( |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 1458 | HLoopInformation* loop, HBoundsCheck* check, HInstruction* index, bool needs_infinite_test) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1459 | if (needs_infinite_test) { |
| 1460 | // If we already forced the loop to be finite, allow directly. |
| 1461 | const uint32_t loop_id = loop->GetHeader()->GetBlockId(); |
| 1462 | if (finite_loop_.find(loop_id) != finite_loop_.end()) { |
| 1463 | return true; |
| 1464 | } |
| 1465 | // Otherwise, allow dynamic bce if the index (which is necessarily an induction at |
| 1466 | // this point) is the direct loop index (viz. a[i]), since then the runtime tests |
| 1467 | // ensure upper bound cannot cause an infinite loop. |
| 1468 | HInstruction* control = loop->GetHeader()->GetLastInstruction(); |
| 1469 | if (control->IsIf()) { |
| 1470 | HInstruction* if_expr = control->AsIf()->InputAt(0); |
| 1471 | if (if_expr->IsCondition()) { |
| 1472 | HCondition* condition = if_expr->AsCondition(); |
| 1473 | if (index == condition->InputAt(0) || |
| 1474 | index == condition->InputAt(1)) { |
| 1475 | finite_loop_.insert(loop_id); |
| 1476 | return true; |
| 1477 | } |
| 1478 | } |
| 1479 | } |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 1480 | // If bounds check made it this far, it is worthwhile to check later if |
| 1481 | // the loop was forced finite by another candidate. |
Vladimir Marko | b75878e | 2016-03-14 13:56:02 +0000 | [diff] [blame] | 1482 | if (record_dynamic_bce_standby_) { |
| 1483 | dynamic_bce_standby_.push_back(check); |
| 1484 | } |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1485 | return false; |
| 1486 | } |
| 1487 | return true; |
| 1488 | } |
| 1489 | |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1490 | /** |
| 1491 | * Returns appropriate preheader for the loop, depending on whether the |
| 1492 | * instruction appears in the loop header or proper loop-body. |
| 1493 | */ |
| 1494 | HBasicBlock* GetPreHeader(HLoopInformation* loop, HInstruction* instruction) { |
| 1495 | // Use preheader unless there is an earlier generated deoptimization block since |
| 1496 | // hoisted expressions may depend on and/or used by the deoptimization tests. |
| 1497 | HBasicBlock* header = loop->GetHeader(); |
| 1498 | const uint32_t loop_id = header->GetBlockId(); |
| 1499 | auto it = taken_test_loop_.find(loop_id); |
| 1500 | if (it != taken_test_loop_.end()) { |
| 1501 | HBasicBlock* block = it->second; |
| 1502 | // If always taken, keep it that way by returning the original preheader, |
| 1503 | // which can be found by following the predecessor of the true-block twice. |
| 1504 | if (instruction->GetBlock() == header) { |
| 1505 | return block->GetSinglePredecessor()->GetSinglePredecessor(); |
| 1506 | } |
| 1507 | return block; |
| 1508 | } |
| 1509 | return loop->GetPreHeader(); |
| 1510 | } |
| 1511 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1512 | /** Inserts a deoptimization test in a loop preheader. */ |
| 1513 | void InsertDeoptInLoop(HLoopInformation* loop, HBasicBlock* block, HInstruction* condition) { |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1514 | HInstruction* suspend = loop->GetSuspendCheck(); |
| 1515 | block->InsertInstructionBefore(condition, block->GetLastInstruction()); |
| 1516 | HDeoptimize* deoptimize = |
| 1517 | new (GetGraph()->GetArena()) HDeoptimize(condition, suspend->GetDexPc()); |
| 1518 | block->InsertInstructionBefore(deoptimize, block->GetLastInstruction()); |
| 1519 | if (suspend->HasEnvironment()) { |
| 1520 | deoptimize->CopyEnvironmentFromWithLoopPhiAdjustment( |
| 1521 | suspend->GetEnvironment(), loop->GetHeader()); |
| 1522 | } |
| 1523 | } |
| 1524 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1525 | /** Inserts a deoptimization test right before a bounds check. */ |
| 1526 | void InsertDeoptInBlock(HBoundsCheck* bounds_check, HInstruction* condition) { |
| 1527 | HBasicBlock* block = bounds_check->GetBlock(); |
| 1528 | block->InsertInstructionBefore(condition, bounds_check); |
| 1529 | HDeoptimize* deoptimize = |
| 1530 | new (GetGraph()->GetArena()) HDeoptimize(condition, bounds_check->GetDexPc()); |
| 1531 | block->InsertInstructionBefore(deoptimize, bounds_check); |
| 1532 | deoptimize->CopyEnvironmentFrom(bounds_check->GetEnvironment()); |
| 1533 | } |
| 1534 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1535 | /** Hoists instruction out of the loop to preheader or deoptimization block. */ |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1536 | void HoistToPreHeaderOrDeoptBlock(HLoopInformation* loop, HInstruction* instruction) { |
| 1537 | HBasicBlock* block = GetPreHeader(loop, instruction); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1538 | DCHECK(!instruction->HasEnvironment()); |
| 1539 | instruction->MoveBefore(block->GetLastInstruction()); |
| 1540 | } |
| 1541 | |
| 1542 | /** |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1543 | * Adds a new taken-test structure to a loop if needed and not already done. |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1544 | * The taken-test protects range analysis evaluation code to avoid any |
| 1545 | * deoptimization caused by incorrect trip-count evaluation in non-taken loops. |
| 1546 | * |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1547 | * old_preheader |
| 1548 | * | |
| 1549 | * if_block <- taken-test protects deoptimization block |
| 1550 | * / \ |
| 1551 | * true_block false_block <- deoptimizations/invariants are placed in true_block |
| 1552 | * \ / |
| 1553 | * new_preheader <- may require phi nodes to preserve SSA structure |
| 1554 | * | |
| 1555 | * header |
| 1556 | * |
| 1557 | * For example, this loop: |
| 1558 | * |
| 1559 | * for (int i = lower; i < upper; i++) { |
| 1560 | * array[i] = 0; |
| 1561 | * } |
| 1562 | * |
| 1563 | * will be transformed to: |
| 1564 | * |
| 1565 | * if (lower < upper) { |
| 1566 | * if (array == null) deoptimize; |
| 1567 | * array_length = array.length; |
| 1568 | * if (lower > upper) deoptimize; // unsigned |
| 1569 | * if (upper >= array_length) deoptimize; // unsigned |
| 1570 | * } else { |
| 1571 | * array_length = 0; |
| 1572 | * } |
| 1573 | * for (int i = lower; i < upper; i++) { |
| 1574 | * // Loop without null check and bounds check, and any array.length replaced with array_length. |
| 1575 | * array[i] = 0; |
| 1576 | * } |
| 1577 | */ |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1578 | void TransformLoopForDeoptimizationIfNeeded(HLoopInformation* loop, bool needs_taken_test) { |
| 1579 | // Not needed (can use preheader) or already done (can reuse)? |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1580 | const uint32_t loop_id = loop->GetHeader()->GetBlockId(); |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1581 | if (!needs_taken_test || taken_test_loop_.find(loop_id) != taken_test_loop_.end()) { |
| 1582 | return; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | // Generate top test structure. |
| 1586 | HBasicBlock* header = loop->GetHeader(); |
| 1587 | GetGraph()->TransformLoopHeaderForBCE(header); |
| 1588 | HBasicBlock* new_preheader = loop->GetPreHeader(); |
| 1589 | HBasicBlock* if_block = new_preheader->GetDominator(); |
| 1590 | HBasicBlock* true_block = if_block->GetSuccessors()[0]; // True successor. |
| 1591 | HBasicBlock* false_block = if_block->GetSuccessors()[1]; // False successor. |
| 1592 | |
| 1593 | // Goto instructions. |
| 1594 | true_block->AddInstruction(new (GetGraph()->GetArena()) HGoto()); |
| 1595 | false_block->AddInstruction(new (GetGraph()->GetArena()) HGoto()); |
| 1596 | new_preheader->AddInstruction(new (GetGraph()->GetArena()) HGoto()); |
| 1597 | |
| 1598 | // Insert the taken-test to see if the loop body is entered. If the |
| 1599 | // loop isn't entered at all, it jumps around the deoptimization block. |
| 1600 | if_block->AddInstruction(new (GetGraph()->GetArena()) HGoto()); // placeholder |
| 1601 | HInstruction* condition = nullptr; |
| 1602 | induction_range_.GenerateTakenTest(header->GetLastInstruction(), |
| 1603 | GetGraph(), |
| 1604 | if_block, |
| 1605 | &condition); |
| 1606 | DCHECK(condition != nullptr); |
| 1607 | if_block->RemoveInstruction(if_block->GetLastInstruction()); |
| 1608 | if_block->AddInstruction(new (GetGraph()->GetArena()) HIf(condition)); |
| 1609 | |
| 1610 | taken_test_loop_.Put(loop_id, true_block); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /** |
| 1614 | * Inserts phi nodes that preserve SSA structure in generated top test structures. |
| 1615 | * All uses of instructions in the deoptimization block that reach the loop need |
| 1616 | * a phi node in the new loop preheader to fix the dominance relation. |
| 1617 | * |
| 1618 | * Example: |
| 1619 | * if_block |
| 1620 | * / \ |
| 1621 | * x_0 = .. false_block |
| 1622 | * \ / |
| 1623 | * x_1 = phi(x_0, null) <- synthetic phi |
| 1624 | * | |
Aart Bik | 55b14df | 2016-01-12 14:12:47 -0800 | [diff] [blame] | 1625 | * new_preheader |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1626 | */ |
| 1627 | void InsertPhiNodes() { |
| 1628 | // Scan all new deoptimization blocks. |
| 1629 | for (auto it1 = taken_test_loop_.begin(); it1 != taken_test_loop_.end(); ++it1) { |
| 1630 | HBasicBlock* true_block = it1->second; |
| 1631 | HBasicBlock* new_preheader = true_block->GetSingleSuccessor(); |
| 1632 | // Scan all instructions in a new deoptimization block. |
| 1633 | for (HInstructionIterator it(true_block->GetInstructions()); !it.Done(); it.Advance()) { |
| 1634 | HInstruction* instruction = it.Current(); |
| 1635 | Primitive::Type type = instruction->GetType(); |
| 1636 | HPhi* phi = nullptr; |
| 1637 | // Scan all uses of an instruction and replace each later use with a phi node. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 1638 | const HUseList<HInstruction*>& uses = instruction->GetUses(); |
| 1639 | for (auto it2 = uses.begin(), end2 = uses.end(); it2 != end2; /* ++it2 below */) { |
| 1640 | HInstruction* user = it2->GetUser(); |
| 1641 | size_t index = it2->GetIndex(); |
| 1642 | // Increment `it2` now because `*it2` may disappear thanks to user->ReplaceInput(). |
| 1643 | ++it2; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1644 | if (user->GetBlock() != true_block) { |
| 1645 | if (phi == nullptr) { |
| 1646 | phi = NewPhi(new_preheader, instruction, type); |
| 1647 | } |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 1648 | user->ReplaceInput(phi, index); // Removes the use node from the list. |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | // Scan all environment uses of an instruction and replace each later use with a phi node. |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 1652 | const HUseList<HEnvironment*>& env_uses = instruction->GetEnvUses(); |
| 1653 | for (auto it2 = env_uses.begin(), end2 = env_uses.end(); it2 != end2; /* ++it2 below */) { |
| 1654 | HEnvironment* user = it2->GetUser(); |
| 1655 | size_t index = it2->GetIndex(); |
| 1656 | // Increment `it2` now because `*it2` may disappear thanks to user->RemoveAsUserOfInput(). |
| 1657 | ++it2; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1658 | if (user->GetHolder()->GetBlock() != true_block) { |
| 1659 | if (phi == nullptr) { |
| 1660 | phi = NewPhi(new_preheader, instruction, type); |
| 1661 | } |
Vladimir Marko | 46817b8 | 2016-03-29 12:21:58 +0100 | [diff] [blame] | 1662 | user->RemoveAsUserOfInput(index); |
| 1663 | user->SetRawEnvAt(index, phi); |
| 1664 | phi->AddEnvUseAt(user, index); |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /** |
| 1672 | * Construct a phi(instruction, 0) in the new preheader to fix the dominance relation. |
| 1673 | * These are synthetic phi nodes without a virtual register. |
| 1674 | */ |
| 1675 | HPhi* NewPhi(HBasicBlock* new_preheader, |
| 1676 | HInstruction* instruction, |
| 1677 | Primitive::Type type) { |
| 1678 | HGraph* graph = GetGraph(); |
| 1679 | HInstruction* zero; |
| 1680 | switch (type) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 1681 | case Primitive::kPrimNot: zero = graph->GetNullConstant(); break; |
| 1682 | case Primitive::kPrimFloat: zero = graph->GetFloatConstant(0); break; |
| 1683 | case Primitive::kPrimDouble: zero = graph->GetDoubleConstant(0); break; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1684 | default: zero = graph->GetConstant(type, 0); break; |
| 1685 | } |
| 1686 | HPhi* phi = new (graph->GetArena()) |
| 1687 | HPhi(graph->GetArena(), kNoRegNumber, /*number_of_inputs*/ 2, HPhi::ToPhiType(type)); |
| 1688 | phi->SetRawInputAt(0, instruction); |
| 1689 | phi->SetRawInputAt(1, zero); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 1690 | if (type == Primitive::kPrimNot) { |
| 1691 | phi->SetReferenceTypeInfo(instruction->GetReferenceTypeInfo()); |
| 1692 | } |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1693 | new_preheader->AddPhi(phi); |
| 1694 | return phi; |
| 1695 | } |
| 1696 | |
| 1697 | /** Helper method to replace an instruction with another instruction. */ |
| 1698 | static void ReplaceInstruction(HInstruction* instruction, HInstruction* replacement) { |
| 1699 | instruction->ReplaceWith(replacement); |
| 1700 | instruction->GetBlock()->RemoveInstruction(instruction); |
| 1701 | } |
| 1702 | |
| 1703 | // A set of maps, one per basic block, from instruction to range. |
Vladimir Marko | 5233f93 | 2015-09-29 19:01:15 +0100 | [diff] [blame] | 1704 | ArenaVector<ArenaSafeMap<int, ValueRange*>> maps_; |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1705 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1706 | // Map an HArrayLength instruction's id to the first HBoundsCheck instruction |
| 1707 | // in a block that checks an index against that HArrayLength. |
| 1708 | ArenaSafeMap<int, HBoundsCheck*> first_index_bounds_check_map_; |
Mingyao Yang | d43b3ac | 2015-04-01 14:03:04 -0700 | [diff] [blame] | 1709 | |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 1710 | // Stand by list for dynamic bce. |
| 1711 | ArenaVector<HBoundsCheck*> dynamic_bce_standby_; |
Vladimir Marko | b75878e | 2016-03-14 13:56:02 +0000 | [diff] [blame] | 1712 | bool record_dynamic_bce_standby_; |
Aart Bik | 591ad29 | 2016-03-01 10:39:25 -0800 | [diff] [blame] | 1713 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1714 | // Early-exit loop bookkeeping. |
| 1715 | ArenaSafeMap<uint32_t, bool> early_exit_loop_; |
| 1716 | |
| 1717 | // Taken-test loop bookkeeping. |
| 1718 | ArenaSafeMap<uint32_t, HBasicBlock*> taken_test_loop_; |
| 1719 | |
| 1720 | // Finite loop bookkeeping. |
| 1721 | ArenaSet<uint32_t> finite_loop_; |
| 1722 | |
Aart Bik | 1d23982 | 2016-02-09 14:26:34 -0800 | [diff] [blame] | 1723 | // Flag that denotes whether dominator-based dynamic elimination has occurred. |
| 1724 | bool has_dom_based_dynamic_bce_; |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1725 | |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1726 | // Initial number of blocks. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 1727 | uint32_t initial_block_size_; |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1728 | |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1729 | // Side effects. |
| 1730 | const SideEffectsAnalysis& side_effects_; |
| 1731 | |
Aart Bik | 22af3be | 2015-09-10 12:50:58 -0700 | [diff] [blame] | 1732 | // Range analysis based on induction variables. |
| 1733 | InductionVarRange induction_range_; |
| 1734 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1735 | DISALLOW_COPY_AND_ASSIGN(BCEVisitor); |
| 1736 | }; |
| 1737 | |
| 1738 | void BoundsCheckElimination::Run() { |
Mark Mendell | 1152c92 | 2015-04-24 17:06:35 -0400 | [diff] [blame] | 1739 | if (!graph_->HasBoundsChecks()) { |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 1740 | return; |
| 1741 | } |
| 1742 | |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1743 | // Reverse post order guarantees a node's dominators are visited first. |
| 1744 | // We want to visit in the dominator-based order since if a value is known to |
| 1745 | // be bounded by a range at one instruction, it must be true that all uses of |
| 1746 | // that value dominated by that instruction fits in that range. Range of that |
| 1747 | // value can be narrowed further down in the dominator tree. |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1748 | BCEVisitor visitor(graph_, side_effects_, induction_analysis_); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1749 | for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) { |
| 1750 | HBasicBlock* current = it.Current(); |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1751 | if (visitor.IsAddedBlock(current)) { |
| 1752 | // Skip added blocks. Their effects are already taken care of. |
| 1753 | continue; |
| 1754 | } |
| 1755 | visitor.VisitBasicBlock(current); |
Aart Bik | b6347b7 | 2016-02-29 13:56:44 -0800 | [diff] [blame] | 1756 | // Skip forward to the current block in case new basic blocks were inserted |
| 1757 | // (which always appear earlier in reverse post order) to avoid visiting the |
| 1758 | // same basic block twice. |
| 1759 | for ( ; !it.Done() && it.Current() != current; it.Advance()) { |
| 1760 | } |
Mingyao Yang | 3584bce | 2015-05-19 16:01:59 -0700 | [diff] [blame] | 1761 | } |
Aart Bik | 4a34277 | 2015-11-30 10:17:46 -0800 | [diff] [blame] | 1762 | |
| 1763 | // Perform cleanup. |
| 1764 | visitor.Finish(); |
Mingyao Yang | f384f88 | 2014-10-22 16:08:18 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
| 1767 | } // namespace art |