1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2014 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include "ssa_liveness_analysis.h"
18*795d594fSAndroid Build Coastguard Worker
19*795d594fSAndroid Build Coastguard Worker #include "base/bit_vector-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
21*795d594fSAndroid Build Coastguard Worker #include "linear_order.h"
22*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
25*795d594fSAndroid Build Coastguard Worker
Analyze()26*795d594fSAndroid Build Coastguard Worker void SsaLivenessAnalysis::Analyze() {
27*795d594fSAndroid Build Coastguard Worker // Compute the linear order directly in the graph's data structure
28*795d594fSAndroid Build Coastguard Worker // (there are no more following graph mutations).
29*795d594fSAndroid Build Coastguard Worker LinearizeGraph(graph_, &graph_->linear_order_);
30*795d594fSAndroid Build Coastguard Worker
31*795d594fSAndroid Build Coastguard Worker // Liveness analysis.
32*795d594fSAndroid Build Coastguard Worker NumberInstructions();
33*795d594fSAndroid Build Coastguard Worker ComputeLiveness();
34*795d594fSAndroid Build Coastguard Worker }
35*795d594fSAndroid Build Coastguard Worker
NumberInstructions()36*795d594fSAndroid Build Coastguard Worker void SsaLivenessAnalysis::NumberInstructions() {
37*795d594fSAndroid Build Coastguard Worker int ssa_index = 0;
38*795d594fSAndroid Build Coastguard Worker size_t lifetime_position = 0;
39*795d594fSAndroid Build Coastguard Worker // Each instruction gets a lifetime position, and a block gets a lifetime
40*795d594fSAndroid Build Coastguard Worker // start and end position. Non-phi instructions have a distinct lifetime position than
41*795d594fSAndroid Build Coastguard Worker // the block they are in. Phi instructions have the lifetime start of their block as
42*795d594fSAndroid Build Coastguard Worker // lifetime position.
43*795d594fSAndroid Build Coastguard Worker //
44*795d594fSAndroid Build Coastguard Worker // Because the register allocator will insert moves in the graph, we need
45*795d594fSAndroid Build Coastguard Worker // to differentiate between the start and end of an instruction. Adding 2 to
46*795d594fSAndroid Build Coastguard Worker // the lifetime position for each instruction ensures the start of an
47*795d594fSAndroid Build Coastguard Worker // instruction is different than the end of the previous instruction.
48*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* block : graph_->GetLinearOrder()) {
49*795d594fSAndroid Build Coastguard Worker block->SetLifetimeStart(lifetime_position);
50*795d594fSAndroid Build Coastguard Worker
51*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
52*795d594fSAndroid Build Coastguard Worker HInstruction* current = inst_it.Current();
53*795d594fSAndroid Build Coastguard Worker codegen_->AllocateLocations(current);
54*795d594fSAndroid Build Coastguard Worker LocationSummary* locations = current->GetLocations();
55*795d594fSAndroid Build Coastguard Worker if (locations != nullptr && locations->Out().IsValid()) {
56*795d594fSAndroid Build Coastguard Worker instructions_from_ssa_index_.push_back(current);
57*795d594fSAndroid Build Coastguard Worker current->SetSsaIndex(ssa_index++);
58*795d594fSAndroid Build Coastguard Worker current->SetLiveInterval(
59*795d594fSAndroid Build Coastguard Worker LiveInterval::MakeInterval(allocator_, current->GetType(), current));
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker current->SetLifetimePosition(lifetime_position);
62*795d594fSAndroid Build Coastguard Worker }
63*795d594fSAndroid Build Coastguard Worker lifetime_position += 2;
64*795d594fSAndroid Build Coastguard Worker
65*795d594fSAndroid Build Coastguard Worker // Add a null marker to notify we are starting a block.
66*795d594fSAndroid Build Coastguard Worker instructions_from_lifetime_position_.push_back(nullptr);
67*795d594fSAndroid Build Coastguard Worker
68*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator inst_it(block->GetInstructions()); !inst_it.Done();
69*795d594fSAndroid Build Coastguard Worker inst_it.Advance()) {
70*795d594fSAndroid Build Coastguard Worker HInstruction* current = inst_it.Current();
71*795d594fSAndroid Build Coastguard Worker codegen_->AllocateLocations(current);
72*795d594fSAndroid Build Coastguard Worker LocationSummary* locations = current->GetLocations();
73*795d594fSAndroid Build Coastguard Worker if (locations != nullptr && locations->Out().IsValid()) {
74*795d594fSAndroid Build Coastguard Worker instructions_from_ssa_index_.push_back(current);
75*795d594fSAndroid Build Coastguard Worker current->SetSsaIndex(ssa_index++);
76*795d594fSAndroid Build Coastguard Worker current->SetLiveInterval(
77*795d594fSAndroid Build Coastguard Worker LiveInterval::MakeInterval(allocator_, current->GetType(), current));
78*795d594fSAndroid Build Coastguard Worker }
79*795d594fSAndroid Build Coastguard Worker instructions_from_lifetime_position_.push_back(current);
80*795d594fSAndroid Build Coastguard Worker current->SetLifetimePosition(lifetime_position);
81*795d594fSAndroid Build Coastguard Worker lifetime_position += 2;
82*795d594fSAndroid Build Coastguard Worker }
83*795d594fSAndroid Build Coastguard Worker
84*795d594fSAndroid Build Coastguard Worker block->SetLifetimeEnd(lifetime_position);
85*795d594fSAndroid Build Coastguard Worker }
86*795d594fSAndroid Build Coastguard Worker number_of_ssa_values_ = ssa_index;
87*795d594fSAndroid Build Coastguard Worker }
88*795d594fSAndroid Build Coastguard Worker
ComputeLiveness()89*795d594fSAndroid Build Coastguard Worker void SsaLivenessAnalysis::ComputeLiveness() {
90*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* block : graph_->GetLinearOrder()) {
91*795d594fSAndroid Build Coastguard Worker block_infos_[block->GetBlockId()] =
92*795d594fSAndroid Build Coastguard Worker new (allocator_) BlockInfo(allocator_, *block, number_of_ssa_values_);
93*795d594fSAndroid Build Coastguard Worker }
94*795d594fSAndroid Build Coastguard Worker
95*795d594fSAndroid Build Coastguard Worker // Compute the live ranges, as well as the initial live_in, live_out, and kill sets.
96*795d594fSAndroid Build Coastguard Worker // This method does not handle backward branches for the sets, therefore live_in
97*795d594fSAndroid Build Coastguard Worker // and live_out sets are not yet correct.
98*795d594fSAndroid Build Coastguard Worker ComputeLiveRanges();
99*795d594fSAndroid Build Coastguard Worker
100*795d594fSAndroid Build Coastguard Worker // Do a fixed point calculation to take into account backward branches,
101*795d594fSAndroid Build Coastguard Worker // that will update live_in of loop headers, and therefore live_out and live_in
102*795d594fSAndroid Build Coastguard Worker // of blocks in the loop.
103*795d594fSAndroid Build Coastguard Worker ComputeLiveInAndLiveOutSets();
104*795d594fSAndroid Build Coastguard Worker }
105*795d594fSAndroid Build Coastguard Worker
RecursivelyProcessInputs(HInstruction * current,HInstruction * actual_user,BitVector * live_in)106*795d594fSAndroid Build Coastguard Worker void SsaLivenessAnalysis::RecursivelyProcessInputs(HInstruction* current,
107*795d594fSAndroid Build Coastguard Worker HInstruction* actual_user,
108*795d594fSAndroid Build Coastguard Worker BitVector* live_in) {
109*795d594fSAndroid Build Coastguard Worker HInputsRef inputs = current->GetInputs();
110*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < inputs.size(); ++i) {
111*795d594fSAndroid Build Coastguard Worker HInstruction* input = inputs[i];
112*795d594fSAndroid Build Coastguard Worker bool has_in_location = current->GetLocations()->InAt(i).IsValid();
113*795d594fSAndroid Build Coastguard Worker bool has_out_location = input->GetLocations()->Out().IsValid();
114*795d594fSAndroid Build Coastguard Worker
115*795d594fSAndroid Build Coastguard Worker if (has_in_location) {
116*795d594fSAndroid Build Coastguard Worker DCHECK(has_out_location)
117*795d594fSAndroid Build Coastguard Worker << "Instruction " << current->DebugName() << current->GetId()
118*795d594fSAndroid Build Coastguard Worker << " expects an input value at index " << i << " but "
119*795d594fSAndroid Build Coastguard Worker << input->DebugName() << input->GetId() << " does not produce one.";
120*795d594fSAndroid Build Coastguard Worker DCHECK(input->HasSsaIndex());
121*795d594fSAndroid Build Coastguard Worker // `input` generates a result used by `current`. Add use and update
122*795d594fSAndroid Build Coastguard Worker // the live-in set.
123*795d594fSAndroid Build Coastguard Worker input->GetLiveInterval()->AddUse(current, /* environment= */ nullptr, i, actual_user);
124*795d594fSAndroid Build Coastguard Worker live_in->SetBit(input->GetSsaIndex());
125*795d594fSAndroid Build Coastguard Worker } else if (has_out_location) {
126*795d594fSAndroid Build Coastguard Worker // `input` generates a result but it is not used by `current`.
127*795d594fSAndroid Build Coastguard Worker } else {
128*795d594fSAndroid Build Coastguard Worker // `input` is inlined into `current`. Walk over its inputs and record
129*795d594fSAndroid Build Coastguard Worker // uses at `current`.
130*795d594fSAndroid Build Coastguard Worker DCHECK(input->IsEmittedAtUseSite());
131*795d594fSAndroid Build Coastguard Worker // Check that the inlined input is not a phi. Recursing on loop phis could
132*795d594fSAndroid Build Coastguard Worker // lead to an infinite loop.
133*795d594fSAndroid Build Coastguard Worker DCHECK(!input->IsPhi());
134*795d594fSAndroid Build Coastguard Worker DCHECK(!input->HasEnvironment());
135*795d594fSAndroid Build Coastguard Worker RecursivelyProcessInputs(input, actual_user, live_in);
136*795d594fSAndroid Build Coastguard Worker }
137*795d594fSAndroid Build Coastguard Worker }
138*795d594fSAndroid Build Coastguard Worker }
139*795d594fSAndroid Build Coastguard Worker
ProcessEnvironment(HInstruction * current,HInstruction * actual_user,BitVector * live_in)140*795d594fSAndroid Build Coastguard Worker void SsaLivenessAnalysis::ProcessEnvironment(HInstruction* current,
141*795d594fSAndroid Build Coastguard Worker HInstruction* actual_user,
142*795d594fSAndroid Build Coastguard Worker BitVector* live_in) {
143*795d594fSAndroid Build Coastguard Worker for (HEnvironment* environment = current->GetEnvironment();
144*795d594fSAndroid Build Coastguard Worker environment != nullptr;
145*795d594fSAndroid Build Coastguard Worker environment = environment->GetParent()) {
146*795d594fSAndroid Build Coastguard Worker // Handle environment uses. See statements (b) and (c) of the
147*795d594fSAndroid Build Coastguard Worker // SsaLivenessAnalysis.
148*795d594fSAndroid Build Coastguard Worker for (size_t i = 0, e = environment->Size(); i < e; ++i) {
149*795d594fSAndroid Build Coastguard Worker HInstruction* instruction = environment->GetInstructionAt(i);
150*795d594fSAndroid Build Coastguard Worker if (instruction == nullptr) {
151*795d594fSAndroid Build Coastguard Worker continue;
152*795d594fSAndroid Build Coastguard Worker }
153*795d594fSAndroid Build Coastguard Worker bool should_be_live = ShouldBeLiveForEnvironment(current, instruction);
154*795d594fSAndroid Build Coastguard Worker // If this environment use does not keep the instruction live, it does not
155*795d594fSAndroid Build Coastguard Worker // affect the live range of that instruction.
156*795d594fSAndroid Build Coastguard Worker if (should_be_live) {
157*795d594fSAndroid Build Coastguard Worker CHECK(instruction->HasSsaIndex()) << instruction->DebugName();
158*795d594fSAndroid Build Coastguard Worker live_in->SetBit(instruction->GetSsaIndex());
159*795d594fSAndroid Build Coastguard Worker instruction->GetLiveInterval()->AddUse(current,
160*795d594fSAndroid Build Coastguard Worker environment,
161*795d594fSAndroid Build Coastguard Worker i,
162*795d594fSAndroid Build Coastguard Worker actual_user);
163*795d594fSAndroid Build Coastguard Worker }
164*795d594fSAndroid Build Coastguard Worker }
165*795d594fSAndroid Build Coastguard Worker }
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker
ComputeLiveRanges()168*795d594fSAndroid Build Coastguard Worker void SsaLivenessAnalysis::ComputeLiveRanges() {
169*795d594fSAndroid Build Coastguard Worker // Do a post order visit, adding inputs of instructions live in the block where
170*795d594fSAndroid Build Coastguard Worker // that instruction is defined, and killing instructions that are being visited.
171*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* block : ReverseRange(graph_->GetLinearOrder())) {
172*795d594fSAndroid Build Coastguard Worker BitVector* kill = GetKillSet(*block);
173*795d594fSAndroid Build Coastguard Worker BitVector* live_in = GetLiveInSet(*block);
174*795d594fSAndroid Build Coastguard Worker
175*795d594fSAndroid Build Coastguard Worker // Set phi inputs of successors of this block corresponding to this block
176*795d594fSAndroid Build Coastguard Worker // as live_in.
177*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* successor : block->GetSuccessors()) {
178*795d594fSAndroid Build Coastguard Worker live_in->Union(GetLiveInSet(*successor));
179*795d594fSAndroid Build Coastguard Worker if (successor->IsCatchBlock()) {
180*795d594fSAndroid Build Coastguard Worker // Inputs of catch phis will be kept alive through their environment
181*795d594fSAndroid Build Coastguard Worker // uses, allowing the runtime to copy their values to the corresponding
182*795d594fSAndroid Build Coastguard Worker // catch phi spill slots when an exception is thrown.
183*795d594fSAndroid Build Coastguard Worker // The only instructions which may not be recorded in the environments
184*795d594fSAndroid Build Coastguard Worker // are constants created by the SSA builder as typed equivalents of
185*795d594fSAndroid Build Coastguard Worker // untyped constants from the bytecode, or phis with only such constants
186*795d594fSAndroid Build Coastguard Worker // as inputs (verified by GraphChecker). Their raw binary value must
187*795d594fSAndroid Build Coastguard Worker // therefore be the same and we only need to keep alive one.
188*795d594fSAndroid Build Coastguard Worker } else {
189*795d594fSAndroid Build Coastguard Worker size_t phi_input_index = successor->GetPredecessorIndexOf(block);
190*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator phi_it(successor->GetPhis()); !phi_it.Done(); phi_it.Advance()) {
191*795d594fSAndroid Build Coastguard Worker HInstruction* phi = phi_it.Current();
192*795d594fSAndroid Build Coastguard Worker HInstruction* input = phi->InputAt(phi_input_index);
193*795d594fSAndroid Build Coastguard Worker input->GetLiveInterval()->AddPhiUse(phi, phi_input_index, block);
194*795d594fSAndroid Build Coastguard Worker // A phi input whose last user is the phi dies at the end of the predecessor block,
195*795d594fSAndroid Build Coastguard Worker // and not at the phi's lifetime position.
196*795d594fSAndroid Build Coastguard Worker live_in->SetBit(input->GetSsaIndex());
197*795d594fSAndroid Build Coastguard Worker }
198*795d594fSAndroid Build Coastguard Worker }
199*795d594fSAndroid Build Coastguard Worker }
200*795d594fSAndroid Build Coastguard Worker
201*795d594fSAndroid Build Coastguard Worker // Add a range that covers this block to all instructions live_in because of successors.
202*795d594fSAndroid Build Coastguard Worker // Instructions defined in this block will have their start of the range adjusted.
203*795d594fSAndroid Build Coastguard Worker for (uint32_t idx : live_in->Indexes()) {
204*795d594fSAndroid Build Coastguard Worker HInstruction* current = GetInstructionFromSsaIndex(idx);
205*795d594fSAndroid Build Coastguard Worker current->GetLiveInterval()->AddRange(block->GetLifetimeStart(), block->GetLifetimeEnd());
206*795d594fSAndroid Build Coastguard Worker }
207*795d594fSAndroid Build Coastguard Worker
208*795d594fSAndroid Build Coastguard Worker for (HBackwardInstructionIterator back_it(block->GetInstructions()); !back_it.Done();
209*795d594fSAndroid Build Coastguard Worker back_it.Advance()) {
210*795d594fSAndroid Build Coastguard Worker HInstruction* current = back_it.Current();
211*795d594fSAndroid Build Coastguard Worker if (current->HasSsaIndex()) {
212*795d594fSAndroid Build Coastguard Worker // Kill the instruction and shorten its interval.
213*795d594fSAndroid Build Coastguard Worker kill->SetBit(current->GetSsaIndex());
214*795d594fSAndroid Build Coastguard Worker live_in->ClearBit(current->GetSsaIndex());
215*795d594fSAndroid Build Coastguard Worker current->GetLiveInterval()->SetFrom(current->GetLifetimePosition());
216*795d594fSAndroid Build Coastguard Worker }
217*795d594fSAndroid Build Coastguard Worker
218*795d594fSAndroid Build Coastguard Worker // Process inputs of instructions.
219*795d594fSAndroid Build Coastguard Worker if (current->IsEmittedAtUseSite()) {
220*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
221*795d594fSAndroid Build Coastguard Worker DCHECK(!current->GetLocations()->Out().IsValid());
222*795d594fSAndroid Build Coastguard Worker for (const HUseListNode<HInstruction*>& use : current->GetUses()) {
223*795d594fSAndroid Build Coastguard Worker HInstruction* user = use.GetUser();
224*795d594fSAndroid Build Coastguard Worker size_t index = use.GetIndex();
225*795d594fSAndroid Build Coastguard Worker DCHECK(!user->GetLocations()->InAt(index).IsValid());
226*795d594fSAndroid Build Coastguard Worker }
227*795d594fSAndroid Build Coastguard Worker DCHECK(!current->HasEnvironmentUses());
228*795d594fSAndroid Build Coastguard Worker }
229*795d594fSAndroid Build Coastguard Worker } else {
230*795d594fSAndroid Build Coastguard Worker // Process the environment first, because we know their uses come after
231*795d594fSAndroid Build Coastguard Worker // or at the same liveness position of inputs.
232*795d594fSAndroid Build Coastguard Worker ProcessEnvironment(current, current, live_in);
233*795d594fSAndroid Build Coastguard Worker
234*795d594fSAndroid Build Coastguard Worker // Special case implicit null checks. We want their environment uses to be
235*795d594fSAndroid Build Coastguard Worker // emitted at the instruction doing the actual null check.
236*795d594fSAndroid Build Coastguard Worker HNullCheck* check = current->GetImplicitNullCheck();
237*795d594fSAndroid Build Coastguard Worker if (check != nullptr) {
238*795d594fSAndroid Build Coastguard Worker ProcessEnvironment(check, current, live_in);
239*795d594fSAndroid Build Coastguard Worker }
240*795d594fSAndroid Build Coastguard Worker RecursivelyProcessInputs(current, current, live_in);
241*795d594fSAndroid Build Coastguard Worker }
242*795d594fSAndroid Build Coastguard Worker }
243*795d594fSAndroid Build Coastguard Worker
244*795d594fSAndroid Build Coastguard Worker // Kill phis defined in this block.
245*795d594fSAndroid Build Coastguard Worker for (HInstructionIterator inst_it(block->GetPhis()); !inst_it.Done(); inst_it.Advance()) {
246*795d594fSAndroid Build Coastguard Worker HInstruction* current = inst_it.Current();
247*795d594fSAndroid Build Coastguard Worker if (current->HasSsaIndex()) {
248*795d594fSAndroid Build Coastguard Worker kill->SetBit(current->GetSsaIndex());
249*795d594fSAndroid Build Coastguard Worker live_in->ClearBit(current->GetSsaIndex());
250*795d594fSAndroid Build Coastguard Worker LiveInterval* interval = current->GetLiveInterval();
251*795d594fSAndroid Build Coastguard Worker DCHECK((interval->GetFirstRange() == nullptr)
252*795d594fSAndroid Build Coastguard Worker || (interval->GetStart() == current->GetLifetimePosition()));
253*795d594fSAndroid Build Coastguard Worker interval->SetFrom(current->GetLifetimePosition());
254*795d594fSAndroid Build Coastguard Worker }
255*795d594fSAndroid Build Coastguard Worker }
256*795d594fSAndroid Build Coastguard Worker
257*795d594fSAndroid Build Coastguard Worker if (block->IsLoopHeader()) {
258*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
259*795d594fSAndroid Build Coastguard Worker CheckNoLiveInIrreducibleLoop(*block);
260*795d594fSAndroid Build Coastguard Worker }
261*795d594fSAndroid Build Coastguard Worker size_t last_position = block->GetLoopInformation()->GetLifetimeEnd();
262*795d594fSAndroid Build Coastguard Worker // For all live_in instructions at the loop header, we need to create a range
263*795d594fSAndroid Build Coastguard Worker // that covers the full loop.
264*795d594fSAndroid Build Coastguard Worker for (uint32_t idx : live_in->Indexes()) {
265*795d594fSAndroid Build Coastguard Worker HInstruction* current = GetInstructionFromSsaIndex(idx);
266*795d594fSAndroid Build Coastguard Worker current->GetLiveInterval()->AddLoopRange(block->GetLifetimeStart(), last_position);
267*795d594fSAndroid Build Coastguard Worker }
268*795d594fSAndroid Build Coastguard Worker }
269*795d594fSAndroid Build Coastguard Worker }
270*795d594fSAndroid Build Coastguard Worker }
271*795d594fSAndroid Build Coastguard Worker
ComputeLiveInAndLiveOutSets()272*795d594fSAndroid Build Coastguard Worker void SsaLivenessAnalysis::ComputeLiveInAndLiveOutSets() {
273*795d594fSAndroid Build Coastguard Worker bool changed;
274*795d594fSAndroid Build Coastguard Worker do {
275*795d594fSAndroid Build Coastguard Worker changed = false;
276*795d594fSAndroid Build Coastguard Worker
277*795d594fSAndroid Build Coastguard Worker for (const HBasicBlock* block : graph_->GetPostOrder()) {
278*795d594fSAndroid Build Coastguard Worker // The live_in set depends on the kill set (which does not
279*795d594fSAndroid Build Coastguard Worker // change in this loop), and the live_out set. If the live_out
280*795d594fSAndroid Build Coastguard Worker // set does not change, there is no need to update the live_in set.
281*795d594fSAndroid Build Coastguard Worker if (UpdateLiveOut(*block) && UpdateLiveIn(*block)) {
282*795d594fSAndroid Build Coastguard Worker if (kIsDebugBuild) {
283*795d594fSAndroid Build Coastguard Worker CheckNoLiveInIrreducibleLoop(*block);
284*795d594fSAndroid Build Coastguard Worker }
285*795d594fSAndroid Build Coastguard Worker changed = true;
286*795d594fSAndroid Build Coastguard Worker }
287*795d594fSAndroid Build Coastguard Worker }
288*795d594fSAndroid Build Coastguard Worker } while (changed);
289*795d594fSAndroid Build Coastguard Worker }
290*795d594fSAndroid Build Coastguard Worker
UpdateLiveOut(const HBasicBlock & block)291*795d594fSAndroid Build Coastguard Worker bool SsaLivenessAnalysis::UpdateLiveOut(const HBasicBlock& block) {
292*795d594fSAndroid Build Coastguard Worker BitVector* live_out = GetLiveOutSet(block);
293*795d594fSAndroid Build Coastguard Worker bool changed = false;
294*795d594fSAndroid Build Coastguard Worker // The live_out set of a block is the union of live_in sets of its successors.
295*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* successor : block.GetSuccessors()) {
296*795d594fSAndroid Build Coastguard Worker if (live_out->Union(GetLiveInSet(*successor))) {
297*795d594fSAndroid Build Coastguard Worker changed = true;
298*795d594fSAndroid Build Coastguard Worker }
299*795d594fSAndroid Build Coastguard Worker }
300*795d594fSAndroid Build Coastguard Worker return changed;
301*795d594fSAndroid Build Coastguard Worker }
302*795d594fSAndroid Build Coastguard Worker
303*795d594fSAndroid Build Coastguard Worker
UpdateLiveIn(const HBasicBlock & block)304*795d594fSAndroid Build Coastguard Worker bool SsaLivenessAnalysis::UpdateLiveIn(const HBasicBlock& block) {
305*795d594fSAndroid Build Coastguard Worker BitVector* live_out = GetLiveOutSet(block);
306*795d594fSAndroid Build Coastguard Worker BitVector* kill = GetKillSet(block);
307*795d594fSAndroid Build Coastguard Worker BitVector* live_in = GetLiveInSet(block);
308*795d594fSAndroid Build Coastguard Worker // If live_out is updated (because of backward branches), we need to make
309*795d594fSAndroid Build Coastguard Worker // sure instructions in live_out are also in live_in, unless they are killed
310*795d594fSAndroid Build Coastguard Worker // by this block.
311*795d594fSAndroid Build Coastguard Worker return live_in->UnionIfNotIn(live_out, kill);
312*795d594fSAndroid Build Coastguard Worker }
313*795d594fSAndroid Build Coastguard Worker
DumpWithContext(std::ostream & stream,const CodeGenerator & codegen) const314*795d594fSAndroid Build Coastguard Worker void LiveInterval::DumpWithContext(std::ostream& stream,
315*795d594fSAndroid Build Coastguard Worker const CodeGenerator& codegen) const {
316*795d594fSAndroid Build Coastguard Worker Dump(stream);
317*795d594fSAndroid Build Coastguard Worker if (IsFixed()) {
318*795d594fSAndroid Build Coastguard Worker stream << ", register:" << GetRegister() << "(";
319*795d594fSAndroid Build Coastguard Worker if (IsFloatingPoint()) {
320*795d594fSAndroid Build Coastguard Worker codegen.DumpFloatingPointRegister(stream, GetRegister());
321*795d594fSAndroid Build Coastguard Worker } else {
322*795d594fSAndroid Build Coastguard Worker codegen.DumpCoreRegister(stream, GetRegister());
323*795d594fSAndroid Build Coastguard Worker }
324*795d594fSAndroid Build Coastguard Worker stream << ")";
325*795d594fSAndroid Build Coastguard Worker } else {
326*795d594fSAndroid Build Coastguard Worker stream << ", spill slot:" << GetSpillSlot();
327*795d594fSAndroid Build Coastguard Worker }
328*795d594fSAndroid Build Coastguard Worker stream << ", requires_register:" << (GetDefinedBy() != nullptr && RequiresRegister());
329*795d594fSAndroid Build Coastguard Worker if (GetParent()->GetDefinedBy() != nullptr) {
330*795d594fSAndroid Build Coastguard Worker stream << ", defined_by:" << GetParent()->GetDefinedBy()->GetKind();
331*795d594fSAndroid Build Coastguard Worker stream << "(" << GetParent()->GetDefinedBy()->GetLifetimePosition() << ")";
332*795d594fSAndroid Build Coastguard Worker }
333*795d594fSAndroid Build Coastguard Worker }
334*795d594fSAndroid Build Coastguard Worker
RegisterOrLowRegister(Location location)335*795d594fSAndroid Build Coastguard Worker static int RegisterOrLowRegister(Location location) {
336*795d594fSAndroid Build Coastguard Worker return location.IsPair() ? location.low() : location.reg();
337*795d594fSAndroid Build Coastguard Worker }
338*795d594fSAndroid Build Coastguard Worker
FindFirstRegisterHint(size_t * free_until,const SsaLivenessAnalysis & liveness) const339*795d594fSAndroid Build Coastguard Worker int LiveInterval::FindFirstRegisterHint(size_t* free_until,
340*795d594fSAndroid Build Coastguard Worker const SsaLivenessAnalysis& liveness) const {
341*795d594fSAndroid Build Coastguard Worker DCHECK(!IsHighInterval());
342*795d594fSAndroid Build Coastguard Worker if (IsTemp()) return kNoRegister;
343*795d594fSAndroid Build Coastguard Worker
344*795d594fSAndroid Build Coastguard Worker if (GetParent() == this && defined_by_ != nullptr) {
345*795d594fSAndroid Build Coastguard Worker // This is the first interval for the instruction. Try to find
346*795d594fSAndroid Build Coastguard Worker // a register based on its definition.
347*795d594fSAndroid Build Coastguard Worker DCHECK_EQ(defined_by_->GetLiveInterval(), this);
348*795d594fSAndroid Build Coastguard Worker int hint = FindHintAtDefinition();
349*795d594fSAndroid Build Coastguard Worker if (hint != kNoRegister && free_until[hint] > GetStart()) {
350*795d594fSAndroid Build Coastguard Worker return hint;
351*795d594fSAndroid Build Coastguard Worker }
352*795d594fSAndroid Build Coastguard Worker }
353*795d594fSAndroid Build Coastguard Worker
354*795d594fSAndroid Build Coastguard Worker if (IsSplit() && liveness.IsAtBlockBoundary(GetStart() / 2)) {
355*795d594fSAndroid Build Coastguard Worker // If the start of this interval is at a block boundary, we look at the
356*795d594fSAndroid Build Coastguard Worker // location of the interval in blocks preceding the block this interval
357*795d594fSAndroid Build Coastguard Worker // starts at. If one location is a register we return it as a hint. This
358*795d594fSAndroid Build Coastguard Worker // will avoid a move between the two blocks.
359*795d594fSAndroid Build Coastguard Worker HBasicBlock* block = liveness.GetBlockFromPosition(GetStart() / 2);
360*795d594fSAndroid Build Coastguard Worker size_t next_register_use = FirstRegisterUse();
361*795d594fSAndroid Build Coastguard Worker for (HBasicBlock* predecessor : block->GetPredecessors()) {
362*795d594fSAndroid Build Coastguard Worker size_t position = predecessor->GetLifetimeEnd() - 1;
363*795d594fSAndroid Build Coastguard Worker // We know positions above GetStart() do not have a location yet.
364*795d594fSAndroid Build Coastguard Worker if (position < GetStart()) {
365*795d594fSAndroid Build Coastguard Worker LiveInterval* existing = GetParent()->GetSiblingAt(position);
366*795d594fSAndroid Build Coastguard Worker if (existing != nullptr
367*795d594fSAndroid Build Coastguard Worker && existing->HasRegister()
368*795d594fSAndroid Build Coastguard Worker // It's worth using that register if it is available until
369*795d594fSAndroid Build Coastguard Worker // the next use.
370*795d594fSAndroid Build Coastguard Worker && (free_until[existing->GetRegister()] >= next_register_use)) {
371*795d594fSAndroid Build Coastguard Worker return existing->GetRegister();
372*795d594fSAndroid Build Coastguard Worker }
373*795d594fSAndroid Build Coastguard Worker }
374*795d594fSAndroid Build Coastguard Worker }
375*795d594fSAndroid Build Coastguard Worker }
376*795d594fSAndroid Build Coastguard Worker
377*795d594fSAndroid Build Coastguard Worker size_t start = GetStart();
378*795d594fSAndroid Build Coastguard Worker size_t end = GetEnd();
379*795d594fSAndroid Build Coastguard Worker for (const UsePosition& use : GetUses()) {
380*795d594fSAndroid Build Coastguard Worker size_t use_position = use.GetPosition();
381*795d594fSAndroid Build Coastguard Worker if (use_position > end) {
382*795d594fSAndroid Build Coastguard Worker break;
383*795d594fSAndroid Build Coastguard Worker }
384*795d594fSAndroid Build Coastguard Worker if (use_position >= start && !use.IsSynthesized()) {
385*795d594fSAndroid Build Coastguard Worker HInstruction* user = use.GetUser();
386*795d594fSAndroid Build Coastguard Worker size_t input_index = use.GetInputIndex();
387*795d594fSAndroid Build Coastguard Worker if (user->IsPhi()) {
388*795d594fSAndroid Build Coastguard Worker // If the phi has a register, try to use the same.
389*795d594fSAndroid Build Coastguard Worker Location phi_location = user->GetLiveInterval()->ToLocation();
390*795d594fSAndroid Build Coastguard Worker if (phi_location.IsRegisterKind()) {
391*795d594fSAndroid Build Coastguard Worker DCHECK(SameRegisterKind(phi_location));
392*795d594fSAndroid Build Coastguard Worker int reg = RegisterOrLowRegister(phi_location);
393*795d594fSAndroid Build Coastguard Worker if (free_until[reg] >= use_position) {
394*795d594fSAndroid Build Coastguard Worker return reg;
395*795d594fSAndroid Build Coastguard Worker }
396*795d594fSAndroid Build Coastguard Worker }
397*795d594fSAndroid Build Coastguard Worker // If the instruction dies at the phi assignment, we can try having the
398*795d594fSAndroid Build Coastguard Worker // same register.
399*795d594fSAndroid Build Coastguard Worker if (end == user->GetBlock()->GetPredecessors()[input_index]->GetLifetimeEnd()) {
400*795d594fSAndroid Build Coastguard Worker HInputsRef inputs = user->GetInputs();
401*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < inputs.size(); ++i) {
402*795d594fSAndroid Build Coastguard Worker if (i == input_index) {
403*795d594fSAndroid Build Coastguard Worker continue;
404*795d594fSAndroid Build Coastguard Worker }
405*795d594fSAndroid Build Coastguard Worker Location location = inputs[i]->GetLiveInterval()->GetLocationAt(
406*795d594fSAndroid Build Coastguard Worker user->GetBlock()->GetPredecessors()[i]->GetLifetimeEnd() - 1);
407*795d594fSAndroid Build Coastguard Worker if (location.IsRegisterKind()) {
408*795d594fSAndroid Build Coastguard Worker int reg = RegisterOrLowRegister(location);
409*795d594fSAndroid Build Coastguard Worker if (free_until[reg] >= use_position) {
410*795d594fSAndroid Build Coastguard Worker return reg;
411*795d594fSAndroid Build Coastguard Worker }
412*795d594fSAndroid Build Coastguard Worker }
413*795d594fSAndroid Build Coastguard Worker }
414*795d594fSAndroid Build Coastguard Worker }
415*795d594fSAndroid Build Coastguard Worker } else {
416*795d594fSAndroid Build Coastguard Worker // If the instruction is expected in a register, try to use it.
417*795d594fSAndroid Build Coastguard Worker LocationSummary* locations = user->GetLocations();
418*795d594fSAndroid Build Coastguard Worker Location expected = locations->InAt(use.GetInputIndex());
419*795d594fSAndroid Build Coastguard Worker // We use the user's lifetime position - 1 (and not `use_position`) because the
420*795d594fSAndroid Build Coastguard Worker // register is blocked at the beginning of the user.
421*795d594fSAndroid Build Coastguard Worker size_t position = user->GetLifetimePosition() - 1;
422*795d594fSAndroid Build Coastguard Worker if (expected.IsRegisterKind()) {
423*795d594fSAndroid Build Coastguard Worker DCHECK(SameRegisterKind(expected));
424*795d594fSAndroid Build Coastguard Worker int reg = RegisterOrLowRegister(expected);
425*795d594fSAndroid Build Coastguard Worker if (free_until[reg] >= position) {
426*795d594fSAndroid Build Coastguard Worker return reg;
427*795d594fSAndroid Build Coastguard Worker }
428*795d594fSAndroid Build Coastguard Worker }
429*795d594fSAndroid Build Coastguard Worker }
430*795d594fSAndroid Build Coastguard Worker }
431*795d594fSAndroid Build Coastguard Worker }
432*795d594fSAndroid Build Coastguard Worker
433*795d594fSAndroid Build Coastguard Worker return kNoRegister;
434*795d594fSAndroid Build Coastguard Worker }
435*795d594fSAndroid Build Coastguard Worker
FindHintAtDefinition() const436*795d594fSAndroid Build Coastguard Worker int LiveInterval::FindHintAtDefinition() const {
437*795d594fSAndroid Build Coastguard Worker if (defined_by_->IsPhi()) {
438*795d594fSAndroid Build Coastguard Worker // Try to use the same register as one of the inputs.
439*795d594fSAndroid Build Coastguard Worker const ArenaVector<HBasicBlock*>& predecessors = defined_by_->GetBlock()->GetPredecessors();
440*795d594fSAndroid Build Coastguard Worker HInputsRef inputs = defined_by_->GetInputs();
441*795d594fSAndroid Build Coastguard Worker for (size_t i = 0; i < inputs.size(); ++i) {
442*795d594fSAndroid Build Coastguard Worker size_t end = predecessors[i]->GetLifetimeEnd();
443*795d594fSAndroid Build Coastguard Worker LiveInterval* input_interval = inputs[i]->GetLiveInterval()->GetSiblingAt(end - 1);
444*795d594fSAndroid Build Coastguard Worker if (input_interval->GetEnd() == end) {
445*795d594fSAndroid Build Coastguard Worker // If the input dies at the end of the predecessor, we know its register can
446*795d594fSAndroid Build Coastguard Worker // be reused.
447*795d594fSAndroid Build Coastguard Worker Location input_location = input_interval->ToLocation();
448*795d594fSAndroid Build Coastguard Worker if (input_location.IsRegisterKind()) {
449*795d594fSAndroid Build Coastguard Worker DCHECK(SameRegisterKind(input_location));
450*795d594fSAndroid Build Coastguard Worker return RegisterOrLowRegister(input_location);
451*795d594fSAndroid Build Coastguard Worker }
452*795d594fSAndroid Build Coastguard Worker }
453*795d594fSAndroid Build Coastguard Worker }
454*795d594fSAndroid Build Coastguard Worker } else {
455*795d594fSAndroid Build Coastguard Worker LocationSummary* locations = GetDefinedBy()->GetLocations();
456*795d594fSAndroid Build Coastguard Worker Location out = locations->Out();
457*795d594fSAndroid Build Coastguard Worker if (out.IsUnallocated() && out.GetPolicy() == Location::kSameAsFirstInput) {
458*795d594fSAndroid Build Coastguard Worker // Try to use the same register as the first input.
459*795d594fSAndroid Build Coastguard Worker LiveInterval* input_interval =
460*795d594fSAndroid Build Coastguard Worker GetDefinedBy()->InputAt(0)->GetLiveInterval()->GetSiblingAt(GetStart() - 1);
461*795d594fSAndroid Build Coastguard Worker if (input_interval->GetEnd() == GetStart()) {
462*795d594fSAndroid Build Coastguard Worker // If the input dies at the start of this instruction, we know its register can
463*795d594fSAndroid Build Coastguard Worker // be reused.
464*795d594fSAndroid Build Coastguard Worker Location location = input_interval->ToLocation();
465*795d594fSAndroid Build Coastguard Worker if (location.IsRegisterKind()) {
466*795d594fSAndroid Build Coastguard Worker DCHECK(SameRegisterKind(location));
467*795d594fSAndroid Build Coastguard Worker return RegisterOrLowRegister(location);
468*795d594fSAndroid Build Coastguard Worker }
469*795d594fSAndroid Build Coastguard Worker }
470*795d594fSAndroid Build Coastguard Worker }
471*795d594fSAndroid Build Coastguard Worker }
472*795d594fSAndroid Build Coastguard Worker return kNoRegister;
473*795d594fSAndroid Build Coastguard Worker }
474*795d594fSAndroid Build Coastguard Worker
SameRegisterKind(Location other) const475*795d594fSAndroid Build Coastguard Worker bool LiveInterval::SameRegisterKind(Location other) const {
476*795d594fSAndroid Build Coastguard Worker if (IsFloatingPoint()) {
477*795d594fSAndroid Build Coastguard Worker if (IsLowInterval() || IsHighInterval()) {
478*795d594fSAndroid Build Coastguard Worker return other.IsFpuRegisterPair();
479*795d594fSAndroid Build Coastguard Worker } else {
480*795d594fSAndroid Build Coastguard Worker return other.IsFpuRegister();
481*795d594fSAndroid Build Coastguard Worker }
482*795d594fSAndroid Build Coastguard Worker } else {
483*795d594fSAndroid Build Coastguard Worker if (IsLowInterval() || IsHighInterval()) {
484*795d594fSAndroid Build Coastguard Worker return other.IsRegisterPair();
485*795d594fSAndroid Build Coastguard Worker } else {
486*795d594fSAndroid Build Coastguard Worker return other.IsRegister();
487*795d594fSAndroid Build Coastguard Worker }
488*795d594fSAndroid Build Coastguard Worker }
489*795d594fSAndroid Build Coastguard Worker }
490*795d594fSAndroid Build Coastguard Worker
NumberOfSpillSlotsNeeded() const491*795d594fSAndroid Build Coastguard Worker size_t LiveInterval::NumberOfSpillSlotsNeeded() const {
492*795d594fSAndroid Build Coastguard Worker // For a SIMD operation, compute the number of needed spill slots.
493*795d594fSAndroid Build Coastguard Worker // TODO: do through vector type?
494*795d594fSAndroid Build Coastguard Worker HInstruction* definition = GetParent()->GetDefinedBy();
495*795d594fSAndroid Build Coastguard Worker if (definition != nullptr && HVecOperation::ReturnsSIMDValue(definition)) {
496*795d594fSAndroid Build Coastguard Worker if (definition->IsPhi()) {
497*795d594fSAndroid Build Coastguard Worker definition = definition->InputAt(1); // SIMD always appears on back-edge
498*795d594fSAndroid Build Coastguard Worker }
499*795d594fSAndroid Build Coastguard Worker return definition->AsVecOperation()->GetVectorNumberOfBytes() / kVRegSize;
500*795d594fSAndroid Build Coastguard Worker }
501*795d594fSAndroid Build Coastguard Worker // Return number of needed spill slots based on type.
502*795d594fSAndroid Build Coastguard Worker return (type_ == DataType::Type::kInt64 || type_ == DataType::Type::kFloat64) ? 2 : 1;
503*795d594fSAndroid Build Coastguard Worker }
504*795d594fSAndroid Build Coastguard Worker
ToLocation() const505*795d594fSAndroid Build Coastguard Worker Location LiveInterval::ToLocation() const {
506*795d594fSAndroid Build Coastguard Worker DCHECK(!IsHighInterval());
507*795d594fSAndroid Build Coastguard Worker if (HasRegister()) {
508*795d594fSAndroid Build Coastguard Worker if (IsFloatingPoint()) {
509*795d594fSAndroid Build Coastguard Worker if (HasHighInterval()) {
510*795d594fSAndroid Build Coastguard Worker return Location::FpuRegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
511*795d594fSAndroid Build Coastguard Worker } else {
512*795d594fSAndroid Build Coastguard Worker return Location::FpuRegisterLocation(GetRegister());
513*795d594fSAndroid Build Coastguard Worker }
514*795d594fSAndroid Build Coastguard Worker } else {
515*795d594fSAndroid Build Coastguard Worker if (HasHighInterval()) {
516*795d594fSAndroid Build Coastguard Worker return Location::RegisterPairLocation(GetRegister(), GetHighInterval()->GetRegister());
517*795d594fSAndroid Build Coastguard Worker } else {
518*795d594fSAndroid Build Coastguard Worker return Location::RegisterLocation(GetRegister());
519*795d594fSAndroid Build Coastguard Worker }
520*795d594fSAndroid Build Coastguard Worker }
521*795d594fSAndroid Build Coastguard Worker } else {
522*795d594fSAndroid Build Coastguard Worker HInstruction* defined_by = GetParent()->GetDefinedBy();
523*795d594fSAndroid Build Coastguard Worker if (defined_by->IsConstant()) {
524*795d594fSAndroid Build Coastguard Worker return defined_by->GetLocations()->Out();
525*795d594fSAndroid Build Coastguard Worker } else if (GetParent()->HasSpillSlot()) {
526*795d594fSAndroid Build Coastguard Worker return Location::StackSlotByNumOfSlots(NumberOfSpillSlotsNeeded(),
527*795d594fSAndroid Build Coastguard Worker GetParent()->GetSpillSlot());
528*795d594fSAndroid Build Coastguard Worker } else {
529*795d594fSAndroid Build Coastguard Worker return Location();
530*795d594fSAndroid Build Coastguard Worker }
531*795d594fSAndroid Build Coastguard Worker }
532*795d594fSAndroid Build Coastguard Worker }
533*795d594fSAndroid Build Coastguard Worker
GetLocationAt(size_t position)534*795d594fSAndroid Build Coastguard Worker Location LiveInterval::GetLocationAt(size_t position) {
535*795d594fSAndroid Build Coastguard Worker LiveInterval* sibling = GetSiblingAt(position);
536*795d594fSAndroid Build Coastguard Worker DCHECK(sibling != nullptr);
537*795d594fSAndroid Build Coastguard Worker return sibling->ToLocation();
538*795d594fSAndroid Build Coastguard Worker }
539*795d594fSAndroid Build Coastguard Worker
GetSiblingAt(size_t position)540*795d594fSAndroid Build Coastguard Worker LiveInterval* LiveInterval::GetSiblingAt(size_t position) {
541*795d594fSAndroid Build Coastguard Worker LiveInterval* current = this;
542*795d594fSAndroid Build Coastguard Worker while (current != nullptr && !current->IsDefinedAt(position)) {
543*795d594fSAndroid Build Coastguard Worker current = current->GetNextSibling();
544*795d594fSAndroid Build Coastguard Worker }
545*795d594fSAndroid Build Coastguard Worker return current;
546*795d594fSAndroid Build Coastguard Worker }
547*795d594fSAndroid Build Coastguard Worker
548*795d594fSAndroid Build Coastguard Worker } // namespace art
549