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 results 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 /// \brief This is an alternative analysis pass to
31 /// BlockFrequencyInfoWrapperPass. The difference is that with this pass the
32 /// block frequencies are not computed when the analysis pass is executed but
33 /// rather when the BFI results is explicitly requested by the analysis client.
35 /// There are some additional requirements for any client pass that wants to use
38 /// 1. The pass needs to initialize dependent passes with:
40 /// INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
42 /// 2. Similarly, getAnalysisUsage should call:
44 /// LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU)
46 /// 3. The computed BFI should be requested with
47 /// getAnalysis<LazyBlockFrequencyInfoPass>().getBFI() before either LoopInfo
48 /// or BPI could be invalidated for example by changing the CFG.
50 /// Note that it is expected that we wouldn't need this functionality for the
51 /// new PM since with the new PM, analyses are executed on demand.
52 class LazyBlockFrequencyInfoPass : public FunctionPass {
54 /// Wraps a BFI to allow lazy computation of the block frequencies.
56 /// A pass that only conditionally uses BFI can uncondtionally require the
57 /// analysis without paying for the overhead if BFI doesn't end up being used.
58 class LazyBlockFrequencyInfo {
60 LazyBlockFrequencyInfo()
61 : Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {}
63 /// Set up the per-function input.
64 void setAnalysis(const Function *F, LazyBranchProbabilityInfoPass *BPIPass,
67 this->BPIPass = BPIPass;
71 /// Retrieve the BFI with the block frequencies computed.
72 BlockFrequencyInfo &getCalculated() {
74 assert(F && BPIPass && LI && "call setAnalysis");
75 BFI.calculate(*F, BPIPass->getBPI(), *LI);
81 const BlockFrequencyInfo &getCalculated() const {
82 return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
85 void releaseMemory() {
88 setAnalysis(nullptr, nullptr, nullptr);
92 BlockFrequencyInfo BFI;
95 LazyBranchProbabilityInfoPass *BPIPass;
99 LazyBlockFrequencyInfo LBFI;
104 LazyBlockFrequencyInfoPass();
106 /// \brief Compute and return the block frequencies.
107 BlockFrequencyInfo &getBFI() { return LBFI.getCalculated(); }
109 /// \brief Compute and return the block frequencies.
110 const BlockFrequencyInfo &getBFI() const { return LBFI.getCalculated(); }
112 void getAnalysisUsage(AnalysisUsage &AU) const override;
114 /// Helper for client passes to set up the analysis usage on behalf of this
116 static void getLazyBFIAnalysisUsage(AnalysisUsage &AU);
118 bool runOnFunction(Function &F) override;
119 void releaseMemory() override;
120 void print(raw_ostream &OS, const Module *M) const override;
123 /// \brief Helper for client passes to initialize dependent passes for LBFI.
124 void initializeLazyBFIPassPass(PassRegistry &Registry);