10b57cec5SDimitry Andric //===- LazyBranchProbabilityInfo.cpp - Lazy Branch Probability Analysis ---===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This is an alternative analysis pass to BranchProbabilityInfoWrapperPass.
100b57cec5SDimitry Andric // The difference is that with this pass the branch probabilities are not
110b57cec5SDimitry Andric // computed when the analysis pass is executed but rather when the BPI results
120b57cec5SDimitry Andric // is explicitly requested by the analysis client.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/TargetLibraryInfo.h"
190b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
20480093f4SDimitry Andric #include "llvm/InitializePasses.h"
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric using namespace llvm;
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric #define DEBUG_TYPE "lazy-branch-prob"
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
270b57cec5SDimitry Andric "Lazy Branch Probability Analysis", true, true)
280b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
290b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
300b57cec5SDimitry Andric INITIALIZE_PASS_END(LazyBranchProbabilityInfoPass, DEBUG_TYPE,
310b57cec5SDimitry Andric "Lazy Branch Probability Analysis", true, true)
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric char LazyBranchProbabilityInfoPass::ID = 0;
340b57cec5SDimitry Andric
LazyBranchProbabilityInfoPass()350b57cec5SDimitry Andric LazyBranchProbabilityInfoPass::LazyBranchProbabilityInfoPass()
360b57cec5SDimitry Andric : FunctionPass(ID) {
370b57cec5SDimitry Andric initializeLazyBranchProbabilityInfoPassPass(*PassRegistry::getPassRegistry());
380b57cec5SDimitry Andric }
390b57cec5SDimitry Andric
print(raw_ostream & OS,const Module *) const400b57cec5SDimitry Andric void LazyBranchProbabilityInfoPass::print(raw_ostream &OS,
410b57cec5SDimitry Andric const Module *) const {
420b57cec5SDimitry Andric LBPI->getCalculated().print(OS);
430b57cec5SDimitry Andric }
440b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const450b57cec5SDimitry Andric void LazyBranchProbabilityInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
460b57cec5SDimitry Andric // We require DT so it's available when LI is available. The LI updating code
470b57cec5SDimitry Andric // asserts that DT is also present so if we don't make sure that we have DT
480b57cec5SDimitry Andric // here, that assert will trigger.
49*fe6060f1SDimitry Andric AU.addRequiredTransitive<DominatorTreeWrapperPass>();
50*fe6060f1SDimitry Andric AU.addRequiredTransitive<LoopInfoWrapperPass>();
51*fe6060f1SDimitry Andric AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
520b57cec5SDimitry Andric AU.setPreservesAll();
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric
releaseMemory()550b57cec5SDimitry Andric void LazyBranchProbabilityInfoPass::releaseMemory() { LBPI.reset(); }
560b57cec5SDimitry Andric
runOnFunction(Function & F)570b57cec5SDimitry Andric bool LazyBranchProbabilityInfoPass::runOnFunction(Function &F) {
580b57cec5SDimitry Andric LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
598bcb0991SDimitry Andric TargetLibraryInfo &TLI =
608bcb0991SDimitry Andric getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
618bcb0991SDimitry Andric LBPI = std::make_unique<LazyBranchProbabilityInfo>(&F, &LI, &TLI);
620b57cec5SDimitry Andric return false;
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric
getLazyBPIAnalysisUsage(AnalysisUsage & AU)650b57cec5SDimitry Andric void LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AnalysisUsage &AU) {
66*fe6060f1SDimitry Andric AU.addRequiredTransitive<LazyBranchProbabilityInfoPass>();
67*fe6060f1SDimitry Andric AU.addRequiredTransitive<LoopInfoWrapperPass>();
68*fe6060f1SDimitry Andric AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric
initializeLazyBPIPassPass(PassRegistry & Registry)710b57cec5SDimitry Andric void llvm::initializeLazyBPIPassPass(PassRegistry &Registry) {
720b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LazyBranchProbabilityInfoPass);
730b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
740b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass);
750b57cec5SDimitry Andric }
76