blob: b74e65584f36e5773c646413708e4b405202140f [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;
26
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010027static constexpr int kNoRegister = -1;
28
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070029class BlockInfo : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010030 public:
31 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
32 : block_(block),
33 live_in_(allocator, number_of_ssa_values, false),
34 live_out_(allocator, number_of_ssa_values, false),
35 kill_(allocator, number_of_ssa_values, false) {
Ian Rogerscf7f1912014-10-22 22:06:39 -070036 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010037 live_in_.ClearAllBits();
38 live_out_.ClearAllBits();
39 kill_.ClearAllBits();
40 }
41
42 private:
43 const HBasicBlock& block_;
44 ArenaBitVector live_in_;
45 ArenaBitVector live_out_;
46 ArenaBitVector kill_;
47
48 friend class SsaLivenessAnalysis;
49
50 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
51};
52
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010053/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010055 * is live.
56 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070057class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010059 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010061 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010062 }
63
64 size_t GetStart() const { return start_; }
65 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010066 LiveRange* GetNext() const { return next_; }
67
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070068 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010069 return (start_ >= other.start_ && start_ < other.end_)
70 || (other.start_ >= start_ && other.start_ < end_);
71 }
72
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070073 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010074 return end_ <= other.start_;
75 }
76
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070077 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010078 stream << "[" << start_ << ", " << end_ << ")";
79 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010080
Nicolas Geoffray840e5462015-01-07 16:01:24 +000081 LiveRange* Dup(ArenaAllocator* allocator) const {
82 return new (allocator) LiveRange(
83 start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator));
84 }
85
86 LiveRange* GetLastRange() {
87 return next_ == nullptr ? this : next_->GetLastRange();
88 }
89
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010090 private:
91 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010092 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010093 LiveRange* next_;
94
95 friend class LiveInterval;
96
97 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010098};
99
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100/**
101 * A use position represents a live interval use at a given position.
102 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700103class UsePosition : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100104 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100105 UsePosition(HInstruction* user,
106 size_t input_index,
107 bool is_environment,
108 size_t position,
109 UsePosition* next)
110 : user_(user),
111 input_index_(input_index),
112 is_environment_(is_environment),
113 position_(position),
114 next_(next) {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100115 DCHECK((user == nullptr)
116 || user->IsPhi()
Nicolas Geoffray76905622014-09-25 14:39:26 +0100117 || (GetPosition() == user->GetLifetimePosition() + 1)
118 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100119 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
120 }
121
Nicolas Geoffray57902602015-04-21 14:28:41 +0100122 static constexpr size_t kNoInput = -1;
123
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124 size_t GetPosition() const { return position_; }
125
126 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100127 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100128
129 HInstruction* GetUser() const { return user_; }
130
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100131 bool GetIsEnvironment() const { return is_environment_; }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100132 bool IsSynthesized() const { return user_ == nullptr; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100133
134 size_t GetInputIndex() const { return input_index_; }
135
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100136 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100137 stream << position_;
Nicolas Geoffray57902602015-04-21 14:28:41 +0100138 }
139
140 HLoopInformation* GetLoopInformation() const {
141 return user_->GetBlock()->GetLoopInformation();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100142 }
143
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000144 UsePosition* Dup(ArenaAllocator* allocator) const {
145 return new (allocator) UsePosition(
146 user_, input_index_, is_environment_, position_,
147 next_ == nullptr ? nullptr : next_->Dup(allocator));
148 }
149
Nicolas Geoffray57902602015-04-21 14:28:41 +0100150 bool RequiresRegister() const {
151 if (GetIsEnvironment()) return false;
152 if (IsSynthesized()) return false;
153 Location location = GetUser()->GetLocations()->InAt(GetInputIndex());
154 return location.IsUnallocated()
155 && (location.GetPolicy() == Location::kRequiresRegister
156 || location.GetPolicy() == Location::kRequiresFpuRegister);
157 }
158
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100159 private:
160 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100161 const size_t input_index_;
162 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100163 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100164 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100165
166 DISALLOW_COPY_AND_ASSIGN(UsePosition);
167};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100168
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100169class SafepointPosition : public ArenaObject<kArenaAllocMisc> {
170 public:
171 explicit SafepointPosition(HInstruction* instruction)
172 : instruction_(instruction),
173 next_(nullptr) {}
174
175 void SetNext(SafepointPosition* next) {
176 next_ = next;
177 }
178
179 size_t GetPosition() const {
180 return instruction_->GetLifetimePosition();
181 }
182
183 SafepointPosition* GetNext() const {
184 return next_;
185 }
186
187 LocationSummary* GetLocations() const {
188 return instruction_->GetLocations();
189 }
190
191 HInstruction* GetInstruction() const {
192 return instruction_;
193 }
194
195 private:
196 HInstruction* const instruction_;
197 SafepointPosition* next_;
198
199 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
200};
201
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100202/**
203 * An interval is a list of disjoint live ranges where an instruction is live.
204 * Each instruction that has uses gets an interval.
205 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700206class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100207 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700208 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
209 Primitive::Type type,
210 HInstruction* instruction = nullptr) {
211 return new (allocator) LiveInterval(allocator, type, instruction);
212 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100213
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100214 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
215 return new (allocator) LiveInterval(
216 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
217 }
218
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100219 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100220 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
221 }
222
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100223 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
224 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100225 }
226
227 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700228 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100229 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700230 // This interval is the result of a split.
231 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100232
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000233 void AddTempUse(HInstruction* instruction, size_t temp_index) {
234 DCHECK(IsTemp());
235 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100236 DCHECK(first_env_use_ == nullptr) << "A temporary cannot have environment user";
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000237 size_t position = instruction->GetLifetimePosition();
238 first_use_ = new (allocator_) UsePosition(
239 instruction, temp_index, /* is_environment */ false, position, first_use_);
240 AddRange(position, position + 1);
241 }
242
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000243 void AddUse(HInstruction* instruction,
244 size_t input_index,
245 bool is_environment,
246 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100247 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000248 size_t position = instruction->GetLifetimePosition() + 1;
249 LocationSummary* locations = instruction->GetLocations();
250 if (!is_environment) {
251 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
252 // For fixed inputs and output same as input, the register allocator
253 // requires to have inputs die at the instruction, so that input moves use the
254 // location of the input just before that instruction (and not potential moves due
255 // to splitting).
256 position = instruction->GetLifetimePosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100257 } else if (!locations->InAt(input_index).IsValid()) {
258 return;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000259 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100260 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000261
Nicolas Geoffray57902602015-04-21 14:28:41 +0100262 if (!is_environment && instruction->IsInLoop()) {
263 AddBackEdgeUses(*instruction->GetBlock());
264 }
265
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000266 DCHECK(position == instruction->GetLifetimePosition()
267 || position == instruction->GetLifetimePosition() + 1);
268
Nicolas Geoffray76905622014-09-25 14:39:26 +0100269 if ((first_use_ != nullptr)
270 && (first_use_->GetUser() == instruction)
271 && (first_use_->GetPosition() < position)) {
272 // The user uses the instruction multiple times, and one use dies before the other.
273 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000274 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100275 UsePosition* cursor = first_use_;
276 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
277 cursor = cursor->GetNext();
278 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100279 DCHECK(first_use_->GetPosition() + 1 == position);
280 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100281 instruction, input_index, is_environment, position, cursor->GetNext());
282 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100283 if (first_range_->GetEnd() == first_use_->GetPosition()) {
284 first_range_->end_ = position;
285 }
286 return;
287 }
288
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100289 if (is_environment) {
290 first_env_use_ = new (allocator_) UsePosition(
291 instruction, input_index, is_environment, position, first_env_use_);
292 } else {
293 first_use_ = new (allocator_) UsePosition(
294 instruction, input_index, is_environment, position, first_use_);
295 }
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000296
297 if (is_environment && !keep_alive) {
298 // If this environment use does not keep the instruction live, it does not
299 // affect the live range of that instruction.
300 return;
301 }
302
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100303 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100304 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100305 // First time we see a use of that interval.
David Brazdil3fc992f2015-04-16 18:31:55 +0100306 first_range_ = last_range_ = range_search_start_ =
307 new (allocator_) LiveRange(start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100309 // There is a use later in the same block or in a following block.
310 // Note that in such a case, `AddRange` for the whole blocks has been called
311 // before arriving in this method, and this is the reason the start of
312 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100313 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100314 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100315 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100316 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100317 // Note that the start of `first_range_` can be equal to `end`: two blocks
318 // having adjacent lifetime positions are not necessarily
319 // predecessor/successor. When two blocks are predecessor/successor, the
320 // liveness algorithm has called `AddRange` before arriving in this method,
321 // and the check line 205 would succeed.
David Brazdil3fc992f2015-04-16 18:31:55 +0100322 first_range_ = range_search_start_ =
323 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100324 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100325 }
326
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100327 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100328 DCHECK(instruction->IsPhi());
Nicolas Geoffray57902602015-04-21 14:28:41 +0100329 if (block->IsInLoop()) {
330 AddBackEdgeUses(*block);
331 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100332 first_use_ = new (allocator_) UsePosition(
333 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100334 }
335
336 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100337 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100338 first_range_ = last_range_ = range_search_start_ =
339 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100340 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100341 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100343 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
344 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100345 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100346 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100347 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100348 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100349 }
350 }
351
352 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100353 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000354 DCHECK_LE(start, first_range_->GetStart());
355 // Find the range that covers the positions after the loop.
356 LiveRange* after_loop = first_range_;
357 LiveRange* last_in_loop = nullptr;
358 while (after_loop != nullptr && after_loop->GetEnd() < end) {
359 DCHECK_LE(start, after_loop->GetStart());
360 last_in_loop = after_loop;
361 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100362 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000363 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100364 // Uses are only in the loop.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700365 first_range_ = last_range_ = range_search_start_ =
366 new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000367 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100368 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100369 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100370 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000371 } else {
372 // The use after the loop is after a lifetime hole.
373 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100374 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000375 first_range_->start_ = start;
376 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100377 }
378 }
379
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100380 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100381 void SetSpillSlot(int slot) {
382 DCHECK(!is_fixed_);
383 DCHECK(!is_temp_);
384 spill_slot_ = slot;
385 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100386 int GetSpillSlot() const { return spill_slot_; }
387
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100388 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100389 if (first_range_ != nullptr) {
390 first_range_->start_ = from;
391 } else {
392 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000393 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100394 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100395 first_range_ = last_range_ = range_search_start_ =
396 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100397 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100398 }
399
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100400 LiveInterval* GetParent() const { return parent_; }
401
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100402 // Returns whether this interval is the parent interval, that is, the interval
403 // that starts where the HInstruction is defined.
404 bool IsParent() const { return parent_ == this; }
405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100406 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000407 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100408
409 int GetRegister() const { return register_; }
410 void SetRegister(int reg) { register_ = reg; }
411 void ClearRegister() { register_ = kNoRegister; }
412 bool HasRegister() const { return register_ != kNoRegister; }
413
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100414 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100415 return GetEnd() <= position;
416 }
417
418 bool IsDefinedAt(size_t position) const {
419 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100420 }
421
David Brazdil3fc992f2015-04-16 18:31:55 +0100422 // Returns true if the interval contains a LiveRange covering `position`.
423 // The range at or immediately after the current position of linear scan
424 // is cached for better performance. If `position` can be smaller than
425 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000426 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100427 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
428 range_search_start_ = candidate;
429 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100430 }
431
David Brazdil3fc992f2015-04-16 18:31:55 +0100432 // Same as Covers but always tests all ranges.
433 bool CoversSlow(size_t position) const {
434 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
435 return candidate != nullptr && candidate->GetStart() <= position;
436 }
437
438 // Returns the first intersection of this interval with `current`, which
439 // must be the interval currently being allocated by linear scan.
440 size_t FirstIntersectionWith(LiveInterval* current) const {
441 // Find the first range after the start of `current`. We use the search
442 // cache to improve performance.
443 DCHECK(GetStart() <= current->GetStart() || IsFixed());
444 LiveRange* other_range = current->first_range_;
445 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
446 if (my_range == nullptr) {
447 return kNoLifetime;
448 }
449
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100450 // Advance both intervals and find the first matching range start in
451 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100452 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000453 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100454 my_range = my_range->GetNext();
455 if (my_range == nullptr) {
456 return kNoLifetime;
457 }
David Brazdil714e14f2015-02-25 11:57:05 +0000458 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100459 other_range = other_range->GetNext();
460 if (other_range == nullptr) {
461 return kNoLifetime;
462 }
David Brazdil714e14f2015-02-25 11:57:05 +0000463 } else {
464 DCHECK(my_range->IntersectsWith(*other_range));
465 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100466 }
467 } while (true);
468 }
469
470 size_t GetStart() const {
471 return first_range_->GetStart();
472 }
473
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100474 size_t GetEnd() const {
475 return last_range_->GetEnd();
476 }
477
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100478 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100479 if (is_temp_) {
480 return position == GetStart() ? position : kNoLifetime;
481 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100482
483 if (IsDefiningPosition(position) && DefinitionRequiresRegister()) {
484 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100485 }
486
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100487 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100488 size_t end = GetEnd();
489 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100490 size_t use_position = use->GetPosition();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100491 if (use_position > position) {
Nicolas Geoffray57902602015-04-21 14:28:41 +0100492 if (use->RequiresRegister()) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100493 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100494 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100495 }
496 use = use->GetNext();
497 }
498 return kNoLifetime;
499 }
500
501 size_t FirstRegisterUse() const {
502 return FirstRegisterUseAfter(GetStart());
503 }
504
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000505 size_t FirstUseAfter(size_t position) const {
506 if (is_temp_) {
507 return position == GetStart() ? position : kNoLifetime;
508 }
509
Nicolas Geoffray57902602015-04-21 14:28:41 +0100510 if (IsDefiningPosition(position)) {
511 DCHECK(defined_by_->GetLocations()->Out().IsValid());
512 return position;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100513 }
514
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000515 UsePosition* use = first_use_;
516 size_t end = GetEnd();
517 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100518 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100519 if (use_position > position) {
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100520 return use_position;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000521 }
522 use = use->GetNext();
523 }
524 return kNoLifetime;
525 }
526
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100527 UsePosition* GetFirstUse() const {
528 return first_use_;
529 }
530
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100531 UsePosition* GetFirstEnvironmentUse() const {
532 return first_env_use_;
533 }
534
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100535 Primitive::Type GetType() const {
536 return type_;
537 }
538
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100539 HInstruction* GetDefinedBy() const {
540 return defined_by_;
541 }
542
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100543 SafepointPosition* FindSafepointJustBefore(size_t position) const {
544 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
545 safepoint != nullptr;
546 previous = safepoint, safepoint = safepoint->GetNext()) {
547 if (safepoint->GetPosition() >= position) return previous;
548 }
549 return last_safepoint_;
550 }
551
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100552 /**
553 * Split this interval at `position`. This interval is changed to:
554 * [start ... position).
555 *
556 * The new interval covers:
557 * [position ... end)
558 */
559 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100560 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100561 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100562 DCHECK_GT(position, GetStart());
563
David Brazdil241a4862015-04-16 17:59:03 +0100564 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100565 // This range dies before `position`, no need to split.
566 return nullptr;
567 }
568
569 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100570 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
571 if (new_last_safepoint == nullptr) {
572 new_interval->first_safepoint_ = first_safepoint_;
573 new_interval->last_safepoint_ = last_safepoint_;
574 first_safepoint_ = last_safepoint_ = nullptr;
575 } else if (last_safepoint_ != new_last_safepoint) {
576 new_interval->last_safepoint_ = last_safepoint_;
577 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
578 DCHECK(new_interval->first_safepoint_ != nullptr);
579 last_safepoint_ = new_last_safepoint;
580 last_safepoint_->SetNext(nullptr);
581 }
582
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100583 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100584 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100585 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100586
587 new_interval->first_use_ = first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100588 new_interval->first_env_use_ = first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100589 LiveRange* current = first_range_;
590 LiveRange* previous = nullptr;
591 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000592 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100593 do {
594 if (position >= current->GetEnd()) {
595 // Move to next range.
596 previous = current;
597 current = current->next_;
598 } else if (position <= current->GetStart()) {
599 // If the previous range did not cover this position, we know position is in
600 // a lifetime hole. We can just break the first_range_ and last_range_ links
601 // and return the new interval.
602 DCHECK(previous != nullptr);
603 DCHECK(current != first_range_);
604 new_interval->last_range_ = last_range_;
605 last_range_ = previous;
606 previous->next_ = nullptr;
607 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100608 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700609 // Search start point is inside `new_interval`. Change it to null
David Brazdil3fc992f2015-04-16 18:31:55 +0100610 // (i.e. the end of the interval) in the original interval.
611 range_search_start_ = nullptr;
612 }
613 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100614 return new_interval;
615 } else {
616 // This range covers position. We create a new last_range_ for this interval
617 // that covers last_range_->Start() and position. We also shorten the current
618 // range and make it the first range of the new interval.
619 DCHECK(position < current->GetEnd() && position > current->GetStart());
620 new_interval->last_range_ = last_range_;
621 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
622 if (previous != nullptr) {
623 previous->next_ = last_range_;
624 } else {
625 first_range_ = last_range_;
626 }
627 new_interval->first_range_ = current;
628 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100629 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
630 // Search start point is inside `new_interval`. Change it to `last_range`
631 // in the original interval. This is conservative but always correct.
632 range_search_start_ = last_range_;
633 }
634 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100635 return new_interval;
636 }
637 } while (current != nullptr);
638
639 LOG(FATAL) << "Unreachable";
640 return nullptr;
641 }
642
Nicolas Geoffray76905622014-09-25 14:39:26 +0100643 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100644 return GetStart() <= other->GetStart();
645 }
646
647 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100648 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100649 }
650
651 void Dump(std::ostream& stream) const {
652 stream << "ranges: { ";
653 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000654 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100655 current->Dump(stream);
656 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000657 current = current->GetNext();
658 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100659 stream << "}, uses: { ";
660 UsePosition* use = first_use_;
661 if (use != nullptr) {
662 do {
663 use->Dump(stream);
664 stream << " ";
665 } while ((use = use->GetNext()) != nullptr);
666 }
Nicolas Geoffray57902602015-04-21 14:28:41 +0100667 stream << "}, { ";
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100668 use = first_env_use_;
669 if (use != nullptr) {
670 do {
671 use->Dump(stream);
672 stream << " ";
673 } while ((use = use->GetNext()) != nullptr);
674 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100675 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700676 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000677 stream << " is_low: " << IsLowInterval();
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100678 stream << " is_high: " << IsHighInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100679 }
680
681 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000682 LiveInterval* GetLastSibling() {
683 LiveInterval* result = this;
684 while (result->next_sibling_ != nullptr) {
685 result = result->next_sibling_;
686 }
687 return result;
688 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100689
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100690 // Returns the first register hint that is at least free before
691 // the value contained in `free_until`. If none is found, returns
692 // `kNoRegister`.
693 int FindFirstRegisterHint(size_t* free_until) const;
694
695 // If there is enough at the definition site to find a register (for example
696 // it uses the same input as the first input), returns the register as a hint.
697 // Returns kNoRegister otherwise.
698 int FindHintAtDefinition() const;
699
700 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
701 // slots for spilling.
702 bool NeedsTwoSpillSlots() const;
703
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100704 bool IsFloatingPoint() const {
705 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
706 }
707
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100708 // Converts the location of the interval to a `Location` object.
709 Location ToLocation() const;
710
711 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000712 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100713
David Brazdil241a4862015-04-16 17:59:03 +0100714 // Finds the sibling that is defined at `position`.
715 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100716
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100717 // Returns whether `other` and `this` share the same kind of register.
718 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000719 bool SameRegisterKind(const LiveInterval& other) const {
720 return IsFloatingPoint() == other.IsFloatingPoint();
721 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100722
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000723 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000724 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000725 }
726
727 bool HasLowInterval() const {
728 return IsHighInterval();
729 }
730
731 LiveInterval* GetLowInterval() const {
732 DCHECK(HasLowInterval());
733 return high_or_low_interval_;
734 }
735
736 LiveInterval* GetHighInterval() const {
737 DCHECK(HasHighInterval());
738 return high_or_low_interval_;
739 }
740
741 bool IsHighInterval() const {
742 return GetParent()->is_high_interval_;
743 }
744
745 bool IsLowInterval() const {
746 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
747 }
748
749 void SetLowInterval(LiveInterval* low) {
750 DCHECK(IsHighInterval());
751 high_or_low_interval_ = low;
752 }
753
754 void SetHighInterval(LiveInterval* high) {
755 DCHECK(IsLowInterval());
756 high_or_low_interval_ = high;
757 }
758
759 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100760 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000761 DCHECK(!HasHighInterval());
762 DCHECK(!HasLowInterval());
763 high_or_low_interval_ = new (allocator_) LiveInterval(
764 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
765 high_or_low_interval_->high_or_low_interval_ = this;
766 if (first_range_ != nullptr) {
767 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100768 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100769 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000770 }
771 if (first_use_ != nullptr) {
772 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
773 }
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100774
775 if (first_env_use_ != nullptr) {
776 high_or_low_interval_->first_env_use_ = first_env_use_->Dup(allocator_);
777 }
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000778 }
779
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000780 // Returns whether an interval, when it is non-split, is using
781 // the same register of one of its input.
782 bool IsUsingInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100783 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000784 if (defined_by_ != nullptr && !IsSplit()) {
785 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
786 LiveInterval* interval = it.Current()->GetLiveInterval();
787
David Brazdil3fc992f2015-04-16 18:31:55 +0100788 // Find the interval that covers `defined_by`_. Calls to this function
789 // are made outside the linear scan, hence we need to use CoversSlow.
790 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000791 interval = interval->GetNextSibling();
792 }
793
794 // Check if both intervals have the same register of the same kind.
795 if (interval != nullptr
796 && interval->SameRegisterKind(*this)
797 && interval->GetRegister() == GetRegister()) {
798 return true;
799 }
800 }
801 }
802 return false;
803 }
804
805 // Returns whether an interval, when it is non-split, can safely use
806 // the same register of one of its input. Note that this method requires
807 // IsUsingInputRegister() to be true.
808 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100809 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000810 DCHECK(IsUsingInputRegister());
811 if (defined_by_ != nullptr && !IsSplit()) {
812 LocationSummary* locations = defined_by_->GetLocations();
813 if (locations->OutputCanOverlapWithInputs()) {
814 return false;
815 }
816 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
817 LiveInterval* interval = it.Current()->GetLiveInterval();
818
David Brazdil3fc992f2015-04-16 18:31:55 +0100819 // Find the interval that covers `defined_by`_. Calls to this function
820 // are made outside the linear scan, hence we need to use CoversSlow.
821 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000822 interval = interval->GetNextSibling();
823 }
824
825 if (interval != nullptr
826 && interval->SameRegisterKind(*this)
827 && interval->GetRegister() == GetRegister()) {
828 // We found the input that has the same register. Check if it is live after
829 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100830 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000831 }
832 }
833 }
834 LOG(FATAL) << "Unreachable";
835 UNREACHABLE();
836 }
837
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100838 void AddSafepoint(HInstruction* instruction) {
839 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
840 if (first_safepoint_ == nullptr) {
841 first_safepoint_ = last_safepoint_ = safepoint;
842 } else {
843 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
844 last_safepoint_->SetNext(safepoint);
845 last_safepoint_ = safepoint;
846 }
847 }
848
849 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100850 return first_safepoint_;
851 }
852
David Brazdil3fc992f2015-04-16 18:31:55 +0100853 // Resets the starting point for range-searching queries to the first range.
854 // Intervals must be reset prior to starting a new linear scan over them.
855 void ResetSearchCache() {
856 range_search_start_ = first_range_;
857 }
858
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100859 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700860 LiveInterval(ArenaAllocator* allocator,
861 Primitive::Type type,
862 HInstruction* defined_by = nullptr,
863 bool is_fixed = false,
864 int reg = kNoRegister,
865 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000866 bool is_slow_path_safepoint = false,
867 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700868 : allocator_(allocator),
869 first_range_(nullptr),
870 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100871 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100872 first_safepoint_(nullptr),
873 last_safepoint_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700874 first_use_(nullptr),
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +0100875 first_env_use_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700876 type_(type),
877 next_sibling_(nullptr),
878 parent_(this),
879 register_(reg),
880 spill_slot_(kNoSpillSlot),
881 is_fixed_(is_fixed),
882 is_temp_(is_temp),
883 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000884 is_high_interval_(is_high_interval),
885 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700886 defined_by_(defined_by) {}
887
David Brazdil3fc992f2015-04-16 18:31:55 +0100888 // Searches for a LiveRange that either covers the given position or is the
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700889 // first next LiveRange. Returns null if no such LiveRange exists. Ranges
David Brazdil3fc992f2015-04-16 18:31:55 +0100890 // known to end before `position` can be skipped with `search_start`.
891 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000892 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100893 if (search_start != first_range_) {
894 // If we are not searching the entire list of ranges, make sure we do
895 // not skip the range we are searching for.
896 if (search_start == nullptr) {
897 DCHECK(IsDeadAt(position));
898 } else if (search_start->GetStart() > position) {
899 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
900 }
David Brazdil5b8e6a52015-02-25 16:17:05 +0000901 }
902 }
903
David Brazdil3fc992f2015-04-16 18:31:55 +0100904 LiveRange* range;
905 for (range = search_start;
906 range != nullptr && range->GetEnd() <= position;
907 range = range->GetNext()) {
908 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000909 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100910 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000911 }
912
Nicolas Geoffray57902602015-04-21 14:28:41 +0100913 bool DefinitionRequiresRegister() const {
914 DCHECK(IsParent());
915 LocationSummary* locations = defined_by_->GetLocations();
916 Location location = locations->Out();
917 // This interval is the first interval of the instruction. If the output
918 // of the instruction requires a register, we return the position of that instruction
919 // as the first register use.
920 if (location.IsUnallocated()) {
921 if ((location.GetPolicy() == Location::kRequiresRegister)
922 || (location.GetPolicy() == Location::kSameAsFirstInput
923 && (locations->InAt(0).IsRegister()
924 || locations->InAt(0).IsRegisterPair()
925 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
926 return true;
927 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
928 || (location.GetPolicy() == Location::kSameAsFirstInput
929 && (locations->InAt(0).IsFpuRegister()
930 || locations->InAt(0).IsFpuRegisterPair()
931 || locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister))) {
932 return true;
933 }
934 } else if (location.IsRegister() || location.IsRegisterPair()) {
935 return true;
936 }
937 return false;
938 }
939
940 bool IsDefiningPosition(size_t position) const {
941 return IsParent() && (position == GetStart());
942 }
943
944 bool HasSynthesizeUseAt(size_t position) const {
945 UsePosition* use = first_use_;
946 while (use != nullptr) {
947 size_t use_position = use->GetPosition();
948 if ((use_position == position) && use->IsSynthesized()) {
949 return true;
950 }
951 if (use_position > position) break;
952 use = use->GetNext();
953 }
954 return false;
955 }
956
957 void AddBackEdgeUses(const HBasicBlock& block_at_use) {
958 DCHECK(block_at_use.IsInLoop());
959 // Add synthesized uses at the back edge of loops to help the register allocator.
960 // Note that this method is called in decreasing liveness order, to faciliate adding
961 // uses at the head of the `first_use_` linked list. Because below
962 // we iterate from inner-most to outer-most, which is in increasing liveness order,
963 // we need to take extra care of how the `first_use_` linked list is being updated.
964 UsePosition* first_in_new_list = nullptr;
965 UsePosition* last_in_new_list = nullptr;
966 for (HLoopInformationOutwardIterator it(block_at_use);
967 !it.Done();
968 it.Advance()) {
969 HLoopInformation* current = it.Current();
970 if (GetDefinedBy()->GetLifetimePosition() >= current->GetHeader()->GetLifetimeStart()) {
971 // This interval is defined in the loop. We can stop going outward.
972 break;
973 }
974
975 size_t back_edge_use_position = current->GetSingleBackEdge()->GetLifetimeEnd();
976 if ((first_use_ != nullptr) && (first_use_->GetPosition() <= back_edge_use_position)) {
977 // There was a use already seen in this loop. Therefore the previous call to `AddUse`
978 // already inserted the backedge use. We can stop going outward.
979 DCHECK(HasSynthesizeUseAt(back_edge_use_position));
980 break;
981 }
982
983 DCHECK(last_in_new_list == nullptr
984 || back_edge_use_position > last_in_new_list->GetPosition());
985
986 UsePosition* new_use = new (allocator_) UsePosition(
987 nullptr, UsePosition::kNoInput, /* is_environment */ false,
988 back_edge_use_position, nullptr);
989
990 if (last_in_new_list != nullptr) {
991 // Going outward. The latest created use needs to point to the new use.
992 last_in_new_list->SetNext(new_use);
993 } else {
994 // This is the inner-most loop.
995 DCHECK_EQ(current, block_at_use.GetLoopInformation());
996 first_in_new_list = new_use;
997 }
998 last_in_new_list = new_use;
999 }
1000 // Link the newly created linked list with `first_use_`.
1001 if (last_in_new_list != nullptr) {
1002 last_in_new_list->SetNext(first_use_);
1003 first_use_ = first_in_new_list;
1004 }
1005 }
1006
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001007 ArenaAllocator* const allocator_;
1008
1009 // Ranges of this interval. We need a quick access to the last range to test
1010 // for liveness (see `IsDeadAt`).
1011 LiveRange* first_range_;
1012 LiveRange* last_range_;
1013
David Brazdil3fc992f2015-04-16 18:31:55 +01001014 // The first range at or after the current position of a linear scan. It is
1015 // used to optimize range-searching queries.
1016 LiveRange* range_search_start_;
1017
Nicolas Geoffray43af7282015-04-16 13:01:01 +01001018 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +01001019 SafepointPosition* first_safepoint_;
1020 SafepointPosition* last_safepoint_;
1021
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001022 // Uses of this interval. Note that this linked list is shared amongst siblings.
1023 UsePosition* first_use_;
Nicolas Geoffray4ed947a2015-04-27 16:58:06 +01001024 UsePosition* first_env_use_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001025
1026 // The instruction type this interval corresponds to.
1027 const Primitive::Type type_;
1028
1029 // Live interval that is the result of a split.
1030 LiveInterval* next_sibling_;
1031
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001032 // The first interval from which split intervals come from.
1033 LiveInterval* parent_;
1034
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001035 // The register allocated to this interval.
1036 int register_;
1037
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001038 // The spill slot allocated to this interval.
1039 int spill_slot_;
1040
1041 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +01001042 const bool is_fixed_;
1043
1044 // Whether the interval is for a temporary.
1045 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001046
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +01001047 // Whether the interval is for a safepoint that calls on slow path.
1048 const bool is_slow_path_safepoint_;
1049
Nicolas Geoffray840e5462015-01-07 16:01:24 +00001050 // Whether this interval is a synthesized interval for register pair.
1051 const bool is_high_interval_;
1052
1053 // If this interval needs a register pair, the high or low equivalent.
1054 // `is_high_interval_` tells whether this holds the low or the high.
1055 LiveInterval* high_or_low_interval_;
1056
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001057 // The instruction represented by this interval.
1058 HInstruction* const defined_by_;
1059
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001060 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001061 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001062
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +00001063 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1064
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001065 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
1066};
1067
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001068/**
1069 * Analysis that computes the liveness of instructions:
1070 *
1071 * (a) Non-environment uses of an instruction always make
1072 * the instruction live.
1073 * (b) Environment uses of an instruction whose type is
1074 * object (that is, non-primitive), make the instruction live.
1075 * This is due to having to keep alive objects that have
1076 * finalizers deleting native objects.
1077 * (c) When the graph has the debuggable property, environment uses
1078 * of an instruction that has a primitive type make the instruction live.
1079 * If the graph does not have the debuggable property, the environment
1080 * use has no effect, and may get a 'none' value after register allocation.
1081 *
1082 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
1083 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001084class SsaLivenessAnalysis : public ValueObject {
1085 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001086 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001087 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001088 codegen_(codegen),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001089 block_infos_(graph->GetArena(), graph->GetBlocks().Size()),
1090 instructions_from_ssa_index_(graph->GetArena(), 0),
1091 instructions_from_lifetime_position_(graph->GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001092 number_of_ssa_values_(0) {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001093 block_infos_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001094 }
1095
1096 void Analyze();
1097
1098 BitVector* GetLiveInSet(const HBasicBlock& block) const {
1099 return &block_infos_.Get(block.GetBlockId())->live_in_;
1100 }
1101
1102 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
1103 return &block_infos_.Get(block.GetBlockId())->live_out_;
1104 }
1105
1106 BitVector* GetKillSet(const HBasicBlock& block) const {
1107 return &block_infos_.Get(block.GetBlockId())->kill_;
1108 }
1109
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001110 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001111 return instructions_from_ssa_index_.Get(index);
1112 }
1113
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001114 HInstruction* GetInstructionFromPosition(size_t index) const {
1115 return instructions_from_lifetime_position_.Get(index);
1116 }
1117
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001118 HBasicBlock* GetBlockFromPosition(size_t index) const {
1119 HInstruction* instruction = GetInstructionFromPosition(index / 2);
1120 if (instruction == nullptr) {
1121 // If we are at a block boundary, get the block following.
1122 instruction = GetInstructionFromPosition((index / 2) + 1);
1123 }
1124 return instruction->GetBlock();
1125 }
1126
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001127 HInstruction* GetTempUser(LiveInterval* temp) const {
1128 // A temporary shares the same lifetime start as the instruction that requires it.
1129 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001130 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
1131 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
1132 return user;
1133 }
1134
1135 size_t GetTempIndex(LiveInterval* temp) const {
1136 // We use the input index to store the index of the temporary in the user's temporary list.
1137 DCHECK(temp->IsTemp());
1138 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001139 }
1140
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001141 size_t GetMaxLifetimePosition() const {
1142 return instructions_from_lifetime_position_.Size() * 2 - 1;
1143 }
1144
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001145 size_t GetNumberOfSsaValues() const {
1146 return number_of_ssa_values_;
1147 }
1148
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001149 static constexpr const char* kLivenessPassName = "liveness";
1150
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001151 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +01001152 // Linearize the graph so that:
1153 // (1): a block is always after its dominator,
1154 // (2): blocks of loops are contiguous.
1155 // This creates a natural and efficient ordering when visualizing live ranges.
1156 void LinearizeGraph();
1157
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001158 // Give an SSA number to each instruction that defines a value used by another instruction,
1159 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001160 void NumberInstructions();
1161
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001162 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1163 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001164
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001165 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1166 // kill sets, that do not take into account backward branches.
1167 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001168
1169 // After computing the initial sets, this method does a fixed point
1170 // calculation over the live_in and live_out set to take into account
1171 // backwards branches.
1172 void ComputeLiveInAndLiveOutSets();
1173
1174 // Update the live_in set of the block and returns whether it has changed.
1175 bool UpdateLiveIn(const HBasicBlock& block);
1176
1177 // Update the live_out set of the block and returns whether it has changed.
1178 bool UpdateLiveOut(const HBasicBlock& block);
1179
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001180 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
1181 if (instruction == nullptr) return false;
1182 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1183 return instruction->GetType() == Primitive::kPrimNot;
1184 }
1185
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001186 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001187 CodeGenerator* const codegen_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001188 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001189
1190 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001191 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001192
1193 // Temporary array used when inserting moves in the graph.
1194 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001195 size_t number_of_ssa_values_;
1196
Nicolas Geoffray8cbab3c2015-04-23 15:14:36 +01001197 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
1198
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001199 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1200};
1201
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001202} // namespace art
1203
1204#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_