blob: 03f5545755e9bc4c6dd3480324af7f4849e911ef [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 Geoffray76905622014-09-25 14:39:26 +0100115 DCHECK(user->IsPhi()
116 || (GetPosition() == user->GetLifetimePosition() + 1)
117 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100118 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
119 }
120
121 size_t GetPosition() const { return position_; }
122
123 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100124 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125
126 HInstruction* GetUser() const { return user_; }
127
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100128 bool GetIsEnvironment() const { return is_environment_; }
129
130 size_t GetInputIndex() const { return input_index_; }
131
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100132 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100133 stream << position_;
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100134 if (is_environment_) {
135 stream << " (env)";
136 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100137 }
138
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000139 UsePosition* Dup(ArenaAllocator* allocator) const {
140 return new (allocator) UsePosition(
141 user_, input_index_, is_environment_, position_,
142 next_ == nullptr ? nullptr : next_->Dup(allocator));
143 }
144
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100145 private:
146 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100147 const size_t input_index_;
148 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100149 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100150 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100151
152 DISALLOW_COPY_AND_ASSIGN(UsePosition);
153};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100154
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100155class SafepointPosition : public ArenaObject<kArenaAllocMisc> {
156 public:
157 explicit SafepointPosition(HInstruction* instruction)
158 : instruction_(instruction),
159 next_(nullptr) {}
160
161 void SetNext(SafepointPosition* next) {
162 next_ = next;
163 }
164
165 size_t GetPosition() const {
166 return instruction_->GetLifetimePosition();
167 }
168
169 SafepointPosition* GetNext() const {
170 return next_;
171 }
172
173 LocationSummary* GetLocations() const {
174 return instruction_->GetLocations();
175 }
176
177 HInstruction* GetInstruction() const {
178 return instruction_;
179 }
180
181 private:
182 HInstruction* const instruction_;
183 SafepointPosition* next_;
184
185 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
186};
187
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100188/**
189 * An interval is a list of disjoint live ranges where an instruction is live.
190 * Each instruction that has uses gets an interval.
191 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700192class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100193 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700194 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
195 Primitive::Type type,
196 HInstruction* instruction = nullptr) {
197 return new (allocator) LiveInterval(allocator, type, instruction);
198 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100199
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100200 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
201 return new (allocator) LiveInterval(
202 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
203 }
204
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100205 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100206 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
207 }
208
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100209 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
210 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100211 }
212
213 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700214 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100215 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700216 // This interval is the result of a split.
217 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100218
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000219 void AddTempUse(HInstruction* instruction, size_t temp_index) {
220 DCHECK(IsTemp());
221 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
222 size_t position = instruction->GetLifetimePosition();
223 first_use_ = new (allocator_) UsePosition(
224 instruction, temp_index, /* is_environment */ false, position, first_use_);
225 AddRange(position, position + 1);
226 }
227
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000228 void AddUse(HInstruction* instruction,
229 size_t input_index,
230 bool is_environment,
231 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100232 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000233 size_t position = instruction->GetLifetimePosition() + 1;
234 LocationSummary* locations = instruction->GetLocations();
235 if (!is_environment) {
236 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
237 // For fixed inputs and output same as input, the register allocator
238 // requires to have inputs die at the instruction, so that input moves use the
239 // location of the input just before that instruction (and not potential moves due
240 // to splitting).
241 position = instruction->GetLifetimePosition();
242 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100243 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000244
245 DCHECK(position == instruction->GetLifetimePosition()
246 || position == instruction->GetLifetimePosition() + 1);
247
Nicolas Geoffray76905622014-09-25 14:39:26 +0100248 if ((first_use_ != nullptr)
249 && (first_use_->GetUser() == instruction)
250 && (first_use_->GetPosition() < position)) {
251 // The user uses the instruction multiple times, and one use dies before the other.
252 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000253 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100254 UsePosition* cursor = first_use_;
255 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
256 cursor = cursor->GetNext();
257 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100258 DCHECK(first_use_->GetPosition() + 1 == position);
259 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100260 instruction, input_index, is_environment, position, cursor->GetNext());
261 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100262 if (first_range_->GetEnd() == first_use_->GetPosition()) {
263 first_range_->end_ = position;
264 }
265 return;
266 }
267
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000268 first_use_ = new (allocator_) UsePosition(
269 instruction, input_index, is_environment, position, first_use_);
270
271 if (is_environment && !keep_alive) {
272 // If this environment use does not keep the instruction live, it does not
273 // affect the live range of that instruction.
274 return;
275 }
276
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100277 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100278 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100279 // First time we see a use of that interval.
David Brazdil3fc992f2015-04-16 18:31:55 +0100280 first_range_ = last_range_ = range_search_start_ =
281 new (allocator_) LiveRange(start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100282 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100283 // There is a use later in the same block or in a following block.
284 // Note that in such a case, `AddRange` for the whole blocks has been called
285 // before arriving in this method, and this is the reason the start of
286 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100287 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100288 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100289 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100290 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100291 // Note that the start of `first_range_` can be equal to `end`: two blocks
292 // having adjacent lifetime positions are not necessarily
293 // predecessor/successor. When two blocks are predecessor/successor, the
294 // liveness algorithm has called `AddRange` before arriving in this method,
295 // and the check line 205 would succeed.
David Brazdil3fc992f2015-04-16 18:31:55 +0100296 first_range_ = range_search_start_ =
297 new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100298 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100299 }
300
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100301 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100302 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100303 first_use_ = new (allocator_) UsePosition(
304 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100305 }
306
307 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 if (first_range_ == nullptr) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100309 first_range_ = last_range_ = range_search_start_ =
310 new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100311 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100312 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100313 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100314 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
315 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100316 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100317 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 // There is a hole in the interval. Create a new range.
David Brazdil3fc992f2015-04-16 18:31:55 +0100319 first_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320 }
321 }
322
323 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100324 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000325 DCHECK_LE(start, first_range_->GetStart());
326 // Find the range that covers the positions after the loop.
327 LiveRange* after_loop = first_range_;
328 LiveRange* last_in_loop = nullptr;
329 while (after_loop != nullptr && after_loop->GetEnd() < end) {
330 DCHECK_LE(start, after_loop->GetStart());
331 last_in_loop = after_loop;
332 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100333 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000334 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100335 // Uses are only in the loop.
David Brazdil3fc992f2015-04-16 18:31:55 +0100336 first_range_ = last_range_ = range_search_start_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000337 } else if (after_loop->GetStart() <= end) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100338 first_range_ = range_search_start_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100339 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100340 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000341 } else {
342 // The use after the loop is after a lifetime hole.
343 DCHECK(last_in_loop != nullptr);
David Brazdil3fc992f2015-04-16 18:31:55 +0100344 first_range_ = range_search_start_ = last_in_loop;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000345 first_range_->start_ = start;
346 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100347 }
348 }
349
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100350 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100351 void SetSpillSlot(int slot) {
352 DCHECK(!is_fixed_);
353 DCHECK(!is_temp_);
354 spill_slot_ = slot;
355 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100356 int GetSpillSlot() const { return spill_slot_; }
357
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100358 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100359 if (first_range_ != nullptr) {
360 first_range_->start_ = from;
361 } else {
362 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000363 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100364 DCHECK(from == defined_by_->GetLifetimePosition());
David Brazdil3fc992f2015-04-16 18:31:55 +0100365 first_range_ = last_range_ = range_search_start_ =
366 new (allocator_) LiveRange(from, from + 2, nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100367 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100368 }
369
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100370 LiveInterval* GetParent() const { return parent_; }
371
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100372 // Returns whether this interval is the parent interval, that is, the interval
373 // that starts where the HInstruction is defined.
374 bool IsParent() const { return parent_ == this; }
375
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100376 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000377 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100378
379 int GetRegister() const { return register_; }
380 void SetRegister(int reg) { register_ = reg; }
381 void ClearRegister() { register_ = kNoRegister; }
382 bool HasRegister() const { return register_ != kNoRegister; }
383
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100384 bool IsDeadAt(size_t position) const {
David Brazdil241a4862015-04-16 17:59:03 +0100385 return GetEnd() <= position;
386 }
387
388 bool IsDefinedAt(size_t position) const {
389 return GetStart() <= position && !IsDeadAt(position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100390 }
391
David Brazdil3fc992f2015-04-16 18:31:55 +0100392 // Returns true if the interval contains a LiveRange covering `position`.
393 // The range at or immediately after the current position of linear scan
394 // is cached for better performance. If `position` can be smaller than
395 // that, CoversSlow should be used instead.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000396 bool Covers(size_t position) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100397 LiveRange* candidate = FindRangeAtOrAfter(position, range_search_start_);
398 range_search_start_ = candidate;
399 return (candidate != nullptr && candidate->GetStart() <= position);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100400 }
401
David Brazdil3fc992f2015-04-16 18:31:55 +0100402 // Same as Covers but always tests all ranges.
403 bool CoversSlow(size_t position) const {
404 LiveRange* candidate = FindRangeAtOrAfter(position, first_range_);
405 return candidate != nullptr && candidate->GetStart() <= position;
406 }
407
408 // Returns the first intersection of this interval with `current`, which
409 // must be the interval currently being allocated by linear scan.
410 size_t FirstIntersectionWith(LiveInterval* current) const {
411 // Find the first range after the start of `current`. We use the search
412 // cache to improve performance.
413 DCHECK(GetStart() <= current->GetStart() || IsFixed());
414 LiveRange* other_range = current->first_range_;
415 LiveRange* my_range = FindRangeAtOrAfter(other_range->GetStart(), range_search_start_);
416 if (my_range == nullptr) {
417 return kNoLifetime;
418 }
419
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100420 // Advance both intervals and find the first matching range start in
421 // this interval.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100422 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000423 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100424 my_range = my_range->GetNext();
425 if (my_range == nullptr) {
426 return kNoLifetime;
427 }
David Brazdil714e14f2015-02-25 11:57:05 +0000428 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100429 other_range = other_range->GetNext();
430 if (other_range == nullptr) {
431 return kNoLifetime;
432 }
David Brazdil714e14f2015-02-25 11:57:05 +0000433 } else {
434 DCHECK(my_range->IntersectsWith(*other_range));
435 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100436 }
437 } while (true);
438 }
439
440 size_t GetStart() const {
441 return first_range_->GetStart();
442 }
443
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100444 size_t GetEnd() const {
445 return last_range_->GetEnd();
446 }
447
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100448 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100449 if (is_temp_) {
450 return position == GetStart() ? position : kNoLifetime;
451 }
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100452 if (position == GetStart() && IsParent()) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100453 LocationSummary* locations = defined_by_->GetLocations();
454 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100455 // This interval is the first interval of the instruction. If the output
456 // of the instruction requires a register, we return the position of that instruction
457 // as the first register use.
458 if (location.IsUnallocated()) {
459 if ((location.GetPolicy() == Location::kRequiresRegister)
460 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000461 && (locations->InAt(0).IsRegister()
462 || locations->InAt(0).IsRegisterPair()
463 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100464 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100465 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
466 || (location.GetPolicy() == Location::kSameAsFirstInput
467 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
468 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100469 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000470 } else if (location.IsRegister() || location.IsRegisterPair()) {
471 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100472 }
473 }
474
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100475 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100476 size_t end = GetEnd();
477 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100478 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100479 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100480 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100481 if (location.IsUnallocated()
482 && (location.GetPolicy() == Location::kRequiresRegister
483 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100484 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100485 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100486 }
487 use = use->GetNext();
488 }
489 return kNoLifetime;
490 }
491
492 size_t FirstRegisterUse() const {
493 return FirstRegisterUseAfter(GetStart());
494 }
495
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000496 size_t FirstUseAfter(size_t position) const {
497 if (is_temp_) {
498 return position == GetStart() ? position : kNoLifetime;
499 }
500
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100501 if (position == GetStart() && IsParent()) {
502 if (defined_by_->GetLocations()->Out().IsValid()) {
503 return position;
504 }
505 }
506
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000507 UsePosition* use = first_use_;
508 size_t end = GetEnd();
509 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000510 if (!use->GetIsEnvironment()) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100511 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000512 size_t use_position = use->GetPosition();
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100513 if (use_position > position && location.IsValid()) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000514 return use_position;
515 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000516 }
517 use = use->GetNext();
518 }
519 return kNoLifetime;
520 }
521
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100522 UsePosition* GetFirstUse() const {
523 return first_use_;
524 }
525
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100526 Primitive::Type GetType() const {
527 return type_;
528 }
529
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100530 HInstruction* GetDefinedBy() const {
531 return defined_by_;
532 }
533
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100534 SafepointPosition* FindSafepointJustBefore(size_t position) const {
535 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
536 safepoint != nullptr;
537 previous = safepoint, safepoint = safepoint->GetNext()) {
538 if (safepoint->GetPosition() >= position) return previous;
539 }
540 return last_safepoint_;
541 }
542
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100543 /**
544 * Split this interval at `position`. This interval is changed to:
545 * [start ... position).
546 *
547 * The new interval covers:
548 * [position ... end)
549 */
550 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100551 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100552 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100553 DCHECK_GT(position, GetStart());
554
David Brazdil241a4862015-04-16 17:59:03 +0100555 if (GetEnd() <= position) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100556 // This range dies before `position`, no need to split.
557 return nullptr;
558 }
559
560 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100561 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
562 if (new_last_safepoint == nullptr) {
563 new_interval->first_safepoint_ = first_safepoint_;
564 new_interval->last_safepoint_ = last_safepoint_;
565 first_safepoint_ = last_safepoint_ = nullptr;
566 } else if (last_safepoint_ != new_last_safepoint) {
567 new_interval->last_safepoint_ = last_safepoint_;
568 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
569 DCHECK(new_interval->first_safepoint_ != nullptr);
570 last_safepoint_ = new_last_safepoint;
571 last_safepoint_->SetNext(nullptr);
572 }
573
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100574 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100575 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100576 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100577
578 new_interval->first_use_ = first_use_;
579 LiveRange* current = first_range_;
580 LiveRange* previous = nullptr;
581 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000582 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100583 do {
584 if (position >= current->GetEnd()) {
585 // Move to next range.
586 previous = current;
587 current = current->next_;
588 } else if (position <= current->GetStart()) {
589 // If the previous range did not cover this position, we know position is in
590 // a lifetime hole. We can just break the first_range_ and last_range_ links
591 // and return the new interval.
592 DCHECK(previous != nullptr);
593 DCHECK(current != first_range_);
594 new_interval->last_range_ = last_range_;
595 last_range_ = previous;
596 previous->next_ = nullptr;
597 new_interval->first_range_ = current;
David Brazdil3fc992f2015-04-16 18:31:55 +0100598 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
599 // Search start point is inside `new_interval`. Change it to nullptr
600 // (i.e. the end of the interval) in the original interval.
601 range_search_start_ = nullptr;
602 }
603 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100604 return new_interval;
605 } else {
606 // This range covers position. We create a new last_range_ for this interval
607 // that covers last_range_->Start() and position. We also shorten the current
608 // range and make it the first range of the new interval.
609 DCHECK(position < current->GetEnd() && position > current->GetStart());
610 new_interval->last_range_ = last_range_;
611 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
612 if (previous != nullptr) {
613 previous->next_ = last_range_;
614 } else {
615 first_range_ = last_range_;
616 }
617 new_interval->first_range_ = current;
618 current->start_ = position;
David Brazdil3fc992f2015-04-16 18:31:55 +0100619 if (range_search_start_ != nullptr && range_search_start_->GetEnd() >= current->GetEnd()) {
620 // Search start point is inside `new_interval`. Change it to `last_range`
621 // in the original interval. This is conservative but always correct.
622 range_search_start_ = last_range_;
623 }
624 new_interval->range_search_start_ = new_interval->first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100625 return new_interval;
626 }
627 } while (current != nullptr);
628
629 LOG(FATAL) << "Unreachable";
630 return nullptr;
631 }
632
Nicolas Geoffray76905622014-09-25 14:39:26 +0100633 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100634 return GetStart() <= other->GetStart();
635 }
636
637 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100638 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100639 }
640
641 void Dump(std::ostream& stream) const {
642 stream << "ranges: { ";
643 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000644 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100645 current->Dump(stream);
646 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000647 current = current->GetNext();
648 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100649 stream << "}, uses: { ";
650 UsePosition* use = first_use_;
651 if (use != nullptr) {
652 do {
653 use->Dump(stream);
654 stream << " ";
655 } while ((use = use->GetNext()) != nullptr);
656 }
657 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700658 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000659 stream << " is_high: " << IsHighInterval();
660 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100661 }
662
663 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000664 LiveInterval* GetLastSibling() {
665 LiveInterval* result = this;
666 while (result->next_sibling_ != nullptr) {
667 result = result->next_sibling_;
668 }
669 return result;
670 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100671
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100672 // Returns the first register hint that is at least free before
673 // the value contained in `free_until`. If none is found, returns
674 // `kNoRegister`.
675 int FindFirstRegisterHint(size_t* free_until) const;
676
677 // If there is enough at the definition site to find a register (for example
678 // it uses the same input as the first input), returns the register as a hint.
679 // Returns kNoRegister otherwise.
680 int FindHintAtDefinition() const;
681
682 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
683 // slots for spilling.
684 bool NeedsTwoSpillSlots() const;
685
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100686 bool IsFloatingPoint() const {
687 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
688 }
689
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100690 // Converts the location of the interval to a `Location` object.
691 Location ToLocation() const;
692
693 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000694 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100695
David Brazdil241a4862015-04-16 17:59:03 +0100696 // Finds the sibling that is defined at `position`.
697 LiveInterval* GetSiblingAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100698
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100699 // Returns whether `other` and `this` share the same kind of register.
700 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000701 bool SameRegisterKind(const LiveInterval& other) const {
702 return IsFloatingPoint() == other.IsFloatingPoint();
703 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100704
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000705 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000706 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000707 }
708
709 bool HasLowInterval() const {
710 return IsHighInterval();
711 }
712
713 LiveInterval* GetLowInterval() const {
714 DCHECK(HasLowInterval());
715 return high_or_low_interval_;
716 }
717
718 LiveInterval* GetHighInterval() const {
719 DCHECK(HasHighInterval());
720 return high_or_low_interval_;
721 }
722
723 bool IsHighInterval() const {
724 return GetParent()->is_high_interval_;
725 }
726
727 bool IsLowInterval() const {
728 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
729 }
730
731 void SetLowInterval(LiveInterval* low) {
732 DCHECK(IsHighInterval());
733 high_or_low_interval_ = low;
734 }
735
736 void SetHighInterval(LiveInterval* high) {
737 DCHECK(IsLowInterval());
738 high_or_low_interval_ = high;
739 }
740
741 void AddHighInterval(bool is_temp = false) {
Nicolas Geoffray1ba19812015-04-21 09:12:40 +0100742 DCHECK(IsParent());
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000743 DCHECK(!HasHighInterval());
744 DCHECK(!HasLowInterval());
745 high_or_low_interval_ = new (allocator_) LiveInterval(
746 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
747 high_or_low_interval_->high_or_low_interval_ = this;
748 if (first_range_ != nullptr) {
749 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
David Brazdilc08675c2015-04-17 15:49:51 +0100750 high_or_low_interval_->last_range_ = high_or_low_interval_->first_range_->GetLastRange();
David Brazdil3fc992f2015-04-16 18:31:55 +0100751 high_or_low_interval_->range_search_start_ = high_or_low_interval_->first_range_;
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000752 }
753 if (first_use_ != nullptr) {
754 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
755 }
756 }
757
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000758 // Returns whether an interval, when it is non-split, is using
759 // the same register of one of its input.
760 bool IsUsingInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100761 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000762 if (defined_by_ != nullptr && !IsSplit()) {
763 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
764 LiveInterval* interval = it.Current()->GetLiveInterval();
765
David Brazdil3fc992f2015-04-16 18:31:55 +0100766 // Find the interval that covers `defined_by`_. Calls to this function
767 // are made outside the linear scan, hence we need to use CoversSlow.
768 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000769 interval = interval->GetNextSibling();
770 }
771
772 // Check if both intervals have the same register of the same kind.
773 if (interval != nullptr
774 && interval->SameRegisterKind(*this)
775 && interval->GetRegister() == GetRegister()) {
776 return true;
777 }
778 }
779 }
780 return false;
781 }
782
783 // Returns whether an interval, when it is non-split, can safely use
784 // the same register of one of its input. Note that this method requires
785 // IsUsingInputRegister() to be true.
786 bool CanUseInputRegister() const {
David Brazdil3fc992f2015-04-16 18:31:55 +0100787 CHECK(kIsDebugBuild) << "Function should be used only for DCHECKs";
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000788 DCHECK(IsUsingInputRegister());
789 if (defined_by_ != nullptr && !IsSplit()) {
790 LocationSummary* locations = defined_by_->GetLocations();
791 if (locations->OutputCanOverlapWithInputs()) {
792 return false;
793 }
794 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
795 LiveInterval* interval = it.Current()->GetLiveInterval();
796
David Brazdil3fc992f2015-04-16 18:31:55 +0100797 // Find the interval that covers `defined_by`_. Calls to this function
798 // are made outside the linear scan, hence we need to use CoversSlow.
799 while (interval != nullptr && !interval->CoversSlow(defined_by_->GetLifetimePosition())) {
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000800 interval = interval->GetNextSibling();
801 }
802
803 if (interval != nullptr
804 && interval->SameRegisterKind(*this)
805 && interval->GetRegister() == GetRegister()) {
806 // We found the input that has the same register. Check if it is live after
807 // `defined_by`_.
David Brazdil3fc992f2015-04-16 18:31:55 +0100808 return !interval->CoversSlow(defined_by_->GetLifetimePosition() + 1);
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000809 }
810 }
811 }
812 LOG(FATAL) << "Unreachable";
813 UNREACHABLE();
814 }
815
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100816 void AddSafepoint(HInstruction* instruction) {
817 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
818 if (first_safepoint_ == nullptr) {
819 first_safepoint_ = last_safepoint_ = safepoint;
820 } else {
821 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
822 last_safepoint_->SetNext(safepoint);
823 last_safepoint_ = safepoint;
824 }
825 }
826
827 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100828 return first_safepoint_;
829 }
830
David Brazdil3fc992f2015-04-16 18:31:55 +0100831 // Resets the starting point for range-searching queries to the first range.
832 // Intervals must be reset prior to starting a new linear scan over them.
833 void ResetSearchCache() {
834 range_search_start_ = first_range_;
835 }
836
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100837 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700838 LiveInterval(ArenaAllocator* allocator,
839 Primitive::Type type,
840 HInstruction* defined_by = nullptr,
841 bool is_fixed = false,
842 int reg = kNoRegister,
843 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000844 bool is_slow_path_safepoint = false,
845 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700846 : allocator_(allocator),
847 first_range_(nullptr),
848 last_range_(nullptr),
David Brazdil3fc992f2015-04-16 18:31:55 +0100849 range_search_start_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100850 first_safepoint_(nullptr),
851 last_safepoint_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700852 first_use_(nullptr),
853 type_(type),
854 next_sibling_(nullptr),
855 parent_(this),
856 register_(reg),
857 spill_slot_(kNoSpillSlot),
858 is_fixed_(is_fixed),
859 is_temp_(is_temp),
860 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000861 is_high_interval_(is_high_interval),
862 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700863 defined_by_(defined_by) {}
864
David Brazdil3fc992f2015-04-16 18:31:55 +0100865 // Searches for a LiveRange that either covers the given position or is the
866 // first next LiveRange. Returns nullptr if no such LiveRange exists. Ranges
867 // known to end before `position` can be skipped with `search_start`.
868 LiveRange* FindRangeAtOrAfter(size_t position, LiveRange* search_start) const {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000869 if (kIsDebugBuild) {
David Brazdil3fc992f2015-04-16 18:31:55 +0100870 if (search_start != first_range_) {
871 // If we are not searching the entire list of ranges, make sure we do
872 // not skip the range we are searching for.
873 if (search_start == nullptr) {
874 DCHECK(IsDeadAt(position));
875 } else if (search_start->GetStart() > position) {
876 DCHECK_EQ(search_start, FindRangeAtOrAfter(position, first_range_));
877 }
David Brazdil5b8e6a52015-02-25 16:17:05 +0000878 }
879 }
880
David Brazdil3fc992f2015-04-16 18:31:55 +0100881 LiveRange* range;
882 for (range = search_start;
883 range != nullptr && range->GetEnd() <= position;
884 range = range->GetNext()) {
885 continue;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000886 }
David Brazdil3fc992f2015-04-16 18:31:55 +0100887 return range;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000888 }
889
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100890 ArenaAllocator* const allocator_;
891
892 // Ranges of this interval. We need a quick access to the last range to test
893 // for liveness (see `IsDeadAt`).
894 LiveRange* first_range_;
895 LiveRange* last_range_;
896
David Brazdil3fc992f2015-04-16 18:31:55 +0100897 // The first range at or after the current position of a linear scan. It is
898 // used to optimize range-searching queries.
899 LiveRange* range_search_start_;
900
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100901 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100902 SafepointPosition* first_safepoint_;
903 SafepointPosition* last_safepoint_;
904
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100905 // Uses of this interval. Note that this linked list is shared amongst siblings.
906 UsePosition* first_use_;
907
908 // The instruction type this interval corresponds to.
909 const Primitive::Type type_;
910
911 // Live interval that is the result of a split.
912 LiveInterval* next_sibling_;
913
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100914 // The first interval from which split intervals come from.
915 LiveInterval* parent_;
916
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100917 // The register allocated to this interval.
918 int register_;
919
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100920 // The spill slot allocated to this interval.
921 int spill_slot_;
922
923 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100924 const bool is_fixed_;
925
926 // Whether the interval is for a temporary.
927 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100928
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100929 // Whether the interval is for a safepoint that calls on slow path.
930 const bool is_slow_path_safepoint_;
931
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000932 // Whether this interval is a synthesized interval for register pair.
933 const bool is_high_interval_;
934
935 // If this interval needs a register pair, the high or low equivalent.
936 // `is_high_interval_` tells whether this holds the low or the high.
937 LiveInterval* high_or_low_interval_;
938
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100939 // The instruction represented by this interval.
940 HInstruction* const defined_by_;
941
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100942 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100943 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100944
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000945 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
946
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100947 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
948};
949
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000950/**
951 * Analysis that computes the liveness of instructions:
952 *
953 * (a) Non-environment uses of an instruction always make
954 * the instruction live.
955 * (b) Environment uses of an instruction whose type is
956 * object (that is, non-primitive), make the instruction live.
957 * This is due to having to keep alive objects that have
958 * finalizers deleting native objects.
959 * (c) When the graph has the debuggable property, environment uses
960 * of an instruction that has a primitive type make the instruction live.
961 * If the graph does not have the debuggable property, the environment
962 * use has no effect, and may get a 'none' value after register allocation.
963 *
964 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
965 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100966class SsaLivenessAnalysis : public ValueObject {
967 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100968 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100969 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100970 codegen_(codegen),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100971 block_infos_(graph->GetArena(), graph->GetBlocks().Size()),
972 instructions_from_ssa_index_(graph->GetArena(), 0),
973 instructions_from_lifetime_position_(graph->GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100974 number_of_ssa_values_(0) {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100975 block_infos_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100976 }
977
978 void Analyze();
979
980 BitVector* GetLiveInSet(const HBasicBlock& block) const {
981 return &block_infos_.Get(block.GetBlockId())->live_in_;
982 }
983
984 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
985 return &block_infos_.Get(block.GetBlockId())->live_out_;
986 }
987
988 BitVector* GetKillSet(const HBasicBlock& block) const {
989 return &block_infos_.Get(block.GetBlockId())->kill_;
990 }
991
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100992 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100993 return instructions_from_ssa_index_.Get(index);
994 }
995
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100996 HInstruction* GetInstructionFromPosition(size_t index) const {
997 return instructions_from_lifetime_position_.Get(index);
998 }
999
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001000 HInstruction* GetTempUser(LiveInterval* temp) const {
1001 // A temporary shares the same lifetime start as the instruction that requires it.
1002 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +00001003 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
1004 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
1005 return user;
1006 }
1007
1008 size_t GetTempIndex(LiveInterval* temp) const {
1009 // We use the input index to store the index of the temporary in the user's temporary list.
1010 DCHECK(temp->IsTemp());
1011 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +01001012 }
1013
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001014 size_t GetMaxLifetimePosition() const {
1015 return instructions_from_lifetime_position_.Size() * 2 - 1;
1016 }
1017
Nicolas Geoffraya7062e02014-05-22 12:50:17 +01001018 size_t GetNumberOfSsaValues() const {
1019 return number_of_ssa_values_;
1020 }
1021
Andreas Gampe7c3952f2015-02-19 18:21:24 -08001022 static constexpr const char* kLivenessPassName = "liveness";
1023
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001024 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +01001025 // Linearize the graph so that:
1026 // (1): a block is always after its dominator,
1027 // (2): blocks of loops are contiguous.
1028 // This creates a natural and efficient ordering when visualizing live ranges.
1029 void LinearizeGraph();
1030
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001031 // Give an SSA number to each instruction that defines a value used by another instruction,
1032 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001033 void NumberInstructions();
1034
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001035 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
1036 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001037
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001038 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
1039 // kill sets, that do not take into account backward branches.
1040 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001041
1042 // After computing the initial sets, this method does a fixed point
1043 // calculation over the live_in and live_out set to take into account
1044 // backwards branches.
1045 void ComputeLiveInAndLiveOutSets();
1046
1047 // Update the live_in set of the block and returns whether it has changed.
1048 bool UpdateLiveIn(const HBasicBlock& block);
1049
1050 // Update the live_out set of the block and returns whether it has changed.
1051 bool UpdateLiveOut(const HBasicBlock& block);
1052
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001053 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
1054 if (instruction == nullptr) return false;
1055 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1056 return instruction->GetType() == Primitive::kPrimNot;
1057 }
1058
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001059 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001060 CodeGenerator* const codegen_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001061 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001062
1063 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001064 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001065
1066 // Temporary array used when inserting moves in the graph.
1067 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001068 size_t number_of_ssa_values_;
1069
1070 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1071};
1072
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001073} // namespace art
1074
1075#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_