LazyBlockFrequencyInfo.h 4.5 KB

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