RegionPass.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===- RegionPass.h - RegionPass class --------------------------*- C++ -*-===//
  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 defines the RegionPass class. All region based analysis,
  11. // optimization and transformation passes are derived from RegionPass.
  12. // This class is implemented following the some ideas of the LoopPass.h class.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_ANALYSIS_REGIONPASS_H
  16. #define LLVM_ANALYSIS_REGIONPASS_H
  17. #include "llvm/Analysis/RegionInfo.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/LegacyPassManagers.h"
  20. #include "llvm/Pass.h"
  21. #include <deque>
  22. namespace llvm {
  23. class RGPassManager;
  24. class Function;
  25. // //
  26. ///////////////////////////////////////////////////////////////////////////////
  27. /// @brief A pass that runs on each Region in a function.
  28. ///
  29. /// RegionPass is managed by RGPassManager.
  30. class RegionPass : public Pass {
  31. public:
  32. explicit RegionPass(char &pid) : Pass(PT_Region, pid) {}
  33. //===--------------------------------------------------------------------===//
  34. /// @name To be implemented by every RegionPass
  35. ///
  36. //@{
  37. /// @brief Run the pass on a specific Region
  38. ///
  39. /// Accessing regions not contained in the current region is not allowed.
  40. ///
  41. /// @param R The region this pass is run on.
  42. /// @param RGM The RegionPassManager that manages this Pass.
  43. ///
  44. /// @return True if the pass modifies this Region.
  45. virtual bool runOnRegion(Region *R, RGPassManager &RGM) = 0;
  46. /// @brief Get a pass to print the LLVM IR in the region.
  47. ///
  48. /// @param O The output stream to print the Region.
  49. /// @param Banner The banner to separate different printed passes.
  50. ///
  51. /// @return The pass to print the LLVM IR in the region.
  52. Pass *createPrinterPass(raw_ostream &O,
  53. const std::string &Banner) const override;
  54. using llvm::Pass::doInitialization;
  55. using llvm::Pass::doFinalization;
  56. virtual bool doInitialization(Region *R, RGPassManager &RGM) { return false; }
  57. virtual bool doFinalization() { return false; }
  58. //@}
  59. //===--------------------------------------------------------------------===//
  60. /// @name PassManager API
  61. ///
  62. //@{
  63. void preparePassManager(PMStack &PMS) override;
  64. void assignPassManager(PMStack &PMS,
  65. PassManagerType PMT = PMT_RegionPassManager) override;
  66. PassManagerType getPotentialPassManagerType() const override {
  67. return PMT_RegionPassManager;
  68. }
  69. //@}
  70. };
  71. /// @brief The pass manager to schedule RegionPasses.
  72. class RGPassManager : public FunctionPass, public PMDataManager {
  73. std::deque<Region*> RQ;
  74. bool skipThisRegion;
  75. bool redoThisRegion;
  76. RegionInfo *RI;
  77. Region *CurrentRegion;
  78. public:
  79. static char ID;
  80. explicit RGPassManager();
  81. /// @brief Execute all of the passes scheduled for execution.
  82. ///
  83. /// @return True if any of the passes modifies the function.
  84. bool runOnFunction(Function &F) override;
  85. /// Pass Manager itself does not invalidate any analysis info.
  86. /// RGPassManager needs RegionInfo.
  87. void getAnalysisUsage(AnalysisUsage &Info) const override;
  88. const char *getPassName() const override {
  89. return "Region Pass Manager";
  90. }
  91. PMDataManager *getAsPMDataManager() override { return this; }
  92. Pass *getAsPass() override { return this; }
  93. /// @brief Print passes managed by this manager.
  94. void dumpPassStructure(unsigned Offset) override;
  95. /// @brief Get passes contained by this manager.
  96. Pass *getContainedPass(unsigned N) {
  97. assert(N < PassVector.size() && "Pass number out of range!");
  98. Pass *FP = static_cast<Pass *>(PassVector[N]);
  99. return FP;
  100. }
  101. PassManagerType getPassManagerType() const override {
  102. return PMT_RegionPassManager;
  103. }
  104. };
  105. } // End llvm namespace
  106. #endif