blob: bc78dc2e766ec138ff394f5cee6808ad6c471dca [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
152/**
153 * An interval is a list of disjoint live ranges where an instruction is live.
154 * Each instruction that has uses gets an interval.
155 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700156class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100157 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700158 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
159 Primitive::Type type,
160 HInstruction* instruction = nullptr) {
161 return new (allocator) LiveInterval(allocator, type, instruction);
162 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100163
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100164 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
165 return new (allocator) LiveInterval(
166 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
167 }
168
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100169 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100170 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
171 }
172
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100173 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
174 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100175 }
176
177 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700178 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100179 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700180 // This interval is the result of a split.
181 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100182
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000183 void AddTempUse(HInstruction* instruction, size_t temp_index) {
184 DCHECK(IsTemp());
185 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
186 size_t position = instruction->GetLifetimePosition();
187 first_use_ = new (allocator_) UsePosition(
188 instruction, temp_index, /* is_environment */ false, position, first_use_);
189 AddRange(position, position + 1);
190 }
191
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100192 void AddUse(HInstruction* instruction, size_t input_index, bool is_environment) {
193 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000194 size_t position = instruction->GetLifetimePosition() + 1;
195 LocationSummary* locations = instruction->GetLocations();
196 if (!is_environment) {
197 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
198 // For fixed inputs and output same as input, the register allocator
199 // requires to have inputs die at the instruction, so that input moves use the
200 // location of the input just before that instruction (and not potential moves due
201 // to splitting).
202 position = instruction->GetLifetimePosition();
203 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100204 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000205
206 DCHECK(position == instruction->GetLifetimePosition()
207 || position == instruction->GetLifetimePosition() + 1);
208
Nicolas Geoffray76905622014-09-25 14:39:26 +0100209 if ((first_use_ != nullptr)
210 && (first_use_->GetUser() == instruction)
211 && (first_use_->GetPosition() < position)) {
212 // The user uses the instruction multiple times, and one use dies before the other.
213 // We update the use list so that the latter is first.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100214 UsePosition* cursor = first_use_;
215 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
216 cursor = cursor->GetNext();
217 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100218 DCHECK(first_use_->GetPosition() + 1 == position);
219 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100220 instruction, input_index, is_environment, position, cursor->GetNext());
221 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100222 if (first_range_->GetEnd() == first_use_->GetPosition()) {
223 first_range_->end_ = position;
224 }
225 return;
226 }
227
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100228 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100229 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100230 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100231 first_range_ = last_range_ = new (allocator_) LiveRange(
232 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100233 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100234 // There is a use later in the same block or in a following block.
235 // Note that in such a case, `AddRange` for the whole blocks has been called
236 // before arriving in this method, and this is the reason the start of
237 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100238 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100239 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100240 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100241 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100242 // Note that the start of `first_range_` can be equal to `end`: two blocks
243 // having adjacent lifetime positions are not necessarily
244 // predecessor/successor. When two blocks are predecessor/successor, the
245 // liveness algorithm has called `AddRange` before arriving in this method,
246 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100247 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100248 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100249 first_use_ = new (allocator_) UsePosition(
250 instruction, input_index, is_environment, position, first_use_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100251 }
252
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100253 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100254 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100255 first_use_ = new (allocator_) UsePosition(
256 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100257 }
258
259 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100260 if (first_range_ == nullptr) {
261 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
262 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100263 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100264 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100265 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
266 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100267 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100268 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100269 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100270 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100271 }
272 }
273
274 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100275 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000276 DCHECK_LE(start, first_range_->GetStart());
277 // Find the range that covers the positions after the loop.
278 LiveRange* after_loop = first_range_;
279 LiveRange* last_in_loop = nullptr;
280 while (after_loop != nullptr && after_loop->GetEnd() < end) {
281 DCHECK_LE(start, after_loop->GetStart());
282 last_in_loop = after_loop;
283 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100284 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000285 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100286 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100287 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000288 } else if (after_loop->GetStart() <= end) {
289 first_range_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100290 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100291 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000292 } else {
293 // The use after the loop is after a lifetime hole.
294 DCHECK(last_in_loop != nullptr);
295 first_range_ = last_in_loop;
296 first_range_->start_ = start;
297 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100298 }
299 }
300
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100301 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100302 void SetSpillSlot(int slot) {
303 DCHECK(!is_fixed_);
304 DCHECK(!is_temp_);
305 spill_slot_ = slot;
306 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100307 int GetSpillSlot() const { return spill_slot_; }
308
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100309 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100310 if (first_range_ != nullptr) {
311 first_range_->start_ = from;
312 } else {
313 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000314 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100315 DCHECK(from == defined_by_->GetLifetimePosition());
316 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
317 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100318 }
319
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100320 LiveInterval* GetParent() const { return parent_; }
321
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100322 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000323 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100324
325 int GetRegister() const { return register_; }
326 void SetRegister(int reg) { register_ = reg; }
327 void ClearRegister() { register_ = kNoRegister; }
328 bool HasRegister() const { return register_ != kNoRegister; }
329
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100330 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100331 return last_range_->GetEnd() <= position;
332 }
333
David Brazdil5b8e6a52015-02-25 16:17:05 +0000334 bool Covers(size_t position) {
335 return !IsDeadAt(position) && FindRangeAt(position) != nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100336 }
337
338 /**
339 * Returns the first intersection of this interval with `other`.
340 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100341 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 // Advance both intervals and find the first matching range start in
343 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100344 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100345 LiveRange* other_range = other->first_range_;
346 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000347 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100348 my_range = my_range->GetNext();
349 if (my_range == nullptr) {
350 return kNoLifetime;
351 }
David Brazdil714e14f2015-02-25 11:57:05 +0000352 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100353 other_range = other_range->GetNext();
354 if (other_range == nullptr) {
355 return kNoLifetime;
356 }
David Brazdil714e14f2015-02-25 11:57:05 +0000357 } else {
358 DCHECK(my_range->IntersectsWith(*other_range));
359 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100360 }
361 } while (true);
362 }
363
364 size_t GetStart() const {
365 return first_range_->GetStart();
366 }
367
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100368 size_t GetEnd() const {
369 return last_range_->GetEnd();
370 }
371
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100372 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100373 if (is_temp_) {
374 return position == GetStart() ? position : kNoLifetime;
375 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100376 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100377 LocationSummary* locations = defined_by_->GetLocations();
378 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100379 // This interval is the first interval of the instruction. If the output
380 // of the instruction requires a register, we return the position of that instruction
381 // as the first register use.
382 if (location.IsUnallocated()) {
383 if ((location.GetPolicy() == Location::kRequiresRegister)
384 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000385 && (locations->InAt(0).IsRegister()
386 || locations->InAt(0).IsRegisterPair()
387 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100388 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100389 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
390 || (location.GetPolicy() == Location::kSameAsFirstInput
391 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
392 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100393 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000394 } else if (location.IsRegister() || location.IsRegisterPair()) {
395 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100396 }
397 }
398
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100399 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100400 size_t end = GetEnd();
401 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100402 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100403 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100404 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100405 if (location.IsUnallocated()
406 && (location.GetPolicy() == Location::kRequiresRegister
407 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100408 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100409 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100410 }
411 use = use->GetNext();
412 }
413 return kNoLifetime;
414 }
415
416 size_t FirstRegisterUse() const {
417 return FirstRegisterUseAfter(GetStart());
418 }
419
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000420 size_t FirstUseAfter(size_t position) const {
421 if (is_temp_) {
422 return position == GetStart() ? position : kNoLifetime;
423 }
424
425 UsePosition* use = first_use_;
426 size_t end = GetEnd();
427 while (use != nullptr && use->GetPosition() <= end) {
428 size_t use_position = use->GetPosition();
429 if (use_position > position) {
430 return use_position;
431 }
432 use = use->GetNext();
433 }
434 return kNoLifetime;
435 }
436
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100437 UsePosition* GetFirstUse() const {
438 return first_use_;
439 }
440
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100441 Primitive::Type GetType() const {
442 return type_;
443 }
444
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100445 HInstruction* GetDefinedBy() const {
446 return defined_by_;
447 }
448
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100449 /**
450 * Split this interval at `position`. This interval is changed to:
451 * [start ... position).
452 *
453 * The new interval covers:
454 * [position ... end)
455 */
456 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100457 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100458 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100459 DCHECK_GT(position, GetStart());
460
461 if (last_range_->GetEnd() <= position) {
462 // This range dies before `position`, no need to split.
463 return nullptr;
464 }
465
466 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100467 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100468 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100469 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100470
471 new_interval->first_use_ = first_use_;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000472 last_visited_range_ = nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100473 LiveRange* current = first_range_;
474 LiveRange* previous = nullptr;
475 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000476 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100477 do {
478 if (position >= current->GetEnd()) {
479 // Move to next range.
480 previous = current;
481 current = current->next_;
482 } else if (position <= current->GetStart()) {
483 // If the previous range did not cover this position, we know position is in
484 // a lifetime hole. We can just break the first_range_ and last_range_ links
485 // and return the new interval.
486 DCHECK(previous != nullptr);
487 DCHECK(current != first_range_);
488 new_interval->last_range_ = last_range_;
489 last_range_ = previous;
490 previous->next_ = nullptr;
491 new_interval->first_range_ = current;
492 return new_interval;
493 } else {
494 // This range covers position. We create a new last_range_ for this interval
495 // that covers last_range_->Start() and position. We also shorten the current
496 // range and make it the first range of the new interval.
497 DCHECK(position < current->GetEnd() && position > current->GetStart());
498 new_interval->last_range_ = last_range_;
499 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
500 if (previous != nullptr) {
501 previous->next_ = last_range_;
502 } else {
503 first_range_ = last_range_;
504 }
505 new_interval->first_range_ = current;
506 current->start_ = position;
507 return new_interval;
508 }
509 } while (current != nullptr);
510
511 LOG(FATAL) << "Unreachable";
512 return nullptr;
513 }
514
Nicolas Geoffray76905622014-09-25 14:39:26 +0100515 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100516 return GetStart() <= other->GetStart();
517 }
518
519 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100520 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100521 }
522
523 void Dump(std::ostream& stream) const {
524 stream << "ranges: { ";
525 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000526 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100527 current->Dump(stream);
528 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000529 current = current->GetNext();
530 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100531 stream << "}, uses: { ";
532 UsePosition* use = first_use_;
533 if (use != nullptr) {
534 do {
535 use->Dump(stream);
536 stream << " ";
537 } while ((use = use->GetNext()) != nullptr);
538 }
539 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700540 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000541 stream << " is_high: " << IsHighInterval();
542 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100543 }
544
545 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000546 LiveInterval* GetLastSibling() {
547 LiveInterval* result = this;
548 while (result->next_sibling_ != nullptr) {
549 result = result->next_sibling_;
550 }
551 return result;
552 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100553
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100554 // Returns the first register hint that is at least free before
555 // the value contained in `free_until`. If none is found, returns
556 // `kNoRegister`.
557 int FindFirstRegisterHint(size_t* free_until) const;
558
559 // If there is enough at the definition site to find a register (for example
560 // it uses the same input as the first input), returns the register as a hint.
561 // Returns kNoRegister otherwise.
562 int FindHintAtDefinition() const;
563
564 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
565 // slots for spilling.
566 bool NeedsTwoSpillSlots() const;
567
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100568 bool IsFloatingPoint() const {
569 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
570 }
571
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100572 // Converts the location of the interval to a `Location` object.
573 Location ToLocation() const;
574
575 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000576 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100577
578 // Finds the interval that covers `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000579 const LiveInterval& GetIntervalAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100580
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100581 // Returns whether `other` and `this` share the same kind of register.
582 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000583 bool SameRegisterKind(const LiveInterval& other) const {
584 return IsFloatingPoint() == other.IsFloatingPoint();
585 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100586
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000587 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000588 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000589 }
590
591 bool HasLowInterval() const {
592 return IsHighInterval();
593 }
594
595 LiveInterval* GetLowInterval() const {
596 DCHECK(HasLowInterval());
597 return high_or_low_interval_;
598 }
599
600 LiveInterval* GetHighInterval() const {
601 DCHECK(HasHighInterval());
602 return high_or_low_interval_;
603 }
604
605 bool IsHighInterval() const {
606 return GetParent()->is_high_interval_;
607 }
608
609 bool IsLowInterval() const {
610 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
611 }
612
613 void SetLowInterval(LiveInterval* low) {
614 DCHECK(IsHighInterval());
615 high_or_low_interval_ = low;
616 }
617
618 void SetHighInterval(LiveInterval* high) {
619 DCHECK(IsLowInterval());
620 high_or_low_interval_ = high;
621 }
622
623 void AddHighInterval(bool is_temp = false) {
624 DCHECK_EQ(GetParent(), this);
625 DCHECK(!HasHighInterval());
626 DCHECK(!HasLowInterval());
627 high_or_low_interval_ = new (allocator_) LiveInterval(
628 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
629 high_or_low_interval_->high_or_low_interval_ = this;
630 if (first_range_ != nullptr) {
631 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
632 high_or_low_interval_->last_range_ = first_range_->GetLastRange();
633 }
634 if (first_use_ != nullptr) {
635 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
636 }
637 }
638
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000639 // Returns whether an interval, when it is non-split, is using
640 // the same register of one of its input.
641 bool IsUsingInputRegister() const {
642 if (defined_by_ != nullptr && !IsSplit()) {
643 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
644 LiveInterval* interval = it.Current()->GetLiveInterval();
645
646 // Find the interval that covers `defined_by`_.
647 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
648 interval = interval->GetNextSibling();
649 }
650
651 // Check if both intervals have the same register of the same kind.
652 if (interval != nullptr
653 && interval->SameRegisterKind(*this)
654 && interval->GetRegister() == GetRegister()) {
655 return true;
656 }
657 }
658 }
659 return false;
660 }
661
662 // Returns whether an interval, when it is non-split, can safely use
663 // the same register of one of its input. Note that this method requires
664 // IsUsingInputRegister() to be true.
665 bool CanUseInputRegister() const {
666 DCHECK(IsUsingInputRegister());
667 if (defined_by_ != nullptr && !IsSplit()) {
668 LocationSummary* locations = defined_by_->GetLocations();
669 if (locations->OutputCanOverlapWithInputs()) {
670 return false;
671 }
672 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
673 LiveInterval* interval = it.Current()->GetLiveInterval();
674
675 // Find the interval that covers `defined_by`_.
676 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
677 interval = interval->GetNextSibling();
678 }
679
680 if (interval != nullptr
681 && interval->SameRegisterKind(*this)
682 && interval->GetRegister() == GetRegister()) {
683 // We found the input that has the same register. Check if it is live after
684 // `defined_by`_.
685 return !interval->Covers(defined_by_->GetLifetimePosition() + 1);
686 }
687 }
688 }
689 LOG(FATAL) << "Unreachable";
690 UNREACHABLE();
691 }
692
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100693 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700694 LiveInterval(ArenaAllocator* allocator,
695 Primitive::Type type,
696 HInstruction* defined_by = nullptr,
697 bool is_fixed = false,
698 int reg = kNoRegister,
699 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000700 bool is_slow_path_safepoint = false,
701 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700702 : allocator_(allocator),
703 first_range_(nullptr),
704 last_range_(nullptr),
David Brazdil5b8e6a52015-02-25 16:17:05 +0000705 last_visited_range_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700706 first_use_(nullptr),
707 type_(type),
708 next_sibling_(nullptr),
709 parent_(this),
710 register_(reg),
711 spill_slot_(kNoSpillSlot),
712 is_fixed_(is_fixed),
713 is_temp_(is_temp),
714 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000715 is_high_interval_(is_high_interval),
716 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700717 defined_by_(defined_by) {}
718
David Brazdil5b8e6a52015-02-25 16:17:05 +0000719 // Returns a LiveRange covering the given position or nullptr if no such range
720 // exists in the interval.
721 // This is a linear search optimized for multiple queries in a non-decreasing
722 // position order typical for linear scan register allocation.
723 LiveRange* FindRangeAt(size_t position) {
724 // Make sure operations on the interval didn't leave us with a cached result
725 // from a sibling.
726 if (kIsDebugBuild) {
727 if (last_visited_range_ != nullptr) {
728 DCHECK_GE(last_visited_range_->GetStart(), GetStart());
729 DCHECK_LE(last_visited_range_->GetEnd(), GetEnd());
730 }
731 }
732
733 // If this method was called earlier on a lower position, use that result as
734 // a starting point to save time. However, linear scan performs 3 scans:
735 // integers, floats, and resolution. Instead of resetting at the beginning
736 // of a scan, we do it here.
737 LiveRange* current;
738 if (last_visited_range_ != nullptr && position >= last_visited_range_->GetStart()) {
739 current = last_visited_range_;
740 } else {
741 current = first_range_;
742 }
743 while (current != nullptr && current->GetEnd() <= position) {
744 current = current->GetNext();
745 }
746 last_visited_range_ = current;
747 if (current != nullptr && position >= current->GetStart()) {
748 return current;
749 } else {
750 return nullptr;
751 }
752 }
753
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100754 ArenaAllocator* const allocator_;
755
756 // Ranges of this interval. We need a quick access to the last range to test
757 // for liveness (see `IsDeadAt`).
758 LiveRange* first_range_;
759 LiveRange* last_range_;
760
David Brazdil5b8e6a52015-02-25 16:17:05 +0000761 // Last visited range. This is a range search optimization leveraging the fact
762 // that the register allocator does a linear scan through the intervals.
763 LiveRange* last_visited_range_;
764
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100765 // Uses of this interval. Note that this linked list is shared amongst siblings.
766 UsePosition* first_use_;
767
768 // The instruction type this interval corresponds to.
769 const Primitive::Type type_;
770
771 // Live interval that is the result of a split.
772 LiveInterval* next_sibling_;
773
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100774 // The first interval from which split intervals come from.
775 LiveInterval* parent_;
776
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100777 // The register allocated to this interval.
778 int register_;
779
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100780 // The spill slot allocated to this interval.
781 int spill_slot_;
782
783 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100784 const bool is_fixed_;
785
786 // Whether the interval is for a temporary.
787 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100788
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100789 // Whether the interval is for a safepoint that calls on slow path.
790 const bool is_slow_path_safepoint_;
791
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000792 // Whether this interval is a synthesized interval for register pair.
793 const bool is_high_interval_;
794
795 // If this interval needs a register pair, the high or low equivalent.
796 // `is_high_interval_` tells whether this holds the low or the high.
797 LiveInterval* high_or_low_interval_;
798
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100799 // The instruction represented by this interval.
800 HInstruction* const defined_by_;
801
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100802 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100803 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100804
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000805 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
806
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100807 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
808};
809
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000810/**
811 * Analysis that computes the liveness of instructions:
812 *
813 * (a) Non-environment uses of an instruction always make
814 * the instruction live.
815 * (b) Environment uses of an instruction whose type is
816 * object (that is, non-primitive), make the instruction live.
817 * This is due to having to keep alive objects that have
818 * finalizers deleting native objects.
819 * (c) When the graph has the debuggable property, environment uses
820 * of an instruction that has a primitive type make the instruction live.
821 * If the graph does not have the debuggable property, the environment
822 * use has no effect, and may get a 'none' value after register allocation.
823 *
824 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
825 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100826class SsaLivenessAnalysis : public ValueObject {
827 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100828 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100829 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100830 codegen_(codegen),
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000831 linear_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100832 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100833 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100834 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100835 number_of_ssa_values_(0) {
836 block_infos_.SetSize(graph.GetBlocks().Size());
837 }
838
839 void Analyze();
840
841 BitVector* GetLiveInSet(const HBasicBlock& block) const {
842 return &block_infos_.Get(block.GetBlockId())->live_in_;
843 }
844
845 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
846 return &block_infos_.Get(block.GetBlockId())->live_out_;
847 }
848
849 BitVector* GetKillSet(const HBasicBlock& block) const {
850 return &block_infos_.Get(block.GetBlockId())->kill_;
851 }
852
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000853 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
854 return linear_order_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100855 }
856
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100857 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100858 return instructions_from_ssa_index_.Get(index);
859 }
860
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100861 HInstruction* GetInstructionFromPosition(size_t index) const {
862 return instructions_from_lifetime_position_.Get(index);
863 }
864
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100865 HInstruction* GetTempUser(LiveInterval* temp) const {
866 // A temporary shares the same lifetime start as the instruction that requires it.
867 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000868 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
869 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
870 return user;
871 }
872
873 size_t GetTempIndex(LiveInterval* temp) const {
874 // We use the input index to store the index of the temporary in the user's temporary list.
875 DCHECK(temp->IsTemp());
876 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100877 }
878
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100879 size_t GetMaxLifetimePosition() const {
880 return instructions_from_lifetime_position_.Size() * 2 - 1;
881 }
882
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100883 size_t GetNumberOfSsaValues() const {
884 return number_of_ssa_values_;
885 }
886
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800887 static constexpr const char* kLivenessPassName = "liveness";
888
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100889 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100890 // Linearize the graph so that:
891 // (1): a block is always after its dominator,
892 // (2): blocks of loops are contiguous.
893 // This creates a natural and efficient ordering when visualizing live ranges.
894 void LinearizeGraph();
895
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100896 // Give an SSA number to each instruction that defines a value used by another instruction,
897 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100898 void NumberInstructions();
899
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100900 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
901 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100902
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100903 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
904 // kill sets, that do not take into account backward branches.
905 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100906
907 // After computing the initial sets, this method does a fixed point
908 // calculation over the live_in and live_out set to take into account
909 // backwards branches.
910 void ComputeLiveInAndLiveOutSets();
911
912 // Update the live_in set of the block and returns whether it has changed.
913 bool UpdateLiveIn(const HBasicBlock& block);
914
915 // Update the live_out set of the block and returns whether it has changed.
916 bool UpdateLiveOut(const HBasicBlock& block);
917
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000918 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
919 if (instruction == nullptr) return false;
920 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
921 return instruction->GetType() == Primitive::kPrimNot;
922 }
923
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100924 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100925 CodeGenerator* const codegen_;
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000926 GrowableArray<HBasicBlock*> linear_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100927 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100928
929 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100930 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100931
932 // Temporary array used when inserting moves in the graph.
933 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100934 size_t number_of_ssa_values_;
935
936 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
937};
938
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000939class HLinearPostOrderIterator : public ValueObject {
940 public:
941 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000942 : order_(liveness.GetLinearOrder()), index_(liveness.GetLinearOrder().Size()) {}
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000943
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000944 bool Done() const { return index_ == 0; }
945
946 HBasicBlock* Current() const { return order_.Get(index_ -1); }
947
948 void Advance() {
949 --index_;
950 DCHECK_GE(index_, 0U);
951 }
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000952
953 private:
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000954 const GrowableArray<HBasicBlock*>& order_;
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000955 size_t index_;
956
957 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
958};
959
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000960class HLinearOrderIterator : public ValueObject {
961 public:
962 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
963 : order_(liveness.GetLinearOrder()), index_(0) {}
964
965 bool Done() const { return index_ == order_.Size(); }
966 HBasicBlock* Current() const { return order_.Get(index_); }
967 void Advance() { ++index_; }
968
969 private:
970 const GrowableArray<HBasicBlock*>& order_;
971 size_t index_;
972
973 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
974};
975
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100976} // namespace art
977
978#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_