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