Home | History | Annotate | Download | only in llvm
      1 //===- llvm/PassSupport.h - Pass Support code -------------------*- 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 stuff that is used to define and "use" Passes.  This file
     11 // is automatically #included by Pass.h, so:
     12 //
     13 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
     14 //
     15 // Instead, #include Pass.h.
     16 //
     17 // This file defines Pass registration code and classes used for it.
     18 //
     19 //===----------------------------------------------------------------------===//
     20 
     21 #ifndef LLVM_PASS_SUPPORT_H
     22 #define LLVM_PASS_SUPPORT_H
     23 
     24 #include "Pass.h"
     25 #include "llvm/PassRegistry.h"
     26 #include "llvm/InitializePasses.h"
     27 #include "llvm/Support/Atomic.h"
     28 #include <vector>
     29 
     30 namespace llvm {
     31 
     32 //===---------------------------------------------------------------------------
     33 /// PassInfo class - An instance of this class exists for every pass known by
     34 /// the system, and can be obtained from a live Pass by calling its
     35 /// getPassInfo() method.  These objects are set up by the RegisterPass<>
     36 /// template, defined below.
     37 ///
     38 class PassInfo {
     39 public:
     40   typedef Pass* (*NormalCtor_t)();
     41 
     42 private:
     43   const char      *const PassName;     // Nice name for Pass
     44   const char      *const PassArgument; // Command Line argument to run this pass
     45   const void *PassID;
     46   const bool IsCFGOnlyPass;            // Pass only looks at the CFG.
     47   const bool IsAnalysis;               // True if an analysis pass.
     48   const bool IsAnalysisGroup;          // True if an analysis group.
     49   std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
     50 
     51   NormalCtor_t NormalCtor;
     52 
     53 public:
     54   /// PassInfo ctor - Do not call this directly, this should only be invoked
     55   /// through RegisterPass.
     56   PassInfo(const char *name, const char *arg, const void *pi,
     57            NormalCtor_t normal, bool isCFGOnly, bool is_analysis)
     58     : PassName(name), PassArgument(arg), PassID(pi),
     59       IsCFGOnlyPass(isCFGOnly),
     60       IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal) { }
     61   /// PassInfo ctor - Do not call this directly, this should only be invoked
     62   /// through RegisterPass. This version is for use by analysis groups; it
     63   /// does not auto-register the pass.
     64   PassInfo(const char *name, const void *pi)
     65     : PassName(name), PassArgument(""), PassID(pi),
     66       IsCFGOnlyPass(false),
     67       IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(0) { }
     68 
     69   /// getPassName - Return the friendly name for the pass, never returns null
     70   ///
     71   const char *getPassName() const { return PassName; }
     72 
     73   /// getPassArgument - Return the command line option that may be passed to
     74   /// 'opt' that will cause this pass to be run.  This will return null if there
     75   /// is no argument.
     76   ///
     77   const char *getPassArgument() const { return PassArgument; }
     78 
     79   /// getTypeInfo - Return the id object for the pass...
     80   /// TODO : Rename
     81   const void *getTypeInfo() const { return PassID; }
     82 
     83   /// Return true if this PassID implements the specified ID pointer.
     84   bool isPassID(const void *IDPtr) const {
     85     return PassID == IDPtr;
     86   }
     87 
     88   /// isAnalysisGroup - Return true if this is an analysis group, not a normal
     89   /// pass.
     90   ///
     91   bool isAnalysisGroup() const { return IsAnalysisGroup; }
     92   bool isAnalysis() const { return IsAnalysis; }
     93 
     94   /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
     95   /// function.
     96   bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
     97 
     98   /// getNormalCtor - Return a pointer to a function, that when called, creates
     99   /// an instance of the pass and returns it.  This pointer may be null if there
    100   /// is no default constructor for the pass.
    101   ///
    102   NormalCtor_t getNormalCtor() const {
    103     return NormalCtor;
    104   }
    105   void setNormalCtor(NormalCtor_t Ctor) {
    106     NormalCtor = Ctor;
    107   }
    108 
    109   /// createPass() - Use this method to create an instance of this pass.
    110   Pass *createPass() const;
    111 
    112   /// addInterfaceImplemented - This method is called when this pass is
    113   /// registered as a member of an analysis group with the RegisterAnalysisGroup
    114   /// template.
    115   ///
    116   void addInterfaceImplemented(const PassInfo *ItfPI) {
    117     ItfImpl.push_back(ItfPI);
    118   }
    119 
    120   /// getInterfacesImplemented - Return a list of all of the analysis group
    121   /// interfaces implemented by this pass.
    122   ///
    123   const std::vector<const PassInfo*> &getInterfacesImplemented() const {
    124     return ItfImpl;
    125   }
    126 
    127 private:
    128   void operator=(const PassInfo &); // do not implement
    129   PassInfo(const PassInfo &);       // do not implement
    130 };
    131 
    132 #define CALL_ONCE_INITIALIZATION(function) \
    133   static volatile sys::cas_flag initialized = 0; \
    134   sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
    135   if (old_val == 0) { \
    136     function(Registry); \
    137     sys::MemoryFence(); \
    138     initialized = 2; \
    139   } else { \
    140     sys::cas_flag tmp = initialized; \
    141     sys::MemoryFence(); \
    142     while (tmp != 2) { \
    143       tmp = initialized; \
    144       sys::MemoryFence(); \
    145     } \
    146   }
    147 
    148 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
    149   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
    150     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
    151       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
    152     Registry.registerPass(*PI, true); \
    153     return PI; \
    154   } \
    155   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
    156     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
    157   }
    158 
    159 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
    160   static void* initialize##passName##PassOnce(PassRegistry &Registry) {
    161 
    162 #define INITIALIZE_PASS_DEPENDENCY(depName) \
    163     initialize##depName##Pass(Registry);
    164 #define INITIALIZE_AG_DEPENDENCY(depName) \
    165     initialize##depName##AnalysisGroup(Registry);
    166 
    167 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
    168     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
    169       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
    170     Registry.registerPass(*PI, true); \
    171     return PI; \
    172   } \
    173   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
    174     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
    175   }
    176 
    177 template<typename PassName>
    178 Pass *callDefaultCtor() { return new PassName(); }
    179 
    180 //===---------------------------------------------------------------------------
    181 /// RegisterPass<t> template - This template class is used to notify the system
    182 /// that a Pass is available for use, and registers it into the internal
    183 /// database maintained by the PassManager.  Unless this template is used, opt,
    184 /// for example will not be able to see the pass and attempts to create the pass
    185 /// will fail. This template is used in the follow manner (at global scope, in
    186 /// your .cpp file):
    187 ///
    188 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
    189 ///
    190 /// This statement will cause your pass to be created by calling the default
    191 /// constructor exposed by the pass.  If you have a different constructor that
    192 /// must be called, create a global constructor function (which takes the
    193 /// arguments you need and returns a Pass*) and register your pass like this:
    194 ///
    195 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
    196 ///
    197 template<typename passName>
    198 struct RegisterPass : public PassInfo {
    199 
    200   // Register Pass using default constructor...
    201   RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
    202                bool is_analysis = false)
    203     : PassInfo(Name, PassArg, &passName::ID,
    204                PassInfo::NormalCtor_t(callDefaultCtor<passName>),
    205                CFGOnly, is_analysis) {
    206     PassRegistry::getPassRegistry()->registerPass(*this);
    207   }
    208 };
    209 
    210 
    211 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
    212 /// Analysis groups are used to define an interface (which need not derive from
    213 /// Pass) that is required by passes to do their job.  Analysis Groups differ
    214 /// from normal analyses because any available implementation of the group will
    215 /// be used if it is available.
    216 ///
    217 /// If no analysis implementing the interface is available, a default
    218 /// implementation is created and added.  A pass registers itself as the default
    219 /// implementation by specifying 'true' as the second template argument of this
    220 /// class.
    221 ///
    222 /// In addition to registering itself as an analysis group member, a pass must
    223 /// register itself normally as well.  Passes may be members of multiple groups
    224 /// and may still be "required" specifically by name.
    225 ///
    226 /// The actual interface may also be registered as well (by not specifying the
    227 /// second template argument).  The interface should be registered to associate
    228 /// a nice name with the interface.
    229 ///
    230 class RegisterAGBase : public PassInfo {
    231 public:
    232   RegisterAGBase(const char *Name,
    233                  const void *InterfaceID,
    234                  const void *PassID = 0,
    235                  bool isDefault = false);
    236 };
    237 
    238 template<typename Interface, bool Default = false>
    239 struct RegisterAnalysisGroup : public RegisterAGBase {
    240   explicit RegisterAnalysisGroup(PassInfo &RPB)
    241     : RegisterAGBase(RPB.getPassName(),
    242                      &Interface::ID, RPB.getTypeInfo(),
    243                      Default) {
    244   }
    245 
    246   explicit RegisterAnalysisGroup(const char *Name)
    247     : RegisterAGBase(Name, &Interface::ID) {
    248   }
    249 };
    250 
    251 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
    252   static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
    253     initialize##defaultPass##Pass(Registry); \
    254     PassInfo *AI = new PassInfo(name, & agName :: ID); \
    255     Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
    256     return AI; \
    257   } \
    258   void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
    259     CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
    260   }
    261 
    262 
    263 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
    264   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
    265     if (!def) initialize##agName##AnalysisGroup(Registry); \
    266     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
    267       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
    268     Registry.registerPass(*PI, true); \
    269     \
    270     PassInfo *AI = new PassInfo(name, & agName :: ID); \
    271     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
    272                                    *AI, def, true); \
    273     return AI; \
    274   } \
    275   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
    276     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
    277   }
    278 
    279 
    280 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
    281   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
    282     if (!def) initialize##agName##AnalysisGroup(Registry);
    283 
    284 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
    285     PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
    286       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
    287     Registry.registerPass(*PI, true); \
    288     \
    289     PassInfo *AI = new PassInfo(n, & agName :: ID); \
    290     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
    291                                    *AI, def, true); \
    292     return AI; \
    293   } \
    294   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
    295     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
    296   }
    297 
    298 //===---------------------------------------------------------------------------
    299 /// PassRegistrationListener class - This class is meant to be derived from by
    300 /// clients that are interested in which passes get registered and unregistered
    301 /// at runtime (which can be because of the RegisterPass constructors being run
    302 /// as the program starts up, or may be because a shared object just got
    303 /// loaded).  Deriving from the PassRegistationListener class automatically
    304 /// registers your object to receive callbacks indicating when passes are loaded
    305 /// and removed.
    306 ///
    307 struct PassRegistrationListener {
    308 
    309   /// PassRegistrationListener ctor - Add the current object to the list of
    310   /// PassRegistrationListeners...
    311   PassRegistrationListener();
    312 
    313   /// dtor - Remove object from list of listeners...
    314   ///
    315   virtual ~PassRegistrationListener();
    316 
    317   /// Callback functions - These functions are invoked whenever a pass is loaded
    318   /// or removed from the current executable.
    319   ///
    320   virtual void passRegistered(const PassInfo *) {}
    321 
    322   /// enumeratePasses - Iterate over the registered passes, calling the
    323   /// passEnumerate callback on each PassInfo object.
    324   ///
    325   void enumeratePasses();
    326 
    327   /// passEnumerate - Callback function invoked when someone calls
    328   /// enumeratePasses on this PassRegistrationListener object.
    329   ///
    330   virtual void passEnumerate(const PassInfo *) {}
    331 };
    332 
    333 
    334 } // End llvm namespace
    335 
    336 #endif
    337