Add support for passes that use a TargetMachine object.
[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
175 template<typename PassName>
176 struct RegisterPass : public RegisterPassBase {
177   
178   // Register Pass using default constructor...
179   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
180     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
181                               callDefaultCtor<PassName>));
182   }
183
184   // Register Pass using default constructor explicitly...
185   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
186                Pass *(*ctor)()) {
187     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor));
188   }
189
190   // Register Pass using TargetData constructor...
191   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
192                Pass *(*datactor)(const TargetData &)) {
193     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
194                               0, datactor));
195   }
196
197   // Register Pass using TargetMachine constructor...
198   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
199                Pass *(*targetctor)(TargetMachine &)) {
200     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
201                               0, 0, targetctor));
202   }
203
204   // Generic constructor version that has an unknown ctor type...
205   template<typename CtorType>
206   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
207                CtorType *Fn) {
208     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0));
209   }
210 };
211
212 /// RegisterOpt - Register something that is to show up in Opt, this is just a
213 /// shortcut for specifying RegisterPass...
214 ///
215 template<typename PassName>
216 struct RegisterOpt : public RegisterPassBase {
217   RegisterOpt(const char *PassArg, const char *Name) {
218     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
219                               PassInfo::Optimization,
220                               callDefaultCtor<PassName>));
221   }
222
223   /// Register Pass using default constructor explicitly...
224   ///
225   RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) {
226     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
227                               PassInfo::Optimization, ctor));
228   }
229
230   /// Register Pass using TargetData constructor...
231   ///
232   RegisterOpt(const char *PassArg, const char *Name,
233                Pass *(*datactor)(const TargetData &)) {
234     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
235                               PassInfo::Optimization, 0, datactor));
236   }
237
238   /// Register Pass using TargetMachine constructor...
239   ///
240   RegisterOpt(const char *PassArg, const char *Name,
241                Pass *(*targetctor)(TargetMachine &)) {
242     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
243                               PassInfo::Optimization, 0, 0, targetctor));
244   }
245 };
246
247 /// RegisterAnalysis - Register something that is to show up in Analysis, this
248 /// is just a shortcut for specifying RegisterPass...  Analyses take a special
249 /// argument that, when set to true, tells the system that the analysis ONLY
250 /// depends on the shape of the CFG, so if a transformation preserves the CFG
251 /// that the analysis is not invalidated.
252 ///
253 template<typename PassName>
254 struct RegisterAnalysis : public RegisterPassBase {
255   RegisterAnalysis(const char *PassArg, const char *Name,
256                    bool CFGOnly = false) {
257     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
258                               PassInfo::Analysis,
259                               callDefaultCtor<PassName>));
260     if (CFGOnly)
261       setPreservesCFG();
262   }
263 };
264
265 /// RegisterLLC - Register something that is to show up in LLC, this is just a
266 /// shortcut for specifying RegisterPass...
267 ///
268 template<typename PassName>
269 struct RegisterLLC : public RegisterPassBase {
270   RegisterLLC(const char *PassArg, const char *Name) {
271     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
272                               PassInfo::LLC,
273                               callDefaultCtor<PassName>));
274   }
275
276   /// Register Pass using default constructor explicitly...
277   ///
278   RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
279     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
280                               PassInfo::LLC, ctor));
281   }
282
283   /// Register Pass using TargetData constructor...
284   ///
285   RegisterLLC(const char *PassArg, const char *Name,
286                Pass *(*datactor)(const TargetData &)) {
287     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
288                               PassInfo::LLC, 0, datactor));
289   }
290
291   /// Register Pass using TargetMachine constructor...
292   ///
293   RegisterLLC(const char *PassArg, const char *Name,
294                Pass *(*datactor)(TargetMachine &)) {
295     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
296                               PassInfo::LLC));
297   }
298 };
299
300
301 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
302 /// Analysis groups are used to define an interface (which need not derive from
303 /// Pass) that is required by passes to do their job.  Analysis Groups differ
304 /// from normal analyses because any available implementation of the group will
305 /// be used if it is available.
306 ///
307 /// If no analysis implementing the interface is available, a default
308 /// implementation is created and added.  A pass registers itself as the default
309 /// implementation by specifying 'true' as the third template argument of this
310 /// class.
311 ///
312 /// In addition to registering itself as an analysis group member, a pass must
313 /// register itself normally as well.  Passes may be members of multiple groups
314 /// and may still be "required" specifically by name.
315 ///
316 /// The actual interface may also be registered as well (by not specifying the
317 /// second template argument).  The interface should be registered to associate
318 /// a nice name with the interface.
319 ///
320 class RegisterAGBase : public RegisterPassBase {
321   PassInfo *InterfaceInfo;
322   const PassInfo *ImplementationInfo;
323   bool isDefaultImplementation;
324 protected:
325   RegisterAGBase(const std::type_info &Interface,
326                  const std::type_info *Pass = 0,
327                  bool isDefault = false);
328   void setGroupName(const char *Name);
329 public:
330   ~RegisterAGBase();
331 };
332
333
334 template<typename Interface, typename DefaultImplementationPass = void,
335          bool Default = false>
336 struct RegisterAnalysisGroup : public RegisterAGBase {
337   RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface),
338                                            &typeid(DefaultImplementationPass),
339                                            Default) {
340   }
341 };
342
343 /// Define a specialization of RegisterAnalysisGroup that is used to set the
344 /// name for the analysis group.
345 ///
346 template<typename Interface>
347 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase {
348   RegisterAnalysisGroup(const char *Name)
349     : RegisterAGBase(typeid(Interface)) {
350     setGroupName(Name);
351   }
352 };
353
354
355
356 //===---------------------------------------------------------------------------
357 /// PassRegistrationListener class - This class is meant to be derived from by
358 /// clients that are interested in which passes get registered and unregistered
359 /// at runtime (which can be because of the RegisterPass constructors being run
360 /// as the program starts up, or may be because a shared object just got
361 /// loaded).  Deriving from the PassRegistationListener class automatically
362 /// registers your object to receive callbacks indicating when passes are loaded
363 /// and removed.
364 ///
365 struct PassRegistrationListener {
366
367   /// PassRegistrationListener ctor - Add the current object to the list of
368   /// PassRegistrationListeners...
369   PassRegistrationListener();
370
371   /// dtor - Remove object from list of listeners...
372   ///
373   virtual ~PassRegistrationListener();
374
375   /// Callback functions - These functions are invoked whenever a pass is loaded
376   /// or removed from the current executable.
377   ///
378   virtual void passRegistered(const PassInfo *P) {}
379   virtual void passUnregistered(const PassInfo *P) {}
380
381   /// enumeratePasses - Iterate over the registered passes, calling the
382   /// passEnumerate callback on each PassInfo object.
383   ///
384   void enumeratePasses();
385
386   /// passEnumerate - Callback function invoked when someone calls
387   /// enumeratePasses on this PassRegistrationListener object.
388   ///
389   virtual void passEnumerate(const PassInfo *P) {}
390 };
391
392
393 //===---------------------------------------------------------------------------
394 /// IncludeFile class - This class is used as a hack to make sure that the
395 /// implementation of a header file is included into a tool that uses the
396 /// header.  This is solely to overcome problems linking .a files and not
397 /// getting the implementation of passes we need.
398 ///
399 struct IncludeFile {
400   IncludeFile(void *);
401 };
402 #endif