LLVM  8.0.1
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 class TargetLibraryInfo;
28 
29 /// This is an alternative analysis pass to
30 /// BranchProbabilityInfoWrapperPass. The difference is that with this pass the
31 /// branch probabilities are not computed when the analysis pass is executed but
32 /// rather when the BPI results is explicitly requested by the analysis client.
33 ///
34 /// There are some additional requirements for any client pass that wants to use
35 /// the analysis:
36 ///
37 /// 1. The pass needs to initialize dependent passes with:
38 ///
39 /// INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
40 ///
41 /// 2. Similarly, getAnalysisUsage should call:
42 ///
43 /// LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU)
44 ///
45 /// 3. The computed BPI should be requested with
46 /// getAnalysis<LazyBranchProbabilityInfoPass>().getBPI() before LoopInfo
47 /// could be invalidated for example by changing the CFG.
48 ///
49 /// Note that it is expected that we wouldn't need this functionality for the
50 /// new PM since with the new PM, analyses are executed on demand.
52 
53  /// Wraps a BPI to allow lazy computation of the branch probabilities.
54  ///
55  /// A pass that only conditionally uses BPI can uncondtionally require the
56  /// analysis without paying for the overhead if BPI doesn't end up being used.
57  class LazyBranchProbabilityInfo {
58  public:
59  LazyBranchProbabilityInfo(const Function *F, const LoopInfo *LI,
60  const TargetLibraryInfo *TLI)
61  : Calculated(false), F(F), LI(LI), TLI(TLI) {}
62 
63  /// Retrieve the BPI with the branch probabilities computed.
64  BranchProbabilityInfo &getCalculated() {
65  if (!Calculated) {
66  assert(F && LI && "call setAnalysis");
67  BPI.calculate(*F, *LI, TLI);
68  Calculated = true;
69  }
70  return BPI;
71  }
72 
73  const BranchProbabilityInfo &getCalculated() const {
74  return const_cast<LazyBranchProbabilityInfo *>(this)->getCalculated();
75  }
76 
77  private:
79  bool Calculated;
80  const Function *F;
81  const LoopInfo *LI;
82  const TargetLibraryInfo *TLI;
83  };
84 
85  std::unique_ptr<LazyBranchProbabilityInfo> LBPI;
86 
87 public:
88  static char ID;
89 
91 
92  /// Compute and return the branch probabilities.
93  BranchProbabilityInfo &getBPI() { return LBPI->getCalculated(); }
94 
95  /// Compute and return the branch probabilities.
96  const BranchProbabilityInfo &getBPI() const { return LBPI->getCalculated(); }
97 
98  void getAnalysisUsage(AnalysisUsage &AU) const override;
99 
100  /// Helper for client passes to set up the analysis usage on behalf of this
101  /// pass.
102  static void getLazyBPIAnalysisUsage(AnalysisUsage &AU);
103 
104  bool runOnFunction(Function &F) override;
105  void releaseMemory() override;
106  void print(raw_ostream &OS, const Module *M) const override;
107 };
108 
109 /// Helper for client passes to initialize dependent passes for LBPI.
111 
112 /// Simple trait class that provides a mapping between BPI passes and the
113 /// corresponding BPInfo.
114 template <typename PassT> struct BPIPassTrait {
115  static PassT &getBPI(PassT *P) { return *P; }
116 };
117 
120  return P->getBPI();
121  }
122 };
123 }
124 #endif
static BranchProbabilityInfo & getBPI(LazyBranchProbabilityInfoPass *P)
const BranchProbabilityInfo & getBPI() const
Compute and return the branch probabilities.
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 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.
F(f)
Simple trait class that provides a mapping between BPI passes and the corresponding BPInfo...
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 P(N)
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:285
Provides information about what library functions are available for the current target.
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:46
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:39
static PassT & getBPI(PassT *P)