1 //===--- BranchProbabilityInfo.h - Branch Probability Analysis --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass is used to evaluate branch probabilties.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
15 #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
17 #include "llvm/InitializePasses.h"
18 #include "llvm/Support/BranchProbability.h"
19 #include "llvm/Analysis/LoopInfo.h"
25 class BranchProbabilityInfo : public FunctionPass {
27 // Default weight value. Used when we don't have information about the edge.
28 // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
29 // the successors have a weight yet. But it doesn't make sense when providing
30 // weight to an edge that may have siblings with non-zero weights. This can
31 // be handled various ways, but it's probably fine for an edge with unknown
32 // weight to just "inherit" the non-zero weight of an adjacent successor.
33 static const uint32_t DEFAULT_WEIGHT = 16;
35 typedef std::pair<BasicBlock *, BasicBlock *> Edge;
37 DenseMap<Edge, uint32_t> Weights;
39 // Get sum of the block successors' weights.
40 uint32_t getSumForBlock(BasicBlock *BB) const;
45 BranchProbabilityInfo() : FunctionPass(ID) {
46 initializeBranchProbabilityInfoPass(*PassRegistry::getPassRegistry());
49 void getAnalysisUsage(AnalysisUsage &AU) const {
50 AU.addRequired<LoopInfo>();
54 bool runOnFunction(Function &F);
56 // Returned value is between 1 and UINT32_MAX. Look at
57 // BranchProbabilityInfo.cpp for details.
58 uint32_t getEdgeWeight(BasicBlock *Src, BasicBlock *Dst) const;
60 // Look at BranchProbabilityInfo.cpp for details. Use it with caution!
61 void setEdgeWeight(BasicBlock *Src, BasicBlock *Dst, uint32_t Weight);
63 // A 'Hot' edge is an edge which probability is >= 80%.
64 bool isEdgeHot(BasicBlock *Src, BasicBlock *Dst) const;
66 // Return a hot successor for the block BB or null if there isn't one.
67 BasicBlock *getHotSucc(BasicBlock *BB) const;
69 // Return a probability as a fraction between 0 (0% probability) and
70 // 1 (100% probability), however the value is never equal to 0, and can be 1
71 // only iff SRC block has only one successor.
72 BranchProbability getEdgeProbability(BasicBlock *Src, BasicBlock *Dst) const;
74 // Print value between 0 (0% probability) and 1 (100% probability),
75 // however the value is never equal to 0, and can be 1 only iff SRC block
76 // has only one successor.
77 raw_ostream &printEdgeProbability(raw_ostream &OS, BasicBlock *Src,
78 BasicBlock *Dst) const;