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