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