1 ///===- LazyMachineBlockFrequencyInfo.cpp - Lazy Machine Block Frequency --===//
2 ///
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
6 ///
7 ///===---------------------------------------------------------------------===//
8 /// \file
9 /// This is an alternative analysis pass to MachineBlockFrequencyInfo.  The
10 /// difference is that with this pass the block frequencies are not computed
11 /// when the analysis pass is executed but rather when the BFI result is
12 /// explicitly requested by the analysis client.
13 ///
14 ///===---------------------------------------------------------------------===//
15 
16 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
17 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
18 #include "llvm/InitializePasses.h"
19 
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "lazy-machine-block-freq"
23 
24 INITIALIZE_PASS_BEGIN(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
25                       "Lazy Machine Block Frequency Analysis", true, true)
26 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
27 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
28 INITIALIZE_PASS_END(LazyMachineBlockFrequencyInfoPass, DEBUG_TYPE,
29                     "Lazy Machine Block Frequency Analysis", true, true)
30 
31 char LazyMachineBlockFrequencyInfoPass::ID = 0;
32 
33 LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass()
34     : MachineFunctionPass(ID) {
35   initializeLazyMachineBlockFrequencyInfoPassPass(
36       *PassRegistry::getPassRegistry());
37 }
38 
39 void LazyMachineBlockFrequencyInfoPass::print(raw_ostream &OS,
40                                               const Module *M) const {
41   getBFI().print(OS, M);
42 }
43 
44 void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage(
45     AnalysisUsage &AU) const {
46   AU.addRequired<MachineBranchProbabilityInfo>();
47   AU.setPreservesAll();
48   MachineFunctionPass::getAnalysisUsage(AU);
49 }
50 
51 void LazyMachineBlockFrequencyInfoPass::releaseMemory() {
52   OwnedMBFI.reset();
53   OwnedMLI.reset();
54   OwnedMDT.reset();
55 }
56 
57 MachineBlockFrequencyInfo &
58 LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const {
59   auto *MBFI = getAnalysisIfAvailable<MachineBlockFrequencyInfo>();
60   if (MBFI) {
61     LLVM_DEBUG(dbgs() << "MachineBlockFrequencyInfo is available\n");
62     return *MBFI;
63   }
64 
65   auto &MBPI = getAnalysis<MachineBranchProbabilityInfo>();
66   auto *MLI = getAnalysisIfAvailable<MachineLoopInfo>();
67   auto *MDT = getAnalysisIfAvailable<MachineDominatorTree>();
68   LLVM_DEBUG(dbgs() << "Building MachineBlockFrequencyInfo on the fly\n");
69   LLVM_DEBUG(if (MLI) dbgs() << "LoopInfo is available\n");
70 
71   if (!MLI) {
72     LLVM_DEBUG(dbgs() << "Building LoopInfo on the fly\n");
73     // First create a dominator tree.
74     LLVM_DEBUG(if (MDT) dbgs() << "DominatorTree is available\n");
75 
76     if (!MDT) {
77       LLVM_DEBUG(dbgs() << "Building DominatorTree on the fly\n");
78       OwnedMDT = std::make_unique<MachineDominatorTree>();
79       OwnedMDT->getBase().recalculate(*MF);
80       MDT = OwnedMDT.get();
81     }
82 
83     // Generate LoopInfo from it.
84     OwnedMLI = std::make_unique<MachineLoopInfo>();
85     OwnedMLI->getBase().analyze(MDT->getBase());
86     MLI = OwnedMLI.get();
87   }
88 
89   OwnedMBFI = std::make_unique<MachineBlockFrequencyInfo>();
90   OwnedMBFI->calculate(*MF, MBPI, *MLI);
91   return *OwnedMBFI;
92 }
93 
94 bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(
95     MachineFunction &F) {
96   MF = &F;
97   return false;
98 }
99