46 #define DEBUG_TYPE "branchfolding"
48 STATISTIC(NumDeadBlocks,
"Number of dead blocks removed");
49 STATISTIC(NumBranchOpts,
"Number of branches optimized");
50 STATISTIC(NumTailMerge ,
"Number of block tails merged");
51 STATISTIC(NumHoist ,
"Number of times common instructions are hoisted");
59 cl::desc(
"Max number of predecessors to consider tail merging"),
66 cl::desc(
"Min number of instructions to consider tail merging"),
91 "Control Flow Optimizer",
false,
false)
94 if (skipFunction(*MF.getFunction()))
100 bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
103 getAnalysis<MachineBlockFrequencyInfo>());
105 getAnalysis<MachineBranchProbabilityInfo>());
107 MF.getSubtarget().getRegisterInfo(),
108 getAnalysisIfAvailable<MachineModuleInfo>());
114 unsigned MinTailLength)
115 : EnableHoistCommonCode(CommonHoist), MinCommonTailLength(MinTailLength),
116 MBBFreqInfo(FreqInfo), MBPI(ProbInfo) {
117 if (MinCommonTailLength == 0)
120 case cl::BOU_UNSET: EnableTailMerge = defaultEnableTailMerge;
break;
130 DEBUG(
dbgs() <<
"\nRemoving MBB: " << *MBB);
138 TriedMerging.erase(MBB);
142 FuncletMembership.erase(MBB);
155 if (!tii)
return false;
157 TriedMerging.clear();
159 AfterBlockPlacement = AfterPlacement;
171 bool MadeChange =
false;
182 bool MadeChangeThisIteration =
true;
183 while (MadeChangeThisIteration) {
184 MadeChangeThisIteration = TailMergeBlocks(MF);
187 if (!AfterBlockPlacement || MadeChangeThisIteration)
188 MadeChangeThisIteration |= OptimizeBranches(MF);
189 if (EnableHoistCommonCode)
190 MadeChangeThisIteration |= HoistCommonCode(MF);
191 MadeChange |= MadeChangeThisIteration;
205 if (!
Op.isJTI())
continue;
208 JTIsLive.
set(
Op.getIndex());
214 for (
unsigned i = 0, e = JTIsLive.size();
i != e; ++
i)
215 if (!JTIsLive.test(
i)) {
236 unsigned OperandHash = 0;
239 OperandHash = Op.
getReg();
242 OperandHash = Op.
getImm();
262 Hash += ((OperandHash << 3) | Op.
getType()) << (
i & 31);
286 unsigned TailLen = 0;
287 while (I1 != MBB1->
begin() && I2 != MBB2->
begin()) {
290 while (I1->isDebugValue()) {
291 if (I1==MBB1->
begin()) {
292 while (I2->isDebugValue()) {
293 if (I2==MBB2->
begin())
305 while (I2->isDebugValue()) {
306 if (I2==MBB2->
begin()) {
314 if (!I1->isIdenticalTo(*I2) ||
330 if (I1 == MBB1->
begin() && I2 != MBB2->
begin()) {
332 while (I2->isDebugValue()) {
333 if (I2 == MBB2->
begin())
339 if (I2 == MBB2->
begin() && I1 != MBB1->
begin()) {
341 while (I1->isDebugValue()) {
342 if (I1 == MBB1->
begin())
388 NewMBB->
splice(NewMBB->
end(), &CurMBB, BBI1, CurMBB.
end());
393 ML->addBasicBlockToLoop(NewMBB, MLI->
getBase());
402 const auto &FuncletI = FuncletMembership.find(&CurMBB);
403 if (FuncletI != FuncletMembership.end()) {
404 auto n = FuncletI->second;
405 FuncletMembership[NewMBB] = n;
416 for (; I !=
E; ++
I) {
417 if (I->isDebugValue())
421 else if (I->mayLoad() || I->mayStore())
442 if (TBB == NextBB && !Cond.
empty() && !FBB) {
456 if (getHash() < o.getHash())
458 if (getHash() > o.getHash())
460 if (getBlock()->getNumber() < o.getBlock()->getNumber())
462 if (getBlock()->getNumber() > o.getBlock()->getNumber())
466 #ifndef _GLIBCXX_DEBUG
475 auto I = MergedBBFreq.find(MBB);
477 if (
I != MergedBBFreq.end())
485 MergedBBFreq[
MBB] =
F;
491 return MBFI.printBlockFreq(OS, getBlockFreq(MBB));
497 return MBFI.printBlockFreq(OS, Freq);
506 unsigned NumTerms = 0;
508 if (I == MBB->
begin()) {
513 if (!I->isTerminator())
break;
537 unsigned MinCommonTailLength,
unsigned &CommonTailLen,
542 bool AfterPlacement) {
544 if (!FuncletMembership.
empty()) {
545 auto Funclet1 = FuncletMembership.
find(MBB1);
546 assert(Funclet1 != FuncletMembership.
end());
547 auto Funclet2 = FuncletMembership.
find(MBB2);
548 assert(Funclet2 != FuncletMembership.
end());
549 if (Funclet1->second != Funclet2->second)
554 if (CommonTailLen == 0)
557 <<
" and BB#" << MBB2->
getNumber() <<
" is " << CommonTailLen
565 if ((MBB1 == PredBB || MBB2 == PredBB) &&
566 (!AfterPlacement || MBB1->
succ_size() == 1)) {
569 if (CommonTailLen > NumTerms)
587 unsigned EffectiveTailLen = CommonTailLen;
588 if (SuccBB && MBB1 != PredBB && MBB2 != PredBB &&
589 (MBB1->
succ_size() == 1 || !AfterPlacement) &&
595 if (EffectiveTailLen >= MinCommonTailLength)
617 unsigned BranchFolder::ComputeSameTails(
unsigned CurHash,
618 unsigned MinCommonTailLength,
621 unsigned maxCommonTailLength = 0U;
624 MPIterator HighestMPIter = std::prev(MergePotentials.end());
625 for (MPIterator CurMPIter = std::prev(MergePotentials.end()),
626 B = MergePotentials.begin();
627 CurMPIter !=
B && CurMPIter->getHash() == CurHash; --CurMPIter) {
628 for (MPIterator
I = std::prev(CurMPIter);
I->getHash() == CurHash; --
I) {
629 unsigned CommonTailLen;
632 CommonTailLen, TrialBBI1, TrialBBI2,
635 AfterBlockPlacement)) {
636 if (CommonTailLen > maxCommonTailLength) {
638 maxCommonTailLength = CommonTailLen;
639 HighestMPIter = CurMPIter;
640 SameTails.push_back(SameTailElt(CurMPIter, TrialBBI1));
642 if (HighestMPIter == CurMPIter &&
643 CommonTailLen == maxCommonTailLength)
644 SameTails.push_back(SameTailElt(
I, TrialBBI2));
650 return maxCommonTailLength;
655 void BranchFolder::RemoveBlocksWithHash(
unsigned CurHash,
658 MPIterator CurMPIter,
B;
659 for (CurMPIter = std::prev(MergePotentials.end()),
660 B = MergePotentials.begin();
661 CurMPIter->getHash() == CurHash; --CurMPIter) {
664 if (SuccBB && CurMBB != PredBB)
669 if (CurMPIter->getHash() != CurHash)
671 MergePotentials.erase(CurMPIter, MergePotentials.end());
678 unsigned maxCommonTailLength,
679 unsigned &commonTailIndex) {
681 unsigned TimeEstimate = ~0U;
682 for (
unsigned i = 0, e = SameTails.size();
i != e; ++
i) {
684 if (SameTails[
i].getBlock() == PredBB) {
691 SameTails[
i].getTailStartPos());
692 if (t <= TimeEstimate) {
699 SameTails[commonTailIndex].getTailStartPos();
703 << maxCommonTailLength);
716 SameTails[commonTailIndex].setBlock(newMBB);
717 SameTails[commonTailIndex].setTailStartPos(newMBB->
begin());
733 unsigned CommonTailLen = 0;
734 for (
auto E = MBB->
end(); MBBIStartPos !=
E; ++MBBIStartPos)
742 while (CommonTailLen--) {
743 assert(MBBI != MBBIE &&
"Reached BB end within common tail length!");
746 if (MBBI->isDebugValue()) {
751 while ((MBBICommon != MBBIECommon) && MBBICommon->isDebugValue())
754 assert(MBBICommon != MBBIECommon &&
755 "Reached BB end within common tail length!");
756 assert(MBBICommon->isIdenticalTo(*MBBI) &&
"Expected matching MIIs!");
759 if (MBBICommon->mayLoad() || MBBICommon->mayStore())
760 MBBICommon->setMemRefs(MBBICommon->mergeMemRefsWith(*MBBI));
762 for (
unsigned I = 0,
E = MBBICommon->getNumOperands();
I !=
E; ++
I) {
787 unsigned MinCommonTailLength) {
788 bool MadeChange =
false;
790 DEBUG(
dbgs() <<
"\nTryTailMergeBlocks: ";
791 for (
unsigned i = 0, e = MergePotentials.size();
i != e; ++
i)
792 dbgs() <<
"BB#" << MergePotentials[
i].getBlock()->getNumber()
793 << (
i == e-1 ?
"" :
", ");
796 dbgs() <<
" with successor BB#" << SuccBB->
getNumber() <<
'\n';
798 dbgs() <<
" which has fall-through from BB#"
801 dbgs() <<
"Looking for common tails of at least "
802 << MinCommonTailLength <<
" instruction"
803 << (MinCommonTailLength == 1 ?
"" :
"s") <<
'\n';
811 while (MergePotentials.size() > 1) {
812 unsigned CurHash = MergePotentials.back().getHash();
816 unsigned maxCommonTailLength = ComputeSameTails(CurHash,
822 if (SameTails.empty()) {
823 RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
832 &MergePotentials.front().getBlock()->getParent()->front();
833 unsigned commonTailIndex = SameTails.size();
836 if (SameTails.size() == 2 &&
837 SameTails[0].getBlock()->isLayoutSuccessor(SameTails[1].getBlock()) &&
838 SameTails[1].tailIsWholeBlock())
840 else if (SameTails.size() == 2 &&
841 SameTails[1].getBlock()->isLayoutSuccessor(
842 SameTails[0].getBlock()) &&
843 SameTails[0].tailIsWholeBlock())
848 for (
unsigned i = 0, e = SameTails.size();
i != e; ++
i) {
850 if (MBB == EntryBB && SameTails[
i].tailIsWholeBlock())
856 if (SameTails[
i].tailIsWholeBlock())
861 if (commonTailIndex == SameTails.size() ||
862 (SameTails[commonTailIndex].getBlock() == PredBB &&
863 !SameTails[commonTailIndex].tailIsWholeBlock())) {
866 if (!CreateCommonTailOnlyBlock(PredBB, SuccBB,
867 maxCommonTailLength, commonTailIndex)) {
868 RemoveBlocksWithHash(CurHash, SuccBB, PredBB);
876 setCommonTailEdgeWeights(*MBB);
879 for (
auto &
MI : *MBB)
880 if (!
MI.isDebugValue())
885 DEBUG(
dbgs() <<
"\nUsing common tail in BB#" << MBB->getNumber()
887 for (
unsigned int i=0, e = SameTails.size();
i != e; ++
i) {
888 if (commonTailIndex ==
i)
890 DEBUG(
dbgs() <<
"BB#" << SameTails[
i].getBlock()->getNumber()
891 << (
i == e-1 ?
"" :
", "));
895 ReplaceTailWithBranchTo(SameTails[
i].getTailStartPos(), MBB);
897 MergePotentials.erase(SameTails[
i].getMPIter());
908 bool MadeChange =
false;
909 if (!EnableTailMerge)
return MadeChange;
914 if (!AfterBlockPlacement) {
915 MergePotentials.clear();
919 if (!TriedMerging.count(&MBB) && MBB.succ_empty())
920 MergePotentials.push_back(MergePotentialsElt(
HashEndOfMBB(MBB), &MBB));
926 for (
unsigned i = 0, e = MergePotentials.size();
i != e; ++
i)
927 TriedMerging.insert(MergePotentials[
i].getBlock());
930 if (MergePotentials.size() >= 2)
931 MadeChange |= TryTailMergeBlocks(
nullptr,
nullptr, MinCommonTailLength);
955 if (
I->pred_size() < 2)
continue;
959 MergePotentials.clear();
972 if (AfterBlockPlacement && MLI) {
982 if (TriedMerging.count(PBB))
990 if (!UniquePreds.
insert(PBB).second)
994 if (PBB->hasEHPadSuccessor())
1000 if (AfterBlockPlacement && MLI)
1010 if (!Cond.
empty() && TBB == IBB) {
1015 auto Next = ++PBB->getIterator();
1016 if (Next != MF.end())
1030 if (IBB != PredNextBB)
1033 if (TBB != IBB && FBB != IBB)
1035 }
else if (Cond.
empty()) {
1039 if (TBB != IBB && IBB != PredNextBB)
1045 if (TBB && (Cond.
empty() || FBB)) {
1050 TII->
insertBranch(*PBB, (TBB == IBB) ? FBB : TBB,
nullptr,
1054 MergePotentials.push_back(MergePotentialsElt(
HashEndOfMBB(*PBB), PBB));
1061 for (
unsigned i = 0, e = MergePotentials.size();
i != e; ++
i)
1062 TriedMerging.insert(MergePotentials[
i].getBlock());
1064 if (MergePotentials.size() >= 2)
1065 MadeChange |= TryTailMergeBlocks(IBB, PredBB, MinCommonTailLength);
1069 PredBB = &*std::prev(
I);
1070 if (MergePotentials.size() == 1 &&
1071 MergePotentials.begin()->getBlock() != PredBB)
1072 FixTail(MergePotentials.begin()->getBlock(), IBB, TII);
1085 for (
const auto &Src : SameTails) {
1088 AccumulatedMBBFreq += BlockFreq;
1095 auto EdgeFreq = EdgeFreqLs.begin();
1098 SuccI != SuccE; ++SuccI, ++EdgeFreq)
1102 MBBFreqInfo.
setBlockFreq(&TailMBB, AccumulatedMBBFreq);
1108 std::accumulate(EdgeFreqLs.begin(), EdgeFreqLs.end(),
BlockFrequency(0))
1110 auto EdgeFreq = EdgeFreqLs.begin();
1112 if (SumEdgeFreq > 0) {
1114 SuccI != SuccE; ++SuccI, ++EdgeFreq) {
1116 EdgeFreq->getFrequency(), SumEdgeFreq);
1127 bool MadeChange =
false;
1137 MadeChange |= OptimizeBlock(MBB);
1141 RemoveDeadBlock(MBB);
1160 assert(I != MBB->
end() &&
"empty block!");
1161 return I->isBranch();
1176 if (MBB1I == MBB1->
end() || MBB2I == MBB2->
end())
1184 return MBB2I->isCall() && !MBB1I->isCall();
1191 if (I != MBB.
end() && I->isBranch())
1192 return I->getDebugLoc();
1199 bool MadeChange =
false;
1207 bool SameFunclet =
true;
1208 if (!FuncletMembership.empty() && FallThrough != MF.
end()) {
1209 auto MBBFunclet = FuncletMembership.find(MBB);
1210 assert(MBBFunclet != FuncletMembership.end());
1211 auto FallThroughFunclet = FuncletMembership.find(&*FallThrough);
1212 assert(FallThroughFunclet != FuncletMembership.end());
1213 SameFunclet = MBBFunclet->second == FallThroughFunclet->second;
1225 if (FallThrough == MF.
end()) {
1227 }
else if (FallThrough->isEHPad()) {
1242 MJTI->ReplaceMBBInJumpTables(MBB, &*FallThrough);
1254 bool PriorUnAnalyzable =
1255 TII->
analyzeBranch(PrevBB, PriorTBB, PriorFBB, PriorCond,
true);
1256 if (!PriorUnAnalyzable) {
1259 !PriorCond.
empty());
1264 if (PriorTBB && PriorTBB == PriorFBB) {
1268 if (PriorTBB != MBB)
1269 TII->
insertBranch(PrevBB, PriorTBB,
nullptr, PriorCond, dl);
1272 goto ReoptimizeBlock;
1285 DEBUG(
dbgs() <<
"\nMerging into block: " << PrevBB
1286 <<
"From MBB: " << *MBB);
1288 if (PrevBB.
begin() != PrevBB.
end()) {
1294 while (PrevBBIter != PrevBB.
begin() && MBBIter != MBB->
end()
1295 && PrevBBIter->isDebugValue() && MBBIter->isDebugValue()) {
1296 if (!MBBIter->isIdenticalTo(*PrevBBIter))
1299 ++MBBIter; -- PrevBBIter;
1313 if (PriorTBB == MBB && !PriorFBB) {
1317 goto ReoptimizeBlock;
1322 if (PriorFBB == MBB) {
1325 TII->
insertBranch(PrevBB, PriorTBB,
nullptr, PriorCond, dl);
1328 goto ReoptimizeBlock;
1334 if (PriorTBB == MBB) {
1339 TII->
insertBranch(PrevBB, PriorFBB,
nullptr, NewPriorCond, dl);
1342 goto ReoptimizeBlock;
1357 bool DoTransform =
true;
1364 if (FallThrough == --MF.
end() &&
1366 DoTransform =
false;
1372 DEBUG(
dbgs() <<
"\nMoving MBB: " << *MBB
1373 <<
"To make fallthrough to: " << *PriorTBB <<
"\n");
1377 TII->
insertBranch(PrevBB, MBB,
nullptr, NewPriorCond, dl);
1392 bool CurUnAnalyzable =
1394 if (!CurUnAnalyzable) {
1403 if (CurTBB && CurFBB && CurFBB == MBB && CurTBB != MBB) {
1411 goto ReoptimizeBlock;
1417 if (CurTBB && CurCond.
empty() && !CurFBB &&
1440 if (PredHasNoFallThrough || !PriorUnAnalyzable ||
1444 if (!PredHasNoFallThrough && PrevBB.
isSuccessor(MBB) &&
1445 PriorTBB != MBB && PriorFBB !=
MBB) {
1448 "Bad branch analysis");
1451 assert(!PriorFBB &&
"Machine CFG out of date!");
1456 TII->
insertBranch(PrevBB, PriorTBB, PriorFBB, PriorCond, pdl);
1461 bool DidChange =
false;
1462 bool HasBranchToSelf =
false;
1468 HasBranchToSelf =
true;
1478 *PMBB, NewCurTBB, NewCurFBB, NewCurCond,
true);
1479 if (!NewCurUnAnalyzable && NewCurTBB && NewCurTBB == NewCurFBB) {
1483 TII->
insertBranch(*PMBB, NewCurTBB,
nullptr, NewCurCond, pdl);
1493 MJTI->ReplaceMBBInJumpTables(MBB, CurTBB);
1497 if (!HasBranchToSelf)
return MadeChange;
1524 !TII->
analyzeBranch(*PredBB, PredTBB, PredFBB, PredCond,
true) &&
1525 (!CurFallsThru || !CurTBB || !CurFBB) &&
1544 goto ReoptimizeBlock;
1549 if (!CurFallsThru) {
1559 if (SuccBB != MBB && &*SuccPrev != MBB &&
1560 !SuccPrev->canFallThrough() && !CurUnAnalyzable &&
1564 goto ReoptimizeBlock;
1584 if (FallThrough != MF.
end() &&
1585 !FallThrough->isEHPad() &&
1586 !TII->
analyzeBranch(PrevBB, PrevTBB, PrevFBB, PrevCond,
true) &&
1605 bool MadeChange =
false;
1608 MadeChange |= HoistCommonCodeInSuccs(MBB);
1619 if (SuccBB != TrueBB)
1624 template <
class Container>
1655 unsigned Reg = MO.getReg();
1674 if (Loc == MBB->
begin())
1687 if (!MO.isReg() || MO.isUse())
1689 unsigned Reg = MO.getReg();
1692 if (Uses.
count(Reg)) {
1708 bool DontMoveAcrossStore =
true;
1709 if (!PI->isSafeToMove(
nullptr, DontMoveAcrossStore) || TII->
isPredicated(*PI))
1718 unsigned Reg = MO.getReg();
1724 if (Uses.
erase(Reg)) {
1727 Uses.
erase(*SubRegs);
1753 if (TBB->
pred_size() > 1 || FBB->pred_size() > 1)
1762 if (Loc == MBB->
end())
1765 bool HasDups =
false;
1772 while (TIB != TIE && FIB != FIE) {
1776 if (TIB == TIE || FIB == FIE)
1789 if (MO.isRegMask()) {
1795 unsigned Reg = MO.getReg();
1799 if (Uses.
count(Reg)) {
1806 if (Defs.
count(Reg) && !MO.isDead()) {
1821 }
else if (!LocalDefsSet.
count(Reg)) {
1822 if (Defs.
count(Reg)) {
1828 if (MO.isKill() && Uses.
count(Reg))
1831 MO.setIsKill(
false);
1837 bool DontMoveAcrossStore =
true;
1838 if (!TIB->isSafeToMove(
nullptr, DontMoveAcrossStore))
1843 if (!MO.isReg() || !MO.isUse() || !MO.isKill())
1845 unsigned Reg = MO.getReg();
1846 if (!Reg || !LocalDefsSet.
count(Reg))
1850 LocalDefsSet.
erase(*AI);
1852 LocalDefsSet.
erase(Reg);
1858 if (!MO.isReg() || !MO.isDef() || MO.isDead())
1860 unsigned Reg = MO.getReg();
1876 FBB->erase(FBB->begin(), FIB);
1879 bool AddedLiveIns =
false;
1880 for (
unsigned i = 0, e = LocalDefs.
size();
i != e; ++
i) {
1881 unsigned Def = LocalDefs[
i];
1882 if (LocalDefsSet.
count(Def)) {
1884 FBB->addLiveIn(Def);
1885 AddedLiveIns =
true;
1891 FBB->sortUniqueLiveIns();
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
Return the MachineFunction containing this basic block.
bool isEHPad() const
Returns true if the block is a landing pad.
instr_iterator erase(instr_iterator I)
Remove an instruction from the instruction list and delete it.
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
static cl::opt< unsigned > TailMergeThreshold("tail-merge-threshold", cl::desc("Max number of predecessors to consider tail merging"), cl::init(150), cl::Hidden)
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
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
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.
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.
void transferSuccessors(MachineBasicBlock *FromMBB)
Transfers all the successors from MBB to this machine basic block (i.e., copies all the successors Fr...
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...
void computeLiveIns(LivePhysRegs &LiveRegs, const TargetRegisterInfo &TRI, MachineBasicBlock &MBB)
Compute the live-in list for MBB assuming all of its successors live-in lists are up-to-date...
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...
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
static DebugLoc getBranchDebugLoc(MachineBasicBlock &MBB)
getBranchDebugLoc - Find and return, if any, the DebugLoc of the branch instructions on the block...
bool optForSize() const
Optimize this function for size (-Os) or minimum size (-Oz).
BlockT * getHeader() const
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const
getblockFreq - Return block frequency.
iterator_range< succ_iterator > successors()
const std::vector< MachineJumpTableEntry > & getJumpTables() const
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
bool OptimizeFunction(MachineFunction &MF, const TargetInstrInfo *tii, const TargetRegisterInfo *tri, MachineModuleInfo *mmi, MachineLoopInfo *mli=nullptr, bool AfterPlacement=false)
OptimizeFunction - Perhaps branch folding, tail merging and other CFG optimizations on the given func...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
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.
Reg
All possible values of the reg field in the ModR/M byte.
static unsigned HashEndOfMBB(const MachineBasicBlock &MBB)
HashEndOfMBB - Hash the last instruction in the MBB.
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...
INITIALIZE_PASS(BranchFolderPass,"branch-folder","Control Flow Optimizer", false, false) bool BranchFolderPass
Target-Independent Code Generator Pass Configuration Options.
LLVM_NODISCARD bool empty() const
unsigned getNumOperands() const
Access to explicit operands of the instruction.
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 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 canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
const MachineJumpTableInfo * getJumpTableInfo() const
getJumpTableInfo - Return the jump table info object for the current function.
virtual bool isUnpredicatedTerminator(const MachineInstr &MI) const
Returns true if the instruction is a terminator instruction that has not been predicated.
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
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()
const BasicBlock * getBasicBlock() const
Return the LLVM basic block that this instance corresponded to originally.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F)
TargetInstrInfo - Interface to description of machine instruction set.
static void mergeOperations(MachineBasicBlock::iterator MBBIStartPos, MachineBasicBlock &MBBCommon)
MachineLoop * getLoopFor(const MachineBasicBlock *BB) const
Return the innermost loop that BB lives in.
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.
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
unsigned const MachineRegisterInfo * MRI
LLVM Basic Block Representation.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual unsigned removeBranch(MachineBasicBlock &MBB, int *BytesRemoved=nullptr) const
Remove the branching code at the end of the specific MBB.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
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.
virtual bool reverseBranchCondition(SmallVectorImpl< MachineOperand > &Cond) const
Reverses the branch condition of the specified condition list, returning false on success and true if...
MCRegAliasIterator enumerates all registers aliasing Reg.
Represent the analysis usage information of a pass.
void clearLiveIns()
Clear live in list.
bool getEnableTailMerge() const
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 ...
self_iterator getIterator()
iterator_range< pred_iterator > predecessors()
succ_iterator succ_begin()
void moveBefore(MachineBasicBlock *NewAfter)
Move 'this' block before or after the specified block.
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()
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)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
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.
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...
DenseMap< const MachineBasicBlock *, int > getFuncletMembership(const MachineFunction &MF)
static void addRegAndItsAliases(unsigned Reg, const TargetRegisterInfo *TRI, Container &Set)
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
bool isSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB is a successor of this block.
virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const
Returns true if the live-ins should be tracked after register allocation.
void invalidateLiveness()
invalidateLiveness - Indicates that register liveness is no longer being tracked accurately.
void sortUniqueLiveIns()
Sorts and uniques the LiveIns vector.
IterT skipDebugInstructionsBackward(IterT It, IterT Begin)
Decrement It until it points to a non-debug instruction or to Begin and return the resulting iterator...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
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.
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.
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
bool hasAddressTaken() const
Test whether this block is potentially the target of an indirect branch.
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.
raw_ostream & printBlockFreq(raw_ostream &OS, const MachineBasicBlock *MBB) const
BranchFolder(bool defaultEnableTailMerge, bool CommonHoist, MBFIWrapper &MBFI, const MachineBranchProbabilityInfo &MBPI, unsigned MinCommonTailLength=0)
virtual bool isPredicated(const MachineInstr &MI) const
Returns true if the instruction is already predicated.
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
iterator find(const KeyT &Val)
static bool IsBranchOnlyBlock(MachineBasicBlock *MBB)
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
static MachineBasicBlock * findFalseBlock(MachineBasicBlock *BB, MachineBasicBlock *TrueBB)
findFalseBlock - BB has a fallthrough.
unsigned getReg() const
getReg - Returns the register number.
void erase(iterator MBBI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void insert(iterator MBBI, MachineBasicBlock *MBB)
bool operator<(int64_t V1, const APSInt &V2)
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 > &FuncletMembership, bool AfterPlacement)
ProfitableToMerge - Check if two machine basic blocks have a common tail and decide if it would be pr...
BasicBlockListType::iterator iterator
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...
const MachineBasicBlock & back() const
std::string Hash(const Unit &U)
This class keeps track of branch frequencies of newly created blocks and tail-merged blocks...
Address of indexed Constant in Constant Pool.
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
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
unsigned pred_size() const
bool isBarrier(QueryType Type=AnyInBundle) const
Returns true if the specified instruction stops control flow from executing the instruction immediate...
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()