1 //===- LazyBlockFrequencyInfo.h - Lazy Block Frequency Analysis -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is an alternative analysis pass to BlockFrequencyInfoWrapperPass. The
11 // difference is that with this pass the block frequencies are not computed when
12 // the analysis pass is executed but rather when the BFI result is explicitly
13 // requested by the analysis client.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
18 #define LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
20 #include "llvm/Analysis/BlockFrequencyInfo.h"
21 #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
22 #include "llvm/Pass.h"
26 class BranchProbabilityInfo;
30 /// Wraps a BFI to allow lazy computation of the block frequencies.
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 {
38 LazyBlockFrequencyInfo()
39 : Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {}
41 /// Set up the per-function input.
42 void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass,
43 const LoopInfoT *LI) {
45 this->BPIPass = BPIPass;
49 /// Retrieve the BFI with the block frequencies computed.
50 BlockFrequencyInfoT &getCalculated() {
52 assert(F && BPIPass && LI && "call setAnalysis");
54 *F, BPIPassTrait<BranchProbabilityInfoPassT>::getBPI(BPIPass), *LI);
60 const BlockFrequencyInfoT &getCalculated() const {
61 return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
64 void releaseMemory() {
67 setAnalysis(nullptr, nullptr, nullptr);
71 BlockFrequencyInfoT BFI;
74 BranchProbabilityInfoPassT *BPIPass;
78 /// \brief This is an alternative analysis pass to
79 /// BlockFrequencyInfoWrapperPass. The difference is that with this pass the
80 /// block frequencies are not computed when the analysis pass is executed but
81 /// rather when the BFI result is explicitly requested by the analysis client.
83 /// There are some additional requirements for any client pass that wants to use
86 /// 1. The pass needs to initialize dependent passes with:
88 /// INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
90 /// 2. Similarly, getAnalysisUsage should call:
92 /// LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU)
94 /// 3. The computed BFI should be requested with
95 /// getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() before either LoopInfo
96 /// or BPI could be invalidated for example by changing the CFG.
98 /// Note that it is expected that we wouldn't need this functionality for the
99 /// new PM since with the new PM, analyses are executed on demand.
101 class LazyBlockFrequencyInfoPass : public FunctionPass {
103 LazyBlockFrequencyInfo<Function, LazyBranchProbabilityInfoPass, LoopInfo,
110 LazyBlockFrequencyInfoPass();
112 /// \brief Compute and return the block frequencies.
113 BlockFrequencyInfo &getBFI() { return LBFI.getCalculated(); }
115 /// \brief Compute and return the block frequencies.
116 const BlockFrequencyInfo &getBFI() const { return LBFI.getCalculated(); }
118 void getAnalysisUsage(AnalysisUsage &AU) const override;
120 /// Helper for client passes to set up the analysis usage on behalf of this
122 static void getLazyBFIAnalysisUsage(AnalysisUsage &AU);
124 bool runOnFunction(Function &F) override;
125 void releaseMemory() override;
126 void print(raw_ostream &OS, const Module *M) const override;
129 /// \brief Helper for client passes to initialize dependent passes for LBFI.
130 void initializeLazyBFIPassPass(PassRegistry &Registry);