blob: 83698598e1563f98f206ec36dd4a7d986dafde02 [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"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080020#include "llvm/InitializePasses.h"
Adam Nemetaa3506c2016-07-28 23:31:12 +000021
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)
John Brawnda4a68a2017-06-08 09:44:40 +000029INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Adam Nemetaa3506c2016-07-28 23:31:12 +000030INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
31 "Lazy Branch Probability Analysis", true, true)
32
33char LazyBranchProbabilityInfoPass::ID = 0;
34
35LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass()
36 : FunctionPass(ID) {
37 initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry());
38}
39
40void LazyBranchProbabilityInfoPass::print(raw_ostream &OS,
41 const Module *) const {
42 LBPI->getCalculated().print(OS);
43}
44
45void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
Mikael Holmen2ca16892018-05-17 09:05:40 +000046 // We require DT so it's available when LI is available. The LI updating code
47 // asserts that DT is also present so if we don't make sure that we have DT
48 // here, that assert will trigger.
49 AU.addRequired<DominatorTreeWrapperPass>();
Adam Nemetaa3506c2016-07-28 23:31:12 +000050 AU.addRequired<LoopInfoWrapperPass>();
John Brawnda4a68a2017-06-08 09:44:40 +000051 AU.addRequired<TargetLibraryInfoWrapperPass>();
Adam Nemetaa3506c2016-07-28 23:31:12 +000052 AU.setPreservesAll();
53}
54
55void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); }
56
57bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) {
58 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
Teresa Johnson9c27b592019-09-07 03:09:36 +000059 TargetLibraryInfo &TLI =
60 getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
Jonas Devlieghere0eaee542019-08-15 15:54:37 +000061 LBPI = std::make_unique<LazyBranchProbabilityInfo>(&F, &LI, &TLI);
Adam Nemetaa3506c2016-07-28 23:31:12 +000062 return false;
63}
64
65void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) {
66 AU.addRequired<LazyBranchProbabilityInfoPass>();
67 AU.addRequired<LoopInfoWrapperPass>();
John Brawnda4a68a2017-06-08 09:44:40 +000068 AU.addRequired<TargetLibraryInfoWrapperPass>();
Adam Nemetaa3506c2016-07-28 23:31:12 +000069}
70
71void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) {
72 INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass);
73 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
John Brawnda4a68a2017-06-08 09:44:40 +000074 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass);
Adam Nemetaa3506c2016-07-28 23:31:12 +000075}