RegionPass.h 4.2 KB

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