91using namespace PatternMatch;
93#define DEBUG_TYPE "simplifycfg"
96 "simplifycfg-require-and-preserve-domtree",
cl::Hidden,
98 cl::desc(
"Temorary development switch used to gradually uplift SimplifyCFG "
99 "into preserving DomTree,"));
108 "Control the amount of phi node folding to perform (default = 2)"));
112 cl::desc(
"Control the maximal total instruction cost that we are willing "
113 "to speculatively execute to fold a 2-entry PHI node into a "
114 "select (default = 4)"));
118 cl::desc(
"Hoist common instructions up to the parent block"));
123 cl::desc(
"Allow reordering across at most this many "
124 "instructions when hoisting"));
128 cl::desc(
"Sink common instructions down to the end block"));
132 cl::desc(
"Hoist conditional stores if an unconditional store precedes"));
136 cl::desc(
"Hoist conditional stores even if an unconditional store does not "
137 "precede - hoist multiple conditional stores into a single "
138 "predicated store"));
142 cl::desc(
"When merging conditional stores, do so even if the resultant "
143 "basic blocks are unlikely to be if-converted as a result"));
147 cl::desc(
"Allow exactly one expensive instruction to be speculatively "
152 cl::desc(
"Limit maximum recursion depth when calculating costs of "
153 "speculatively executed instructions"));
158 cl::desc(
"Max size of a block which is still considered "
159 "small enough to thread through"));
165 cl::desc(
"Maximum cost of combining conditions when "
166 "folding branches"));
169 "simplifycfg-branch-fold-common-dest-vector-multiplier",
cl::Hidden,
171 cl::desc(
"Multiplier to apply to threshold when determining whether or not "
172 "to fold branch to common destination when vector operations are "
177 cl::desc(
"Allow SimplifyCFG to merge invokes together when appropriate"));
181 cl::desc(
"Limit cases to analyze when converting a switch to select"));
183STATISTIC(NumBitMaps,
"Number of switch instructions turned into bitmaps");
185 "Number of switch instructions turned into linear mapping");
187 "Number of switch instructions turned into lookup tables");
189 NumLookupTablesHoles,
190 "Number of switch instructions turned into lookup tables (holes checked)");
191STATISTIC(NumTableCmpReuses,
"Number of reused switch table lookup compares");
193 "Number of value comparisons folded into predecessor basic blocks");
195 "Number of branches folded into predecessor basic block");
198 "Number of common instruction 'blocks' hoisted up to the begin block");
200 "Number of common instructions hoisted up to the begin block");
202 "Number of common instruction 'blocks' sunk down to the end block");
204 "Number of common instructions sunk down to the end block");
205STATISTIC(NumSpeculations,
"Number of speculative executed instructions");
207 "Number of invokes with empty resume blocks simplified into calls");
208STATISTIC(NumInvokesMerged,
"Number of invokes that were merged together");
209STATISTIC(NumInvokeSetsFormed,
"Number of invoke sets that were formed");
216using SwitchCaseResultVectorTy =
225struct ValueEqualityComparisonCase {
232 bool operator<(ValueEqualityComparisonCase RHS)
const {
240class SimplifyCFGOpt {
250 Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases);
251 bool simplifyEqualityComparisonWithOnlyPredecessor(
Instruction *TI,
254 bool performValueComparisonIntoPredecessorFolding(
Instruction *TI,
Value *&CV,
257 bool foldValueComparisonIntoPredecessors(
Instruction *TI,
271 bool tryToSimplifyUncondBranchWithICmpInIt(
ICmpInst *ICI,
274 bool hoistCommonCodeFromSuccessors(
BasicBlock *BB,
bool EqTermsOnly);
275 bool hoistSuccIdenticalTerminatorToSwitchOrIf(
294 "SimplifyCFG is not yet capable of maintaining validity of a "
295 "PostDomTree, so don't ask for it.");
302 bool requestResimplify() {
321 "Only for a pair of incoming blocks at the time!");
327 Value *IV0 = PN.getIncomingValueForBlock(IncomingBlocks[0]);
328 Value *IV1 = PN.getIncomingValueForBlock(IncomingBlocks[1]);
331 if (EquivalenceSet && EquivalenceSet->contains(IV0) &&
332 EquivalenceSet->contains(IV1))
355 if (!SI1Succs.
count(Succ))
361 FailBlocks->insert(Succ);
377 PN.addIncoming(PN.getIncomingValueForBlock(ExistPred), NewPred);
379 if (
auto *MPhi = MSSAU->getMemorySSA()->getMemoryAccess(Succ))
380 MPhi->addIncoming(MPhi->getIncomingValueForBlock(ExistPred), NewPred);
389 assert((!isa<Instruction>(
I) ||
391 "Instruction is not safe to speculatively execute!");
417 unsigned Depth = 0) {
446 if (AggressiveInsts.
count(
I))
470 for (
Use &
Op :
I->operands())
484 if (CI || !isa<Constant>(V) || !V->getType()->isPointerTy() ||
485 DL.isNonIntegralPointerType(V->getType()))
490 IntegerType *PtrTy = cast<IntegerType>(
DL.getIntPtrType(V->getType()));
493 if (isa<ConstantPointerNull>(V))
494 return ConstantInt::get(PtrTy, 0);
498 if (CE->getOpcode() == Instruction::IntToPtr)
499 if (
ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(0))) {
504 return cast<ConstantInt>(
522struct ConstantComparesGatherer {
526 Value *CompValue =
nullptr;
529 Value *Extra =
nullptr;
535 unsigned UsedICmps = 0;
542 ConstantComparesGatherer(
const ConstantComparesGatherer &) =
delete;
543 ConstantComparesGatherer &
544 operator=(
const ConstantComparesGatherer &) =
delete;
549 bool setValueOnce(
Value *NewVal) {
550 if (CompValue && CompValue != NewVal)
553 return (CompValue !=
nullptr);
567 if (!((ICI = dyn_cast<ICmpInst>(
I)) &&
578 if (ICI->
getPredicate() == (isEQ ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE)) {
622 if (
Mask.isPowerOf2() && (
C->getValue() & ~Mask) ==
C->getValue()) {
624 if (!setValueOnce(RHSVal))
629 ConstantInt::get(
C->getContext(),
630 C->getValue() | Mask));
645 if (
Mask.isPowerOf2() && (
C->getValue() | Mask) ==
C->getValue()) {
647 if (!setValueOnce(RHSVal))
651 Vals.
push_back(ConstantInt::get(
C->getContext(),
652 C->getValue() & ~Mask));
673 Value *CandidateVal =
I->getOperand(0);
676 CandidateVal = RHSVal;
691 if (!setValueOnce(CandidateVal))
696 Vals.
push_back(ConstantInt::get(
I->getContext(), Tmp));
707 void gather(
Value *V) {
718 while (!DFT.
empty()) {
726 if (Visited.
insert(Op1).second)
728 if (Visited.
insert(Op0).second)
735 if (matchInstruction(
I, isEQ))
759 if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
760 Cond = dyn_cast<Instruction>(SI->getCondition());
761 }
else if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
762 if (BI->isConditional())
763 Cond = dyn_cast<Instruction>(BI->getCondition());
765 Cond = dyn_cast<Instruction>(IBI->getAddress());
777 if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
780 if (!
SI->getParent()->hasNPredecessorsOrMore(128 /
SI->getNumSuccessors()))
781 CV =
SI->getCondition();
782 }
else if (
BranchInst *BI = dyn_cast<BranchInst>(TI))
783 if (BI->isConditional() && BI->getCondition()->hasOneUse())
784 if (
ICmpInst *ICI = dyn_cast<ICmpInst>(BI->getCondition())) {
792 Value *
Ptr = PTII->getPointerOperand();
793 if (PTII->getType() ==
DL.getIntPtrType(
Ptr->getType()))
802BasicBlock *SimplifyCFGOpt::getValueEqualityComparisonCases(
803 Instruction *TI, std::vector<ValueEqualityComparisonCase> &Cases) {
804 if (
SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
805 Cases.reserve(
SI->getNumCases());
806 for (
auto Case :
SI->cases())
807 Cases.push_back(ValueEqualityComparisonCase(Case.getCaseValue(),
808 Case.getCaseSuccessor()));
809 return SI->getDefaultDest();
815 Cases.push_back(ValueEqualityComparisonCase(
824 std::vector<ValueEqualityComparisonCase> &Cases) {
830 std::vector<ValueEqualityComparisonCase> &C2) {
831 std::vector<ValueEqualityComparisonCase> *V1 = &C1, *V2 = &C2;
834 if (V1->size() > V2->size())
839 if (V1->size() == 1) {
842 for (
const ValueEqualityComparisonCase &VECC : *V2)
843 if (TheVal == VECC.
Value)
850 unsigned i1 = 0, i2 = 0, e1 = V1->size(), e2 = V2->size();
851 while (i1 != e1 && i2 != e2) {
872 SI->setMetadata(LLVMContext::MD_prof,
N);
878 uint32_t FalseWeight,
bool IsExpected) {
879 assert(isa<BranchInst>(
I) || isa<SelectInst>(
I));
883 if (TrueWeight || FalseWeight)
886 I->setMetadata(LLVMContext::MD_prof,
N);
894bool SimplifyCFGOpt::simplifyEqualityComparisonWithOnlyPredecessor(
900 Value *ThisVal = isValueEqualityComparison(TI);
901 assert(ThisVal &&
"This isn't a value comparison!!");
902 if (ThisVal != PredVal)
909 std::vector<ValueEqualityComparisonCase> PredCases;
911 getValueEqualityComparisonCases(Pred->
getTerminator(), PredCases);
915 std::vector<ValueEqualityComparisonCase> ThisCases;
916 BasicBlock *ThisDef = getValueEqualityComparisonCases(TI, ThisCases);
928 if (isa<BranchInst>(TI)) {
931 assert(ThisCases.size() == 1 &&
"Branch can only have one case!");
937 ThisCases[0].Dest->removePredecessor(PredDef);
940 <<
"Through successor TI: " << *TI <<
"Leaving: " << *NI
947 {{DominatorTree::Delete, PredDef, ThisCases[0].Dest}});
955 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
959 <<
"Through successor TI: " << *TI);
967 if (DeadCases.
count(i->getCaseValue())) {
976 std::vector<DominatorTree::UpdateType> Updates;
977 for (
const std::pair<BasicBlock *, int> &
I : NumPerSuccessorCases)
979 Updates.push_back({DominatorTree::Delete, PredDef,
I.first});
980 DTU->applyUpdates(Updates);
991 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
992 if (PredCases[i].Dest == TIBB) {
995 TIV = PredCases[i].
Value;
997 assert(TIV &&
"No edge from pred to succ?");
1002 for (
unsigned i = 0, e = ThisCases.size(); i != e; ++i)
1003 if (ThisCases[i].
Value == TIV) {
1004 TheRealDest = ThisCases[i].Dest;
1010 TheRealDest = ThisDef;
1017 if (Succ != CheckEdge) {
1018 if (Succ != TheRealDest)
1019 RemovedSuccs.
insert(Succ);
1022 CheckEdge =
nullptr;
1029 <<
"Through successor TI: " << *TI <<
"Leaving: " << *NI
1036 for (
auto *RemovedSucc : RemovedSuccs)
1037 Updates.
push_back({DominatorTree::Delete, TIBB, RemovedSucc});
1038 DTU->applyUpdates(Updates);
1048struct ConstantIntOrdering {
1050 return LHS->getValue().ult(
RHS->getValue());
1062 return LHS->getValue().ult(
RHS->getValue()) ? 1 : -1;
1071 assert(MD &&
"Invalid branch-weight metadata");
1077 if (
BranchInst *BI = dyn_cast<BranchInst>(TI)) {
1088 if (Max > UINT_MAX) {
1103 if (BonusInst.isTerminator())
1108 if (!isa<DbgInfoIntrinsic>(BonusInst) &&
1132 if (isa<DbgInfoIntrinsic>(BonusInst))
1135 NewBonusInst->
takeName(&BonusInst);
1136 BonusInst.setName(NewBonusInst->
getName() +
".old");
1137 VMap[&BonusInst] = NewBonusInst;
1143 auto *UI = cast<Instruction>(U.getUser());
1144 auto *PN = dyn_cast<PHINode>(UI);
1146 assert(UI->getParent() == BB && BonusInst.comesBefore(UI) &&
1147 "If the user is not a PHI node, then it should be in the same "
1148 "block as, and come after, the original bonus instruction.");
1152 if (PN->getIncomingBlock(U) == BB)
1156 assert(PN->getIncomingBlock(U) == PredBlock &&
1157 "Not in block-closed SSA form?");
1158 U.set(NewBonusInst);
1163bool SimplifyCFGOpt::performValueComparisonIntoPredecessorFolding(
1171 std::vector<ValueEqualityComparisonCase> BBCases;
1172 BasicBlock *BBDefault = getValueEqualityComparisonCases(TI, BBCases);
1174 std::vector<ValueEqualityComparisonCase> PredCases;
1175 BasicBlock *PredDefault = getValueEqualityComparisonCases(PTI, PredCases);
1187 if (PredHasWeights) {
1190 if (Weights.
size() != 1 + PredCases.size())
1191 PredHasWeights = SuccHasWeights =
false;
1192 }
else if (SuccHasWeights)
1196 Weights.
assign(1 + PredCases.size(), 1);
1199 if (SuccHasWeights) {
1202 if (SuccWeights.
size() != 1 + BBCases.size())
1203 PredHasWeights = SuccHasWeights =
false;
1204 }
else if (PredHasWeights)
1205 SuccWeights.
assign(1 + BBCases.size(), 1);
1207 if (PredDefault == BB) {
1210 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1211 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
1212 if (PredCases[i].Dest != BB)
1213 PTIHandled.insert(PredCases[i].
Value);
1216 std::swap(PredCases[i], PredCases.back());
1218 if (PredHasWeights || SuccHasWeights) {
1220 Weights[0] += Weights[i + 1];
1225 PredCases.pop_back();
1231 if (PredDefault != BBDefault) {
1233 if (DTU && PredDefault != BB)
1234 Updates.
push_back({DominatorTree::Delete, Pred, PredDefault});
1235 PredDefault = BBDefault;
1236 ++NewSuccessors[BBDefault];
1239 unsigned CasesFromPred = Weights.
size();
1241 for (
unsigned i = 0, e = BBCases.size(); i != e; ++i)
1242 if (!PTIHandled.count(BBCases[i].Value) && BBCases[i].Dest != BBDefault) {
1243 PredCases.push_back(BBCases[i]);
1244 ++NewSuccessors[BBCases[i].Dest];
1245 if (SuccHasWeights || PredHasWeights) {
1249 Weights.
push_back(Weights[0] * SuccWeights[i + 1]);
1250 ValidTotalSuccWeight += SuccWeights[i + 1];
1254 if (SuccHasWeights || PredHasWeights) {
1255 ValidTotalSuccWeight += SuccWeights[0];
1257 for (
unsigned i = 1; i < CasesFromPred; ++i)
1258 Weights[i] *= ValidTotalSuccWeight;
1260 Weights[0] *= SuccWeights[0];
1266 std::set<ConstantInt *, ConstantIntOrdering> PTIHandled;
1267 std::map<ConstantInt *, uint64_t> WeightsForHandled;
1268 for (
unsigned i = 0, e = PredCases.size(); i != e; ++i)
1269 if (PredCases[i].Dest == BB) {
1272 if (PredHasWeights || SuccHasWeights) {
1273 WeightsForHandled[PredCases[i].Value] = Weights[i + 1];
1278 std::swap(PredCases[i], PredCases.back());
1279 PredCases.pop_back();
1286 for (
unsigned i = 0, e = BBCases.size(); i != e; ++i)
1287 if (PTIHandled.count(BBCases[i].Value)) {
1289 if (PredHasWeights || SuccHasWeights)
1291 PredCases.push_back(BBCases[i]);
1292 ++NewSuccessors[BBCases[i].Dest];
1299 if (PredHasWeights || SuccHasWeights)
1301 PredCases.push_back(ValueEqualityComparisonCase(
I, BBDefault));
1302 ++NewSuccessors[BBDefault];
1314 for (
const std::pair<BasicBlock *, int /*Num*/> &NewSuccessor :
1316 for (
auto I :
seq(NewSuccessor.second)) {
1320 if (DTU && !SuccsOfPred.
contains(NewSuccessor.first))
1321 Updates.
push_back({DominatorTree::Insert, Pred, NewSuccessor.first});
1334 for (ValueEqualityComparisonCase &V : PredCases)
1337 if (PredHasWeights || SuccHasWeights) {
1354 if (!InfLoopBlock) {
1362 {DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
1369 Updates.
push_back({DominatorTree::Insert, Pred, InfLoopBlock});
1371 Updates.
push_back({DominatorTree::Delete, Pred, BB});
1373 DTU->applyUpdates(Updates);
1376 ++NumFoldValueComparisonIntoPredecessors;
1384bool SimplifyCFGOpt::foldValueComparisonIntoPredecessors(
Instruction *TI,
1387 Value *CV = isValueEqualityComparison(TI);
1388 assert(CV &&
"Not a comparison?");
1390 bool Changed =
false;
1393 while (!Preds.empty()) {
1402 Value *PCV = isValueEqualityComparison(PTI);
1408 for (
auto *Succ : FailBlocks) {
1414 performValueComparisonIntoPredecessorFolding(TI, CV, PTI, Builder);
1428 Value *BB1V = PN.getIncomingValueForBlock(BB1);
1429 Value *BB2V = PN.getIncomingValueForBlock(BB2);
1430 if (BB1V != BB2V && (BB1V == I1 || BB2V == I2)) {
1449 if (
I->mayReadFromMemory())
1453 if (
I->mayHaveSideEffects() || isa<AllocaInst>(
I))
1470 (
I->mayReadFromMemory() ||
I->mayHaveSideEffects() || isa<AllocaInst>(
I)))
1480 if (
auto *CB = dyn_cast<CallBase>(
I))
1481 if (CB->getIntrinsicID() == Intrinsic::experimental_deoptimize)
1488 if (
auto *J = dyn_cast<Instruction>(
Op))
1489 if (J->getParent() == BB)
1508 auto *C1 = dyn_cast<CallInst>(I1);
1509 auto *C2 = dyn_cast<CallInst>(I2);
1511 if (C1->isMustTailCall() != C2->isMustTailCall())
1519 if (
const auto *CB1 = dyn_cast<CallBase>(I1))
1520 if (CB1->cannotMerge() || CB1->isConvergent())
1522 if (
const auto *CB2 = dyn_cast<CallBase>(I2))
1523 if (CB2->cannotMerge() || CB2->isConvergent())
1538 if (!I1->hasDbgRecords())
1540 using CurrentAndEndIt =
1541 std::pair<DbgRecord::self_iterator, DbgRecord::self_iterator>;
1547 auto atEnd = [](
const CurrentAndEndIt &Pair) {
1548 return Pair.first == Pair.second;
1554 return Itrs[0].first->isIdenticalToWhenDefined(*
I);
1560 {I1->getDbgRecordRange().begin(), I1->getDbgRecordRange().end()});
1562 if (!
Other->hasDbgRecords())
1565 {
Other->getDbgRecordRange().begin(),
Other->getDbgRecordRange().end()});
1572 while (
none_of(Itrs, atEnd)) {
1573 bool HoistDVRs = allIdentical(Itrs);
1574 for (CurrentAndEndIt &Pair : Itrs) {
1591bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(
BasicBlock *BB,
1613 using SuccIterPair = std::pair<BasicBlock::iterator, unsigned>;
1617 if (isa<PHINode>(*SuccItr))
1619 SuccIterPairs.
push_back(SuccIterPair(SuccItr, 0));
1628 if (!INonDbg->isTerminator())
1638 unsigned NumSkipped = 0;
1641 if (SuccIterPairs.
size() > 2) {
1643 [](
const auto &Pair) {
return isa<UnreachableInst>(Pair.first); });
1644 if (SuccIterPairs.
size() < 2)
1648 bool Changed =
false;
1651 auto *SuccIterPairBegin = SuccIterPairs.
begin();
1652 auto &BB1ItrPair = *SuccIterPairBegin++;
1653 auto OtherSuccIterPairRange =
1660 bool AllDbgInstsAreIdentical =
all_of(OtherSuccIterRange, [I1](
auto &Iter) {
1662 return I1->isIdenticalToWhenDefined(I2);
1664 if (!AllDbgInstsAreIdentical) {
1665 while (isa<DbgInfoIntrinsic>(I1))
1666 I1 = &*++BB1ItrPair.first;
1667 for (
auto &SuccIter : OtherSuccIterRange) {
1669 while (isa<DbgInfoIntrinsic>(I2))
1674 bool AllInstsAreIdentical =
true;
1675 bool HasTerminator =
I1->isTerminator();
1676 for (
auto &SuccIter : OtherSuccIterRange) {
1679 if (AllInstsAreIdentical && (!
I1->isIdenticalToWhenDefined(I2) ||
1681 AllInstsAreIdentical =
false;
1685 for (
auto &SuccIter : OtherSuccIterRange)
1690 if (HasTerminator) {
1694 if (NumSkipped || !AllInstsAreIdentical) {
1699 return hoistSuccIdenticalTerminatorToSwitchOrIf(TI, I1, OtherInsts) ||
1703 if (AllInstsAreIdentical) {
1704 unsigned SkipFlagsBB1 = BB1ItrPair.second;
1705 AllInstsAreIdentical =
1707 all_of(OtherSuccIterPairRange, [=](
const auto &Pair) {
1709 unsigned SkipFlagsBB2 = Pair.second;
1719 if (AllInstsAreIdentical) {
1721 if (isa<DbgInfoIntrinsic>(I1)) {
1730 for (
auto &SuccIter : OtherSuccIterRange) {
1731 auto *I2 = &*SuccIter++;
1732 assert(isa<DbgInfoIntrinsic>(I2));
1744 for (
auto &SuccIter : OtherSuccIterRange) {
1758 NumHoistCommonCode += SuccIterPairs.
size();
1760 NumHoistCommonInstrs += SuccIterPairs.
size();
1769 for (
auto &SuccIterPair : SuccIterPairs) {
1778bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
1782 auto *BI = dyn_cast<BranchInst>(TI);
1784 bool Changed =
false;
1789 auto *I2 = *OtherSuccTIs.
begin();
1805 if (isa<CallBrInst>(I1))
1810 Value *BB1V = PN.getIncomingValueForBlock(BB1);
1812 Value *BB2V = PN.getIncomingValueForBlock(OtherSuccTI->getParent());
1832 if (!
NT->getType()->isVoidTy()) {
1833 I1->replaceAllUsesWith(NT);
1835 OtherSuccTI->replaceAllUsesWith(NT);
1839 NumHoistCommonInstrs += OtherSuccTIs.size() + 1;
1845 for (
auto *OtherSuccTI : OtherSuccTIs)
1846 Locs.
push_back(OtherSuccTI->getDebugLoc());
1858 std::map<std::pair<Value *, Value *>,
SelectInst *> InsertedSelects;
1861 Value *BB1V = PN.getIncomingValueForBlock(BB1);
1862 Value *BB2V = PN.getIncomingValueForBlock(BB2);
1868 SelectInst *&
SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
1872 if (isa<FPMathOperator>(PN))
1881 for (
unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
1882 if (PN.getIncomingBlock(i) == BB1 || PN.getIncomingBlock(i) == BB2)
1883 PN.setIncomingValue(i, SI);
1894 Updates.
push_back({DominatorTree::Insert, TIParent, Succ});
1899 Updates.
push_back({DominatorTree::Delete, TIParent, Succ});
1903 DTU->applyUpdates(Updates);
1909 if (
auto II = dyn_cast<IntrinsicInst>(
I)) {
1910 switch (
II->getIntrinsicID()) {
1913 case Intrinsic::lifetime_start:
1914 case Intrinsic::lifetime_end:
1925 return !isa<IntrinsicInst>(
I);
1938 std::optional<unsigned> NumUses;
1939 for (
auto *
I : Insts) {
1941 if (isa<PHINode>(
I) ||
I->isEHPad() || isa<AllocaInst>(
I) ||
1942 I->getType()->isTokenTy())
1947 if (
I->getParent()->getSingleSuccessor() ==
I->getParent())
1954 if (
const auto *
C = dyn_cast<CallBase>(
I))
1955 if (
C->isInlineAsm() ||
C->cannotMerge() ||
C->isConvergent())
1959 NumUses =
I->getNumUses();
1960 else if (NumUses !=
I->getNumUses())
1966 for (
auto *
I : Insts) {
1967 if (!
I->isSameOperationAs(I0))
1973 if (isa<StoreInst>(
I) &&
I->getOperand(1)->isSwiftError())
1975 if (isa<LoadInst>(
I) &&
I->getOperand(0)->isSwiftError())
1988 for (
const Use &U : I0->
uses()) {
1989 auto It = PHIOperands.find(&U);
1990 if (It == PHIOperands.end())
1993 if (!
equal(Insts, It->second))
2007 return isa<AllocaInst>(
I->getOperand(1)->stripPointerCasts());
2011 return isa<AllocaInst>(
I->getOperand(0)->stripPointerCasts());
2015 return isa<AllocaInst>(
I->getOperand(1)->stripPointerCasts());
2023 if (isa<CallBase>(I0)) {
2025 return cast<CallBase>(
I)->isIndirectCall();
2027 bool HaveIndirectCalls =
any_of(Insts, IsIndirectCall);
2028 bool AllCallsAreIndirect =
all_of(Insts, IsIndirectCall);
2029 if (HaveIndirectCalls) {
2030 if (!AllCallsAreIndirect)
2034 Value *Callee =
nullptr;
2036 Value *CurrCallee = cast<CallBase>(
I)->getCalledOperand();
2038 Callee = CurrCallee;
2039 else if (Callee != CurrCallee)
2045 for (
unsigned OI = 0, OE = I0->
getNumOperands(); OI != OE; ++OI) {
2047 if (
Op->getType()->isTokenTy())
2055 if (!
all_of(Insts, SameAsI0)) {
2061 for (
auto *
I : Insts)
2062 Ops.push_back(
I->getOperand(OI));
2072 auto *BBEnd =
Blocks[0]->getTerminator()->getSuccessor(0);
2077 for (
auto *BB :
Blocks) {
2080 I =
I->getPrevNode();
2081 }
while (isa<DbgInfoIntrinsic>(
I) &&
I != &BB->
front());
2082 if (!isa<DbgInfoIntrinsic>(
I))
2107 assert(!
Op->getType()->isTokenTy() &&
"Can't PHI tokens!");
2110 PN->insertBefore(BBEnd->begin());
2111 for (
auto *
I : Insts)
2112 PN->addIncoming(
I->getOperand(O),
I->getParent());
2121 I0->
moveBefore(*BBEnd, BBEnd->getFirstInsertionPt());
2124 for (
auto *
I : Insts)
2142 auto *PN = cast<PHINode>(U);
2143 PN->replaceAllUsesWith(I0);
2144 PN->eraseFromParent();
2148 for (
auto *
I : Insts) {
2153 assert(
I->user_empty() &&
"Inst unexpectedly still has non-dbg users");
2154 I->replaceAllUsesWith(I0);
2155 I->eraseFromParent();
2169 class LockstepReverseIterator {
2182 for (
auto *BB : Blocks) {
2184 for (Inst = Inst->
getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
2202 for (
auto *&Inst : Insts) {
2203 for (Inst = Inst->
getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
2216 for (
auto *&Inst : Insts) {
2217 for (Inst = Inst->
getNextNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
2280 bool HaveNonUnconditionalPredecessors =
false;
2282 auto *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
2283 if (PredBr && PredBr->isUnconditional())
2286 HaveNonUnconditionalPredecessors =
true;
2288 if (UnconditionalPreds.
size() < 2)
2301 for (
const Use &U : PN.incoming_values())
2302 IncomingVals.
insert({PN.getIncomingBlock(U), &U});
2303 auto &Ops = PHIOperands[IncomingVals[UnconditionalPreds[0]]];
2305 Ops.push_back(*IncomingVals[Pred]);
2310 LockstepReverseIterator LRI(UnconditionalPreds);
2311 while (LRI.isValid() &&
2313 LLVM_DEBUG(
dbgs() <<
"SINK: instruction can be sunk: " << *(*LRI)[0]
2315 InstructionsToSink.
insert((*LRI).begin(), (*LRI).end());
2326 if (!followedByDeoptOrUnreachable) {
2330 auto ProfitableToSinkInstruction = [&](LockstepReverseIterator &LRI) {
2331 unsigned NumPHIInsts = 0;
2332 for (
Use &U : (*LRI)[0]->operands()) {
2333 auto It = PHIOperands.
find(&U);
2334 if (It != PHIOperands.
end() && !
all_of(It->second, [&](
Value *V) {
2335 return InstructionsToSink.contains(V);
2343 LLVM_DEBUG(
dbgs() <<
"SINK: #phi insts: " << NumPHIInsts <<
"\n");
2344 return NumPHIInsts <= 1;
2361 while (
Idx < ScanIdx) {
2362 if (!ProfitableToSinkInstruction(LRI)) {
2365 dbgs() <<
"SINK: stopping here, too many PHIs would be created!\n");
2368 InstructionsProfitableToSink.
insert((*LRI).begin(), (*LRI).end());
2378 if (
Idx < ScanIdx) {
2381 InstructionsToSink = InstructionsProfitableToSink;
2387 !ProfitableToSinkInstruction(LRI) &&
2388 "We already know that the last instruction is unprofitable to sink");
2396 for (
auto *
I : *LRI)
2397 InstructionsProfitableToSink.
erase(
I);
2398 if (!ProfitableToSinkInstruction(LRI)) {
2401 InstructionsToSink = InstructionsProfitableToSink;
2413 bool Changed =
false;
2415 if (HaveNonUnconditionalPredecessors) {
2416 if (!followedByDeoptOrUnreachable) {
2424 bool Profitable =
false;
2425 while (
Idx < ScanIdx) {
2459 for (; SinkIdx != ScanIdx; ++SinkIdx) {
2461 << *UnconditionalPreds[0]->getTerminator()->getPrevNode()
2469 NumSinkCommonInstrs++;
2473 ++NumSinkCommonCode;
2479struct CompatibleSets {
2497 if (CompatibleSets::shouldBelongToSameSet({
Set.front(),
II}))
2502 return Sets.emplace_back();
2506 getCompatibleSet(
II).emplace_back(
II);
2510 assert(Invokes.
size() == 2 &&
"Always called with exactly two candidates.");
2514 return II->cannotMerge() ||
II->isInlineAsm();
2516 if (
any_of(Invokes, IsIllegalToMerge))
2521 auto IsIndirectCall = [](
InvokeInst *
II) {
return II->isIndirectCall(); };
2522 bool HaveIndirectCalls =
any_of(Invokes, IsIndirectCall);
2523 bool AllCallsAreIndirect =
all_of(Invokes, IsIndirectCall);
2524 if (HaveIndirectCalls) {
2525 if (!AllCallsAreIndirect)
2531 Value *CurrCallee =
II->getCalledOperand();
2532 assert(CurrCallee &&
"There is always a called operand.");
2535 else if (Callee != CurrCallee)
2543 return !isa<UnreachableInst>(
II->getNormalDest()->getFirstNonPHIOrDbg());
2545 if (
any_of(Invokes, HasNormalDest)) {
2548 if (!
all_of(Invokes, HasNormalDest))
2555 assert(CurrNormalBB &&
"There is always a 'continue to' basic block.");
2557 NormalBB = CurrNormalBB;
2558 else if (NormalBB != CurrNormalBB)
2566 NormalBB, {Invokes[0]->getParent(), Invokes[1]->
getParent()},
2577 assert(CurrUnwindBB &&
"There is always an 'unwind to' basic block.");
2579 UnwindBB = CurrUnwindBB;
2581 assert(UnwindBB == CurrUnwindBB &&
"Unexpected unwind destination.");
2588 Invokes.front()->getUnwindDest(),
2589 {Invokes[0]->getParent(), Invokes[1]->getParent()}))
2595 for (
auto *
II : Invokes.drop_front())
2596 if (!
II->isSameOperationAs(II0))
2600 auto IsIllegalToMergeArguments = [](
auto Ops) {
2601 Use &U0 = std::get<0>(Ops);
2602 Use &U1 = std::get<1>(Ops);
2609 assert(Invokes.size() == 2 &&
"Always called with exactly two candidates.");
2610 if (
any_of(
zip(Invokes[0]->data_ops(), Invokes[1]->data_ops()),
2611 IsIllegalToMergeArguments))
2623 assert(Invokes.
size() >= 2 &&
"Must have at least two invokes to merge.");
2629 bool HasNormalDest =
2630 !isa<UnreachableInst>(Invokes[0]->getNormalDest()->getFirstNonPHIOrDbg());
2634 InvokeInst *MergedInvoke = [&Invokes, HasNormalDest]() {
2638 II0->
getParent()->getIterator()->getNextNode();
2643 Ctx, II0BB->
getName() +
".invoke", Func, InsertBeforeBlock);
2645 auto *MergedInvoke = cast<InvokeInst>(II0->
clone());
2647 MergedInvoke->
insertInto(MergedInvokeBB, MergedInvokeBB->
end());
2649 if (!HasNormalDest) {
2653 Ctx, II0BB->
getName() +
".cont", Func, InsertBeforeBlock);
2660 return MergedInvoke;
2668 {DominatorTree::Insert,
II->getParent(), MergedInvoke->
getParent()});
2674 SuccBBOfMergedInvoke});
2681 {DominatorTree::Delete,
II->getParent(), SuccOfPredBB});
2684 bool IsIndirectCall = Invokes[0]->isIndirectCall();
2687 for (
Use &U : MergedInvoke->operands()) {
2689 if (MergedInvoke->isCallee(&U)) {
2690 if (!IsIndirectCall)
2692 }
else if (!MergedInvoke->isDataOperand(&U))
2697 return II->getOperand(
U.getOperandNo()) !=
U.get();
2704 U->getType(), Invokes.size(),
"", MergedInvoke->getIterator());
2716 Invokes.front()->getParent());
2724 if (!MergedDebugLoc)
2725 MergedDebugLoc =
II->getDebugLoc();
2733 OrigSuccBB->removePredecessor(
II->getParent());
2735 II->replaceAllUsesWith(MergedInvoke);
2736 II->eraseFromParent();
2739 MergedInvoke->setDebugLoc(MergedDebugLoc);
2740 ++NumInvokeSetsFormed;
2743 DTU->applyUpdates(Updates);
2770 bool Changed =
false;
2776 CompatibleSets Grouper;
2782 Grouper.insert(cast<InvokeInst>(PredBB->getTerminator()));
2786 if (Invokes.
size() < 2)
2798class EphemeralValueTracker {
2802 if (isa<AssumeInst>(
I))
2804 return !
I->mayHaveSideEffects() && !
I->isTerminator() &&
2806 return EphValues.count(cast<Instruction>(U));
2812 if (isEphemeral(
I)) {
2851 StoreInst *StoreToHoist = dyn_cast<StoreInst>(
I);
2863 unsigned MaxNumInstToLookAt = 9;
2867 if (!MaxNumInstToLookAt)
2869 --MaxNumInstToLookAt;
2872 if (CurI.mayWriteToMemory() && !isa<StoreInst>(CurI))
2875 if (
auto *SI = dyn_cast<StoreInst>(&CurI)) {
2879 if (SI->getPointerOperand() == StorePtr &&
2880 SI->getValueOperand()->getType() == StoreTy && SI->isSimple() &&
2881 SI->getAlign() >= StoreToHoist->
getAlign())
2883 return SI->getValueOperand();
2887 if (
auto *LI = dyn_cast<LoadInst>(&CurI)) {
2888 if (LI->getPointerOperand() == StorePtr && LI->
getType() == StoreTy &&
2889 LI->isSimple() && LI->getAlign() >= StoreToHoist->
getAlign()) {
2912 unsigned &SpeculatedInstructions,
2920 bool HaveRewritablePHIs =
false;
2938 HaveRewritablePHIs =
true;
2941 if (!OrigCE && !ThenCE)
2948 if (OrigCost + ThenCost > MaxCost)
2955 ++SpeculatedInstructions;
2956 if (SpeculatedInstructions > 1)
2960 return HaveRewritablePHIs;
2967 if (BI->
getMetadata(LLVMContext::MD_unpredictable))
2974 uint64_t EndWeight = Invert ? TWeight : FWeight;
2978 return BIEndProb < Likely;
3018bool SimplifyCFGOpt::speculativelyExecuteBB(
BranchInst *BI,
3025 if (isa<FCmpInst>(BrCond))
3035 bool Invert =
false;
3054 unsigned SpeculatedInstructions = 0;
3055 Value *SpeculatedStoreValue =
nullptr;
3057 EphemeralValueTracker EphTracker;
3060 if (isa<DbgInfoIntrinsic>(
I)) {
3068 if (isa<PseudoProbeInst>(
I)) {
3078 if (EphTracker.track(&
I))
3083 ++SpeculatedInstructions;
3084 if (SpeculatedInstructions > 1)
3090 &
I, BB, ThenBB, EndBB))))
3092 if (!SpeculatedStoreValue &&
3098 if (SpeculatedStoreValue)
3099 SpeculatedStore = cast<StoreInst>(&
I);
3104 for (
Use &
Op :
I.operands()) {
3109 ++SinkCandidateUseCounts[OpI];
3116 for (
const auto &[Inst, Count] : SinkCandidateUseCounts)
3118 ++SpeculatedInstructions;
3119 if (SpeculatedInstructions > 1)
3125 bool Convert = SpeculatedStore !=
nullptr;
3128 SpeculatedInstructions,
3130 if (!Convert ||
Cost > Budget)
3134 LLVM_DEBUG(
dbgs() <<
"SPECULATIVELY EXECUTING BB" << *ThenBB <<
"\n";);
3137 if (SpeculatedStoreValue) {
3141 Value *FalseV = SpeculatedStoreValue;
3145 BrCond, TrueV, FalseV,
"spec.store.select", BI);
3174 auto replaceVariable = [OrigV, S](
auto *DbgAssign) {
3176 DbgAssign->replaceVariableLocationOp(OrigV, S);
3189 if (!SpeculatedStoreValue || &
I != SpeculatedStore) {
3191 if (!isa<DbgAssignIntrinsic>(&
I))
3194 I.dropUBImplyingAttrsAndMetadata();
3197 if (EphTracker.contains(&
I)) {
3199 I.eraseFromParent();
3213 It.dropOneDbgRecord(&DR);
3215 std::prev(ThenBB->
end()));
3232 Value *TrueV = ThenV, *FalseV = OrigV;
3246 if (!isa<DbgAssignIntrinsic>(
I))
3247 I->eraseFromParent();
3257 EphemeralValueTracker EphTracker;
3263 if (
CallInst *CI = dyn_cast<CallInst>(&
I))
3264 if (CI->cannotDuplicate() || CI->isConvergent())
3270 if (!EphTracker.track(&
I) && !isa<PHINode>(
I)) {
3277 for (
User *U :
I.users()) {
3279 if (UI->
getParent() != BB || isa<PHINode>(UI))
3293 auto *
I = dyn_cast<Instruction>(V);
3294 if (
I &&
I->getParent() == To)
3298 auto *BI = dyn_cast<BranchInst>(
From->getTerminator());
3310static std::optional<bool>
3326 if (
auto *CB = dyn_cast<ConstantInt>(U))
3331 KnownValues[CB].
insert(Pred);
3335 if (KnownValues.
empty())
3344 for (
const auto &Pair : KnownValues) {
3362 <<
" has value " << *Pair.first <<
" in predecessors:\n";
3365 dbgs() <<
"Threading to destination " << RealDest->
getName() <<
".\n";
3373 EdgeBB->setName(RealDest->
getName() +
".critedge");
3374 EdgeBB->moveBefore(RealDest);
3384 TranslateMap[
Cond] = CB;
3390 if (
PHINode *PN = dyn_cast<PHINode>(BBI)) {
3397 N->insertInto(EdgeBB, InsertPt);
3400 N->setName(BBI->getName() +
".c");
3403 for (
Use &
Op :
N->operands()) {
3405 if (PI != TranslateMap.
end())
3411 if (!BBI->use_empty())
3412 TranslateMap[&*BBI] = V;
3413 if (!
N->mayHaveSideEffects()) {
3414 N->eraseFromParent();
3419 if (!BBI->use_empty())
3420 TranslateMap[&*BBI] =
N;
3426 for (; SrcDbgCursor != BBI; ++SrcDbgCursor)
3427 N->cloneDebugInfoFrom(&*SrcDbgCursor);
3428 SrcDbgCursor = std::next(BBI);
3430 N->cloneDebugInfoFrom(&*BBI);
3433 if (
auto *Assume = dyn_cast<AssumeInst>(
N))
3439 for (; &*SrcDbgCursor != BI; ++SrcDbgCursor)
3440 InsertPt->cloneDebugInfoFrom(&*SrcDbgCursor);
3441 InsertPt->cloneDebugInfoFrom(BI);
3444 BranchInst *EdgeBI = cast<BranchInst>(EdgeBB->getTerminator());
3450 Updates.
push_back({DominatorTree::Delete, EdgeBB, BB});
3451 Updates.
push_back({DominatorTree::Insert, EdgeBB, RealDest});
3462 return std::nullopt;
3472 std::optional<bool> Result;
3473 bool EverChanged =
false;
3477 EverChanged |= Result == std::nullopt || *Result;
3478 }
while (Result == std::nullopt);
3486 bool SpeculateUnpredictables) {
3501 if (isa<ConstantInt>(IfCond))
3508 return cast<BranchInst>(IfBlock->getTerminator())->isUnconditional();
3511 "Will have either one or two blocks to speculate.");
3518 bool IsUnpredictable = DomBI->
getMetadata(LLVMContext::MD_unpredictable);
3519 if (!IsUnpredictable) {
3522 (TWeight + FWeight) != 0) {
3527 if (IfBlocks.
size() == 1) {
3529 DomBI->
getSuccessor(0) == BB ? BITrueProb : BIFalseProb;
3530 if (BIBBProb >= Likely)
3533 if (BITrueProb >= Likely || BIFalseProb >= Likely)
3541 if (
auto *IfCondPhiInst = dyn_cast<PHINode>(IfCond))
3542 if (IfCondPhiInst->getParent() == BB)
3550 unsigned NumPhis = 0;
3562 if (SpeculateUnpredictables && IsUnpredictable)
3565 bool Changed =
false;
3584 PN = dyn_cast<PHINode>(BB->
begin());
3590 auto CanHoistNotFromBothValues = [](
Value *V0,
Value *V1) {
3601 auto IsBinOpOrAnd = [](
Value *V) {
3621 if (!AggressiveInsts.
count(&*
I) && !
I->isDebugOrPseudoInst()) {
3634 if (IsUnpredictable)
dbgs() <<
" (unpredictable)";
3636 <<
" F: " << IfFalse->
getName() <<
"\n");
3650 if (isa<FPMathOperator>(PN))
3670 Updates.
push_back({DominatorTree::Insert, DomBlock, BB});
3688 if (Opc == Instruction::And)
3690 if (Opc == Instruction::Or)
3703 bool PredHasWeights =
3705 bool SuccHasWeights =
3707 if (PredHasWeights || SuccHasWeights) {
3708 if (!PredHasWeights)
3709 PredTrueWeight = PredFalseWeight = 1;
3710 if (!SuccHasWeights)
3711 SuccTrueWeight = SuccFalseWeight = 1;
3721static std::optional<std::tuple<BasicBlock *, Instruction::BinaryOps, bool>>
3725 "Both blocks must end with a conditional branches.");
3727 "PredBB must be a predecessor of BB.");
3735 (PTWeight + PFWeight) != 0) {
3743 if (PBITrueProb.
isUnknown() || PBITrueProb < Likely)
3744 return {{BI->
getSuccessor(0), Instruction::Or,
false}};
3748 return {{BI->
getSuccessor(1), Instruction::And,
false}};
3751 if (PBITrueProb.
isUnknown() || PBITrueProb < Likely)
3752 return {{BI->
getSuccessor(1), Instruction::And,
true}};
3758 return std::nullopt;
3771 bool InvertPredCond;
3772 std::tie(CommonSucc, Opc, InvertPredCond) =
3775 LLVM_DEBUG(
dbgs() <<
"FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
3782 {LLVMContext::MD_annotation});
3785 if (InvertPredCond) {
3798 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3800 SuccTrueWeight, SuccFalseWeight)) {
3807 NewWeights.
push_back(PredTrueWeight * SuccTrueWeight);
3813 (SuccFalseWeight + SuccTrueWeight) +
3814 PredTrueWeight * SuccFalseWeight);
3820 NewWeights.
push_back(PredTrueWeight * (SuccFalseWeight + SuccTrueWeight) +
3821 PredFalseWeight * SuccTrueWeight);
3823 NewWeights.
push_back(PredFalseWeight * SuccFalseWeight);
3841 DTU->
applyUpdates({{DominatorTree::Insert, PredBlock, UniqueSucc},
3842 {DominatorTree::Delete, PredBlock, BB}});
3869 ++NumFoldBranchToCommonDest;
3876 return I.getType()->isVectorTy() ||
any_of(
I.operands(), [](
Use &U) {
3877 return U->getType()->isVectorTy();
3887 unsigned BonusInstThreshold) {
3901 (!isa<CmpInst>(
Cond) && !isa<BinaryOperator>(
Cond) &&
3902 !isa<SelectInst>(
Cond)) ||
3903 Cond->getParent() != BB || !
Cond->hasOneUse())
3913 BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
3924 bool InvertPredCond;
3926 std::tie(CommonSucc, Opc, InvertPredCond) = *Recipe;
3958 unsigned NumBonusInsts = 0;
3959 bool SawVectorOp =
false;
3960 const unsigned PredCount = Preds.
size();
3966 if (isa<DbgInfoIntrinsic>(
I) || isa<BranchInst>(
I))
3977 NumBonusInsts += PredCount;
3985 auto IsBCSSAUse = [BB, &
I](
Use &U) {
3986 auto *UI = cast<Instruction>(U.getUser());
3987 if (
auto *PN = dyn_cast<PHINode>(UI))
3989 return UI->
getParent() == BB &&
I.comesBefore(UI);
3993 if (!
all_of(
I.uses(), IsBCSSAUse))
3997 BonusInstThreshold *
4003 auto *PBI = cast<BranchInst>(PredBlock->getTerminator());
4013 for (
auto *BB : {BB1, BB2}) {
4017 if (
auto *SI = dyn_cast<StoreInst>(&
I)) {
4029 Value *AlternativeV =
nullptr) {
4047 for (
auto I = Succ->
begin(); isa<PHINode>(
I); ++
I)
4048 if (cast<PHINode>(
I)->getIncomingValueForBlock(BB) == V) {
4049 PHI = cast<PHINode>(
I);
4055 BasicBlock *OtherPredBB = *PredI == BB ? *++PredI : *PredI;
4056 if (
PHI->getIncomingValueForBlock(OtherPredBB) == AlternativeV)
4064 if (!AlternativeV &&
4065 (!isa<Instruction>(V) || cast<Instruction>(V)->
getParent() != BB))
4070 PHI->addIncoming(V, BB);
4080 BasicBlock *PostBB,
Value *Address,
bool InvertPCond,
bool InvertQCond,
4089 if (!PStore || !QStore)
4110 if (
I.mayReadOrWriteMemory())
4112 for (
auto &
I : *QFB)
4113 if (&
I != QStore &&
I.mayReadOrWriteMemory())
4116 for (
auto &
I : *QTB)
4117 if (&
I != QStore &&
I.mayReadOrWriteMemory())
4121 if (&*
I != PStore &&
I->mayReadOrWriteMemory())
4137 if (
I.isTerminator())
4141 if (
auto *S = dyn_cast<StoreInst>(&
I))
4145 if (!isa<BinaryOperator>(
I) && !isa<GetElementPtrInst>(
I))
4155 "When we run out of budget we will eagerly return from within the "
4156 "per-instruction loop.");
4160 const std::array<StoreInst *, 2> FreeStores = {PStore, QStore};
4162 (!IsWorthwhile(PTB, FreeStores) || !IsWorthwhile(PFB, FreeStores) ||
4163 !IsWorthwhile(QTB, FreeStores) || !IsWorthwhile(QFB, FreeStores)))
4271 bool InvertPCond =
false, InvertQCond =
false;
4277 if (QFB == PostBB) {
4296 !HasOnePredAndOneSucc(QFB, QBI->
getParent(), PostBB))
4299 (QTB && !HasOnePredAndOneSucc(QTB, QBI->
getParent(), PostBB)))
4307 for (
auto *BB : {PTB, PFB}) {
4312 PStoreAddresses.
insert(SI->getPointerOperand());
4314 for (
auto *BB : {QTB, QFB}) {
4319 QStoreAddresses.
insert(SI->getPointerOperand());
4325 auto &CommonAddresses = PStoreAddresses;
4327 bool Changed =
false;
4328 for (
auto *Address : CommonAddresses)
4331 InvertPCond, InvertQCond, DTU,
DL,
TTI);
4349 !BI->
getParent()->getSinglePredecessor())
4351 if (!IfFalseBB->
phis().empty())
4361 return I.mayWriteToMemory() ||
I.mayHaveSideEffects();
4372 {{DominatorTree::Insert, BI->
getParent(), IfFalseBB},
4373 {DominatorTree::Delete, BI->
getParent(), OldSuccessor}});
4384 {{DominatorTree::Insert, BI->
getParent(), IfFalseBB},
4385 {DominatorTree::Delete, BI->
getParent(), OldSuccessor}});
4464 if (!PBI->
getMetadata(LLVMContext::MD_unpredictable) &&
4466 (
static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]) != 0) {
4470 static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]);
4473 if (CommonDestProb >= Likely)
4483 unsigned NumPhis = 0;
4505 if (OtherDest == BB) {
4512 Updates.
push_back({DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
4513 OtherDest = InfLoopBlock;
4533 createLogicalOp(Builder, Instruction::Or, PBICond, BICond,
"brmerge");
4548 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
4549 uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
4552 SuccTrueWeight, SuccFalseWeight);
4554 PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
4555 PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
4556 SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
4557 SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
4561 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
4562 PredOther * SuccCommon,
4563 PredOther * SuccOther};
4592 uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
4593 uint64_t PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
4594 uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
4595 uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
4598 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
4599 PredOther * SuccCommon};
4622bool SimplifyCFGOpt::simplifyTerminatorOnSelect(
Instruction *OldTerm,
4633 BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB :
nullptr;
4640 if (Succ == KeepEdge1)
4641 KeepEdge1 =
nullptr;
4642 else if (Succ == KeepEdge2)
4643 KeepEdge2 =
nullptr;
4648 if (Succ != TrueBB && Succ != FalseBB)
4649 RemovedSuccessors.
insert(Succ);
4657 if (!KeepEdge1 && !KeepEdge2) {
4658 if (TrueBB == FalseBB) {
4666 if (TrueWeight != FalseWeight)
4669 }
else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
4691 for (
auto *RemovedSuccessor : RemovedSuccessors)
4692 Updates.
push_back({DominatorTree::Delete, BB, RemovedSuccessor});
4693 DTU->applyUpdates(Updates);
4703bool SimplifyCFGOpt::simplifySwitchOnSelect(
SwitchInst *SI,
4708 if (!TrueVal || !FalseVal)
4713 BasicBlock *TrueBB =
SI->findCaseValue(TrueVal)->getCaseSuccessor();
4714 BasicBlock *FalseBB =
SI->findCaseValue(FalseVal)->getCaseSuccessor();
4717 uint32_t TrueWeight = 0, FalseWeight = 0;
4722 if (Weights.
size() == 1 +
SI->getNumCases()) {
4724 (
uint32_t)Weights[
SI->findCaseValue(TrueVal)->getSuccessorIndex()];
4726 (
uint32_t)Weights[
SI->findCaseValue(FalseVal)->getSuccessorIndex()];
4731 return simplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB, TrueWeight,
4740bool SimplifyCFGOpt::simplifyIndirectBrOnSelect(
IndirectBrInst *IBI,
4753 return simplifyTerminatorOnSelect(IBI,
SI->getCondition(), TrueBB, FalseBB, 0,
4774bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
4794 if (
SI->getCondition() != V)
4800 if (
SI->getDefaultDest() != BB) {
4802 assert(VVal &&
"Should have a unique destination value");
4810 return requestResimplify();
4816 if (
SI->findCaseValue(Cst) !=
SI->case_default()) {
4826 return requestResimplify();
4833 if (PHIUse ==
nullptr || PHIUse != &SuccBlock->
front() ||
4858 auto W0 = SIW.getSuccessorWeight(0);
4862 SIW.setSuccessorWeight(0, *NewW);
4864 SIW.addCase(Cst, NewBB, NewW);
4866 Updates.
push_back({DominatorTree::Insert, Pred, NewBB});
4875 Updates.
push_back({DominatorTree::Insert, NewBB, SuccBlock});
4876 DTU->applyUpdates(Updates);
4884bool SimplifyCFGOpt::simplifyBranchOnICmpChain(
BranchInst *BI,
4896 ConstantComparesGatherer ConstantCompare(
Cond,
DL);
4899 Value *CompVal = ConstantCompare.CompValue;
4900 unsigned UsedICmps = ConstantCompare.UsedICmps;
4901 Value *ExtraCase = ConstantCompare.Extra;
4920 if (ExtraCase && Values.
size() < 2)
4935 <<
" cases into SWITCH. BB is:\n"
4945 nullptr,
"switch.early.test");
4969 Updates.
push_back({DominatorTree::Insert, BB, EdgeBB});
4975 LLVM_DEBUG(
dbgs() <<
" ** 'icmp' chain unhandled condition: " << *ExtraCase
4976 <<
"\nEXTRABB = " << *BB);
4984 CompVal,
DL.getIntPtrType(CompVal->
getType()),
"magicptr");
4991 for (
unsigned i = 0, e = Values.
size(); i != e; ++i)
4992 New->addCase(Values[i], EdgeBB);
4998 PHINode *PN = cast<PHINode>(BBI);
5000 for (
unsigned i = 0, e = Values.
size() - 1; i != e; ++i)
5007 DTU->applyUpdates(Updates);
5009 LLVM_DEBUG(
dbgs() <<
" ** 'icmp' chain result is:\n" << *BB <<
'\n');
5015 return simplifyCommonResume(RI);
5016 else if (isa<LandingPadInst>(RI->
getParent()->getFirstNonPHI()) &&
5019 return simplifySingleResume(RI);
5027 auto *
II = dyn_cast<IntrinsicInst>(&
I);
5032 switch (IntrinsicID) {
5033 case Intrinsic::dbg_declare:
5034 case Intrinsic::dbg_value:
5035 case Intrinsic::dbg_label:
5036 case Intrinsic::lifetime_end:
5046bool SimplifyCFGOpt::simplifyCommonResume(
ResumeInst *RI) {
5056 auto *PhiLPInst = cast<PHINode>(RI->
getValue());
5059 for (
unsigned Idx = 0,
End = PhiLPInst->getNumIncomingValues();
Idx !=
End;
5061 auto *IncomingBB = PhiLPInst->getIncomingBlock(
Idx);
5062 auto *IncomingValue = PhiLPInst->getIncomingValue(
Idx);
5066 if (IncomingBB->getUniqueSuccessor() != BB)
5069 auto *LandingPad = dyn_cast<LandingPadInst>(IncomingBB->getFirstNonPHI());
5071 if (IncomingValue != LandingPad)
5075 make_range(LandingPad->getNextNode(), IncomingBB->getTerminator())))
5076 TrivialUnwindBlocks.
insert(IncomingBB);
5080 if (TrivialUnwindBlocks.
empty())
5084 for (
auto *TrivialBB : TrivialUnwindBlocks) {
5088 while (PhiLPInst->getBasicBlockIndex(TrivialBB) != -1)
5102 TrivialBB->getTerminator()->eraseFromParent();
5105 DTU->applyUpdates({{DominatorTree::Delete, TrivialBB, BB}});
5112 return !TrivialUnwindBlocks.empty();
5116bool SimplifyCFGOpt::simplifySingleResume(
ResumeInst *RI) {
5120 "Resume must unwind the exception that caused control to here");
5124 make_range<Instruction *>(LPInst->getNextNode(), RI)))