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/InitializePasses.h" 18 #include "llvm/Pass.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/Support/BranchProbability.h" 21 22 namespace llvm { 23 24 class raw_ostream; 25 26 class BranchProbabilityInfo : public FunctionPass { 27 28 // Default weight value. Used when we don't have information about the edge. 29 // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of 30 // the successors have a weight yet. But it doesn't make sense when providing 31 // weight to an edge that may have siblings with non-zero weights. This can 32 // be handled various ways, but it's probably fine for an edge with unknown 33 // weight to just "inherit" the non-zero weight of an adjacent successor. 34 static const uint32_t DEFAULT_WEIGHT = 16; 35 36 typedef std::pair<const BasicBlock *, const BasicBlock *> Edge; 37 38 DenseMap<Edge, uint32_t> Weights; 39 40 // Get sum of the block successors' weights. 41 uint32_t getSumForBlock(const BasicBlock *BB) const; 42 43 public: 44 static char ID; 45 46 BranchProbabilityInfo() : FunctionPass(ID) { 47 initializeBranchProbabilityInfoPass(*PassRegistry::getPassRegistry()); 48 } 49 50 void getAnalysisUsage(AnalysisUsage &AU) const; 51 52 bool runOnFunction(Function &F); 53 54 // Returned value is between 1 and UINT32_MAX. Look at 55 // BranchProbabilityInfo.cpp for details. 56 uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const; 57 58 // Look at BranchProbabilityInfo.cpp for details. Use it with caution! 59 void setEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst, 60 uint32_t Weight); 61 62 // A 'Hot' edge is an edge which probability is >= 80%. 63 bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const; 64 65 // Return a hot successor for the block BB or null if there isn't one. 66 BasicBlock *getHotSucc(BasicBlock *BB) const; 67 68 // Return a probability as a fraction between 0 (0% probability) and 69 // 1 (100% probability), however the value is never equal to 0, and can be 1 70 // only iff SRC block has only one successor. 71 BranchProbability getEdgeProbability(const BasicBlock *Src, 72 const BasicBlock *Dst) const; 73 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; 79 }; 80 81 } 82 83 #endif 84