47 "Live Variable Analysis",
false,
false)
61 for (
unsigned i = 0, e =
Kills.size(); i != e; ++i)
68 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
69 dbgs() <<
" Alive in blocks: ";
71 E = AliveBlocks.end();
I != E; ++
I)
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!");
88 VirtRegInfo.grow(RegIdx);
89 return VirtRegInfo[RegIdx];
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!");
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);
214 for (
unsigned i = 0, e = LastDef->
getNumOperands(); i != e; ++i) {
218 unsigned DefReg = MO.
getReg();
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]) {
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, 0);
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;
494 PhysRegDef[SubReg] =
MI;
495 PhysRegUse[SubReg] = NULL;
509 std::fill(PhysRegDef, PhysRegDef + NumRegs, (
MachineInstr*)0);
510 std::fill(PhysRegUse, PhysRegUse + NumRegs, (
MachineInstr*)0);
538 "Cannot have a live-in virtual register!");
539 HandlePhysRegDef(*II, 0, Defs);
550 DistanceMap.insert(std::make_pair(MI, Dist++));
558 NumOperandsToProcess = 1;
564 for (
unsigned i = 0; i != NumOperandsToProcess; ++i) {
572 unsigned MOReg = MO.
getReg();
584 for (
unsigned i = 0, e = UseRegs.
size(); i != e; ++i) {
585 unsigned MOReg = UseRegs[i];
589 HandlePhysRegUse(MOReg, MI);
593 for (
unsigned i = 0, e = RegMasks.
size(); i != e; ++i)
597 for (
unsigned i = 0, e = DefRegs.
size(); i != e; ++i) {
598 unsigned MOReg = DefRegs[i];
602 HandlePhysRegDef(MOReg, MI, Defs);
604 UpdatePhysRegDefs(MI, Defs);
615 E = VarInfoVec.
end();
I != E; ++
I)
625 SE = MBB->
succ_end(); SI != SE; ++SI) {
640 for (
unsigned i = 0; i != NumRegs; ++i)
641 if ((PhysRegDef[i] || PhysRegUse[i]) && !LiveOuts.
count(i))
642 HandlePhysRegDef(i, 0, Defs);
644 std::fill(PhysRegDef, PhysRegDef + NumRegs, (
MachineInstr*)0);
645 std::fill(PhysRegUse, PhysRegUse + NumRegs, (
MachineInstr*)0);
650 for (
unsigned i = 0, e1 = VirtRegInfo.size(); i != e1; ++i) {
652 for (
unsigned j = 0, e2 = VirtRegInfo[Reg].Kills.size(); j != e2; ++j)
653 if (VirtRegInfo[Reg].Kills[j] == MRI->
getVRegDef(Reg))
654 VirtRegInfo[Reg].Kills[j]->addRegisterDead(Reg, TRI);
656 VirtRegInfo[
Reg].Kills[j]->addRegisterKilled(Reg, TRI);
664 assert(Visited.
count(&*i) != 0 &&
"unreachable basic block found");
679 std::replace(VI.
Kills.begin(), VI.
Kills.end(), OldMI, NewMI);
689 unsigned Reg = MO.
getReg();
692 assert(removed &&
"kill not in register's VarInfo?");
707 BBI != BBE && BBI->isPHI(); ++BBI)
708 for (
unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
709 if (BBI->getOperand(i).readsReg())
710 PHIVarInfo[BBI->getOperand(i + 1).getMBB()->getNumber()]
711 .push_back(BBI->getOperand(i).getReg());
720 if (AliveBlocks.test(Num))
729 return findKill(&MBB);
739 E = MBB.
succ_end(); SI != E; ++SI) {
751 switch (OpSuccBlocks.
size()) {
754 for (
unsigned i = 0, e = VI.
Kills.size(); i != e; ++i)
755 if (VI.
Kills[i]->getParent() == SuccMBB)
761 for (
unsigned i = 0, e = VI.
Kills.size(); i != e; ++i)
762 if (VI.
Kills[i]->getParent() == SuccMBB1 ||
763 VI.
Kills[i]->getParent() == SuccMBB2)
768 std::sort(OpSuccBlocks.
begin(), OpSuccBlocks.
end());
769 for (
unsigned i = 0, e = VI.
Kills.size(); i != e; ++i)
770 if (std::binary_search(OpSuccBlocks.
begin(), OpSuccBlocks.
end(),
771 VI.
Kills[i]->getParent()))
788 for (; BBI != BBE && BBI->isPHI(); ++BBI) {
790 Defs.
insert(BBI->getOperand(0).getReg());
793 for (
unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
794 if (BBI->getOperand(i+1).getMBB() == BB)
795 getVarInfo(BBI->getOperand(i).getReg()).AliveBlocks.set(NumNew);
799 for (; BBI != BBE; ++BBI) {
801 E = BBI->operands_end();
I != E; ++
I) {
805 else if (
I->isKill())
pred_reverse_iterator pred_rbegin()
void push_back(const T &Elt)
pred_reverse_iterator pred_rend()
bool removeKill(MachineInstr *MI)
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
static unsigned index2VirtReg(unsigned Index)
bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
INITIALIZE_PASS_BEGIN(LiveVariables,"livevars","Live Variable Analysis", false, false) INITIALIZE_PASS_END(LiveVariables
std::vector< unsigned >::const_iterator livein_iterator
static bool isVirtualRegister(unsigned Reg)
bool isLiveOut(unsigned Reg, const MachineBasicBlock &MBB)
void setIsDead(bool Val=true)
bool isSubRegister(unsigned RegA, unsigned RegB) const
Returns true if RegB is a sub-register of RegA.
LoopInfoBase< BlockT, LoopT > * LI
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
livein_iterator livein_begin() const
unsigned getNumVirtRegs() const
#define INITIALIZE_PASS_DEPENDENCY(depName)
unsigned getNumBlockIDs() const
static MachineOperand CreateReg(unsigned 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)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void replaceKillInstruction(unsigned Reg, MachineInstr *OldMI, MachineInstr *NewMI)
SparseBitVector AliveBlocks
unsigned getNumOperands() const
char & UnreachableMachineBlockElimID
void setIsEarlyClobber(bool Val=true)
const MachineBasicBlock & front() const
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
bool count(PtrType Ptr) const
count - Return true if the specified pointer is in the set.
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
void removeVirtualRegistersKilled(MachineInstr *MI)
bool isInAllocatableClass(unsigned RegNo) const
const MachineBasicBlock * getParent() const
bool isDebugValue() const
bool isEarlyClobber() const
bundle_iterator< MachineInstr, instr_iterator > iterator
MachineOperand * findRegisterDefOperand(unsigned Reg, bool isDead=false, const TargetRegisterInfo *TRI=NULL)
df_ext_iterator< T, SetTy > df_ext_end(const T &G, SetTy &S)
bool isReserved(unsigned PhysReg) const
livein_iterator livein_end() const
const MachineOperand & getOperand(unsigned i) const
df_ext_iterator< T, SetTy > df_ext_begin(const T &G, SetTy &S)
succ_iterator succ_begin()
VarInfo & getVarInfo(unsigned RegIdx)
getVarInfo - Get (possibly creating) a VarInfo object for the given vreg.
pred_iterator pred_begin()
void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *DefBlock, MachineBasicBlock *BB)
void setIsKill(bool Val=true)
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
void addNewBlock(MachineBasicBlock *BB, MachineBasicBlock *DomBB, MachineBasicBlock *SuccBB)
std::vector< MachineInstr * > Kills
void addOperand(MachineFunction &MF, const MachineOperand &Op)
bool isLiveIn(const MachineBasicBlock &MBB, unsigned Reg, MachineRegisterInfo &MRI)
void HandleVirtRegDef(unsigned reg, MachineInstr *MI)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
static bool clobbersPhysReg(const uint32_t *RegMask, unsigned PhysReg)
bool count(const T &V) const
count - Return true if the element is in the set.
MachineInstr * findKill(const MachineBasicBlock *MBB) const
findKill - Find a kill instruction in MBB. Return NULL if none is found.
bundle_iterator< const MachineInstr, const_instr_iterator > const_iterator
static bool isPhysicalRegister(unsigned Reg)
bool isLandingPad() const
MachineRegisterInfo & getRegInfo()
virtual void getAnalysisUsage(AnalysisUsage &AU) const
const TargetMachine & getTarget() const
virtual const TargetRegisterInfo * getRegisterInfo() const
MachineInstr * getVRegDef(unsigned Reg) const
unsigned getReg() const
getReg - Returns the register number.
std::vector< MachineBasicBlock * >::const_iterator const_succ_iterator
virtual bool runOnMachineFunction(MachineFunction &MF)
SparseBitVectorIterator iterator
static const Function * getParent(const Value *V)
BasicBlockListType::iterator iterator
bool addRegisterKilled(unsigned IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
void HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB, MachineInstr *MI)