LazyMachineBlockFrequencyInfo.cpp 3.4 KB

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