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