MachineRegionInfo.cpp 4.8 KB

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