Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [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 | #ifndef ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_ |
| 19 | |
| 20 | #include "nodes.h" |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 21 | #include <iostream> |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 25 | class CodeGenerator; |
| 26 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 27 | static constexpr int kNoRegister = -1; |
| 28 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 29 | class BlockInfo : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 30 | public: |
| 31 | BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values) |
| 32 | : block_(block), |
| 33 | live_in_(allocator, number_of_ssa_values, false), |
| 34 | live_out_(allocator, number_of_ssa_values, false), |
| 35 | kill_(allocator, number_of_ssa_values, false) { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 36 | UNUSED(block_); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 37 | live_in_.ClearAllBits(); |
| 38 | live_out_.ClearAllBits(); |
| 39 | kill_.ClearAllBits(); |
| 40 | } |
| 41 | |
| 42 | private: |
| 43 | const HBasicBlock& block_; |
| 44 | ArenaBitVector live_in_; |
| 45 | ArenaBitVector live_out_; |
| 46 | ArenaBitVector kill_; |
| 47 | |
| 48 | friend class SsaLivenessAnalysis; |
| 49 | |
| 50 | DISALLOW_COPY_AND_ASSIGN(BlockInfo); |
| 51 | }; |
| 52 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 53 | /** |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 54 | * A live range contains the start and end of a range where an instruction or a temporary |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 55 | * is live. |
| 56 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 57 | class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 58 | public: |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 59 | LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 60 | DCHECK_LT(start, end); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 61 | DCHECK(next_ == nullptr || next_->GetStart() > GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | size_t GetStart() const { return start_; } |
| 65 | size_t GetEnd() const { return end_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 66 | LiveRange* GetNext() const { return next_; } |
| 67 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 68 | bool IntersectsWith(const LiveRange& other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 69 | return (start_ >= other.start_ && start_ < other.end_) |
| 70 | || (other.start_ >= start_ && other.start_ < end_); |
| 71 | } |
| 72 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 73 | bool IsBefore(const LiveRange& other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 74 | return end_ <= other.start_; |
| 75 | } |
| 76 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 77 | void Dump(std::ostream& stream) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 78 | stream << "[" << start_ << ", " << end_ << ")"; |
| 79 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 80 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 81 | LiveRange* Dup(ArenaAllocator* allocator) const { |
| 82 | return new (allocator) LiveRange( |
| 83 | start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator)); |
| 84 | } |
| 85 | |
| 86 | LiveRange* GetLastRange() { |
| 87 | return next_ == nullptr ? this : next_->GetLastRange(); |
| 88 | } |
| 89 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 90 | private: |
| 91 | size_t start_; |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 92 | size_t end_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 93 | LiveRange* next_; |
| 94 | |
| 95 | friend class LiveInterval; |
| 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(LiveRange); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 100 | /** |
| 101 | * A use position represents a live interval use at a given position. |
| 102 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 103 | class UsePosition : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 104 | public: |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 105 | UsePosition(HInstruction* user, |
| 106 | size_t input_index, |
| 107 | bool is_environment, |
| 108 | size_t position, |
| 109 | UsePosition* next) |
| 110 | : user_(user), |
| 111 | input_index_(input_index), |
| 112 | is_environment_(is_environment), |
| 113 | position_(position), |
| 114 | next_(next) { |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 115 | DCHECK(user->IsPhi() |
| 116 | || (GetPosition() == user->GetLifetimePosition() + 1) |
| 117 | || (GetPosition() == user->GetLifetimePosition())); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 118 | DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition()); |
| 119 | } |
| 120 | |
| 121 | size_t GetPosition() const { return position_; } |
| 122 | |
| 123 | UsePosition* GetNext() const { return next_; } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 124 | void SetNext(UsePosition* next) { next_ = next; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 125 | |
| 126 | HInstruction* GetUser() const { return user_; } |
| 127 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 128 | bool GetIsEnvironment() const { return is_environment_; } |
| 129 | |
| 130 | size_t GetInputIndex() const { return input_index_; } |
| 131 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 132 | void Dump(std::ostream& stream) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 133 | stream << position_; |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 134 | if (is_environment_) { |
| 135 | stream << " (env)"; |
| 136 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 139 | UsePosition* Dup(ArenaAllocator* allocator) const { |
| 140 | return new (allocator) UsePosition( |
| 141 | user_, input_index_, is_environment_, position_, |
| 142 | next_ == nullptr ? nullptr : next_->Dup(allocator)); |
| 143 | } |
| 144 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 145 | private: |
| 146 | HInstruction* const user_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 147 | const size_t input_index_; |
| 148 | const bool is_environment_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 149 | const size_t position_; |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 150 | UsePosition* next_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 151 | |
| 152 | DISALLOW_COPY_AND_ASSIGN(UsePosition); |
| 153 | }; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 154 | |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 155 | class SafepointPosition : public ArenaObject<kArenaAllocMisc> { |
| 156 | public: |
| 157 | explicit SafepointPosition(HInstruction* instruction) |
| 158 | : instruction_(instruction), |
| 159 | next_(nullptr) {} |
| 160 | |
| 161 | void SetNext(SafepointPosition* next) { |
| 162 | next_ = next; |
| 163 | } |
| 164 | |
| 165 | size_t GetPosition() const { |
| 166 | return instruction_->GetLifetimePosition(); |
| 167 | } |
| 168 | |
| 169 | SafepointPosition* GetNext() const { |
| 170 | return next_; |
| 171 | } |
| 172 | |
| 173 | LocationSummary* GetLocations() const { |
| 174 | return instruction_->GetLocations(); |
| 175 | } |
| 176 | |
| 177 | HInstruction* GetInstruction() const { |
| 178 | return instruction_; |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | HInstruction* const instruction_; |
| 183 | SafepointPosition* next_; |
| 184 | |
| 185 | DISALLOW_COPY_AND_ASSIGN(SafepointPosition); |
| 186 | }; |
| 187 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 188 | /** |
| 189 | * An interval is a list of disjoint live ranges where an instruction is live. |
| 190 | * Each instruction that has uses gets an interval. |
| 191 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 192 | class LiveInterval : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 193 | public: |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 194 | static LiveInterval* MakeInterval(ArenaAllocator* allocator, |
| 195 | Primitive::Type type, |
| 196 | HInstruction* instruction = nullptr) { |
| 197 | return new (allocator) LiveInterval(allocator, type, instruction); |
| 198 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 199 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 200 | static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) { |
| 201 | return new (allocator) LiveInterval( |
| 202 | allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true); |
| 203 | } |
| 204 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 205 | static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 206 | return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false); |
| 207 | } |
| 208 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 209 | static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) { |
| 210 | return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | bool IsFixed() const { return is_fixed_; } |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 214 | bool IsTemp() const { return is_temp_; } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 215 | bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; } |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 216 | // This interval is the result of a split. |
| 217 | bool IsSplit() const { return parent_ != this; } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 218 | |
Nicolas Geoffray | f01d344 | 2015-03-27 17:15:49 +0000 | [diff] [blame] | 219 | void AddTempUse(HInstruction* instruction, size_t temp_index) { |
| 220 | DCHECK(IsTemp()); |
| 221 | DCHECK(first_use_ == nullptr) << "A temporary can only have one user"; |
| 222 | size_t position = instruction->GetLifetimePosition(); |
| 223 | first_use_ = new (allocator_) UsePosition( |
| 224 | instruction, temp_index, /* is_environment */ false, position, first_use_); |
| 225 | AddRange(position, position + 1); |
| 226 | } |
| 227 | |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 228 | void AddUse(HInstruction* instruction, |
| 229 | size_t input_index, |
| 230 | bool is_environment, |
| 231 | bool keep_alive = false) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 232 | // Set the use within the instruction. |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 233 | size_t position = instruction->GetLifetimePosition() + 1; |
| 234 | LocationSummary* locations = instruction->GetLocations(); |
| 235 | if (!is_environment) { |
| 236 | if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) { |
| 237 | // For fixed inputs and output same as input, the register allocator |
| 238 | // requires to have inputs die at the instruction, so that input moves use the |
| 239 | // location of the input just before that instruction (and not potential moves due |
| 240 | // to splitting). |
| 241 | position = instruction->GetLifetimePosition(); |
| 242 | } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 243 | } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 244 | |
| 245 | DCHECK(position == instruction->GetLifetimePosition() |
| 246 | || position == instruction->GetLifetimePosition() + 1); |
| 247 | |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 248 | if ((first_use_ != nullptr) |
| 249 | && (first_use_->GetUser() == instruction) |
| 250 | && (first_use_->GetPosition() < position)) { |
| 251 | // The user uses the instruction multiple times, and one use dies before the other. |
| 252 | // We update the use list so that the latter is first. |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 253 | DCHECK(!is_environment); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 254 | UsePosition* cursor = first_use_; |
| 255 | while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) { |
| 256 | cursor = cursor->GetNext(); |
| 257 | } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 258 | DCHECK(first_use_->GetPosition() + 1 == position); |
| 259 | UsePosition* new_use = new (allocator_) UsePosition( |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 260 | instruction, input_index, is_environment, position, cursor->GetNext()); |
| 261 | cursor->SetNext(new_use); |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 262 | if (first_range_->GetEnd() == first_use_->GetPosition()) { |
| 263 | first_range_->end_ = position; |
| 264 | } |
| 265 | return; |
| 266 | } |
| 267 | |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 268 | first_use_ = new (allocator_) UsePosition( |
| 269 | instruction, input_index, is_environment, position, first_use_); |
| 270 | |
| 271 | if (is_environment && !keep_alive) { |
| 272 | // If this environment use does not keep the instruction live, it does not |
| 273 | // affect the live range of that instruction. |
| 274 | return; |
| 275 | } |
| 276 | |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 277 | size_t start_block_position = instruction->GetBlock()->GetLifetimeStart(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 278 | if (first_range_ == nullptr) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 279 | // First time we see a use of that interval. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 280 | first_range_ = last_range_ = range_search_start_ = |
| 281 | new (allocator_) LiveRange(start_block_position, position, nullptr); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 282 | } else if (first_range_->GetStart() == start_block_position) { |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 283 | // There is a use later in the same block or in a following block. |
| 284 | // Note that in such a case, `AddRange` for the whole blocks has been called |
| 285 | // before arriving in this method, and this is the reason the start of |
| 286 | // `first_range_` is before the given `position`. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 287 | DCHECK_LE(position, first_range_->GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 288 | } else { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 289 | DCHECK(first_range_->GetStart() > position); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 290 | // There is a hole in the interval. Create a new range. |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 291 | // Note that the start of `first_range_` can be equal to `end`: two blocks |
| 292 | // having adjacent lifetime positions are not necessarily |
| 293 | // predecessor/successor. When two blocks are predecessor/successor, the |
| 294 | // liveness algorithm has called `AddRange` before arriving in this method, |
| 295 | // and the check line 205 would succeed. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 296 | first_range_ = range_search_start_ = |
| 297 | new (allocator_) LiveRange(start_block_position, position, first_range_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 298 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 301 | void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) { |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 302 | DCHECK(instruction->IsPhi()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 303 | first_use_ = new (allocator_) UsePosition( |
| 304 | instruction, input_index, false, block->GetLifetimeEnd(), first_use_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void AddRange(size_t start, size_t end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 308 | if (first_range_ == nullptr) { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 309 | first_range_ = last_range_ = range_search_start_ = |
| 310 | new (allocator_) LiveRange(start, end, first_range_); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 311 | } else if (first_range_->GetStart() == end) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 312 | // There is a use in the following block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 313 | first_range_->start_ = start; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 314 | } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) { |
| 315 | DCHECK(is_fixed_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 316 | } else { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 317 | DCHECK_GT(first_range_->GetStart(), end); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 318 | // There is a hole in the interval. Create a new range. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 319 | first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | void AddLoopRange(size_t start, size_t end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 324 | DCHECK(first_range_ != nullptr); |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 325 | DCHECK_LE(start, first_range_->GetStart()); |
| 326 | // Find the range that covers the positions after the loop. |
| 327 | LiveRange* after_loop = first_range_; |
| 328 | LiveRange* last_in_loop = nullptr; |
| 329 | while (after_loop != nullptr && after_loop->GetEnd() < end) { |
| 330 | DCHECK_LE(start, after_loop->GetStart()); |
| 331 | last_in_loop = after_loop; |
| 332 | after_loop = after_loop->GetNext(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 333 | } |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 334 | if (after_loop == nullptr) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 335 | // Uses are only in the loop. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 336 | first_range_ = last_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, nullptr); |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 337 | } else if (after_loop->GetStart() <= end) { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 338 | first_range_ = range_search_start_ = after_loop; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 339 | // There are uses after the loop. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 340 | first_range_->start_ = start; |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 341 | } else { |
| 342 | // The use after the loop is after a lifetime hole. |
| 343 | DCHECK(last_in_loop != nullptr); |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 344 | first_range_ = range_search_start_ = last_in_loop; |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 345 | first_range_->start_ = start; |
| 346 | first_range_->end_ = end; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 350 | bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 351 | void SetSpillSlot(int slot) { |
| 352 | DCHECK(!is_fixed_); |
| 353 | DCHECK(!is_temp_); |
| 354 | spill_slot_ = slot; |
| 355 | } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 356 | int GetSpillSlot() const { return spill_slot_; } |
| 357 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 358 | void SetFrom(size_t from) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 359 | if (first_range_ != nullptr) { |
| 360 | first_range_->start_ = from; |
| 361 | } else { |
| 362 | // Instruction without uses. |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 363 | DCHECK(!defined_by_->HasNonEnvironmentUses()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 364 | DCHECK(from == defined_by_->GetLifetimePosition()); |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 365 | first_range_ = last_range_ = range_search_start_ = |
| 366 | new (allocator_) LiveRange(from, from + 2, nullptr); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 367 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 368 | } |
| 369 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 370 | LiveInterval* GetParent() const { return parent_; } |
| 371 | |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 372 | // Returns whether this interval is the parent interval, that is, the interval |
| 373 | // that starts where the HInstruction is defined. |
| 374 | bool IsParent() const { return parent_ == this; } |
| 375 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 376 | LiveRange* GetFirstRange() const { return first_range_; } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 377 | LiveRange* GetLastRange() const { return last_range_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 378 | |
| 379 | int GetRegister() const { return register_; } |
| 380 | void SetRegister(int reg) { register_ = reg; } |
| 381 | void ClearRegister() { register_ = kNoRegister; } |
| 382 | bool HasRegister() const { return register_ != kNoRegister; } |
| 383 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 384 | bool IsDeadAt(size_t position) const { |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 385 | return GetEnd() <= position; |
| 386 | } |
| 387 | |
| 388 | bool IsDefinedAt(size_t position) const { |
| 389 | return GetStart() <= position && !IsDeadAt(position); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 390 | } |
| 391 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 392 | // Returns true if the interval contains a LiveRange covering `position`. |
| 393 | // The range at or immediately after the current position of linear scan |
| 394 | // is cached for better performance. If `position` can be smaller than |
| 395 | // that, CoversSlow should be used instead. |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 396 | bool Covers(size_t position) { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 397 | LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_); |
| 398 | range_search_start_ = candidate; |
| 399 | return (candidate != nullptr && candidate->GetStart() <= position); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 400 | } |
| 401 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 402 | // Same as Covers but always tests all ranges. |
| 403 | bool CoversSlow(size_t position) const { |
| 404 | LiveRange* candidate = FindRangeAtOrAfter(position, first_range_); |
| 405 | return candidate != nullptr && candidate->GetStart() <= position; |
| 406 | } |
| 407 | |
| 408 | // Returns the first intersection of this interval with `current`, which |
| 409 | // must be the interval currently being allocated by linear scan. |
| 410 | size_t FirstIntersectionWith(LiveInterval* current) const { |
| 411 | // Find the first range after the start of `current`. We use the search |
| 412 | // cache to improve performance. |
| 413 | DCHECK(GetStart() <= current->GetStart() || IsFixed()); |
| 414 | LiveRange* other_range = current->first_range_; |
| 415 | LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_); |
| 416 | if (my_range == nullptr) { |
| 417 | return kNoLifetime; |
| 418 | } |
| 419 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 420 | // Advance both intervals and find the first matching range start in |
| 421 | // this interval. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 422 | do { |
David Brazdil | 714e14f | 2015-02-25 11:57:05 +0000 | [diff] [blame] | 423 | if (my_range->IsBefore(*other_range)) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 424 | my_range = my_range->GetNext(); |
| 425 | if (my_range == nullptr) { |
| 426 | return kNoLifetime; |
| 427 | } |
David Brazdil | 714e14f | 2015-02-25 11:57:05 +0000 | [diff] [blame] | 428 | } else if (other_range->IsBefore(*my_range)) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 429 | other_range = other_range->GetNext(); |
| 430 | if (other_range == nullptr) { |
| 431 | return kNoLifetime; |
| 432 | } |
David Brazdil | 714e14f | 2015-02-25 11:57:05 +0000 | [diff] [blame] | 433 | } else { |
| 434 | DCHECK(my_range->IntersectsWith(*other_range)); |
| 435 | return std::max(my_range->GetStart(), other_range->GetStart()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 436 | } |
| 437 | } while (true); |
| 438 | } |
| 439 | |
| 440 | size_t GetStart() const { |
| 441 | return first_range_->GetStart(); |
| 442 | } |
| 443 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 444 | size_t GetEnd() const { |
| 445 | return last_range_->GetEnd(); |
| 446 | } |
| 447 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 448 | size_t FirstRegisterUseAfter(size_t position) const { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 449 | if (is_temp_) { |
| 450 | return position == GetStart() ? position : kNoLifetime; |
| 451 | } |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 452 | if (position == GetStart() && IsParent()) { |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 453 | LocationSummary* locations = defined_by_->GetLocations(); |
| 454 | Location location = locations->Out(); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 455 | // This interval is the first interval of the instruction. If the output |
| 456 | // of the instruction requires a register, we return the position of that instruction |
| 457 | // as the first register use. |
| 458 | if (location.IsUnallocated()) { |
| 459 | if ((location.GetPolicy() == Location::kRequiresRegister) |
| 460 | || (location.GetPolicy() == Location::kSameAsFirstInput |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 461 | && (locations->InAt(0).IsRegister() |
| 462 | || locations->InAt(0).IsRegisterPair() |
| 463 | || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 464 | return position; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 465 | } else if ((location.GetPolicy() == Location::kRequiresFpuRegister) |
| 466 | || (location.GetPolicy() == Location::kSameAsFirstInput |
| 467 | && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) { |
| 468 | return position; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 469 | } |
Nicolas Geoffray | 234d69d | 2015-03-09 10:28:50 +0000 | [diff] [blame] | 470 | } else if (location.IsRegister() || location.IsRegisterPair()) { |
| 471 | return position; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 475 | UsePosition* use = first_use_; |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 476 | size_t end = GetEnd(); |
| 477 | while (use != nullptr && use->GetPosition() <= end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 478 | size_t use_position = use->GetPosition(); |
Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 479 | if (use_position > position && !use->GetIsEnvironment()) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 480 | Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 481 | if (location.IsUnallocated() |
| 482 | && (location.GetPolicy() == Location::kRequiresRegister |
| 483 | || location.GetPolicy() == Location::kRequiresFpuRegister)) { |
Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 484 | return use_position; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 485 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 486 | } |
| 487 | use = use->GetNext(); |
| 488 | } |
| 489 | return kNoLifetime; |
| 490 | } |
| 491 | |
| 492 | size_t FirstRegisterUse() const { |
| 493 | return FirstRegisterUseAfter(GetStart()); |
| 494 | } |
| 495 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 496 | size_t FirstUseAfter(size_t position) const { |
| 497 | if (is_temp_) { |
| 498 | return position == GetStart() ? position : kNoLifetime; |
| 499 | } |
| 500 | |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 501 | if (position == GetStart() && IsParent()) { |
| 502 | if (defined_by_->GetLocations()->Out().IsValid()) { |
| 503 | return position; |
| 504 | } |
| 505 | } |
| 506 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 507 | UsePosition* use = first_use_; |
| 508 | size_t end = GetEnd(); |
| 509 | while (use != nullptr && use->GetPosition() <= end) { |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 510 | if (!use->GetIsEnvironment()) { |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 511 | Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex()); |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 512 | size_t use_position = use->GetPosition(); |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 513 | if (use_position > position && location.IsValid()) { |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 514 | return use_position; |
| 515 | } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 516 | } |
| 517 | use = use->GetNext(); |
| 518 | } |
| 519 | return kNoLifetime; |
| 520 | } |
| 521 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 522 | UsePosition* GetFirstUse() const { |
| 523 | return first_use_; |
| 524 | } |
| 525 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 526 | Primitive::Type GetType() const { |
| 527 | return type_; |
| 528 | } |
| 529 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 530 | HInstruction* GetDefinedBy() const { |
| 531 | return defined_by_; |
| 532 | } |
| 533 | |
Nicolas Geoffray | 43af728 | 2015-04-16 13:01:01 +0100 | [diff] [blame] | 534 | SafepointPosition* FindSafepointJustBefore(size_t position) const { |
| 535 | for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr; |
| 536 | safepoint != nullptr; |
| 537 | previous = safepoint, safepoint = safepoint->GetNext()) { |
| 538 | if (safepoint->GetPosition() >= position) return previous; |
| 539 | } |
| 540 | return last_safepoint_; |
| 541 | } |
| 542 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 543 | /** |
| 544 | * Split this interval at `position`. This interval is changed to: |
| 545 | * [start ... position). |
| 546 | * |
| 547 | * The new interval covers: |
| 548 | * [position ... end) |
| 549 | */ |
| 550 | LiveInterval* SplitAt(size_t position) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 551 | DCHECK(!is_temp_); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 552 | DCHECK(!is_fixed_); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 553 | DCHECK_GT(position, GetStart()); |
| 554 | |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 555 | if (GetEnd() <= position) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 556 | // This range dies before `position`, no need to split. |
| 557 | return nullptr; |
| 558 | } |
| 559 | |
| 560 | LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_); |
Nicolas Geoffray | 43af728 | 2015-04-16 13:01:01 +0100 | [diff] [blame] | 561 | SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position); |
| 562 | if (new_last_safepoint == nullptr) { |
| 563 | new_interval->first_safepoint_ = first_safepoint_; |
| 564 | new_interval->last_safepoint_ = last_safepoint_; |
| 565 | first_safepoint_ = last_safepoint_ = nullptr; |
| 566 | } else if (last_safepoint_ != new_last_safepoint) { |
| 567 | new_interval->last_safepoint_ = last_safepoint_; |
| 568 | new_interval->first_safepoint_ = new_last_safepoint->GetNext(); |
| 569 | DCHECK(new_interval->first_safepoint_ != nullptr); |
| 570 | last_safepoint_ = new_last_safepoint; |
| 571 | last_safepoint_->SetNext(nullptr); |
| 572 | } |
| 573 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 574 | new_interval->next_sibling_ = next_sibling_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 575 | next_sibling_ = new_interval; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 576 | new_interval->parent_ = parent_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 577 | |
| 578 | new_interval->first_use_ = first_use_; |
| 579 | LiveRange* current = first_range_; |
| 580 | LiveRange* previous = nullptr; |
| 581 | // Iterate over the ranges, and either find a range that covers this position, or |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 582 | // two ranges in between this position (that is, the position is in a lifetime hole). |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 583 | do { |
| 584 | if (position >= current->GetEnd()) { |
| 585 | // Move to next range. |
| 586 | previous = current; |
| 587 | current = current->next_; |
| 588 | } else if (position <= current->GetStart()) { |
| 589 | // If the previous range did not cover this position, we know position is in |
| 590 | // a lifetime hole. We can just break the first_range_ and last_range_ links |
| 591 | // and return the new interval. |
| 592 | DCHECK(previous != nullptr); |
| 593 | DCHECK(current != first_range_); |
| 594 | new_interval->last_range_ = last_range_; |
| 595 | last_range_ = previous; |
| 596 | previous->next_ = nullptr; |
| 597 | new_interval->first_range_ = current; |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 598 | if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) { |
| 599 | // Search start point is inside `new_interval`. Change it to nullptr |
| 600 | // (i.e. the end of the interval) in the original interval. |
| 601 | range_search_start_ = nullptr; |
| 602 | } |
| 603 | new_interval->range_search_start_ = new_interval->first_range_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 604 | return new_interval; |
| 605 | } else { |
| 606 | // This range covers position. We create a new last_range_ for this interval |
| 607 | // that covers last_range_->Start() and position. We also shorten the current |
| 608 | // range and make it the first range of the new interval. |
| 609 | DCHECK(position < current->GetEnd() && position > current->GetStart()); |
| 610 | new_interval->last_range_ = last_range_; |
| 611 | last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr); |
| 612 | if (previous != nullptr) { |
| 613 | previous->next_ = last_range_; |
| 614 | } else { |
| 615 | first_range_ = last_range_; |
| 616 | } |
| 617 | new_interval->first_range_ = current; |
| 618 | current->start_ = position; |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 619 | if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) { |
| 620 | // Search start point is inside `new_interval`. Change it to `last_range` |
| 621 | // in the original interval. This is conservative but always correct. |
| 622 | range_search_start_ = last_range_; |
| 623 | } |
| 624 | new_interval->range_search_start_ = new_interval->first_range_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 625 | return new_interval; |
| 626 | } |
| 627 | } while (current != nullptr); |
| 628 | |
| 629 | LOG(FATAL) << "Unreachable"; |
| 630 | return nullptr; |
| 631 | } |
| 632 | |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 633 | bool StartsBeforeOrAt(LiveInterval* other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 634 | return GetStart() <= other->GetStart(); |
| 635 | } |
| 636 | |
| 637 | bool StartsAfter(LiveInterval* other) const { |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 638 | return GetStart() > other->GetStart(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | void Dump(std::ostream& stream) const { |
| 642 | stream << "ranges: { "; |
| 643 | LiveRange* current = first_range_; |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 644 | while (current != nullptr) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 645 | current->Dump(stream); |
| 646 | stream << " "; |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 647 | current = current->GetNext(); |
| 648 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 649 | stream << "}, uses: { "; |
| 650 | UsePosition* use = first_use_; |
| 651 | if (use != nullptr) { |
| 652 | do { |
| 653 | use->Dump(stream); |
| 654 | stream << " "; |
| 655 | } while ((use = use->GetNext()) != nullptr); |
| 656 | } |
| 657 | stream << "}"; |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 658 | stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit(); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 659 | stream << " is_high: " << IsHighInterval(); |
| 660 | stream << " is_low: " << IsLowInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | LiveInterval* GetNextSibling() const { return next_sibling_; } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 664 | LiveInterval* GetLastSibling() { |
| 665 | LiveInterval* result = this; |
| 666 | while (result->next_sibling_ != nullptr) { |
| 667 | result = result->next_sibling_; |
| 668 | } |
| 669 | return result; |
| 670 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 671 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 672 | // Returns the first register hint that is at least free before |
| 673 | // the value contained in `free_until`. If none is found, returns |
| 674 | // `kNoRegister`. |
| 675 | int FindFirstRegisterHint(size_t* free_until) const; |
| 676 | |
| 677 | // If there is enough at the definition site to find a register (for example |
| 678 | // it uses the same input as the first input), returns the register as a hint. |
| 679 | // Returns kNoRegister otherwise. |
| 680 | int FindHintAtDefinition() const; |
| 681 | |
| 682 | // Returns whether the interval needs two (Dex virtual register size `kVRegSize`) |
| 683 | // slots for spilling. |
| 684 | bool NeedsTwoSpillSlots() const; |
| 685 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 686 | bool IsFloatingPoint() const { |
| 687 | return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble; |
| 688 | } |
| 689 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 690 | // Converts the location of the interval to a `Location` object. |
| 691 | Location ToLocation() const; |
| 692 | |
| 693 | // Returns the location of the interval following its siblings at `position`. |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 694 | Location GetLocationAt(size_t position); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 695 | |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 696 | // Finds the sibling that is defined at `position`. |
| 697 | LiveInterval* GetSiblingAt(size_t position); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 698 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 699 | // Returns whether `other` and `this` share the same kind of register. |
| 700 | bool SameRegisterKind(Location other) const; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 701 | bool SameRegisterKind(const LiveInterval& other) const { |
| 702 | return IsFloatingPoint() == other.IsFloatingPoint(); |
| 703 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 704 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 705 | bool HasHighInterval() const { |
Nicolas Geoffray | 3747b48 | 2015-01-19 17:17:16 +0000 | [diff] [blame] | 706 | return IsLowInterval(); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | bool HasLowInterval() const { |
| 710 | return IsHighInterval(); |
| 711 | } |
| 712 | |
| 713 | LiveInterval* GetLowInterval() const { |
| 714 | DCHECK(HasLowInterval()); |
| 715 | return high_or_low_interval_; |
| 716 | } |
| 717 | |
| 718 | LiveInterval* GetHighInterval() const { |
| 719 | DCHECK(HasHighInterval()); |
| 720 | return high_or_low_interval_; |
| 721 | } |
| 722 | |
| 723 | bool IsHighInterval() const { |
| 724 | return GetParent()->is_high_interval_; |
| 725 | } |
| 726 | |
| 727 | bool IsLowInterval() const { |
| 728 | return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr); |
| 729 | } |
| 730 | |
| 731 | void SetLowInterval(LiveInterval* low) { |
| 732 | DCHECK(IsHighInterval()); |
| 733 | high_or_low_interval_ = low; |
| 734 | } |
| 735 | |
| 736 | void SetHighInterval(LiveInterval* high) { |
| 737 | DCHECK(IsLowInterval()); |
| 738 | high_or_low_interval_ = high; |
| 739 | } |
| 740 | |
| 741 | void AddHighInterval(bool is_temp = false) { |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 742 | DCHECK(IsParent()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 743 | DCHECK(!HasHighInterval()); |
| 744 | DCHECK(!HasLowInterval()); |
| 745 | high_or_low_interval_ = new (allocator_) LiveInterval( |
| 746 | allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true); |
| 747 | high_or_low_interval_->high_or_low_interval_ = this; |
| 748 | if (first_range_ != nullptr) { |
| 749 | high_or_low_interval_->first_range_ = first_range_->Dup(allocator_); |
David Brazdil | c08675c | 2015-04-17 15:49:51 +0100 | [diff] [blame] | 750 | high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange(); |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 751 | high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_; |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 752 | } |
| 753 | if (first_use_ != nullptr) { |
| 754 | high_or_low_interval_->first_use_ = first_use_->Dup(allocator_); |
| 755 | } |
| 756 | } |
| 757 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 758 | // Returns whether an interval, when it is non-split, is using |
| 759 | // the same register of one of its input. |
| 760 | bool IsUsingInputRegister() const { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 761 | CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs"; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 762 | if (defined_by_ != nullptr && !IsSplit()) { |
| 763 | for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) { |
| 764 | LiveInterval* interval = it.Current()->GetLiveInterval(); |
| 765 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 766 | // Find the interval that covers `defined_by`_. Calls to this function |
| 767 | // are made outside the linear scan, hence we need to use CoversSlow. |
| 768 | while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) { |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 769 | interval = interval->GetNextSibling(); |
| 770 | } |
| 771 | |
| 772 | // Check if both intervals have the same register of the same kind. |
| 773 | if (interval != nullptr |
| 774 | && interval->SameRegisterKind(*this) |
| 775 | && interval->GetRegister() == GetRegister()) { |
| 776 | return true; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | return false; |
| 781 | } |
| 782 | |
| 783 | // Returns whether an interval, when it is non-split, can safely use |
| 784 | // the same register of one of its input. Note that this method requires |
| 785 | // IsUsingInputRegister() to be true. |
| 786 | bool CanUseInputRegister() const { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 787 | CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs"; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 788 | DCHECK(IsUsingInputRegister()); |
| 789 | if (defined_by_ != nullptr && !IsSplit()) { |
| 790 | LocationSummary* locations = defined_by_->GetLocations(); |
| 791 | if (locations->OutputCanOverlapWithInputs()) { |
| 792 | return false; |
| 793 | } |
| 794 | for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) { |
| 795 | LiveInterval* interval = it.Current()->GetLiveInterval(); |
| 796 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 797 | // Find the interval that covers `defined_by`_. Calls to this function |
| 798 | // are made outside the linear scan, hence we need to use CoversSlow. |
| 799 | while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) { |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 800 | interval = interval->GetNextSibling(); |
| 801 | } |
| 802 | |
| 803 | if (interval != nullptr |
| 804 | && interval->SameRegisterKind(*this) |
| 805 | && interval->GetRegister() == GetRegister()) { |
| 806 | // We found the input that has the same register. Check if it is live after |
| 807 | // `defined_by`_. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 808 | return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1); |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | } |
| 812 | LOG(FATAL) << "Unreachable"; |
| 813 | UNREACHABLE(); |
| 814 | } |
| 815 | |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 816 | void AddSafepoint(HInstruction* instruction) { |
| 817 | SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction); |
| 818 | if (first_safepoint_ == nullptr) { |
| 819 | first_safepoint_ = last_safepoint_ = safepoint; |
| 820 | } else { |
| 821 | DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition()); |
| 822 | last_safepoint_->SetNext(safepoint); |
| 823 | last_safepoint_ = safepoint; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | SafepointPosition* GetFirstSafepoint() const { |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 828 | return first_safepoint_; |
| 829 | } |
| 830 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 831 | // Resets the starting point for range-searching queries to the first range. |
| 832 | // Intervals must be reset prior to starting a new linear scan over them. |
| 833 | void ResetSearchCache() { |
| 834 | range_search_start_ = first_range_; |
| 835 | } |
| 836 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 837 | private: |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 838 | LiveInterval(ArenaAllocator* allocator, |
| 839 | Primitive::Type type, |
| 840 | HInstruction* defined_by = nullptr, |
| 841 | bool is_fixed = false, |
| 842 | int reg = kNoRegister, |
| 843 | bool is_temp = false, |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 844 | bool is_slow_path_safepoint = false, |
| 845 | bool is_high_interval = false) |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 846 | : allocator_(allocator), |
| 847 | first_range_(nullptr), |
| 848 | last_range_(nullptr), |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 849 | range_search_start_(nullptr), |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 850 | first_safepoint_(nullptr), |
| 851 | last_safepoint_(nullptr), |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 852 | first_use_(nullptr), |
| 853 | type_(type), |
| 854 | next_sibling_(nullptr), |
| 855 | parent_(this), |
| 856 | register_(reg), |
| 857 | spill_slot_(kNoSpillSlot), |
| 858 | is_fixed_(is_fixed), |
| 859 | is_temp_(is_temp), |
| 860 | is_slow_path_safepoint_(is_slow_path_safepoint), |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 861 | is_high_interval_(is_high_interval), |
| 862 | high_or_low_interval_(nullptr), |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 863 | defined_by_(defined_by) {} |
| 864 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 865 | // Searches for a LiveRange that either covers the given position or is the |
| 866 | // first next LiveRange. Returns nullptr if no such LiveRange exists. Ranges |
| 867 | // known to end before `position` can be skipped with `search_start`. |
| 868 | LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const { |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 869 | if (kIsDebugBuild) { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 870 | if (search_start != first_range_) { |
| 871 | // If we are not searching the entire list of ranges, make sure we do |
| 872 | // not skip the range we are searching for. |
| 873 | if (search_start == nullptr) { |
| 874 | DCHECK(IsDeadAt(position)); |
| 875 | } else if (search_start->GetStart() > position) { |
| 876 | DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_)); |
| 877 | } |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 878 | } |
| 879 | } |
| 880 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 881 | LiveRange* range; |
| 882 | for (range = search_start; |
| 883 | range != nullptr && range->GetEnd() <= position; |
| 884 | range = range->GetNext()) { |
| 885 | continue; |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 886 | } |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 887 | return range; |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 890 | ArenaAllocator* const allocator_; |
| 891 | |
| 892 | // Ranges of this interval. We need a quick access to the last range to test |
| 893 | // for liveness (see `IsDeadAt`). |
| 894 | LiveRange* first_range_; |
| 895 | LiveRange* last_range_; |
| 896 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 897 | // The first range at or after the current position of a linear scan. It is |
| 898 | // used to optimize range-searching queries. |
| 899 | LiveRange* range_search_start_; |
| 900 | |
Nicolas Geoffray | 43af728 | 2015-04-16 13:01:01 +0100 | [diff] [blame] | 901 | // Safepoints where this interval is live. |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 902 | SafepointPosition* first_safepoint_; |
| 903 | SafepointPosition* last_safepoint_; |
| 904 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 905 | // Uses of this interval. Note that this linked list is shared amongst siblings. |
| 906 | UsePosition* first_use_; |
| 907 | |
| 908 | // The instruction type this interval corresponds to. |
| 909 | const Primitive::Type type_; |
| 910 | |
| 911 | // Live interval that is the result of a split. |
| 912 | LiveInterval* next_sibling_; |
| 913 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 914 | // The first interval from which split intervals come from. |
| 915 | LiveInterval* parent_; |
| 916 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 917 | // The register allocated to this interval. |
| 918 | int register_; |
| 919 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 920 | // The spill slot allocated to this interval. |
| 921 | int spill_slot_; |
| 922 | |
| 923 | // Whether the interval is for a fixed register. |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 924 | const bool is_fixed_; |
| 925 | |
| 926 | // Whether the interval is for a temporary. |
| 927 | const bool is_temp_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 928 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 929 | // Whether the interval is for a safepoint that calls on slow path. |
| 930 | const bool is_slow_path_safepoint_; |
| 931 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 932 | // Whether this interval is a synthesized interval for register pair. |
| 933 | const bool is_high_interval_; |
| 934 | |
| 935 | // If this interval needs a register pair, the high or low equivalent. |
| 936 | // `is_high_interval_` tells whether this holds the low or the high. |
| 937 | LiveInterval* high_or_low_interval_; |
| 938 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 939 | // The instruction represented by this interval. |
| 940 | HInstruction* const defined_by_; |
| 941 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 942 | static constexpr int kNoRegister = -1; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 943 | static constexpr int kNoSpillSlot = -1; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 944 | |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 945 | ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive); |
| 946 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 947 | DISALLOW_COPY_AND_ASSIGN(LiveInterval); |
| 948 | }; |
| 949 | |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 950 | /** |
| 951 | * Analysis that computes the liveness of instructions: |
| 952 | * |
| 953 | * (a) Non-environment uses of an instruction always make |
| 954 | * the instruction live. |
| 955 | * (b) Environment uses of an instruction whose type is |
| 956 | * object (that is, non-primitive), make the instruction live. |
| 957 | * This is due to having to keep alive objects that have |
| 958 | * finalizers deleting native objects. |
| 959 | * (c) When the graph has the debuggable property, environment uses |
| 960 | * of an instruction that has a primitive type make the instruction live. |
| 961 | * If the graph does not have the debuggable property, the environment |
| 962 | * use has no effect, and may get a 'none' value after register allocation. |
| 963 | * |
| 964 | * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment. |
| 965 | */ |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 966 | class SsaLivenessAnalysis : public ValueObject { |
| 967 | public: |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 968 | SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 969 | : graph_(graph), |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 970 | codegen_(codegen), |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 971 | block_infos_(graph->GetArena(), graph->GetBlocks().Size()), |
| 972 | instructions_from_ssa_index_(graph->GetArena(), 0), |
| 973 | instructions_from_lifetime_position_(graph->GetArena(), 0), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 974 | number_of_ssa_values_(0) { |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 975 | block_infos_.SetSize(graph->GetBlocks().Size()); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | void Analyze(); |
| 979 | |
| 980 | BitVector* GetLiveInSet(const HBasicBlock& block) const { |
| 981 | return &block_infos_.Get(block.GetBlockId())->live_in_; |
| 982 | } |
| 983 | |
| 984 | BitVector* GetLiveOutSet(const HBasicBlock& block) const { |
| 985 | return &block_infos_.Get(block.GetBlockId())->live_out_; |
| 986 | } |
| 987 | |
| 988 | BitVector* GetKillSet(const HBasicBlock& block) const { |
| 989 | return &block_infos_.Get(block.GetBlockId())->kill_; |
| 990 | } |
| 991 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 992 | HInstruction* GetInstructionFromSsaIndex(size_t index) const { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 993 | return instructions_from_ssa_index_.Get(index); |
| 994 | } |
| 995 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 996 | HInstruction* GetInstructionFromPosition(size_t index) const { |
| 997 | return instructions_from_lifetime_position_.Get(index); |
| 998 | } |
| 999 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1000 | HInstruction* GetTempUser(LiveInterval* temp) const { |
| 1001 | // A temporary shares the same lifetime start as the instruction that requires it. |
| 1002 | DCHECK(temp->IsTemp()); |
Nicolas Geoffray | f01d344 | 2015-03-27 17:15:49 +0000 | [diff] [blame] | 1003 | HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2); |
| 1004 | DCHECK_EQ(user, temp->GetFirstUse()->GetUser()); |
| 1005 | return user; |
| 1006 | } |
| 1007 | |
| 1008 | size_t GetTempIndex(LiveInterval* temp) const { |
| 1009 | // We use the input index to store the index of the temporary in the user's temporary list. |
| 1010 | DCHECK(temp->IsTemp()); |
| 1011 | return temp->GetFirstUse()->GetInputIndex(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1012 | } |
| 1013 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1014 | size_t GetMaxLifetimePosition() const { |
| 1015 | return instructions_from_lifetime_position_.Size() * 2 - 1; |
| 1016 | } |
| 1017 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1018 | size_t GetNumberOfSsaValues() const { |
| 1019 | return number_of_ssa_values_; |
| 1020 | } |
| 1021 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 1022 | static constexpr const char* kLivenessPassName = "liveness"; |
| 1023 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1024 | private: |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 1025 | // Linearize the graph so that: |
| 1026 | // (1): a block is always after its dominator, |
| 1027 | // (2): blocks of loops are contiguous. |
| 1028 | // This creates a natural and efficient ordering when visualizing live ranges. |
| 1029 | void LinearizeGraph(); |
| 1030 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1031 | // Give an SSA number to each instruction that defines a value used by another instruction, |
| 1032 | // and setup the lifetime information of each instruction and block. |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1033 | void NumberInstructions(); |
| 1034 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1035 | // Compute live ranges of instructions, as well as live_in, live_out and kill sets. |
| 1036 | void ComputeLiveness(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1037 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1038 | // Compute the live ranges of instructions, as well as the initial live_in, live_out and |
| 1039 | // kill sets, that do not take into account backward branches. |
| 1040 | void ComputeLiveRanges(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1041 | |
| 1042 | // After computing the initial sets, this method does a fixed point |
| 1043 | // calculation over the live_in and live_out set to take into account |
| 1044 | // backwards branches. |
| 1045 | void ComputeLiveInAndLiveOutSets(); |
| 1046 | |
| 1047 | // Update the live_in set of the block and returns whether it has changed. |
| 1048 | bool UpdateLiveIn(const HBasicBlock& block); |
| 1049 | |
| 1050 | // Update the live_out set of the block and returns whether it has changed. |
| 1051 | bool UpdateLiveOut(const HBasicBlock& block); |
| 1052 | |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 1053 | static bool ShouldBeLiveForEnvironment(HInstruction* instruction) { |
| 1054 | if (instruction == nullptr) return false; |
| 1055 | if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true; |
| 1056 | return instruction->GetType() == Primitive::kPrimNot; |
| 1057 | } |
| 1058 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 1059 | HGraph* const graph_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1060 | CodeGenerator* const codegen_; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1061 | GrowableArray<BlockInfo*> block_infos_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1062 | |
| 1063 | // Temporary array used when computing live_in, live_out, and kill sets. |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1064 | GrowableArray<HInstruction*> instructions_from_ssa_index_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1065 | |
| 1066 | // Temporary array used when inserting moves in the graph. |
| 1067 | GrowableArray<HInstruction*> instructions_from_lifetime_position_; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1068 | size_t number_of_ssa_values_; |
| 1069 | |
| 1070 | DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis); |
| 1071 | }; |
| 1072 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1073 | } // namespace art |
| 1074 | |
| 1075 | #endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_ |