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