BranchProbabilityInfo.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- BranchProbabilityInfo.h - Branch Probability Analysis ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This pass is used to evaluate branch probabilties.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
  18. #define LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/DenseMapInfo.h"
  21. #include "llvm/ADT/DenseSet.h"
  22. #include "llvm/ADT/SmallPtrSet.h"
  23. #include "llvm/IR/BasicBlock.h"
  24. #include "llvm/IR/CFG.h"
  25. #include "llvm/IR/PassManager.h"
  26. #include "llvm/IR/ValueHandle.h"
  27. #include "llvm/Pass.h"
  28. #include "llvm/Support/BranchProbability.h"
  29. #include "llvm/Support/Casting.h"
  30. #include <algorithm>
  31. #include <cassert>
  32. #include <cstdint>
  33. #include <memory>
  34. #include <utility>
  35. namespace llvm {
  36. class Function;
  37. class Loop;
  38. class LoopInfo;
  39. class raw_ostream;
  40. class DominatorTree;
  41. class PostDominatorTree;
  42. class TargetLibraryInfo;
  43. class Value;
  44. /// Analysis providing branch probability information.
  45. ///
  46. /// This is a function analysis which provides information on the relative
  47. /// probabilities of each "edge" in the function's CFG where such an edge is
  48. /// defined by a pair (PredBlock and an index in the successors). The
  49. /// probability of an edge from one block is always relative to the
  50. /// probabilities of other edges from the block. The probabilites of all edges
  51. /// from a block sum to exactly one (100%).
  52. /// We use a pair (PredBlock and an index in the successors) to uniquely
  53. /// identify an edge, since we can have multiple edges from Src to Dst.
  54. /// As an example, we can have a switch which jumps to Dst with value 0 and
  55. /// value 10.
  56. ///
  57. /// Process of computing branch probabilities can be logically viewed as three
  58. /// step process:
  59. ///
  60. /// First, if there is a profile information associated with the branch then
  61. /// it is trivially translated to branch probabilities. There is one exception
  62. /// from this rule though. Probabilities for edges leading to "unreachable"
  63. /// blocks (blocks with the estimated weight not greater than
  64. /// UNREACHABLE_WEIGHT) are evaluated according to static estimation and
  65. /// override profile information. If no branch probabilities were calculated
  66. /// on this step then take the next one.
  67. ///
  68. /// Second, estimate absolute execution weights for each block based on
  69. /// statically known information. Roots of such information are "cold",
  70. /// "unreachable", "noreturn" and "unwind" blocks. Those blocks get their
  71. /// weights set to BlockExecWeight::COLD, BlockExecWeight::UNREACHABLE,
  72. /// BlockExecWeight::NORETURN and BlockExecWeight::UNWIND respectively. Then the
  73. /// weights are propagated to the other blocks up the domination line. In
  74. /// addition, if all successors have estimated weights set then maximum of these
  75. /// weights assigned to the block itself (while this is not ideal heuristic in
  76. /// theory it's simple and works reasonably well in most cases) and the process
  77. /// repeats. Once the process of weights propagation converges branch
  78. /// probabilities are set for all such branches that have at least one successor
  79. /// with the weight set. Default execution weight (BlockExecWeight::DEFAULT) is
  80. /// used for any successors which doesn't have its weight set. For loop back
  81. /// branches we use their weights scaled by loop trip count equal to
  82. /// 'LBH_TAKEN_WEIGHT/LBH_NOTTAKEN_WEIGHT'.
  83. ///
  84. /// Here is a simple example demonstrating how the described algorithm works.
  85. ///
  86. /// BB1
  87. /// / \
  88. /// v v
  89. /// BB2 BB3
  90. /// / \
  91. /// v v
  92. /// ColdBB UnreachBB
  93. ///
  94. /// Initially, ColdBB is associated with COLD_WEIGHT and UnreachBB with
  95. /// UNREACHABLE_WEIGHT. COLD_WEIGHT is set to BB2 as maximum between its
  96. /// successors. BB1 and BB3 has no explicit estimated weights and assumed to
  97. /// have DEFAULT_WEIGHT. Based on assigned weights branches will have the
  98. /// following probabilities:
  99. /// P(BB1->BB2) = COLD_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) =
  100. /// 0xffff / (0xffff + 0xfffff) = 0.0588(5.9%)
  101. /// P(BB1->BB3) = DEFAULT_WEIGHT_WEIGHT/(COLD_WEIGHT + DEFAULT_WEIGHT) =
  102. /// 0xfffff / (0xffff + 0xfffff) = 0.941(94.1%)
  103. /// P(BB2->ColdBB) = COLD_WEIGHT/(COLD_WEIGHT + UNREACHABLE_WEIGHT) = 1(100%)
  104. /// P(BB2->UnreachBB) =
  105. /// UNREACHABLE_WEIGHT/(COLD_WEIGHT+UNREACHABLE_WEIGHT) = 0(0%)
  106. ///
  107. /// If no branch probabilities were calculated on this step then take the next
  108. /// one.
  109. ///
  110. /// Third, apply different kinds of local heuristics for each individual
  111. /// branch until first match. For example probability of a pointer to be null is
  112. /// estimated as PH_TAKEN_WEIGHT/(PH_TAKEN_WEIGHT + PH_NONTAKEN_WEIGHT). If
  113. /// no local heuristic has been matched then branch is left with no explicit
  114. /// probability set and assumed to have default probability.
  115. class BranchProbabilityInfo {
  116. public:
  117. BranchProbabilityInfo() = default;
  118. BranchProbabilityInfo(const Function &F, const LoopInfo &LI,
  119. const TargetLibraryInfo *TLI = nullptr,
  120. DominatorTree *DT = nullptr,
  121. PostDominatorTree *PDT = nullptr) {
  122. calculate(F, LI, TLI, DT, PDT);
  123. }
  124. BranchProbabilityInfo(BranchProbabilityInfo &&Arg)
  125. : Probs(std::move(Arg.Probs)), LastF(Arg.LastF),
  126. EstimatedBlockWeight(std::move(Arg.EstimatedBlockWeight)) {}
  127. BranchProbabilityInfo(const BranchProbabilityInfo &) = delete;
  128. BranchProbabilityInfo &operator=(const BranchProbabilityInfo &) = delete;
  129. BranchProbabilityInfo &operator=(BranchProbabilityInfo &&RHS) {
  130. releaseMemory();
  131. Probs = std::move(RHS.Probs);
  132. EstimatedBlockWeight = std::move(RHS.EstimatedBlockWeight);
  133. return *this;
  134. }
  135. bool invalidate(Function &, const PreservedAnalyses &PA,
  136. FunctionAnalysisManager::Invalidator &);
  137. void releaseMemory();
  138. void print(raw_ostream &OS) const;
  139. /// Get an edge's probability, relative to other out-edges of the Src.
  140. ///
  141. /// This routine provides access to the fractional probability between zero
  142. /// (0%) and one (100%) of this edge executing, relative to other edges
  143. /// leaving the 'Src' block. The returned probability is never zero, and can
  144. /// only be one if the source block has only one successor.
  145. BranchProbability getEdgeProbability(const BasicBlock *Src,
  146. unsigned IndexInSuccessors) const;
  147. /// Get the probability of going from Src to Dst.
  148. ///
  149. /// It returns the sum of all probabilities for edges from Src to Dst.
  150. BranchProbability getEdgeProbability(const BasicBlock *Src,
  151. const BasicBlock *Dst) const;
  152. BranchProbability getEdgeProbability(const BasicBlock *Src,
  153. const_succ_iterator Dst) const;
  154. /// Test if an edge is hot relative to other out-edges of the Src.
  155. ///
  156. /// Check whether this edge out of the source block is 'hot'. We define hot
  157. /// as having a relative probability >= 80%.
  158. bool isEdgeHot(const BasicBlock *Src, const BasicBlock *Dst) const;
  159. /// Retrieve the hot successor of a block if one exists.
  160. ///
  161. /// Given a basic block, look through its successors and if one exists for
  162. /// which \see isEdgeHot would return true, return that successor block.
  163. const BasicBlock *getHotSucc(const BasicBlock *BB) const;
  164. /// Print an edge's probability.
  165. ///
  166. /// Retrieves an edge's probability similarly to \see getEdgeProbability, but
  167. /// then prints that probability to the provided stream. That stream is then
  168. /// returned.
  169. raw_ostream &printEdgeProbability(raw_ostream &OS, const BasicBlock *Src,
  170. const BasicBlock *Dst) const;
  171. public:
  172. /// Set the raw probabilities for all edges from the given block.
  173. ///
  174. /// This allows a pass to explicitly set edge probabilities for a block. It
  175. /// can be used when updating the CFG to update the branch probability
  176. /// information.
  177. void setEdgeProbability(const BasicBlock *Src,
  178. const SmallVectorImpl<BranchProbability> &Probs);
  179. /// Copy outgoing edge probabilities from \p Src to \p Dst.
  180. ///
  181. /// This allows to keep probabilities unset for the destination if they were
  182. /// unset for source.
  183. void copyEdgeProbabilities(BasicBlock *Src, BasicBlock *Dst);
  184. static BranchProbability getBranchProbStackProtector(bool IsLikely) {
  185. static const BranchProbability LikelyProb((1u << 20) - 1, 1u << 20);
  186. return IsLikely ? LikelyProb : LikelyProb.getCompl();
  187. }
  188. void calculate(const Function &F, const LoopInfo &LI,
  189. const TargetLibraryInfo *TLI, DominatorTree *DT,
  190. PostDominatorTree *PDT);
  191. /// Forget analysis results for the given basic block.
  192. void eraseBlock(const BasicBlock *BB);
  193. // Data structure to track SCCs for handling irreducible loops.
  194. class SccInfo {
  195. // Enum of types to classify basic blocks in SCC. Basic block belonging to
  196. // SCC is 'Inner' until it is either 'Header' or 'Exiting'. Note that a
  197. // basic block can be 'Header' and 'Exiting' at the same time.
  198. enum SccBlockType {
  199. Inner = 0x0,
  200. Header = 0x1,
  201. Exiting = 0x2,
  202. };
  203. // Map of basic blocks to SCC IDs they belong to. If basic block doesn't
  204. // belong to any SCC it is not in the map.
  205. using SccMap = DenseMap<const BasicBlock *, int>;
  206. // Each basic block in SCC is attributed with one or several types from
  207. // SccBlockType. Map value has uint32_t type (instead of SccBlockType)
  208. // since basic block may be for example "Header" and "Exiting" at the same
  209. // time and we need to be able to keep more than one value from
  210. // SccBlockType.
  211. using SccBlockTypeMap = DenseMap<const BasicBlock *, uint32_t>;
  212. // Vector containing classification of basic blocks for all SCCs where i'th
  213. // vector element corresponds to SCC with ID equal to i.
  214. using SccBlockTypeMaps = std::vector<SccBlockTypeMap>;
  215. SccMap SccNums;
  216. SccBlockTypeMaps SccBlocks;
  217. public:
  218. explicit SccInfo(const Function &F);
  219. /// If \p BB belongs to some SCC then ID of that SCC is returned, otherwise
  220. /// -1 is returned. If \p BB belongs to more than one SCC at the same time
  221. /// result is undefined.
  222. int getSCCNum(const BasicBlock *BB) const;
  223. /// Returns true if \p BB is a 'header' block in SCC with \p SccNum ID,
  224. /// false otherwise.
  225. bool isSCCHeader(const BasicBlock *BB, int SccNum) const {
  226. return getSccBlockType(BB, SccNum) & Header;
  227. }
  228. /// Returns true if \p BB is an 'exiting' block in SCC with \p SccNum ID,
  229. /// false otherwise.
  230. bool isSCCExitingBlock(const BasicBlock *BB, int SccNum) const {
  231. return getSccBlockType(BB, SccNum) & Exiting;
  232. }
  233. /// Fills in \p Enters vector with all such blocks that don't belong to
  234. /// SCC with \p SccNum ID but there is an edge to a block belonging to the
  235. /// SCC.
  236. void getSccEnterBlocks(int SccNum,
  237. SmallVectorImpl<BasicBlock *> &Enters) const;
  238. /// Fills in \p Exits vector with all such blocks that don't belong to
  239. /// SCC with \p SccNum ID but there is an edge from a block belonging to the
  240. /// SCC.
  241. void getSccExitBlocks(int SccNum,
  242. SmallVectorImpl<BasicBlock *> &Exits) const;
  243. private:
  244. /// Returns \p BB's type according to classification given by SccBlockType
  245. /// enum. Please note that \p BB must belong to SSC with \p SccNum ID.
  246. uint32_t getSccBlockType(const BasicBlock *BB, int SccNum) const;
  247. /// Calculates \p BB's type and stores it in internal data structures for
  248. /// future use. Please note that \p BB must belong to SSC with \p SccNum ID.
  249. void calculateSccBlockType(const BasicBlock *BB, int SccNum);
  250. };
  251. private:
  252. // We need to store CallbackVH's in order to correctly handle basic block
  253. // removal.
  254. class BasicBlockCallbackVH final : public CallbackVH {
  255. BranchProbabilityInfo *BPI;
  256. void deleted() override {
  257. assert(BPI != nullptr);
  258. BPI->eraseBlock(cast<BasicBlock>(getValPtr()));
  259. }
  260. public:
  261. BasicBlockCallbackVH(const Value *V, BranchProbabilityInfo *BPI = nullptr)
  262. : CallbackVH(const_cast<Value *>(V)), BPI(BPI) {}
  263. };
  264. /// Pair of Loop and SCC ID number. Used to unify handling of normal and
  265. /// SCC based loop representations.
  266. using LoopData = std::pair<Loop *, int>;
  267. /// Helper class to keep basic block along with its loop data information.
  268. class LoopBlock {
  269. public:
  270. explicit LoopBlock(const BasicBlock *BB, const LoopInfo &LI,
  271. const SccInfo &SccI);
  272. const BasicBlock *getBlock() const { return BB; }
  273. BasicBlock *getBlock() { return const_cast<BasicBlock *>(BB); }
  274. LoopData getLoopData() const { return LD; }
  275. Loop *getLoop() const { return LD.first; }
  276. int getSccNum() const { return LD.second; }
  277. bool belongsToLoop() const { return getLoop() || getSccNum() != -1; }
  278. bool belongsToSameLoop(const LoopBlock &LB) const {
  279. return (LB.getLoop() && getLoop() == LB.getLoop()) ||
  280. (LB.getSccNum() != -1 && getSccNum() == LB.getSccNum());
  281. }
  282. private:
  283. const BasicBlock *const BB = nullptr;
  284. LoopData LD = {nullptr, -1};
  285. };
  286. // Pair of LoopBlocks representing an edge from first to second block.
  287. using LoopEdge = std::pair<const LoopBlock &, const LoopBlock &>;
  288. DenseSet<BasicBlockCallbackVH, DenseMapInfo<Value*>> Handles;
  289. // Since we allow duplicate edges from one basic block to another, we use
  290. // a pair (PredBlock and an index in the successors) to specify an edge.
  291. using Edge = std::pair<const BasicBlock *, unsigned>;
  292. DenseMap<Edge, BranchProbability> Probs;
  293. /// Track the last function we run over for printing.
  294. const Function *LastF = nullptr;
  295. const LoopInfo *LI = nullptr;
  296. /// Keeps information about all SCCs in a function.
  297. std::unique_ptr<const SccInfo> SccI;
  298. /// Keeps mapping of a basic block to its estimated weight.
  299. SmallDenseMap<const BasicBlock *, uint32_t> EstimatedBlockWeight;
  300. /// Keeps mapping of a loop to estimated weight to enter the loop.
  301. SmallDenseMap<LoopData, uint32_t> EstimatedLoopWeight;
  302. /// Helper to construct LoopBlock for \p BB.
  303. LoopBlock getLoopBlock(const BasicBlock *BB) const {
  304. return LoopBlock(BB, *LI, *SccI.get());
  305. }
  306. /// Returns true if destination block belongs to some loop and source block is
  307. /// either doesn't belong to any loop or belongs to a loop which is not inner
  308. /// relative to the destination block.
  309. bool isLoopEnteringEdge(const LoopEdge &Edge) const;
  310. /// Returns true if source block belongs to some loop and destination block is
  311. /// either doesn't belong to any loop or belongs to a loop which is not inner
  312. /// relative to the source block.
  313. bool isLoopExitingEdge(const LoopEdge &Edge) const;
  314. /// Returns true if \p Edge is either enters to or exits from some loop, false
  315. /// in all other cases.
  316. bool isLoopEnteringExitingEdge(const LoopEdge &Edge) const;
  317. /// Returns true if source and destination blocks belongs to the same loop and
  318. /// destination block is loop header.
  319. bool isLoopBackEdge(const LoopEdge &Edge) const;
  320. // Fills in \p Enters vector with all "enter" blocks to a loop \LB belongs to.
  321. void getLoopEnterBlocks(const LoopBlock &LB,
  322. SmallVectorImpl<BasicBlock *> &Enters) const;
  323. // Fills in \p Exits vector with all "exit" blocks from a loop \LB belongs to.
  324. void getLoopExitBlocks(const LoopBlock &LB,
  325. SmallVectorImpl<BasicBlock *> &Exits) const;
  326. /// Returns estimated weight for \p BB. None if \p BB has no estimated weight.
  327. Optional<uint32_t> getEstimatedBlockWeight(const BasicBlock *BB) const;
  328. /// Returns estimated weight to enter \p L. In other words it is weight of
  329. /// loop's header block not scaled by trip count. Returns None if \p L has no
  330. /// no estimated weight.
  331. Optional<uint32_t> getEstimatedLoopWeight(const LoopData &L) const;
  332. /// Return estimated weight for \p Edge. Returns None if estimated weight is
  333. /// unknown.
  334. Optional<uint32_t> getEstimatedEdgeWeight(const LoopEdge &Edge) const;
  335. /// Iterates over all edges leading from \p SrcBB to \p Successors and
  336. /// returns maximum of all estimated weights. If at least one edge has unknown
  337. /// estimated weight None is returned.
  338. template <class IterT>
  339. Optional<uint32_t>
  340. getMaxEstimatedEdgeWeight(const LoopBlock &SrcBB,
  341. iterator_range<IterT> Successors) const;
  342. /// If \p LoopBB has no estimated weight then set it to \p BBWeight and
  343. /// return true. Otherwise \p BB's weight remains unchanged and false is
  344. /// returned. In addition all blocks/loops that might need their weight to be
  345. /// re-estimated are put into BlockWorkList/LoopWorkList.
  346. bool updateEstimatedBlockWeight(LoopBlock &LoopBB, uint32_t BBWeight,
  347. SmallVectorImpl<BasicBlock *> &BlockWorkList,
  348. SmallVectorImpl<LoopBlock> &LoopWorkList);
  349. /// Starting from \p LoopBB (including \p LoopBB itself) propagate \p BBWeight
  350. /// up the domination tree.
  351. void propagateEstimatedBlockWeight(const LoopBlock &LoopBB, DominatorTree *DT,
  352. PostDominatorTree *PDT, uint32_t BBWeight,
  353. SmallVectorImpl<BasicBlock *> &WorkList,
  354. SmallVectorImpl<LoopBlock> &LoopWorkList);
  355. /// Returns block's weight encoded in the IR.
  356. Optional<uint32_t> getInitialEstimatedBlockWeight(const BasicBlock *BB);
  357. // Computes estimated weights for all blocks in \p F.
  358. void computeEestimateBlockWeight(const Function &F, DominatorTree *DT,
  359. PostDominatorTree *PDT);
  360. /// Based on computed weights by \p computeEstimatedBlockWeight set
  361. /// probabilities on branches.
  362. bool calcEstimatedHeuristics(const BasicBlock *BB);
  363. bool calcMetadataWeights(const BasicBlock *BB);
  364. bool calcPointerHeuristics(const BasicBlock *BB);
  365. bool calcZeroHeuristics(const BasicBlock *BB, const TargetLibraryInfo *TLI);
  366. bool calcFloatingPointHeuristics(const BasicBlock *BB);
  367. };
  368. /// Analysis pass which computes \c BranchProbabilityInfo.
  369. class BranchProbabilityAnalysis
  370. : public AnalysisInfoMixin<BranchProbabilityAnalysis> {
  371. friend AnalysisInfoMixin<BranchProbabilityAnalysis>;
  372. static AnalysisKey Key;
  373. public:
  374. /// Provide the result type for this analysis pass.
  375. using Result = BranchProbabilityInfo;
  376. /// Run the analysis pass over a function and produce BPI.
  377. BranchProbabilityInfo run(Function &F, FunctionAnalysisManager &AM);
  378. };
  379. /// Printer pass for the \c BranchProbabilityAnalysis results.
  380. class BranchProbabilityPrinterPass
  381. : public PassInfoMixin<BranchProbabilityPrinterPass> {
  382. raw_ostream &OS;
  383. public:
  384. explicit BranchProbabilityPrinterPass(raw_ostream &OS) : OS(OS) {}
  385. PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
  386. };
  387. /// Legacy analysis pass which computes \c BranchProbabilityInfo.
  388. class BranchProbabilityInfoWrapperPass : public FunctionPass {
  389. BranchProbabilityInfo BPI;
  390. public:
  391. static char ID;
  392. BranchProbabilityInfoWrapperPass();
  393. BranchProbabilityInfo &getBPI() { return BPI; }
  394. const BranchProbabilityInfo &getBPI() const { return BPI; }
  395. void getAnalysisUsage(AnalysisUsage &AU) const override;
  396. bool runOnFunction(Function &F) override;
  397. void releaseMemory() override;
  398. void print(raw_ostream &OS, const Module *M = nullptr) const override;
  399. };
  400. } // end namespace llvm
  401. #endif // LLVM_ANALYSIS_BRANCHPROBABILITYINFO_H
  402. #ifdef __GNUC__
  403. #pragma GCC diagnostic pop
  404. #endif