LLVM 22.0.0git
LiveVariables.cpp
Go to the documentation of this file.
1//===-- LiveVariables.cpp - Live Variable Analysis for Machine Code -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the LiveVariable analysis pass. For each machine
10// instruction in the function, this pass calculates the set of registers that
11// are immediately dead after the instruction (i.e., the instruction calculates
12// the value, but it is never used) and the set of registers that are used by
13// the instruction, but are never used after the instruction (i.e., they are
14// killed).
15//
16// This class computes live variables using a sparse implementation based on
17// the machine code SSA form. This class computes live variable information for
18// each virtual and _register allocatable_ physical register in a function. It
19// uses the dominance properties of SSA form to efficiently compute live
20// variables for virtual registers, and assumes that physical registers are only
21// live within a single basic block (allowing it to do a single local analysis
22// to resolve physical register lifetimes in each basic block). If a physical
23// register is not register allocatable, it is not tracked. This is useful for
24// things like the stack pointer and condition codes.
25//
26//===----------------------------------------------------------------------===//
27
29#include "llvm/ADT/DenseSet.h"
31#include "llvm/ADT/STLExtras.h"
33#include "llvm/ADT/SmallSet.h"
36#include "llvm/CodeGen/Passes.h"
37#include "llvm/Config/llvm-config.h"
38#include "llvm/Support/Debug.h"
41using namespace llvm;
42
43AnalysisKey LiveVariablesAnalysis::Key;
44
48 return Result(MF);
49}
50
54 OS << "Live variables in machine function: " << MF.getName() << '\n';
57}
58
62 "Live Variable Analysis", false, false)
63INITIALIZE_PASS_DEPENDENCY(UnreachableMachineBlockElimLegacy)
65 "Live Variable Analysis", false, false)
66
67void LiveVariablesWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
68 AU.addRequiredID(UnreachableMachineBlockElimID);
69 AU.setPreservesAll();
71}
72
73LiveVariables::LiveVariables(MachineFunction &MF)
74 : MF(&MF), MRI(&MF.getRegInfo()), TRI(MF.getSubtarget().getRegisterInfo()) {
75 analyze(MF);
76}
77
79 for (size_t I = 0, E = VirtRegInfo.size(); I != E; ++I) {
81 OS << "Virtual register '%" << I << "':\n";
82 VirtRegInfo[Reg].print(OS);
83 }
84}
85
88 for (MachineInstr *MI : Kills)
89 if (MI->getParent() == MBB)
90 return MI;
91 return nullptr;
92}
93
95 OS << " Alive in blocks: ";
96 for (unsigned AB : AliveBlocks)
97 OS << AB << ", ";
98 OS << "\n Killed by:";
99 if (Kills.empty())
100 OS << " No instructions.\n\n";
101 else {
102 for (unsigned i = 0, e = Kills.size(); i != e; ++i)
103 OS << "\n #" << i << ": " << *Kills[i];
104 OS << "\n";
105 }
106}
107
108#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
110#endif
111
112/// getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
114 assert(Reg.isVirtual() && "getVarInfo: not a virtual register!");
115 VirtRegInfo.grow(Reg);
116 return VirtRegInfo[Reg];
117}
118
120 VarInfo &VRInfo, MachineBasicBlock *DefBlock, MachineBasicBlock *MBB,
122 unsigned BBNum = MBB->getNumber();
123
124 // Check to see if this basic block is one of the killing blocks. If so,
125 // remove it.
126 for (unsigned i = 0, e = VRInfo.Kills.size(); i != e; ++i)
127 if (VRInfo.Kills[i]->getParent() == MBB) {
128 VRInfo.Kills.erase(VRInfo.Kills.begin()+i); // Erase entry
129 break;
130 }
131
132 if (MBB == DefBlock) return; // Terminate recursion
133
134 if (VRInfo.AliveBlocks.test(BBNum))
135 return; // We already know the block is live
136
137 // Mark the variable known alive in this bb
138 VRInfo.AliveBlocks.set(BBNum);
139
140 assert(MBB != &MF->front() && "Can't find reaching def for virtreg");
141 WorkList.insert(WorkList.end(), MBB->pred_rbegin(), MBB->pred_rend());
142}
143
145 MachineBasicBlock *DefBlock,
148 MarkVirtRegAliveInBlock(VRInfo, DefBlock, MBB, WorkList);
149
150 while (!WorkList.empty()) {
151 MachineBasicBlock *Pred = WorkList.pop_back_val();
152 MarkVirtRegAliveInBlock(VRInfo, DefBlock, Pred, WorkList);
153 }
154}
155
157 MachineInstr &MI) {
158 assert(MRI->getVRegDef(Reg) && "Register use before def!");
159
160 unsigned BBNum = MBB->getNumber();
161
162 VarInfo &VRInfo = getVarInfo(Reg);
163
164 // Check to see if this basic block is already a kill block.
165 if (!VRInfo.Kills.empty() && VRInfo.Kills.back()->getParent() == MBB) {
166 // Yes, this register is killed in this basic block already. Increase the
167 // live range by updating the kill instruction.
168 VRInfo.Kills.back() = &MI;
169 return;
170 }
171
172#ifndef NDEBUG
173 for (MachineInstr *Kill : VRInfo.Kills)
174 assert(Kill->getParent() != MBB && "entry should be at end!");
175#endif
176
177 // This situation can occur:
178 //
179 // ,------.
180 // | |
181 // | v
182 // | t2 = phi ... t1 ...
183 // | |
184 // | v
185 // | t1 = ...
186 // | ... = ... t1 ...
187 // | |
188 // `------'
189 //
190 // where there is a use in a PHI node that's a predecessor to the defining
191 // block. We don't want to mark all predecessors as having the value "alive"
192 // in this case.
193 if (MBB == MRI->getVRegDef(Reg)->getParent())
194 return;
195
196 // Add a new kill entry for this basic block. If this virtual register is
197 // already marked as alive in this basic block, that means it is alive in at
198 // least one of the successor blocks, it's not a kill.
199 if (!VRInfo.AliveBlocks.test(BBNum))
200 VRInfo.Kills.push_back(&MI);
201
202 // Update all dominating blocks to mark them as "known live".
203 for (MachineBasicBlock *Pred : MBB->predecessors())
204 MarkVirtRegAliveInBlock(VRInfo, MRI->getVRegDef(Reg)->getParent(), Pred);
205}
206
208 VarInfo &VRInfo = getVarInfo(Reg);
209
210 if (VRInfo.AliveBlocks.empty())
211 // If vr is not alive in any block, then defaults to dead.
212 VRInfo.Kills.push_back(&MI);
213}
214
215/// FindLastPartialDef - Return the last partial def of the specified register.
216MachineInstr *LiveVariables::FindLastPartialDef(Register Reg) {
217 unsigned LastDefDist = 0;
218 MachineInstr *LastDef = nullptr;
219 for (MCPhysReg SubReg : TRI->subregs(Reg)) {
220 MachineInstr *Def = PhysRegDef[SubReg];
221 if (!Def)
222 continue;
223 unsigned Dist = DistanceMap[Def];
224 if (Dist > LastDefDist) {
225 LastDef = Def;
226 LastDefDist = Dist;
227 }
228 }
229
230 if (!LastDef)
231 return nullptr;
232
233 return LastDef;
234}
235
236/// HandlePhysRegUse - Turn previous partial def's into read/mod/writes. Add
237/// implicit defs to a machine instruction if there was an earlier def of its
238/// super-register.
239void LiveVariables::HandlePhysRegUse(Register Reg, MachineInstr &MI) {
240 MachineInstr *LastDef = PhysRegDef[Reg.id()];
241 // If there was a previous use or a "full" def all is well.
242 if (!LastDef && !PhysRegUse[Reg.id()]) {
243 // Otherwise, the last sub-register def implicitly defines this register.
244 // e.g.
245 // AH =
246 // AL = ... implicit-def EAX, implicit killed AH
247 // = AH
248 // ...
249 // = EAX
250 // All of the sub-registers must have been defined before the use of Reg!
251 MachineInstr *LastPartialDef = FindLastPartialDef(Reg);
252 // If LastPartialDef is NULL, it must be using a livein register.
253 if (LastPartialDef) {
254 LastPartialDef->addOperand(
255 MachineOperand::CreateReg(Reg, /*IsDef=*/true, /*IsImp=*/true));
256 }
257 } else if (LastDef && !PhysRegUse[Reg.id()] &&
258 !LastDef->findRegisterDefOperand(Reg, /*TRI=*/nullptr))
259 // Last def defines the super register, add an implicit def of reg.
260 LastDef->addOperand(MachineOperand::CreateReg(Reg, true/*IsDef*/,
261 true/*IsImp*/));
262
263 // Remember this use.
264 for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg))
265 PhysRegUse[SubReg] = &MI;
266}
267
268/// FindLastRefOrPartRef - Return the last reference or partial reference of
269/// the specified register.
270MachineInstr *LiveVariables::FindLastRefOrPartRef(Register Reg) {
271 MachineInstr *LastDef = PhysRegDef[Reg.id()];
272 MachineInstr *LastUse = PhysRegUse[Reg.id()];
273 if (!LastDef && !LastUse)
274 return nullptr;
275
276 MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
277 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
278 unsigned LastPartDefDist = 0;
279 for (MCPhysReg SubReg : TRI->subregs(Reg)) {
280 MachineInstr *Def = PhysRegDef[SubReg];
281 if (Def && Def != LastDef) {
282 // There was a def of this sub-register in between. This is a partial
283 // def, keep track of the last one.
284 unsigned Dist = DistanceMap[Def];
285 if (Dist > LastPartDefDist)
286 LastPartDefDist = Dist;
287 } else if (MachineInstr *Use = PhysRegUse[SubReg]) {
288 unsigned Dist = DistanceMap[Use];
289 if (Dist > LastRefOrPartRefDist) {
290 LastRefOrPartRefDist = Dist;
291 LastRefOrPartRef = Use;
292 }
293 }
294 }
295
296 return LastRefOrPartRef;
297}
298
299bool LiveVariables::HandlePhysRegKill(Register Reg, MachineInstr *MI) {
300 MachineInstr *LastDef = PhysRegDef[Reg.id()];
301 MachineInstr *LastUse = PhysRegUse[Reg.id()];
302 if (!LastDef && !LastUse)
303 return false;
304
305 MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
306 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
307 // The whole register is used.
308 // AL =
309 // AH =
310 //
311 // = AX
312 // = AL, implicit killed AX
313 // AX =
314 //
315 // Or whole register is defined, but not used at all.
316 // dead AX =
317 // ...
318 // AX =
319 //
320 // Or whole register is defined, but only partly used.
321 // dead AX = implicit-def AL
322 // = killed AL
323 // AX =
324 MachineInstr *LastPartDef = nullptr;
325 unsigned LastPartDefDist = 0;
326 SmallSet<unsigned, 8> PartUses;
327 for (MCPhysReg SubReg : TRI->subregs(Reg)) {
328 MachineInstr *Def = PhysRegDef[SubReg];
329 if (Def && Def != LastDef) {
330 // There was a def of this sub-register in between. This is a partial
331 // def, keep track of the last one.
332 unsigned Dist = DistanceMap[Def];
333 if (Dist > LastPartDefDist) {
334 LastPartDefDist = Dist;
335 LastPartDef = Def;
336 }
337 continue;
338 }
339 if (MachineInstr *Use = PhysRegUse[SubReg]) {
340 PartUses.insert_range(TRI->subregs_inclusive(SubReg));
341 unsigned Dist = DistanceMap[Use];
342 if (Dist > LastRefOrPartRefDist) {
343 LastRefOrPartRefDist = Dist;
344 LastRefOrPartRef = Use;
345 }
346 }
347 }
348
349 if (!PhysRegUse[Reg.id()]) {
350 // Partial uses. Mark register def dead and add implicit def of
351 // sub-registers which are used.
352 // dead EAX = op implicit-def AL
353 // That is, EAX def is dead but AL def extends pass it.
354 PhysRegDef[Reg.id()]->addRegisterDead(Reg, TRI, true);
355 for (MCPhysReg SubReg : TRI->subregs(Reg)) {
356 if (!PartUses.count(SubReg))
357 continue;
358 bool NeedDef = true;
359 if (PhysRegDef[Reg.id()] == PhysRegDef[SubReg]) {
360 MachineOperand *MO = PhysRegDef[Reg.id()]->findRegisterDefOperand(
361 SubReg, /*TRI=*/nullptr);
362 if (MO) {
363 NeedDef = false;
364 assert(!MO->isDead());
365 }
366 }
367 if (NeedDef)
368 PhysRegDef[Reg.id()]->addOperand(
369 MachineOperand::CreateReg(SubReg, true /*IsDef*/, true /*IsImp*/));
370 MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg);
371 if (LastSubRef)
372 LastSubRef->addRegisterKilled(SubReg, TRI, true);
373 else {
374 LastRefOrPartRef->addRegisterKilled(SubReg, TRI, true);
375 for (MCPhysReg SS : TRI->subregs_inclusive(SubReg))
376 PhysRegUse[SS] = LastRefOrPartRef;
377 }
378 for (MCPhysReg SS : TRI->subregs(SubReg))
379 PartUses.erase(SS);
380 }
381 } else if (LastRefOrPartRef == PhysRegDef[Reg.id()] &&
382 LastRefOrPartRef != MI) {
383 if (LastPartDef)
384 // The last partial def kills the register.
385 LastPartDef->addOperand(MachineOperand::CreateReg(Reg, false/*IsDef*/,
386 true/*IsImp*/, true/*IsKill*/));
387 else {
388 MachineOperand *MO =
389 LastRefOrPartRef->findRegisterDefOperand(Reg, TRI, false, false);
390 bool NeedEC = MO->isEarlyClobber() && MO->getReg() != Reg;
391 // If the last reference is the last def, then it's not used at all.
392 // That is, unless we are currently processing the last reference itself.
393 LastRefOrPartRef->addRegisterDead(Reg, TRI, true);
394 if (NeedEC) {
395 // If we are adding a subreg def and the superreg def is marked early
396 // clobber, add an early clobber marker to the subreg def.
397 MO = LastRefOrPartRef->findRegisterDefOperand(Reg, /*TRI=*/nullptr);
398 if (MO)
399 MO->setIsEarlyClobber();
400 }
401 }
402 } else
403 LastRefOrPartRef->addRegisterKilled(Reg, TRI, true);
404 return true;
405}
406
407void LiveVariables::HandleRegMask(const MachineOperand &MO, unsigned NumRegs) {
408 // Call HandlePhysRegKill() for all live registers clobbered by Mask.
409 // Clobbered registers are always dead, sp there is no need to use
410 // HandlePhysRegDef().
411 for (unsigned Reg = 1; Reg != NumRegs; ++Reg) {
412 // Skip dead regs.
413 if (!PhysRegDef[Reg] && !PhysRegUse[Reg])
414 continue;
415 // Skip mask-preserved regs.
416 if (!MO.clobbersPhysReg(Reg))
417 continue;
418 // Kill the largest clobbered super-register.
419 // This avoids needless implicit operands.
420 unsigned Super = Reg;
421 for (MCPhysReg SR : TRI->superregs(Reg))
422 if (SR < NumRegs && (PhysRegDef[SR] || PhysRegUse[SR]) &&
423 MO.clobbersPhysReg(SR))
424 Super = SR;
425 HandlePhysRegKill(Super, nullptr);
426 }
427}
428
429void LiveVariables::HandlePhysRegDef(Register Reg, MachineInstr *MI,
431 // What parts of the register are previously defined?
433 if (PhysRegDef[Reg.id()] || PhysRegUse[Reg.id()]) {
434 Live.insert_range(TRI->subregs_inclusive(Reg));
435 } else {
436 for (MCPhysReg SubReg : TRI->subregs(Reg)) {
437 // If a register isn't itself defined, but all parts that make up of it
438 // are defined, then consider it also defined.
439 // e.g.
440 // AL =
441 // AH =
442 // = AX
443 if (Live.count(SubReg))
444 continue;
445 if (PhysRegDef[SubReg] || PhysRegUse[SubReg])
446 Live.insert_range(TRI->subregs_inclusive(SubReg));
447 }
448 }
449
450 // Start from the largest piece, find the last time any part of the register
451 // is referenced.
452 HandlePhysRegKill(Reg, MI);
453 // Only some of the sub-registers are used.
454 for (MCPhysReg SubReg : TRI->subregs(Reg)) {
455 if (!Live.count(SubReg))
456 // Skip if this sub-register isn't defined.
457 continue;
458 HandlePhysRegKill(SubReg, MI);
459 }
460
461 if (MI)
462 Defs.push_back(Reg); // Remember this def.
463}
464
465void LiveVariables::UpdatePhysRegDefs(MachineInstr &MI,
467 while (!Defs.empty()) {
468 Register Reg = Defs.pop_back_val();
469 for (MCPhysReg SubReg : TRI->subregs_inclusive(Reg)) {
470 PhysRegDef[SubReg] = &MI;
471 PhysRegUse[SubReg] = nullptr;
472 }
473 }
474}
475
476void LiveVariables::runOnInstr(MachineInstr &MI,
478 unsigned NumRegs) {
479 assert(!MI.isDebugOrPseudoInstr());
480 // Process all of the operands of the instruction...
481 unsigned NumOperandsToProcess = MI.getNumOperands();
482
483 // Unless it is a PHI node. In this case, ONLY process the DEF, not any
484 // of the uses. They will be handled in other basic blocks.
485 if (MI.isPHI())
486 NumOperandsToProcess = 1;
487
488 // Clear kill and dead markers. LV will recompute them.
492 for (unsigned i = 0; i != NumOperandsToProcess; ++i) {
493 MachineOperand &MO = MI.getOperand(i);
494 if (MO.isRegMask()) {
495 RegMasks.push_back(i);
496 continue;
497 }
498 if (!MO.isReg() || !MO.getReg())
499 continue;
500 Register MOReg = MO.getReg();
501 if (MO.isUse()) {
502 if (!(MOReg.isPhysical() && MRI->isReserved(MOReg)))
503 MO.setIsKill(false);
504 if (MO.readsReg())
505 UseRegs.push_back(MOReg);
506 } else {
507 assert(MO.isDef());
508 // FIXME: We should not remove any dead flags. However the MIPS RDDSP
509 // instruction needs it at the moment: http://llvm.org/PR27116.
510 if (MOReg.isPhysical() && !MRI->isReserved(MOReg))
511 MO.setIsDead(false);
512 DefRegs.push_back(MOReg);
513 }
514 }
515
516 MachineBasicBlock *MBB = MI.getParent();
517 // Process all uses.
518 for (Register MOReg : UseRegs) {
519 if (MOReg.isVirtual())
520 HandleVirtRegUse(MOReg, MBB, MI);
521 else if (!MRI->isReserved(MOReg))
522 HandlePhysRegUse(MOReg, MI);
523 }
524
525 // Process all masked registers. (Call clobbers).
526 for (unsigned Mask : RegMasks)
527 HandleRegMask(MI.getOperand(Mask), NumRegs);
528
529 // Process all defs.
530 for (Register MOReg : DefRegs) {
531 if (MOReg.isVirtual())
532 HandleVirtRegDef(MOReg, MI);
533 else if (!MRI->isReserved(MOReg))
534 HandlePhysRegDef(MOReg, &MI, Defs);
535 }
536 UpdatePhysRegDefs(MI, Defs);
537}
538
539void LiveVariables::runOnBlock(MachineBasicBlock *MBB, unsigned NumRegs) {
540 // Mark live-in registers as live-in.
542 for (const auto &LI : MBB->liveins()) {
543 assert(LI.PhysReg.isPhysical() &&
544 "Cannot have a live-in virtual register!");
545 HandlePhysRegDef(LI.PhysReg, nullptr, Defs);
546 }
547
548 // Loop over all of the instructions, processing them.
549 DistanceMap.clear();
550 unsigned Dist = 0;
551 for (MachineInstr &MI : *MBB) {
552 if (MI.isDebugOrPseudoInstr())
553 continue;
554 DistanceMap.insert(std::make_pair(&MI, Dist++));
555
556 runOnInstr(MI, Defs, NumRegs);
557 }
558
559 // Handle any virtual assignments from PHI nodes which might be at the
560 // bottom of this basic block. We check all of our successor blocks to see
561 // if they have PHI nodes, and if so, we simulate an assignment at the end
562 // of the current block.
563 if (!PHIVarInfo[MBB->getNumber()].empty()) {
564 SmallVectorImpl<Register> &VarInfoVec = PHIVarInfo[MBB->getNumber()];
565
566 for (Register I : VarInfoVec)
567 // Mark it alive only in the block we are representing.
569 MBB);
570 }
571
572 // MachineCSE may CSE instructions which write to non-allocatable physical
573 // registers across MBBs. Remember if any reserved register is liveout.
574 SmallSet<unsigned, 4> LiveOuts;
575 for (const MachineBasicBlock *SuccMBB : MBB->successors()) {
576 if (SuccMBB->isEHPad())
577 continue;
578 for (const auto &LI : SuccMBB->liveins()) {
579 if (!TRI->isInAllocatableClass(LI.PhysReg))
580 // Ignore other live-ins, e.g. those that are live into landing pads.
581 LiveOuts.insert(LI.PhysReg);
582 }
583 }
584
585 // Loop over PhysRegDef / PhysRegUse, killing any registers that are
586 // available at the end of the basic block.
587 for (unsigned i = 0; i != NumRegs; ++i)
588 if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.count(i))
589 HandlePhysRegDef(i, nullptr, Defs);
590}
591
592void LiveVariables::analyze(MachineFunction &mf) {
593 MF = &mf;
594 MRI = &mf.getRegInfo();
595 TRI = MF->getSubtarget().getRegisterInfo();
596
597 const unsigned NumRegs = TRI->getNumSupportedRegs(mf);
598 PhysRegDef.assign(NumRegs, nullptr);
599 PhysRegUse.assign(NumRegs, nullptr);
600 PHIVarInfo.resize(MF->getNumBlockIDs());
601
602 // FIXME: LiveIntervals will be updated to remove its dependence on
603 // LiveVariables to improve compilation time and eliminate bizarre pass
604 // dependencies. Until then, we can't change much in -O0.
605 if (!MRI->isSSA())
606 reportFatalUsageError("regalloc=... not currently supported with -O0");
607
608 analyzePHINodes(mf);
609
610 // Calculate live variable information in depth first order on the CFG of the
611 // function. This guarantees that we will see the definition of a virtual
612 // register before its uses due to dominance properties of SSA (except for PHI
613 // nodes, which are treated as a special case).
614 MachineBasicBlock *Entry = &MF->front();
616
617 for (MachineBasicBlock *MBB : depth_first_ext(Entry, Visited)) {
618 runOnBlock(MBB, NumRegs);
619
620 PhysRegDef.assign(NumRegs, nullptr);
621 PhysRegUse.assign(NumRegs, nullptr);
622 }
623
624 // Convert and transfer the dead / killed information we have gathered into
625 // VirtRegInfo onto MI's.
626 for (unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) {
628 for (unsigned j = 0, e2 = VirtRegInfo[Reg].Kills.size(); j != e2; ++j)
629 if (VirtRegInfo[Reg].Kills[j] == MRI->getVRegDef(Reg))
630 VirtRegInfo[Reg].Kills[j]->addRegisterDead(Reg, TRI);
631 else
632 VirtRegInfo[Reg].Kills[j]->addRegisterKilled(Reg, TRI);
633 }
634
635 // Check to make sure there are no unreachable blocks in the MC CFG for the
636 // function. If so, it is due to a bug in the instruction selector or some
637 // other part of the code generator if this happens.
638#ifndef NDEBUG
639 for (const MachineBasicBlock &MBB : *MF)
640 assert(Visited.contains(&MBB) && "unreachable basic block found");
641#endif
642
643 PhysRegDef.clear();
644 PhysRegUse.clear();
645 PHIVarInfo.clear();
646}
647
649 assert(Reg.isVirtual());
650
651 VarInfo &VI = getVarInfo(Reg);
652 VI.AliveBlocks.clear();
653 VI.Kills.clear();
654
655 MachineInstr &DefMI = *MRI->getUniqueVRegDef(Reg);
656 MachineBasicBlock &DefBB = *DefMI.getParent();
657
658 // Initialize a worklist of BBs that Reg is live-to-end of. (Here
659 // "live-to-end" means Reg is live at the end of a block even if it is only
660 // live because of phi uses in a successor. This is different from isLiveOut()
661 // which does not consider phi uses.)
662 SmallVector<MachineBasicBlock *> LiveToEndBlocks;
663 SparseBitVector<> UseBlocks;
664 unsigned NumRealUses = 0;
665 for (auto &UseMO : MRI->use_nodbg_operands(Reg)) {
666 UseMO.setIsKill(false);
667 if (!UseMO.readsReg())
668 continue;
669 ++NumRealUses;
670 MachineInstr &UseMI = *UseMO.getParent();
671 MachineBasicBlock &UseBB = *UseMI.getParent();
672 UseBlocks.set(UseBB.getNumber());
673 if (UseMI.isPHI()) {
674 // If Reg is used in a phi then it is live-to-end of the corresponding
675 // predecessor.
676 unsigned Idx = UseMO.getOperandNo();
677 LiveToEndBlocks.push_back(UseMI.getOperand(Idx + 1).getMBB());
678 } else if (&UseBB == &DefBB) {
679 // A non-phi use in the same BB as the single def must come after the def.
680 } else {
681 // Otherwise Reg must be live-to-end of all predecessors.
682 LiveToEndBlocks.append(UseBB.pred_begin(), UseBB.pred_end());
683 }
684 }
685
686 // Handle the case where all uses have been removed.
687 if (NumRealUses == 0) {
688 VI.Kills.push_back(&DefMI);
689 DefMI.addRegisterDead(Reg, nullptr);
690 return;
691 }
692 DefMI.clearRegisterDeads(Reg);
693
694 // Iterate over the worklist adding blocks to AliveBlocks.
695 bool LiveToEndOfDefBB = false;
696 while (!LiveToEndBlocks.empty()) {
697 MachineBasicBlock &BB = *LiveToEndBlocks.pop_back_val();
698 if (&BB == &DefBB) {
699 LiveToEndOfDefBB = true;
700 continue;
701 }
702 if (VI.AliveBlocks.test(BB.getNumber()))
703 continue;
704 VI.AliveBlocks.set(BB.getNumber());
705 LiveToEndBlocks.append(BB.pred_begin(), BB.pred_end());
706 }
707
708 // Recompute kill flags. For each block in which Reg is used but is not
709 // live-through, find the last instruction that uses Reg. Ignore phi nodes
710 // because they should not be included in Kills.
711 for (unsigned UseBBNum : UseBlocks) {
712 if (VI.AliveBlocks.test(UseBBNum))
713 continue;
714 MachineBasicBlock &UseBB = *MF->getBlockNumbered(UseBBNum);
715 if (&UseBB == &DefBB && LiveToEndOfDefBB)
716 continue;
717 for (auto &MI : reverse(UseBB)) {
718 if (MI.isDebugOrPseudoInstr())
719 continue;
720 if (MI.isPHI())
721 break;
722 if (MI.readsVirtualRegister(Reg)) {
723 assert(!MI.killsRegister(Reg, /*TRI=*/nullptr));
724 MI.addRegisterKilled(Reg, nullptr);
725 VI.Kills.push_back(&MI);
726 break;
727 }
728 }
729 }
730}
731
732/// replaceKillInstruction - Update register kill info by replacing a kill
733/// instruction with a new one.
735 MachineInstr &NewMI) {
736 VarInfo &VI = getVarInfo(Reg);
737 llvm::replace(VI.Kills, &OldMI, &NewMI);
738}
739
740/// removeVirtualRegistersKilled - Remove all killed info for the specified
741/// instruction.
743 for (MachineOperand &MO : MI.operands()) {
744 if (MO.isReg() && MO.isKill()) {
745 MO.setIsKill(false);
746 Register Reg = MO.getReg();
747 if (Reg.isVirtual()) {
748 bool removed = getVarInfo(Reg).removeKill(MI);
749 assert(removed && "kill not in register's VarInfo?");
750 (void)removed;
751 }
752 }
753 }
754}
755
756/// analyzePHINodes - Gather information about the PHI nodes in here. In
757/// particular, we want to map the variable information of a virtual register
758/// which is used in a PHI node. We map that to the BB the vreg is coming from.
759///
760void LiveVariables::analyzePHINodes(const MachineFunction& Fn) {
761 for (const auto &MBB : Fn)
762 for (const auto &BBI : MBB) {
763 if (!BBI.isPHI())
764 break;
765 for (unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2)
766 if (BBI.getOperand(i).readsReg())
767 PHIVarInfo[BBI.getOperand(i + 1).getMBB()->getNumber()]
768 .push_back(BBI.getOperand(i).getReg());
769 }
770}
771
773 Register Reg, MachineRegisterInfo &MRI) {
774 unsigned Num = MBB.getNumber();
775
776 // Reg is live-through.
777 if (AliveBlocks.test(Num))
778 return true;
779
780 // Registers defined in MBB cannot be live in.
781 const MachineInstr *Def = MRI.getVRegDef(Reg);
782 if (Def && Def->getParent() == &MBB)
783 return false;
784
785 // Reg was not defined in MBB, was it killed here?
786 return findKill(&MBB);
787}
788
791
793 for (MachineInstr *MI : VI.Kills)
794 Kills.insert(MI->getParent());
795
796 // Loop over all of the successors of the basic block, checking to see if
797 // the value is either live in the block, or if it is killed in the block.
798 for (const MachineBasicBlock *SuccMBB : MBB.successors()) {
799 // Is it alive in this successor?
800 unsigned SuccIdx = SuccMBB->getNumber();
801 if (VI.AliveBlocks.test(SuccIdx))
802 return true;
803 // Or is it live because there is a use in a successor that kills it?
804 if (Kills.count(SuccMBB))
805 return true;
806 }
807
808 return false;
809}
810
811/// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
812/// variables that are live out of DomBB will be marked as passing live through
813/// BB.
815 MachineBasicBlock *DomBB,
816 MachineBasicBlock *SuccBB) {
817 const unsigned NumNew = BB->getNumber();
818
819 DenseSet<Register> Defs, Kills;
820
821 MachineBasicBlock::iterator BBI = SuccBB->begin(), BBE = SuccBB->end();
822 for (; BBI != BBE && BBI->isPHI(); ++BBI) {
823 // Record the def of the PHI node.
824 Defs.insert(BBI->getOperand(0).getReg());
825
826 // All registers used by PHI nodes in SuccBB must be live through BB.
827 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
828 if (BBI->getOperand(i+1).getMBB() == BB)
829 getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew);
830 }
831
832 // Record all vreg defs and kills of all instructions in SuccBB.
833 for (; BBI != BBE; ++BBI) {
834 for (const MachineOperand &Op : BBI->operands()) {
835 if (Op.isReg() && Op.getReg().isVirtual()) {
836 if (Op.isDef())
837 Defs.insert(Op.getReg());
838 else if (Op.isKill())
839 Kills.insert(Op.getReg());
840 }
841 }
842 }
843
844 // Update info for all live variables
845 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
847
848 // If the Defs is defined in the successor it can't be live in BB.
849 if (Defs.count(Reg))
850 continue;
851
852 // If the register is either killed in or live through SuccBB it's also live
853 // through BB.
854 VarInfo &VI = getVarInfo(Reg);
855 if (Kills.count(Reg) || VI.AliveBlocks.test(SuccBB->getNumber()))
856 VI.AliveBlocks.set(NumNew);
857 }
858}
859
860/// addNewBlock - Add a new basic block BB as an empty succcessor to DomBB. All
861/// variables that are live out of DomBB will be marked as passing live through
862/// BB. LiveInSets[BB] is *not* updated (because it is not needed during
863/// PHIElimination).
865 MachineBasicBlock *DomBB,
866 MachineBasicBlock *SuccBB,
867 std::vector<SparseBitVector<>> &LiveInSets) {
868 const unsigned NumNew = BB->getNumber();
869
870 SparseBitVector<> &BV = LiveInSets[SuccBB->getNumber()];
871 for (unsigned R : BV) {
873 LiveVariables::VarInfo &VI = getVarInfo(VirtReg);
874 VI.AliveBlocks.set(NumNew);
875 }
876 // All registers used by PHI nodes in SuccBB must be live through BB.
877 for (MachineBasicBlock::iterator BBI = SuccBB->begin(),
878 BBE = SuccBB->end();
879 BBI != BBE && BBI->isPHI(); ++BBI) {
880 for (unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
881 if (BBI->getOperand(i + 1).getMBB() == BB &&
882 BBI->getOperand(i).readsReg())
883 getVarInfo(BBI->getOperand(i).getReg())
884 .AliveBlocks.set(NumNew);
885 }
886}
unsigned SubReg
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
block Block Frequency Analysis
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:638
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Looks at all the uses of the given value Returns the Liveness deduced from the uses of this value Adds all uses that cause the result to be MaybeLive to MaybeLiveRetUses If the result is Live
This file defines the DenseSet and SmallDenseSet classes.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
IRTranslator LLVM IR MI
livevars
#define I(x, y, z)
Definition: MD5.cpp:58
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:39
This file contains some templates that are useful if you are working with the STL at all.
raw_pwrite_stream & OS
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:412
Represent the analysis usage information of a pass.
This class represents an Operation in the Expression.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:263
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
LLVM_ABI void replaceKillInstruction(Register Reg, MachineInstr &OldMI, MachineInstr &NewMI)
replaceKillInstruction - Update register kill info by replacing a kill instruction with a new one.
LLVM_ABI void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *DefBlock, MachineBasicBlock *BB)
LLVM_ABI void removeVirtualRegistersKilled(MachineInstr &MI)
removeVirtualRegistersKilled - Remove all killed info for the specified instruction.
LLVM_ABI bool isLiveOut(Register Reg, const MachineBasicBlock &MBB)
isLiveOut - Determine if Reg is live out from MBB, when not considering PHI nodes.
LLVM_ABI void HandleVirtRegDef(Register reg, MachineInstr &MI)
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI void recomputeForSingleDefVirtReg(Register Reg)
Recompute liveness from scratch for a virtual register Reg that is known to have a single def that do...
LLVM_ABI void HandleVirtRegUse(Register reg, MachineBasicBlock *MBB, MachineInstr &MI)
LLVM_ABI VarInfo & getVarInfo(Register Reg)
getVarInfo - Return the VarInfo structure for the specified VIRTUAL register.
LLVM_ABI void addNewBlock(MachineBasicBlock *BB, MachineBasicBlock *DomBB, MachineBasicBlock *SuccBB)
addNewBlock - Add a new basic block BB between DomBB and SuccBB.
iterator_range< MCSuperRegIterator > superregs(MCRegister Reg) const
Return an iterator range over all super-registers of Reg, excluding Reg.
iterator_range< MCSubRegIterator > subregs_inclusive(MCRegister Reg) const
Return an iterator range over all sub-registers of Reg, including Reg.
iterator_range< MCSubRegIterator > subregs(MCRegister Reg) const
Return an iterator range over all sub-registers of Reg, excluding Reg.
iterator_range< livein_iterator > liveins() const
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
pred_reverse_iterator pred_rbegin()
pred_reverse_iterator pred_rend()
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const MachineBasicBlock & front() const
Representation of each machine instruction.
Definition: MachineInstr.h:72
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:359
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
LLVM_ABI bool addRegisterKilled(Register IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
MachineOperand * findRegisterDefOperand(Register Reg, const TargetRegisterInfo *TRI, bool isDead=false, bool Overlap=false)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
LLVM_ABI bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
MachineOperand class - Representation of each machine instruction operand.
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
void setIsDead(bool Val=true)
void setIsKill(bool Val=true)
void setIsEarlyClobber(bool Val=true)
bool isEarlyClobber() const
Register getReg() const
getReg - Returns the register number.
static bool clobbersPhysReg(const uint32_t *RegMask, MCRegister PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
static MachineOperand CreateReg(Register Reg, bool isDef, bool isImp=false, bool isKill=false, bool isDead=false, bool isUndef=false, bool isEarlyClobber=false, unsigned SubReg=0, bool isDebug=false, bool isInternalRead=false, bool isRenamable=false)
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
iterator_range< use_nodbg_iterator > use_nodbg_operands(Register Reg) const
bool isReserved(MCRegister PhysReg) const
isReserved - Returns true when PhysReg is a reserved register.
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
LLVM_ABI MachineInstr * getUniqueVRegDef(Register Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:118
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition: Register.h:67
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition: Register.h:74
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition: Register.h:78
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:470
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:476
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:134
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:176
void insert_range(Range &&R)
Definition: SmallSet.h:194
bool erase(const T &V)
Definition: SmallSet.h:198
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:182
bool empty() const
Definition: SmallVector.h:82
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:806
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
void set(unsigned Idx)
bool test(unsigned Idx) const
virtual unsigned getNumSupportedRegs(const MachineFunction &) const
Return the number of registers for the function. (may overestimate)
bool isInAllocatableClass(MCRegister RegNo) const
Return true if the register is in the allocation of any register class.
virtual const TargetRegisterInfo * getRegisterInfo() const =0
Return the target's register information.
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:194
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
Definition: DenseSet.h:174
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
@ Entry
Definition: COFF.h:862
@ SS
Definition: X86.h:215
Reg
All possible values of the reg field in the ModR/M byte.
NodeAddr< DefNode * > Def
Definition: RDFGraph.h:384
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:428
LLVM_ABI char & UnreachableMachineBlockElimID
UnreachableMachineBlockElimination - This pass removes unreachable machine basic blocks.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:207
void replace(R &&Range, const T &OldValue, const T &NewValue)
Provide wrappers to std::replace which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1879
LLVM_ABI char & LiveVariablesID
LiveVariables pass - This pass computes the set of blocks in which each variable is life and sets mac...
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition: Error.cpp:180
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29
VarInfo - This represents the regions where a virtual register is live in the program.
Definition: LiveVariables.h:79
bool removeKill(MachineInstr &MI)
removeKill - Delete a kill corresponding to the specified machine instruction.
Definition: LiveVariables.h:94
LLVM_ABI void dump() const
std::vector< MachineInstr * > Kills
Kills - List of MachineInstruction's which are the last use of this virtual register (kill it) in the...
Definition: LiveVariables.h:89
SparseBitVector AliveBlocks
AliveBlocks - Set of blocks in which this value is alive completely through.
Definition: LiveVariables.h:84
LLVM_ABI MachineInstr * findKill(const MachineBasicBlock *MBB) const
findKill - Find a kill instruction in MBB. Return NULL if none is found.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI bool isLiveIn(const MachineBasicBlock &MBB, Register Reg, MachineRegisterInfo &MRI)
isLiveIn - Is Reg live in to MBB? This means that Reg is live through MBB, or it is killed in MBB.
VirtRegInfo - Information about a virtual register used by a set of operands.