43 #define DEBUG_TYPE "branchfolding"
45 STATISTIC(NumDeadBlocks,
"Number of dead blocks removed");
46 STATISTIC(NumBranchOpts,
"Number of branches optimized");
47 STATISTIC(NumTailMerge ,
"Number of block tails merged");
48 STATISTIC(NumHoist ,
"Number of times common instructions are hoisted");
56 cl::desc(
"Max number of predecessors to consider tail merging"),
63 cl::desc(
"Min number of instructions to consider tail merging"),
88 "Control Flow Optimizer",
false,
false)
91 if (skipOptnoneFunction(*MF.getFunction()))
97 bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
100 getAnalysis<MachineBlockFrequencyInfo>(),
101 getAnalysis<MachineBranchProbabilityInfo>());
103 MF.getSubtarget().getRegisterInfo(),
104 getAnalysisIfAvailable<MachineModuleInfo>());
110 : EnableHoistCommonCode(CommonHoist), MBBFreqInfo(FreqInfo),
113 case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge;
break;
122 assert(MBB->
pred_empty() &&
"MBB must be dead!");
123 DEBUG(
dbgs() <<
"\nRemoving MBB: " << *MBB);
131 TriedMerging.erase(MBB);
149 while (I != MBB->
end()) {
150 if (!I->isImplicitDef())
152 unsigned Reg = I->getOperand(0).getReg();
155 ImpDefRegs.
insert(*SubRegs);
158 if (ImpDefRegs.
empty())
162 while (I != MBB->
end()) {
166 for (
unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
170 unsigned Reg = MO.
getReg();
171 if (ImpDefRegs.
count(Reg))
178 while (I != FirstTerm) {
181 MBB->
erase(ImpDefMI);
193 if (!tii)
return false;
195 TriedMerging.clear();
210 bool MadeChange =
false;
216 MadeChange |= OptimizeImpDefsBlock(MBB);
219 bool MadeChangeThisIteration =
true;
220 while (MadeChangeThisIteration) {
221 MadeChangeThisIteration = TailMergeBlocks(MF);
222 MadeChangeThisIteration |= OptimizeBranches(MF);
223 if (EnableHoistCommonCode)
224 MadeChangeThisIteration |= HoistCommonCode(MF);
225 MadeChange |= MadeChangeThisIteration;
242 for (
unsigned op = 0, e = I->getNumOperands();
op != e; ++
op) {
244 if (!Op.
isJTI())
continue;
253 for (
unsigned i = 0, e = JTIsLive.size(); i != e; ++i)
254 if (!JTIsLive.test(i)) {
276 unsigned OperandHash = 0;
279 OperandHash = Op.
getReg();
282 OperandHash = Op.
getImm();
302 Hash += ((OperandHash << 3) | Op.
getType()) << (i & 31);
326 unsigned TailLen = 0;
327 while (I1 != MBB1->
begin() && I2 != MBB2->
begin()) {
330 while (I1->isDebugValue()) {
331 if (I1==MBB1->
begin()) {
332 while (I2->isDebugValue()) {
333 if (I2==MBB2->
begin())
345 while (I2->isDebugValue()) {
346 if (I2==MBB2->
begin()) {
354 if (!I1->isIdenticalTo(I2) ||
370 if (I1 == MBB1->
begin() && I2 != MBB2->
begin()) {
372 while (I2->isDebugValue()) {
373 if (I2 == MBB2->
begin())
379 if (I2 == MBB2->
begin() && I1 != MBB1->
begin()) {
381 while (I1->isDebugValue()) {
382 if (I1 == MBB1->
begin())
395 if (!CurMBB->
empty())
397 for (
unsigned int i = 1, e = TRI->
getNumRegs(); i != e; i++)
412 MaintainLiveIns(CurMBB, NewDest);
440 NewMBB->
splice(NewMBB->
end(), &CurMBB, BBI1, CurMBB.
end());
443 MBBFreqInfo.setBlockFreq(NewMBB, MBBFreqInfo.getBlockFreq(&CurMBB));
446 MaintainLiveIns(&CurMBB, NewMBB);
456 for (; I != E; ++
I) {
457 if (I->isDebugValue())
461 else if (I->mayLoad() || I->mayStore())
480 if (I != MF->
end() &&
483 if (TBB == NextBB && !Cond.
empty() && !FBB) {
497 if (getHash() < o.getHash())
499 if (getHash() > o.getHash())
501 if (getBlock()->getNumber() < o.getBlock()->getNumber())
503 if (getBlock()->getNumber() > o.getBlock()->getNumber())
507 #ifndef _GLIBCXX_DEBUG
516 auto I = MergedBBFreq.find(MBB);
518 if (I != MergedBBFreq.end())
521 return MBFI.getBlockFreq(MBB);
526 MergedBBFreq[MBB] =
F;
535 unsigned NumTerms = 0;
537 if (I == MBB->
begin()) {
542 if (!I->isTerminator())
break;
554 unsigned minCommonTailLength,
555 unsigned &CommonTailLen,
561 if (CommonTailLen == 0)
564 <<
" and BB#" << MBB2->
getNumber() <<
" is " << CommonTailLen
569 if (MBB1 == PredBB || MBB2 == PredBB) {
572 if (CommonTailLen > NumTerms)
588 unsigned EffectiveTailLen = CommonTailLen;
589 if (SuccBB && MBB1 != PredBB && MBB2 != PredBB &&
595 if (EffectiveTailLen >= minCommonTailLength)
603 if (EffectiveTailLen >= 2 &&
621 unsigned BranchFolder::ComputeSameTails(
unsigned CurHash,
622 unsigned minCommonTailLength,
625 unsigned maxCommonTailLength = 0U;
628 MPIterator HighestMPIter = std::prev(MergePotentials.end());
629 for (MPIterator CurMPIter = std::prev(MergePotentials.end()),
630 B = MergePotentials.begin();
631 CurMPIter != B && CurMPIter->getHash() == CurHash; --CurMPIter) {
632 for (MPIterator I = std::prev(CurMPIter); I->getHash() == CurHash; --
I) {
633 unsigned CommonTailLen;
636 CommonTailLen, TrialBBI1, TrialBBI2,
638 if (CommonTailLen > maxCommonTailLength) {
640 maxCommonTailLength = CommonTailLen;
641 HighestMPIter = CurMPIter;
642 SameTails.push_back(SameTailElt(CurMPIter, TrialBBI1));
644 if (HighestMPIter == CurMPIter &&
645 CommonTailLen == maxCommonTailLength)
646 SameTails.push_back(SameTailElt(I, TrialBBI2));
652 return maxCommonTailLength;
657 void BranchFolder::RemoveBlocksWithHash(
unsigned CurHash,
660 MPIterator CurMPIter, B;
661 for (CurMPIter = std::prev(MergePotentials.end()),
662 B = MergePotentials.begin();
663 CurMPIter->getHash() == CurHash; --CurMPIter) {
666 if (SuccBB && CurMBB != PredBB)
671 if (CurMPIter->getHash() != CurHash)
673 MergePotentials.erase(CurMPIter, MergePotentials.end());
680 unsigned maxCommonTailLength,
681 unsigned &commonTailIndex) {
683 unsigned TimeEstimate = ~0U;
684 for (
unsigned i = 0, e = SameTails.size(); i != e; ++i) {
686 if (SameTails[i].getBlock() == PredBB) {
693 SameTails[i].getTailStartPos());
694 if (t <= TimeEstimate) {
701 SameTails[commonTailIndex].getTailStartPos();
707 << maxCommonTailLength);
720 SameTails[commonTailIndex].setBlock(newMBB);
721 SameTails[commonTailIndex].setTailStartPos(newMBB->
begin());
733 if ((E1 - I1) != (E2 - I2))
735 for (; I1 != E1; ++I1, ++I2) {
752 unsigned CommonTailLen = 0;
753 for (
auto E = MBB->
end(); MBBIStartPos != E; ++MBBIStartPos)
761 while (CommonTailLen--) {
762 assert(MBBI != MBBIE &&
"Reached BB end within common tail length!");
765 if (MBBI->isDebugValue()) {
770 while ((MBBICommon != MBBIECommon) && MBBICommon->isDebugValue())
773 assert(MBBICommon != MBBIECommon &&
774 "Reached BB end within common tail length!");
775 assert(MBBICommon->isIdenticalTo(&*MBBI) &&
"Expected matching MIIs!");
777 if (MBBICommon->mayLoad() || MBBICommon->mayStore())
779 MBBICommon->clearMemRefs();
796 bool MadeChange =
false;
802 DEBUG(
dbgs() <<
"\nTryTailMergeBlocks: ";
803 for (
unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
804 dbgs() <<
"BB#" << MergePotentials[i].getBlock()->getNumber()
805 << (i == e-1 ?
"" :
", ");
808 dbgs() <<
" with successor BB#" << SuccBB->
getNumber() <<
'\n';
810 dbgs() <<
" which has fall-through from BB#"
813 dbgs() <<
"Looking for common tails of at least "
814 << minCommonTailLength <<
" instruction"
815 << (minCommonTailLength == 1 ?
"" :
"s") <<
'\n';
823 while (MergePotentials.size() > 1) {
824 unsigned CurHash = MergePotentials.back().getHash();
828 unsigned maxCommonTailLength = ComputeSameTails(CurHash,
834 if (SameTails.empty()) {
835 RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
845 unsigned commonTailIndex = SameTails.size();
848 if (SameTails.size() == 2 &&
849 SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) &&
850 SameTails[1].tailIsWholeBlock())
852 else if (SameTails.size() == 2 &&
853 SameTails[1].getBlock()->isLayoutSuccessor(
854 SameTails[0].getBlock()) &&
855 SameTails[0].tailIsWholeBlock())
860 for (
unsigned i = 0, e = SameTails.size(); i != e; ++i) {
862 if (MBB == EntryBB && SameTails[i].tailIsWholeBlock())
868 if (SameTails[i].tailIsWholeBlock())
873 if (commonTailIndex == SameTails.size() ||
874 (SameTails[commonTailIndex].getBlock() == PredBB &&
875 !SameTails[commonTailIndex].tailIsWholeBlock())) {
878 if (!CreateCommonTailOnlyBlock(PredBB, SuccBB,
879 maxCommonTailLength, commonTailIndex)) {
880 RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
888 setCommonTailEdgeWeights(*MBB);
894 for (
unsigned int i=0, e = SameTails.size(); i != e; ++i) {
895 if (commonTailIndex == i)
897 DEBUG(
dbgs() <<
"BB#" << SameTails[i].getBlock()->getNumber()
898 << (i == e-1 ?
"" :
", "));
902 ReplaceTailWithBranchTo(SameTails[i].getTailStartPos(), MBB);
904 MergePotentials.erase(SameTails[i].getMPIter());
915 bool MadeChange =
false;
916 if (!EnableTailMerge)
return MadeChange;
919 MergePotentials.clear();
922 if (TriedMerging.count(I))
925 MergePotentials.push_back(MergePotentialsElt(
HashEndOfMBB(I), I));
931 for (
unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
932 TriedMerging.insert(MergePotentials[i].getBlock());
935 if (MergePotentials.size() >= 2)
936 MadeChange |= TryTailMergeBlocks(
nullptr,
nullptr);
959 if (I->pred_size() < 2)
continue;
963 MergePotentials.clear();
968 if (TriedMerging.count(PBB))
976 if (!UniquePreds.
insert(PBB).second)
989 if (!Cond.
empty() && TBB == IBB) {
1006 if (IBB != PredNextBB)
1009 if (TBB != IBB && FBB != IBB)
1011 }
else if (Cond.
empty()) {
1015 if (TBB != IBB && IBB != PredNextBB)
1021 if (TBB && (Cond.
empty() || FBB)) {
1026 TII->
InsertBranch(*PBB, (TBB == IBB) ? FBB : TBB,
nullptr,
1030 MergePotentials.push_back(MergePotentialsElt(
HashEndOfMBB(PBB), *
P));
1037 for (
unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
1038 TriedMerging.insert(MergePotentials[i].getBlock());
1040 if (MergePotentials.size() >= 2)
1041 MadeChange |= TryTailMergeBlocks(IBB, PredBB);
1045 PredBB = std::prev(I);
1046 if (MergePotentials.size() == 1 &&
1047 MergePotentials.begin()->getBlock() != PredBB)
1048 FixTail(MergePotentials.begin()->getBlock(), IBB, TII);
1061 for (
const auto &Src : SameTails) {
1064 AccumulatedMBBFreq += BlockFreq;
1071 auto EdgeFreq = EdgeFreqLs.begin();
1074 SuccI != SuccE; ++SuccI, ++EdgeFreq)
1078 MBBFreqInfo.setBlockFreq(&TailMBB, AccumulatedMBBFreq);
1083 auto MaxEdgeFreq = *std::max_element(EdgeFreqLs.begin(), EdgeFreqLs.end());
1084 uint64_t Scale = MaxEdgeFreq.getFrequency() / UINT32_MAX + 1;
1085 auto EdgeFreq = EdgeFreqLs.begin();
1088 SuccI != SuccE; ++SuccI, ++EdgeFreq)
1089 TailMBB.
setSuccWeight(SuccI, EdgeFreq->getFrequency() / Scale);
1097 bool MadeChange =
false;
1105 MadeChange |= OptimizeBlock(MBB);
1109 RemoveDeadBlock(MBB);
1127 assert(I != MBB->
end() &&
"empty block!");
1128 return I->isBranch();
1143 if (MBB1I == MBB1->
end() || MBB2I == MBB2->
end())
1151 return MBB2I->isCall() && !MBB1I->isCall();
1158 if (I != MBB.
end() && I->isBranch())
1159 return I->getDebugLoc();
1166 bool MadeChange =
false;
1181 if (FallThrough == MF.
end()) {
1183 }
else if (FallThrough->isLandingPad()) {
1198 MJTI->ReplaceMBBInJumpTables(MBB, FallThrough);
1210 bool PriorUnAnalyzable =
1211 TII->
AnalyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond,
true);
1212 if (!PriorUnAnalyzable) {
1215 !PriorCond.
empty());
1220 if (PriorTBB && PriorTBB == PriorFBB) {
1224 if (PriorTBB != MBB)
1225 TII->
InsertBranch(PrevBB, PriorTBB,
nullptr, PriorCond, dl);
1228 goto ReoptimizeBlock;
1241 DEBUG(
dbgs() <<
"\nMerging into block: " << PrevBB
1242 <<
"From MBB: " << *MBB);
1244 if (PrevBB.
begin() != PrevBB.
end()) {
1250 while (PrevBBIter != PrevBB.
begin() && MBBIter != MBB->
end()
1251 && PrevBBIter->isDebugValue() && MBBIter->isDebugValue()) {
1252 if (!MBBIter->isIdenticalTo(PrevBBIter))
1255 ++MBBIter; -- PrevBBIter;
1269 if (PriorTBB == MBB && !PriorFBB) {
1273 goto ReoptimizeBlock;
1278 if (PriorFBB == MBB) {
1281 TII->
InsertBranch(PrevBB, PriorTBB,
nullptr, PriorCond, dl);
1284 goto ReoptimizeBlock;
1290 if (PriorTBB == MBB) {
1295 TII->
InsertBranch(PrevBB, PriorFBB,
nullptr, NewPriorCond, dl);
1298 goto ReoptimizeBlock;
1313 bool DoTransform =
true;
1320 if (FallThrough == --MF.
end() &&
1322 DoTransform =
false;
1328 DEBUG(
dbgs() <<
"\nMoving MBB: " << *MBB
1329 <<
"To make fallthrough to: " << *PriorTBB <<
"\n");
1333 TII->
InsertBranch(PrevBB, MBB,
nullptr, NewPriorCond, dl);
1348 bool CurUnAnalyzable= TII->
AnalyzeBranch(*MBB, CurTBB, CurFBB, CurCond,
true);
1349 if (!CurUnAnalyzable) {
1358 if (CurTBB && CurFBB && CurFBB == MBB && CurTBB != MBB) {
1366 goto ReoptimizeBlock;
1372 if (CurTBB && CurCond.
empty() && !CurFBB &&
1395 if (PredHasNoFallThrough || !PriorUnAnalyzable ||
1399 if (!PredHasNoFallThrough && PrevBB.
isSuccessor(MBB) &&
1400 PriorTBB != MBB && PriorFBB != MBB) {
1402 assert(PriorCond.
empty() && !PriorFBB &&
1403 "Bad branch analysis");
1406 assert(!PriorFBB &&
"Machine CFG out of date!");
1411 TII->
InsertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, pdl);
1416 bool DidChange =
false;
1417 bool HasBranchToSelf =
false;
1423 HasBranchToSelf =
true;
1432 bool NewCurUnAnalyzable = TII->
AnalyzeBranch(*PMBB, NewCurTBB,
1433 NewCurFBB, NewCurCond,
true);
1434 if (!NewCurUnAnalyzable && NewCurTBB && NewCurTBB == NewCurFBB) {
1438 TII->
InsertBranch(*PMBB, NewCurTBB,
nullptr, NewCurCond, pdl);
1448 MJTI->ReplaceMBBInJumpTables(MBB, CurTBB);
1452 if (!HasBranchToSelf)
return MadeChange;
1475 E = MBB->
pred_end(); PI != E; ++PI) {
1482 !TII->
AnalyzeBranch(*PredBB, PredTBB, PredFBB, PredCond,
true)
1483 && (!CurFallsThru || !CurTBB || !CurFBB)
1503 goto ReoptimizeBlock;
1508 if (!CurFallsThru) {
1520 if (SuccBB != MBB && &*SuccPrev != MBB &&
1525 goto ReoptimizeBlock;
1534 if (FallThrough != MF.
end() &&
1535 !TII->
AnalyzeBranch(PrevBB, PrevTBB, PrevFBB, PrevCond,
true) &&
1554 bool MadeChange =
false;
1557 MadeChange |= HoistCommonCodeInSuccs(MBB);
1570 if (SuccBB != TrueBB)
1593 for (
unsigned i = 0, e = Loc->getNumOperands(); i != e; ++i) {
1597 unsigned Reg = MO.
getReg();
1618 if (Loc == MBB->
begin())
1625 while (PI != MBB->
begin() && PI->isDebugValue())
1629 for (
unsigned i = 0, e = PI->getNumOperands(); !IsDef && i != e; ++i) {
1636 unsigned Reg = MO.
getReg();
1639 if (Uses.
count(Reg))
1653 bool DontMoveAcrossStore =
true;
1654 if (!PI->isSafeToMove(
nullptr, DontMoveAcrossStore) || TII->
isPredicated(PI))
1660 for (
unsigned i = 0, e = PI->getNumOperands(); i != e; ++i) {
1664 unsigned Reg = MO.
getReg();
1671 if (Uses.
erase(Reg)) {
1673 Uses.
erase(*SubRegs);
1699 if (TBB->
pred_size() > 1 || FBB->pred_size() > 1)
1708 if (Loc == MBB->
end())
1711 bool HasDups =
false;
1718 while (TIB != TIE && FIB != FIE) {
1720 if (TIB->isDebugValue()) {
1721 while (TIB != TIE && TIB->isDebugValue())
1726 if (FIB->isDebugValue()) {
1727 while (FIB != FIE && FIB->isDebugValue())
1740 for (
unsigned i = 0, e = TIB->getNumOperands(); i != e; ++i) {
1749 unsigned Reg = MO.
getReg();
1753 if (Uses.
count(Reg)) {
1775 }
else if (!LocalDefsSet.
count(Reg)) {
1776 if (Defs.
count(Reg)) {
1791 bool DontMoveAcrossStore =
true;
1792 if (!TIB->isSafeToMove(
nullptr, DontMoveAcrossStore))
1796 for (
unsigned i = 0, e = TIB->getNumOperands(); i != e; ++i) {
1800 unsigned Reg = MO.
getReg();
1801 if (!Reg || !LocalDefsSet.
count(Reg))
1804 LocalDefsSet.
erase(*AI);
1808 for (
unsigned i = 0, e = TIB->getNumOperands(); i != e; ++i) {
1812 unsigned Reg = MO.
getReg();
1817 LocalDefsSet.
insert(*AI);
1829 FBB->erase(FBB->begin(), FIB);
1832 for (
unsigned i = 0, e = LocalDefs.
size(); i != e; ++i) {
1833 unsigned Def = LocalDefs[i];
1834 if (LocalDefsSet.
count(Def)) {
1836 FBB->addLiveIn(Def);
unsigned succ_size() const
static unsigned EstimateRuntime(MachineBasicBlock::iterator I, MachineBasicBlock::iterator E)
EstimateRuntime - Make a rough estimate for how long it will take to run the specified code...
void push_back(const T &Elt)
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
static cl::opt< unsigned > TailMergeThreshold("tail-merge-threshold", cl::desc("Max number of predecessors to consider tail merging"), cl::init(150), cl::Hidden)
static void removeMMOsFromMemoryOperations(MachineBasicBlock::iterator MBBIStartPos, MachineBasicBlock &MBBCommon)
STATISTIC(NumFunctions,"Total number of functions")
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them...
MachineBasicBlock * getMBB() const
int getNumber() const
getNumber - MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a M...
iterator getFirstNonDebugInstr()
getFirstNonDebugInstr - returns an iterator to the first non-debug instruction in the basic block...
iterator getFirstTerminator()
getFirstTerminator - returns an iterator to the first terminator instruction of this basic block...
Address of indexed Jump Table for switch.
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...
void RemoveJumpTable(unsigned Idx)
RemoveJumpTable - Mark the specific index as being dead.
void addLiveIn(unsigned Reg)
Adds the specified register as a live in.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
const_iterator begin(StringRef path)
Get begin iterator over path.
MachineBasicBlock reference.
void moveAfter(MachineBasicBlock *NewBefore)
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
virtual unsigned RemoveBranch(MachineBasicBlock &MBB) const
Remove the branching code at the end of the specific MBB.
static DebugLoc getBranchDebugLoc(MachineBasicBlock &MBB)
getBranchDebugLoc - Find and return, if any, the DebugLoc of the branch instructions on the block...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
const std::vector< MachineJumpTableEntry > & getJumpTables() const
bool isJTI() const
isJTI - Tests if this is a MO_JumpTableIndex operand.
AnalysisUsage & addRequired()
void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New)
ReplaceUsesOfBlockWith - Given a machine basic block that branched to 'Old', change the code and CFG ...
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Name of external global symbol.
std::vector< MachineBasicBlock * >::iterator succ_iterator
Reg
All possible values of the reg field in the ModR/M byte.
static bool IsEmptyBlock(MachineBasicBlock *MBB)
INITIALIZE_PASS(BranchFolderPass,"branch-folder","Control Flow Optimizer", false, false) bool BranchFolderPass
Target-Independent Code Generator Pass Configuration Options.
unsigned getNumOperands() const
Access to explicit operands of the instruction.
void forward()
Move the internal MBB iterator and update register states.
unsigned getNumRegs() const
Return the number of registers this target has (useful for sizing arrays holding per register informa...
void transferSuccessors(MachineBasicBlock *fromMBB)
transferSuccessors - Transfers all the successors from MBB to this machine basic block (i...
static cl::opt< cl::boolOrDefault > FlagEnableTailMerge("enable-tail-merge", cl::init(cl::BOU_UNSET), cl::Hidden)
static void FixTail(MachineBasicBlock *CurMBB, MachineBasicBlock *SuccBB, const TargetInstrInfo *TII)
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
bool canFallThrough()
canFallThrough - Return true if the block can implicitly transfer control to the block after it by fa...
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
iterator getLastNonDebugInstr()
getLastNonDebugInstr - returns an iterator to the last non-debug instruction in the basic block...
void enterBasicBlock(MachineBasicBlock *mbb)
Start tracking liveness from the begin of the specific basic block.
virtual bool ReverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const
Reverses the branch condition of the specified condition list, returning false on success and true if...
std::vector< MachineBasicBlock * >::iterator pred_iterator
static MachineBasicBlock::iterator findHoistingInsertPosAndDeps(MachineBasicBlock *MBB, const TargetInstrInfo *TII, const TargetRegisterInfo *TRI, SmallSet< unsigned, 4 > &Uses, SmallSet< unsigned, 4 > &Defs)
findHoistingInsertPosAndDeps - Find the location to move common instructions in successors to...
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
reverse_iterator rbegin()
const BasicBlock * getBasicBlock() const
getBasicBlock - Return the LLVM basic block that this instance corresponded to originally.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
TargetInstrInfo - Interface to description of machine instruction set.
mmo_iterator memoperands_end() const
bundle_iterator< MachineInstr, instr_iterator > iterator
Address of a global value.
initializer< Ty > init(const Ty &Val)
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
LLVM Basic Block Representation.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
bundle_iterator - MachineBasicBlock iterator that automatically skips over MIs that are inside bundle...
const MachineOperand & getOperand(unsigned i) const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool tracksLiveness() const
tracksLiveness - Returns true when tracking register liveness accurately.
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
bool getEnableTailMerge() const
const MachineBasicBlock * getLandingPadSuccessor() const
getLandingPadSuccessor - If this block has a successor that is a landing pad, return it...
int64_t getOffset() const
Return the offset from the symbol in this operand.
static bool IsBetterFallthrough(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2)
IsBetterFallthrough - Return true if it would be clearly better to fall-through to MBB1 than to fall ...
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
succ_iterator succ_begin()
void removeSuccessor(MachineBasicBlock *succ)
removeSuccessor - Remove successor from the successors list of this MachineBasicBlock.
void moveBefore(MachineBasicBlock *NewAfter)
moveBefore/moveAfter - move 'this' block before or after the specified block.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
pred_iterator pred_begin()
MCSubRegIterator enumerates all sub-registers of Reg.
static unsigned CountTerminators(MachineBasicBlock *MBB, MachineBasicBlock::iterator &I)
CountTerminators - Count the number of terminators in the given block and set I to the position of th...
static cl::opt< unsigned > TailMergeSize("tail-merge-size", cl::desc("Min number of instructions to consider tail merging"), cl::init(3), cl::Hidden)
void setIsKill(bool Val=true)
bool isRegMask() const
isRegMask - Tests if this is a MO_RegisterMask operand.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
static bool hasIdenticalMMOs(const MachineInstr *MI1, const MachineInstr *MI2)
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...
static bool ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2, unsigned minCommonTailLength, unsigned &CommonTailLen, MachineBasicBlock::iterator &I1, MachineBasicBlock::iterator &I2, MachineBasicBlock *SuccBB, MachineBasicBlock *PredBB)
ProfitableToMerge - Check if two machine basic blocks have a common tail and decide if it would be pr...
bool isRegUsed(unsigned Reg, bool includeReserved=true) const
Return if a specific register is currently used.
bool isSuccessor(const MachineBasicBlock *MBB) const
isSuccessor - Return true if the specified MBB is a successor of this block.
virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const
trackLivenessAfterRegAlloc - returns true if the live-ins should be tracked after register allocation...
virtual bool isUnpredicatedTerminator(const MachineInstr *MI) const
Returns true if the instruction is a terminator instruction that has not been predicated.
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
virtual bool isLegalToSplitMBBAt(MachineBasicBlock &MBB, MachineBasicBlock::iterator MBBI) const
Return true if it's legal to split the given basic block at the specified instruction (i...
Representation of each machine instruction.
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
bundle_iterator< const MachineInstr, const_instr_iterator > const_iterator
bool hasAddressTaken() const
hasAddressTaken - Test whether this block is potentially the target of an indirect branch...
bool isLandingPad() const
isLandingPad - Returns true if the block is a landing pad.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
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.
static bool IsBranchOnlyBlock(MachineBasicBlock *MBB)
virtual unsigned InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB, MachineBasicBlock *FBB, ArrayRef< MachineOperand > Cond, DebugLoc DL) const
Insert branch code into the end of the specified MachineBasicBlock.
Abstract Stack Frame Index.
void setSuccWeight(succ_iterator I, uint32_t weight)
Set successor weight of a given iterator.
BranchFolder(bool defaultEnableTailMerge, bool CommonHoist, const MachineBlockFrequencyInfo &MBFI, const MachineBranchProbabilityInfo &MBPI)
static MachineBasicBlock * findFalseBlock(MachineBasicBlock *BB, MachineBasicBlock *TrueBB)
findFalseBlock - BB has a fallthrough.
unsigned getReg() const
getReg - Returns the register number.
void erase(iterator MBBI)
void insert(iterator MBBI, MachineBasicBlock *MBB)
bool operator<(int64_t V1, const APSInt &V2)
std::reverse_iterator< iterator > reverse_iterator
static const Function * getParent(const Value *V)
BasicBlockListType::iterator iterator
char & BranchFolderPassID
BranchFolding - This pass performs machine code CFG based optimizations to delete branches to branche...
static unsigned HashMachineInstr(const MachineInstr *MI)
HashMachineInstr - Compute a hash value for MI and its operands.
std::string Hash(const Unit &U)
virtual bool isPredicated(const MachineInstr *MI) const
Returns true if the instruction is already predicated.
Address of indexed Constant in Constant Pool.
bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii, const TargetRegisterInfo *tri, MachineModuleInfo *mmi)
OptimizeFunction - Perhaps branch folding, tail merging and other CFG optimizations on the given func...
static unsigned HashEndOfMBB(const MachineBasicBlock *MBB)
HashEndOfMBB - Hash the last instruction in the MBB.
virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail, MachineBasicBlock *NewDest) const
Delete the instruction OldInst and everything after it, replacing it with an unconditional branch to ...
BranchProbability getEdgeProbability(const MachineBasicBlock *Src, const MachineBasicBlock *Dst) const
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
isLayoutSuccessor - Return true if the specified MBB will be emitted immediately after this block...
void addSuccessor(MachineBasicBlock *succ, uint32_t weight=0)
addSuccessor - Add succ as a successor of this MachineBasicBlock.
unsigned pred_size() const
bool CorrectExtraCFGEdges(MachineBasicBlock *DestA, MachineBasicBlock *DestB, bool isCond)
CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the CFG to be inserted...
bool isBarrier(QueryType Type=AnyInBundle) const
Returns true if the specified instruction stops control flow from executing the instruction immediate...
MachineModuleInfo - This class contains meta information specific to a module.
static unsigned ComputeCommonTailLength(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2, MachineBasicBlock::iterator &I1, MachineBasicBlock::iterator &I2)
ComputeCommonTailLength - Given two machine basic blocks, compute the number of instructions they act...
mmo_iterator memoperands_begin() const
Access to memory operands of the instruction.