1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
3 // This file defines stuff that is used to define and "use" Passes. This file
4 // is automatically #included by Pass.h, so:
6 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
8 // Instead, #include Pass.h.
10 // This file defines Pass registration code and classes used for it.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_PASS_SUPPORT_H
15 #define LLVM_PASS_SUPPORT_H
17 // No need to include Pass.h, we are being included by it!
22 //===---------------------------------------------------------------------------
23 // PassInfo class - An instance of this class exists for every pass known by the
24 // system, and can be obtained from a live Pass by calling its getPassInfo()
25 // method. These objects are set up by the RegisterPass<> template, defined
29 const char *PassName; // Nice name for Pass
30 const char *PassArgument; // Command Line argument to run this pass
31 const std::type_info &TypeInfo; // type_info object for this Pass class
32 unsigned char PassType; // Set of enums values below...
34 Pass *(*NormalCtor)(); // No argument ctor
35 Pass *(*DataCtor)(const TargetData&);// Ctor taking TargetData object...
38 // PassType - Define symbolic constants that can be used to test to see if
39 // this pass should be listed by analyze or opt. Passes can use none, one or
40 // many of these flags or'd together. It is not legal to combine the
41 // AnalysisGroup flag with others.
44 Analysis = 1, Optimization = 2, LLC = 4, AnalysisGroup = 8
47 // PassInfo ctor - Do not call this directly, this should only be invoked
48 // through RegisterPass.
49 PassInfo(const char *name, const char *arg, const std::type_info &ti,
50 unsigned pt, Pass *(*normal)(), Pass *(*data)(const TargetData &))
51 : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt),
52 NormalCtor(normal), DataCtor(data) {
55 // getPassName - Return the friendly name for the pass, never returns null
56 const char *getPassName() const { return PassName; }
57 void setPassName(const char *Name) { PassName = Name; }
59 // getPassArgument - Return the command line option that may be passed to
60 // 'opt' that will cause this pass to be run. This will return null if there
63 const char *getPassArgument() const { return PassArgument; }
65 // getTypeInfo - Return the type_info object for the pass...
66 const std::type_info &getTypeInfo() const { return TypeInfo; }
68 // getPassType - Return the PassType of a pass. Note that this can be several
69 // different types or'd together. This is _strictly_ for use by opt, analyze
70 // and llc for deciding which passes to use as command line options.
72 unsigned getPassType() const { return PassType; }
74 // getNormalCtor - Return a pointer to a function, that when called, creates
75 // an instance of the pass and returns it. This pointer may be null if there
76 // is no default constructor for the pass.
78 Pass *(*getNormalCtor() const)() {
81 void setNormalCtor(Pass *(*Ctor)()) {
85 // createPass() - Use this
86 Pass *createPass() const {
87 assert((PassType != AnalysisGroup || NormalCtor) &&
88 "No default implementation found for analysis group!");
90 "Cannot call createPass on PassInfo without default ctor!");
94 // getDataCtor - Return a pointer to a function that creates an instance of
95 // the pass and returns it. This returns a constructor for a version of the
96 // pass that takes a TArgetData object as a parameter.
98 Pass *(*getDataCtor() const)(const TargetData &) {
104 //===---------------------------------------------------------------------------
105 // RegisterPass<t> template - This template class is used to notify the system
106 // that a Pass is available for use, and registers it into the internal database
107 // maintained by the PassManager. Unless this template is used, opt, for
108 // example will not be able to see the pass and attempts to create the pass will
109 // fail. This template is used in the follow manner (at global scope, in your
112 // static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
114 // This statement will cause your pass to be created by calling the default
115 // constructor exposed by the pass. If you have a different constructor that
116 // must be called, create a global constructor function (which takes the
117 // arguments you need and returns a Pass*) and register your pass like this:
119 // Pass *createMyPass(foo &opt) { return new MyPass(opt); }
120 // static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
122 struct RegisterPassBase {
123 // getPassInfo - Get the pass info for the registered class...
124 const PassInfo *getPassInfo() const { return PIObj; }
126 RegisterPassBase() : PIObj(0) {}
127 ~RegisterPassBase() { // Intentionally non-virtual...
128 if (PIObj) unregisterPass(PIObj);
132 PassInfo *PIObj; // The PassInfo object for this pass
133 void registerPass(PassInfo *);
134 void unregisterPass(PassInfo *);
136 // setPreservesCFG - Notice that this pass only depends on the CFG, so
137 // transformations that do not modify the CFG do not invalidate this pass.
139 void setPreservesCFG();
142 template<typename PassName>
143 Pass *callDefaultCtor() { return new PassName(); }
145 template<typename PassName>
146 struct RegisterPass : public RegisterPassBase {
148 // Register Pass using default constructor...
149 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
150 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
151 callDefaultCtor<PassName>, 0));
154 // Register Pass using default constructor explicitly...
155 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
157 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor,0));
160 // Register Pass using TargetData constructor...
161 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
162 Pass *(*datactor)(const TargetData &)) {
163 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
167 // Generic constructor version that has an unknown ctor type...
168 template<typename CtorType>
169 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
171 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0, 0));
175 // RegisterOpt - Register something that is to show up in Opt, this is just a
176 // shortcut for specifying RegisterPass...
178 template<typename PassName>
179 struct RegisterOpt : public RegisterPassBase {
180 RegisterOpt(const char *PassArg, const char *Name) {
181 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
182 PassInfo::Optimization,
183 callDefaultCtor<PassName>, 0));
186 // Register Pass using default constructor explicitly...
187 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) {
188 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
189 PassInfo::Optimization, ctor, 0));
192 // Register Pass using TargetData constructor...
193 RegisterOpt(const char *PassArg, const char *Name,
194 Pass *(*datactor)(const TargetData &)) {
195 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
196 PassInfo::Optimization, 0, datactor));
200 // RegisterAnalysis - Register something that is to show up in Analysis, this is
201 // just a shortcut for specifying RegisterPass... Analyses take a special
202 // argument that, when set to true, tells the system that the analysis ONLY
203 // depends on the shape of the CFG, so if a transformation preserves the CFG
204 // that the analysis is not invalidated.
206 template<typename PassName>
207 struct RegisterAnalysis : public RegisterPassBase {
208 RegisterAnalysis(const char *PassArg, const char *Name,
209 bool CFGOnly = false) {
210 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
212 callDefaultCtor<PassName>, 0));
218 // RegisterLLC - Register something that is to show up in LLC, this is just a
219 // shortcut for specifying RegisterPass...
221 template<typename PassName>
222 struct RegisterLLC : public RegisterPassBase {
223 RegisterLLC(const char *PassArg, const char *Name) {
224 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
226 callDefaultCtor<PassName>, 0));
229 // Register Pass using default constructor explicitly...
230 RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
231 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
232 PassInfo::LLC, ctor, 0));
235 // Register Pass using TargetData constructor...
236 RegisterLLC(const char *PassArg, const char *Name,
237 Pass *(*datactor)(const TargetData &)) {
238 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
239 PassInfo::LLC, 0, datactor));
242 // Register Pass using TargetMachine constructor...
243 RegisterLLC(const char *PassArg, const char *Name,
244 Pass *(*datactor)(TargetMachine &)) {
245 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
246 PassInfo::LLC, 0, 0));
251 // RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
252 // Analysis groups are used to define an interface (which need not derive from
253 // Pass) that is required by passes to do their job. Analysis Groups differ
254 // from normal analyses because any available implementation of the group will
255 // be used if it is available.
257 // If no analysis implementing the interface is available, a default
258 // implementation is created and added. A pass registers itself as the default
259 // implementation by specifying 'true' as the third template argument of this
262 // In addition to registering itself as an analysis group member, a pass must
263 // register itself normally as well. Passes may be members of multiple groups
264 // and may still be "required" specifically by name.
266 // The actual interface may also be registered as well (by not specifying the
267 // second template argument). The interface should be registered to associate a
268 // nice name with the interface.
270 class RegisterAGBase : public RegisterPassBase {
271 PassInfo *InterfaceInfo;
272 const PassInfo *ImplementationInfo;
273 bool isDefaultImplementation;
275 RegisterAGBase(const std::type_info &Interface,
276 const std::type_info *Pass = 0,
277 bool isDefault = false);
278 void setGroupName(const char *Name);
284 template<typename Interface, typename DefaultImplementationPass = void,
285 bool Default = false>
286 struct RegisterAnalysisGroup : public RegisterAGBase {
287 RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface),
288 &typeid(DefaultImplementationPass),
293 // Define a specialization of RegisterAnalysisGroup that is used to set the name
294 // for the analysis group.
296 template<typename Interface>
297 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase {
298 RegisterAnalysisGroup(const char *Name)
299 : RegisterAGBase(typeid(Interface)) {
306 //===---------------------------------------------------------------------------
307 // PassRegistrationListener class - This class is meant to be derived from by
308 // clients that are interested in which passes get registered and unregistered
309 // at runtime (which can be because of the RegisterPass constructors being run
310 // as the program starts up, or may be because a shared object just got loaded).
311 // Deriving from the PassRegistationListener class automatically registers your
312 // object to receive callbacks indicating when passes are loaded and removed.
314 struct PassRegistrationListener {
316 // PassRegistrationListener ctor - Add the current object to the list of
317 // PassRegistrationListeners...
318 PassRegistrationListener();
320 // dtor - Remove object from list of listeners...
321 virtual ~PassRegistrationListener();
323 // Callback functions - These functions are invoked whenever a pass is loaded
324 // or removed from the current executable.
326 virtual void passRegistered(const PassInfo *P) {}
327 virtual void passUnregistered(const PassInfo *P) {}
329 // enumeratePasses - Iterate over the registered passes, calling the
330 // passEnumerate callback on each PassInfo object.
332 void enumeratePasses();
334 // passEnumerate - Callback function invoked when someone calls
335 // enumeratePasses on this PassRegistrationListener object.
337 virtual void passEnumerate(const PassInfo *P) {}
341 //===---------------------------------------------------------------------------
342 // IncludeFile class - This class is used as a hack to make sure that the
343 // implementation of a header file is included into a tool that uses the header.
344 // This is solely to overcome problems linking .a files and not getting the
345 // implementation of passes we need.