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