37 #include "llvm/Config/llvm-config.h" 47 "Live Variable Analysis",
false,
false)
50 "Live Variable Analysis",
false, false)
61 for (
unsigned i = 0,
e =
Kills.size(); i !=
e; ++i)
67 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 69 dbgs() <<
" Alive in blocks: ";
73 dbgs() <<
"\n Killed by:";
75 dbgs() <<
" No instructions.\n";
77 for (
unsigned i = 0,
e =
Kills.size(); i !=
e; ++i)
78 dbgs() <<
"\n #" << i <<
": " << *
Kills[i];
87 "getVarInfo: not a virtual register!");
95 std::vector<MachineBasicBlock*> &WorkList) {
100 for (
unsigned i = 0,
e = VRInfo.
Kills.size(); i !=
e; ++i)
101 if (VRInfo.
Kills[i]->getParent() == MBB) {
106 if (MBB == DefBlock)
return;
114 assert(MBB != &MF->
front() &&
"Can't find reaching def for virtreg");
121 std::vector<MachineBasicBlock*> WorkList;
124 while (!WorkList.empty()) {
133 assert(
MRI->getVRegDef(reg) &&
"Register use before def!");
140 if (!VRInfo.
Kills.empty() && VRInfo.
Kills.back()->getParent() == MBB) {
148 for (
unsigned i = 0,
e = VRInfo.
Kills.size(); i !=
e; ++i)
149 assert(VRInfo.
Kills[i]->getParent() != MBB &&
"entry should be at end!");
168 if (MBB ==
MRI->getVRegDef(reg)->getParent())
return;
174 VRInfo.
Kills.push_back(&MI);
187 VRInfo.
Kills.push_back(&MI);
194 unsigned LastDefReg = 0;
195 unsigned LastDefDist = 0;
198 unsigned SubReg = *SubRegs;
202 unsigned Dist = DistanceMap[
Def];
203 if (Dist > LastDefDist) {
213 PartDefRegs.
insert(LastDefReg);
219 if (
TRI->isSubRegister(Reg, DefReg)) {
222 PartDefRegs.
insert(*SubRegs);
231 void LiveVariables::HandlePhysRegUse(
unsigned Reg,
MachineInstr &
MI) {
234 if (!LastDef && !PhysRegUse[Reg]) {
244 MachineInstr *LastPartialDef = FindLastPartialDef(Reg, PartDefRegs);
246 if (LastPartialDef) {
249 PhysRegDef[
Reg] = LastPartialDef;
252 unsigned SubReg = *SubRegs;
253 if (Processed.
count(SubReg))
255 if (PartDefRegs.
count(SubReg))
262 PhysRegDef[
SubReg] = LastPartialDef;
267 }
else if (LastDef && !PhysRegUse[Reg] &&
276 PhysRegUse[*SubRegs] = &MI;
281 MachineInstr *LiveVariables::FindLastRefOrPartRef(
unsigned Reg) {
284 if (!LastDef && !LastUse)
287 MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
288 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
289 unsigned LastPartDefDist = 0;
291 unsigned SubReg = *SubRegs;
293 if (Def && Def != LastDef) {
296 unsigned Dist = DistanceMap[
Def];
297 if (Dist > LastPartDefDist)
298 LastPartDefDist = Dist;
300 unsigned Dist = DistanceMap[
Use];
301 if (Dist > LastRefOrPartRefDist) {
302 LastRefOrPartRefDist = Dist;
303 LastRefOrPartRef =
Use;
308 return LastRefOrPartRef;
311 bool LiveVariables::HandlePhysRegKill(
unsigned Reg,
MachineInstr *MI) {
314 if (!LastDef && !LastUse)
317 MachineInstr *LastRefOrPartRef = LastUse ? LastUse : LastDef;
318 unsigned LastRefOrPartRefDist = DistanceMap[LastRefOrPartRef];
337 unsigned LastPartDefDist = 0;
340 unsigned SubReg = *SubRegs;
342 if (Def && Def != LastDef) {
345 unsigned Dist = DistanceMap[
Def];
346 if (Dist > LastPartDefDist) {
347 LastPartDefDist = Dist;
356 unsigned Dist = DistanceMap[
Use];
357 if (Dist > LastRefOrPartRefDist) {
358 LastRefOrPartRefDist = Dist;
359 LastRefOrPartRef =
Use;
364 if (!PhysRegUse[Reg]) {
369 PhysRegDef[
Reg]->addRegisterDead(Reg,
TRI,
true);
371 unsigned SubReg = *SubRegs;
372 if (!PartUses.
count(SubReg))
375 if (PhysRegDef[Reg] == PhysRegDef[SubReg]) {
385 MachineInstr *LastSubRef = FindLastRefOrPartRef(SubReg);
392 PhysRegUse[*
SS] = LastRefOrPartRef;
397 }
else if (LastRefOrPartRef == PhysRegDef[Reg] && LastRefOrPartRef != MI) {
426 for (
unsigned Reg = 1, NumRegs =
TRI->getNumRegs(); Reg != NumRegs; ++
Reg) {
428 if (!PhysRegDef[Reg] && !PhysRegUse[Reg])
435 unsigned Super =
Reg;
439 HandlePhysRegKill(Super,
nullptr);
443 void LiveVariables::HandlePhysRegDef(
unsigned Reg,
MachineInstr *MI,
447 if (PhysRegDef[Reg] || PhysRegUse[Reg]) {
453 unsigned SubReg = *SubRegs;
460 if (Live.
count(SubReg))
462 if (PhysRegDef[SubReg] || PhysRegUse[SubReg]) {
472 HandlePhysRegKill(Reg, MI);
475 unsigned SubReg = *SubRegs;
476 if (!Live.
count(SubReg))
479 HandlePhysRegKill(SubReg, MI);
488 while (!Defs.
empty()) {
489 unsigned Reg = Defs.
back();
492 SubRegs.
isValid(); ++SubRegs) {
493 unsigned SubReg = *SubRegs;
495 PhysRegUse[
SubReg] =
nullptr;
509 NumOperandsToProcess = 1;
515 for (
unsigned i = 0; i != NumOperandsToProcess; ++i) {
541 for (
unsigned i = 0,
e = UseRegs.
size(); i !=
e; ++i) {
542 unsigned MOReg = UseRegs[i];
545 else if (!
MRI->isReserved(MOReg))
546 HandlePhysRegUse(MOReg, MI);
550 for (
unsigned i = 0,
e = RegMasks.
size(); i !=
e; ++i)
554 for (
unsigned i = 0,
e = DefRegs.
size(); i !=
e; ++i) {
555 unsigned MOReg = DefRegs[i];
558 else if (!
MRI->isReserved(MOReg))
559 HandlePhysRegDef(MOReg, &MI, Defs);
561 UpdatePhysRegDefs(MI, Defs);
567 for (
const auto &LI : MBB->
liveins()) {
569 "Cannot have a live-in virtual register!");
570 HandlePhysRegDef(LI.PhysReg,
nullptr, Defs);
579 DistanceMap.insert(std::make_pair(&MI, Dist++));
581 runOnInstr(MI, Defs);
588 if (!PHIVarInfo[MBB->getNumber()].empty()) {
592 E = VarInfoVec.
end();
I !=
E; ++
I)
602 SE = MBB->succ_end();
SI != SE; ++
SI) {
606 for (
const auto &LI : SuccMBB->
liveins()) {
607 if (!
TRI->isInAllocatableClass(LI.PhysReg))
609 LiveOuts.
insert(LI.PhysReg);
615 for (
unsigned i = 0; i != NumRegs; ++i)
616 if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.
count(i))
617 HandlePhysRegDef(i,
nullptr, Defs);
623 TRI = MF->getSubtarget().getRegisterInfo();
625 const unsigned NumRegs =
TRI->getNumRegs();
626 PhysRegDef.assign(NumRegs,
nullptr);
627 PhysRegUse.assign(NumRegs,
nullptr);
628 PHIVarInfo.resize(MF->getNumBlockIDs());
647 runOnBlock(MBB, NumRegs);
649 PhysRegDef.assign(NumRegs,
nullptr);
650 PhysRegUse.assign(NumRegs,
nullptr);
655 for (
unsigned i = 0, e1 =
VirtRegInfo.size(); i != e1; ++i) {
669 assert(Visited.
count(&*i) != 0 &&
"unreachable basic block found");
697 assert(removed &&
"kill not in register's VarInfo?");
709 for (
const auto &MBB : Fn)
710 for (
const auto &BBI : MBB) {
713 for (
unsigned i = 1,
e = BBI.getNumOperands(); i !=
e; i += 2)
714 if (BBI.getOperand(i).readsReg())
715 PHIVarInfo[BBI.getOperand(i + 1).getMBB()->getNumber()]
716 .push_back(BBI.getOperand(i).getReg());
742 for (
unsigned i = 0,
e = VI.
Kills.size(); i !=
e; ++i)
749 unsigned SuccIdx = SuccMBB->getNumber();
753 if (Kills.
count(SuccMBB))
771 for (; BBI != BBE && BBI->isPHI(); ++BBI) {
773 Defs.
insert(BBI->getOperand(0).getReg());
776 for (
unsigned i = 1,
e = BBI->getNumOperands(); i !=
e; i += 2)
777 if (BBI->getOperand(i+1).getMBB() == BB)
782 for (; BBI != BBE; ++BBI) {
784 E = BBI->operands_end();
I !=
E; ++
I) {
788 else if (
I->isKill())
795 for (
unsigned i = 0,
e =
MRI->getNumVirtRegs(); i !=
e; ++i) {
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
pred_reverse_iterator pred_rbegin()
pred_reverse_iterator pred_rend()
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
void push_back(const T &Elt)
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
bool isLiveOut(unsigned Reg, const MachineBasicBlock &MBB)
isLiveOut - Determine if Reg is live out from MBB, when not considering PHI nodes.
bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI defined a register without a use.
void HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB, MachineInstr &MI)
VarInfo - This represents the regions where a virtual register is live in the program.
unsigned const TargetRegisterInfo * TRI
void setIsDead(bool Val=true)
static unsigned index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
MachineInstr * findKill(const MachineBasicBlock *MBB) const
findKill - Find a kill instruction in MBB. Return NULL if none is found.
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
iterator_range< succ_iterator > successors()
bool removeKill(MachineInstr &MI)
removeKill - Delete a kill corresponding to the specified machine instruction.
bool isEarlyClobber() const
bool test(unsigned Idx) const
#define INITIALIZE_PASS_DEPENDENCY(depName)
MCSuperRegIterator enumerates all super-registers of Reg.
unsigned getNumOperands() const
Retuns the total number of operands.
A Use represents the edge between a Value definition and its users.
SparseBitVector AliveBlocks
AliveBlocks - Set of blocks in which this value is alive completely through.
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
char & UnreachableMachineBlockElimID
UnreachableMachineBlockElimination - This pass removes unreachable machine basic blocks.
void setIsEarlyClobber(bool Val=true)
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register. ...
bool addRegisterKilled(Register IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
char & LiveVariablesID
LiveVariables pass - This pass computes the set of blocks in which each variable is life and sets mac...
unsigned const MachineRegisterInfo * MRI
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< iterator, bool > insert(const ValueT &V)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
iterator_range< df_ext_iterator< T, SetTy > > depth_first_ext(const T &G, SetTy &S)
Represent the analysis usage information of a pass.
static void replace(Module &M, GlobalVariable *Old, GlobalVariable *New)
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
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)
VarInfo & getVarInfo(unsigned RegIdx)
getVarInfo - Return the VarInfo structure for the specified VIRTUAL register.
MCSubRegIterator enumerates all sub-registers of Reg.
pred_iterator pred_begin()
bool isDebugInstr() const
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *DefBlock, MachineBasicBlock *BB)
MachineOperand * findRegisterDefOperand(Register Reg, bool isDead=false, bool Overlap=false, const TargetRegisterInfo *TRI=nullptr)
Wrapper for findRegisterDefOperandIdx, it returns a pointer to the MachineOperand rather than an inde...
void setIsKill(bool Val=true)
void addNewBlock(MachineBasicBlock *BB, MachineBasicBlock *DomBB, MachineBasicBlock *SuccBB)
addNewBlock - Add a new basic block BB between DomBB and SuccBB.
std::vector< MachineInstr * > Kills
Kills - List of MachineInstruction's which are the last use of this virtual register (kill it) in the...
Iterator for intrusive lists based on ilist_node.
void replaceKillInstruction(unsigned Reg, MachineInstr &OldMI, MachineInstr &NewMI)
replaceKillInstruction - Update register kill info by replacing a kill instruction with a new one...
void HandleVirtRegDef(unsigned reg, MachineInstr &MI)
void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
bool isLiveIn(const MachineBasicBlock &MBB, unsigned Reg, MachineRegisterInfo &MRI)
isLiveIn - Is Reg live in to MBB? This means that Reg is live through MBB, or it is killed in MBB...
MachineOperand class - Representation of each machine instruction operand.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
clobbersPhysReg - Returns true if this RegMask clobbers PhysReg.
void removeVirtualRegistersKilled(MachineInstr &MI)
removeVirtualRegistersKilled - Remove all killed info for the specified instruction.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool isEHPad() const
Returns true if the block is a landing pad.
LLVM_NODISCARD bool empty() const
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
iterator_range< livein_iterator > liveins() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
VirtRegInfo - Information about a virtual register used by a set of operands.
SparseBitVectorIterator iterator
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
static const Function * getParent(const Value *V)
INITIALIZE_PASS_BEGIN(LiveVariables, "livevars", "Live Variable Analysis", false, false) INITIALIZE_PASS_END(LiveVariables
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
Wrapper class representing virtual and physical registers.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.