Adam Nemet | b17a45c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 1 | //===- LazyBranchProbabilityInfo.cpp - Lazy Branch Probability Analysis ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is an alternative analysis pass to BranchProbabilityInfoWrapperPass. |
| 11 | // The difference is that with this pass the branch probabilities are not |
| 12 | // computed when the analysis pass is executed but rather when the BPI results |
| 13 | // is explicitly requested by the analysis client. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #include "llvm/Analysis/LazyBranchProbabilityInfo.h" |
| 18 | #include "llvm/Analysis/LoopInfo.h" |
John Brawn | 436ad28 | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Mikael Holmen | fadab83 | 2018-05-17 09:05:40 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Dominators.h" |
Adam Nemet | b17a45c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | #define DEBUG_TYPE "lazy-branch-prob" |
| 25 | |
| 26 | INITIALIZE_PASS_BEGIN(LazyBranchProbabilityInfoPass, DEBUG_TYPE, |
| 27 | "Lazy Branch Probability Analysis", true, true) |
| 28 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
John Brawn | 436ad28 | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 29 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Adam Nemet | b17a45c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 30 | INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE, |
| 31 | "Lazy Branch Probability Analysis", true, true) |
| 32 | |
| 33 | char LazyBranchProbabilityInfoPass::ID = 0; |
| 34 | |
| 35 | LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass() |
| 36 | : FunctionPass(ID) { |
| 37 | initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry()); |
| 38 | } |
| 39 | |
| 40 | void LazyBranchProbabilityInfoPass::print(raw_ostream &OS, |
| 41 | const Module *) const { |
| 42 | LBPI->getCalculated().print(OS); |
| 43 | } |
| 44 | |
| 45 | void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Mikael Holmen | fadab83 | 2018-05-17 09:05:40 +0000 | [diff] [blame] | 46 | // 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 Nemet | b17a45c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 50 | AU.addRequired<LoopInfoWrapperPass>(); |
John Brawn | 436ad28 | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 51 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Adam Nemet | b17a45c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 52 | AU.setPreservesAll(); |
| 53 | } |
| 54 | |
| 55 | void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); } |
| 56 | |
| 57 | bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) { |
| 58 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
John Brawn | 436ad28 | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 59 | TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
| 60 | LBPI = llvm::make_unique<LazyBranchProbabilityInfo>(&F, &LI, &TLI); |
Adam Nemet | b17a45c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | |
| 64 | void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) { |
| 65 | AU.addRequired<LazyBranchProbabilityInfoPass>(); |
| 66 | AU.addRequired<LoopInfoWrapperPass>(); |
John Brawn | 436ad28 | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 67 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Adam Nemet | b17a45c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) { |
| 71 | INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass); |
| 72 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
John Brawn | 436ad28 | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 73 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass); |
Adam Nemet | b17a45c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 74 | } |