LLVM  4.0.0
LazyBranchProbabilityInfo.h
Go to the documentation of this file.
1 //===- LazyBranchProbabilityInfo.h - Lazy Branch Probability ----*- 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 BranchProbabilityInfoWrapperPass.
11 // The difference is that with this pass the branch probabilities are not
12 // computed when the analysis pass is executed but rather when the BPI results
13 // is explicitly requested by the analysis client.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
18 #define LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
19 
21 #include "llvm/Pass.h"
22 
23 namespace llvm {
24 class AnalysisUsage;
25 class Function;
26 class LoopInfo;
27 
28 /// \brief This is an alternative analysis pass to
29 /// BranchProbabilityInfoWrapperPass. The difference is that with this pass the
30 /// branch probabilities are not computed when the analysis pass is executed but
31 /// rather when the BPI results is explicitly requested by the analysis client.
32 ///
33 /// There are some additional requirements for any client pass that wants to use
34 /// the analysis:
35 ///
36 /// 1. The pass needs to initialize dependent passes with:
37 ///
38 /// INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
39 ///
40 /// 2. Similarly, getAnalysisUsage should call:
41 ///
42 /// LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU)
43 ///
44 /// 3. The computed BPI should be requested with
45 /// getAnalysis<LazyBranchProbabilityInfoPass>().getBPI() before LoopInfo
46 /// could be invalidated for example by changing the CFG.
47 ///
48 /// Note that it is expected that we wouldn't need this functionality for the
49 /// new PM since with the new PM, analyses are executed on demand.
51 
52  /// Wraps a BPI to allow lazy computation of the branch probabilities.
53  ///
54  /// A pass that only conditionally uses BPI can uncondtionally require the
55  /// analysis without paying for the overhead if BPI doesn't end up being used.
56  class LazyBranchProbabilityInfo {
57  public:
58  LazyBranchProbabilityInfo(const Function *F, const LoopInfo *LI)
59  : Calculated(false), F(F), LI(LI) {}
60 
61  /// Retrieve the BPI with the branch probabilities computed.
62  BranchProbabilityInfo &getCalculated() {
63  if (!Calculated) {
64  assert(F && LI && "call setAnalysis");
65  BPI.calculate(*F, *LI);
66  Calculated = true;
67  }
68  return BPI;
69  }
70 
71  const BranchProbabilityInfo &getCalculated() const {
72  return const_cast<LazyBranchProbabilityInfo *>(this)->getCalculated();
73  }
74 
75  private:
77  bool Calculated;
78  const Function *F;
79  const LoopInfo *LI;
80  };
81 
82  std::unique_ptr<LazyBranchProbabilityInfo> LBPI;
83 
84 public:
85  static char ID;
86 
88 
89  /// \brief Compute and return the branch probabilities.
90  BranchProbabilityInfo &getBPI() { return LBPI->getCalculated(); }
91 
92  /// \brief Compute and return the branch probabilities.
93  const BranchProbabilityInfo &getBPI() const { return LBPI->getCalculated(); }
94 
95  void getAnalysisUsage(AnalysisUsage &AU) const override;
96 
97  /// Helper for client passes to set up the analysis usage on behalf of this
98  /// pass.
99  static void getLazyBPIAnalysisUsage(AnalysisUsage &AU);
100 
101  bool runOnFunction(Function &F) override;
102  void releaseMemory() override;
103  void print(raw_ostream &OS, const Module *M) const override;
104 };
105 
106 /// \brief Helper for client passes to initialize dependent passes for LBPI.
107 void initializeLazyBPIPassPass(PassRegistry &Registry);
108 }
109 #endif
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
static void getLazyBPIAnalysisUsage(AnalysisUsage &AU)
Helper for client passes to set up the analysis usage on behalf of this pass.
const BranchProbabilityInfo & getBPI() const
Compute and return the branch probabilities.
This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
#define F(x, y, z)
Definition: MD5.cpp:51
void initializeLazyBPIPassPass(PassRegistry &Registry)
Helper for client passes to initialize dependent passes for LBPI.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass...
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
BranchProbabilityInfo & getBPI()
Compute and return the branch probabilities.
Analysis providing branch probability information.
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