LazyMachineBlockFrequencyInfo.cpp 3.3 KB

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