Home | History | Annotate | Download | only in llvm
      1 //===- llvm/Pass.h - Base class for Passes ----------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines a base class that indicates that a specified class is a
     11 // transformation pass implementation.
     12 //
     13 // Passes are designed this way so that it is possible to run passes in a cache
     14 // and organizationally optimal order without having to specify it at the front
     15 // end.  This allows arbitrary passes to be strung together and have them
     16 // executed as efficiently as possible.
     17 //
     18 // Passes should extend one of the classes below, depending on the guarantees
     19 // that it can make about what will be modified as it is run.  For example, most
     20 // global optimizations should derive from FunctionPass, because they do not add
     21 // or delete functions, they operate on the internals of the function.
     22 //
     23 // Note that this file #includes PassSupport.h and PassAnalysisSupport.h (at the
     24 // bottom), so the APIs exposed by these files are also automatically available
     25 // to all users of this file.
     26 //
     27 //===----------------------------------------------------------------------===//
     28 
     29 #ifndef LLVM_PASS_H
     30 #define LLVM_PASS_H
     31 
     32 #include <string>
     33 
     34 namespace llvm {
     35 
     36 class BasicBlock;
     37 class Function;
     38 class Module;
     39 class AnalysisUsage;
     40 class PassInfo;
     41 class ImmutablePass;
     42 class PMStack;
     43 class AnalysisResolver;
     44 class PMDataManager;
     45 class raw_ostream;
     46 class StringRef;
     47 
     48 // AnalysisID - Use the PassInfo to identify a pass...
     49 typedef const void* AnalysisID;
     50 
     51 /// Different types of internal pass managers. External pass managers
     52 /// (PassManager and FunctionPassManager) are not represented here.
     53 /// Ordering of pass manager types is important here.
     54 enum PassManagerType {
     55   PMT_Unknown = 0,
     56   PMT_ModulePassManager = 1, ///< MPPassManager
     57   PMT_CallGraphPassManager,  ///< CGPassManager
     58   PMT_FunctionPassManager,   ///< FPPassManager
     59   PMT_LoopPassManager,       ///< LPPassManager
     60   PMT_RegionPassManager,     ///< RGPassManager
     61   PMT_BasicBlockPassManager, ///< BBPassManager
     62   PMT_Last
     63 };
     64 
     65 // Different types of passes.
     66 enum PassKind {
     67   PT_BasicBlock,
     68   PT_Region,
     69   PT_Loop,
     70   PT_Function,
     71   PT_CallGraphSCC,
     72   PT_Module,
     73   PT_PassManager
     74 };
     75 
     76 //===----------------------------------------------------------------------===//
     77 /// Pass interface - Implemented by all 'passes'.  Subclass this if you are an
     78 /// interprocedural optimization or you do not fit into any of the more
     79 /// constrained passes described below.
     80 ///
     81 class Pass {
     82   AnalysisResolver *Resolver;  // Used to resolve analysis
     83   const void *PassID;
     84   PassKind Kind;
     85   void operator=(const Pass&);  // DO NOT IMPLEMENT
     86   Pass(const Pass &);           // DO NOT IMPLEMENT
     87 
     88 public:
     89   explicit Pass(PassKind K, char &pid);
     90   virtual ~Pass();
     91 
     92 
     93   PassKind getPassKind() const { return Kind; }
     94 
     95   /// getPassName - Return a nice clean name for a pass.  This usually
     96   /// implemented in terms of the name that is registered by one of the
     97   /// Registration templates, but can be overloaded directly.
     98   ///
     99   virtual const char *getPassName() const;
    100 
    101   /// getPassID - Return the PassID number that corresponds to this pass.
    102   virtual AnalysisID getPassID() const {
    103     return PassID;
    104   }
    105 
    106   /// print - Print out the internal state of the pass.  This is called by
    107   /// Analyze to print out the contents of an analysis.  Otherwise it is not
    108   /// necessary to implement this method.  Beware that the module pointer MAY be
    109   /// null.  This automatically forwards to a virtual function that does not
    110   /// provide the Module* in case the analysis doesn't need it it can just be
    111   /// ignored.
    112   ///
    113   virtual void print(raw_ostream &O, const Module *M) const;
    114   void dump() const; // dump - Print to stderr.
    115 
    116   /// createPrinterPass - Get a Pass appropriate to print the IR this
    117   /// pass operates on (Module, Function or MachineFunction).
    118   virtual Pass *createPrinterPass(raw_ostream &O,
    119                                   const std::string &Banner) const = 0;
    120 
    121   /// Each pass is responsible for assigning a pass manager to itself.
    122   /// PMS is the stack of available pass manager.
    123   virtual void assignPassManager(PMStack &,
    124                                  PassManagerType) {}
    125   /// Check if available pass managers are suitable for this pass or not.
    126   virtual void preparePassManager(PMStack &);
    127 
    128   ///  Return what kind of Pass Manager can manage this pass.
    129   virtual PassManagerType getPotentialPassManagerType() const;
    130 
    131   // Access AnalysisResolver
    132   void setResolver(AnalysisResolver *AR);
    133   AnalysisResolver *getResolver() const { return Resolver; }
    134 
    135   /// getAnalysisUsage - This function should be overriden by passes that need
    136   /// analysis information to do their job.  If a pass specifies that it uses a
    137   /// particular analysis result to this function, it can then use the
    138   /// getAnalysis<AnalysisType>() function, below.
    139   ///
    140   virtual void getAnalysisUsage(AnalysisUsage &) const;
    141 
    142   /// releaseMemory() - This member can be implemented by a pass if it wants to
    143   /// be able to release its memory when it is no longer needed.  The default
    144   /// behavior of passes is to hold onto memory for the entire duration of their
    145   /// lifetime (which is the entire compile time).  For pipelined passes, this
    146   /// is not a big deal because that memory gets recycled every time the pass is
    147   /// invoked on another program unit.  For IP passes, it is more important to
    148   /// free memory when it is unused.
    149   ///
    150   /// Optionally implement this function to release pass memory when it is no
    151   /// longer used.
    152   ///
    153   virtual void releaseMemory();
    154 
    155   /// getAdjustedAnalysisPointer - This method is used when a pass implements
    156   /// an analysis interface through multiple inheritance.  If needed, it should
    157   /// override this to adjust the this pointer as needed for the specified pass
    158   /// info.
    159   virtual void *getAdjustedAnalysisPointer(AnalysisID ID);
    160   virtual ImmutablePass *getAsImmutablePass();
    161   virtual PMDataManager *getAsPMDataManager();
    162 
    163   /// verifyAnalysis() - This member can be implemented by a analysis pass to
    164   /// check state of analysis information.
    165   virtual void verifyAnalysis() const;
    166 
    167   // dumpPassStructure - Implement the -debug-passes=PassStructure option
    168   virtual void dumpPassStructure(unsigned Offset = 0);
    169 
    170   // lookupPassInfo - Return the pass info object for the specified pass class,
    171   // or null if it is not known.
    172   static const PassInfo *lookupPassInfo(const void *TI);
    173 
    174   // lookupPassInfo - Return the pass info object for the pass with the given
    175   // argument string, or null if it is not known.
    176   static const PassInfo *lookupPassInfo(StringRef Arg);
    177 
    178   /// getAnalysisIfAvailable<AnalysisType>() - Subclasses use this function to
    179   /// get analysis information that might be around, for example to update it.
    180   /// This is different than getAnalysis in that it can fail (if the analysis
    181   /// results haven't been computed), so should only be used if you can handle
    182   /// the case when the analysis is not available.  This method is often used by
    183   /// transformation APIs to update analysis results for a pass automatically as
    184   /// the transform is performed.
    185   ///
    186   template<typename AnalysisType> AnalysisType *
    187     getAnalysisIfAvailable() const; // Defined in PassAnalysisSupport.h
    188 
    189   /// mustPreserveAnalysisID - This method serves the same function as
    190   /// getAnalysisIfAvailable, but works if you just have an AnalysisID.  This
    191   /// obviously cannot give you a properly typed instance of the class if you
    192   /// don't have the class name available (use getAnalysisIfAvailable if you
    193   /// do), but it can tell you if you need to preserve the pass at least.
    194   ///
    195   bool mustPreserveAnalysisID(char &AID) const;
    196 
    197   /// getAnalysis<AnalysisType>() - This function is used by subclasses to get
    198   /// to the analysis information that they claim to use by overriding the
    199   /// getAnalysisUsage function.
    200   ///
    201   template<typename AnalysisType>
    202   AnalysisType &getAnalysis() const; // Defined in PassAnalysisSupport.h
    203 
    204   template<typename AnalysisType>
    205   AnalysisType &getAnalysis(Function &F); // Defined in PassAnalysisSupport.h
    206 
    207   template<typename AnalysisType>
    208   AnalysisType &getAnalysisID(AnalysisID PI) const;
    209 
    210   template<typename AnalysisType>
    211   AnalysisType &getAnalysisID(AnalysisID PI, Function &F);
    212 };
    213 
    214 
    215 //===----------------------------------------------------------------------===//
    216 /// ModulePass class - This class is used to implement unstructured
    217 /// interprocedural optimizations and analyses.  ModulePasses may do anything
    218 /// they want to the program.
    219 ///
    220 class ModulePass : public Pass {
    221 public:
    222   /// createPrinterPass - Get a module printer pass.
    223   Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
    224 
    225   /// runOnModule - Virtual method overriden by subclasses to process the module
    226   /// being operated on.
    227   virtual bool runOnModule(Module &M) = 0;
    228 
    229   virtual void assignPassManager(PMStack &PMS,
    230                                  PassManagerType T);
    231 
    232   ///  Return what kind of Pass Manager can manage this pass.
    233   virtual PassManagerType getPotentialPassManagerType() const;
    234 
    235   explicit ModulePass(char &pid) : Pass(PT_Module, pid) {}
    236   // Force out-of-line virtual method.
    237   virtual ~ModulePass();
    238 };
    239 
    240 
    241 //===----------------------------------------------------------------------===//
    242 /// ImmutablePass class - This class is used to provide information that does
    243 /// not need to be run.  This is useful for things like target information and
    244 /// "basic" versions of AnalysisGroups.
    245 ///
    246 class ImmutablePass : public ModulePass {
    247 public:
    248   /// initializePass - This method may be overriden by immutable passes to allow
    249   /// them to perform various initialization actions they require.  This is
    250   /// primarily because an ImmutablePass can "require" another ImmutablePass,
    251   /// and if it does, the overloaded version of initializePass may get access to
    252   /// these passes with getAnalysis<>.
    253   ///
    254   virtual void initializePass();
    255 
    256   virtual ImmutablePass *getAsImmutablePass() { return this; }
    257 
    258   /// ImmutablePasses are never run.
    259   ///
    260   bool runOnModule(Module &) { return false; }
    261 
    262   explicit ImmutablePass(char &pid)
    263   : ModulePass(pid) {}
    264 
    265   // Force out-of-line virtual method.
    266   virtual ~ImmutablePass();
    267 };
    268 
    269 //===----------------------------------------------------------------------===//
    270 /// FunctionPass class - This class is used to implement most global
    271 /// optimizations.  Optimizations should subclass this class if they meet the
    272 /// following constraints:
    273 ///
    274 ///  1. Optimizations are organized globally, i.e., a function at a time
    275 ///  2. Optimizing a function does not cause the addition or removal of any
    276 ///     functions in the module
    277 ///
    278 class FunctionPass : public Pass {
    279 public:
    280   explicit FunctionPass(char &pid) : Pass(PT_Function, pid) {}
    281 
    282   /// createPrinterPass - Get a function printer pass.
    283   Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
    284 
    285   /// doInitialization - Virtual method overridden by subclasses to do
    286   /// any necessary per-module initialization.
    287   ///
    288   virtual bool doInitialization(Module &);
    289 
    290   /// runOnFunction - Virtual method overriden by subclasses to do the
    291   /// per-function processing of the pass.
    292   ///
    293   virtual bool runOnFunction(Function &F) = 0;
    294 
    295   /// doFinalization - Virtual method overriden by subclasses to do any post
    296   /// processing needed after all passes have run.
    297   ///
    298   virtual bool doFinalization(Module &);
    299 
    300   virtual void assignPassManager(PMStack &PMS,
    301                                  PassManagerType T);
    302 
    303   ///  Return what kind of Pass Manager can manage this pass.
    304   virtual PassManagerType getPotentialPassManagerType() const;
    305 };
    306 
    307 
    308 
    309 //===----------------------------------------------------------------------===//
    310 /// BasicBlockPass class - This class is used to implement most local
    311 /// optimizations.  Optimizations should subclass this class if they
    312 /// meet the following constraints:
    313 ///   1. Optimizations are local, operating on either a basic block or
    314 ///      instruction at a time.
    315 ///   2. Optimizations do not modify the CFG of the contained function, or any
    316 ///      other basic block in the function.
    317 ///   3. Optimizations conform to all of the constraints of FunctionPasses.
    318 ///
    319 class BasicBlockPass : public Pass {
    320 public:
    321   explicit BasicBlockPass(char &pid) : Pass(PT_BasicBlock, pid) {}
    322 
    323   /// createPrinterPass - Get a basic block printer pass.
    324   Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const;
    325 
    326   /// doInitialization - Virtual method overridden by subclasses to do
    327   /// any necessary per-module initialization.
    328   ///
    329   virtual bool doInitialization(Module &);
    330 
    331   /// doInitialization - Virtual method overridden by BasicBlockPass subclasses
    332   /// to do any necessary per-function initialization.
    333   ///
    334   virtual bool doInitialization(Function &);
    335 
    336   /// runOnBasicBlock - Virtual method overriden by subclasses to do the
    337   /// per-basicblock processing of the pass.
    338   ///
    339   virtual bool runOnBasicBlock(BasicBlock &BB) = 0;
    340 
    341   /// doFinalization - Virtual method overriden by BasicBlockPass subclasses to
    342   /// do any post processing needed after all passes have run.
    343   ///
    344   virtual bool doFinalization(Function &);
    345 
    346   /// doFinalization - Virtual method overriden by subclasses to do any post
    347   /// processing needed after all passes have run.
    348   ///
    349   virtual bool doFinalization(Module &);
    350 
    351   virtual void assignPassManager(PMStack &PMS,
    352                                  PassManagerType T);
    353 
    354   ///  Return what kind of Pass Manager can manage this pass.
    355   virtual PassManagerType getPotentialPassManagerType() const;
    356 };
    357 
    358 /// If the user specifies the -time-passes argument on an LLVM tool command line
    359 /// then the value of this boolean will be true, otherwise false.
    360 /// @brief This is the storage for the -time-passes option.
    361 extern bool TimePassesIsEnabled;
    362 
    363 } // End llvm namespace
    364 
    365 // Include support files that contain important APIs commonly used by Passes,
    366 // but that we want to separate out to make it easier to read the header files.
    367 //
    368 #include "llvm/PassSupport.h"
    369 #include "llvm/PassAnalysisSupport.h"
    370 
    371 #endif
    372