regioninfo.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #if 0 // HLSL Change Starts - option pending
  34. static cl::opt<bool,true>
  35. VerifyRegionInfoX(
  36. "verify-region-info",
  37. cl::location(RegionInfoBase<RegionTraits<Function>>::VerifyRegionInfo),
  38. cl::desc("Verify region info (time consuming)"));
  39. static cl::opt<Region::PrintStyle, true> printStyleX("print-region-style",
  40. cl::location(RegionInfo::printStyle),
  41. cl::Hidden,
  42. cl::desc("style of printing regions"),
  43. cl::values(
  44. clEnumValN(Region::PrintNone, "none", "print no details"),
  45. clEnumValN(Region::PrintBB, "bb",
  46. "print regions in detail with block_iterator"),
  47. clEnumValN(Region::PrintRN, "rn",
  48. "print regions in detail with element_iterator"),
  49. clEnumValEnd));
  50. #else
  51. #endif // HLSL Change Ends
  52. //===----------------------------------------------------------------------===//
  53. // Region implementation
  54. //
  55. Region::Region(BasicBlock *Entry, BasicBlock *Exit,
  56. RegionInfo* RI,
  57. DominatorTree *DT, Region *Parent) :
  58. RegionBase<RegionTraits<Function>>(Entry, Exit, RI, DT, Parent) {
  59. }
  60. Region::~Region() { }
  61. //===----------------------------------------------------------------------===//
  62. // RegionInfo implementation
  63. //
  64. RegionInfo::RegionInfo() :
  65. RegionInfoBase<RegionTraits<Function>>() {
  66. }
  67. RegionInfo::~RegionInfo() {
  68. }
  69. void RegionInfo::updateStatistics(Region *R) {
  70. ++numRegions;
  71. // TODO: Slow. Should only be enabled if -stats is used.
  72. if (R->isSimple())
  73. ++numSimpleRegions;
  74. }
  75. void RegionInfo::recalculate(Function &F, DominatorTree *DT_,
  76. PostDominatorTree *PDT_, DominanceFrontier *DF_) {
  77. DT = DT_;
  78. PDT = PDT_;
  79. DF = DF_;
  80. TopLevelRegion = new Region(&F.getEntryBlock(), nullptr,
  81. this, DT, nullptr);
  82. updateStatistics(TopLevelRegion);
  83. calculate(F);
  84. }
  85. //===----------------------------------------------------------------------===//
  86. // RegionInfoPass implementation
  87. //
  88. RegionInfoPass::RegionInfoPass() : FunctionPass(ID) {
  89. initializeRegionInfoPassPass(*PassRegistry::getPassRegistry());
  90. }
  91. RegionInfoPass::~RegionInfoPass() {
  92. }
  93. bool RegionInfoPass::runOnFunction(Function &F) {
  94. releaseMemory();
  95. auto DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
  96. auto PDT = &getAnalysis<PostDominatorTree>();
  97. auto DF = &getAnalysis<DominanceFrontier>();
  98. RI.recalculate(F, DT, PDT, DF);
  99. return false;
  100. }
  101. void RegionInfoPass::releaseMemory() {
  102. RI.releaseMemory();
  103. }
  104. void RegionInfoPass::verifyAnalysis() const {
  105. RI.verifyAnalysis();
  106. }
  107. void RegionInfoPass::getAnalysisUsage(AnalysisUsage &AU) const {
  108. AU.setPreservesAll();
  109. AU.addRequiredTransitive<DominatorTreeWrapperPass>();
  110. AU.addRequired<PostDominatorTree>();
  111. AU.addRequired<DominanceFrontier>();
  112. }
  113. void RegionInfoPass::print(raw_ostream &OS, const Module *) const {
  114. RI.print(OS);
  115. }
  116. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  117. void RegionInfoPass::dump() const {
  118. RI.dump();
  119. }
  120. #endif
  121. char RegionInfoPass::ID = 0;
  122. INITIALIZE_PASS_BEGIN(RegionInfoPass, "regions",
  123. "Detect single entry single exit regions", true, true)
  124. INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
  125. INITIALIZE_PASS_DEPENDENCY(PostDominatorTree)
  126. INITIALIZE_PASS_DEPENDENCY(DominanceFrontier)
  127. INITIALIZE_PASS_END(RegionInfoPass, "regions",
  128. "Detect single entry single exit regions", true, true)
  129. // Create methods available outside of this file, to use them
  130. // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
  131. // the link time optimization.
  132. namespace llvm {
  133. FunctionPass *createRegionInfoPass() {
  134. return new RegionInfoPass();
  135. }
  136. }