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