blob: b6e4028c3fd1095170478bbd5a1729aa50a101ff [file] [log] [blame]
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_
18#define ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_
19
20#include "nodes.h"
Nicolas Geoffray829280c2015-01-28 10:20:37 +000021#include <iostream>
Nicolas Geoffray804d0932014-05-02 08:46:00 +010022
23namespace art {
24
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010025class CodeGenerator;
26
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010027static constexpr int kNoRegister = -1;
28
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070029class BlockInfo : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010030 public:
31 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
32 : block_(block),
33 live_in_(allocator, number_of_ssa_values, false),
34 live_out_(allocator, number_of_ssa_values, false),
35 kill_(allocator, number_of_ssa_values, false) {
Ian Rogerscf7f1912014-10-22 22:06:39 -070036 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010037 live_in_.ClearAllBits();
38 live_out_.ClearAllBits();
39 kill_.ClearAllBits();
40 }
41
42 private:
43 const HBasicBlock& block_;
44 ArenaBitVector live_in_;
45 ArenaBitVector live_out_;
46 ArenaBitVector kill_;
47
48 friend class SsaLivenessAnalysis;
49
50 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
51};
52
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010053/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010055 * is live.
56 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070057class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010058 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010059 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010060 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010061 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010062 }
63
64 size_t GetStart() const { return start_; }
65 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010066 LiveRange* GetNext() const { return next_; }
67
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070068 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010069 return (start_ >= other.start_ && start_ < other.end_)
70 || (other.start_ >= start_ && other.start_ < end_);
71 }
72
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070073 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010074 return end_ <= other.start_;
75 }
76
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070077 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010078 stream << "[" << start_ << ", " << end_ << ")";
79 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010080
Nicolas Geoffray840e5462015-01-07 16:01:24 +000081 LiveRange* Dup(ArenaAllocator* allocator) const {
82 return new (allocator) LiveRange(
83 start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator));
84 }
85
86 LiveRange* GetLastRange() {
87 return next_ == nullptr ? this : next_->GetLastRange();
88 }
89
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010090 private:
91 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010092 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010093 LiveRange* next_;
94
95 friend class LiveInterval;
96
97 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010098};
99
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100100/**
101 * A use position represents a live interval use at a given position.
102 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700103class UsePosition : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100104 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100105 UsePosition(HInstruction* user,
106 size_t input_index,
107 bool is_environment,
108 size_t position,
109 UsePosition* next)
110 : user_(user),
111 input_index_(input_index),
112 is_environment_(is_environment),
113 position_(position),
114 next_(next) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100115 DCHECK(user->IsPhi()
116 || (GetPosition() == user->GetLifetimePosition() + 1)
117 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100118 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
119 }
120
121 size_t GetPosition() const { return position_; }
122
123 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100124 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100125
126 HInstruction* GetUser() const { return user_; }
127
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100128 bool GetIsEnvironment() const { return is_environment_; }
129
130 size_t GetInputIndex() const { return input_index_; }
131
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100132 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100133 stream << position_;
134 }
135
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000136 UsePosition* Dup(ArenaAllocator* allocator) const {
137 return new (allocator) UsePosition(
138 user_, input_index_, is_environment_, position_,
139 next_ == nullptr ? nullptr : next_->Dup(allocator));
140 }
141
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100142 private:
143 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100144 const size_t input_index_;
145 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100146 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100147 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100148
149 DISALLOW_COPY_AND_ASSIGN(UsePosition);
150};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100151
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100152class SafepointPosition : public ArenaObject<kArenaAllocMisc> {
153 public:
154 explicit SafepointPosition(HInstruction* instruction)
155 : instruction_(instruction),
156 next_(nullptr) {}
157
158 void SetNext(SafepointPosition* next) {
159 next_ = next;
160 }
161
162 size_t GetPosition() const {
163 return instruction_->GetLifetimePosition();
164 }
165
166 SafepointPosition* GetNext() const {
167 return next_;
168 }
169
170 LocationSummary* GetLocations() const {
171 return instruction_->GetLocations();
172 }
173
174 HInstruction* GetInstruction() const {
175 return instruction_;
176 }
177
178 private:
179 HInstruction* const instruction_;
180 SafepointPosition* next_;
181
182 DISALLOW_COPY_AND_ASSIGN(SafepointPosition);
183};
184
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100185/**
186 * An interval is a list of disjoint live ranges where an instruction is live.
187 * Each instruction that has uses gets an interval.
188 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700189class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100190 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700191 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
192 Primitive::Type type,
193 HInstruction* instruction = nullptr) {
194 return new (allocator) LiveInterval(allocator, type, instruction);
195 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100196
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100197 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
198 return new (allocator) LiveInterval(
199 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
200 }
201
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100202 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100203 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
204 }
205
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100206 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
207 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100208 }
209
210 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700211 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100212 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700213 // This interval is the result of a split.
214 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100215
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000216 void AddTempUse(HInstruction* instruction, size_t temp_index) {
217 DCHECK(IsTemp());
218 DCHECK(first_use_ == nullptr) << "A temporary can only have one user";
219 size_t position = instruction->GetLifetimePosition();
220 first_use_ = new (allocator_) UsePosition(
221 instruction, temp_index, /* is_environment */ false, position, first_use_);
222 AddRange(position, position + 1);
223 }
224
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000225 void AddUse(HInstruction* instruction,
226 size_t input_index,
227 bool is_environment,
228 bool keep_alive = false) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100229 // Set the use within the instruction.
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000230 size_t position = instruction->GetLifetimePosition() + 1;
231 LocationSummary* locations = instruction->GetLocations();
232 if (!is_environment) {
233 if (locations->IsFixedInput(input_index) || locations->OutputUsesSameAs(input_index)) {
234 // For fixed inputs and output same as input, the register allocator
235 // requires to have inputs die at the instruction, so that input moves use the
236 // location of the input just before that instruction (and not potential moves due
237 // to splitting).
238 position = instruction->GetLifetimePosition();
239 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100240 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000241
242 DCHECK(position == instruction->GetLifetimePosition()
243 || position == instruction->GetLifetimePosition() + 1);
244
Nicolas Geoffray76905622014-09-25 14:39:26 +0100245 if ((first_use_ != nullptr)
246 && (first_use_->GetUser() == instruction)
247 && (first_use_->GetPosition() < position)) {
248 // The user uses the instruction multiple times, and one use dies before the other.
249 // We update the use list so that the latter is first.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000250 DCHECK(!is_environment);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100251 UsePosition* cursor = first_use_;
252 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
253 cursor = cursor->GetNext();
254 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100255 DCHECK(first_use_->GetPosition() + 1 == position);
256 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100257 instruction, input_index, is_environment, position, cursor->GetNext());
258 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100259 if (first_range_->GetEnd() == first_use_->GetPosition()) {
260 first_range_->end_ = position;
261 }
262 return;
263 }
264
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000265 first_use_ = new (allocator_) UsePosition(
266 instruction, input_index, is_environment, position, first_use_);
267
268 if (is_environment && !keep_alive) {
269 // If this environment use does not keep the instruction live, it does not
270 // affect the live range of that instruction.
271 return;
272 }
273
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100274 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100275 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100276 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100277 first_range_ = last_range_ = new (allocator_) LiveRange(
278 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100279 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100280 // There is a use later in the same block or in a following block.
281 // Note that in such a case, `AddRange` for the whole blocks has been called
282 // before arriving in this method, and this is the reason the start of
283 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100284 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100285 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100286 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100287 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100288 // Note that the start of `first_range_` can be equal to `end`: two blocks
289 // having adjacent lifetime positions are not necessarily
290 // predecessor/successor. When two blocks are predecessor/successor, the
291 // liveness algorithm has called `AddRange` before arriving in this method,
292 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100293 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100294 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100295 }
296
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100297 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100298 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100299 first_use_ = new (allocator_) UsePosition(
300 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100301 }
302
303 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100304 if (first_range_ == nullptr) {
305 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
306 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100307 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100308 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100309 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
310 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100311 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100312 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100313 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100314 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100315 }
316 }
317
318 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100319 DCHECK(first_range_ != nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000320 DCHECK_LE(start, first_range_->GetStart());
321 // Find the range that covers the positions after the loop.
322 LiveRange* after_loop = first_range_;
323 LiveRange* last_in_loop = nullptr;
324 while (after_loop != nullptr && after_loop->GetEnd() < end) {
325 DCHECK_LE(start, after_loop->GetStart());
326 last_in_loop = after_loop;
327 after_loop = after_loop->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100328 }
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000329 if (after_loop == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100330 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100331 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000332 } else if (after_loop->GetStart() <= end) {
333 first_range_ = after_loop;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100334 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100335 first_range_->start_ = start;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000336 } else {
337 // The use after the loop is after a lifetime hole.
338 DCHECK(last_in_loop != nullptr);
339 first_range_ = last_in_loop;
340 first_range_->start_ = start;
341 first_range_->end_ = end;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100342 }
343 }
344
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100345 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100346 void SetSpillSlot(int slot) {
347 DCHECK(!is_fixed_);
348 DCHECK(!is_temp_);
349 spill_slot_ = slot;
350 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100351 int GetSpillSlot() const { return spill_slot_; }
352
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100353 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100354 if (first_range_ != nullptr) {
355 first_range_->start_ = from;
356 } else {
357 // Instruction without uses.
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000358 DCHECK(!defined_by_->HasNonEnvironmentUses());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100359 DCHECK(from == defined_by_->GetLifetimePosition());
360 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
361 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100362 }
363
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100364 LiveInterval* GetParent() const { return parent_; }
365
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100366 LiveRange* GetFirstRange() const { return first_range_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000367 LiveRange* GetLastRange() const { return last_range_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100368
369 int GetRegister() const { return register_; }
370 void SetRegister(int reg) { register_ = reg; }
371 void ClearRegister() { register_ = kNoRegister; }
372 bool HasRegister() const { return register_ != kNoRegister; }
373
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100374 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100375 return last_range_->GetEnd() <= position;
376 }
377
David Brazdil5b8e6a52015-02-25 16:17:05 +0000378 bool Covers(size_t position) {
379 return !IsDeadAt(position) && FindRangeAt(position) != nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100380 }
381
382 /**
383 * Returns the first intersection of this interval with `other`.
384 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100385 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100386 // Advance both intervals and find the first matching range start in
387 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100388 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100389 LiveRange* other_range = other->first_range_;
390 do {
David Brazdil714e14f2015-02-25 11:57:05 +0000391 if (my_range->IsBefore(*other_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100392 my_range = my_range->GetNext();
393 if (my_range == nullptr) {
394 return kNoLifetime;
395 }
David Brazdil714e14f2015-02-25 11:57:05 +0000396 } else if (other_range->IsBefore(*my_range)) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100397 other_range = other_range->GetNext();
398 if (other_range == nullptr) {
399 return kNoLifetime;
400 }
David Brazdil714e14f2015-02-25 11:57:05 +0000401 } else {
402 DCHECK(my_range->IntersectsWith(*other_range));
403 return std::max(my_range->GetStart(), other_range->GetStart());
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100404 }
405 } while (true);
406 }
407
408 size_t GetStart() const {
409 return first_range_->GetStart();
410 }
411
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100412 size_t GetEnd() const {
413 return last_range_->GetEnd();
414 }
415
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100416 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100417 if (is_temp_) {
418 return position == GetStart() ? position : kNoLifetime;
419 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100420 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100421 LocationSummary* locations = defined_by_->GetLocations();
422 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100423 // This interval is the first interval of the instruction. If the output
424 // of the instruction requires a register, we return the position of that instruction
425 // as the first register use.
426 if (location.IsUnallocated()) {
427 if ((location.GetPolicy() == Location::kRequiresRegister)
428 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000429 && (locations->InAt(0).IsRegister()
430 || locations->InAt(0).IsRegisterPair()
431 || locations->InAt(0).GetPolicy() == Location::kRequiresRegister))) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100432 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100433 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
434 || (location.GetPolicy() == Location::kSameAsFirstInput
435 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
436 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100437 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000438 } else if (location.IsRegister() || location.IsRegisterPair()) {
439 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100440 }
441 }
442
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100443 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100444 size_t end = GetEnd();
445 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100446 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100447 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100448 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100449 if (location.IsUnallocated()
450 && (location.GetPolicy() == Location::kRequiresRegister
451 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100452 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100453 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100454 }
455 use = use->GetNext();
456 }
457 return kNoLifetime;
458 }
459
460 size_t FirstRegisterUse() const {
461 return FirstRegisterUseAfter(GetStart());
462 }
463
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000464 size_t FirstUseAfter(size_t position) const {
465 if (is_temp_) {
466 return position == GetStart() ? position : kNoLifetime;
467 }
468
469 UsePosition* use = first_use_;
470 size_t end = GetEnd();
471 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000472 if (!use->GetIsEnvironment()) {
473 size_t use_position = use->GetPosition();
474 if (use_position > position) {
475 return use_position;
476 }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000477 }
478 use = use->GetNext();
479 }
480 return kNoLifetime;
481 }
482
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100483 UsePosition* GetFirstUse() const {
484 return first_use_;
485 }
486
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100487 Primitive::Type GetType() const {
488 return type_;
489 }
490
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100491 HInstruction* GetDefinedBy() const {
492 return defined_by_;
493 }
494
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100495 /**
496 * Split this interval at `position`. This interval is changed to:
497 * [start ... position).
498 *
499 * The new interval covers:
500 * [position ... end)
501 */
502 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100503 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100504 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100505 DCHECK_GT(position, GetStart());
506
507 if (last_range_->GetEnd() <= position) {
508 // This range dies before `position`, no need to split.
509 return nullptr;
510 }
511
512 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100513 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100514 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100515 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100516
517 new_interval->first_use_ = first_use_;
David Brazdil5b8e6a52015-02-25 16:17:05 +0000518 last_visited_range_ = nullptr;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100519 LiveRange* current = first_range_;
520 LiveRange* previous = nullptr;
521 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000522 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100523 do {
524 if (position >= current->GetEnd()) {
525 // Move to next range.
526 previous = current;
527 current = current->next_;
528 } else if (position <= current->GetStart()) {
529 // If the previous range did not cover this position, we know position is in
530 // a lifetime hole. We can just break the first_range_ and last_range_ links
531 // and return the new interval.
532 DCHECK(previous != nullptr);
533 DCHECK(current != first_range_);
534 new_interval->last_range_ = last_range_;
535 last_range_ = previous;
536 previous->next_ = nullptr;
537 new_interval->first_range_ = current;
538 return new_interval;
539 } else {
540 // This range covers position. We create a new last_range_ for this interval
541 // that covers last_range_->Start() and position. We also shorten the current
542 // range and make it the first range of the new interval.
543 DCHECK(position < current->GetEnd() && position > current->GetStart());
544 new_interval->last_range_ = last_range_;
545 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
546 if (previous != nullptr) {
547 previous->next_ = last_range_;
548 } else {
549 first_range_ = last_range_;
550 }
551 new_interval->first_range_ = current;
552 current->start_ = position;
553 return new_interval;
554 }
555 } while (current != nullptr);
556
557 LOG(FATAL) << "Unreachable";
558 return nullptr;
559 }
560
Nicolas Geoffray76905622014-09-25 14:39:26 +0100561 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100562 return GetStart() <= other->GetStart();
563 }
564
565 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100566 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100567 }
568
569 void Dump(std::ostream& stream) const {
570 stream << "ranges: { ";
571 LiveRange* current = first_range_;
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000572 while (current != nullptr) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100573 current->Dump(stream);
574 stream << " ";
Nicolas Geoffrayaedc3282015-01-23 18:01:51 +0000575 current = current->GetNext();
576 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100577 stream << "}, uses: { ";
578 UsePosition* use = first_use_;
579 if (use != nullptr) {
580 do {
581 use->Dump(stream);
582 stream << " ";
583 } while ((use = use->GetNext()) != nullptr);
584 }
585 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700586 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000587 stream << " is_high: " << IsHighInterval();
588 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100589 }
590
591 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000592 LiveInterval* GetLastSibling() {
593 LiveInterval* result = this;
594 while (result->next_sibling_ != nullptr) {
595 result = result->next_sibling_;
596 }
597 return result;
598 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100599
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100600 // Returns the first register hint that is at least free before
601 // the value contained in `free_until`. If none is found, returns
602 // `kNoRegister`.
603 int FindFirstRegisterHint(size_t* free_until) const;
604
605 // If there is enough at the definition site to find a register (for example
606 // it uses the same input as the first input), returns the register as a hint.
607 // Returns kNoRegister otherwise.
608 int FindHintAtDefinition() const;
609
610 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
611 // slots for spilling.
612 bool NeedsTwoSpillSlots() const;
613
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100614 bool IsFloatingPoint() const {
615 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
616 }
617
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100618 // Converts the location of the interval to a `Location` object.
619 Location ToLocation() const;
620
621 // Returns the location of the interval following its siblings at `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000622 Location GetLocationAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100623
624 // Finds the interval that covers `position`.
David Brazdil5b8e6a52015-02-25 16:17:05 +0000625 const LiveInterval& GetIntervalAt(size_t position);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100626
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100627 // Returns whether `other` and `this` share the same kind of register.
628 bool SameRegisterKind(Location other) const;
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000629 bool SameRegisterKind(const LiveInterval& other) const {
630 return IsFloatingPoint() == other.IsFloatingPoint();
631 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100632
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000633 bool HasHighInterval() const {
Nicolas Geoffray3747b482015-01-19 17:17:16 +0000634 return IsLowInterval();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000635 }
636
637 bool HasLowInterval() const {
638 return IsHighInterval();
639 }
640
641 LiveInterval* GetLowInterval() const {
642 DCHECK(HasLowInterval());
643 return high_or_low_interval_;
644 }
645
646 LiveInterval* GetHighInterval() const {
647 DCHECK(HasHighInterval());
648 return high_or_low_interval_;
649 }
650
651 bool IsHighInterval() const {
652 return GetParent()->is_high_interval_;
653 }
654
655 bool IsLowInterval() const {
656 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
657 }
658
659 void SetLowInterval(LiveInterval* low) {
660 DCHECK(IsHighInterval());
661 high_or_low_interval_ = low;
662 }
663
664 void SetHighInterval(LiveInterval* high) {
665 DCHECK(IsLowInterval());
666 high_or_low_interval_ = high;
667 }
668
669 void AddHighInterval(bool is_temp = false) {
670 DCHECK_EQ(GetParent(), this);
671 DCHECK(!HasHighInterval());
672 DCHECK(!HasLowInterval());
673 high_or_low_interval_ = new (allocator_) LiveInterval(
674 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
675 high_or_low_interval_->high_or_low_interval_ = this;
676 if (first_range_ != nullptr) {
677 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
678 high_or_low_interval_->last_range_ = first_range_->GetLastRange();
679 }
680 if (first_use_ != nullptr) {
681 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
682 }
683 }
684
Nicolas Geoffray829280c2015-01-28 10:20:37 +0000685 // Returns whether an interval, when it is non-split, is using
686 // the same register of one of its input.
687 bool IsUsingInputRegister() const {
688 if (defined_by_ != nullptr && !IsSplit()) {
689 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
690 LiveInterval* interval = it.Current()->GetLiveInterval();
691
692 // Find the interval that covers `defined_by`_.
693 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
694 interval = interval->GetNextSibling();
695 }
696
697 // Check if both intervals have the same register of the same kind.
698 if (interval != nullptr
699 && interval->SameRegisterKind(*this)
700 && interval->GetRegister() == GetRegister()) {
701 return true;
702 }
703 }
704 }
705 return false;
706 }
707
708 // Returns whether an interval, when it is non-split, can safely use
709 // the same register of one of its input. Note that this method requires
710 // IsUsingInputRegister() to be true.
711 bool CanUseInputRegister() const {
712 DCHECK(IsUsingInputRegister());
713 if (defined_by_ != nullptr && !IsSplit()) {
714 LocationSummary* locations = defined_by_->GetLocations();
715 if (locations->OutputCanOverlapWithInputs()) {
716 return false;
717 }
718 for (HInputIterator it(defined_by_); !it.Done(); it.Advance()) {
719 LiveInterval* interval = it.Current()->GetLiveInterval();
720
721 // Find the interval that covers `defined_by`_.
722 while (interval != nullptr && !interval->Covers(defined_by_->GetLifetimePosition())) {
723 interval = interval->GetNextSibling();
724 }
725
726 if (interval != nullptr
727 && interval->SameRegisterKind(*this)
728 && interval->GetRegister() == GetRegister()) {
729 // We found the input that has the same register. Check if it is live after
730 // `defined_by`_.
731 return !interval->Covers(defined_by_->GetLifetimePosition() + 1);
732 }
733 }
734 }
735 LOG(FATAL) << "Unreachable";
736 UNREACHABLE();
737 }
738
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100739 void AddSafepoint(HInstruction* instruction) {
740 SafepointPosition* safepoint = new (allocator_) SafepointPosition(instruction);
741 if (first_safepoint_ == nullptr) {
742 first_safepoint_ = last_safepoint_ = safepoint;
743 } else {
744 DCHECK_LT(last_safepoint_->GetPosition(), safepoint->GetPosition());
745 last_safepoint_->SetNext(safepoint);
746 last_safepoint_ = safepoint;
747 }
748 }
749
750 SafepointPosition* GetFirstSafepoint() const {
751 DCHECK_EQ(GetParent(), this) << "Only the first sibling lists safepoints";
752 return first_safepoint_;
753 }
754
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100755 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700756 LiveInterval(ArenaAllocator* allocator,
757 Primitive::Type type,
758 HInstruction* defined_by = nullptr,
759 bool is_fixed = false,
760 int reg = kNoRegister,
761 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000762 bool is_slow_path_safepoint = false,
763 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700764 : allocator_(allocator),
765 first_range_(nullptr),
766 last_range_(nullptr),
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100767 first_safepoint_(nullptr),
768 last_safepoint_(nullptr),
David Brazdil5b8e6a52015-02-25 16:17:05 +0000769 last_visited_range_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700770 first_use_(nullptr),
771 type_(type),
772 next_sibling_(nullptr),
773 parent_(this),
774 register_(reg),
775 spill_slot_(kNoSpillSlot),
776 is_fixed_(is_fixed),
777 is_temp_(is_temp),
778 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000779 is_high_interval_(is_high_interval),
780 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700781 defined_by_(defined_by) {}
782
David Brazdil5b8e6a52015-02-25 16:17:05 +0000783 // Returns a LiveRange covering the given position or nullptr if no such range
784 // exists in the interval.
785 // This is a linear search optimized for multiple queries in a non-decreasing
786 // position order typical for linear scan register allocation.
787 LiveRange* FindRangeAt(size_t position) {
788 // Make sure operations on the interval didn't leave us with a cached result
789 // from a sibling.
790 if (kIsDebugBuild) {
791 if (last_visited_range_ != nullptr) {
792 DCHECK_GE(last_visited_range_->GetStart(), GetStart());
793 DCHECK_LE(last_visited_range_->GetEnd(), GetEnd());
794 }
795 }
796
797 // If this method was called earlier on a lower position, use that result as
798 // a starting point to save time. However, linear scan performs 3 scans:
799 // integers, floats, and resolution. Instead of resetting at the beginning
800 // of a scan, we do it here.
801 LiveRange* current;
802 if (last_visited_range_ != nullptr && position >= last_visited_range_->GetStart()) {
803 current = last_visited_range_;
804 } else {
805 current = first_range_;
806 }
807 while (current != nullptr && current->GetEnd() <= position) {
808 current = current->GetNext();
809 }
810 last_visited_range_ = current;
811 if (current != nullptr && position >= current->GetStart()) {
812 return current;
813 } else {
814 return nullptr;
815 }
816 }
817
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100818 ArenaAllocator* const allocator_;
819
820 // Ranges of this interval. We need a quick access to the last range to test
821 // for liveness (see `IsDeadAt`).
822 LiveRange* first_range_;
823 LiveRange* last_range_;
824
Nicolas Geoffray5588e582015-04-14 14:10:59 +0100825 // Safepoints where this interval is live. Only set in the parent interval.
826 SafepointPosition* first_safepoint_;
827 SafepointPosition* last_safepoint_;
828
David Brazdil5b8e6a52015-02-25 16:17:05 +0000829 // Last visited range. This is a range search optimization leveraging the fact
830 // that the register allocator does a linear scan through the intervals.
831 LiveRange* last_visited_range_;
832
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100833 // Uses of this interval. Note that this linked list is shared amongst siblings.
834 UsePosition* first_use_;
835
836 // The instruction type this interval corresponds to.
837 const Primitive::Type type_;
838
839 // Live interval that is the result of a split.
840 LiveInterval* next_sibling_;
841
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100842 // The first interval from which split intervals come from.
843 LiveInterval* parent_;
844
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100845 // The register allocated to this interval.
846 int register_;
847
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100848 // The spill slot allocated to this interval.
849 int spill_slot_;
850
851 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100852 const bool is_fixed_;
853
854 // Whether the interval is for a temporary.
855 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100856
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100857 // Whether the interval is for a safepoint that calls on slow path.
858 const bool is_slow_path_safepoint_;
859
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000860 // Whether this interval is a synthesized interval for register pair.
861 const bool is_high_interval_;
862
863 // If this interval needs a register pair, the high or low equivalent.
864 // `is_high_interval_` tells whether this holds the low or the high.
865 LiveInterval* high_or_low_interval_;
866
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100867 // The instruction represented by this interval.
868 HInstruction* const defined_by_;
869
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100870 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100871 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100872
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000873 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
874
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100875 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
876};
877
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000878/**
879 * Analysis that computes the liveness of instructions:
880 *
881 * (a) Non-environment uses of an instruction always make
882 * the instruction live.
883 * (b) Environment uses of an instruction whose type is
884 * object (that is, non-primitive), make the instruction live.
885 * This is due to having to keep alive objects that have
886 * finalizers deleting native objects.
887 * (c) When the graph has the debuggable property, environment uses
888 * of an instruction that has a primitive type make the instruction live.
889 * If the graph does not have the debuggable property, the environment
890 * use has no effect, and may get a 'none' value after register allocation.
891 *
892 * (b) and (c) are implemented through SsaLivenessAnalysis::ShouldBeLiveForEnvironment.
893 */
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100894class SsaLivenessAnalysis : public ValueObject {
895 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100896 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100897 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100898 codegen_(codegen),
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000899 linear_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100900 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100901 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100902 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100903 number_of_ssa_values_(0) {
904 block_infos_.SetSize(graph.GetBlocks().Size());
905 }
906
907 void Analyze();
908
909 BitVector* GetLiveInSet(const HBasicBlock& block) const {
910 return &block_infos_.Get(block.GetBlockId())->live_in_;
911 }
912
913 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
914 return &block_infos_.Get(block.GetBlockId())->live_out_;
915 }
916
917 BitVector* GetKillSet(const HBasicBlock& block) const {
918 return &block_infos_.Get(block.GetBlockId())->kill_;
919 }
920
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000921 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
922 return linear_order_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100923 }
924
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100925 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100926 return instructions_from_ssa_index_.Get(index);
927 }
928
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100929 HInstruction* GetInstructionFromPosition(size_t index) const {
930 return instructions_from_lifetime_position_.Get(index);
931 }
932
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100933 HInstruction* GetTempUser(LiveInterval* temp) const {
934 // A temporary shares the same lifetime start as the instruction that requires it.
935 DCHECK(temp->IsTemp());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000936 HInstruction* user = GetInstructionFromPosition(temp->GetStart() / 2);
937 DCHECK_EQ(user, temp->GetFirstUse()->GetUser());
938 return user;
939 }
940
941 size_t GetTempIndex(LiveInterval* temp) const {
942 // We use the input index to store the index of the temporary in the user's temporary list.
943 DCHECK(temp->IsTemp());
944 return temp->GetFirstUse()->GetInputIndex();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100945 }
946
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100947 size_t GetMaxLifetimePosition() const {
948 return instructions_from_lifetime_position_.Size() * 2 - 1;
949 }
950
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100951 size_t GetNumberOfSsaValues() const {
952 return number_of_ssa_values_;
953 }
954
Andreas Gampe7c3952f2015-02-19 18:21:24 -0800955 static constexpr const char* kLivenessPassName = "liveness";
956
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100957 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100958 // Linearize the graph so that:
959 // (1): a block is always after its dominator,
960 // (2): blocks of loops are contiguous.
961 // This creates a natural and efficient ordering when visualizing live ranges.
962 void LinearizeGraph();
963
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100964 // Give an SSA number to each instruction that defines a value used by another instruction,
965 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100966 void NumberInstructions();
967
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100968 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
969 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100970
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100971 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
972 // kill sets, that do not take into account backward branches.
973 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100974
975 // After computing the initial sets, this method does a fixed point
976 // calculation over the live_in and live_out set to take into account
977 // backwards branches.
978 void ComputeLiveInAndLiveOutSets();
979
980 // Update the live_in set of the block and returns whether it has changed.
981 bool UpdateLiveIn(const HBasicBlock& block);
982
983 // Update the live_out set of the block and returns whether it has changed.
984 bool UpdateLiveOut(const HBasicBlock& block);
985
Nicolas Geoffray915b9d02015-03-11 15:11:19 +0000986 static bool ShouldBeLiveForEnvironment(HInstruction* instruction) {
987 if (instruction == nullptr) return false;
988 if (instruction->GetBlock()->GetGraph()->IsDebuggable()) return true;
989 return instruction->GetType() == Primitive::kPrimNot;
990 }
991
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100992 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100993 CodeGenerator* const codegen_;
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000994 GrowableArray<HBasicBlock*> linear_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100995 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100996
997 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100998 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100999
1000 // Temporary array used when inserting moves in the graph.
1001 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001002 size_t number_of_ssa_values_;
1003
1004 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
1005};
1006
Nicolas Geoffraye50fa582014-11-24 17:44:15 +00001007class HLinearPostOrderIterator : public ValueObject {
1008 public:
1009 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +00001010 : order_(liveness.GetLinearOrder()), index_(liveness.GetLinearOrder().Size()) {}
Nicolas Geoffraye50fa582014-11-24 17:44:15 +00001011
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +00001012 bool Done() const { return index_ == 0; }
1013
1014 HBasicBlock* Current() const { return order_.Get(index_ -1); }
1015
1016 void Advance() {
1017 --index_;
1018 DCHECK_GE(index_, 0U);
1019 }
Nicolas Geoffraye50fa582014-11-24 17:44:15 +00001020
1021 private:
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +00001022 const GrowableArray<HBasicBlock*>& order_;
Nicolas Geoffraye50fa582014-11-24 17:44:15 +00001023 size_t index_;
1024
1025 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
1026};
1027
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +00001028class HLinearOrderIterator : public ValueObject {
1029 public:
1030 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
1031 : order_(liveness.GetLinearOrder()), index_(0) {}
1032
1033 bool Done() const { return index_ == order_.Size(); }
1034 HBasicBlock* Current() const { return order_.Get(index_); }
1035 void Advance() { ++index_; }
1036
1037 private:
1038 const GrowableArray<HBasicBlock*>& order_;
1039 size_t index_;
1040
1041 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
1042};
1043
Nicolas Geoffray804d0932014-05-02 08:46:00 +01001044} // namespace art
1045
1046#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_