LazyBranchProbabilityInfo.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //===- LazyBranchProbabilityInfo.h - Lazy Branch Probability ----*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
  10. // The difference is that with this pass the branch probabilities are not
  11. // computed when the analysis pass is executed but rather when the BPI results
  12. // is explicitly requested by the analysis client.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
  16. #define LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
  17. #include "llvm/Analysis/BranchProbabilityInfo.h"
  18. #include "llvm/Pass.h"
  19. namespace llvm {
  20. class AnalysisUsage;
  21. class Function;
  22. class LoopInfo;
  23. class TargetLibraryInfo;
  24. /// This is an alternative analysis pass to
  25. /// BranchProbabilityInfoWrapperPass. The difference is that with this pass the
  26. /// branch probabilities are not computed when the analysis pass is executed but
  27. /// rather when the BPI results is explicitly requested by the analysis client.
  28. ///
  29. /// There are some additional requirements for any client pass that wants to use
  30. /// the analysis:
  31. ///
  32. /// 1. The pass needs to initialize dependent passes with:
  33. ///
  34. /// INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
  35. ///
  36. /// 2. Similarly, getAnalysisUsage should call:
  37. ///
  38. /// LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU)
  39. ///
  40. /// 3. The computed BPI should be requested with
  41. /// getAnalysis<LazyBranchProbabilityInfoPass>().getBPI() before LoopInfo
  42. /// could be invalidated for example by changing the CFG.
  43. ///
  44. /// Note that it is expected that we wouldn't need this functionality for the
  45. /// new PM since with the new PM, analyses are executed on demand.
  46. class LazyBranchProbabilityInfoPass : public FunctionPass {
  47. /// Wraps a BPI to allow lazy computation of the branch probabilities.
  48. ///
  49. /// A pass that only conditionally uses BPI can uncondtionally require the
  50. /// analysis without paying for the overhead if BPI doesn't end up being used.
  51. class LazyBranchProbabilityInfo {
  52. public:
  53. LazyBranchProbabilityInfo(const Function *F, const LoopInfo *LI,
  54. const TargetLibraryInfo *TLI)
  55. : Calculated(false), F(F), LI(LI), TLI(TLI) {}
  56. /// Retrieve the BPI with the branch probabilities computed.
  57. BranchProbabilityInfo &getCalculated() {
  58. if (!Calculated) {
  59. assert(F && LI && "call setAnalysis");
  60. BPI.calculate(*F, *LI, TLI, nullptr, nullptr);
  61. Calculated = true;
  62. }
  63. return BPI;
  64. }
  65. const BranchProbabilityInfo &getCalculated() const {
  66. return const_cast<LazyBranchProbabilityInfo *>(this)->getCalculated();
  67. }
  68. private:
  69. BranchProbabilityInfo BPI;
  70. bool Calculated;
  71. const Function *F;
  72. const LoopInfo *LI;
  73. const TargetLibraryInfo *TLI;
  74. };
  75. std::unique_ptr<LazyBranchProbabilityInfo> LBPI;
  76. public:
  77. static char ID;
  78. LazyBranchProbabilityInfoPass();
  79. /// Compute and return the branch probabilities.
  80. BranchProbabilityInfo &getBPI() { return LBPI->getCalculated(); }
  81. /// Compute and return the branch probabilities.
  82. const BranchProbabilityInfo &getBPI() const { return LBPI->getCalculated(); }
  83. void getAnalysisUsage(AnalysisUsage &AU) const override;
  84. /// Helper for client passes to set up the analysis usage on behalf of this
  85. /// pass.
  86. static void getLazyBPIAnalysisUsage(AnalysisUsage &AU);
  87. bool runOnFunction(Function &F) override;
  88. void releaseMemory() override;
  89. void print(raw_ostream &OS, const Module *M) const override;
  90. };
  91. /// Helper for client passes to initialize dependent passes for LBPI.
  92. void initializeLazyBPIPassPass(PassRegistry &Registry);
  93. /// Simple trait class that provides a mapping between BPI passes and the
  94. /// corresponding BPInfo.
  95. template <typename PassT> struct BPIPassTrait {
  96. static PassT &getBPI(PassT *P) { return *P; }
  97. };
  98. template <> struct BPIPassTrait<LazyBranchProbabilityInfoPass> {
  99. static BranchProbabilityInfo &getBPI(LazyBranchProbabilityInfoPass *P) {
  100. return P->getBPI();
  101. }
  102. };
  103. }
  104. #endif