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