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" |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 21 | #include <iostream> |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 22 | |
| 23 | namespace art { |
| 24 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 25 | class CodeGenerator; |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 26 | class SsaLivenessAnalysis; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 27 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 28 | static constexpr int kNoRegister = -1; |
| 29 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 30 | class BlockInfo : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 31 | public: |
| 32 | BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values) |
| 33 | : block_(block), |
| 34 | live_in_(allocator, number_of_ssa_values, false), |
| 35 | live_out_(allocator, number_of_ssa_values, false), |
| 36 | kill_(allocator, number_of_ssa_values, false) { |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 37 | UNUSED(block_); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 38 | live_in_.ClearAllBits(); |
| 39 | live_out_.ClearAllBits(); |
| 40 | kill_.ClearAllBits(); |
| 41 | } |
| 42 | |
| 43 | private: |
| 44 | const HBasicBlock& block_; |
| 45 | ArenaBitVector live_in_; |
| 46 | ArenaBitVector live_out_; |
| 47 | ArenaBitVector kill_; |
| 48 | |
| 49 | friend class SsaLivenessAnalysis; |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(BlockInfo); |
| 52 | }; |
| 53 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 54 | /** |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 55 | * 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] | 56 | * is live. |
| 57 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 58 | class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 59 | public: |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 60 | 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] | 61 | DCHECK_LT(start, end); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 62 | DCHECK(next_ == nullptr || next_->GetStart() > GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | size_t GetStart() const { return start_; } |
| 66 | size_t GetEnd() const { return end_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 67 | LiveRange* GetNext() const { return next_; } |
| 68 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 69 | bool IntersectsWith(const LiveRange& other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 70 | return (start_ >= other.start_ && start_ < other.end_) |
| 71 | || (other.start_ >= start_ && other.start_ < end_); |
| 72 | } |
| 73 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 74 | bool IsBefore(const LiveRange& other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 75 | return end_ <= other.start_; |
| 76 | } |
| 77 | |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 78 | void Dump(std::ostream& stream) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 79 | stream << "[" << start_ << ", " << end_ << ")"; |
| 80 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 81 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 82 | LiveRange* Dup(ArenaAllocator* allocator) const { |
| 83 | return new (allocator) LiveRange( |
| 84 | start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator)); |
| 85 | } |
| 86 | |
| 87 | LiveRange* GetLastRange() { |
| 88 | return next_ == nullptr ? this : next_->GetLastRange(); |
| 89 | } |
| 90 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 91 | private: |
| 92 | size_t start_; |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 93 | size_t end_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 94 | LiveRange* next_; |
| 95 | |
| 96 | friend class LiveInterval; |
| 97 | |
| 98 | DISALLOW_COPY_AND_ASSIGN(LiveRange); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 101 | /** |
| 102 | * A use position represents a live interval use at a given position. |
| 103 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 104 | class UsePosition : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 105 | public: |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 106 | UsePosition(HInstruction* user, |
| 107 | size_t input_index, |
| 108 | bool is_environment, |
| 109 | size_t position, |
| 110 | UsePosition* next) |
| 111 | : user_(user), |
| 112 | input_index_(input_index), |
| 113 | is_environment_(is_environment), |
| 114 | position_(position), |
| 115 | next_(next) { |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 116 | DCHECK((user == nullptr) |
| 117 | || user->IsPhi() |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 118 | || (GetPosition() == user->GetLifetimePosition() + 1) |
| 119 | || (GetPosition() == user->GetLifetimePosition())); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 120 | DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition()); |
| 121 | } |
| 122 | |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 123 | static constexpr size_t kNoInput = -1; |
| 124 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 125 | size_t GetPosition() const { return position_; } |
| 126 | |
| 127 | UsePosition* GetNext() const { return next_; } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 128 | void SetNext(UsePosition* next) { next_ = next; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 129 | |
| 130 | HInstruction* GetUser() const { return user_; } |
| 131 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 132 | bool GetIsEnvironment() const { return is_environment_; } |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 133 | bool IsSynthesized() const { return user_ == nullptr; } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 134 | |
| 135 | size_t GetInputIndex() const { return input_index_; } |
| 136 | |
Nicolas Geoffray | ec7e472 | 2014-06-06 11:24:33 +0100 | [diff] [blame] | 137 | void Dump(std::ostream& stream) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 138 | stream << position_; |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | HLoopInformation* GetLoopInformation() const { |
| 142 | return user_->GetBlock()->GetLoopInformation(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 145 | UsePosition* Dup(ArenaAllocator* allocator) const { |
| 146 | return new (allocator) UsePosition( |
| 147 | user_, input_index_, is_environment_, position_, |
| 148 | next_ == nullptr ? nullptr : next_->Dup(allocator)); |
| 149 | } |
| 150 | |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 151 | bool RequiresRegister() const { |
| 152 | if (GetIsEnvironment()) return false; |
| 153 | if (IsSynthesized()) return false; |
| 154 | Location location = GetUser()->GetLocations()->InAt(GetInputIndex()); |
| 155 | return location.IsUnallocated() |
| 156 | && (location.GetPolicy() == Location::kRequiresRegister |
| 157 | || location.GetPolicy() == Location::kRequiresFpuRegister); |
| 158 | } |
| 159 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 160 | private: |
| 161 | HInstruction* const user_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 162 | const size_t input_index_; |
| 163 | const bool is_environment_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 164 | const size_t position_; |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 165 | UsePosition* next_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 166 | |
| 167 | DISALLOW_COPY_AND_ASSIGN(UsePosition); |
| 168 | }; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 169 | |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 170 | class SafepointPosition : public ArenaObject<kArenaAllocMisc> { |
| 171 | public: |
| 172 | explicit SafepointPosition(HInstruction* instruction) |
| 173 | : instruction_(instruction), |
| 174 | next_(nullptr) {} |
| 175 | |
| 176 | void SetNext(SafepointPosition* next) { |
| 177 | next_ = next; |
| 178 | } |
| 179 | |
| 180 | size_t GetPosition() const { |
| 181 | return instruction_->GetLifetimePosition(); |
| 182 | } |
| 183 | |
| 184 | SafepointPosition* GetNext() const { |
| 185 | return next_; |
| 186 | } |
| 187 | |
| 188 | LocationSummary* GetLocations() const { |
| 189 | return instruction_->GetLocations(); |
| 190 | } |
| 191 | |
| 192 | HInstruction* GetInstruction() const { |
| 193 | return instruction_; |
| 194 | } |
| 195 | |
| 196 | private: |
| 197 | HInstruction* const instruction_; |
| 198 | SafepointPosition* next_; |
| 199 | |
| 200 | DISALLOW_COPY_AND_ASSIGN(SafepointPosition); |
| 201 | }; |
| 202 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 203 | /** |
| 204 | * An interval is a list of disjoint live ranges where an instruction is live. |
| 205 | * Each instruction that has uses gets an interval. |
| 206 | */ |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 207 | class LiveInterval : public ArenaObject<kArenaAllocMisc> { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 208 | public: |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 209 | static LiveInterval* MakeInterval(ArenaAllocator* allocator, |
| 210 | Primitive::Type type, |
| 211 | HInstruction* instruction = nullptr) { |
| 212 | return new (allocator) LiveInterval(allocator, type, instruction); |
| 213 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 214 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 215 | static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) { |
| 216 | return new (allocator) LiveInterval( |
| 217 | allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true); |
| 218 | } |
| 219 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 220 | static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 221 | return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false); |
| 222 | } |
| 223 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 224 | static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) { |
| 225 | return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | bool IsFixed() const { return is_fixed_; } |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 229 | bool IsTemp() const { return is_temp_; } |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 230 | bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; } |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 231 | // This interval is the result of a split. |
| 232 | bool IsSplit() const { return parent_ != this; } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 233 | |
Nicolas Geoffray | f01d344 | 2015-03-27 17:15:49 +0000 | [diff] [blame] | 234 | void AddTempUse(HInstruction* instruction, size_t temp_index) { |
| 235 | DCHECK(IsTemp()); |
| 236 | DCHECK(first_use_ == nullptr) << "A temporary can only have one user"; |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 237 | DCHECK(first_env_use_ == nullptr) << "A temporary cannot have environment user"; |
Nicolas Geoffray | f01d344 | 2015-03-27 17:15:49 +0000 | [diff] [blame] | 238 | size_t position = instruction->GetLifetimePosition(); |
| 239 | first_use_ = new (allocator_) UsePosition( |
| 240 | instruction, temp_index, /* is_environment */ false, position, first_use_); |
| 241 | AddRange(position, position + 1); |
| 242 | } |
| 243 | |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 244 | void AddUse(HInstruction* instruction, |
| 245 | size_t input_index, |
| 246 | bool is_environment, |
| 247 | bool keep_alive = false) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 248 | // Set the use within the instruction. |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 249 | size_t position = instruction->GetLifetimePosition() + 1; |
| 250 | LocationSummary* locations = instruction->GetLocations(); |
| 251 | if (!is_environment) { |
| 252 | if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) { |
| 253 | // For fixed inputs and output same as input, the register allocator |
| 254 | // requires to have inputs die at the instruction, so that input moves use the |
| 255 | // location of the input just before that instruction (and not potential moves due |
| 256 | // to splitting). |
| 257 | position = instruction->GetLifetimePosition(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 258 | } else if (!locations->InAt(input_index).IsValid()) { |
| 259 | return; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 260 | } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 261 | } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 262 | |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 263 | if (!is_environment && instruction->IsInLoop()) { |
| 264 | AddBackEdgeUses(*instruction->GetBlock()); |
| 265 | } |
| 266 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 267 | DCHECK(position == instruction->GetLifetimePosition() |
| 268 | || position == instruction->GetLifetimePosition() + 1); |
| 269 | |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 270 | if ((first_use_ != nullptr) |
| 271 | && (first_use_->GetUser() == instruction) |
| 272 | && (first_use_->GetPosition() < position)) { |
| 273 | // The user uses the instruction multiple times, and one use dies before the other. |
| 274 | // We update the use list so that the latter is first. |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 275 | DCHECK(!is_environment); |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 276 | UsePosition* cursor = first_use_; |
| 277 | while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) { |
| 278 | cursor = cursor->GetNext(); |
| 279 | } |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 280 | DCHECK(first_use_->GetPosition() + 1 == position); |
| 281 | UsePosition* new_use = new (allocator_) UsePosition( |
Nicolas Geoffray | 8e3964b | 2014-10-17 11:06:38 +0100 | [diff] [blame] | 282 | instruction, input_index, is_environment, position, cursor->GetNext()); |
| 283 | cursor->SetNext(new_use); |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 284 | if (first_range_->GetEnd() == first_use_->GetPosition()) { |
| 285 | first_range_->end_ = position; |
| 286 | } |
| 287 | return; |
| 288 | } |
| 289 | |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 290 | if (is_environment) { |
| 291 | first_env_use_ = new (allocator_) UsePosition( |
| 292 | instruction, input_index, is_environment, position, first_env_use_); |
| 293 | } else { |
| 294 | first_use_ = new (allocator_) UsePosition( |
| 295 | instruction, input_index, is_environment, position, first_use_); |
| 296 | } |
Nicolas Geoffray | d8126be | 2015-03-27 10:22:41 +0000 | [diff] [blame] | 297 | |
| 298 | if (is_environment && !keep_alive) { |
| 299 | // If this environment use does not keep the instruction live, it does not |
| 300 | // affect the live range of that instruction. |
| 301 | return; |
| 302 | } |
| 303 | |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 304 | size_t start_block_position = instruction->GetBlock()->GetLifetimeStart(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 305 | if (first_range_ == nullptr) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 306 | // First time we see a use of that interval. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 307 | first_range_ = last_range_ = range_search_start_ = |
| 308 | new (allocator_) LiveRange(start_block_position, position, nullptr); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 309 | } else if (first_range_->GetStart() == start_block_position) { |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 310 | // There is a use later in the same block or in a following block. |
| 311 | // Note that in such a case, `AddRange` for the whole blocks has been called |
| 312 | // before arriving in this method, and this is the reason the start of |
| 313 | // `first_range_` is before the given `position`. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 314 | DCHECK_LE(position, first_range_->GetEnd()); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 315 | } else { |
Nicolas Geoffray | 86dbb9a | 2014-06-04 11:12:39 +0100 | [diff] [blame] | 316 | DCHECK(first_range_->GetStart() > position); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 317 | // There is a hole in the interval. Create a new range. |
Nicolas Geoffray | 8ddb00c | 2014-09-29 12:00:40 +0100 | [diff] [blame] | 318 | // Note that the start of `first_range_` can be equal to `end`: two blocks |
| 319 | // having adjacent lifetime positions are not necessarily |
| 320 | // predecessor/successor. When two blocks are predecessor/successor, the |
| 321 | // liveness algorithm has called `AddRange` before arriving in this method, |
| 322 | // and the check line 205 would succeed. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 323 | first_range_ = range_search_start_ = |
| 324 | new (allocator_) LiveRange(start_block_position, position, first_range_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 325 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 328 | void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) { |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 329 | DCHECK(instruction->IsPhi()); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 330 | if (block->IsInLoop()) { |
| 331 | AddBackEdgeUses(*block); |
| 332 | } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 333 | first_use_ = new (allocator_) UsePosition( |
| 334 | instruction, input_index, false, block->GetLifetimeEnd(), first_use_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void AddRange(size_t start, size_t end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 338 | if (first_range_ == nullptr) { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 339 | first_range_ = last_range_ = range_search_start_ = |
| 340 | new (allocator_) LiveRange(start, end, first_range_); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 341 | } else if (first_range_->GetStart() == end) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 342 | // There is a use in the following block. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 343 | first_range_->start_ = start; |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 344 | } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) { |
| 345 | DCHECK(is_fixed_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 346 | } else { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 347 | DCHECK_GT(first_range_->GetStart(), end); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 348 | // There is a hole in the interval. Create a new range. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 349 | first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | void AddLoopRange(size_t start, size_t end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 354 | DCHECK(first_range_ != nullptr); |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 355 | DCHECK_LE(start, first_range_->GetStart()); |
| 356 | // Find the range that covers the positions after the loop. |
| 357 | LiveRange* after_loop = first_range_; |
| 358 | LiveRange* last_in_loop = nullptr; |
| 359 | while (after_loop != nullptr && after_loop->GetEnd() < end) { |
| 360 | DCHECK_LE(start, after_loop->GetStart()); |
| 361 | last_in_loop = after_loop; |
| 362 | after_loop = after_loop->GetNext(); |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 363 | } |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 364 | if (after_loop == nullptr) { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 365 | // Uses are only in the loop. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 366 | first_range_ = last_range_ = range_search_start_ = |
| 367 | new (allocator_) LiveRange(start, end, nullptr); |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 368 | } else if (after_loop->GetStart() <= end) { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 369 | first_range_ = range_search_start_ = after_loop; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 370 | // There are uses after the loop. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 371 | first_range_->start_ = start; |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 372 | } else { |
| 373 | // The use after the loop is after a lifetime hole. |
| 374 | DCHECK(last_in_loop != nullptr); |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 375 | first_range_ = range_search_start_ = last_in_loop; |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 376 | first_range_->start_ = start; |
| 377 | first_range_->end_ = end; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 381 | bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; } |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 382 | void SetSpillSlot(int slot) { |
| 383 | DCHECK(!is_fixed_); |
| 384 | DCHECK(!is_temp_); |
| 385 | spill_slot_ = slot; |
| 386 | } |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 387 | int GetSpillSlot() const { return spill_slot_; } |
| 388 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 389 | void SetFrom(size_t from) { |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 390 | if (first_range_ != nullptr) { |
| 391 | first_range_->start_ = from; |
| 392 | } else { |
| 393 | // Instruction without uses. |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 394 | DCHECK(!defined_by_->HasNonEnvironmentUses()); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 395 | DCHECK(from == defined_by_->GetLifetimePosition()); |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 396 | first_range_ = last_range_ = range_search_start_ = |
| 397 | new (allocator_) LiveRange(from, from + 2, nullptr); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 398 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 399 | } |
| 400 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 401 | LiveInterval* GetParent() const { return parent_; } |
| 402 | |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 403 | // Returns whether this interval is the parent interval, that is, the interval |
| 404 | // that starts where the HInstruction is defined. |
| 405 | bool IsParent() const { return parent_ == this; } |
| 406 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 407 | LiveRange* GetFirstRange() const { return first_range_; } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 408 | LiveRange* GetLastRange() const { return last_range_; } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 409 | |
| 410 | int GetRegister() const { return register_; } |
| 411 | void SetRegister(int reg) { register_ = reg; } |
| 412 | void ClearRegister() { register_ = kNoRegister; } |
| 413 | bool HasRegister() const { return register_ != kNoRegister; } |
| 414 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 415 | bool IsDeadAt(size_t position) const { |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 416 | return GetEnd() <= position; |
| 417 | } |
| 418 | |
| 419 | bool IsDefinedAt(size_t position) const { |
| 420 | return GetStart() <= position && !IsDeadAt(position); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 421 | } |
| 422 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 423 | // Returns true if the interval contains a LiveRange covering `position`. |
| 424 | // The range at or immediately after the current position of linear scan |
| 425 | // is cached for better performance. If `position` can be smaller than |
| 426 | // that, CoversSlow should be used instead. |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 427 | bool Covers(size_t position) { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 428 | LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_); |
| 429 | range_search_start_ = candidate; |
| 430 | return (candidate != nullptr && candidate->GetStart() <= position); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 431 | } |
| 432 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 433 | // Same as Covers but always tests all ranges. |
| 434 | bool CoversSlow(size_t position) const { |
| 435 | LiveRange* candidate = FindRangeAtOrAfter(position, first_range_); |
| 436 | return candidate != nullptr && candidate->GetStart() <= position; |
| 437 | } |
| 438 | |
| 439 | // Returns the first intersection of this interval with `current`, which |
| 440 | // must be the interval currently being allocated by linear scan. |
| 441 | size_t FirstIntersectionWith(LiveInterval* current) const { |
| 442 | // Find the first range after the start of `current`. We use the search |
| 443 | // cache to improve performance. |
| 444 | DCHECK(GetStart() <= current->GetStart() || IsFixed()); |
| 445 | LiveRange* other_range = current->first_range_; |
| 446 | LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_); |
| 447 | if (my_range == nullptr) { |
| 448 | return kNoLifetime; |
| 449 | } |
| 450 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 451 | // Advance both intervals and find the first matching range start in |
| 452 | // this interval. |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 453 | do { |
David Brazdil | 714e14f | 2015-02-25 11:57:05 +0000 | [diff] [blame] | 454 | if (my_range->IsBefore(*other_range)) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 455 | my_range = my_range->GetNext(); |
| 456 | if (my_range == nullptr) { |
| 457 | return kNoLifetime; |
| 458 | } |
David Brazdil | 714e14f | 2015-02-25 11:57:05 +0000 | [diff] [blame] | 459 | } else if (other_range->IsBefore(*my_range)) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 460 | other_range = other_range->GetNext(); |
| 461 | if (other_range == nullptr) { |
| 462 | return kNoLifetime; |
| 463 | } |
David Brazdil | 714e14f | 2015-02-25 11:57:05 +0000 | [diff] [blame] | 464 | } else { |
| 465 | DCHECK(my_range->IntersectsWith(*other_range)); |
| 466 | return std::max(my_range->GetStart(), other_range->GetStart()); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 467 | } |
| 468 | } while (true); |
| 469 | } |
| 470 | |
| 471 | size_t GetStart() const { |
| 472 | return first_range_->GetStart(); |
| 473 | } |
| 474 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 475 | size_t GetEnd() const { |
| 476 | return last_range_->GetEnd(); |
| 477 | } |
| 478 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 479 | size_t FirstRegisterUseAfter(size_t position) const { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 480 | if (is_temp_) { |
| 481 | return position == GetStart() ? position : kNoLifetime; |
| 482 | } |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 483 | |
| 484 | if (IsDefiningPosition(position) && DefinitionRequiresRegister()) { |
| 485 | return position; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 486 | } |
| 487 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 488 | UsePosition* use = first_use_; |
Nicolas Geoffray | de025a7 | 2014-06-19 17:06:46 +0100 | [diff] [blame] | 489 | size_t end = GetEnd(); |
| 490 | while (use != nullptr && use->GetPosition() <= end) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 491 | size_t use_position = use->GetPosition(); |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 492 | if (use_position > position) { |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 493 | if (use->RequiresRegister()) { |
Nicolas Geoffray | c8147a7 | 2014-10-21 16:06:20 +0100 | [diff] [blame] | 494 | return use_position; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 495 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 496 | } |
| 497 | use = use->GetNext(); |
| 498 | } |
| 499 | return kNoLifetime; |
| 500 | } |
| 501 | |
| 502 | size_t FirstRegisterUse() const { |
| 503 | return FirstRegisterUseAfter(GetStart()); |
| 504 | } |
| 505 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 506 | size_t FirstUseAfter(size_t position) const { |
| 507 | if (is_temp_) { |
| 508 | return position == GetStart() ? position : kNoLifetime; |
| 509 | } |
| 510 | |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 511 | if (IsDefiningPosition(position)) { |
| 512 | DCHECK(defined_by_->GetLocations()->Out().IsValid()); |
| 513 | return position; |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 514 | } |
| 515 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 516 | UsePosition* use = first_use_; |
| 517 | size_t end = GetEnd(); |
| 518 | while (use != nullptr && use->GetPosition() <= end) { |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 519 | size_t use_position = use->GetPosition(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 520 | if (use_position > position) { |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 521 | return use_position; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 522 | } |
| 523 | use = use->GetNext(); |
| 524 | } |
| 525 | return kNoLifetime; |
| 526 | } |
| 527 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 528 | UsePosition* GetFirstUse() const { |
| 529 | return first_use_; |
| 530 | } |
| 531 | |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 532 | UsePosition* GetFirstEnvironmentUse() const { |
| 533 | return first_env_use_; |
| 534 | } |
| 535 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 536 | Primitive::Type GetType() const { |
| 537 | return type_; |
| 538 | } |
| 539 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 540 | HInstruction* GetDefinedBy() const { |
| 541 | return defined_by_; |
| 542 | } |
| 543 | |
Nicolas Geoffray | 43af728 | 2015-04-16 13:01:01 +0100 | [diff] [blame] | 544 | SafepointPosition* FindSafepointJustBefore(size_t position) const { |
| 545 | for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr; |
| 546 | safepoint != nullptr; |
| 547 | previous = safepoint, safepoint = safepoint->GetNext()) { |
| 548 | if (safepoint->GetPosition() >= position) return previous; |
| 549 | } |
| 550 | return last_safepoint_; |
| 551 | } |
| 552 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 553 | /** |
| 554 | * Split this interval at `position`. This interval is changed to: |
| 555 | * [start ... position). |
| 556 | * |
| 557 | * The new interval covers: |
| 558 | * [position ... end) |
| 559 | */ |
| 560 | LiveInterval* SplitAt(size_t position) { |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 561 | DCHECK(!is_temp_); |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 562 | DCHECK(!is_fixed_); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 563 | DCHECK_GT(position, GetStart()); |
| 564 | |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 565 | if (GetEnd() <= position) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 566 | // This range dies before `position`, no need to split. |
| 567 | return nullptr; |
| 568 | } |
| 569 | |
| 570 | LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_); |
Nicolas Geoffray | 43af728 | 2015-04-16 13:01:01 +0100 | [diff] [blame] | 571 | SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position); |
| 572 | if (new_last_safepoint == nullptr) { |
| 573 | new_interval->first_safepoint_ = first_safepoint_; |
| 574 | new_interval->last_safepoint_ = last_safepoint_; |
| 575 | first_safepoint_ = last_safepoint_ = nullptr; |
| 576 | } else if (last_safepoint_ != new_last_safepoint) { |
| 577 | new_interval->last_safepoint_ = last_safepoint_; |
| 578 | new_interval->first_safepoint_ = new_last_safepoint->GetNext(); |
| 579 | DCHECK(new_interval->first_safepoint_ != nullptr); |
| 580 | last_safepoint_ = new_last_safepoint; |
| 581 | last_safepoint_->SetNext(nullptr); |
| 582 | } |
| 583 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 584 | new_interval->next_sibling_ = next_sibling_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 585 | next_sibling_ = new_interval; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 586 | new_interval->parent_ = parent_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 587 | |
| 588 | new_interval->first_use_ = first_use_; |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 589 | new_interval->first_env_use_ = first_env_use_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 590 | LiveRange* current = first_range_; |
| 591 | LiveRange* previous = nullptr; |
| 592 | // Iterate over the ranges, and either find a range that covers this position, or |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 593 | // two ranges in between this position (that is, the position is in a lifetime hole). |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 594 | do { |
| 595 | if (position >= current->GetEnd()) { |
| 596 | // Move to next range. |
| 597 | previous = current; |
| 598 | current = current->next_; |
| 599 | } else if (position <= current->GetStart()) { |
| 600 | // If the previous range did not cover this position, we know position is in |
| 601 | // a lifetime hole. We can just break the first_range_ and last_range_ links |
| 602 | // and return the new interval. |
| 603 | DCHECK(previous != nullptr); |
| 604 | DCHECK(current != first_range_); |
| 605 | new_interval->last_range_ = last_range_; |
| 606 | last_range_ = previous; |
| 607 | previous->next_ = nullptr; |
| 608 | new_interval->first_range_ = current; |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 609 | if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 610 | // Search start point is inside `new_interval`. Change it to null |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 611 | // (i.e. the end of the interval) in the original interval. |
| 612 | range_search_start_ = nullptr; |
| 613 | } |
| 614 | new_interval->range_search_start_ = new_interval->first_range_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 615 | return new_interval; |
| 616 | } else { |
| 617 | // This range covers position. We create a new last_range_ for this interval |
| 618 | // that covers last_range_->Start() and position. We also shorten the current |
| 619 | // range and make it the first range of the new interval. |
| 620 | DCHECK(position < current->GetEnd() && position > current->GetStart()); |
| 621 | new_interval->last_range_ = last_range_; |
| 622 | last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr); |
| 623 | if (previous != nullptr) { |
| 624 | previous->next_ = last_range_; |
| 625 | } else { |
| 626 | first_range_ = last_range_; |
| 627 | } |
| 628 | new_interval->first_range_ = current; |
| 629 | current->start_ = position; |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 630 | if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) { |
| 631 | // Search start point is inside `new_interval`. Change it to `last_range` |
| 632 | // in the original interval. This is conservative but always correct. |
| 633 | range_search_start_ = last_range_; |
| 634 | } |
| 635 | new_interval->range_search_start_ = new_interval->first_range_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 636 | return new_interval; |
| 637 | } |
| 638 | } while (current != nullptr); |
| 639 | |
| 640 | LOG(FATAL) << "Unreachable"; |
| 641 | return nullptr; |
| 642 | } |
| 643 | |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 644 | bool StartsBeforeOrAt(LiveInterval* other) const { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 645 | return GetStart() <= other->GetStart(); |
| 646 | } |
| 647 | |
| 648 | bool StartsAfter(LiveInterval* other) const { |
Nicolas Geoffray | 7690562 | 2014-09-25 14:39:26 +0100 | [diff] [blame] | 649 | return GetStart() > other->GetStart(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | void Dump(std::ostream& stream) const { |
| 653 | stream << "ranges: { "; |
| 654 | LiveRange* current = first_range_; |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 655 | while (current != nullptr) { |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 656 | current->Dump(stream); |
| 657 | stream << " "; |
Nicolas Geoffray | aedc328 | 2015-01-23 18:01:51 +0000 | [diff] [blame] | 658 | current = current->GetNext(); |
| 659 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 660 | stream << "}, uses: { "; |
| 661 | UsePosition* use = first_use_; |
| 662 | if (use != nullptr) { |
| 663 | do { |
| 664 | use->Dump(stream); |
| 665 | stream << " "; |
| 666 | } while ((use = use->GetNext()) != nullptr); |
| 667 | } |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 668 | stream << "}, { "; |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 669 | use = first_env_use_; |
| 670 | if (use != nullptr) { |
| 671 | do { |
| 672 | use->Dump(stream); |
| 673 | stream << " "; |
| 674 | } while ((use = use->GetNext()) != nullptr); |
| 675 | } |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 676 | stream << "}"; |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 677 | stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit(); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 678 | stream << " is_low: " << IsLowInterval(); |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 679 | stream << " is_high: " << IsHighInterval(); |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | LiveInterval* GetNextSibling() const { return next_sibling_; } |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 683 | LiveInterval* GetLastSibling() { |
| 684 | LiveInterval* result = this; |
| 685 | while (result->next_sibling_ != nullptr) { |
| 686 | result = result->next_sibling_; |
| 687 | } |
| 688 | return result; |
| 689 | } |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 690 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 691 | // Returns the first register hint that is at least free before |
| 692 | // the value contained in `free_until`. If none is found, returns |
| 693 | // `kNoRegister`. |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 694 | int FindFirstRegisterHint(size_t* free_until, const SsaLivenessAnalysis& liveness) const; |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 695 | |
| 696 | // If there is enough at the definition site to find a register (for example |
| 697 | // it uses the same input as the first input), returns the register as a hint. |
| 698 | // Returns kNoRegister otherwise. |
| 699 | int FindHintAtDefinition() const; |
| 700 | |
| 701 | // Returns whether the interval needs two (Dex virtual register size `kVRegSize`) |
| 702 | // slots for spilling. |
| 703 | bool NeedsTwoSpillSlots() const; |
| 704 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 705 | bool IsFloatingPoint() const { |
| 706 | return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble; |
| 707 | } |
| 708 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 709 | // Converts the location of the interval to a `Location` object. |
| 710 | Location ToLocation() const; |
| 711 | |
| 712 | // Returns the location of the interval following its siblings at `position`. |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 713 | Location GetLocationAt(size_t position); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 714 | |
David Brazdil | 241a486 | 2015-04-16 17:59:03 +0100 | [diff] [blame] | 715 | // Finds the sibling that is defined at `position`. |
| 716 | LiveInterval* GetSiblingAt(size_t position); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 717 | |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 718 | // Returns whether `other` and `this` share the same kind of register. |
| 719 | bool SameRegisterKind(Location other) const; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 720 | bool SameRegisterKind(const LiveInterval& other) const { |
| 721 | return IsFloatingPoint() == other.IsFloatingPoint(); |
| 722 | } |
Nicolas Geoffray | 102cbed | 2014-10-15 18:31:05 +0100 | [diff] [blame] | 723 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 724 | bool HasHighInterval() const { |
Nicolas Geoffray | 3747b48 | 2015-01-19 17:17:16 +0000 | [diff] [blame] | 725 | return IsLowInterval(); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | bool HasLowInterval() const { |
| 729 | return IsHighInterval(); |
| 730 | } |
| 731 | |
| 732 | LiveInterval* GetLowInterval() const { |
| 733 | DCHECK(HasLowInterval()); |
| 734 | return high_or_low_interval_; |
| 735 | } |
| 736 | |
| 737 | LiveInterval* GetHighInterval() const { |
| 738 | DCHECK(HasHighInterval()); |
| 739 | return high_or_low_interval_; |
| 740 | } |
| 741 | |
| 742 | bool IsHighInterval() const { |
| 743 | return GetParent()->is_high_interval_; |
| 744 | } |
| 745 | |
| 746 | bool IsLowInterval() const { |
| 747 | return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr); |
| 748 | } |
| 749 | |
| 750 | void SetLowInterval(LiveInterval* low) { |
| 751 | DCHECK(IsHighInterval()); |
| 752 | high_or_low_interval_ = low; |
| 753 | } |
| 754 | |
| 755 | void SetHighInterval(LiveInterval* high) { |
| 756 | DCHECK(IsLowInterval()); |
| 757 | high_or_low_interval_ = high; |
| 758 | } |
| 759 | |
| 760 | void AddHighInterval(bool is_temp = false) { |
Nicolas Geoffray | 1ba1981 | 2015-04-21 09:12:40 +0100 | [diff] [blame] | 761 | DCHECK(IsParent()); |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 762 | DCHECK(!HasHighInterval()); |
| 763 | DCHECK(!HasLowInterval()); |
| 764 | high_or_low_interval_ = new (allocator_) LiveInterval( |
| 765 | allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true); |
| 766 | high_or_low_interval_->high_or_low_interval_ = this; |
| 767 | if (first_range_ != nullptr) { |
| 768 | high_or_low_interval_->first_range_ = first_range_->Dup(allocator_); |
David Brazdil | c08675c | 2015-04-17 15:49:51 +0100 | [diff] [blame] | 769 | high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange(); |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 770 | high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_; |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 771 | } |
| 772 | if (first_use_ != nullptr) { |
| 773 | high_or_low_interval_->first_use_ = first_use_->Dup(allocator_); |
| 774 | } |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 775 | |
| 776 | if (first_env_use_ != nullptr) { |
| 777 | high_or_low_interval_->first_env_use_ = first_env_use_->Dup(allocator_); |
| 778 | } |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 781 | // Returns whether an interval, when it is non-split, is using |
| 782 | // the same register of one of its input. |
| 783 | bool IsUsingInputRegister() const { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 784 | CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs"; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 785 | if (defined_by_ != nullptr && !IsSplit()) { |
| 786 | for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) { |
| 787 | LiveInterval* interval = it.Current()->GetLiveInterval(); |
| 788 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 789 | // Find the interval that covers `defined_by`_. Calls to this function |
| 790 | // are made outside the linear scan, hence we need to use CoversSlow. |
| 791 | while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) { |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 792 | interval = interval->GetNextSibling(); |
| 793 | } |
| 794 | |
| 795 | // Check if both intervals have the same register of the same kind. |
| 796 | if (interval != nullptr |
| 797 | && interval->SameRegisterKind(*this) |
| 798 | && interval->GetRegister() == GetRegister()) { |
| 799 | return true; |
| 800 | } |
| 801 | } |
| 802 | } |
| 803 | return false; |
| 804 | } |
| 805 | |
| 806 | // Returns whether an interval, when it is non-split, can safely use |
| 807 | // the same register of one of its input. Note that this method requires |
| 808 | // IsUsingInputRegister() to be true. |
| 809 | bool CanUseInputRegister() const { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 810 | CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs"; |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 811 | DCHECK(IsUsingInputRegister()); |
| 812 | if (defined_by_ != nullptr && !IsSplit()) { |
| 813 | LocationSummary* locations = defined_by_->GetLocations(); |
| 814 | if (locations->OutputCanOverlapWithInputs()) { |
| 815 | return false; |
| 816 | } |
| 817 | for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) { |
| 818 | LiveInterval* interval = it.Current()->GetLiveInterval(); |
| 819 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 820 | // Find the interval that covers `defined_by`_. Calls to this function |
| 821 | // are made outside the linear scan, hence we need to use CoversSlow. |
| 822 | while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) { |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 823 | interval = interval->GetNextSibling(); |
| 824 | } |
| 825 | |
| 826 | if (interval != nullptr |
| 827 | && interval->SameRegisterKind(*this) |
| 828 | && interval->GetRegister() == GetRegister()) { |
| 829 | // We found the input that has the same register. Check if it is live after |
| 830 | // `defined_by`_. |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 831 | return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1); |
Nicolas Geoffray | 829280c | 2015-01-28 10:20:37 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | } |
| 835 | LOG(FATAL) << "Unreachable"; |
| 836 | UNREACHABLE(); |
| 837 | } |
| 838 | |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 839 | void AddSafepoint(HInstruction* instruction) { |
| 840 | SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction); |
| 841 | if (first_safepoint_ == nullptr) { |
| 842 | first_safepoint_ = last_safepoint_ = safepoint; |
| 843 | } else { |
| 844 | DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition()); |
| 845 | last_safepoint_->SetNext(safepoint); |
| 846 | last_safepoint_ = safepoint; |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | SafepointPosition* GetFirstSafepoint() const { |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 851 | return first_safepoint_; |
| 852 | } |
| 853 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 854 | // Resets the starting point for range-searching queries to the first range. |
| 855 | // Intervals must be reset prior to starting a new linear scan over them. |
| 856 | void ResetSearchCache() { |
| 857 | range_search_start_ = first_range_; |
| 858 | } |
| 859 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 860 | private: |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 861 | LiveInterval(ArenaAllocator* allocator, |
| 862 | Primitive::Type type, |
| 863 | HInstruction* defined_by = nullptr, |
| 864 | bool is_fixed = false, |
| 865 | int reg = kNoRegister, |
| 866 | bool is_temp = false, |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 867 | bool is_slow_path_safepoint = false, |
| 868 | bool is_high_interval = false) |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 869 | : allocator_(allocator), |
| 870 | first_range_(nullptr), |
| 871 | last_range_(nullptr), |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 872 | range_search_start_(nullptr), |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 873 | first_safepoint_(nullptr), |
| 874 | last_safepoint_(nullptr), |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 875 | first_use_(nullptr), |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 876 | first_env_use_(nullptr), |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 877 | type_(type), |
| 878 | next_sibling_(nullptr), |
| 879 | parent_(this), |
| 880 | register_(reg), |
| 881 | spill_slot_(kNoSpillSlot), |
| 882 | is_fixed_(is_fixed), |
| 883 | is_temp_(is_temp), |
| 884 | is_slow_path_safepoint_(is_slow_path_safepoint), |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 885 | is_high_interval_(is_high_interval), |
| 886 | high_or_low_interval_(nullptr), |
Mingyao Yang | 296bd60 | 2014-10-06 16:47:28 -0700 | [diff] [blame] | 887 | defined_by_(defined_by) {} |
| 888 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 889 | // Searches for a LiveRange that either covers the given position or is the |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 890 | // first next LiveRange. Returns null if no such LiveRange exists. Ranges |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 891 | // known to end before `position` can be skipped with `search_start`. |
| 892 | LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const { |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 893 | if (kIsDebugBuild) { |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 894 | if (search_start != first_range_) { |
| 895 | // If we are not searching the entire list of ranges, make sure we do |
| 896 | // not skip the range we are searching for. |
| 897 | if (search_start == nullptr) { |
| 898 | DCHECK(IsDeadAt(position)); |
| 899 | } else if (search_start->GetStart() > position) { |
| 900 | DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_)); |
| 901 | } |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 902 | } |
| 903 | } |
| 904 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 905 | LiveRange* range; |
| 906 | for (range = search_start; |
| 907 | range != nullptr && range->GetEnd() <= position; |
| 908 | range = range->GetNext()) { |
| 909 | continue; |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 910 | } |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 911 | return range; |
David Brazdil | 5b8e6a5 | 2015-02-25 16:17:05 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 914 | bool DefinitionRequiresRegister() const { |
| 915 | DCHECK(IsParent()); |
| 916 | LocationSummary* locations = defined_by_->GetLocations(); |
| 917 | Location location = locations->Out(); |
| 918 | // This interval is the first interval of the instruction. If the output |
| 919 | // of the instruction requires a register, we return the position of that instruction |
| 920 | // as the first register use. |
| 921 | if (location.IsUnallocated()) { |
| 922 | if ((location.GetPolicy() == Location::kRequiresRegister) |
| 923 | || (location.GetPolicy() == Location::kSameAsFirstInput |
| 924 | && (locations->InAt(0).IsRegister() |
| 925 | || locations->InAt(0).IsRegisterPair() |
| 926 | || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) { |
| 927 | return true; |
| 928 | } else if ((location.GetPolicy() == Location::kRequiresFpuRegister) |
| 929 | || (location.GetPolicy() == Location::kSameAsFirstInput |
| 930 | && (locations->InAt(0).IsFpuRegister() |
| 931 | || locations->InAt(0).IsFpuRegisterPair() |
| 932 | || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) { |
| 933 | return true; |
| 934 | } |
| 935 | } else if (location.IsRegister() || location.IsRegisterPair()) { |
| 936 | return true; |
| 937 | } |
| 938 | return false; |
| 939 | } |
| 940 | |
| 941 | bool IsDefiningPosition(size_t position) const { |
| 942 | return IsParent() && (position == GetStart()); |
| 943 | } |
| 944 | |
| 945 | bool HasSynthesizeUseAt(size_t position) const { |
| 946 | UsePosition* use = first_use_; |
| 947 | while (use != nullptr) { |
| 948 | size_t use_position = use->GetPosition(); |
| 949 | if ((use_position == position) && use->IsSynthesized()) { |
| 950 | return true; |
| 951 | } |
| 952 | if (use_position > position) break; |
| 953 | use = use->GetNext(); |
| 954 | } |
| 955 | return false; |
| 956 | } |
| 957 | |
| 958 | void AddBackEdgeUses(const HBasicBlock& block_at_use) { |
| 959 | DCHECK(block_at_use.IsInLoop()); |
| 960 | // Add synthesized uses at the back edge of loops to help the register allocator. |
| 961 | // Note that this method is called in decreasing liveness order, to faciliate adding |
| 962 | // uses at the head of the `first_use_` linked list. Because below |
| 963 | // we iterate from inner-most to outer-most, which is in increasing liveness order, |
| 964 | // we need to take extra care of how the `first_use_` linked list is being updated. |
| 965 | UsePosition* first_in_new_list = nullptr; |
| 966 | UsePosition* last_in_new_list = nullptr; |
| 967 | for (HLoopInformationOutwardIterator it(block_at_use); |
| 968 | !it.Done(); |
| 969 | it.Advance()) { |
| 970 | HLoopInformation* current = it.Current(); |
| 971 | if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) { |
| 972 | // This interval is defined in the loop. We can stop going outward. |
| 973 | break; |
| 974 | } |
| 975 | |
Nicolas Geoffray | db216f4 | 2015-05-05 17:02:20 +0100 | [diff] [blame^] | 976 | // We're only adding a synthesized use at the last back edge. Adding syntehsized uses on |
| 977 | // all back edges is not necessary: anything used in the loop will have its use at the |
| 978 | // last back edge. If we want branches in a loop to have better register allocation than |
| 979 | // another branch, then it is the linear order we should change. |
| 980 | size_t back_edge_use_position = current->GetLifetimeEnd(); |
Nicolas Geoffray | 5790260 | 2015-04-21 14:28:41 +0100 | [diff] [blame] | 981 | if ((first_use_ != nullptr) && (first_use_->GetPosition() <= back_edge_use_position)) { |
| 982 | // There was a use already seen in this loop. Therefore the previous call to `AddUse` |
| 983 | // already inserted the backedge use. We can stop going outward. |
| 984 | DCHECK(HasSynthesizeUseAt(back_edge_use_position)); |
| 985 | break; |
| 986 | } |
| 987 | |
| 988 | DCHECK(last_in_new_list == nullptr |
| 989 | || back_edge_use_position > last_in_new_list->GetPosition()); |
| 990 | |
| 991 | UsePosition* new_use = new (allocator_) UsePosition( |
| 992 | nullptr, UsePosition::kNoInput, /* is_environment */ false, |
| 993 | back_edge_use_position, nullptr); |
| 994 | |
| 995 | if (last_in_new_list != nullptr) { |
| 996 | // Going outward. The latest created use needs to point to the new use. |
| 997 | last_in_new_list->SetNext(new_use); |
| 998 | } else { |
| 999 | // This is the inner-most loop. |
| 1000 | DCHECK_EQ(current, block_at_use.GetLoopInformation()); |
| 1001 | first_in_new_list = new_use; |
| 1002 | } |
| 1003 | last_in_new_list = new_use; |
| 1004 | } |
| 1005 | // Link the newly created linked list with `first_use_`. |
| 1006 | if (last_in_new_list != nullptr) { |
| 1007 | last_in_new_list->SetNext(first_use_); |
| 1008 | first_use_ = first_in_new_list; |
| 1009 | } |
| 1010 | } |
| 1011 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1012 | ArenaAllocator* const allocator_; |
| 1013 | |
| 1014 | // Ranges of this interval. We need a quick access to the last range to test |
| 1015 | // for liveness (see `IsDeadAt`). |
| 1016 | LiveRange* first_range_; |
| 1017 | LiveRange* last_range_; |
| 1018 | |
David Brazdil | 3fc992f | 2015-04-16 18:31:55 +0100 | [diff] [blame] | 1019 | // The first range at or after the current position of a linear scan. It is |
| 1020 | // used to optimize range-searching queries. |
| 1021 | LiveRange* range_search_start_; |
| 1022 | |
Nicolas Geoffray | 43af728 | 2015-04-16 13:01:01 +0100 | [diff] [blame] | 1023 | // Safepoints where this interval is live. |
Nicolas Geoffray | 5588e58 | 2015-04-14 14:10:59 +0100 | [diff] [blame] | 1024 | SafepointPosition* first_safepoint_; |
| 1025 | SafepointPosition* last_safepoint_; |
| 1026 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1027 | // Uses of this interval. Note that this linked list is shared amongst siblings. |
| 1028 | UsePosition* first_use_; |
Nicolas Geoffray | 4ed947a | 2015-04-27 16:58:06 +0100 | [diff] [blame] | 1029 | UsePosition* first_env_use_; |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1030 | |
| 1031 | // The instruction type this interval corresponds to. |
| 1032 | const Primitive::Type type_; |
| 1033 | |
| 1034 | // Live interval that is the result of a split. |
| 1035 | LiveInterval* next_sibling_; |
| 1036 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1037 | // The first interval from which split intervals come from. |
| 1038 | LiveInterval* parent_; |
| 1039 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1040 | // The register allocated to this interval. |
| 1041 | int register_; |
| 1042 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1043 | // The spill slot allocated to this interval. |
| 1044 | int spill_slot_; |
| 1045 | |
| 1046 | // Whether the interval is for a fixed register. |
Nicolas Geoffray | 3946844 | 2014-09-02 15:17:15 +0100 | [diff] [blame] | 1047 | const bool is_fixed_; |
| 1048 | |
| 1049 | // Whether the interval is for a temporary. |
| 1050 | const bool is_temp_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1051 | |
Nicolas Geoffray | 3bca0df | 2014-09-19 11:01:00 +0100 | [diff] [blame] | 1052 | // Whether the interval is for a safepoint that calls on slow path. |
| 1053 | const bool is_slow_path_safepoint_; |
| 1054 | |
Nicolas Geoffray | 840e546 | 2015-01-07 16:01:24 +0000 | [diff] [blame] | 1055 | // Whether this interval is a synthesized interval for register pair. |
| 1056 | const bool is_high_interval_; |
| 1057 | |
| 1058 | // If this interval needs a register pair, the high or low equivalent. |
| 1059 | // `is_high_interval_` tells whether this holds the low or the high. |
| 1060 | LiveInterval* high_or_low_interval_; |
| 1061 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1062 | // The instruction represented by this interval. |
| 1063 | HInstruction* const defined_by_; |
| 1064 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1065 | static constexpr int kNoRegister = -1; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1066 | static constexpr int kNoSpillSlot = -1; |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1067 | |
Nicolas Geoffray | dd8f887 | 2015-01-15 15:37:37 +0000 | [diff] [blame] | 1068 | ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive); |
| 1069 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1070 | DISALLOW_COPY_AND_ASSIGN(LiveInterval); |
| 1071 | }; |
| 1072 | |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 1073 | /** |
| 1074 | * Analysis that computes the liveness of instructions: |
| 1075 | * |
| 1076 | * (a) Non-environment uses of an instruction always make |
| 1077 | * the instruction live. |
| 1078 | * (b) Environment uses of an instruction whose type is |
| 1079 | * object (that is, non-primitive), make the instruction live. |
| 1080 | * This is due to having to keep alive objects that have |
| 1081 | * finalizers deleting native objects. |
| 1082 | * (c) When the graph has the debuggable property, environment uses |
| 1083 | * of an instruction that has a primitive type make the instruction live. |
| 1084 | * If the graph does not have the debuggable property, the environment |
| 1085 | * use has no effect, and may get a 'none' value after register allocation. |
| 1086 | * |
| 1087 | * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment. |
| 1088 | */ |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1089 | class SsaLivenessAnalysis : public ValueObject { |
| 1090 | public: |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 1091 | SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen) |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1092 | : graph_(graph), |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1093 | codegen_(codegen), |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 1094 | block_infos_(graph->GetArena(), graph->GetBlocks().Size()), |
| 1095 | instructions_from_ssa_index_(graph->GetArena(), 0), |
| 1096 | instructions_from_lifetime_position_(graph->GetArena(), 0), |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1097 | number_of_ssa_values_(0) { |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 1098 | block_infos_.SetSize(graph->GetBlocks().Size()); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | void Analyze(); |
| 1102 | |
| 1103 | BitVector* GetLiveInSet(const HBasicBlock& block) const { |
| 1104 | return &block_infos_.Get(block.GetBlockId())->live_in_; |
| 1105 | } |
| 1106 | |
| 1107 | BitVector* GetLiveOutSet(const HBasicBlock& block) const { |
| 1108 | return &block_infos_.Get(block.GetBlockId())->live_out_; |
| 1109 | } |
| 1110 | |
| 1111 | BitVector* GetKillSet(const HBasicBlock& block) const { |
| 1112 | return &block_infos_.Get(block.GetBlockId())->kill_; |
| 1113 | } |
| 1114 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1115 | HInstruction* GetInstructionFromSsaIndex(size_t index) const { |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1116 | return instructions_from_ssa_index_.Get(index); |
| 1117 | } |
| 1118 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1119 | HInstruction* GetInstructionFromPosition(size_t index) const { |
| 1120 | return instructions_from_lifetime_position_.Get(index); |
| 1121 | } |
| 1122 | |
Nicolas Geoffray | 8cbab3c | 2015-04-23 15:14:36 +0100 | [diff] [blame] | 1123 | HBasicBlock* GetBlockFromPosition(size_t index) const { |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 1124 | HInstruction* instruction = GetInstructionFromPosition(index); |
Nicolas Geoffray | 8cbab3c | 2015-04-23 15:14:36 +0100 | [diff] [blame] | 1125 | if (instruction == nullptr) { |
| 1126 | // If we are at a block boundary, get the block following. |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 1127 | instruction = GetInstructionFromPosition(index + 1); |
Nicolas Geoffray | 8cbab3c | 2015-04-23 15:14:36 +0100 | [diff] [blame] | 1128 | } |
| 1129 | return instruction->GetBlock(); |
| 1130 | } |
| 1131 | |
Nicolas Geoffray | fbda5f3 | 2015-04-29 14:16:00 +0100 | [diff] [blame] | 1132 | bool IsAtBlockBoundary(size_t index) const { |
| 1133 | return GetInstructionFromPosition(index) == nullptr; |
| 1134 | } |
| 1135 | |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1136 | HInstruction* GetTempUser(LiveInterval* temp) const { |
| 1137 | // A temporary shares the same lifetime start as the instruction that requires it. |
| 1138 | DCHECK(temp->IsTemp()); |
Nicolas Geoffray | f01d344 | 2015-03-27 17:15:49 +0000 | [diff] [blame] | 1139 | HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2); |
| 1140 | DCHECK_EQ(user, temp->GetFirstUse()->GetUser()); |
| 1141 | return user; |
| 1142 | } |
| 1143 | |
| 1144 | size_t GetTempIndex(LiveInterval* temp) const { |
| 1145 | // We use the input index to store the index of the temporary in the user's temporary list. |
| 1146 | DCHECK(temp->IsTemp()); |
| 1147 | return temp->GetFirstUse()->GetInputIndex(); |
Nicolas Geoffray | 01ef345 | 2014-10-01 11:32:17 +0100 | [diff] [blame] | 1148 | } |
| 1149 | |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1150 | size_t GetMaxLifetimePosition() const { |
| 1151 | return instructions_from_lifetime_position_.Size() * 2 - 1; |
| 1152 | } |
| 1153 | |
Nicolas Geoffray | a7062e0 | 2014-05-22 12:50:17 +0100 | [diff] [blame] | 1154 | size_t GetNumberOfSsaValues() const { |
| 1155 | return number_of_ssa_values_; |
| 1156 | } |
| 1157 | |
Andreas Gampe | 7c3952f | 2015-02-19 18:21:24 -0800 | [diff] [blame] | 1158 | static constexpr const char* kLivenessPassName = "liveness"; |
| 1159 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1160 | private: |
Nicolas Geoffray | 0d3f578 | 2014-05-14 09:43:38 +0100 | [diff] [blame] | 1161 | // Linearize the graph so that: |
| 1162 | // (1): a block is always after its dominator, |
| 1163 | // (2): blocks of loops are contiguous. |
| 1164 | // This creates a natural and efficient ordering when visualizing live ranges. |
| 1165 | void LinearizeGraph(); |
| 1166 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1167 | // Give an SSA number to each instruction that defines a value used by another instruction, |
| 1168 | // and setup the lifetime information of each instruction and block. |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1169 | void NumberInstructions(); |
| 1170 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1171 | // Compute live ranges of instructions, as well as live_in, live_out and kill sets. |
| 1172 | void ComputeLiveness(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1173 | |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1174 | // Compute the live ranges of instructions, as well as the initial live_in, live_out and |
| 1175 | // kill sets, that do not take into account backward branches. |
| 1176 | void ComputeLiveRanges(); |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1177 | |
| 1178 | // After computing the initial sets, this method does a fixed point |
| 1179 | // calculation over the live_in and live_out set to take into account |
| 1180 | // backwards branches. |
| 1181 | void ComputeLiveInAndLiveOutSets(); |
| 1182 | |
| 1183 | // Update the live_in set of the block and returns whether it has changed. |
| 1184 | bool UpdateLiveIn(const HBasicBlock& block); |
| 1185 | |
| 1186 | // Update the live_out set of the block and returns whether it has changed. |
| 1187 | bool UpdateLiveOut(const HBasicBlock& block); |
| 1188 | |
Nicolas Geoffray | 915b9d0 | 2015-03-11 15:11:19 +0000 | [diff] [blame] | 1189 | static bool ShouldBeLiveForEnvironment(HInstruction* instruction) { |
| 1190 | if (instruction == nullptr) return false; |
| 1191 | if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true; |
| 1192 | return instruction->GetType() == Primitive::kPrimNot; |
| 1193 | } |
| 1194 | |
Nicolas Geoffray | 0d9f17d | 2015-04-15 14:17:44 +0100 | [diff] [blame] | 1195 | HGraph* const graph_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1196 | CodeGenerator* const codegen_; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1197 | GrowableArray<BlockInfo*> block_infos_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1198 | |
| 1199 | // Temporary array used when computing live_in, live_out, and kill sets. |
Nicolas Geoffray | ddb311f | 2014-05-16 09:28:54 +0100 | [diff] [blame] | 1200 | GrowableArray<HInstruction*> instructions_from_ssa_index_; |
Nicolas Geoffray | 31d76b4 | 2014-06-09 15:02:22 +0100 | [diff] [blame] | 1201 | |
| 1202 | // Temporary array used when inserting moves in the graph. |
| 1203 | GrowableArray<HInstruction*> instructions_from_lifetime_position_; |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1204 | size_t number_of_ssa_values_; |
| 1205 | |
Nicolas Geoffray | 8cbab3c | 2015-04-23 15:14:36 +0100 | [diff] [blame] | 1206 | ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive); |
| 1207 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1208 | DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis); |
| 1209 | }; |
| 1210 | |
Nicolas Geoffray | 804d093 | 2014-05-02 08:46:00 +0100 | [diff] [blame] | 1211 | } // namespace art |
| 1212 | |
| 1213 | #endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_ |