79using namespace jumpthreading;
81#define DEBUG_TYPE "jump-threading"
85STATISTIC(NumDupes,
"Number of branch blocks duplicated to eliminate phi");
89 cl::desc(
"Max block size to duplicate for jump threading"),
94 "jump-threading-implication-search-threshold",
95 cl::desc(
"The number of predecessors to search for a stronger "
96 "condition to use to thread over a weaker condition"),
100 "jump-threading-phi-threshold",
105 "jump-threading-across-loop-headers",
106 cl::desc(
"Allow JumpThreading to thread across loop headers, for testing"),
157 if (TrueWeight + FalseWeight == 0)
165 auto GetPredOutEdge =
167 BasicBlock *PhiBB) -> std::pair<BasicBlock *, BasicBlock *> {
168 auto *PredBB = IncomingBB;
169 auto *SuccBB = PhiBB;
172 BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
174 return {PredBB, SuccBB};
176 auto *SinglePredBB = PredBB->getSinglePredecessor();
178 return {
nullptr,
nullptr};
182 if (Visited.
count(SinglePredBB))
183 return {
nullptr,
nullptr};
186 PredBB = SinglePredBB;
199 TrueWeight, TrueWeight + FalseWeight)
201 FalseWeight, TrueWeight + FalseWeight));
204 if (!PredOutEdge.first)
212 uint64_t PredTrueWeight, PredFalseWeight;
250 std::make_unique<DomTreeUpdater>(
251 &DT,
nullptr, DomTreeUpdater::UpdateStrategy::Lazy),
260#if defined(EXPENSIVE_CHECKS)
262 DominatorTree::VerificationLevel::Full) &&
263 "DT broken after JumpThreading");
267 "PDT broken after JumpThreading");
270 DominatorTree::VerificationLevel::Fast) &&
271 "DT broken after JumpThreading");
275 "PDT broken after JumpThreading");
278 return getPreservedAnalysis();
285 std::unique_ptr<DomTreeUpdater> DTU_,
295 DTU = std::move(DTU_);
299 F->
getParent(), Intrinsic::experimental_guard);
300 HasGuards = GuardDecl && !GuardDecl->use_empty();
309 BBDupThreshold = DefaultBBDupThreshold;
311 assert(DTU &&
"DTU isn't passed into JumpThreading before using it.");
312 assert(DTU->hasDomTree() &&
"JumpThreading relies on DomTree to proceed.");
318 Unreachable.insert(&BB);
323 bool EverChanged =
false;
327 for (
auto &BB : *
F) {
328 if (Unreachable.count(&BB))
331 Changed = ChangedSinceLastAnalysisUpdate =
true;
336 if (&BB == &
F->getEntryBlock() || DTU->isBBPendingDeletion(&BB))
343 <<
"' with terminator: " << *BB.getTerminator()
345 LoopHeaders.erase(&BB);
348 Changed = ChangedSinceLastAnalysisUpdate =
true;
354 auto *BI = dyn_cast<BranchInst>(BB.getTerminator());
355 if (BI && BI->isUnconditional()) {
359 BB.getFirstNonPHIOrDbg(
true)->isTerminator() &&
362 !LoopHeaders.count(&BB) && !LoopHeaders.count(Succ) &&
367 Changed = ChangedSinceLastAnalysisUpdate =
true;
371 EverChanged |= Changed;
377 for (
auto &BB : *
F) {
394 bool Changed =
false;
399 if (
Cond->getParent() == KnownAtEndOfBB)
404 DVR.replaceVariableLocationOp(
Cond, ToVal,
true);
414 Changed |=
I.replaceUsesOfWith(
Cond, ToVal);
416 if (
Cond->use_empty() && !
Cond->mayHaveSideEffects()) {
417 Cond->eraseFromParent();
429 unsigned Threshold) {
430 assert(
StopAt->getParent() == BB &&
"Not an instruction from proper BB?");
435 unsigned PhiCount = 0;
438 if (!isa<PHINode>(&
I)) {
457 if (isa<SwitchInst>(
StopAt))
461 if (isa<IndirectBrInst>(
StopAt))
475 if (
Size > Threshold)
480 if (
I->getType()->isTokenTy() &&
I->isUsedOutsideOfBlock(BB))
485 if (
const CallInst *CI = dyn_cast<CallInst>(
I))
486 if (CI->cannotDuplicate() || CI->isConvergent())
500 if (
const CallInst *CI = dyn_cast<CallInst>(
I)) {
501 if (!isa<IntrinsicInst>(CI))
503 else if (!CI->getType()->isVectorTy())
508 return Size > Bonus ?
Size - Bonus : 0;
541 if (
UndefValue *U = dyn_cast<UndefValue>(Val))
547 return dyn_cast<ConstantInt>(Val);
566 if (!RecursionSet.
insert(V).second)
572 Result.emplace_back(KC, Pred);
574 return !Result.empty();
580 if (!
I ||
I->getParent() != BB) {
585 using namespace PatternMatch;
599 Result.emplace_back(KC,
P);
602 return !Result.empty();
606 if (
PHINode *PN = dyn_cast<PHINode>(
I)) {
607 for (
unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
608 Value *InVal = PN->getIncomingValue(i);
610 Result.emplace_back(KC, PN->getIncomingBlock(i));
613 PN->getIncomingBlock(i),
616 Result.emplace_back(KC, PN->getIncomingBlock(i));
620 return !Result.empty();
624 if (
CastInst *CI = dyn_cast<CastInst>(
I)) {
625 Value *Source = CI->getOperand(0);
633 for (
auto &Val : Vals)
636 Result.emplace_back(Folded, Val.second);
638 return !Result.empty();
642 Value *Source = FI->getOperand(0);
650 return !Result.empty();
654 if (
I->getType()->getPrimitiveSizeInBits() == 1) {
655 using namespace PatternMatch;
683 for (
const auto &LHSVal : LHSVals)
684 if (LHSVal.first == InterestingVal || isa<UndefValue>(LHSVal.first)) {
685 Result.emplace_back(InterestingVal, LHSVal.second);
686 LHSKnownBBs.
insert(LHSVal.second);
688 for (
const auto &RHSVal : RHSVals)
689 if (RHSVal.first == InterestingVal || isa<UndefValue>(RHSVal.first)) {
692 if (!LHSKnownBBs.
count(RHSVal.second))
693 Result.emplace_back(InterestingVal, RHSVal.second);
696 return !Result.empty();
700 if (
I->getOpcode() == Instruction::Xor &&
701 isa<ConstantInt>(
I->getOperand(1)) &&
702 cast<ConstantInt>(
I->getOperand(1))->isOne()) {
709 for (
auto &R : Result)
719 if (
ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
725 for (
const auto &LHSVal : LHSVals) {
731 Result.emplace_back(KC, LHSVal.second);
735 return !Result.empty();
739 if (
CmpInst *Cmp = dyn_cast<CmpInst>(
I)) {
742 Type *CmpType = Cmp->getType();
743 Value *CmpLHS = Cmp->getOperand(0);
744 Value *CmpRHS = Cmp->getOperand(1);
747 PHINode *PN = dyn_cast<PHINode>(CmpLHS);
749 PN = dyn_cast<PHINode>(CmpRHS);
753 if (PN && PN->
getParent() == BB && !LoopHeaders.contains(BB)) {
769 if (!isa<Constant>(
RHS))
773 auto LHSInst = dyn_cast<Instruction>(
LHS);
774 if (LHSInst && LHSInst->getParent() == BB)
778 BB, CxtI ? CxtI : Cmp);
782 Result.emplace_back(KC, PredBB);
785 return !Result.empty();
790 if (isa<Constant>(CmpRHS) && !CmpType->
isVectorTy()) {
791 Constant *CmpConst = cast<Constant>(CmpRHS);
793 if (!isa<Instruction>(CmpLHS) ||
794 cast<Instruction>(CmpLHS)->
getParent() != BB) {
801 Result.emplace_back(KC,
P);
804 return !Result.empty();
811 using namespace PatternMatch;
815 if (isa<ConstantInt>(CmpConst) &&
817 if (!isa<Instruction>(AddLHS) ||
818 cast<Instruction>(AddLHS)->
getParent() != BB) {
824 AddLHS,
P, BB, CxtI ? CxtI : cast<Instruction>(CmpLHS));
830 Pred, cast<ConstantInt>(CmpConst)->getValue());
840 Result.emplace_back(ResC,
P);
843 return !Result.empty();
854 for (
const auto &LHSVal : LHSVals) {
859 Result.emplace_back(KC, LHSVal.second);
862 return !Result.empty();
872 if ((TrueVal || FalseVal) &&
875 for (
auto &
C : Conds) {
882 KnownCond = CI->isOne();
884 assert(isa<UndefValue>(
Cond) &&
"Unexpected condition value");
888 KnownCond = (TrueVal !=
nullptr);
892 if (
Constant *Val = KnownCond ? TrueVal : FalseVal)
893 Result.emplace_back(Val,
C.second);
896 return !Result.empty();
905 Result.emplace_back(KC, Pred);
908 return !Result.empty();
918 unsigned MinSucc = 0;
921 unsigned MinNumPreds =
pred_size(TestBB);
925 if (NumPreds < MinNumPreds) {
927 MinNumPreds = NumPreds;
949 if (DTU->isBBPendingDeletion(BB) ||
974 if (
BranchInst *BI = dyn_cast<BranchInst>(Terminator)) {
976 if (BI->isUnconditional())
return false;
977 Condition = BI->getCondition();
978 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(Terminator)) {
979 Condition = SI->getCondition();
980 }
else if (
IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) {
982 if (IB->getNumSuccessors() == 0)
return false;
983 Condition = IB->getAddress()->stripPointerCasts();
990 bool ConstantFolded =
false;
994 if (
Instruction *
I = dyn_cast<Instruction>(Condition)) {
998 I->replaceAllUsesWith(SimpleVal);
1000 I->eraseFromParent();
1001 Condition = SimpleVal;
1002 ConstantFolded =
true;
1008 auto *FI = dyn_cast<FreezeInst>(Condition);
1009 if (isa<UndefValue>(Condition) ||
1010 (FI && isa<UndefValue>(FI->getOperand(0)) && FI->hasOneUse())) {
1012 std::vector<DominatorTree::UpdateType> Updates;
1018 if (i == BestSucc)
continue;
1025 <<
"' folding undef terminator: " << *BBTerm <<
'\n');
1030 DTU->applyUpdatesPermissive(Updates);
1032 FI->eraseFromParent();
1045 if (
auto *BPI = getBPI())
1050 Instruction *CondInst = dyn_cast<Instruction>(Condition);
1057 return ConstantFolded;
1061 Value *CondWithoutFreeze = CondInst;
1062 if (
auto *FI = dyn_cast<FreezeInst>(CondInst))
1063 CondWithoutFreeze = FI->getOperand(0);
1065 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(CondWithoutFreeze)) {
1069 if (
Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1))) {
1071 LVI->
getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
1101 Value *SimplifyValue = CondWithoutFreeze;
1103 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue))
1104 if (isa<Constant>(CondCmp->getOperand(1)))
1105 SimplifyValue = CondCmp->getOperand(0);
1109 if (
LoadInst *LoadI = dyn_cast<LoadInst>(SimplifyValue))
1114 if (
PHINode *PN = dyn_cast<PHINode>(CondInst))
1115 if (PN->getParent() == BB && isa<BranchInst>(BB->
getTerminator()))
1126 PHINode *PN = dyn_cast<PHINode>(CondWithoutFreeze);
1131 if (CondInst->
getOpcode() == Instruction::Xor &&
1145 if (!BI || !BI->isConditional())
1154 auto *FICond = dyn_cast<FreezeInst>(
Cond);
1155 if (FICond && FICond->hasOneUse())
1156 Cond = FICond->getOperand(0);
1167 auto *PBI = dyn_cast<BranchInst>(CurrentPred->
getTerminator());
1168 if (!PBI || !PBI->isConditional())
1170 if (PBI->getSuccessor(0) != CurrentBB && PBI->getSuccessor(1) != CurrentBB)
1173 bool CondIsTrue = PBI->getSuccessor(0) == CurrentBB;
1174 std::optional<bool> Implication =
1179 if (!Implication && FICond && isa<FreezeInst>(PBI->getCondition())) {
1180 if (cast<FreezeInst>(PBI->getCondition())->getOperand(0) ==
1181 FICond->getOperand(0))
1182 Implication = CondIsTrue;
1186 BasicBlock *KeepSucc = BI->getSuccessor(*Implication ? 0 : 1);
1187 BasicBlock *RemoveSucc = BI->getSuccessor(*Implication ? 1 : 0);
1192 BI->eraseFromParent();
1194 FICond->eraseFromParent();
1197 if (
auto *BPI = getBPI())
1201 CurrentBB = CurrentPred;
1211 if (OpInst->getParent() == BB)
1256 LoadInst *NLoadI = cast<LoadInst>(AvailableVal);
1263 if (AvailableVal == LoadI)
1265 if (AvailableVal->getType() != LoadI->
getType()) {
1268 cast<Instruction>(AvailableVal)->setDebugLoc(LoadI->
getDebugLoc());
1278 if (BBIt != LoadBB->
begin())
1289 AvailablePredsTy AvailablePreds;
1297 if (!PredsScanned.
insert(PredBB).second)
1300 BBIt = PredBB->
end();
1301 unsigned NumScanedInst = 0;
1302 Value *PredAvailable =
nullptr;
1306 "Attempting to CSE volatile or atomic loads");
1316 &BatchAA, &IsLoadCSE, &NumScanedInst);
1321 while (!PredAvailable && SinglePredBB && BBIt == SinglePredBB->
begin() &&
1325 BBIt = SinglePredBB->
end();
1327 Loc, AccessTy, LoadI->
isAtomic(), SinglePredBB, BBIt,
1333 if (!PredAvailable) {
1334 OneUnavailablePred = PredBB;
1339 CSELoads.
push_back(cast<LoadInst>(PredAvailable));
1343 AvailablePreds.emplace_back(PredBB, PredAvailable);
1348 if (AvailablePreds.empty())
return false;
1365 if (PredsScanned.
size() != AvailablePreds.size() &&
1367 for (
auto I = LoadBB->
begin(); &*
I != LoadI; ++
I)
1374 if (PredsScanned.
size() == AvailablePreds.size()+1 &&
1376 UnavailablePred = OneUnavailablePred;
1377 }
else if (PredsScanned.
size() != AvailablePreds.size()) {
1387 if (isa<IndirectBrInst>(
P->getTerminator()))
1390 if (!AvailablePredSet.
count(
P))
1395 UnavailablePred = splitBlockPreds(LoadBB, PredsToSplit,
"thread-pre-split");
1401 if (UnavailablePred) {
1403 "Can't handle critical edge here!");
1413 AvailablePreds.emplace_back(UnavailablePred, NewVal);
1429 AvailablePredsTy::iterator
I =
1432 assert(
I != AvailablePreds.end() &&
I->first ==
P &&
1433 "Didn't find entry for predecessor!");
1439 Value *&PredV =
I->second;
1442 PredV, LoadI->
getType(),
"",
P->getTerminator()->getIterator());
1447 for (
LoadInst *PredLoadI : CSELoads) {
1465 assert(!PredToDestList.empty());
1477 DestPopularity[
nullptr] = 0;
1479 DestPopularity[SuccBB] = 0;
1481 for (
const auto &PredToDest : PredToDestList)
1482 if (PredToDest.second)
1483 DestPopularity[PredToDest.second]++;
1489 return MostPopular->first;
1505 if (!Visited.
insert(V).second)
1510 assert(PredBB &&
"Expected a single predecessor");
1512 if (
Constant *Cst = dyn_cast<Constant>(V)) {
1518 if (!
I || (
I->getParent() != BB &&
I->getParent() != PredBB)) {
1524 if (
PHI->getParent() == PredBB)
1525 return dyn_cast<Constant>(
PHI->getIncomingValueForBlock(PredPredBB));
1534 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(V)) {
1535 if (CondCmp->getParent() == BB) {
1537 BB, PredPredBB, CondCmp->getOperand(0),
DL, Visited);
1539 BB, PredPredBB, CondCmp->getOperand(1),
DL, Visited);
1556 if (LoopHeaders.count(BB))
1568 "computeValueKnownInPredecessors returned true with no values");
1571 for (
const auto &PredValue : PredValues) {
1573 <<
"': FOUND condition = " << *PredValue.first
1574 <<
" for pred '" << PredValue.second->getName() <<
"'.\n";
1589 for (
const auto &PredValue : PredValues) {
1591 if (!SeenPreds.insert(Pred).second)
1597 if (isa<UndefValue>(Val))
1600 assert(isa<ConstantInt>(Val) &&
"Expecting a constant integer");
1601 DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->
isZero());
1603 assert(isa<ConstantInt>(Val) &&
"Expecting a constant integer");
1604 DestBB = SI->findCaseValue(cast<ConstantInt>(Val))->getCaseSuccessor();
1607 &&
"Unexpected terminator");
1608 assert(isa<BlockAddress>(Val) &&
"Expecting a constant blockaddress");
1609 DestBB = cast<BlockAddress>(Val)->getBasicBlock();
1613 if (PredToDestList.
empty()) {
1617 if (OnlyDest != DestBB)
1618 OnlyDest = MultipleDestSentinel;
1622 OnlyVal = MultipleVal;
1634 if (PredToDestList.
empty())
1640 if (OnlyDest && OnlyDest != MultipleDestSentinel) {
1642 bool SeenFirstBranchToOnlyDest =
false;
1643 std::vector <DominatorTree::UpdateType> Updates;
1646 if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest) {
1647 SeenFirstBranchToOnlyDest =
true;
1649 SuccBB->removePredecessor(BB,
true);
1659 Term->eraseFromParent();
1660 DTU->applyUpdatesPermissive(Updates);
1661 if (
auto *BPI = getBPI())
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);
1911 if (Unreachable.count(SinglePred))
1915 if (LoopHeaders.erase(SinglePred))
1916 LoopHeaders.insert(BB);
1968 for (
Use &U :
I.uses()) {
1971 if (UserPN->getIncomingBlock(U) == BB)
1973 }
else if (
User->getParent() == BB)
1986 if (UsesToRename.
empty() && DbgVariableRecords.
empty())
1988 LLVM_DEBUG(
dbgs() <<
"JT: Renaming non-local uses of: " <<
I <<
"\n");
1997 while (!UsesToRename.
empty())
1999 if (!DbgVariableRecords.
empty()) {
2001 DbgVariableRecords.
clear();
2012 for (
auto It = Begin; It !=
End; ++It)
2032 for (
auto *
Op : DVR->location_ops()) {
2037 auto I = ValueMapping.
find(OpInst);
2038 if (
I != ValueMapping.
end())
2039 OperandsToRemap.
insert({OpInst,
I->second});
2042 for (
auto &[OldOp, MappedOp] : OperandsToRemap)
2043 DVR->replaceVariableLocationOp(OldOp, MappedOp);
2051 for (;
PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2054 ValueMapping[PN] = NewPN;
2071 RetargetDbgVariableRecordIfPossible(&DVR);
2077 for (; BI != BE; ++BI) {
2079 New->setName(BI->getName());
2080 New->insertInto(NewBB, NewBB->
end());
2081 ValueMapping[&*BI] = New;
2084 CloneAndRemapDbgInfo(New, &*BI);
2089 for (
unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2090 if (
Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2092 if (
I != ValueMapping.
end())
2093 New->setOperand(i,
I->second);
2099 if (BE != RangeBB->
end() && BE->hasDbgRecords()) {
2105 RetargetDbgVariableRecordIfPossible(&DVR);
2163 if (LoopHeaders.count(PredBB))
2173 unsigned ZeroCount = 0;
2174 unsigned OneCount = 0;
2180 if (isa<IndirectBrInst>(
P->getTerminator()))
2182 if (
ConstantInt *CI = dyn_cast_or_null<ConstantInt>(
2187 }
else if (CI->isOne()) {
2196 if (ZeroCount == 1) {
2197 PredPredBB = ZeroPred;
2198 }
else if (OneCount == 1) {
2199 PredPredBB = OnePred;
2209 <<
"' - would thread to self!\n");
2215 if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
2217 bool BBIsHeader = LoopHeaders.count(BB);
2218 bool SuccIsHeader = LoopHeaders.count(SuccBB);
2219 dbgs() <<
" Not threading across "
2220 << (BBIsHeader ?
"loop header BB '" :
"block BB '")
2221 << BB->
getName() <<
"' to dest "
2222 << (SuccIsHeader ?
"loop header BB '" :
"block BB '")
2224 <<
"' - it might create an irreducible loop!\n";
2238 if (BBCost > BBDupThreshold || PredBBCost > BBDupThreshold ||
2239 BBCost + PredBBCost > BBDupThreshold) {
2241 <<
"' - Cost is too high: " << PredBBCost
2242 <<
" for PredBB, " << BBCost <<
"for BB\n");
2259 bool HasProfile = doesBlockHaveProfileData(BB);
2260 auto *BFI = getOrCreateBFI(HasProfile);
2261 auto *BPI = getOrCreateBPI(BFI !=
nullptr);
2273 assert(BPI &&
"It's expected BPI to exist along with BFI");
2305 DTU->applyUpdatesPermissive(
2333 <<
"' - would thread to self!\n");
2339 if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
2341 bool BBIsHeader = LoopHeaders.count(BB);
2342 bool SuccIsHeader = LoopHeaders.count(SuccBB);
2343 dbgs() <<
" Not threading across "
2344 << (BBIsHeader ?
"loop header BB '" :
"block BB '") << BB->
getName()
2345 <<
"' to dest " << (SuccIsHeader ?
"loop header BB '" :
"block BB '")
2346 << SuccBB->
getName() <<
"' - it might create an irreducible loop!\n";
2353 if (JumpThreadCost > BBDupThreshold) {
2355 <<
"' - Cost is too high: " << JumpThreadCost <<
"\n");
2369 assert(SuccBB != BB &&
"Don't create an infinite loop");
2371 assert(!LoopHeaders.count(BB) && !LoopHeaders.count(SuccBB) &&
2372 "Don't thread across loop headers");
2375 bool HasProfile = doesBlockHaveProfileData(BB);
2376 auto *BFI = getOrCreateBFI(HasProfile);
2377 auto *BPI = getOrCreateBPI(BFI !=
nullptr);
2381 if (PredBBs.
size() == 1)
2382 PredBB = PredBBs[0];
2385 <<
" common predecessors.\n");
2386 PredBB = splitBlockPreds(BB, PredBBs,
".thr_comm");
2391 <<
"' to '" << SuccBB->
getName()
2392 <<
", across block:\n " << *BB <<
"\n");
2403 assert(BPI &&
"It's expected BPI to exist along with BFI");
2447 updateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB, BFI, BPI, HasProfile);
2458 const char *Suffix) {
2464 auto *BFI = getBFI();
2466 auto *BPI = getOrCreateBPI(
true);
2467 for (
auto *Pred : Preds)
2468 FreqMap.
insert(std::make_pair(
2475 std::string NewName = std::string(Suffix) +
".split-lp";
2481 std::vector<DominatorTree::UpdateType> Updates;
2482 Updates.reserve((2 * Preds.size()) + NewBBs.
size());
2483 for (
auto *NewBB : NewBBs) {
2490 NewBBFreq += FreqMap.
lookup(Pred);
2496 DTU->applyUpdatesPermissive(Updates);
2500bool JumpThreadingPass::doesBlockHaveProfileData(
BasicBlock *BB) {
2511void JumpThreadingPass::updateBlockFreqAndEdgeWeight(
BasicBlock *PredBB,
2518 assert(((BFI && BPI) || (!BFI && !BFI)) &&
2519 "Both BFI & BPI should either be set or unset");
2523 "It's expected to have BFI/BPI when profile info exists");
2529 auto BBOrigFreq =
BFI->getBlockFreq(BB);
2530 auto NewBBFreq =
BFI->getBlockFreq(NewBB);
2531 auto BBNewFreq = BBOrigFreq - NewBBFreq;
2532 BFI->setBlockFreq(BB, BBNewFreq);
2538 auto BB2SuccBBFreq =
2540 auto SuccFreq = (*
I == SuccBB) ? BB2SuccBBFreq - NewBBFreq : BB2SuccBBFreq;
2541 BBSuccFreq.
push_back(SuccFreq.getFrequency());
2547 if (MaxBBSuccFreq == 0)
2549 {1, static_cast<unsigned>(BBSuccFreq.size())});
2596 if (BBSuccProbs.
size() >= 2 && HasProfile) {
2598 for (
auto Prob : BBSuccProbs)
2613 assert(!PredBBs.
empty() &&
"Can't handle an empty set");
2618 if (LoopHeaders.count(BB)) {
2620 <<
"' into predecessor block '" << PredBBs[0]->getName()
2621 <<
"' - it might create an irreducible loop!\n");
2627 if (DuplicationCost > BBDupThreshold) {
2629 <<
"' - Cost is too high: " << DuplicationCost <<
"\n");
2634 std::vector<DominatorTree::UpdateType> Updates;
2636 if (PredBBs.
size() == 1)
2637 PredBB = PredBBs[0];
2640 <<
" common predecessors.\n");
2641 PredBB = splitBlockPreds(BB, PredBBs,
".thr_comm");
2648 <<
"' into end of '" << PredBB->
getName()
2649 <<
"' to eliminate branch on phi. Cost: "
2650 << DuplicationCost <<
" block is:" << *BB <<
"\n");
2673 for (;
PHINode *PN = dyn_cast<PHINode>(BI); ++BI)
2677 for (; BI != BB->
end(); ++BI) {
2679 New->insertInto(PredBB, OldPredBranch->
getIterator());
2682 for (
unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2683 if (
Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2685 if (
I != ValueMapping.
end())
2686 New->setOperand(i,
I->second);
2700 ValueMapping[&*BI] =
IV;
2701 if (!New->mayHaveSideEffects()) {
2702 New->eraseFromParent();
2709 ValueMapping[&*BI] = New;
2713 New->setName(BI->getName());
2715 New->cloneDebugInfoFrom(&*BI);
2717 for (
unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2718 if (
BasicBlock *SuccBB = dyn_cast<BasicBlock>(New->getOperand(i)))
2732 remapSourceAtoms(ValueMapping, std::prev(RItBeforeInsertPt)->getIterator(),
2743 if (
auto *BPI = getBPI())
2745 DTU->applyUpdatesPermissive(Updates);
2776 BI->applyMergedLocation(PredTerm->
getDebugLoc(), SI->getDebugLoc());
2777 BI->copyMetadata(*SI, {LLVMContext::MD_prof});
2785 (TrueWeight + FalseWeight) != 0) {
2788 TrueWeight, TrueWeight + FalseWeight));
2790 FalseWeight, TrueWeight + FalseWeight));
2792 if (
auto *BPI = getBPI())
2796 if (
auto *BFI = getBFI()) {
2797 if ((TrueWeight + FalseWeight) == 0) {
2802 TrueWeight, TrueWeight + FalseWeight);
2803 auto NewBBFreq = BFI->getBlockFreq(Pred) * PredToNewBBProb;
2804 BFI->setBlockFreq(NewBB, NewBBFreq);
2808 SI->eraseFromParent();
2814 PHINode *Phi = dyn_cast<PHINode>(BI); ++BI)
2816 Phi->addIncoming(Phi->getIncomingValueForBlock(Pred), NewBB);
2820 PHINode *CondPHI = dyn_cast<PHINode>(SI->getCondition());
2822 if (!CondPHI || CondPHI->
getParent() != BB)
2872 if (!SI || SI->getParent() != Pred || !SI->hasOneUse())
2884 CondRHS, Pred, BB, CondCmp);
2887 CondRHS, Pred, BB, CondCmp);
2888 if ((LHSRes || RHSRes) && LHSRes != RHSRes) {
2924 if (LoopHeaders.count(BB))
2928 PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2931 [](
Value *V) { return !isa<ConstantInt>(V); }))
2935 using namespace PatternMatch;
2938 if (SI->getParent() != BB)
2942 return Cond &&
Cond == V &&
Cond->getType()->isIntegerTy(1) && !IsAndOr;
2946 for (
Use &U : PN->uses()) {
2947 if (
ICmpInst *Cmp = dyn_cast<ICmpInst>(U.getUser())) {
2950 if (Cmp->getParent() == BB && Cmp->hasOneUse() &&
2951 isa<ConstantInt>(Cmp->getOperand(1 - U.getOperandNo())))
2952 if (
SelectInst *SelectI = dyn_cast<SelectInst>(Cmp->user_back()))
2953 if (isUnfoldCandidate(SelectI, Cmp->use_begin()->get())) {
2957 }
else if (
SelectInst *SelectI = dyn_cast<SelectInst>(U.getUser())) {
2959 if (isUnfoldCandidate(SelectI, U.get())) {
2980 NewPN->
addIncoming(SI->getTrueValue(), Term->getParent());
2983 SI->replaceAllUsesWith(NewPN);
2984 SI->eraseFromParent();
2986 std::vector<DominatorTree::UpdateType> Updates;
2996 DTU->applyUpdatesPermissive(Updates);
3022 using namespace PatternMatch;
3044 if (
auto *BI = dyn_cast<BranchInst>(Parent->getTerminator()))
3065 bool TrueDestIsSafe =
false;
3066 bool FalseDestIsSafe =
false;
3071 TrueDestIsSafe =
true;
3076 FalseDestIsSafe =
true;
3079 if (!TrueDestIsSafe && !FalseDestIsSafe)
3082 BasicBlock *PredUnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
3083 BasicBlock *PredGuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
3089 if (
Cost > BBDupThreshold)
3094 BB, PredGuardedBlock, AfterGuard, GuardedMapping, *DTU);
3095 assert(GuardedBlock &&
"Could not create the guarded block?");
3100 BB, PredUnguardedBlock, Guard, UnguardedMapping, *DTU);
3101 assert(UnguardedBlock &&
"Could not create the unguarded block?");
3103 << GuardedBlock->
getName() <<
"\n");
3108 for (
auto BI = BB->
begin(); &*BI != AfterGuard; ++BI)
3109 if (!isa<PHINode>(&*BI))
3116 if (!Inst->use_empty()) {
3118 NewPN->
addIncoming(UnguardedMapping[Inst], UnguardedBlock);
3119 NewPN->
addIncoming(GuardedMapping[Inst], GuardedBlock);
3122 Inst->replaceAllUsesWith(NewPN);
3124 Inst->dropDbgRecords();
3125 Inst->eraseFromParent();
3140template <
typename AnalysisT>
3141typename AnalysisT::Result *JumpThreadingPass::runExternalAnalysis() {
3142 assert(FAM &&
"Can't run external analysis without FunctionAnalysisManager");
3147 if (!ChangedSinceLastAnalysisUpdate) {
3148 assert(!DTU->hasPendingUpdates() &&
3149 "Lost update of 'ChangedSinceLastAnalysisUpdate'?");
3153 ChangedSinceLastAnalysisUpdate =
false;
3155 auto PA = getPreservedAnalysis();
3165 assert(DTU->getDomTree().verify(DominatorTree::VerificationLevel::Fast));
3166 assert((!DTU->hasPostDomTree() ||
3167 DTU->getPostDomTree().verify(
3181 assert(FAM &&
"Can't create BPI without FunctionAnalysisManager");
3189 assert(FAM &&
"Can't create BFI without FunctionAnalysisManager");
3199 auto *Res = getBPI();
3204 BPI = runExternalAnalysis<BranchProbabilityAnalysis>();
3210 auto *Res = getBFI();
3215 BFI = runExternalAnalysis<BlockFrequencyAnalysis>();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ReachingDefAnalysis InstSet & ToRemove
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
BlockVerifier::State From
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 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...
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
This defines the Use class.
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 void remapSourceAtoms(ValueToValueMapTy &VM, BasicBlock::iterator Begin, BasicBlock::iterator End)
static void addPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, BasicBlock *OldPred, BasicBlock *NewPred, ValueToValueMapTy &ValueMap)
addPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new predecessor to the PHIBB block.
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< 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 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.
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
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)
static const uint32_t IV[8]
A manager for alias analyses.
A private abstract base class describing the concept of an individual alias analysis implementation.
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.
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
LLVM_ABI DbgMarker * createMarker(Instruction *I)
Attach a DbgMarker to the given instruction.
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.
LLVM_ABI void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
LLVM_ABI bool hasNPredecessors(unsigned N) const
Return true if this block has exactly N predecessors.
LLVM_ABI 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.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
LLVM_ABI DbgMarker * getMarker(InstListType::iterator It)
Return the DbgMarker for the position given by It, so that DbgRecords can be inserted there.
InstListType::iterator iterator
Instruction iterators...
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
LLVM_ABI 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...
LLVM_ABI void removePredecessor(BasicBlock *Pred, bool KeepOneInputPHIs=false)
Update PHI nodes in this BasicBlock before removal of predecessor Pred.
This class is a wrapper over an AAResults, and it is intended to be used only when there are no IR ch...
void disableDominatorTree()
Disable the use of the dominator tree during alias analysis queries.
The address of a basic block.
static LLVM_ABI 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...
LLVM_ABI void setBlockFreq(const BasicBlock *BB, BlockFrequency Freq)
LLVM_ABI BlockFrequency getBlockFreq(const BasicBlock *BB) const
getblockFreq - Return block frequency.
Conditional or Unconditional Branch instruction.
bool isConditional() const
unsigned getNumSuccessors() const
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
Value * getCondition() const
Analysis pass which computes BranchProbabilityInfo.
Analysis providing branch probability information.
LLVM_ABI void eraseBlock(const BasicBlock *BB)
Forget analysis results for the given basic block.
LLVM_ABI void setEdgeProbability(const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs)
Set the raw probabilities for all edges from the given block.
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst)
Copy outgoing edge probabilities from Src to Dst.
static LLVM_ABI 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 LLVM_ABI CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", InsertPosition 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.
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI Constant * getNot(Constant *C)
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.
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
const APInt & getValue() const
Return the constant as an APInt value reference.
This class represents a range of values.
LLVM_ABI 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 LLVM_ABI 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...
LLVM_ABI ConstantRange inverse() const
Return a new range that is the logical not of the current set.
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
This is an important base class in LLVM.
LLVM_ABI void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Per-instruction record of debug-info.
LLVM_ABI iterator_range< simple_ilist< DbgRecord >::iterator > cloneDebugInfoFrom(DbgMarker *From, std::optional< simple_ilist< DbgRecord >::iterator > FromHere, bool InsertAtHead=false)
Clone all DbgMarkers from From into this marker.
LLVM_ABI const BasicBlock * getParent() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
static DebugLoc getTemporary()
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...
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
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.
LLVM_ABI 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 hasMinSize() const
Optimize this function for minimum size (-Oz).
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
void flush()
Apply all pending updates to available trees and flush all BasicBlocks awaiting deletion.
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.
LLVM_ABI void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
LLVM_ABI iterator_range< simple_ilist< DbgRecord >::iterator > cloneDebugInfoFrom(const Instruction *From, std::optional< simple_ilist< DbgRecord >::iterator > FromHere=std::nullopt, bool InsertAtHead=false)
Clone any debug-info attached to From onto this instruction.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
LLVM_ABI bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
LLVM_ABI AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
LLVM_ABI void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
bool isSpecialTerminator() const
LLVM_ABI InstListType::iterator insertInto(BasicBlock *ParentBB, InstListType::iterator It)
Inserts an unlinked instruction into ParentBB at position It and returns the iterator of the inserted...
A wrapper class for inspecting calls to intrinsic functions.
LLVM_ABI bool simplifyPartiallyRedundantLoad(LoadInst *LI)
simplifyPartiallyRedundantLoad - If LoadI is an obviously partially redundant load instruction,...
LLVM_ABI bool processBranchOnXOR(BinaryOperator *BO)
processBranchOnXOR - We have an otherwise unthreadable conditional branch on a xor instruction in the...
LLVM_ABI bool processGuards(BasicBlock *BB)
Try to propagate a guard from the current BB into one of its predecessors in case if another branch o...
LLVM_ABI void updateSSA(BasicBlock *BB, BasicBlock *NewBB, ValueToValueMapTy &ValueMapping)
Update the SSA form.
bool computeValueKnownInPredecessors(Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
LLVM_ABI void findLoopHeaders(Function &F)
findLoopHeaders - We do not want jump threading to turn proper loop structures into irreducible loops...
LLVM_ABI bool maybeMergeBasicBlockIntoOnlyPred(BasicBlock *BB)
Merge basic block BB into its sole predecessor if possible.
LLVM_ABI JumpThreadingPass(int T=-1)
LLVM_ABI void cloneInstructions(ValueToValueMapTy &ValueMapping, BasicBlock::iterator BI, BasicBlock::iterator BE, BasicBlock *NewBB, BasicBlock *PredBB)
Clone instructions in range [BI, BE) to NewBB.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI Constant * evaluateOnPredecessorEdge(BasicBlock *BB, BasicBlock *PredPredBB, Value *cond, const DataLayout &DL)
LLVM_ABI bool processBranchOnPHI(PHINode *PN)
processBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node (or freeze PH...
LLVM_ABI bool maybethreadThroughTwoBasicBlocks(BasicBlock *BB, Value *Cond)
Attempt to thread through two successive basic blocks.
LLVM_ABI bool computeValueKnownInPredecessorsImpl(Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, SmallPtrSet< Value *, 4 > &RecursionSet, Instruction *CxtI=nullptr)
computeValueKnownInPredecessors - Given a basic block BB and a value V, see if we can infer that the ...
LLVM_ABI void unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB, SelectInst *SI, PHINode *SIUse, unsigned Idx)
DomTreeUpdater * getDomTreeUpdater() const
LLVM_ABI bool runImpl(Function &F, FunctionAnalysisManager *FAM, TargetLibraryInfo *TLI, TargetTransformInfo *TTI, LazyValueInfo *LVI, AAResults *AA, std::unique_ptr< DomTreeUpdater > DTU, BlockFrequencyInfo *BFI, BranchProbabilityInfo *BPI)
LLVM_ABI bool processThreadableEdges(Value *Cond, BasicBlock *BB, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
LLVM_ABI bool processBlock(BasicBlock *BB)
processBlock - If there are any predecessors whose control can be threaded through to a successor,...
LLVM_ABI bool processImpliedCondition(BasicBlock *BB)
LLVM_ABI bool duplicateCondBranchOnPHIIntoPred(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs)
duplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch to BB which contains an i1...
LLVM_ABI void threadThroughTwoBasicBlocks(BasicBlock *PredPredBB, BasicBlock *PredBB, BasicBlock *BB, BasicBlock *SuccBB)
LLVM_ABI bool tryThreadEdge(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs, BasicBlock *SuccBB)
tryThreadEdge - Thread an edge if it's safe and profitable to do so.
LLVM_ABI bool tryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB)
tryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb2
LLVM_ABI bool tryToUnfoldSelectInCurrBB(BasicBlock *BB)
tryToUnfoldSelectInCurrBB - Look for PHI/Select or PHI/CMP/Select in the same BB in the form bb: p = ...
LLVM_ABI 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...
LLVM_ABI 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...
Constant * getPredicateOnEdge(CmpInst::Predicate 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 ...
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...
Constant * getConstant(Value *V, Instruction *CxtI)
Determine whether the specified value is known to be a constant at the specified instruction.
void forgetValue(Value *V)
Remove information related to this value from the cache.
Constant * getPredicateAt(CmpInst::Predicate 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 ...
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)
This class implements a map that also provides access to all stored values in a deterministic order.
Representation for a specific memory location.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
void setIncomingValue(unsigned i, Value *V)
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 PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI 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.
PreservedAnalyses & 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.
bool erase(PtrType Ptr)
Remove pointer from the set.
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)
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.
bool isIntegerTy() const
True if this is an instance of IntegerType.
'undef' values are things that do not have specified contents.
static LLVM_ABI 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)
DMAtomT AtomMap
Map {(InlinedAt, old atom number) -> new atom number}.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI 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.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
LLVM_ABI void takeName(Value *V)
Transfer the name from V to this value.
const ParentTy * getParent() const
reverse_self_iterator getReverseIterator()
self_iterator getIterator()
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
@ C
The default llvm calling convention, compatible with C.
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
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.
LLVM_ABI 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.
LLVM_ABI 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...
static cl::opt< unsigned long > StopAt("sbvec-stop-at", cl::init(StopAtDisabled), cl::Hidden, cl::desc("Vectorize if the invocation count is < than this. 0 " "disables vectorization."))
LLVM_ABI void findDbgValues(Value *V, SmallVectorImpl< DbgVariableRecord * > &DbgVariableRecords)
Finds the dbg.values describing a value.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
auto pred_end(const MachineBasicBlock *BB)
LLVM_ABI unsigned replaceNonLocalUsesWith(Instruction *From, Value *To)
auto successors(const MachineBasicBlock *BB)
LLVM_ABI Constant * ConstantFoldInstruction(const Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
constexpr from_range_t from_range
LLVM_ABI MDNode * getBranchWeightMDNode(const Instruction &I)
Get the branch weights metadata node.
LLVM_ABI Value * findAvailablePtrLoadStore(const MemoryLocation &Loc, Type *AccessTy, bool AtLeastAtomic, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan, BatchAAResults *AA, bool *IsLoadCSE, unsigned *NumScanedInst)
Scan backwards to see if we have the value of the given pointer available locally within a small numb...
LLVM_ABI void remapDebugVariable(ValueToValueMapTy &Mapping, Instruction *Inst)
Remap the operands of the debug records attached to Inst, and the operands of Inst itself if it's a d...
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
auto pred_size(const MachineBasicBlock *BB)
LLVM_ABI 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...
LLVM_ABI void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU=nullptr, bool KeepOneInputPHIs=false)
Delete the specified block, which must have no predecessors.
LLVM_ABI Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, BatchAAResults *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 ...
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI bool hasBranchWeightOrigin(const Instruction &I)
Check if Branch Weight Metadata has an "expected" field from an llvm.expect* intrinsic.
LLVM_ABI 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...
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI 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....
LLVM_ABI 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)
LLVM_ABI void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
LLVM_ABI bool hasValidBranchWeightMD(const Instruction &I)
Checks if an instructions has valid Branch Weight Metadata.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto make_first_range(ContainerTy &&c)
Given a container of pairs, return a range over the first elements.
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI void cloneNoAliasScopes(ArrayRef< MDNode * > NoAliasDeclScopes, DenseMap< MDNode *, MDNode * > &ClonedScopes, StringRef Ext, LLVMContext &Context)
Duplicate the specified list of noalias decl scopes.
LLVM_ABI cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
LLVM_ABI void SplitLandingPadPredecessors(BasicBlock *OrigBB, ArrayRef< BasicBlock * > Preds, const char *Suffix, const char *Suffix2, SmallVectorImpl< BasicBlock * > &NewBBs, DomTreeUpdater *DTU=nullptr, 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...
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
LLVM_ABI void combineMetadataForCSE(Instruction *K, const Instruction *J, bool DoesKMove)
Combine the metadata of two instructions so that K can replace J.
LLVM_ABI 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...
LLVM_ABI void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DomTreeUpdater *DTU=nullptr)
BB is a block with one predecessor and its predecessor is known to have one successor (BB!...
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
LLVM_ABI void adaptNoAliasScopes(llvm::Instruction *I, const DenseMap< MDNode *, MDNode * > &ClonedScopes, LLVMContext &Context)
Adapt the metadata for the specified instruction according to the provided mapping.
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
LLVM_ABI 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.
auto make_second_range(ContainerTy &&c)
Given a container of pairs, return a range over the second elements.
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
auto pred_begin(const MachineBasicBlock *BB)
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)
LLVM_ABI Instruction * SplitBlockAndInsertIfThen(Value *Cond, BasicBlock::iterator SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DomTreeUpdater *DTU=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
LLVM_ABI Value * simplifyCmpInst(CmpPredicate Predicate, Value *LHS, Value *RHS, const SimplifyQuery &Q)
Given operands for a CmpInst, fold the result or return null.
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
LLVM_ABI void identifyNoAliasScopesToClone(ArrayRef< BasicBlock * > BBs, SmallVectorImpl< MDNode * > &NoAliasDeclScopes)
Find the 'llvm.experimental.noalias.scope.decl' intrinsics in the specified basic blocks and extract ...
LLVM_ABI 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...
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI 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.
LLVM_ABI void RemapSourceAtom(Instruction *I, ValueToValueMapTy &VM)
Remap source location atom.
LLVM_ABI 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.
LLVM_ABI void mapAtomInstance(const DebugLoc &DL, ValueToValueMapTy &VMap)
Mark a cloned instruction as a new instance so that its source loc can be updated when remapped.
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:...