BranchProbabilityInfo.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
  14. #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
  15. #include "llvm/ADT/DenseMap.h"
  16. #include "llvm/ADT/SmallPtrSet.h"
  17. #include "llvm/IR/CFG.h"
  18. #include "llvm/InitializePasses.h"
  19. #include "llvm/Pass.h"
  20. #include "llvm/Support/BranchProbability.h"
  21. namespace llvm {
  22. class LoopInfo;
  23. class raw_ostream;
  24. /// \brief Analysis pass providing branch probability information.
  25. ///
  26. /// This is a function analysis pass which provides information on the relative
  27. /// probabilities of each "edge" in the function's CFG where such an edge is
  28. /// defined by a pair (PredBlock and an index in the successors). The
  29. /// probability of an edge from one block is always relative to the
  30. /// probabilities of other edges from the block. The probabilites of all edges
  31. /// from a block sum to exactly one (100%).
  32. /// We use a pair (PredBlock and an index in the successors) to uniquely
  33. /// identify an edge, since we can have multiple edges from Src to Dst.
  34. /// As an example, we can have a switch which jumps to Dst with value 0 and
  35. /// value 10.
  36. class BranchProbabilityInfo : public FunctionPass {
  37. public:
  38. static char ID;
  39. BranchProbabilityInfo() : FunctionPass(ID) {
  40. initializeBranchProbabilityInfoPass(*PassRegistry::getPassRegistry());
  41. }
  42. void getAnalysisUsage(AnalysisUsage &AU) const override;
  43. bool runOnFunction(Function &F) override;
  44. void releaseMemory() override;
  45. void print(raw_ostream &OS, const Module *M = nullptr) const override;
  46. /// \brief Get an edge's probability, relative to other out-edges of the Src.
  47. ///
  48. /// This routine provides access to the fractional probability between zero
  49. /// (0%) and one (100%) of this edge executing, relative to other edges
  50. /// leaving the 'Src' block. The returned probability is never zero, and can
  51. /// only be one if the source block has only one successor.
  52. BranchProbability getEdgeProbability(const BasicBlock *Src,
  53. unsigned IndexInSuccessors) const;
  54. /// \brief Get the probability of going from Src to Dst.
  55. ///
  56. /// It returns the sum of all probabilities for edges from Src to Dst.
  57. BranchProbability getEdgeProbability(const BasicBlock *Src,
  58. const BasicBlock *Dst) const;
  59. /// \brief Test if an edge is hot relative to other out-edges of the Src.
  60. ///
  61. /// Check whether this edge out of the source block is 'hot'. We define hot
  62. /// as having a relative probability >= 80%.
  63. bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const;
  64. /// \brief Retrieve the hot successor of a block if one exists.
  65. ///
  66. /// Given a basic block, look through its successors and if one exists for
  67. /// which \see isEdgeHot would return true, return that successor block.
  68. BasicBlock *getHotSucc(BasicBlock *BB) const;
  69. /// \brief Print an edge's probability.
  70. ///
  71. /// Retrieves an edge's probability similarly to \see getEdgeProbability, but
  72. /// then prints that probability to the provided stream. That stream is then
  73. /// returned.
  74. raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,
  75. const BasicBlock *Dst) const;
  76. /// \brief Get the raw edge weight calculated for the edge.
  77. ///
  78. /// This returns the raw edge weight. It is guaranteed to fall between 1 and
  79. /// UINT32_MAX. Note that the raw edge weight is not meaningful in isolation.
  80. /// This interface should be very carefully, and primarily by routines that
  81. /// are updating the analysis by later calling setEdgeWeight.
  82. uint32_t getEdgeWeight(const BasicBlock *Src,
  83. unsigned IndexInSuccessors) const;
  84. /// \brief Get the raw edge weight calculated for the block pair.
  85. ///
  86. /// This returns the sum of all raw edge weights from Src to Dst.
  87. /// It is guaranteed to fall between 1 and UINT32_MAX.
  88. uint32_t getEdgeWeight(const BasicBlock *Src, const BasicBlock *Dst) const;
  89. uint32_t getEdgeWeight(const BasicBlock *Src,
  90. succ_const_iterator Dst) const;
  91. /// \brief Set the raw edge weight for a given edge.
  92. ///
  93. /// This allows a pass to explicitly set the edge weight for an edge. It can
  94. /// be used when updating the CFG to update and preserve the branch
  95. /// probability information. Read the implementation of how these edge
  96. /// weights are calculated carefully before using!
  97. void setEdgeWeight(const BasicBlock *Src, unsigned IndexInSuccessors,
  98. uint32_t Weight);
  99. static uint32_t getBranchWeightStackProtector(bool IsLikely) {
  100. return IsLikely ? (1u << 20) - 1 : 1;
  101. }
  102. private:
  103. // Since we allow duplicate edges from one basic block to another, we use
  104. // a pair (PredBlock and an index in the successors) to specify an edge.
  105. typedef std::pair<const BasicBlock *, unsigned> Edge;
  106. // Default weight value. Used when we don't have information about the edge.
  107. // TODO: DEFAULT_WEIGHT makes sense during static predication, when none of
  108. // the successors have a weight yet. But it doesn't make sense when providing
  109. // weight to an edge that may have siblings with non-zero weights. This can
  110. // be handled various ways, but it's probably fine for an edge with unknown
  111. // weight to just "inherit" the non-zero weight of an adjacent successor.
  112. static const uint32_t DEFAULT_WEIGHT = 16;
  113. DenseMap<Edge, uint32_t> Weights;
  114. /// \brief Handle to the LoopInfo analysis.
  115. LoopInfo *LI;
  116. /// \brief Track the last function we run over for printing.
  117. Function *LastF;
  118. /// \brief Track the set of blocks directly succeeded by a returning block.
  119. SmallPtrSet<BasicBlock *, 16> PostDominatedByUnreachable;
  120. /// \brief Track the set of blocks that always lead to a cold call.
  121. SmallPtrSet<BasicBlock *, 16> PostDominatedByColdCall;
  122. /// \brief Get sum of the block successors' weights.
  123. uint32_t getSumForBlock(const BasicBlock *BB) const;
  124. bool calcUnreachableHeuristics(BasicBlock *BB);
  125. bool calcMetadataWeights(BasicBlock *BB);
  126. bool calcColdCallHeuristics(BasicBlock *BB);
  127. bool calcPointerHeuristics(BasicBlock *BB);
  128. bool calcLoopBranchHeuristics(BasicBlock *BB);
  129. bool calcZeroHeuristics(BasicBlock *BB);
  130. bool calcFloatingPointHeuristics(BasicBlock *BB);
  131. bool calcInvokeHeuristics(BasicBlock *BB);
  132. };
  133. }
  134. #endif