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