blob: d41157b8d8b7c8fbd15b46cae393d749db646eb3 [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"
Nicolas Geoffray804d0932014-05-02 08:46:00 +010021#include "nodes.h"
22
23namespace art {
24
25void SsaLivenessAnalysis::Analyze() {
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010026 LinearizeGraph();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010027 NumberInstructions();
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +010028 ComputeLiveness();
Nicolas Geoffray804d0932014-05-02 08:46:00 +010029}
30
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010031static bool IsLoop(HLoopInformation* info) {
32 return info != nullptr;
33}
34
35static bool InSameLoop(HLoopInformation* first_loop, HLoopInformation* second_loop) {
36 return first_loop == second_loop;
37}
38
39static bool IsInnerLoop(HLoopInformation* outer, HLoopInformation* inner) {
40 return (inner != outer)
41 && (inner != nullptr)
42 && (outer != nullptr)
43 && inner->IsIn(*outer);
44}
45
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000046static void AddToListForLinearization(GrowableArray<HBasicBlock*>* worklist, HBasicBlock* block) {
47 size_t insert_at = worklist->Size();
48 HLoopInformation* block_loop = block->GetLoopInformation();
49 for (; insert_at > 0; --insert_at) {
50 HBasicBlock* current = worklist->Get(insert_at - 1);
51 HLoopInformation* current_loop = current->GetLoopInformation();
52 if (InSameLoop(block_loop, current_loop)
53 || !IsLoop(current_loop)
54 || IsInnerLoop(current_loop, block_loop)) {
55 // The block can be processed immediately.
56 break;
Nicolas Geoffraye50fa582014-11-24 17:44:15 +000057 }
Nicolas Geoffraye50fa582014-11-24 17:44:15 +000058 }
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000059 worklist->InsertAt(insert_at, block);
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010060}
61
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +010062void SsaLivenessAnalysis::LinearizeGraph() {
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000063 // Create a reverse post ordering with the following properties:
64 // - Blocks in a loop are consecutive,
65 // - Back-edge is the last block before loop exits.
66
67 // (1): Record the number of forward predecessors for each block. This is to
68 // ensure the resulting order is reverse post order. We could use the
69 // current reverse post order in the graph, but it would require making
70 // order queries to a GrowableArray, which is not the best data structure
71 // for it.
72 GrowableArray<uint32_t> forward_predecessors(graph_.GetArena(), graph_.GetBlocks().Size());
73 forward_predecessors.SetSize(graph_.GetBlocks().Size());
74 for (size_t i = 0, e = graph_.GetBlocks().Size(); i < e; ++i) {
75 HBasicBlock* block = graph_.GetBlocks().Get(i);
76 size_t number_of_forward_predecessors = block->GetPredecessors().Size();
77 if (block->IsLoopHeader()) {
78 // We rely on having simplified the CFG.
79 DCHECK_EQ(1u, block->GetLoopInformation()->NumberOfBackEdges());
80 number_of_forward_predecessors--;
81 }
82 forward_predecessors.Put(block->GetBlockId(), number_of_forward_predecessors);
83 }
84
85 // (2): Following a worklist approach, first start with the entry block, and
86 // iterate over the successors. When all non-back edge predecessors of a
87 // successor block are visited, the successor block is added in the worklist
88 // following an order that satisfies the requirements to build our linear graph.
89 GrowableArray<HBasicBlock*> worklist(graph_.GetArena(), 1);
90 worklist.Add(graph_.GetEntryBlock());
91 do {
92 HBasicBlock* current = worklist.Pop();
93 linear_order_.Add(current);
94 for (size_t i = 0, e = current->GetSuccessors().Size(); i < e; ++i) {
95 HBasicBlock* successor = current->GetSuccessors().Get(i);
96 int block_id = successor->GetBlockId();
97 size_t number_of_remaining_predecessors = forward_predecessors.Get(block_id);
98 if (number_of_remaining_predecessors == 1) {
99 AddToListForLinearization(&worklist, successor);
100 }
101 forward_predecessors.Put(block_id, number_of_remaining_predecessors - 1);
102 }
103 } while (!worklist.IsEmpty());
Nicolas Geoffray0d3f5782014-05-14 09:43:38 +0100104}
105
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100106void SsaLivenessAnalysis::NumberInstructions() {
107 int ssa_index = 0;
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100108 size_t lifetime_position = 0;
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100109 // Each instruction gets a lifetime position, and a block gets a lifetime
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100110 // start and end position. Non-phi instructions have a distinct lifetime position than
111 // the block they are in. Phi instructions have the lifetime start of their block as
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100112 // lifetime position.
113 //
114 // Because the register allocator will insert moves in the graph, we need
115 // to differentiate between the start and end of an instruction. Adding 2 to
116 // the lifetime position for each instruction ensures the start of an
117 // instruction is different than the end of the previous instruction.
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100118 HGraphVisitor* location_builder = codegen_->GetLocationBuilder();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100119 for (HLinearOrderIterator it(*this); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100120 HBasicBlock* block = it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100121 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100122
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800123 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
124 HInstruction* current = inst_it.Current();
Nicolas Geoffray8a16d972014-09-11 10:30:02 +0100125 current->Accept(location_builder);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100126 LocationSummary* locations = current->GetLocations();
127 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100128 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100129 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100130 current->SetLiveInterval(
Mingyao Yang296bd602014-10-06 16:47:28 -0700131 LiveInterval::MakeInterval(graph_.GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100132 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100133 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100134 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100135 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100136
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100137 // Add a null marker to notify we are starting a block.
138 instructions_from_lifetime_position_.Add(nullptr);
139
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800140 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
141 inst_it.Advance()) {
142 HInstruction* current = inst_it.Current();
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100143 current->Accept(codegen_->GetLocationBuilder());
144 LocationSummary* locations = current->GetLocations();
145 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100146 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100147 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100148 current->SetLiveInterval(
Mingyao Yang296bd602014-10-06 16:47:28 -0700149 LiveInterval::MakeInterval(graph_.GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100150 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100151 instructions_from_lifetime_position_.Add(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100152 current->SetLifetimePosition(lifetime_position);
153 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100154 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100155
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100156 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100157 }
158 number_of_ssa_values_ = ssa_index;
159}
160
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100161void SsaLivenessAnalysis::ComputeLiveness() {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100162 for (HLinearOrderIterator it(*this); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100163 HBasicBlock* block = it.Current();
164 block_infos_.Put(
165 block->GetBlockId(),
166 new (graph_.GetArena()) BlockInfo(graph_.GetArena(), *block, number_of_ssa_values_));
167 }
168
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100169 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
170 // This method does not handle backward branches for the sets, therefore live_in
171 // and live_out sets are not yet correct.
172 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100173
174 // Do a fixed point calculation to take into account backward branches,
175 // that will update live_in of loop headers, and therefore live_out and live_in
176 // of blocks in the loop.
177 ComputeLiveInAndLiveOutSets();
178}
179
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100180void SsaLivenessAnalysis::ComputeLiveRanges() {
181 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100182 // that instruction is defined, and killing instructions that are being visited.
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100183 for (HLinearPostOrderIterator it(*this); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100184 HBasicBlock* block = it.Current();
185
186 BitVector* kill = GetKillSet(*block);
187 BitVector* live_in = GetLiveInSet(*block);
188
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100189 // Set phi inputs of successors of this block corresponding to this block
190 // as live_in.
191 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
192 HBasicBlock* successor = block->GetSuccessors().Get(i);
193 live_in->Union(GetLiveInSet(*successor));
194 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800195 for (HInstructionIterator inst_it(successor->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
196 HInstruction* phi = inst_it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100197 HInstruction* input = phi->InputAt(phi_input_index);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100198 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100199 // A phi input whose last user is the phi dies at the end of the predecessor block,
200 // and not at the phi's lifetime position.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100201 live_in->SetBit(input->GetSsaIndex());
202 }
203 }
204
205 // Add a range that covers this block to all instructions live_in because of successors.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100206 // Instructions defined in this block will have their start of the range adjusted.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100207 for (uint32_t idx : live_in->Indexes()) {
208 HInstruction* current = instructions_from_ssa_index_.Get(idx);
209 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100210 }
211
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800212 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
213 back_it.Advance()) {
214 HInstruction* current = back_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100215 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100216 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100217 kill->SetBit(current->GetSsaIndex());
218 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100219 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100220 }
221
222 // All inputs of an instruction must be live.
223 for (size_t i = 0, e = current->InputCount(); i < e; ++i) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100224 HInstruction* input = current->InputAt(i);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100225 // Some instructions 'inline' their inputs, that is they do not need
226 // to be materialized.
227 if (input->HasSsaIndex()) {
228 live_in->SetBit(input->GetSsaIndex());
229 input->GetLiveInterval()->AddUse(current, i, false);
230 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100231 }
232
233 if (current->HasEnvironment()) {
234 // All instructions in the environment must be live.
235 GrowableArray<HInstruction*>* environment = current->GetEnvironment()->GetVRegs();
236 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
237 HInstruction* instruction = environment->Get(i);
238 if (instruction != nullptr) {
239 DCHECK(instruction->HasSsaIndex());
240 live_in->SetBit(instruction->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100241 instruction->GetLiveInterval()->AddUse(current, i, true);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100242 }
243 }
244 }
245 }
246
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100247 // Kill phis defined in this block.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800248 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
249 HInstruction* current = inst_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100250 if (current->HasSsaIndex()) {
251 kill->SetBit(current->GetSsaIndex());
252 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100253 LiveInterval* interval = current->GetLiveInterval();
254 DCHECK((interval->GetFirstRange() == nullptr)
255 || (interval->GetStart() == current->GetLifetimePosition()));
256 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100257 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100258 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100259
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100260 if (block->IsLoopHeader()) {
261 HBasicBlock* back_edge = block->GetLoopInformation()->GetBackEdges().Get(0);
262 // For all live_in instructions at the loop header, we need to create a range
263 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100264 for (uint32_t idx : live_in->Indexes()) {
265 HInstruction* current = instructions_from_ssa_index_.Get(idx);
266 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(),
267 back_edge->GetLifetimeEnd());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100268 }
269 }
270 }
271}
272
273void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
274 bool changed;
275 do {
276 changed = false;
277
278 for (HPostOrderIterator it(graph_); !it.Done(); it.Advance()) {
279 const HBasicBlock& block = *it.Current();
280
281 // The live_in set depends on the kill set (which does not
282 // change in this loop), and the live_out set. If the live_out
283 // set does not change, there is no need to update the live_in set.
284 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
285 changed = true;
286 }
287 }
288 } while (changed);
289}
290
291bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
292 BitVector* live_out = GetLiveOutSet(block);
293 bool changed = false;
294 // The live_out set of a block is the union of live_in sets of its successors.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100295 for (size_t i = 0, e = block.GetSuccessors().Size(); i < e; ++i) {
296 HBasicBlock* successor = block.GetSuccessors().Get(i);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100297 if (live_out->Union(GetLiveInSet(*successor))) {
298 changed = true;
299 }
300 }
301 return changed;
302}
303
304
305bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
306 BitVector* live_out = GetLiveOutSet(block);
307 BitVector* kill = GetKillSet(block);
308 BitVector* live_in = GetLiveInSet(block);
309 // If live_out is updated (because of backward branches), we need to make
310 // sure instructions in live_out are also in live_in, unless they are killed
311 // by this block.
312 return live_in->UnionIfNotIn(live_out, kill);
313}
314
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100315int LiveInterval::FindFirstRegisterHint(size_t* free_until) const {
316 if (GetParent() == this && defined_by_ != nullptr) {
317 // This is the first interval for the instruction. Try to find
318 // a register based on its definition.
319 DCHECK_EQ(defined_by_->GetLiveInterval(), this);
320 int hint = FindHintAtDefinition();
321 if (hint != kNoRegister && free_until[hint] > GetStart()) {
322 return hint;
323 }
324 }
325
326 UsePosition* use = first_use_;
327 size_t start = GetStart();
328 size_t end = GetEnd();
329 while (use != nullptr && use->GetPosition() <= end) {
330 size_t use_position = use->GetPosition();
331 if (use_position >= start && !use->GetIsEnvironment()) {
332 HInstruction* user = use->GetUser();
333 size_t input_index = use->GetInputIndex();
334 if (user->IsPhi()) {
335 // If the phi has a register, try to use the same.
336 Location phi_location = user->GetLiveInterval()->ToLocation();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100337 if (SameRegisterKind(phi_location) && free_until[phi_location.reg()] >= use_position) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100338 return phi_location.reg();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100339 }
340 const GrowableArray<HBasicBlock*>& predecessors = user->GetBlock()->GetPredecessors();
341 // If the instruction dies at the phi assignment, we can try having the
342 // same register.
343 if (end == predecessors.Get(input_index)->GetLifetimeEnd()) {
344 for (size_t i = 0, e = user->InputCount(); i < e; ++i) {
345 if (i == input_index) {
346 continue;
347 }
348 HInstruction* input = user->InputAt(i);
349 Location location = input->GetLiveInterval()->GetLocationAt(
350 predecessors.Get(i)->GetLifetimeEnd() - 1);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100351 if (location.IsRegister() && free_until[location.reg()] >= use_position) {
352 return location.reg();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100353 }
354 }
355 }
356 } else {
357 // If the instruction is expected in a register, try to use it.
358 LocationSummary* locations = user->GetLocations();
359 Location expected = locations->InAt(use->GetInputIndex());
360 // We use the user's lifetime position - 1 (and not `use_position`) because the
361 // register is blocked at the beginning of the user.
362 size_t position = user->GetLifetimePosition() - 1;
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100363 if (SameRegisterKind(expected) && free_until[expected.reg()] >= position) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100364 return expected.reg();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100365 }
366 }
367 }
368 use = use->GetNext();
369 }
370
371 return kNoRegister;
372}
373
374int LiveInterval::FindHintAtDefinition() const {
375 if (defined_by_->IsPhi()) {
376 // Try to use the same register as one of the inputs.
377 const GrowableArray<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
378 for (size_t i = 0, e = defined_by_->InputCount(); i < e; ++i) {
379 HInstruction* input = defined_by_->InputAt(i);
380 size_t end = predecessors.Get(i)->GetLifetimeEnd();
381 const LiveInterval& input_interval = input->GetLiveInterval()->GetIntervalAt(end - 1);
382 if (input_interval.GetEnd() == end) {
383 // If the input dies at the end of the predecessor, we know its register can
384 // be reused.
385 Location input_location = input_interval.ToLocation();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100386 if (SameRegisterKind(input_location)) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100387 return input_location.reg();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100388 }
389 }
390 }
391 } else {
392 LocationSummary* locations = GetDefinedBy()->GetLocations();
393 Location out = locations->Out();
394 if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) {
395 // Try to use the same register as the first input.
396 const LiveInterval& input_interval =
397 GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetIntervalAt(GetStart() - 1);
398 if (input_interval.GetEnd() == GetStart()) {
399 // If the input dies at the start of this instruction, we know its register can
400 // be reused.
401 Location location = input_interval.ToLocation();
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100402 if (SameRegisterKind(location)) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100403 return location.reg();
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100404 }
405 }
406 }
407 }
408 return kNoRegister;
409}
410
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100411bool LiveInterval::SameRegisterKind(Location other) const {
412 return IsFloatingPoint()
413 ? other.IsFpuRegister()
414 : other.IsRegister();
415}
416
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100417bool LiveInterval::NeedsTwoSpillSlots() const {
418 return type_ == Primitive::kPrimLong || type_ == Primitive::kPrimDouble;
419}
420
421Location LiveInterval::ToLocation() const {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000422 DCHECK(!IsHighInterval());
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100423 if (HasRegister()) {
Nicolas Geoffray840e5462015-01-07 16:01:24 +0000424 if (IsFloatingPoint()) {
425 if (HasHighInterval()) {
426 return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
427 } else {
428 return Location::FpuRegisterLocation(GetRegister());
429 }
430 } else {
431 if (HasHighInterval()) {
432 return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
433 } else {
434 return Location::RegisterLocation(GetRegister());
435 }
436 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100437 } else {
438 HInstruction* defined_by = GetParent()->GetDefinedBy();
439 if (defined_by->IsConstant()) {
440 return defined_by->GetLocations()->Out();
441 } else if (GetParent()->HasSpillSlot()) {
442 if (NeedsTwoSpillSlots()) {
443 return Location::DoubleStackSlot(GetParent()->GetSpillSlot());
444 } else {
445 return Location::StackSlot(GetParent()->GetSpillSlot());
446 }
447 } else {
448 return Location();
449 }
450 }
451}
452
453Location LiveInterval::GetLocationAt(size_t position) const {
454 return GetIntervalAt(position).ToLocation();
455}
456
457const LiveInterval& LiveInterval::GetIntervalAt(size_t position) const {
458 const LiveInterval* current = this;
459 while (!current->Covers(position)) {
460 current = current->GetNextSibling();
461 DCHECK(current != nullptr);
462 }
463 return *current;
464}
465
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100466} // namespace art