LoopPass.h 4.5 KB

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