LLVM  3.7.0
BranchProbabilityInfo.h
Go to the documentation of this file.
1 //===--- BranchProbabilityInfo.h - Branch Probability Analysis --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass is used to evaluate branch probabilties.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
15 #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
16 
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/IR/CFG.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Pass.h"
23 
24 namespace llvm {
25 class LoopInfo;
26 class raw_ostream;
27 
28 /// \brief Analysis pass providing branch probability information.
29 ///
30 /// This is a function analysis pass which provides information on the relative
31 /// probabilities of each "edge" in the function's CFG where such an edge is
32 /// defined by a pair (PredBlock and an index in the successors). The
33 /// probability of an edge from one block is always relative to the
34 /// probabilities of other edges from the block. The probabilites of all edges
35 /// from a block sum to exactly one (100%).
36 /// We use a pair (PredBlock and an index in the successors) to uniquely
37 /// identify an edge, since we can have multiple edges from Src to Dst.
38 /// As an example, we can have a switch which jumps to Dst with value 0 and
39 /// value 10.
41 public:
42  static char ID;
43 
46  }
47 
48  void getAnalysisUsage(AnalysisUsage &AU) const override;
49  bool runOnFunction(Function &F) override;
50 
51  void releaseMemory() override;
52 
53  void print(raw_ostream &OS, const Module *M = nullptr) const override;
54 
55  /// \brief Get an edge's probability, relative to other out-edges of the Src.
56  ///
57  /// This routine provides access to the fractional probability between zero
58  /// (0%) and one (100%) of this edge executing, relative to other edges
59  /// leaving the 'Src' block. The returned probability is never zero, and can
60  /// only be one if the source block has only one successor.
62  unsigned IndexInSuccessors) const;
63 
64  /// \brief Get the probability of going from Src to Dst.
65  ///
66  /// It returns the sum of all probabilities for edges from Src to Dst.
68  const BasicBlock *Dst) const;
69 
70  /// \brief Test if an edge is hot relative to other out-edges of the Src.
71  ///
72  /// Check whether this edge out of the source block is 'hot'. We define hot
73  /// as having a relative probability >= 80%.
74  bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const;
75 
76  /// \brief Retrieve the hot successor of a block if one exists.
77  ///
78  /// Given a basic block, look through its successors and if one exists for
79  /// which \see isEdgeHot would return true, return that successor block.
80  BasicBlock *getHotSucc(BasicBlock *BB) const;
81 
82  /// \brief Print an edge's probability.
83  ///
84  /// Retrieves an edge's probability similarly to \see getEdgeProbability, but
85  /// then prints that probability to the provided stream. That stream is then
86  /// returned.
88  const BasicBlock *Dst) const;
89 
90  /// \brief Get the raw edge weight calculated for the edge.
91  ///
92  /// This returns the raw edge weight. It is guaranteed to fall between 1 and
93  /// UINT32_MAX. Note that the raw edge weight is not meaningful in isolation.
94  /// This interface should be very carefully, and primarily by routines that
95  /// are updating the analysis by later calling setEdgeWeight.
96  uint32_t getEdgeWeight(const BasicBlock *Src,
97  unsigned IndexInSuccessors) const;
98 
99  /// \brief Get the raw edge weight calculated for the block pair.
100  ///
101  /// This returns the sum of all raw edge weights from Src to Dst.
102  /// It is guaranteed to fall between 1 and UINT32_MAX.
103  uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const;
104 
105  uint32_t getEdgeWeight(const BasicBlock *Src,
106  succ_const_iterator Dst) const;
107 
108  /// \brief Set the raw edge weight for a given edge.
109  ///
110  /// This allows a pass to explicitly set the edge weight for an edge. It can
111  /// be used when updating the CFG to update and preserve the branch
112  /// probability information. Read the implementation of how these edge
113  /// weights are calculated carefully before using!
114  void setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,
115  uint32_t Weight);
116 
117  static uint32_t getBranchWeightStackProtector(bool IsLikely) {
118  return IsLikely ? (1u << 20) - 1 : 1;
119  }
120 
121 private:
122  // Since we allow duplicate edges from one basic block to another, we use
123  // a pair (PredBlock and an index in the successors) to specify an edge.
124  typedef std::pair<const BasicBlock *, unsigned> Edge;
125 
126  // Default weight value. Used when we don't have information about the edge.
127  // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
128  // the successors have a weight yet. But it doesn't make sense when providing
129  // weight to an edge that may have siblings with non-zero weights. This can
130  // be handled various ways, but it's probably fine for an edge with unknown
131  // weight to just "inherit" the non-zero weight of an adjacent successor.
132  static const uint32_t DEFAULT_WEIGHT = 16;
133 
134  DenseMap<Edge, uint32_t> Weights;
135 
136  /// \brief Handle to the LoopInfo analysis.
137  LoopInfo *LI;
138 
139  /// \brief Track the last function we run over for printing.
140  Function *LastF;
141 
142  /// \brief Track the set of blocks directly succeeded by a returning block.
143  SmallPtrSet<BasicBlock *, 16> PostDominatedByUnreachable;
144 
145  /// \brief Track the set of blocks that always lead to a cold call.
146  SmallPtrSet<BasicBlock *, 16> PostDominatedByColdCall;
147 
148  /// \brief Get sum of the block successors' weights.
149  uint32_t getSumForBlock(const BasicBlock *BB) const;
150 
151  bool calcUnreachableHeuristics(BasicBlock *BB);
152  bool calcMetadataWeights(BasicBlock *BB);
153  bool calcColdCallHeuristics(BasicBlock *BB);
154  bool calcPointerHeuristics(BasicBlock *BB);
155  bool calcLoopBranchHeuristics(BasicBlock *BB);
156  bool calcZeroHeuristics(BasicBlock *BB);
157  bool calcFloatingPointHeuristics(BasicBlock *BB);
158  bool calcInvokeHeuristics(BasicBlock *BB);
159 };
160 
161 }
162 
163 #endif
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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. ...
Definition: Module.h:114
F(f)
BasicBlock * getHotSucc(BasicBlock *BB) const
Retrieve the hot successor of a block if one exists.
void initializeBranchProbabilityInfoPass(PassRegistry &)
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
void setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors, uint32_t Weight)
Set the raw edge weight for a given edge.
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.
static uint32_t getBranchWeightStackProtector(bool IsLikely)
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const
Test if an edge is hot relative to other out-edges of the Src.
Analysis pass providing branch probability information.
void print(raw_ostream &OS, const Module *M=nullptr) const override
print - Print out the internal state of the pass.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
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.