LoopPass.h revision 206124
150472Speter//===- LoopPass.h - LoopPass class ----------------------------------------===// 234495Sjdp// 334495Sjdp// The LLVM Compiler Infrastructure 4234782Skib// 534495Sjdp// This file is distributed under the University of Illinois Open Source 634495Sjdp// License. See LICENSE.TXT for details. 734495Sjdp// 8172326Smarcel//===----------------------------------------------------------------------===// 9172326Smarcel// 10172326Smarcel// This file defines LoopPass class. All loop optimization 11172326Smarcel// and transformation passes are derived from LoopPass. 1284910Sobrien// 1384910Sobrien//===----------------------------------------------------------------------===// 14218822Sdim 15218822Sdim#ifndef LLVM_LOOP_PASS_H 16218822Sdim#define LLVM_LOOP_PASS_H 17218822Sdim 18218822Sdim#include "llvm/Analysis/LoopInfo.h" 19218822Sdim#include "llvm/Pass.h" 20218822Sdim#include "llvm/PassManagers.h" 21218822Sdim#include "llvm/Function.h" 22218822Sdim 23218822Sdimnamespace llvm { 24218822Sdim 25218822Sdimclass LPPassManager; 26218822Sdimclass Function; 27218822Sdimclass PMStack; 28218822Sdim 2983877Sruclass LoopPass : public Pass { 30131832Sobrienpublic: 31131832Sobrien explicit LoopPass(intptr_t pid) : Pass(PT_Loop, pid) {} 3290330Sobrien explicit LoopPass(void *pid) : Pass(PT_Loop, pid) {} 3389886Sobrien 34257662Sbrooks /// getPrinterPass - Get a pass to print the function corresponding 35257662Sbrooks /// to a Loop. 36257662Sbrooks Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const; 37257662Sbrooks 38257662Sbrooks // runOnLoop - This method should be implemented by the subclass to perform 39257662Sbrooks // whatever action is necessary for the specified Loop. 40130757Sobrien virtual bool runOnLoop(Loop *L, LPPassManager &LPM) = 0; 41130575Sobrien 4284910Sobrien // Initialization and finalization hooks. 43234782Skib virtual bool doInitialization(Loop *L, LPPassManager &LPM) { 44139112Sru return false; 45234782Skib } 46113398Sru 47113398Sru // Finalization hook does not supply Loop because at this time 4884910Sobrien // loop nest is completely different. 4934495Sjdp virtual bool doFinalization() { return false; } 5060777Sobrien 5134495Sjdp // Check if this pass is suitable for the current LPPassManager, if 52131832Sobrien // available. This pass P is not suitable for a LPPassManager if P 53219811Smarcel // is not preserving higher level analysis info used by other 54218822Sdim // LPPassManager passes. In such case, pop LPPassManager from the 55131832Sobrien // stack. This will force assignPassManager() to create new 56131832Sobrien // LPPassManger as expected. 57131832Sobrien void preparePassManager(PMStack &PMS); 5852938Sjb 5935717Sjb /// Assign pass manager to manage this pass 6035717Sjb virtual void assignPassManager(PMStack &PMS, 61218822Sdim PassManagerType PMT = PMT_LoopPassManager); 6293361Sobrien 6393361Sobrien /// Return what kind of Pass Manager can manage this pass. 6435717Sjb virtual PassManagerType getPotentialPassManagerType() const { 6535717Sjb return PMT_LoopPassManager; 6635717Sjb } 6784910Sobrien 6884910Sobrien //===--------------------------------------------------------------------===// 6935717Sjb /// SimpleAnalysis - Provides simple interface to update analysis info 7060777Sobrien /// maintained by various passes. Note, if required this interface can 7169164Sobrien /// be extracted into a separate abstract class but it would require 7260777Sobrien /// additional use of multiple inheritance in Pass class hierarchy, something 7334495Sjdp /// we are trying to avoid. 74100872Sru 7534495Sjdp /// Each loop pass can override these simple analysis hooks to update 7634495Sjdp /// desired analysis information. 7734495Sjdp /// cloneBasicBlockAnalysis - Clone analysis info associated with basic block. 78 virtual void cloneBasicBlockAnalysis(BasicBlock *F, BasicBlock *T, Loop *L) {} 79 80 /// deleteAnalysisValue - Delete analysis info associated with value V. 81 virtual void deleteAnalysisValue(Value *V, Loop *L) {} 82}; 83 84class LPPassManager : public FunctionPass, public PMDataManager { 85public: 86 static char ID; 87 explicit LPPassManager(int Depth); 88 89 /// run - Execute all of the passes scheduled for execution. Keep track of 90 /// whether any of the passes modifies the module, and if so, return true. 91 bool runOnFunction(Function &F); 92 93 /// Pass Manager itself does not invalidate any analysis info. 94 // LPPassManager needs LoopInfo. 95 void getAnalysisUsage(AnalysisUsage &Info) const; 96 97 virtual const char *getPassName() const { 98 return "Loop Pass Manager"; 99 } 100 101 virtual PMDataManager *getAsPMDataManager() { return this; } 102 virtual Pass *getAsPass() { return this; } 103 104 /// Print passes managed by this manager 105 void dumpPassStructure(unsigned Offset); 106 107 Pass *getContainedPass(unsigned N) { 108 assert(N < PassVector.size() && "Pass number out of range!"); 109 Pass *FP = static_cast<Pass *>(PassVector[N]); 110 return FP; 111 } 112 113 virtual PassManagerType getPassManagerType() const { 114 return PMT_LoopPassManager; 115 } 116 117public: 118 // Delete loop from the loop queue and loop nest (LoopInfo). 119 void deleteLoopFromQueue(Loop *L); 120 121 // Insert loop into the loop queue and add it as a child of the 122 // given parent. 123 void insertLoop(Loop *L, Loop *ParentLoop); 124 125 // Insert a loop into the loop queue. 126 void insertLoopIntoQueue(Loop *L); 127 128 // Reoptimize this loop. LPPassManager will re-insert this loop into the 129 // queue. This allows LoopPass to change loop nest for the loop. This 130 // utility may send LPPassManager into infinite loops so use caution. 131 void redoLoop(Loop *L); 132 133 //===--------------------------------------------------------------------===// 134 /// SimpleAnalysis - Provides simple interface to update analysis info 135 /// maintained by various passes. Note, if required this interface can 136 /// be extracted into a separate abstract class but it would require 137 /// additional use of multiple inheritance in Pass class hierarchy, something 138 /// we are trying to avoid. 139 140 /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for 141 /// all passes that implement simple analysis interface. 142 void cloneBasicBlockSimpleAnalysis(BasicBlock *From, BasicBlock *To, Loop *L); 143 144 /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes 145 /// that implement simple analysis interface. 146 void deleteSimpleAnalysisValue(Value *V, Loop *L); 147 148private: 149 std::deque<Loop *> LQ; 150 bool skipThisLoop; 151 bool redoThisLoop; 152 LoopInfo *LI; 153 Loop *CurrentLoop; 154}; 155 156} // End llvm namespace 157 158#endif 159