]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Analysis/LazyBlockFrequencyInfo.h
MFV r317581: less v491.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Analysis / LazyBlockFrequencyInfo.h
1 //===- LazyBlockFrequencyInfo.h - Lazy Block Frequency Analysis -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
18 #define LLVM_ANALYSIS_LAZYBLOCKFREQUENCYINFO_H
19
20 #include "llvm/Analysis/BlockFrequencyInfo.h"
21 #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
22 #include "llvm/Pass.h"
23
24 namespace llvm {
25 class AnalysisUsage;
26 class BranchProbabilityInfo;
27 class Function;
28 class LoopInfo;
29
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.
34 ///
35 /// There are some additional requirements for any client pass that wants to use
36 /// the analysis:
37 ///
38 /// 1. The pass needs to initialize dependent passes with:
39 ///
40 ///   INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
41 ///
42 /// 2. Similarly, getAnalysisUsage should call:
43 ///
44 ///   LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU)
45 ///
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.
49 ///
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 {
53
54   /// Wraps a BFI to allow lazy computation of the block frequencies.
55   ///
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 {
59   public:
60     LazyBlockFrequencyInfo()
61         : Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {}
62
63     /// Set up the per-function input.
64     void setAnalysis(const Function *F, LazyBranchProbabilityInfoPass *BPIPass,
65                      const LoopInfo *LI) {
66       this->F = F;
67       this->BPIPass = BPIPass;
68       this->LI = LI;
69     }
70
71     /// Retrieve the BFI with the block frequencies computed.
72     BlockFrequencyInfo &getCalculated() {
73       if (!Calculated) {
74         assert(F && BPIPass && LI && "call setAnalysis");
75         BFI.calculate(*F, BPIPass->getBPI(), *LI);
76         Calculated = true;
77       }
78       return BFI;
79     }
80
81     const BlockFrequencyInfo &getCalculated() const {
82       return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
83     }
84
85     void releaseMemory() {
86       BFI.releaseMemory();
87       Calculated = false;
88       setAnalysis(nullptr, nullptr, nullptr);
89     }
90
91   private:
92     BlockFrequencyInfo BFI;
93     bool Calculated;
94     const Function *F;
95     LazyBranchProbabilityInfoPass *BPIPass;
96     const LoopInfo *LI;
97   };
98
99   LazyBlockFrequencyInfo LBFI;
100
101 public:
102   static char ID;
103
104   LazyBlockFrequencyInfoPass();
105
106   /// \brief Compute and return the block frequencies.
107   BlockFrequencyInfo &getBFI() { return LBFI.getCalculated(); }
108
109   /// \brief Compute and return the block frequencies.
110   const BlockFrequencyInfo &getBFI() const { return LBFI.getCalculated(); }
111
112   void getAnalysisUsage(AnalysisUsage &AU) const override;
113
114   /// Helper for client passes to set up the analysis usage on behalf of this
115   /// pass.
116   static void getLazyBFIAnalysisUsage(AnalysisUsage &AU);
117
118   bool runOnFunction(Function &F) override;
119   void releaseMemory() override;
120   void print(raw_ostream &OS, const Module *M) const override;
121 };
122
123 /// \brief Helper for client passes to initialize dependent passes for LBFI.
124 void initializeLazyBFIPassPass(PassRegistry &Registry);
125 }
126 #endif