LazyBlockFrequencyInfo.h 4.5 KB

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