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#include "llvm/InitializePasses.h"
21
22using namespace llvm;
23
24#define DEBUG_TYPE "lazy-branch-prob"
25
26INITIALIZE_PASS_BEGIN(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
27 "Lazy Branch Probability Analysis", true, true)
28INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
29INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
30INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
31 "Lazy Branch Probability Analysis", true, true)
32
33char LazyBranchProbabilityInfoPass::ID = 0;
34
35LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass()
36 : FunctionPass(ID) {}
37
38void LazyBranchProbabilityInfoPass::print(raw_ostream &OS,
39 const Module *) const {
40 LBPI->getCalculated().print(OS);
41}
42
43void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
44 // We require DT so it's available when LI is available. The LI updating code
45 // asserts that DT is also present so if we don't make sure that we have DT
46 // here, that assert will trigger.
47 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
48 AU.addRequiredTransitive<LoopInfoWrapperPass>();
49 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
50 AU.setPreservesAll();
51}
52
53void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); }
54
55bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) {
56 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
57 TargetLibraryInfo &TLI =
58 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
59 LBPI = std::make_unique<LazyBranchProbabilityInfo>(args: &F, args: &LI, args: &TLI);
60 return false;
61}
62
63void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) {
64 AU.addRequiredTransitive<LazyBranchProbabilityInfoPass>();
65 AU.addRequiredTransitive<LoopInfoWrapperPass>();
66 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
67}
68
69void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) {
70 INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass);
71 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
72 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass);
73}
74