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"
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.
33 /// There are some additional requirements for any client pass that wants to use
36 /// 1. The pass needs to initialize dependent passes with:
38 /// INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
40 /// 2. Similarly, getAnalysisUsage should call:
42 /// LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU)
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.
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.
50 class LazyBranchProbabilityInfoPass : public FunctionPass {
52 /// Wraps a BPI to allow lazy computation of the branch probabilities.
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 {
58 LazyBranchProbabilityInfo(const Function *F, const LoopInfo *LI)
59 : Calculated(false), F(F), LI(LI) {}
61 /// Retrieve the BPI with the branch probabilities computed.
62 BranchProbabilityInfo &getCalculated() {
64 assert(F && LI && "call setAnalysis");
65 BPI.calculate(*F, *LI);
71 const BranchProbabilityInfo &getCalculated() const {
72 return const_cast<LazyBranchProbabilityInfo *>(this)->getCalculated();
76 BranchProbabilityInfo BPI;
82 std::unique_ptr<LazyBranchProbabilityInfo> LBPI;
87 LazyBranchProbabilityInfoPass();
89 /// \brief Compute and return the branch probabilities.
90 BranchProbabilityInfo &getBPI() { return LBPI->getCalculated(); }
92 /// \brief Compute and return the branch probabilities.
93 const BranchProbabilityInfo &getBPI() const { return LBPI->getCalculated(); }
95 void getAnalysisUsage(AnalysisUsage &AU) const override;
97 /// Helper for client passes to set up the analysis usage on behalf of this
99 static void getLazyBPIAnalysisUsage(AnalysisUsage &AU);
101 bool runOnFunction(Function &F) override;
102 void releaseMemory() override;
103 void print(raw_ostream &OS, const Module *M) const override;
106 /// \brief Helper for client passes to initialize dependent passes for LBPI.
107 void initializeLazyBPIPassPass(PassRegistry &Registry);