Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 1 | //===- LazyBranchProbabilityInfo.cpp - Lazy Branch Probability Analysis ---===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 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" |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Mikael Holmen | 2ca1689 | 2018-05-17 09:05:40 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Dominators.h" |
Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | #define DEBUG_TYPE "lazy-branch-prob" |
| 24 | |
| 25 | INITIALIZE_PASS_BEGIN(LazyBranchProbabilityInfoPass, DEBUG_TYPE, |
| 26 | "Lazy Branch Probability Analysis", true, true) |
| 27 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 28 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass) |
Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 29 | INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE, |
| 30 | "Lazy Branch Probability Analysis", true, true) |
| 31 | |
| 32 | char LazyBranchProbabilityInfoPass::ID = 0; |
| 33 | |
| 34 | LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass() |
| 35 | : FunctionPass(ID) { |
| 36 | initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry()); |
| 37 | } |
| 38 | |
| 39 | void LazyBranchProbabilityInfoPass::print(raw_ostream &OS, |
| 40 | const Module *) const { |
| 41 | LBPI->getCalculated().print(OS); |
| 42 | } |
| 43 | |
| 44 | void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const { |
Mikael Holmen | 2ca1689 | 2018-05-17 09:05:40 +0000 | [diff] [blame] | 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>(); |
Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 49 | AU.addRequired<LoopInfoWrapperPass>(); |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 50 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 51 | AU.setPreservesAll(); |
| 52 | } |
| 53 | |
| 54 | void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); } |
| 55 | |
| 56 | bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) { |
| 57 | LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 58 | TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(); |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 59 | LBPI = std::make_unique<LazyBranchProbabilityInfo>(&F, &LI, &TLI); |
Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | |
| 63 | void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) { |
| 64 | AU.addRequired<LazyBranchProbabilityInfoPass>(); |
| 65 | AU.addRequired<LoopInfoWrapperPass>(); |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 66 | AU.addRequired<TargetLibraryInfoWrapperPass>(); |
Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) { |
| 70 | INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass); |
| 71 | INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); |
John Brawn | da4a68a | 2017-06-08 09:44:40 +0000 | [diff] [blame] | 72 | INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass); |
Adam Nemet | aa3506c | 2016-07-28 23:31:12 +0000 | [diff] [blame] | 73 | } |