LLVM API Documentation
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_PASS_SUPPORT_H 00022 #define LLVM_PASS_SUPPORT_H 00023 00024 // No need to include Pass.h, we are being included by it! 00025 00026 namespace llvm { 00027 00028 class TargetMachine; 00029 00030 //===--------------------------------------------------------------------------- 00031 /// PassInfo class - An instance of this class exists for every pass known by 00032 /// the system, and can be obtained from a live Pass by calling its 00033 /// getPassInfo() method. These objects are set up by the RegisterPass<> 00034 /// template, defined below. 00035 /// 00036 class PassInfo { 00037 public: 00038 typedef Pass* (*NormalCtor_t)(); 00039 00040 private: 00041 const char *const PassName; // Nice name for Pass 00042 const char *const PassArgument; // Command Line argument to run this pass 00043 const intptr_t PassID; 00044 const bool IsCFGOnlyPass; // Pass only looks at the CFG. 00045 const bool IsAnalysis; // True if an analysis pass. 00046 const bool IsAnalysisGroup; // True if an analysis group. 00047 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass 00048 00049 NormalCtor_t NormalCtor; 00050 00051 public: 00052 /// PassInfo ctor - Do not call this directly, this should only be invoked 00053 /// through RegisterPass. 00054 PassInfo(const char *name, const char *arg, intptr_t pi, 00055 NormalCtor_t normal = 0, 00056 bool isCFGOnly = false, bool is_analysis = false) 00057 : PassName(name), PassArgument(arg), PassID(pi), 00058 IsCFGOnlyPass(isCFGOnly), 00059 IsAnalysis(is_analysis), IsAnalysisGroup(false), NormalCtor(normal) { 00060 registerPass(); 00061 } 00062 /// PassInfo ctor - Do not call this directly, this should only be invoked 00063 /// through RegisterPass. This version is for use by analysis groups; it 00064 /// does not auto-register the pass. 00065 PassInfo(const char *name, intptr_t pi) 00066 : PassName(name), PassArgument(""), PassID(pi), 00067 IsCFGOnlyPass(false), 00068 IsAnalysis(false), IsAnalysisGroup(true), NormalCtor(0) { 00069 } 00070 00071 /// getPassName - Return the friendly name for the pass, never returns null 00072 /// 00073 const char *getPassName() const { return PassName; } 00074 00075 /// getPassArgument - Return the command line option that may be passed to 00076 /// 'opt' that will cause this pass to be run. This will return null if there 00077 /// is no argument. 00078 /// 00079 const char *getPassArgument() const { return PassArgument; } 00080 00081 /// getTypeInfo - Return the id object for the pass... 00082 /// TODO : Rename 00083 intptr_t getTypeInfo() const { return PassID; } 00084 00085 /// isAnalysisGroup - Return true if this is an analysis group, not a normal 00086 /// pass. 00087 /// 00088 bool isAnalysisGroup() const { return IsAnalysisGroup; } 00089 bool isAnalysis() const { return IsAnalysis; } 00090 00091 /// isCFGOnlyPass - return true if this pass only looks at the CFG for the 00092 /// function. 00093 bool isCFGOnlyPass() const { return IsCFGOnlyPass; } 00094 00095 /// getNormalCtor - Return a pointer to a function, that when called, creates 00096 /// an instance of the pass and returns it. This pointer may be null if there 00097 /// is no default constructor for the pass. 00098 /// 00099 NormalCtor_t getNormalCtor() const { 00100 return NormalCtor; 00101 } 00102 void setNormalCtor(NormalCtor_t Ctor) { 00103 NormalCtor = Ctor; 00104 } 00105 00106 /// createPass() - Use this method to create an instance of this pass. 00107 Pass *createPass() const { 00108 assert((!isAnalysisGroup() || NormalCtor) && 00109 "No default implementation found for analysis group!"); 00110 assert(NormalCtor && 00111 "Cannot call createPass on PassInfo without default ctor!"); 00112 return NormalCtor(); 00113 } 00114 00115 /// addInterfaceImplemented - This method is called when this pass is 00116 /// registered as a member of an analysis group with the RegisterAnalysisGroup 00117 /// template. 00118 /// 00119 void addInterfaceImplemented(const PassInfo *ItfPI) { 00120 ItfImpl.push_back(ItfPI); 00121 } 00122 00123 /// getInterfacesImplemented - Return a list of all of the analysis group 00124 /// interfaces implemented by this pass. 00125 /// 00126 const std::vector<const PassInfo*> &getInterfacesImplemented() const { 00127 return ItfImpl; 00128 } 00129 00130 /// getPassInfo - Deprecated API compaatibility function. This function 00131 /// just returns 'this'. 00132 /// 00133 const PassInfo *getPassInfo() const { 00134 return this; 00135 } 00136 00137 protected: 00138 void registerPass(); 00139 void unregisterPass(); 00140 00141 private: 00142 void operator=(const PassInfo &); // do not implement 00143 PassInfo(const PassInfo &); // do not implement 00144 }; 00145 00146 00147 template<typename PassName> 00148 Pass *callDefaultCtor() { return new PassName(); } 00149 00150 //===--------------------------------------------------------------------------- 00151 /// RegisterPass<t> template - This template class is used to notify the system 00152 /// that a Pass is available for use, and registers it into the internal 00153 /// database maintained by the PassManager. Unless this template is used, opt, 00154 /// for example will not be able to see the pass and attempts to create the pass 00155 /// will fail. This template is used in the follow manner (at global scope, in 00156 /// your .cpp file): 00157 /// 00158 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name"); 00159 /// 00160 /// This statement will cause your pass to be created by calling the default 00161 /// constructor exposed by the pass. If you have a different constructor that 00162 /// must be called, create a global constructor function (which takes the 00163 /// arguments you need and returns a Pass*) and register your pass like this: 00164 /// 00165 /// static RegisterPass<PassClassName> tmp("passopt", "My Name"); 00166 /// 00167 template<typename passName> 00168 struct RegisterPass : public PassInfo { 00169 00170 // Register Pass using default constructor... 00171 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false, 00172 bool is_analysis = false) 00173 : PassInfo(Name, PassArg, intptr_t(&passName::ID), 00174 PassInfo::NormalCtor_t(callDefaultCtor<passName>), 00175 CFGOnly, is_analysis) { 00176 } 00177 }; 00178 00179 00180 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_. 00181 /// Analysis groups are used to define an interface (which need not derive from 00182 /// Pass) that is required by passes to do their job. Analysis Groups differ 00183 /// from normal analyses because any available implementation of the group will 00184 /// be used if it is available. 00185 /// 00186 /// If no analysis implementing the interface is available, a default 00187 /// implementation is created and added. A pass registers itself as the default 00188 /// implementation by specifying 'true' as the second template argument of this 00189 /// class. 00190 /// 00191 /// In addition to registering itself as an analysis group member, a pass must 00192 /// register itself normally as well. Passes may be members of multiple groups 00193 /// and may still be "required" specifically by name. 00194 /// 00195 /// The actual interface may also be registered as well (by not specifying the 00196 /// second template argument). The interface should be registered to associate 00197 /// a nice name with the interface. 00198 /// 00199 class RegisterAGBase : public PassInfo { 00200 PassInfo *InterfaceInfo; 00201 const PassInfo *ImplementationInfo; 00202 bool isDefaultImplementation; 00203 protected: 00204 explicit RegisterAGBase(const char *Name, 00205 intptr_t InterfaceID, 00206 intptr_t PassID = 0, 00207 bool isDefault = false); 00208 }; 00209 00210 template<typename Interface, bool Default = false> 00211 struct RegisterAnalysisGroup : public RegisterAGBase { 00212 explicit RegisterAnalysisGroup(PassInfo &RPB) 00213 : RegisterAGBase(RPB.getPassName(), 00214 intptr_t(&Interface::ID), RPB.getTypeInfo(), 00215 Default) { 00216 } 00217 00218 explicit RegisterAnalysisGroup(const char *Name) 00219 : RegisterAGBase(Name, intptr_t(&Interface::ID)) { 00220 } 00221 }; 00222 00223 00224 00225 //===--------------------------------------------------------------------------- 00226 /// PassRegistrationListener class - This class is meant to be derived from by 00227 /// clients that are interested in which passes get registered and unregistered 00228 /// at runtime (which can be because of the RegisterPass constructors being run 00229 /// as the program starts up, or may be because a shared object just got 00230 /// loaded). Deriving from the PassRegistationListener class automatically 00231 /// registers your object to receive callbacks indicating when passes are loaded 00232 /// and removed. 00233 /// 00234 struct PassRegistrationListener { 00235 00236 /// PassRegistrationListener ctor - Add the current object to the list of 00237 /// PassRegistrationListeners... 00238 PassRegistrationListener(); 00239 00240 /// dtor - Remove object from list of listeners... 00241 /// 00242 virtual ~PassRegistrationListener(); 00243 00244 /// Callback functions - These functions are invoked whenever a pass is loaded 00245 /// or removed from the current executable. 00246 /// 00247 virtual void passRegistered(const PassInfo *) {} 00248 00249 /// enumeratePasses - Iterate over the registered passes, calling the 00250 /// passEnumerate callback on each PassInfo object. 00251 /// 00252 void enumeratePasses(); 00253 00254 /// passEnumerate - Callback function invoked when someone calls 00255 /// enumeratePasses on this PassRegistrationListener object. 00256 /// 00257 virtual void passEnumerate(const PassInfo *) {} 00258 }; 00259 00260 00261 } // End llvm namespace 00262 00263 #endif