83using namespace jumpthreading;
85#define DEBUG_TYPE "jump-threading"
89STATISTIC(NumDupes,
"Number of branch blocks duplicated to eliminate phi");
93 cl::desc(
"Max block size to duplicate for jump threading"),
98 "jump-threading-implication-search-threshold",
99 cl::desc(
"The number of predecessors to search for a stronger "
100 "condition to use to thread over a weaker condition"),
104 "jump-threading-phi-threshold",
109 "print-lvi-after-jump-threading",
110 cl::desc(
"Print the LazyValueInfo cache after JumpThreading"),
cl::init(
false),
114 "jump-threading-across-loop-headers",
115 cl::desc(
"Allow JumpThreading to thread across loop headers, for testing"),
166 if (TrueWeight + FalseWeight == 0)
174 auto GetPredOutEdge =
176 BasicBlock *PhiBB) -> std::pair<BasicBlock *, BasicBlock *> {
177 auto *PredBB = IncomingBB;
178 auto *SuccBB = PhiBB;
181 BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
183 return {PredBB, SuccBB};
185 auto *SinglePredBB = PredBB->getSinglePredecessor();
187 return {
nullptr,
nullptr};
191 if (Visited.
count(SinglePredBB))
192 return {
nullptr,
nullptr};
195 PredBB = SinglePredBB;
208 TrueWeight, TrueWeight + FalseWeight)
210 FalseWeight, TrueWeight + FalseWeight));
213 if (!PredOutEdge.first)
221 uint64_t PredTrueWeight, PredFalseWeight;
261 std::make_unique<DomTreeUpdater>(
263 std::nullopt, std::nullopt);
266 dbgs() <<
"LVI for function '" <<
F.getName() <<
"':\n";
276#if defined(EXPENSIVE_CHECKS)
278 DominatorTree::VerificationLevel::Full) &&
279 "DT broken after JumpThreading");
283 "PDT broken after JumpThreading");
286 DominatorTree::VerificationLevel::Fast) &&
287 "DT broken after JumpThreading");
291 "PDT broken after JumpThreading");
294 return getPreservedAnalysis();
301 std::unique_ptr<DomTreeUpdater> DTU_,
302 std::optional<BlockFrequencyInfo *> BFI_,
303 std::optional<BranchProbabilityInfo *> BPI_) {
311 DTU = std::move(DTU_);
316 HasGuards = GuardDecl && !GuardDecl->use_empty();
325 BBDupThreshold = DefaultBBDupThreshold;
330 assert(DTU &&
"DTU isn't passed into JumpThreading before using it.");
331 assert(DTU->hasDomTree() &&
"JumpThreading relies on DomTree to proceed.");
340 bool EverChanged =
false;
344 for (
auto &BB : *
F) {
345 if (Unreachable.
count(&BB))
348 Changed = ChangedSinceLastAnalysisUpdate =
true;
358 if (&BB == &
F->getEntryBlock() || DTU->isBBPendingDeletion(&BB))
365 <<
"' with terminator: " << *BB.getTerminator()
367 LoopHeaders.erase(&BB);
370 Changed = ChangedSinceLastAnalysisUpdate =
true;
376 auto *BI = dyn_cast<BranchInst>(BB.getTerminator());
377 if (BI && BI->isUnconditional()) {
381 BB.getFirstNonPHIOrDbg(
true)->isTerminator() &&
384 !LoopHeaders.count(&BB) && !LoopHeaders.count(Succ) &&
390 Changed = ChangedSinceLastAnalysisUpdate =
true;
394 EverChanged |= Changed;
410 bool Changed =
false;
415 if (
Cond->getParent() == KnownAtEndOfBB)
426 Changed |=
I.replaceUsesOfWith(
Cond, ToVal);
428 if (
Cond->use_empty() && !
Cond->mayHaveSideEffects()) {
429 Cond->eraseFromParent();
441 unsigned Threshold) {
442 assert(StopAt->
getParent() == BB &&
"Not an instruction from proper BB?");
447 unsigned PhiCount = 0;
450 if (!isa<PHINode>(&
I)) {
469 if (isa<SwitchInst>(StopAt))
473 if (isa<IndirectBrInst>(StopAt))
484 for (; &*
I != StopAt; ++
I) {
487 if (
Size > Threshold)
492 if (
I->getType()->isTokenTy() &&
I->isUsedOutsideOfBlock(BB))
497 if (
const CallInst *CI = dyn_cast<CallInst>(
I))
498 if (CI->cannotDuplicate() || CI->isConvergent())
512 if (
const CallInst *CI = dyn_cast<CallInst>(
I)) {
513 if (!isa<IntrinsicInst>(CI))
515 else if (!CI->getType()->isVectorTy())
520 return Size > Bonus ?
Size - Bonus : 0;
541 for (
const auto &Edge : Edges)
542 LoopHeaders.insert(Edge.second);
555 if (
UndefValue *U = dyn_cast<UndefValue>(Val))
561 return dyn_cast<ConstantInt>(Val);
578 if (!RecursionSet.
insert(V).second)
584 Result.emplace_back(KC, Pred);
586 return !Result.empty();
592 if (!
I ||
I->getParent() != BB) {
597 using namespace PatternMatch;
614 Result.emplace_back(KC,
P);
617 return !Result.empty();
621 if (
PHINode *PN = dyn_cast<PHINode>(
I)) {
622 for (
unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
623 Value *InVal = PN->getIncomingValue(i);
625 Result.emplace_back(KC, PN->getIncomingBlock(i));
628 PN->getIncomingBlock(i),
631 Result.emplace_back(KC, PN->getIncomingBlock(i));
635 return !Result.empty();
639 if (
CastInst *CI = dyn_cast<CastInst>(
I)) {
640 Value *Source = CI->getOperand(0);
647 for (
auto &R : Result)
654 Value *Source = FI->getOperand(0);
662 return !Result.empty();
666 if (
I->getType()->getPrimitiveSizeInBits() == 1) {
667 using namespace PatternMatch;
695 for (
const auto &LHSVal : LHSVals)
696 if (LHSVal.first == InterestingVal || isa<UndefValue>(LHSVal.first)) {
697 Result.emplace_back(InterestingVal, LHSVal.second);
698 LHSKnownBBs.
insert(LHSVal.second);
700 for (
const auto &RHSVal : RHSVals)
701 if (RHSVal.first == InterestingVal || isa<UndefValue>(RHSVal.first)) {
704 if (!LHSKnownBBs.
count(RHSVal.second))
705 Result.emplace_back(InterestingVal, RHSVal.second);
708 return !Result.empty();
712 if (
I->getOpcode() == Instruction::Xor &&
713 isa<ConstantInt>(
I->getOperand(1)) &&
714 cast<ConstantInt>(
I->getOperand(1))->isOne()) {
721 for (
auto &R : Result)
731 if (
ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
732 const DataLayout &
DL = BO->getModule()->getDataLayout();
738 for (
const auto &LHSVal : LHSVals) {
744 Result.emplace_back(KC, LHSVal.second);
748 return !Result.empty();
752 if (
CmpInst *Cmp = dyn_cast<CmpInst>(
I)) {
755 Type *CmpType = Cmp->getType();
756 Value *CmpLHS = Cmp->getOperand(0);
757 Value *CmpRHS = Cmp->getOperand(1);
760 PHINode *PN = dyn_cast<PHINode>(CmpLHS);
762 PN = dyn_cast<PHINode>(CmpRHS);
779 if (!isa<Constant>(
RHS))
783 auto LHSInst = dyn_cast<Instruction>(
LHS);
784 if (LHSInst && LHSInst->getParent() == BB)
789 cast<Constant>(
RHS), PredBB, BB,
797 Result.emplace_back(KC, PredBB);
800 return !Result.empty();
805 if (isa<Constant>(CmpRHS) && !CmpType->
isVectorTy()) {
806 Constant *CmpConst = cast<Constant>(CmpRHS);
808 if (!isa<Instruction>(CmpLHS) ||
809 cast<Instruction>(CmpLHS)->
getParent() != BB) {
815 CmpConst,
P, BB, CxtI ? CxtI : Cmp);
820 Result.emplace_back(ResC,
P);
823 return !Result.empty();
830 using namespace PatternMatch;
834 if (isa<ConstantInt>(CmpConst) &&
836 if (!isa<Instruction>(AddLHS) ||
837 cast<Instruction>(AddLHS)->
getParent() != BB) {
843 AddLHS,
P, BB, CxtI ? CxtI : cast<Instruction>(CmpLHS));
849 Pred, cast<ConstantInt>(CmpConst)->getValue());
859 Result.emplace_back(ResC,
P);
862 return !Result.empty();
873 for (
const auto &LHSVal : LHSVals) {
877 Result.emplace_back(KC, LHSVal.second);
880 return !Result.empty();
890 if ((TrueVal || FalseVal) &&
893 for (
auto &
C : Conds) {
900 KnownCond = CI->isOne();
902 assert(isa<UndefValue>(
Cond) &&
"Unexpected condition value");
906 KnownCond = (TrueVal !=
nullptr);
910 if (
Constant *Val = KnownCond ? TrueVal : FalseVal)
911 Result.emplace_back(Val,
C.second);
914 return !Result.empty();
923 Result.emplace_back(KC, Pred);
926 return !Result.empty();
936 unsigned MinSucc = 0;
939 unsigned MinNumPreds =
pred_size(TestBB);
943 if (NumPreds < MinNumPreds) {
945 MinNumPreds = NumPreds;
967 if (DTU->isBBPendingDeletion(BB) ||
992 if (
BranchInst *BI = dyn_cast<BranchInst>(Terminator)) {
994 if (BI->isUnconditional())
return false;
995 Condition = BI->getCondition();
996 }
else if (
SwitchInst *
SI = dyn_cast<SwitchInst>(Terminator)) {
997 Condition =
SI->getCondition();
998 }
else if (
IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) {
1000 if (IB->getNumSuccessors() == 0)
return false;
1001 Condition = IB->getAddress()->stripPointerCasts();
1008 bool ConstantFolded =
false;
1012 if (
Instruction *
I = dyn_cast<Instruction>(Condition)) {
1016 I->replaceAllUsesWith(SimpleVal);
1018 I->eraseFromParent();
1019 Condition = SimpleVal;
1020 ConstantFolded =
true;
1026 auto *FI = dyn_cast<FreezeInst>(Condition);
1027 if (isa<UndefValue>(Condition) ||
1028 (FI && isa<UndefValue>(FI->getOperand(0)) && FI->hasOneUse())) {
1030 std::vector<DominatorTree::UpdateType> Updates;
1036 if (i == BestSucc)
continue;
1043 <<
"' folding undef terminator: " << *BBTerm <<
'\n');
1047 DTU->applyUpdatesPermissive(Updates);
1049 FI->eraseFromParent();
1062 if (
auto *BPI = getBPI())
1063 BPI->eraseBlock(BB);
1067 Instruction *CondInst = dyn_cast<Instruction>(Condition);
1074 return ConstantFolded;
1078 Value *CondWithoutFreeze = CondInst;
1079 if (
auto *FI = dyn_cast<FreezeInst>(CondInst))
1080 CondWithoutFreeze = FI->getOperand(0);
1082 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(CondWithoutFreeze)) {
1086 if (
Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1))) {
1088 LVI->
getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
1121 Value *SimplifyValue = CondWithoutFreeze;
1123 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue))
1124 if (isa<Constant>(CondCmp->getOperand(1)))
1125 SimplifyValue = CondCmp->getOperand(0);
1129 if (
LoadInst *LoadI = dyn_cast<LoadInst>(SimplifyValue))
1134 if (
PHINode *PN = dyn_cast<PHINode>(CondInst))
1135 if (PN->getParent() == BB && isa<BranchInst>(BB->
getTerminator()))
1146 PHINode *PN = dyn_cast<PHINode>(CondWithoutFreeze);
1151 if (CondInst->
getOpcode() == Instruction::Xor &&
1165 if (!BI || !BI->isConditional())
1174 auto *FICond = dyn_cast<FreezeInst>(
Cond);
1175 if (FICond && FICond->hasOneUse())
1176 Cond = FICond->getOperand(0);
1187 auto *PBI = dyn_cast<BranchInst>(CurrentPred->
getTerminator());
1188 if (!PBI || !PBI->isConditional())
1190 if (PBI->getSuccessor(0) != CurrentBB && PBI->getSuccessor(1) != CurrentBB)
1193 bool CondIsTrue = PBI->getSuccessor(0) == CurrentBB;
1194 std::optional<bool> Implication =
1199 if (!Implication && FICond && isa<FreezeInst>(PBI->getCondition())) {
1200 if (cast<FreezeInst>(PBI->getCondition())->getOperand(0) ==
1201 FICond->getOperand(0))
1202 Implication = CondIsTrue;
1206 BasicBlock *KeepSucc = BI->getSuccessor(*Implication ? 0 : 1);
1207 BasicBlock *RemoveSucc = BI->getSuccessor(*Implication ? 1 : 0);
1212 BI->eraseFromParent();
1214 FICond->eraseFromParent();
1217 if (
auto *BPI = getBPI())
1218 BPI->eraseBlock(BB);
1221 CurrentBB = CurrentPred;
1230 if (
Instruction *OpInst = dyn_cast<Instruction>(Op))
1231 if (OpInst->getParent() == BB)
1273 LoadInst *NLoadI = cast<LoadInst>(AvailableVal);
1279 if (AvailableVal == LoadI)
1281 if (AvailableVal->getType() != LoadI->
getType())
1283 AvailableVal, LoadI->
getType(),
"", LoadI);
1292 if (BBIt != LoadBB->
begin())
1303 AvailablePredsTy AvailablePreds;
1311 if (!PredsScanned.
insert(PredBB).second)
1314 BBIt = PredBB->
end();
1315 unsigned NumScanedInst = 0;
1316 Value *PredAvailable =
nullptr;
1320 "Attempting to CSE volatile or atomic loads");
1330 AA, &IsLoadCSE, &NumScanedInst);
1335 while (!PredAvailable && SinglePredBB && BBIt == SinglePredBB->
begin() &&
1339 BBIt = SinglePredBB->
end();
1341 Loc, AccessTy, LoadI->
isAtomic(), SinglePredBB, BBIt,
1347 if (!PredAvailable) {
1348 OneUnavailablePred = PredBB;
1353 CSELoads.
push_back(cast<LoadInst>(PredAvailable));
1357 AvailablePreds.emplace_back(PredBB, PredAvailable);
1362 if (AvailablePreds.empty())
return false;
1379 if (PredsScanned.
size() != AvailablePreds.size() &&
1381 for (
auto I = LoadBB->
begin(); &*
I != LoadI; ++
I)
1388 if (PredsScanned.
size() == AvailablePreds.size()+1 &&
1390 UnavailablePred = OneUnavailablePred;
1391 }
else if (PredsScanned.
size() != AvailablePreds.size()) {
1397 for (
const auto &AvailablePred : AvailablePreds)
1398 AvailablePredSet.
insert(AvailablePred.first);
1403 if (isa<IndirectBrInst>(
P->getTerminator()))
1406 if (!AvailablePredSet.
count(
P))
1411 UnavailablePred = splitBlockPreds(LoadBB, PredsToSplit,
"thread-pre-split");
1417 if (UnavailablePred) {
1419 "Can't handle critical edge here!");
1429 AvailablePreds.emplace_back(UnavailablePred, NewVal);
1447 AvailablePredsTy::iterator
I =
1450 assert(
I != AvailablePreds.end() &&
I->first ==
P &&
1451 "Didn't find entry for predecessor!");
1457 Value *&PredV =
I->second;
1460 P->getTerminator());
1465 for (
LoadInst *PredLoadI : CSELoads) {
1482 assert(!PredToDestList.empty());
1494 DestPopularity[
nullptr] = 0;
1496 DestPopularity[SuccBB] = 0;
1498 for (
const auto &PredToDest : PredToDestList)
1499 if (PredToDest.second)
1500 DestPopularity[PredToDest.second]++;
1503 auto MostPopular = std::max_element(
1507 return MostPopular->first;
1516 assert(PredBB &&
"Expected a single predecessor");
1518 if (
Constant *Cst = dyn_cast<Constant>(V)) {
1524 if (!
I || (
I->getParent() != BB &&
I->getParent() != PredBB)) {
1530 if (
PHI->getParent() == PredBB)
1531 return dyn_cast<Constant>(
PHI->getIncomingValueForBlock(PredPredBB));
1536 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(V)) {
1537 if (CondCmp->getParent() == BB) {
1557 if (LoopHeaders.count(BB))
1569 "computeValueKnownInPredecessors returned true with no values");
1572 for (
const auto &PredValue : PredValues) {
1574 <<
"': FOUND condition = " << *PredValue.first
1575 <<
" for pred '" << PredValue.second->getName() <<
"'.\n";
1590 for (
const auto &PredValue : PredValues) {
1592 if (!SeenPreds.insert(Pred).second)
1598 if (isa<UndefValue>(Val))
1601 assert(isa<ConstantInt>(Val) &&
"Expecting a constant integer");
1602 DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->
isZero());
1604 assert(isa<ConstantInt>(Val) &&
"Expecting a constant integer");
1605 DestBB =
SI->findCaseValue(cast<ConstantInt>(Val))->getCaseSuccessor();
1608 &&
"Unexpected terminator");
1609 assert(isa<BlockAddress>(Val) &&
"Expecting a constant blockaddress");
1610 DestBB = cast<BlockAddress>(Val)->getBasicBlock();
1614 if (PredToDestList.
empty()) {
1618 if (OnlyDest != DestBB)
1619 OnlyDest = MultipleDestSentinel;
1623 OnlyVal = MultipleVal;
1635 if (PredToDestList.
empty())
1641 if (OnlyDest && OnlyDest != MultipleDestSentinel) {
1643 bool SeenFirstBranchToOnlyDest =
false;
1644 std::vector <DominatorTree::UpdateType> Updates;
1647 if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest) {
1648 SeenFirstBranchToOnlyDest =
true;
1650 SuccBB->removePredecessor(BB,
true);
1659 Term->eraseFromParent();
1660 DTU->applyUpdatesPermissive(Updates);
1661 if (
auto *BPI = getBPI())
1662 BPI->eraseBlock(BB);
1666 if (
auto *CondInst = dyn_cast<Instruction>(
Cond)) {
1667 if (CondInst->use_empty() && !CondInst->mayHaveSideEffects())
1668 CondInst->eraseFromParent();
1676 else if (OnlyVal && OnlyVal != MultipleVal)
1689 if (MostPopularDest == MultipleDestSentinel) {
1694 [&](
const std::pair<BasicBlock *, BasicBlock *> &PredToDest) {
1695 return LoopHeaders.contains(PredToDest.second);
1698 if (PredToDestList.
empty())
1707 for (
const auto &PredToDest : PredToDestList)
1708 if (PredToDest.second == MostPopularDest) {
1721 if (!MostPopularDest)
1750 if (PredBr->isUnconditional()) {
1751 PredBBs[0] = PredBB;
1775 if (!isa<PHINode>(BB->
front()))
1812 "computeValueKnownInPredecessors returned true with no values");
1816 unsigned NumTrue = 0, NumFalse = 0;
1817 for (
const auto &XorOpValue : XorOpValues) {
1818 if (isa<UndefValue>(XorOpValue.first))
1821 if (cast<ConstantInt>(XorOpValue.first)->isZero())
1829 if (NumTrue > NumFalse)
1831 else if (NumTrue != 0 || NumFalse != 0)
1837 for (
const auto &XorOpValue : XorOpValues) {
1838 if (XorOpValue.first != SplitVal && !isa<UndefValue>(XorOpValue.first))
1841 BlocksToFoldInto.
push_back(XorOpValue.second);
1846 if (BlocksToFoldInto.
size() ==
1847 cast<PHINode>(BB->
front()).getNumIncomingValues()) {
1885 Value *
IV = PN.getIncomingValueForBlock(OldPred);
1894 PN.addIncoming(
IV, NewPred);
1910 if (LoopHeaders.erase(SinglePred))
1911 LoopHeaders.insert(BB);
1964 for (
Use &U :
I.uses()) {
1967 if (UserPN->getIncomingBlock(U) == BB)
1969 }
else if (
User->getParent() == BB)
1984 if (UsesToRename.
empty() && DbgValues.
empty())
1986 LLVM_DEBUG(
dbgs() <<
"JT: Renaming non-local uses of: " <<
I <<
"\n");
1995 while (!UsesToRename.
empty())
1997 if (!DbgValues.
empty()) {
2019 auto RetargetDbgValueIfPossible = [&](
Instruction *NewInst) ->
bool {
2020 auto DbgInstruction = dyn_cast<DbgValueInst>(NewInst);
2021 if (!DbgInstruction)
2025 for (
auto DbgOperand : DbgInstruction->location_ops()) {
2026 auto DbgOperandInstruction = dyn_cast<Instruction>(DbgOperand);
2027 if (!DbgOperandInstruction)
2030 auto I = ValueMapping.
find(DbgOperandInstruction);
2031 if (
I != ValueMapping.
end()) {
2033 std::pair<Value *, Value *>(DbgOperand,
I->second));
2037 for (
auto &[OldOp, MappedOp] : OperandsToRemap)
2038 DbgInstruction->replaceVariableLocationOp(OldOp, MappedOp);
2045 for (;
PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2048 ValueMapping[PN] = NewPN;
2063 for (; BI != BE; ++BI) {
2065 New->setName(BI->getName());
2066 New->insertInto(NewBB, NewBB->
end());
2067 ValueMapping[&*BI] = New;
2070 if (RetargetDbgValueIfPossible(New))
2074 for (
unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2075 if (
Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2077 if (
I != ValueMapping.
end())
2078 New->setOperand(i,
I->second);
2082 return ValueMapping;
2139 if (LoopHeaders.count(PredBB))
2149 unsigned ZeroCount = 0;
2150 unsigned OneCount = 0;
2155 if (isa<IndirectBrInst>(
P->getTerminator()))
2157 if (
ConstantInt *CI = dyn_cast_or_null<ConstantInt>(
2162 }
else if (CI->isOne()) {
2171 if (ZeroCount == 1) {
2172 PredPredBB = ZeroPred;
2173 }
else if (OneCount == 1) {
2174 PredPredBB = OnePred;
2184 <<
"' - would thread to self!\n");
2190 if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
2192 bool BBIsHeader = LoopHeaders.count(BB);
2193 bool SuccIsHeader = LoopHeaders.count(SuccBB);
2194 dbgs() <<
" Not threading across "
2195 << (BBIsHeader ?
"loop header BB '" :
"block BB '")
2196 << BB->
getName() <<
"' to dest "
2197 << (SuccIsHeader ?
"loop header BB '" :
"block BB '")
2199 <<
"' - it might create an irreducible loop!\n";
2213 if (BBCost > BBDupThreshold || PredBBCost > BBDupThreshold ||
2214 BBCost + PredBBCost > BBDupThreshold) {
2216 <<
"' - Cost is too high: " << PredBBCost
2217 <<
" for PredBB, " << BBCost <<
"for BB\n");
2234 bool HasProfile = doesBlockHaveProfileData(BB);
2235 auto *BFI = getOrCreateBFI(HasProfile);
2236 auto *BPI = getOrCreateBPI(BFI !=
nullptr);
2248 assert(BPI &&
"It's expected BPI to exist along with BFI");
2249 auto NewBBFreq = BFI->getBlockFreq(PredPredBB) *
2250 BPI->getEdgeProbability(PredPredBB, PredBB);
2251 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2262 BPI->copyEdgeProbabilities(PredBB, NewBB);
2279 DTU->applyUpdatesPermissive(
2304 <<
"' - would thread to self!\n");
2310 if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
2312 bool BBIsHeader = LoopHeaders.count(BB);
2313 bool SuccIsHeader = LoopHeaders.count(SuccBB);
2314 dbgs() <<
" Not threading across "
2315 << (BBIsHeader ?
"loop header BB '" :
"block BB '") << BB->
getName()
2316 <<
"' to dest " << (SuccIsHeader ?
"loop header BB '" :
"block BB '")
2317 << SuccBB->
getName() <<
"' - it might create an irreducible loop!\n";
2324 if (JumpThreadCost > BBDupThreshold) {
2326 <<
"' - Cost is too high: " << JumpThreadCost <<
"\n");
2340 assert(SuccBB != BB &&
"Don't create an infinite loop");
2342 assert(!LoopHeaders.count(BB) && !LoopHeaders.count(SuccBB) &&
2343 "Don't thread across loop headers");
2346 bool HasProfile = doesBlockHaveProfileData(BB);
2347 auto *BFI = getOrCreateBFI(HasProfile);
2348 auto *BPI = getOrCreateBPI(BFI !=
nullptr);
2352 if (PredBBs.
size() == 1)
2353 PredBB = PredBBs[0];
2356 <<
" common predecessors.\n");
2357 PredBB = splitBlockPreds(BB, PredBBs,
".thr_comm");
2362 <<
"' to '" << SuccBB->
getName()
2363 <<
", across block:\n " << *BB <<
"\n");
2374 assert(BPI &&
"It's expected BPI to exist along with BFI");
2376 BFI->getBlockFreq(PredBB) * BPI->getEdgeProbability(PredBB, BB);
2377 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2416 updateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB, BFI, BPI, HasProfile);
2427 const char *Suffix) {
2433 auto *BFI = getBFI();
2435 auto *BPI = getOrCreateBPI(
true);
2436 for (
auto *Pred : Preds)
2437 FreqMap.
insert(std::make_pair(
2438 Pred, BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB)));
2444 std::string NewName = std::string(Suffix) +
".split-lp";
2450 std::vector<DominatorTree::UpdateType> Updates;
2451 Updates.reserve((2 * Preds.size()) + NewBBs.
size());
2452 for (
auto *NewBB : NewBBs) {
2459 NewBBFreq += FreqMap.
lookup(Pred);
2462 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2465 DTU->applyUpdatesPermissive(Updates);
2469bool JumpThreadingPass::doesBlockHaveProfileData(
BasicBlock *BB) {
2480void JumpThreadingPass::updateBlockFreqAndEdgeWeight(
BasicBlock *PredBB,
2487 assert(((BFI && BPI) || (!BFI && !BFI)) &&
2488 "Both BFI & BPI should either be set or unset");
2492 "It's expected to have BFI/BPI when profile info exists");
2498 auto BBOrigFreq =
BFI->getBlockFreq(BB);
2499 auto NewBBFreq =
BFI->getBlockFreq(NewBB);
2501 auto BBNewFreq = BBOrigFreq - NewBBFreq;
2502 BFI->setBlockFreq(BB, BBNewFreq.getFrequency());
2508 auto SuccFreq = (Succ == SuccBB)
2509 ? BB2SuccBBFreq - NewBBFreq
2511 BBSuccFreq.
push_back(SuccFreq.getFrequency());
2515 *std::max_element(BBSuccFreq.
begin(), BBSuccFreq.
end());
2518 if (MaxBBSuccFreq == 0)
2520 {1, static_cast<unsigned>(BBSuccFreq.size())});
2567 if (BBSuccProbs.
size() >= 2 && HasProfile) {
2569 for (
auto Prob : BBSuccProbs)
2574 LLVMContext::MD_prof,
2586 assert(!PredBBs.
empty() &&
"Can't handle an empty set");
2591 if (LoopHeaders.count(BB)) {
2593 <<
"' into predecessor block '" << PredBBs[0]->getName()
2594 <<
"' - it might create an irreducible loop!\n");
2600 if (DuplicationCost > BBDupThreshold) {
2602 <<
"' - Cost is too high: " << DuplicationCost <<
"\n");
2607 std::vector<DominatorTree::UpdateType> Updates;
2609 if (PredBBs.
size() == 1)
2610 PredBB = PredBBs[0];
2613 <<
" common predecessors.\n");
2614 PredBB = splitBlockPreds(BB, PredBBs,
".thr_comm");
2621 <<
"' into end of '" << PredBB->
getName()
2622 <<
"' to eliminate branch on phi. Cost: "
2623 << DuplicationCost <<
" block is:" << *BB <<
"\n");
2643 for (;
PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
2647 for (; BI != BB->
end(); ++BI) {
2651 for (
unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2652 if (
Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2654 if (
I != ValueMapping.
end())
2655 New->setOperand(i,
I->second);
2663 {BB->
getModule()->getDataLayout(), TLI,
nullptr,
nullptr, New})) {
2664 ValueMapping[&*BI] =
IV;
2665 if (!New->mayHaveSideEffects()) {
2670 ValueMapping[&*BI] = New;
2674 New->setName(BI->getName());
2675 New->insertInto(PredBB, OldPredBranch->
getIterator());
2677 for (
unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2678 if (
BasicBlock *SuccBB = dyn_cast<BasicBlock>(New->getOperand(i)))
2691 updateSSA(BB, PredBB, ValueMapping);
2698 OldPredBranch->eraseFromParent();
2699 if (
auto *BPI = getBPI())
2701 DTU->applyUpdatesPermissive(Updates);
2732 BI->applyMergedLocation(PredTerm->
getDebugLoc(),
SI->getDebugLoc());
2733 BI->copyMetadata(*
SI, {LLVMContext::MD_prof});
2741 (TrueWeight + FalseWeight) != 0) {
2744 TrueWeight, TrueWeight + FalseWeight));
2746 FalseWeight, TrueWeight + FalseWeight));
2748 if (
auto *BPI = getBPI())
2752 if (
auto *BFI = getBFI()) {
2753 if ((TrueWeight + FalseWeight) == 0) {
2758 TrueWeight, TrueWeight + FalseWeight);
2759 auto NewBBFreq = BFI->getBlockFreq(Pred) * PredToNewBBProb;
2760 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
2764 SI->eraseFromParent();
2770 PHINode *Phi = dyn_cast<PHINode>(BI); ++BI)
2772 Phi->addIncoming(Phi->getIncomingValueForBlock(Pred), NewBB);
2776 PHINode *CondPHI = dyn_cast<PHINode>(
SI->getCondition());
2778 if (!CondPHI || CondPHI->
getParent() != BB)
2828 if (!
SI ||
SI->getParent() != Pred || !
SI->hasOneUse())
2840 CondRHS, Pred, BB, CondCmp);
2843 CondRHS, Pred, BB, CondCmp);
2846 LHSFolds != RHSFolds) {
2882 if (LoopHeaders.count(BB))
2886 PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2889 [](
Value *V) { return !isa<ConstantInt>(V); }))
2893 using namespace PatternMatch;
2896 if (
SI->getParent() != BB)
2900 return Cond &&
Cond == V &&
Cond->getType()->isIntegerTy(1) && !IsAndOr;
2904 for (
Use &U : PN->uses()) {
2905 if (
ICmpInst *Cmp = dyn_cast<ICmpInst>(U.getUser())) {
2908 if (Cmp->getParent() == BB && Cmp->hasOneUse() &&
2909 isa<ConstantInt>(Cmp->getOperand(1 - U.getOperandNo())))
2910 if (
SelectInst *SelectI = dyn_cast<SelectInst>(Cmp->user_back()))
2911 if (isUnfoldCandidate(SelectI, Cmp->use_begin()->get())) {
2915 }
else if (
SelectInst *SelectI = dyn_cast<SelectInst>(U.getUser())) {
2917 if (isUnfoldCandidate(SelectI, U.get())) {
2936 SI->replaceAllUsesWith(NewPN);
2937 SI->eraseFromParent();
2939 std::vector<DominatorTree::UpdateType> Updates;
2949 DTU->applyUpdatesPermissive(Updates);
2975 using namespace PatternMatch;
2997 if (
auto *BI = dyn_cast<BranchInst>(Parent->getTerminator()))
3018 bool TrueDestIsSafe =
false;
3019 bool FalseDestIsSafe =
false;
3024 TrueDestIsSafe =
true;
3029 FalseDestIsSafe =
true;
3032 if (!TrueDestIsSafe && !FalseDestIsSafe)
3035 BasicBlock *PredUnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
3036 BasicBlock *PredGuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
3042 if (
Cost > BBDupThreshold)
3047 BB, PredGuardedBlock, AfterGuard, GuardedMapping, *DTU);
3048 assert(GuardedBlock &&
"Could not create the guarded block?");
3053 BB, PredUnguardedBlock, Guard, UnguardedMapping, *DTU);
3054 assert(UnguardedBlock &&
"Could not create the unguarded block?");
3056 << GuardedBlock->
getName() <<
"\n");
3061 for (
auto BI = BB->
begin(); &*BI != AfterGuard; ++BI)
3062 if (!isa<PHINode>(&*BI))
3066 assert(InsertionPoint &&
"Empty block?");
3069 if (!Inst->use_empty()) {
3071 NewPN->
addIncoming(UnguardedMapping[Inst], UnguardedBlock);
3072 NewPN->
addIncoming(GuardedMapping[Inst], GuardedBlock);
3074 Inst->replaceAllUsesWith(NewPN);
3076 Inst->eraseFromParent();
3091template <
typename AnalysisT>
3092typename AnalysisT::Result *JumpThreadingPass::runExternalAnalysis() {
3093 assert(FAM &&
"Can't run external analysis without FunctionAnalysisManager");
3098 if (!ChangedSinceLastAnalysisUpdate) {
3099 assert(!DTU->hasPendingUpdates() &&
3100 "Lost update of 'ChangedSinceLastAnalysisUpdate'?");
3104 ChangedSinceLastAnalysisUpdate =
false;
3106 auto PA = getPreservedAnalysis();
3116 assert(DTU->getDomTree().verify(DominatorTree::VerificationLevel::Fast));
3117 assert((!DTU->hasPostDomTree() ||
3118 DTU->getPostDomTree().verify(
3132 assert(FAM &&
"Can't create BPI without FunctionAnalysisManager");
3140 assert(FAM &&
"Can't create BFI without FunctionAnalysisManager");
3150 auto *Res = getBPI();
3155 BPI = runExternalAnalysis<BranchProbabilityAnalysis>();
3161 auto *Res = getBFI();
3166 BFI = runExternalAnalysis<BlockFrequencyAnalysis>();
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
ReachingDefAnalysis InstSet & ToRemove
static const Function * getParent(const Value *V)
SmallVector< MachineOperand, 4 > Cond
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file defines the DenseMap class.
This file defines the DenseSet and SmallDenseSet classes.
This is the interface for a simple mod/ref and alias analysis over globals.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
static unsigned getBestDestForJumpOnUndef(BasicBlock *BB)
GetBestDestForBranchOnUndef - If we determine that the specified block ends in an undefined jump,...
static cl::opt< unsigned > PhiDuplicateThreshold("jump-threading-phi-threshold", cl::desc("Max PHIs in BB to duplicate for jump threading"), cl::init(76), cl::Hidden)
static bool replaceFoldableUses(Instruction *Cond, Value *ToVal, BasicBlock *KnownAtEndOfBB)
static cl::opt< unsigned > BBDuplicateThreshold("jump-threading-threshold", cl::desc("Max block size to duplicate for jump threading"), cl::init(6), cl::Hidden)
static cl::opt< bool > ThreadAcrossLoopHeaders("jump-threading-across-loop-headers", cl::desc("Allow JumpThreading to thread across loop headers, for testing"), cl::init(false), cl::Hidden)
static unsigned getJumpThreadDuplicationCost(const TargetTransformInfo *TTI, BasicBlock *BB, Instruction *StopAt, unsigned Threshold)
Return the cost of duplicating a piece of this block from first non-phi and before StopAt instruction...
static BasicBlock * findMostPopularDest(BasicBlock *BB, const SmallVectorImpl< std::pair< BasicBlock *, BasicBlock * > > &PredToDestList)
findMostPopularDest - The specified list contains multiple possible threadable destinations.
static Constant * getKnownConstant(Value *Val, ConstantPreference Preference)
getKnownConstant - Helper method to determine if we can thread over a terminator with the given value...
static cl::opt< bool > PrintLVIAfterJumpThreading("print-lvi-after-jump-threading", cl::desc("Print the LazyValueInfo cache after JumpThreading"), cl::init(false), cl::Hidden)
static cl::opt< unsigned > ImplicationSearchThreshold("jump-threading-implication-search-threshold", cl::desc("The number of predecessors to search for a stronger " "condition to use to thread over a weaker condition"), cl::init(3), cl::Hidden)
static void addPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, BasicBlock *OldPred, BasicBlock *NewPred, DenseMap< Instruction *, Value * > &ValueMap)
addPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new predecessor to the PHIBB block.
static bool isOpDefinedInBlock(Value *Op, BasicBlock *BB)
Return true if Op is an instruction defined in the given block.
static void updatePredecessorProfileMetadata(PHINode *PN, BasicBlock *BB)
static bool hasAddressTakenAndUsed(BasicBlock *BB)
See the comments on JumpThreadingPass.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
This file implements a map that provides insertion order iteration.
This file provides utility analysis objects describing memory locations.
Module.h This file contains the declarations for the Module class.
PassBuilder PB(Machine, PassOpts->PTO, std::nullopt, &PIC)
This header defines various interfaces for pass management in LLVM.
This file contains the declarations for profiling metadata utility functions.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
This defines the Use class.
static const uint32_t IV[8]
A manager for alias analyses.
A container for analyses that lazily runs them and caches their results.
void invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Invalidate cached analyses for an IR unit.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
iterator begin()
Instruction iterator methods.
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches,...
InstListType::const_iterator const_iterator
const Instruction & front() const
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
bool hasNPredecessors(unsigned N) const
Return true if this block has exactly N predecessors.
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
const Function * getParent() const
Return the enclosing method, or null if none.
InstListType::iterator iterator
Instruction iterators...
LLVMContext & getContext() const
Get the context in which this basic block lives.
bool isLandingPad() const
Return true if this basic block is a landing pad.
bool isEHPad() const
Return true if this basic block is an exception handling block.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
The address of a basic block.
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
Analysis pass which computes BlockFrequencyInfo.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Conditional or Unconditional Branch instruction.
bool isConditional() const
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
unsigned getNumSuccessors() const
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
Value * getCondition() const
Analysis pass which computes BranchProbabilityInfo.
Analysis providing branch probability information.
void setEdgeProbability(const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs)
Set the raw probabilities for all edges from the given block.
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst)
Copy outgoing edge probabilities from Src to Dst.
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
uint32_t getNumerator() const
BranchProbability getCompl() const
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
Value * getArgOperand(unsigned i) const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
This class is the base class for the comparison instructions.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Predicate getPredicate() const
Return the predicate for this instruction.
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
static Constant * getNot(Constant *C)
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
This is the shared class of boolean and integer constants.
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
static ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static ConstantInt * getFalse(LLVMContext &Context)
const APInt & getValue() const
Return the constant as an APInt value reference.
static ConstantInt * getBool(LLVMContext &Context, bool V)
This class represents a range of values.
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantRange inverse() const
Return a new range that is the logical not of the current set.
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
This is an important base class in LLVM.
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
A parsed version of the target data layout string in and methods for querying it.
This represents the llvm.dbg.value instruction.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Implements a dense probed hash-table based set.
void flush()
Apply all pending updates to available trees and flush all BasicBlocks awaiting deletion.
Analysis pass which computes a DominatorTree.
static constexpr UpdateKind Delete
static constexpr UpdateKind Insert
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
This class represents a freeze function that returns random concrete value if an operand is either a ...
const BasicBlock & getEntryBlock() const
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Module * getParent()
Get the module that this global value is contained inside of...
This instruction compares its operands according to the predicate given to the constructor.
Indirect Branch Instruction.
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
void insertBefore(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified instruction.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
const BasicBlock * getParent() const
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
bool isExceptionalTerminator() const
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
SymbolTableList< Instruction >::iterator insertInto(BasicBlock *ParentBB, SymbolTableList< Instruction >::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
A wrapper class for inspecting calls to intrinsic functions.
bool simplifyPartiallyRedundantLoad(LoadInst *LI)
simplifyPartiallyRedundantLoad - If LoadI is an obviously partially redundant load instruction,...
bool processBranchOnXOR(BinaryOperator *BO)
processBranchOnXOR - We have an otherwise unthreadable conditional branch on a xor instruction in the...
bool processGuards(BasicBlock *BB)
Try to propagate a guard from the current BB into one of its predecessors in case if another branch o...
DenseMap< Instruction *, Value * > cloneInstructions(BasicBlock::iterator BI, BasicBlock::iterator BE, BasicBlock *NewBB, BasicBlock *PredBB)
Clone instructions in range [BI, BE) to NewBB.
bool computeValueKnownInPredecessors(Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
void findLoopHeaders(Function &F)
findLoopHeaders - We do not want jump threading to turn proper loop structures into irreducible loops...
bool maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB)
Merge basic block BB into its sole predecessor if possible.
JumpThreadingPass(int T=-1)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
bool runImpl(Function &F, FunctionAnalysisManager *FAM, TargetLibraryInfo *TLI, TargetTransformInfo *TTI, LazyValueInfo *LVI, AAResults *AA, std::unique_ptr< DomTreeUpdater > DTU, std::optional< BlockFrequencyInfo * > BFI, std::optional< BranchProbabilityInfo * > BPI)
bool processBranchOnPHI(PHINode *PN)
processBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node (or freeze PH...
bool maybethreadThroughTwoBasicBlocks(BasicBlock *BB, Value *Cond)
Attempt to thread through two successive basic blocks.
void unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB, SelectInst *SI, PHINode *SIUse, unsigned Idx)
DomTreeUpdater * getDomTreeUpdater() const
Constant * evaluateOnPredecessorEdge(BasicBlock *BB, BasicBlock *PredPredBB, Value *cond)
bool processThreadableEdges(Value *Cond, BasicBlock *BB, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
bool computeValueKnownInPredecessorsImpl(Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, DenseSet< Value * > &RecursionSet, Instruction *CxtI=nullptr)
computeValueKnownInPredecessors - Given a basic block BB and a value V, see if we can infer that the ...
bool processBlock(BasicBlock *BB)
processBlock - If there are any predecessors whose control can be threaded through to a successor,...
bool processImpliedCondition(BasicBlock *BB)
bool duplicateCondBranchOnPHIIntoPred(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs)
duplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch to BB which contains an i1...
void updateSSA(BasicBlock *BB, BasicBlock *NewBB, DenseMap< Instruction *, Value * > &ValueMapping)
Update the SSA form.
void threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, BasicBlock *PredBB, BasicBlock *BB, BasicBlock *SuccBB)
bool tryThreadEdge(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs, BasicBlock *SuccBB)
tryThreadEdge - Thread an edge if it's safe and profitable to do so.
bool tryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB)
tryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb2
bool tryToUnfoldSelectInCurrBB(BasicBlock *BB)
tryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the same BB in the form bb: p = ...
void threadEdge(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs, BasicBlock *SuccBB)
threadEdge - We have decided that it is safe and profitable to factor the blocks in PredBBs to one pr...
bool threadGuard(BasicBlock *BB, IntrinsicInst *Guard, BranchInst *BI)
Try to propagate the guard from BB which is the lower block of a diamond to one of its branches,...
This is an important class for using LLVM in a threaded context.
Analysis to compute lazy value information.
This pass computes, caches, and vends lazy value constraint information.
void eraseBlock(BasicBlock *BB)
Inform the analysis cache that we have erased a block.
void threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc, BasicBlock *NewSucc)
Inform the analysis cache that we have threaded an edge from PredBB to OldSucc to be from PredBB to N...
Tristate
This is used to return true/false/dunno results.
Constant * getConstantOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value is known to be a constant on the specified edge.
ConstantRange getConstantRangeOnEdge(Value *V, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Return the ConstantRage constraint that is known to hold for the specified value on the specified edg...
Tristate getPredicateOnEdge(unsigned Pred, Value *V, Constant *C, BasicBlock *FromBB, BasicBlock *ToBB, Instruction *CxtI=nullptr)
Determine whether the specified value comparison with a constant is known to be true or false on the ...
Tristate getPredicateAt(unsigned Pred, Value *V, Constant *C, Instruction *CxtI, bool UseBlockValue)
Determine whether the specified value comparison with a constant is known to be true or false at the ...
Constant * getConstant(Value *V, Instruction *CxtI)
Determine whether the specified value is known to be a constant at the specified instruction.
An instruction for reading from memory.
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this load instruction.
Align getAlign() const
Return the alignment of the access that is being performed.
static LocationSize precise(uint64_t Value)
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight)
Return metadata containing two branch weights.
This class implements a map that also provides access to all stored values in a deterministic order.
Representation for a specific memory location.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingValue(unsigned i, Value *V)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
void preserve()
Mark an analysis as preserved.
Helper class for SSA formation on a set of values defined in multiple blocks.
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
void UpdateDebugValues(Instruction *I)
Rewrite debug value intrinsics to conform to a new SSA form.
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
This class represents the LLVM 'select' instruction.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
iterator erase(const_iterator CI)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Analysis pass providing the TargetTransformInfo.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
bool isVectorTy() const
True if this is an instance of VectorType.
static IntegerType * getInt1Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
'undef' values are things that do not have specified contents.
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
void setOperand(unsigned i, Value *Val)
Value * getOperand(unsigned i) const
iterator find(const KeyT &Val)
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
const Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) const
Translate PHI node to its predecessor from the given basic block.
bool hasOneUse() const
Return true if there is exactly one use of this value.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
LLVMContext & getContext() const
All values hold a context through their type.
StringRef getName() const
Return a constant reference to the value's name.
void takeName(Value *V)
Transfer the name from V to this value.
std::pair< iterator, bool > insert(const ValueT &V)
self_iterator getIterator()
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
@ C
The default llvm calling convention, compatible with C.
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
bool match(Val *V, const Pattern &P)
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
bool RemoveRedundantDbgInstrs(BasicBlock *BB)
Try to remove redundant dbg.value instructions from given basic block.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr, DomTreeUpdater *DTU=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
unsigned replaceNonLocalUsesWith(Instruction *From, Value *To)
auto successors(const MachineBasicBlock *BB)
void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method transforms the landing pad, OrigBB, by introducing two new basic blocks into the function...
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AAResults *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
bool SimplifyInstructionsInBlock(BasicBlock *BB, const TargetLibraryInfo *TLI=nullptr)
Scan the specified basic block and try to simplify any instructions in it and recursively delete dead...
void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
BasicBlock * DuplicateInstructionsInSplitBetween(BasicBlock *BB, BasicBlock *PredBB, Instruction *StopAt, ValueToValueMapTy &ValueMapping, DomTreeUpdater &DTU)
Split edge between BB and PredBB and duplicate all non-Phi instructions from BB between its beginning...
Value * findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, AAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst)
Scan backwards to see if we have the value of the given pointer available locally within a small numb...
Interval::pred_iterator pred_end(Interval *I)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
BB is known to contain an unconditional branch, and contains no instructions other than PHI nodes,...
auto reverse(ContainerTy &&C)
bool hasValidBranchWeightMD(const Instruction &I)
Checks if an instructions has valid Branch Weight Metadata.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
void findDbgValues(SmallVectorImpl< DbgValueInst * > &DbgValues, Value *V)
Finds the llvm.dbg.value intrinsics describing a value.
void cloneNoAliasScopes(ArrayRef< MDNode * > NoAliasDeclScopes, DenseMap< MDNode *, MDNode * > &ClonedScopes, StringRef Ext, LLVMContext &Context)
Duplicate the specified list of noalias decl scopes.
cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
void combineMetadataForCSE(Instruction *K, const Instruction *J, bool DoesKMove)
Combine the metadata of two instructions so that K can replace J.
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
auto remove_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly.
void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
BB is a block with one predecessor and its predecessor is known to have one successor (BB!...
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
Value * simplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
void adaptNoAliasScopes(llvm::Instruction *I, const DenseMap< MDNode *, MDNode * > &ClonedScopes, LLVMContext &Context)
Adapt the metadata for the specified instruction according to the provided mapping.
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
bool pred_empty(const BasicBlock *BB)
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
void identifyNoAliasScopesToClone(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< MDNode * > &NoAliasDeclScopes)
Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified basic blocks and extract ...
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, MemorySSAUpdater *MSSAU=nullptr, const Twine &BBName="")
Split the edge connecting the specified blocks, and return the newly created basic block between From...
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights, DominatorTree *DT, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
unsigned pred_size(const MachineBasicBlock *BB)
void FindFunctionBackedges(const Function &F, SmallVectorImpl< std::pair< const BasicBlock *, const BasicBlock * > > &Result)
Analyze the specified function to find all of the loop backedges in the function and return them.
std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Function object to check whether the second component of a container supported by std::get (like std:...