78using namespace jumpthreading;
80#define DEBUG_TYPE "jump-threading"
84STATISTIC(NumDupes,
"Number of branch blocks duplicated to eliminate phi");
88 cl::desc(
"Max block size to duplicate for jump threading"),
93 "jump-threading-implication-search-threshold",
94 cl::desc(
"The number of predecessors to search for a stronger "
95 "condition to use to thread over a weaker condition"),
99 "jump-threading-phi-threshold",
104 "jump-threading-across-loop-headers",
105 cl::desc(
"Allow JumpThreading to thread across loop headers, for testing"),
156 if (TrueWeight + FalseWeight == 0)
164 auto GetPredOutEdge =
166 BasicBlock *PhiBB) -> std::pair<BasicBlock *, BasicBlock *> {
167 auto *PredBB = IncomingBB;
168 auto *SuccBB = PhiBB;
171 BranchInst *PredBr = dyn_cast<BranchInst>(PredBB->getTerminator());
173 return {PredBB, SuccBB};
175 auto *SinglePredBB = PredBB->getSinglePredecessor();
177 return {
nullptr,
nullptr};
181 if (Visited.
count(SinglePredBB))
182 return {
nullptr,
nullptr};
185 PredBB = SinglePredBB;
198 TrueWeight, TrueWeight + FalseWeight)
200 FalseWeight, TrueWeight + FalseWeight));
203 if (!PredOutEdge.first)
211 uint64_t PredTrueWeight, PredFalseWeight;
249 std::make_unique<DomTreeUpdater>(
250 &DT,
nullptr, DomTreeUpdater::UpdateStrategy::Lazy),
251 std::nullopt, std::nullopt);
259#if defined(EXPENSIVE_CHECKS)
261 DominatorTree::VerificationLevel::Full) &&
262 "DT broken after JumpThreading");
266 "PDT broken after JumpThreading");
269 DominatorTree::VerificationLevel::Fast) &&
270 "DT broken after JumpThreading");
274 "PDT broken after JumpThreading");
277 return getPreservedAnalysis();
284 std::unique_ptr<DomTreeUpdater> DTU_,
285 std::optional<BlockFrequencyInfo *> BFI_,
286 std::optional<BranchProbabilityInfo *> BPI_) {
294 DTU = std::move(DTU_);
298 F->
getParent(), Intrinsic::experimental_guard);
299 HasGuards = GuardDecl && !GuardDecl->use_empty();
308 BBDupThreshold = DefaultBBDupThreshold;
313 assert(DTU &&
"DTU isn't passed into JumpThreading before using it.");
314 assert(DTU->hasDomTree() &&
"JumpThreading relies on DomTree to proceed.");
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))
472 for (; &*
I != StopAt; ++
I) {
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;
529 for (
const auto &Edge : Edges)
530 LoopHeaders.insert(Edge.second);
543 if (
UndefValue *U = dyn_cast<UndefValue>(Val))
549 return dyn_cast<ConstantInt>(Val);
568 if (!RecursionSet.
insert(V).second)
574 Result.emplace_back(KC, Pred);
576 return !Result.empty();
582 if (!
I ||
I->getParent() != BB) {
587 using namespace PatternMatch;
601 Result.emplace_back(KC,
P);
604 return !Result.empty();
608 if (
PHINode *PN = dyn_cast<PHINode>(
I)) {
609 for (
unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
610 Value *InVal = PN->getIncomingValue(i);
612 Result.emplace_back(KC, PN->getIncomingBlock(i));
615 PN->getIncomingBlock(i),
618 Result.emplace_back(KC, PN->getIncomingBlock(i));
622 return !Result.empty();
626 if (
CastInst *CI = dyn_cast<CastInst>(
I)) {
627 Value *Source = CI->getOperand(0);
635 for (
auto &Val : Vals)
638 Result.emplace_back(Folded, Val.second);
640 return !Result.empty();
644 Value *Source = FI->getOperand(0);
652 return !Result.empty();
656 if (
I->getType()->getPrimitiveSizeInBits() == 1) {
657 using namespace PatternMatch;
685 for (
const auto &LHSVal : LHSVals)
686 if (LHSVal.first == InterestingVal || isa<UndefValue>(LHSVal.first)) {
687 Result.emplace_back(InterestingVal, LHSVal.second);
688 LHSKnownBBs.
insert(LHSVal.second);
690 for (
const auto &RHSVal : RHSVals)
691 if (RHSVal.first == InterestingVal || isa<UndefValue>(RHSVal.first)) {
694 if (!LHSKnownBBs.
count(RHSVal.second))
695 Result.emplace_back(InterestingVal, RHSVal.second);
698 return !Result.empty();
702 if (
I->getOpcode() == Instruction::Xor &&
703 isa<ConstantInt>(
I->getOperand(1)) &&
704 cast<ConstantInt>(
I->getOperand(1))->isOne()) {
711 for (
auto &R : Result)
721 if (
ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
727 for (
const auto &LHSVal : LHSVals) {
733 Result.emplace_back(KC, LHSVal.second);
737 return !Result.empty();
741 if (
CmpInst *Cmp = dyn_cast<CmpInst>(
I)) {
744 Type *CmpType = Cmp->getType();
745 Value *CmpLHS = Cmp->getOperand(0);
746 Value *CmpRHS = Cmp->getOperand(1);
749 PHINode *PN = dyn_cast<PHINode>(CmpLHS);
751 PN = dyn_cast<PHINode>(CmpRHS);
755 if (PN && PN->
getParent() == BB && !LoopHeaders.contains(BB)) {
771 if (!isa<Constant>(
RHS))
775 auto LHSInst = dyn_cast<Instruction>(
LHS);
776 if (LHSInst && LHSInst->getParent() == BB)
780 BB, CxtI ? CxtI : Cmp);
784 Result.emplace_back(KC, PredBB);
787 return !Result.empty();
792 if (isa<Constant>(CmpRHS) && !CmpType->
isVectorTy()) {
793 Constant *CmpConst = cast<Constant>(CmpRHS);
795 if (!isa<Instruction>(CmpLHS) ||
796 cast<Instruction>(CmpLHS)->
getParent() != BB) {
803 Result.emplace_back(KC,
P);
806 return !Result.empty();
813 using namespace PatternMatch;
817 if (isa<ConstantInt>(CmpConst) &&
819 if (!isa<Instruction>(AddLHS) ||
820 cast<Instruction>(AddLHS)->
getParent() != BB) {
826 AddLHS,
P, BB, CxtI ? CxtI : cast<Instruction>(CmpLHS));
832 Pred, cast<ConstantInt>(CmpConst)->getValue());
842 Result.emplace_back(ResC,
P);
845 return !Result.empty();
856 for (
const auto &LHSVal : LHSVals) {
861 Result.emplace_back(KC, LHSVal.second);
864 return !Result.empty();
874 if ((TrueVal || FalseVal) &&
877 for (
auto &
C : Conds) {
884 KnownCond = CI->isOne();
886 assert(isa<UndefValue>(
Cond) &&
"Unexpected condition value");
890 KnownCond = (TrueVal !=
nullptr);
894 if (
Constant *Val = KnownCond ? TrueVal : FalseVal)
895 Result.emplace_back(Val,
C.second);
898 return !Result.empty();
907 Result.emplace_back(KC, Pred);
910 return !Result.empty();
920 unsigned MinSucc = 0;
923 unsigned MinNumPreds =
pred_size(TestBB);
927 if (NumPreds < MinNumPreds) {
929 MinNumPreds = NumPreds;
951 if (DTU->isBBPendingDeletion(BB) ||
976 if (
BranchInst *BI = dyn_cast<BranchInst>(Terminator)) {
978 if (BI->isUnconditional())
return false;
979 Condition = BI->getCondition();
980 }
else if (
SwitchInst *SI = dyn_cast<SwitchInst>(Terminator)) {
981 Condition = SI->getCondition();
982 }
else if (
IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) {
984 if (IB->getNumSuccessors() == 0)
return false;
985 Condition = IB->getAddress()->stripPointerCasts();
992 bool ConstantFolded =
false;
996 if (
Instruction *
I = dyn_cast<Instruction>(Condition)) {
1000 I->replaceAllUsesWith(SimpleVal);
1002 I->eraseFromParent();
1003 Condition = SimpleVal;
1004 ConstantFolded =
true;
1010 auto *FI = dyn_cast<FreezeInst>(Condition);
1011 if (isa<UndefValue>(Condition) ||
1012 (FI && isa<UndefValue>(FI->getOperand(0)) && FI->hasOneUse())) {
1014 std::vector<DominatorTree::UpdateType> Updates;
1020 if (i == BestSucc)
continue;
1027 <<
"' folding undef terminator: " << *BBTerm <<
'\n');
1032 DTU->applyUpdatesPermissive(Updates);
1034 FI->eraseFromParent();
1047 if (
auto *BPI = getBPI())
1048 BPI->eraseBlock(BB);
1052 Instruction *CondInst = dyn_cast<Instruction>(Condition);
1059 return ConstantFolded;
1063 Value *CondWithoutFreeze = CondInst;
1064 if (
auto *FI = dyn_cast<FreezeInst>(CondInst))
1065 CondWithoutFreeze = FI->getOperand(0);
1067 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(CondWithoutFreeze)) {
1071 if (
Constant *CondConst = dyn_cast<Constant>(CondCmp->getOperand(1))) {
1073 LVI->
getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
1103 Value *SimplifyValue = CondWithoutFreeze;
1105 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue))
1106 if (isa<Constant>(CondCmp->getOperand(1)))
1107 SimplifyValue = CondCmp->getOperand(0);
1111 if (
LoadInst *LoadI = dyn_cast<LoadInst>(SimplifyValue))
1116 if (
PHINode *PN = dyn_cast<PHINode>(CondInst))
1117 if (PN->getParent() == BB && isa<BranchInst>(BB->
getTerminator()))
1128 PHINode *PN = dyn_cast<PHINode>(CondWithoutFreeze);
1133 if (CondInst->
getOpcode() == Instruction::Xor &&
1147 if (!BI || !BI->isConditional())
1156 auto *FICond = dyn_cast<FreezeInst>(
Cond);
1157 if (FICond && FICond->hasOneUse())
1158 Cond = FICond->getOperand(0);
1169 auto *PBI = dyn_cast<BranchInst>(CurrentPred->
getTerminator());
1170 if (!PBI || !PBI->isConditional())
1172 if (PBI->getSuccessor(0) != CurrentBB && PBI->getSuccessor(1) != CurrentBB)
1175 bool CondIsTrue = PBI->getSuccessor(0) == CurrentBB;
1176 std::optional<bool> Implication =
1181 if (!Implication && FICond && isa<FreezeInst>(PBI->getCondition())) {
1182 if (cast<FreezeInst>(PBI->getCondition())->getOperand(0) ==
1183 FICond->getOperand(0))
1184 Implication = CondIsTrue;
1188 BasicBlock *KeepSucc = BI->getSuccessor(*Implication ? 0 : 1);
1189 BasicBlock *RemoveSucc = BI->getSuccessor(*Implication ? 1 : 0);
1194 BI->eraseFromParent();
1196 FICond->eraseFromParent();
1199 if (
auto *BPI = getBPI())
1200 BPI->eraseBlock(BB);
1203 CurrentBB = CurrentPred;
1213 if (OpInst->getParent() == BB)
1258 LoadInst *NLoadI = cast<LoadInst>(AvailableVal);
1265 if (AvailableVal == LoadI)
1267 if (AvailableVal->getType() != LoadI->
getType()) {
1270 cast<Instruction>(AvailableVal)->setDebugLoc(LoadI->
getDebugLoc());
1280 if (BBIt != LoadBB->
begin())
1291 AvailablePredsTy AvailablePreds;
1299 if (!PredsScanned.
insert(PredBB).second)
1302 BBIt = PredBB->
end();
1303 unsigned NumScanedInst = 0;
1304 Value *PredAvailable =
nullptr;
1308 "Attempting to CSE volatile or atomic loads");
1318 &BatchAA, &IsLoadCSE, &NumScanedInst);
1323 while (!PredAvailable && SinglePredBB && BBIt == SinglePredBB->
begin() &&
1327 BBIt = SinglePredBB->
end();
1329 Loc, AccessTy, LoadI->
isAtomic(), SinglePredBB, BBIt,
1335 if (!PredAvailable) {
1336 OneUnavailablePred = PredBB;
1341 CSELoads.
push_back(cast<LoadInst>(PredAvailable));
1345 AvailablePreds.emplace_back(PredBB, PredAvailable);
1350 if (AvailablePreds.empty())
return false;
1367 if (PredsScanned.
size() != AvailablePreds.size() &&
1369 for (
auto I = LoadBB->
begin(); &*
I != LoadI; ++
I)
1376 if (PredsScanned.
size() == AvailablePreds.size()+1 &&
1378 UnavailablePred = OneUnavailablePred;
1379 }
else if (PredsScanned.
size() != AvailablePreds.size()) {
1385 for (
const auto &AvailablePred : AvailablePreds)
1386 AvailablePredSet.
insert(AvailablePred.first);
1391 if (isa<IndirectBrInst>(
P->getTerminator()))
1394 if (!AvailablePredSet.
count(
P))
1399 UnavailablePred = splitBlockPreds(LoadBB, PredsToSplit,
"thread-pre-split");
1405 if (UnavailablePred) {
1407 "Can't handle critical edge here!");
1417 AvailablePreds.emplace_back(UnavailablePred, NewVal);
1433 AvailablePredsTy::iterator
I =
1436 assert(
I != AvailablePreds.end() &&
I->first ==
P &&
1437 "Didn't find entry for predecessor!");
1443 Value *&PredV =
I->second;
1446 PredV, LoadI->
getType(),
"",
P->getTerminator()->getIterator());
1451 for (
LoadInst *PredLoadI : CSELoads) {
1469 assert(!PredToDestList.empty());
1481 DestPopularity[
nullptr] = 0;
1483 DestPopularity[SuccBB] = 0;
1485 for (
const auto &PredToDest : PredToDestList)
1486 if (PredToDest.second)
1487 DestPopularity[PredToDest.second]++;
1493 return MostPopular->first;
1503 assert(PredBB &&
"Expected a single predecessor");
1505 if (
Constant *Cst = dyn_cast<Constant>(V)) {
1511 if (!
I || (
I->getParent() != BB &&
I->getParent() != PredBB)) {
1517 if (
PHI->getParent() == PredBB)
1518 return dyn_cast<Constant>(
PHI->getIncomingValueForBlock(PredPredBB));
1523 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(V)) {
1524 if (CondCmp->getParent() == BB) {
1545 if (LoopHeaders.count(BB))
1557 "computeValueKnownInPredecessors returned true with no values");
1560 for (
const auto &PredValue : PredValues) {
1562 <<
"': FOUND condition = " << *PredValue.first
1563 <<
" for pred '" << PredValue.second->getName() <<
"'.\n";
1578 for (
const auto &PredValue : PredValues) {
1580 if (!SeenPreds.insert(Pred).second)
1586 if (isa<UndefValue>(Val))
1589 assert(isa<ConstantInt>(Val) &&
"Expecting a constant integer");
1590 DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->
isZero());
1592 assert(isa<ConstantInt>(Val) &&
"Expecting a constant integer");
1593 DestBB = SI->findCaseValue(cast<ConstantInt>(Val))->getCaseSuccessor();
1596 &&
"Unexpected terminator");
1597 assert(isa<BlockAddress>(Val) &&
"Expecting a constant blockaddress");
1598 DestBB = cast<BlockAddress>(Val)->getBasicBlock();
1602 if (PredToDestList.
empty()) {
1606 if (OnlyDest != DestBB)
1607 OnlyDest = MultipleDestSentinel;
1611 OnlyVal = MultipleVal;
1623 if (PredToDestList.
empty())
1629 if (OnlyDest && OnlyDest != MultipleDestSentinel) {
1631 bool SeenFirstBranchToOnlyDest =
false;
1632 std::vector <DominatorTree::UpdateType> Updates;
1635 if (SuccBB == OnlyDest && !SeenFirstBranchToOnlyDest) {
1636 SeenFirstBranchToOnlyDest =
true;
1638 SuccBB->removePredecessor(BB,
true);
1648 Term->eraseFromParent();
1649 DTU->applyUpdatesPermissive(Updates);
1650 if (
auto *BPI = getBPI())
1651 BPI->eraseBlock(BB);
1655 if (
auto *CondInst = dyn_cast<Instruction>(
Cond)) {
1656 if (CondInst->use_empty() && !CondInst->mayHaveSideEffects())
1657 CondInst->eraseFromParent();
1665 else if (OnlyVal && OnlyVal != MultipleVal)
1678 if (MostPopularDest == MultipleDestSentinel) {
1683 [&](
const std::pair<BasicBlock *, BasicBlock *> &PredToDest) {
1684 return LoopHeaders.contains(PredToDest.second);
1687 if (PredToDestList.
empty())
1696 for (
const auto &PredToDest : PredToDestList)
1697 if (PredToDest.second == MostPopularDest) {
1710 if (!MostPopularDest)
1739 if (PredBr->isUnconditional()) {
1740 PredBBs[0] = PredBB;
1764 if (!isa<PHINode>(BB->
front()))
1801 "computeValueKnownInPredecessors returned true with no values");
1805 unsigned NumTrue = 0, NumFalse = 0;
1806 for (
const auto &XorOpValue : XorOpValues) {
1807 if (isa<UndefValue>(XorOpValue.first))
1810 if (cast<ConstantInt>(XorOpValue.first)->isZero())
1818 if (NumTrue > NumFalse)
1820 else if (NumTrue != 0 || NumFalse != 0)
1826 for (
const auto &XorOpValue : XorOpValues) {
1827 if (XorOpValue.first != SplitVal && !isa<UndefValue>(XorOpValue.first))
1830 BlocksToFoldInto.
push_back(XorOpValue.second);
1835 if (BlocksToFoldInto.
size() ==
1836 cast<PHINode>(BB->
front()).getNumIncomingValues()) {
1874 Value *
IV = PN.getIncomingValueForBlock(OldPred);
1883 PN.addIncoming(
IV, NewPred);
1899 if (LoopHeaders.erase(SinglePred))
1900 LoopHeaders.insert(BB);
1953 for (
Use &U :
I.uses()) {
1956 if (UserPN->getIncomingBlock(U) == BB)
1958 }
else if (
User->getParent() == BB)
1974 if (UsesToRename.
empty() && DbgValues.
empty() && DbgVariableRecords.
empty())
1976 LLVM_DEBUG(
dbgs() <<
"JT: Renaming non-local uses of: " <<
I <<
"\n");
1985 while (!UsesToRename.
empty())
1987 if (!DbgValues.
empty() || !DbgVariableRecords.
empty()) {
1991 DbgVariableRecords.
clear();
2012 auto RetargetDbgValueIfPossible = [&](
Instruction *NewInst) ->
bool {
2013 auto DbgInstruction = dyn_cast<DbgValueInst>(NewInst);
2014 if (!DbgInstruction)
2018 for (
auto DbgOperand : DbgInstruction->location_ops()) {
2019 auto DbgOperandInstruction = dyn_cast<Instruction>(DbgOperand);
2020 if (!DbgOperandInstruction)
2023 auto I = ValueMapping.
find(DbgOperandInstruction);
2024 if (
I != ValueMapping.
end()) {
2026 std::pair<Value *, Value *>(DbgOperand,
I->second));
2030 for (
auto &[OldOp, MappedOp] : OperandsToRemap)
2031 DbgInstruction->replaceVariableLocationOp(OldOp, MappedOp);
2039 for (
auto *
Op : DVR->location_ops()) {
2044 auto I = ValueMapping.
find(OpInst);
2045 if (
I != ValueMapping.
end())
2046 OperandsToRemap.
insert({OpInst,
I->second});
2049 for (
auto &[OldOp, MappedOp] : OperandsToRemap)
2050 DVR->replaceVariableLocationOp(OldOp, MappedOp);
2058 for (;
PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2061 ValueMapping[PN] = NewPN;
2076 RetargetDbgVariableRecordIfPossible(&DVR);
2082 for (; BI != BE; ++BI) {
2084 New->setName(BI->getName());
2085 New->insertInto(NewBB, NewBB->
end());
2086 ValueMapping[&*BI] = New;
2089 CloneAndRemapDbgInfo(New, &*BI);
2091 if (RetargetDbgValueIfPossible(New))
2095 for (
unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2096 if (
Instruction *Inst = dyn_cast<Instruction>(New->getOperand(i))) {
2098 if (
I != ValueMapping.
end())
2099 New->setOperand(i,
I->second);
2105 if (BE != RangeBB->
end() && BE->hasDbgRecords()) {
2111 RetargetDbgVariableRecordIfPossible(&DVR);
2169 if (LoopHeaders.count(PredBB))
2179 unsigned ZeroCount = 0;
2180 unsigned OneCount = 0;
2186 if (isa<IndirectBrInst>(
P->getTerminator()))
2188 if (
ConstantInt *CI = dyn_cast_or_null<ConstantInt>(
2193 }
else if (CI->isOne()) {
2202 if (ZeroCount == 1) {
2203 PredPredBB = ZeroPred;
2204 }
else if (OneCount == 1) {
2205 PredPredBB = OnePred;
2215 <<
"' - would thread to self!\n");
2221 if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
2223 bool BBIsHeader = LoopHeaders.count(BB);
2224 bool SuccIsHeader = LoopHeaders.count(SuccBB);
2225 dbgs() <<
" Not threading across "
2226 << (BBIsHeader ?
"loop header BB '" :
"block BB '")
2227 << BB->
getName() <<
"' to dest "
2228 << (SuccIsHeader ?
"loop header BB '" :
"block BB '")
2230 <<
"' - it might create an irreducible loop!\n";
2244 if (BBCost > BBDupThreshold || PredBBCost > BBDupThreshold ||
2245 BBCost + PredBBCost > BBDupThreshold) {
2247 <<
"' - Cost is too high: " << PredBBCost
2248 <<
" for PredBB, " << BBCost <<
"for BB\n");
2265 bool HasProfile = doesBlockHaveProfileData(BB);
2266 auto *BFI = getOrCreateBFI(HasProfile);
2267 auto *BPI = getOrCreateBPI(BFI !=
nullptr);
2279 assert(BPI &&
"It's expected BPI to exist along with BFI");
2280 auto NewBBFreq = BFI->getBlockFreq(PredPredBB) *
2281 BPI->getEdgeProbability(PredPredBB, PredBB);
2282 BFI->setBlockFreq(NewBB, NewBBFreq);
2294 BPI->copyEdgeProbabilities(PredBB, NewBB);
2311 DTU->applyUpdatesPermissive(
2336 <<
"' - would thread to self!\n");
2342 if (LoopHeaders.count(BB) || LoopHeaders.count(SuccBB)) {
2344 bool BBIsHeader = LoopHeaders.count(BB);
2345 bool SuccIsHeader = LoopHeaders.count(SuccBB);
2346 dbgs() <<
" Not threading across "
2347 << (BBIsHeader ?
"loop header BB '" :
"block BB '") << BB->
getName()
2348 <<
"' to dest " << (SuccIsHeader ?
"loop header BB '" :
"block BB '")
2349 << SuccBB->
getName() <<
"' - it might create an irreducible loop!\n";
2356 if (JumpThreadCost > BBDupThreshold) {
2358 <<
"' - Cost is too high: " << JumpThreadCost <<
"\n");
2372 assert(SuccBB != BB &&
"Don't create an infinite loop");
2374 assert(!LoopHeaders.count(BB) && !LoopHeaders.count(SuccBB) &&
2375 "Don't thread across loop headers");
2378 bool HasProfile = doesBlockHaveProfileData(BB);
2379 auto *BFI = getOrCreateBFI(HasProfile);
2380 auto *BPI = getOrCreateBPI(BFI !=
nullptr);
2384 if (PredBBs.
size() == 1)
2385 PredBB = PredBBs[0];
2388 <<
" common predecessors.\n");
2389 PredBB = splitBlockPreds(BB, PredBBs,
".thr_comm");
2394 <<
"' to '" << SuccBB->
getName()
2395 <<
", across block:\n " << *BB <<
"\n");
2406 assert(BPI &&
"It's expected BPI to exist along with BFI");
2408 BFI->getBlockFreq(PredBB) * BPI->getEdgeProbability(PredBB, BB);
2409 BFI->setBlockFreq(NewBB, NewBBFreq);
2449 updateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB, BFI, BPI, HasProfile);
2460 const char *Suffix) {
2466 auto *BFI = getBFI();
2468 auto *BPI = getOrCreateBPI(
true);
2469 for (
auto *Pred : Preds)
2470 FreqMap.
insert(std::make_pair(
2471 Pred, BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB)));
2477 std::string NewName = std::string(Suffix) +
".split-lp";
2483 std::vector<DominatorTree::UpdateType> Updates;
2484 Updates.reserve((2 * Preds.size()) + NewBBs.
size());
2485 for (
auto *NewBB : NewBBs) {
2492 NewBBFreq += FreqMap.
lookup(Pred);
2495 BFI->setBlockFreq(NewBB, NewBBFreq);
2498 DTU->applyUpdatesPermissive(Updates);
2502bool JumpThreadingPass::doesBlockHaveProfileData(
BasicBlock *BB) {
2513void JumpThreadingPass::updateBlockFreqAndEdgeWeight(
BasicBlock *PredBB,
2520 assert(((BFI && BPI) || (!BFI && !BFI)) &&
2521 "Both BFI & BPI should either be set or unset");
2525 "It's expected to have BFI/BPI when profile info exists");
2531 auto BBOrigFreq =
BFI->getBlockFreq(BB);
2532 auto NewBBFreq =
BFI->getBlockFreq(NewBB);
2534 auto BBNewFreq = BBOrigFreq - NewBBFreq;
2535 BFI->setBlockFreq(BB, BBNewFreq);
2541 auto SuccFreq = (Succ == SuccBB)
2542 ? BB2SuccBBFreq - NewBBFreq
2544 BBSuccFreq.
push_back(SuccFreq.getFrequency());
2550 if (MaxBBSuccFreq == 0)
2552 {1, static_cast<unsigned>(BBSuccFreq.size())});
2599 if (BBSuccProbs.
size() >= 2 && HasProfile) {
2601 for (
auto Prob : BBSuccProbs)
2616 assert(!PredBBs.
empty() &&
"Can't handle an empty set");
2621 if (LoopHeaders.count(BB)) {
2623 <<
"' into predecessor block '" << PredBBs[0]->getName()
2624 <<
"' - it might create an irreducible loop!\n");
2630 if (DuplicationCost > BBDupThreshold) {
2632 <<
"' - Cost is too high: " << DuplicationCost <<
"\n");
2637 std::vector<DominatorTree::UpdateType> Updates;
2639 if (PredBBs.
size() == 1)
2640 PredBB = PredBBs[0];
2643 <<
" common predecessors.\n");
2644 PredBB = splitBlockPreds(BB, PredBBs,
".thr_comm");
2651 <<
"' into end of '" << PredBB->
getName()
2652 <<
"' to eliminate branch on phi. Cost: "
2653 << 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);
2698 ValueMapping[&*BI] =
IV;
2699 if (!New->mayHaveSideEffects()) {
2700 New->eraseFromParent();
2707 ValueMapping[&*BI] = New;
2711 New->setName(BI->getName());
2713 New->cloneDebugInfoFrom(&*BI);
2715 for (
unsigned i = 0, e = New->getNumOperands(); i != e; ++i)
2716 if (
BasicBlock *SuccBB = dyn_cast<BasicBlock>(New->getOperand(i)))
2737 if (
auto *BPI = getBPI())
2739 DTU->applyUpdatesPermissive(Updates);
2770 BI->applyMergedLocation(PredTerm->
getDebugLoc(), SI->getDebugLoc());
2771 BI->copyMetadata(*SI, {LLVMContext::MD_prof});
2779 (TrueWeight + FalseWeight) != 0) {
2782 TrueWeight, TrueWeight + FalseWeight));
2784 FalseWeight, TrueWeight + FalseWeight));
2786 if (
auto *BPI = getBPI())
2790 if (
auto *BFI = getBFI()) {
2791 if ((TrueWeight + FalseWeight) == 0) {
2796 TrueWeight, TrueWeight + FalseWeight);
2797 auto NewBBFreq = BFI->getBlockFreq(Pred) * PredToNewBBProb;
2798 BFI->setBlockFreq(NewBB, NewBBFreq);
2802 SI->eraseFromParent();
2808 PHINode *Phi = dyn_cast<PHINode>(BI); ++BI)
2810 Phi->addIncoming(Phi->getIncomingValueForBlock(Pred), NewBB);
2814 PHINode *CondPHI = dyn_cast<PHINode>(SI->getCondition());
2816 if (!CondPHI || CondPHI->
getParent() != BB)
2866 if (!SI || SI->getParent() != Pred || !SI->hasOneUse())
2878 CondRHS, Pred, BB, CondCmp);
2881 CondRHS, Pred, BB, CondCmp);
2882 if ((LHSRes || RHSRes) && LHSRes != RHSRes) {
2918 if (LoopHeaders.count(BB))
2922 PHINode *PN = dyn_cast<PHINode>(BI); ++BI) {
2925 [](
Value *V) { return !isa<ConstantInt>(V); }))
2929 using namespace PatternMatch;
2932 if (SI->getParent() != BB)
2936 return Cond &&
Cond == V &&
Cond->getType()->isIntegerTy(1) && !IsAndOr;
2940 for (
Use &U : PN->uses()) {
2941 if (
ICmpInst *Cmp = dyn_cast<ICmpInst>(U.getUser())) {
2944 if (Cmp->getParent() == BB && Cmp->hasOneUse() &&
2945 isa<ConstantInt>(Cmp->getOperand(1 - U.getOperandNo())))
2946 if (
SelectInst *SelectI = dyn_cast<SelectInst>(Cmp->user_back()))
2947 if (isUnfoldCandidate(SelectI, Cmp->use_begin()->get())) {
2951 }
else if (
SelectInst *SelectI = dyn_cast<SelectInst>(U.getUser())) {
2953 if (isUnfoldCandidate(SelectI, U.get())) {
2972 NewPN->
addIncoming(SI->getTrueValue(), Term->getParent());
2975 SI->replaceAllUsesWith(NewPN);
2976 SI->eraseFromParent();
2978 std::vector<DominatorTree::UpdateType> Updates;
2988 DTU->applyUpdatesPermissive(Updates);
3014 using namespace PatternMatch;
3036 if (
auto *BI = dyn_cast<BranchInst>(Parent->getTerminator()))
3057 bool TrueDestIsSafe =
false;
3058 bool FalseDestIsSafe =
false;
3063 TrueDestIsSafe =
true;
3068 FalseDestIsSafe =
true;
3071 if (!TrueDestIsSafe && !FalseDestIsSafe)
3074 BasicBlock *PredUnguardedBlock = TrueDestIsSafe ? TrueDest : FalseDest;
3075 BasicBlock *PredGuardedBlock = FalseDestIsSafe ? TrueDest : FalseDest;
3081 if (
Cost > BBDupThreshold)
3086 BB, PredGuardedBlock, AfterGuard, GuardedMapping, *DTU);
3087 assert(GuardedBlock &&
"Could not create the guarded block?");
3092 BB, PredUnguardedBlock, Guard, UnguardedMapping, *DTU);
3093 assert(UnguardedBlock &&
"Could not create the unguarded block?");
3095 << GuardedBlock->
getName() <<
"\n");
3100 for (
auto BI = BB->
begin(); &*BI != AfterGuard; ++BI)
3101 if (!isa<PHINode>(&*BI))
3108 if (!Inst->use_empty()) {
3110 NewPN->
addIncoming(UnguardedMapping[Inst], UnguardedBlock);
3111 NewPN->
addIncoming(GuardedMapping[Inst], GuardedBlock);
3114 Inst->replaceAllUsesWith(NewPN);
3116 Inst->dropDbgRecords();
3117 Inst->eraseFromParent();
3132template <
typename AnalysisT>
3133typename AnalysisT::Result *JumpThreadingPass::runExternalAnalysis() {
3134 assert(FAM &&
"Can't run external analysis without FunctionAnalysisManager");
3139 if (!ChangedSinceLastAnalysisUpdate) {
3140 assert(!DTU->hasPendingUpdates() &&
3141 "Lost update of 'ChangedSinceLastAnalysisUpdate'?");
3145 ChangedSinceLastAnalysisUpdate =
false;
3147 auto PA = getPreservedAnalysis();
3157 assert(DTU->getDomTree().verify(DominatorTree::VerificationLevel::Fast));
3158 assert((!DTU->hasPostDomTree() ||
3159 DTU->getPostDomTree().verify(
3173 assert(FAM &&
"Can't create BPI without FunctionAnalysisManager");
3181 assert(FAM &&
"Can't create BFI without FunctionAnalysisManager");
3191 auto *Res = getBPI();
3196 BPI = runExternalAnalysis<BranchProbabilityAnalysis>();
3202 auto *Res = getBFI();
3207 BFI = runExternalAnalysis<BlockFrequencyAnalysis>();
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 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
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)
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...
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.
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.
const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
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...
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...
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 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
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.
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="", 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 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 ConstantInt * getTrue(LLVMContext &Context)
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
static ConstantInt * getFalse(LLVMContext &Context)
const APInt & getValue() const
Return the constant as an APInt value reference.
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.
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.
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.
const BasicBlock * getParent() const
This represents the llvm.dbg.value instruction.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
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.
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.
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.
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
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.
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.
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.
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
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.
void setSuccessor(unsigned Idx, BasicBlock *BB)
Update the specified successor to point at the provided block.
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
bool isSpecialTerminator() const
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.
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...
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)
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)
void cloneInstructions(ValueToValueMapTy &ValueMapping, BasicBlock::iterator BI, BasicBlock::iterator BE, BasicBlock *NewBB, BasicBlock *PredBB)
Clone instructions in range [BI, BE) to NewBB.
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)
Constant * evaluateOnPredecessorEdge(BasicBlock *BB, BasicBlock *PredPredBB, Value *cond, const DataLayout &DL)
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.
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 ...
void unfoldSelectInstr(BasicBlock *Pred, BasicBlock *BB, SelectInst *SI, PHINode *SIUse, unsigned Idx)
DomTreeUpdater * getDomTreeUpdater() const
bool processThreadableEdges(Value *Cond, BasicBlock *BB, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
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 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...
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 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)
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 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.
StringRef getName() const
Return a constant reference to the value's name.
void takeName(Value *V)
Transfer the name from V to this value.
const ParentTy * getParent() const
self_iterator getIterator()
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
@ C
The default llvm calling convention, compatible with C.
Function * getDeclarationIfExists(Module *M, ID id, ArrayRef< Type * > Tys, FunctionType *FT=nullptr)
This version supports overloaded intrinsics.
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...
auto pred_end(const MachineBasicBlock *BB)
unsigned replaceNonLocalUsesWith(Instruction *From, Value *To)
auto successors(const MachineBasicBlock *BB)
MDNode * getBranchWeightMDNode(const Instruction &I)
Get the branch weights metadata node.
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...
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...
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)
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.
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 ...
bool hasBranchWeightOrigin(const Instruction &I)
Check if Branch Weight Metadata has an "expected" field from an llvm.expect* intrinsic.
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...
void findDbgValues(SmallVectorImpl< DbgValueInst * > &DbgValues, Value *V, SmallVectorImpl< DbgVariableRecord * > *DbgVariableRecords=nullptr)
Finds the llvm.dbg.value intrinsics describing a value.
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.
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)
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...
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.
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
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().
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...
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...
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...
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...
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 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.
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)
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 ...
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.
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...
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
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:...