Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:13

0001 //===- LazyBranchProbabilityInfo.h - Lazy Branch Probability ----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
0010 // The difference is that with this pass the branch probabilities are not
0011 // computed when the analysis pass is executed but rather when the BPI results
0012 // is explicitly requested by the analysis client.
0013 //
0014 //===----------------------------------------------------------------------===//
0015 
0016 #ifndef LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
0017 #define LLVM_ANALYSIS_LAZYBRANCHPROBABILITYINFO_H
0018 
0019 #include "llvm/Analysis/BranchProbabilityInfo.h"
0020 #include "llvm/Pass.h"
0021 
0022 namespace llvm {
0023 class Function;
0024 class LoopInfo;
0025 class TargetLibraryInfo;
0026 
0027 /// This is an alternative analysis pass to
0028 /// BranchProbabilityInfoWrapperPass.  The difference is that with this pass the
0029 /// branch probabilities are not computed when the analysis pass is executed but
0030 /// rather when the BPI results is explicitly requested by the analysis client.
0031 ///
0032 /// There are some additional requirements for any client pass that wants to use
0033 /// the analysis:
0034 ///
0035 /// 1. The pass needs to initialize dependent passes with:
0036 ///
0037 ///   INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
0038 ///
0039 /// 2. Similarly, getAnalysisUsage should call:
0040 ///
0041 ///   LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU)
0042 ///
0043 /// 3. The computed BPI should be requested with
0044 ///    getAnalysis<LazyBranchProbabilityInfoPass>().getBPI() before LoopInfo
0045 ///    could be invalidated for example by changing the CFG.
0046 ///
0047 /// Note that it is expected that we wouldn't need this functionality for the
0048 /// new PM since with the new PM, analyses are executed on demand.
0049 class LazyBranchProbabilityInfoPass : public FunctionPass {
0050 
0051   /// Wraps a BPI to allow lazy computation of the branch probabilities.
0052   ///
0053   /// A pass that only conditionally uses BPI can uncondtionally require the
0054   /// analysis without paying for the overhead if BPI doesn't end up being used.
0055   class LazyBranchProbabilityInfo {
0056   public:
0057     LazyBranchProbabilityInfo(const Function *F, const LoopInfo *LI,
0058                               const TargetLibraryInfo *TLI)
0059         : F(F), LI(LI), TLI(TLI) {}
0060 
0061     /// Retrieve the BPI with the branch probabilities computed.
0062     BranchProbabilityInfo &getCalculated() {
0063       if (!Calculated) {
0064         assert(F && LI && "call setAnalysis");
0065         BPI.calculate(*F, *LI, TLI, nullptr, nullptr);
0066         Calculated = true;
0067       }
0068       return BPI;
0069     }
0070 
0071     const BranchProbabilityInfo &getCalculated() const {
0072       return const_cast<LazyBranchProbabilityInfo *>(this)->getCalculated();
0073     }
0074 
0075   private:
0076     BranchProbabilityInfo BPI;
0077     bool Calculated = false;
0078     const Function *F;
0079     const LoopInfo *LI;
0080     const TargetLibraryInfo *TLI;
0081   };
0082 
0083   std::unique_ptr<LazyBranchProbabilityInfo> LBPI;
0084 
0085 public:
0086   static char ID;
0087 
0088   LazyBranchProbabilityInfoPass();
0089 
0090   /// Compute and return the branch probabilities.
0091   BranchProbabilityInfo &getBPI() { return LBPI->getCalculated(); }
0092 
0093   /// Compute and return the branch probabilities.
0094   const BranchProbabilityInfo &getBPI() const { return LBPI->getCalculated(); }
0095 
0096   void getAnalysisUsage(AnalysisUsage &AU) const override;
0097 
0098   /// Helper for client passes to set up the analysis usage on behalf of this
0099   /// pass.
0100   static void getLazyBPIAnalysisUsage(AnalysisUsage &AU);
0101 
0102   bool runOnFunction(Function &F) override;
0103   void releaseMemory() override;
0104   void print(raw_ostream &OS, const Module *M) const override;
0105 };
0106 
0107 /// Helper for client passes to initialize dependent passes for LBPI.
0108 void initializeLazyBPIPassPass(PassRegistry &Registry);
0109 
0110 /// Simple trait class that provides a mapping between BPI passes and the
0111 /// corresponding BPInfo.
0112 template <typename PassT> struct BPIPassTrait {
0113   static PassT &getBPI(PassT *P) { return *P; }
0114 };
0115 
0116 template <> struct BPIPassTrait<LazyBranchProbabilityInfoPass> {
0117   static BranchProbabilityInfo &getBPI(LazyBranchProbabilityInfoPass *P) {
0118     return P->getBPI();
0119   }
0120 };
0121 } // namespace llvm
0122 #endif