LLVM API Documentation

Pass.h

Go to the documentation of this file.
00001 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
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 a base class that indicates that a specified class is a
00011 // transformation pass implementation.
00012 //
00013 // Passes are designed this way so that it is possible to run passes in a cache
00014 // and organizationally optimal order without having to specify it at the front
00015 // end.  This allows arbitrary passes to be strung together and have them
00016 // executed as effeciently as possible.
00017 //
00018 // Passes should extend one of the classes below, depending on the guarantees
00019 // that it can make about what will be modified as it is run.  For example, most
00020 // global optimizations should derive from FunctionPass, because they do not add
00021 // or delete functions, they operate on the internals of the function.
00022 //
00023 // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
00024 // bottom), so the APIs exposed by these files are also automatically available
00025 // to all users of this file.
00026 //
00027 //===----------------------------------------------------------------------===//
00028 
00029 #ifndef LLVM_PASS_H
00030 #define LLVM_PASS_H
00031 
00032 #include "llvm/Support/DataTypes.h"
00033 #include "llvm/Support/Streams.h"
00034 #include <cassert>
00035 #include <iosfwd>
00036 #include <utility>
00037 #include <vector>
00038 
00039 namespace llvm {
00040 
00041 class BasicBlock;
00042 class Function;
00043 class Module;
00044 class AnalysisUsage;
00045 class PassInfo;
00046 class ImmutablePass;
00047 class PMStack;
00048 class AnalysisResolver;
00049 class PMDataManager;
00050 
00051 // AnalysisID - Use the PassInfo to identify a pass...
00052 typedef const PassInfo* AnalysisID;
00053 
00054 /// Different types of internal pass managers. External pass managers
00055 /// (PassManager and FunctionPassManager) are not represented here.
00056 /// Ordering of pass manager types is important here.
00057 enum PassManagerType {
00058   PMT_Unknown = 0,
00059   PMT_ModulePassManager = 1, /// MPPassManager 
00060   PMT_CallGraphPassManager,  /// CGPassManager
00061   PMT_FunctionPassManager,   /// FPPassManager
00062   PMT_LoopPassManager,       /// LPPassManager
00063   PMT_BasicBlockPassManager, /// BBPassManager
00064   PMT_Last
00065 };
00066 
00067 //===----------------------------------------------------------------------===//
00068 /// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
00069 /// interprocedural optimization or you do not fit into any of the more
00070 /// constrained passes described below.
00071 ///
00072 class Pass {
00073   AnalysisResolver *Resolver;  // Used to resolve analysis
00074   intptr_t PassID;
00075   // AnalysisImpls - This keeps track of which passes implement the interfaces
00076   // that are required by the current pass (to implement getAnalysis()).
00077   //
00078   std::vector<std::pair<const PassInfo*, Pass*> > AnalysisImpls;
00079 
00080   void operator=(const Pass&);  // DO NOT IMPLEMENT
00081   Pass(const Pass &);           // DO NOT IMPLEMENT
00082 public:
00083   explicit Pass(intptr_t pid) : Resolver(0), PassID(pid) {
00084     assert(pid && "pid cannot be 0");
00085   }
00086   explicit Pass(const void *pid) : Resolver(0), PassID((intptr_t)pid) {
00087     assert(pid && "pid cannot be 0"); 
00088   }
00089   virtual ~Pass();
00090 
00091   /// getPassName - Return a nice clean name for a pass.  This usually
00092   /// implemented in terms of the name that is registered by one of the
00093   /// Registration templates, but can be overloaded directly.
00094   ///
00095   virtual const char *getPassName() const;
00096 
00097   /// getPassInfo - Return the PassInfo data structure that corresponds to this
00098   /// pass...  If the pass has not been registered, this will return null.
00099   ///
00100   const PassInfo *getPassInfo() const;
00101 
00102   /// print - Print out the internal state of the pass.  This is called by
00103   /// Analyze to print out the contents of an analysis.  Otherwise it is not
00104   /// necessary to implement this method.  Beware that the module pointer MAY be
00105   /// null.  This automatically forwards to a virtual function that does not
00106   /// provide the Module* in case the analysis doesn't need it it can just be
00107   /// ignored.
00108   ///
00109   virtual void print(std::ostream &O, const Module *M) const;
00110   void print(std::ostream *O, const Module *M) const { if (O) print(*O, M); }
00111   void dump() const; // dump - call print(std::cerr, 0);
00112 
00113   /// Each pass is responsible for assigning a pass manager to itself.
00114   /// PMS is the stack of available pass manager. 
00115   virtual void assignPassManager(PMStack &, 
00116                                  PassManagerType = PMT_Unknown) {}
00117   /// Check if available pass managers are suitable for this pass or not.
00118   virtual void preparePassManager(PMStack &) {}
00119   
00120   ///  Return what kind of Pass Manager can manage this pass.
00121   virtual PassManagerType getPotentialPassManagerType() const {
00122     return PMT_Unknown; 
00123   }
00124 
00125   // Access AnalysisResolver
00126   inline void setResolver(AnalysisResolver *AR) { 
00127     assert (!Resolver && "Resolver is already set");
00128     Resolver = AR; 
00129   }
00130   inline AnalysisResolver *getResolver() { 
00131     return Resolver; 
00132   }
00133 
00134   /// getAnalysisUsage - This function should be overriden by passes that need
00135   /// analysis information to do their job.  If a pass specifies that it uses a
00136   /// particular analysis result to this function, it can then use the
00137   /// getAnalysis<AnalysisType>() function, below.
00138   ///
00139   virtual void getAnalysisUsage(AnalysisUsage &) const {
00140     // By default, no analysis results are used, all are invalidated.
00141   }
00142 
00143   /// releaseMemory() - This member can be implemented by a pass if it wants to
00144   /// be able to release its memory when it is no longer needed.  The default
00145   /// behavior of passes is to hold onto memory for the entire duration of their
00146   /// lifetime (which is the entire compile time).  For pipelined passes, this
00147   /// is not a big deal because that memory gets recycled every time the pass is
00148   /// invoked on another program unit.  For IP passes, it is more important to
00149   /// free memory when it is unused.
00150   ///
00151   /// Optionally implement this function to release pass memory when it is no
00152   /// longer used.
00153   ///
00154   virtual void releaseMemory() {}
00155 
00156   /// verifyAnalysis() - This member can be implemented by a analysis pass to
00157   /// check state of analysis information. 
00158   virtual void verifyAnalysis() const {}
00159 
00160   // dumpPassStructure - Implement the -debug-passes=PassStructure option
00161   virtual void dumpPassStructure(unsigned Offset = 0);
00162 
00163   template<typename AnalysisClass>
00164   static const PassInfo *getClassPassInfo() {
00165     return lookupPassInfo(intptr_t(&AnalysisClass::ID));
00166   }
00167 
00168   // lookupPassInfo - Return the pass info object for the specified pass class,
00169   // or null if it is not known.
00170   static const PassInfo *lookupPassInfo(intptr_t TI);
00171 
00172   /// getAnalysisToUpdate<AnalysisType>() - This function is used by subclasses
00173   /// to get to the analysis information that might be around that needs to be
00174   /// updated.  This is different than getAnalysis in that it can fail (ie the
00175   /// analysis results haven't been computed), so should only be used if you
00176   /// provide the capability to update an analysis that exists.  This method is
00177   /// often used by transformation APIs to update analysis results for a pass
00178   /// automatically as the transform is performed.
00179   ///
00180   template<typename AnalysisType>
00181   AnalysisType *getAnalysisToUpdate() const; // Defined in PassAnalysisSupport.h
00182 
00183   /// mustPreserveAnalysisID - This method serves the same function as
00184   /// getAnalysisToUpdate, but works if you just have an AnalysisID.  This
00185   /// obviously cannot give you a properly typed instance of the class if you
00186   /// don't have the class name available (use getAnalysisToUpdate if you do),
00187   /// but it can tell you if you need to preserve the pass at least.
00188   ///
00189   bool mustPreserveAnalysisID(const PassInfo *AnalysisID) const;
00190 
00191   /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
00192   /// to the analysis information that they claim to use by overriding the
00193   /// getAnalysisUsage function.
00194   ///
00195   template<typename AnalysisType>
00196   AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
00197 
00198   template<typename AnalysisType>
00199   AnalysisType &getAnalysis(Function &F); // Defined in PassanalysisSupport.h
00200 
00201   template<typename AnalysisType>
00202   AnalysisType &getAnalysisID(const PassInfo *PI) const;
00203 
00204   template<typename AnalysisType>
00205   AnalysisType &getAnalysisID(const PassInfo *PI, Function &F);
00206 };
00207 
00208 inline std::ostream &operator<<(std::ostream &OS, const Pass &P) {
00209   P.print(OS, 0); return OS;
00210 }
00211 
00212 //===----------------------------------------------------------------------===//
00213 /// ModulePass class - This class is used to implement unstructured
00214 /// interprocedural optimizations and analyses.  ModulePasses may do anything
00215 /// they want to the program.
00216 ///
00217 class ModulePass : public Pass {
00218 public:
00219   /// runOnModule - Virtual method overriden by subclasses to process the module
00220   /// being operated on.
00221   virtual bool runOnModule(Module &M) = 0;
00222 
00223   virtual void assignPassManager(PMStack &PMS, 
00224                                  PassManagerType T = PMT_ModulePassManager);
00225 
00226   ///  Return what kind of Pass Manager can manage this pass.
00227   virtual PassManagerType getPotentialPassManagerType() const {
00228     return PMT_ModulePassManager;
00229   }
00230 
00231   explicit ModulePass(intptr_t pid) : Pass(pid) {}
00232   explicit ModulePass(const void *pid) : Pass(pid) {}
00233   // Force out-of-line virtual method.
00234   virtual ~ModulePass();
00235 };
00236 
00237 
00238 //===----------------------------------------------------------------------===//
00239 /// ImmutablePass class - This class is used to provide information that does
00240 /// not need to be run.  This is useful for things like target information and
00241 /// "basic" versions of AnalysisGroups.
00242 ///
00243 class ImmutablePass : public ModulePass {
00244 public:
00245   /// initializePass - This method may be overriden by immutable passes to allow
00246   /// them to perform various initialization actions they require.  This is
00247   /// primarily because an ImmutablePass can "require" another ImmutablePass,
00248   /// and if it does, the overloaded version of initializePass may get access to
00249   /// these passes with getAnalysis<>.
00250   ///
00251   virtual void initializePass() {}
00252 
00253   /// ImmutablePasses are never run.
00254   ///
00255   bool runOnModule(Module &) { return false; }
00256 
00257   explicit ImmutablePass(intptr_t pid) : ModulePass(pid) {}
00258   explicit ImmutablePass(const void *pid) 
00259   : ModulePass(pid) {}
00260   
00261   // Force out-of-line virtual method.
00262   virtual ~ImmutablePass();
00263 };
00264 
00265 //===----------------------------------------------------------------------===//
00266 /// FunctionPass class - This class is used to implement most global
00267 /// optimizations.  Optimizations should subclass this class if they meet the
00268 /// following constraints:
00269 ///
00270 ///  1. Optimizations are organized globally, i.e., a function at a time
00271 ///  2. Optimizing a function does not cause the addition or removal of any
00272 ///     functions in the module
00273 ///
00274 class FunctionPass : public Pass {
00275 public:
00276   explicit FunctionPass(intptr_t pid) : Pass(pid) {}
00277   explicit FunctionPass(const void *pid) : Pass(pid) {}
00278 
00279   /// doInitialization - Virtual method overridden by subclasses to do
00280   /// any necessary per-module initialization.
00281   ///
00282   virtual bool doInitialization(Module &) { return false; }
00283 
00284   /// runOnFunction - Virtual method overriden by subclasses to do the
00285   /// per-function processing of the pass.
00286   ///
00287   virtual bool runOnFunction(Function &F) = 0;
00288 
00289   /// doFinalization - Virtual method overriden by subclasses to do any post
00290   /// processing needed after all passes have run.
00291   ///
00292   virtual bool doFinalization(Module &) { return false; }
00293 
00294   /// runOnModule - On a module, we run this pass by initializing,
00295   /// ronOnFunction'ing once for every function in the module, then by
00296   /// finalizing.
00297   ///
00298   virtual bool runOnModule(Module &M);
00299 
00300   /// run - On a function, we simply initialize, run the function, then
00301   /// finalize.
00302   ///
00303   bool run(Function &F);
00304 
00305   virtual void assignPassManager(PMStack &PMS, 
00306                                  PassManagerType T = PMT_FunctionPassManager);
00307 
00308   ///  Return what kind of Pass Manager can manage this pass.
00309   virtual PassManagerType getPotentialPassManagerType() const {
00310     return PMT_FunctionPassManager;
00311   }
00312 };
00313 
00314 
00315 
00316 //===----------------------------------------------------------------------===//
00317 /// BasicBlockPass class - This class is used to implement most local
00318 /// optimizations.  Optimizations should subclass this class if they
00319 /// meet the following constraints:
00320 ///   1. Optimizations are local, operating on either a basic block or
00321 ///      instruction at a time.
00322 ///   2. Optimizations do not modify the CFG of the contained function, or any
00323 ///      other basic block in the function.
00324 ///   3. Optimizations conform to all of the constraints of FunctionPasses.
00325 ///
00326 class BasicBlockPass : public Pass {
00327 public:
00328   explicit BasicBlockPass(intptr_t pid) : Pass(pid) {}
00329   explicit BasicBlockPass(const void *pid) : Pass(pid) {}
00330 
00331   /// doInitialization - Virtual method overridden by subclasses to do
00332   /// any necessary per-module initialization.
00333   ///
00334   virtual bool doInitialization(Module &) { return false; }
00335 
00336   /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
00337   /// to do any necessary per-function initialization.
00338   ///
00339   virtual bool doInitialization(Function &) { return false; }
00340 
00341   /// runOnBasicBlock - Virtual method overriden by subclasses to do the
00342   /// per-basicblock processing of the pass.
00343   ///
00344   virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
00345 
00346   /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
00347   /// do any post processing needed after all passes have run.
00348   ///
00349   virtual bool doFinalization(Function &) { return false; }
00350 
00351   /// doFinalization - Virtual method overriden by subclasses to do any post
00352   /// processing needed after all passes have run.
00353   ///
00354   virtual bool doFinalization(Module &) { return false; }
00355 
00356 
00357   // To run this pass on a function, we simply call runOnBasicBlock once for
00358   // each function.
00359   //
00360   bool runOnFunction(Function &F);
00361 
00362   virtual void assignPassManager(PMStack &PMS, 
00363                                  PassManagerType T = PMT_BasicBlockPassManager);
00364 
00365   ///  Return what kind of Pass Manager can manage this pass.
00366   virtual PassManagerType getPotentialPassManagerType() const {
00367     return PMT_BasicBlockPassManager; 
00368   }
00369 };
00370 
00371 /// If the user specifies the -time-passes argument on an LLVM tool command line
00372 /// then the value of this boolean will be true, otherwise false.
00373 /// @brief This is the storage for the -time-passes option.
00374 extern bool TimePassesIsEnabled;
00375 
00376 } // End llvm namespace
00377 
00378 // Include support files that contain important APIs commonly used by Passes,
00379 // but that we want to separate out to make it easier to read the header files.
00380 //
00381 #include "llvm/PassSupport.h"
00382 #include "llvm/PassAnalysisSupport.h"
00383 
00384 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.