|
LLVM 23.0.0git
|
Analysis providing branch probability information. More...
#include "llvm/Analysis/BranchProbabilityInfo.h"
Public Member Functions | |
| BranchProbabilityInfo ()=default | |
| BranchProbabilityInfo (const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI=nullptr, DominatorTree *DT=nullptr, PostDominatorTree *PDT=nullptr) | |
| LLVM_ABI bool | invalidate (Function &, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &) |
| LLVM_ABI void | print (raw_ostream &OS) const |
| 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 BranchProbability | getEdgeProbability (const BasicBlock *Src, const BasicBlock *Dst) const |
| Get the probability of going from Src to Dst. | |
| 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 raw_ostream & | printEdgeProbability (raw_ostream &OS, const BasicBlock *Src, const BasicBlock *Dst) const |
| Print an edge's probability. | |
| LLVM_ABI void | setEdgeProbability (const BasicBlock *Src, const SmallVectorImpl< BranchProbability > &Probs) |
| Set the raw probabilities for all edges from the given block. | |
| LLVM_ABI void | copyEdgeProbabilities (BasicBlock *Src, BasicBlock *Dst) |
Copy outgoing edge probabilities from Src to Dst. | |
| LLVM_ABI void | swapSuccEdgesProbabilities (const BasicBlock *Src) |
Swap outgoing edges probabilities for Src with branch terminator. | |
| LLVM_ABI void | calculate (const Function &F, const LoopInfo &LI, const TargetLibraryInfo *TLI, DominatorTree *DT, PostDominatorTree *PDT) |
| LLVM_ABI void | eraseBlock (const BasicBlock *BB) |
| Forget analysis results for the given basic block. | |
Static Public Member Functions | |
| static BranchProbability | getBranchProbStackProtector (bool IsLikely) |
Analysis providing branch probability information.
This is a function analysis which provides information on the relative probabilities of each "edge" in the function's CFG where such an edge is defined by a pair (PredBlock and an index in the successors). The probability of an edge from one block is always relative to the probabilities of other edges from the block. The probabilites of all edges from a block sum to exactly one (100%). We use a pair (PredBlock and an index in the successors) to uniquely identify an edge, since we can have multiple edges from Src to Dst. As an example, we can have a switch which jumps to Dst with value 0 and value 10.
Process of computing branch probabilities can be logically viewed as three step process:
First, if there is a profile information associated with the branch then it is trivially translated to branch probabilities. There is one exception from this rule though. Probabilities for edges leading to "unreachable" blocks (blocks with the estimated weight not greater than UNREACHABLE_WEIGHT) are evaluated according to static estimation and override profile information. If no branch probabilities were calculated on this step then take the next one.
Second, estimate absolute execution weights for each block based on statically known information. Roots of such information are "cold", "unreachable", "noreturn" and "unwind" blocks. Those blocks get their weights set to BlockExecWeight::COLD, BlockExecWeight::UNREACHABLE, BlockExecWeight::NORETURN and BlockExecWeight::UNWIND respectively. Then the weights are propagated to the other blocks up the domination line. In addition, if all successors have estimated weights set then maximum of these weights assigned to the block itself (while this is not ideal heuristic in theory it's simple and works reasonably well in most cases) and the process repeats. Once the process of weights propagation converges branch probabilities are set for all such branches that have at least one successor with the weight set. Default execution weight (BlockExecWeight::DEFAULT) is used for any successors which doesn't have its weight set. For loop back branches we use their weights scaled by loop trip count equal to 'LBH_TAKEN_WEIGHT/LBH_NOTTAKEN_WEIGHT'.
Here is a simple example demonstrating how the described algorithm works.
BB1
/ \
v v
BB2 BB3
/ \
v v ColdBB UnreachBB
Initially, ColdBB is associated with COLD_WEIGHT and UnreachBB with UNREACHABLE_WEIGHT. COLD_WEIGHT is set to BB2 as maximum between its successors. BB1 and BB3 has no explicit estimated weights and assumed to have DEFAULT_WEIGHT. Based on assigned weights branches will have the following probabilities: P(BB1->BB2) = COLD_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) = 0xffff / (0xffff + 0xfffff) = 0.0588(5.9%) P(BB1->BB3) = DEFAULT_WEIGHT_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) = 0xfffff / (0xffff + 0xfffff) = 0.941(94.1%) P(BB2->ColdBB) = COLD_WEIGHT/(COLD_WEIGHT + UNREACHABLE_WEIGHT) = 1(100%) P(BB2->UnreachBB) = UNREACHABLE_WEIGHT/(COLD_WEIGHT+UNREACHABLE_WEIGHT) = 0(0%)
If no branch probabilities were calculated on this step then take the next one.
Third, apply different kinds of local heuristics for each individual branch until first match. For example probability of a pointer to be null is estimated as PH_TAKEN_WEIGHT/(PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT). If no local heuristic has been matched then branch is left with no explicit probability set and assumed to have default probability.
Definition at line 109 of file BranchProbabilityInfo.h.
|
default |
|
inline |
Definition at line 113 of file BranchProbabilityInfo.h.
References calculate(), and F.
| void BranchProbabilityInfo::calculate | ( | const Function & | F, |
| const LoopInfo & | LI, | ||
| const TargetLibraryInfo * | TLI, | ||
| DominatorTree * | DT, | ||
| PostDominatorTree * | PDT ) |
Definition at line 1454 of file BranchProbabilityInfo.cpp.
References llvm::dbgs(), F, LLVM_DEBUG, print(), PrintBranchProb, and PrintBranchProbFuncName.
Referenced by BranchProbabilityInfo(), and llvm::BranchProbabilityAnalysis::run().
| void BranchProbabilityInfo::copyEdgeProbabilities | ( | BasicBlock * | Src, |
| BasicBlock * | Dst ) |
Copy outgoing edge probabilities from Src to Dst.
This allows to keep probabilities unset for the destination if they were unset for source.
Definition at line 1403 of file BranchProbabilityInfo.cpp.
References assert(), llvm::dbgs(), llvm::ArrayRef< T >::empty(), eraseBlock(), LLVM_DEBUG, llvm::ArrayRef< T >::size(), and llvm::succ_size().
| void BranchProbabilityInfo::eraseBlock | ( | const BasicBlock * | BB | ) |
Forget analysis results for the given basic block.
Definition at line 1446 of file BranchProbabilityInfo.cpp.
References assert(), llvm::dbgs(), llvm::Value::getName(), llvm::BasicBlock::getNumber(), llvm::BasicBlock::getParent(), and LLVM_DEBUG.
Referenced by copyEdgeProbabilities(), and llvm::SplitIndirectBrCriticalEdges().
|
inlinestatic |
Definition at line 173 of file BranchProbabilityInfo.h.
References llvm::BranchProbability::getCompl().
Referenced by InsertStackProtectors().
| BranchProbability BranchProbabilityInfo::getEdgeProbability | ( | const BasicBlock * | Src, |
| const BasicBlock * | Dst ) const |
Get the probability of going from Src to Dst.
Get the raw edge probability calculated for the block pair.
It returns the sum of all probabilities for edges from Src to Dst.
This returns the sum of all raw edge probabilities from Src to Dst.
Definition at line 1363 of file BranchProbabilityInfo.cpp.
References llvm::count(), llvm::enumerate(), llvm::BranchProbability::getZero(), P, llvm::succ_size(), and llvm::successors().
| BranchProbability BranchProbabilityInfo::getEdgeProbability | ( | const BasicBlock * | Src, |
| unsigned | IndexInSuccessors ) const |
Get an edge's probability, relative to other out-edges of the Src.
Get the raw edge probability for the edge.
This routine provides access to the fractional probability between zero (0%) and one (100%) of this edge executing, relative to other edges leaving the 'Src' block. The returned probability is never zero, and can only be one if the source block has only one successor.
If can't find it, return a default probability 1/N where N is the number of successors. Here an edge is specified using PredBlock and an index to the successors.
Definition at line 1353 of file BranchProbabilityInfo.cpp.
References P, and llvm::succ_size().
Referenced by findUnwindDestinations(), getBranchHint(), llvm::DOTGraphTraits< DOTFuncInfo * >::getEdgeAttributes(), isEdgeHot(), printEdgeProbability(), and llvm::SplitIndirectBrCriticalEdges().
| bool BranchProbabilityInfo::invalidate | ( | Function & | , |
| const PreservedAnalyses & | PA, | ||
| FunctionAnalysisManager::Invalidator & | ) |
Definition at line 1321 of file BranchProbabilityInfo.cpp.
References llvm::PreservedAnalyses::getChecker().
| bool BranchProbabilityInfo::isEdgeHot | ( | const BasicBlock * | Src, |
| const BasicBlock * | Dst ) const |
Test if an edge is hot relative to other out-edges of the Src.
Check whether this edge out of the source block is 'hot'. We define hot as having a relative probability > 80%.
Definition at line 1341 of file BranchProbabilityInfo.cpp.
References getEdgeProbability().
Referenced by printEdgeProbability(), and llvm::SelectionDAGBuilder::shouldKeepJumpConditionsTogether().
| void BranchProbabilityInfo::print | ( | raw_ostream & | OS | ) | const |
Definition at line 1330 of file BranchProbabilityInfo.cpp.
References assert(), printEdgeProbability(), and llvm::successors().
| raw_ostream & BranchProbabilityInfo::printEdgeProbability | ( | raw_ostream & | OS, |
| const BasicBlock * | Src, | ||
| const BasicBlock * | Dst ) const |
Print an edge's probability.
Retrieves an edge's probability similarly to
Definition at line 1432 of file BranchProbabilityInfo.cpp.
References getEdgeProbability(), and isEdgeHot().
Referenced by print().
| void BranchProbabilityInfo::setEdgeProbability | ( | const BasicBlock * | Src, |
| const SmallVectorImpl< BranchProbability > & | Probs ) |
Set the raw probabilities for all edges from the given block.
Set the edge probability for all edges at once.
This allows a pass to explicitly set edge probabilities for a block. It can be used when updating the CFG to update the branch probability information.
Definition at line 1378 of file BranchProbabilityInfo.cpp.
References assert(), llvm::dbgs(), llvm::BranchProbability::getDenominator(), LLVM_DEBUG, and P.
Referenced by llvm::SplitIndirectBrCriticalEdges().
| void BranchProbabilityInfo::swapSuccEdgesProbabilities | ( | const BasicBlock * | Src | ) |
Swap outgoing edges probabilities for Src with branch terminator.
Definition at line 1421 of file BranchProbabilityInfo.cpp.
References assert(), P, and std::swap().