blob: c88390775c633094e83836e4e6f0ca960459896c [file] [log] [blame]
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001/*
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 Geoffray829280c2015-01-28 10:20:37 +000020#include <iostream>
Nicolas Geoffray804d0932014-05-02 08:46:00 +010021
Vladimir Marko82b07402017-03-01 19:02:04 +000022#include "base/iteration_range.h"
Vladimir Markoe764d2e2017-10-05 14:35:55 +010023#include "base/scoped_arena_allocator.h"
24#include "base/scoped_arena_containers.h"
Vladimir Marko356bd282017-03-01 12:01:11 +000025#include "nodes.h"
Vladimir Marko82b07402017-03-01 19:02:04 +000026#include "utils/intrusive_forward_list.h"
Vladimir Marko356bd282017-03-01 12:01:11 +000027
Nicolas Geoffray804d0932014-05-02 08:46:00 +010028namespace art {
29
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010030class CodeGenerator;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +010031class SsaLivenessAnalysis;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010032
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010033static constexpr int kNoRegister = -1;
34
Vladimir Marko5233f932015-09-29 19:01:15 +010035class BlockInfo : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010036 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010037 BlockInfo(ScopedArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
Nicolas Geoffray804d0932014-05-02 08:46:00 +010038 : block_(block),
Vladimir Markof6a35de2016-03-21 12:01:50 +000039 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 Rogerscf7f1912014-10-22 22:06:39 -070042 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010043 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 Geoffrayddb311f2014-05-16 09:28:54 +010059/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010060 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061 * is live.
62 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010063class LiveRange final : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010064 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010065 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010066 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010067 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010068 }
69
70 size_t GetStart() const { return start_; }
71 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010072 LiveRange* GetNext() const { return next_; }
73
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070074 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010075 return (start_ >= other.start_ && start_ < other.end_)
76 || (other.start_ >= start_ && other.start_ < end_);
77 }
78
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070079 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 return end_ <= other.start_;
81 }
82
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070083 void Dump(std::ostream& stream) const {
David Brazdilc7a24852015-05-15 16:44:05 +010084 stream << "[" << start_ << "," << end_ << ")";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010085 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010086
Vladimir Markoe764d2e2017-10-05 14:35:55 +010087 LiveRange* Dup(ScopedArenaAllocator* allocator) const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +000088 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 Geoffrayddb311f2014-05-16 09:28:54 +010096 private:
97 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010098 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010099 LiveRange* next_;
100
101 friend class LiveInterval;
102
103 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100104};
105
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100106/**
107 * A use position represents a live interval use at a given position.
108 */
Vladimir Marko82b07402017-03-01 19:02:04 +0000109class UsePosition : public ArenaObject<kArenaAllocSsaLiveness>,
110 public IntrusiveForwardListNode<UsePosition> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100111 public:
Vladimir Marko82b07402017-03-01 19:02:04 +0000112 UsePosition(HInstruction* user, size_t input_index, size_t position)
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100113 : user_(user),
114 input_index_(input_index),
Vladimir Marko82b07402017-03-01 19:02:04 +0000115 position_(position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100116 }
117
Vladimir Marko356bd282017-03-01 12:01:11 +0000118 explicit UsePosition(size_t position)
119 : user_(nullptr),
120 input_index_(kNoInput),
Vladimir Marko82b07402017-03-01 19:02:04 +0000121 position_(dchecked_integral_cast<uint32_t>(position)) {
Vladimir Marko356bd282017-03-01 12:01:11 +0000122 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100123
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124 size_t GetPosition() const { return position_; }
125
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100126 HInstruction* GetUser() const { return user_; }
127
Nicolas Geoffray57902602015-04-21 14:28:41 +0100128 bool IsSynthesized() const { return user_ == nullptr; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100129
130 size_t GetInputIndex() const { return input_index_; }
131
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100132 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100133 stream << position_;
Nicolas Geoffray57902602015-04-21 14:28:41 +0100134 }
135
136 HLoopInformation* GetLoopInformation() const {
137 return user_->GetBlock()->GetLoopInformation();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100138 }
139
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100140 UsePosition* Clone(ScopedArenaAllocator* allocator) const {
Vladimir Marko82b07402017-03-01 19:02:04 +0000141 return new (allocator) UsePosition(user_, input_index_, position_);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000142 }
143
Nicolas Geoffray57902602015-04-21 14:28:41 +0100144 bool RequiresRegister() const {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100145 if (IsSynthesized()) return false;
146 Location location = GetUser()->GetLocations()->InAt(GetInputIndex());
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700147 return location.IsUnallocated() && location.RequiresRegisterKind();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100148 }
149
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100150 private:
Vladimir Marko356bd282017-03-01 12:01:11 +0000151 static constexpr uint32_t kNoInput = static_cast<uint32_t>(-1);
152
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100153 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100154 const size_t input_index_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100155 const size_t position_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100156
157 DISALLOW_COPY_AND_ASSIGN(UsePosition);
158};
Vladimir Marko82b07402017-03-01 19:02:04 +0000159using UsePositionList = IntrusiveForwardList<UsePosition>;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100160
Vladimir Marko356bd282017-03-01 12:01:11 +0000161/**
162 * An environment use position represents a live interval for environment use at a given position.
163 */
Vladimir Marko82b07402017-03-01 19:02:04 +0000164class EnvUsePosition : public ArenaObject<kArenaAllocSsaLiveness>,
165 public IntrusiveForwardListNode<EnvUsePosition> {
Vladimir Marko356bd282017-03-01 12:01:11 +0000166 public:
167 EnvUsePosition(HEnvironment* environment,
168 size_t input_index,
Vladimir Marko82b07402017-03-01 19:02:04 +0000169 size_t position)
Vladimir Marko356bd282017-03-01 12:01:11 +0000170 : environment_(environment),
171 input_index_(input_index),
Vladimir Marko82b07402017-03-01 19:02:04 +0000172 position_(position) {
Vladimir Marko356bd282017-03-01 12:01:11 +0000173 DCHECK(environment != nullptr);
Vladimir Marko356bd282017-03-01 12:01:11 +0000174 }
175
176 size_t GetPosition() const { return position_; }
177
Vladimir Marko356bd282017-03-01 12:01:11 +0000178 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 Markoe764d2e2017-10-05 14:35:55 +0100185 EnvUsePosition* Clone(ScopedArenaAllocator* allocator) const {
Vladimir Marko82b07402017-03-01 19:02:04 +0000186 return new (allocator) EnvUsePosition(environment_, input_index_, position_);
Vladimir Marko356bd282017-03-01 12:01:11 +0000187 }
188
189 private:
190 HEnvironment* const environment_;
191 const size_t input_index_;
192 const size_t position_;
Vladimir Marko356bd282017-03-01 12:01:11 +0000193
194 DISALLOW_COPY_AND_ASSIGN(EnvUsePosition);
195};
Vladimir Marko82b07402017-03-01 19:02:04 +0000196using EnvUsePositionList = IntrusiveForwardList<EnvUsePosition>;
197
198template <typename Iterator>
199inline 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
217template <typename Iterator>
218inline 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 Marko356bd282017-03-01 12:01:11 +0000226
Vladimir Marko5233f932015-09-29 19:01:15 +0100227class SafepointPosition : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100228 public:
229 explicit SafepointPosition(HInstruction* instruction)
230 : instruction_(instruction),
231 next_(nullptr) {}
232
Nicolas Geoffray2aee3af2018-08-27 20:56:45 +0100233 static size_t ComputePosition(HInstruction* instruction) {
234 // We special case instructions emitted at use site, as their
235 // safepoint position needs to be at their use.
236 if (instruction->IsEmittedAtUseSite()) {
237 // Currently only applies to implicit null checks, which are emitted
238 // at the next instruction.
239 DCHECK(instruction->IsNullCheck()) << instruction->DebugName();
240 return instruction->GetLifetimePosition() + 2;
241 } else {
242 return instruction->GetLifetimePosition();
243 }
244 }
245
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100246 void SetNext(SafepointPosition* next) {
247 next_ = next;
248 }
249
250 size_t GetPosition() const {
Nicolas Geoffray2aee3af2018-08-27 20:56:45 +0100251 return ComputePosition(instruction_);
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100252 }
253
254 SafepointPosition* GetNext() const {
255 return next_;
256 }
257
258 LocationSummary* GetLocations() const {
259 return instruction_->GetLocations();
260 }
261
262 HInstruction* GetInstruction() const {
263 return instruction_;
264 }
265
266 private:
267 HInstruction* const instruction_;
268 SafepointPosition* next_;
269
270 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
271};
272
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100273/**
274 * An interval is a list of disjoint live ranges where an instruction is live.
275 * Each instruction that has uses gets an interval.
276 */
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100277class LiveInterval : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100278 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100279 static LiveInterval* MakeInterval(ScopedArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100280 DataType::Type type,
Mingyao Yang296bd602014-10-06 16:47:28 -0700281 HInstruction* instruction = nullptr) {
282 return new (allocator) LiveInterval(allocator, type, instruction);
283 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100284
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100285 static LiveInterval* MakeFixedInterval(ScopedArenaAllocator* allocator,
286 int reg,
287 DataType::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100288 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
289 }
290
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100291 static LiveInterval* MakeTempInterval(ScopedArenaAllocator* allocator, DataType::Type type) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100292 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100293 }
294
295 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700296 bool IsTemp() const { return is_temp_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700297 // This interval is the result of a split.
298 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100299
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000300 void AddTempUse(HInstruction* instruction, size_t temp_index) {
301 DCHECK(IsTemp());
Vladimir Marko82b07402017-03-01 19:02:04 +0000302 DCHECK(GetUses().empty()) << "A temporary can only have one user";
303 DCHECK(GetEnvironmentUses().empty()) << "A temporary cannot have environment user";
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000304 size_t position = instruction->GetLifetimePosition();
Vladimir Marko82b07402017-03-01 19:02:04 +0000305 UsePosition* new_use = new (allocator_) UsePosition(instruction, temp_index, position);
306 uses_.push_front(*new_use);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000307 AddRange(position, position + 1);
308 }
309
David Brazdilb3e773e2016-01-26 11:28:37 +0000310 // Record use of an input. The use will be recorded as an environment use if
311 // `environment` is not null and as register use otherwise. If `actual_user`
312 // is specified, the use will be recorded at `actual_user`'s lifetime position.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000313 void AddUse(HInstruction* instruction,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100314 HEnvironment* environment,
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000315 size_t input_index,
Artem Serovd6750532018-05-30 20:07:43 +0100316 HInstruction* actual_user = nullptr) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100317 bool is_environment = (environment != nullptr);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000318 LocationSummary* locations = instruction->GetLocations();
David Brazdilb3e773e2016-01-26 11:28:37 +0000319 if (actual_user == nullptr) {
320 actual_user = instruction;
321 }
322
323 // Set the use within the instruction.
324 size_t position = actual_user->GetLifetimePosition() + 1;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000325 if (!is_environment) {
326 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
327 // For fixed inputs and output same as input, the register allocator
328 // requires to have inputs die at the instruction, so that input moves use the
329 // location of the input just before that instruction (and not potential moves due
330 // to splitting).
David Brazdilb3e773e2016-01-26 11:28:37 +0000331 DCHECK_EQ(instruction, actual_user);
332 position = actual_user->GetLifetimePosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100333 } else if (!locations->InAt(input_index).IsValid()) {
334 return;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000335 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100336 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000337
Nicolas Geoffray57902602015-04-21 14:28:41 +0100338 if (!is_environment && instruction->IsInLoop()) {
339 AddBackEdgeUses(*instruction->GetBlock());
340 }
341
Vladimir Marko82b07402017-03-01 19:02:04 +0000342 if ((!uses_.empty()) &&
343 (uses_.front().GetUser() == actual_user) &&
344 (uses_.front().GetPosition() < position)) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100345 // The user uses the instruction multiple times, and one use dies before the other.
346 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000347 DCHECK(!is_environment);
Vladimir Marko82b07402017-03-01 19:02:04 +0000348 DCHECK(uses_.front().GetPosition() + 1 == position);
349 UsePositionList::iterator next_pos = uses_.begin();
350 UsePositionList::iterator insert_pos;
351 do {
352 insert_pos = next_pos;
353 ++next_pos;
354 } while (next_pos != uses_.end() && next_pos->GetPosition() < position);
355 UsePosition* new_use = new (allocator_) UsePosition(instruction, input_index, position);
356 uses_.insert_after(insert_pos, *new_use);
357 if (first_range_->GetEnd() == uses_.front().GetPosition()) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100358 first_range_->end_ = position;
359 }
360 return;
361 }
362
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100363 if (is_environment) {
Vladimir Marko82b07402017-03-01 19:02:04 +0000364 DCHECK(env_uses_.empty() || position <= env_uses_.front().GetPosition());
365 EnvUsePosition* new_env_use =
366 new (allocator_) EnvUsePosition(environment, input_index, position);
367 env_uses_.push_front(*new_env_use);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100368 } else {
Vladimir Marko82b07402017-03-01 19:02:04 +0000369 DCHECK(uses_.empty() || position <= uses_.front().GetPosition());
370 UsePosition* new_use = new (allocator_) UsePosition(instruction, input_index, position);
371 uses_.push_front(*new_use);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100372 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000373
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100374 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100375 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100376 // First time we see a use of that interval.
David Brazdil3fc992f2015-04-16 18:31:55 +0100377 first_range_ = last_range_ = range_search_start_ =
378 new (allocator_) LiveRange(start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100379 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100380 // There is a use later in the same block or in a following block.
381 // Note that in such a case, `AddRange` for the whole blocks has been called
382 // before arriving in this method, and this is the reason the start of
383 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100384 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100385 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100386 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100387 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100388 // Note that the start of `first_range_` can be equal to `end`: two blocks
389 // having adjacent lifetime positions are not necessarily
390 // predecessor/successor. When two blocks are predecessor/successor, the
391 // liveness algorithm has called `AddRange` before arriving in this method,
392 // and the check line 205 would succeed.
David Brazdil3fc992f2015-04-16 18:31:55 +0100393 first_range_ = range_search_start_ =
394 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100395 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100396 }
397
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100398 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100399 DCHECK(instruction->IsPhi());
Nicolas Geoffray57902602015-04-21 14:28:41 +0100400 if (block->IsInLoop()) {
401 AddBackEdgeUses(*block);
402 }
Vladimir Marko82b07402017-03-01 19:02:04 +0000403 UsePosition* new_use =
404 new (allocator_) UsePosition(instruction, input_index, block->GetLifetimeEnd());
405 uses_.push_front(*new_use);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100406 }
407
Mingyao Yang01b47b02017-02-03 12:09:57 -0800408 ALWAYS_INLINE void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100409 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100410 first_range_ = last_range_ = range_search_start_ =
411 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100412 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100413 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100414 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100415 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
416 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100417 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100418 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100419 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100420 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100421 }
422 }
423
424 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100425 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000426 DCHECK_LE(start, first_range_->GetStart());
427 // Find the range that covers the positions after the loop.
428 LiveRange* after_loop = first_range_;
429 LiveRange* last_in_loop = nullptr;
430 while (after_loop != nullptr && after_loop->GetEnd() < end) {
431 DCHECK_LE(start, after_loop->GetStart());
432 last_in_loop = after_loop;
433 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100434 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000435 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100436 // Uses are only in the loop.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700437 first_range_ = last_range_ = range_search_start_ =
438 new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000439 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100440 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100441 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100442 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000443 } else {
444 // The use after the loop is after a lifetime hole.
445 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100446 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000447 first_range_->start_ = start;
448 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100449 }
450 }
451
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100452 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100453 void SetSpillSlot(int slot) {
454 DCHECK(!is_fixed_);
455 DCHECK(!is_temp_);
456 spill_slot_ = slot;
457 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100458 int GetSpillSlot() const { return spill_slot_; }
459
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100460 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100461 if (first_range_ != nullptr) {
462 first_range_->start_ = from;
463 } else {
464 // Instruction without uses.
Vladimir Marko82b07402017-03-01 19:02:04 +0000465 DCHECK(uses_.empty());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100466 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100467 first_range_ = last_range_ = range_search_start_ =
468 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100469 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100470 }
471
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100472 LiveInterval* GetParent() const { return parent_; }
473
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100474 // Returns whether this interval is the parent interval, that is, the interval
475 // that starts where the HInstruction is defined.
476 bool IsParent() const { return parent_ == this; }
477
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100478 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000479 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100480
481 int GetRegister() const { return register_; }
482 void SetRegister(int reg) { register_ = reg; }
483 void ClearRegister() { register_ = kNoRegister; }
484 bool HasRegister() const { return register_ != kNoRegister; }
485
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100486 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100487 return GetEnd() <= position;
488 }
489
490 bool IsDefinedAt(size_t position) const {
491 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100492 }
493
David Brazdil3fc992f2015-04-16 18:31:55 +0100494 // Returns true if the interval contains a LiveRange covering `position`.
495 // The range at or immediately after the current position of linear scan
496 // is cached for better performance. If `position` can be smaller than
497 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000498 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100499 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
500 range_search_start_ = candidate;
501 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100502 }
503
David Brazdil3fc992f2015-04-16 18:31:55 +0100504 // Same as Covers but always tests all ranges.
505 bool CoversSlow(size_t position) const {
506 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
507 return candidate != nullptr && candidate->GetStart() <= position;
508 }
509
510 // Returns the first intersection of this interval with `current`, which
511 // must be the interval currently being allocated by linear scan.
512 size_t FirstIntersectionWith(LiveInterval* current) const {
513 // Find the first range after the start of `current`. We use the search
514 // cache to improve performance.
515 DCHECK(GetStart() <= current->GetStart() || IsFixed());
516 LiveRange* other_range = current->first_range_;
517 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
518 if (my_range == nullptr) {
519 return kNoLifetime;
520 }
521
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100522 // Advance both intervals and find the first matching range start in
523 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100524 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000525 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100526 my_range = my_range->GetNext();
527 if (my_range == nullptr) {
528 return kNoLifetime;
529 }
David Brazdil714e14f2015-02-25 11:57:05 +0000530 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100531 other_range = other_range->GetNext();
532 if (other_range == nullptr) {
533 return kNoLifetime;
534 }
David Brazdil714e14f2015-02-25 11:57:05 +0000535 } else {
536 DCHECK(my_range->IntersectsWith(*other_range));
537 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100538 }
539 } while (true);
540 }
541
542 size_t GetStart() const {
543 return first_range_->GetStart();
544 }
545
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100546 size_t GetEnd() const {
547 return last_range_->GetEnd();
548 }
549
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700550 size_t GetLength() const {
551 return GetEnd() - GetStart();
552 }
553
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100554 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100555 if (is_temp_) {
556 return position == GetStart() ? position : kNoLifetime;
557 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100558
559 if (IsDefiningPosition(position) && DefinitionRequiresRegister()) {
560 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100561 }
562
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100563 size_t end = GetEnd();
Vladimir Marko82b07402017-03-01 19:02:04 +0000564 for (const UsePosition& use : GetUses()) {
565 size_t use_position = use.GetPosition();
566 if (use_position > end) {
567 break;
568 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100569 if (use_position > position) {
Vladimir Marko82b07402017-03-01 19:02:04 +0000570 if (use.RequiresRegister()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100571 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100572 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100573 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100574 }
575 return kNoLifetime;
576 }
577
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700578 // Returns the location of the first register use for this live interval,
579 // including a register definition if applicable.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100580 size_t FirstRegisterUse() const {
581 return FirstRegisterUseAfter(GetStart());
582 }
583
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700584 // Whether the interval requires a register rather than a stack location.
585 // If needed for performance, this could be cached.
Matthew Gharrity2ccae4a2016-08-12 16:10:45 +0000586 bool RequiresRegister() const {
587 return !HasRegister() && FirstRegisterUse() != kNoLifetime;
588 }
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700589
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000590 size_t FirstUseAfter(size_t position) const {
591 if (is_temp_) {
592 return position == GetStart() ? position : kNoLifetime;
593 }
594
Nicolas Geoffray57902602015-04-21 14:28:41 +0100595 if (IsDefiningPosition(position)) {
596 DCHECK(defined_by_->GetLocations()->Out().IsValid());
597 return position;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100598 }
599
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000600 size_t end = GetEnd();
Vladimir Marko82b07402017-03-01 19:02:04 +0000601 for (const UsePosition& use : GetUses()) {
602 size_t use_position = use.GetPosition();
603 if (use_position > end) {
604 break;
605 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100606 if (use_position > position) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100607 return use_position;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000608 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000609 }
610 return kNoLifetime;
611 }
612
Vladimir Marko82b07402017-03-01 19:02:04 +0000613 const UsePositionList& GetUses() const {
614 return parent_->uses_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100615 }
616
Vladimir Marko82b07402017-03-01 19:02:04 +0000617 const EnvUsePositionList& GetEnvironmentUses() const {
618 return parent_->env_uses_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100619 }
620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100621 DataType::Type GetType() const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100622 return type_;
623 }
624
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100625 HInstruction* GetDefinedBy() const {
626 return defined_by_;
627 }
628
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100629 bool HasWillCallSafepoint() const {
630 for (SafepointPosition* safepoint = first_safepoint_;
631 safepoint != nullptr;
632 safepoint = safepoint->GetNext()) {
633 if (safepoint->GetLocations()->WillCall()) return true;
634 }
635 return false;
636 }
637
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100638 SafepointPosition* FindSafepointJustBefore(size_t position) const {
639 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
640 safepoint != nullptr;
641 previous = safepoint, safepoint = safepoint->GetNext()) {
642 if (safepoint->GetPosition() >= position) return previous;
643 }
644 return last_safepoint_;
645 }
646
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100647 /**
648 * Split this interval at `position`. This interval is changed to:
649 * [start ... position).
650 *
651 * The new interval covers:
652 * [position ... end)
653 */
654 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100655 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100656 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100657 DCHECK_GT(position, GetStart());
658
David Brazdil241a4862015-04-16 17:59:03 +0100659 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100660 // This range dies before `position`, no need to split.
661 return nullptr;
662 }
663
664 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100665 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
666 if (new_last_safepoint == nullptr) {
667 new_interval->first_safepoint_ = first_safepoint_;
668 new_interval->last_safepoint_ = last_safepoint_;
669 first_safepoint_ = last_safepoint_ = nullptr;
670 } else if (last_safepoint_ != new_last_safepoint) {
671 new_interval->last_safepoint_ = last_safepoint_;
672 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
673 DCHECK(new_interval->first_safepoint_ != nullptr);
674 last_safepoint_ = new_last_safepoint;
675 last_safepoint_->SetNext(nullptr);
676 }
677
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100678 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100679 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100680 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100681
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100682 LiveRange* current = first_range_;
683 LiveRange* previous = nullptr;
684 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000685 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100686 do {
687 if (position >= current->GetEnd()) {
688 // Move to next range.
689 previous = current;
690 current = current->next_;
691 } else if (position <= current->GetStart()) {
692 // If the previous range did not cover this position, we know position is in
693 // a lifetime hole. We can just break the first_range_ and last_range_ links
694 // and return the new interval.
695 DCHECK(previous != nullptr);
696 DCHECK(current != first_range_);
697 new_interval->last_range_ = last_range_;
698 last_range_ = previous;
699 previous->next_ = nullptr;
700 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100701 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700702 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100703 // (i.e. the end of the interval) in the original interval.
704 range_search_start_ = nullptr;
705 }
706 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100707 return new_interval;
708 } else {
709 // This range covers position. We create a new last_range_ for this interval
710 // that covers last_range_->Start() and position. We also shorten the current
711 // range and make it the first range of the new interval.
712 DCHECK(position < current->GetEnd() && position > current->GetStart());
713 new_interval->last_range_ = last_range_;
714 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
715 if (previous != nullptr) {
716 previous->next_ = last_range_;
717 } else {
718 first_range_ = last_range_;
719 }
720 new_interval->first_range_ = current;
721 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100722 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
723 // Search start point is inside `new_interval`. Change it to `last_range`
724 // in the original interval. This is conservative but always correct.
725 range_search_start_ = last_range_;
726 }
727 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100728 return new_interval;
729 }
730 } while (current != nullptr);
731
732 LOG(FATAL) << "Unreachable";
733 return nullptr;
734 }
735
Nicolas Geoffray76905622014-09-25 14:39:26 +0100736 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100737 return GetStart() <= other->GetStart();
738 }
739
740 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100741 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100742 }
743
744 void Dump(std::ostream& stream) const {
745 stream << "ranges: { ";
746 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000747 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100748 current->Dump(stream);
749 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000750 current = current->GetNext();
751 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100752 stream << "}, uses: { ";
Vladimir Marko82b07402017-03-01 19:02:04 +0000753 for (const UsePosition& use : GetUses()) {
754 use.Dump(stream);
755 stream << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100756 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100757 stream << "}, { ";
Vladimir Marko82b07402017-03-01 19:02:04 +0000758 for (const EnvUsePosition& env_use : GetEnvironmentUses()) {
759 env_use.Dump(stream);
760 stream << " ";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100761 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100762 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700763 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000764 stream << " is_low: " << IsLowInterval();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100765 stream << " is_high: " << IsHighInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100766 }
767
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700768 // Same as Dump, but adds context such as the instruction defining this interval, and
769 // the register currently assigned to this interval.
770 void DumpWithContext(std::ostream& stream, const CodeGenerator& codegen) const;
771
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100772 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000773 LiveInterval* GetLastSibling() {
774 LiveInterval* result = this;
775 while (result->next_sibling_ != nullptr) {
776 result = result->next_sibling_;
777 }
778 return result;
779 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100780
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100781 // Returns the first register hint that is at least free before
782 // the value contained in `free_until`. If none is found, returns
783 // `kNoRegister`.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100784 int FindFirstRegisterHint(size_t* free_until, const SsaLivenessAnalysis& liveness) const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100785
786 // If there is enough at the definition site to find a register (for example
787 // it uses the same input as the first input), returns the register as a hint.
788 // Returns kNoRegister otherwise.
789 int FindHintAtDefinition() const;
790
Aart Bikcc895252017-03-21 10:55:15 -0700791 // Returns the number of required spilling slots (measured as a multiple of the
792 // Dex virtual register size `kVRegSize`).
793 size_t NumberOfSpillSlotsNeeded() const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100794
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100795 bool IsFloatingPoint() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100796 return type_ == DataType::Type::kFloat32 || type_ == DataType::Type::kFloat64;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100797 }
798
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100799 // Converts the location of the interval to a `Location` object.
800 Location ToLocation() const;
801
802 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000803 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100804
David Brazdil241a4862015-04-16 17:59:03 +0100805 // Finds the sibling that is defined at `position`.
806 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100807
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100808 // Returns whether `other` and `this` share the same kind of register.
809 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000810 bool SameRegisterKind(const LiveInterval& other) const {
811 return IsFloatingPoint() == other.IsFloatingPoint();
812 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100813
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000814 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000815 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000816 }
817
818 bool HasLowInterval() const {
819 return IsHighInterval();
820 }
821
822 LiveInterval* GetLowInterval() const {
823 DCHECK(HasLowInterval());
824 return high_or_low_interval_;
825 }
826
827 LiveInterval* GetHighInterval() const {
828 DCHECK(HasHighInterval());
829 return high_or_low_interval_;
830 }
831
832 bool IsHighInterval() const {
833 return GetParent()->is_high_interval_;
834 }
835
836 bool IsLowInterval() const {
837 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
838 }
839
840 void SetLowInterval(LiveInterval* low) {
841 DCHECK(IsHighInterval());
842 high_or_low_interval_ = low;
843 }
844
845 void SetHighInterval(LiveInterval* high) {
846 DCHECK(IsLowInterval());
847 high_or_low_interval_ = high;
848 }
849
850 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100851 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000852 DCHECK(!HasHighInterval());
853 DCHECK(!HasLowInterval());
854 high_or_low_interval_ = new (allocator_) LiveInterval(
Vladimir Marko70e97462016-08-09 11:04:26 +0100855 allocator_, type_, defined_by_, false, kNoRegister, is_temp, true);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000856 high_or_low_interval_->high_or_low_interval_ = this;
857 if (first_range_ != nullptr) {
858 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100859 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100860 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000861 }
Vladimir Marko82b07402017-03-01 19:02:04 +0000862 auto pos = high_or_low_interval_->uses_.before_begin();
863 for (const UsePosition& use : uses_) {
864 UsePosition* new_use = use.Clone(allocator_);
865 pos = high_or_low_interval_->uses_.insert_after(pos, *new_use);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000866 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100867
Vladimir Marko82b07402017-03-01 19:02:04 +0000868 auto env_pos = high_or_low_interval_->env_uses_.before_begin();
869 for (const EnvUsePosition& env_use : env_uses_) {
870 EnvUsePosition* new_env_use = env_use.Clone(allocator_);
871 env_pos = high_or_low_interval_->env_uses_.insert_after(env_pos, *new_env_use);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100872 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000873 }
874
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000875 // Returns whether an interval, when it is non-split, is using
876 // the same register of one of its input.
877 bool IsUsingInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100878 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000879 if (defined_by_ != nullptr && !IsSplit()) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100880 for (const HInstruction* input : defined_by_->GetInputs()) {
881 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000882
David Brazdil3fc992f2015-04-16 18:31:55 +0100883 // Find the interval that covers `defined_by`_. Calls to this function
884 // are made outside the linear scan, hence we need to use CoversSlow.
885 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000886 interval = interval->GetNextSibling();
887 }
888
889 // Check if both intervals have the same register of the same kind.
890 if (interval != nullptr
891 && interval->SameRegisterKind(*this)
892 && interval->GetRegister() == GetRegister()) {
893 return true;
894 }
895 }
896 }
897 return false;
898 }
899
900 // Returns whether an interval, when it is non-split, can safely use
901 // the same register of one of its input. Note that this method requires
902 // IsUsingInputRegister() to be true.
903 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100904 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000905 DCHECK(IsUsingInputRegister());
906 if (defined_by_ != nullptr && !IsSplit()) {
907 LocationSummary* locations = defined_by_->GetLocations();
908 if (locations->OutputCanOverlapWithInputs()) {
909 return false;
910 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100911 for (const HInstruction* input : defined_by_->GetInputs()) {
912 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000913
David Brazdil3fc992f2015-04-16 18:31:55 +0100914 // Find the interval that covers `defined_by`_. Calls to this function
915 // are made outside the linear scan, hence we need to use CoversSlow.
916 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000917 interval = interval->GetNextSibling();
918 }
919
920 if (interval != nullptr
921 && interval->SameRegisterKind(*this)
922 && interval->GetRegister() == GetRegister()) {
923 // We found the input that has the same register. Check if it is live after
924 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100925 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000926 }
927 }
928 }
929 LOG(FATAL) << "Unreachable";
930 UNREACHABLE();
931 }
932
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100933 void AddSafepoint(HInstruction* instruction) {
934 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
935 if (first_safepoint_ == nullptr) {
936 first_safepoint_ = last_safepoint_ = safepoint;
937 } else {
Nicolas Geoffray2aee3af2018-08-27 20:56:45 +0100938 DCHECK_LE(last_safepoint_->GetPosition(), safepoint->GetPosition());
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100939 last_safepoint_->SetNext(safepoint);
940 last_safepoint_ = safepoint;
941 }
942 }
943
944 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100945 return first_safepoint_;
946 }
947
David Brazdil3fc992f2015-04-16 18:31:55 +0100948 // Resets the starting point for range-searching queries to the first range.
949 // Intervals must be reset prior to starting a new linear scan over them.
950 void ResetSearchCache() {
951 range_search_start_ = first_range_;
952 }
953
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700954 bool DefinitionRequiresRegister() const {
955 DCHECK(IsParent());
956 LocationSummary* locations = defined_by_->GetLocations();
957 Location location = locations->Out();
958 // This interval is the first interval of the instruction. If the output
959 // of the instruction requires a register, we return the position of that instruction
960 // as the first register use.
961 if (location.IsUnallocated()) {
962 if ((location.GetPolicy() == Location::kRequiresRegister)
963 || (location.GetPolicy() == Location::kSameAsFirstInput
964 && (locations->InAt(0).IsRegister()
965 || locations->InAt(0).IsRegisterPair()
966 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
967 return true;
968 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
969 || (location.GetPolicy() == Location::kSameAsFirstInput
970 && (locations->InAt(0).IsFpuRegister()
971 || locations->InAt(0).IsFpuRegisterPair()
972 || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) {
973 return true;
974 }
975 } else if (location.IsRegister() || location.IsRegisterPair()) {
976 return true;
977 }
978 return false;
979 }
980
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100981 private:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100982 LiveInterval(ScopedArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100983 DataType::Type type,
Mingyao Yang296bd602014-10-06 16:47:28 -0700984 HInstruction* defined_by = nullptr,
985 bool is_fixed = false,
986 int reg = kNoRegister,
987 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000988 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700989 : allocator_(allocator),
990 first_range_(nullptr),
991 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100992 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100993 first_safepoint_(nullptr),
994 last_safepoint_(nullptr),
Vladimir Marko82b07402017-03-01 19:02:04 +0000995 uses_(),
996 env_uses_(),
Mingyao Yang296bd602014-10-06 16:47:28 -0700997 type_(type),
998 next_sibling_(nullptr),
999 parent_(this),
1000 register_(reg),
1001 spill_slot_(kNoSpillSlot),
1002 is_fixed_(is_fixed),
1003 is_temp_(is_temp),
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001004 is_high_interval_(is_high_interval),
1005 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -07001006 defined_by_(defined_by) {}
1007
David Brazdil3fc992f2015-04-16 18:31:55 +01001008 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001009 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +01001010 // known to end before `position` can be skipped with `search_start`.
1011 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +00001012 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +01001013 if (search_start != first_range_) {
1014 // If we are not searching the entire list of ranges, make sure we do
1015 // not skip the range we are searching for.
1016 if (search_start == nullptr) {
1017 DCHECK(IsDeadAt(position));
1018 } else if (search_start->GetStart() > position) {
1019 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
1020 }
David Brazdil5b8e6a52015-02-25 16:17:05 +00001021 }
1022 }
1023
David Brazdil3fc992f2015-04-16 18:31:55 +01001024 LiveRange* range;
1025 for (range = search_start;
1026 range != nullptr && range->GetEnd() <= position;
1027 range = range->GetNext()) {
1028 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +00001029 }
David Brazdil3fc992f2015-04-16 18:31:55 +01001030 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +00001031 }
1032
Nicolas Geoffray57902602015-04-21 14:28:41 +01001033 bool IsDefiningPosition(size_t position) const {
1034 return IsParent() && (position == GetStart());
1035 }
1036
1037 bool HasSynthesizeUseAt(size_t position) const {
Vladimir Marko82b07402017-03-01 19:02:04 +00001038 for (const UsePosition& use : GetUses()) {
1039 size_t use_position = use.GetPosition();
1040 if ((use_position == position) && use.IsSynthesized()) {
Nicolas Geoffray57902602015-04-21 14:28:41 +01001041 return true;
1042 }
1043 if (use_position > position) break;
Nicolas Geoffray57902602015-04-21 14:28:41 +01001044 }
1045 return false;
1046 }
1047
1048 void AddBackEdgeUses(const HBasicBlock& block_at_use) {
1049 DCHECK(block_at_use.IsInLoop());
David Brazdil07b35102016-04-27 15:33:22 +01001050 if (block_at_use.GetGraph()->HasIrreducibleLoops()) {
1051 // Linear order may not be well formed when irreducible loops are present,
1052 // i.e. loop blocks may not be adjacent and a back edge may not be last,
1053 // which violates assumptions made in this method.
1054 return;
1055 }
1056
Nicolas Geoffray57902602015-04-21 14:28:41 +01001057 // Add synthesized uses at the back edge of loops to help the register allocator.
1058 // Note that this method is called in decreasing liveness order, to faciliate adding
Vladimir Marko82b07402017-03-01 19:02:04 +00001059 // uses at the head of the `uses_` list. Because below
Nicolas Geoffray57902602015-04-21 14:28:41 +01001060 // we iterate from inner-most to outer-most, which is in increasing liveness order,
Vladimir Marko82b07402017-03-01 19:02:04 +00001061 // we need to add subsequent entries after the last inserted entry.
1062 const UsePositionList::iterator old_begin = uses_.begin();
1063 UsePositionList::iterator insert_pos = uses_.before_begin();
Nicolas Geoffray57902602015-04-21 14:28:41 +01001064 for (HLoopInformationOutwardIterator it(block_at_use);
1065 !it.Done();
1066 it.Advance()) {
1067 HLoopInformation* current = it.Current();
1068 if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) {
1069 // This interval is defined in the loop. We can stop going outward.
1070 break;
1071 }
1072
Vladimir Marko82b07402017-03-01 19:02:04 +00001073 // We're only adding a synthesized use at the last back edge. Adding synthesized uses on
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001074 // all back edges is not necessary: anything used in the loop will have its use at the
1075 // last back edge. If we want branches in a loop to have better register allocation than
1076 // another branch, then it is the linear order we should change.
1077 size_t back_edge_use_position = current->GetLifetimeEnd();
Vladimir Marko82b07402017-03-01 19:02:04 +00001078 if ((old_begin != uses_.end()) && (old_begin->GetPosition() <= back_edge_use_position)) {
Nicolas Geoffray57902602015-04-21 14:28:41 +01001079 // There was a use already seen in this loop. Therefore the previous call to `AddUse`
1080 // already inserted the backedge use. We can stop going outward.
David Brazdil07b35102016-04-27 15:33:22 +01001081 DCHECK(HasSynthesizeUseAt(back_edge_use_position));
Nicolas Geoffray57902602015-04-21 14:28:41 +01001082 break;
1083 }
1084
Vladimir Marko82b07402017-03-01 19:02:04 +00001085 DCHECK(insert_pos != uses_.before_begin()
1086 ? back_edge_use_position > insert_pos->GetPosition()
1087 : current == block_at_use.GetLoopInformation())
1088 << std::distance(uses_.before_begin(), insert_pos);
Nicolas Geoffray57902602015-04-21 14:28:41 +01001089
Vladimir Marko356bd282017-03-01 12:01:11 +00001090 UsePosition* new_use = new (allocator_) UsePosition(back_edge_use_position);
Vladimir Marko82b07402017-03-01 19:02:04 +00001091 insert_pos = uses_.insert_after(insert_pos, *new_use);
Nicolas Geoffray57902602015-04-21 14:28:41 +01001092 }
1093 }
1094
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001095 ScopedArenaAllocator* const allocator_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001096
1097 // Ranges of this interval. We need a quick access to the last range to test
1098 // for liveness (see `IsDeadAt`).
1099 LiveRange* first_range_;
1100 LiveRange* last_range_;
1101
David Brazdil3fc992f2015-04-16 18:31:55 +01001102 // The first range at or after the current position of a linear scan. It is
1103 // used to optimize range-searching queries.
1104 LiveRange* range_search_start_;
1105
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001106 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001107 SafepointPosition* first_safepoint_;
1108 SafepointPosition* last_safepoint_;
1109
Vladimir Marko82b07402017-03-01 19:02:04 +00001110 // Uses of this interval. Only the parent interval keeps these lists.
1111 UsePositionList uses_;
1112 EnvUsePositionList env_uses_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001113
1114 // The instruction type this interval corresponds to.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001115 const DataType::Type type_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001116
1117 // Live interval that is the result of a split.
1118 LiveInterval* next_sibling_;
1119
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001120 // The first interval from which split intervals come from.
1121 LiveInterval* parent_;
1122
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001123 // The register allocated to this interval.
1124 int register_;
1125
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001126 // The spill slot allocated to this interval.
1127 int spill_slot_;
1128
1129 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001130 const bool is_fixed_;
1131
1132 // Whether the interval is for a temporary.
1133 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001134
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001135 // Whether this interval is a synthesized interval for register pair.
1136 const bool is_high_interval_;
1137
1138 // If this interval needs a register pair, the high or low equivalent.
1139 // `is_high_interval_` tells whether this holds the low or the high.
1140 LiveInterval* high_or_low_interval_;
1141
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001142 // The instruction represented by this interval.
1143 HInstruction* const defined_by_;
1144
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001145 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001146 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001147
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001148 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1149
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001150 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
1151};
1152
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001153/**
1154 * Analysis that computes the liveness of instructions:
1155 *
1156 * (a) Non-environment uses of an instruction always make
1157 * the instruction live.
Hans Boehm206348c2018-12-05 11:11:33 -08001158 * (b) Environment uses of an instruction whose type is object (that is, non-primitive), make the
1159 * instruction live, unless the class has an @DeadReferenceSafe annotation.
1160 * This avoids unexpected premature reference enqueuing or finalization, which could
1161 * result in premature deletion of native objects. In the presence of @DeadReferenceSafe,
1162 * object references are treated like primitive types.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001163 * (c) When the graph has the debuggable property, environment uses
1164 * of an instruction that has a primitive type make the instruction live.
1165 * If the graph does not have the debuggable property, the environment
1166 * use has no effect, and may get a 'none' value after register allocation.
Artem Serovd6750532018-05-30 20:07:43 +01001167 * (d) When compiling in OSR mode, all loops in the compiled method may be entered
1168 * from the interpreter via SuspendCheck; such use in SuspendCheck makes the instruction
1169 * live.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001170 *
Artem Serovd6750532018-05-30 20:07:43 +01001171 * (b), (c) and (d) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001172 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001173class SsaLivenessAnalysis : public ValueObject {
1174 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001175 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen, ScopedArenaAllocator* allocator)
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001176 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001177 codegen_(codegen),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001178 allocator_(allocator),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001179 block_infos_(graph->GetBlocks().size(),
1180 nullptr,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001181 allocator_->Adapter(kArenaAllocSsaLiveness)),
1182 instructions_from_ssa_index_(allocator_->Adapter(kArenaAllocSsaLiveness)),
1183 instructions_from_lifetime_position_(allocator_->Adapter(kArenaAllocSsaLiveness)),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001184 number_of_ssa_values_(0) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001185 }
1186
1187 void Analyze();
1188
1189 BitVector* GetLiveInSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001190 return &block_infos_[block.GetBlockId()]->live_in_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001191 }
1192
1193 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001194 return &block_infos_[block.GetBlockId()]->live_out_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001195 }
1196
1197 BitVector* GetKillSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001198 return &block_infos_[block.GetBlockId()]->kill_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001199 }
1200
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001201 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001202 return instructions_from_ssa_index_[index];
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001203 }
1204
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001205 HInstruction* GetInstructionFromPosition(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001206 return instructions_from_lifetime_position_[index];
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001207 }
1208
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001209 HBasicBlock* GetBlockFromPosition(size_t index) const {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001210 HInstruction* instruction = GetInstructionFromPosition(index);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001211 if (instruction == nullptr) {
1212 // If we are at a block boundary, get the block following.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001213 instruction = GetInstructionFromPosition(index + 1);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001214 }
1215 return instruction->GetBlock();
1216 }
1217
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001218 bool IsAtBlockBoundary(size_t index) const {
1219 return GetInstructionFromPosition(index) == nullptr;
1220 }
1221
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001222 HInstruction* GetTempUser(LiveInterval* temp) const {
1223 // A temporary shares the same lifetime start as the instruction that requires it.
1224 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001225 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
Vladimir Marko82b07402017-03-01 19:02:04 +00001226 DCHECK_EQ(user, temp->GetUses().front().GetUser());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001227 return user;
1228 }
1229
1230 size_t GetTempIndex(LiveInterval* temp) const {
1231 // We use the input index to store the index of the temporary in the user's temporary list.
1232 DCHECK(temp->IsTemp());
Vladimir Marko82b07402017-03-01 19:02:04 +00001233 return temp->GetUses().front().GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001234 }
1235
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001236 size_t GetMaxLifetimePosition() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001237 return instructions_from_lifetime_position_.size() * 2 - 1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001238 }
1239
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001240 size_t GetNumberOfSsaValues() const {
1241 return number_of_ssa_values_;
1242 }
1243
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001244 static constexpr const char* kLivenessPassName = "liveness";
1245
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001246 private:
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001247 // Give an SSA number to each instruction that defines a value used by another instruction,
1248 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001249 void NumberInstructions();
1250
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001251 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1252 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001253
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001254 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1255 // kill sets, that do not take into account backward branches.
1256 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001257
1258 // After computing the initial sets, this method does a fixed point
1259 // calculation over the live_in and live_out set to take into account
1260 // backwards branches.
1261 void ComputeLiveInAndLiveOutSets();
1262
1263 // Update the live_in set of the block and returns whether it has changed.
1264 bool UpdateLiveIn(const HBasicBlock& block);
1265
1266 // Update the live_out set of the block and returns whether it has changed.
1267 bool UpdateLiveOut(const HBasicBlock& block);
1268
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01001269 static void ProcessEnvironment(HInstruction* instruction,
1270 HInstruction* actual_user,
1271 BitVector* live_in);
1272 static void RecursivelyProcessInputs(HInstruction* instruction,
1273 HInstruction* actual_user,
1274 BitVector* live_in);
1275
Mingyao Yang718493c2015-07-22 15:56:34 -07001276 // Returns whether `instruction` in an HEnvironment held by `env_holder`
1277 // should be kept live by the HEnvironment.
Vladimir Marko356bd282017-03-01 12:01:11 +00001278 static bool ShouldBeLiveForEnvironment(HInstruction* env_holder, HInstruction* instruction) {
Artem Serovd6750532018-05-30 20:07:43 +01001279 DCHECK(instruction != nullptr);
Mingyao Yang718493c2015-07-22 15:56:34 -07001280 // A value that's not live in compiled code may still be needed in interpreter,
1281 // due to code motion, etc.
1282 if (env_holder->IsDeoptimize()) return true;
David Brazdil77a48ae2015-09-15 12:34:04 +00001283 // A value live at a throwing instruction in a try block may be copied by
1284 // the exception handler to its location at the top of the catch block.
1285 if (env_holder->CanThrowIntoCatchBlock()) return true;
Artem Serovd6750532018-05-30 20:07:43 +01001286 HGraph* graph = instruction->GetBlock()->GetGraph();
1287 if (graph->IsDebuggable()) return true;
1288 // When compiling in OSR mode, all loops in the compiled method may be entered
1289 // from the interpreter via SuspendCheck; thus we need to preserve the environment.
1290 if (env_holder->IsSuspendCheck() && graph->IsCompilingOsr()) return true;
Hans Boehm206348c2018-12-05 11:11:33 -08001291 if (graph -> IsDeadReferenceSafe()) return false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001292 return instruction->GetType() == DataType::Type::kReference;
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001293 }
1294
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +01001295 void CheckNoLiveInIrreducibleLoop(const HBasicBlock& block) const {
1296 if (!block.IsLoopHeader() || !block.GetLoopInformation()->IsIrreducible()) {
1297 return;
1298 }
1299 BitVector* live_in = GetLiveInSet(block);
1300 // To satisfy our liveness algorithm, we need to ensure loop headers of
1301 // irreducible loops do not have any live-in instructions, except constants
1302 // and the current method, which can be trivially re-materialized.
1303 for (uint32_t idx : live_in->Indexes()) {
1304 HInstruction* instruction = GetInstructionFromSsaIndex(idx);
1305 DCHECK(instruction->GetBlock()->IsEntryBlock()) << instruction->DebugName();
1306 DCHECK(!instruction->IsParameterValue());
1307 DCHECK(instruction->IsCurrentMethod() || instruction->IsConstant())
1308 << instruction->DebugName();
1309 }
1310 }
1311
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001312 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001313 CodeGenerator* const codegen_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001314
1315 // Use a local ScopedArenaAllocator for allocating memory.
1316 // This allocator must remain alive while doing register allocation.
Vladimir Marko69d310e2017-10-09 14:12:23 +01001317 ScopedArenaAllocator* const allocator_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001318
1319 ScopedArenaVector<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001320
1321 // Temporary array used when computing live_in, live_out, and kill sets.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001322 ScopedArenaVector<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001323
1324 // Temporary array used when inserting moves in the graph.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001325 ScopedArenaVector<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001326 size_t number_of_ssa_values_;
1327
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001328 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffray23a81882015-06-01 18:12:38 +01001329 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001330
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001331 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1332};
1333
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001334} // namespace art
1335
1336#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_