blob: 98f98a29d12eb94722595d2240fa63e4085ea00d [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_;
134 }
135
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000136 UsePosition* Dup(ArenaAllocator* allocator) const {
137 return new (allocator) UsePosition(
138 user_, input_index_, is_environment_, position_,
139 next_ == nullptr ? nullptr : next_->Dup(allocator));
140 }
141
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100142 private:
143 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100144 const size_t input_index_;
145 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100147 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100148
149 DISALLOW_COPY_AND_ASSIGN(UsePosition);
150};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100151
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100152class SafepointPosition : public ArenaObject<kArenaAllocMisc> {
153 public:
154 explicit SafepointPosition(HInstruction* instruction)
155 : instruction_(instruction),
156 next_(nullptr) {}
157
158 void SetNext(SafepointPosition* next) {
159 next_ = next;
160 }
161
162 size_t GetPosition() const {
163 return instruction_->GetLifetimePosition();
164 }
165
166 SafepointPosition* GetNext() const {
167 return next_;
168 }
169
170 LocationSummary* GetLocations() const {
171 return instruction_->GetLocations();
172 }
173
174 HInstruction* GetInstruction() const {
175 return instruction_;
176 }
177
178 private:
179 HInstruction* const instruction_;
180 SafepointPosition* next_;
181
182 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
183};
184
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185/**
186 * An interval is a list of disjoint live ranges where an instruction is live.
187 * Each instruction that has uses gets an interval.
188 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700189class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100190 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700191 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
192 Primitive::Type type,
193 HInstruction* instruction = nullptr) {
194 return new (allocator) LiveInterval(allocator, type, instruction);
195 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100196
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100197 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
198 return new (allocator) LiveInterval(
199 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
200 }
201
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100202 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100203 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
204 }
205
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100206 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
207 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100208 }
209
210 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700211 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100212 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700213 // This interval is the result of a split.
214 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100215
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000216 void AddTempUse(HInstruction* instruction, size_t temp_index) {
217 DCHECK(IsTemp());
218 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
219 size_t position = instruction->GetLifetimePosition();
220 first_use_ = new (allocator_) UsePosition(
221 instruction, temp_index, /* is_environment */ false, position, first_use_);
222 AddRange(position, position + 1);
223 }
224
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000225 void AddUse(HInstruction* instruction,
226 size_t input_index,
227 bool is_environment,
228 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100229 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000230 size_t position = instruction->GetLifetimePosition() + 1;
231 LocationSummary* locations = instruction->GetLocations();
232 if (!is_environment) {
233 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
234 // For fixed inputs and output same as input, the register allocator
235 // requires to have inputs die at the instruction, so that input moves use the
236 // location of the input just before that instruction (and not potential moves due
237 // to splitting).
238 position = instruction->GetLifetimePosition();
239 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100240 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000241
242 DCHECK(position == instruction->GetLifetimePosition()
243 || position == instruction->GetLifetimePosition() + 1);
244
Nicolas Geoffray76905622014-09-25 14:39:26 +0100245 if ((first_use_ != nullptr)
246 && (first_use_->GetUser() == instruction)
247 && (first_use_->GetPosition() < position)) {
248 // The user uses the instruction multiple times, and one use dies before the other.
249 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000250 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100251 UsePosition* cursor = first_use_;
252 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
253 cursor = cursor->GetNext();
254 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100255 DCHECK(first_use_->GetPosition() + 1 == position);
256 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100257 instruction, input_index, is_environment, position, cursor->GetNext());
258 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100259 if (first_range_->GetEnd() == first_use_->GetPosition()) {
260 first_range_->end_ = position;
261 }
262 return;
263 }
264
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000265 first_use_ = new (allocator_) UsePosition(
266 instruction, input_index, is_environment, position, first_use_);
267
268 if (is_environment && !keep_alive) {
269 // If this environment use does not keep the instruction live, it does not
270 // affect the live range of that instruction.
271 return;
272 }
273
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100274 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100275 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100277 first_range_ = last_range_ = new (allocator_) LiveRange(
278 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100279 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100280 // There is a use later in the same block or in a following block.
281 // Note that in such a case, `AddRange` for the whole blocks has been called
282 // before arriving in this method, and this is the reason the start of
283 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100284 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100285 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100286 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100287 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100288 // Note that the start of `first_range_` can be equal to `end`: two blocks
289 // having adjacent lifetime positions are not necessarily
290 // predecessor/successor. When two blocks are predecessor/successor, the
291 // liveness algorithm has called `AddRange` before arriving in this method,
292 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100293 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100294 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100295 }
296
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100297 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100298 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100299 first_use_ = new (allocator_) UsePosition(
300 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100301 }
302
303 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100304 if (first_range_ == nullptr) {
305 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
306 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100307 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100309 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
310 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100311 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100312 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100313 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100314 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100315 }
316 }
317
318 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100319 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000320 DCHECK_LE(start, first_range_->GetStart());
321 // Find the range that covers the positions after the loop.
322 LiveRange* after_loop = first_range_;
323 LiveRange* last_in_loop = nullptr;
324 while (after_loop != nullptr && after_loop->GetEnd() < end) {
325 DCHECK_LE(start, after_loop->GetStart());
326 last_in_loop = after_loop;
327 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100328 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000329 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100330 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100331 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000332 } else if (after_loop->GetStart() <= end) {
333 first_range_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100334 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100335 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000336 } else {
337 // The use after the loop is after a lifetime hole.
338 DCHECK(last_in_loop != nullptr);
339 first_range_ = last_in_loop;
340 first_range_->start_ = start;
341 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100342 }
343 }
344
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100345 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100346 void SetSpillSlot(int slot) {
347 DCHECK(!is_fixed_);
348 DCHECK(!is_temp_);
349 spill_slot_ = slot;
350 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100351 int GetSpillSlot() const { return spill_slot_; }
352
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100353 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100354 if (first_range_ != nullptr) {
355 first_range_->start_ = from;
356 } else {
357 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000358 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100359 DCHECK(from == defined_by_->GetLifetimePosition());
360 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
361 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100362 }
363
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100364 LiveInterval* GetParent() const { return parent_; }
365
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100366 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000367 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100368
369 int GetRegister() const { return register_; }
370 void SetRegister(int reg) { register_ = reg; }
371 void ClearRegister() { register_ = kNoRegister; }
372 bool HasRegister() const { return register_ != kNoRegister; }
373
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100374 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100375 return last_range_->GetEnd() <= position;
376 }
377
David Brazdil5b8e6a52015-02-25 16:17:05 +0000378 bool Covers(size_t position) {
379 return !IsDeadAt(position) && FindRangeAt(position) != nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100380 }
381
382 /**
383 * Returns the first intersection of this interval with `other`.
384 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100385 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100386 // Advance both intervals and find the first matching range start in
387 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100388 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100389 LiveRange* other_range = other->first_range_;
390 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000391 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100392 my_range = my_range->GetNext();
393 if (my_range == nullptr) {
394 return kNoLifetime;
395 }
David Brazdil714e14f2015-02-25 11:57:05 +0000396 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100397 other_range = other_range->GetNext();
398 if (other_range == nullptr) {
399 return kNoLifetime;
400 }
David Brazdil714e14f2015-02-25 11:57:05 +0000401 } else {
402 DCHECK(my_range->IntersectsWith(*other_range));
403 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100404 }
405 } while (true);
406 }
407
408 size_t GetStart() const {
409 return first_range_->GetStart();
410 }
411
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100412 size_t GetEnd() const {
413 return last_range_->GetEnd();
414 }
415
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100416 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100417 if (is_temp_) {
418 return position == GetStart() ? position : kNoLifetime;
419 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100420 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100421 LocationSummary* locations = defined_by_->GetLocations();
422 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100423 // This interval is the first interval of the instruction. If the output
424 // of the instruction requires a register, we return the position of that instruction
425 // as the first register use.
426 if (location.IsUnallocated()) {
427 if ((location.GetPolicy() == Location::kRequiresRegister)
428 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000429 && (locations->InAt(0).IsRegister()
430 || locations->InAt(0).IsRegisterPair()
431 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100432 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100433 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
434 || (location.GetPolicy() == Location::kSameAsFirstInput
435 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
436 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100437 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000438 } else if (location.IsRegister() || location.IsRegisterPair()) {
439 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100440 }
441 }
442
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100443 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100444 size_t end = GetEnd();
445 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100446 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100447 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100448 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100449 if (location.IsUnallocated()
450 && (location.GetPolicy() == Location::kRequiresRegister
451 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100452 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100453 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100454 }
455 use = use->GetNext();
456 }
457 return kNoLifetime;
458 }
459
460 size_t FirstRegisterUse() const {
461 return FirstRegisterUseAfter(GetStart());
462 }
463
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000464 size_t FirstUseAfter(size_t position) const {
465 if (is_temp_) {
466 return position == GetStart() ? position : kNoLifetime;
467 }
468
469 UsePosition* use = first_use_;
470 size_t end = GetEnd();
471 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000472 if (!use->GetIsEnvironment()) {
473 size_t use_position = use->GetPosition();
474 if (use_position > position) {
475 return use_position;
476 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000477 }
478 use = use->GetNext();
479 }
480 return kNoLifetime;
481 }
482
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100483 UsePosition* GetFirstUse() const {
484 return first_use_;
485 }
486
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100487 Primitive::Type GetType() const {
488 return type_;
489 }
490
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100491 HInstruction* GetDefinedBy() const {
492 return defined_by_;
493 }
494
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100495 SafepointPosition* FindSafepointJustBefore(size_t position) const {
496 for (SafepointPosition* safepoint = first_safepoint_, *previous = nullptr;
497 safepoint != nullptr;
498 previous = safepoint, safepoint = safepoint->GetNext()) {
499 if (safepoint->GetPosition() >= position) return previous;
500 }
501 return last_safepoint_;
502 }
503
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100504 /**
505 * Split this interval at `position`. This interval is changed to:
506 * [start ... position).
507 *
508 * The new interval covers:
509 * [position ... end)
510 */
511 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100512 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100513 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100514 DCHECK_GT(position, GetStart());
515
516 if (last_range_->GetEnd() <= position) {
517 // This range dies before `position`, no need to split.
518 return nullptr;
519 }
520
521 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100522 SafepointPosition* new_last_safepoint = FindSafepointJustBefore(position);
523 if (new_last_safepoint == nullptr) {
524 new_interval->first_safepoint_ = first_safepoint_;
525 new_interval->last_safepoint_ = last_safepoint_;
526 first_safepoint_ = last_safepoint_ = nullptr;
527 } else if (last_safepoint_ != new_last_safepoint) {
528 new_interval->last_safepoint_ = last_safepoint_;
529 new_interval->first_safepoint_ = new_last_safepoint->GetNext();
530 DCHECK(new_interval->first_safepoint_ != nullptr);
531 last_safepoint_ = new_last_safepoint;
532 last_safepoint_->SetNext(nullptr);
533 }
534
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100535 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100536 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100537 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100538
539 new_interval->first_use_ = first_use_;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000540 last_visited_range_ = nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100541 LiveRange* current = first_range_;
542 LiveRange* previous = nullptr;
543 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000544 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100545 do {
546 if (position >= current->GetEnd()) {
547 // Move to next range.
548 previous = current;
549 current = current->next_;
550 } else if (position <= current->GetStart()) {
551 // If the previous range did not cover this position, we know position is in
552 // a lifetime hole. We can just break the first_range_ and last_range_ links
553 // and return the new interval.
554 DCHECK(previous != nullptr);
555 DCHECK(current != first_range_);
556 new_interval->last_range_ = last_range_;
557 last_range_ = previous;
558 previous->next_ = nullptr;
559 new_interval->first_range_ = current;
560 return new_interval;
561 } else {
562 // This range covers position. We create a new last_range_ for this interval
563 // that covers last_range_->Start() and position. We also shorten the current
564 // range and make it the first range of the new interval.
565 DCHECK(position < current->GetEnd() && position > current->GetStart());
566 new_interval->last_range_ = last_range_;
567 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
568 if (previous != nullptr) {
569 previous->next_ = last_range_;
570 } else {
571 first_range_ = last_range_;
572 }
573 new_interval->first_range_ = current;
574 current->start_ = position;
575 return new_interval;
576 }
577 } while (current != nullptr);
578
579 LOG(FATAL) << "Unreachable";
580 return nullptr;
581 }
582
Nicolas Geoffray76905622014-09-25 14:39:26 +0100583 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100584 return GetStart() <= other->GetStart();
585 }
586
587 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100588 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100589 }
590
591 void Dump(std::ostream& stream) const {
592 stream << "ranges: { ";
593 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000594 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100595 current->Dump(stream);
596 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000597 current = current->GetNext();
598 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100599 stream << "}, uses: { ";
600 UsePosition* use = first_use_;
601 if (use != nullptr) {
602 do {
603 use->Dump(stream);
604 stream << " ";
605 } while ((use = use->GetNext()) != nullptr);
606 }
607 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700608 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000609 stream << " is_high: " << IsHighInterval();
610 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100611 }
612
613 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000614 LiveInterval* GetLastSibling() {
615 LiveInterval* result = this;
616 while (result->next_sibling_ != nullptr) {
617 result = result->next_sibling_;
618 }
619 return result;
620 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100621
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100622 // Returns the first register hint that is at least free before
623 // the value contained in `free_until`. If none is found, returns
624 // `kNoRegister`.
625 int FindFirstRegisterHint(size_t* free_until) const;
626
627 // If there is enough at the definition site to find a register (for example
628 // it uses the same input as the first input), returns the register as a hint.
629 // Returns kNoRegister otherwise.
630 int FindHintAtDefinition() const;
631
632 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
633 // slots for spilling.
634 bool NeedsTwoSpillSlots() const;
635
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100636 bool IsFloatingPoint() const {
637 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
638 }
639
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100640 // Converts the location of the interval to a `Location` object.
641 Location ToLocation() const;
642
643 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000644 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100645
646 // Finds the interval that covers `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000647 const LiveInterval& GetIntervalAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100648
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100649 // Returns whether `other` and `this` share the same kind of register.
650 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000651 bool SameRegisterKind(const LiveInterval& other) const {
652 return IsFloatingPoint() == other.IsFloatingPoint();
653 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100654
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000655 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000656 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000657 }
658
659 bool HasLowInterval() const {
660 return IsHighInterval();
661 }
662
663 LiveInterval* GetLowInterval() const {
664 DCHECK(HasLowInterval());
665 return high_or_low_interval_;
666 }
667
668 LiveInterval* GetHighInterval() const {
669 DCHECK(HasHighInterval());
670 return high_or_low_interval_;
671 }
672
673 bool IsHighInterval() const {
674 return GetParent()->is_high_interval_;
675 }
676
677 bool IsLowInterval() const {
678 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
679 }
680
681 void SetLowInterval(LiveInterval* low) {
682 DCHECK(IsHighInterval());
683 high_or_low_interval_ = low;
684 }
685
686 void SetHighInterval(LiveInterval* high) {
687 DCHECK(IsLowInterval());
688 high_or_low_interval_ = high;
689 }
690
691 void AddHighInterval(bool is_temp = false) {
692 DCHECK_EQ(GetParent(), this);
693 DCHECK(!HasHighInterval());
694 DCHECK(!HasLowInterval());
695 high_or_low_interval_ = new (allocator_) LiveInterval(
696 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
697 high_or_low_interval_->high_or_low_interval_ = this;
698 if (first_range_ != nullptr) {
699 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
700 high_or_low_interval_->last_range_ = first_range_->GetLastRange();
701 }
702 if (first_use_ != nullptr) {
703 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
704 }
705 }
706
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000707 // Returns whether an interval, when it is non-split, is using
708 // the same register of one of its input.
709 bool IsUsingInputRegister() const {
710 if (defined_by_ != nullptr && !IsSplit()) {
711 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
712 LiveInterval* interval = it.Current()->GetLiveInterval();
713
714 // Find the interval that covers `defined_by`_.
715 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
716 interval = interval->GetNextSibling();
717 }
718
719 // Check if both intervals have the same register of the same kind.
720 if (interval != nullptr
721 && interval->SameRegisterKind(*this)
722 && interval->GetRegister() == GetRegister()) {
723 return true;
724 }
725 }
726 }
727 return false;
728 }
729
730 // Returns whether an interval, when it is non-split, can safely use
731 // the same register of one of its input. Note that this method requires
732 // IsUsingInputRegister() to be true.
733 bool CanUseInputRegister() const {
734 DCHECK(IsUsingInputRegister());
735 if (defined_by_ != nullptr && !IsSplit()) {
736 LocationSummary* locations = defined_by_->GetLocations();
737 if (locations->OutputCanOverlapWithInputs()) {
738 return false;
739 }
740 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
741 LiveInterval* interval = it.Current()->GetLiveInterval();
742
743 // Find the interval that covers `defined_by`_.
744 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
745 interval = interval->GetNextSibling();
746 }
747
748 if (interval != nullptr
749 && interval->SameRegisterKind(*this)
750 && interval->GetRegister() == GetRegister()) {
751 // We found the input that has the same register. Check if it is live after
752 // `defined_by`_.
753 return !interval->Covers(defined_by_->GetLifetimePosition() + 1);
754 }
755 }
756 }
757 LOG(FATAL) << "Unreachable";
758 UNREACHABLE();
759 }
760
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100761 void AddSafepoint(HInstruction* instruction) {
762 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
763 if (first_safepoint_ == nullptr) {
764 first_safepoint_ = last_safepoint_ = safepoint;
765 } else {
766 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
767 last_safepoint_->SetNext(safepoint);
768 last_safepoint_ = safepoint;
769 }
770 }
771
772 SafepointPosition* GetFirstSafepoint() const {
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100773 return first_safepoint_;
774 }
775
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100776 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700777 LiveInterval(ArenaAllocator* allocator,
778 Primitive::Type type,
779 HInstruction* defined_by = nullptr,
780 bool is_fixed = false,
781 int reg = kNoRegister,
782 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000783 bool is_slow_path_safepoint = false,
784 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700785 : allocator_(allocator),
786 first_range_(nullptr),
787 last_range_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100788 first_safepoint_(nullptr),
789 last_safepoint_(nullptr),
David Brazdil5b8e6a52015-02-25 16:17:05 +0000790 last_visited_range_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700791 first_use_(nullptr),
792 type_(type),
793 next_sibling_(nullptr),
794 parent_(this),
795 register_(reg),
796 spill_slot_(kNoSpillSlot),
797 is_fixed_(is_fixed),
798 is_temp_(is_temp),
799 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000800 is_high_interval_(is_high_interval),
801 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700802 defined_by_(defined_by) {}
803
David Brazdil5b8e6a52015-02-25 16:17:05 +0000804 // Returns a LiveRange covering the given position or nullptr if no such range
805 // exists in the interval.
806 // This is a linear search optimized for multiple queries in a non-decreasing
807 // position order typical for linear scan register allocation.
808 LiveRange* FindRangeAt(size_t position) {
809 // Make sure operations on the interval didn't leave us with a cached result
810 // from a sibling.
811 if (kIsDebugBuild) {
812 if (last_visited_range_ != nullptr) {
813 DCHECK_GE(last_visited_range_->GetStart(), GetStart());
814 DCHECK_LE(last_visited_range_->GetEnd(), GetEnd());
815 }
816 }
817
818 // If this method was called earlier on a lower position, use that result as
819 // a starting point to save time. However, linear scan performs 3 scans:
820 // integers, floats, and resolution. Instead of resetting at the beginning
821 // of a scan, we do it here.
822 LiveRange* current;
823 if (last_visited_range_ != nullptr && position >= last_visited_range_->GetStart()) {
824 current = last_visited_range_;
825 } else {
826 current = first_range_;
827 }
828 while (current != nullptr && current->GetEnd() <= position) {
829 current = current->GetNext();
830 }
831 last_visited_range_ = current;
832 if (current != nullptr && position >= current->GetStart()) {
833 return current;
834 } else {
835 return nullptr;
836 }
837 }
838
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100839 ArenaAllocator* const allocator_;
840
841 // Ranges of this interval. We need a quick access to the last range to test
842 // for liveness (see `IsDeadAt`).
843 LiveRange* first_range_;
844 LiveRange* last_range_;
845
Nicolas Geoffray43af7282015-04-16 13:01:01 +0100846 // Safepoints where this interval is live.
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100847 SafepointPosition* first_safepoint_;
848 SafepointPosition* last_safepoint_;
849
David Brazdil5b8e6a52015-02-25 16:17:05 +0000850 // Last visited range. This is a range search optimization leveraging the fact
851 // that the register allocator does a linear scan through the intervals.
852 LiveRange* last_visited_range_;
853
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100854 // Uses of this interval. Note that this linked list is shared amongst siblings.
855 UsePosition* first_use_;
856
857 // The instruction type this interval corresponds to.
858 const Primitive::Type type_;
859
860 // Live interval that is the result of a split.
861 LiveInterval* next_sibling_;
862
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100863 // The first interval from which split intervals come from.
864 LiveInterval* parent_;
865
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100866 // The register allocated to this interval.
867 int register_;
868
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100869 // The spill slot allocated to this interval.
870 int spill_slot_;
871
872 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100873 const bool is_fixed_;
874
875 // Whether the interval is for a temporary.
876 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100877
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100878 // Whether the interval is for a safepoint that calls on slow path.
879 const bool is_slow_path_safepoint_;
880
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000881 // Whether this interval is a synthesized interval for register pair.
882 const bool is_high_interval_;
883
884 // If this interval needs a register pair, the high or low equivalent.
885 // `is_high_interval_` tells whether this holds the low or the high.
886 LiveInterval* high_or_low_interval_;
887
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100888 // The instruction represented by this interval.
889 HInstruction* const defined_by_;
890
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100891 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100892 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100893
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000894 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
895
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100896 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
897};
898
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000899/**
900 * Analysis that computes the liveness of instructions:
901 *
902 * (a) Non-environment uses of an instruction always make
903 * the instruction live.
904 * (b) Environment uses of an instruction whose type is
905 * object (that is, non-primitive), make the instruction live.
906 * This is due to having to keep alive objects that have
907 * finalizers deleting native objects.
908 * (c) When the graph has the debuggable property, environment uses
909 * of an instruction that has a primitive type make the instruction live.
910 * If the graph does not have the debuggable property, the environment
911 * use has no effect, and may get a 'none' value after register allocation.
912 *
913 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
914 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100915class SsaLivenessAnalysis : public ValueObject {
916 public:
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100917 SsaLivenessAnalysis(HGraph* graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100918 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100919 codegen_(codegen),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100920 block_infos_(graph->GetArena(), graph->GetBlocks().Size()),
921 instructions_from_ssa_index_(graph->GetArena(), 0),
922 instructions_from_lifetime_position_(graph->GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100923 number_of_ssa_values_(0) {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100924 block_infos_.SetSize(graph->GetBlocks().Size());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100925 }
926
927 void Analyze();
928
929 BitVector* GetLiveInSet(const HBasicBlock& block) const {
930 return &block_infos_.Get(block.GetBlockId())->live_in_;
931 }
932
933 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
934 return &block_infos_.Get(block.GetBlockId())->live_out_;
935 }
936
937 BitVector* GetKillSet(const HBasicBlock& block) const {
938 return &block_infos_.Get(block.GetBlockId())->kill_;
939 }
940
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100941 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100942 return instructions_from_ssa_index_.Get(index);
943 }
944
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100945 HInstruction* GetInstructionFromPosition(size_t index) const {
946 return instructions_from_lifetime_position_.Get(index);
947 }
948
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100949 HInstruction* GetTempUser(LiveInterval* temp) const {
950 // A temporary shares the same lifetime start as the instruction that requires it.
951 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000952 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
953 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
954 return user;
955 }
956
957 size_t GetTempIndex(LiveInterval* temp) const {
958 // We use the input index to store the index of the temporary in the user's temporary list.
959 DCHECK(temp->IsTemp());
960 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100961 }
962
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100963 size_t GetMaxLifetimePosition() const {
964 return instructions_from_lifetime_position_.Size() * 2 - 1;
965 }
966
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100967 size_t GetNumberOfSsaValues() const {
968 return number_of_ssa_values_;
969 }
970
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800971 static constexpr const char* kLivenessPassName = "liveness";
972
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100973 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100974 // Linearize the graph so that:
975 // (1): a block is always after its dominator,
976 // (2): blocks of loops are contiguous.
977 // This creates a natural and efficient ordering when visualizing live ranges.
978 void LinearizeGraph();
979
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100980 // Give an SSA number to each instruction that defines a value used by another instruction,
981 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100982 void NumberInstructions();
983
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100984 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
985 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100986
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100987 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
988 // kill sets, that do not take into account backward branches.
989 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100990
991 // After computing the initial sets, this method does a fixed point
992 // calculation over the live_in and live_out set to take into account
993 // backwards branches.
994 void ComputeLiveInAndLiveOutSets();
995
996 // Update the live_in set of the block and returns whether it has changed.
997 bool UpdateLiveIn(const HBasicBlock& block);
998
999 // Update the live_out set of the block and returns whether it has changed.
1000 bool UpdateLiveOut(const HBasicBlock& block);
1001
Nicolas Geoffray915b9d02015-03-11 15:11:19 +00001002 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
1003 if (instruction == nullptr) return false;
1004 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
1005 return instruction->GetType() == Primitive::kPrimNot;
1006 }
1007
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +01001008 HGraph* const graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001009 CodeGenerator* const codegen_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001010 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001011
1012 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +01001013 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01001014
1015 // Temporary array used when inserting moves in the graph.
1016 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001017 size_t number_of_ssa_values_;
1018
1019 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1020};
1021
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001022} // namespace art
1023
1024#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_