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