LoopPass.h revision 1997473cf72957d0e70322e2fe6fe2ab141c58a6
1//===- LoopPass.h - LoopPass class ----------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Devang Patel and is distributed under 6// the University of Illinois Open Source 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_LOOP_PASS_H 16#define LLVM_LOOP_PASS_H 17 18#include "llvm/Analysis/LoopInfo.h" 19#include "llvm/Pass.h" 20#include "llvm/PassManagers.h" 21#include "llvm/Function.h" 22 23namespace llvm { 24 25class LPPassManager; 26class Loop; 27class Function; 28 29class LoopPass : public Pass { 30 31 public: 32 LoopPass(intptr_t pid) : Pass(pid) {} 33 34 // runOnLoop - THis method should be implemented by the subclass to perform 35 // whatever action is necessary for the specfied Loop. 36 virtual bool runOnLoop (Loop *L, LPPassManager &LPM) = 0; 37 virtual bool runOnFunctionBody (Function &F, LPPassManager &LPM) { 38 return false; 39 } 40 41 // Initialization and finalization hooks. 42 virtual bool doInitialization(Loop *L, LPPassManager &LPM) { 43 return false; 44 } 45 46 // Finalization hook does not supply Loop because at this time 47 // loop nest is completely different. 48 virtual bool doFinalization() { return false; } 49 50 // Check if this pass is suitable for the current LPPassManager, if 51 // available. This pass P is not suitable for a LPPassManager if P 52 // is not preserving higher level analysis info used by other 53 // LPPassManager passes. In such case, pop LPPassManager from the 54 // stack. This will force assignPassManager() to create new 55 // LPPassManger as expected. 56 void preparePassManager(PMStack &PMS); 57 58 /// Assign pass manager to manager this pass 59 virtual void assignPassManager(PMStack &PMS, 60 PassManagerType PMT = PMT_LoopPassManager); 61 62 /// Return what kind of Pass Manager can manage this pass. 63 virtual PassManagerType getPotentialPassManagerType() const { 64 return PMT_LoopPassManager; 65 } 66}; 67 68class LPPassManager : public FunctionPass, public PMDataManager { 69 70public: 71 static char ID; 72 LPPassManager(int Depth); 73 74 /// run - Execute all of the passes scheduled for execution. Keep track of 75 /// whether any of the passes modifies the module, and if so, return true. 76 bool runOnFunction(Function &F); 77 78 /// Pass Manager itself does not invalidate any analysis info. 79 // LPPassManager needs LoopInfo. 80 void getAnalysisUsage(AnalysisUsage &Info) const; 81 82 virtual const char *getPassName() const { 83 return "Loop Pass Manager"; 84 } 85 86 // Print passes managed by this manager 87 void dumpPassStructure(unsigned Offset) { 88 llvm::cerr << std::string(Offset*2, ' ') << "Loop Pass Manager\n"; 89 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { 90 Pass *P = getContainedPass(Index); 91 P->dumpPassStructure(Offset + 1); 92 dumpLastUses(P, Offset+1); 93 } 94 } 95 96 Pass *getContainedPass(unsigned N) { 97 assert ( N < PassVector.size() && "Pass number out of range!"); 98 Pass *FP = static_cast<Pass *>(PassVector[N]); 99 return FP; 100 } 101 102 virtual PassManagerType getPassManagerType() const { 103 return PMT_LoopPassManager; 104 } 105 106public: 107 // Delete loop from the loop queue and loop nest (LoopInfo). 108 void deleteLoopFromQueue(Loop *L); 109 110 // Inset loop into the loop nest(LoopInfo) and loop queue(LQ). 111 void insertLoop(Loop *L, Loop *ParentLoop); 112 113 // Reoptimize this loop. LPPassManager will re-insert this loop into the 114 // queue. This allows LoopPass to change loop nest for the loop. This 115 // utility may send LPPassManager into infinite loops so use caution. 116 void redoLoop(Loop *L); 117private: 118 std::deque<Loop *> LQ; 119 bool skipThisLoop; 120 bool redoThisLoop; 121 LoopInfo *LI; 122 Loop *CurrentLoop; 123}; 124 125} // End llvm namespace 126 127#endif 128