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