1 //===- LazyBranchProbabilityInfo.cpp - Lazy Branch Probability Analysis ---===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This is an alternative analysis pass to BranchProbabilityInfoWrapperPass. 10 // The difference is that with this pass the branch probabilities are not 11 // computed when the analysis pass is executed but rather when the BPI results 12 // is explicitly requested by the analysis client. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "llvm/Analysis/LazyBranchProbabilityInfo.h" 17 #include "llvm/Analysis/LoopInfo.h" 18 #include "llvm/Analysis/TargetLibraryInfo.h" 19 #include "llvm/IR/Dominators.h" 20 21 using namespace llvm; 22 23 #define DEBUG_TYPE "lazy-branch-prob" 24 25 INITIALIZE_PASS_BEGIN(LazyBranchProbabilityInfoPass, DEBUG_TYPE, 26 "Lazy Branch Probability Analysis", true, true) 27 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 28 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) 29 INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE, 30 "Lazy Branch Probability Analysis", true, true) 31 32 char LazyBranchProbabilityInfoPass::ID = 0; 33 34 LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass() 35 : FunctionPass(ID) { 36 initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry()); 37 } 38 39 void LazyBranchProbabilityInfoPass::print(raw_ostream &OS, 40 const Module *) const { 41 LBPI->getCalculated().print(OS); 42 } 43 44 void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const { 45 // We require DT so it's available when LI is available. The LI updating code 46 // asserts that DT is also present so if we don't make sure that we have DT 47 // here, that assert will trigger. 48 AU.addRequired<DominatorTreeWrapperPass>(); 49 AU.addRequired<LoopInfoWrapperPass>(); 50 AU.addRequired<TargetLibraryInfoWrapperPass>(); 51 AU.setPreservesAll(); 52 } 53 54 void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); } 55 56 bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) { 57 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 58 TargetLibraryInfo &TLI = 59 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F); 60 LBPI = std::make_unique<LazyBranchProbabilityInfo>(&F, &LI, &TLI); 61 return false; 62 } 63 64 void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) { 65 AU.addRequired<LazyBranchProbabilityInfoPass>(); 66 AU.addRequired<LoopInfoWrapperPass>(); 67 AU.addRequired<TargetLibraryInfoWrapperPass>(); 68 } 69 70 void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) { 71 INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass); 72 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 73 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass); 74 } 75