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