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(
292 :
TTI(
TTI), DTU(DTU),
DL(
DL), LoopHeaders(LoopHeaders), Options(Opts) {
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) {
1588 if (I1->isIdenticalToWhenDefined(I2))
1591 if (
auto *Cmp1 = dyn_cast<CmpInst>(I1))
1592 if (
auto *Cmp2 = dyn_cast<CmpInst>(I2))
1593 return Cmp1->getPredicate() == Cmp2->getSwappedPredicate() &&
1594 Cmp1->getOperand(0) == Cmp2->getOperand(1) &&
1595 Cmp1->getOperand(1) == Cmp2->getOperand(0);
1597 if (I1->isCommutative() && I1->isSameOperationAs(I2)) {
1598 return I1->getOperand(0) == I2->
getOperand(1) &&
1611bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(
BasicBlock *BB,
1633 using SuccIterPair = std::pair<BasicBlock::iterator, unsigned>;
1637 if (isa<PHINode>(*SuccItr))
1639 SuccIterPairs.
push_back(SuccIterPair(SuccItr, 0));
1648 if (!INonDbg->isTerminator())
1658 unsigned NumSkipped = 0;
1661 if (SuccIterPairs.
size() > 2) {
1663 [](
const auto &Pair) {
return isa<UnreachableInst>(Pair.first); });
1664 if (SuccIterPairs.
size() < 2)
1668 bool Changed =
false;
1671 auto *SuccIterPairBegin = SuccIterPairs.
begin();
1672 auto &BB1ItrPair = *SuccIterPairBegin++;
1673 auto OtherSuccIterPairRange =
1680 bool AllDbgInstsAreIdentical =
all_of(OtherSuccIterRange, [I1](
auto &Iter) {
1682 return I1->isIdenticalToWhenDefined(I2);
1684 if (!AllDbgInstsAreIdentical) {
1685 while (isa<DbgInfoIntrinsic>(I1))
1686 I1 = &*++BB1ItrPair.first;
1687 for (
auto &SuccIter : OtherSuccIterRange) {
1689 while (isa<DbgInfoIntrinsic>(I2))
1694 bool AllInstsAreIdentical =
true;
1695 bool HasTerminator =
I1->isTerminator();
1696 for (
auto &SuccIter : OtherSuccIterRange) {
1701 AllInstsAreIdentical =
false;
1705 for (
auto &SuccIter : OtherSuccIterRange)
1710 if (HasTerminator) {
1714 if (NumSkipped || !AllInstsAreIdentical) {
1719 return hoistSuccIdenticalTerminatorToSwitchOrIf(TI, I1, OtherInsts) ||
1723 if (AllInstsAreIdentical) {
1724 unsigned SkipFlagsBB1 = BB1ItrPair.second;
1725 AllInstsAreIdentical =
1727 all_of(OtherSuccIterPairRange, [=](
const auto &Pair) {
1729 unsigned SkipFlagsBB2 = Pair.second;
1739 if (AllInstsAreIdentical) {
1741 if (isa<DbgInfoIntrinsic>(I1)) {
1750 for (
auto &SuccIter : OtherSuccIterRange) {
1751 auto *I2 = &*SuccIter++;
1752 assert(isa<DbgInfoIntrinsic>(I2));
1764 for (
auto &SuccIter : OtherSuccIterRange) {
1778 NumHoistCommonCode += SuccIterPairs.
size();
1780 NumHoistCommonInstrs += SuccIterPairs.
size();
1789 for (
auto &SuccIterPair : SuccIterPairs) {
1798bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
1802 auto *BI = dyn_cast<BranchInst>(TI);
1804 bool Changed =
false;
1809 auto *I2 = *OtherSuccTIs.
begin();
1825 if (isa<CallBrInst>(I1))
1830 Value *BB1V = PN.getIncomingValueForBlock(BB1);
1832 Value *BB2V = PN.getIncomingValueForBlock(OtherSuccTI->getParent());
1852 if (!
NT->getType()->isVoidTy()) {
1853 I1->replaceAllUsesWith(NT);
1855 OtherSuccTI->replaceAllUsesWith(NT);
1859 NumHoistCommonInstrs += OtherSuccTIs.size() + 1;
1865 for (
auto *OtherSuccTI : OtherSuccTIs)
1866 Locs.
push_back(OtherSuccTI->getDebugLoc());
1878 std::map<std::pair<Value *, Value *>,
SelectInst *> InsertedSelects;
1881 Value *BB1V = PN.getIncomingValueForBlock(BB1);
1882 Value *BB2V = PN.getIncomingValueForBlock(BB2);
1888 SelectInst *&
SI = InsertedSelects[std::make_pair(BB1V, BB2V)];
1892 if (isa<FPMathOperator>(PN))
1901 for (
unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
1902 if (PN.getIncomingBlock(i) == BB1 || PN.getIncomingBlock(i) == BB2)
1903 PN.setIncomingValue(i, SI);
1914 Updates.
push_back({DominatorTree::Insert, TIParent, Succ});
1919 Updates.
push_back({DominatorTree::Delete, TIParent, Succ});
1923 DTU->applyUpdates(Updates);
1929 if (
auto II = dyn_cast<IntrinsicInst>(
I)) {
1930 switch (
II->getIntrinsicID()) {
1933 case Intrinsic::lifetime_start:
1934 case Intrinsic::lifetime_end:
1945 return !isa<IntrinsicInst>(
I);
1958 std::optional<unsigned> NumUses;
1959 for (
auto *
I : Insts) {
1961 if (isa<PHINode>(
I) ||
I->isEHPad() || isa<AllocaInst>(
I) ||
1962 I->getType()->isTokenTy())
1967 if (
I->getParent()->getSingleSuccessor() ==
I->getParent())
1974 if (
const auto *
C = dyn_cast<CallBase>(
I))
1975 if (
C->isInlineAsm() ||
C->cannotMerge() ||
C->isConvergent())
1979 NumUses =
I->getNumUses();
1980 else if (NumUses !=
I->getNumUses())
1986 for (
auto *
I : Insts) {
1987 if (!
I->isSameOperationAs(I0))
1993 if (isa<StoreInst>(
I) &&
I->getOperand(1)->isSwiftError())
1995 if (isa<LoadInst>(
I) &&
I->getOperand(0)->isSwiftError())
2008 for (
const Use &U : I0->
uses()) {
2009 auto It = PHIOperands.find(&U);
2010 if (It == PHIOperands.end())
2013 if (!
equal(Insts, It->second))
2021 if (isa<CallBase>(I0)) {
2023 return cast<CallBase>(
I)->isIndirectCall();
2025 bool HaveIndirectCalls =
any_of(Insts, IsIndirectCall);
2026 bool AllCallsAreIndirect =
all_of(Insts, IsIndirectCall);
2027 if (HaveIndirectCalls) {
2028 if (!AllCallsAreIndirect)
2032 Value *Callee =
nullptr;
2034 Value *CurrCallee = cast<CallBase>(
I)->getCalledOperand();
2036 Callee = CurrCallee;
2037 else if (Callee != CurrCallee)
2043 for (
unsigned OI = 0, OE = I0->
getNumOperands(); OI != OE; ++OI) {
2045 if (
Op->getType()->isTokenTy())
2053 if (!
all_of(Insts, SameAsI0)) {
2059 if (isa<StoreInst>(I0) && OI == 1 &&
2061 return isa<AllocaInst>(
I->getOperand(1)->stripPointerCasts());
2064 if (isa<LoadInst>(I0) && OI == 0 &&
2066 return isa<AllocaInst>(
I->getOperand(0)->stripPointerCasts());
2071 return isa<AllocaInst>(
I->getOperand(1)->stripPointerCasts());
2080 for (
auto *
I : Insts)
2081 Ops.push_back(
I->getOperand(OI));
2091 auto *BBEnd =
Blocks[0]->getTerminator()->getSuccessor(0);
2096 for (
auto *BB :
Blocks) {
2099 I =
I->getPrevNode();
2100 }
while (isa<DbgInfoIntrinsic>(
I) &&
I != &BB->
front());
2101 if (!isa<DbgInfoIntrinsic>(
I))
2126 assert(!
Op->getType()->isTokenTy() &&
"Can't PHI tokens!");
2129 PN->insertBefore(BBEnd->begin());
2130 for (
auto *
I : Insts)
2131 PN->addIncoming(
I->getOperand(O),
I->getParent());
2140 I0->
moveBefore(*BBEnd, BBEnd->getFirstInsertionPt());
2143 for (
auto *
I : Insts)
2161 auto *PN = cast<PHINode>(U);
2162 PN->replaceAllUsesWith(I0);
2163 PN->eraseFromParent();
2167 for (
auto *
I : Insts) {
2172 assert(
I->user_empty() &&
"Inst unexpectedly still has non-dbg users");
2173 I->replaceAllUsesWith(I0);
2174 I->eraseFromParent();
2188 class LockstepReverseIterator {
2201 for (
auto *BB : Blocks) {
2203 for (Inst = Inst->
getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
2221 for (
auto *&Inst : Insts) {
2222 for (Inst = Inst->
getPrevNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
2235 for (
auto *&Inst : Insts) {
2236 for (Inst = Inst->
getNextNode(); Inst && isa<DbgInfoIntrinsic>(Inst);)
2299 bool HaveNonUnconditionalPredecessors =
false;
2301 auto *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
2302 if (PredBr && PredBr->isUnconditional())
2305 HaveNonUnconditionalPredecessors =
true;
2307 if (UnconditionalPreds.
size() < 2)
2320 for (
const Use &U : PN.incoming_values())
2321 IncomingVals.
insert({PN.getIncomingBlock(U), &U});
2322 auto &Ops = PHIOperands[IncomingVals[UnconditionalPreds[0]]];
2324 Ops.push_back(*IncomingVals[Pred]);
2329 LockstepReverseIterator LRI(UnconditionalPreds);
2330 while (LRI.isValid() &&
2332 LLVM_DEBUG(
dbgs() <<
"SINK: instruction can be sunk: " << *(*LRI)[0]
2334 InstructionsToSink.
insert((*LRI).begin(), (*LRI).end());
2345 if (!followedByDeoptOrUnreachable) {
2349 auto ProfitableToSinkInstruction = [&](LockstepReverseIterator &LRI) {
2350 unsigned NumPHIInsts = 0;
2351 for (
Use &U : (*LRI)[0]->operands()) {
2352 auto It = PHIOperands.
find(&U);
2353 if (It != PHIOperands.
end() && !
all_of(It->second, [&](
Value *V) {
2354 return InstructionsToSink.contains(V);
2362 LLVM_DEBUG(
dbgs() <<
"SINK: #phi insts: " << NumPHIInsts <<
"\n");
2363 return NumPHIInsts <= 1;
2380 while (
Idx < ScanIdx) {
2381 if (!ProfitableToSinkInstruction(LRI)) {
2384 dbgs() <<
"SINK: stopping here, too many PHIs would be created!\n");
2387 InstructionsProfitableToSink.
insert((*LRI).begin(), (*LRI).end());
2397 if (
Idx < ScanIdx) {
2400 InstructionsToSink = InstructionsProfitableToSink;
2406 !ProfitableToSinkInstruction(LRI) &&
2407 "We already know that the last instruction is unprofitable to sink");
2415 for (
auto *
I : *LRI)
2416 InstructionsProfitableToSink.
erase(
I);
2417 if (!ProfitableToSinkInstruction(LRI)) {
2420 InstructionsToSink = InstructionsProfitableToSink;
2432 bool Changed =
false;
2434 if (HaveNonUnconditionalPredecessors) {
2435 if (!followedByDeoptOrUnreachable) {
2443 bool Profitable =
false;
2444 while (
Idx < ScanIdx) {
2478 for (; SinkIdx != ScanIdx; ++SinkIdx) {
2480 << *UnconditionalPreds[0]->getTerminator()->getPrevNode()
2488 NumSinkCommonInstrs++;
2492 ++NumSinkCommonCode;
2498struct CompatibleSets {
2516 if (CompatibleSets::shouldBelongToSameSet({
Set.front(),
II}))
2521 return Sets.emplace_back();
2525 getCompatibleSet(
II).emplace_back(
II);
2529 assert(Invokes.
size() == 2 &&
"Always called with exactly two candidates.");
2533 return II->cannotMerge() ||
II->isInlineAsm();
2535 if (
any_of(Invokes, IsIllegalToMerge))
2540 auto IsIndirectCall = [](
InvokeInst *
II) {
return II->isIndirectCall(); };
2541 bool HaveIndirectCalls =
any_of(Invokes, IsIndirectCall);
2542 bool AllCallsAreIndirect =
all_of(Invokes, IsIndirectCall);
2543 if (HaveIndirectCalls) {
2544 if (!AllCallsAreIndirect)
2550 Value *CurrCallee =
II->getCalledOperand();
2551 assert(CurrCallee &&
"There is always a called operand.");
2554 else if (Callee != CurrCallee)
2562 return !isa<UnreachableInst>(
II->getNormalDest()->getFirstNonPHIOrDbg());
2564 if (
any_of(Invokes, HasNormalDest)) {
2567 if (!
all_of(Invokes, HasNormalDest))
2574 assert(CurrNormalBB &&
"There is always a 'continue to' basic block.");
2576 NormalBB = CurrNormalBB;
2577 else if (NormalBB != CurrNormalBB)
2585 NormalBB, {Invokes[0]->getParent(), Invokes[1]->
getParent()},
2596 assert(CurrUnwindBB &&
"There is always an 'unwind to' basic block.");
2598 UnwindBB = CurrUnwindBB;
2600 assert(UnwindBB == CurrUnwindBB &&
"Unexpected unwind destination.");
2607 Invokes.front()->getUnwindDest(),
2608 {Invokes[0]->getParent(), Invokes[1]->getParent()}))
2614 for (
auto *
II : Invokes.drop_front())
2615 if (!
II->isSameOperationAs(II0))
2619 auto IsIllegalToMergeArguments = [](
auto Ops) {
2620 Use &U0 = std::get<0>(Ops);
2621 Use &U1 = std::get<1>(Ops);
2628 assert(Invokes.size() == 2 &&
"Always called with exactly two candidates.");
2629 if (
any_of(
zip(Invokes[0]->data_ops(), Invokes[1]->data_ops()),
2630 IsIllegalToMergeArguments))
2642 assert(Invokes.
size() >= 2 &&
"Must have at least two invokes to merge.");
2648 bool HasNormalDest =
2649 !isa<UnreachableInst>(Invokes[0]->getNormalDest()->getFirstNonPHIOrDbg());
2653 InvokeInst *MergedInvoke = [&Invokes, HasNormalDest]() {
2657 II0->
getParent()->getIterator()->getNextNode();
2662 Ctx, II0BB->
getName() +
".invoke", Func, InsertBeforeBlock);
2664 auto *MergedInvoke = cast<InvokeInst>(II0->
clone());
2666 MergedInvoke->
insertInto(MergedInvokeBB, MergedInvokeBB->
end());
2668 if (!HasNormalDest) {
2672 Ctx, II0BB->
getName() +
".cont", Func, InsertBeforeBlock);
2679 return MergedInvoke;
2687 {DominatorTree::Insert,
II->getParent(), MergedInvoke->
getParent()});
2693 SuccBBOfMergedInvoke});
2700 {DominatorTree::Delete,
II->getParent(), SuccOfPredBB});
2703 bool IsIndirectCall = Invokes[0]->isIndirectCall();
2706 for (
Use &U : MergedInvoke->operands()) {
2708 if (MergedInvoke->isCallee(&U)) {
2709 if (!IsIndirectCall)
2711 }
else if (!MergedInvoke->isDataOperand(&U))
2716 return II->getOperand(
U.getOperandNo()) !=
U.get();
2723 U->getType(), Invokes.size(),
"", MergedInvoke->getIterator());
2735 Invokes.front()->getParent());
2743 if (!MergedDebugLoc)
2744 MergedDebugLoc =
II->getDebugLoc();
2752 OrigSuccBB->removePredecessor(
II->getParent());
2754 II->replaceAllUsesWith(MergedInvoke);
2755 II->eraseFromParent();
2758 MergedInvoke->setDebugLoc(MergedDebugLoc);
2759 ++NumInvokeSetsFormed;
2762 DTU->applyUpdates(Updates);
2789 bool Changed =
false;
2795 CompatibleSets Grouper;
2801 Grouper.insert(cast<InvokeInst>(PredBB->getTerminator()));
2805 if (Invokes.
size() < 2)
2817class EphemeralValueTracker {
2821 if (isa<AssumeInst>(
I))
2823 return !
I->mayHaveSideEffects() && !
I->isTerminator() &&
2825 return EphValues.count(cast<Instruction>(U));
2831 if (isEphemeral(
I)) {
2870 StoreInst *StoreToHoist = dyn_cast<StoreInst>(
I);
2882 unsigned MaxNumInstToLookAt = 9;
2886 if (!MaxNumInstToLookAt)
2888 --MaxNumInstToLookAt;
2891 if (CurI.mayWriteToMemory() && !isa<StoreInst>(CurI))
2894 if (
auto *SI = dyn_cast<StoreInst>(&CurI)) {
2898 if (SI->getPointerOperand() == StorePtr &&
2899 SI->getValueOperand()->getType() == StoreTy && SI->isSimple() &&
2900 SI->getAlign() >= StoreToHoist->
getAlign())
2902 return SI->getValueOperand();
2906 if (
auto *LI = dyn_cast<LoadInst>(&CurI)) {
2907 if (LI->getPointerOperand() == StorePtr && LI->
getType() == StoreTy &&
2908 LI->isSimple() && LI->getAlign() >= StoreToHoist->
getAlign()) {
2931 unsigned &SpeculatedInstructions,
2939 bool HaveRewritablePHIs =
false;
2957 HaveRewritablePHIs =
true;
2960 if (!OrigCE && !ThenCE)
2967 if (OrigCost + ThenCost > MaxCost)
2974 ++SpeculatedInstructions;
2975 if (SpeculatedInstructions > 1)
2979 return HaveRewritablePHIs;
2986 if (BI->
getMetadata(LLVMContext::MD_unpredictable))
2993 uint64_t EndWeight = Invert ? TWeight : FWeight;
2997 return BIEndProb < Likely;
3037bool SimplifyCFGOpt::speculativelyExecuteBB(
BranchInst *BI,
3044 if (isa<FCmpInst>(BrCond))
3054 bool Invert =
false;
3073 unsigned SpeculatedInstructions = 0;
3074 Value *SpeculatedStoreValue =
nullptr;
3076 EphemeralValueTracker EphTracker;
3079 if (isa<DbgInfoIntrinsic>(
I)) {
3087 if (isa<PseudoProbeInst>(
I)) {
3097 if (EphTracker.track(&
I))
3102 ++SpeculatedInstructions;
3103 if (SpeculatedInstructions > 1)
3109 &
I, BB, ThenBB, EndBB))))
3111 if (!SpeculatedStoreValue &&
3117 if (SpeculatedStoreValue)
3118 SpeculatedStore = cast<StoreInst>(&
I);
3123 for (
Use &
Op :
I.operands()) {
3128 ++SinkCandidateUseCounts[OpI];
3135 for (
const auto &[Inst, Count] : SinkCandidateUseCounts)
3137 ++SpeculatedInstructions;
3138 if (SpeculatedInstructions > 1)
3144 bool Convert = SpeculatedStore !=
nullptr;
3147 SpeculatedInstructions,
3149 if (!Convert ||
Cost > Budget)
3153 LLVM_DEBUG(
dbgs() <<
"SPECULATIVELY EXECUTING BB" << *ThenBB <<
"\n";);
3156 if (SpeculatedStoreValue) {
3160 Value *FalseV = SpeculatedStoreValue;
3164 BrCond, TrueV, FalseV,
"spec.store.select", BI);
3193 auto replaceVariable = [OrigV, S](
auto *DbgAssign) {
3195 DbgAssign->replaceVariableLocationOp(OrigV, S);
3208 if (!SpeculatedStoreValue || &
I != SpeculatedStore) {
3210 if (!isa<DbgAssignIntrinsic>(&
I))
3213 I.dropUBImplyingAttrsAndMetadata();
3216 if (EphTracker.contains(&
I)) {
3218 I.eraseFromParent();
3232 It.dropOneDbgRecord(&DR);
3234 std::prev(ThenBB->
end()));
3251 Value *TrueV = ThenV, *FalseV = OrigV;
3265 if (!isa<DbgAssignIntrinsic>(
I))
3266 I->eraseFromParent();
3276 EphemeralValueTracker EphTracker;
3282 if (
CallInst *CI = dyn_cast<CallInst>(&
I))
3283 if (CI->cannotDuplicate() || CI->isConvergent())
3289 if (!EphTracker.track(&
I) && !isa<PHINode>(
I)) {
3296 for (
User *U :
I.users()) {
3298 if (UI->
getParent() != BB || isa<PHINode>(UI))
3312 auto *
I = dyn_cast<Instruction>(V);
3313 if (
I &&
I->getParent() == To)
3317 auto *BI = dyn_cast<BranchInst>(
From->getTerminator());
3329static std::optional<bool>
3345 if (
auto *CB = dyn_cast<ConstantInt>(U))
3350 KnownValues[CB].
insert(Pred);
3354 if (KnownValues.
empty())
3363 for (
const auto &Pair : KnownValues) {
3381 <<
" has value " << *Pair.first <<
" in predecessors:\n";
3384 dbgs() <<
"Threading to destination " << RealDest->
getName() <<
".\n";
3392 EdgeBB->setName(RealDest->
getName() +
".critedge");
3393 EdgeBB->moveBefore(RealDest);
3403 TranslateMap[
Cond] = CB;
3409 if (
PHINode *PN = dyn_cast<PHINode>(BBI)) {
3416 N->insertInto(EdgeBB, InsertPt);
3419 N->setName(BBI->getName() +
".c");
3422 for (
Use &
Op :
N->operands()) {
3424 if (PI != TranslateMap.
end())
3430 if (!BBI->use_empty())
3431 TranslateMap[&*BBI] = V;
3432 if (!
N->mayHaveSideEffects()) {
3433 N->eraseFromParent();
3438 if (!BBI->use_empty())
3439 TranslateMap[&*BBI] =
N;
3445 for (; SrcDbgCursor != BBI; ++SrcDbgCursor)
3446 N->cloneDebugInfoFrom(&*SrcDbgCursor);
3447 SrcDbgCursor = std::next(BBI);
3449 N->cloneDebugInfoFrom(&*BBI);
3452 if (
auto *Assume = dyn_cast<AssumeInst>(
N))
3458 for (; &*SrcDbgCursor != BI; ++SrcDbgCursor)
3459 InsertPt->cloneDebugInfoFrom(&*SrcDbgCursor);
3460 InsertPt->cloneDebugInfoFrom(BI);
3463 BranchInst *EdgeBI = cast<BranchInst>(EdgeBB->getTerminator());
3469 Updates.
push_back({DominatorTree::Delete, EdgeBB, BB});
3470 Updates.
push_back({DominatorTree::Insert, EdgeBB, RealDest});
3481 return std::nullopt;
3491 std::optional<bool> Result;
3492 bool EverChanged =
false;
3496 EverChanged |= Result == std::nullopt || *Result;
3497 }
while (Result == std::nullopt);
3505 bool SpeculateUnpredictables) {
3520 if (isa<ConstantInt>(IfCond))
3527 return cast<BranchInst>(IfBlock->getTerminator())->isUnconditional();
3530 "Will have either one or two blocks to speculate.");
3537 bool IsUnpredictable = DomBI->
getMetadata(LLVMContext::MD_unpredictable);
3538 if (!IsUnpredictable) {
3541 (TWeight + FWeight) != 0) {
3546 if (IfBlocks.
size() == 1) {
3548 DomBI->
getSuccessor(0) == BB ? BITrueProb : BIFalseProb;
3549 if (BIBBProb >= Likely)
3552 if (BITrueProb >= Likely || BIFalseProb >= Likely)
3560 if (
auto *IfCondPhiInst = dyn_cast<PHINode>(IfCond))
3561 if (IfCondPhiInst->getParent() == BB)
3569 unsigned NumPhis = 0;
3581 if (SpeculateUnpredictables && IsUnpredictable)
3584 bool Changed =
false;
3603 PN = dyn_cast<PHINode>(BB->
begin());
3609 auto CanHoistNotFromBothValues = [](
Value *V0,
Value *V1) {
3620 auto IsBinOpOrAnd = [](
Value *V) {
3640 if (!AggressiveInsts.
count(&*
I) && !
I->isDebugOrPseudoInst()) {
3653 if (IsUnpredictable)
dbgs() <<
" (unpredictable)";
3655 <<
" F: " << IfFalse->
getName() <<
"\n");
3669 if (isa<FPMathOperator>(PN))
3689 Updates.
push_back({DominatorTree::Insert, DomBlock, BB});
3707 if (Opc == Instruction::And)
3709 if (Opc == Instruction::Or)
3722 bool PredHasWeights =
3724 bool SuccHasWeights =
3726 if (PredHasWeights || SuccHasWeights) {
3727 if (!PredHasWeights)
3728 PredTrueWeight = PredFalseWeight = 1;
3729 if (!SuccHasWeights)
3730 SuccTrueWeight = SuccFalseWeight = 1;
3740static std::optional<std::tuple<BasicBlock *, Instruction::BinaryOps, bool>>
3744 "Both blocks must end with a conditional branches.");
3746 "PredBB must be a predecessor of BB.");
3754 (PTWeight + PFWeight) != 0) {
3762 if (PBITrueProb.
isUnknown() || PBITrueProb < Likely)
3763 return {{BI->
getSuccessor(0), Instruction::Or,
false}};
3767 return {{BI->
getSuccessor(1), Instruction::And,
false}};
3770 if (PBITrueProb.
isUnknown() || PBITrueProb < Likely)
3771 return {{BI->
getSuccessor(1), Instruction::And,
true}};
3777 return std::nullopt;
3790 bool InvertPredCond;
3791 std::tie(CommonSucc, Opc, InvertPredCond) =
3794 LLVM_DEBUG(
dbgs() <<
"FOLDING BRANCH TO COMMON DEST:\n" << *PBI << *BB);
3801 {LLVMContext::MD_annotation});
3804 if (InvertPredCond) {
3817 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
3819 SuccTrueWeight, SuccFalseWeight)) {
3826 NewWeights.
push_back(PredTrueWeight * SuccTrueWeight);
3832 (SuccFalseWeight + SuccTrueWeight) +
3833 PredTrueWeight * SuccFalseWeight);
3839 NewWeights.
push_back(PredTrueWeight * (SuccFalseWeight + SuccTrueWeight) +
3840 PredFalseWeight * SuccTrueWeight);
3842 NewWeights.
push_back(PredFalseWeight * SuccFalseWeight);
3860 DTU->
applyUpdates({{DominatorTree::Insert, PredBlock, UniqueSucc},
3861 {DominatorTree::Delete, PredBlock, BB}});
3888 ++NumFoldBranchToCommonDest;
3895 return I.getType()->isVectorTy() ||
any_of(
I.operands(), [](
Use &U) {
3896 return U->getType()->isVectorTy();
3906 unsigned BonusInstThreshold) {
3920 (!isa<CmpInst>(
Cond) && !isa<BinaryOperator>(
Cond) &&
3921 !isa<SelectInst>(
Cond)) ||
3922 Cond->getParent() != BB || !
Cond->hasOneUse())
3932 BranchInst *PBI = dyn_cast<BranchInst>(PredBlock->getTerminator());
3943 bool InvertPredCond;
3945 std::tie(CommonSucc, Opc, InvertPredCond) = *Recipe;
3977 unsigned NumBonusInsts = 0;
3978 bool SawVectorOp =
false;
3979 const unsigned PredCount = Preds.
size();
3985 if (isa<DbgInfoIntrinsic>(
I) || isa<BranchInst>(
I))
3996 NumBonusInsts += PredCount;
4004 auto IsBCSSAUse = [BB, &
I](
Use &U) {
4005 auto *UI = cast<Instruction>(U.getUser());
4006 if (
auto *PN = dyn_cast<PHINode>(UI))
4008 return UI->
getParent() == BB &&
I.comesBefore(UI);
4012 if (!
all_of(
I.uses(), IsBCSSAUse))
4016 BonusInstThreshold *
4022 auto *PBI = cast<BranchInst>(PredBlock->getTerminator());
4032 for (
auto *BB : {BB1, BB2}) {
4036 if (
auto *SI = dyn_cast<StoreInst>(&
I)) {
4048 Value *AlternativeV =
nullptr) {
4066 for (
auto I = Succ->
begin(); isa<PHINode>(
I); ++
I)
4067 if (cast<PHINode>(
I)->getIncomingValueForBlock(BB) == V) {
4068 PHI = cast<PHINode>(
I);
4074 BasicBlock *OtherPredBB = *PredI == BB ? *++PredI : *PredI;
4075 if (
PHI->getIncomingValueForBlock(OtherPredBB) == AlternativeV)
4083 if (!AlternativeV &&
4084 (!isa<Instruction>(V) || cast<Instruction>(V)->
getParent() != BB))
4089 PHI->addIncoming(V, BB);
4099 BasicBlock *PostBB,
Value *Address,
bool InvertPCond,
bool InvertQCond,
4108 if (!PStore || !QStore)
4129 if (
I.mayReadOrWriteMemory())
4131 for (
auto &
I : *QFB)
4132 if (&
I != QStore &&
I.mayReadOrWriteMemory())
4135 for (
auto &
I : *QTB)
4136 if (&
I != QStore &&
I.mayReadOrWriteMemory())
4140 if (&*
I != PStore &&
I->mayReadOrWriteMemory())
4156 if (
I.isTerminator())
4160 if (
auto *S = dyn_cast<StoreInst>(&
I))
4164 if (!isa<BinaryOperator>(
I) && !isa<GetElementPtrInst>(
I))
4174 "When we run out of budget we will eagerly return from within the "
4175 "per-instruction loop.");
4179 const std::array<StoreInst *, 2> FreeStores = {PStore, QStore};
4181 (!IsWorthwhile(PTB, FreeStores) || !IsWorthwhile(PFB, FreeStores) ||
4182 !IsWorthwhile(QTB, FreeStores) || !IsWorthwhile(QFB, FreeStores)))
4290 bool InvertPCond =
false, InvertQCond =
false;
4296 if (QFB == PostBB) {
4315 !HasOnePredAndOneSucc(QFB, QBI->
getParent(), PostBB))
4318 (QTB && !HasOnePredAndOneSucc(QTB, QBI->
getParent(), PostBB)))
4326 for (
auto *BB : {PTB, PFB}) {
4331 PStoreAddresses.
insert(SI->getPointerOperand());
4333 for (
auto *BB : {QTB, QFB}) {
4338 QStoreAddresses.
insert(SI->getPointerOperand());
4344 auto &CommonAddresses = PStoreAddresses;
4346 bool Changed =
false;
4347 for (
auto *Address : CommonAddresses)
4350 InvertPCond, InvertQCond, DTU,
DL,
TTI);
4368 !BI->
getParent()->getSinglePredecessor())
4370 if (!IfFalseBB->
phis().empty())
4380 return I.mayWriteToMemory() ||
I.mayHaveSideEffects();
4391 {{DominatorTree::Insert, BI->
getParent(), IfFalseBB},
4392 {DominatorTree::Delete, BI->
getParent(), OldSuccessor}});
4403 {{DominatorTree::Insert, BI->
getParent(), IfFalseBB},
4404 {DominatorTree::Delete, BI->
getParent(), OldSuccessor}});
4483 if (!PBI->
getMetadata(LLVMContext::MD_unpredictable) &&
4485 (
static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]) != 0) {
4489 static_cast<uint64_t>(PredWeights[0]) + PredWeights[1]);
4492 if (CommonDestProb >= Likely)
4502 unsigned NumPhis = 0;
4524 if (OtherDest == BB) {
4531 Updates.
push_back({DominatorTree::Insert, InfLoopBlock, InfLoopBlock});
4532 OtherDest = InfLoopBlock;
4552 createLogicalOp(Builder, Instruction::Or, PBICond, BICond,
"brmerge");
4567 uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
4568 uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
4571 SuccTrueWeight, SuccFalseWeight);
4573 PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
4574 PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
4575 SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
4576 SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
4580 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
4581 PredOther * SuccCommon,
4582 PredOther * SuccOther};
4611 uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
4612 uint64_t PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
4613 uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
4614 uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
4617 uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
4618 PredOther * SuccCommon};
4641bool SimplifyCFGOpt::simplifyTerminatorOnSelect(
Instruction *OldTerm,
4652 BasicBlock *KeepEdge2 = TrueBB != FalseBB ? FalseBB :
nullptr;
4659 if (Succ == KeepEdge1)
4660 KeepEdge1 =
nullptr;
4661 else if (Succ == KeepEdge2)
4662 KeepEdge2 =
nullptr;
4667 if (Succ != TrueBB && Succ != FalseBB)
4668 RemovedSuccessors.
insert(Succ);
4676 if (!KeepEdge1 && !KeepEdge2) {
4677 if (TrueBB == FalseBB) {
4685 if (TrueWeight != FalseWeight)
4688 }
else if (KeepEdge1 && (KeepEdge2 || TrueBB == FalseBB)) {
4710 for (
auto *RemovedSuccessor : RemovedSuccessors)
4711 Updates.
push_back({DominatorTree::Delete, BB, RemovedSuccessor});
4712 DTU->applyUpdates(Updates);
4722bool SimplifyCFGOpt::simplifySwitchOnSelect(
SwitchInst *SI,
4727 if (!TrueVal || !FalseVal)
4732 BasicBlock *TrueBB =
SI->findCaseValue(TrueVal)->getCaseSuccessor();
4733 BasicBlock *FalseBB =
SI->findCaseValue(FalseVal)->getCaseSuccessor();
4736 uint32_t TrueWeight = 0, FalseWeight = 0;
4741 if (Weights.
size() == 1 +
SI->getNumCases()) {
4743 (
uint32_t)Weights[
SI->findCaseValue(TrueVal)->getSuccessorIndex()];
4745 (
uint32_t)Weights[
SI->findCaseValue(FalseVal)->getSuccessorIndex()];
4750 return simplifyTerminatorOnSelect(SI, Condition, TrueBB, FalseBB, TrueWeight,
4759bool SimplifyCFGOpt::simplifyIndirectBrOnSelect(
IndirectBrInst *IBI,
4772 return simplifyTerminatorOnSelect(IBI,
SI->getCondition(), TrueBB, FalseBB, 0,
4793bool SimplifyCFGOpt::tryToSimplifyUncondBranchWithICmpInIt(
4813 if (
SI->getCondition() != V)
4819 if (
SI->getDefaultDest() != BB) {
4821 assert(VVal &&
"Should have a unique destination value");
4829 return requestResimplify();
4835 if (
SI->findCaseValue(Cst) !=
SI->case_default()) {
4845 return requestResimplify();
4852 if (PHIUse ==
nullptr || PHIUse != &SuccBlock->
front() ||
4877 auto W0 = SIW.getSuccessorWeight(0);
4881 SIW.setSuccessorWeight(0, *NewW);
4883 SIW.addCase(Cst, NewBB, NewW);
4885 Updates.
push_back({DominatorTree::Insert, Pred, NewBB});
4894 Updates.
push_back({DominatorTree::Insert, NewBB, SuccBlock});
4895 DTU->applyUpdates(Updates);
4903bool SimplifyCFGOpt::simplifyBranchOnICmpChain(
BranchInst *BI,
4915 ConstantComparesGatherer ConstantCompare(
Cond,
DL);
4918 Value *CompVal = ConstantCompare.CompValue;
4919 unsigned UsedICmps = ConstantCompare.UsedICmps;
4920 Value *ExtraCase = ConstantCompare.Extra;
4939 if (ExtraCase && Values.
size() < 2)
4954 <<
" cases into SWITCH. BB is:\n"
4964 nullptr,
"switch.early.test");
4988 Updates.
push_back({DominatorTree::Insert, BB, EdgeBB});
4994 LLVM_DEBUG(
dbgs() <<
" ** 'icmp' chain unhandled condition: " << *ExtraCase
4995 <<
"\nEXTRABB = " << *BB);
5003 CompVal,
DL.getIntPtrType(CompVal->
getType()),
"magicptr");
5010 for (
unsigned i = 0, e = Values.
size(); i != e; ++i)
5011 New->addCase(Values[i], EdgeBB);
5017 PHINode *PN = cast<PHINode>(BBI);
5019 for (
unsigned i = 0, e = Values.
size() - 1; i != e; ++i)
5026 DTU->applyUpdates(Updates);
5028 LLVM_DEBUG(
dbgs() <<
" ** 'icmp' chain result is:\n" << *BB <<
'\n');
5034 return simplifyCommonResume(RI);
5035 else if (isa<LandingPadInst>(RI->
getParent()->getFirstNonPHI()) &&
5038 return simplifySingleResume(RI);
5046 auto *
II = dyn_cast<IntrinsicInst>(&
I);
5051 switch (IntrinsicID) {
5052 case Intrinsic::dbg_declare:
5053 case Intrinsic::dbg_value:
5054 case Intrinsic::dbg_label:
5055 case Intrinsic::lifetime_end:
5065bool SimplifyCFGOpt::simplifyCommonResume(
ResumeInst *RI) {
5075 auto *PhiLPInst = cast<PHINode>(RI->
getValue());
5078 for (
unsigned Idx = 0,
End = PhiLPInst->getNumIncomingValues();
Idx !=
End;
5080 auto *IncomingBB = PhiLPInst->getIncomingBlock(
Idx);
5081 auto *IncomingValue = PhiLPInst->getIncomingValue(
Idx);
5085 if (IncomingBB->getUniqueSuccessor() != BB)
5088 auto *LandingPad = dyn_cast<LandingPadInst>(IncomingBB->getFirstNonPHI());
5090 if (IncomingValue != LandingPad)
5094 make_range(LandingPad->getNextNode(), IncomingBB->getTerminator())))
5095 TrivialUnwindBlocks.
insert(IncomingBB);
5099 if (TrivialUnwindBlocks.
empty())
5103 for (
auto *TrivialBB : TrivialUnwindBlocks) {
5107 while (PhiLPInst->getBasicBlockIndex(TrivialBB) != -1)
5121 TrivialBB->getTerminator()->eraseFromParent();
5124 DTU->applyUpdates({{DominatorTree::Delete, TrivialBB, BB}});
5131 return !TrivialUnwindBlocks.empty();
5135bool SimplifyCFGOpt::simplifySingleResume(
ResumeInst *RI) {
5139 "Resume must unwind the exception that caused control to here");
5143 make_range<Instruction *>(LPInst->getNextNode(), RI)))