2
0

RegionPass.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //===- RegionPass.cpp - Region Pass and Region Pass Manager ---------------===//
  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. //
  10. // This file implements RegionPass and RGPassManager. All region optimization
  11. // and transformation passes are derived from RegionPass. RGPassManager is
  12. // responsible for managing RegionPasses.
  13. // most of these codes are COPY from LoopPass.cpp
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Analysis/RegionPass.h"
  17. #include "llvm/Analysis/RegionIterator.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/Timer.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "regionpassmgr"
  23. //===----------------------------------------------------------------------===//
  24. // RGPassManager
  25. //
  26. char RGPassManager::ID = 0;
  27. RGPassManager::RGPassManager()
  28. : FunctionPass(ID), PMDataManager() {
  29. skipThisRegion = false;
  30. redoThisRegion = false;
  31. RI = nullptr;
  32. CurrentRegion = nullptr;
  33. }
  34. // Recurse through all subregions and all regions into RQ.
  35. static void addRegionIntoQueue(Region &R, std::deque<Region *> &RQ) {
  36. RQ.push_back(&R);
  37. for (const auto &E : R)
  38. addRegionIntoQueue(*E, RQ);
  39. }
  40. /// Pass Manager itself does not invalidate any analysis info.
  41. void RGPassManager::getAnalysisUsage(AnalysisUsage &Info) const {
  42. Info.addRequired<RegionInfoPass>();
  43. Info.setPreservesAll();
  44. }
  45. /// run - Execute all of the passes scheduled for execution. Keep track of
  46. /// whether any of the passes modifies the function, and if so, return true.
  47. bool RGPassManager::runOnFunction(Function &F) {
  48. RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
  49. bool Changed = false;
  50. // Collect inherited analysis from Module level pass manager.
  51. populateInheritedAnalysis(TPM->activeStack);
  52. addRegionIntoQueue(*RI->getTopLevelRegion(), RQ);
  53. if (RQ.empty()) // No regions, skip calling finalizers
  54. return false;
  55. // Initialization
  56. for (std::deque<Region *>::const_iterator I = RQ.begin(), E = RQ.end();
  57. I != E; ++I) {
  58. Region *R = *I;
  59. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  60. RegionPass *RP = (RegionPass *)getContainedPass(Index);
  61. Changed |= RP->doInitialization(R, *this);
  62. }
  63. }
  64. // Walk Regions
  65. while (!RQ.empty()) {
  66. CurrentRegion = RQ.back();
  67. skipThisRegion = false;
  68. redoThisRegion = false;
  69. // Run all passes on the current Region.
  70. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  71. RegionPass *P = (RegionPass*)getContainedPass(Index);
  72. if (isPassDebuggingExecutionsOrMore()) {
  73. dumpPassInfo(P, EXECUTION_MSG, ON_REGION_MSG,
  74. CurrentRegion->getNameStr());
  75. dumpRequiredSet(P);
  76. }
  77. initializeAnalysisImpl(P);
  78. {
  79. PassManagerPrettyStackEntry X(P, *CurrentRegion->getEntry());
  80. TimeRegion PassTimer(getPassTimer(P));
  81. Changed |= P->runOnRegion(CurrentRegion, *this);
  82. }
  83. if (isPassDebuggingExecutionsOrMore()) {
  84. if (Changed)
  85. dumpPassInfo(P, MODIFICATION_MSG, ON_REGION_MSG,
  86. skipThisRegion ? "<deleted>" :
  87. CurrentRegion->getNameStr());
  88. dumpPreservedSet(P);
  89. }
  90. if (!skipThisRegion) {
  91. // Manually check that this region is still healthy. This is done
  92. // instead of relying on RegionInfo::verifyRegion since RegionInfo
  93. // is a function pass and it's really expensive to verify every
  94. // Region in the function every time. That level of checking can be
  95. // enabled with the -verify-region-info option.
  96. {
  97. TimeRegion PassTimer(getPassTimer(P));
  98. CurrentRegion->verifyRegion();
  99. }
  100. // Then call the regular verifyAnalysis functions.
  101. verifyPreservedAnalysis(P);
  102. }
  103. removeNotPreservedAnalysis(P);
  104. recordAvailableAnalysis(P);
  105. removeDeadPasses(P,
  106. (!isPassDebuggingExecutionsOrMore() || skipThisRegion) ?
  107. "<deleted>" : CurrentRegion->getNameStr(),
  108. ON_REGION_MSG);
  109. if (skipThisRegion)
  110. // Do not run other passes on this region.
  111. break;
  112. }
  113. // If the region was deleted, release all the region passes. This frees up
  114. // some memory, and avoids trouble with the pass manager trying to call
  115. // verifyAnalysis on them.
  116. if (skipThisRegion)
  117. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  118. Pass *P = getContainedPass(Index);
  119. freePass(P, "<deleted>", ON_REGION_MSG);
  120. }
  121. // Pop the region from queue after running all passes.
  122. RQ.pop_back();
  123. if (redoThisRegion)
  124. RQ.push_back(CurrentRegion);
  125. // Free all region nodes created in region passes.
  126. RI->clearNodeCache();
  127. }
  128. // Finalization
  129. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  130. RegionPass *P = (RegionPass*)getContainedPass(Index);
  131. Changed |= P->doFinalization();
  132. }
  133. // Print the region tree after all pass.
  134. DEBUG(
  135. dbgs() << "\nRegion tree of function " << F.getName()
  136. << " after all region Pass:\n";
  137. RI->dump();
  138. dbgs() << "\n";
  139. );
  140. return Changed;
  141. }
  142. /// Print passes managed by this manager
  143. void RGPassManager::dumpPassStructure(unsigned Offset) {
  144. errs().indent(Offset*2) << "Region Pass Manager\n";
  145. for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
  146. Pass *P = getContainedPass(Index);
  147. P->dumpPassStructure(Offset + 1);
  148. dumpLastUses(P, Offset+1);
  149. }
  150. }
  151. namespace {
  152. //===----------------------------------------------------------------------===//
  153. // PrintRegionPass
  154. class PrintRegionPass : public RegionPass {
  155. private:
  156. std::string Banner;
  157. raw_ostream &Out; // raw_ostream to print on.
  158. public:
  159. static char ID;
  160. PrintRegionPass(const std::string &B, raw_ostream &o)
  161. : RegionPass(ID), Banner(B), Out(o) {}
  162. void getAnalysisUsage(AnalysisUsage &AU) const override {
  163. AU.setPreservesAll();
  164. }
  165. bool runOnRegion(Region *R, RGPassManager &RGM) override {
  166. Out << Banner;
  167. for (const auto *BB : R->blocks()) {
  168. if (BB)
  169. BB->print(Out);
  170. else
  171. Out << "Printing <null> Block";
  172. }
  173. return false;
  174. }
  175. };
  176. char PrintRegionPass::ID = 0;
  177. } //end anonymous namespace
  178. //===----------------------------------------------------------------------===//
  179. // RegionPass
  180. // Check if this pass is suitable for the current RGPassManager, if
  181. // available. This pass P is not suitable for a RGPassManager if P
  182. // is not preserving higher level analysis info used by other
  183. // RGPassManager passes. In such case, pop RGPassManager from the
  184. // stack. This will force assignPassManager() to create new
  185. // LPPassManger as expected.
  186. void RegionPass::preparePassManager(PMStack &PMS) {
  187. // Find RGPassManager
  188. while (!PMS.empty() &&
  189. PMS.top()->getPassManagerType() > PMT_RegionPassManager)
  190. PMS.pop();
  191. // If this pass is destroying high level information that is used
  192. // by other passes that are managed by LPM then do not insert
  193. // this pass in current LPM. Use new RGPassManager.
  194. if (PMS.top()->getPassManagerType() == PMT_RegionPassManager &&
  195. !PMS.top()->preserveHigherLevelAnalysis(this))
  196. PMS.pop();
  197. }
  198. /// Assign pass manager to manage this pass.
  199. void RegionPass::assignPassManager(PMStack &PMS,
  200. PassManagerType PreferredType) {
  201. std::unique_ptr<RegionPass> thisPtr(this); // HLSL Change
  202. // Find RGPassManager
  203. while (!PMS.empty() &&
  204. PMS.top()->getPassManagerType() > PMT_RegionPassManager)
  205. PMS.pop();
  206. RGPassManager *RGPM;
  207. // Create new Region Pass Manager if it does not exist.
  208. if (PMS.top()->getPassManagerType() == PMT_RegionPassManager)
  209. RGPM = (RGPassManager*)PMS.top();
  210. else {
  211. assert (!PMS.empty() && "Unable to create Region Pass Manager");
  212. PMDataManager *PMD = PMS.top();
  213. // [1] Create new Region Pass Manager
  214. RGPM = new RGPassManager();
  215. RGPM->populateInheritedAnalysis(PMS);
  216. // [2] Set up new manager's top level manager
  217. PMTopLevelManager *TPM = PMD->getTopLevelManager();
  218. TPM->addIndirectPassManager(RGPM);
  219. // [3] Assign manager to manage this new manager. This may create
  220. // and push new managers into PMS
  221. TPM->schedulePass(RGPM);
  222. // [4] Push new manager into PMS
  223. PMS.push(RGPM);
  224. }
  225. thisPtr.release(); // HLSL Change
  226. RGPM->add(this);
  227. }
  228. /// Get the printer pass
  229. Pass *RegionPass::createPrinterPass(raw_ostream &O,
  230. const std::string &Banner) const {
  231. return new PrintRegionPass(Banner, O);
  232. }