LLVM API Documentation

PassSupport.h
Go to the documentation of this file.
00001 //===- llvm/PassSupport.h - Pass Support code -------------------*- 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 stuff that is used to define and "use" Passes.  This file
00011 // is automatically #included by Pass.h, so:
00012 //
00013 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
00014 //
00015 // Instead, #include Pass.h.
00016 //
00017 // This file defines Pass registration code and classes used for it.
00018 //
00019 //===----------------------------------------------------------------------===//
00020 
00021 #ifndef LLVM_PASSSUPPORT_H
00022 #define LLVM_PASSSUPPORT_H
00023 
00024 #include "Pass.h"
00025 #include "llvm/InitializePasses.h"
00026 #include "llvm/PassRegistry.h"
00027 #include "llvm/Support/Atomic.h"
00028 #include "llvm/Support/Valgrind.h"
00029 #include <vector>
00030 
00031 namespace llvm {
00032 
00033 class TargetMachine;
00034 //===---------------------------------------------------------------------------
00035 /// PassInfo class - An instance of this class exists for every pass known by
00036 /// the system, and can be obtained from a live Pass by calling its
00037 /// getPassInfo() method.  These objects are set up by the RegisterPass<>
00038 /// template, defined below.
00039 ///
00040 class PassInfo {
00041 public:
00042   typedef Pass* (*NormalCtor_t)();
00043   typedef Pass *(*TargetMachineCtor_t)(TargetMachine *);
00044 
00045 private:
00046   const char      *const PassName;     // Nice name for Pass
00047   const char      *const PassArgument; // Command Line argument to run this pass
00048   const void *PassID;      
00049   const bool IsCFGOnlyPass;            // Pass only looks at the CFG.
00050   const bool IsAnalysis;               // True if an analysis pass.
00051   const bool IsAnalysisGroup;          // True if an analysis group.
00052   std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
00053 
00054   NormalCtor_t NormalCtor;
00055   TargetMachineCtor_t TargetMachineCtor;
00056 
00057 public:
00058   /// PassInfo ctor - Do not call this directly, this should only be invoked
00059   /// through RegisterPass.
00060   PassInfo(const char *name, const char *arg, const void *pi,
00061            NormalCtor_t normal, bool isCFGOnly, bool is_analysis,
00062            TargetMachineCtor_t machine = nullptr)
00063     : PassName(name), PassArgument(arg), PassID(pi), 
00064       IsCFGOnlyPass(isCFGOnly), 
00065       IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal),
00066       TargetMachineCtor(machine) {}
00067   /// PassInfo ctor - Do not call this directly, this should only be invoked
00068   /// through RegisterPass. This version is for use by analysis groups; it
00069   /// does not auto-register the pass.
00070   PassInfo(const char *name, const void *pi)
00071     : PassName(name), PassArgument(""), PassID(pi), 
00072       IsCFGOnlyPass(false), 
00073       IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(nullptr),
00074       TargetMachineCtor(nullptr) {}
00075 
00076   /// getPassName - Return the friendly name for the pass, never returns null
00077   ///
00078   const char *getPassName() const { return PassName; }
00079 
00080   /// getPassArgument - Return the command line option that may be passed to
00081   /// 'opt' that will cause this pass to be run.  This will return null if there
00082   /// is no argument.
00083   ///
00084   const char *getPassArgument() const { return PassArgument; }
00085 
00086   /// getTypeInfo - Return the id object for the pass...
00087   /// TODO : Rename
00088   const void *getTypeInfo() const { return PassID; }
00089 
00090   /// Return true if this PassID implements the specified ID pointer.
00091   bool isPassID(const void *IDPtr) const {
00092     return PassID == IDPtr;
00093   }
00094   
00095   /// isAnalysisGroup - Return true if this is an analysis group, not a normal
00096   /// pass.
00097   ///
00098   bool isAnalysisGroup() const { return IsAnalysisGroup; }
00099   bool isAnalysis() const { return IsAnalysis; }
00100 
00101   /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
00102   /// function.
00103   bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
00104   
00105   /// getNormalCtor - Return a pointer to a function, that when called, creates
00106   /// an instance of the pass and returns it.  This pointer may be null if there
00107   /// is no default constructor for the pass.
00108   ///
00109   NormalCtor_t getNormalCtor() const {
00110     return NormalCtor;
00111   }
00112   void setNormalCtor(NormalCtor_t Ctor) {
00113     NormalCtor = Ctor;
00114   }
00115 
00116   /// getTargetMachineCtor - Return a pointer to a function, that when called
00117   /// with a TargetMachine, creates an instance of the pass and returns it.
00118   /// This pointer may be null if there is no constructor with a TargetMachine
00119   /// for the pass.
00120   ///
00121   TargetMachineCtor_t getTargetMachineCtor() const { return TargetMachineCtor; }
00122   void setTargetMachineCtor(TargetMachineCtor_t Ctor) {
00123     TargetMachineCtor = Ctor;
00124   }
00125 
00126   /// createPass() - Use this method to create an instance of this pass.
00127   Pass *createPass() const;
00128 
00129   /// addInterfaceImplemented - This method is called when this pass is
00130   /// registered as a member of an analysis group with the RegisterAnalysisGroup
00131   /// template.
00132   ///
00133   void addInterfaceImplemented(const PassInfo *ItfPI) {
00134     ItfImpl.push_back(ItfPI);
00135   }
00136 
00137   /// getInterfacesImplemented - Return a list of all of the analysis group
00138   /// interfaces implemented by this pass.
00139   ///
00140   const std::vector<const PassInfo*> &getInterfacesImplemented() const {
00141     return ItfImpl;
00142   }
00143 
00144 private:
00145   void operator=(const PassInfo &) LLVM_DELETED_FUNCTION;
00146   PassInfo(const PassInfo &) LLVM_DELETED_FUNCTION;
00147 };
00148 
00149 #define CALL_ONCE_INITIALIZATION(function) \
00150   static volatile sys::cas_flag initialized = 0; \
00151   sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
00152   if (old_val == 0) { \
00153     function(Registry); \
00154     sys::MemoryFence(); \
00155     TsanIgnoreWritesBegin(); \
00156     TsanHappensBefore(&initialized); \
00157     initialized = 2; \
00158     TsanIgnoreWritesEnd(); \
00159   } else { \
00160     sys::cas_flag tmp = initialized; \
00161     sys::MemoryFence(); \
00162     while (tmp != 2) { \
00163       tmp = initialized; \
00164       sys::MemoryFence(); \
00165     } \
00166   } \
00167   TsanHappensAfter(&initialized);
00168 
00169 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
00170   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
00171     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
00172       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
00173     Registry.registerPass(*PI, true); \
00174     return PI; \
00175   } \
00176   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
00177     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
00178   }
00179 
00180 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
00181   static void* initialize##passName##PassOnce(PassRegistry &Registry) {
00182 
00183 #define INITIALIZE_PASS_DEPENDENCY(depName) \
00184     initialize##depName##Pass(Registry);
00185 #define INITIALIZE_AG_DEPENDENCY(depName) \
00186     initialize##depName##AnalysisGroup(Registry);
00187 
00188 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
00189     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
00190       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
00191     Registry.registerPass(*PI, true); \
00192     return PI; \
00193   } \
00194   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
00195     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
00196   }
00197 
00198 template<typename PassName>
00199 Pass *callDefaultCtor() { return new PassName(); }
00200 
00201 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
00202   return new PassName(TM);
00203 }
00204 
00205 //===---------------------------------------------------------------------------
00206 /// RegisterPass<t> template - This template class is used to notify the system
00207 /// that a Pass is available for use, and registers it into the internal
00208 /// database maintained by the PassManager.  Unless this template is used, opt,
00209 /// for example will not be able to see the pass and attempts to create the pass
00210 /// will fail. This template is used in the follow manner (at global scope, in
00211 /// your .cpp file):
00212 ///
00213 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
00214 ///
00215 /// This statement will cause your pass to be created by calling the default
00216 /// constructor exposed by the pass.  If you have a different constructor that
00217 /// must be called, create a global constructor function (which takes the
00218 /// arguments you need and returns a Pass*) and register your pass like this:
00219 ///
00220 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
00221 ///
00222 template<typename passName>
00223 struct RegisterPass : public PassInfo {
00224 
00225   // Register Pass using default constructor...
00226   RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
00227                bool is_analysis = false)
00228     : PassInfo(Name, PassArg, &passName::ID,
00229                PassInfo::NormalCtor_t(callDefaultCtor<passName>),
00230                CFGOnly, is_analysis) {
00231     PassRegistry::getPassRegistry()->registerPass(*this);
00232   }
00233 };
00234 
00235 
00236 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
00237 /// Analysis groups are used to define an interface (which need not derive from
00238 /// Pass) that is required by passes to do their job.  Analysis Groups differ
00239 /// from normal analyses because any available implementation of the group will
00240 /// be used if it is available.
00241 ///
00242 /// If no analysis implementing the interface is available, a default
00243 /// implementation is created and added.  A pass registers itself as the default
00244 /// implementation by specifying 'true' as the second template argument of this
00245 /// class.
00246 ///
00247 /// In addition to registering itself as an analysis group member, a pass must
00248 /// register itself normally as well.  Passes may be members of multiple groups
00249 /// and may still be "required" specifically by name.
00250 ///
00251 /// The actual interface may also be registered as well (by not specifying the
00252 /// second template argument).  The interface should be registered to associate
00253 /// a nice name with the interface.
00254 ///
00255 class RegisterAGBase : public PassInfo {
00256 public:
00257   RegisterAGBase(const char *Name,
00258                  const void *InterfaceID,
00259                  const void *PassID = nullptr,
00260                  bool isDefault = false);
00261 };
00262 
00263 template<typename Interface, bool Default = false>
00264 struct RegisterAnalysisGroup : public RegisterAGBase {
00265   explicit RegisterAnalysisGroup(PassInfo &RPB)
00266     : RegisterAGBase(RPB.getPassName(),
00267                      &Interface::ID, RPB.getTypeInfo(),
00268                      Default) {
00269   }
00270 
00271   explicit RegisterAnalysisGroup(const char *Name)
00272     : RegisterAGBase(Name, &Interface::ID) {
00273   }
00274 };
00275 
00276 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
00277   static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
00278     initialize##defaultPass##Pass(Registry); \
00279     PassInfo *AI = new PassInfo(name, & agName :: ID); \
00280     Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
00281     return AI; \
00282   } \
00283   void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
00284     CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
00285   }
00286 
00287 
00288 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
00289   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
00290     if (!def) initialize##agName##AnalysisGroup(Registry); \
00291     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
00292       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
00293     Registry.registerPass(*PI, true); \
00294     \
00295     PassInfo *AI = new PassInfo(name, & agName :: ID); \
00296     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
00297                                    *AI, def, true); \
00298     return AI; \
00299   } \
00300   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
00301     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
00302   }
00303 
00304 
00305 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
00306   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
00307     if (!def) initialize##agName##AnalysisGroup(Registry);
00308 
00309 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
00310     PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
00311       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
00312     Registry.registerPass(*PI, true); \
00313     \
00314     PassInfo *AI = new PassInfo(n, & agName :: ID); \
00315     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
00316                                    *AI, def, true); \
00317     return AI; \
00318   } \
00319   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
00320     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
00321   }
00322 
00323 //===---------------------------------------------------------------------------
00324 /// PassRegistrationListener class - This class is meant to be derived from by
00325 /// clients that are interested in which passes get registered and unregistered
00326 /// at runtime (which can be because of the RegisterPass constructors being run
00327 /// as the program starts up, or may be because a shared object just got
00328 /// loaded).  Deriving from the PassRegistrationListener class automatically
00329 /// registers your object to receive callbacks indicating when passes are loaded
00330 /// and removed.
00331 ///
00332 struct PassRegistrationListener {
00333 
00334   /// PassRegistrationListener ctor - Add the current object to the list of
00335   /// PassRegistrationListeners...
00336   PassRegistrationListener();
00337 
00338   /// dtor - Remove object from list of listeners...
00339   ///
00340   virtual ~PassRegistrationListener();
00341 
00342   /// Callback functions - These functions are invoked whenever a pass is loaded
00343   /// or removed from the current executable.
00344   ///
00345   virtual void passRegistered(const PassInfo *) {}
00346 
00347   /// enumeratePasses - Iterate over the registered passes, calling the
00348   /// passEnumerate callback on each PassInfo object.
00349   ///
00350   void enumeratePasses();
00351 
00352   /// passEnumerate - Callback function invoked when someone calls
00353   /// enumeratePasses on this PassRegistrationListener object.
00354   ///
00355   virtual void passEnumerate(const PassInfo *) {}
00356 };
00357 
00358 
00359 } // End llvm namespace
00360 
00361 #endif