50 #define DEBUG_TYPE "tailduplication" 52 STATISTIC(NumTails,
"Number of tails duplicated");
53 STATISTIC(NumTailDups,
"Number of tail duplicated blocks");
55 "Number of instructions added due to tail duplication");
57 "Number of instructions removed due to tail duplication");
58 STATISTIC(NumDeadBlocks,
"Number of dead blocks removed");
59 STATISTIC(NumAddedPHIs,
"Number of phis added");
68 "tail-dup-indirect-size",
69 cl::desc(
"Maximum instructions to consider tail duplicating blocks that " 70 "end with indirect branches."),
cl::init(20),
75 cl::desc(
"Verify sanity of PHI instructions during taildup"),
85 bool LayoutModeIn,
unsigned TailDupSizeIn) {
94 TailDupSize = TailDupSizeIn;
96 assert(MBPI !=
nullptr &&
"Machine Branch Probability Info required");
98 LayoutMode = LayoutModeIn;
99 this->PreRegAlloc = PreRegAlloc;
108 while (MI != MBB->
end()) {
113 for (
unsigned i = 1,
e = MI->getNumOperands(); i !=
e; i += 2) {
115 if (PHIBB == PredBB) {
123 dbgs() <<
" missing input from predecessor " 129 for (
unsigned i = 1,
e = MI->getNumOperands(); i !=
e; i += 2) {
131 if (CheckExtra && !Preds.count(PHIBB)) {
134 dbgs() <<
" extra input from predecessor " 169 if (!tailDuplicate(IsSimple, MBB, ForcedLayoutPred, TDBBs, Copies))
182 updateSuccessorsPHIs(MBB, isDead, TDBBs, Succs);
186 NumTailDupRemoved += MBB->
size();
187 removeDeadBlock(MBB, RemovalCallback);
192 if (!SSAUpdateVRs.
empty()) {
193 for (
unsigned i = 0,
e = SSAUpdateVRs.
size(); i !=
e; ++i) {
194 unsigned VReg = SSAUpdateVRs[i];
208 SSAUpdateVals.
find(VReg);
209 for (
unsigned j = 0, ee = LI->second.
size(); j != ee; ++j) {
211 unsigned SrcReg = LI->second[j].second;
235 SSAUpdateVRs.
clear();
236 SSAUpdateVals.
clear();
241 for (
unsigned i = 0,
e = Copies.
size(); i !=
e; ++i) {
256 NumAddedPHIs += NewPHIs.
size();
259 *DuplicatedPreds = std::move(TDBBs);
268 bool MadeChange =
false;
298 if (
UseMI.isDebugValue())
300 if (
UseMI.getParent() != BB)
318 for (
const auto &
MI : BB) {
321 for (
unsigned i = 1,
e =
MI.getNumOperands(); i !=
e; i += 2) {
323 UsedByPhi->
insert(SrcReg);
329 void TailDuplicator::addSSAUpdateEntry(
unsigned OrigReg,
unsigned NewReg,
332 SSAUpdateVals.
find(OrigReg);
333 if (LI != SSAUpdateVals.
end())
334 LI->second.push_back(std::make_pair(BB, NewReg));
337 Vals.push_back(std::make_pair(BB, NewReg));
338 SSAUpdateVals.
insert(std::make_pair(OrigReg, Vals));
345 void TailDuplicator::processPHI(
352 assert(SrcOpIdx &&
"Unable to find matching PHI source?");
363 addSSAUpdateEntry(DefReg, NewDef, PredBB);
377 void TailDuplicator::duplicateInstruction(
384 TII->
get(TargetOpcode::CFI_INSTRUCTION)).addCFIIndex(
403 addSSAUpdateEntry(Reg, NewReg, PredBB);
405 auto VI = LocalVRMap.
find(Reg);
406 if (
VI != LocalVRMap.
end()) {
413 if (
VI->second.SubReg != 0) {
441 if (NewRC ==
nullptr)
445 TII->
get(TargetOpcode::COPY), NewReg)
446 .addReg(
VI->second.Reg, 0,
VI->second.SubReg);
467 void TailDuplicator::updateSuccessorsPHIs(
479 if (MO.
getMBB() == FromBB) {
494 if (MO.
getMBB() == FromBB) {
506 SSAUpdateVals.
find(Reg);
507 if (LI != SSAUpdateVals.
end()) {
509 for (
unsigned j = 0, ee = LI->second.
size(); j != ee; ++j) {
518 unsigned SrcReg = LI->second[j].second;
524 MIB.addReg(SrcReg).addMBB(SrcBB);
529 for (
unsigned j = 0, ee = TDBBs.
size(); j != ee; ++j) {
536 MIB.addReg(Reg).addMBB(SrcBB);
564 unsigned MaxDuplicateCount;
567 if (TailDupSize == 0)
570 MaxDuplicateCount = TailDupSize;
572 MaxDuplicateCount = 1;
580 if (TII->
analyzeBranch(TailBB, PredTBB, PredFBB, PredCond) &&
590 bool HasIndirectbr =
false;
594 if (HasIndirectbr && PreRegAlloc)
607 (TailBB.getParent()->getTarget().getTargetTriple().isOSDarwin() ||
625 if (PreRegAlloc && MI.
isCall())
631 if (InstrCount > MaxDuplicateCount)
644 for (
auto SB : TailBB.successors()) {
645 for (
auto &
I : *SB) {
656 if (HasIndirectbr && PreRegAlloc)
665 return canCompletelyDuplicateBB(TailBB);
675 if (I == TailBB->
end())
677 return I->isUnconditionalBranch();
699 if (!PredCond.
empty())
705 bool TailDuplicator::duplicateSimpleBB(
713 bool Changed =
false;
727 LLVM_DEBUG(
dbgs() <<
"\nTail-duplicating into PredBB: " << *PredBB
728 <<
"From simple Succ: " << *TailBB);
734 if (PredCond.
empty())
744 if (PredFBB == TailBB)
746 if (PredTBB == TailBB)
750 if (PredTBB == PredFBB) {
756 if (PredFBB == NextBB)
758 if (PredTBB == NextBB && PredFBB ==
nullptr)
772 TII->
insertBranch(*PredBB, PredTBB, PredFBB, PredCond, DL);
789 if (!PredCond.
empty())
815 return duplicateSimpleBB(TailBB, TDBBs, UsedByPhi, Copies);
820 bool Changed =
false;
824 assert(TailBB != PredBB &&
825 "Single-block loop should have been rejected earlier!");
831 bool IsLayoutSuccessor =
false;
832 if (ForcedLayoutPred)
833 IsLayoutSuccessor = (ForcedLayoutPred == PredBB);
835 IsLayoutSuccessor =
true;
836 if (IsLayoutSuccessor)
839 LLVM_DEBUG(
dbgs() <<
"\nTail-duplicating into PredBB: " << *PredBB
840 <<
"From Succ: " << *TailBB);
857 processPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi,
true);
861 duplicateInstruction(MI, TailBB, PredBB, LocalVRMap, UsedByPhi);
864 appendCopies(PredBB, CopyInfos, Copies);
866 NumTailDupAdded += TailBB->
size() - 1;
871 "TailDuplicate called on block with multiple successors!");
892 !TII->
analyzeBranch(*PrevBB, PriorTBB, PriorFBB, PriorCond) &&
894 (!PriorTBB || PriorTBB == TailBB) &&
898 <<
"From MBB: " << *TailBB);
908 while (I != TailBB->
end() && I->isPHI()) {
912 processPHI(MI, TailBB, PrevBB, LocalVRMap, CopyInfos, UsedByPhi,
true);
916 while (I != TailBB->
end()) {
920 assert(!MI->
isBundle() &&
"Not expecting bundles before regalloc!");
921 duplicateInstruction(MI, TailBB, PrevBB, LocalVRMap, UsedByPhi);
924 appendCopies(PrevBB, CopyInfos, Copies);
971 while (I != TailBB->
end() && I->isPHI()) {
975 processPHI(MI, TailBB, PredBB, LocalVRMap, CopyInfos, UsedByPhi,
false);
977 appendCopies(PredBB, CopyInfos, Copies);
990 for (
auto &CI : CopyInfos) {
992 .
addReg(CI.second.Reg, 0, CI.second.SubReg);
999 void TailDuplicator::removeDeadBlock(
1005 if (RemovalCallback)
1006 (*RemovalCallback)(MBB);
bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB)
Determine if it is profitable to duplicate this block.
void initMF(MachineFunction &MF, bool PreRegAlloc, const MachineBranchProbabilityInfo *MBPI, const MachineBlockFrequencyInfo *MBFI, ProfileSummaryInfo *PSI, bool LayoutMode, unsigned TailDupSize=0)
Prepare to run on a specific machine function.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
bool isCall(QueryType Type=AnyInBundle) const
static cl::opt< unsigned > TailDuplicateSize("tail-dup-size", cl::desc("Maximum instructions to consider tail duplicating"), cl::init(2), cl::Hidden)
MachineBasicBlock * getMBB() const
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This class represents lattice values for constants.
bool hasOptSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
bool isCFIInstruction() const
void push_back(const T &Elt)
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
iterator getFirstNonDebugInstr()
Returns an iterator to the first non-debug instruction in the basic block, or end().
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
Describe properties that are true of each instruction in the target description file.
Analysis providing profile information.
virtual MachineInstr & duplicate(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const
Clones instruction or the whole instruction bundle Orig and insert into MBB before InsertBefore...
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
unsigned getSubReg() const
static void VerifyPHIs(MachineFunction &MF, bool CheckExtra)
virtual unsigned insertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, const DebugLoc &DL, int *BytesAdded=nullptr) const
Insert branch code into the end of the specified MachineBasicBlock.
An efficient, type-erasing, non-owning reference to a callable.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
STATISTIC(NumFunctions, "Total number of functions")
void RewriteUse(MachineOperand &U)
RewriteUse - Rewrite a use of the symbolic value.
MachineModuleInfo & getMMI() const
static unsigned InstrCount
bool isMetaInstruction() const
Return true if this instruction doesn't produce any output in the form of executable instructions...
virtual unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const
Remove the branching code at the end of the specific MBB.
MachineSSAUpdater - This class updates SSA form for a set of virtual registers defined in multiple bl...
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
iterator_range< succ_iterator > successors()
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
static use_iterator use_end()
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
const TargetRegisterClass * getRegClassConstraint(unsigned OpIdx, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const
Compute the static register class constraint for operand OpIdx.
unsigned getNumOperands() const
Retuns the total number of operands.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
static bool bothUsedInPHI(const MachineBasicBlock &A, const SmallPtrSet< MachineBasicBlock *, 8 > &SuccsB)
bool hasEHPadSuccessor() const
unsigned getCFIIndex() const
bool tailDuplicateBlocks()
Look for small blocks that are unconditionally branched to and do not fall through.
MachineInstr * getVRegDef(unsigned Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
bool isIndirectBranch(QueryType Type=AnyInBundle) const
Return true if this is an indirect branch, such as a branch through a register.
bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
void setReg(Register Reg)
Change the register this operand corresponds to.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
virtual const TargetInstrInfo * getInstrInfo() const
void Initialize(unsigned V)
Initialize - Reset this object to get ready for a new set of SSA updates.
const TargetRegisterClass * constrainRegClass(unsigned Reg, const TargetRegisterClass *RC, unsigned MinNumRegs=0)
constrainRegClass - Constrain the register class of the specified virtual register to be a common sub...
iterator find(const_arg_type_t< KeyT > Val)
bool isReturn(QueryType Type=AnyInBundle) const
static bool isSimpleBB(MachineBasicBlock *TailBB)
True if this BB has only one unconditional jump.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool tailDuplicateAndUpdate(bool IsSimple, MachineBasicBlock *MBB, MachineBasicBlock *ForcedLayoutPred, SmallVectorImpl< MachineBasicBlock *> *DuplicatedPreds=nullptr, function_ref< void(MachineBasicBlock *)> *RemovalCallback=nullptr)
Tail duplicate a single basic block into its predecessors, and then clean up.
initializer< Ty > init(const Ty &Val)
bool erase(const KeyT &Val)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineInstrBuilder & UseMI
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any DBG_VALUE and DBG_LABEL instructions...
unsigned composeSubRegIndices(unsigned a, unsigned b) const
Return the subregister index you get from composing two subregister indices.
std::pair< iterator, bool > insert(const ValueT &V)
bool canTailDuplicate(MachineBasicBlock *TailBB, MachineBasicBlock *PredBB)
Returns true if TailBB can successfully be duplicated into PredBB.
void setMBB(MachineBasicBlock *MBB)
virtual const TargetRegisterClass * getMatchingSuperRegClass(const TargetRegisterClass *A, const TargetRegisterClass *B, unsigned Idx) const
Return a subclass of the specified register class A so that each register in it has a sub-register of...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
self_iterator getIterator()
iterator_range< pred_iterator > predecessors()
succ_iterator succ_begin()
bool hasAddressTaken() const
Test whether this block is potentially the target of an indirect branch.
bool isConvergent(QueryType Type=AnyInBundle) const
Return true if this instruction is convergent.
virtual bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify=false) const
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e...
pred_iterator pred_begin()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setIsKill(bool Val=true)
A SetVector that performs no allocations if smaller than a certain size.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
void addSuccessor(MachineBasicBlock *Succ, BranchProbability Prob=BranchProbability::getUnknown())
Add Succ as a successor of this MachineBasicBlock.
static bool isDefLiveOut(unsigned Reg, MachineBasicBlock *BB, const MachineRegisterInfo *MRI)
bool isDebugValue() const
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
A pair composed of a register and a sub-register index.
MachineInstrBuilder MachineInstrBuilder & DefMI
static unsigned getPHISrcRegOpIdx(MachineInstr *MI, MachineBasicBlock *SrcBB)
unsigned pred_size() const
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static cl::opt< unsigned > TailDupLimit("tail-dup-limit", cl::init(~0U), cl::Hidden)
bool shouldOptimizeForSize(const MachineFunction *MF, ProfileSummaryInfo *PSI, const MachineBlockFrequencyInfo *BFI, PGSOQueryType QueryType=PGSOQueryType::Other)
Returns true if machine function MF is suggested to be size-optimized based on the profile...
void replaceSuccessor(MachineBasicBlock *Old, MachineBasicBlock *New)
Replace successor OLD with NEW and update probability info.
DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
void replaceRegWith(unsigned FromReg, unsigned ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
unsigned succ_size() const
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
use_iterator use_begin(unsigned RegNo) const
LLVM_NODISCARD bool empty() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
void setSubReg(unsigned subReg)
void AddAvailableValue(MachineBasicBlock *BB, unsigned V)
AddAvailableValue - Indicate that a rewritten value is available at the end of the specified block wi...
size_type count(const_arg_type_t< ValueT > V) const
Return 1 if the specified key is in the set, 0 otherwise.
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
bool hasOneNonDBGUse(unsigned RegNo) const
hasOneNonDBGUse - Return true if there is exactly one non-Debug use of the specified register...
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
iterator_range< use_instr_iterator > use_instructions(unsigned Reg) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
static cl::opt< unsigned > TailDupIndirectBranchSize("tail-dup-indirect-size", cl::desc("Maximum instructions to consider tail duplicating blocks that " "end with indirect branches."), cl::init(20), cl::Hidden)
void setRegClass(unsigned Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
void RemoveOperand(unsigned OpNo)
Erase an operand from an instruction, leaving it with one fewer operand than it started with...
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
reg_begin/reg_end - Provide iteration support to walk over all definitions and uses of a register wit...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
static void getRegsUsedByPHIs(const MachineBasicBlock &BB, DenseSet< unsigned > *UsedByPhi)
static cl::opt< bool > TailDupVerify("tail-dup-verify", cl::desc("Verify sanity of PHI instructions during taildup"), cl::init(false), cl::Hidden)
Wrapper class representing virtual and physical registers.
bool isNotDuplicable(QueryType Type=AnyInBundle) const
Return true if this instruction cannot be safely duplicated.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.