blob: 17841685b1de78177bb7e20625d1f8099d1a605f [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.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010072 GrowableArray<uint32_t> forward_predecessors(graph_->GetArena(), graph_->GetBlocks().Size());
73 forward_predecessors.SetSize(graph_->GetBlocks().Size());
74 for (HReversePostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
David Brazdil46e2a392015-03-16 17:31:52 +000075 HBasicBlock* block = it.Current();
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000076 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.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010089 GrowableArray<HBasicBlock*> worklist(graph_->GetArena(), 1);
90 worklist.Add(graph_->GetEntryBlock());
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000091 do {
92 HBasicBlock* current = worklist.Pop();
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +010093 graph_->linear_order_.Add(current);
Nicolas Geoffraya8eed3a2014-11-24 17:47:10 +000094 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 Geoffray0d9f17d2015-04-15 14:17:44 +0100118 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100119 HBasicBlock* block = it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100120 block->SetLifetimeStart(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100121
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800122 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
123 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000124 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100125 LocationSummary* locations = current->GetLocations();
126 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100127 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100128 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100129 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100130 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100131 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100132 current->SetLifetimePosition(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100133 }
Nicolas Geoffrayec7e4722014-06-06 11:24:33 +0100134 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100135
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100136 // Add a null marker to notify we are starting a block.
137 instructions_from_lifetime_position_.Add(nullptr);
138
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800139 for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
140 inst_it.Advance()) {
141 HInstruction* current = inst_it.Current();
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000142 codegen_->AllocateLocations(current);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100143 LocationSummary* locations = current->GetLocations();
144 if (locations != nullptr && locations->Out().IsValid()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100145 instructions_from_ssa_index_.Add(current);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100146 current->SetSsaIndex(ssa_index++);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100147 current->SetLiveInterval(
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100148 LiveInterval::MakeInterval(graph_->GetArena(), current->GetType(), current));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100149 }
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100150 instructions_from_lifetime_position_.Add(current);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100151 current->SetLifetimePosition(lifetime_position);
152 lifetime_position += 2;
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100153 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100154
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100155 block->SetLifetimeEnd(lifetime_position);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100156 }
157 number_of_ssa_values_ = ssa_index;
158}
159
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100160void SsaLivenessAnalysis::ComputeLiveness() {
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100161 for (HLinearOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100162 HBasicBlock* block = it.Current();
163 block_infos_.Put(
164 block->GetBlockId(),
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100165 new (graph_->GetArena()) BlockInfo(graph_->GetArena(), *block, number_of_ssa_values_));
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100166 }
167
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100168 // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
169 // This method does not handle backward branches for the sets, therefore live_in
170 // and live_out sets are not yet correct.
171 ComputeLiveRanges();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100172
173 // Do a fixed point calculation to take into account backward branches,
174 // that will update live_in of loop headers, and therefore live_out and live_in
175 // of blocks in the loop.
176 ComputeLiveInAndLiveOutSets();
177}
178
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100179void SsaLivenessAnalysis::ComputeLiveRanges() {
180 // Do a post order visit, adding inputs of instructions live in the block where
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100181 // that instruction is defined, and killing instructions that are being visited.
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100182 for (HLinearPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100183 HBasicBlock* block = it.Current();
184
185 BitVector* kill = GetKillSet(*block);
186 BitVector* live_in = GetLiveInSet(*block);
187
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100188 // Set phi inputs of successors of this block corresponding to this block
189 // as live_in.
190 for (size_t i = 0, e = block->GetSuccessors().Size(); i < e; ++i) {
191 HBasicBlock* successor = block->GetSuccessors().Get(i);
192 live_in->Union(GetLiveInSet(*successor));
193 size_t phi_input_index = successor->GetPredecessorIndexOf(block);
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800194 for (HInstructionIterator inst_it(successor->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
195 HInstruction* phi = inst_it.Current();
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100196 HInstruction* input = phi->InputAt(phi_input_index);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100197 input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100198 // A phi input whose last user is the phi dies at the end of the predecessor block,
199 // and not at the phi's lifetime position.
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100200 live_in->SetBit(input->GetSsaIndex());
201 }
202 }
203
204 // Add a range that covers this block to all instructions live_in because of successors.
Nicolas Geoffray8ddb00c2014-09-29 12:00:40 +0100205 // Instructions defined in this block will have their start of the range adjusted.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100206 for (uint32_t idx : live_in->Indexes()) {
207 HInstruction* current = instructions_from_ssa_index_.Get(idx);
208 current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100209 }
210
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800211 for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
212 back_it.Advance()) {
213 HInstruction* current = back_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100214 if (current->HasSsaIndex()) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100215 // Kill the instruction and shorten its interval.
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100216 kill->SetBit(current->GetSsaIndex());
217 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100218 current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100219 }
220
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000221 // Process the environment first, because we know their uses come after
222 // or at the same liveness position of inputs.
223 if (current->HasEnvironment()) {
224 // Handle environment uses. See statements (b) and (c) of the
225 // SsaLivenessAnalysis.
226 HEnvironment* environment = current->GetEnvironment();
227 for (size_t i = 0, e = environment->Size(); i < e; ++i) {
228 HInstruction* instruction = environment->GetInstructionAt(i);
229 bool should_be_live = ShouldBeLiveForEnvironment(instruction);
230 if (should_be_live) {
231 DCHECK(instruction->HasSsaIndex());
232 live_in->SetBit(instruction->GetSsaIndex());
233 }
234 if (instruction != nullptr) {
235 instruction->GetLiveInterval()->AddUse(
236 current, i, /* is_environment */ true, should_be_live);
237 }
238 }
239 }
240
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100241 // All inputs of an instruction must be live.
242 for (size_t i = 0, e = current->InputCount(); i < e; ++i) {
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100243 HInstruction* input = current->InputAt(i);
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100244 // Some instructions 'inline' their inputs, that is they do not need
245 // to be materialized.
246 if (input->HasSsaIndex()) {
247 live_in->SetBit(input->GetSsaIndex());
Nicolas Geoffrayd8126be2015-03-27 10:22:41 +0000248 input->GetLiveInterval()->AddUse(current, i, /* is_environment */ false);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100249 }
250 }
251 }
252
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100253 // Kill phis defined in this block.
Andreas Gampe277ccbd2014-11-03 21:36:10 -0800254 for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
255 HInstruction* current = inst_it.Current();
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100256 if (current->HasSsaIndex()) {
257 kill->SetBit(current->GetSsaIndex());
258 live_in->ClearBit(current->GetSsaIndex());
Nicolas Geoffray31d76b42014-06-09 15:02:22 +0100259 LiveInterval* interval = current->GetLiveInterval();
260 DCHECK((interval->GetFirstRange() == nullptr)
261 || (interval->GetStart() == current->GetLifetimePosition()));
262 interval->SetFrom(current->GetLifetimePosition());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100263 }
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100264 }
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100265
Nicolas Geoffrayddb311f2014-05-16 09:28:54 +0100266 if (block->IsLoopHeader()) {
267 HBasicBlock* back_edge = block->GetLoopInformation()->GetBackEdges().Get(0);
268 // For all live_in instructions at the loop header, we need to create a range
269 // that covers the full loop.
Vladimir Markoa5b8fde2014-05-23 15:16:44 +0100270 for (uint32_t idx : live_in->Indexes()) {
271 HInstruction* current = instructions_from_ssa_index_.Get(idx);
272 current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(),
273 back_edge->GetLifetimeEnd());
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100274 }
275 }
276 }
277}
278
279void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
280 bool changed;
281 do {
282 changed = false;
283
Nicolas Geoffray0d9f17d2015-04-15 14:17:44 +0100284 for (HPostOrderIterator it(*graph_); !it.Done(); it.Advance()) {
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100285 const HBasicBlock& block = *it.Current();
286
287 // The live_in set depends on the kill set (which does not
288 // change in this loop), and the live_out set. If the live_out
289 // set does not change, there is no need to update the live_in set.
290 if (UpdateLiveOut(block) && UpdateLiveIn(block)) {
291 changed = true;
292 }
293 }
294 } while (changed);
295}
296
297bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
298 BitVector* live_out = GetLiveOutSet(block);
299 bool changed = false;
300 // The live_out set of a block is the union of live_in sets of its successors.
Nicolas Geoffray622d9c32014-05-12 16:11:02 +0100301 for (size_t i = 0, e = block.GetSuccessors().Size(); i < e; ++i) {
302 HBasicBlock* successor = block.GetSuccessors().Get(i);
Nicolas Geoffray804d0932014-05-02 08:46:00 +0100303 if (live_out->Union(GetLiveInSet(*successor))) {
304 changed = true;
305 }
306 }
307 return changed;
308}
309
310
311bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
312 BitVector* live_out = GetLiveOutSet(block);
313 BitVector* kill = GetKillSet(block);
314 BitVector* live_in = GetLiveInSet(block);
315 // If live_out is updated (because of backward branches), we need to make
316 // sure instructions in live_out are also in live_in, unless they are killed
317 // by this block.
318 return live_in->UnionIfNotIn(live_out, kill);
319}
320
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000321static int RegisterOrLowRegister(Location location) {
322 return location.IsPair() ? location.low() : location.reg();
323}
324
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100325int LiveInterval::FindFirstRegisterHint(size_t* free_until,
326 const SsaLivenessAnalysis& liveness) const {
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000327 DCHECK(!IsHighInterval());
Nicolas Geoffrayf01d3442015-03-27 17:15:49 +0000328 if (IsTemp()) return kNoRegister;
329
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100330 if (GetParent() == this && defined_by_ != nullptr) {
331 // This is the first interval for the instruction. Try to find
332 // a register based on its definition.
333 DCHECK_EQ(defined_by_->GetLiveInterval(), this);
334 int hint = FindHintAtDefinition();
335 if (hint != kNoRegister && free_until[hint] > GetStart()) {
336 return hint;
337 }
338 }
339
Nicolas Geoffrayfbda5f32015-04-29 14:16:00 +0100340 if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) {
341 // If the start of this interval is at a block boundary, we look at the
342 // location of the interval in blocks preceding the block this interval
343 // starts at. If one location is a register we return it as a hint. This
344 // will avoid a move between the two blocks.
345 HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2);
346 for (size_t i = 0; i < block->GetPredecessors().Size(); ++i) {
347 size_t position = block->GetPredecessors().Get(i)->GetLifetimeEnd() - 1;
348 // We know positions above GetStart() do not have a location yet.
349 if (position < GetStart()) {
350 LiveInterval* existing = GetParent()->GetSiblingAt(position);
351 if (existing != nullptr
352 && existing->HasRegister()
353 && (free_until[existing->GetRegister()] > GetStart())) {
354 return existing->GetRegister();
355 }
356 }
357 }
358 }
359
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100360 UsePosition* use = first_use_;
361 size_t start = GetStart();
362 size_t end = GetEnd();
363 while (use != nullptr && use->GetPosition() <= end) {
364 size_t use_position = use->GetPosition();
Nicolas Geoffray57902602015-04-21 14:28:41 +0100365 if (use_position >= start && !use->IsSynthesized()) {
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100366 HInstruction* user = use->GetUser();
367 size_t input_index = use->GetInputIndex();
368 if (user->IsPhi()) {
369 // If the phi has a register, try to use the same.
370 Location phi_location = user->GetLiveInterval()->ToLocation();
Nicolas Geoffrayda02afe2015-02-11 02:29:42 +0000371 if (phi_location.IsRegisterKind()) {
372 DCHECK(SameRegisterKind(phi_location));
373 int reg = RegisterOrLowRegister(phi_location);
374 if (free_until[reg] >= use_position) {
375 return reg;
376 }
Nicolas Geoffray01ef3452014-10-01 11:32:17 +0100377 }
378 const GrowableArray<HBasicBlock*>& predecessors = user->GetBlock()->GetPredecessors();
379 // If the instruction dies at the phi assignment, we can try having the
380 // same register.
381 if (end == predecessors.Get(input_index)->GetLifetimeEnd()) {
382 for (size_t i = 0, e = user->InputCount(); i < e; ++i) {
383 if (i == input_index) {
384 continue;
385 }
386 HInstruction* input = user->InputAt(i);
387 Location location = input->GetLiveInterval()->GetLocationAt(
388 predecessors.Get(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.
422 const GrowableArray<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
423 for (size_t i = 0, e = defined_by_->InputCount(); i < e; ++i) {
424 HInstruction* input = defined_by_->InputAt(i);
425 size_t end = predecessors.Get(i)->GetLifetimeEnd();
David Brazdil241a4862015-04-16 17:59:03 +0100426 LiveInterval* input_interval = input->GetLiveInterval()->GetSiblingAt(end - 1);
427 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