Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame] | 1 | ///===- LazyMachineBlockFrequencyInfo.cpp - Lazy Machine Block Frequency --===// |
| 2 | /// |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame] | 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 | |
| 18 | using namespace llvm; |
| 19 | |
| 20 | #define DEBUG_TYPE "lazy-machine-block-freq" |
| 21 | |
| 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) |
| 28 | |
| 29 | char LazyMachineBlockFrequencyInfoPass::ID = 0; |
| 30 | |
| 31 | LazyMachineBlockFrequencyInfoPass::LazyMachineBlockFrequencyInfoPass() |
| 32 | : MachineFunctionPass(ID) { |
| 33 | initializeLazyMachineBlockFrequencyInfoPassPass( |
| 34 | *PassRegistry::getPassRegistry()); |
| 35 | } |
| 36 | |
| 37 | void LazyMachineBlockFrequencyInfoPass::print(raw_ostream &OS, |
| 38 | const Module *M) const { |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 39 | getBFI().print(OS, M); |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void LazyMachineBlockFrequencyInfoPass::getAnalysisUsage( |
| 43 | AnalysisUsage &AU) const { |
| 44 | AU.addRequired<MachineBranchProbabilityInfo>(); |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame] | 45 | AU.setPreservesAll(); |
| 46 | MachineFunctionPass::getAnalysisUsage(AU); |
| 47 | } |
| 48 | |
| 49 | void LazyMachineBlockFrequencyInfoPass::releaseMemory() { |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 50 | OwnedMBFI.reset(); |
| 51 | OwnedMLI.reset(); |
| 52 | OwnedMDT.reset(); |
| 53 | } |
| 54 | |
| 55 | MachineBlockFrequencyInfo & |
| 56 | LazyMachineBlockFrequencyInfoPass::calculateIfNotAvailable() const { |
| 57 | auto *MBFI = getAnalysisIfAvailable<MachineBlockFrequencyInfo>(); |
| 58 | if (MBFI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 59 | LLVM_DEBUG(dbgs() << "MachineBlockFrequencyInfo is available\n"); |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 60 | return *MBFI; |
| 61 | } |
| 62 | |
| 63 | auto &MBPI = getAnalysis<MachineBranchProbabilityInfo>(); |
| 64 | auto *MLI = getAnalysisIfAvailable<MachineLoopInfo>(); |
| 65 | auto *MDT = getAnalysisIfAvailable<MachineDominatorTree>(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 66 | LLVM_DEBUG(dbgs() << "Building MachineBlockFrequencyInfo on the fly\n"); |
| 67 | LLVM_DEBUG(if (MLI) dbgs() << "LoopInfo is available\n"); |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 68 | |
| 69 | if (!MLI) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 70 | LLVM_DEBUG(dbgs() << "Building LoopInfo on the fly\n"); |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 71 | // First create a dominator tree. |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 72 | LLVM_DEBUG(if (MDT) dbgs() << "DominatorTree is available\n"); |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 73 | |
| 74 | if (!MDT) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 75 | LLVM_DEBUG(dbgs() << "Building DominatorTree on the fly\n"); |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 76 | OwnedMDT = std::make_unique<MachineDominatorTree>(); |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 77 | OwnedMDT->getBase().recalculate(*MF); |
| 78 | MDT = OwnedMDT.get(); |
| 79 | } |
| 80 | |
| 81 | // Generate LoopInfo from it. |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 82 | OwnedMLI = std::make_unique<MachineLoopInfo>(); |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 83 | OwnedMLI->getBase().analyze(MDT->getBase()); |
| 84 | MLI = OwnedMLI.get(); |
| 85 | } |
| 86 | |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 87 | OwnedMBFI = std::make_unique<MachineBlockFrequencyInfo>(); |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 88 | OwnedMBFI->calculate(*MF, MBPI, *MLI); |
| 89 | return *OwnedMBFI.get(); |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | bool LazyMachineBlockFrequencyInfoPass::runOnMachineFunction( |
Adam Nemet | b516cf3 | 2017-02-23 17:30:01 +0000 | [diff] [blame] | 93 | MachineFunction &F) { |
| 94 | MF = &F; |
Adam Nemet | bbb141c | 2017-02-14 17:21:09 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |