43 using namespace jumpthreading;
45 #define DEBUG_TYPE "jump-threading"
47 STATISTIC(NumThreads,
"Number of jumps threaded");
48 STATISTIC(NumFolds,
"Number of terminators folded");
49 STATISTIC(NumDupes,
"Number of branch blocks duplicated to eliminate phi");
53 cl::desc(
"Max block size to duplicate for jump threading"),
58 "jump-threading-implication-search-threshold",
59 cl::desc(
"The number of predecessors to search for a stronger "
60 "condition to use to thread over a weaker condition"),
104 "Jump Threading",
false,
false)
119 bool JumpThreading::runOnFunction(
Function &
F) {
122 auto TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
123 auto LVI = &getAnalysis<LazyValueInfoWrapperPass>().getLVI();
124 std::unique_ptr<BlockFrequencyInfo>
BFI;
125 std::unique_ptr<BranchProbabilityInfo> BPI;
127 if (HasProfileData) {
132 return Impl.runImpl(F, TLI, LVI, HasProfileData, std::move(BFI),
141 std::unique_ptr<BlockFrequencyInfo>
BFI;
142 std::unique_ptr<BranchProbabilityInfo> BPI;
144 if (HasProfileData) {
150 runImpl(F, &TLI, &LVI, HasProfileData, std::move(BFI), std::move(BPI));
165 std::unique_ptr<BlockFrequencyInfo> BFI_,
166 std::unique_ptr<BranchProbabilityInfo> BPI_) {
175 HasProfileData = HasProfileData_;
176 if (HasProfileData) {
177 BPI = std::move(BPI_);
178 BFI = std::move(BFI_);
188 bool EverChanged =
false;
210 LoopHeaders.erase(BB);
232 bool ErasedFromLoopHeaders = LoopHeaders.erase(BB);
247 if (ErasedFromLoopHeaders)
248 LoopHeaders.insert(BB);
251 EverChanged |= Changed;
272 if (isa<SwitchInst>(BBTerm))
276 if (isa<IndirectBrInst>(BBTerm))
286 for (; !isa<TerminatorInst>(
I); ++
I) {
289 if (Size > Threshold)
293 if (isa<DbgInfoIntrinsic>(
I))
continue;
296 if (isa<BitCastInst>(
I) &&
I->getType()->isPointerTy())
301 if (
I->getType()->isTokenTy() &&
I->isUsedOutsideOfBlock(BB))
311 if (
const CallInst *CI = dyn_cast<CallInst>(
I)) {
312 if (CI->cannotDuplicate() || CI->isConvergent())
316 else if (!isa<IntrinsicInst>(CI))
318 else if (!CI->getType()->isVectorTy())
323 return Size > Bonus ? Size - Bonus : 0;
345 for (
const auto &Edge : Edges)
346 LoopHeaders.insert(Edge.second);
359 if (
UndefValue *U = dyn_cast<UndefValue>(Val))
382 if (!RecursionSet.insert(std::make_pair(V, BB)).second)
387 RecursionSetRemover remover(RecursionSet, std::make_pair(V, BB));
392 Result.
push_back(std::make_pair(KC, Pred));
394 return !Result.
empty();
418 Constant *PredCst = LVI->getConstantOnEdge(V,
P, BB, CxtI);
423 return !Result.
empty();
427 if (
PHINode *PN = dyn_cast<PHINode>(I)) {
428 for (
unsigned i = 0, e = PN->getNumIncomingValues();
i != e; ++
i) {
429 Value *InVal = PN->getIncomingValue(
i);
431 Result.
push_back(std::make_pair(KC, PN->getIncomingBlock(
i)));
433 Constant *CI = LVI->getConstantOnEdge(InVal,
434 PN->getIncomingBlock(
i),
437 Result.
push_back(std::make_pair(KC, PN->getIncomingBlock(
i)));
441 return !Result.
empty();
446 if (
CastInst *CI = dyn_cast<CastInst>(I)) {
450 if (!isa<PHINode>(Source) && !isa<CmpInst>(
Source))
452 ComputeValueKnownInPredecessors(Source, BB, Result, Preference, CxtI);
457 for (
auto &R : Result)
472 ComputeValueKnownInPredecessors(I->
getOperand(0), BB, LHSVals,
474 ComputeValueKnownInPredecessors(I->
getOperand(1), BB, RHSVals,
477 if (LHSVals.empty() && RHSVals.empty())
490 for (
const auto &LHSVal : LHSVals)
491 if (LHSVal.first == InterestingVal || isa<UndefValue>(LHSVal.first)) {
493 LHSKnownBBs.insert(LHSVal.second);
495 for (
const auto &RHSVal : RHSVals)
496 if (RHSVal.first == InterestingVal || isa<UndefValue>(RHSVal.first)) {
499 if (!LHSKnownBBs.count(RHSVal.second))
503 return !Result.
empty();
509 cast<ConstantInt>(I->
getOperand(1))->isOne()) {
510 ComputeValueKnownInPredecessors(I->
getOperand(0), BB, Result,
516 for (
auto &R : Result)
525 &&
"A binary operator creating a block address?");
526 if (
ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1))) {
528 ComputeValueKnownInPredecessors(BO->getOperand(0), BB, LHSVals,
532 for (
const auto &LHSVal : LHSVals) {
537 Result.
push_back(std::make_pair(KC, LHSVal.second));
541 return !Result.
empty();
545 if (
CmpInst *Cmp = dyn_cast<CmpInst>(I)) {
559 if (!isa<Constant>(RHS))
563 ResT = LVI->getPredicateOnEdge(Cmp->getPredicate(), LHS,
564 cast<Constant>(RHS), PredBB, BB,
572 Result.
push_back(std::make_pair(KC, PredBB));
575 return !Result.
empty();
580 if (isa<Constant>(Cmp->getOperand(1)) && Cmp->getType()->isIntegerTy()) {
581 if (!isa<Instruction>(Cmp->getOperand(0)) ||
582 cast<Instruction>(Cmp->getOperand(0))->
getParent() != BB) {
583 Constant *RHSCst = cast<Constant>(Cmp->getOperand(1));
589 LVI->getPredicateOnEdge(Cmp->getPredicate(), Cmp->getOperand(0),
590 RHSCst,
P, BB, CxtI ? CxtI : Cmp);
595 Result.
push_back(std::make_pair(ResC, P));
598 return !Result.
empty();
603 if (
Constant *CmpConst = dyn_cast<Constant>(Cmp->getOperand(1))) {
605 ComputeValueKnownInPredecessors(I->
getOperand(0), BB, LHSVals,
608 for (
const auto &LHSVal : LHSVals) {
613 Result.
push_back(std::make_pair(KC, LHSVal.second));
616 return !Result.
empty();
627 if ((TrueVal || FalseVal) &&
628 ComputeValueKnownInPredecessors(
SI->getCondition(), BB, Conds,
630 for (
auto &
C : Conds) {
635 if (
ConstantInt *CI = dyn_cast<ConstantInt>(Cond)) {
637 KnownCond = CI->isOne();
639 assert(isa<UndefValue>(Cond) &&
"Unexpected condition value");
643 KnownCond = (TrueVal !=
nullptr);
647 if (
Constant *Val = KnownCond ? TrueVal : FalseVal)
648 Result.
push_back(std::make_pair(Val,
C.second));
651 return !Result.
empty();
656 Constant *CI = LVI->getConstant(V, BB, CxtI);
659 Result.
push_back(std::make_pair(KC, Pred));
662 return !Result.
empty();
675 unsigned MinSucc = 0;
682 if (NumPreds < MinNumPreds) {
684 MinNumPreds = NumPreds;
719 if (LoopHeaders.erase(SinglePred))
720 LoopHeaders.insert(BB);
722 LVI->eraseBlock(SinglePred);
729 if (TryToUnfoldSelectInCurrBB(BB))
739 if (
BranchInst *BI = dyn_cast<BranchInst>(Terminator)) {
741 if (BI->isUnconditional())
return false;
742 Condition = BI->getCondition();
743 }
else if (
SwitchInst *
SI = dyn_cast<SwitchInst>(Terminator)) {
744 Condition =
SI->getCondition();
745 }
else if (
IndirectBrInst *IB = dyn_cast<IndirectBrInst>(Terminator)) {
747 if (IB->getNumSuccessors() == 0)
return false;
756 if (
Instruction *
I = dyn_cast<Instruction>(Condition)) {
760 I->replaceAllUsesWith(SimpleVal);
762 I->eraseFromParent();
763 Condition = SimpleVal;
769 if (isa<UndefValue>(Condition)) {
775 if (
i == BestSucc)
continue;
780 <<
"' folding undef terminator: " << *BBTerm <<
'\n');
791 <<
"' folding terminator: " << *BB->
getTerminator() <<
'\n');
802 if (ProcessThreadableEdges(Condition, BB, Preference, Terminator))
808 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(CondInst)) {
817 LVI->getPredicateAt(CondCmp->getPredicate(), CondCmp->getOperand(0),
825 if (CondCmp->use_empty())
826 CondCmp->eraseFromParent();
827 else if (CondCmp->getParent() == BB) {
834 CondCmp->eraseFromParent();
840 if (CondBr && CondConst && TryToUnfoldSelect(CondCmp, BB))
849 Value *SimplifyValue = CondInst;
850 if (
CmpInst *CondCmp = dyn_cast<CmpInst>(SimplifyValue))
851 if (isa<Constant>(CondCmp->getOperand(1)))
852 SimplifyValue = CondCmp->getOperand(0);
856 if (
LoadInst *LI = dyn_cast<LoadInst>(SimplifyValue))
857 if (SimplifyPartiallyRedundantLoad(LI))
865 if (ProcessThreadableEdges(CondInst, BB, Preference, Terminator))
870 if (
PHINode *PN = dyn_cast<PHINode>(CondInst))
871 if (PN->getParent() == BB && isa<BranchInst>(BB->
getTerminator()))
872 return ProcessBranchOnPHI(PN);
878 return ProcessBranchOnXOR(cast<BinaryOperator>(CondInst));
882 if (ProcessImpliedCondition(BB))
890 if (!BI || !BI->isConditional())
893 Value *Cond = BI->getCondition();
902 if (!PBI || !PBI->isConditional())
904 if (PBI->getSuccessor(0) != CurrentBB && PBI->getSuccessor(1) != CurrentBB)
911 BI->getSuccessor(*Implication ? 1 : 0)->removePredecessor(BB);
916 CurrentBB = CurrentPred;
947 if (
Instruction *PtrOp = dyn_cast<Instruction>(LoadedPtr))
948 if (PtrOp->getParent() == LoadBB)
955 if (
Value *AvailableVal =
961 LoadInst *NLI = cast<LoadInst>(AvailableVal);
968 if (AvailableVal->getType() != LI->
getType())
979 if (BBIt != LoadBB->
begin())
989 AvailablePredsTy AvailablePreds;
997 if (!PredsScanned.
insert(PredBB).second)
1001 BBIt = PredBB->end();
1006 if (!PredAvailable) {
1007 OneUnavailablePred = PredBB;
1012 CSELoads.
push_back(cast<LoadInst>(PredAvailable));
1016 AvailablePreds.push_back(std::make_pair(PredBB, PredAvailable));
1021 if (AvailablePreds.empty())
return false;
1033 if (PredsScanned.
size() == AvailablePreds.size()+1 &&
1035 UnavailablePred = OneUnavailablePred;
1036 }
else if (PredsScanned.
size() != AvailablePreds.size()) {
1042 for (
const auto &AvailablePred : AvailablePreds)
1043 AvailablePredSet.
insert(AvailablePred.first);
1048 if (isa<IndirectBrInst>(
P->getTerminator()))
1051 if (!AvailablePredSet.
count(
P))
1056 UnavailablePred = SplitBlockPreds(LoadBB, PredsToSplit,
"thread-pre-split");
1062 if (UnavailablePred) {
1064 "Can't handle critical edge here!");
1071 NewVal->setAAMetadata(AATags);
1073 AvailablePreds.push_back(std::make_pair(UnavailablePred, NewVal));
1091 AvailablePredsTy::iterator
I =
1092 std::lower_bound(AvailablePreds.begin(), AvailablePreds.end(),
1093 std::make_pair(P, (
Value*)
nullptr));
1095 assert(I != AvailablePreds.end() && I->first == P &&
1096 "Didn't find entry for predecessor!");
1102 Value *&PredV = I->second;
1103 if (PredV->getType() != LI->
getType())
1110 for (
LoadInst *PredLI : CSELoads) {
1127 assert(!PredToDestList.empty());
1134 for (
const auto &PredToDest : PredToDestList)
1135 if (PredToDest.second)
1136 DestPopularity[PredToDest.second]++;
1141 unsigned Popularity = DPI->second;
1144 for (++DPI; DPI != DestPopularity.
end(); ++DPI) {
1147 if (DPI->second < Popularity)
1149 else if (DPI->second == Popularity) {
1154 SamePopularity.
clear();
1155 MostPopularDest = DPI->first;
1156 Popularity = DPI->second;
1164 if (!SamePopularity.
empty()) {
1165 SamePopularity.
push_back(MostPopularDest);
1167 for (
unsigned i = 0; ; ++
i) {
1179 return MostPopularDest;
1187 if (LoopHeaders.count(BB))
1191 if (!ComputeValueKnownInPredecessors(Cond, BB, PredValues, Preference, CxtI))
1195 "ComputeValueKnownInPredecessors returned true with no values");
1198 for (
const auto &PredValue : PredValues) {
1199 dbgs() <<
" BB '" << BB->
getName() <<
"': FOUND condition = "
1201 <<
" for pred '" << PredValue.second->getName() <<
"'.\n";
1214 for (
const auto &PredValue : PredValues) {
1216 if (!SeenPreds.insert(Pred).second)
1227 if (isa<UndefValue>(Val))
1230 DestBB = BI->getSuccessor(cast<ConstantInt>(Val)->
isZero());
1232 DestBB =
SI->findCaseValue(cast<ConstantInt>(Val)).getCaseSuccessor();
1235 &&
"Unexpected terminator");
1236 DestBB = cast<BlockAddress>(Val)->getBasicBlock();
1240 if (PredToDestList.
empty())
1242 else if (OnlyDest != DestBB)
1243 OnlyDest = MultipleDestSentinel;
1245 PredToDestList.
push_back(std::make_pair(Pred, DestBB));
1249 if (PredToDestList.
empty())
1258 if (MostPopularDest == MultipleDestSentinel)
1264 for (
const auto &PredToDest : PredToDestList)
1265 if (PredToDest.second == MostPopularDest) {
1278 if (!MostPopularDest)
1283 return ThreadEdge(BB, PredsToFactor, MostPopularDest);
1305 if (PredBr->isUnconditional()) {
1306 PredBBs[0] = PredBB;
1308 if (DuplicateCondBranchOnPHIIntoPred(BB, PredBBs))
1331 if (!isa<PHINode>(BB->
front()))
1358 if (!ComputeValueKnownInPredecessors(BO->
getOperand(0), BB, XorOpValues,
1360 assert(XorOpValues.empty());
1361 if (!ComputeValueKnownInPredecessors(BO->
getOperand(1), BB, XorOpValues,
1367 assert(!XorOpValues.empty() &&
1368 "ComputeValueKnownInPredecessors returned true with no values");
1372 unsigned NumTrue = 0, NumFalse = 0;
1373 for (
const auto &XorOpValue : XorOpValues) {
1374 if (isa<UndefValue>(XorOpValue.first))
1377 if (cast<ConstantInt>(XorOpValue.first)->isZero())
1385 if (NumTrue > NumFalse)
1387 else if (NumTrue != 0 || NumFalse != 0)
1393 for (
const auto &XorOpValue : XorOpValues) {
1394 if (XorOpValue.first != SplitVal && !isa<UndefValue>(XorOpValue.first))
1397 BlocksToFoldInto.
push_back(XorOpValue.second);
1402 if (BlocksToFoldInto.size() ==
1403 cast<PHINode>(BB->
front()).getNumIncomingValues()) {
1408 }
else if (SplitVal->
isZero()) {
1421 return DuplicateCondBranchOnPHIIntoPred(BB, BlocksToFoldInto);
1439 if (
Instruction *Inst = dyn_cast<Instruction>(IV)) {
1441 if (I != ValueMap.
end())
1458 <<
"' - would thread to self!\n");
1464 if (LoopHeaders.count(BB)) {
1466 <<
"' to dest BB '" << SuccBB->
getName()
1467 <<
"' - it might create an irreducible loop!\n");
1472 if (JumpThreadCost > BBDupThreshold) {
1474 <<
"' - Cost is too high: " << JumpThreadCost <<
"\n");
1480 if (PredBBs.
size() == 1)
1481 PredBB = PredBBs[0];
1484 <<
" common predecessors.\n");
1485 PredBB = SplitBlockPreds(BB, PredBBs,
".thr_comm");
1490 << SuccBB->
getName() <<
"' with cost: " << JumpThreadCost
1491 <<
", across block:\n "
1494 LVI->threadEdge(PredBB, BB, SuccBB);
1507 if (HasProfileData) {
1509 BFI->getBlockFreq(PredBB) * BPI->getEdgeProbability(PredBB, BB);
1510 BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency());
1519 for (; !isa<TerminatorInst>(BI); ++BI) {
1523 ValueMapping[&*BI] = New;
1529 if (I != ValueMapping.
end())
1552 for (
Use &U :
I.uses()) {
1554 if (
PHINode *UserPN = dyn_cast<PHINode>(User)) {
1555 if (UserPN->getIncomingBlock(U) == BB)
1564 if (UsesToRename.
empty())
1567 DEBUG(
dbgs() <<
"JT: Renaming non-local uses of: " <<
I <<
"\n");
1576 while (!UsesToRename.
empty())
1588 BB->removePredecessor(PredBB,
true);
1598 UpdateBlockFreqAndEdgeWeight(PredBB, BB, NewBB, SuccBB);
1610 const char *Suffix) {
1615 for (
auto Pred : Preds)
1616 PredBBFreq += BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, BB);
1623 BFI->setBlockFreq(PredBB, PredBBFreq.getFrequency());
1627 bool JumpThreadingPass::doesBlockHaveProfileData(
BasicBlock *BB) {
1635 MDString *MDName = cast<MDString>(WeightsNode->getOperand(0));
1636 if (MDName->
getString() !=
"branch_weights")
1647 void JumpThreadingPass::UpdateBlockFreqAndEdgeWeight(
BasicBlock *PredBB,
1651 if (!HasProfileData)
1654 assert(BFI && BPI &&
"BFI & BPI should have been created here");
1658 auto BBOrigFreq = BFI->getBlockFreq(BB);
1659 auto NewBBFreq = BFI->getBlockFreq(NewBB);
1660 auto BB2SuccBBFreq = BBOrigFreq * BPI->getEdgeProbability(BB, SuccBB);
1661 auto BBNewFreq = BBOrigFreq - NewBBFreq;
1662 BFI->setBlockFreq(BB, BBNewFreq.getFrequency());
1668 auto SuccFreq = (Succ == SuccBB)
1669 ? BB2SuccBBFreq - NewBBFreq
1670 : BBOrigFreq * BPI->getEdgeProbability(BB, Succ);
1671 BBSuccFreq.
push_back(SuccFreq.getFrequency());
1674 uint64_t MaxBBSuccFreq =
1675 *std::max_element(BBSuccFreq.
begin(), BBSuccFreq.
end());
1678 if (MaxBBSuccFreq == 0)
1680 {1,
static_cast<unsigned>(BBSuccFreq.
size())});
1682 for (uint64_t Freq : BBSuccFreq)
1683 BBSuccProbs.push_back(
1691 for (
int I = 0,
E = BBSuccProbs.size();
I <
E;
I++)
1692 BPI->setEdgeProbability(BB,
I, BBSuccProbs[
I]);
1728 if (BBSuccProbs.size() >= 2 && doesBlockHaveProfileData(BB)) {
1730 for (
auto Prob : BBSuccProbs)
1747 assert(!PredBBs.
empty() &&
"Can't handle an empty set");
1752 if (LoopHeaders.count(BB)) {
1754 <<
"' into predecessor block '" << PredBBs[0]->getName()
1755 <<
"' - it might create an irreducible loop!\n");
1760 if (DuplicationCost > BBDupThreshold) {
1762 <<
"' - Cost is too high: " << DuplicationCost <<
"\n");
1768 if (PredBBs.
size() == 1)
1769 PredBB = PredBBs[0];
1772 <<
" common predecessors.\n");
1773 PredBB = SplitBlockPreds(BB, PredBBs,
".thr_comm");
1778 DEBUG(
dbgs() <<
" Duplicating block '" << BB->
getName() <<
"' into end of '"
1779 << PredBB->
getName() <<
"' to eliminate branch on phi. Cost: "
1780 << DuplicationCost <<
" block is:" << *BB <<
"\n");
1786 if (!OldPredBranch || !OldPredBranch->isUnconditional()) {
1800 for (; BI != BB->
end(); ++BI) {
1807 if (I != ValueMapping.
end())
1816 ValueMapping[&*BI] = IV;
1822 ValueMapping[&*BI] = New;
1848 for (
Use &U : I.uses()) {
1850 if (
PHINode *UserPN = dyn_cast<PHINode>(User)) {
1851 if (UserPN->getIncomingBlock(U) == BB)
1860 if (UsesToRename.
empty())
1863 DEBUG(
dbgs() <<
"JT: Renaming non-local uses of: " << I <<
"\n");
1868 SSAUpdate.Initialize(I.getType(), I.getName());
1869 SSAUpdate.AddAvailableValue(BB, &I);
1870 SSAUpdate.AddAvailableValue(PredBB, ValueMapping[&I]);
1872 while (!UsesToRename.
empty())
1879 BB->removePredecessor(PredBB,
true);
1882 OldPredBranch->eraseFromParent();
1906 CondLHS->getParent() != BB)
1909 for (
unsigned I = 0, E = CondLHS->getNumIncomingValues(); I !=
E; ++
I) {
1910 BasicBlock *Pred = CondLHS->getIncomingBlock(I);
1927 CondRHS, Pred, BB, CondCmp);
1930 CondRHS, Pred, BB, CondCmp);
1933 LHSFolds != RHSFolds) {
1944 BB->getParent(), BB);
1981 if (LoopHeaders.count(BB))
1990 if (NumPHIValues == 0 || !PN->
hasOneUse())
2001 bool HasConst =
false;
2002 for (
unsigned i = 0;
i != NumPHIValues; ++
i) {
Legacy wrapper pass to provide the GlobalsAAResult object.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void push_back(const T &Elt)
A parsed version of the target data layout string in and methods for querying it. ...
static ConstantInt * getFalse(LLVMContext &Context)
This class is the base class for the comparison instructions.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void removePredecessor(BasicBlock *Pred, bool DontDeleteUselessPHIs=false)
Notify the BasicBlock that the predecessor Pred is no longer able to reach it.
static IntegerType * getInt1Ty(LLVMContext &C)
Helper class for SSA formation on a set of values defined in multiple blocks.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
bool 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...
Wrapper around LazyValueInfo.
This is the interface for a simple mod/ref and alias analysis over globals.
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
virtual void releaseMemory()
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void initializeJumpThreadingPass(PassRegistry &)
unsigned getNumOperands() const
void DeleteDeadBlock(BasicBlock *BB)
Delete the specified block, which must have no predecessors.
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value...
This class represents a function call, abstracting a target machine's calling convention.
cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
size_type count(PtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void MergeBasicBlockIntoOnlyPred(BasicBlock *BB, DominatorTree *DT=nullptr)
BB is a block with one predecessor and its predecessor is known to have one successor (BB!)...
bool mayHaveSideEffects() const
Return true if the instruction may have side effects.
static CastInst * CreateBitOrPointerCast(Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Create a BitCast, a PtrToInt, or an IntToPTr cast instruction.
bool isEHPad() const
Return true if this basic block is an exception handling block.
const Function * getParent() const
Return the enclosing method, or null if none.
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...
const Instruction & front() const
An instruction for reading from memory.
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
INITIALIZE_PASS_BEGIN(JumpThreading,"jump-threading","Jump Threading", false, false) INITIALIZE_PASS_END(JumpThreading
FunctionPass * createJumpThreadingPass(int Threshold=-1)
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
Instruction * getFirstNonPHIOrDbg()
Returns a pointer to the first instruction in this block that is not a PHINode or a debug intrinsic...
The address of a basic block.
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
bool isUnconditional() const
static void AddPHINodeEntriesForMappedBlock(BasicBlock *PHIBB, BasicBlock *OldPred, BasicBlock *NewPred, DenseMap< Instruction *, Value * > &ValueMap)
AddPHINodeEntriesForMappedBlock - We're adding 'NewPred' as a new predecessor to the PHIBB block...
This class represents the LLVM 'select' instruction.
This is the base class for all instructions that perform data casts.
static cl::opt< unsigned > BBDuplicateThreshold("jump-threading-threshold", cl::desc("Max block size to duplicate for jump threading"), cl::init(6), cl::Hidden)
'undef' values are things that do not have specified contents.
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr it the function does no...
A Use represents the edge between a Value definition and its users.
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=nullptr)
If a terminator instruction is predicated on a constant value, convert it into an unconditional branc...
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc.
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)
void setName(const Twine &Name)
Change the name of the value.
bool TryToUnfoldSelectInCurrBB(BasicBlock *BB)
TryToUnfoldSelectInCurrBB - Look for PHI/Select in the same BB of the form bb: p = phi [false...
SynchronizationScope getSynchScope() const
LLVM_NODISCARD bool empty() const
void assign(size_type NumElts, const T &Elt)
static Constant * get(unsigned Opcode, Constant *C1, Constant *C2, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a binary or shift operator constant expression, folding if possible. ...
static void normalizeProbabilities(ProbabilityIter Begin, ProbabilityIter End)
BasicBlock * getSuccessor(unsigned i) const
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void setSuccessor(unsigned idx, BasicBlock *B)
Update the specified successor to point at the provided block.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static bool ProcessBlock(BasicBlock &BB, DominatorTree &DT, LoopInfo &LI, AAResults &AA)
Optional< uint64_t > getEntryCount() const
Get the entry count for this function.
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AliasAnalysis *AA=nullptr, bool *IsLoadCSE=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
void takeName(Value *V)
Transfer the name from V to this value.
static Constant * getKnownConstant(Value *Val, ConstantPreference Preference)
getKnownConstant - Helper method to determine if we can thread over a terminator with the given value...
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
unsigned getNumIncomingValues() const
Return the number of incoming edges.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
initializer< Ty > init(const Ty &Val)
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Subclasses of this class are all able to terminate a basic block.
A set of analyses that are preserved following a run of a transformation pass.
void setDebugLoc(DebugLoc Loc)
Set the debug location information for this instruction.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs...ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
bool TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB)
BB is known to contain an unconditional branch, and contains no instructions other than PHI nodes...
Conditional or Unconditional Branch instruction.
static BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
This is an important base class in LLVM.
const Value * getCondition() const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Indirect Branch Instruction.
bool DuplicateCondBranchOnPHIIntoPred(BasicBlock *BB, const SmallVectorImpl< BasicBlock * > &PredBBs)
DuplicateCondBranchOnPHIIntoPred - PredBB contains an unconditional branch to BB which contains an i1...
APInt Or(const APInt &LHS, const APInt &RHS)
Bitwise OR function for APInt.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
APInt Xor(const APInt &LHS, const APInt &RHS)
Bitwise XOR function for APInt.
Constant * ConstantFoldInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldInstruction - Try to constant fold the specified instruction.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Represent the analysis usage information of a pass.
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const InstListType & getInstList() const
Return the underlying instruction list container.
Analysis pass providing a never-invalidated alias analysis result.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
JumpThreadingPass(int T=-1)
FunctionPass class - This class is used to implement most global optimizations.
Value * getOperand(unsigned i) const
Interval::pred_iterator pred_end(Interval *I)
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Predicate getPredicate() const
Return the predicate for this instruction.
bool pred_empty(const BasicBlock *BB)
Optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool InvertAPred=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Return true if RHS is known to be implied true by LHS.
static Constant * getNot(Constant *C)
void FindLoopHeaders(Function &F)
FindLoopHeaders - We do not want jump threading to turn proper loop structures into irreducible loops...
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
LLVMContext & getContext() const
All values hold a context through their type.
const Value * getTrueValue() const
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
static unsigned GetBestDestForJumpOnUndef(BasicBlock *BB)
GetBestDestForBranchOnUndef - If we determine that the specified block ends in an undefined jump...
Tristate
This is used to return true/false/dunno results.
bool isTerminator() const
bool ProcessBranchOnPHI(PHINode *PN)
ProcessBranchOnPHI - We have an otherwise unthreadable conditional branch on a PHI node in the curren...
bool isConditional() const
bool ComputeValueKnownInPredecessors(Value *V, BasicBlock *BB, jumpthreading::PredValueInfo &Result, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
ComputeValueKnownInPredecessors - Given a basic block BB and a value V, see if we can infer that the ...
StringRef getString() const
Iterator for intrusive lists based on ilist_node.
void moveAfter(BasicBlock *MovePos)
Unlink this basic block from its current function and insert it right after MovePos in the function M...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
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...
This is the shared class of boolean and integer constants.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB)
Translate PHI node to its predecessor from the given basic block.
bool removeUnreachableBlocks(Function &F, LazyValueInfo *LVI=nullptr)
Remove all blocks that can not be reached from the function's entry.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Type * getType() const
All values are typed, get the type of this value.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Provides information about what library functions are available for the current target.
bool runImpl(Function &F, TargetLibraryInfo *TLI_, LazyValueInfo *LVI_, bool HasProfileData_, std::unique_ptr< BlockFrequencyInfo > BFI_, std::unique_ptr< BranchProbabilityInfo > BPI_)
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
This pass performs 'jump threading', which looks at blocks that have multiple predecessors and multip...
Value * SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr)
Given operands for a CmpInst, fold the result or return null.
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
TerminatorInst * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
LLVM_NODISCARD T pop_back_val()
void invalidate(IRUnitT &IR)
Invalidate a specific analysis pass for an IR module.
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
bool SimplifyPartiallyRedundantLoad(LoadInst *LI)
SimplifyPartiallyRedundantLoad - If LI is an obviously partially redundant load instruction, eliminate it by replacing it with a PHI node.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
AtomicOrdering getOrdering() const
Returns the ordering effect of this fence.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
pred_range predecessors(BasicBlock *BB)
const BasicBlock & getEntryBlock() const
static ConstantInt * getTrue(LLVMContext &Context)
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void setOperand(unsigned i, Value *Val)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static unsigned getJumpThreadDuplicationCost(const BasicBlock *BB, unsigned Threshold)
getJumpThreadDuplicationCost - Return the cost of duplicating this block to thread across it...
void push_back(pointer val)
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool isIntegerTy() const
True if this is an instance of IntegerType.
BasicBlock * getSinglePredecessor()
Return the predecessor of this block if it has a single predecessor block.
static Constant * getCast(unsigned ops, Constant *C, Type *Ty, bool OnlyIfReduced=false)
Convenience function for getting a Cast operation.
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
void removeFromParent()
This method unlinks 'this' from the containing basic block, but does not delete it.
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Analysis providing branch probability information.
iterator insert(iterator where, pointer New)
void emplace_back(ArgTypes &&...Args)
bool ProcessBranchOnXOR(BinaryOperator *BO)
ProcessBranchOnXOR - We have an otherwise unthreadable conditional branch on a xor instruction in the...
unsigned getAlignment() const
Return the alignment of the access that is being performed.
void getAAMetadata(AAMDNodes &N, bool Merge=false) const
Fills the AAMDNodes structure with AA metadata from this instruction.
bool ProcessImpliedCondition(BasicBlock *BB)
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
bool hasOneUse() const
Return true if there is exactly one user of this value.
iterator find(const KeyT &Val)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void preserve()
Mark an analysis as preserved.
void combineMetadataForCSE(Instruction *K, const Instruction *J)
Combine the metadata of two instructions so that K can replace J.
bool ProcessThreadableEdges(Value *Cond, BasicBlock *BB, jumpthreading::ConstantPreference Preference, Instruction *CxtI=nullptr)
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
Analysis pass providing the TargetLibraryInfo.
static int const Threshold
TODO: Write a new FunctionPass AliasAnalysis so that it can keep a cache.
Helper struct that represents how a value is mapped through different register banks.
This pass computes, caches, and vends lazy value constraint information.
static BasicBlock * FindMostPopularDest(BasicBlock *BB, const SmallVectorImpl< std::pair< BasicBlock *, BasicBlock * > > &PredToDestList)
FindMostPopularDest - The specified list contains multiple possible threadable destinations.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
See the comments on JumpThreadingPass.
LLVMContext & getContext() const
Get the context in which this basic block lives.
void removeDeadConstantUsers() const
If there are any dead constant users dangling off of this constant, remove them.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction has no side ef...
LLVM Value Representation.
succ_range successors(BasicBlock *BB)
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool ProcessBlock(BasicBlock *BB)
ProcessBlock - If there are any predecessors whose control can be threaded through to a successor...
static const Function * getParent(const Value *V)
BasicBlock * SplitEdge(BasicBlock *From, BasicBlock *To, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr)
Split the edge connecting specified block.
bool isExceptional() const
const Value * getFalseValue() const
A container for analyses that lazily runs them and caches their results.
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
See if we can compute a simplified version of this instruction.
void RewriteUse(Use &U)
Rewrite a use of the symbolic value.
static bool hasAddressTakenAndUsed(BasicBlock *BB)
const BasicBlock * getParent() const
Analysis to compute lazy value information.
bool TryToUnfoldSelect(CmpInst *CondCmp, BasicBlock *BB)
TryToUnfoldSelect - Look for blocks of the form bb1: a = select br bb.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.