48 #define DEBUG_TYPE "phi-node-elimination" 53 "during PHI elimination"));
62 cl::desc(
"Do not use an early exit if isLiveOutPastPHIs returns true."));
105 using BBVRegPair = std::pair<unsigned, unsigned>;
108 VRegPHIUse VRegPHIUseCount;
114 using LoweredPHIMap =
116 LoweredPHIMap LoweredPHIs;
121 STATISTIC(NumLowered,
"Number of phis lowered");
122 STATISTIC(NumCriticalEdgesSplit,
"Number of critical edges split");
123 STATISTIC(NumReused,
"Number of reused lowered phis");
130 "Eliminate PHI nodes for register allocation",
136 void PHIElimination::getAnalysisUsage(
AnalysisUsage &AU)
const {
148 LV = getAnalysisIfAvailable<LiveVariables>();
149 LIS = getAnalysisIfAvailable<LiveIntervals>();
151 bool Changed =
false;
160 Changed |= SplitPHIEdges(MF, MBB, MLI);
168 Changed |= EliminatePHINodes(MF, MBB);
173 if (
MRI->use_nodbg_empty(DefReg)) {
175 LIS->RemoveMachineInstrFromMaps(*
DefMI);
181 for (
auto &
I : LoweredPHIs) {
183 LIS->RemoveMachineInstrFromMaps(*
I.first);
184 MF.DeleteMachineInstr(
I.first);
189 VRegPHIUseCount.clear();
208 LowerPHINode(MBB, LastPHIIt);
218 if (!DI.isImplicitDef())
250 unsigned IncomingReg = 0;
251 bool reusedIncoming =
false;
261 TII->
get(TargetOpcode::IMPLICIT_DEF), DestReg);
265 unsigned &
entry = LoweredPHIs[MPhi];
269 reusedIncoming =
true;
278 TII->
get(TargetOpcode::COPY), DestReg)
279 .addReg(IncomingReg);
290 LV->setPHIJoin(IncomingReg);
298 LV->removeVirtualRegisterKilled(IncomingReg, *OldKill);
306 LV->addVirtualRegisterKilled(IncomingReg, PHICopy);
312 LV->removeVirtualRegistersKilled(*MPhi);
316 LV->addVirtualRegisterDead(DestReg, PHICopy);
317 LV->removeVirtualRegisterDead(DestReg, *MPhi);
324 LIS->InsertMachineInstrInMaps(*std::prev(AfterPHIsIt));
326 SlotIndex MBBStartIndex = LIS->getMBBStartIdx(&MBB);
330 LiveInterval &IncomingLI = LIS->createEmptyInterval(IncomingReg);
334 LIS->getVNInfoAllocator());
335 IncomingLI.
addSegment(LiveInterval::Segment(MBBStartIndex,
342 "PHIs should have nonempty LiveIntervals.");
348 assert(OrigDestVNI &&
"PHI destination should be live at block entry.");
351 LIS->getVNInfoAllocator());
358 assert(DestVNI &&
"PHI destination should be live at its definition.");
371 for (
int i = NumSrcs - 1; i >= 0; --i) {
377 "Machine PHI Operands must all be virtual registers!");
386 if (!MBBsInsertedInto.
insert(&opBlock).second)
396 if (!reusedIncoming && IncomingReg) {
402 TII->
get(TargetOpcode::IMPLICIT_DEF),
408 ImpDefs.insert(
DefMI);
411 TII->
get(TargetOpcode::COPY), IncomingReg)
412 .addReg(SrcReg, 0, SrcSubReg);
419 if (LV && !SrcUndef &&
420 !VRegPHIUseCount[BBVRegPair(opBlock.
getNumber(), SrcReg)] &&
421 !LV->isLiveOut(SrcReg, opBlock)) {
441 Term != opBlock.
end(); ++Term) {
442 if (Term->readsRegister(SrcReg))
446 if (KillInst == opBlock.
end()) {
449 if (reusedIncoming || !IncomingReg) {
451 KillInst = FirstTerm;
452 while (KillInst != opBlock.
begin()) {
454 if (KillInst->isDebugInstr())
456 if (KillInst->readsRegister(SrcReg))
461 KillInst = std::prev(InsertPos);
464 assert(KillInst->readsRegister(SrcReg) &&
"Cannot find kill instruction");
467 LV->addVirtualRegisterKilled(SrcReg, *KillInst);
470 unsigned opBlockNum = opBlock.
getNumber();
471 LV->getVarInfo(SrcReg).AliveBlocks.reset(opBlockNum);
476 LIS->InsertMachineInstrInMaps(*NewSrcInstr);
477 LIS->addSegmentToEndOfBlock(IncomingReg, *NewSrcInstr);
481 !VRegPHIUseCount[BBVRegPair(opBlock.
getNumber(), SrcReg)]) {
491 if (VNI && VNI->
def != startIdx) {
501 Term != opBlock.
end(); ++Term) {
502 if (Term->readsRegister(SrcReg))
506 if (KillInst == opBlock.
end()) {
509 if (reusedIncoming || !IncomingReg) {
511 KillInst = FirstTerm;
512 while (KillInst != opBlock.
begin()) {
514 if (KillInst->isDebugInstr())
516 if (KillInst->readsRegister(SrcReg))
521 KillInst = std::prev(InsertPos);
524 assert(KillInst->readsRegister(SrcReg) &&
525 "Cannot find kill instruction");
527 SlotIndex LastUseIndex = LIS->getInstructionIndex(*KillInst);
529 LIS->getMBBEndIdx(&opBlock));
536 if (reusedIncoming || !IncomingReg) {
538 LIS->RemoveMachineInstrFromMaps(*MPhi);
548 for (
const auto &MBB : MF)
549 for (
const auto &BBI : MBB) {
552 for (
unsigned i = 1, e = BBI.getNumOperands(); i != e; i += 2)
553 ++VRegPHIUseCount[BBVRegPair(BBI.getOperand(i+1).getMBB()->getNumber(),
554 BBI.getOperand(i).getReg())];
565 bool IsLoopHeader = CurLoop && &MBB == CurLoop->
getHeader();
567 bool Changed =
false;
569 BBI != BBE && BBI->isPHI(); ++BBI) {
570 for (
unsigned i = 1, e = BBI->getNumOperands(); i != e; i += 2) {
571 unsigned Reg = BBI->getOperand(i).getReg();
591 bool ShouldSplit = isLiveOutPastPHIs(Reg, PreMBB);
608 ShouldSplit = ShouldSplit && !isLiveIn(Reg, &MBB);
611 if (!ShouldSplit && CurLoop != PreLoop) {
613 dbgs() <<
"Split wouldn't help, maybe avoid loop copies?\n";
615 dbgs() <<
"PreLoop: " << *PreLoop;
617 dbgs() <<
"CurLoop: " << *CurLoop;
623 ShouldSplit = PreLoop && !PreLoop->
contains(CurLoop);
632 ++NumCriticalEdgesSplit;
640 "isLiveIn() requires either LiveVariables or LiveIntervals");
642 return LIS->isLiveInToMBB(LIS->getInterval(Reg), MBB);
644 return LV->isLiveIn(Reg, *MBB);
647 bool PHIElimination::isLiveOutPastPHIs(
unsigned Reg,
650 "isLiveOutPastPHIs() requires either LiveVariables or LiveIntervals");
659 if (LI.
liveAt(LIS->getMBBStartIdx(
SI)))
663 return LV->isLiveOut(Reg, *MBB);
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
MachineBasicBlock * getMBB() const
const TargetRegisterClass * getRegClass(unsigned Reg) const
Return the register class of the specified virtual register.
SlotIndex def
The index of the defining instruction.
This class represents lattice values for constants.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LiveInterval - This class represents the liveness of a register, or stack slot.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
unsigned getReg() const
getReg - Returns the register number.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
MachineBasicBlock::iterator findPHICopyInsertPoint(MachineBasicBlock *MBB, MachineBasicBlock *SuccMBB, unsigned SrcReg)
findPHICopyInsertPoint - Find a safe place in MBB to insert a copy from SrcReg when following the CFG...
unsigned getSubReg() const
bool isDead() const
isDead - Returns true if this is a dead def kill slot.
VarInfo - This represents the regions where a virtual register is live in the program.
STATISTIC(NumFunctions, "Total number of functions")
unsigned SplitAllCriticalEdges(Function &F, const CriticalEdgeSplittingOptions &Options=CriticalEdgeSplittingOptions())
Loop over all of the edges in the CFG, breaking critical edges as they are found. ...
MachineInstr * findKill(const MachineBasicBlock *MBB) const
findKill - Find a kill instruction in MBB. Return NULL if none is found.
VNInfo - Value Number Information.
iterator_range< succ_iterator > successors()
#define INITIALIZE_PASS_DEPENDENCY(depName)
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
static bool isImplicitlyDefined(unsigned VirtReg, const MachineRegisterInfo &MRI)
Return true if all defs of VirtReg are implicit-defs.
const HexagonInstrInfo * TII
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
unsigned getNumOperands() const
Retuns the total number of operands.
Printable printReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
BlockT * getHeader() const
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
virtual const TargetInstrInfo * getInstrInfo() const
static cl::opt< bool > NoPhiElimLiveOutEarlyExit("no-phi-elim-live-out-early-exit", cl::init(false), cl::Hidden, cl::desc("Do not use an early exit if isLiveOutPastPHIs returns true."))
VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc)
createDeadDef - Make sure the range has a value defined at Def.
TargetInstrInfo - Interface to description of machine instruction set.
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
char & PHIEliminationID
PHIElimination - This pass eliminates machine instruction PHI nodes by inserting copy instructions...
Eliminate PHI nodes for register allocation
void removeValNo(VNInfo *ValNo)
removeValNo - Remove all the segments defined by the specified value#.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
initializer< Ty > init(const Ty &Val)
SlotIndex endIndex() const
endNumber - return the maximum point of the range of the whole, exclusive.
iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
bool liveAt(SlotIndex index) const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Represent the analysis usage information of a pass.
INITIALIZE_PASS_BEGIN(PHIElimination, DEBUG_TYPE, "Eliminate PHI nodes for register allocation", false, false) INITIALIZE_PASS_END(PHIElimination
static bool allPhiOperandsUndefined(const MachineInstr &MPhi, const MachineRegisterInfo &MRI)
Return true if all sources of the phi node are implicit_def's, or undef's.
succ_iterator succ_begin()
void DeleteMachineInstr(MachineInstr *MI)
DeleteMachineInstr - Delete the given MachineInstr.
bool isImplicitDef() const
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
void initializePHIEliminationPass(PassRegistry &)
void removeSegment(SlotIndex Start, SlotIndex End, bool RemoveDeadValNo=false)
Remove the specified segment from this range.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
MachineOperand class - Representation of each machine instruction operand.
MachineInstrBuilder MachineInstrBuilder & DefMI
MachineInstr * remove(MachineInstr *I)
Remove the unbundled instruction from the instruction list without deleting it.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned succ_size() 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.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< typename GraphTraits< GraphType >::nodes_iterator > nodes(const GraphType &G)
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
static cl::opt< bool > DisableEdgeSplitting("disable-phi-elim-edge-splitting", cl::init(false), cl::Hidden, cl::desc("Disable critical edge splitting " "during PHI elimination"))
bool isEHPad() const
Returns true if the block is a landing pad.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
VNInfo * getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator)
getNextValue - Create a new value number and return it.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachineBasicBlock * SplitCriticalEdge(MachineBasicBlock *Succ, Pass &P)
Split the critical edge from this block to the given successor block, and return the newly created bl...
MachineLoop * getLoopFor(const MachineBasicBlock *BB) const
Return the innermost loop that BB lives in.
iterator_range< def_instr_iterator > def_instructions(unsigned Reg) const
static bool isLiveOut(const MachineBasicBlock &MBB, unsigned Reg)
print Instructions which execute on loop entry
for(unsigned i=Desc.getNumOperands(), e=OldMI.getNumOperands();i !=e;++i)
const MachineOperand & getOperand(unsigned i) const
SlotIndex - An opaque wrapper around machine indexes.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
std::vector< MachineBasicBlock * >::iterator succ_iterator
unsigned createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...