blob: ce4bbd4fc78c17b33ad2744a5b83c4e4c27ee51e [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
20#include "nodes.h"
Nicolas Geoffray829280c2015-01-28 10:20:37 +000021#include <iostream>
Nicolas Geoffray804d0932014-05-02 08:46:00 +010022
23namespace art {
24
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010025class CodeGenerator;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +010026class SsaLivenessAnalysis;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010027
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010028static constexpr int kNoRegister = -1;
29
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070030class BlockInfo : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010031 public:
32 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
33 : block_(block),
34 live_in_(allocator, number_of_ssa_values, false),
35 live_out_(allocator, number_of_ssa_values, false),
36 kill_(allocator, number_of_ssa_values, false) {
Ian Rogerscf7f1912014-10-22 22:06:39 -070037 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010038 live_in_.ClearAllBits();
39 live_out_.ClearAllBits();
40 kill_.ClearAllBits();
41 }
42
43 private:
44 const HBasicBlock& block_;
45 ArenaBitVector live_in_;
46 ArenaBitVector live_out_;
47 ArenaBitVector kill_;
48
49 friend class SsaLivenessAnalysis;
50
51 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
52};
53
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010054/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010055 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010056 * is live.
57 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070058class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010060 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010062 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010063 }
64
65 size_t GetStart() const { return start_; }
66 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010067 LiveRange* GetNext() const { return next_; }
68
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070069 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010070 return (start_ >= other.start_ && start_ < other.end_)
71 || (other.start_ >= start_ && other.start_ < end_);
72 }
73
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070074 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010075 return end_ <= other.start_;
76 }
77
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070078 void Dump(std::ostream& stream) const {
David Brazdilc74652862015-05-13 17:50:09 +010079 stream << start_ << "-" << end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010081
Nicolas Geoffray840e5462015-01-07 16:01:24 +000082 LiveRange* Dup(ArenaAllocator* allocator) const {
83 return new (allocator) LiveRange(
84 start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator));
85 }
86
87 LiveRange* GetLastRange() {
88 return next_ == nullptr ? this : next_->GetLastRange();
89 }
90
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010091 private:
92 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010093 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010094 LiveRange* next_;
95
96 friend class LiveInterval;
97
98 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010099};
100
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100101/**
102 * A use position represents a live interval use at a given position.
103 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700104class UsePosition : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100105 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100106 UsePosition(HInstruction* user,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100107 HEnvironment* environment,
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100108 size_t input_index,
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100109 size_t position,
110 UsePosition* next)
111 : user_(user),
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100112 environment_(environment),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100113 input_index_(input_index),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100114 position_(position),
115 next_(next) {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100116 DCHECK((user == nullptr)
117 || user->IsPhi()
Nicolas Geoffray76905622014-09-25 14:39:26 +0100118 || (GetPosition() == user->GetLifetimePosition() + 1)
119 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100120 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
121 }
122
Nicolas Geoffray57902602015-04-21 14:28:41 +0100123 static constexpr size_t kNoInput = -1;
124
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125 size_t GetPosition() const { return position_; }
126
127 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100128 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100129
130 HInstruction* GetUser() const { return user_; }
131
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100132 bool GetIsEnvironment() const { return environment_ != nullptr; }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100133 bool IsSynthesized() const { return user_ == nullptr; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100134
135 size_t GetInputIndex() const { return input_index_; }
136
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100137 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100138 stream << position_;
Nicolas Geoffray57902602015-04-21 14:28:41 +0100139 }
140
141 HLoopInformation* GetLoopInformation() const {
142 return user_->GetBlock()->GetLoopInformation();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100143 }
144
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000145 UsePosition* Dup(ArenaAllocator* allocator) const {
146 return new (allocator) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100147 user_, environment_, input_index_, position_,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000148 next_ == nullptr ? nullptr : next_->Dup(allocator));
149 }
150
Nicolas Geoffray57902602015-04-21 14:28:41 +0100151 bool RequiresRegister() const {
152 if (GetIsEnvironment()) return false;
153 if (IsSynthesized()) return false;
154 Location location = GetUser()->GetLocations()->InAt(GetInputIndex());
155 return location.IsUnallocated()
156 && (location.GetPolicy() == Location::kRequiresRegister
157 || location.GetPolicy() == Location::kRequiresFpuRegister);
158 }
159
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100160 private:
161 HInstruction* const user_;
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100162 HEnvironment* const environment_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100163 const size_t input_index_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100164 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100165 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100166
167 DISALLOW_COPY_AND_ASSIGN(UsePosition);
168};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100169
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100170class SafepointPosition : public ArenaObject<kArenaAllocMisc> {
171 public:
172 explicit SafepointPosition(HInstruction* instruction)
173 : instruction_(instruction),
174 next_(nullptr) {}
175
176 void SetNext(SafepointPosition* next) {
177 next_ = next;
178 }
179
180 size_t GetPosition() const {
181 return instruction_->GetLifetimePosition();
182 }
183
184 SafepointPosition* GetNext() const {
185 return next_;
186 }
187
188 LocationSummary* GetLocations() const {
189 return instruction_->GetLocations();
190 }
191
192 HInstruction* GetInstruction() const {
193 return instruction_;
194 }
195
196 private:
197 HInstruction* const instruction_;
198 SafepointPosition* next_;
199
200 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
201};
202
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100203/**
204 * An interval is a list of disjoint live ranges where an instruction is live.
205 * Each instruction that has uses gets an interval.
206 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700207class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100208 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700209 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
210 Primitive::Type type,
211 HInstruction* instruction = nullptr) {
212 return new (allocator) LiveInterval(allocator, type, instruction);
213 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100214
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100215 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
216 return new (allocator) LiveInterval(
217 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
218 }
219
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100220 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100221 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
222 }
223
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100224 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
225 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100226 }
227
228 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700229 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100230 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700231 // This interval is the result of a split.
232 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100233
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000234 void AddTempUse(HInstruction* instruction, size_t temp_index) {
235 DCHECK(IsTemp());
236 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100237 DCHECK(first_env_use_ == nullptr) << "A temporary cannot have environment user";
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000238 size_t position = instruction->GetLifetimePosition();
239 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100240 instruction, /* environment */ nullptr, temp_index, position, first_use_);
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000241 AddRange(position, position + 1);
242 }
243
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000244 void AddUse(HInstruction* instruction,
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100245 HEnvironment* environment,
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000246 size_t input_index,
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000247 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100248 // Set the use within the instruction.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100249 bool is_environment = (environment != nullptr);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000250 size_t position = instruction->GetLifetimePosition() + 1;
251 LocationSummary* locations = instruction->GetLocations();
252 if (!is_environment) {
253 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
254 // For fixed inputs and output same as input, the register allocator
255 // requires to have inputs die at the instruction, so that input moves use the
256 // location of the input just before that instruction (and not potential moves due
257 // to splitting).
258 position = instruction->GetLifetimePosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100259 } else if (!locations->InAt(input_index).IsValid()) {
260 return;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000261 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100262 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000263
Nicolas Geoffray57902602015-04-21 14:28:41 +0100264 if (!is_environment && instruction->IsInLoop()) {
265 AddBackEdgeUses(*instruction->GetBlock());
266 }
267
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000268 DCHECK(position == instruction->GetLifetimePosition()
269 || position == instruction->GetLifetimePosition() + 1);
270
Nicolas Geoffray76905622014-09-25 14:39:26 +0100271 if ((first_use_ != nullptr)
272 && (first_use_->GetUser() == instruction)
273 && (first_use_->GetPosition() < position)) {
274 // The user uses the instruction multiple times, and one use dies before the other.
275 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000276 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100277 UsePosition* cursor = first_use_;
278 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
279 cursor = cursor->GetNext();
280 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100281 DCHECK(first_use_->GetPosition() + 1 == position);
282 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100283 instruction, environment, input_index, position, cursor->GetNext());
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100284 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100285 if (first_range_->GetEnd() == first_use_->GetPosition()) {
286 first_range_->end_ = position;
287 }
288 return;
289 }
290
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100291 if (is_environment) {
292 first_env_use_ = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100293 instruction, environment, input_index, position, first_env_use_);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100294 } else {
295 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100296 instruction, environment, input_index, position, first_use_);
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100297 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000298
299 if (is_environment && !keep_alive) {
300 // If this environment use does not keep the instruction live, it does not
301 // affect the live range of that instruction.
302 return;
303 }
304
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100305 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100306 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100307 // First time we see a use of that interval.
David Brazdil3fc992f2015-04-16 18:31:55 +0100308 first_range_ = last_range_ = range_search_start_ =
309 new (allocator_) LiveRange(start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100310 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100311 // There is a use later in the same block or in a following block.
312 // Note that in such a case, `AddRange` for the whole blocks has been called
313 // before arriving in this method, and this is the reason the start of
314 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100315 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100316 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100317 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100319 // Note that the start of `first_range_` can be equal to `end`: two blocks
320 // having adjacent lifetime positions are not necessarily
321 // predecessor/successor. When two blocks are predecessor/successor, the
322 // liveness algorithm has called `AddRange` before arriving in this method,
323 // and the check line 205 would succeed.
David Brazdil3fc992f2015-04-16 18:31:55 +0100324 first_range_ = range_search_start_ =
325 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100326 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100327 }
328
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100329 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100330 DCHECK(instruction->IsPhi());
Nicolas Geoffray57902602015-04-21 14:28:41 +0100331 if (block->IsInLoop()) {
332 AddBackEdgeUses(*block);
333 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100334 first_use_ = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100335 instruction, /* environment */ nullptr, input_index, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100336 }
337
338 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100339 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100340 first_range_ = last_range_ = range_search_start_ =
341 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100343 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100344 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100345 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
346 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100347 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100348 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100349 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100350 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100351 }
352 }
353
354 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100355 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000356 DCHECK_LE(start, first_range_->GetStart());
357 // Find the range that covers the positions after the loop.
358 LiveRange* after_loop = first_range_;
359 LiveRange* last_in_loop = nullptr;
360 while (after_loop != nullptr && after_loop->GetEnd() < end) {
361 DCHECK_LE(start, after_loop->GetStart());
362 last_in_loop = after_loop;
363 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100364 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000365 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100366 // Uses are only in the loop.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700367 first_range_ = last_range_ = range_search_start_ =
368 new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000369 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100370 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100371 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100372 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000373 } else {
374 // The use after the loop is after a lifetime hole.
375 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100376 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000377 first_range_->start_ = start;
378 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100379 }
380 }
381
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100382 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100383 void SetSpillSlot(int slot) {
384 DCHECK(!is_fixed_);
385 DCHECK(!is_temp_);
386 spill_slot_ = slot;
387 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100388 int GetSpillSlot() const { return spill_slot_; }
389
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100390 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100391 if (first_range_ != nullptr) {
392 first_range_->start_ = from;
393 } else {
394 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000395 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100396 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100397 first_range_ = last_range_ = range_search_start_ =
398 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100399 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100400 }
401
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100402 LiveInterval* GetParent() const { return parent_; }
403
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100404 // Returns whether this interval is the parent interval, that is, the interval
405 // that starts where the HInstruction is defined.
406 bool IsParent() const { return parent_ == this; }
407
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100408 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000409 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100410
411 int GetRegister() const { return register_; }
412 void SetRegister(int reg) { register_ = reg; }
413 void ClearRegister() { register_ = kNoRegister; }
414 bool HasRegister() const { return register_ != kNoRegister; }
415
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100416 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100417 return GetEnd() <= position;
418 }
419
420 bool IsDefinedAt(size_t position) const {
421 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100422 }
423
David Brazdil3fc992f2015-04-16 18:31:55 +0100424 // Returns true if the interval contains a LiveRange covering `position`.
425 // The range at or immediately after the current position of linear scan
426 // is cached for better performance. If `position` can be smaller than
427 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000428 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100429 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
430 range_search_start_ = candidate;
431 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100432 }
433
David Brazdil3fc992f2015-04-16 18:31:55 +0100434 // Same as Covers but always tests all ranges.
435 bool CoversSlow(size_t position) const {
436 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
437 return candidate != nullptr && candidate->GetStart() <= position;
438 }
439
440 // Returns the first intersection of this interval with `current`, which
441 // must be the interval currently being allocated by linear scan.
442 size_t FirstIntersectionWith(LiveInterval* current) const {
443 // Find the first range after the start of `current`. We use the search
444 // cache to improve performance.
445 DCHECK(GetStart() <= current->GetStart() || IsFixed());
446 LiveRange* other_range = current->first_range_;
447 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
448 if (my_range == nullptr) {
449 return kNoLifetime;
450 }
451
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100452 // Advance both intervals and find the first matching range start in
453 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100454 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000455 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100456 my_range = my_range->GetNext();
457 if (my_range == nullptr) {
458 return kNoLifetime;
459 }
David Brazdil714e14f2015-02-25 11:57:05 +0000460 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100461 other_range = other_range->GetNext();
462 if (other_range == nullptr) {
463 return kNoLifetime;
464 }
David Brazdil714e14f2015-02-25 11:57:05 +0000465 } else {
466 DCHECK(my_range->IntersectsWith(*other_range));
467 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100468 }
469 } while (true);
470 }
471
472 size_t GetStart() const {
473 return first_range_->GetStart();
474 }
475
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100476 size_t GetEnd() const {
477 return last_range_->GetEnd();
478 }
479
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100480 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100481 if (is_temp_) {
482 return position == GetStart() ? position : kNoLifetime;
483 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100484
485 if (IsDefiningPosition(position) && DefinitionRequiresRegister()) {
486 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100487 }
488
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100489 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100490 size_t end = GetEnd();
491 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100492 size_t use_position = use->GetPosition();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100493 if (use_position > position) {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100494 if (use->RequiresRegister()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100495 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100496 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100497 }
498 use = use->GetNext();
499 }
500 return kNoLifetime;
501 }
502
503 size_t FirstRegisterUse() const {
504 return FirstRegisterUseAfter(GetStart());
505 }
506
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000507 size_t FirstUseAfter(size_t position) const {
508 if (is_temp_) {
509 return position == GetStart() ? position : kNoLifetime;
510 }
511
Nicolas Geoffray57902602015-04-21 14:28:41 +0100512 if (IsDefiningPosition(position)) {
513 DCHECK(defined_by_->GetLocations()->Out().IsValid());
514 return position;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100515 }
516
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000517 UsePosition* use = first_use_;
518 size_t end = GetEnd();
519 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100520 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100521 if (use_position > position) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100522 return use_position;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000523 }
524 use = use->GetNext();
525 }
526 return kNoLifetime;
527 }
528
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100529 UsePosition* GetFirstUse() const {
530 return first_use_;
531 }
532
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100533 UsePosition* GetFirstEnvironmentUse() const {
534 return first_env_use_;
535 }
536
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100537 Primitive::Type GetType() const {
538 return type_;
539 }
540
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100541 HInstruction* GetDefinedBy() const {
542 return defined_by_;
543 }
544
Nicolas Geoffray8826f672015-04-17 09:15:11 +0100545 bool HasWillCallSafepoint() const {
546 for (SafepointPosition* safepoint = first_safepoint_;
547 safepoint != nullptr;
548 safepoint = safepoint->GetNext()) {
549 if (safepoint->GetLocations()->WillCall()) return true;
550 }
551 return false;
552 }
553
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100554 SafepointPosition* FindSafepointJustBefore(size_t position) const {
555 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
556 safepoint != nullptr;
557 previous = safepoint, safepoint = safepoint->GetNext()) {
558 if (safepoint->GetPosition() >= position) return previous;
559 }
560 return last_safepoint_;
561 }
562
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100563 /**
564 * Split this interval at `position`. This interval is changed to:
565 * [start ... position).
566 *
567 * The new interval covers:
568 * [position ... end)
569 */
570 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100571 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100572 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100573 DCHECK_GT(position, GetStart());
574
David Brazdil241a4862015-04-16 17:59:03 +0100575 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100576 // This range dies before `position`, no need to split.
577 return nullptr;
578 }
579
580 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100581 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
582 if (new_last_safepoint == nullptr) {
583 new_interval->first_safepoint_ = first_safepoint_;
584 new_interval->last_safepoint_ = last_safepoint_;
585 first_safepoint_ = last_safepoint_ = nullptr;
586 } else if (last_safepoint_ != new_last_safepoint) {
587 new_interval->last_safepoint_ = last_safepoint_;
588 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
589 DCHECK(new_interval->first_safepoint_ != nullptr);
590 last_safepoint_ = new_last_safepoint;
591 last_safepoint_->SetNext(nullptr);
592 }
593
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100594 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100595 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100596 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100597
598 new_interval->first_use_ = first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100599 new_interval->first_env_use_ = first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100600 LiveRange* current = first_range_;
601 LiveRange* previous = nullptr;
602 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000603 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100604 do {
605 if (position >= current->GetEnd()) {
606 // Move to next range.
607 previous = current;
608 current = current->next_;
609 } else if (position <= current->GetStart()) {
610 // If the previous range did not cover this position, we know position is in
611 // a lifetime hole. We can just break the first_range_ and last_range_ links
612 // and return the new interval.
613 DCHECK(previous != nullptr);
614 DCHECK(current != first_range_);
615 new_interval->last_range_ = last_range_;
616 last_range_ = previous;
617 previous->next_ = nullptr;
618 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100619 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700620 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100621 // (i.e. the end of the interval) in the original interval.
622 range_search_start_ = nullptr;
623 }
624 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100625 return new_interval;
626 } else {
627 // This range covers position. We create a new last_range_ for this interval
628 // that covers last_range_->Start() and position. We also shorten the current
629 // range and make it the first range of the new interval.
630 DCHECK(position < current->GetEnd() && position > current->GetStart());
631 new_interval->last_range_ = last_range_;
632 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
633 if (previous != nullptr) {
634 previous->next_ = last_range_;
635 } else {
636 first_range_ = last_range_;
637 }
638 new_interval->first_range_ = current;
639 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100640 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
641 // Search start point is inside `new_interval`. Change it to `last_range`
642 // in the original interval. This is conservative but always correct.
643 range_search_start_ = last_range_;
644 }
645 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100646 return new_interval;
647 }
648 } while (current != nullptr);
649
650 LOG(FATAL) << "Unreachable";
651 return nullptr;
652 }
653
Nicolas Geoffray76905622014-09-25 14:39:26 +0100654 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100655 return GetStart() <= other->GetStart();
656 }
657
658 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100659 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100660 }
661
662 void Dump(std::ostream& stream) const {
663 stream << "ranges: { ";
664 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000665 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100666 current->Dump(stream);
667 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000668 current = current->GetNext();
669 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100670 stream << "}, uses: { ";
671 UsePosition* use = first_use_;
672 if (use != nullptr) {
673 do {
674 use->Dump(stream);
675 stream << " ";
676 } while ((use = use->GetNext()) != nullptr);
677 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100678 stream << "}, { ";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100679 use = first_env_use_;
680 if (use != nullptr) {
681 do {
682 use->Dump(stream);
683 stream << " ";
684 } while ((use = use->GetNext()) != nullptr);
685 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100686 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700687 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000688 stream << " is_low: " << IsLowInterval();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100689 stream << " is_high: " << IsHighInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100690 }
691
692 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000693 LiveInterval* GetLastSibling() {
694 LiveInterval* result = this;
695 while (result->next_sibling_ != nullptr) {
696 result = result->next_sibling_;
697 }
698 return result;
699 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100700
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100701 // Returns the first register hint that is at least free before
702 // the value contained in `free_until`. If none is found, returns
703 // `kNoRegister`.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100704 int FindFirstRegisterHint(size_t* free_until, const SsaLivenessAnalysis& liveness) const;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100705
706 // If there is enough at the definition site to find a register (for example
707 // it uses the same input as the first input), returns the register as a hint.
708 // Returns kNoRegister otherwise.
709 int FindHintAtDefinition() const;
710
711 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
712 // slots for spilling.
713 bool NeedsTwoSpillSlots() const;
714
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100715 bool IsFloatingPoint() const {
716 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
717 }
718
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100719 // Converts the location of the interval to a `Location` object.
720 Location ToLocation() const;
721
722 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000723 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100724
David Brazdil241a4862015-04-16 17:59:03 +0100725 // Finds the sibling that is defined at `position`.
726 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100727
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100728 // Returns whether `other` and `this` share the same kind of register.
729 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000730 bool SameRegisterKind(const LiveInterval& other) const {
731 return IsFloatingPoint() == other.IsFloatingPoint();
732 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100733
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000734 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000735 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000736 }
737
738 bool HasLowInterval() const {
739 return IsHighInterval();
740 }
741
742 LiveInterval* GetLowInterval() const {
743 DCHECK(HasLowInterval());
744 return high_or_low_interval_;
745 }
746
747 LiveInterval* GetHighInterval() const {
748 DCHECK(HasHighInterval());
749 return high_or_low_interval_;
750 }
751
752 bool IsHighInterval() const {
753 return GetParent()->is_high_interval_;
754 }
755
756 bool IsLowInterval() const {
757 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
758 }
759
760 void SetLowInterval(LiveInterval* low) {
761 DCHECK(IsHighInterval());
762 high_or_low_interval_ = low;
763 }
764
765 void SetHighInterval(LiveInterval* high) {
766 DCHECK(IsLowInterval());
767 high_or_low_interval_ = high;
768 }
769
770 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100771 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000772 DCHECK(!HasHighInterval());
773 DCHECK(!HasLowInterval());
774 high_or_low_interval_ = new (allocator_) LiveInterval(
775 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
776 high_or_low_interval_->high_or_low_interval_ = this;
777 if (first_range_ != nullptr) {
778 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100779 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100780 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000781 }
782 if (first_use_ != nullptr) {
783 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
784 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100785
786 if (first_env_use_ != nullptr) {
787 high_or_low_interval_->first_env_use_ = first_env_use_->Dup(allocator_);
788 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000789 }
790
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000791 // Returns whether an interval, when it is non-split, is using
792 // the same register of one of its input.
793 bool IsUsingInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100794 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000795 if (defined_by_ != nullptr && !IsSplit()) {
796 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
797 LiveInterval* interval = it.Current()->GetLiveInterval();
798
David Brazdil3fc992f2015-04-16 18:31:55 +0100799 // Find the interval that covers `defined_by`_. Calls to this function
800 // are made outside the linear scan, hence we need to use CoversSlow.
801 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000802 interval = interval->GetNextSibling();
803 }
804
805 // Check if both intervals have the same register of the same kind.
806 if (interval != nullptr
807 && interval->SameRegisterKind(*this)
808 && interval->GetRegister() == GetRegister()) {
809 return true;
810 }
811 }
812 }
813 return false;
814 }
815
816 // Returns whether an interval, when it is non-split, can safely use
817 // the same register of one of its input. Note that this method requires
818 // IsUsingInputRegister() to be true.
819 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100820 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000821 DCHECK(IsUsingInputRegister());
822 if (defined_by_ != nullptr && !IsSplit()) {
823 LocationSummary* locations = defined_by_->GetLocations();
824 if (locations->OutputCanOverlapWithInputs()) {
825 return false;
826 }
827 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
828 LiveInterval* interval = it.Current()->GetLiveInterval();
829
David Brazdil3fc992f2015-04-16 18:31:55 +0100830 // Find the interval that covers `defined_by`_. Calls to this function
831 // are made outside the linear scan, hence we need to use CoversSlow.
832 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000833 interval = interval->GetNextSibling();
834 }
835
836 if (interval != nullptr
837 && interval->SameRegisterKind(*this)
838 && interval->GetRegister() == GetRegister()) {
839 // We found the input that has the same register. Check if it is live after
840 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100841 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000842 }
843 }
844 }
845 LOG(FATAL) << "Unreachable";
846 UNREACHABLE();
847 }
848
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100849 void AddSafepoint(HInstruction* instruction) {
850 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
851 if (first_safepoint_ == nullptr) {
852 first_safepoint_ = last_safepoint_ = safepoint;
853 } else {
854 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
855 last_safepoint_->SetNext(safepoint);
856 last_safepoint_ = safepoint;
857 }
858 }
859
860 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100861 return first_safepoint_;
862 }
863
David Brazdil3fc992f2015-04-16 18:31:55 +0100864 // Resets the starting point for range-searching queries to the first range.
865 // Intervals must be reset prior to starting a new linear scan over them.
866 void ResetSearchCache() {
867 range_search_start_ = first_range_;
868 }
869
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100870 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700871 LiveInterval(ArenaAllocator* allocator,
872 Primitive::Type type,
873 HInstruction* defined_by = nullptr,
874 bool is_fixed = false,
875 int reg = kNoRegister,
876 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000877 bool is_slow_path_safepoint = false,
878 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700879 : allocator_(allocator),
880 first_range_(nullptr),
881 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100882 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100883 first_safepoint_(nullptr),
884 last_safepoint_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700885 first_use_(nullptr),
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100886 first_env_use_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700887 type_(type),
888 next_sibling_(nullptr),
889 parent_(this),
890 register_(reg),
891 spill_slot_(kNoSpillSlot),
892 is_fixed_(is_fixed),
893 is_temp_(is_temp),
894 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000895 is_high_interval_(is_high_interval),
896 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700897 defined_by_(defined_by) {}
898
David Brazdil3fc992f2015-04-16 18:31:55 +0100899 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700900 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +0100901 // known to end before `position` can be skipped with `search_start`.
902 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000903 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100904 if (search_start != first_range_) {
905 // If we are not searching the entire list of ranges, make sure we do
906 // not skip the range we are searching for.
907 if (search_start == nullptr) {
908 DCHECK(IsDeadAt(position));
909 } else if (search_start->GetStart() > position) {
910 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
911 }
David Brazdil5b8e6a52015-02-25 16:17:05 +0000912 }
913 }
914
David Brazdil3fc992f2015-04-16 18:31:55 +0100915 LiveRange* range;
916 for (range = search_start;
917 range != nullptr && range->GetEnd() <= position;
918 range = range->GetNext()) {
919 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000920 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100921 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000922 }
923
Nicolas Geoffray57902602015-04-21 14:28:41 +0100924 bool DefinitionRequiresRegister() const {
925 DCHECK(IsParent());
926 LocationSummary* locations = defined_by_->GetLocations();
927 Location location = locations->Out();
928 // This interval is the first interval of the instruction. If the output
929 // of the instruction requires a register, we return the position of that instruction
930 // as the first register use.
931 if (location.IsUnallocated()) {
932 if ((location.GetPolicy() == Location::kRequiresRegister)
933 || (location.GetPolicy() == Location::kSameAsFirstInput
934 && (locations->InAt(0).IsRegister()
935 || locations->InAt(0).IsRegisterPair()
936 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
937 return true;
938 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
939 || (location.GetPolicy() == Location::kSameAsFirstInput
940 && (locations->InAt(0).IsFpuRegister()
941 || locations->InAt(0).IsFpuRegisterPair()
942 || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) {
943 return true;
944 }
945 } else if (location.IsRegister() || location.IsRegisterPair()) {
946 return true;
947 }
948 return false;
949 }
950
951 bool IsDefiningPosition(size_t position) const {
952 return IsParent() && (position == GetStart());
953 }
954
955 bool HasSynthesizeUseAt(size_t position) const {
956 UsePosition* use = first_use_;
957 while (use != nullptr) {
958 size_t use_position = use->GetPosition();
959 if ((use_position == position) && use->IsSynthesized()) {
960 return true;
961 }
962 if (use_position > position) break;
963 use = use->GetNext();
964 }
965 return false;
966 }
967
968 void AddBackEdgeUses(const HBasicBlock& block_at_use) {
969 DCHECK(block_at_use.IsInLoop());
970 // Add synthesized uses at the back edge of loops to help the register allocator.
971 // Note that this method is called in decreasing liveness order, to faciliate adding
972 // uses at the head of the `first_use_` linked list. Because below
973 // we iterate from inner-most to outer-most, which is in increasing liveness order,
974 // we need to take extra care of how the `first_use_` linked list is being updated.
975 UsePosition* first_in_new_list = nullptr;
976 UsePosition* last_in_new_list = nullptr;
977 for (HLoopInformationOutwardIterator it(block_at_use);
978 !it.Done();
979 it.Advance()) {
980 HLoopInformation* current = it.Current();
981 if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) {
982 // This interval is defined in the loop. We can stop going outward.
983 break;
984 }
985
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100986 // We're only adding a synthesized use at the last back edge. Adding syntehsized uses on
987 // all back edges is not necessary: anything used in the loop will have its use at the
988 // last back edge. If we want branches in a loop to have better register allocation than
989 // another branch, then it is the linear order we should change.
990 size_t back_edge_use_position = current->GetLifetimeEnd();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100991 if ((first_use_ != nullptr) && (first_use_->GetPosition() <= back_edge_use_position)) {
992 // There was a use already seen in this loop. Therefore the previous call to `AddUse`
993 // already inserted the backedge use. We can stop going outward.
994 DCHECK(HasSynthesizeUseAt(back_edge_use_position));
995 break;
996 }
997
998 DCHECK(last_in_new_list == nullptr
999 || back_edge_use_position > last_in_new_list->GetPosition());
1000
1001 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray0a23d742015-05-07 11:57:35 +01001002 /* user */ nullptr,
1003 /* environment */ nullptr,
1004 UsePosition::kNoInput,
1005 back_edge_use_position,
1006 /* next */ nullptr);
Nicolas Geoffray57902602015-04-21 14:28:41 +01001007
1008 if (last_in_new_list != nullptr) {
1009 // Going outward. The latest created use needs to point to the new use.
1010 last_in_new_list->SetNext(new_use);
1011 } else {
1012 // This is the inner-most loop.
1013 DCHECK_EQ(current, block_at_use.GetLoopInformation());
1014 first_in_new_list = new_use;
1015 }
1016 last_in_new_list = new_use;
1017 }
1018 // Link the newly created linked list with `first_use_`.
1019 if (last_in_new_list != nullptr) {
1020 last_in_new_list->SetNext(first_use_);
1021 first_use_ = first_in_new_list;
1022 }
1023 }
1024
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001025 ArenaAllocator* const allocator_;
1026
1027 // Ranges of this interval. We need a quick access to the last range to test
1028 // for liveness (see `IsDeadAt`).
1029 LiveRange* first_range_;
1030 LiveRange* last_range_;
1031
David Brazdil3fc992f2015-04-16 18:31:55 +01001032 // The first range at or after the current position of a linear scan. It is
1033 // used to optimize range-searching queries.
1034 LiveRange* range_search_start_;
1035
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001036 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001037 SafepointPosition* first_safepoint_;
1038 SafepointPosition* last_safepoint_;
1039
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001040 // Uses of this interval. Note that this linked list is shared amongst siblings.
1041 UsePosition* first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001042 UsePosition* first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001043
1044 // The instruction type this interval corresponds to.
1045 const Primitive::Type type_;
1046
1047 // Live interval that is the result of a split.
1048 LiveInterval* next_sibling_;
1049
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001050 // The first interval from which split intervals come from.
1051 LiveInterval* parent_;
1052
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001053 // The register allocated to this interval.
1054 int register_;
1055
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001056 // The spill slot allocated to this interval.
1057 int spill_slot_;
1058
1059 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001060 const bool is_fixed_;
1061
1062 // Whether the interval is for a temporary.
1063 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001064
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001065 // Whether the interval is for a safepoint that calls on slow path.
1066 const bool is_slow_path_safepoint_;
1067
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001068 // Whether this interval is a synthesized interval for register pair.
1069 const bool is_high_interval_;
1070
1071 // If this interval needs a register pair, the high or low equivalent.
1072 // `is_high_interval_` tells whether this holds the low or the high.
1073 LiveInterval* high_or_low_interval_;
1074
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001075 // The instruction represented by this interval.
1076 HInstruction* const defined_by_;
1077
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001078 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001079 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001080
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001081 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1082
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001083 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
1084};
1085
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001086/**
1087 * Analysis that computes the liveness of instructions:
1088 *
1089 * (a) Non-environment uses of an instruction always make
1090 * the instruction live.
1091 * (b) Environment uses of an instruction whose type is
1092 * object (that is, non-primitive), make the instruction live.
1093 * This is due to having to keep alive objects that have
1094 * finalizers deleting native objects.
1095 * (c) When the graph has the debuggable property, environment uses
1096 * of an instruction that has a primitive type make the instruction live.
1097 * If the graph does not have the debuggable property, the environment
1098 * use has no effect, and may get a 'none' value after register allocation.
1099 *
1100 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
1101 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001102class SsaLivenessAnalysis : public ValueObject {
1103 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001104 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001105 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001106 codegen_(codegen),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001107 block_infos_(graph->GetArena(), graph->GetBlocks().Size()),
1108 instructions_from_ssa_index_(graph->GetArena(), 0),
1109 instructions_from_lifetime_position_(graph->GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001110 number_of_ssa_values_(0) {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001111 block_infos_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001112 }
1113
1114 void Analyze();
1115
1116 BitVector* GetLiveInSet(const HBasicBlock& block) const {
1117 return &block_infos_.Get(block.GetBlockId())->live_in_;
1118 }
1119
1120 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
1121 return &block_infos_.Get(block.GetBlockId())->live_out_;
1122 }
1123
1124 BitVector* GetKillSet(const HBasicBlock& block) const {
1125 return &block_infos_.Get(block.GetBlockId())->kill_;
1126 }
1127
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001128 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001129 return instructions_from_ssa_index_.Get(index);
1130 }
1131
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001132 HInstruction* GetInstructionFromPosition(size_t index) const {
1133 return instructions_from_lifetime_position_.Get(index);
1134 }
1135
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001136 HBasicBlock* GetBlockFromPosition(size_t index) const {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001137 HInstruction* instruction = GetInstructionFromPosition(index);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001138 if (instruction == nullptr) {
1139 // If we are at a block boundary, get the block following.
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001140 instruction = GetInstructionFromPosition(index + 1);
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001141 }
1142 return instruction->GetBlock();
1143 }
1144
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +01001145 bool IsAtBlockBoundary(size_t index) const {
1146 return GetInstructionFromPosition(index) == nullptr;
1147 }
1148
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001149 HInstruction* GetTempUser(LiveInterval* temp) const {
1150 // A temporary shares the same lifetime start as the instruction that requires it.
1151 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001152 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
1153 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
1154 return user;
1155 }
1156
1157 size_t GetTempIndex(LiveInterval* temp) const {
1158 // We use the input index to store the index of the temporary in the user's temporary list.
1159 DCHECK(temp->IsTemp());
1160 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001161 }
1162
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001163 size_t GetMaxLifetimePosition() const {
1164 return instructions_from_lifetime_position_.Size() * 2 - 1;
1165 }
1166
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001167 size_t GetNumberOfSsaValues() const {
1168 return number_of_ssa_values_;
1169 }
1170
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001171 static constexpr const char* kLivenessPassName = "liveness";
1172
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001173 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +01001174 // Linearize the graph so that:
1175 // (1): a block is always after its dominator,
1176 // (2): blocks of loops are contiguous.
1177 // This creates a natural and efficient ordering when visualizing live ranges.
1178 void LinearizeGraph();
1179
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001180 // Give an SSA number to each instruction that defines a value used by another instruction,
1181 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001182 void NumberInstructions();
1183
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001184 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1185 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001186
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001187 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1188 // kill sets, that do not take into account backward branches.
1189 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001190
1191 // After computing the initial sets, this method does a fixed point
1192 // calculation over the live_in and live_out set to take into account
1193 // backwards branches.
1194 void ComputeLiveInAndLiveOutSets();
1195
1196 // Update the live_in set of the block and returns whether it has changed.
1197 bool UpdateLiveIn(const HBasicBlock& block);
1198
1199 // Update the live_out set of the block and returns whether it has changed.
1200 bool UpdateLiveOut(const HBasicBlock& block);
1201
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001202 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
1203 if (instruction == nullptr) return false;
1204 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1205 return instruction->GetType() == Primitive::kPrimNot;
1206 }
1207
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001208 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001209 CodeGenerator* const codegen_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001210 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001211
1212 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001213 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001214
1215 // Temporary array used when inserting moves in the graph.
1216 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001217 size_t number_of_ssa_values_;
1218
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001219 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1220
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001221 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1222};
1223
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001224} // namespace art
1225
1226#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_