blob: b632c4d05a0a9a76512de6b3b4e2399148ed7222 [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"
21
22namespace art {
23
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010024class CodeGenerator;
25
Nicolas Geoffray01ef3452014-10-01 11:32:17 +010026static constexpr int kNoRegister = -1;
27
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070028class BlockInfo : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffray804d0932014-05-02 08:46:00 +010029 public:
30 BlockInfo(ArenaAllocator* allocator, const HBasicBlock& block, size_t number_of_ssa_values)
31 : block_(block),
32 live_in_(allocator, number_of_ssa_values, false),
33 live_out_(allocator, number_of_ssa_values, false),
34 kill_(allocator, number_of_ssa_values, false) {
Ian Rogerscf7f1912014-10-22 22:06:39 -070035 UNUSED(block_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010036 live_in_.ClearAllBits();
37 live_out_.ClearAllBits();
38 kill_.ClearAllBits();
39 }
40
41 private:
42 const HBasicBlock& block_;
43 ArenaBitVector live_in_;
44 ArenaBitVector live_out_;
45 ArenaBitVector kill_;
46
47 friend class SsaLivenessAnalysis;
48
49 DISALLOW_COPY_AND_ASSIGN(BlockInfo);
50};
51
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010052/**
Nicolas Geoffray39468442014-09-02 15:17:15 +010053 * A live range contains the start and end of a range where an instruction or a temporary
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010054 * is live.
55 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070056class LiveRange FINAL : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010057 public:
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010058 LiveRange(size_t start, size_t end, LiveRange* next) : start_(start), end_(end), next_(next) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010059 DCHECK_LT(start, end);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010060 DCHECK(next_ == nullptr || next_->GetStart() > GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061 }
62
63 size_t GetStart() const { return start_; }
64 size_t GetEnd() const { return end_; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010065 LiveRange* GetNext() const { return next_; }
66
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070067 bool IntersectsWith(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010068 return (start_ >= other.start_ && start_ < other.end_)
69 || (other.start_ >= start_ && other.start_ < end_);
70 }
71
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070072 bool IsBefore(const LiveRange& other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010073 return end_ <= other.start_;
74 }
75
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070076 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010077 stream << "[" << start_ << ", " << end_ << ")";
78 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010079
Nicolas Geoffray840e5462015-01-07 16:01:24 +000080 LiveRange* Dup(ArenaAllocator* allocator) const {
81 return new (allocator) LiveRange(
82 start_, end_, next_ == nullptr ? nullptr : next_->Dup(allocator));
83 }
84
85 LiveRange* GetLastRange() {
86 return next_ == nullptr ? this : next_->GetLastRange();
87 }
88
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010089 private:
90 size_t start_;
Nicolas Geoffray76905622014-09-25 14:39:26 +010091 size_t end_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010092 LiveRange* next_;
93
94 friend class LiveInterval;
95
96 DISALLOW_COPY_AND_ASSIGN(LiveRange);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010097};
98
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010099/**
100 * A use position represents a live interval use at a given position.
101 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700102class UsePosition : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100103 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100104 UsePosition(HInstruction* user,
105 size_t input_index,
106 bool is_environment,
107 size_t position,
108 UsePosition* next)
109 : user_(user),
110 input_index_(input_index),
111 is_environment_(is_environment),
112 position_(position),
113 next_(next) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100114 DCHECK(user->IsPhi()
115 || (GetPosition() == user->GetLifetimePosition() + 1)
116 || (GetPosition() == user->GetLifetimePosition()));
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100117 DCHECK(next_ == nullptr || next->GetPosition() >= GetPosition());
118 }
119
120 size_t GetPosition() const { return position_; }
121
122 UsePosition* GetNext() const { return next_; }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100123 void SetNext(UsePosition* next) { next_ = next; }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100124
125 HInstruction* GetUser() const { return user_; }
126
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100127 bool GetIsEnvironment() const { return is_environment_; }
128
129 size_t GetInputIndex() const { return input_index_; }
130
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100131 void Dump(std::ostream& stream) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100132 stream << position_;
133 }
134
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000135 UsePosition* Dup(ArenaAllocator* allocator) const {
136 return new (allocator) UsePosition(
137 user_, input_index_, is_environment_, position_,
138 next_ == nullptr ? nullptr : next_->Dup(allocator));
139 }
140
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100141 private:
142 HInstruction* const user_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100143 const size_t input_index_;
144 const bool is_environment_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100145 const size_t position_;
Nicolas Geoffray76905622014-09-25 14:39:26 +0100146 UsePosition* next_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100147
148 DISALLOW_COPY_AND_ASSIGN(UsePosition);
149};
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100150
151/**
152 * An interval is a list of disjoint live ranges where an instruction is live.
153 * Each instruction that has uses gets an interval.
154 */
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700155class LiveInterval : public ArenaObject<kArenaAllocMisc> {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100156 public:
Mingyao Yang296bd602014-10-06 16:47:28 -0700157 static LiveInterval* MakeInterval(ArenaAllocator* allocator,
158 Primitive::Type type,
159 HInstruction* instruction = nullptr) {
160 return new (allocator) LiveInterval(allocator, type, instruction);
161 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100162
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100163 static LiveInterval* MakeSlowPathInterval(ArenaAllocator* allocator, HInstruction* instruction) {
164 return new (allocator) LiveInterval(
165 allocator, Primitive::kPrimVoid, instruction, false, kNoRegister, false, true);
166 }
167
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100168 static LiveInterval* MakeFixedInterval(ArenaAllocator* allocator, int reg, Primitive::Type type) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100169 return new (allocator) LiveInterval(allocator, type, nullptr, true, reg, false);
170 }
171
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100172 static LiveInterval* MakeTempInterval(ArenaAllocator* allocator, Primitive::Type type) {
173 return new (allocator) LiveInterval(allocator, type, nullptr, false, kNoRegister, true);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100174 }
175
176 bool IsFixed() const { return is_fixed_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700177 bool IsTemp() const { return is_temp_; }
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100178 bool IsSlowPathSafepoint() const { return is_slow_path_safepoint_; }
Mingyao Yang296bd602014-10-06 16:47:28 -0700179 // This interval is the result of a split.
180 bool IsSplit() const { return parent_ != this; }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100181
182 void AddUse(HInstruction* instruction, size_t input_index, bool is_environment) {
183 // Set the use within the instruction.
Nicolas Geoffray76905622014-09-25 14:39:26 +0100184 size_t position = instruction->GetLifetimePosition();
185 if (instruction->GetLocations()->InputOverlapsWithOutputOrTemp(input_index, is_environment)) {
186 // If it overlaps, we need to make sure the user will not try to allocate a temp
187 // or its output to the same register.
188 ++position;
189 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100190 if ((first_use_ != nullptr)
191 && (first_use_->GetUser() == instruction)
192 && (first_use_->GetPosition() < position)) {
193 // The user uses the instruction multiple times, and one use dies before the other.
194 // We update the use list so that the latter is first.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100195 UsePosition* cursor = first_use_;
196 while ((cursor->GetNext() != nullptr) && (cursor->GetNext()->GetPosition() < position)) {
197 cursor = cursor->GetNext();
198 }
Nicolas Geoffray76905622014-09-25 14:39:26 +0100199 DCHECK(first_use_->GetPosition() + 1 == position);
200 UsePosition* new_use = new (allocator_) UsePosition(
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +0100201 instruction, input_index, is_environment, position, cursor->GetNext());
202 cursor->SetNext(new_use);
Nicolas Geoffray76905622014-09-25 14:39:26 +0100203 if (first_range_->GetEnd() == first_use_->GetPosition()) {
204 first_range_->end_ = position;
205 }
206 return;
207 }
208
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100209 size_t start_block_position = instruction->GetBlock()->GetLifetimeStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100210 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100211 // First time we see a use of that interval.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100212 first_range_ = last_range_ = new (allocator_) LiveRange(
213 start_block_position, position, nullptr);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100214 } else if (first_range_->GetStart() == start_block_position) {
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100215 // There is a use later in the same block or in a following block.
216 // Note that in such a case, `AddRange` for the whole blocks has been called
217 // before arriving in this method, and this is the reason the start of
218 // `first_range_` is before the given `position`.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100219 DCHECK_LE(position, first_range_->GetEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100220 } else {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100221 DCHECK(first_range_->GetStart() > position);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100222 // There is a hole in the interval. Create a new range.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100223 // Note that the start of `first_range_` can be equal to `end`: two blocks
224 // having adjacent lifetime positions are not necessarily
225 // predecessor/successor. When two blocks are predecessor/successor, the
226 // liveness algorithm has called `AddRange` before arriving in this method,
227 // and the check line 205 would succeed.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100228 first_range_ = new (allocator_) LiveRange(start_block_position, position, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100229 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100230 first_use_ = new (allocator_) UsePosition(
231 instruction, input_index, is_environment, position, first_use_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100232 }
233
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100234 void AddPhiUse(HInstruction* instruction, size_t input_index, HBasicBlock* block) {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100235 DCHECK(instruction->IsPhi());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100236 first_use_ = new (allocator_) UsePosition(
237 instruction, input_index, false, block->GetLifetimeEnd(), first_use_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100238 }
239
240 void AddRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100241 if (first_range_ == nullptr) {
242 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, first_range_);
243 } else if (first_range_->GetStart() == end) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100244 // There is a use in the following block.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100245 first_range_->start_ = start;
Nicolas Geoffray39468442014-09-02 15:17:15 +0100246 } else if (first_range_->GetStart() == start && first_range_->GetEnd() == end) {
247 DCHECK(is_fixed_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100248 } else {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100249 DCHECK_GT(first_range_->GetStart(), end);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100250 // There is a hole in the interval. Create a new range.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100251 first_range_ = new (allocator_) LiveRange(start, end, first_range_);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100252 }
253 }
254
255 void AddLoopRange(size_t start, size_t end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100256 DCHECK(first_range_ != nullptr);
257 while (first_range_ != nullptr && first_range_->GetEnd() < end) {
258 DCHECK_LE(start, first_range_->GetStart());
259 first_range_ = first_range_->GetNext();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100260 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100261 if (first_range_ == nullptr) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100262 // Uses are only in the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100263 first_range_ = last_range_ = new (allocator_) LiveRange(start, end, nullptr);
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100264 } else {
265 // There are uses after the loop.
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100266 first_range_->start_ = start;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100267 }
268 }
269
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100270 bool HasSpillSlot() const { return spill_slot_ != kNoSpillSlot; }
Nicolas Geoffray39468442014-09-02 15:17:15 +0100271 void SetSpillSlot(int slot) {
272 DCHECK(!is_fixed_);
273 DCHECK(!is_temp_);
274 spill_slot_ = slot;
275 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100276 int GetSpillSlot() const { return spill_slot_; }
277
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100278 void SetFrom(size_t from) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100279 if (first_range_ != nullptr) {
280 first_range_->start_ = from;
281 } else {
282 // Instruction without uses.
283 DCHECK(!defined_by_->HasUses());
284 DCHECK(from == defined_by_->GetLifetimePosition());
285 first_range_ = last_range_ = new (allocator_) LiveRange(from, from + 2, nullptr);
286 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100287 }
288
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100289 LiveInterval* GetParent() const { return parent_; }
290
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100291 LiveRange* GetFirstRange() const { return first_range_; }
292
293 int GetRegister() const { return register_; }
294 void SetRegister(int reg) { register_ = reg; }
295 void ClearRegister() { register_ = kNoRegister; }
296 bool HasRegister() const { return register_ != kNoRegister; }
297
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100298 bool IsDeadAt(size_t position) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100299 return last_range_->GetEnd() <= position;
300 }
301
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100302 bool Covers(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100303 if (IsDeadAt(position)) {
304 return false;
305 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100306 LiveRange* current = first_range_;
307 while (current != nullptr) {
308 if (position >= current->GetStart() && position < current->GetEnd()) {
309 return true;
310 }
311 current = current->GetNext();
312 }
313 return false;
314 }
315
316 /**
317 * Returns the first intersection of this interval with `other`.
318 */
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100319 size_t FirstIntersectionWith(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100320 // Advance both intervals and find the first matching range start in
321 // this interval.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100322 LiveRange* my_range = first_range_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100323 LiveRange* other_range = other->first_range_;
324 do {
325 if (my_range->IntersectsWith(*other_range)) {
326 return std::max(my_range->GetStart(), other_range->GetStart());
327 } else if (my_range->IsBefore(*other_range)) {
328 my_range = my_range->GetNext();
329 if (my_range == nullptr) {
330 return kNoLifetime;
331 }
332 } else {
333 DCHECK(other_range->IsBefore(*my_range));
334 other_range = other_range->GetNext();
335 if (other_range == nullptr) {
336 return kNoLifetime;
337 }
338 }
339 } while (true);
340 }
341
342 size_t GetStart() const {
343 return first_range_->GetStart();
344 }
345
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100346 size_t GetEnd() const {
347 return last_range_->GetEnd();
348 }
349
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100350 size_t FirstRegisterUseAfter(size_t position) const {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100351 if (is_temp_) {
352 return position == GetStart() ? position : kNoLifetime;
353 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100354 if (position == GetStart() && defined_by_ != nullptr) {
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100355 LocationSummary* locations = defined_by_->GetLocations();
356 Location location = locations->Out();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100357 // This interval is the first interval of the instruction. If the output
358 // of the instruction requires a register, we return the position of that instruction
359 // as the first register use.
360 if (location.IsUnallocated()) {
361 if ((location.GetPolicy() == Location::kRequiresRegister)
362 || (location.GetPolicy() == Location::kSameAsFirstInput
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100363 && locations->InAt(0).GetPolicy() == Location::kRequiresRegister)) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100364 return position;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100365 } else if ((location.GetPolicy() == Location::kRequiresFpuRegister)
366 || (location.GetPolicy() == Location::kSameAsFirstInput
367 && locations->InAt(0).GetPolicy() == Location::kRequiresFpuRegister)) {
368 return position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100369 }
370 }
371 }
372
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100373 UsePosition* use = first_use_;
Nicolas Geoffrayde025a72014-06-19 17:06:46 +0100374 size_t end = GetEnd();
375 while (use != nullptr && use->GetPosition() <= end) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100376 size_t use_position = use->GetPosition();
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100377 if (use_position > position && !use->GetIsEnvironment()) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100378 Location location = use->GetUser()->GetLocations()->InAt(use->GetInputIndex());
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100379 if (location.IsUnallocated()
380 && (location.GetPolicy() == Location::kRequiresRegister
381 || location.GetPolicy() == Location::kRequiresFpuRegister)) {
Nicolas Geoffrayc8147a72014-10-21 16:06:20 +0100382 return use_position;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100383 }
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100384 }
385 use = use->GetNext();
386 }
387 return kNoLifetime;
388 }
389
390 size_t FirstRegisterUse() const {
391 return FirstRegisterUseAfter(GetStart());
392 }
393
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100394 UsePosition* GetFirstUse() const {
395 return first_use_;
396 }
397
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100398 Primitive::Type GetType() const {
399 return type_;
400 }
401
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100402 HInstruction* GetDefinedBy() const {
403 return defined_by_;
404 }
405
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100406 /**
407 * Split this interval at `position`. This interval is changed to:
408 * [start ... position).
409 *
410 * The new interval covers:
411 * [position ... end)
412 */
413 LiveInterval* SplitAt(size_t position) {
Nicolas Geoffray39468442014-09-02 15:17:15 +0100414 DCHECK(!is_temp_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100415 DCHECK(!is_fixed_);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100416 DCHECK_GT(position, GetStart());
417
418 if (last_range_->GetEnd() <= position) {
419 // This range dies before `position`, no need to split.
420 return nullptr;
421 }
422
423 LiveInterval* new_interval = new (allocator_) LiveInterval(allocator_, type_);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100424 new_interval->next_sibling_ = next_sibling_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100425 next_sibling_ = new_interval;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100426 new_interval->parent_ = parent_;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100427
428 new_interval->first_use_ = first_use_;
429 LiveRange* current = first_range_;
430 LiveRange* previous = nullptr;
431 // Iterate over the ranges, and either find a range that covers this position, or
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000432 // two ranges in between this position (that is, the position is in a lifetime hole).
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100433 do {
434 if (position >= current->GetEnd()) {
435 // Move to next range.
436 previous = current;
437 current = current->next_;
438 } else if (position <= current->GetStart()) {
439 // If the previous range did not cover this position, we know position is in
440 // a lifetime hole. We can just break the first_range_ and last_range_ links
441 // and return the new interval.
442 DCHECK(previous != nullptr);
443 DCHECK(current != first_range_);
444 new_interval->last_range_ = last_range_;
445 last_range_ = previous;
446 previous->next_ = nullptr;
447 new_interval->first_range_ = current;
448 return new_interval;
449 } else {
450 // This range covers position. We create a new last_range_ for this interval
451 // that covers last_range_->Start() and position. We also shorten the current
452 // range and make it the first range of the new interval.
453 DCHECK(position < current->GetEnd() && position > current->GetStart());
454 new_interval->last_range_ = last_range_;
455 last_range_ = new (allocator_) LiveRange(current->start_, position, nullptr);
456 if (previous != nullptr) {
457 previous->next_ = last_range_;
458 } else {
459 first_range_ = last_range_;
460 }
461 new_interval->first_range_ = current;
462 current->start_ = position;
463 return new_interval;
464 }
465 } while (current != nullptr);
466
467 LOG(FATAL) << "Unreachable";
468 return nullptr;
469 }
470
Nicolas Geoffray76905622014-09-25 14:39:26 +0100471 bool StartsBeforeOrAt(LiveInterval* other) const {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100472 return GetStart() <= other->GetStart();
473 }
474
475 bool StartsAfter(LiveInterval* other) const {
Nicolas Geoffray76905622014-09-25 14:39:26 +0100476 return GetStart() > other->GetStart();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100477 }
478
479 void Dump(std::ostream& stream) const {
480 stream << "ranges: { ";
481 LiveRange* current = first_range_;
482 do {
483 current->Dump(stream);
484 stream << " ";
485 } while ((current = current->GetNext()) != nullptr);
486 stream << "}, uses: { ";
487 UsePosition* use = first_use_;
488 if (use != nullptr) {
489 do {
490 use->Dump(stream);
491 stream << " ";
492 } while ((use = use->GetNext()) != nullptr);
493 }
494 stream << "}";
Mingyao Yang296bd602014-10-06 16:47:28 -0700495 stream << " is_fixed: " << is_fixed_ << ", is_split: " << IsSplit();
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000496 stream << " is_high: " << IsHighInterval();
497 stream << " is_low: " << IsLowInterval();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100498 }
499
500 LiveInterval* GetNextSibling() const { return next_sibling_; }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100501
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100502 // Returns the first register hint that is at least free before
503 // the value contained in `free_until`. If none is found, returns
504 // `kNoRegister`.
505 int FindFirstRegisterHint(size_t* free_until) const;
506
507 // If there is enough at the definition site to find a register (for example
508 // it uses the same input as the first input), returns the register as a hint.
509 // Returns kNoRegister otherwise.
510 int FindHintAtDefinition() const;
511
512 // Returns whether the interval needs two (Dex virtual register size `kVRegSize`)
513 // slots for spilling.
514 bool NeedsTwoSpillSlots() const;
515
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100516 bool IsFloatingPoint() const {
517 return type_ == Primitive::kPrimFloat || type_ == Primitive::kPrimDouble;
518 }
519
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100520 // Converts the location of the interval to a `Location` object.
521 Location ToLocation() const;
522
523 // Returns the location of the interval following its siblings at `position`.
524 Location GetLocationAt(size_t position) const;
525
526 // Finds the interval that covers `position`.
527 const LiveInterval& GetIntervalAt(size_t position) const;
528
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100529 // Returns whether `other` and `this` share the same kind of register.
530 bool SameRegisterKind(Location other) const;
531
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000532 bool HasHighInterval() const {
533 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
534 }
535
536 bool HasLowInterval() const {
537 return IsHighInterval();
538 }
539
540 LiveInterval* GetLowInterval() const {
541 DCHECK(HasLowInterval());
542 return high_or_low_interval_;
543 }
544
545 LiveInterval* GetHighInterval() const {
546 DCHECK(HasHighInterval());
547 return high_or_low_interval_;
548 }
549
550 bool IsHighInterval() const {
551 return GetParent()->is_high_interval_;
552 }
553
554 bool IsLowInterval() const {
555 return !IsHighInterval() && (GetParent()->high_or_low_interval_ != nullptr);
556 }
557
558 void SetLowInterval(LiveInterval* low) {
559 DCHECK(IsHighInterval());
560 high_or_low_interval_ = low;
561 }
562
563 void SetHighInterval(LiveInterval* high) {
564 DCHECK(IsLowInterval());
565 high_or_low_interval_ = high;
566 }
567
568 void AddHighInterval(bool is_temp = false) {
569 DCHECK_EQ(GetParent(), this);
570 DCHECK(!HasHighInterval());
571 DCHECK(!HasLowInterval());
572 high_or_low_interval_ = new (allocator_) LiveInterval(
573 allocator_, type_, defined_by_, false, kNoRegister, is_temp, false, true);
574 high_or_low_interval_->high_or_low_interval_ = this;
575 if (first_range_ != nullptr) {
576 high_or_low_interval_->first_range_ = first_range_->Dup(allocator_);
577 high_or_low_interval_->last_range_ = first_range_->GetLastRange();
578 }
579 if (first_use_ != nullptr) {
580 high_or_low_interval_->first_use_ = first_use_->Dup(allocator_);
581 }
582 }
583
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100584 private:
Mingyao Yang296bd602014-10-06 16:47:28 -0700585 LiveInterval(ArenaAllocator* allocator,
586 Primitive::Type type,
587 HInstruction* defined_by = nullptr,
588 bool is_fixed = false,
589 int reg = kNoRegister,
590 bool is_temp = false,
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000591 bool is_slow_path_safepoint = false,
592 bool is_high_interval = false)
Mingyao Yang296bd602014-10-06 16:47:28 -0700593 : allocator_(allocator),
594 first_range_(nullptr),
595 last_range_(nullptr),
596 first_use_(nullptr),
597 type_(type),
598 next_sibling_(nullptr),
599 parent_(this),
600 register_(reg),
601 spill_slot_(kNoSpillSlot),
602 is_fixed_(is_fixed),
603 is_temp_(is_temp),
604 is_slow_path_safepoint_(is_slow_path_safepoint),
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000605 is_high_interval_(is_high_interval),
606 high_or_low_interval_(nullptr),
Mingyao Yang296bd602014-10-06 16:47:28 -0700607 defined_by_(defined_by) {}
608
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100609 ArenaAllocator* const allocator_;
610
611 // Ranges of this interval. We need a quick access to the last range to test
612 // for liveness (see `IsDeadAt`).
613 LiveRange* first_range_;
614 LiveRange* last_range_;
615
616 // Uses of this interval. Note that this linked list is shared amongst siblings.
617 UsePosition* first_use_;
618
619 // The instruction type this interval corresponds to.
620 const Primitive::Type type_;
621
622 // Live interval that is the result of a split.
623 LiveInterval* next_sibling_;
624
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100625 // The first interval from which split intervals come from.
626 LiveInterval* parent_;
627
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100628 // The register allocated to this interval.
629 int register_;
630
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100631 // The spill slot allocated to this interval.
632 int spill_slot_;
633
634 // Whether the interval is for a fixed register.
Nicolas Geoffray39468442014-09-02 15:17:15 +0100635 const bool is_fixed_;
636
637 // Whether the interval is for a temporary.
638 const bool is_temp_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100639
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100640 // Whether the interval is for a safepoint that calls on slow path.
641 const bool is_slow_path_safepoint_;
642
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000643 // Whether this interval is a synthesized interval for register pair.
644 const bool is_high_interval_;
645
646 // If this interval needs a register pair, the high or low equivalent.
647 // `is_high_interval_` tells whether this holds the low or the high.
648 LiveInterval* high_or_low_interval_;
649
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100650 // The instruction represented by this interval.
651 HInstruction* const defined_by_;
652
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100653 static constexpr int kNoRegister = -1;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100654 static constexpr int kNoSpillSlot = -1;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100655
Nicolas Geoffraydd8f8872015-01-15 15:37:37 +0000656 ART_FRIEND_TEST(RegisterAllocatorTest, SpillInactive);
657
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100658 DISALLOW_COPY_AND_ASSIGN(LiveInterval);
659};
660
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100661class SsaLivenessAnalysis : public ValueObject {
662 public:
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100663 SsaLivenessAnalysis(const HGraph& graph, CodeGenerator* codegen)
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100664 : graph_(graph),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100665 codegen_(codegen),
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000666 linear_order_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100667 block_infos_(graph.GetArena(), graph.GetBlocks().Size()),
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100668 instructions_from_ssa_index_(graph.GetArena(), 0),
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100669 instructions_from_lifetime_position_(graph.GetArena(), 0),
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100670 number_of_ssa_values_(0) {
671 block_infos_.SetSize(graph.GetBlocks().Size());
672 }
673
674 void Analyze();
675
676 BitVector* GetLiveInSet(const HBasicBlock& block) const {
677 return &block_infos_.Get(block.GetBlockId())->live_in_;
678 }
679
680 BitVector* GetLiveOutSet(const HBasicBlock& block) const {
681 return &block_infos_.Get(block.GetBlockId())->live_out_;
682 }
683
684 BitVector* GetKillSet(const HBasicBlock& block) const {
685 return &block_infos_.Get(block.GetBlockId())->kill_;
686 }
687
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000688 const GrowableArray<HBasicBlock*>& GetLinearOrder() const {
689 return linear_order_;
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100690 }
691
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100692 HInstruction* GetInstructionFromSsaIndex(size_t index) const {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100693 return instructions_from_ssa_index_.Get(index);
694 }
695
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100696 HInstruction* GetInstructionFromPosition(size_t index) const {
697 return instructions_from_lifetime_position_.Get(index);
698 }
699
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100700 HInstruction* GetTempUser(LiveInterval* temp) const {
701 // A temporary shares the same lifetime start as the instruction that requires it.
702 DCHECK(temp->IsTemp());
703 return GetInstructionFromPosition(temp->GetStart() / 2);
704 }
705
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100706 size_t GetMaxLifetimePosition() const {
707 return instructions_from_lifetime_position_.Size() * 2 - 1;
708 }
709
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100710 size_t GetNumberOfSsaValues() const {
711 return number_of_ssa_values_;
712 }
713
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100714 private:
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100715 // Linearize the graph so that:
716 // (1): a block is always after its dominator,
717 // (2): blocks of loops are contiguous.
718 // This creates a natural and efficient ordering when visualizing live ranges.
719 void LinearizeGraph();
720
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100721 // Give an SSA number to each instruction that defines a value used by another instruction,
722 // and setup the lifetime information of each instruction and block.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100723 void NumberInstructions();
724
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100725 // Compute live ranges of instructions, as well as live_in, live_out and kill sets.
726 void ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100727
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100728 // Compute the live ranges of instructions, as well as the initial live_in, live_out and
729 // kill sets, that do not take into account backward branches.
730 void ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100731
732 // After computing the initial sets, this method does a fixed point
733 // calculation over the live_in and live_out set to take into account
734 // backwards branches.
735 void ComputeLiveInAndLiveOutSets();
736
737 // Update the live_in set of the block and returns whether it has changed.
738 bool UpdateLiveIn(const HBasicBlock& block);
739
740 // Update the live_out set of the block and returns whether it has changed.
741 bool UpdateLiveOut(const HBasicBlock& block);
742
743 const HGraph& graph_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100744 CodeGenerator* const codegen_;
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000745 GrowableArray<HBasicBlock*> linear_order_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100746 GrowableArray<BlockInfo*> block_infos_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100747
748 // Temporary array used when computing live_in, live_out, and kill sets.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100749 GrowableArray<HInstruction*> instructions_from_ssa_index_;
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100750
751 // Temporary array used when inserting moves in the graph.
752 GrowableArray<HInstruction*> instructions_from_lifetime_position_;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100753 size_t number_of_ssa_values_;
754
755 DISALLOW_COPY_AND_ASSIGN(SsaLivenessAnalysis);
756};
757
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000758class HLinearPostOrderIterator : public ValueObject {
759 public:
760 explicit HLinearPostOrderIterator(const SsaLivenessAnalysis& liveness)
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000761 : order_(liveness.GetLinearOrder()), index_(liveness.GetLinearOrder().Size()) {}
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000762
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000763 bool Done() const { return index_ == 0; }
764
765 HBasicBlock* Current() const { return order_.Get(index_ -1); }
766
767 void Advance() {
768 --index_;
769 DCHECK_GE(index_, 0U);
770 }
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000771
772 private:
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000773 const GrowableArray<HBasicBlock*>& order_;
Nicolas Geoffraye50fa582014-11-24 17:44:15 +0000774 size_t index_;
775
776 DISALLOW_COPY_AND_ASSIGN(HLinearPostOrderIterator);
777};
778
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +0000779class HLinearOrderIterator : public ValueObject {
780 public:
781 explicit HLinearOrderIterator(const SsaLivenessAnalysis& liveness)
782 : order_(liveness.GetLinearOrder()), index_(0) {}
783
784 bool Done() const { return index_ == order_.Size(); }
785 HBasicBlock* Current() const { return order_.Get(index_); }
786 void Advance() { ++index_; }
787
788 private:
789 const GrowableArray<HBasicBlock*>& order_;
790 size_t index_;
791
792 DISALLOW_COPY_AND_ASSIGN(HLinearOrderIterator);
793};
794
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100795} // namespace art
796
797#endif // ART_COMPILER_OPTIMIZING_SSA_LIVENESS_ANALYSIS_H_