LazyMachineBlockFrequencyInfo.cpp 3.4 KB

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