28 #define DEBUG_TYPE "branch-prob"
31 "Branch Probability Analysis",
false,
true)
36 char BranchProbabilityInfo::
ID = 0;
122 bool BranchProbabilityInfo::calcUnreachableHeuristics(
BasicBlock *BB) {
125 if (isa<UnreachableInst>(TI))
126 PostDominatedByUnreachable.insert(BB);
134 if (PostDominatedByUnreachable.count(*
I))
135 UnreachableEdges.
push_back(
I.getSuccessorIndex());
137 ReachableEdges.
push_back(
I.getSuccessorIndex());
143 PostDominatedByUnreachable.insert(BB);
150 uint32_t UnreachableWeight =
151 std::max(UR_TAKEN_WEIGHT / (
unsigned)UnreachableEdges.
size(),
MIN_WEIGHT);
153 E = UnreachableEdges.
end();
155 setEdgeWeight(BB, *
I, UnreachableWeight);
157 if (ReachableEdges.
empty())
159 uint32_t ReachableWeight =
160 std::max(UR_NONTAKEN_WEIGHT / (
unsigned)ReachableEdges.
size(),
163 E = ReachableEdges.
end();
165 setEdgeWeight(BB, *
I, ReachableWeight);
172 bool BranchProbabilityInfo::calcMetadataWeights(
BasicBlock *BB) {
176 if (!isa<BranchInst>(TI) && !isa<SwitchInst>(TI))
194 uint64_t WeightSum = 0;
197 for (
unsigned i = 1, e = WeightsNode->
getNumOperands(); i != e; ++i) {
199 mdconst::dyn_extract<ConstantInt>(WeightsNode->
getOperand(i));
203 "Too many bits for uint32_t");
205 WeightSum += Weights.
back();
211 uint64_t ScalingFactor =
212 (WeightSum > UINT32_MAX) ? WeightSum / UINT32_MAX + 1 : 1;
216 uint32_t W = Weights[i] / ScalingFactor;
220 assert(WeightSum <= UINT32_MAX &&
221 "Expected weights to scale down to 32 bits");
234 bool BranchProbabilityInfo::calcColdCallHeuristics(
BasicBlock *BB) {
243 if (PostDominatedByColdCall.count(*
I))
251 PostDominatedByColdCall.insert(BB);
255 assert(!PostDominatedByColdCall.count(BB));
257 if (
CallInst *CI = dyn_cast<CallInst>(
I))
259 PostDominatedByColdCall.insert(BB);
268 uint32_t ColdWeight =
275 if (NormalEdges.
empty())
277 uint32_t NormalWeight = std::max(
280 E = NormalEdges.
end();
289 bool BranchProbabilityInfo::calcPointerHeuristics(
BasicBlock *BB) {
310 unsigned TakenIdx = 0, NonTakenIdx = 1;
322 bool BranchProbabilityInfo::calcLoopBranchHeuristics(
BasicBlock *BB) {
333 ExitingEdges.
push_back(
I.getSuccessorIndex());
340 if (BackEdges.
empty() && ExitingEdges.
empty())
343 if (uint32_t numBackEdges = BackEdges.
size()) {
349 EE = BackEdges.
end(); EI != EE; ++EI) {
354 if (uint32_t numInEdges = InEdges.
size()) {
360 EE = InEdges.
end(); EI != EE; ++EI) {
365 if (uint32_t numExitingEdges = ExitingEdges.
size()) {
371 EE = ExitingEdges.
end(); EI != EE; ++EI) {
379 bool BranchProbabilityInfo::calcZeroHeuristics(
BasicBlock *BB) {
398 if (
ConstantInt *AndRHS = dyn_cast<ConstantInt>(LHS->getOperand(1)))
399 if (AndRHS->getUniqueInteger().isPowerOf2())
450 unsigned TakenIdx = 0, NonTakenIdx = 1;
461 bool BranchProbabilityInfo::calcFloatingPointHeuristics(
BasicBlock *BB) {
486 unsigned TakenIdx = 0, NonTakenIdx = 1;
497 bool BranchProbabilityInfo::calcInvokeHeuristics(
BasicBlock *BB) {
516 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
517 assert(PostDominatedByUnreachable.empty());
518 assert(PostDominatedByColdCall.empty());
524 if (calcUnreachableHeuristics(BB))
526 if (calcMetadataWeights(BB))
528 if (calcColdCallHeuristics(BB))
530 if (calcLoopBranchHeuristics(BB))
532 if (calcPointerHeuristics(BB))
534 if (calcZeroHeuristics(BB))
536 if (calcFloatingPointHeuristics(BB))
538 calcInvokeHeuristics(BB);
541 PostDominatedByUnreachable.clear();
542 PostDominatedByColdCall.clear();
551 OS <<
"---- Branch Probabilities ----\n";
554 assert(LastF &&
"Cannot print prior to running over a function");
564 uint32_t BranchProbabilityInfo::getSumForBlock(
const BasicBlock *BB)
const {
569 uint32_t PrevSum = Sum;
572 assert(Sum >= PrevSum); (void) PrevSum;
587 uint32_t MaxWeight = 0;
593 uint32_t PrevSum = Sum;
596 assert(Sum > PrevSum); (void) PrevSum;
598 if (Weight > MaxWeight) {
617 Weights.find(std::make_pair(Src, IndexInSuccessors));
619 if (I != Weights.
end())
622 return DEFAULT_WEIGHT;
635 bool FoundWeight =
false;
639 MapI = Weights.find(std::make_pair(Src,
I.getSuccessorIndex()));
640 if (MapI != Weights.
end()) {
642 Weight += MapI->second;
645 return (!FoundWeight) ? DEFAULT_WEIGHT : Weight;
653 Weights[std::make_pair(Src, IndexInSuccessors)] = Weight;
655 << IndexInSuccessors <<
" successor weight to "
663 uint32_t D = getSumForBlock(Src);
674 uint32_t D = getSumForBlock(Src);
686 <<
" probability is " << Prob
687 << (
isEdgeHot(Src, Dst) ?
" [HOT edge]\n" :
"\n");
void push_back(const T &Elt)
static bool isEquality(Predicate Pred)
Determine if this is an equality predicate.
raw_ostream & printEdgeProbability(raw_ostream &OS, const BasicBlock *Src, const BasicBlock *Dst) const
Print an edge's probability.
A Module instance is used to store all the information related to an LLVM module. ...
unsigned getNumOperands() const
Return number of MDNode operands.
CallInst - This class represents a function call, abstracting a target machine's calling convention...
static const uint32_t FPH_TAKEN_WEIGHT
INITIALIZE_PASS_BEGIN(BranchProbabilityInfo,"branch-prob","Branch Probability Analysis", false, true) INITIALIZE_PASS_END(BranchProbabilityInfo
static bool isEquality(Predicate P)
isEquality - Return true if this predicate is either EQ or NE.
BasicBlock * getHotSucc(BasicBlock *BB) const
Retrieve the hot successor of a block if one exists.
LoopT * getLoopFor(const BlockT *BB) const
getLoopFor - Return the inner most loop that BB lives in.
void reserve(size_type N)
BlockT * getHeader() const
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
branch Branch Probability false
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
const APInt & getValue() const
Return the constant as an APInt value reference.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
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...
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
This instruction compares its operands according to the predicate given to the constructor.
static const uint32_t UR_TAKEN_WEIGHT
Unreachable-terminating branch taken weight.
unsigned getActiveBits() const
Compute the number of active bits in the value.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
Subclasses of this class are all able to terminate a basic block.
static const uint32_t IH_NONTAKEN_WEIGHT
Invoke-terminating normal branch not-taken weight.
LLVM Basic Block Representation.
BranchInst - Conditional or Unconditional Branch instruction.
void setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors, uint32_t Weight)
Set the raw edge weight for a given edge.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static const uint32_t CC_TAKEN_WEIGHT
Weight for a branch taken going into a cold block.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Represent the analysis usage information of a pass.
bool contains(const LoopT *L) const
contains - Return true if the specified loop is contained within in this loop.
This instruction compares its operands according to the predicate given to the constructor.
Value * getOperand(unsigned i) const
0 1 1 1 True if ordered (no nans)
iterator_range< po_iterator< T > > post_order(const T &G)
Predicate getPredicate() const
Return the predicate for this instruction.
static const uint32_t ZH_NONTAKEN_WEIGHT
Marks function as being in a cold path.
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
bool isConditional() const
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
const MDOperand & getOperand(unsigned I) const
This is the shared class of boolean and integer constants.
static const uint32_t NORMAL_WEIGHT
Type * getType() const
All values are typed, get the type of this value.
MDNode * getMetadata(unsigned KindID) const
getMetadata - Get the metadata of given kind attached to this Instruction.
unsigned getSuccessorIndex() const
getSuccessorIndex - This is used to interface between code that wants to operate on terminator instru...
bool isTrueWhenEqual() const
This is just a convenience.
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
const BasicBlock & getEntryBlock() const
bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
branch Branch Probability Analysis
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool isAllOnesValue() const
isAllOnesValue - Return true if this is the value that would be returned by getAllOnesValue.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
static const uint32_t FPH_NONTAKEN_WEIGHT
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
APInt And(const APInt &LHS, const APInt &RHS)
Bitwise AND function for APInt.
static const uint32_t MIN_WEIGHT
static const uint32_t IH_TAKEN_WEIGHT
Invoke-terminating normal branch taken weight.
Value * getCondition() const
Analysis pass providing branch probability information.
static const uint32_t PH_NONTAKEN_WEIGHT
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
static const uint32_t LBH_NONTAKEN_WEIGHT
static const uint32_t LBH_TAKEN_WEIGHT
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
static const uint32_t CC_NONTAKEN_WEIGHT
Weight for a branch not-taken into a cold block.
LLVM Value Representation.
static const uint32_t ZH_TAKEN_WEIGHT
static const uint32_t PH_TAKEN_WEIGHT
This class implements an extremely fast bulk output stream that can only output to a stream...
InvokeInst - Invoke instruction.
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
uint32_t getEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get the raw edge weight calculated for the edge.
The legacy pass manager's analysis pass to compute loop information.
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
static const uint32_t UR_NONTAKEN_WEIGHT
Unreachable-terminating branch not-taken weight.