LLVM API Documentation

LoopPass.h

Go to the documentation of this file.
00001 //===- LoopPass.h - LoopPass class ----------------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines LoopPass class. All loop optimization
00011 // and transformation passes are derived from LoopPass.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_LOOP_PASS_H
00016 #define LLVM_LOOP_PASS_H
00017 
00018 #include "llvm/Analysis/LoopInfo.h"
00019 #include "llvm/Pass.h"
00020 #include "llvm/PassManagers.h"
00021 #include "llvm/Function.h"
00022 
00023 namespace llvm {
00024 
00025 class LPPassManager;
00026 class Function;
00027 class PMStack;
00028 
00029 class LoopPass : public Pass {
00030 
00031  public:
00032   explicit LoopPass(intptr_t pid) : Pass(pid) {}
00033   explicit LoopPass(void *pid) : Pass(pid) {}
00034 
00035   // runOnLoop - This method should be implemented by the subclass to perform
00036   // whatever action is necessary for the specified Loop.
00037   virtual bool runOnLoop (Loop *L, LPPassManager &LPM) = 0;
00038   virtual bool runOnFunctionBody (Function &F, LPPassManager &LPM) { 
00039     return false; 
00040   }
00041 
00042   // Initialization and finalization hooks.
00043   virtual bool doInitialization(Loop *L, LPPassManager &LPM) { 
00044     return false; 
00045   }
00046 
00047   // Finalization hook does not supply Loop because at this time
00048   // loop nest is completely different.
00049   virtual bool doFinalization() { return false; }
00050 
00051   // Check if this pass is suitable for the current LPPassManager, if
00052   // available. This pass P is not suitable for a LPPassManager if P
00053   // is not preserving higher level analysis info used by other
00054   // LPPassManager passes. In such case, pop LPPassManager from the
00055   // stack. This will force assignPassManager() to create new
00056   // LPPassManger as expected.
00057   void preparePassManager(PMStack &PMS);
00058 
00059   /// Assign pass manager to manager this pass
00060   virtual void assignPassManager(PMStack &PMS,
00061                                  PassManagerType PMT = PMT_LoopPassManager);
00062 
00063   ///  Return what kind of Pass Manager can manage this pass.
00064   virtual PassManagerType getPotentialPassManagerType() const {
00065     return PMT_LoopPassManager;
00066   }
00067 
00068   //===--------------------------------------------------------------------===//
00069   /// SimpleAnalysis - Provides simple interface to update analysis info
00070   /// maintained by various passes. Note, if required this interface can
00071   /// be extracted into a separate abstract class but it would require
00072   /// additional use of multiple inheritance in Pass class hierarchy, something
00073   /// we are trying to avoid.
00074 
00075   /// Each loop pass can override these simple analysis hooks to update
00076   /// desired analysis information.
00077   /// cloneBasicBlockAnalysis - Clone analysis info associated with basic block.
00078   virtual void cloneBasicBlockAnalysis(BasicBlock *F, BasicBlock *T, Loop *L) {}
00079 
00080   /// deletekAnalysisValue - Delete analysis info associated with value V.
00081   virtual void deleteAnalysisValue(Value *V, Loop *L) {}
00082 };
00083 
00084 class LPPassManager : public FunctionPass, public PMDataManager {
00085 
00086 public:
00087   static char ID;
00088   explicit LPPassManager(int Depth);
00089 
00090   /// run - Execute all of the passes scheduled for execution.  Keep track of
00091   /// whether any of the passes modifies the module, and if so, return true.
00092   bool runOnFunction(Function &F);
00093 
00094   /// Pass Manager itself does not invalidate any analysis info.
00095   // LPPassManager needs LoopInfo. 
00096   void getAnalysisUsage(AnalysisUsage &Info) const; 
00097   
00098   virtual const char *getPassName() const {
00099     return "Loop Pass Manager";
00100   }
00101   
00102   // Print passes managed by this manager
00103   void dumpPassStructure(unsigned Offset) {
00104     llvm::cerr << std::string(Offset*2, ' ') << "Loop Pass Manager\n";
00105     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
00106       Pass *P = getContainedPass(Index);
00107       P->dumpPassStructure(Offset + 1);
00108       dumpLastUses(P, Offset+1);
00109     }
00110   }
00111   
00112   Pass *getContainedPass(unsigned N) {
00113     assert ( N < PassVector.size() && "Pass number out of range!");
00114     Pass *FP = static_cast<Pass *>(PassVector[N]);
00115     return FP;
00116   }
00117 
00118   virtual PassManagerType getPassManagerType() const { 
00119     return PMT_LoopPassManager; 
00120   }
00121 
00122 public:
00123   // Delete loop from the loop queue and loop nest (LoopInfo).
00124   void deleteLoopFromQueue(Loop *L);
00125   
00126   // Insert loop into the loop nest(LoopInfo) and loop queue(LQ).
00127   void insertLoop(Loop *L, Loop *ParentLoop);
00128 
00129   // Reoptimize this loop. LPPassManager will re-insert this loop into the
00130   // queue. This allows LoopPass to change loop nest for the loop. This
00131   // utility may send LPPassManager into infinite loops so use caution.
00132   void redoLoop(Loop *L);
00133 
00134   //===--------------------------------------------------------------------===//
00135   /// SimpleAnalysis - Provides simple interface to update analysis info
00136   /// maintained by various passes. Note, if required this interface can
00137   /// be extracted into a separate abstract class but it would require
00138   /// additional use of multiple inheritance in Pass class hierarchy, something
00139   /// we are trying to avoid.
00140 
00141   /// cloneBasicBlockSimpleAnalysis - Invoke cloneBasicBlockAnalysis hook for
00142   /// all passes that implement simple analysis interface.
00143   void cloneBasicBlockSimpleAnalysis(BasicBlock *From, BasicBlock *To, Loop *L);
00144 
00145   /// deleteSimpleAnalysisValue - Invoke deleteAnalysisValue hook for all passes
00146   /// that implement simple analysis interface.
00147   void deleteSimpleAnalysisValue(Value *V, Loop *L);
00148 private:
00149   std::deque<Loop *> LQ;
00150   bool skipThisLoop;
00151   bool redoThisLoop;
00152   LoopInfo *LI;
00153   Loop *CurrentLoop;
00154 };
00155 
00156 } // End llvm namespace
00157 
00158 #endif