LazyBranchProbabilityInfo.h 4.4 KB

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