blob: 7fd61b4e0b2b06e389e313f9a1ffc24c40bec43d [file] [log] [blame]
Adam Nemetaa3506c2016-07-28 23:31:12 +00001//===- LazyBranchProbabilityInfo.cpp - Lazy Branch Probability Analysis ---===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Adam Nemetaa3506c2016-07-28 23:31:12 +00006//
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"
John Brawnda4a68a2017-06-08 09:44:40 +000018#include "llvm/Analysis/TargetLibraryInfo.h"
Mikael Holmen2ca16892018-05-17 09:05:40 +000019#include "llvm/IR/Dominators.h"
Adam Nemetaa3506c2016-07-28 23:31:12 +000020
21using namespace llvm;
22
23#define DEBUG_TYPE "lazy-branch-prob"
24
25INITIALIZE_PASS_BEGIN(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
26 "Lazy Branch Probability Analysis", true, true)
27INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
John Brawnda4a68a2017-06-08 09:44:40 +000028INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Adam Nemetaa3506c2016-07-28 23:31:12 +000029INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
30 "Lazy Branch Probability Analysis", true, true)
31
32char LazyBranchProbabilityInfoPass::ID = 0;
33
34LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass()
35 : FunctionPass(ID) {
36 initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry());
37}
38
39void LazyBranchProbabilityInfoPass::print(raw_ostream &OS,
40 const Module *) const {
41 LBPI->getCalculated().print(OS);
42}
43
44void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
Mikael Holmen2ca16892018-05-17 09:05:40 +000045 // 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>();
Adam Nemetaa3506c2016-07-28 23:31:12 +000049 AU.addRequired<LoopInfoWrapperPass>();
John Brawnda4a68a2017-06-08 09:44:40 +000050 AU.addRequired<TargetLibraryInfoWrapperPass>();
Adam Nemetaa3506c2016-07-28 23:31:12 +000051 AU.setPreservesAll();
52}
53
54void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); }
55
56bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) {
57 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
John Brawnda4a68a2017-06-08 09:44:40 +000058 TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000059 LBPI = std::make_unique<LazyBranchProbabilityInfo>(&F, &LI, &TLI);
Adam Nemetaa3506c2016-07-28 23:31:12 +000060 return false;
61}
62
63void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) {
64 AU.addRequired<LazyBranchProbabilityInfoPass>();
65 AU.addRequired<LoopInfoWrapperPass>();
John Brawnda4a68a2017-06-08 09:44:40 +000066 AU.addRequired<TargetLibraryInfoWrapperPass>();
Adam Nemetaa3506c2016-07-28 23:31:12 +000067}
68
69void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) {
70 INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass);
71 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
John Brawnda4a68a2017-06-08 09:44:40 +000072 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass);
Adam Nemetaa3506c2016-07-28 23:31:12 +000073}