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