1 //===- LazyBranchProbabilityInfo.h - Lazy Branch Probability ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
18 #define LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
20 #include "llvm/Analysis/BranchProbabilityInfo.h"
21 #include "llvm/Pass.h"
27 class TargetLibraryInfo
;
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.
34 /// There are some additional requirements for any client pass that wants to use
37 /// 1. The pass needs to initialize dependent passes with:
39 /// INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
41 /// 2. Similarly, getAnalysisUsage should call:
43 /// LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU)
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.
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.
51 class LazyBranchProbabilityInfoPass
: public FunctionPass
{
53 /// Wraps a BPI to allow lazy computation of the branch probabilities.
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
{
59 LazyBranchProbabilityInfo(const Function
*F
, const LoopInfo
*LI
,
60 const TargetLibraryInfo
*TLI
)
61 : Calculated(false), F(F
), LI(LI
), TLI(TLI
) {}
63 /// Retrieve the BPI with the branch probabilities computed.
64 BranchProbabilityInfo
&getCalculated() {
66 assert(F
&& LI
&& "call setAnalysis");
67 BPI
.calculate(*F
, *LI
, TLI
);
73 const BranchProbabilityInfo
&getCalculated() const {
74 return const_cast<LazyBranchProbabilityInfo
*>(this)->getCalculated();
78 BranchProbabilityInfo BPI
;
82 const TargetLibraryInfo
*TLI
;
85 std::unique_ptr
<LazyBranchProbabilityInfo
> LBPI
;
90 LazyBranchProbabilityInfoPass();
92 /// Compute and return the branch probabilities.
93 BranchProbabilityInfo
&getBPI() { return LBPI
->getCalculated(); }
95 /// Compute and return the branch probabilities.
96 const BranchProbabilityInfo
&getBPI() const { return LBPI
->getCalculated(); }
98 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
100 /// Helper for client passes to set up the analysis usage on behalf of this
102 static void getLazyBPIAnalysisUsage(AnalysisUsage
&AU
);
104 bool runOnFunction(Function
&F
) override
;
105 void releaseMemory() override
;
106 void print(raw_ostream
&OS
, const Module
*M
) const override
;
109 /// Helper for client passes to initialize dependent passes for LBPI.
110 void initializeLazyBPIPassPass(PassRegistry
&Registry
);
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
; }
118 template <> struct BPIPassTrait
<LazyBranchProbabilityInfoPass
> {
119 static BranchProbabilityInfo
&getBPI(LazyBranchProbabilityInfoPass
*P
) {