17a7e6055SDimitry Andric ///===- LazyMachineBlockFrequencyInfo.cpp - Lazy Machine Block Frequency --===//
27a7e6055SDimitry Andric ///
37a7e6055SDimitry Andric /// The LLVM Compiler Infrastructure
47a7e6055SDimitry Andric ///
57a7e6055SDimitry Andric /// This file is distributed under the University of Illinois Open Source
67a7e6055SDimitry Andric /// License. See LICENSE.TXT for details.
77a7e6055SDimitry Andric ///
87a7e6055SDimitry Andric ///===---------------------------------------------------------------------===//
97a7e6055SDimitry Andric /// \file
107a7e6055SDimitry Andric /// This is an alternative analysis pass to MachineBlockFrequencyInfo. The
117a7e6055SDimitry Andric /// difference is that with this pass the block frequencies are not computed
127a7e6055SDimitry Andric /// when the analysis pass is executed but rather when the BFI result is
137a7e6055SDimitry Andric /// explicitly requested by the analysis client.
147a7e6055SDimitry Andric ///
157a7e6055SDimitry Andric ///===---------------------------------------------------------------------===//
167a7e6055SDimitry Andric
177a7e6055SDimitry Andric #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
187a7e6055SDimitry Andric
197a7e6055SDimitry Andric using namespace llvm;
207a7e6055SDimitry Andric
217a7e6055SDimitry Andric #define DEBUG_TYPE "lazy-machine-block-freq"
227a7e6055SDimitry Andric
237a7e6055SDimitry Andric INITIALIZE_PASS_BEGIN(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
247a7e6055SDimitry Andric "Lazy Machine Block Frequency Analysis", true, true)
257a7e6055SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
267a7e6055SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
277a7e6055SDimitry Andric INITIALIZE_PASS_END(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
287a7e6055SDimitry Andric "Lazy Machine Block Frequency Analysis", true, true)
297a7e6055SDimitry Andric
307a7e6055SDimitry Andric char LazyMachineBlockFrequencyInfoPass::ID = 0;
317a7e6055SDimitry Andric
LazyMachineBlockFrequencyInfoPass()327a7e6055SDimitry Andric LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass()
337a7e6055SDimitry Andric : MachineFunctionPass(ID) {
347a7e6055SDimitry Andric initializeLazyMachineBlockFrequencyInfoPassPass(
357a7e6055SDimitry Andric *PassRegistry::getPassRegistry());
367a7e6055SDimitry Andric }
377a7e6055SDimitry Andric
print(raw_ostream & OS,const Module * M) const387a7e6055SDimitry Andric void LazyMachineBlockFrequencyInfoPass::print(raw_ostream &OS,
397a7e6055SDimitry Andric const Module *M) const {
407a7e6055SDimitry Andric getBFI().print(OS, M);
417a7e6055SDimitry Andric }
427a7e6055SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const437a7e6055SDimitry Andric void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage(
447a7e6055SDimitry Andric AnalysisUsage &AU) const {
457a7e6055SDimitry Andric AU.addRequired<MachineBranchProbabilityInfo>();
467a7e6055SDimitry Andric AU.setPreservesAll();
477a7e6055SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU);
487a7e6055SDimitry Andric }
497a7e6055SDimitry Andric
releaseMemory()507a7e6055SDimitry Andric void LazyMachineBlockFrequencyInfoPass::releaseMemory() {
517a7e6055SDimitry Andric OwnedMBFI.reset();
527a7e6055SDimitry Andric OwnedMLI.reset();
537a7e6055SDimitry Andric OwnedMDT.reset();
547a7e6055SDimitry Andric }
557a7e6055SDimitry Andric
567a7e6055SDimitry Andric MachineBlockFrequencyInfo &
calculateIfNotAvailable() const577a7e6055SDimitry Andric LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const {
587a7e6055SDimitry Andric auto *MBFI = getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
597a7e6055SDimitry Andric if (MBFI) {
60*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "MachineBlockFrequencyInfo is available\n");
617a7e6055SDimitry Andric return *MBFI;
627a7e6055SDimitry Andric }
637a7e6055SDimitry Andric
647a7e6055SDimitry Andric auto &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
657a7e6055SDimitry Andric auto *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
667a7e6055SDimitry Andric auto *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
67*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Building MachineBlockFrequencyInfo on the fly\n");
68*4ba319b5SDimitry Andric LLVM_DEBUG(if (MLI) dbgs() << "LoopInfo is available\n");
697a7e6055SDimitry Andric
707a7e6055SDimitry Andric if (!MLI) {
71*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Building LoopInfo on the fly\n");
727a7e6055SDimitry Andric // First create a dominator tree.
73*4ba319b5SDimitry Andric LLVM_DEBUG(if (MDT) dbgs() << "DominatorTree is available\n");
747a7e6055SDimitry Andric
757a7e6055SDimitry Andric if (!MDT) {
76*4ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Building DominatorTree on the fly\n");
777a7e6055SDimitry Andric OwnedMDT = make_unique<MachineDominatorTree>();
787a7e6055SDimitry Andric OwnedMDT->getBase().recalculate(*MF);
797a7e6055SDimitry Andric MDT = OwnedMDT.get();
807a7e6055SDimitry Andric }
817a7e6055SDimitry Andric
827a7e6055SDimitry Andric // Generate LoopInfo from it.
837a7e6055SDimitry Andric OwnedMLI = make_unique<MachineLoopInfo>();
847a7e6055SDimitry Andric OwnedMLI->getBase().analyze(MDT->getBase());
857a7e6055SDimitry Andric MLI = OwnedMLI.get();
867a7e6055SDimitry Andric }
877a7e6055SDimitry Andric
887a7e6055SDimitry Andric OwnedMBFI = make_unique<MachineBlockFrequencyInfo>();
897a7e6055SDimitry Andric OwnedMBFI->calculate(*MF, MBPI, *MLI);
907a7e6055SDimitry Andric return *OwnedMBFI.get();
917a7e6055SDimitry Andric }
927a7e6055SDimitry Andric
runOnMachineFunction(MachineFunction & F)937a7e6055SDimitry Andric bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(
947a7e6055SDimitry Andric MachineFunction &F) {
957a7e6055SDimitry Andric MF = &F;
967a7e6055SDimitry Andric return false;
977a7e6055SDimitry Andric }
98