1 ///===- LazyMachineBlockFrequencyInfo.cpp - Lazy Machine Block Frequency --===//
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
7 ///===---------------------------------------------------------------------===//
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.
14 ///===---------------------------------------------------------------------===//
16 #include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
20 #define DEBUG_TYPE "lazy-machine-block-freq"
22 INITIALIZE_PASS_BEGIN(LazyMachineBlockFrequencyInfoPass
, DEBUG_TYPE
,
23 "Lazy Machine Block Frequency Analysis", true, true)
24 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo
)
25 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo
)
26 INITIALIZE_PASS_END(LazyMachineBlockFrequencyInfoPass
, DEBUG_TYPE
,
27 "Lazy Machine Block Frequency Analysis", true, true)
29 char LazyMachineBlockFrequencyInfoPass::ID
= 0;
31 LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass()
32 : MachineFunctionPass(ID
) {
33 initializeLazyMachineBlockFrequencyInfoPassPass(
34 *PassRegistry::getPassRegistry());
37 void LazyMachineBlockFrequencyInfoPass::print(raw_ostream
&OS
,
38 const Module
*M
) const {
39 getBFI().print(OS
, M
);
42 void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage(
43 AnalysisUsage
&AU
) const {
44 AU
.addRequired
<MachineBranchProbabilityInfo
>();
46 MachineFunctionPass::getAnalysisUsage(AU
);
49 void LazyMachineBlockFrequencyInfoPass::releaseMemory() {
55 MachineBlockFrequencyInfo
&
56 LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const {
57 auto *MBFI
= getAnalysisIfAvailable
<MachineBlockFrequencyInfo
>();
59 LLVM_DEBUG(dbgs() << "MachineBlockFrequencyInfo is available\n");
63 auto &MBPI
= getAnalysis
<MachineBranchProbabilityInfo
>();
64 auto *MLI
= getAnalysisIfAvailable
<MachineLoopInfo
>();
65 auto *MDT
= getAnalysisIfAvailable
<MachineDominatorTree
>();
66 LLVM_DEBUG(dbgs() << "Building MachineBlockFrequencyInfo on the fly\n");
67 LLVM_DEBUG(if (MLI
) dbgs() << "LoopInfo is available\n");
70 LLVM_DEBUG(dbgs() << "Building LoopInfo on the fly\n");
71 // First create a dominator tree.
72 LLVM_DEBUG(if (MDT
) dbgs() << "DominatorTree is available\n");
75 LLVM_DEBUG(dbgs() << "Building DominatorTree on the fly\n");
76 OwnedMDT
= std::make_unique
<MachineDominatorTree
>();
77 OwnedMDT
->getBase().recalculate(*MF
);
81 // Generate LoopInfo from it.
82 OwnedMLI
= std::make_unique
<MachineLoopInfo
>();
83 OwnedMLI
->getBase().analyze(MDT
->getBase());
87 OwnedMBFI
= std::make_unique
<MachineBlockFrequencyInfo
>();
88 OwnedMBFI
->calculate(*MF
, MBPI
, *MLI
);
89 return *OwnedMBFI
.get();
92 bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction(