LoopPass.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //===- LoopPass.h - LoopPass class ----------------------------------------===//
  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 LoopPass class. All loop optimization
  11. // and transformation passes are derived from LoopPass.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ANALYSIS_LOOPPASS_H
  15. #define LLVM_ANALYSIS_LOOPPASS_H
  16. #include "llvm/Analysis/LoopInfo.h"
  17. #include "llvm/IR/LegacyPassManagers.h"
  18. #include "llvm/Pass.h"
  19. #include <deque>
  20. namespace llvm {
  21. class LPPassManager;
  22. class Function;
  23. class PMStack;
  24. class LoopPass : public Pass {
  25. public:
  26. explicit LoopPass(char &pid) : Pass(PT_Loop, pid) {}
  27. /// getPrinterPass - Get a pass to print the function corresponding
  28. /// to a Loop.
  29. Pass *createPrinterPass(raw_ostream &O,
  30. const std::string &Banner) const override;
  31. // runOnLoop - This method should be implemented by the subclass to perform
  32. // whatever action is necessary for the specified Loop.
  33. virtual bool runOnLoop(Loop *L, LPPassManager &LPM) = 0;
  34. using llvm::Pass::doInitialization;
  35. using llvm::Pass::doFinalization;
  36. // Initialization and finalization hooks.
  37. virtual bool doInitialization(Loop *L, LPPassManager &LPM) {
  38. return false;
  39. }
  40. // Finalization hook does not supply Loop because at this time
  41. // loop nest is completely different.
  42. virtual bool doFinalization() { return false; }
  43. // Check if this pass is suitable for the current LPPassManager, if
  44. // available. This pass P is not suitable for a LPPassManager if P
  45. // is not preserving higher level analysis info used by other
  46. // LPPassManager passes. In such case, pop LPPassManager from the
  47. // stack. This will force assignPassManager() to create new
  48. // LPPassManger as expected.
  49. void preparePassManager(PMStack &PMS) override;
  50. /// Assign pass manager to manage this pass
  51. void assignPassManager(PMStack &PMS, PassManagerType PMT) override;
  52. /// Return what kind of Pass Manager can manage this pass.
  53. PassManagerType getPotentialPassManagerType() const override {
  54. return PMT_LoopPassManager;
  55. }
  56. //===--------------------------------------------------------------------===//
  57. /// SimpleAnalysis - Provides simple interface to update analysis info
  58. /// maintained by various passes. Note, if required this interface can
  59. /// be extracted into a separate abstract class but it would require
  60. /// additional use of multiple inheritance in Pass class hierarchy, something
  61. /// we are trying to avoid.
  62. /// Each loop pass can override these simple analysis hooks to update
  63. /// desired analysis information.
  64. /// cloneBasicBlockAnalysis - Clone analysis info associated with basic block.
  65. virtual void cloneBasicBlockAnalysis(BasicBlock *F, BasicBlock *T, Loop *L) {}
  66. /// deleteAnalysisValue - Delete analysis info associated with value V.
  67. virtual void deleteAnalysisValue(Value *V, Loop *L) {}
  68. /// Delete analysis info associated with Loop L.
  69. /// Called to notify a Pass that a loop has been deleted and any
  70. /// associated analysis values can be deleted.
  71. virtual void deleteAnalysisLoop(Loop *L) {}
  72. protected:
  73. /// skipOptnoneFunction - Containing function has Attribute::OptimizeNone
  74. /// and most transformation passes should skip it.
  75. bool skipOptnoneFunction(const Loop *L) const;
  76. };
  77. class LPPassManager : public FunctionPass, public PMDataManager {
  78. public:
  79. static char ID;
  80. explicit LPPassManager();
  81. /// run - Execute all of the passes scheduled for execution. Keep track of
  82. /// whether any of the passes modifies the module, and if so, return true.
  83. bool runOnFunction(Function &F) override;
  84. /// Pass Manager itself does not invalidate any analysis info.
  85. // LPPassManager needs LoopInfo.
  86. void getAnalysisUsage(AnalysisUsage &Info) const override;
  87. const char *getPassName() const override {
  88. return "Loop Pass Manager";
  89. }
  90. PMDataManager *getAsPMDataManager() override { return this; }
  91. Pass *getAsPass() override { return this; }
  92. /// Print passes managed by this manager
  93. void dumpPassStructure(unsigned Offset) override;
  94. LoopPass *getContainedPass(unsigned N) {
  95. assert(N < PassVector.size() && "Pass number out of range!");
  96. LoopPass *LP = static_cast<LoopPass *>(PassVector[N]);
  97. return LP;
  98. }
  99. PassManagerType getPassManagerType() const override {
  100. return PMT_LoopPassManager;
  101. }
  102. public:
  103. // Delete loop from the loop queue and loop nest (LoopInfo).
  104. void deleteLoopFromQueue(Loop *L);
  105. // Insert loop into the loop queue and add it as a child of the
  106. // given parent.
  107. void insertLoop(Loop *L, Loop *ParentLoop);
  108. // Insert a loop into the loop queue.
  109. void insertLoopIntoQueue(Loop *L);
  110. // Reoptimize this loop. LPPassManager will re-insert this loop into the
  111. // queue. This allows LoopPass to change loop nest for the loop. This
  112. // utility may send LPPassManager into infinite loops so use caution.
  113. void redoLoop(Loop *L);
  114. //===--------------------------------------------------------------------===//
  115. /// SimpleAnalysis - Provides simple interface to update analysis info
  116. /// maintained by various passes. Note, if required this interface can
  117. /// be extracted into a separate abstract class but it would require
  118. /// additional use of multiple inheritance in Pass class hierarchy, something
  119. /// we are trying to avoid.
  120. /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
  121. /// all passes that implement simple analysis interface.
  122. void cloneBasicBlockSimpleAnalysis(BasicBlock *From, BasicBlock *To, Loop *L);
  123. /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes
  124. /// that implement simple analysis interface.
  125. void deleteSimpleAnalysisValue(Value *V, Loop *L);
  126. /// Invoke deleteAnalysisLoop hook for all passes that implement simple
  127. /// analysis interface.
  128. void deleteSimpleAnalysisLoop(Loop *L);
  129. private:
  130. std::deque<Loop *> LQ;
  131. bool skipThisLoop;
  132. bool redoThisLoop;
  133. LoopInfo *LI;
  134. Loop *CurrentLoop;
  135. };
  136. } // End llvm namespace
  137. #endif