16 #define DEBUG_TYPE "phielim"
41 "during PHI elimination"));
88 typedef std::pair<unsigned, unsigned> BBVRegPair;
91 VRegPHIUse VRegPHIUseCount;
99 LoweredPHIMap LoweredPHIs;
103 STATISTIC(NumLowered,
"Number of phis lowered");
104 STATISTIC(NumCriticalEdgesSplit,
"Number of critical edges split");
105 STATISTIC(NumReused,
"Number of reused lowered phis");
111 "Eliminate PHI nodes for register allocation",
117 void PHIElimination::getAnalysisUsage(
AnalysisUsage &AU)
const {
128 LV = getAnalysisIfAvailable<LiveVariables>();
129 LIS = getAnalysisIfAvailable<LiveIntervals>();
131 bool Changed =
false;
141 Changed |= SplitPHIEdges(MF, *
I, MLI);
149 Changed |= EliminatePHINodes(MF, *
I);
153 E = ImpDefs.end();
I != E; ++
I) {
156 if (
MRI->use_nodbg_empty(DefReg)) {
158 LIS->RemoveMachineInstrFromMaps(DefMI);
164 for (LoweredPHIMap::iterator
I = LoweredPHIs.begin(), E = LoweredPHIs.end();
167 LIS->RemoveMachineInstrFromMaps(
I->first);
173 VRegPHIUseCount.clear();
192 LowerPHINode(MBB, LastPHIIt);
203 if (!DI->isImplicitDef())
237 unsigned IncomingReg = 0;
238 bool reusedIncoming =
false;
252 unsigned &entry = LoweredPHIs[MPhi];
256 reusedIncoming =
true;
265 .addReg(IncomingReg);
276 LV->setPHIJoin(IncomingReg);
283 DEBUG(
dbgs() <<
"Remove old kill from " << *OldKill);
284 LV->removeVirtualRegisterKilled(IncomingReg, OldKill);
292 LV->addVirtualRegisterKilled(IncomingReg, PHICopy);
298 LV->removeVirtualRegistersKilled(MPhi);
302 LV->addVirtualRegisterDead(DestReg, PHICopy);
303 LV->removeVirtualRegisterDead(DestReg, MPhi);
310 SlotIndex DestCopyIndex = LIS->InsertMachineInstrInMaps(NewInstr);
312 SlotIndex MBBStartIndex = LIS->getMBBStartIdx(&MBB);
316 LiveInterval &IncomingLI = LIS->createEmptyInterval(IncomingReg);
320 LIS->getVNInfoAllocator());
321 IncomingLI.
addSegment(LiveInterval::Segment(MBBStartIndex,
327 assert(DestLI.
begin() != DestLI.
end() &&
328 "PHIs should have nonempty LiveIntervals.");
334 assert(OrigDestVNI &&
"PHI destination should be live at block entry.");
337 LIS->getVNInfoAllocator());
344 assert(DestVNI &&
"PHI destination should be live at its definition.");
357 for (
int i = NumSrcs - 1; i >= 0; --i) {
363 "Machine PHI Operands must all be virtual registers!");
372 if (!MBBsInsertedInto.
insert(&opBlock))
382 if (!reusedIncoming && IncomingReg) {
394 ImpDefs.insert(DefMI);
398 .addReg(SrcReg, 0, SrcSubReg);
405 if (LV && !SrcUndef &&
406 !VRegPHIUseCount[BBVRegPair(opBlock.
getNumber(), SrcReg)] &&
407 !LV->isLiveOut(SrcReg, opBlock)) {
427 Term != opBlock.
end(); ++Term) {
428 if (Term->readsRegister(SrcReg))
432 if (KillInst == opBlock.
end()) {
435 if (reusedIncoming || !IncomingReg) {
437 KillInst = FirstTerm;
438 while (KillInst != opBlock.
begin()) {
440 if (KillInst->isDebugValue())
442 if (KillInst->readsRegister(SrcReg))
447 KillInst =
prior(InsertPos);
450 assert(KillInst->readsRegister(SrcReg) &&
"Cannot find kill instruction");
453 LV->addVirtualRegisterKilled(SrcReg, KillInst);
456 unsigned opBlockNum = opBlock.
getNumber();
457 LV->getVarInfo(SrcReg).AliveBlocks.reset(opBlockNum);
462 LIS->InsertMachineInstrInMaps(NewSrcInstr);
463 LIS->addSegmentToEndOfBlock(IncomingReg, NewSrcInstr);
467 !VRegPHIUseCount[BBVRegPair(opBlock.
getNumber(), SrcReg)]) {
470 bool isLiveOut =
false;
472 SE = opBlock.
succ_end(); SI != SE; ++SI) {
473 SlotIndex startIdx = LIS->getMBBStartIdx(*SI);
477 if (VNI && VNI->
def != startIdx) {
487 Term != opBlock.
end(); ++Term) {
488 if (Term->readsRegister(SrcReg))
492 if (KillInst == opBlock.
end()) {
495 if (reusedIncoming || !IncomingReg) {
497 KillInst = FirstTerm;
498 while (KillInst != opBlock.
begin()) {
500 if (KillInst->isDebugValue())
502 if (KillInst->readsRegister(SrcReg))
507 KillInst =
prior(InsertPos);
510 assert(KillInst->readsRegister(SrcReg) &&
511 "Cannot find kill instruction");
513 SlotIndex LastUseIndex = LIS->getInstructionIndex(KillInst);
515 LIS->getMBBEndIdx(&opBlock));
522 if (reusedIncoming || !IncomingReg) {
524 LIS->RemoveMachineInstrFromMaps(MPhi);
538 BBI != BBE && BBI->isPHI(); ++BBI)
539 for (
unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2)
540 ++VRegPHIUseCount[BBVRegPair(BBI->getOperand(i+1).getMBB()->getNumber(),
541 BBI->getOperand(i).getReg())];
551 bool IsLoopHeader = CurLoop && &MBB == CurLoop->
getHeader();
553 bool Changed =
false;
555 BBI != BBE && BBI->isPHI(); ++BBI) {
556 for (
unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
557 unsigned Reg = BBI->getOperand(i).getReg();
581 << PreMBB->
getNumber() <<
" -> BB#" << MBB.getNumber()
595 if (!ShouldSplit && CurLoop != PreLoop) {
597 dbgs() <<
"Split wouldn't help, maybe avoid loop copies?\n";
598 if (PreLoop)
dbgs() <<
"PreLoop: " << *PreLoop;
599 if (CurLoop)
dbgs() <<
"CurLoop: " << *CurLoop;
605 ShouldSplit = PreLoop && !PreLoop->
contains(CurLoop);
610 DEBUG(
dbgs() <<
"Failed to split ciritcal edge.\n");
614 ++NumCriticalEdgesSplit;
621 assert((LV || LIS) &&
622 "isLiveIn() requires either LiveVariables or LiveIntervals");
624 return LIS->isLiveInToMBB(LIS->getInterval(Reg), MBB);
626 return LV->isLiveIn(Reg, *MBB);
630 assert((LV || LIS) &&
631 "isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals");
640 SE = MBB->
succ_end(); SI != SE; ++SI) {
641 if (LI.
liveAt(LIS->getMBBStartIdx(*SI)))
646 return LV->isLiveOut(Reg, *MBB);
unsigned succ_size() const
const MachineFunction * getParent() const
static PassRegistry * getPassRegistry()
SlotIndex def
The index of the defining instruction.
MachineBasicBlock * getMBB() const
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
iterator getFirstTerminator()
static bool isVirtualRegister(unsigned Reg)
MachineBasicBlock::iterator findPHICopyInsertPoint(MachineBasicBlock *MBB, MachineBasicBlock *SuccMBB, unsigned SrcReg)
phi node Eliminate PHI nodes for register false
static cl::opt< bool > DisableEdgeSplitting("disable-phi-elim-edge-splitting", cl::init(false), cl::Hidden, cl::desc("Disable critical edge splitting ""during PHI elimination"))
static bool isImplicitlyDefined(unsigned VirtReg, const MachineRegisterInfo *MRI)
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
BlockT * getHeader() const
LoopInfoBase< BlockT, LoopT > * LI
#define INITIALIZE_PASS_DEPENDENCY(depName)
const HexagonInstrInfo * TII
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
const TargetRegisterClass * getRegClass(unsigned Reg) const
std::vector< MachineBasicBlock * >::iterator succ_iterator
MachineBasicBlock * SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P)
ID
LLVM Calling Convention Representation.
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
unsigned getNumOperands() const
bool isDead() const
isDead - Returns true if this is a dead def kill slot.
iterator addSegment(Segment S)
bool isImplicitDef() const
MachineLoop * getLoopFor(const MachineBasicBlock *BB) const
bundle_iterator< MachineInstr, instr_iterator > iterator
void removeValNo(VNInfo *ValNo)
initializer< Ty > init(const Ty &Val)
iterator SkipPHIsAndLabels(iterator I)
const MachineOperand & getOperand(unsigned i) const
ItTy next(ItTy it, Dist n)
bool contains(const LoopT *L) const
for(unsigned i=0, e=MI->getNumOperands();i!=e;++i)
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
succ_iterator succ_begin()
unsigned getSubReg() const
void DeleteMachineInstr(MachineInstr *MI)
bool liveAt(SlotIndex index) const
void initializePHIEliminationPass(PassRegistry &)
const MCInstrDesc & get(unsigned Opcode) const
SmallPtrSetIterator - This implements a const_iterator for SmallPtrSet.
void removeSegment(SlotIndex Start, SlotIndex End, bool RemoveDeadValNo=false)
virtual const TargetInstrInfo * getInstrInfo() const
MachineInstr * remove(MachineInstr *I)
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
INITIALIZE_PASS_BEGIN(PHIElimination,"phi-node-elimination","Eliminate PHI nodes for register allocation", false, false) INITIALIZE_PASS_END(PHIElimination
def_iterator def_begin(unsigned RegNo) const
phi node Eliminate PHI nodes for register allocation
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
bool isLandingPad() const
MachineRegisterInfo & getRegInfo()
VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator)
virtual void getAnalysisUsage(AnalysisUsage &AU) const
IMPLICIT_DEF - This is the MachineInstr-level equivalent of undef.
SlotIndex endIndex() const
const TargetMachine & getTarget() const
SlotIndex getRegSlot(bool EC=false) const
unsigned getReg() const
getReg - Returns the register number.
VNInfo * getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator)
static def_iterator def_end()
static bool isSourceDefinedByImplicitDef(const MachineInstr *MPhi, const MachineRegisterInfo *MRI)
STATISTIC(NumLowered,"Number of phis lowered")
BasicBlockListType::iterator iterator
ItTy prior(ItTy it, Dist n)
const MCRegisterInfo & MRI
static cl::opt< bool > SplitAllCriticalEdges("phi-elim-split-all-critical-edges", cl::init(false), cl::Hidden, cl::desc("Split all critical edges during ""PHI elimination"))
SlotIndex - An opaque wrapper around machine indexes.
DebugLoc getDebugLoc() const