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_PASSSUPPORT_H
     22 #define LLVM_PASSSUPPORT_H
     23 
     24 #include "llvm/ADT/StringRef.h"
     25 #include "llvm/PassInfo.h"
     26 #include "llvm/PassRegistry.h"
     27 #include "llvm/Support/Threading.h"
     28 #include <functional>
     29 
     30 namespace llvm {
     31 
     32 class Pass;
     33 
     34 #define INITIALIZE_PASS(passName, arg, name, cfg, analysis)                    \
     35   static void *initialize##passName##PassOnce(PassRegistry &Registry) {        \
     36     PassInfo *PI = new PassInfo(                                               \
     37         name, arg, &passName::ID,                                              \
     38         PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis);     \
     39     Registry.registerPass(*PI, true);                                          \
     40     return PI;                                                                 \
     41   }                                                                            \
     42   static llvm::once_flag Initialize##passName##PassFlag;                       \
     43   void llvm::initialize##passName##Pass(PassRegistry &Registry) {              \
     44     llvm::call_once(Initialize##passName##PassFlag,                            \
     45                     initialize##passName##PassOnce, std::ref(Registry));       \
     46   }
     47 
     48 #define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)              \
     49   static void *initialize##passName##PassOnce(PassRegistry &Registry) {
     50 
     51 #define INITIALIZE_PASS_DEPENDENCY(depName) initialize##depName##Pass(Registry);
     52 #define INITIALIZE_AG_DEPENDENCY(depName)                                      \
     53   initialize##depName##AnalysisGroup(Registry);
     54 
     55 #define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)                \
     56   PassInfo *PI = new PassInfo(                                                 \
     57       name, arg, &passName::ID,                                                \
     58       PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis);       \
     59   Registry.registerPass(*PI, true);                                            \
     60   return PI;                                                                   \
     61   }                                                                            \
     62   static llvm::once_flag Initialize##passName##PassFlag;                       \
     63   void llvm::initialize##passName##Pass(PassRegistry &Registry) {              \
     64     llvm::call_once(Initialize##passName##PassFlag,                            \
     65                     initialize##passName##PassOnce, std::ref(Registry));       \
     66   }
     67 
     68 #define INITIALIZE_PASS_WITH_OPTIONS(PassName, Arg, Name, Cfg, Analysis)       \
     69   INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis)                    \
     70   PassName::registerOptions();                                                 \
     71   INITIALIZE_PASS_END(PassName, Arg, Name, Cfg, Analysis)
     72 
     73 #define INITIALIZE_PASS_WITH_OPTIONS_BEGIN(PassName, Arg, Name, Cfg, Analysis) \
     74   INITIALIZE_PASS_BEGIN(PassName, Arg, Name, Cfg, Analysis)                    \
     75   PassName::registerOptions();
     76 
     77 template <typename PassName> Pass *callDefaultCtor() { return new PassName(); }
     78 
     79 //===---------------------------------------------------------------------------
     80 /// RegisterPass<t> template - This template class is used to notify the system
     81 /// that a Pass is available for use, and registers it into the internal
     82 /// database maintained by the PassManager.  Unless this template is used, opt,
     83 /// for example will not be able to see the pass and attempts to create the pass
     84 /// will fail. This template is used in the follow manner (at global scope, in
     85 /// your .cpp file):
     86 ///
     87 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
     88 ///
     89 /// This statement will cause your pass to be created by calling the default
     90 /// constructor exposed by the pass.
     91 template <typename passName> struct RegisterPass : public PassInfo {
     92   // Register Pass using default constructor...
     93   RegisterPass(StringRef PassArg, StringRef Name, bool CFGOnly = false,
     94                bool is_analysis = false)
     95       : PassInfo(Name, PassArg, &passName::ID,
     96                  PassInfo::NormalCtor_t(callDefaultCtor<passName>), CFGOnly,
     97                  is_analysis) {
     98     PassRegistry::getPassRegistry()->registerPass(*this);
     99   }
    100 };
    101 
    102 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
    103 /// Analysis groups are used to define an interface (which need not derive from
    104 /// Pass) that is required by passes to do their job.  Analysis Groups differ
    105 /// from normal analyses because any available implementation of the group will
    106 /// be used if it is available.
    107 ///
    108 /// If no analysis implementing the interface is available, a default
    109 /// implementation is created and added.  A pass registers itself as the default
    110 /// implementation by specifying 'true' as the second template argument of this
    111 /// class.
    112 ///
    113 /// In addition to registering itself as an analysis group member, a pass must
    114 /// register itself normally as well.  Passes may be members of multiple groups
    115 /// and may still be "required" specifically by name.
    116 ///
    117 /// The actual interface may also be registered as well (by not specifying the
    118 /// second template argument).  The interface should be registered to associate
    119 /// a nice name with the interface.
    120 class RegisterAGBase : public PassInfo {
    121 public:
    122   RegisterAGBase(StringRef Name, const void *InterfaceID,
    123                  const void *PassID = nullptr, bool isDefault = false);
    124 };
    125 
    126 template <typename Interface, bool Default = false>
    127 struct RegisterAnalysisGroup : public RegisterAGBase {
    128   explicit RegisterAnalysisGroup(PassInfo &RPB)
    129       : RegisterAGBase(RPB.getPassName(), &Interface::ID, RPB.getTypeInfo(),
    130                        Default) {}
    131 
    132   explicit RegisterAnalysisGroup(const char *Name)
    133       : RegisterAGBase(Name, &Interface::ID) {}
    134 };
    135 
    136 #define INITIALIZE_ANALYSIS_GROUP(agName, name, defaultPass)                   \
    137   static void *initialize##agName##AnalysisGroupOnce(PassRegistry &Registry) { \
    138     initialize##defaultPass##Pass(Registry);                                   \
    139     PassInfo *AI = new PassInfo(name, &agName::ID);                            \
    140     Registry.registerAnalysisGroup(&agName::ID, 0, *AI, false, true);          \
    141     return AI;                                                                 \
    142   }                                                                            \
    143   static llvm::once_flag Initialize##agName##AnalysisGroupFlag;                \
    144   void llvm::initialize##agName##AnalysisGroup(PassRegistry &Registry) {       \
    145     llvm::call_once(Initialize##agName##AnalysisGroupFlag,                     \
    146                     initialize##agName##AnalysisGroupOnce,                     \
    147                     std::ref(Registry));                                       \
    148   }
    149 
    150 #define INITIALIZE_AG_PASS(passName, agName, arg, name, cfg, analysis, def)    \
    151   static void *initialize##passName##PassOnce(PassRegistry &Registry) {        \
    152     if (!def)                                                                  \
    153       initialize##agName##AnalysisGroup(Registry);                             \
    154     PassInfo *PI = new PassInfo(                                               \
    155         name, arg, &passName::ID,                                              \
    156         PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis);     \
    157     Registry.registerPass(*PI, true);                                          \
    158                                                                                \
    159     PassInfo *AI = new PassInfo(name, &agName::ID);                            \
    160     Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def,       \
    161                                    true);                                      \
    162     return AI;                                                                 \
    163   }                                                                            \
    164   static llvm::once_flag Initialize##passName##PassFlag;                       \
    165   void llvm::initialize##passName##Pass(PassRegistry &Registry) {              \
    166     llvm::call_once(Initialize##passName##PassFlag,                            \
    167                     initialize##passName##PassOnce, std::ref(Registry));       \
    168   }
    169 
    170 #define INITIALIZE_AG_PASS_BEGIN(passName, agName, arg, n, cfg, analysis, def) \
    171   static void *initialize##passName##PassOnce(PassRegistry &Registry) {        \
    172     if (!def)                                                                  \
    173       initialize##agName##AnalysisGroup(Registry);
    174 
    175 #define INITIALIZE_AG_PASS_END(passName, agName, arg, n, cfg, analysis, def)   \
    176   PassInfo *PI = new PassInfo(                                                 \
    177       n, arg, &passName::ID,                                                   \
    178       PassInfo::NormalCtor_t(callDefaultCtor<passName>), cfg, analysis);       \
    179   Registry.registerPass(*PI, true);                                            \
    180                                                                                \
    181   PassInfo *AI = new PassInfo(n, &agName::ID);                                 \
    182   Registry.registerAnalysisGroup(&agName::ID, &passName::ID, *AI, def, true);  \
    183   return AI;                                                                   \
    184   }                                                                            \
    185   static llvm::once_flag Initialize##passName##PassFlag;                       \
    186   void llvm::initialize##passName##Pass(PassRegistry &Registry) {              \
    187     llvm::call_once(Initialize##passName##PassFlag,                            \
    188                     initialize##passName##PassOnce, std::ref(Registry));       \
    189   }
    190 
    191 //===---------------------------------------------------------------------------
    192 /// PassRegistrationListener class - This class is meant to be derived from by
    193 /// clients that are interested in which passes get registered and unregistered
    194 /// at runtime (which can be because of the RegisterPass constructors being run
    195 /// as the program starts up, or may be because a shared object just got
    196 /// loaded).
    197 struct PassRegistrationListener {
    198   PassRegistrationListener() = default;
    199   virtual ~PassRegistrationListener() = default;
    200 
    201   /// Callback functions - These functions are invoked whenever a pass is loaded
    202   /// or removed from the current executable.
    203   virtual void passRegistered(const PassInfo *) {}
    204 
    205   /// enumeratePasses - Iterate over the registered passes, calling the
    206   /// passEnumerate callback on each PassInfo object.
    207   void enumeratePasses();
    208 
    209   /// passEnumerate - Callback function invoked when someone calls
    210   /// enumeratePasses on this PassRegistrationListener object.
    211   virtual void passEnumerate(const PassInfo *) {}
    212 };
    213 
    214 } // end namespace llvm
    215 
    216 #endif // LLVM_PASSSUPPORT_H
    217