31 #define DEBUG_TYPE "ppc-branch-coalescing" 33 STATISTIC(NumBlocksCoalesced,
"Number of blocks coalesced");
34 STATISTIC(NumPHINotMoved,
"Number of PHI Nodes that cannot be merged");
35 STATISTIC(NumBlocksNotCoalesced,
"Number of blocks not coalesced");
137 struct CoalescingCandidateInfo {
145 CoalescingCandidateInfo();
155 bool canCoalesceBranch(CoalescingCandidateInfo &Cand);
158 bool validateCandidates(CoalescingCandidateInfo &SourceRegion,
159 CoalescingCandidateInfo &TargetRegion)
const;
174 StringRef getPassName()
const override {
return "Branch Coalescing"; }
176 bool mergeCandidates(CoalescingCandidateInfo &SourceRegion,
177 CoalescingCandidateInfo &TargetRegion);
182 bool canMerge(CoalescingCandidateInfo &SourceRegion,
183 CoalescingCandidateInfo &TargetRegion)
const;
194 return new PPCBranchCoalescing();
198 "Branch Coalescing",
false,
false)
204 PPCBranchCoalescing::CoalescingCandidateInfo::CoalescingCandidateInfo()
205 : BranchBlock(
nullptr), BranchTargetBlock(
nullptr),
206 FallThroughBlock(
nullptr), MustMoveDown(false), MustMoveUp(false) {}
209 BranchBlock =
nullptr;
210 BranchTargetBlock =
nullptr;
211 FallThroughBlock =
nullptr;
213 MustMoveDown =
false;
218 MDT = &getAnalysis<MachineDominatorTree>();
219 MPDT = &getAnalysis<MachinePostDominatorTree>();
234 bool PPCBranchCoalescing::canCoalesceBranch(CoalescingCandidateInfo &Cand) {
236 << Cand.BranchBlock->getNumber() <<
" can be coalesced:");
245 for (
auto &
I : Cand.BranchBlock->terminators()) {
263 if (
I.getNumOperands() !=
I.getNumExplicitOperands()) {
264 LLVM_DEBUG(
dbgs() <<
"Terminator contains implicit operands - skip : " 270 if (Cand.BranchBlock->isEHPad() || Cand.BranchBlock->hasEHPadSuccessor()) {
277 if (!Cand.BranchTargetBlock || FalseMBB ||
278 !Cand.BranchBlock->isSuccessor(Cand.BranchTargetBlock)) {
284 if (Cand.BranchBlock->succ_size() != 2) {
290 assert(Cand.BranchBlock->canFallThrough() &&
291 "Expecting the block to fall through!");
297 (*Cand.BranchBlock->succ_begin() == Cand.BranchTargetBlock)
298 ? *Cand.BranchBlock->succ_rbegin()
299 : *Cand.BranchBlock->succ_begin();
301 assert(Succ &&
"Expecting a valid fall-through block\n");
303 if (!Succ->empty()) {
304 LLVM_DEBUG(
dbgs() <<
"Fall-through block contains code -- skip\n");
308 if (!Succ->isSuccessor(Cand.BranchTargetBlock)) {
311 <<
"Successor of fall through block is not branch taken block\n");
315 Cand.FallThroughBlock = Succ;
327 bool PPCBranchCoalescing::identicalOperands(
330 if (OpList1.
size() != OpList2.
size()) {
335 for (
unsigned i = 0; i < OpList1.
size(); ++i) {
340 <<
"Op2: " << Op2 <<
"\n");
349 LLVM_DEBUG(
dbgs() <<
"The operands are not provably identical.\n");
364 if (
TII->produceSameValue(*Op1Def, *Op2Def,
MRI)) {
366 <<
" produce the same value!\n");
372 LLVM_DEBUG(
dbgs() <<
"The operands are not provably identical.\n");
396 LLVM_DEBUG(
dbgs() <<
"SourceMBB contains no PHI instructions.\n");
405 if (MO.
getMBB() == SourceMBB)
422 bool PPCBranchCoalescing::canMoveToBeginning(
const MachineInstr &
MI,
426 LLVM_DEBUG(
dbgs() <<
"Checking if " << MI <<
" can move to beginning of " 430 for (
auto &
Use :
MRI->use_instructions(
Def.getReg())) {
431 if (
Use.isPHI() &&
Use.getParent() == &TargetMBB) {
453 bool PPCBranchCoalescing::canMoveToEnd(
const MachineInstr &MI,
457 LLVM_DEBUG(
dbgs() <<
"Checking if " << MI <<
" can move to end of " 468 dbgs() <<
" *** def is in another block -- safe to move!\n");
486 bool PPCBranchCoalescing::validateCandidates(
487 CoalescingCandidateInfo &SourceRegion,
488 CoalescingCandidateInfo &TargetRegion)
const {
490 if (TargetRegion.BranchTargetBlock != SourceRegion.BranchBlock)
491 llvm_unreachable(
"Expecting SourceRegion to immediately follow TargetRegion");
492 else if (!MDT->dominates(TargetRegion.BranchBlock, SourceRegion.BranchBlock))
494 else if (!MPDT->dominates(SourceRegion.BranchBlock, TargetRegion.BranchBlock))
496 else if (!TargetRegion.FallThroughBlock->empty() ||
497 !SourceRegion.FallThroughBlock->empty())
529 bool PPCBranchCoalescing::canMerge(CoalescingCandidateInfo &SourceRegion,
530 CoalescingCandidateInfo &TargetRegion)
const {
531 if (!validateCandidates(SourceRegion, TargetRegion))
537 I = SourceRegion.BranchBlock->instr_begin(),
538 E = SourceRegion.BranchBlock->getFirstNonPHI();
540 for (
auto &
Def :
I->defs())
541 for (
auto &
Use :
MRI->use_instructions(
Def.getReg())) {
542 if (
Use.isPHI() &&
Use.getParent() == SourceRegion.BranchTargetBlock) {
545 <<
" defines register used in another " 546 "PHI within branch target block -- can't merge\n");
550 if (
Use.getParent() == SourceRegion.BranchBlock) {
552 <<
" defines register used in this " 553 "block -- all must move down\n");
554 SourceRegion.MustMoveDown =
true;
562 I = SourceRegion.BranchBlock->getFirstNonPHI(),
563 E = SourceRegion.BranchBlock->end();
565 if (!canMoveToBeginning(*
I, *SourceRegion.BranchTargetBlock)) {
567 <<
" cannot move down - must move up!\n");
568 SourceRegion.MustMoveUp =
true;
570 if (!canMoveToEnd(*
I, *TargetRegion.BranchBlock)) {
572 <<
" cannot move up - must move down!\n");
573 SourceRegion.MustMoveDown =
true;
577 return (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) ?
false :
true;
636 bool PPCBranchCoalescing::mergeCandidates(CoalescingCandidateInfo &SourceRegion,
637 CoalescingCandidateInfo &TargetRegion) {
639 if (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) {
644 if (!validateCandidates(SourceRegion, TargetRegion))
649 moveAndUpdatePHIs(SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock);
654 SourceRegion.BranchBlock->getFirstNonPHI();
656 SourceRegion.BranchBlock->getFirstTerminator();
659 ? SourceRegion.BranchTargetBlock
660 : TargetRegion.BranchBlock;
663 SourceRegion.MustMoveDown
664 ? SourceRegion.BranchTargetBlock->getFirstNonPHI()
665 : TargetRegion.BranchBlock->getFirstTerminator();
667 Source->
splice(Target, SourceRegion.BranchBlock, firstInstr, lastInstr);
674 SourceRegion.BranchBlock->removeSuccessor(SourceRegion.FallThroughBlock);
675 TargetRegion.BranchBlock->transferSuccessorsAndUpdatePHIs(
676 SourceRegion.BranchBlock);
680 TargetRegion.BranchBlock->ReplaceUsesOfBlockWith(
681 SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock);
684 SourceRegion.BranchBlock->terminators().begin();
685 while (I != SourceRegion.BranchBlock->terminators().end()) {
694 assert(TargetRegion.FallThroughBlock->empty() &&
695 "FallThroughBlocks should be empty!");
699 TargetRegion.FallThroughBlock->transferSuccessorsAndUpdatePHIs(
700 SourceRegion.FallThroughBlock);
701 TargetRegion.FallThroughBlock->removeSuccessor(SourceRegion.BranchBlock);
704 assert(SourceRegion.BranchBlock->empty() &&
705 "Expecting branch block to be empty!");
706 SourceRegion.BranchBlock->eraseFromParent();
708 assert(SourceRegion.FallThroughBlock->empty() &&
709 "Expecting fall-through block to be empty!\n");
710 SourceRegion.FallThroughBlock->eraseFromParent();
712 NumBlocksCoalesced++;
721 bool didSomething =
false;
728 CoalescingCandidateInfo Cand1, Cand2;
733 bool MergedCandidates =
false;
735 MergedCandidates =
false;
739 Cand1.BranchBlock = &MBB;
742 if (!canCoalesceBranch(Cand1))
745 Cand2.BranchBlock = Cand1.BranchTargetBlock;
746 if (!canCoalesceBranch(Cand2))
752 assert(MPDT->dominates(Cand2.BranchTargetBlock, Cand1.BranchBlock) &&
753 "Branch-taken block should post-dominate first candidate");
755 if (!identicalOperands(Cand1.Cond, Cand2.Cond)) {
757 <<
" and " << Cand2.BranchBlock->getNumber()
758 <<
" have different branches\n");
761 if (!canMerge(Cand2, Cand1)) {
763 << Cand1.BranchBlock->getNumber() <<
" and " 764 << Cand2.BranchBlock->getNumber() <<
"\n");
765 NumBlocksNotCoalesced++;
768 LLVM_DEBUG(
dbgs() <<
"Merging blocks " << Cand1.BranchBlock->getNumber()
769 <<
" and " << Cand1.BranchTargetBlock->getNumber()
771 MergedCandidates = mergeCandidates(Cand2, Cand1);
772 if (MergedCandidates)
777 }
while (MergedCandidates);
783 MF.verify(
nullptr,
"Error in code produced by branch coalescing");
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
iterator_range< mop_iterator > uses()
Returns a range that includes all operands that are register uses.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
STATISTIC(NumFunctions, "Total number of functions")
void dump() const
dump - Print the current MachineFunction to cerr, useful for debugger use.
bool analyzeBranch(MachineBasicBlock &MBB, MachineBasicBlock *&TBB, MachineBasicBlock *&FBB, SmallVectorImpl< MachineOperand > &Cond, bool AllowModify) const override
Analyze the branching code at the end of MBB, returning true if it cannot be understood (e...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
A Use represents the edge between a Value definition and its users.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
void initializePPCBranchCoalescingPass(PassRegistry &)
FunctionPass * createPPCBranchCoalescingPass()
createPPCBranchCoalescingPass - returns an instance of the Branch Coalescing Pass ...
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
virtual const TargetInstrInfo * getInstrInfo() const
bool isBranch(QueryType Type=AnyInBundle) const
Returns true if this is a conditional, unconditional, or indirect branch.
TargetInstrInfo - Interface to description of machine instruction set.
bool isIdenticalTo(const MachineOperand &Other) const
Returns true if this operand is identical to the specified operand except for liveness related flags ...
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.
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void setMBB(MachineBasicBlock *MBB)
Represent the analysis usage information of a pass.
iterator_range< mop_iterator > defs()
Returns a range over all explicit operands that are register definitions.
FunctionPass class - This class is used to implement most global optimizations.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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...
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.
Target - Wrapper for Target specific information.
static void clear(coro::Shape &Shape)
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.
INITIALIZE_PASS_BEGIN(PPCBranchCoalescing, DEBUG_TYPE, "Branch Coalescing", false, false) INITIALIZE_PASS_END(PPCBranchCoalescing
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.
iterator getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MachinePostDominatorTree - an analysis pass wrapper for DominatorTree used to compute the post-domina...
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...