LLVM  8.0.1
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 result 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 /// Wraps a BFI to allow lazy computation of the block frequencies.
31 ///
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>
37 public:
39  : Calculated(false), F(nullptr), BPIPass(nullptr), LI(nullptr) {}
40 
41  /// Set up the per-function input.
42  void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass,
43  const LoopInfoT *LI) {
44  this->F = F;
45  this->BPIPass = BPIPass;
46  this->LI = LI;
47  }
48 
49  /// Retrieve the BFI with the block frequencies computed.
50  BlockFrequencyInfoT &getCalculated() {
51  if (!Calculated) {
52  assert(F && BPIPass && LI && "call setAnalysis");
53  BFI.calculate(
55  Calculated = true;
56  }
57  return BFI;
58  }
59 
60  const BlockFrequencyInfoT &getCalculated() const {
61  return const_cast<LazyBlockFrequencyInfo *>(this)->getCalculated();
62  }
63 
64  void releaseMemory() {
65  BFI.releaseMemory();
66  Calculated = false;
67  setAnalysis(nullptr, nullptr, nullptr);
68  }
69 
70 private:
71  BlockFrequencyInfoT BFI;
72  bool Calculated;
73  const FunctionT *F;
74  BranchProbabilityInfoPassT *BPIPass;
75  const LoopInfoT *LI;
76 };
77 
78 /// 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.
82 ///
83 /// There are some additional requirements for any client pass that wants to use
84 /// the analysis:
85 ///
86 /// 1. The pass needs to initialize dependent passes with:
87 ///
88 /// INITIALIZE_PASS_DEPENDENCY(LazyBFIPass)
89 ///
90 /// 2. Similarly, getAnalysisUsage should call:
91 ///
92 /// LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AU)
93 ///
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.
97 ///
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.
100 
102 private:
105  LBFI;
106 
107 public:
108  static char ID;
109 
111 
112  /// Compute and return the block frequencies.
113  BlockFrequencyInfo &getBFI() { return LBFI.getCalculated(); }
114 
115  /// Compute and return the block frequencies.
116  const BlockFrequencyInfo &getBFI() const { return LBFI.getCalculated(); }
117 
118  void getAnalysisUsage(AnalysisUsage &AU) const override;
119 
120  /// Helper for client passes to set up the analysis usage on behalf of this
121  /// pass.
122  static void getLazyBFIAnalysisUsage(AnalysisUsage &AU);
123 
124  bool runOnFunction(Function &F) override;
125  void releaseMemory() override;
126  void print(raw_ostream &OS, const Module *M) const override;
127 };
128 
129 /// Helper for client passes to initialize dependent passes for LBFI.
131 }
132 #endif
This is an alternative analysis pass to BlockFrequencyInfoWrapperPass.
This class represents lattice values for constants.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:45
void initializeLazyBFIPassPass(PassRegistry &Registry)
Helper for client passes to initialize dependent passes for LBFI.
Simple trait class that provides a mapping between BPI passes and the corresponding BPInfo...
This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
const BlockFrequencyInfoT & getCalculated() const
#define F(x, y, z)
Definition: MD5.cpp:55
BlockFrequencyInfoT & getCalculated()
Retrieve the BFI with the block frequencies computed.
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
static bool runOnFunction(Function &F, bool PostInlining)
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:285
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
const BlockFrequencyInfo & getBFI() const
Compute and return the block frequencies.
Wraps a BFI to allow lazy computation of the block frequencies.
BlockFrequencyInfo & getBFI()
Compute and return the block frequencies.
void setAnalysis(const FunctionT *F, BranchProbabilityInfoPassT *BPIPass, const LoopInfoT *LI)
Set up the per-function input.
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:46
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39