47 #define DEBUG_TYPE "branch-prob" 51 cl::desc(
"Print the branch probability info."));
55 cl::desc(
"The option to specify the name of the function " 56 "whose branch probability info is printed."));
59 "Branch Probability Analysis",
false,
true)
65 char BranchProbabilityInfoWrapperPass::
ID = 0;
140 if (isa<UnreachableInst>(TI) ||
145 BB->getTerminatingDeoptimizeCall())
146 PostDominatedByUnreachable.insert(BB);
152 if (
auto *II = dyn_cast<InvokeInst>(TI)) {
153 if (PostDominatedByUnreachable.count(II->getNormalDest()))
154 PostDominatedByUnreachable.insert(BB);
160 if (!PostDominatedByUnreachable.count(
I))
163 PostDominatedByUnreachable.insert(BB);
168 BranchProbabilityInfo::updatePostDominatedByColdCall(
const BasicBlock *BB) {
169 assert(!PostDominatedByColdCall.count(BB));
171 if (TI->getNumSuccessors() == 0)
176 return PostDominatedByColdCall.count(SuccBB);
178 PostDominatedByColdCall.insert(BB);
184 if (
auto *II = dyn_cast<InvokeInst>(TI))
185 if (PostDominatedByColdCall.count(II->getNormalDest())) {
186 PostDominatedByColdCall.insert(BB);
193 if (
const CallInst *CI = dyn_cast<CallInst>(&
I))
195 PostDominatedByColdCall.insert(BB);
204 bool BranchProbabilityInfo::calcUnreachableHeuristics(
const BasicBlock *BB) {
208 assert(!isa<InvokeInst>(TI) &&
209 "Invokes should have already been handled by calcInvokeHeuristics");
215 if (PostDominatedByUnreachable.count(*
I))
216 UnreachableEdges.
push_back(
I.getSuccessorIndex());
218 ReachableEdges.
push_back(
I.getSuccessorIndex());
221 if (UnreachableEdges.
empty())
224 if (ReachableEdges.
empty()) {
226 for (
unsigned SuccIdx : UnreachableEdges)
234 ReachableEdges.
size();
236 for (
unsigned SuccIdx : UnreachableEdges)
238 for (
unsigned SuccIdx : ReachableEdges)
248 bool BranchProbabilityInfo::calcMetadataWeights(
const BasicBlock *BB) {
251 if (!(isa<BranchInst>(TI) || isa<SwitchInst>(TI) || isa<IndirectBrInst>(TI)))
269 uint64_t WeightSum = 0;
274 for (
unsigned i = 1, e = WeightsNode->
getNumOperands(); i != e; ++i) {
276 mdconst::dyn_extract<ConstantInt>(WeightsNode->
getOperand(i));
280 "Too many bits for uint32_t");
282 WeightSum += Weights.
back();
283 if (PostDominatedByUnreachable.count(TI->
getSuccessor(i - 1)))
292 uint64_t ScalingFactor =
293 (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;
295 if (ScalingFactor > 1) {
298 Weights[i] /= ScalingFactor;
299 WeightSum += Weights[i];
302 assert(WeightSum <= UINT32_MAX &&
303 "Expected weights to scale down to 32 bits");
305 if (WeightSum == 0 || ReachableIdxs.
size() == 0) {
314 BP.
push_back({ Weights[i], static_cast<uint32_t>(WeightSum) });
318 if (UnreachableIdxs.
size() > 0 && ReachableIdxs.
size() > 0) {
321 for (
auto i : UnreachableIdxs)
322 if (UnreachableProb < BP[i]) {
323 ToDistribute += BP[i] - UnreachableProb;
324 BP[i] = UnreachableProb;
331 for (
auto i : ReachableIdxs)
350 bool BranchProbabilityInfo::calcColdCallHeuristics(
const BasicBlock *BB) {
354 assert(!isa<InvokeInst>(TI) &&
355 "Invokes should have already been handled by calcInvokeHeuristics");
361 if (PostDominatedByColdCall.count(*
I))
367 if (ColdEdges.
empty())
370 if (NormalEdges.
empty()) {
372 for (
unsigned SuccIdx : ColdEdges)
384 for (
unsigned SuccIdx : ColdEdges)
386 for (
unsigned SuccIdx : NormalEdges)
394 bool BranchProbabilityInfo::calcPointerHeuristics(
const BasicBlock *BB) {
415 unsigned TakenIdx = 0, NonTakenIdx = 1;
432 return SccIt->second;
442 if (SccI.
SccHeaders.size() <=
static_cast<unsigned>(SccNum))
447 std::tie(HeaderMapIt, Inserted) = HeaderMap.insert(std::make_pair(BB,
false));
453 HeaderMapIt->second = IsHeader;
456 return HeaderMapIt->second;
492 if (!CI || !isa<Instruction>(CI->
getOperand(0)) ||
504 while (!CmpPHI && CmpLHS && isa<BinaryOperator>(CmpLHS) &&
509 InstChain.
push_back(cast<BinaryOperator>(CmpLHS));
514 if (!CmpPHI || !L->
contains(CmpPHI))
521 VisitedInsts.
insert(CmpPHI);
522 while (!WorkList.
empty()) {
532 if (
PHINode *PN = dyn_cast<PHINode>(V)) {
533 if (VisitedInsts.
insert(PN).second)
547 cast<Constant>(
I->getOperand(1)),
true);
555 CmpLHSConst, CmpConst,
true);
568 bool BranchProbabilityInfo::calcLoopBranchHeuristics(
const BasicBlock *BB,
592 if (UnlikelyBlocks.
count(*
I) != 0)
593 UnlikelyEdges.
push_back(
I.getSuccessorIndex());
595 ExitingEdges.
push_back(
I.getSuccessorIndex());
602 ExitingEdges.
push_back(
I.getSuccessorIndex());
610 if (BackEdges.
empty() && ExitingEdges.
empty() && UnlikelyEdges.
empty())
622 auto Prob = TakenProb / numBackEdges;
623 for (
unsigned SuccIdx : BackEdges)
629 auto Prob = TakenProb / numInEdges;
630 for (
unsigned SuccIdx : InEdges)
637 auto Prob = NotTakenProb / numExitingEdges;
638 for (
unsigned SuccIdx : ExitingEdges)
645 auto Prob = UnlikelyProb / numUnlikelyEdges;
646 for (
unsigned SuccIdx : UnlikelyEdges)
653 bool BranchProbabilityInfo::calcZeroHeuristics(
const BasicBlock *BB,
672 if (LHS->getOpcode() == Instruction::And)
673 if (
ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1)))
674 if (AndRHS->getValue().isPowerOf2())
681 if (
Function *CalledFn = Call->getCalledFunction())
685 if (Func == LibFunc_strcasecmp ||
686 Func == LibFunc_strcmp ||
687 Func == LibFunc_strncasecmp ||
688 Func == LibFunc_strncmp ||
689 Func == LibFunc_memcmp) {
707 }
else if (CV->
isZero()) {
754 unsigned TakenIdx = 0, NonTakenIdx = 1;
766 bool BranchProbabilityInfo::calcFloatingPointHeuristics(
const BasicBlock *BB) {
791 unsigned TakenIdx = 0, NonTakenIdx = 1;
803 bool BranchProbabilityInfo::calcInvokeHeuristics(
const BasicBlock *BB) {
820 OS <<
"---- Branch Probabilities ----\n";
823 assert(LastF &&
"Cannot print prior to running over a function");
824 for (
const auto &BI : *LastF) {
847 if (Prob > MaxProb) {
866 unsigned IndexInSuccessors)
const {
867 auto I = Probs.find(std::make_pair(Src, IndexInSuccessors));
869 if (
I != Probs.end())
872 return {1, static_cast<uint32_t>(
succ_size(Src))};
887 bool FoundProb =
false;
890 auto MapI = Probs.find(std::make_pair(Src,
I.getSuccessorIndex()));
891 if (MapI != Probs.end()) {
893 Prob += MapI->second;
903 unsigned IndexInSuccessors,
905 Probs[std::make_pair(Src, IndexInSuccessors)] = Prob;
906 Handles.insert(BasicBlockCallbackVH(Src,
this));
908 << IndexInSuccessors <<
" successor probability to " << Prob
918 <<
" probability is " << Prob
919 << (
isEdgeHot(Src, Dst) ?
" [HOT edge]\n" :
"\n");
925 for (
auto I = Probs.begin(),
E = Probs.end();
I !=
E; ++
I) {
937 assert(PostDominatedByUnreachable.empty());
938 assert(PostDominatedByColdCall.empty());
949 const std::vector<const BasicBlock *> &Scc = *It;
954 for (
auto *BB : Scc) {
964 LLVM_DEBUG(
dbgs() <<
"Computing probabilities for " << BB->getName()
966 updatePostDominatedByUnreachable(BB);
967 updatePostDominatedByColdCall(BB);
969 if (BB->getTerminator()->getNumSuccessors() < 2)
971 if (calcMetadataWeights(BB))
973 if (calcInvokeHeuristics(BB))
975 if (calcUnreachableHeuristics(BB))
977 if (calcColdCallHeuristics(BB))
979 if (calcLoopBranchHeuristics(BB, LI, SccI))
981 if (calcPointerHeuristics(BB))
983 if (calcZeroHeuristics(BB, TLI))
985 if (calcFloatingPointHeuristics(BB))
989 PostDominatedByUnreachable.clear();
990 PostDominatedByColdCall.clear();
1011 const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
1012 const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
1013 BPI.calculate(F, LI, &TLI);
1034 OS <<
"Printing analysis results of BPI for function " static bool isEquality(Predicate Pred)
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
This class is the base class for the comparison instructions.
BranchProbability getCompl() const
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
BasicBlock * getSuccessor(unsigned Idx) const
Return the specified successor. This instruction must be a terminator.
void calculate(const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI=nullptr)
void push_back(const T &Elt)
int getSuccessorIndex() const
This is used to interface between code that wants to operate on terminator instructions directly...
This class represents a function call, abstracting a target machine's calling convention.
static const uint32_t FPH_TAKEN_WEIGHT
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
BasicBlock * getSuccessor(unsigned i) const
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
const MDOperand & getOperand(unsigned I) const
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
block Block Frequency true
static Constant * getCompare(unsigned short pred, Constant *C1, Constant *C2, bool OnlyIfReduced=false)
Return an ICmp or FCmp comparison operator constant expression.
Value * getCondition() const
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...
static BranchProbability getOne()
void reserve(size_type N)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
const BasicBlock * getHotSucc(const BasicBlock *BB) const
Retrieve the hot successor of a block if one exists.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
INITIALIZE_PASS_BEGIN(BranchProbabilityInfoWrapperPass, "branch-prob", "Branch Probability Analysis", false, true) INITIALIZE_PASS_END(BranchProbabilityInfoWrapperPass
static int getSCCNum(const BasicBlock *BB, const BranchProbabilityInfo::SccInfo &SccI)
This file contains the simple types necessary to represent the attributes associated with functions a...
Analysis pass that exposes the LoopInfo for a function.
BlockT * getHeader() const
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
Analysis pass which computes BranchProbabilityInfo.
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
unsigned getActiveBits() const
Compute the number of active bits in the value.
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Type * getType() const
All values are typed, get the type of this value.
This instruction compares its operands according to the predicate given to the constructor.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
const APInt & getValue() const
Return the constant as an APInt value reference.
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Legacy analysis pass which computes BranchProbabilityInfo.
unsigned getNumSuccessors() const
Return the number of successors that this instruction has.
Value * getOperand(unsigned i) const
Interval::succ_iterator succ_end(Interval *I)
iterator find(const_arg_type_t< KeyT > Val)
bool isZeroValue() const
Return true if the value is negative zero or null value.
const BasicBlock & getEntryBlock() const
initializer< Ty > init(const Ty &Val)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A set of analyses that are preserved following a run of a transformation pass.
static const uint32_t IH_NONTAKEN_WEIGHT
Invoke-terminating normal branch not-taken weight.
LLVM Basic Block Representation.
Conditional or Unconditional Branch instruction.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
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...
static const uint32_t CC_TAKEN_WEIGHT
Weight for a branch taken going into a cold block.
void eraseBlock(const BasicBlock *BB)
Forget analysis results for the given basic block.
Represent the analysis usage information of a pass.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
static void computeUnlikelySuccessors(const BasicBlock *BB, Loop *L, SmallPtrSetImpl< const BasicBlock *> &UnlikelyBlocks)
This instruction compares its operands according to the predicate given to the constructor.
Interval::pred_iterator pred_end(Interval *I)
0 1 1 1 True if ordered (no nans)
iterator_range< po_iterator< T > > post_order(const T &G)
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
iterator_range< block_iterator > blocks()
static const uint32_t ZH_NONTAKEN_WEIGHT
BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM)
Run the analysis pass over a function and produce BPI.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is the shared class of boolean and integer constants.
static bool isSCCHeader(const BasicBlock *BB, int SccNum, BranchProbabilityInfo::SccInfo &SccI)
Provides information about what library functions are available for the current target.
static BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
void setEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors, BranchProbability Prob)
Set the raw edge probability for the given edge.
bool isConditional() const
branch Branch Probability Analysis
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
bool isTrueWhenEqual() const
This is just a convenience.
void print(raw_ostream &OS) const
static const uint32_t FPH_NONTAKEN_WEIGHT
void setPreservesAll()
Set by analyses that do not transform their input at all.
static const BranchProbability UR_TAKEN_PROB
Unreachable-terminating branch taken probability.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
static const uint32_t IH_TAKEN_WEIGHT
Invoke-terminating normal branch taken weight.
unsigned succ_size(const Instruction *I)
Predicate getPredicate() const
Return the predicate for this instruction.
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Analysis providing branch probability information.
static const uint32_t LBH_UNLIKELY_WEIGHT
static const uint32_t PH_NONTAKEN_WEIGHT
LLVM_NODISCARD bool empty() const
Represents a single loop in the control flow graph.
static const uint32_t LBH_NONTAKEN_WEIGHT
DenseMapIterator< const BasicBlock *, bool, DenseMapInfo< const BasicBlock * >, llvm::detail::DenseMapPair< const BasicBlock *, bool > > iterator
StringRef getName() const
Return a constant reference to the value's name.
static const uint32_t LBH_TAKEN_WEIGHT
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Analysis pass providing the TargetLibraryInfo.
bool isOneValue() const
Returns true if the value is one.
static const uint32_t CC_NONTAKEN_WEIGHT
Weight for a branch not-taken into a cold block.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
static const uint32_t ZH_TAKEN_WEIGHT
succ_range successors(Instruction *I)
static const uint32_t PH_TAKEN_WEIGHT
raw_ostream & printEdgeProbability(raw_ostream &OS, const BasicBlock *Src, const BasicBlock *Dst) const
Print an edge's probability.
This class implements an extremely fast bulk output stream that can only output to a stream...
The legacy pass manager's analysis pass to compute loop information.
A container for analyses that lazily runs them and caches their results.
Legacy analysis pass which computes a DominatorTree.
static BranchProbability getZero()
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."))
This header defines various interfaces for pass management in LLVM.
unsigned getNumOperands() const
Return number of MDNode operands.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
A special type used by analysis passes to provide an address that identifies that particular analysis...
static cl::opt< bool > PrintBranchProb("print-bpi", cl::init(false), cl::Hidden, cl::desc("Print the branch probability info."))
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.
static Constant * get(unsigned Opcode, Constant *C1, unsigned Flags=0, Type *OnlyIfReducedTy=nullptr)
get - Return a unary operator constant expression, folding if possible.