/// value 10.
class BranchProbabilityInfo {
public:
+ BranchProbabilityInfo() {}
+ BranchProbabilityInfo(Function &F, const LoopInfo &LI) { calculate(F, LI); }
+
void releaseMemory();
void print(raw_ostream &OS) const;
BranchProbability getEdgeProbability(const BasicBlock *Src,
const BasicBlock *Dst) const;
+ BranchProbability getEdgeProbability(const BasicBlock *Src,
+ succ_const_iterator Dst) const;
+
/// \brief 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
raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,
const BasicBlock *Dst) const;
- /// \brief Get the raw edge weight calculated for the edge.
+ /// \brief Set the raw edge probability for the given edge.
///
- /// This returns the raw edge weight. It is guaranteed to fall between 1 and
- /// UINT32_MAX. Note that the raw edge weight is not meaningful in isolation.
- /// This interface should be very carefully, and primarily by routines that
- /// are updating the analysis by later calling setEdgeWeight.
- uint32_t getEdgeWeight(const BasicBlock *Src,
- unsigned IndexInSuccessors) const;
-
- /// \brief Get the raw edge weight calculated for the block pair.
- ///
- /// This returns the sum of all raw edge weights from Src to Dst.
- /// It is guaranteed to fall between 1 and UINT32_MAX.
- uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const;
-
- uint32_t getEdgeWeight(const BasicBlock *Src,
- succ_const_iterator Dst) const;
-
- /// \brief Set the raw edge weight for a given edge.
- ///
- /// This allows a pass to explicitly set the edge weight for an edge. It can
- /// be used when updating the CFG to update and preserve the branch
+ /// This allows a pass to explicitly set the edge probability for an edge. It
+ /// can be used when updating the CFG to update and preserve the branch
/// probability information. Read the implementation of how these edge
- /// weights are calculated carefully before using!
- void setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,
- uint32_t Weight);
+ /// probabilities are calculated carefully before using!
+ void setEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors,
+ BranchProbability Prob);
- static uint32_t getBranchWeightStackProtector(bool IsLikely) {
- return IsLikely ? (1u << 20) - 1 : 1;
+ static BranchProbability getBranchProbStackProtector(bool IsLikely) {
+ static const BranchProbability LikelyProb((1u << 20) - 1, 1u << 20);
+ return IsLikely ? LikelyProb : LikelyProb.getCompl();
}
void calculate(Function &F, const LoopInfo& LI);
// weight to just "inherit" the non-zero weight of an adjacent successor.
static const uint32_t DEFAULT_WEIGHT = 16;
- DenseMap<Edge, uint32_t> Weights;
-
- /// \brief Handle to the LoopInfo analysis.
- LoopInfo *LI;
+ DenseMap<Edge, BranchProbability> Probs;
/// \brief Track the last function we run over for printing.
Function *LastF;
/// \brief Track the set of blocks that always lead to a cold call.
SmallPtrSet<BasicBlock *, 16> PostDominatedByColdCall;
- /// \brief Get sum of the block successors' weights.
- uint32_t getSumForBlock(const BasicBlock *BB) const;
-
bool calcUnreachableHeuristics(BasicBlock *BB);
bool calcMetadataWeights(BasicBlock *BB);
bool calcColdCallHeuristics(BasicBlock *BB);