RegionPass.h 4.2 KB

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