xref: /freebsd-src/contrib/llvm-project/llvm/lib/CodeGen/LazyMachineBlockFrequencyInfo.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric ///===- LazyMachineBlockFrequencyInfo.cpp - Lazy Machine Block Frequency --===//
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 /// \file
90b57cec5SDimitry Andric /// This is an alternative analysis pass to MachineBlockFrequencyInfo.  The
100b57cec5SDimitry Andric /// difference is that with this pass the block frequencies are not computed
110b57cec5SDimitry Andric /// when the analysis pass is executed but rather when the BFI result is
120b57cec5SDimitry Andric /// explicitly requested by the analysis client.
130b57cec5SDimitry Andric ///
140b57cec5SDimitry Andric ///===---------------------------------------------------------------------===//
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
1781ad6265SDimitry Andric #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
18480093f4SDimitry Andric #include "llvm/InitializePasses.h"
190b57cec5SDimitry Andric 
200b57cec5SDimitry Andric using namespace llvm;
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric #define DEBUG_TYPE "lazy-machine-block-freq"
230b57cec5SDimitry Andric 
240b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
250b57cec5SDimitry Andric                       "Lazy Machine Block Frequency Analysis", true, true)
26*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfoWrapperPass)
27*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineLoopInfoWrapperPass)
280b57cec5SDimitry Andric INITIALIZE_PASS_END(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
290b57cec5SDimitry Andric                     "Lazy Machine Block Frequency Analysis", true, true)
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric char LazyMachineBlockFrequencyInfoPass::ID = 0;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass()
340b57cec5SDimitry Andric     : MachineFunctionPass(ID) {
350b57cec5SDimitry Andric   initializeLazyMachineBlockFrequencyInfoPassPass(
360b57cec5SDimitry Andric       *PassRegistry::getPassRegistry());
370b57cec5SDimitry Andric }
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage(
400b57cec5SDimitry Andric     AnalysisUsage &AU) const {
41*0fca6ea1SDimitry Andric   AU.addRequired<MachineBranchProbabilityInfoWrapperPass>();
420b57cec5SDimitry Andric   AU.setPreservesAll();
430b57cec5SDimitry Andric   MachineFunctionPass::getAnalysisUsage(AU);
440b57cec5SDimitry Andric }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric void LazyMachineBlockFrequencyInfoPass::releaseMemory() {
470b57cec5SDimitry Andric   OwnedMBFI.reset();
480b57cec5SDimitry Andric   OwnedMLI.reset();
490b57cec5SDimitry Andric   OwnedMDT.reset();
500b57cec5SDimitry Andric }
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric MachineBlockFrequencyInfo &
530b57cec5SDimitry Andric LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const {
54*0fca6ea1SDimitry Andric   auto *MBFIWrapper =
55*0fca6ea1SDimitry Andric       getAnalysisIfAvailable<MachineBlockFrequencyInfoWrapperPass>();
56*0fca6ea1SDimitry Andric   if (MBFIWrapper) {
570b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "MachineBlockFrequencyInfo is available\n");
58*0fca6ea1SDimitry Andric     return MBFIWrapper->getMBFI();
590b57cec5SDimitry Andric   }
600b57cec5SDimitry Andric 
61*0fca6ea1SDimitry Andric   auto &MBPI = getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI();
62*0fca6ea1SDimitry Andric   auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
63*0fca6ea1SDimitry Andric   auto *MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
64*0fca6ea1SDimitry Andric   auto *MDTWrapper = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
65*0fca6ea1SDimitry Andric   auto *MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
660b57cec5SDimitry Andric   LLVM_DEBUG(dbgs() << "Building MachineBlockFrequencyInfo on the fly\n");
670b57cec5SDimitry Andric   LLVM_DEBUG(if (MLI) dbgs() << "LoopInfo is available\n");
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   if (!MLI) {
700b57cec5SDimitry Andric     LLVM_DEBUG(dbgs() << "Building LoopInfo on the fly\n");
710b57cec5SDimitry Andric     // First create a dominator tree.
720b57cec5SDimitry Andric     LLVM_DEBUG(if (MDT) dbgs() << "DominatorTree is available\n");
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric     if (!MDT) {
750b57cec5SDimitry Andric       LLVM_DEBUG(dbgs() << "Building DominatorTree on the fly\n");
768bcb0991SDimitry Andric       OwnedMDT = std::make_unique<MachineDominatorTree>();
770b57cec5SDimitry Andric       OwnedMDT->getBase().recalculate(*MF);
780b57cec5SDimitry Andric       MDT = OwnedMDT.get();
790b57cec5SDimitry Andric     }
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric     // Generate LoopInfo from it.
828bcb0991SDimitry Andric     OwnedMLI = std::make_unique<MachineLoopInfo>();
83*0fca6ea1SDimitry Andric     OwnedMLI->analyze(MDT->getBase());
840b57cec5SDimitry Andric     MLI = OwnedMLI.get();
850b57cec5SDimitry Andric   }
860b57cec5SDimitry Andric 
878bcb0991SDimitry Andric   OwnedMBFI = std::make_unique<MachineBlockFrequencyInfo>();
880b57cec5SDimitry Andric   OwnedMBFI->calculate(*MF, MBPI, *MLI);
8981ad6265SDimitry Andric   return *OwnedMBFI;
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(
930b57cec5SDimitry Andric     MachineFunction &F) {
940b57cec5SDimitry Andric   MF = &F;
950b57cec5SDimitry Andric   return false;
960b57cec5SDimitry Andric }
97