Add new helper template function
[oota-llvm.git] / include / llvm / PassSupport.h
1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
2 //
3 // This file defines stuff that is used to define and "use" Passes.  This file
4 // is automatically #included by Pass.h, so:
5 //
6 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
7 //
8 // Instead, #include Pass.h.
9 //
10 // This file defines Pass registration code and classes used for it.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_PASS_SUPPORT_H
15 #define LLVM_PASS_SUPPORT_H
16
17 // No need to include Pass.h, we are being included by it!
18
19 class TargetData;
20 class TargetMachine;
21
22 //===---------------------------------------------------------------------------
23 /// PassInfo class - An instance of this class exists for every pass known by
24 /// the system, and can be obtained from a live Pass by calling its
25 /// getPassInfo() method.  These objects are set up by the RegisterPass<>
26 /// template, defined below.
27 ///
28 class PassInfo {
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...
33   std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
34
35   Pass *(*NormalCtor)();               // No argument ctor
36   Pass *(*DataCtor)(const TargetData&);// Ctor taking const TargetData object...
37   Pass *(*TargetCtor)(TargetMachine&);   // Ctor taking TargetMachine object...
38
39 public:
40   /// PassType - Define symbolic constants that can be used to test to see if
41   /// this pass should be listed by analyze or opt.  Passes can use none, one or
42   /// many of these flags or'd together.  It is not legal to combine the
43   /// AnalysisGroup flag with others.
44   ///
45   enum {
46     Analysis = 1, Optimization = 2, LLC = 4, AnalysisGroup = 8
47   };
48
49   /// PassInfo ctor - Do not call this directly, this should only be invoked
50   /// through RegisterPass.
51   PassInfo(const char *name, const char *arg, const std::type_info &ti, 
52            unsigned pt, Pass *(*normal)() = 0,
53            Pass *(*datactor)(const TargetData &) = 0,
54            Pass *(*targetctor)(TargetMachine &) = 0)
55     : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt),
56       NormalCtor(normal), DataCtor(datactor), TargetCtor(targetctor)  {
57   }
58
59   /// getPassName - Return the friendly name for the pass, never returns null
60   ///
61   const char *getPassName() const { return PassName; }
62   void setPassName(const char *Name) { PassName = Name; }
63
64   /// getPassArgument - Return the command line option that may be passed to
65   /// 'opt' that will cause this pass to be run.  This will return null if there
66   /// is no argument.
67   ///
68   const char *getPassArgument() const { return PassArgument; }
69
70   /// getTypeInfo - Return the type_info object for the pass...
71   ///
72   const std::type_info &getTypeInfo() const { return TypeInfo; }
73
74   /// getPassType - Return the PassType of a pass.  Note that this can be
75   /// several different types or'd together.  This is _strictly_ for use by opt,
76   /// analyze and llc for deciding which passes to use as command line options.
77   ///
78   unsigned getPassType() const { return PassType; }
79
80   /// getNormalCtor - Return a pointer to a function, that when called, creates
81   /// an instance of the pass and returns it.  This pointer may be null if there
82   /// is no default constructor for the pass.
83   /// 
84   Pass *(*getNormalCtor() const)() {
85     return NormalCtor;
86   }
87   void setNormalCtor(Pass *(*Ctor)()) {
88     NormalCtor = Ctor;
89   }
90
91   /// createPass() - Use this method to create an instance of this pass.
92   Pass *createPass() const {
93     assert((PassType != AnalysisGroup || NormalCtor) &&
94            "No default implementation found for analysis group!");
95     assert(NormalCtor &&
96            "Cannot call createPass on PassInfo without default ctor!");
97     return NormalCtor();
98   }
99
100   /// getDataCtor - Return a pointer to a function that creates an instance of
101   /// the pass and returns it.  This returns a constructor for a version of the
102   /// pass that takes a TargetData object as a parameter.
103   ///
104   Pass *(*getDataCtor() const)(const TargetData &) {
105     return DataCtor;
106   }
107
108   /// getTargetCtor - Return a pointer to a function that creates an instance of
109   /// the pass and returns it.  This returns a constructor for a version of the
110   /// pass that takes a TargetMachine object as a parameter.
111   ///
112   Pass *(*getTargetCtor() const)(TargetMachine &) {
113     return TargetCtor;
114   }
115
116   /// addInterfaceImplemented - This method is called when this pass is
117   /// registered as a member of an analysis group with the RegisterAnalysisGroup
118   /// template.
119   ///
120   void addInterfaceImplemented(const PassInfo *ItfPI) {
121     ItfImpl.push_back(ItfPI);
122   }
123
124   /// getInterfacesImplemented - Return a list of all of the analysis group
125   /// interfaces implemented by this pass.
126   ///
127   const std::vector<const PassInfo*> &getInterfacesImplemented() const {
128     return ItfImpl;
129   }
130 };
131
132
133 //===---------------------------------------------------------------------------
134 /// RegisterPass<t> template - This template class is used to notify the system
135 /// that a Pass is available for use, and registers it into the internal
136 /// database maintained by the PassManager.  Unless this template is used, opt,
137 /// for example will not be able to see the pass and attempts to create the pass
138 /// will fail. This template is used in the follow manner (at global scope, in
139 /// your .cpp file):
140 /// 
141 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
142 ///
143 /// This statement will cause your pass to be created by calling the default
144 /// constructor exposed by the pass.  If you have a different constructor that
145 /// must be called, create a global constructor function (which takes the
146 /// arguments you need and returns a Pass*) and register your pass like this:
147 ///
148 /// Pass *createMyPass(foo &opt) { return new MyPass(opt); }
149 /// static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
150 /// 
151 struct RegisterPassBase {
152   /// getPassInfo - Get the pass info for the registered class...
153   ///
154   const PassInfo *getPassInfo() const { return PIObj; }
155
156   RegisterPassBase() : PIObj(0) {}
157   ~RegisterPassBase() {   // Intentionally non-virtual...
158     if (PIObj) unregisterPass(PIObj);
159   }
160
161 protected:
162   PassInfo *PIObj;       // The PassInfo object for this pass
163   void registerPass(PassInfo *);
164   void unregisterPass(PassInfo *);
165
166   /// setPreservesCFG - Notice that this pass only depends on the CFG, so
167   /// transformations that do not modify the CFG do not invalidate this pass.
168   ///
169   void setPreservesCFG();
170 };
171
172 template<typename PassName>
173 Pass *callDefaultCtor() { return new PassName(); }
174 template<typename PassName>
175 Pass *callTargetDataCtor(const TargetData &TD) { return new PassName(TD); }
176
177 template<typename PassName>
178 struct RegisterPass : public RegisterPassBase {
179   
180   // Register Pass using default constructor...
181   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
182     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
183                               callDefaultCtor<PassName>));
184   }
185
186   // Register Pass using default constructor explicitly...
187   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
188                Pass *(*ctor)()) {
189     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor));
190   }
191
192   // Register Pass using TargetData constructor...
193   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
194                Pass *(*datactor)(const TargetData &)) {
195     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
196                               0, datactor));
197   }
198
199   // Register Pass using TargetMachine constructor...
200   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
201                Pass *(*targetctor)(TargetMachine &)) {
202     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
203                               0, 0, targetctor));
204   }
205
206   // Generic constructor version that has an unknown ctor type...
207   template<typename CtorType>
208   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
209                CtorType *Fn) {
210     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0));
211   }
212 };
213
214 /// RegisterOpt - Register something that is to show up in Opt, this is just a
215 /// shortcut for specifying RegisterPass...
216 ///
217 template<typename PassName>
218 struct RegisterOpt : public RegisterPassBase {
219   RegisterOpt(const char *PassArg, const char *Name) {
220     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
221                               PassInfo::Optimization,
222                               callDefaultCtor<PassName>));
223   }
224
225   /// Register Pass using default constructor explicitly...
226   ///
227   RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) {
228     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
229                               PassInfo::Optimization, ctor));
230   }
231
232   /// Register Pass using TargetData constructor...
233   ///
234   RegisterOpt(const char *PassArg, const char *Name,
235                Pass *(*datactor)(const TargetData &)) {
236     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
237                               PassInfo::Optimization, 0, datactor));
238   }
239
240   /// Register Pass using TargetMachine constructor...
241   ///
242   RegisterOpt(const char *PassArg, const char *Name,
243                Pass *(*targetctor)(TargetMachine &)) {
244     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
245                               PassInfo::Optimization, 0, 0, targetctor));
246   }
247 };
248
249 /// RegisterAnalysis - Register something that is to show up in Analysis, this
250 /// is just a shortcut for specifying RegisterPass...  Analyses take a special
251 /// argument that, when set to true, tells the system that the analysis ONLY
252 /// depends on the shape of the CFG, so if a transformation preserves the CFG
253 /// that the analysis is not invalidated.
254 ///
255 template<typename PassName>
256 struct RegisterAnalysis : public RegisterPassBase {
257   RegisterAnalysis(const char *PassArg, const char *Name,
258                    bool CFGOnly = false) {
259     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
260                               PassInfo::Analysis,
261                               callDefaultCtor<PassName>));
262     if (CFGOnly)
263       setPreservesCFG();
264   }
265 };
266
267 /// RegisterLLC - Register something that is to show up in LLC, this is just a
268 /// shortcut for specifying RegisterPass...
269 ///
270 template<typename PassName>
271 struct RegisterLLC : public RegisterPassBase {
272   RegisterLLC(const char *PassArg, const char *Name) {
273     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
274                               PassInfo::LLC,
275                               callDefaultCtor<PassName>));
276   }
277
278   /// Register Pass using default constructor explicitly...
279   ///
280   RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
281     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
282                               PassInfo::LLC, ctor));
283   }
284
285   /// Register Pass using TargetData constructor...
286   ///
287   RegisterLLC(const char *PassArg, const char *Name,
288                Pass *(*datactor)(const TargetData &)) {
289     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
290                               PassInfo::LLC, 0, datactor));
291   }
292
293   /// Register Pass using TargetMachine constructor...
294   ///
295   RegisterLLC(const char *PassArg, const char *Name,
296                Pass *(*datactor)(TargetMachine &)) {
297     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
298                               PassInfo::LLC));
299   }
300 };
301
302
303 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
304 /// Analysis groups are used to define an interface (which need not derive from
305 /// Pass) that is required by passes to do their job.  Analysis Groups differ
306 /// from normal analyses because any available implementation of the group will
307 /// be used if it is available.
308 ///
309 /// If no analysis implementing the interface is available, a default
310 /// implementation is created and added.  A pass registers itself as the default
311 /// implementation by specifying 'true' as the third template argument of this
312 /// class.
313 ///
314 /// In addition to registering itself as an analysis group member, a pass must
315 /// register itself normally as well.  Passes may be members of multiple groups
316 /// and may still be "required" specifically by name.
317 ///
318 /// The actual interface may also be registered as well (by not specifying the
319 /// second template argument).  The interface should be registered to associate
320 /// a nice name with the interface.
321 ///
322 class RegisterAGBase : public RegisterPassBase {
323   PassInfo *InterfaceInfo;
324   const PassInfo *ImplementationInfo;
325   bool isDefaultImplementation;
326 protected:
327   RegisterAGBase(const std::type_info &Interface,
328                  const std::type_info *Pass = 0,
329                  bool isDefault = false);
330   void setGroupName(const char *Name);
331 public:
332   ~RegisterAGBase();
333 };
334
335
336 template<typename Interface, typename DefaultImplementationPass = void,
337          bool Default = false>
338 struct RegisterAnalysisGroup : public RegisterAGBase {
339   RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface),
340                                            &typeid(DefaultImplementationPass),
341                                            Default) {
342   }
343 };
344
345 /// Define a specialization of RegisterAnalysisGroup that is used to set the
346 /// name for the analysis group.
347 ///
348 template<typename Interface>
349 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase {
350   RegisterAnalysisGroup(const char *Name)
351     : RegisterAGBase(typeid(Interface)) {
352     setGroupName(Name);
353   }
354 };
355
356
357
358 //===---------------------------------------------------------------------------
359 /// PassRegistrationListener class - This class is meant to be derived from by
360 /// clients that are interested in which passes get registered and unregistered
361 /// at runtime (which can be because of the RegisterPass constructors being run
362 /// as the program starts up, or may be because a shared object just got
363 /// loaded).  Deriving from the PassRegistationListener class automatically
364 /// registers your object to receive callbacks indicating when passes are loaded
365 /// and removed.
366 ///
367 struct PassRegistrationListener {
368
369   /// PassRegistrationListener ctor - Add the current object to the list of
370   /// PassRegistrationListeners...
371   PassRegistrationListener();
372
373   /// dtor - Remove object from list of listeners...
374   ///
375   virtual ~PassRegistrationListener();
376
377   /// Callback functions - These functions are invoked whenever a pass is loaded
378   /// or removed from the current executable.
379   ///
380   virtual void passRegistered(const PassInfo *P) {}
381   virtual void passUnregistered(const PassInfo *P) {}
382
383   /// enumeratePasses - Iterate over the registered passes, calling the
384   /// passEnumerate callback on each PassInfo object.
385   ///
386   void enumeratePasses();
387
388   /// passEnumerate - Callback function invoked when someone calls
389   /// enumeratePasses on this PassRegistrationListener object.
390   ///
391   virtual void passEnumerate(const PassInfo *P) {}
392 };
393
394
395 //===---------------------------------------------------------------------------
396 /// IncludeFile class - This class is used as a hack to make sure that the
397 /// implementation of a header file is included into a tool that uses the
398 /// header.  This is solely to overcome problems linking .a files and not
399 /// getting the implementation of passes we need.
400 ///
401 struct IncludeFile {
402   IncludeFile(void *);
403 };
404 #endif