10b57cec5SDimitry Andric //===- LazyBlockFrequencyInfo.cpp - Lazy Block Frequency 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 BlockFrequencyInfoWrapperPass. The
100b57cec5SDimitry Andric // difference is that with this pass the block frequencies are not computed when
110b57cec5SDimitry Andric // the analysis pass is executed but rather when the BFI result is explicitly
120b57cec5SDimitry Andric // requested by the analysis client.
130b57cec5SDimitry Andric //
140b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
170b57cec5SDimitry Andric #include "llvm/Analysis/LazyBranchProbabilityInfo.h"
180b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.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-block-freq"
250b57cec5SDimitry Andric
260b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(LazyBlockFrequencyInfoPass, DEBUG_TYPE,
270b57cec5SDimitry Andric "Lazy Block Frequency Analysis", true, true)
280b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LazyBPIPass)
290b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
300b57cec5SDimitry Andric INITIALIZE_PASS_END(LazyBlockFrequencyInfoPass, DEBUG_TYPE,
310b57cec5SDimitry Andric "Lazy Block Frequency Analysis", true, true)
320b57cec5SDimitry Andric
330b57cec5SDimitry Andric char LazyBlockFrequencyInfoPass::ID = 0;
340b57cec5SDimitry Andric
LazyBlockFrequencyInfoPass()350b57cec5SDimitry Andric LazyBlockFrequencyInfoPass::LazyBlockFrequencyInfoPass() : FunctionPass(ID) {
360b57cec5SDimitry Andric initializeLazyBlockFrequencyInfoPassPass(*PassRegistry::getPassRegistry());
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric
print(raw_ostream & OS,const Module *) const390b57cec5SDimitry Andric void LazyBlockFrequencyInfoPass::print(raw_ostream &OS, const Module *) const {
400b57cec5SDimitry Andric LBFI.getCalculated().print(OS);
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const430b57cec5SDimitry Andric void LazyBlockFrequencyInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
440b57cec5SDimitry Andric LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU);
450b57cec5SDimitry Andric // We require DT so it's available when LI is available. The LI updating code
460b57cec5SDimitry Andric // asserts that DT is also present so if we don't make sure that we have DT
470b57cec5SDimitry Andric // here, that assert will trigger.
48*fe6060f1SDimitry Andric AU.addRequiredTransitive<DominatorTreeWrapperPass>();
49*fe6060f1SDimitry Andric AU.addRequiredTransitive<LoopInfoWrapperPass>();
500b57cec5SDimitry Andric AU.setPreservesAll();
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
releaseMemory()530b57cec5SDimitry Andric void LazyBlockFrequencyInfoPass::releaseMemory() { LBFI.releaseMemory(); }
540b57cec5SDimitry Andric
runOnFunction(Function & F)550b57cec5SDimitry Andric bool LazyBlockFrequencyInfoPass::runOnFunction(Function &F) {
560b57cec5SDimitry Andric auto &BPIPass = getAnalysis<LazyBranchProbabilityInfoPass>();
570b57cec5SDimitry Andric LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
580b57cec5SDimitry Andric LBFI.setAnalysis(&F, &BPIPass, &LI);
590b57cec5SDimitry Andric return false;
600b57cec5SDimitry Andric }
610b57cec5SDimitry Andric
getLazyBFIAnalysisUsage(AnalysisUsage & AU)620b57cec5SDimitry Andric void LazyBlockFrequencyInfoPass::getLazyBFIAnalysisUsage(AnalysisUsage &AU) {
630b57cec5SDimitry Andric LazyBranchProbabilityInfoPass::getLazyBPIAnalysisUsage(AU);
64*fe6060f1SDimitry Andric AU.addRequiredTransitive<LazyBlockFrequencyInfoPass>();
65*fe6060f1SDimitry Andric AU.addRequiredTransitive<LoopInfoWrapperPass>();
660b57cec5SDimitry Andric }
670b57cec5SDimitry Andric
initializeLazyBFIPassPass(PassRegistry & Registry)680b57cec5SDimitry Andric void llvm::initializeLazyBFIPassPass(PassRegistry &Registry) {
690b57cec5SDimitry Andric initializeLazyBPIPassPass(Registry);
700b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LazyBlockFrequencyInfoPass);
710b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
720b57cec5SDimitry Andric }
73