Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:13

0001 //===- LazyBlockFrequencyInfo.h - Lazy Block Frequency Analysis -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.  The
0010 // difference is that with this pass the block frequencies are not computed when
0011 // the analysis pass is executed but rather when the BFI result is explicitly
0012 // requested by the analysis client.
0013 //
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
0017 #define LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
0018 
0019 #include "llvm/Analysis/BlockFrequencyInfo.h"
0020 #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
0021 #include "llvm/Pass.h"
0022 
0023 namespace llvm {
0024 class Function;
0025 class LoopInfo;
0026 
0027 /// Wraps a BFI to allow lazy computation of the block frequencies.
0028 ///
0029 /// A pass that only conditionally uses BFI can uncondtionally require the
0030 /// analysis without paying for the overhead if BFI doesn't end up being used.
0031 template <typename FunctionT, typename BranchProbabilityInfoPassT,
0032           typename LoopInfoT, typename BlockFrequencyInfoT>
0033 class LazyBlockFrequencyInfo {
0034 public:
0035   LazyBlockFrequencyInfo() = default;
0036 
0037   /// Set up the per-function input.
0038   void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass,
0039                    const LoopInfoT *LI) {
0040     this->F = F;
0041     this->BPIPass = BPIPass;
0042     this->LI = LI;
0043   }
0044 
0045   /// Retrieve the BFI with the block frequencies computed.
0046   BlockFrequencyInfoT &getCalculated() {
0047     if (!Calculated) {
0048       assert(F && BPIPass && LI && "call setAnalysis");
0049       BFI.calculate(
0050           *F, BPIPassTrait<BranchProbabilityInfoPassT>::getBPI(BPIPass), *LI);
0051       Calculated = true;
0052     }
0053     return BFI;
0054   }
0055 
0056   const BlockFrequencyInfoT &getCalculated() const {
0057     return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
0058   }
0059 
0060   void releaseMemory() {
0061     BFI.releaseMemory();
0062     Calculated = false;
0063     setAnalysis(nullptr, nullptr, nullptr);
0064   }
0065 
0066 private:
0067   BlockFrequencyInfoT BFI;
0068   bool Calculated = false;
0069   const FunctionT *F = nullptr;
0070   BranchProbabilityInfoPassT *BPIPass = nullptr;
0071   const LoopInfoT *LI = nullptr;
0072 };
0073 
0074 /// This is an alternative analysis pass to
0075 /// BlockFrequencyInfoWrapperPass.  The difference is that with this pass the
0076 /// block frequencies are not computed when the analysis pass is executed but
0077 /// rather when the BFI result is explicitly requested by the analysis client.
0078 ///
0079 /// There are some additional requirements for any client pass that wants to use
0080 /// the analysis:
0081 ///
0082 /// 1. The pass needs to initialize dependent passes with:
0083 ///
0084 ///   INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
0085 ///
0086 /// 2. Similarly, getAnalysisUsage should call:
0087 ///
0088 ///   LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU)
0089 ///
0090 /// 3. The computed BFI should be requested with
0091 ///    getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() before either LoopInfo
0092 ///    or BPI could be invalidated for example by changing the CFG.
0093 ///
0094 /// Note that it is expected that we wouldn't need this functionality for the
0095 /// new PM since with the new PM, analyses are executed on demand.
0096 
0097 class LazyBlockFrequencyInfoPass : public FunctionPass {
0098 private:
0099   LazyBlockFrequencyInfo<Function, LazyBranchProbabilityInfoPass, LoopInfo,
0100                          BlockFrequencyInfo>
0101       LBFI;
0102 
0103 public:
0104   static char ID;
0105 
0106   LazyBlockFrequencyInfoPass();
0107 
0108   /// Compute and return the block frequencies.
0109   BlockFrequencyInfo &getBFI() { return LBFI.getCalculated(); }
0110 
0111   /// Compute and return the block frequencies.
0112   const BlockFrequencyInfo &getBFI() const { return LBFI.getCalculated(); }
0113 
0114   void getAnalysisUsage(AnalysisUsage &AU) const override;
0115 
0116   /// Helper for client passes to set up the analysis usage on behalf of this
0117   /// pass.
0118   static void getLazyBFIAnalysisUsage(AnalysisUsage &AU);
0119 
0120   bool runOnFunction(Function &F) override;
0121   void releaseMemory() override;
0122   void print(raw_ostream &OS, const Module *M) const override;
0123 };
0124 
0125 /// Helper for client passes to initialize dependent passes for LBFI.
0126 void initializeLazyBFIPassPass(PassRegistry &Registry);
0127 } // namespace llvm
0128 #endif