blob: cc2b49cf224a4ccb64f51e3f9e8afa00c0120c20 [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 Marko3fae1292019-06-07 11:26:25 +010022#include "base/intrusive_forward_list.h"
Vladimir Marko82b07402017-03-01 19:02:04 +000023#include "base/iteration_range.h"
Vladimír Marko434d9682022-11-04 14:04:17 +000024#include "base/macros.h"
Vladimir Markoe764d2e2017-10-05 14:35:55 +010025#include "base/scoped_arena_allocator.h"
26#include "base/scoped_arena_containers.h"
Vladimir Marko356bd282017-03-01 12:01:11 +000027#include "nodes.h"
28
Vladimír Marko434d9682022-11-04 14:04:17 +000029namespace art HIDDEN {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010030
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010031class CodeGenerator;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +010032class SsaLivenessAnalysis;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010033
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010034static constexpr int kNoRegister = -1;
35
Vladimir Marko5233f932015-09-29 19:01:15 +010036class BlockInfo : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010037 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +010038 BlockInfo(ScopedArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
Nicolas Geoffray804d0932014-05-02 08:46:00 +010039 : block_(block),
Vladimir Markof6a35de2016-03-21 12:01:50 +000040 live_in_(allocator, number_of_ssa_values, false, kArenaAllocSsaLiveness),
41 live_out_(allocator, number_of_ssa_values, false, kArenaAllocSsaLiveness),
42 kill_(allocator, number_of_ssa_values, false, kArenaAllocSsaLiveness) {
Ian Rogerscf7f1912014-10-22 22:06:39 -070043 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010044 live_in_.ClearAllBits();
45 live_out_.ClearAllBits();
46 kill_.ClearAllBits();
47 }
48
49 private:
50 const HBasicBlock& block_;
51 ArenaBitVector live_in_;
52 ArenaBitVector live_out_;
53 ArenaBitVector kill_;
54
55 friend class SsaLivenessAnalysis;
56
57 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
58};
59
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010061 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010062 * is live.
63 */
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010064class LiveRange final : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010065 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010066 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010067 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010068 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010069 }
70
71 size_t GetStart() const { return start_; }
72 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010073 LiveRange* GetNext() const { return next_; }
74
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070075 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010076 return (start_ >= other.start_ && start_ < other.end_)
77 || (other.start_ >= start_ && other.start_ < end_);
78 }
79
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070080 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010081 return end_ <= other.start_;
82 }
83
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070084 void Dump(std::ostream& stream) const {
David Brazdilc7a24852015-05-15 16:44:05 +010085 stream << "[" << start_ << "," << end_ << ")";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010086 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010087
Vladimir Markoe764d2e2017-10-05 14:35:55 +010088 LiveRange* Dup(ScopedArenaAllocator* allocator) const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +000089 return new (allocator) LiveRange(
90 start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator));
91 }
92
93 LiveRange* GetLastRange() {
94 return next_ == nullptr ? this : next_->GetLastRange();
95 }
96
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010097 private:
98 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010099 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100 LiveRange* next_;
101
102 friend class LiveInterval;
103
104 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100105};
106
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100107/**
108 * A use position represents a live interval use at a given position.
109 */
Vladimir Marko82b07402017-03-01 19:02:04 +0000110class UsePosition : public ArenaObject<kArenaAllocSsaLiveness>,
111 public IntrusiveForwardListNode<UsePosition> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100112 public:
Vladimir Marko82b07402017-03-01 19:02:04 +0000113 UsePosition(HInstruction* user, size_t input_index, size_t position)
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100114 : user_(user),
115 input_index_(input_index),
Vladimir Marko82b07402017-03-01 19:02:04 +0000116 position_(position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100117 }
118
Vladimir Marko356bd282017-03-01 12:01:11 +0000119 explicit UsePosition(size_t position)
120 : user_(nullptr),
121 input_index_(kNoInput),
Vladimir Marko82b07402017-03-01 19:02:04 +0000122 position_(dchecked_integral_cast<uint32_t>(position)) {
Vladimir Marko356bd282017-03-01 12:01:11 +0000123 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100124
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125 size_t GetPosition() const { return position_; }
126
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100127 HInstruction* GetUser() const { return user_; }
128
Nicolas Geoffray57902602015-04-21 14:28:41 +0100129 bool IsSynthesized() const { return user_ == nullptr; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100130
131 size_t GetInputIndex() const { return input_index_; }
132
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100133 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100134 stream << position_;
Nicolas Geoffray57902602015-04-21 14:28:41 +0100135 }
136
137 HLoopInformation* GetLoopInformation() const {
138 return user_->GetBlock()->GetLoopInformation();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100139 }
140
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100141 UsePosition* Clone(ScopedArenaAllocator* allocator) const {
Vladimir Marko82b07402017-03-01 19:02:04 +0000142 return new (allocator) UsePosition(user_, input_index_, position_);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000143 }
144
Nicolas Geoffray57902602015-04-21 14:28:41 +0100145 bool RequiresRegister() const {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100146 if (IsSynthesized()) return false;
147 Location location = GetUser()->GetLocations()->InAt(GetInputIndex());
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700148 return location.IsUnallocated() && location.RequiresRegisterKind();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100149 }
150
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100151 private:
Vladimir Marko356bd282017-03-01 12:01:11 +0000152 static constexpr uint32_t kNoInput = static_cast<uint32_t>(-1);
153
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100154 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100155 const size_t input_index_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100156 const size_t position_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100157
158 DISALLOW_COPY_AND_ASSIGN(UsePosition);
159};
Vladimir Marko82b07402017-03-01 19:02:04 +0000160using UsePositionList = IntrusiveForwardList<UsePosition>;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100161
Vladimir Marko356bd282017-03-01 12:01:11 +0000162/**
163 * An environment use position represents a live interval for environment use at a given position.
164 */
Vladimir Marko82b07402017-03-01 19:02:04 +0000165class EnvUsePosition : public ArenaObject<kArenaAllocSsaLiveness>,
166 public IntrusiveForwardListNode<EnvUsePosition> {
Vladimir Marko356bd282017-03-01 12:01:11 +0000167 public:
168 EnvUsePosition(HEnvironment* environment,
169 size_t input_index,
Vladimir Marko82b07402017-03-01 19:02:04 +0000170 size_t position)
Vladimir Marko356bd282017-03-01 12:01:11 +0000171 : environment_(environment),
172 input_index_(input_index),
Vladimir Marko82b07402017-03-01 19:02:04 +0000173 position_(position) {
Vladimir Marko356bd282017-03-01 12:01:11 +0000174 DCHECK(environment != nullptr);
Vladimir Marko356bd282017-03-01 12:01:11 +0000175 }
176
177 size_t GetPosition() const { return position_; }
178
Vladimir Marko356bd282017-03-01 12:01:11 +0000179 HEnvironment* GetEnvironment() const { return environment_; }
180 size_t GetInputIndex() const { return input_index_; }
181
182 void Dump(std::ostream& stream) const {
183 stream << position_;
184 }
185
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100186 EnvUsePosition* Clone(ScopedArenaAllocator* allocator) const {
Vladimir Marko82b07402017-03-01 19:02:04 +0000187 return new (allocator) EnvUsePosition(environment_, input_index_, position_);
Vladimir Marko356bd282017-03-01 12:01:11 +0000188 }
189
190 private:
191 HEnvironment* const environment_;
192 const size_t input_index_;
193 const size_t position_;
Vladimir Marko356bd282017-03-01 12:01:11 +0000194
195 DISALLOW_COPY_AND_ASSIGN(EnvUsePosition);
196};
Vladimir Marko82b07402017-03-01 19:02:04 +0000197using EnvUsePositionList = IntrusiveForwardList<EnvUsePosition>;
198
199template <typename Iterator>
200inline Iterator FindUseAtOrAfterPosition(Iterator first, Iterator last, size_t position) {
201 using value_type = const typename Iterator::value_type;
202 static_assert(std::is_same<value_type, const UsePosition>::value ||
203 std::is_same<value_type, const EnvUsePosition>::value,
204 "Expecting value type UsePosition or EnvUsePosition.");
205 Iterator ret = std::find_if(
206 first, last, [position](const value_type& use) { return use.GetPosition() >= position; });
207 // Check that the processed range is sorted. Do not check the rest of the range to avoid
208 // increasing the complexity of callers from O(n) to O(n^2).
209 DCHECK(std::is_sorted(
210 first,
211 ret,
212 [](const value_type& lhs, const value_type& rhs) {
213 return lhs.GetPosition() < rhs.GetPosition();
214 }));
215 return ret;
216}
217
218template <typename Iterator>
219inline IterationRange<Iterator> FindMatchingUseRange(Iterator first,
220 Iterator last,
221 size_t position_begin,
222 size_t position_end) {
223 Iterator begin = FindUseAtOrAfterPosition(first, last, position_begin);
224 Iterator end = FindUseAtOrAfterPosition(begin, last, position_end);
225 return MakeIterationRange(begin, end);
226}
Vladimir Marko356bd282017-03-01 12:01:11 +0000227
Vladimir Marko5233f932015-09-29 19:01:15 +0100228class SafepointPosition : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100229 public:
230 explicit SafepointPosition(HInstruction* instruction)
231 : instruction_(instruction),
232 next_(nullptr) {}
233
Nicolas Geoffray2aee3af2018-08-27 20:56:45 +0100234 static size_t ComputePosition(HInstruction* instruction) {
235 // We special case instructions emitted at use site, as their
236 // safepoint position needs to be at their use.
237 if (instruction->IsEmittedAtUseSite()) {
238 // Currently only applies to implicit null checks, which are emitted
239 // at the next instruction.
240 DCHECK(instruction->IsNullCheck()) << instruction->DebugName();
241 return instruction->GetLifetimePosition() + 2;
242 } else {
243 return instruction->GetLifetimePosition();
244 }
245 }
246
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100247 void SetNext(SafepointPosition* next) {
248 next_ = next;
249 }
250
251 size_t GetPosition() const {
Nicolas Geoffray2aee3af2018-08-27 20:56:45 +0100252 return ComputePosition(instruction_);
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100253 }
254
255 SafepointPosition* GetNext() const {
256 return next_;
257 }
258
259 LocationSummary* GetLocations() const {
260 return instruction_->GetLocations();
261 }
262
263 HInstruction* GetInstruction() const {
264 return instruction_;
265 }
266
267 private:
268 HInstruction* const instruction_;
269 SafepointPosition* next_;
270
271 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
272};
273
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274/**
275 * An interval is a list of disjoint live ranges where an instruction is live.
276 * Each instruction that has uses gets an interval.
277 */
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100278class LiveInterval : public ArenaObject<kArenaAllocSsaLiveness> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100279 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100280 static LiveInterval* MakeInterval(ScopedArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100281 DataType::Type type,
Mingyao Yang296bd602014-10-06 16:47:28 -0700282 HInstruction* instruction = nullptr) {
283 return new (allocator) LiveInterval(allocator, type, instruction);
284 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100285
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100286 static LiveInterval* MakeFixedInterval(ScopedArenaAllocator* allocator,
287 int reg,
288 DataType::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100289 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
290 }
291
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100292 static LiveInterval* MakeTempInterval(ScopedArenaAllocator* allocator, DataType::Type type) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100293 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100294 }
295
296 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700297 bool IsTemp() const { return is_temp_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700298 // This interval is the result of a split.
299 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100300
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000301 void AddTempUse(HInstruction* instruction, size_t temp_index) {
302 DCHECK(IsTemp());
Vladimir Marko82b07402017-03-01 19:02:04 +0000303 DCHECK(GetUses().empty()) << "A temporary can only have one user";
304 DCHECK(GetEnvironmentUses().empty()) << "A temporary cannot have environment user";
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000305 size_t position = instruction->GetLifetimePosition();
Vladimir Marko82b07402017-03-01 19:02:04 +0000306 UsePosition* new_use = new (allocator_) UsePosition(instruction, temp_index, position);
307 uses_.push_front(*new_use);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000308 AddRange(position, position + 1);
309 }
310
David Brazdilb3e773e2016-01-26 11:28:37 +0000311 // Record use of an input. The use will be recorded as an environment use if
312 // `environment` is not null and as register use otherwise. If `actual_user`
313 // is specified, the use will be recorded at `actual_user`'s lifetime position.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000314 void AddUse(HInstruction* instruction,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100315 HEnvironment* environment,
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000316 size_t input_index,
Artem Serovd6750532018-05-30 20:07:43 +0100317 HInstruction* actual_user = nullptr) {
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100318 bool is_environment = (environment != nullptr);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000319 LocationSummary* locations = instruction->GetLocations();
David Brazdilb3e773e2016-01-26 11:28:37 +0000320 if (actual_user == nullptr) {
321 actual_user = instruction;
322 }
323
324 // Set the use within the instruction.
325 size_t position = actual_user->GetLifetimePosition() + 1;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000326 if (!is_environment) {
327 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
328 // For fixed inputs and output same as input, the register allocator
329 // requires to have inputs die at the instruction, so that input moves use the
330 // location of the input just before that instruction (and not potential moves due
331 // to splitting).
David Brazdilb3e773e2016-01-26 11:28:37 +0000332 DCHECK_EQ(instruction, actual_user);
333 position = actual_user->GetLifetimePosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100334 } else if (!locations->InAt(input_index).IsValid()) {
335 return;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000336 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100337 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000338
Nicolas Geoffray57902602015-04-21 14:28:41 +0100339 if (!is_environment && instruction->IsInLoop()) {
340 AddBackEdgeUses(*instruction->GetBlock());
341 }
342
Vladimir Marko82b07402017-03-01 19:02:04 +0000343 if ((!uses_.empty()) &&
344 (uses_.front().GetUser() == actual_user) &&
345 (uses_.front().GetPosition() < position)) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100346 // The user uses the instruction multiple times, and one use dies before the other.
347 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000348 DCHECK(!is_environment);
Vladimir Marko82b07402017-03-01 19:02:04 +0000349 DCHECK(uses_.front().GetPosition() + 1 == position);
350 UsePositionList::iterator next_pos = uses_.begin();
351 UsePositionList::iterator insert_pos;
352 do {
353 insert_pos = next_pos;
354 ++next_pos;
355 } while (next_pos != uses_.end() && next_pos->GetPosition() < position);
356 UsePosition* new_use = new (allocator_) UsePosition(instruction, input_index, position);
357 uses_.insert_after(insert_pos, *new_use);
358 if (first_range_->GetEnd() == uses_.front().GetPosition()) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100359 first_range_->end_ = position;
360 }
361 return;
362 }
363
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100364 if (is_environment) {
Vladimir Marko82b07402017-03-01 19:02:04 +0000365 DCHECK(env_uses_.empty() || position <= env_uses_.front().GetPosition());
366 EnvUsePosition* new_env_use =
367 new (allocator_) EnvUsePosition(environment, input_index, position);
368 env_uses_.push_front(*new_env_use);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100369 } else {
Vladimir Marko82b07402017-03-01 19:02:04 +0000370 DCHECK(uses_.empty() || position <= uses_.front().GetPosition());
371 UsePosition* new_use = new (allocator_) UsePosition(instruction, input_index, position);
372 uses_.push_front(*new_use);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100373 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000374
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100375 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100376 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100377 // First time we see a use of that interval.
David Brazdil3fc992f2015-04-16 18:31:55 +0100378 first_range_ = last_range_ = range_search_start_ =
379 new (allocator_) LiveRange(start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100380 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100381 // There is a use later in the same block or in a following block.
382 // Note that in such a case, `AddRange` for the whole blocks has been called
383 // before arriving in this method, and this is the reason the start of
384 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100385 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100386 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100387 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100388 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100389 // Note that the start of `first_range_` can be equal to `end`: two blocks
390 // having adjacent lifetime positions are not necessarily
391 // predecessor/successor. When two blocks are predecessor/successor, the
392 // liveness algorithm has called `AddRange` before arriving in this method,
393 // and the check line 205 would succeed.
David Brazdil3fc992f2015-04-16 18:31:55 +0100394 first_range_ = range_search_start_ =
395 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100396 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100397 }
398
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100399 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100400 DCHECK(instruction->IsPhi());
Nicolas Geoffray57902602015-04-21 14:28:41 +0100401 if (block->IsInLoop()) {
402 AddBackEdgeUses(*block);
403 }
Vladimir Marko82b07402017-03-01 19:02:04 +0000404 UsePosition* new_use =
405 new (allocator_) UsePosition(instruction, input_index, block->GetLifetimeEnd());
406 uses_.push_front(*new_use);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100407 }
408
Mingyao Yang01b47b02017-02-03 12:09:57 -0800409 ALWAYS_INLINE void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100410 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100411 first_range_ = last_range_ = range_search_start_ =
412 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100413 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100414 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100415 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100416 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
417 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100418 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100419 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100420 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100421 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100422 }
423 }
424
425 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100426 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000427 DCHECK_LE(start, first_range_->GetStart());
428 // Find the range that covers the positions after the loop.
429 LiveRange* after_loop = first_range_;
430 LiveRange* last_in_loop = nullptr;
431 while (after_loop != nullptr && after_loop->GetEnd() < end) {
432 DCHECK_LE(start, after_loop->GetStart());
433 last_in_loop = after_loop;
434 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100435 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000436 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100437 // Uses are only in the loop.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700438 first_range_ = last_range_ = range_search_start_ =
439 new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000440 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100441 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100442 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100443 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000444 } else {
445 // The use after the loop is after a lifetime hole.
446 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100447 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000448 first_range_->start_ = start;
449 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100450 }
451 }
452
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100453 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100454 void SetSpillSlot(int slot) {
455 DCHECK(!is_fixed_);
456 DCHECK(!is_temp_);
457 spill_slot_ = slot;
458 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100459 int GetSpillSlot() const { return spill_slot_; }
460
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100461 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100462 if (first_range_ != nullptr) {
463 first_range_->start_ = from;
464 } else {
465 // Instruction without uses.
Vladimir Marko82b07402017-03-01 19:02:04 +0000466 DCHECK(uses_.empty());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100467 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100468 first_range_ = last_range_ = range_search_start_ =
469 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100470 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100471 }
472
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100473 LiveInterval* GetParent() const { return parent_; }
474
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100475 // Returns whether this interval is the parent interval, that is, the interval
476 // that starts where the HInstruction is defined.
477 bool IsParent() const { return parent_ == this; }
478
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100479 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000480 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100481
482 int GetRegister() const { return register_; }
483 void SetRegister(int reg) { register_ = reg; }
484 void ClearRegister() { register_ = kNoRegister; }
485 bool HasRegister() const { return register_ != kNoRegister; }
486
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100487 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100488 return GetEnd() <= position;
489 }
490
491 bool IsDefinedAt(size_t position) const {
492 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100493 }
494
David Brazdil3fc992f2015-04-16 18:31:55 +0100495 // Returns true if the interval contains a LiveRange covering `position`.
496 // The range at or immediately after the current position of linear scan
497 // is cached for better performance. If `position` can be smaller than
498 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000499 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100500 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
501 range_search_start_ = candidate;
502 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100503 }
504
David Brazdil3fc992f2015-04-16 18:31:55 +0100505 // Same as Covers but always tests all ranges.
506 bool CoversSlow(size_t position) const {
507 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
508 return candidate != nullptr && candidate->GetStart() <= position;
509 }
510
511 // Returns the first intersection of this interval with `current`, which
512 // must be the interval currently being allocated by linear scan.
513 size_t FirstIntersectionWith(LiveInterval* current) const {
514 // Find the first range after the start of `current`. We use the search
515 // cache to improve performance.
516 DCHECK(GetStart() <= current->GetStart() || IsFixed());
517 LiveRange* other_range = current->first_range_;
518 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
519 if (my_range == nullptr) {
520 return kNoLifetime;
521 }
522
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100523 // Advance both intervals and find the first matching range start in
524 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100525 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000526 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100527 my_range = my_range->GetNext();
528 if (my_range == nullptr) {
529 return kNoLifetime;
530 }
David Brazdil714e14f2015-02-25 11:57:05 +0000531 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100532 other_range = other_range->GetNext();
533 if (other_range == nullptr) {
534 return kNoLifetime;
535 }
David Brazdil714e14f2015-02-25 11:57:05 +0000536 } else {
537 DCHECK(my_range->IntersectsWith(*other_range));
538 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100539 }
540 } while (true);
541 }
542
543 size_t GetStart() const {
544 return first_range_->GetStart();
545 }
546
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100547 size_t GetEnd() const {
548 return last_range_->GetEnd();
549 }
550
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700551 size_t GetLength() const {
552 return GetEnd() - GetStart();
553 }
554
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100555 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100556 if (is_temp_) {
557 return position == GetStart() ? position : kNoLifetime;
558 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100559
560 if (IsDefiningPosition(position) && DefinitionRequiresRegister()) {
561 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100562 }
563
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100564 size_t end = GetEnd();
Vladimir Marko82b07402017-03-01 19:02:04 +0000565 for (const UsePosition& use : GetUses()) {
566 size_t use_position = use.GetPosition();
567 if (use_position > end) {
568 break;
569 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100570 if (use_position > position) {
Vladimir Marko82b07402017-03-01 19:02:04 +0000571 if (use.RequiresRegister()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100572 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100573 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100574 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100575 }
576 return kNoLifetime;
577 }
578
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700579 // Returns the location of the first register use for this live interval,
580 // including a register definition if applicable.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100581 size_t FirstRegisterUse() const {
582 return FirstRegisterUseAfter(GetStart());
583 }
584
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700585 // Whether the interval requires a register rather than a stack location.
586 // If needed for performance, this could be cached.
Matthew Gharrity2ccae4a2016-08-12 16:10:45 +0000587 bool RequiresRegister() const {
588 return !HasRegister() && FirstRegisterUse() != kNoLifetime;
589 }
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700590
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000591 size_t FirstUseAfter(size_t position) const {
592 if (is_temp_) {
593 return position == GetStart() ? position : kNoLifetime;
594 }
595
Nicolas Geoffray57902602015-04-21 14:28:41 +0100596 if (IsDefiningPosition(position)) {
597 DCHECK(defined_by_->GetLocations()->Out().IsValid());
598 return position;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100599 }
600
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000601 size_t end = GetEnd();
Vladimir Marko82b07402017-03-01 19:02:04 +0000602 for (const UsePosition& use : GetUses()) {
603 size_t use_position = use.GetPosition();
604 if (use_position > end) {
605 break;
606 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100607 if (use_position > position) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100608 return use_position;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000609 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000610 }
611 return kNoLifetime;
612 }
613
Vladimir Marko82b07402017-03-01 19:02:04 +0000614 const UsePositionList& GetUses() const {
615 return parent_->uses_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100616 }
617
Vladimir Marko82b07402017-03-01 19:02:04 +0000618 const EnvUsePositionList& GetEnvironmentUses() const {
619 return parent_->env_uses_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100620 }
621
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100622 DataType::Type GetType() const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100623 return type_;
624 }
625
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100626 HInstruction* GetDefinedBy() const {
627 return defined_by_;
628 }
629
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100630 bool HasWillCallSafepoint() const {
631 for (SafepointPosition* safepoint = first_safepoint_;
632 safepoint != nullptr;
633 safepoint = safepoint->GetNext()) {
634 if (safepoint->GetLocations()->WillCall()) return true;
635 }
636 return false;
637 }
638
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100639 SafepointPosition* FindSafepointJustBefore(size_t position) const {
640 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
641 safepoint != nullptr;
642 previous = safepoint, safepoint = safepoint->GetNext()) {
643 if (safepoint->GetPosition() >= position) return previous;
644 }
645 return last_safepoint_;
646 }
647
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100648 /**
649 * Split this interval at `position`. This interval is changed to:
650 * [start ... position).
651 *
652 * The new interval covers:
653 * [position ... end)
654 */
655 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100656 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100657 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100658 DCHECK_GT(position, GetStart());
659
David Brazdil241a4862015-04-16 17:59:03 +0100660 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100661 // This range dies before `position`, no need to split.
662 return nullptr;
663 }
664
665 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100666 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
667 if (new_last_safepoint == nullptr) {
668 new_interval->first_safepoint_ = first_safepoint_;
669 new_interval->last_safepoint_ = last_safepoint_;
670 first_safepoint_ = last_safepoint_ = nullptr;
671 } else if (last_safepoint_ != new_last_safepoint) {
672 new_interval->last_safepoint_ = last_safepoint_;
673 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
674 DCHECK(new_interval->first_safepoint_ != nullptr);
675 last_safepoint_ = new_last_safepoint;
676 last_safepoint_->SetNext(nullptr);
677 }
678
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100679 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100680 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100681 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100682
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100683 LiveRange* current = first_range_;
684 LiveRange* previous = nullptr;
685 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000686 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100687 do {
688 if (position >= current->GetEnd()) {
689 // Move to next range.
690 previous = current;
691 current = current->next_;
692 } else if (position <= current->GetStart()) {
693 // If the previous range did not cover this position, we know position is in
694 // a lifetime hole. We can just break the first_range_ and last_range_ links
695 // and return the new interval.
696 DCHECK(previous != nullptr);
697 DCHECK(current != first_range_);
698 new_interval->last_range_ = last_range_;
699 last_range_ = previous;
700 previous->next_ = nullptr;
701 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100702 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700703 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100704 // (i.e. the end of the interval) in the original interval.
705 range_search_start_ = nullptr;
706 }
707 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100708 return new_interval;
709 } else {
710 // This range covers position. We create a new last_range_ for this interval
711 // that covers last_range_->Start() and position. We also shorten the current
712 // range and make it the first range of the new interval.
713 DCHECK(position < current->GetEnd() && position > current->GetStart());
714 new_interval->last_range_ = last_range_;
715 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
716 if (previous != nullptr) {
717 previous->next_ = last_range_;
718 } else {
719 first_range_ = last_range_;
720 }
721 new_interval->first_range_ = current;
722 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100723 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
724 // Search start point is inside `new_interval`. Change it to `last_range`
725 // in the original interval. This is conservative but always correct.
726 range_search_start_ = last_range_;
727 }
728 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100729 return new_interval;
730 }
731 } while (current != nullptr);
732
733 LOG(FATAL) << "Unreachable";
734 return nullptr;
735 }
736
Nicolas Geoffray76905622014-09-25 14:39:26 +0100737 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100738 return GetStart() <= other->GetStart();
739 }
740
741 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100742 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100743 }
744
745 void Dump(std::ostream& stream) const {
746 stream << "ranges: { ";
747 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000748 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749 current->Dump(stream);
750 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000751 current = current->GetNext();
752 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100753 stream << "}, uses: { ";
Vladimir Marko82b07402017-03-01 19:02:04 +0000754 for (const UsePosition& use : GetUses()) {
755 use.Dump(stream);
756 stream << " ";
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100757 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100758 stream << "}, { ";
Vladimir Marko82b07402017-03-01 19:02:04 +0000759 for (const EnvUsePosition& env_use : GetEnvironmentUses()) {
760 env_use.Dump(stream);
761 stream << " ";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100762 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100763 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700764 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000765 stream << " is_low: " << IsLowInterval();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100766 stream << " is_high: " << IsHighInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100767 }
768
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700769 // Same as Dump, but adds context such as the instruction defining this interval, and
770 // the register currently assigned to this interval.
771 void DumpWithContext(std::ostream& stream, const CodeGenerator& codegen) const;
772
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100773 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000774 LiveInterval* GetLastSibling() {
775 LiveInterval* result = this;
776 while (result->next_sibling_ != nullptr) {
777 result = result->next_sibling_;
778 }
779 return result;
780 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100781
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100782 // Returns the first register hint that is at least free before
783 // the value contained in `free_until`. If none is found, returns
784 // `kNoRegister`.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100785 int FindFirstRegisterHint(size_t* free_until, const SsaLivenessAnalysis& liveness) const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100786
787 // If there is enough at the definition site to find a register (for example
788 // it uses the same input as the first input), returns the register as a hint.
789 // Returns kNoRegister otherwise.
790 int FindHintAtDefinition() const;
791
Aart Bikcc895252017-03-21 10:55:15 -0700792 // Returns the number of required spilling slots (measured as a multiple of the
793 // Dex virtual register size `kVRegSize`).
794 size_t NumberOfSpillSlotsNeeded() const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100795
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100796 bool IsFloatingPoint() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100797 return type_ == DataType::Type::kFloat32 || type_ == DataType::Type::kFloat64;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100798 }
799
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100800 // Converts the location of the interval to a `Location` object.
801 Location ToLocation() const;
802
803 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000804 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100805
David Brazdil241a4862015-04-16 17:59:03 +0100806 // Finds the sibling that is defined at `position`.
807 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100808
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100809 // Returns whether `other` and `this` share the same kind of register.
810 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000811 bool SameRegisterKind(const LiveInterval& other) const {
812 return IsFloatingPoint() == other.IsFloatingPoint();
813 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100814
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000815 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000816 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000817 }
818
819 bool HasLowInterval() const {
820 return IsHighInterval();
821 }
822
823 LiveInterval* GetLowInterval() const {
824 DCHECK(HasLowInterval());
825 return high_or_low_interval_;
826 }
827
828 LiveInterval* GetHighInterval() const {
829 DCHECK(HasHighInterval());
830 return high_or_low_interval_;
831 }
832
833 bool IsHighInterval() const {
834 return GetParent()->is_high_interval_;
835 }
836
837 bool IsLowInterval() const {
838 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
839 }
840
841 void SetLowInterval(LiveInterval* low) {
842 DCHECK(IsHighInterval());
843 high_or_low_interval_ = low;
844 }
845
846 void SetHighInterval(LiveInterval* high) {
847 DCHECK(IsLowInterval());
848 high_or_low_interval_ = high;
849 }
850
851 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100852 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000853 DCHECK(!HasHighInterval());
854 DCHECK(!HasLowInterval());
855 high_or_low_interval_ = new (allocator_) LiveInterval(
Vladimir Marko70e97462016-08-09 11:04:26 +0100856 allocator_, type_, defined_by_, false, kNoRegister, is_temp, true);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000857 high_or_low_interval_->high_or_low_interval_ = this;
858 if (first_range_ != nullptr) {
859 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100860 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100861 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000862 }
Vladimir Marko82b07402017-03-01 19:02:04 +0000863 auto pos = high_or_low_interval_->uses_.before_begin();
864 for (const UsePosition& use : uses_) {
865 UsePosition* new_use = use.Clone(allocator_);
866 pos = high_or_low_interval_->uses_.insert_after(pos, *new_use);
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000867 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100868
Vladimir Marko82b07402017-03-01 19:02:04 +0000869 auto env_pos = high_or_low_interval_->env_uses_.before_begin();
870 for (const EnvUsePosition& env_use : env_uses_) {
871 EnvUsePosition* new_env_use = env_use.Clone(allocator_);
872 env_pos = high_or_low_interval_->env_uses_.insert_after(env_pos, *new_env_use);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100873 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000874 }
875
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000876 // Returns whether an interval, when it is non-split, is using
Roland Levillainf0409142021-03-22 15:45:03 +0000877 // the same register of one of its input. This function should
878 // be used only for DCHECKs.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000879 bool IsUsingInputRegister() const {
880 if (defined_by_ != nullptr && !IsSplit()) {
Vladimir Marko372f10e2016-05-17 16:30:10 +0100881 for (const HInstruction* input : defined_by_->GetInputs()) {
882 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000883
David Brazdil3fc992f2015-04-16 18:31:55 +0100884 // Find the interval that covers `defined_by`_. Calls to this function
885 // are made outside the linear scan, hence we need to use CoversSlow.
886 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000887 interval = interval->GetNextSibling();
888 }
889
890 // Check if both intervals have the same register of the same kind.
891 if (interval != nullptr
892 && interval->SameRegisterKind(*this)
893 && interval->GetRegister() == GetRegister()) {
894 return true;
895 }
896 }
897 }
898 return false;
899 }
900
901 // Returns whether an interval, when it is non-split, can safely use
902 // the same register of one of its input. Note that this method requires
Roland Levillainf0409142021-03-22 15:45:03 +0000903 // IsUsingInputRegister() to be true. This function should be used only
904 // for DCHECKs.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000905 bool CanUseInputRegister() const {
906 DCHECK(IsUsingInputRegister());
907 if (defined_by_ != nullptr && !IsSplit()) {
908 LocationSummary* locations = defined_by_->GetLocations();
909 if (locations->OutputCanOverlapWithInputs()) {
910 return false;
911 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100912 for (const HInstruction* input : defined_by_->GetInputs()) {
913 LiveInterval* interval = input->GetLiveInterval();
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000914
David Brazdil3fc992f2015-04-16 18:31:55 +0100915 // Find the interval that covers `defined_by`_. Calls to this function
916 // are made outside the linear scan, hence we need to use CoversSlow.
917 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000918 interval = interval->GetNextSibling();
919 }
920
921 if (interval != nullptr
922 && interval->SameRegisterKind(*this)
923 && interval->GetRegister() == GetRegister()) {
924 // We found the input that has the same register. Check if it is live after
925 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100926 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000927 }
928 }
929 }
930 LOG(FATAL) << "Unreachable";
931 UNREACHABLE();
932 }
933
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100934 void AddSafepoint(HInstruction* instruction) {
935 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
936 if (first_safepoint_ == nullptr) {
937 first_safepoint_ = last_safepoint_ = safepoint;
938 } else {
Nicolas Geoffray2aee3af2018-08-27 20:56:45 +0100939 DCHECK_LE(last_safepoint_->GetPosition(), safepoint->GetPosition());
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100940 last_safepoint_->SetNext(safepoint);
941 last_safepoint_ = safepoint;
942 }
943 }
944
945 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100946 return first_safepoint_;
947 }
948
David Brazdil3fc992f2015-04-16 18:31:55 +0100949 // Resets the starting point for range-searching queries to the first range.
950 // Intervals must be reset prior to starting a new linear scan over them.
951 void ResetSearchCache() {
952 range_search_start_ = first_range_;
953 }
954
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700955 bool DefinitionRequiresRegister() const {
956 DCHECK(IsParent());
957 LocationSummary* locations = defined_by_->GetLocations();
958 Location location = locations->Out();
959 // This interval is the first interval of the instruction. If the output
960 // of the instruction requires a register, we return the position of that instruction
961 // as the first register use.
962 if (location.IsUnallocated()) {
963 if ((location.GetPolicy() == Location::kRequiresRegister)
964 || (location.GetPolicy() == Location::kSameAsFirstInput
965 && (locations->InAt(0).IsRegister()
966 || locations->InAt(0).IsRegisterPair()
967 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
968 return true;
969 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
970 || (location.GetPolicy() == Location::kSameAsFirstInput
971 && (locations->InAt(0).IsFpuRegister()
972 || locations->InAt(0).IsFpuRegisterPair()
973 || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) {
974 return true;
975 }
976 } else if (location.IsRegister() || location.IsRegisterPair()) {
977 return true;
978 }
979 return false;
980 }
981
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100982 private:
Vladimir Markoe764d2e2017-10-05 14:35:55 +0100983 LiveInterval(ScopedArenaAllocator* allocator,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100984 DataType::Type type,
Mingyao Yang296bd602014-10-06 16:47:28 -0700985 HInstruction* defined_by = nullptr,
986 bool is_fixed = false,
987 int reg = kNoRegister,
988 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000989 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700990 : allocator_(allocator),
991 first_range_(nullptr),
992 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100993 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100994 first_safepoint_(nullptr),
995 last_safepoint_(nullptr),
Vladimir Marko82b07402017-03-01 19:02:04 +0000996 uses_(),
997 env_uses_(),
Mingyao Yang296bd602014-10-06 16:47:28 -0700998 type_(type),
999 next_sibling_(nullptr),
1000 parent_(this),
1001 register_(reg),
1002 spill_slot_(kNoSpillSlot),
1003 is_fixed_(is_fixed),
1004 is_temp_(is_temp),
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001005 is_high_interval_(is_high_interval),
1006 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -07001007 defined_by_(defined_by) {}
1008
David Brazdil3fc992f2015-04-16 18:31:55 +01001009 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001010 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +01001011 // known to end before `position` can be skipped with `search_start`.
1012 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +00001013 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +01001014 if (search_start != first_range_) {
1015 // If we are not searching the entire list of ranges, make sure we do
1016 // not skip the range we are searching for.
1017 if (search_start == nullptr) {
1018 DCHECK(IsDeadAt(position));
1019 } else if (search_start->GetStart() > position) {
1020 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
1021 }
David Brazdil5b8e6a52015-02-25 16:17:05 +00001022 }
1023 }
1024
David Brazdil3fc992f2015-04-16 18:31:55 +01001025 LiveRange* range;
1026 for (range = search_start;
1027 range != nullptr && range->GetEnd() <= position;
1028 range = range->GetNext()) {
1029 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +00001030 }
David Brazdil3fc992f2015-04-16 18:31:55 +01001031 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +00001032 }
1033
Nicolas Geoffray57902602015-04-21 14:28:41 +01001034 bool IsDefiningPosition(size_t position) const {
1035 return IsParent() && (position == GetStart());
1036 }
1037
1038 bool HasSynthesizeUseAt(size_t position) const {
Vladimir Marko82b07402017-03-01 19:02:04 +00001039 for (const UsePosition& use : GetUses()) {
1040 size_t use_position = use.GetPosition();
1041 if ((use_position == position) && use.IsSynthesized()) {
Nicolas Geoffray57902602015-04-21 14:28:41 +01001042 return true;
1043 }
1044 if (use_position > position) break;
Nicolas Geoffray57902602015-04-21 14:28:41 +01001045 }
1046 return false;
1047 }
1048
1049 void AddBackEdgeUses(const HBasicBlock& block_at_use) {
1050 DCHECK(block_at_use.IsInLoop());
David Brazdil07b35102016-04-27 15:33:22 +01001051 if (block_at_use.GetGraph()->HasIrreducibleLoops()) {
1052 // Linear order may not be well formed when irreducible loops are present,
1053 // i.e. loop blocks may not be adjacent and a back edge may not be last,
1054 // which violates assumptions made in this method.
1055 return;
1056 }
1057
Nicolas Geoffray57902602015-04-21 14:28:41 +01001058 // Add synthesized uses at the back edge of loops to help the register allocator.
1059 // Note that this method is called in decreasing liveness order, to faciliate adding
Vladimir Marko82b07402017-03-01 19:02:04 +00001060 // uses at the head of the `uses_` list. Because below
Nicolas Geoffray57902602015-04-21 14:28:41 +01001061 // we iterate from inner-most to outer-most, which is in increasing liveness order,
Vladimir Marko82b07402017-03-01 19:02:04 +00001062 // we need to add subsequent entries after the last inserted entry.
1063 const UsePositionList::iterator old_begin = uses_.begin();
1064 UsePositionList::iterator insert_pos = uses_.before_begin();
Nicolas Geoffray57902602015-04-21 14:28:41 +01001065 for (HLoopInformationOutwardIterator it(block_at_use);
1066 !it.Done();
1067 it.Advance()) {
1068 HLoopInformation* current = it.Current();
1069 if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) {
1070 // This interval is defined in the loop. We can stop going outward.
1071 break;
1072 }
1073
Vladimir Marko82b07402017-03-01 19:02:04 +00001074 // We're only adding a synthesized use at the last back edge. Adding synthesized uses on
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01001075 // all back edges is not necessary: anything used in the loop will have its use at the
1076 // last back edge. If we want branches in a loop to have better register allocation than
1077 // another branch, then it is the linear order we should change.
1078 size_t back_edge_use_position = current->GetLifetimeEnd();
Vladimir Marko82b07402017-03-01 19:02:04 +00001079 if ((old_begin != uses_.end()) && (old_begin->GetPosition() <= back_edge_use_position)) {
Nicolas Geoffray57902602015-04-21 14:28:41 +01001080 // There was a use already seen in this loop. Therefore the previous call to `AddUse`
1081 // already inserted the backedge use. We can stop going outward.
David Brazdil07b35102016-04-27 15:33:22 +01001082 DCHECK(HasSynthesizeUseAt(back_edge_use_position));
Nicolas Geoffray57902602015-04-21 14:28:41 +01001083 break;
1084 }
1085
Vladimir Marko82b07402017-03-01 19:02:04 +00001086 DCHECK(insert_pos != uses_.before_begin()
1087 ? back_edge_use_position > insert_pos->GetPosition()
1088 : current == block_at_use.GetLoopInformation())
1089 << std::distance(uses_.before_begin(), insert_pos);
Nicolas Geoffray57902602015-04-21 14:28:41 +01001090
Vladimir Marko356bd282017-03-01 12:01:11 +00001091 UsePosition* new_use = new (allocator_) UsePosition(back_edge_use_position);
Vladimir Marko82b07402017-03-01 19:02:04 +00001092 insert_pos = uses_.insert_after(insert_pos, *new_use);
Nicolas Geoffray57902602015-04-21 14:28:41 +01001093 }
1094 }
1095
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001096 ScopedArenaAllocator* const allocator_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001097
1098 // Ranges of this interval. We need a quick access to the last range to test
1099 // for liveness (see `IsDeadAt`).
1100 LiveRange* first_range_;
1101 LiveRange* last_range_;
1102
David Brazdil3fc992f2015-04-16 18:31:55 +01001103 // The first range at or after the current position of a linear scan. It is
1104 // used to optimize range-searching queries.
1105 LiveRange* range_search_start_;
1106
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001107 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001108 SafepointPosition* first_safepoint_;
1109 SafepointPosition* last_safepoint_;
1110
Vladimir Marko82b07402017-03-01 19:02:04 +00001111 // Uses of this interval. Only the parent interval keeps these lists.
1112 UsePositionList uses_;
1113 EnvUsePositionList env_uses_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001114
1115 // The instruction type this interval corresponds to.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001116 const DataType::Type type_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001117
1118 // Live interval that is the result of a split.
1119 LiveInterval* next_sibling_;
1120
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001121 // The first interval from which split intervals come from.
1122 LiveInterval* parent_;
1123
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001124 // The register allocated to this interval.
1125 int register_;
1126
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001127 // The spill slot allocated to this interval.
1128 int spill_slot_;
1129
1130 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001131 const bool is_fixed_;
1132
1133 // Whether the interval is for a temporary.
1134 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001135
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001136 // Whether this interval is a synthesized interval for register pair.
1137 const bool is_high_interval_;
1138
1139 // If this interval needs a register pair, the high or low equivalent.
1140 // `is_high_interval_` tells whether this holds the low or the high.
1141 LiveInterval* high_or_low_interval_;
1142
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001143 // The instruction represented by this interval.
1144 HInstruction* const defined_by_;
1145
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001146 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001147 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001148
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001149 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1150
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001151 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
1152};
1153
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001154/**
1155 * Analysis that computes the liveness of instructions:
1156 *
1157 * (a) Non-environment uses of an instruction always make
1158 * the instruction live.
Hans Boehm206348c2018-12-05 11:11:33 -08001159 * (b) Environment uses of an instruction whose type is object (that is, non-primitive), make the
1160 * instruction live, unless the class has an @DeadReferenceSafe annotation.
1161 * This avoids unexpected premature reference enqueuing or finalization, which could
1162 * result in premature deletion of native objects. In the presence of @DeadReferenceSafe,
1163 * object references are treated like primitive types.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001164 * (c) When the graph has the debuggable property, environment uses
1165 * of an instruction that has a primitive type make the instruction live.
1166 * If the graph does not have the debuggable property, the environment
1167 * use has no effect, and may get a 'none' value after register allocation.
Artem Serovd6750532018-05-30 20:07:43 +01001168 * (d) When compiling in OSR mode, all loops in the compiled method may be entered
1169 * from the interpreter via SuspendCheck; such use in SuspendCheck makes the instruction
1170 * live.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001171 *
Artem Serovd6750532018-05-30 20:07:43 +01001172 * (b), (c) and (d) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001173 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001174class SsaLivenessAnalysis : public ValueObject {
1175 public:
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001176 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen, ScopedArenaAllocator* allocator)
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001177 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001178 codegen_(codegen),
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001179 allocator_(allocator),
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001180 block_infos_(graph->GetBlocks().size(),
1181 nullptr,
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001182 allocator_->Adapter(kArenaAllocSsaLiveness)),
1183 instructions_from_ssa_index_(allocator_->Adapter(kArenaAllocSsaLiveness)),
1184 instructions_from_lifetime_position_(allocator_->Adapter(kArenaAllocSsaLiveness)),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001185 number_of_ssa_values_(0) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001186 }
1187
1188 void Analyze();
1189
1190 BitVector* GetLiveInSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001191 return &block_infos_[block.GetBlockId()]->live_in_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001192 }
1193
1194 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001195 return &block_infos_[block.GetBlockId()]->live_out_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001196 }
1197
1198 BitVector* GetKillSet(const HBasicBlock& block) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001199 return &block_infos_[block.GetBlockId()]->kill_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001200 }
1201
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001202 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001203 return instructions_from_ssa_index_[index];
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001204 }
1205
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001206 HInstruction* GetInstructionFromPosition(size_t index) const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001207 return instructions_from_lifetime_position_[index];
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001208 }
1209
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001210 HBasicBlock* GetBlockFromPosition(size_t index) const {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001211 HInstruction* instruction = GetInstructionFromPosition(index);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001212 if (instruction == nullptr) {
1213 // If we are at a block boundary, get the block following.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001214 instruction = GetInstructionFromPosition(index + 1);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001215 }
1216 return instruction->GetBlock();
1217 }
1218
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001219 bool IsAtBlockBoundary(size_t index) const {
1220 return GetInstructionFromPosition(index) == nullptr;
1221 }
1222
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001223 HInstruction* GetTempUser(LiveInterval* temp) const {
1224 // A temporary shares the same lifetime start as the instruction that requires it.
1225 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001226 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
Vladimir Marko82b07402017-03-01 19:02:04 +00001227 DCHECK_EQ(user, temp->GetUses().front().GetUser());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001228 return user;
1229 }
1230
1231 size_t GetTempIndex(LiveInterval* temp) const {
1232 // We use the input index to store the index of the temporary in the user's temporary list.
1233 DCHECK(temp->IsTemp());
Vladimir Marko82b07402017-03-01 19:02:04 +00001234 return temp->GetUses().front().GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001235 }
1236
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001237 size_t GetMaxLifetimePosition() const {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +01001238 return instructions_from_lifetime_position_.size() * 2 - 1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001239 }
1240
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001241 size_t GetNumberOfSsaValues() const {
1242 return number_of_ssa_values_;
1243 }
1244
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001245 static constexpr const char* kLivenessPassName = "liveness";
1246
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001247 private:
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001248 // Give an SSA number to each instruction that defines a value used by another instruction,
1249 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001250 void NumberInstructions();
1251
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001252 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1253 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001254
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001255 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1256 // kill sets, that do not take into account backward branches.
1257 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001258
1259 // After computing the initial sets, this method does a fixed point
1260 // calculation over the live_in and live_out set to take into account
1261 // backwards branches.
1262 void ComputeLiveInAndLiveOutSets();
1263
1264 // Update the live_in set of the block and returns whether it has changed.
1265 bool UpdateLiveIn(const HBasicBlock& block);
1266
1267 // Update the live_out set of the block and returns whether it has changed.
1268 bool UpdateLiveOut(const HBasicBlock& block);
1269
Nicolas Geoffray61ba8d22018-08-07 09:55:57 +01001270 static void ProcessEnvironment(HInstruction* instruction,
1271 HInstruction* actual_user,
1272 BitVector* live_in);
1273 static void RecursivelyProcessInputs(HInstruction* instruction,
1274 HInstruction* actual_user,
1275 BitVector* live_in);
1276
Mingyao Yang718493c2015-07-22 15:56:34 -07001277 // Returns whether `instruction` in an HEnvironment held by `env_holder`
1278 // should be kept live by the HEnvironment.
Vladimir Marko356bd282017-03-01 12:01:11 +00001279 static bool ShouldBeLiveForEnvironment(HInstruction* env_holder, HInstruction* instruction) {
Artem Serovd6750532018-05-30 20:07:43 +01001280 DCHECK(instruction != nullptr);
Mingyao Yang718493c2015-07-22 15:56:34 -07001281 // A value that's not live in compiled code may still be needed in interpreter,
1282 // due to code motion, etc.
1283 if (env_holder->IsDeoptimize()) return true;
David Brazdil77a48ae2015-09-15 12:34:04 +00001284 // A value live at a throwing instruction in a try block may be copied by
1285 // the exception handler to its location at the top of the catch block.
1286 if (env_holder->CanThrowIntoCatchBlock()) return true;
Artem Serovd6750532018-05-30 20:07:43 +01001287 HGraph* graph = instruction->GetBlock()->GetGraph();
1288 if (graph->IsDebuggable()) return true;
1289 // When compiling in OSR mode, all loops in the compiled method may be entered
1290 // from the interpreter via SuspendCheck; thus we need to preserve the environment.
1291 if (env_holder->IsSuspendCheck() && graph->IsCompilingOsr()) return true;
Hans Boehm206348c2018-12-05 11:11:33 -08001292 if (graph -> IsDeadReferenceSafe()) return false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001293 return instruction->GetType() == DataType::Type::kReference;
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001294 }
1295
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +01001296 void CheckNoLiveInIrreducibleLoop(const HBasicBlock& block) const {
1297 if (!block.IsLoopHeader() || !block.GetLoopInformation()->IsIrreducible()) {
1298 return;
1299 }
1300 BitVector* live_in = GetLiveInSet(block);
1301 // To satisfy our liveness algorithm, we need to ensure loop headers of
1302 // irreducible loops do not have any live-in instructions, except constants
1303 // and the current method, which can be trivially re-materialized.
1304 for (uint32_t idx : live_in->Indexes()) {
1305 HInstruction* instruction = GetInstructionFromSsaIndex(idx);
1306 DCHECK(instruction->GetBlock()->IsEntryBlock()) << instruction->DebugName();
1307 DCHECK(!instruction->IsParameterValue());
1308 DCHECK(instruction->IsCurrentMethod() || instruction->IsConstant())
1309 << instruction->DebugName();
1310 }
1311 }
1312
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001313 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001314 CodeGenerator* const codegen_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001315
1316 // Use a local ScopedArenaAllocator for allocating memory.
1317 // This allocator must remain alive while doing register allocation.
Vladimir Marko69d310e2017-10-09 14:12:23 +01001318 ScopedArenaAllocator* const allocator_;
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001319
1320 ScopedArenaVector<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001321
1322 // Temporary array used when computing live_in, live_out, and kill sets.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001323 ScopedArenaVector<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001324
1325 // Temporary array used when inserting moves in the graph.
Vladimir Markoe764d2e2017-10-05 14:35:55 +01001326 ScopedArenaVector<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001327 size_t number_of_ssa_values_;
1328
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001329 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
Nicolas Geoffray23a81882015-06-01 18:12:38 +01001330 ART_FRIEND_TEST(RegisterAllocatorTest, FreeUntil);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001331
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001332 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1333};
1334
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001335} // namespace art
1336
1337#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_