LazyBlockFrequencyInfo.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===- LazyBlockFrequencyInfo.h - Lazy Block Frequency Analysis -*- 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 BlockFrequencyInfoWrapperPass. The
  10. // difference is that with this pass the block frequencies are not computed when
  11. // the analysis pass is executed but rather when the BFI result is explicitly
  12. // requested by the analysis client.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
  16. #define LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
  17. #include "llvm/Analysis/BlockFrequencyInfo.h"
  18. #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
  19. #include "llvm/Pass.h"
  20. namespace llvm {
  21. class AnalysisUsage;
  22. class BranchProbabilityInfo;
  23. class Function;
  24. class LoopInfo;
  25. /// Wraps a BFI to allow lazy computation of the block frequencies.
  26. ///
  27. /// A pass that only conditionally uses BFI can uncondtionally require the
  28. /// analysis without paying for the overhead if BFI doesn't end up being used.
  29. template <typename FunctionT, typename BranchProbabilityInfoPassT,
  30. typename LoopInfoT, typename BlockFrequencyInfoT>
  31. class LazyBlockFrequencyInfo {
  32. public:
  33. LazyBlockFrequencyInfo()
  34. : Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {}
  35. /// Set up the per-function input.
  36. void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass,
  37. const LoopInfoT *LI) {
  38. this->F = F;
  39. this->BPIPass = BPIPass;
  40. this->LI = LI;
  41. }
  42. /// Retrieve the BFI with the block frequencies computed.
  43. BlockFrequencyInfoT &getCalculated() {
  44. if (!Calculated) {
  45. assert(F && BPIPass && LI && "call setAnalysis");
  46. BFI.calculate(
  47. *F, BPIPassTrait<BranchProbabilityInfoPassT>::getBPI(BPIPass), *LI);
  48. Calculated = true;
  49. }
  50. return BFI;
  51. }
  52. const BlockFrequencyInfoT &getCalculated() const {
  53. return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
  54. }
  55. void releaseMemory() {
  56. BFI.releaseMemory();
  57. Calculated = false;
  58. setAnalysis(nullptr, nullptr, nullptr);
  59. }
  60. private:
  61. BlockFrequencyInfoT BFI;
  62. bool Calculated;
  63. const FunctionT *F;
  64. BranchProbabilityInfoPassT *BPIPass;
  65. const LoopInfoT *LI;
  66. };
  67. /// This is an alternative analysis pass to
  68. /// BlockFrequencyInfoWrapperPass. The difference is that with this pass the
  69. /// block frequencies are not computed when the analysis pass is executed but
  70. /// rather when the BFI result is explicitly requested by the analysis client.
  71. ///
  72. /// There are some additional requirements for any client pass that wants to use
  73. /// the analysis:
  74. ///
  75. /// 1. The pass needs to initialize dependent passes with:
  76. ///
  77. /// INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
  78. ///
  79. /// 2. Similarly, getAnalysisUsage should call:
  80. ///
  81. /// LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU)
  82. ///
  83. /// 3. The computed BFI should be requested with
  84. /// getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() before either LoopInfo
  85. /// or BPI could be invalidated for example by changing the CFG.
  86. ///
  87. /// Note that it is expected that we wouldn't need this functionality for the
  88. /// new PM since with the new PM, analyses are executed on demand.
  89. class LazyBlockFrequencyInfoPass : public FunctionPass {
  90. private:
  91. LazyBlockFrequencyInfo<Function, LazyBranchProbabilityInfoPass, LoopInfo,
  92. BlockFrequencyInfo>
  93. LBFI;
  94. public:
  95. static char ID;
  96. LazyBlockFrequencyInfoPass();
  97. /// Compute and return the block frequencies.
  98. BlockFrequencyInfo &getBFI() { return LBFI.getCalculated(); }
  99. /// Compute and return the block frequencies.
  100. const BlockFrequencyInfo &getBFI() const { return LBFI.getCalculated(); }
  101. void getAnalysisUsage(AnalysisUsage &AU) const override;
  102. /// Helper for client passes to set up the analysis usage on behalf of this
  103. /// pass.
  104. static void getLazyBFIAnalysisUsage(AnalysisUsage &AU);
  105. bool runOnFunction(Function &F) override;
  106. void releaseMemory() override;
  107. void print(raw_ostream &OS, const Module *M) const override;
  108. };
  109. /// Helper for client passes to initialize dependent passes for LBFI.
  110. void initializeLazyBFIPassPass(PassRegistry &Registry);
  111. }
  112. #endif