LoopPass.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- LoopPass.h - LoopPass class ----------------------------------------===//
  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 LoopPass class. All loop optimization
  15. // and transformation passes are derived from LoopPass.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_ANALYSIS_LOOPPASS_H
  19. #define LLVM_ANALYSIS_LOOPPASS_H
  20. #include "llvm/IR/LegacyPassManagers.h"
  21. #include "llvm/Pass.h"
  22. #include <deque>
  23. namespace llvm {
  24. class Loop;
  25. class LoopInfo;
  26. class LPPassManager;
  27. class Function;
  28. class LoopPass : public Pass {
  29. public:
  30. explicit LoopPass(char &pid) : Pass(PT_Loop, pid) {}
  31. /// getPrinterPass - Get a pass to print the function corresponding
  32. /// to a Loop.
  33. Pass *createPrinterPass(raw_ostream &O,
  34. const std::string &Banner) const override;
  35. // runOnLoop - This method should be implemented by the subclass to perform
  36. // whatever action is necessary for the specified Loop.
  37. virtual bool runOnLoop(Loop *L, LPPassManager &LPM) = 0;
  38. using llvm::Pass::doInitialization;
  39. using llvm::Pass::doFinalization;
  40. // Initialization and finalization hooks.
  41. virtual bool doInitialization(Loop *L, LPPassManager &LPM) {
  42. return false;
  43. }
  44. // Finalization hook does not supply Loop because at this time
  45. // loop nest is completely different.
  46. virtual bool doFinalization() { return false; }
  47. // Check if this pass is suitable for the current LPPassManager, if
  48. // available. This pass P is not suitable for a LPPassManager if P
  49. // is not preserving higher level analysis info used by other
  50. // LPPassManager passes. In such case, pop LPPassManager from the
  51. // stack. This will force assignPassManager() to create new
  52. // LPPassManger as expected.
  53. void preparePassManager(PMStack &PMS) override;
  54. /// Assign pass manager to manage this pass
  55. void assignPassManager(PMStack &PMS, PassManagerType PMT) override;
  56. /// Return what kind of Pass Manager can manage this pass.
  57. PassManagerType getPotentialPassManagerType() const override {
  58. return PMT_LoopPassManager;
  59. }
  60. protected:
  61. /// Optional passes call this function to check whether the pass should be
  62. /// skipped. This is the case when Attribute::OptimizeNone is set or when
  63. /// optimization bisect is over the limit.
  64. bool skipLoop(const Loop *L) const;
  65. };
  66. class LPPassManager : public FunctionPass, public PMDataManager {
  67. public:
  68. static char ID;
  69. explicit LPPassManager();
  70. /// run - Execute all of the passes scheduled for execution. Keep track of
  71. /// whether any of the passes modifies the module, and if so, return true.
  72. bool runOnFunction(Function &F) override;
  73. /// Pass Manager itself does not invalidate any analysis info.
  74. // LPPassManager needs LoopInfo.
  75. void getAnalysisUsage(AnalysisUsage &Info) const override;
  76. StringRef getPassName() const override { return "Loop Pass Manager"; }
  77. PMDataManager *getAsPMDataManager() override { return this; }
  78. Pass *getAsPass() override { return this; }
  79. /// Print passes managed by this manager
  80. void dumpPassStructure(unsigned Offset) override;
  81. LoopPass *getContainedPass(unsigned N) {
  82. assert(N < PassVector.size() && "Pass number out of range!");
  83. LoopPass *LP = static_cast<LoopPass *>(PassVector[N]);
  84. return LP;
  85. }
  86. PassManagerType getPassManagerType() const override {
  87. return PMT_LoopPassManager;
  88. }
  89. public:
  90. // Add a new loop into the loop queue.
  91. void addLoop(Loop &L);
  92. // Mark \p L as deleted.
  93. void markLoopAsDeleted(Loop &L);
  94. private:
  95. std::deque<Loop *> LQ;
  96. LoopInfo *LI;
  97. Loop *CurrentLoop;
  98. bool CurrentLoopDeleted;
  99. };
  100. // This pass is required by the LCSSA transformation. It is used inside
  101. // LPPassManager to check if current pass preserves LCSSA form, and if it does
  102. // pass manager calls lcssa verification for the current loop.
  103. struct LCSSAVerificationPass : public FunctionPass {
  104. static char ID;
  105. LCSSAVerificationPass();
  106. bool runOnFunction(Function &F) override { return false; }
  107. void getAnalysisUsage(AnalysisUsage &AU) const override {
  108. AU.setPreservesAll();
  109. }
  110. };
  111. } // End llvm namespace
  112. #endif
  113. #ifdef __GNUC__
  114. #pragma GCC diagnostic pop
  115. #endif