blob: d2da84c0c0569f623066b6e4d280ba2c3865342c [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 Geoffrayd8126be2015-03-27 10:22:41 +0000192 void AddUse(HInstruction* instruction,
193 size_t input_index,
194 bool is_environment,
195 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100196 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000197 size_t position = instruction->GetLifetimePosition() + 1;
198 LocationSummary* locations = instruction->GetLocations();
199 if (!is_environment) {
200 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
201 // For fixed inputs and output same as input, the register allocator
202 // requires to have inputs die at the instruction, so that input moves use the
203 // location of the input just before that instruction (and not potential moves due
204 // to splitting).
205 position = instruction->GetLifetimePosition();
206 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100207 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000208
209 DCHECK(position == instruction->GetLifetimePosition()
210 || position == instruction->GetLifetimePosition() + 1);
211
Nicolas Geoffray76905622014-09-25 14:39:26 +0100212 if ((first_use_ != nullptr)
213 && (first_use_->GetUser() == instruction)
214 && (first_use_->GetPosition() < position)) {
215 // The user uses the instruction multiple times, and one use dies before the other.
216 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000217 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100218 UsePosition* cursor = first_use_;
219 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
220 cursor = cursor->GetNext();
221 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100222 DCHECK(first_use_->GetPosition() + 1 == position);
223 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100224 instruction, input_index, is_environment, position, cursor->GetNext());
225 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100226 if (first_range_->GetEnd() == first_use_->GetPosition()) {
227 first_range_->end_ = position;
228 }
229 return;
230 }
231
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000232 first_use_ = new (allocator_) UsePosition(
233 instruction, input_index, is_environment, position, first_use_);
234
235 if (is_environment && !keep_alive) {
236 // If this environment use does not keep the instruction live, it does not
237 // affect the live range of that instruction.
238 return;
239 }
240
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100241 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100242 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100243 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100244 first_range_ = last_range_ = new (allocator_) LiveRange(
245 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100246 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100247 // There is a use later in the same block or in a following block.
248 // Note that in such a case, `AddRange` for the whole blocks has been called
249 // before arriving in this method, and this is the reason the start of
250 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100251 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100252 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100253 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100254 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100255 // Note that the start of `first_range_` can be equal to `end`: two blocks
256 // having adjacent lifetime positions are not necessarily
257 // predecessor/successor. When two blocks are predecessor/successor, the
258 // liveness algorithm has called `AddRange` before arriving in this method,
259 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100260 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100261 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100262 }
263
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100264 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100265 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100266 first_use_ = new (allocator_) UsePosition(
267 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100268 }
269
270 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100271 if (first_range_ == nullptr) {
272 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
273 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100274 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100275 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100276 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
277 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100278 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100279 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100280 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100281 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100282 }
283 }
284
285 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100286 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000287 DCHECK_LE(start, first_range_->GetStart());
288 // Find the range that covers the positions after the loop.
289 LiveRange* after_loop = first_range_;
290 LiveRange* last_in_loop = nullptr;
291 while (after_loop != nullptr && after_loop->GetEnd() < end) {
292 DCHECK_LE(start, after_loop->GetStart());
293 last_in_loop = after_loop;
294 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100295 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000296 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100297 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100298 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000299 } else if (after_loop->GetStart() <= end) {
300 first_range_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100301 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100302 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000303 } else {
304 // The use after the loop is after a lifetime hole.
305 DCHECK(last_in_loop != nullptr);
306 first_range_ = last_in_loop;
307 first_range_->start_ = start;
308 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100309 }
310 }
311
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100312 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100313 void SetSpillSlot(int slot) {
314 DCHECK(!is_fixed_);
315 DCHECK(!is_temp_);
316 spill_slot_ = slot;
317 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100318 int GetSpillSlot() const { return spill_slot_; }
319
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100320 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100321 if (first_range_ != nullptr) {
322 first_range_->start_ = from;
323 } else {
324 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000325 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100326 DCHECK(from == defined_by_->GetLifetimePosition());
327 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
328 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100329 }
330
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100331 LiveInterval* GetParent() const { return parent_; }
332
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100333 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000334 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100335
336 int GetRegister() const { return register_; }
337 void SetRegister(int reg) { register_ = reg; }
338 void ClearRegister() { register_ = kNoRegister; }
339 bool HasRegister() const { return register_ != kNoRegister; }
340
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100341 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100342 return last_range_->GetEnd() <= position;
343 }
344
David Brazdil5b8e6a52015-02-25 16:17:05 +0000345 bool Covers(size_t position) {
346 return !IsDeadAt(position) && FindRangeAt(position) != nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100347 }
348
349 /**
350 * Returns the first intersection of this interval with `other`.
351 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100352 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100353 // Advance both intervals and find the first matching range start in
354 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100355 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100356 LiveRange* other_range = other->first_range_;
357 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000358 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100359 my_range = my_range->GetNext();
360 if (my_range == nullptr) {
361 return kNoLifetime;
362 }
David Brazdil714e14f2015-02-25 11:57:05 +0000363 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100364 other_range = other_range->GetNext();
365 if (other_range == nullptr) {
366 return kNoLifetime;
367 }
David Brazdil714e14f2015-02-25 11:57:05 +0000368 } else {
369 DCHECK(my_range->IntersectsWith(*other_range));
370 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100371 }
372 } while (true);
373 }
374
375 size_t GetStart() const {
376 return first_range_->GetStart();
377 }
378
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100379 size_t GetEnd() const {
380 return last_range_->GetEnd();
381 }
382
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100383 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100384 if (is_temp_) {
385 return position == GetStart() ? position : kNoLifetime;
386 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100387 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100388 LocationSummary* locations = defined_by_->GetLocations();
389 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100390 // This interval is the first interval of the instruction. If the output
391 // of the instruction requires a register, we return the position of that instruction
392 // as the first register use.
393 if (location.IsUnallocated()) {
394 if ((location.GetPolicy() == Location::kRequiresRegister)
395 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000396 && (locations->InAt(0).IsRegister()
397 || locations->InAt(0).IsRegisterPair()
398 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100399 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100400 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
401 || (location.GetPolicy() == Location::kSameAsFirstInput
402 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
403 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100404 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000405 } else if (location.IsRegister() || location.IsRegisterPair()) {
406 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100407 }
408 }
409
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100410 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100411 size_t end = GetEnd();
412 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100413 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100414 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100415 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100416 if (location.IsUnallocated()
417 && (location.GetPolicy() == Location::kRequiresRegister
418 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100419 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100420 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100421 }
422 use = use->GetNext();
423 }
424 return kNoLifetime;
425 }
426
427 size_t FirstRegisterUse() const {
428 return FirstRegisterUseAfter(GetStart());
429 }
430
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000431 size_t FirstUseAfter(size_t position) const {
432 if (is_temp_) {
433 return position == GetStart() ? position : kNoLifetime;
434 }
435
436 UsePosition* use = first_use_;
437 size_t end = GetEnd();
438 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000439 if (!use->GetIsEnvironment()) {
440 size_t use_position = use->GetPosition();
441 if (use_position > position) {
442 return use_position;
443 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000444 }
445 use = use->GetNext();
446 }
447 return kNoLifetime;
448 }
449
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100450 UsePosition* GetFirstUse() const {
451 return first_use_;
452 }
453
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100454 Primitive::Type GetType() const {
455 return type_;
456 }
457
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100458 HInstruction* GetDefinedBy() const {
459 return defined_by_;
460 }
461
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100462 /**
463 * Split this interval at `position`. This interval is changed to:
464 * [start ... position).
465 *
466 * The new interval covers:
467 * [position ... end)
468 */
469 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100470 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100471 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100472 DCHECK_GT(position, GetStart());
473
474 if (last_range_->GetEnd() <= position) {
475 // This range dies before `position`, no need to split.
476 return nullptr;
477 }
478
479 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100480 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100481 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100482 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100483
484 new_interval->first_use_ = first_use_;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000485 last_visited_range_ = nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100486 LiveRange* current = first_range_;
487 LiveRange* previous = nullptr;
488 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000489 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100490 do {
491 if (position >= current->GetEnd()) {
492 // Move to next range.
493 previous = current;
494 current = current->next_;
495 } else if (position <= current->GetStart()) {
496 // If the previous range did not cover this position, we know position is in
497 // a lifetime hole. We can just break the first_range_ and last_range_ links
498 // and return the new interval.
499 DCHECK(previous != nullptr);
500 DCHECK(current != first_range_);
501 new_interval->last_range_ = last_range_;
502 last_range_ = previous;
503 previous->next_ = nullptr;
504 new_interval->first_range_ = current;
505 return new_interval;
506 } else {
507 // This range covers position. We create a new last_range_ for this interval
508 // that covers last_range_->Start() and position. We also shorten the current
509 // range and make it the first range of the new interval.
510 DCHECK(position < current->GetEnd() && position > current->GetStart());
511 new_interval->last_range_ = last_range_;
512 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
513 if (previous != nullptr) {
514 previous->next_ = last_range_;
515 } else {
516 first_range_ = last_range_;
517 }
518 new_interval->first_range_ = current;
519 current->start_ = position;
520 return new_interval;
521 }
522 } while (current != nullptr);
523
524 LOG(FATAL) << "Unreachable";
525 return nullptr;
526 }
527
Nicolas Geoffray76905622014-09-25 14:39:26 +0100528 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100529 return GetStart() <= other->GetStart();
530 }
531
532 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100533 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100534 }
535
536 void Dump(std::ostream& stream) const {
537 stream << "ranges: { ";
538 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000539 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100540 current->Dump(stream);
541 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000542 current = current->GetNext();
543 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100544 stream << "}, uses: { ";
545 UsePosition* use = first_use_;
546 if (use != nullptr) {
547 do {
548 use->Dump(stream);
549 stream << " ";
550 } while ((use = use->GetNext()) != nullptr);
551 }
552 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700553 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000554 stream << " is_high: " << IsHighInterval();
555 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100556 }
557
558 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000559 LiveInterval* GetLastSibling() {
560 LiveInterval* result = this;
561 while (result->next_sibling_ != nullptr) {
562 result = result->next_sibling_;
563 }
564 return result;
565 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100566
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100567 // Returns the first register hint that is at least free before
568 // the value contained in `free_until`. If none is found, returns
569 // `kNoRegister`.
570 int FindFirstRegisterHint(size_t* free_until) const;
571
572 // If there is enough at the definition site to find a register (for example
573 // it uses the same input as the first input), returns the register as a hint.
574 // Returns kNoRegister otherwise.
575 int FindHintAtDefinition() const;
576
577 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
578 // slots for spilling.
579 bool NeedsTwoSpillSlots() const;
580
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100581 bool IsFloatingPoint() const {
582 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
583 }
584
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100585 // Converts the location of the interval to a `Location` object.
586 Location ToLocation() const;
587
588 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000589 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100590
591 // Finds the interval that covers `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000592 const LiveInterval& GetIntervalAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100593
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100594 // Returns whether `other` and `this` share the same kind of register.
595 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000596 bool SameRegisterKind(const LiveInterval& other) const {
597 return IsFloatingPoint() == other.IsFloatingPoint();
598 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100599
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000600 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000601 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000602 }
603
604 bool HasLowInterval() const {
605 return IsHighInterval();
606 }
607
608 LiveInterval* GetLowInterval() const {
609 DCHECK(HasLowInterval());
610 return high_or_low_interval_;
611 }
612
613 LiveInterval* GetHighInterval() const {
614 DCHECK(HasHighInterval());
615 return high_or_low_interval_;
616 }
617
618 bool IsHighInterval() const {
619 return GetParent()->is_high_interval_;
620 }
621
622 bool IsLowInterval() const {
623 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
624 }
625
626 void SetLowInterval(LiveInterval* low) {
627 DCHECK(IsHighInterval());
628 high_or_low_interval_ = low;
629 }
630
631 void SetHighInterval(LiveInterval* high) {
632 DCHECK(IsLowInterval());
633 high_or_low_interval_ = high;
634 }
635
636 void AddHighInterval(bool is_temp = false) {
637 DCHECK_EQ(GetParent(), this);
638 DCHECK(!HasHighInterval());
639 DCHECK(!HasLowInterval());
640 high_or_low_interval_ = new (allocator_) LiveInterval(
641 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
642 high_or_low_interval_->high_or_low_interval_ = this;
643 if (first_range_ != nullptr) {
644 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
645 high_or_low_interval_->last_range_ = first_range_->GetLastRange();
646 }
647 if (first_use_ != nullptr) {
648 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
649 }
650 }
651
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000652 // Returns whether an interval, when it is non-split, is using
653 // the same register of one of its input.
654 bool IsUsingInputRegister() const {
655 if (defined_by_ != nullptr && !IsSplit()) {
656 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
657 LiveInterval* interval = it.Current()->GetLiveInterval();
658
659 // Find the interval that covers `defined_by`_.
660 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
661 interval = interval->GetNextSibling();
662 }
663
664 // Check if both intervals have the same register of the same kind.
665 if (interval != nullptr
666 && interval->SameRegisterKind(*this)
667 && interval->GetRegister() == GetRegister()) {
668 return true;
669 }
670 }
671 }
672 return false;
673 }
674
675 // Returns whether an interval, when it is non-split, can safely use
676 // the same register of one of its input. Note that this method requires
677 // IsUsingInputRegister() to be true.
678 bool CanUseInputRegister() const {
679 DCHECK(IsUsingInputRegister());
680 if (defined_by_ != nullptr && !IsSplit()) {
681 LocationSummary* locations = defined_by_->GetLocations();
682 if (locations->OutputCanOverlapWithInputs()) {
683 return false;
684 }
685 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
686 LiveInterval* interval = it.Current()->GetLiveInterval();
687
688 // Find the interval that covers `defined_by`_.
689 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
690 interval = interval->GetNextSibling();
691 }
692
693 if (interval != nullptr
694 && interval->SameRegisterKind(*this)
695 && interval->GetRegister() == GetRegister()) {
696 // We found the input that has the same register. Check if it is live after
697 // `defined_by`_.
698 return !interval->Covers(defined_by_->GetLifetimePosition() + 1);
699 }
700 }
701 }
702 LOG(FATAL) << "Unreachable";
703 UNREACHABLE();
704 }
705
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100706 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700707 LiveInterval(ArenaAllocator* allocator,
708 Primitive::Type type,
709 HInstruction* defined_by = nullptr,
710 bool is_fixed = false,
711 int reg = kNoRegister,
712 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000713 bool is_slow_path_safepoint = false,
714 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700715 : allocator_(allocator),
716 first_range_(nullptr),
717 last_range_(nullptr),
David Brazdil5b8e6a52015-02-25 16:17:05 +0000718 last_visited_range_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700719 first_use_(nullptr),
720 type_(type),
721 next_sibling_(nullptr),
722 parent_(this),
723 register_(reg),
724 spill_slot_(kNoSpillSlot),
725 is_fixed_(is_fixed),
726 is_temp_(is_temp),
727 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000728 is_high_interval_(is_high_interval),
729 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700730 defined_by_(defined_by) {}
731
David Brazdil5b8e6a52015-02-25 16:17:05 +0000732 // Returns a LiveRange covering the given position or nullptr if no such range
733 // exists in the interval.
734 // This is a linear search optimized for multiple queries in a non-decreasing
735 // position order typical for linear scan register allocation.
736 LiveRange* FindRangeAt(size_t position) {
737 // Make sure operations on the interval didn't leave us with a cached result
738 // from a sibling.
739 if (kIsDebugBuild) {
740 if (last_visited_range_ != nullptr) {
741 DCHECK_GE(last_visited_range_->GetStart(), GetStart());
742 DCHECK_LE(last_visited_range_->GetEnd(), GetEnd());
743 }
744 }
745
746 // If this method was called earlier on a lower position, use that result as
747 // a starting point to save time. However, linear scan performs 3 scans:
748 // integers, floats, and resolution. Instead of resetting at the beginning
749 // of a scan, we do it here.
750 LiveRange* current;
751 if (last_visited_range_ != nullptr && position >= last_visited_range_->GetStart()) {
752 current = last_visited_range_;
753 } else {
754 current = first_range_;
755 }
756 while (current != nullptr && current->GetEnd() <= position) {
757 current = current->GetNext();
758 }
759 last_visited_range_ = current;
760 if (current != nullptr && position >= current->GetStart()) {
761 return current;
762 } else {
763 return nullptr;
764 }
765 }
766
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100767 ArenaAllocator* const allocator_;
768
769 // Ranges of this interval. We need a quick access to the last range to test
770 // for liveness (see `IsDeadAt`).
771 LiveRange* first_range_;
772 LiveRange* last_range_;
773
David Brazdil5b8e6a52015-02-25 16:17:05 +0000774 // Last visited range. This is a range search optimization leveraging the fact
775 // that the register allocator does a linear scan through the intervals.
776 LiveRange* last_visited_range_;
777
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100778 // Uses of this interval. Note that this linked list is shared amongst siblings.
779 UsePosition* first_use_;
780
781 // The instruction type this interval corresponds to.
782 const Primitive::Type type_;
783
784 // Live interval that is the result of a split.
785 LiveInterval* next_sibling_;
786
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100787 // The first interval from which split intervals come from.
788 LiveInterval* parent_;
789
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100790 // The register allocated to this interval.
791 int register_;
792
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100793 // The spill slot allocated to this interval.
794 int spill_slot_;
795
796 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100797 const bool is_fixed_;
798
799 // Whether the interval is for a temporary.
800 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100801
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100802 // Whether the interval is for a safepoint that calls on slow path.
803 const bool is_slow_path_safepoint_;
804
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000805 // Whether this interval is a synthesized interval for register pair.
806 const bool is_high_interval_;
807
808 // If this interval needs a register pair, the high or low equivalent.
809 // `is_high_interval_` tells whether this holds the low or the high.
810 LiveInterval* high_or_low_interval_;
811
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100812 // The instruction represented by this interval.
813 HInstruction* const defined_by_;
814
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100815 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100816 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100817
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000818 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
819
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100820 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
821};
822
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000823/**
824 * Analysis that computes the liveness of instructions:
825 *
826 * (a) Non-environment uses of an instruction always make
827 * the instruction live.
828 * (b) Environment uses of an instruction whose type is
829 * object (that is, non-primitive), make the instruction live.
830 * This is due to having to keep alive objects that have
831 * finalizers deleting native objects.
832 * (c) When the graph has the debuggable property, environment uses
833 * of an instruction that has a primitive type make the instruction live.
834 * If the graph does not have the debuggable property, the environment
835 * use has no effect, and may get a 'none' value after register allocation.
836 *
837 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
838 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100839class SsaLivenessAnalysis : public ValueObject {
840 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100841 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100842 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100843 codegen_(codegen),
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000844 linear_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100845 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100846 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100847 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100848 number_of_ssa_values_(0) {
849 block_infos_.SetSize(graph.GetBlocks().Size());
850 }
851
852 void Analyze();
853
854 BitVector* GetLiveInSet(const HBasicBlock& block) const {
855 return &block_infos_.Get(block.GetBlockId())->live_in_;
856 }
857
858 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
859 return &block_infos_.Get(block.GetBlockId())->live_out_;
860 }
861
862 BitVector* GetKillSet(const HBasicBlock& block) const {
863 return &block_infos_.Get(block.GetBlockId())->kill_;
864 }
865
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000866 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
867 return linear_order_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100868 }
869
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100870 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100871 return instructions_from_ssa_index_.Get(index);
872 }
873
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100874 HInstruction* GetInstructionFromPosition(size_t index) const {
875 return instructions_from_lifetime_position_.Get(index);
876 }
877
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100878 HInstruction* GetTempUser(LiveInterval* temp) const {
879 // A temporary shares the same lifetime start as the instruction that requires it.
880 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000881 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
882 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
883 return user;
884 }
885
886 size_t GetTempIndex(LiveInterval* temp) const {
887 // We use the input index to store the index of the temporary in the user's temporary list.
888 DCHECK(temp->IsTemp());
889 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100890 }
891
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100892 size_t GetMaxLifetimePosition() const {
893 return instructions_from_lifetime_position_.Size() * 2 - 1;
894 }
895
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100896 size_t GetNumberOfSsaValues() const {
897 return number_of_ssa_values_;
898 }
899
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800900 static constexpr const char* kLivenessPassName = "liveness";
901
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100902 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100903 // Linearize the graph so that:
904 // (1): a block is always after its dominator,
905 // (2): blocks of loops are contiguous.
906 // This creates a natural and efficient ordering when visualizing live ranges.
907 void LinearizeGraph();
908
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100909 // Give an SSA number to each instruction that defines a value used by another instruction,
910 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100911 void NumberInstructions();
912
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100913 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
914 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100915
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100916 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
917 // kill sets, that do not take into account backward branches.
918 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100919
920 // After computing the initial sets, this method does a fixed point
921 // calculation over the live_in and live_out set to take into account
922 // backwards branches.
923 void ComputeLiveInAndLiveOutSets();
924
925 // Update the live_in set of the block and returns whether it has changed.
926 bool UpdateLiveIn(const HBasicBlock& block);
927
928 // Update the live_out set of the block and returns whether it has changed.
929 bool UpdateLiveOut(const HBasicBlock& block);
930
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000931 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
932 if (instruction == nullptr) return false;
933 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
934 return instruction->GetType() == Primitive::kPrimNot;
935 }
936
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100937 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100938 CodeGenerator* const codegen_;
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000939 GrowableArray<HBasicBlock*> linear_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100940 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100941
942 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100943 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100944
945 // Temporary array used when inserting moves in the graph.
946 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100947 size_t number_of_ssa_values_;
948
949 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
950};
951
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000952class HLinearPostOrderIterator : public ValueObject {
953 public:
954 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000955 : order_(liveness.GetLinearOrder()), index_(liveness.GetLinearOrder().Size()) {}
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000956
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000957 bool Done() const { return index_ == 0; }
958
959 HBasicBlock* Current() const { return order_.Get(index_ -1); }
960
961 void Advance() {
962 --index_;
963 DCHECK_GE(index_, 0U);
964 }
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000965
966 private:
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000967 const GrowableArray<HBasicBlock*>& order_;
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000968 size_t index_;
969
970 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
971};
972
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000973class HLinearOrderIterator : public ValueObject {
974 public:
975 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
976 : order_(liveness.GetLinearOrder()), index_(0) {}
977
978 bool Done() const { return index_ == order_.Size(); }
979 HBasicBlock* Current() const { return order_.Get(index_); }
980 void Advance() { ++index_; }
981
982 private:
983 const GrowableArray<HBasicBlock*>& order_;
984 size_t index_;
985
986 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
987};
988
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100989} // namespace art
990
991#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_