1 //===- LazyBranchProbabilityInfo.cpp - Lazy Branch Probability Analysis ---===//
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
7 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/Analysis/TargetLibraryInfo.h"
19 #include "llvm/IR/Dominators.h"
23 #define DEBUG_TYPE "lazy-branch-prob"
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)
32 char LazyBranchProbabilityInfoPass::ID
= 0;
34 LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass()
36 initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry());
39 void LazyBranchProbabilityInfoPass::print(raw_ostream
&OS
,
40 const Module
*) const {
41 LBPI
->getCalculated().print(OS
);
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
>();
54 void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI
.reset(); }
56 bool LazyBranchProbabilityInfoPass::runOnFunction(Function
&F
) {
57 LoopInfo
&LI
= getAnalysis
<LoopInfoWrapperPass
>().getLoopInfo();
58 TargetLibraryInfo
&TLI
= getAnalysis
<TargetLibraryInfoWrapperPass
>().getTLI();
59 LBPI
= llvm::make_unique
<LazyBranchProbabilityInfo
>(&F
, &LI
, &TLI
);
63 void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage
&AU
) {
64 AU
.addRequired
<LazyBranchProbabilityInfoPass
>();
65 AU
.addRequired
<LoopInfoWrapperPass
>();
66 AU
.addRequired
<TargetLibraryInfoWrapperPass
>();
69 void llvm::initializeLazyBPIPassPass(PassRegistry
&Registry
) {
70 INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass
);
71 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass
);
72 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass
);