regioninfo.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. //===- RegionInfo.cpp - SESE region detection analysis --------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // Detects single entry single exit regions in the control flow graph.
  10. //===----------------------------------------------------------------------===//
  11. #include "llvm/Analysis/RegionInfo.h"
  12. #include "llvm/ADT/PostOrderIterator.h"
  13. #include "llvm/ADT/Statistic.h"
  14. #include "llvm/Analysis/LoopInfo.h"
  15. #include "llvm/Analysis/RegionInfoImpl.h"
  16. #include "llvm/Analysis/RegionIterator.h"
  17. #include "llvm/Support/CommandLine.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include <algorithm>
  21. #include <iterator>
  22. #include <set>
  23. using namespace llvm;
  24. #define DEBUG_TYPE "region"
  25. namespace llvm {
  26. template class RegionBase<RegionTraits<Function>>;
  27. template class RegionNodeBase<RegionTraits<Function>>;
  28. template class RegionInfoBase<RegionTraits<Function>>;
  29. }
  30. STATISTIC(numRegions, "The # of regions");
  31. STATISTIC(numSimpleRegions, "The # of simple regions");
  32. // Always verify if expensive checking is enabled.
  33. static cl::opt<bool,true>
  34. VerifyRegionInfoX(
  35. "verify-region-info",
  36. cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
  37. cl::desc("Verify region info (time consuming)"));
  38. static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
  39. cl::location(RegionInfo::printStyle),
  40. cl::Hidden,
  41. cl::desc("style of printing regions"),
  42. cl::values(
  43. clEnumValN(Region::PrintNone, "none", "print no details"),
  44. clEnumValN(Region::PrintBB, "bb",
  45. "print regions in detail with block_iterator"),
  46. clEnumValN(Region::PrintRN, "rn",
  47. "print regions in detail with element_iterator"),
  48. clEnumValEnd));
  49. //===----------------------------------------------------------------------===//
  50. // Region implementation
  51. //
  52. Region::Region(BasicBlock *Entry, BasicBlock *Exit,
  53. RegionInfo* RI,
  54. DominatorTree *DT, Region *Parent) :
  55. RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
  56. }
  57. Region::~Region() { }
  58. //===----------------------------------------------------------------------===//
  59. // RegionInfo implementation
  60. //
  61. RegionInfo::RegionInfo() :
  62. RegionInfoBase<RegionTraits<Function>>() {
  63. }
  64. RegionInfo::~RegionInfo() {
  65. }
  66. void RegionInfo::updateStatistics(Region *R) {
  67. ++numRegions;
  68. // TODO: Slow. Should only be enabled if -stats is used.
  69. if (R->isSimple())
  70. ++numSimpleRegions;
  71. }
  72. void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
  73. PostDominatorTree *PDT_, DominanceFrontier *DF_) {
  74. DT = DT_;
  75. PDT = PDT_;
  76. DF = DF_;
  77. TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
  78. this, DT, nullptr);
  79. updateStatistics(TopLevelRegion);
  80. calculate(F);
  81. }
  82. //===----------------------------------------------------------------------===//
  83. // RegionInfoPass implementation
  84. //
  85. RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
  86. initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
  87. }
  88. RegionInfoPass::~RegionInfoPass() {
  89. }
  90. bool RegionInfoPass::runOnFunction(Function &F) {
  91. releaseMemory();
  92. auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  93. auto PDT = &getAnalysis<PostDominatorTree>();
  94. auto DF = &getAnalysis<DominanceFrontier>();
  95. RI.recalculate(F, DT, PDT, DF);
  96. return false;
  97. }
  98. void RegionInfoPass::releaseMemory() {
  99. RI.releaseMemory();
  100. }
  101. void RegionInfoPass::verifyAnalysis() const {
  102. RI.verifyAnalysis();
  103. }
  104. void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
  105. AU.setPreservesAll();
  106. AU.addRequiredTransitive<DominatorTreeWrapperPass>();
  107. AU.addRequired<PostDominatorTree>();
  108. AU.addRequired<DominanceFrontier>();
  109. }
  110. void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
  111. RI.print(OS);
  112. }
  113. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  114. void RegionInfoPass::dump() const {
  115. RI.dump();
  116. }
  117. #endif
  118. char RegionInfoPass::ID = 0;
  119. INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
  120. "Detect single entry single exit regions", true, true)
  121. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  122. INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
  123. INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
  124. INITIALIZE_PASS_END(RegionInfoPass, "regions",
  125. "Detect single entry single exit regions", true, true)
  126. // Create methods available outside of this file, to use them
  127. // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
  128. // the link time optimization.
  129. namespace llvm {
  130. FunctionPass *createRegionInfoPass() {
  131. return new RegionInfoPass();
  132. }
  133. }