68 #define DEBUG_TYPE "branch-folder" 70 STATISTIC(NumDeadBlocks,
"Number of dead blocks removed");
71 STATISTIC(NumBranchOpts,
"Number of branches optimized");
72 STATISTIC(NumTailMerge ,
"Number of block tails merged");
73 STATISTIC(NumHoist ,
"Number of times common instructions are hoisted");
74 STATISTIC(NumTailCalls,
"Number of tail calls optimized");
82 cl::desc(
"Max number of predecessors to consider tail merging"),
89 cl::desc(
"Min number of instructions to consider tail merging"),
118 "Control Flow Optimizer",
false,
false)
121 if (skipFunction(MF.getFunction()))
127 bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
130 getAnalysis<MachineBlockFrequencyInfo>());
132 getAnalysis<MachineBranchProbabilityInfo>());
134 MF.getSubtarget().getRegisterInfo(),
135 getAnalysisIfAvailable<MachineModuleInfo>());
141 unsigned MinTailLength)
142 : EnableHoistCommonCode(CommonHoist), MinCommonTailLength(MinTailLength),
143 MBBFreqInfo(FreqInfo), MBPI(ProbInfo) {
144 if (MinCommonTailLength == 0)
147 case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge;
break;
163 TriedMerging.erase(MBB);
167 EHScopeMembership.erase(MBB);
177 if (!tii)
return false;
179 TriedMerging.clear();
182 AfterBlockPlacement = AfterPlacement;
194 bool MadeChange =
false;
205 bool MadeChangeThisIteration =
true;
206 while (MadeChangeThisIteration) {
207 MadeChangeThisIteration = TailMergeBlocks(MF);
210 if (!AfterBlockPlacement || MadeChangeThisIteration)
211 MadeChangeThisIteration |= OptimizeBranches(MF);
212 if (EnableHoistCommonCode)
213 MadeChangeThisIteration |= HoistCommonCode(MF);
214 MadeChange |= MadeChangeThisIteration;
228 if (!
Op.isJTI())
continue;
231 JTIsLive.
set(
Op.getIndex());
237 for (
unsigned i = 0, e = JTIsLive.size(); i != e; ++i)
238 if (!JTIsLive.test(i)) {
259 unsigned OperandHash = 0;
262 OperandHash = Op.
getReg();
265 OperandHash = Op.
getImm();
285 Hash += ((OperandHash << 3) | Op.
getType()) << (i & 31);
314 unsigned TailLen = 0;
315 while (I1 != MBB1->
begin() && I2 != MBB2->
begin()) {
319 if (I1==MBB1->
begin()) {
321 if (I2==MBB2->
begin()) {
323 goto SkipTopCFIAndReturn;
329 goto SkipTopCFIAndReturn;
335 if (I2==MBB2->
begin()) {
338 goto SkipTopCFIAndReturn;
343 if (!I1->isIdenticalTo(*I2) ||
359 if (I1 == MBB1->
begin() && I2 != MBB2->
begin()) {
361 while (I2->isDebugInstr()) {
362 if (I2 == MBB2->
begin())
368 if (I2 == MBB2->
begin() && I1 != MBB1->
begin()) {
370 while (I1->isDebugInstr()) {
371 if (I1 == MBB1->
begin())
400 while (I1 != MBB1->
end() && I1->isCFIInstruction()) {
404 while (I2 != MBB2->
end() && I2->isCFIInstruction()) {
423 }
while (I != OldInst);
432 "Can only handle full register.");
437 BuildMI(OldMBB, OldInst, DL, TII->
get(TargetOpcode::IMPLICIT_DEF),
Reg);
465 NewMBB->
splice(NewMBB->
end(), &CurMBB, BBI1, CurMBB.
end());
470 ML->addBasicBlockToLoop(NewMBB, MLI->
getBase());
479 const auto &EHScopeI = EHScopeMembership.find(&CurMBB);
480 if (EHScopeI != EHScopeMembership.end()) {
481 auto n = EHScopeI->second;
482 EHScopeMembership[NewMBB] = n;
493 for (; I !=
E; ++
I) {
498 else if (I->mayLoad() || I->mayStore())
519 if (TBB == NextBB && !Cond.
empty() && !FBB) {
533 if (getHash() < o.getHash())
535 if (getHash() > o.getHash())
537 if (getBlock()->getNumber() < o.getBlock()->getNumber())
539 if (getBlock()->getNumber() > o.getBlock()->getNumber())
543 #ifndef _GLIBCXX_DEBUG 552 auto I = MergedBBFreq.find(MBB);
554 if (
I != MergedBBFreq.end())
557 return MBFI.getBlockFreq(MBB);
562 MergedBBFreq[MBB] =
F;
568 return MBFI.printBlockFreq(OS, getBlockFreq(MBB));
574 return MBFI.printBlockFreq(OS, Freq);
578 MBFI.view(Name, isSimple);
583 return MBFI.getEntryFreq();
592 unsigned NumTerms = 0;
594 if (I == MBB->
begin()) {
599 if (!I->isTerminator())
break;
634 unsigned MinCommonTailLength,
unsigned &CommonTailLen,
639 bool AfterPlacement) {
641 if (!EHScopeMembership.
empty()) {
642 auto EHScope1 = EHScopeMembership.
find(MBB1);
643 assert(EHScope1 != EHScopeMembership.
end());
644 auto EHScope2 = EHScopeMembership.
find(MBB2);
645 assert(EHScope2 != EHScopeMembership.
end());
646 if (EHScope1->second != EHScope2->second)
651 if (CommonTailLen == 0)
655 << CommonTailLen <<
'\n');
662 if ((MBB1 == PredBB || MBB2 == PredBB) &&
663 (!AfterPlacement || MBB1->
succ_size() == 1)) {
666 if (CommonTailLen > NumTerms)
675 if (I1 == MBB1->
begin() && I2 == MBB2->
begin() &&
692 if (AfterPlacement && I1 == MBB1->
begin() && I2 == MBB2->
begin()) {
698 return (MBB != &*MF->
begin()) && std::prev(I)->canFallThrough();
700 if (!BothFallThrough(MBB1) || !BothFallThrough(MBB2))
709 unsigned EffectiveTailLen = CommonTailLen;
710 if (SuccBB && MBB1 != PredBB && MBB2 != PredBB &&
711 (MBB1->
succ_size() == 1 || !AfterPlacement) &&
717 if (EffectiveTailLen >= MinCommonTailLength)
729 unsigned BranchFolder::ComputeSameTails(
unsigned CurHash,
730 unsigned MinCommonTailLength,
733 unsigned maxCommonTailLength = 0U;
736 MPIterator HighestMPIter = std::prev(MergePotentials.end());
737 for (MPIterator CurMPIter = std::prev(MergePotentials.end()),
738 B = MergePotentials.begin();
739 CurMPIter !=
B && CurMPIter->getHash() == CurHash; --CurMPIter) {
740 for (MPIterator
I = std::prev(CurMPIter);
I->getHash() == CurHash; --
I) {
741 unsigned CommonTailLen;
744 CommonTailLen, TrialBBI1, TrialBBI2,
747 AfterBlockPlacement)) {
748 if (CommonTailLen > maxCommonTailLength) {
750 maxCommonTailLength = CommonTailLen;
751 HighestMPIter = CurMPIter;
752 SameTails.push_back(SameTailElt(CurMPIter, TrialBBI1));
754 if (HighestMPIter == CurMPIter &&
755 CommonTailLen == maxCommonTailLength)
756 SameTails.push_back(SameTailElt(
I, TrialBBI2));
762 return maxCommonTailLength;
765 void BranchFolder::RemoveBlocksWithHash(
unsigned CurHash,
768 MPIterator CurMPIter,
B;
769 for (CurMPIter = std::prev(MergePotentials.end()),
770 B = MergePotentials.begin();
771 CurMPIter->getHash() == CurHash; --CurMPIter) {
774 if (SuccBB && CurMBB != PredBB)
779 if (CurMPIter->getHash() != CurHash)
781 MergePotentials.erase(CurMPIter, MergePotentials.end());
786 unsigned maxCommonTailLength,
787 unsigned &commonTailIndex) {
789 unsigned TimeEstimate = ~0U;
790 for (
unsigned i = 0, e = SameTails.size(); i != e; ++i) {
792 if (SameTails[i].getBlock() == PredBB) {
799 SameTails[i].getTailStartPos());
800 if (t <= TimeEstimate) {
807 SameTails[commonTailIndex].getTailStartPos();
811 << maxCommonTailLength);
824 SameTails[commonTailIndex].setBlock(newMBB);
825 SameTails[commonTailIndex].setTailStartPos(newMBB->
begin());
841 unsigned CommonTailLen = 0;
842 for (
auto E = MBB->
end(); MBBIStartPos !=
E; ++MBBIStartPos)
850 while (CommonTailLen--) {
851 assert(MBBI != MBBIE &&
"Reached BB end within common tail length!");
862 assert(MBBICommon != MBBIECommon &&
863 "Reached BB end within common tail length!");
864 assert(MBBICommon->isIdenticalTo(*MBBI) &&
"Expected matching MIIs!");
867 if (MBBICommon->mayLoad() || MBBICommon->mayStore())
868 MBBICommon->cloneMergedMemRefs(*MBB->
getParent(), {&*MBBICommon, &*MBBI});
870 for (
unsigned I = 0,
E = MBBICommon->getNumOperands();
I !=
E; ++
I) {
884 void BranchFolder::mergeCommonTails(
unsigned commonTailIndex) {
887 std::vector<MachineBasicBlock::iterator> NextCommonInsts(SameTails.size());
888 for (
unsigned int i = 0 ; i != SameTails.size() ; ++i) {
889 if (i != commonTailIndex) {
890 NextCommonInsts[i] = SameTails[i].getTailStartPos();
893 assert(SameTails[i].getTailStartPos() == MBB->
begin() &&
894 "MBB is not a common tail only block");
898 for (
auto &
MI : *MBB) {
902 for (
unsigned int i = 0 ; i < NextCommonInsts.size() ; i++) {
903 if (i == commonTailIndex)
906 auto &Pos = NextCommonInsts[i];
907 assert(Pos != SameTails[i].getBlock()->
end() &&
908 "Reached BB end within common tail");
911 assert(Pos != SameTails[i].getBlock()->
end() &&
912 "Reached BB end within common tail");
914 assert(
MI.isIdenticalTo(*Pos) &&
"Expected matching MIIs!");
916 NextCommonInsts[i] = ++Pos;
932 for (
unsigned Reg : NewLiveIns) {
936 BuildMI(*Pred, InsertBefore, DL, TII->
get(TargetOpcode::IMPLICIT_DEF),
957 unsigned MinCommonTailLength) {
958 bool MadeChange =
false;
961 dbgs() <<
"\nTryTailMergeBlocks: ";
962 for (
unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
dbgs()
964 << (i == e - 1 ?
"" :
", ");
965 dbgs() <<
"\n";
if (SuccBB) {
968 dbgs() <<
" which has fall-through from " 970 }
dbgs() <<
"Looking for common tails of at least " 971 << MinCommonTailLength <<
" instruction" 972 << (MinCommonTailLength == 1 ?
"" :
"s") <<
'\n';);
979 while (MergePotentials.size() > 1) {
980 unsigned CurHash = MergePotentials.back().getHash();
984 unsigned maxCommonTailLength = ComputeSameTails(CurHash,
990 if (SameTails.empty()) {
991 RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
1000 &MergePotentials.front().getBlock()->getParent()->front();
1001 unsigned commonTailIndex = SameTails.size();
1004 if (SameTails.size() == 2 &&
1005 SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) &&
1006 SameTails[1].tailIsWholeBlock())
1007 commonTailIndex = 1;
1008 else if (SameTails.size() == 2 &&
1009 SameTails[1].getBlock()->isLayoutSuccessor(
1010 SameTails[0].getBlock()) &&
1011 SameTails[0].tailIsWholeBlock())
1012 commonTailIndex = 0;
1016 for (
unsigned i = 0, e = SameTails.size(); i != e; ++i) {
1018 if (MBB == EntryBB && SameTails[i].tailIsWholeBlock())
1020 if (MBB == PredBB) {
1021 commonTailIndex = i;
1024 if (SameTails[i].tailIsWholeBlock())
1025 commonTailIndex = i;
1029 if (commonTailIndex == SameTails.size() ||
1030 (SameTails[commonTailIndex].getBlock() == PredBB &&
1031 !SameTails[commonTailIndex].tailIsWholeBlock())) {
1034 if (!CreateCommonTailOnlyBlock(PredBB, SuccBB,
1035 maxCommonTailLength, commonTailIndex)) {
1036 RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
1044 setCommonTailEdgeWeights(*MBB);
1048 mergeCommonTails(commonTailIndex);
1054 for (
unsigned int i=0, e = SameTails.size(); i != e; ++i) {
1055 if (commonTailIndex == i)
1058 << (i == e - 1 ?
"" :
", "));
1060 replaceTailWithBranchTo(SameTails[i].getTailStartPos(), *MBB);
1062 MergePotentials.erase(SameTails[i].getMPIter());
1073 bool MadeChange =
false;
1074 if (!EnableTailMerge)
return MadeChange;
1079 if (!AfterBlockPlacement) {
1080 MergePotentials.clear();
1084 if (!TriedMerging.count(&MBB) && MBB.
succ_empty())
1085 MergePotentials.push_back(MergePotentialsElt(
HashEndOfMBB(MBB), &MBB));
1091 for (
unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
1092 TriedMerging.insert(MergePotentials[i].getBlock());
1095 if (MergePotentials.size() >= 2)
1096 MadeChange |= TryTailMergeBlocks(
nullptr,
nullptr, MinCommonTailLength);
1120 if (
I->pred_size() < 2)
continue;
1124 MergePotentials.clear();
1137 if (AfterBlockPlacement && MLI) {
1147 if (TriedMerging.count(PBB))
1155 if (!UniquePreds.
insert(PBB).second)
1159 if (PBB->hasEHPadSuccessor())
1165 if (AfterBlockPlacement && MLI)
1175 if (!Cond.
empty() && TBB == IBB) {
1180 auto Next = ++PBB->getIterator();
1181 if (Next != MF.
end())
1195 if (IBB != PredNextBB)
1198 if (TBB != IBB && FBB != IBB)
1200 }
else if (Cond.
empty()) {
1204 if (TBB != IBB && IBB != PredNextBB)
1210 if (TBB && (Cond.
empty() || FBB)) {
1211 DebugLoc dl = PBB->findBranchDebugLoc();
1215 TII->
insertBranch(*PBB, (TBB == IBB) ? FBB : TBB,
nullptr,
1219 MergePotentials.push_back(MergePotentialsElt(
HashEndOfMBB(*PBB), PBB));
1226 for (
unsigned i = 0, e = MergePotentials.size(); i != e; ++i)
1227 TriedMerging.insert(MergePotentials[i].getBlock());
1229 if (MergePotentials.size() >= 2)
1230 MadeChange |= TryTailMergeBlocks(IBB, PredBB, MinCommonTailLength);
1234 PredBB = &*std::prev(
I);
1235 if (MergePotentials.size() == 1 &&
1236 MergePotentials.begin()->getBlock() != PredBB)
1237 FixTail(MergePotentials.begin()->getBlock(), IBB, TII);
1250 for (
const auto &Src : SameTails) {
1253 AccumulatedMBBFreq += BlockFreq;
1260 auto EdgeFreq = EdgeFreqLs.begin();
1263 SuccI != SuccE; ++SuccI, ++EdgeFreq)
1267 MBBFreqInfo.
setBlockFreq(&TailMBB, AccumulatedMBBFreq);
1273 std::accumulate(EdgeFreqLs.begin(), EdgeFreqLs.end(),
BlockFrequency(0))
1275 auto EdgeFreq = EdgeFreqLs.begin();
1277 if (SumEdgeFreq > 0) {
1279 SuccI != SuccE; ++SuccI, ++EdgeFreq) {
1281 EdgeFreq->getFrequency(), SumEdgeFreq);
1292 bool MadeChange =
false;
1302 MadeChange |= OptimizeBlock(MBB);
1306 RemoveDeadBlock(MBB);
1325 assert(I != MBB->
end() &&
"empty block!");
1326 return I->isBranch();
1341 if (MBB1I == MBB1->
end() || MBB2I == MBB2->
end())
1349 return MBB2I->isCall() && !MBB1I->isCall();
1356 if (I != MBB.
end() && I->isBranch())
1357 return I->getDebugLoc();
1366 if (
MI.isDebugInstr()) {
1368 LLVM_DEBUG(
dbgs() <<
"Copied debug entity from empty block to pred: " 1378 if (
MI.isDebugInstr()) {
1380 LLVM_DEBUG(
dbgs() <<
"Copied debug entity from empty block to succ: " 1409 bool MadeChange =
false;
1417 bool SameEHScope =
true;
1418 if (!EHScopeMembership.empty() && FallThrough != MF.
end()) {
1419 auto MBBEHScope = EHScopeMembership.find(MBB);
1420 assert(MBBEHScope != EHScopeMembership.end());
1421 auto FallThroughEHScope = EHScopeMembership.find(&*FallThrough);
1422 assert(FallThroughEHScope != EHScopeMembership.end());
1423 SameEHScope = MBBEHScope->second == FallThroughEHScope->second;
1436 if (FallThrough == MF.
end()) {
1438 }
else if (FallThrough->isEHPad()) {
1453 MJTI->ReplaceMBBInJumpTables(MBB, &*FallThrough);
1465 bool PriorUnAnalyzable =
1466 TII->
analyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond,
true);
1467 if (!PriorUnAnalyzable) {
1470 !PriorCond.
empty());
1475 if (PriorTBB && PriorTBB == PriorFBB) {
1479 if (PriorTBB != MBB)
1480 TII->
insertBranch(PrevBB, PriorTBB,
nullptr, PriorCond, dl);
1483 goto ReoptimizeBlock;
1497 <<
"From MBB: " << *MBB);
1499 if (PrevBB.
begin() != PrevBB.
end()) {
1505 while (PrevBBIter != PrevBB.
begin() && MBBIter != MBB->
end()
1506 && PrevBBIter->isDebugInstr() && MBBIter->isDebugInstr()) {
1507 if (!MBBIter->isIdenticalTo(*PrevBBIter))
1510 ++MBBIter; -- PrevBBIter;
1524 if (PriorTBB == MBB && !PriorFBB) {
1528 goto ReoptimizeBlock;
1533 if (PriorFBB == MBB) {
1536 TII->
insertBranch(PrevBB, PriorTBB,
nullptr, PriorCond, dl);
1539 goto ReoptimizeBlock;
1545 if (PriorTBB == MBB) {
1550 TII->
insertBranch(PrevBB, PriorFBB,
nullptr, NewPriorCond, dl);
1553 goto ReoptimizeBlock;
1568 bool DoTransform =
true;
1575 if (FallThrough == --MF.
end() &&
1577 DoTransform =
false;
1584 <<
"To make fallthrough to: " << *PriorTBB <<
"\n");
1588 TII->
insertBranch(PrevBB, MBB,
nullptr, NewPriorCond, dl);
1610 bool PredAnalyzable =
1611 !TII->
analyzeBranch(*Pred, PredTBB, PredFBB, PredCond,
true);
1613 if (PredAnalyzable && !PredCond.
empty() && PredTBB == MBB &&
1614 PredTBB != PredFBB) {
1640 bool CurUnAnalyzable =
1642 if (!CurUnAnalyzable) {
1651 if (CurTBB && CurFBB && CurFBB == MBB && CurTBB != MBB) {
1659 goto ReoptimizeBlock;
1665 if (CurTBB && CurCond.
empty() && !CurFBB &&
1688 if (PredHasNoFallThrough || !PriorUnAnalyzable ||
1692 if (!PredHasNoFallThrough && PrevBB.
isSuccessor(MBB) &&
1693 PriorTBB != MBB && PriorFBB != MBB) {
1696 "Bad branch analysis");
1699 assert(!PriorFBB &&
"Machine CFG out of date!");
1704 TII->
insertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, pdl);
1709 bool DidChange =
false;
1710 bool HasBranchToSelf =
false;
1716 HasBranchToSelf =
true;
1726 *PMBB, NewCurTBB, NewCurFBB, NewCurCond,
true);
1727 if (!NewCurUnAnalyzable && NewCurTBB && NewCurTBB == NewCurFBB) {
1731 TII->
insertBranch(*PMBB, NewCurTBB,
nullptr, NewCurCond, pdl);
1741 MJTI->ReplaceMBBInJumpTables(MBB, CurTBB);
1745 if (!HasBranchToSelf)
return MadeChange;
1771 !TII->
analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond,
true) &&
1772 (!CurFallsThru || !CurTBB || !CurFBB) &&
1791 goto ReoptimizeBlock;
1796 if (!CurFallsThru) {
1806 if (SuccBB != MBB && &*SuccPrev != MBB &&
1807 !SuccPrev->canFallThrough() && !CurUnAnalyzable &&
1811 goto ReoptimizeBlock;
1831 if (FallThrough != MF.
end() &&
1832 !FallThrough->isEHPad() &&
1833 !TII->
analyzeBranch(PrevBB, PrevTBB, PrevFBB, PrevCond,
true) &&
1850 bool MadeChange =
false;
1853 MadeChange |= HoistCommonCodeInSuccs(MBB);
1864 if (SuccBB != TrueBB)
1869 template <
class Container>
1900 unsigned Reg = MO.getReg();
1923 if (Loc == MBB->
begin())
1936 if (!MO.isReg() || MO.isUse())
1938 unsigned Reg = MO.getReg();
1941 if (Uses.
count(Reg)) {
1957 bool DontMoveAcrossStore =
true;
1958 if (!PI->isSafeToMove(
nullptr, DontMoveAcrossStore) || TII->
isPredicated(*PI))
1966 unsigned Reg = MO.getReg();
1972 if (Uses.
erase(Reg)) {
1975 Uses.
erase(*SubRegs);
1998 if (TBB->
pred_size() > 1 || FBB->pred_size() > 1)
2007 if (Loc == MBB->
end())
2010 bool HasDups =
false;
2016 while (TIB != TIE && FIB != FIE) {
2020 if (TIB == TIE || FIB == FIE)
2033 if (MO.isRegMask()) {
2039 unsigned Reg = MO.getReg();
2043 if (Uses.
count(Reg)) {
2050 if (Defs.
count(Reg) && !MO.isDead()) {
2065 }
else if (!ActiveDefsSet.
count(Reg)) {
2066 if (Defs.
count(Reg)) {
2072 if (MO.isKill() && Uses.
count(Reg))
2075 MO.setIsKill(
false);
2081 bool DontMoveAcrossStore =
true;
2082 if (!TIB->isSafeToMove(
nullptr, DontMoveAcrossStore))
2087 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
2089 unsigned Reg = MO.getReg();
2092 if (!AllDefsSet.
count(Reg)) {
2097 ActiveDefsSet.
erase(*AI);
2099 ActiveDefsSet.
erase(Reg);
2105 if (!MO.isReg() || !MO.isDef() || MO.isDead())
2107 unsigned Reg = MO.getReg();
2123 FBB->erase(FBB->begin(), FIB);
2125 if (UpdateLiveIns) {
void view(const Twine &Name, bool isSimple=true)
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...
static bool ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2, unsigned MinCommonTailLength, unsigned &CommonTailLen, MachineBasicBlock::iterator &I1, MachineBasicBlock::iterator &I2, MachineBasicBlock *SuccBB, MachineBasicBlock *PredBB, DenseMap< const MachineBasicBlock *, int > &EHScopeMembership, bool AfterPlacement)
ProfitableToMerge - Check if two machine basic blocks have a common tail and decide if it would be pr...
const_iterator end(StringRef path)
Get end iterator over path.
A common definition of LaneBitmask for use in TableGen and CodeGen.
const std::vector< MachineJumpTableEntry > & getJumpTables() const
static cl::opt< unsigned > TailMergeThreshold("tail-merge-threshold", cl::desc("Max number of predecessors to consider tail merging"), cl::init(150), cl::Hidden)
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
MachineBasicBlock * getMBB() const
This class represents lattice values for constants.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them...
static const DILocation * getMergedLocation(const DILocation *LocA, const DILocation *LocB)
When two instructions are combined into a single instruction we also need to combine the original loc...
bool isCFIInstruction() const
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.
unsigned getReg() const
getReg - Returns the register number.
void setIsUndef(bool Val=true)
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
Address of indexed Jump Table for switch.
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...
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.
static unsigned HashMachineInstr(const MachineInstr &MI)
HashMachineInstr - Compute a hash value for MI and its operands.
void RemoveJumpTable(unsigned Idx)
RemoveJumpTable - Mark the specific index as being dead.
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
MachineBasicBlock reference.
STATISTIC(NumFunctions, "Total number of functions")
void moveAfter(MachineBasicBlock *NewBefore)
static DebugLoc getBranchDebugLoc(MachineBasicBlock &MBB)
getBranchDebugLoc - Find and return, if any, the DebugLoc of the branch instructions on the block...
virtual unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const
Remove the branching code at the end of the specific MBB.
BranchFolder(bool defaultEnableTailMerge, bool CommonHoist, MBFIWrapper &FreqInfo, const MachineBranchProbabilityInfo &ProbInfo, unsigned MinTailLength=0)
iterator_range< succ_iterator > successors()
virtual bool isUnpredicatedTerminator(const MachineInstr &MI) const
Returns true if the instruction is a terminator instruction that has not been predicated.
AnalysisUsage & addRequired()
void ReplaceUsesOfBlockWith(MachineBasicBlock *Old, MachineBasicBlock *New)
Given a machine basic block that branched to 'Old', change the code and CFG so that it branches to 'N...
LLVM_NODISCARD bool empty() const
amdgpu Simplify well known AMD library false Value Value const Twine & Name
bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii, const TargetRegisterInfo *tri, MachineModuleInfo *mmi, MachineLoopInfo *mli=nullptr, bool AfterPlacement=false)
Perhaps branch folding, tail merging and other CFG optimizations on the given function.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
INITIALIZE_PASS(BranchFolderPass, DEBUG_TYPE, "Control Flow Optimizer", false, false) bool BranchFolderPass
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
unsigned getNumOperands() const
Retuns the total number of operands.
static void copyDebugInfoToPredecessor(const TargetInstrInfo *TII, MachineBasicBlock &MBB, MachineBasicBlock &PredMBB)
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Name of external global symbol.
static constexpr LaneBitmask getAll()
static unsigned HashEndOfMBB(const MachineBasicBlock &MBB)
HashEndOfMBB - Hash the last instruction in the MBB.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static bool IsEmptyBlock(MachineBasicBlock *MBB)
void removeBlock(MachineBasicBlock *BB)
This method completely removes BB from all data structures, including all of the Loop objects it is n...
Target-Independent Code Generator Pass Configuration Options.
BlockT * getHeader() const
static bool countsAsInstruction(const MachineInstr &MI)
Whether MI should be counted as an instruction when calculating common tail.
static bool isSimple(Instruction *I)
bool isIndirectBranch(QueryType Type=AnyInBundle) const
Return true if this is an indirect branch, such as a branch through a register.
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)
void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs)
Adds registers contained in LiveRegs to the block live-in list of MBB.
bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
static bool blockEndsInUnreachable(const MachineBasicBlock *MBB)
A no successor, non-return block probably ends in unreachable and is cold.
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
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...
void setSuccProbability(succ_iterator I, BranchProbability Prob)
Set successor probability of a given iterator.
MachineBasicBlock * CreateMachineBasicBlock(const BasicBlock *bb=nullptr)
CreateMachineBasicBlock - Allocate a new MachineBasicBlock.
reverse_iterator rbegin()
BasicBlockListType::iterator iterator
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F)
TargetInstrInfo - Interface to description of machine instruction set.
iterator find(const_arg_type_t< KeyT > Val)
static void mergeOperations(MachineBasicBlock::iterator MBBIStartPos, MachineBasicBlock &MBBCommon)
bool isReturn(QueryType Type=AnyInBundle) const
bool getEnableTailMerge() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Address of a global value.
virtual void ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail, MachineBasicBlock *NewDest) const
Delete the instruction OldInst and everything after it, replacing it with an unconditional branch to ...
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.
iterator SkipPHIsAndLabels(iterator I)
Return the first instruction in MBB after I that is not a PHI or a label.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
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...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
void addLiveOuts(const MachineBasicBlock &MBB)
Adds all live-out registers of basic block MBB.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
static void salvageDebugInfoFromEmptyBlock(const TargetInstrInfo *TII, MachineBasicBlock &MBB)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
void init(const TargetRegisterInfo &TRI)
(re-)initializes and clears the set.
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
void stepBackward(const MachineInstr &MI)
Simulates liveness when stepping backwards over an instruction(bundle).
raw_ostream & printBlockFreq(raw_ostream &OS, const MachineBasicBlock *MBB) const
bool optForSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
static bool IsBetterFallthrough(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2)
IsBetterFallthrough - Return true if it would be clearly better to fall-through to MBB1 than to fall ...
self_iterator getIterator()
iterator_range< pred_iterator > predecessors()
uint64_t getEntryFreq() const
succ_iterator succ_begin()
bool hasAddressTaken() const
Test whether this block is potentially the target of an indirect branch.
void moveBefore(MachineBasicBlock *NewAfter)
Move 'this' block before or after the specified block.
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...
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
MCSubRegIterator enumerates all sub-registers of Reg.
pred_iterator pred_begin()
This file implements the LivePhysRegs utility for tracking liveness of physical registers.
virtual bool canMakeTailCallConditional(SmallVectorImpl< MachineOperand > &Cond, const MachineInstr &TailCall) const
Returns true if the tail call can be made conditional on BranchCond.
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...
virtual bool isPredicated(const MachineInstr &MI) const
Returns true if the instruction is already predicated.
bool isDebugInstr() const
static cl::opt< unsigned > TailMergeSize("tail-merge-size", cl::desc("Min number of instructions to consider tail merging"), cl::init(3), cl::Hidden)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void computeLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB)
Computes registers live-in to MBB assuming all of its successors live-in lists are up-to-date...
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.
bool available(const MachineRegisterInfo &MRI, MCPhysReg Reg) const
Returns true if register Reg and no aliasing register is in the set.
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 void addRegAndItsAliases(unsigned Reg, const TargetRegisterInfo *TRI, Container &Set)
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
DenseMap< const MachineBasicBlock *, int > getEHScopeMembership(const MachineFunction &MF)
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
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...
IterT skipDebugInstructionsBackward(IterT It, IterT Begin)
Decrement It until it points to a non-debug instruction or to Begin and return the resulting iterator...
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.
bool isValid() const
isValid - returns true if this iterator is not yet at the end.
DebugLoc findBranchDebugLoc()
Find and return the merged DebugLoc of the branch instructions of the block.
unsigned succ_size() const
void computeAndAddLiveIns(LivePhysRegs &LiveRegs, MachineBasicBlock &MBB)
Convenience function combining computeLiveIns() and addLiveIns().
IterT skipDebugInstructionsForward(IterT It, IterT End)
Increment It until it points to a non-debug instruction or to End and return the resulting iterator...
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.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
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.
A set of physical registers with utility functions to track liveness when walking backward/forward th...
bool isEHPad() const
Returns true if the block is a landing pad.
LLVM_NODISCARD bool empty() const
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
int64_t getOffset() const
Return the offset from the symbol in this operand.
Pair of physical register and lane mask.
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
const MachineBasicBlock & back() const
bool tracksLiveness() const
tracksLiveness - Returns true when tracking register liveness accurately.
static bool IsBranchOnlyBlock(MachineBasicBlock *MBB)
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
Abstract Stack Frame Index.
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
LLVM_NODISCARD bool empty() const
iterator_range< livein_iterator > liveins() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
static MachineBasicBlock * findFalseBlock(MachineBasicBlock *BB, MachineBasicBlock *TrueBB)
findFalseBlock - BB has a fallthrough.
virtual bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const
Reverses the branch condition of the specified condition list, returning false on success and true if...
void erase(iterator MBBI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
void clear()
Clears the set.
bool operator<(int64_t V1, const APSInt &V2)
static void recomputeLiveIns(MachineBasicBlock &MBB)
Convenience function for recomputing live-in's for MBB.
MachineLoop * getLoopFor(const MachineBasicBlock *BB) const
Return the innermost loop that BB lives in.
virtual void replaceBranchWithTailCall(MachineBasicBlock &MBB, SmallVectorImpl< MachineOperand > &Cond, const MachineInstr &TailCall) const
Replace the conditional branch in MBB with a conditional tail call.
char & BranchFolderPassID
BranchFolding - This pass performs machine code CFG based optimizations to delete branches to branche...
This class implements an extremely fast bulk output stream that can only output to a stream...
This class keeps track of branch frequencies of newly created blocks and tail-merged blocks...
bool isBarrier(QueryType Type=AnyInBundle) const
Returns true if the specified instruction stops control flow from executing the instruction immediate...
virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const
Returns true if the live-ins should be tracked after register allocation.
Address of indexed Constant in Constant Pool.
virtual bool isUnconditionalTailCall(const MachineInstr &MI) const
Returns true if MI is an unconditional tail call.
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
const MachineOperand & getOperand(unsigned i) const
static void copyDebugInfoToSuccessor(const TargetInstrInfo *TII, MachineBasicBlock &MBB, MachineBasicBlock &SuccMBB)
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
bool CorrectExtraCFGEdges(MachineBasicBlock *DestA, MachineBasicBlock *DestB, bool IsCond)
Various pieces of code can cause excess edges in the CFG to be inserted.
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...
LoopInfoBase< MachineBasicBlock, MachineLoop > & getBase()
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.