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