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