[DAGCombiner] Expand combining of FP logical ops to sign-setting FP ops
[llvm-core.git] / include / llvm / Analysis / LazyBranchProbabilityInfo.h
blob9e6bcfedcbb94486bb23bc2926d3ae34eb9171bc
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.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
18 #define LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
20 #include "llvm/Analysis/BranchProbabilityInfo.h"
21 #include "llvm/Pass.h"
23 namespace llvm {
24 class AnalysisUsage;
25 class Function;
26 class LoopInfo;
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.
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.
51 class LazyBranchProbabilityInfoPass : public FunctionPass {
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) {}
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;
70 return BPI;
73 const BranchProbabilityInfo &getCalculated() const {
74 return const_cast<LazyBranchProbabilityInfo *>(this)->getCalculated();
77 private:
78 BranchProbabilityInfo BPI;
79 bool Calculated;
80 const Function *F;
81 const LoopInfo *LI;
82 const TargetLibraryInfo *TLI;
85 std::unique_ptr<LazyBranchProbabilityInfo> LBPI;
87 public:
88 static char ID;
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
101 /// pass.
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) {
120 return P->getBPI();
124 #endif