51#define DEBUG_TYPE "branch-prob"
55 cl::desc(
"Print the branch probability info."));
59 cl::desc(
"The option to specify the name of the function "
60 "whose branch probability info is printed."));
63 "Branch Probability Analysis",
false,
true)
222 const std::vector<const BasicBlock *> &Scc = *It;
227 for (
const auto *BB : Scc) {
229 SccNums[BB] = SccNum;
230 calculateSccBlockType(BB, SccNum);
237 auto SccIt = SccNums.find(BB);
238 if (SccIt == SccNums.end())
240 return SccIt->second;
246 for (
auto MapIt : SccBlocks[SccNum]) {
247 const auto *BB = MapIt.first;
257 for (
auto MapIt : SccBlocks[SccNum]) {
258 const auto *BB = MapIt.first;
268 assert(getSCCNum(BB) == SccNum);
270 assert(SccBlocks.size() >
static_cast<unsigned>(SccNum) &&
"Unknown SCC");
271 const auto &SccBlockTypes = SccBlocks[SccNum];
273 auto It = SccBlockTypes.find(BB);
274 if (It != SccBlockTypes.end()) {
280void BranchProbabilityInfo::SccInfo::calculateSccBlockType(
const BasicBlock *BB,
282 assert(getSCCNum(BB) == SccNum);
288 return getSCCNum(Pred) != SccNum;
293 return getSCCNum(Succ) != SccNum;
295 BlockType |= Exiting;
299 if (SccBlocks.size() <=
static_cast<unsigned>(SccNum))
300 SccBlocks.resize(SccNum + 1);
301 auto &SccBlockTypes = SccBlocks[SccNum];
303 if (BlockType != Inner) {
305 std::tie(std::ignore, IsInserted) =
306 SccBlockTypes.insert(std::make_pair(BB, BlockType));
307 assert(IsInserted &&
"Duplicated block in SCC");
311BranchProbabilityInfo::LoopBlock::LoopBlock(
const BasicBlock *BB,
317 LD.second = SccI.getSCCNum(BB);
321bool BranchProbabilityInfo::isLoopEnteringEdge(
const LoopEdge &Edge)
const {
322 const auto &SrcBlock = Edge.first;
323 const auto &DstBlock = Edge.second;
324 return (DstBlock.getLoop() &&
325 !DstBlock.getLoop()->contains(SrcBlock.getLoop())) ||
327 (DstBlock.getSccNum() != -1 &&
328 SrcBlock.getSccNum() != DstBlock.getSccNum());
331bool BranchProbabilityInfo::isLoopExitingEdge(
const LoopEdge &Edge)
const {
332 return isLoopEnteringEdge({Edge.second, Edge.first});
335bool BranchProbabilityInfo::isLoopEnteringExitingEdge(
336 const LoopEdge &Edge)
const {
337 return isLoopEnteringEdge(Edge) || isLoopExitingEdge(Edge);
340bool BranchProbabilityInfo::isLoopBackEdge(
const LoopEdge &Edge)
const {
341 const auto &SrcBlock = Edge.first;
342 const auto &DstBlock = Edge.second;
343 return SrcBlock.belongsToSameLoop(DstBlock) &&
344 ((DstBlock.getLoop() &&
345 DstBlock.getLoop()->getHeader() == DstBlock.getBlock()) ||
346 (DstBlock.getSccNum() != -1 &&
347 SccI->isSCCHeader(DstBlock.getBlock(), DstBlock.getSccNum())));
350void BranchProbabilityInfo::getLoopEnterBlocks(
351 const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Enters)
const {
353 auto *Header = LB.getLoop()->getHeader();
356 assert(LB.getSccNum() != -1 &&
"LB doesn't belong to any loop?");
357 SccI->getSccEnterBlocks(LB.getSccNum(), Enters);
361void BranchProbabilityInfo::getLoopExitBlocks(
362 const LoopBlock &LB, SmallVectorImpl<BasicBlock *> &Exits)
const {
364 LB.getLoop()->getExitBlocks(Exits);
366 assert(LB.getSccNum() != -1 &&
"LB doesn't belong to any loop?");
367 SccI->getSccExitBlocks(LB.getSccNum(), Exits);
375bool BranchProbabilityInfo::calcMetadataWeights(
const BasicBlock *BB) {
392 uint64_t WeightSum = 0;
394 SmallVector<unsigned, 2> UnreachableIdxs;
395 SmallVector<unsigned, 2> ReachableIdxs;
398 for (
unsigned I = 0,
E = Weights.
size();
I !=
E; ++
I) {
399 WeightSum += Weights[
I];
400 const LoopBlock SrcLoopBB = getLoopBlock(BB);
401 const LoopBlock DstLoopBB = getLoopBlock(TI->
getSuccessor(
I));
402 auto EstimatedWeight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
403 if (EstimatedWeight &&
413 uint64_t ScalingFactor =
414 (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;
416 if (ScalingFactor > 1) {
419 Weights[
I] /= ScalingFactor;
420 WeightSum += Weights[
I];
423 assert(WeightSum <= UINT32_MAX &&
424 "Expected weights to scale down to 32 bits");
426 if (WeightSum == 0 || ReachableIdxs.
size() == 0) {
435 BP.
push_back({ Weights[
I],
static_cast<uint32_t
>(WeightSum) });
439 if (UnreachableIdxs.
size() == 0 || ReachableIdxs.
size() == 0) {
445 for (
auto I : UnreachableIdxs)
446 if (UnreachableProb < BP[
I]) {
447 BP[
I] = UnreachableProb;
471 for (
auto I : UnreachableIdxs)
472 NewUnreachableSum += BP[
I];
474 BranchProbability NewReachableSum =
478 for (
auto I : ReachableIdxs)
479 OldReachableSum += BP[
I];
481 if (OldReachableSum != NewReachableSum) {
482 if (OldReachableSum.
isZero()) {
486 BranchProbability PerEdge = NewReachableSum / ReachableIdxs.size();
487 for (
auto I : ReachableIdxs)
490 for (
auto I : ReachableIdxs) {
496 BP[
I].getNumerator();
497 uint32_t Div =
static_cast<uint32_t
>(
511bool BranchProbabilityInfo::calcPointerHeuristics(
const BasicBlock *BB) {
583 if (!L->contains(CmpLHS))
590 if (!CmpPHI || !L->contains(CmpPHI))
597 VisitedInsts.
insert(CmpPHI);
598 while (!WorkList.
empty()) {
604 Value *V =
P->getIncomingValueForBlock(
B);
608 if (VisitedInsts.
insert(PN).second)
641std::optional<uint32_t>
642BranchProbabilityInfo::getEstimatedBlockWeight(
const BasicBlock *BB)
const {
643 auto WeightIt = EstimatedBlockWeight.find(BB);
644 if (WeightIt == EstimatedBlockWeight.end())
646 return WeightIt->second;
649std::optional<uint32_t>
650BranchProbabilityInfo::getEstimatedLoopWeight(
const LoopData &L)
const {
651 auto WeightIt = EstimatedLoopWeight.find(L);
652 if (WeightIt == EstimatedLoopWeight.end())
654 return WeightIt->second;
657std::optional<uint32_t>
658BranchProbabilityInfo::getEstimatedEdgeWeight(
const LoopEdge &Edge)
const {
661 return isLoopEnteringEdge(Edge)
662 ? getEstimatedLoopWeight(Edge.second.getLoopData())
663 : getEstimatedBlockWeight(Edge.second.getBlock());
666template <
class IterT>
667std::optional<uint32_t> BranchProbabilityInfo::getMaxEstimatedEdgeWeight(
669 std::optional<uint32_t> MaxWeight;
670 for (
const BasicBlock *DstBB : Successors) {
671 const LoopBlock DstLoopBB = getLoopBlock(DstBB);
672 auto Weight = getEstimatedEdgeWeight({SrcLoopBB, DstLoopBB});
677 if (!MaxWeight || *MaxWeight < *Weight)
689bool BranchProbabilityInfo::updateEstimatedBlockWeight(
690 LoopBlock &LoopBB, uint32_t BBWeight,
691 SmallVectorImpl<BasicBlock *> &BlockWorkList,
692 SmallVectorImpl<LoopBlock> &LoopWorkList) {
700 if (!EstimatedBlockWeight.insert({BB, BBWeight}).second)
704 LoopBlock PredLoop = getLoopBlock(PredBlock);
706 if (isLoopExitingEdge({PredLoop, LoopBB})) {
707 if (!EstimatedLoopWeight.count(PredLoop.getLoopData()))
709 }
else if (!EstimatedBlockWeight.count(PredBlock))
727void BranchProbabilityInfo::propagateEstimatedBlockWeight(
728 const LoopBlock &LoopBB, DominatorTree *DT, PostDominatorTree *PDT,
729 uint32_t BBWeight, SmallVectorImpl<BasicBlock *> &BlockWorkList,
730 SmallVectorImpl<LoopBlock> &LoopWorkList) {
732 const auto *DTStartNode = DT->
getNode(BB);
733 const auto *PDTStartNode = PDT->
getNode(BB);
736 for (
const auto *DTNode = DTStartNode; DTNode !=
nullptr;
737 DTNode = DTNode->getIDom()) {
738 auto *DomBB = DTNode->getBlock();
745 LoopBlock DomLoopBB = getLoopBlock(DomBB);
746 const LoopEdge Edge{DomLoopBB, LoopBB};
748 if (!isLoopEnteringExitingEdge(Edge)) {
749 if (!updateEstimatedBlockWeight(DomLoopBB, BBWeight, BlockWorkList,
754 }
else if (isLoopExitingEdge(Edge)) {
760std::optional<uint32_t>
761BranchProbabilityInfo::getInitialEstimatedBlockWeight(
const BasicBlock *BB) {
763 auto hasNoReturn = [&](
const BasicBlock *BB) {
766 if (CI->hasFnAttr(Attribute::NoReturn))
781 return hasNoReturn(BB)
790 for (
const auto &
I : *BB)
792 if (CI->hasFnAttr(Attribute::Cold))
801void BranchProbabilityInfo::estimateBlockWeights(
const Function &
F,
803 PostDominatorTree *PDT) {
804 SmallVector<BasicBlock *, 8> BlockWorkList;
806 SmallDenseMap<LoopData, SmallVector<BasicBlock *, 4>> LoopExitBlocks;
810 ReversePostOrderTraversal<const Function *> RPOT(&
F);
811 for (
const auto *BB : RPOT)
812 if (
auto BBWeight = getInitialEstimatedBlockWeight(BB))
815 propagateEstimatedBlockWeight(getLoopBlock(BB), DT, PDT, *BBWeight,
816 BlockWorkList, LoopWorkList);
823 while (!LoopWorkList.
empty()) {
825 const LoopData
LD = LoopBB.getLoopData();
826 if (EstimatedLoopWeight.count(LD))
830 SmallVectorImpl<BasicBlock *> &Exits = Res.first->second;
832 getLoopExitBlocks(LoopBB, Exits);
833 auto LoopWeight = getMaxEstimatedEdgeWeight(
841 EstimatedLoopWeight.insert({
LD, *LoopWeight});
843 getLoopEnterBlocks(LoopBB, BlockWorkList);
847 while (!BlockWorkList.
empty()) {
850 if (EstimatedBlockWeight.count(BB))
859 const LoopBlock LoopBB = getLoopBlock(BB);
860 auto MaxWeight = getMaxEstimatedEdgeWeight(LoopBB,
successors(BB));
863 propagateEstimatedBlockWeight(LoopBB, DT, PDT, *MaxWeight,
864 BlockWorkList, LoopWorkList);
866 }
while (!BlockWorkList.
empty() || !LoopWorkList.
empty());
872bool BranchProbabilityInfo::calcEstimatedHeuristics(
const BasicBlock *BB) {
874 "expected more than one successor!");
876 const LoopBlock LoopBB = getLoopBlock(BB);
878 SmallPtrSet<const BasicBlock *, 8> UnlikelyBlocks;
880 if (LoopBB.getLoop())
884 bool FoundEstimatedWeight =
false;
885 SmallVector<uint32_t, 4> SuccWeights;
886 uint64_t TotalWeight = 0;
888 for (
const BasicBlock *SuccBB :
successors(BB)) {
889 std::optional<uint32_t> Weight;
890 const LoopBlock SuccLoopBB = getLoopBlock(SuccBB);
891 const LoopEdge Edge{LoopBB, SuccLoopBB};
893 Weight = getEstimatedEdgeWeight(Edge);
895 if (isLoopExitingEdge(Edge) &&
904 bool IsUnlikelyEdge = LoopBB.getLoop() && UnlikelyBlocks.
contains(SuccBB);
905 if (IsUnlikelyEdge &&
915 FoundEstimatedWeight =
true;
919 TotalWeight += WeightVal;
926 if (!FoundEstimatedWeight || TotalWeight == 0)
930 const unsigned SuccCount = SuccWeights.
size();
934 if (TotalWeight > UINT32_MAX) {
935 uint64_t ScalingFactor = TotalWeight / UINT32_MAX + 1;
937 for (
unsigned Idx = 0; Idx < SuccCount; ++Idx) {
938 SuccWeights[Idx] /= ScalingFactor;
942 TotalWeight += SuccWeights[Idx];
944 assert(TotalWeight <= UINT32_MAX &&
"Total weight overflows");
951 for (
unsigned Idx = 0; Idx < SuccCount; ++Idx) {
952 EdgeProbabilities[Idx] =
953 BranchProbability(SuccWeights[Idx], (uint32_t)TotalWeight);
959bool BranchProbabilityInfo::calcZeroHeuristics(
const BasicBlock *BB,
960 const TargetLibraryInfo *TLI) {
970 auto GetConstantInt = [](
Value *
V) {
977 ConstantInt *CV = GetConstantInt(
RHS);
984 if (
LHS->getOpcode() == Instruction::And)
985 if (ConstantInt *AndRHS = GetConstantInt(
LHS->getOperand(1)))
986 if (AndRHS->getValue().isPowerOf2())
996 ProbabilityTable::const_iterator Search;
997 if (Func == LibFunc_strcasecmp ||
998 Func == LibFunc_strcmp ||
999 Func == LibFunc_strncasecmp ||
1000 Func == LibFunc_strncmp ||
1001 Func == LibFunc_memcmp ||
1002 Func == LibFunc_bcmp) {
1006 }
else if (CV->
isZero()) {
1010 }
else if (CV->
isOne()) {
1026bool BranchProbabilityInfo::calcFloatingPointHeuristics(
const BasicBlock *BB) {
1047 ProbList = Search->second;
1060 FunctionAnalysisManager::Invalidator &) {
1069 OS <<
"---- Branch Probabilities ----\n";
1072 assert(LastF &&
"Cannot print prior to running over a function");
1073 for (
const auto &BI : *LastF) {
1092 unsigned IndexInSuccessors)
const {
1093 auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
1094 assert((Probs.end() == Probs.find(std::make_pair(Src, 0))) ==
1095 (Probs.end() ==
I) &&
1096 "Probability for I-th successor must always be defined along with the "
1097 "probability for the first successor");
1099 if (
I != Probs.end())
1116 if (!Probs.count(std::make_pair(Src, 0)))
1122 Prob += Probs.find(std::make_pair(Src,
I.getSuccessorIndex()))->second;
1130 assert(Src->getTerminator()->getNumSuccessors() == Probs.size());
1132 if (Probs.size() == 0)
1135 Handles.insert(BasicBlockCallbackVH(Src,
this));
1137 for (
unsigned SuccIdx = 0; SuccIdx < Probs.size(); ++SuccIdx) {
1138 this->Probs[std::make_pair(Src, SuccIdx)] = Probs[SuccIdx];
1139 LLVM_DEBUG(
dbgs() <<
"set edge " << Src->getName() <<
" -> " << SuccIdx
1140 <<
" successor probability to " << Probs[SuccIdx]
1142 TotalNumerator += Probs[SuccIdx].getNumerator();
1152 (void)TotalNumerator;
1158 unsigned NumSuccessors = Src->getTerminator()->getNumSuccessors();
1159 assert(NumSuccessors == Dst->getTerminator()->getNumSuccessors());
1160 if (NumSuccessors == 0)
1162 if (!this->Probs.contains(std::make_pair(Src, 0)))
1165 Handles.insert(BasicBlockCallbackVH(Dst,
this));
1166 for (
unsigned SuccIdx = 0; SuccIdx < NumSuccessors; ++SuccIdx) {
1167 auto Prob = this->Probs[std::make_pair(Src, SuccIdx)];
1168 this->Probs[std::make_pair(Dst, SuccIdx)] = Prob;
1169 LLVM_DEBUG(
dbgs() <<
"set edge " << Dst->getName() <<
" -> " << SuccIdx
1170 <<
" successor probability to " << Prob <<
"\n");
1175 assert(Src->getTerminator()->getNumSuccessors() == 2);
1176 auto It0 = Probs.find(std::make_pair(Src, 0));
1177 if (It0 == Probs.end())
1179 auto It1 = Probs.find(std::make_pair(Src, 1));
1180 assert(It1 != Probs.end());
1190 Src->printAsOperand(OS,
false, Src->getModule());
1192 Dst->printAsOperand(OS,
false, Dst->getModule());
1193 OS <<
" probability is " << Prob
1194 << (
isEdgeHot(Src, Dst) ?
" [HOT edge]\n" :
"\n");
1209 Handles.erase(BasicBlockCallbackVH(BB,
this));
1210 for (
unsigned I = 0;; ++
I) {
1211 auto MapI = Probs.find(std::make_pair(BB,
I));
1212 if (MapI == Probs.end()) {
1213 assert(Probs.count(std::make_pair(BB,
I + 1)) == 0 &&
1214 "Must be no more successors");
1230 SccI = std::make_unique<SccInfo>(
F);
1232 assert(EstimatedBlockWeight.empty());
1233 assert(EstimatedLoopWeight.empty());
1235 std::unique_ptr<DominatorTree> DTPtr;
1236 std::unique_ptr<PostDominatorTree> PDTPtr;
1239 DTPtr = std::make_unique<DominatorTree>(
const_cast<Function &
>(
F));
1244 PDTPtr = std::make_unique<PostDominatorTree>(
const_cast<Function &
>(
F));
1248 estimateBlockWeights(
F, DT, PDT);
1252 for (
const auto *BB :
post_order(&
F.getEntryBlock())) {
1258 if (calcMetadataWeights(BB))
1260 if (calcEstimatedHeuristics(BB))
1262 if (calcPointerHeuristics(BB))
1264 if (calcZeroHeuristics(BB, TLI))
1266 if (calcFloatingPointHeuristics(BB))
1270 EstimatedLoopWeight.clear();
1271 EstimatedBlockWeight.clear();
1300 BPI.calculate(
F, LI, &TLI, &DT, &PDT);
1325 OS <<
"Printing analysis 'Branch Probability Analysis' for function '"
1326 <<
F.getName() <<
"':\n";
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockExecWeight
Set of dedicated "absolute" execution weights for a block.
@ NORETURN
Weight to a block containing non returning call.
@ UNWIND
Weight to 'unwind' block of an invoke instruction.
@ COLD
Weight to a 'cold' block.
@ ZERO
Special weight used for cases with exact zero probability.
@ UNREACHABLE
Weight to an 'unreachable' block.
@ DEFAULT
Default weight is used in cases when there is no dedicated execution weight set.
@ LOWEST_NON_ZERO
Minimal possible non zero weight.
static const uint32_t FPH_TAKEN_WEIGHT
static const BranchProbability FPUntakenProb(FPH_NONTAKEN_WEIGHT, FPH_TAKEN_WEIGHT+FPH_NONTAKEN_WEIGHT)
static const BranchProbability ZeroUntakenProb(ZH_NONTAKEN_WEIGHT, ZH_TAKEN_WEIGHT+ZH_NONTAKEN_WEIGHT)
static const uint32_t LBH_TAKEN_WEIGHT
static const uint32_t ZH_NONTAKEN_WEIGHT
static const ProbabilityTable ICmpWithLibCallTable
strcmp and similar functions return zero, negative, or positive, if the first string is equal,...
static const ProbabilityTable ICmpWithMinusOneTable
Integer compares with -1:
static const BranchProbability FPOrdTakenProb(FPH_ORD_WEIGHT, FPH_ORD_WEIGHT+FPH_UNO_WEIGHT)
static const ProbabilityTable ICmpWithZeroTable
Integer compares with 0:
static const uint32_t PH_NONTAKEN_WEIGHT
std::map< CmpInst::Predicate, ProbabilityList > ProbabilityTable
static const BranchProbability PtrTakenProb(PH_TAKEN_WEIGHT, PH_TAKEN_WEIGHT+PH_NONTAKEN_WEIGHT)
static const BranchProbability ZeroTakenProb(ZH_TAKEN_WEIGHT, ZH_TAKEN_WEIGHT+ZH_NONTAKEN_WEIGHT)
static const BranchProbability FPOrdUntakenProb(FPH_UNO_WEIGHT, FPH_ORD_WEIGHT+FPH_UNO_WEIGHT)
static const uint32_t PH_TAKEN_WEIGHT
Heuristics and lookup tables for non-loop branches: Pointer Heuristics (PH)
static const BranchProbability FPTakenProb(FPH_TAKEN_WEIGHT, FPH_TAKEN_WEIGHT+FPH_NONTAKEN_WEIGHT)
static const BranchProbability PtrUntakenProb(PH_NONTAKEN_WEIGHT, PH_TAKEN_WEIGHT+PH_NONTAKEN_WEIGHT)
SmallVector< BranchProbability > ProbabilityList
static const ProbabilityTable ICmpWithOneTable
Integer compares with 1:
static const uint32_t ZH_TAKEN_WEIGHT
Zero Heuristics (ZH)
static const uint32_t FPH_NONTAKEN_WEIGHT
static const BranchProbability UR_TAKEN_PROB
Unreachable-terminating branch taken probability.
static const ProbabilityTable FCmpTable
Floating-Point compares:
static const uint32_t LBH_NONTAKEN_WEIGHT
static const ProbabilityTable PointerTable
Pointer comparisons:
static const uint32_t FPH_ORD_WEIGHT
This is the probability for an ordered floating point comparison.
static void computeUnlikelySuccessors(const BasicBlock *BB, Loop *L, SmallPtrSetImpl< const BasicBlock * > &UnlikelyBlocks)
static const uint32_t FPH_UNO_WEIGHT
This is the probability for an unordered floating point comparison, it means one or two of the operan...
static cl::opt< std::string > PrintBranchProbFuncName("print-bpi-func-name", cl::Hidden, cl::desc("The option to specify the name of the function " "whose branch probability info is printed."))
static cl::opt< bool > PrintBranchProb("print-bpi", cl::init(false), cl::Hidden, cl::desc("Print the branch probability info."))
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This header defines various interfaces for pass management in LLVM.
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
This file contains the declarations for profiling metadata utility functions.
const SmallVectorImpl< MachineOperand > & Cond
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
This file defines the SmallVector class.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM Basic Block Representation.
LLVM_ABI const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
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...
Conditional or Unconditional Branch instruction.
bool isConditional() const
BasicBlock * getSuccessor(unsigned i) const
Value * getCondition() const
Analysis pass which computes BranchProbabilityInfo.
LLVM_ABI BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BPI.
Legacy analysis pass which computes BranchProbabilityInfo.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
BranchProbabilityInfoWrapperPass()
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
bool isSCCHeader(const BasicBlock *BB, int SccNum) const
Returns true if BB is a 'header' block in SCC with SccNum ID, false otherwise.
LLVM_ABI void getSccEnterBlocks(int SccNum, SmallVectorImpl< BasicBlock * > &Enters) const
Fills in Enters vector with all such blocks that don't belong to SCC with SccNum ID but there is an e...
bool isSCCExitingBlock(const BasicBlock *BB, int SccNum) const
Returns true if BB is an 'exiting' block in SCC with SccNum ID, false otherwise.
LLVM_ABI SccInfo(const Function &F)
LLVM_ABI void getSccExitBlocks(int SccNum, SmallVectorImpl< BasicBlock * > &Exits) const
Fills in Exits vector with all such blocks that don't belong to SCC with SccNum ID but there is an ed...
LLVM_ABI int getSCCNum(const BasicBlock *BB) const
If BB belongs to some SCC then ID of that SCC is returned, otherwise -1 is returned.
Analysis providing branch probability information.
LLVM_ABI void eraseBlock(const BasicBlock *BB)
Forget analysis results for the given basic block.
LLVM_ABI void setEdgeProbability(const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs)
Set the raw probabilities for all edges from the given block.
LLVM_ABI bool invalidate(Function &, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &)
LLVM_ABI BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
LLVM_ABI void calculate(const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI, DominatorTree *DT, PostDominatorTree *PDT)
LLVM_ABI void releaseMemory()
LLVM_ABI bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
LLVM_ABI void swapSuccEdgesProbabilities(const BasicBlock *Src)
Swap outgoing edges probabilities for Src with branch terminator.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI raw_ostream & printEdgeProbability(raw_ostream &OS, const BasicBlock *Src, const BasicBlock *Dst) const
Print an edge's probability.
LLVM_ABI void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst)
Copy outgoing edge probabilities from Src to Dst.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
static uint32_t getDenominator()
static BranchProbability getRaw(uint32_t N)
static BranchProbability getOne()
static BranchProbability getUnknown()
uint32_t getNumerator() const
static BranchProbability getZero()
Represents analyses that only rely on functions' control flow.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
This class is the base class for the comparison instructions.
@ ICMP_SLT
signed less than
@ ICMP_SGT
signed greater than
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
bool isTrueWhenEqual() const
This is just a convenience.
Predicate getPredicate() const
Return the predicate for this instruction.
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
This is an important base class in LLVM.
A parsed version of the target data layout string in and methods for querying it.
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Analysis pass which computes a DominatorTree.
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
static bool isEquality(Predicate Pred)
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
LLVM_ABI BasicBlock * getSuccessor(unsigned Idx) const LLVM_READONLY
Return the specified successor. This instruction must be a terminator.
Analysis pass that exposes the LoopInfo for a function.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Represents a single loop in the control flow graph.
A Module instance is used to store all the information related to an LLVM module.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
Analysis pass which computes a PostDominatorTree.
PostDominatorTree Class - Concrete subclass of DominatorTree that is used to compute the post-dominat...
LLVM_ABI bool dominates(const Instruction *I1, const Instruction *I2) const
Return true if I1 dominates I2.
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.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
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 TargetLibraryInfo.
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
bool isPointerTy() const
True if this is an instance of PointerType.
Value * getOperand(unsigned i) const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
This class implements an extremely fast bulk output stream that can only output to a stream.
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.
bool isAtEnd() const
Direct loop termination test which is more efficient than comparison with end().
@ BasicBlock
Various leaf nodes.
initializer< Ty > init(const Ty &Val)
NodeAddr< FuncNode * > Func
friend class Instruction
Iterator for Instructions in a `BasicBlock.
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
auto pred_end(const MachineBasicBlock *BB)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto successors(const MachineBasicBlock *BB)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
iterator_range< po_iterator< T > > post_order(const T &G)
constexpr T divideNearest(U Numerator, V Denominator)
Returns (Numerator / Denominator) rounded by round-half-up.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
LLVM_ABI MDNode * getValidBranchWeightMDNode(const Instruction &I)
Get the valid branch weights metadata node.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto succ_size(const MachineBasicBlock *BB)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI Constant * ConstantFoldBinaryOpOperands(unsigned Opcode, Constant *LHS, Constant *RHS, const DataLayout &DL)
Attempt to constant fold a binary operation with the specified operands.
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
@ Mul
Product of integers.
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
auto pred_begin(const MachineBasicBlock *BB)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
SuccIterator< const Instruction, const BasicBlock > const_succ_iterator
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
A special type used by analysis passes to provide an address that identifies that particular analysis...