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