blob: 76cf8fe1ae00f1a140511783c3b81b242f25cd2f [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#include "ssa_liveness_analysis.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010018
Ian Rogerse77493c2014-08-20 15:08:45 -070019#include "base/bit_vector-inl.h"
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010020#include "code_generator.h"
Aart Bik96202302016-10-04 17:33:56 -070021#include "linear_order.h"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010022#include "nodes.h"
23
24namespace art {
25
26void SsaLivenessAnalysis::Analyze() {
Aart Bik96202302016-10-04 17:33:56 -070027 // Compute the linear order directly in the graph's data structure
28 // (there are no more following graph mutations).
29 LinearizeGraph(graph_, graph_->GetArena(), &graph_->linear_order_);
30
31 // Liveness analysis.
Nicolas Geoffray804d0932014-05-02 08:46:00 +010032 NumberInstructions();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010033 ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010034}
35
36void SsaLivenessAnalysis::NumberInstructions() {
37 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010038 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010039 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010040 // start and end position. Non-phi instructions have a distinct lifetime position than
41 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010042 // lifetime position.
43 //
44 // Because the register allocator will insert moves in the graph, we need
45 // to differentiate between the start and end of an instruction. Adding 2 to
46 // the lifetime position for each instruction ensures the start of an
47 // instruction is different than the end of the previous instruction.
Aart Bik96202302016-10-04 17:33:56 -070048 for (HBasicBlock* block : graph_->GetLinearOrder()) {
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010049 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010050
Andreas Gampe277ccbd2014-11-03 21:36:10 -080051 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
52 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000053 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010054 LocationSummary* locations = current->GetLocations();
55 if (locations != nullptr && locations->Out().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010056 instructions_from_ssa_index_.push_back(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010057 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010058 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010059 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +010060 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010061 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010062 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +010063 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010064
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010065 // Add a null marker to notify we are starting a block.
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010066 instructions_from_lifetime_position_.push_back(nullptr);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010067
Andreas Gampe277ccbd2014-11-03 21:36:10 -080068 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
69 inst_it.Advance()) {
70 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +000071 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +010072 LocationSummary* locations = current->GetLocations();
73 if (locations != nullptr && locations->Out().IsValid()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010074 instructions_from_ssa_index_.push_back(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010075 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010076 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010077 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +010078 }
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010079 instructions_from_lifetime_position_.push_back(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010080 current->SetLifetimePosition(lifetime_position);
81 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +010082 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010083
Nicolas Geoffraya7062e02014-05-22 12:50:17 +010084 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010085 }
86 number_of_ssa_values_ = ssa_index;
87}
88
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010089void SsaLivenessAnalysis::ComputeLiveness() {
Aart Bik96202302016-10-04 17:33:56 -070090 for (HBasicBlock* block : graph_->GetLinearOrder()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +010091 block_infos_[block->GetBlockId()] =
92 new (graph_->GetArena()) BlockInfo(graph_->GetArena(), *block, number_of_ssa_values_);
Nicolas Geoffray804d0932014-05-02 08:46:00 +010093 }
94
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010095 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
96 // This method does not handle backward branches for the sets, therefore live_in
97 // and live_out sets are not yet correct.
98 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010099
100 // Do a fixed point calculation to take into account backward branches,
101 // that will update live_in of loop headers, and therefore live_out and live_in
102 // of blocks in the loop.
103 ComputeLiveInAndLiveOutSets();
104}
105
David Brazdil674f5192016-02-02 16:50:46 +0000106static void RecursivelyProcessInputs(HInstruction* current,
107 HInstruction* actual_user,
108 BitVector* live_in) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100109 HInputsRef inputs = current->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100110 for (size_t i = 0; i < inputs.size(); ++i) {
111 HInstruction* input = inputs[i];
David Brazdil674f5192016-02-02 16:50:46 +0000112 bool has_in_location = current->GetLocations()->InAt(i).IsValid();
113 bool has_out_location = input->GetLocations()->Out().IsValid();
114
115 if (has_in_location) {
116 DCHECK(has_out_location)
117 << "Instruction " << current->DebugName() << current->GetId()
118 << " expects an input value at index " << i << " but "
119 << input->DebugName() << input->GetId() << " does not produce one.";
120 DCHECK(input->HasSsaIndex());
121 // `input` generates a result used by `current`. Add use and update
122 // the live-in set.
123 input->GetLiveInterval()->AddUse(current, /* environment */ nullptr, i, actual_user);
124 live_in->SetBit(input->GetSsaIndex());
125 } else if (has_out_location) {
126 // `input` generates a result but it is not used by `current`.
127 } else {
128 // `input` is inlined into `current`. Walk over its inputs and record
129 // uses at `current`.
130 DCHECK(input->IsEmittedAtUseSite());
131 // Check that the inlined input is not a phi. Recursing on loop phis could
132 // lead to an infinite loop.
133 DCHECK(!input->IsPhi());
134 RecursivelyProcessInputs(input, actual_user, live_in);
135 }
136 }
137}
138
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100139void SsaLivenessAnalysis::ComputeLiveRanges() {
140 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100141 // that instruction is defined, and killing instructions that are being visited.
Aart Bik96202302016-10-04 17:33:56 -0700142 for (HBasicBlock* block : LinearPostOrder(graph_->GetLinearOrder())) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100143 BitVector* kill = GetKillSet(*block);
144 BitVector* live_in = GetLiveInSet(*block);
145
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100146 // Set phi inputs of successors of this block corresponding to this block
147 // as live_in.
Vladimir Marko60584552015-09-03 13:35:12 +0000148 for (HBasicBlock* successor : block->GetSuccessors()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100149 live_in->Union(GetLiveInSet(*successor));
David Brazdil77a48ae2015-09-15 12:34:04 +0000150 if (successor->IsCatchBlock()) {
151 // Inputs of catch phis will be kept alive through their environment
152 // uses, allowing the runtime to copy their values to the corresponding
153 // catch phi spill slots when an exception is thrown.
154 // The only instructions which may not be recorded in the environments
155 // are constants created by the SSA builder as typed equivalents of
156 // untyped constants from the bytecode, or phis with only such constants
David Brazdilbadd8262016-02-02 16:28:56 +0000157 // as inputs (verified by GraphChecker). Their raw binary value must
David Brazdil77a48ae2015-09-15 12:34:04 +0000158 // therefore be the same and we only need to keep alive one.
159 } else {
160 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
161 for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
162 HInstruction* phi = phi_it.Current();
163 HInstruction* input = phi->InputAt(phi_input_index);
164 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
165 // A phi input whose last user is the phi dies at the end of the predecessor block,
166 // and not at the phi's lifetime position.
167 live_in->SetBit(input->GetSsaIndex());
168 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100169 }
170 }
171
172 // Add a range that covers this block to all instructions live_in because of successors.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100173 // Instructions defined in this block will have their start of the range adjusted.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100174 for (uint32_t idx : live_in->Indexes()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100175 HInstruction* current = GetInstructionFromSsaIndex(idx);
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100176 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100177 }
178
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800179 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
180 back_it.Advance()) {
181 HInstruction* current = back_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100182 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100183 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100184 kill->SetBit(current->GetSsaIndex());
185 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100186 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100187 }
188
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000189 // Process the environment first, because we know their uses come after
190 // or at the same liveness position of inputs.
Nicolas Geoffray0a23d742015-05-07 11:57:35 +0100191 for (HEnvironment* environment = current->GetEnvironment();
192 environment != nullptr;
193 environment = environment->GetParent()) {
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000194 // Handle environment uses. See statements (b) and (c) of the
195 // SsaLivenessAnalysis.
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000196 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
197 HInstruction* instruction = environment->GetInstructionAt(i);
Mingyao Yang718493c2015-07-22 15:56:34 -0700198 bool should_be_live = ShouldBeLiveForEnvironment(current, instruction);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000199 if (should_be_live) {
200 DCHECK(instruction->HasSsaIndex());
201 live_in->SetBit(instruction->GetSsaIndex());
202 }
203 if (instruction != nullptr) {
204 instruction->GetLiveInterval()->AddUse(
David Brazdilb3e773e2016-01-26 11:28:37 +0000205 current, environment, i, /* actual_user */ nullptr, should_be_live);
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000206 }
207 }
208 }
209
David Brazdilb3e773e2016-01-26 11:28:37 +0000210 // Process inputs of instructions.
211 if (current->IsEmittedAtUseSite()) {
212 if (kIsDebugBuild) {
213 DCHECK(!current->GetLocations()->Out().IsValid());
Vladimir Marko46817b82016-03-29 12:21:58 +0100214 for (const HUseListNode<HInstruction*>& use : current->GetUses()) {
215 HInstruction* user = use.GetUser();
216 size_t index = use.GetIndex();
David Brazdilb3e773e2016-01-26 11:28:37 +0000217 DCHECK(!user->GetLocations()->InAt(index).IsValid());
218 }
219 DCHECK(!current->HasEnvironmentUses());
220 }
221 } else {
David Brazdil674f5192016-02-02 16:50:46 +0000222 RecursivelyProcessInputs(current, current, live_in);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100223 }
224 }
225
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100226 // Kill phis defined in this block.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800227 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
228 HInstruction* current = inst_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100229 if (current->HasSsaIndex()) {
230 kill->SetBit(current->GetSsaIndex());
231 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100232 LiveInterval* interval = current->GetLiveInterval();
233 DCHECK((interval->GetFirstRange() == nullptr)
234 || (interval->GetStart() == current->GetLifetimePosition()));
235 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100236 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100237 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100238
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100239 if (block->IsLoopHeader()) {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100240 if (kIsDebugBuild) {
241 CheckNoLiveInIrreducibleLoop(*block);
Nicolas Geoffray15bd2282016-01-05 15:55:41 +0000242 }
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100243 size_t last_position = block->GetLoopInformation()->GetLifetimeEnd();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100244 // For all live_in instructions at the loop header, we need to create a range
245 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100246 for (uint32_t idx : live_in->Indexes()) {
Vladimir Marko2aaa4b52015-09-17 17:03:26 +0100247 HInstruction* current = GetInstructionFromSsaIndex(idx);
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100248 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100249 }
250 }
251 }
252}
253
254void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
255 bool changed;
256 do {
257 changed = false;
258
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100259 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100260 const HBasicBlock& block = *it.Current();
261
262 // The live_in set depends on the kill set (which does not
263 // change in this loop), and the live_out set. If the live_out
264 // set does not change, there is no need to update the live_in set.
265 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
Nicolas Geoffrayd7c2fdc2016-05-10 14:35:34 +0100266 if (kIsDebugBuild) {
267 CheckNoLiveInIrreducibleLoop(block);
268 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100269 changed = true;
270 }
271 }
272 } while (changed);
273}
274
275bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
276 BitVector* live_out = GetLiveOutSet(block);
277 bool changed = false;
278 // The live_out set of a block is the union of live_in sets of its successors.
Vladimir Marko60584552015-09-03 13:35:12 +0000279 for (HBasicBlock* successor : block.GetSuccessors()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100280 if (live_out->Union(GetLiveInSet(*successor))) {
281 changed = true;
282 }
283 }
284 return changed;
285}
286
287
288bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
289 BitVector* live_out = GetLiveOutSet(block);
290 BitVector* kill = GetKillSet(block);
291 BitVector* live_in = GetLiveInSet(block);
292 // If live_out is updated (because of backward branches), we need to make
293 // sure instructions in live_out are also in live_in, unless they are killed
294 // by this block.
295 return live_in->UnionIfNotIn(live_out, kill);
296}
297
Matthew Gharrityd9ffd0d2016-06-22 10:27:55 -0700298void LiveInterval::DumpWithContext(std::ostream& stream,
299 const CodeGenerator& codegen) const {
300 Dump(stream);
301 if (IsFixed()) {
302 stream << ", register:" << GetRegister() << "(";
303 if (IsFloatingPoint()) {
304 codegen.DumpFloatingPointRegister(stream, GetRegister());
305 } else {
306 codegen.DumpCoreRegister(stream, GetRegister());
307 }
308 stream << ")";
309 } else {
310 stream << ", spill slot:" << GetSpillSlot();
311 }
312 stream << ", requires_register:" << (GetDefinedBy() != nullptr && RequiresRegister());
313 if (GetParent()->GetDefinedBy() != nullptr) {
314 stream << ", defined_by:" << GetParent()->GetDefinedBy()->GetKind();
315 stream << "(" << GetParent()->GetDefinedBy()->GetLifetimePosition() << ")";
316 }
317}
318
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000319static int RegisterOrLowRegister(Location location) {
320 return location.IsPair() ? location.low() : location.reg();
321}
322
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100323int LiveInterval::FindFirstRegisterHint(size_t* free_until,
324 const SsaLivenessAnalysis& liveness) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000325 DCHECK(!IsHighInterval());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000326 if (IsTemp()) return kNoRegister;
327
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100328 if (GetParent() == this && defined_by_ != nullptr) {
329 // This is the first interval for the instruction. Try to find
330 // a register based on its definition.
331 DCHECK_EQ(defined_by_->GetLiveInterval(), this);
332 int hint = FindHintAtDefinition();
333 if (hint != kNoRegister && free_until[hint] > GetStart()) {
334 return hint;
335 }
336 }
337
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100338 if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) {
339 // If the start of this interval is at a block boundary, we look at the
340 // location of the interval in blocks preceding the block this interval
341 // starts at. If one location is a register we return it as a hint. This
342 // will avoid a move between the two blocks.
343 HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2);
Nicolas Geoffray82726882015-06-01 13:51:57 +0100344 size_t next_register_use = FirstRegisterUse();
Vladimir Marko60584552015-09-03 13:35:12 +0000345 for (HBasicBlock* predecessor : block->GetPredecessors()) {
346 size_t position = predecessor->GetLifetimeEnd() - 1;
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100347 // We know positions above GetStart() do not have a location yet.
348 if (position < GetStart()) {
349 LiveInterval* existing = GetParent()->GetSiblingAt(position);
350 if (existing != nullptr
351 && existing->HasRegister()
Nicolas Geoffray82726882015-06-01 13:51:57 +0100352 // It's worth using that register if it is available until
353 // the next use.
354 && (free_until[existing->GetRegister()] >= next_register_use)) {
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100355 return existing->GetRegister();
356 }
357 }
358 }
359 }
360
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100361 UsePosition* use = first_use_;
362 size_t start = GetStart();
363 size_t end = GetEnd();
364 while (use != nullptr && use->GetPosition() <= end) {
365 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100366 if (use_position >= start && !use->IsSynthesized()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100367 HInstruction* user = use->GetUser();
368 size_t input_index = use->GetInputIndex();
369 if (user->IsPhi()) {
370 // If the phi has a register, try to use the same.
371 Location phi_location = user->GetLiveInterval()->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000372 if (phi_location.IsRegisterKind()) {
373 DCHECK(SameRegisterKind(phi_location));
374 int reg = RegisterOrLowRegister(phi_location);
375 if (free_until[reg] >= use_position) {
376 return reg;
377 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100378 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100379 // If the instruction dies at the phi assignment, we can try having the
380 // same register.
Vladimir Markoec7802a2015-10-01 20:57:57 +0100381 if (end == user->GetBlock()->GetPredecessors()[input_index]->GetLifetimeEnd()) {
Vladimir Markoe9004912016-06-16 16:50:52 +0100382 HInputsRef inputs = user->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100383 for (size_t i = 0; i < inputs.size(); ++i) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100384 if (i == input_index) {
385 continue;
386 }
Vladimir Marko372f10e2016-05-17 16:30:10 +0100387 Location location = inputs[i]->GetLiveInterval()->GetLocationAt(
Vladimir Markoec7802a2015-10-01 20:57:57 +0100388 user->GetBlock()->GetPredecessors()[i]->GetLifetimeEnd() - 1);
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000389 if (location.IsRegisterKind()) {
390 int reg = RegisterOrLowRegister(location);
391 if (free_until[reg] >= use_position) {
392 return reg;
393 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100394 }
395 }
396 }
397 } else {
398 // If the instruction is expected in a register, try to use it.
399 LocationSummary* locations = user->GetLocations();
400 Location expected = locations->InAt(use->GetInputIndex());
401 // We use the user's lifetime position - 1 (and not `use_position`) because the
402 // register is blocked at the beginning of the user.
403 size_t position = user->GetLifetimePosition() - 1;
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000404 if (expected.IsRegisterKind()) {
405 DCHECK(SameRegisterKind(expected));
406 int reg = RegisterOrLowRegister(expected);
407 if (free_until[reg] >= position) {
408 return reg;
409 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100410 }
411 }
412 }
413 use = use->GetNext();
414 }
415
416 return kNoRegister;
417}
418
419int LiveInterval::FindHintAtDefinition() const {
420 if (defined_by_->IsPhi()) {
421 // Try to use the same register as one of the inputs.
Vladimir Marko60584552015-09-03 13:35:12 +0000422 const ArenaVector<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
Vladimir Markoe9004912016-06-16 16:50:52 +0100423 HInputsRef inputs = defined_by_->GetInputs();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100424 for (size_t i = 0; i < inputs.size(); ++i) {
Vladimir Marko60584552015-09-03 13:35:12 +0000425 size_t end = predecessors[i]->GetLifetimeEnd();
Vladimir Marko372f10e2016-05-17 16:30:10 +0100426 LiveInterval* input_interval = inputs[i]->GetLiveInterval()->GetSiblingAt(end - 1);
David Brazdil241a4862015-04-16 17:59:03 +0100427 if (input_interval->GetEnd() == end) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100428 // If the input dies at the end of the predecessor, we know its register can
429 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100430 Location input_location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000431 if (input_location.IsRegisterKind()) {
432 DCHECK(SameRegisterKind(input_location));
433 return RegisterOrLowRegister(input_location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100434 }
435 }
436 }
437 } else {
438 LocationSummary* locations = GetDefinedBy()->GetLocations();
439 Location out = locations->Out();
440 if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) {
441 // Try to use the same register as the first input.
David Brazdil241a4862015-04-16 17:59:03 +0100442 LiveInterval* input_interval =
443 GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1);
444 if (input_interval->GetEnd() == GetStart()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100445 // If the input dies at the start of this instruction, we know its register can
446 // be reused.
David Brazdil241a4862015-04-16 17:59:03 +0100447 Location location = input_interval->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000448 if (location.IsRegisterKind()) {
449 DCHECK(SameRegisterKind(location));
450 return RegisterOrLowRegister(location);
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100451 }
452 }
453 }
454 }
455 return kNoRegister;
456}
457
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100458bool LiveInterval::SameRegisterKind(Location other) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000459 if (IsFloatingPoint()) {
460 if (IsLowInterval() || IsHighInterval()) {
461 return other.IsFpuRegisterPair();
462 } else {
463 return other.IsFpuRegister();
464 }
465 } else {
466 if (IsLowInterval() || IsHighInterval()) {
467 return other.IsRegisterPair();
468 } else {
469 return other.IsRegister();
470 }
471 }
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100472}
473
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100474bool LiveInterval::NeedsTwoSpillSlots() const {
475 return type_ == Primitive::kPrimLong || type_ == Primitive::kPrimDouble;
476}
477
478Location LiveInterval::ToLocation() const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000479 DCHECK(!IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100480 if (HasRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000481 if (IsFloatingPoint()) {
482 if (HasHighInterval()) {
483 return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
484 } else {
485 return Location::FpuRegisterLocation(GetRegister());
486 }
487 } else {
488 if (HasHighInterval()) {
489 return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
490 } else {
491 return Location::RegisterLocation(GetRegister());
492 }
493 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100494 } else {
495 HInstruction* defined_by = GetParent()->GetDefinedBy();
496 if (defined_by->IsConstant()) {
497 return defined_by->GetLocations()->Out();
498 } else if (GetParent()->HasSpillSlot()) {
499 if (NeedsTwoSpillSlots()) {
500 return Location::DoubleStackSlot(GetParent()->GetSpillSlot());
501 } else {
502 return Location::StackSlot(GetParent()->GetSpillSlot());
503 }
504 } else {
505 return Location();
506 }
507 }
508}
509
David Brazdil5b8e6a52015-02-25 16:17:05 +0000510Location LiveInterval::GetLocationAt(size_t position) {
David Brazdil241a4862015-04-16 17:59:03 +0100511 LiveInterval* sibling = GetSiblingAt(position);
512 DCHECK(sibling != nullptr);
513 return sibling->ToLocation();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100514}
515
David Brazdil241a4862015-04-16 17:59:03 +0100516LiveInterval* LiveInterval::GetSiblingAt(size_t position) {
David Brazdil5b8e6a52015-02-25 16:17:05 +0000517 LiveInterval* current = this;
David Brazdil241a4862015-04-16 17:59:03 +0100518 while (current != nullptr && !current->IsDefinedAt(position)) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100519 current = current->GetNextSibling();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100520 }
David Brazdil241a4862015-04-16 17:59:03 +0100521 return current;
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100522}
523
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100524} // namespace art