LLVM  4.0.0
LazyBlockFrequencyInfo.h
Go to the documentation of this file.
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 
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.
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:
93  bool Calculated;
94  const Function *F;
96  const LoopInfo *LI;
97  };
98 
99  LazyBlockFrequencyInfo LBFI;
100 
101 public:
102  static char ID;
103 
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
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
void initializeLazyBFIPassPass(PassRegistry &Registry)
Helper for client passes to initialize dependent passes for LBFI.
This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
#define F(x, y, z)
Definition: MD5.cpp:51
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
const BlockFrequencyInfo & getBFI() const
Compute and return the block frequencies.
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
static void getLazyBFIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
BranchProbabilityInfo & getBPI()
Compute and return the branch probabilities.
BlockFrequencyInfo & getBFI()
Compute and return the block frequencies.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...