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" |
| 21 | |
| 22 | namespace art { |
| 23 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 24 | class CodeGenerator; |
| 25 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 26 | static constexpr int kNoRegister = -1; |
| 27 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 28 | class BlockInfo : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 29 | public: |
| 30 | BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values) |
| 31 | : block_(block), |
| 32 | live_in_(allocator, number_of_ssa_values, false), |
| 33 | live_out_(allocator, number_of_ssa_values, false), |
| 34 | kill_(allocator, number_of_ssa_values, false) { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 35 | UNUSED(block_); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 36 | live_in_.ClearAllBits(); |
| 37 | live_out_.ClearAllBits(); |
| 38 | kill_.ClearAllBits(); |
| 39 | } |
| 40 | |
| 41 | private: |
| 42 | const HBasicBlock& block_; |
| 43 | ArenaBitVector live_in_; |
| 44 | ArenaBitVector live_out_; |
| 45 | ArenaBitVector kill_; |
| 46 | |
| 47 | friend class SsaLivenessAnalysis; |
| 48 | |
| 49 | DISALLOW_COPY_AND_ASSIGN(BlockInfo); |
| 50 | }; |
| 51 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 52 | /** |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 53 | * 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] | 54 | * is live. |
| 55 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 56 | class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 57 | public: |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 58 | 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] | 59 | DCHECK_LT(start, end); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 60 | DCHECK(next_ == nullptr || next_->GetStart() > GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | size_t GetStart() const { return start_; } |
| 64 | size_t GetEnd() const { return end_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 65 | LiveRange* GetNext() const { return next_; } |
| 66 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 67 | bool IntersectsWith(const LiveRange& other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 68 | return (start_ >= other.start_ && start_ < other.end_) |
| 69 | || (other.start_ >= start_ && other.start_ < end_); |
| 70 | } |
| 71 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 72 | bool IsBefore(const LiveRange& other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 73 | return end_ <= other.start_; |
| 74 | } |
| 75 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 76 | void Dump(std::ostream& stream) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 77 | stream << "[" << start_ << ", " << end_ << ")"; |
| 78 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 79 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 80 | LiveRange* Dup(ArenaAllocator* allocator) const { |
| 81 | return new (allocator) LiveRange( |
| 82 | start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator)); |
| 83 | } |
| 84 | |
| 85 | LiveRange* GetLastRange() { |
| 86 | return next_ == nullptr ? this : next_->GetLastRange(); |
| 87 | } |
| 88 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 89 | private: |
| 90 | size_t start_; |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 91 | size_t end_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 92 | LiveRange* next_; |
| 93 | |
| 94 | friend class LiveInterval; |
| 95 | |
| 96 | DISALLOW_COPY_AND_ASSIGN(LiveRange); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 97 | }; |
| 98 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 99 | /** |
| 100 | * A use position represents a live interval use at a given position. |
| 101 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 102 | class UsePosition : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 103 | public: |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 104 | UsePosition(HInstruction* user, |
| 105 | size_t input_index, |
| 106 | bool is_environment, |
| 107 | size_t position, |
| 108 | UsePosition* next) |
| 109 | : user_(user), |
| 110 | input_index_(input_index), |
| 111 | is_environment_(is_environment), |
| 112 | position_(position), |
| 113 | next_(next) { |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 114 | DCHECK(user->IsPhi() |
| 115 | || (GetPosition() == user->GetLifetimePosition() + 1) |
| 116 | || (GetPosition() == user->GetLifetimePosition())); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 117 | DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition()); |
| 118 | } |
| 119 | |
| 120 | size_t GetPosition() const { return position_; } |
| 121 | |
| 122 | UsePosition* GetNext() const { return next_; } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 123 | void SetNext(UsePosition* next) { next_ = next; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 124 | |
| 125 | HInstruction* GetUser() const { return user_; } |
| 126 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 127 | bool GetIsEnvironment() const { return is_environment_; } |
| 128 | |
| 129 | size_t GetInputIndex() const { return input_index_; } |
| 130 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 131 | void Dump(std::ostream& stream) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 132 | stream << position_; |
| 133 | } |
| 134 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 135 | UsePosition* Dup(ArenaAllocator* allocator) const { |
| 136 | return new (allocator) UsePosition( |
| 137 | user_, input_index_, is_environment_, position_, |
| 138 | next_ == nullptr ? nullptr : next_->Dup(allocator)); |
| 139 | } |
| 140 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 141 | private: |
| 142 | HInstruction* const user_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 143 | const size_t input_index_; |
| 144 | const bool is_environment_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 145 | const size_t position_; |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 146 | UsePosition* next_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 147 | |
| 148 | DISALLOW_COPY_AND_ASSIGN(UsePosition); |
| 149 | }; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * An interval is a list of disjoint live ranges where an instruction is live. |
| 153 | * Each instruction that has uses gets an interval. |
| 154 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 155 | class LiveInterval : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 156 | public: |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 157 | static LiveInterval* MakeInterval(ArenaAllocator* allocator, |
| 158 | Primitive::Type type, |
| 159 | HInstruction* instruction = nullptr) { |
| 160 | return new (allocator) LiveInterval(allocator, type, instruction); |
| 161 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 162 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 163 | static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) { |
| 164 | return new (allocator) LiveInterval( |
| 165 | allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true); |
| 166 | } |
| 167 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 168 | static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 169 | return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false); |
| 170 | } |
| 171 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 172 | static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) { |
| 173 | return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | bool IsFixed() const { return is_fixed_; } |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 177 | bool IsTemp() const { return is_temp_; } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 178 | bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; } |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 179 | // This interval is the result of a split. |
| 180 | bool IsSplit() const { return parent_ != this; } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 181 | |
| 182 | void AddUse(HInstruction* instruction, size_t input_index, bool is_environment) { |
| 183 | // Set the use within the instruction. |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 184 | size_t position = instruction->GetLifetimePosition(); |
| 185 | if (instruction->GetLocations()->InputOverlapsWithOutputOrTemp(input_index, is_environment)) { |
| 186 | // If it overlaps, we need to make sure the user will not try to allocate a temp |
| 187 | // or its output to the same register. |
| 188 | ++position; |
| 189 | } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 190 | if ((first_use_ != nullptr) |
| 191 | && (first_use_->GetUser() == instruction) |
| 192 | && (first_use_->GetPosition() < position)) { |
| 193 | // The user uses the instruction multiple times, and one use dies before the other. |
| 194 | // We update the use list so that the latter is first. |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 195 | UsePosition* cursor = first_use_; |
| 196 | while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) { |
| 197 | cursor = cursor->GetNext(); |
| 198 | } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 199 | DCHECK(first_use_->GetPosition() + 1 == position); |
| 200 | UsePosition* new_use = new (allocator_) UsePosition( |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 201 | instruction, input_index, is_environment, position, cursor->GetNext()); |
| 202 | cursor->SetNext(new_use); |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 203 | if (first_range_->GetEnd() == first_use_->GetPosition()) { |
| 204 | first_range_->end_ = position; |
| 205 | } |
| 206 | return; |
| 207 | } |
| 208 | |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 209 | size_t start_block_position = instruction->GetBlock()->GetLifetimeStart(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 210 | if (first_range_ == nullptr) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 211 | // First time we see a use of that interval. |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 212 | first_range_ = last_range_ = new (allocator_) LiveRange( |
| 213 | start_block_position, position, nullptr); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 214 | } else if (first_range_->GetStart() == start_block_position) { |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 215 | // There is a use later in the same block or in a following block. |
| 216 | // Note that in such a case, `AddRange` for the whole blocks has been called |
| 217 | // before arriving in this method, and this is the reason the start of |
| 218 | // `first_range_` is before the given `position`. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 219 | DCHECK_LE(position, first_range_->GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 220 | } else { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 221 | DCHECK(first_range_->GetStart() > position); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 222 | // There is a hole in the interval. Create a new range. |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 223 | // Note that the start of `first_range_` can be equal to `end`: two blocks |
| 224 | // having adjacent lifetime positions are not necessarily |
| 225 | // predecessor/successor. When two blocks are predecessor/successor, the |
| 226 | // liveness algorithm has called `AddRange` before arriving in this method, |
| 227 | // and the check line 205 would succeed. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 228 | first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 229 | } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 230 | first_use_ = new (allocator_) UsePosition( |
| 231 | instruction, input_index, is_environment, position, first_use_); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 232 | } |
| 233 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 234 | void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) { |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 235 | DCHECK(instruction->IsPhi()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 236 | first_use_ = new (allocator_) UsePosition( |
| 237 | instruction, input_index, false, block->GetLifetimeEnd(), first_use_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void AddRange(size_t start, size_t end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 241 | if (first_range_ == nullptr) { |
| 242 | first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_); |
| 243 | } else if (first_range_->GetStart() == end) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 244 | // There is a use in the following block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 245 | first_range_->start_ = start; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 246 | } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) { |
| 247 | DCHECK(is_fixed_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 248 | } else { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 249 | DCHECK_GT(first_range_->GetStart(), end); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 250 | // There is a hole in the interval. Create a new range. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 251 | first_range_ = new (allocator_) LiveRange(start, end, first_range_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | void AddLoopRange(size_t start, size_t end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 256 | DCHECK(first_range_ != nullptr); |
| 257 | while (first_range_ != nullptr && first_range_->GetEnd() < end) { |
| 258 | DCHECK_LE(start, first_range_->GetStart()); |
| 259 | first_range_ = first_range_->GetNext(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 260 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 261 | if (first_range_ == nullptr) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 262 | // Uses are only in the loop. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 263 | first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 264 | } else { |
| 265 | // There are uses after the loop. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 266 | first_range_->start_ = start; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 270 | bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 271 | void SetSpillSlot(int slot) { |
| 272 | DCHECK(!is_fixed_); |
| 273 | DCHECK(!is_temp_); |
| 274 | spill_slot_ = slot; |
| 275 | } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 276 | int GetSpillSlot() const { return spill_slot_; } |
| 277 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 278 | void SetFrom(size_t from) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 279 | if (first_range_ != nullptr) { |
| 280 | first_range_->start_ = from; |
| 281 | } else { |
| 282 | // Instruction without uses. |
| 283 | DCHECK(!defined_by_->HasUses()); |
| 284 | DCHECK(from == defined_by_->GetLifetimePosition()); |
| 285 | first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr); |
| 286 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 289 | LiveInterval* GetParent() const { return parent_; } |
| 290 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 291 | LiveRange* GetFirstRange() const { return first_range_; } |
| 292 | |
| 293 | int GetRegister() const { return register_; } |
| 294 | void SetRegister(int reg) { register_ = reg; } |
| 295 | void ClearRegister() { register_ = kNoRegister; } |
| 296 | bool HasRegister() const { return register_ != kNoRegister; } |
| 297 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 298 | bool IsDeadAt(size_t position) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 299 | return last_range_->GetEnd() <= position; |
| 300 | } |
| 301 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 302 | bool Covers(size_t position) const { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 303 | if (IsDeadAt(position)) { |
| 304 | return false; |
| 305 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 306 | LiveRange* current = first_range_; |
| 307 | while (current != nullptr) { |
| 308 | if (position >= current->GetStart() && position < current->GetEnd()) { |
| 309 | return true; |
| 310 | } |
| 311 | current = current->GetNext(); |
| 312 | } |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Returns the first intersection of this interval with `other`. |
| 318 | */ |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 319 | size_t FirstIntersectionWith(LiveInterval* other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 320 | // Advance both intervals and find the first matching range start in |
| 321 | // this interval. |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 322 | LiveRange* my_range = first_range_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 323 | LiveRange* other_range = other->first_range_; |
| 324 | do { |
| 325 | if (my_range->IntersectsWith(*other_range)) { |
| 326 | return std::max(my_range->GetStart(), other_range->GetStart()); |
| 327 | } else if (my_range->IsBefore(*other_range)) { |
| 328 | my_range = my_range->GetNext(); |
| 329 | if (my_range == nullptr) { |
| 330 | return kNoLifetime; |
| 331 | } |
| 332 | } else { |
| 333 | DCHECK(other_range->IsBefore(*my_range)); |
| 334 | other_range = other_range->GetNext(); |
| 335 | if (other_range == nullptr) { |
| 336 | return kNoLifetime; |
| 337 | } |
| 338 | } |
| 339 | } while (true); |
| 340 | } |
| 341 | |
| 342 | size_t GetStart() const { |
| 343 | return first_range_->GetStart(); |
| 344 | } |
| 345 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 346 | size_t GetEnd() const { |
| 347 | return last_range_->GetEnd(); |
| 348 | } |
| 349 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 350 | size_t FirstRegisterUseAfter(size_t position) const { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 351 | if (is_temp_) { |
| 352 | return position == GetStart() ? position : kNoLifetime; |
| 353 | } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 354 | if (position == GetStart() && defined_by_ != nullptr) { |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 355 | LocationSummary* locations = defined_by_->GetLocations(); |
| 356 | Location location = locations->Out(); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 357 | // This interval is the first interval of the instruction. If the output |
| 358 | // of the instruction requires a register, we return the position of that instruction |
| 359 | // as the first register use. |
| 360 | if (location.IsUnallocated()) { |
| 361 | if ((location.GetPolicy() == Location::kRequiresRegister) |
| 362 | || (location.GetPolicy() == Location::kSameAsFirstInput |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 363 | && locations->InAt(0).GetPolicy() == Location::kRequiresRegister)) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 364 | return position; |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 365 | } else if ((location.GetPolicy() == Location::kRequiresFpuRegister) |
| 366 | || (location.GetPolicy() == Location::kSameAsFirstInput |
| 367 | && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) { |
| 368 | return position; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 373 | UsePosition* use = first_use_; |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 374 | size_t end = GetEnd(); |
| 375 | while (use != nullptr && use->GetPosition() <= end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 376 | size_t use_position = use->GetPosition(); |
Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 377 | if (use_position > position && !use->GetIsEnvironment()) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 378 | Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex()); |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 379 | if (location.IsUnallocated() |
| 380 | && (location.GetPolicy() == Location::kRequiresRegister |
| 381 | || location.GetPolicy() == Location::kRequiresFpuRegister)) { |
Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 382 | return use_position; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 383 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 384 | } |
| 385 | use = use->GetNext(); |
| 386 | } |
| 387 | return kNoLifetime; |
| 388 | } |
| 389 | |
| 390 | size_t FirstRegisterUse() const { |
| 391 | return FirstRegisterUseAfter(GetStart()); |
| 392 | } |
| 393 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 394 | UsePosition* GetFirstUse() const { |
| 395 | return first_use_; |
| 396 | } |
| 397 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 398 | Primitive::Type GetType() const { |
| 399 | return type_; |
| 400 | } |
| 401 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 402 | HInstruction* GetDefinedBy() const { |
| 403 | return defined_by_; |
| 404 | } |
| 405 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 406 | /** |
| 407 | * Split this interval at `position`. This interval is changed to: |
| 408 | * [start ... position). |
| 409 | * |
| 410 | * The new interval covers: |
| 411 | * [position ... end) |
| 412 | */ |
| 413 | LiveInterval* SplitAt(size_t position) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 414 | DCHECK(!is_temp_); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 415 | DCHECK(!is_fixed_); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 416 | DCHECK_GT(position, GetStart()); |
| 417 | |
| 418 | if (last_range_->GetEnd() <= position) { |
| 419 | // This range dies before `position`, no need to split. |
| 420 | return nullptr; |
| 421 | } |
| 422 | |
| 423 | LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 424 | new_interval->next_sibling_ = next_sibling_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 425 | next_sibling_ = new_interval; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 426 | new_interval->parent_ = parent_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 427 | |
| 428 | new_interval->first_use_ = first_use_; |
| 429 | LiveRange* current = first_range_; |
| 430 | LiveRange* previous = nullptr; |
| 431 | // 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] | 432 | // 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] | 433 | do { |
| 434 | if (position >= current->GetEnd()) { |
| 435 | // Move to next range. |
| 436 | previous = current; |
| 437 | current = current->next_; |
| 438 | } else if (position <= current->GetStart()) { |
| 439 | // If the previous range did not cover this position, we know position is in |
| 440 | // a lifetime hole. We can just break the first_range_ and last_range_ links |
| 441 | // and return the new interval. |
| 442 | DCHECK(previous != nullptr); |
| 443 | DCHECK(current != first_range_); |
| 444 | new_interval->last_range_ = last_range_; |
| 445 | last_range_ = previous; |
| 446 | previous->next_ = nullptr; |
| 447 | new_interval->first_range_ = current; |
| 448 | return new_interval; |
| 449 | } else { |
| 450 | // This range covers position. We create a new last_range_ for this interval |
| 451 | // that covers last_range_->Start() and position. We also shorten the current |
| 452 | // range and make it the first range of the new interval. |
| 453 | DCHECK(position < current->GetEnd() && position > current->GetStart()); |
| 454 | new_interval->last_range_ = last_range_; |
| 455 | last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr); |
| 456 | if (previous != nullptr) { |
| 457 | previous->next_ = last_range_; |
| 458 | } else { |
| 459 | first_range_ = last_range_; |
| 460 | } |
| 461 | new_interval->first_range_ = current; |
| 462 | current->start_ = position; |
| 463 | return new_interval; |
| 464 | } |
| 465 | } while (current != nullptr); |
| 466 | |
| 467 | LOG(FATAL) << "Unreachable"; |
| 468 | return nullptr; |
| 469 | } |
| 470 | |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 471 | bool StartsBeforeOrAt(LiveInterval* other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 472 | return GetStart() <= other->GetStart(); |
| 473 | } |
| 474 | |
| 475 | bool StartsAfter(LiveInterval* other) const { |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 476 | return GetStart() > other->GetStart(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void Dump(std::ostream& stream) const { |
| 480 | stream << "ranges: { "; |
| 481 | LiveRange* current = first_range_; |
| 482 | do { |
| 483 | current->Dump(stream); |
| 484 | stream << " "; |
| 485 | } while ((current = current->GetNext()) != nullptr); |
| 486 | stream << "}, uses: { "; |
| 487 | UsePosition* use = first_use_; |
| 488 | if (use != nullptr) { |
| 489 | do { |
| 490 | use->Dump(stream); |
| 491 | stream << " "; |
| 492 | } while ((use = use->GetNext()) != nullptr); |
| 493 | } |
| 494 | stream << "}"; |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 495 | stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit(); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 496 | stream << " is_high: " << IsHighInterval(); |
| 497 | stream << " is_low: " << IsLowInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | LiveInterval* GetNextSibling() const { return next_sibling_; } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 501 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 502 | // Returns the first register hint that is at least free before |
| 503 | // the value contained in `free_until`. If none is found, returns |
| 504 | // `kNoRegister`. |
| 505 | int FindFirstRegisterHint(size_t* free_until) const; |
| 506 | |
| 507 | // If there is enough at the definition site to find a register (for example |
| 508 | // it uses the same input as the first input), returns the register as a hint. |
| 509 | // Returns kNoRegister otherwise. |
| 510 | int FindHintAtDefinition() const; |
| 511 | |
| 512 | // Returns whether the interval needs two (Dex virtual register size `kVRegSize`) |
| 513 | // slots for spilling. |
| 514 | bool NeedsTwoSpillSlots() const; |
| 515 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 516 | bool IsFloatingPoint() const { |
| 517 | return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble; |
| 518 | } |
| 519 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 520 | // Converts the location of the interval to a `Location` object. |
| 521 | Location ToLocation() const; |
| 522 | |
| 523 | // Returns the location of the interval following its siblings at `position`. |
| 524 | Location GetLocationAt(size_t position) const; |
| 525 | |
| 526 | // Finds the interval that covers `position`. |
| 527 | const LiveInterval& GetIntervalAt(size_t position) const; |
| 528 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 529 | // Returns whether `other` and `this` share the same kind of register. |
| 530 | bool SameRegisterKind(Location other) const; |
| 531 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 532 | bool HasHighInterval() const { |
| 533 | return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr); |
| 534 | } |
| 535 | |
| 536 | bool HasLowInterval() const { |
| 537 | return IsHighInterval(); |
| 538 | } |
| 539 | |
| 540 | LiveInterval* GetLowInterval() const { |
| 541 | DCHECK(HasLowInterval()); |
| 542 | return high_or_low_interval_; |
| 543 | } |
| 544 | |
| 545 | LiveInterval* GetHighInterval() const { |
| 546 | DCHECK(HasHighInterval()); |
| 547 | return high_or_low_interval_; |
| 548 | } |
| 549 | |
| 550 | bool IsHighInterval() const { |
| 551 | return GetParent()->is_high_interval_; |
| 552 | } |
| 553 | |
| 554 | bool IsLowInterval() const { |
| 555 | return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr); |
| 556 | } |
| 557 | |
| 558 | void SetLowInterval(LiveInterval* low) { |
| 559 | DCHECK(IsHighInterval()); |
| 560 | high_or_low_interval_ = low; |
| 561 | } |
| 562 | |
| 563 | void SetHighInterval(LiveInterval* high) { |
| 564 | DCHECK(IsLowInterval()); |
| 565 | high_or_low_interval_ = high; |
| 566 | } |
| 567 | |
| 568 | void AddHighInterval(bool is_temp = false) { |
| 569 | DCHECK_EQ(GetParent(), this); |
| 570 | DCHECK(!HasHighInterval()); |
| 571 | DCHECK(!HasLowInterval()); |
| 572 | high_or_low_interval_ = new (allocator_) LiveInterval( |
| 573 | allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true); |
| 574 | high_or_low_interval_->high_or_low_interval_ = this; |
| 575 | if (first_range_ != nullptr) { |
| 576 | high_or_low_interval_->first_range_ = first_range_->Dup(allocator_); |
| 577 | high_or_low_interval_->last_range_ = first_range_->GetLastRange(); |
| 578 | } |
| 579 | if (first_use_ != nullptr) { |
| 580 | high_or_low_interval_->first_use_ = first_use_->Dup(allocator_); |
| 581 | } |
| 582 | } |
| 583 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 584 | private: |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 585 | LiveInterval(ArenaAllocator* allocator, |
| 586 | Primitive::Type type, |
| 587 | HInstruction* defined_by = nullptr, |
| 588 | bool is_fixed = false, |
| 589 | int reg = kNoRegister, |
| 590 | bool is_temp = false, |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 591 | bool is_slow_path_safepoint = false, |
| 592 | bool is_high_interval = false) |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 593 | : allocator_(allocator), |
| 594 | first_range_(nullptr), |
| 595 | last_range_(nullptr), |
| 596 | first_use_(nullptr), |
| 597 | type_(type), |
| 598 | next_sibling_(nullptr), |
| 599 | parent_(this), |
| 600 | register_(reg), |
| 601 | spill_slot_(kNoSpillSlot), |
| 602 | is_fixed_(is_fixed), |
| 603 | is_temp_(is_temp), |
| 604 | is_slow_path_safepoint_(is_slow_path_safepoint), |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 605 | is_high_interval_(is_high_interval), |
| 606 | high_or_low_interval_(nullptr), |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 607 | defined_by_(defined_by) {} |
| 608 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 609 | ArenaAllocator* const allocator_; |
| 610 | |
| 611 | // Ranges of this interval. We need a quick access to the last range to test |
| 612 | // for liveness (see `IsDeadAt`). |
| 613 | LiveRange* first_range_; |
| 614 | LiveRange* last_range_; |
| 615 | |
| 616 | // Uses of this interval. Note that this linked list is shared amongst siblings. |
| 617 | UsePosition* first_use_; |
| 618 | |
| 619 | // The instruction type this interval corresponds to. |
| 620 | const Primitive::Type type_; |
| 621 | |
| 622 | // Live interval that is the result of a split. |
| 623 | LiveInterval* next_sibling_; |
| 624 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 625 | // The first interval from which split intervals come from. |
| 626 | LiveInterval* parent_; |
| 627 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 628 | // The register allocated to this interval. |
| 629 | int register_; |
| 630 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 631 | // The spill slot allocated to this interval. |
| 632 | int spill_slot_; |
| 633 | |
| 634 | // Whether the interval is for a fixed register. |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 635 | const bool is_fixed_; |
| 636 | |
| 637 | // Whether the interval is for a temporary. |
| 638 | const bool is_temp_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 639 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 640 | // Whether the interval is for a safepoint that calls on slow path. |
| 641 | const bool is_slow_path_safepoint_; |
| 642 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 643 | // Whether this interval is a synthesized interval for register pair. |
| 644 | const bool is_high_interval_; |
| 645 | |
| 646 | // If this interval needs a register pair, the high or low equivalent. |
| 647 | // `is_high_interval_` tells whether this holds the low or the high. |
| 648 | LiveInterval* high_or_low_interval_; |
| 649 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 650 | // The instruction represented by this interval. |
| 651 | HInstruction* const defined_by_; |
| 652 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 653 | static constexpr int kNoRegister = -1; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 654 | static constexpr int kNoSpillSlot = -1; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 655 | |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 656 | ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive); |
| 657 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 658 | DISALLOW_COPY_AND_ASSIGN(LiveInterval); |
| 659 | }; |
| 660 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 661 | class SsaLivenessAnalysis : public ValueObject { |
| 662 | public: |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 663 | SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 664 | : graph_(graph), |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 665 | codegen_(codegen), |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 666 | linear_order_(graph.GetArena(), graph.GetBlocks().Size()), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 667 | block_infos_(graph.GetArena(), graph.GetBlocks().Size()), |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 668 | instructions_from_ssa_index_(graph.GetArena(), 0), |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 669 | instructions_from_lifetime_position_(graph.GetArena(), 0), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 670 | number_of_ssa_values_(0) { |
| 671 | block_infos_.SetSize(graph.GetBlocks().Size()); |
| 672 | } |
| 673 | |
| 674 | void Analyze(); |
| 675 | |
| 676 | BitVector* GetLiveInSet(const HBasicBlock& block) const { |
| 677 | return &block_infos_.Get(block.GetBlockId())->live_in_; |
| 678 | } |
| 679 | |
| 680 | BitVector* GetLiveOutSet(const HBasicBlock& block) const { |
| 681 | return &block_infos_.Get(block.GetBlockId())->live_out_; |
| 682 | } |
| 683 | |
| 684 | BitVector* GetKillSet(const HBasicBlock& block) const { |
| 685 | return &block_infos_.Get(block.GetBlockId())->kill_; |
| 686 | } |
| 687 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 688 | const GrowableArray<HBasicBlock*>& GetLinearOrder() const { |
| 689 | return linear_order_; |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 690 | } |
| 691 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 692 | HInstruction* GetInstructionFromSsaIndex(size_t index) const { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 693 | return instructions_from_ssa_index_.Get(index); |
| 694 | } |
| 695 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 696 | HInstruction* GetInstructionFromPosition(size_t index) const { |
| 697 | return instructions_from_lifetime_position_.Get(index); |
| 698 | } |
| 699 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 700 | HInstruction* GetTempUser(LiveInterval* temp) const { |
| 701 | // A temporary shares the same lifetime start as the instruction that requires it. |
| 702 | DCHECK(temp->IsTemp()); |
| 703 | return GetInstructionFromPosition(temp->GetStart() / 2); |
| 704 | } |
| 705 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 706 | size_t GetMaxLifetimePosition() const { |
| 707 | return instructions_from_lifetime_position_.Size() * 2 - 1; |
| 708 | } |
| 709 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 710 | size_t GetNumberOfSsaValues() const { |
| 711 | return number_of_ssa_values_; |
| 712 | } |
| 713 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 714 | private: |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 715 | // Linearize the graph so that: |
| 716 | // (1): a block is always after its dominator, |
| 717 | // (2): blocks of loops are contiguous. |
| 718 | // This creates a natural and efficient ordering when visualizing live ranges. |
| 719 | void LinearizeGraph(); |
| 720 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 721 | // Give an SSA number to each instruction that defines a value used by another instruction, |
| 722 | // and setup the lifetime information of each instruction and block. |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 723 | void NumberInstructions(); |
| 724 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 725 | // Compute live ranges of instructions, as well as live_in, live_out and kill sets. |
| 726 | void ComputeLiveness(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 727 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 728 | // Compute the live ranges of instructions, as well as the initial live_in, live_out and |
| 729 | // kill sets, that do not take into account backward branches. |
| 730 | void ComputeLiveRanges(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 731 | |
| 732 | // After computing the initial sets, this method does a fixed point |
| 733 | // calculation over the live_in and live_out set to take into account |
| 734 | // backwards branches. |
| 735 | void ComputeLiveInAndLiveOutSets(); |
| 736 | |
| 737 | // Update the live_in set of the block and returns whether it has changed. |
| 738 | bool UpdateLiveIn(const HBasicBlock& block); |
| 739 | |
| 740 | // Update the live_out set of the block and returns whether it has changed. |
| 741 | bool UpdateLiveOut(const HBasicBlock& block); |
| 742 | |
| 743 | const HGraph& graph_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 744 | CodeGenerator* const codegen_; |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 745 | GrowableArray<HBasicBlock*> linear_order_; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 746 | GrowableArray<BlockInfo*> block_infos_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 747 | |
| 748 | // Temporary array used when computing live_in, live_out, and kill sets. |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 749 | GrowableArray<HInstruction*> instructions_from_ssa_index_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 750 | |
| 751 | // Temporary array used when inserting moves in the graph. |
| 752 | GrowableArray<HInstruction*> instructions_from_lifetime_position_; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 753 | size_t number_of_ssa_values_; |
| 754 | |
| 755 | DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis); |
| 756 | }; |
| 757 | |
Nicolas Geoffray | e50fa58 | 2014-11-24 17:44:15 +0000 | [diff] [blame] | 758 | class HLinearPostOrderIterator : public ValueObject { |
| 759 | public: |
| 760 | explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness) |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 761 | : order_(liveness.GetLinearOrder()), index_(liveness.GetLinearOrder().Size()) {} |
Nicolas Geoffray | e50fa58 | 2014-11-24 17:44:15 +0000 | [diff] [blame] | 762 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 763 | bool Done() const { return index_ == 0; } |
| 764 | |
| 765 | HBasicBlock* Current() const { return order_.Get(index_ -1); } |
| 766 | |
| 767 | void Advance() { |
| 768 | --index_; |
| 769 | DCHECK_GE(index_, 0U); |
| 770 | } |
Nicolas Geoffray | e50fa58 | 2014-11-24 17:44:15 +0000 | [diff] [blame] | 771 | |
| 772 | private: |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 773 | const GrowableArray<HBasicBlock*>& order_; |
Nicolas Geoffray | e50fa58 | 2014-11-24 17:44:15 +0000 | [diff] [blame] | 774 | size_t index_; |
| 775 | |
| 776 | DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator); |
| 777 | }; |
| 778 | |
Nicolas Geoffray | a8eed3a | 2014-11-24 17:47:10 +0000 | [diff] [blame] | 779 | class HLinearOrderIterator : public ValueObject { |
| 780 | public: |
| 781 | explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness) |
| 782 | : order_(liveness.GetLinearOrder()), index_(0) {} |
| 783 | |
| 784 | bool Done() const { return index_ == order_.Size(); } |
| 785 | HBasicBlock* Current() const { return order_.Get(index_); } |
| 786 | void Advance() { ++index_; } |
| 787 | |
| 788 | private: |
| 789 | const GrowableArray<HBasicBlock*>& order_; |
| 790 | size_t index_; |
| 791 | |
| 792 | DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator); |
| 793 | }; |
| 794 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 795 | } // namespace art |
| 796 | |
| 797 | #endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_ |