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