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/PassInfo.h"
00027 #include "llvm/PassRegistry.h"
00028 #include "llvm/Support/Atomic.h"
00029 #include "llvm/Support/Valgrind.h"
00030 #include <vector>
00031 
00032 namespace llvm {
00033 
00034 class TargetMachine;
00035 
00036 #define CALL_ONCE_INITIALIZATION(function) \
00037   static volatile sys::cas_flag initialized = 0; \
00038   sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \
00039   if (old_val == 0) { \
00040     function(Registry); \
00041     sys::MemoryFence(); \
00042     TsanIgnoreWritesBegin(); \
00043     TsanHappensBefore(&initialized); \
00044     initialized = 2; \
00045     TsanIgnoreWritesEnd(); \
00046   } else { \
00047     sys::cas_flag tmp = initialized; \
00048     sys::MemoryFence(); \
00049     while (tmp != 2) { \
00050       tmp = initialized; \
00051       sys::MemoryFence(); \
00052     } \
00053   } \
00054   TsanHappensAfter(&initialized);
00055 
00056 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis) \
00057   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
00058     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
00059       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
00060     Registry.registerPass(*PI, true); \
00061     return PI; \
00062   } \
00063   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
00064     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
00065   }
00066 
00067 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis) \
00068   static void* initialize##passName##PassOnce(PassRegistry &Registry) {
00069 
00070 #define INITIALIZE_PASS_DEPENDENCY(depName) \
00071     initialize##depName##Pass(Registry);
00072 #define INITIALIZE_AG_DEPENDENCY(depName) \
00073     initialize##depName##AnalysisGroup(Registry);
00074 
00075 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis) \
00076     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
00077       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
00078     Registry.registerPass(*PI, true); \
00079     return PI; \
00080   } \
00081   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
00082     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
00083   }
00084 
00085 #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis) \
00086   INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
00087   PassName::registerOptions(); \
00088   INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
00089 
00090 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
00091   INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
00092   PassName::registerOptions(); \
00093 
00094 template<typename PassName>
00095 Pass *callDefaultCtor() { return new PassName(); }
00096 
00097 template <typename PassName> Pass *callTargetMachineCtor(TargetMachine *TM) {
00098   return new PassName(TM);
00099 }
00100 
00101 //===---------------------------------------------------------------------------
00102 /// RegisterPass<t> template - This template class is used to notify the system
00103 /// that a Pass is available for use, and registers it into the internal
00104 /// database maintained by the PassManager.  Unless this template is used, opt,
00105 /// for example will not be able to see the pass and attempts to create the pass
00106 /// will fail. This template is used in the follow manner (at global scope, in
00107 /// your .cpp file):
00108 ///
00109 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
00110 ///
00111 /// This statement will cause your pass to be created by calling the default
00112 /// constructor exposed by the pass.  If you have a different constructor that
00113 /// must be called, create a global constructor function (which takes the
00114 /// arguments you need and returns a Pass*) and register your pass like this:
00115 ///
00116 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
00117 ///
00118 template<typename passName>
00119 struct RegisterPass : public PassInfo {
00120 
00121   // Register Pass using default constructor...
00122   RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
00123                bool is_analysis = false)
00124     : PassInfo(Name, PassArg, &passName::ID,
00125                PassInfo::NormalCtor_t(callDefaultCtor<passName>),
00126                CFGOnly, is_analysis) {
00127     PassRegistry::getPassRegistry()->registerPass(*this);
00128   }
00129 };
00130 
00131 
00132 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
00133 /// Analysis groups are used to define an interface (which need not derive from
00134 /// Pass) that is required by passes to do their job.  Analysis Groups differ
00135 /// from normal analyses because any available implementation of the group will
00136 /// be used if it is available.
00137 ///
00138 /// If no analysis implementing the interface is available, a default
00139 /// implementation is created and added.  A pass registers itself as the default
00140 /// implementation by specifying 'true' as the second template argument of this
00141 /// class.
00142 ///
00143 /// In addition to registering itself as an analysis group member, a pass must
00144 /// register itself normally as well.  Passes may be members of multiple groups
00145 /// and may still be "required" specifically by name.
00146 ///
00147 /// The actual interface may also be registered as well (by not specifying the
00148 /// second template argument).  The interface should be registered to associate
00149 /// a nice name with the interface.
00150 ///
00151 class RegisterAGBase : public PassInfo {
00152 public:
00153   RegisterAGBase(const char *Name,
00154                  const void *InterfaceID,
00155                  const void *PassID = nullptr,
00156                  bool isDefault = false);
00157 };
00158 
00159 template<typename Interface, bool Default = false>
00160 struct RegisterAnalysisGroup : public RegisterAGBase {
00161   explicit RegisterAnalysisGroup(PassInfo &RPB)
00162     : RegisterAGBase(RPB.getPassName(),
00163                      &Interface::ID, RPB.getTypeInfo(),
00164                      Default) {
00165   }
00166 
00167   explicit RegisterAnalysisGroup(const char *Name)
00168     : RegisterAGBase(Name, &Interface::ID) {
00169   }
00170 };
00171 
00172 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass) \
00173   static void* initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
00174     initialize##defaultPass##Pass(Registry); \
00175     PassInfo *AI = new PassInfo(name, & agName :: ID); \
00176     Registry.registerAnalysisGroup(& agName ::ID, 0, *AI, false, true); \
00177     return AI; \
00178   } \
00179   void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) { \
00180     CALL_ONCE_INITIALIZATION(initialize##agName##AnalysisGroupOnce) \
00181   }
00182 
00183 
00184 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def) \
00185   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
00186     if (!def) initialize##agName##AnalysisGroup(Registry); \
00187     PassInfo *PI = new PassInfo(name, arg, & passName ::ID, \
00188       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
00189     Registry.registerPass(*PI, true); \
00190     \
00191     PassInfo *AI = new PassInfo(name, & agName :: ID); \
00192     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
00193                                    *AI, def, true); \
00194     return AI; \
00195   } \
00196   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
00197     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
00198   }
00199 
00200 
00201 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
00202   static void* initialize##passName##PassOnce(PassRegistry &Registry) { \
00203     if (!def) initialize##agName##AnalysisGroup(Registry);
00204 
00205 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def) \
00206     PassInfo *PI = new PassInfo(n, arg, & passName ::ID, \
00207       PassInfo::NormalCtor_t(callDefaultCtor< passName >), cfg, analysis); \
00208     Registry.registerPass(*PI, true); \
00209     \
00210     PassInfo *AI = new PassInfo(n, & agName :: ID); \
00211     Registry.registerAnalysisGroup(& agName ::ID, & passName ::ID, \
00212                                    *AI, def, true); \
00213     return AI; \
00214   } \
00215   void llvm::initialize##passName##Pass(PassRegistry &Registry) { \
00216     CALL_ONCE_INITIALIZATION(initialize##passName##PassOnce) \
00217   }
00218 
00219 //===---------------------------------------------------------------------------
00220 /// PassRegistrationListener class - This class is meant to be derived from by
00221 /// clients that are interested in which passes get registered and unregistered
00222 /// at runtime (which can be because of the RegisterPass constructors being run
00223 /// as the program starts up, or may be because a shared object just got
00224 /// loaded).
00225 ///
00226 struct PassRegistrationListener {
00227 
00228   PassRegistrationListener() {}
00229   virtual ~PassRegistrationListener() {}
00230 
00231   /// Callback functions - These functions are invoked whenever a pass is loaded
00232   /// or removed from the current executable.
00233   ///
00234   virtual void passRegistered(const PassInfo *) {}
00235 
00236   /// enumeratePasses - Iterate over the registered passes, calling the
00237   /// passEnumerate callback on each PassInfo object.
00238   ///
00239   void enumeratePasses();
00240 
00241   /// passEnumerate - Callback function invoked when someone calls
00242   /// enumeratePasses on this PassRegistrationListener object.
00243   ///
00244   virtual void passEnumerate(const PassInfo *) {}
00245 };
00246 
00247 
00248 } // End llvm namespace
00249 
00250 #endif