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