LLVM API Documentation

BranchProbabilityInfo.h
Go to the documentation of this file.
00001 //===--- BranchProbabilityInfo.h - Branch Probability Analysis --*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass is used to evaluate branch probabilties.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
00015 #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
00016 
00017 #include "llvm/ADT/DenseMap.h"
00018 #include "llvm/ADT/SmallPtrSet.h"
00019 #include "llvm/IR/CFG.h"
00020 #include "llvm/InitializePasses.h"
00021 #include "llvm/Pass.h"
00022 #include "llvm/Support/BranchProbability.h"
00023 
00024 namespace llvm {
00025 class LoopInfo;
00026 class raw_ostream;
00027 
00028 /// \brief Analysis pass providing branch probability information.
00029 ///
00030 /// This is a function analysis pass which provides information on the relative
00031 /// probabilities of each "edge" in the function's CFG where such an edge is
00032 /// defined by a pair (PredBlock and an index in the successors). The
00033 /// probability of an edge from one block is always relative to the
00034 /// probabilities of other edges from the block. The probabilites of all edges
00035 /// from a block sum to exactly one (100%).
00036 /// We use a pair (PredBlock and an index in the successors) to uniquely
00037 /// identify an edge, since we can have multiple edges from Src to Dst.
00038 /// As an example, we can have a switch which jumps to Dst with value 0 and
00039 /// value 10.
00040 class BranchProbabilityInfo : public FunctionPass {
00041 public:
00042   static char ID;
00043 
00044   BranchProbabilityInfo() : FunctionPass(ID) {
00045     initializeBranchProbabilityInfoPass(*PassRegistry::getPassRegistry());
00046   }
00047 
00048   void getAnalysisUsage(AnalysisUsage &AU) const override;
00049   bool runOnFunction(Function &F) override;
00050   void print(raw_ostream &OS, const Module *M = nullptr) const override;
00051 
00052   /// \brief Get an edge's probability, relative to other out-edges of the Src.
00053   ///
00054   /// This routine provides access to the fractional probability between zero
00055   /// (0%) and one (100%) of this edge executing, relative to other edges
00056   /// leaving the 'Src' block. The returned probability is never zero, and can
00057   /// only be one if the source block has only one successor.
00058   BranchProbability getEdgeProbability(const BasicBlock *Src,
00059                                        unsigned IndexInSuccessors) const;
00060 
00061   /// \brief Get the probability of going from Src to Dst.
00062   ///
00063   /// It returns the sum of all probabilities for edges from Src to Dst.
00064   BranchProbability getEdgeProbability(const BasicBlock *Src,
00065                                        const BasicBlock *Dst) const;
00066 
00067   /// \brief Test if an edge is hot relative to other out-edges of the Src.
00068   ///
00069   /// Check whether this edge out of the source block is 'hot'. We define hot
00070   /// as having a relative probability >= 80%.
00071   bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const;
00072 
00073   /// \brief Retrieve the hot successor of a block if one exists.
00074   ///
00075   /// Given a basic block, look through its successors and if one exists for
00076   /// which \see isEdgeHot would return true, return that successor block.
00077   BasicBlock *getHotSucc(BasicBlock *BB) const;
00078 
00079   /// \brief Print an edge's probability.
00080   ///
00081   /// Retrieves an edge's probability similarly to \see getEdgeProbability, but
00082   /// then prints that probability to the provided stream. That stream is then
00083   /// returned.
00084   raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,
00085                                     const BasicBlock *Dst) const;
00086 
00087   /// \brief Get the raw edge weight calculated for the edge.
00088   ///
00089   /// This returns the raw edge weight. It is guaranteed to fall between 1 and
00090   /// UINT32_MAX. Note that the raw edge weight is not meaningful in isolation.
00091   /// This interface should be very carefully, and primarily by routines that
00092   /// are updating the analysis by later calling setEdgeWeight.
00093   uint32_t getEdgeWeight(const BasicBlock *Src,
00094                          unsigned IndexInSuccessors) const;
00095 
00096   /// \brief Get the raw edge weight calculated for the block pair.
00097   ///
00098   /// This returns the sum of all raw edge weights from Src to Dst.
00099   /// It is guaranteed to fall between 1 and UINT32_MAX.
00100   uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const;
00101 
00102   uint32_t getEdgeWeight(const BasicBlock *Src,
00103                          succ_const_iterator Dst) const;
00104 
00105   /// \brief Set the raw edge weight for a given edge.
00106   ///
00107   /// This allows a pass to explicitly set the edge weight for an edge. It can
00108   /// be used when updating the CFG to update and preserve the branch
00109   /// probability information. Read the implementation of how these edge
00110   /// weights are calculated carefully before using!
00111   void setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,
00112                      uint32_t Weight);
00113 
00114 private:
00115   // Since we allow duplicate edges from one basic block to another, we use
00116   // a pair (PredBlock and an index in the successors) to specify an edge.
00117   typedef std::pair<const BasicBlock *, unsigned> Edge;
00118 
00119   // Default weight value. Used when we don't have information about the edge.
00120   // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
00121   // the successors have a weight yet. But it doesn't make sense when providing
00122   // weight to an edge that may have siblings with non-zero weights. This can
00123   // be handled various ways, but it's probably fine for an edge with unknown
00124   // weight to just "inherit" the non-zero weight of an adjacent successor.
00125   static const uint32_t DEFAULT_WEIGHT = 16;
00126 
00127   DenseMap<Edge, uint32_t> Weights;
00128 
00129   /// \brief Handle to the LoopInfo analysis.
00130   LoopInfo *LI;
00131 
00132   /// \brief Track the last function we run over for printing.
00133   Function *LastF;
00134 
00135   /// \brief Track the set of blocks directly succeeded by a returning block.
00136   SmallPtrSet<BasicBlock *, 16> PostDominatedByUnreachable;
00137 
00138   /// \brief Track the set of blocks that always lead to a cold call.
00139   SmallPtrSet<BasicBlock *, 16> PostDominatedByColdCall;
00140 
00141   /// \brief Get sum of the block successors' weights.
00142   uint32_t getSumForBlock(const BasicBlock *BB) const;
00143 
00144   bool calcUnreachableHeuristics(BasicBlock *BB);
00145   bool calcMetadataWeights(BasicBlock *BB);
00146   bool calcColdCallHeuristics(BasicBlock *BB);
00147   bool calcPointerHeuristics(BasicBlock *BB);
00148   bool calcLoopBranchHeuristics(BasicBlock *BB);
00149   bool calcZeroHeuristics(BasicBlock *BB);
00150   bool calcFloatingPointHeuristics(BasicBlock *BB);
00151   bool calcInvokeHeuristics(BasicBlock *BB);
00152 };
00153 
00154 }
00155 
00156 #endif