MachineRegionInfo.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //===- lib/Codegen/MachineRegionInfo.cpp ----------------------------------===//
  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. #include "llvm/CodeGen/MachineRegionInfo.h"
  9. #include "llvm/ADT/Statistic.h"
  10. #include "llvm/Analysis/RegionInfoImpl.h"
  11. #include "llvm/CodeGen/MachinePostDominators.h"
  12. #include "llvm/Config/llvm-config.h"
  13. #include "llvm/Pass.h"
  14. #include "llvm/Support/Compiler.h"
  15. #include "llvm/Support/Debug.h"
  16. #define DEBUG_TYPE "machine-region-info"
  17. using namespace llvm;
  18. STATISTIC(numMachineRegions, "The # of machine regions");
  19. STATISTIC(numMachineSimpleRegions, "The # of simple machine regions");
  20. namespace llvm {
  21. template class RegionBase<RegionTraits<MachineFunction>>;
  22. template class RegionNodeBase<RegionTraits<MachineFunction>>;
  23. template class RegionInfoBase<RegionTraits<MachineFunction>>;
  24. } // end namespace llvm
  25. //===----------------------------------------------------------------------===//
  26. // MachineRegion implementation
  27. MachineRegion::MachineRegion(MachineBasicBlock *Entry, MachineBasicBlock *Exit,
  28. MachineRegionInfo* RI,
  29. MachineDominatorTree *DT, MachineRegion *Parent) :
  30. RegionBase<RegionTraits<MachineFunction>>(Entry, Exit, RI, DT, Parent) {}
  31. MachineRegion::~MachineRegion() = default;
  32. //===----------------------------------------------------------------------===//
  33. // MachineRegionInfo implementation
  34. MachineRegionInfo::MachineRegionInfo() = default;
  35. MachineRegionInfo::~MachineRegionInfo() = default;
  36. void MachineRegionInfo::updateStatistics(MachineRegion *R) {
  37. ++numMachineRegions;
  38. // TODO: Slow. Should only be enabled if -stats is used.
  39. if (R->isSimple())
  40. ++numMachineSimpleRegions;
  41. }
  42. void MachineRegionInfo::recalculate(MachineFunction &F,
  43. MachineDominatorTree *DT_,
  44. MachinePostDominatorTree *PDT_,
  45. MachineDominanceFrontier *DF_) {
  46. DT = DT_;
  47. PDT = PDT_;
  48. DF = DF_;
  49. MachineBasicBlock *Entry = GraphTraits<MachineFunction*>::getEntryNode(&F);
  50. TopLevelRegion = new MachineRegion(Entry, nullptr, this, DT, nullptr);
  51. updateStatistics(TopLevelRegion);
  52. calculate(F);
  53. }
  54. //===----------------------------------------------------------------------===//
  55. // MachineRegionInfoPass implementation
  56. //
  57. MachineRegionInfoPass::MachineRegionInfoPass() : MachineFunctionPass(ID) {
  58. initializeMachineRegionInfoPassPass(*PassRegistry::getPassRegistry());
  59. }
  60. MachineRegionInfoPass::~MachineRegionInfoPass() = default;
  61. bool MachineRegionInfoPass::runOnMachineFunction(MachineFunction &F) {
  62. releaseMemory();
  63. auto DT = &getAnalysis<MachineDominatorTree>();
  64. auto PDT = &getAnalysis<MachinePostDominatorTree>();
  65. auto DF = &getAnalysis<MachineDominanceFrontier>();
  66. RI.recalculate(F, DT, PDT, DF);
  67. LLVM_DEBUG(RI.dump());
  68. return false;
  69. }
  70. void MachineRegionInfoPass::releaseMemory() {
  71. RI.releaseMemory();
  72. }
  73. void MachineRegionInfoPass::verifyAnalysis() const {
  74. // Only do verification when user wants to, otherwise this expensive check
  75. // will be invoked by PMDataManager::verifyPreservedAnalysis when
  76. // a regionpass (marked PreservedAll) finish.
  77. if (MachineRegionInfo::VerifyRegionInfo)
  78. RI.verifyAnalysis();
  79. }
  80. void MachineRegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
  81. AU.setPreservesAll();
  82. AU.addRequired<MachineDominatorTree>();
  83. AU.addRequired<MachinePostDominatorTree>();
  84. AU.addRequired<MachineDominanceFrontier>();
  85. MachineFunctionPass::getAnalysisUsage(AU);
  86. }
  87. void MachineRegionInfoPass::print(raw_ostream &OS, const Module *) const {
  88. RI.print(OS);
  89. }
  90. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  91. LLVM_DUMP_METHOD void MachineRegionInfoPass::dump() const {
  92. RI.dump();
  93. }
  94. #endif
  95. char MachineRegionInfoPass::ID = 0;
  96. char &MachineRegionInfoPassID = MachineRegionInfoPass::ID;
  97. INITIALIZE_PASS_BEGIN(MachineRegionInfoPass, DEBUG_TYPE,
  98. "Detect single entry single exit regions", true, true)
  99. INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
  100. INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree)
  101. INITIALIZE_PASS_DEPENDENCY(MachineDominanceFrontier)
  102. INITIALIZE_PASS_END(MachineRegionInfoPass, DEBUG_TYPE,
  103. "Detect single entry single exit regions", true, true)
  104. // Create methods available outside of this file, to use them
  105. // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
  106. // the link time optimization.
  107. namespace llvm {
  108. FunctionPass *createMachineRegionInfoPass() {
  109. return new MachineRegionInfoPass();
  110. }
  111. } // end namespace llvm