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