e3c8ba75b59caf64cb0ff3ae8b34d81ae47ab042
[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 #include <typeinfo>
20 class TargetData;
21
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
26 // 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
34   Pass *(*NormalCtor)();               // No argument ctor
35   Pass *(*DataCtor)(const TargetData&);// Ctor taking TargetData object...
36
37 public:
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.
41   //
42   enum {
43     Analysis = 1, Optimization = 2
44   };
45
46   // PassInfo ctor - Do not call this directly, this should only be invoked
47   // through RegisterPass.
48   PassInfo(const char *name, const char *arg, const std::type_info &ti, 
49            unsigned pt, Pass *(*normal)(), Pass *(*data)(const TargetData &))
50     : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt),
51       NormalCtor(normal), DataCtor(data) {
52   }
53
54   // getPassName - Return the friendly name for the pass, never returns null
55   const char *getPassName() const { return PassName; }
56
57   // getPassArgument - Return the command line option that may be passed to
58   // 'opt' that will cause this pass to be run.  This will return null if there
59   // is no argument.
60   //
61   const char *getPassArgument() const { return PassArgument; }
62
63   // getTypeInfo - Return the type_info object for the pass...
64   const std::type_info &getTypeInfo() const { return TypeInfo; }
65
66   // getPassType - Return the PassType of a pass.  Note that this can be several
67   // different types or'd together.  This is _strictly_ for use by opt, analyze
68   // and llc for deciding which passes to use as command line options.
69   //
70   unsigned getPassType() const { return PassType; }
71
72   // getNormalCtor - Return a pointer to a function, that when called, creates
73   // an instance of the pass and returns it.  This pointer may be null if there
74   // is no default constructor for the pass.
75   
76   Pass *(*getNormalCtor() const)() {
77     return NormalCtor;
78   }
79
80   // getDataCtor - Return a pointer to a function that creates an instance of
81   // the pass and returns it.  This returns a constructor for a version of the
82   // pass that takes a TArgetData object as a parameter.
83   //
84   Pass *(*getDataCtor() const)(const TargetData &) {
85     return DataCtor;
86   }
87 };
88
89
90 //===---------------------------------------------------------------------------
91 // RegisterPass<t> template - This template class is used to notify the system
92 // that a Pass is available for use, and registers it into the internal database
93 // maintained by the PassManager.  Unless this template is used, opt, for
94 // example will not be able to see the pass and attempts to create the pass will
95 // fail. This template is used in the follow manner (at global scope, in your
96 // .cpp file):
97 // 
98 // static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
99 //
100 // This statement will cause your pass to be created by calling the default
101 // constructor exposed by the pass.  If you have a different constructor that
102 // must be called, create a global constructor function (which takes the
103 // arguments you need and returns a Pass*) and register your pass like this:
104 //
105 // Pass *createMyPass(foo &opt) { return new MyPass(opt); }
106 // static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
107 // 
108 struct RegisterPassBase {
109   // getPassInfo - Get the pass info for the registered class...
110   const PassInfo *getPassInfo() const { return PIObj; }
111
112   ~RegisterPassBase();   // Intentionally non-virtual...
113
114 protected:
115   PassInfo *PIObj;       // The PassInfo object for this pass
116   void registerPass(PassInfo *);
117 };
118
119 template<typename PassName>
120 Pass *callDefaultCtor() { return new PassName(); }
121
122 template<typename PassName>
123 struct RegisterPass : public RegisterPassBase {
124   
125   // Register Pass using default constructor...
126   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
127     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
128                               callDefaultCtor<PassName>, 0));
129   }
130
131   // Register Pass using default constructor explicitly...
132   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
133                Pass *(*ctor)()) {
134     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor,0));
135   }
136
137   // Register Pass using TargetData constructor...
138   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
139                Pass *(*datactor)(const TargetData &)) {
140     registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
141                               0, datactor));
142   }
143
144   // Generic constructor version that has an unknown ctor type...
145   template<typename CtorType>
146   RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
147                CtorType *Fn) {
148     registerPass(new PassInfo(Name, PassArg, typeid(PassName), 0, 0));
149   }
150 };
151
152 // RegisterOpt - Register something that is to show up in Opt, this is just a
153 // shortcut for specifying RegisterPass...
154 //
155 template<typename PassName>
156 struct RegisterOpt : public RegisterPassBase {
157   RegisterOpt(const char *PassArg, const char *Name) {
158     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
159                               PassInfo::Optimization,
160                               callDefaultCtor<PassName>, 0));
161   }
162
163   // Register Pass using default constructor explicitly...
164   RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) {
165     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
166                               PassInfo::Optimization, ctor, 0));
167   }
168
169   // Register Pass using TargetData constructor...
170   RegisterOpt(const char *PassArg, const char *Name,
171                Pass *(*datactor)(const TargetData &)) {
172     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
173                               PassInfo::Optimization, 0, datactor));
174   }
175 };
176
177 // RegisterAnalysis - Register something that is to show up in Analysis, this is
178 // just a shortcut for specifying RegisterPass...
179 //
180 template<typename PassName>
181 struct RegisterAnalysis : public RegisterPassBase {
182   RegisterAnalysis(const char *PassArg, const char *Name) {
183     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
184                               PassInfo::Analysis,
185                               callDefaultCtor<PassName>, 0));
186   }
187
188   // Register Pass using default constructor explicitly...
189   RegisterAnalysis(const char *PassArg, const char *Name, Pass *(*ctor)()) {
190     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
191                               PassInfo::Analys, ctor, 0));
192   }
193
194   // Register Pass using TargetData constructor...
195   RegisterAnalysis(const char *PassArg, const char *Name,
196                Pass *(*datactor)(const TargetData &)) {
197     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
198                               PassInfo::Analysis, 0, datactor));
199   }
200 };
201
202
203 //===---------------------------------------------------------------------------
204 // PassRegistrationListener class - This class is meant to be derived from by
205 // clients that are interested in which passes get registered and unregistered
206 // at runtime (which can be because of the RegisterPass constructors being run
207 // as the program starts up, or may be because a shared object just got loaded).
208 // Deriving from the PassRegistationListener class automatically registers your
209 // object to receive callbacks indicating when passes are loaded and removed.
210 //
211 struct PassRegistrationListener {
212
213   // PassRegistrationListener ctor - Add the current object to the list of
214   // PassRegistrationListeners...
215   PassRegistrationListener();
216
217   // dtor - Remove object from list of listeners...
218   virtual ~PassRegistrationListener();
219
220   // Callback functions - These functions are invoked whenever a pass is loaded
221   // or removed from the current executable.
222   //
223   virtual void passRegistered(const PassInfo *P) {}
224   virtual void passUnregistered(const PassInfo *P) {}
225
226   // enumeratePasses - Iterate over the registered passes, calling the
227   // passEnumerate callback on each PassInfo object.
228   //
229   void enumeratePasses();
230
231   // passEnumerate - Callback function invoked when someone calls
232   // enumeratePasses on this PassRegistrationListener object.
233   //
234   virtual void passEnumerate(const PassInfo *P) {}
235 };
236
237 #endif