1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
3 // This file defines stuff that is used to define and "use" Passes. This file
4 // is automatically #included by Pass.h, so:
6 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
8 // Instead, #include Pass.h.
10 // This file defines Pass registration code and classes used for it.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_PASS_SUPPORT_H
15 #define LLVM_PASS_SUPPORT_H
17 // No need to include Pass.h, we are being included by it!
22 //===---------------------------------------------------------------------------
23 /// PassInfo class - An instance of this class exists for every pass known by
24 /// the system, and can be obtained from a live Pass by calling its
25 /// getPassInfo() method. These objects are set up by the RegisterPass<>
26 /// template, defined below.
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 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
35 Pass *(*NormalCtor)(); // No argument ctor
36 Pass *(*DataCtor)(const TargetData&);// Ctor taking const TargetData object...
37 Pass *(*TargetCtor)(TargetMachine&); // Ctor taking TargetMachine object...
40 /// PassType - Define symbolic constants that can be used to test to see if
41 /// this pass should be listed by analyze or opt. Passes can use none, one or
42 /// many of these flags or'd together. It is not legal to combine the
43 /// AnalysisGroup flag with others.
46 Analysis = 1, Optimization = 2, LLC = 4, AnalysisGroup = 8
49 /// PassInfo ctor - Do not call this directly, this should only be invoked
50 /// through RegisterPass.
51 PassInfo(const char *name, const char *arg, const std::type_info &ti,
52 unsigned pt, Pass *(*normal)() = 0,
53 Pass *(*datactor)(const TargetData &) = 0,
54 Pass *(*targetctor)(TargetMachine &) = 0)
55 : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt),
56 NormalCtor(normal), DataCtor(datactor), TargetCtor(targetctor) {
59 /// getPassName - Return the friendly name for the pass, never returns null
61 const char *getPassName() const { return PassName; }
62 void setPassName(const char *Name) { PassName = Name; }
64 /// getPassArgument - Return the command line option that may be passed to
65 /// 'opt' that will cause this pass to be run. This will return null if there
68 const char *getPassArgument() const { return PassArgument; }
70 /// getTypeInfo - Return the type_info object for the pass...
72 const std::type_info &getTypeInfo() const { return TypeInfo; }
74 /// getPassType - Return the PassType of a pass. Note that this can be
75 /// several different types or'd together. This is _strictly_ for use by opt,
76 /// analyze and llc for deciding which passes to use as command line options.
78 unsigned getPassType() const { return PassType; }
80 /// getNormalCtor - Return a pointer to a function, that when called, creates
81 /// an instance of the pass and returns it. This pointer may be null if there
82 /// is no default constructor for the pass.
84 Pass *(*getNormalCtor() const)() {
87 void setNormalCtor(Pass *(*Ctor)()) {
91 /// createPass() - Use this method to create an instance of this pass.
92 Pass *createPass() const {
93 assert((PassType != AnalysisGroup || NormalCtor) &&
94 "No default implementation found for analysis group!");
96 "Cannot call createPass on PassInfo without default ctor!");
100 /// getDataCtor - Return a pointer to a function that creates an instance of
101 /// the pass and returns it. This returns a constructor for a version of the
102 /// pass that takes a TargetData object as a parameter.
104 Pass *(*getDataCtor() const)(const TargetData &) {
108 /// getTargetCtor - Return a pointer to a function that creates an instance of
109 /// the pass and returns it. This returns a constructor for a version of the
110 /// pass that takes a TargetMachine object as a parameter.
112 Pass *(*getTargetCtor() const)(TargetMachine &) {
116 /// addInterfaceImplemented - This method is called when this pass is
117 /// registered as a member of an analysis group with the RegisterAnalysisGroup
120 void addInterfaceImplemented(const PassInfo *ItfPI) {
121 ItfImpl.push_back(ItfPI);
124 /// getInterfacesImplemented - Return a list of all of the analysis group
125 /// interfaces implemented by this pass.
127 const std::vector<const PassInfo*> &getInterfacesImplemented() const {
133 //===---------------------------------------------------------------------------
134 /// RegisterPass<t> template - This template class is used to notify the system
135 /// that a Pass is available for use, and registers it into the internal
136 /// database maintained by the PassManager. Unless this template is used, opt,
137 /// for example will not be able to see the pass and attempts to create the pass
138 /// will fail. This template is used in the follow manner (at global scope, in
141 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
143 /// This statement will cause your pass to be created by calling the default
144 /// constructor exposed by the pass. If you have a different constructor that
145 /// must be called, create a global constructor function (which takes the
146 /// arguments you need and returns a Pass*) and register your pass like this:
148 /// Pass *createMyPass(foo &opt) { return new MyPass(opt); }
149 /// static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
151 struct RegisterPassBase {
152 /// getPassInfo - Get the pass info for the registered class...
154 const PassInfo *getPassInfo() const { return PIObj; }
156 RegisterPassBase() : PIObj(0) {}
157 ~RegisterPassBase() { // Intentionally non-virtual...
158 if (PIObj) unregisterPass(PIObj);
162 PassInfo *PIObj; // The PassInfo object for this pass
163 void registerPass(PassInfo *);
164 void unregisterPass(PassInfo *);
166 /// setPreservesCFG - Notice that this pass only depends on the CFG, so
167 /// transformations that do not modify the CFG do not invalidate this pass.
169 void setPreservesCFG();
172 template<typename PassName>
173 Pass *callDefaultCtor() { return new PassName(); }
175 template<typename PassName>
176 struct RegisterPass : public RegisterPassBase {
178 // Register Pass using default constructor...
179 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
180 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
181 callDefaultCtor<PassName>));
184 // Register Pass using default constructor explicitly...
185 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
187 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor));
190 // Register Pass using TargetData constructor...
191 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
192 Pass *(*datactor)(const TargetData &)) {
193 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
197 // Register Pass using TargetMachine constructor...
198 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
199 Pass *(*targetctor)(TargetMachine &)) {
200 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
204 // Generic constructor version that has an unknown ctor type...
205 template<typename CtorType>
206 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
208 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0));
212 /// RegisterOpt - Register something that is to show up in Opt, this is just a
213 /// shortcut for specifying RegisterPass...
215 template<typename PassName>
216 struct RegisterOpt : public RegisterPassBase {
217 RegisterOpt(const char *PassArg, const char *Name) {
218 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
219 PassInfo::Optimization,
220 callDefaultCtor<PassName>));
223 /// Register Pass using default constructor explicitly...
225 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) {
226 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
227 PassInfo::Optimization, ctor));
230 /// Register Pass using TargetData constructor...
232 RegisterOpt(const char *PassArg, const char *Name,
233 Pass *(*datactor)(const TargetData &)) {
234 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
235 PassInfo::Optimization, 0, datactor));
238 /// Register Pass using TargetMachine constructor...
240 RegisterOpt(const char *PassArg, const char *Name,
241 Pass *(*targetctor)(TargetMachine &)) {
242 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
243 PassInfo::Optimization, 0, 0, targetctor));
247 /// RegisterAnalysis - Register something that is to show up in Analysis, this
248 /// is just a shortcut for specifying RegisterPass... Analyses take a special
249 /// argument that, when set to true, tells the system that the analysis ONLY
250 /// depends on the shape of the CFG, so if a transformation preserves the CFG
251 /// that the analysis is not invalidated.
253 template<typename PassName>
254 struct RegisterAnalysis : public RegisterPassBase {
255 RegisterAnalysis(const char *PassArg, const char *Name,
256 bool CFGOnly = false) {
257 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
259 callDefaultCtor<PassName>));
265 /// RegisterLLC - Register something that is to show up in LLC, this is just a
266 /// shortcut for specifying RegisterPass...
268 template<typename PassName>
269 struct RegisterLLC : public RegisterPassBase {
270 RegisterLLC(const char *PassArg, const char *Name) {
271 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
273 callDefaultCtor<PassName>));
276 /// Register Pass using default constructor explicitly...
278 RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
279 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
280 PassInfo::LLC, ctor));
283 /// Register Pass using TargetData constructor...
285 RegisterLLC(const char *PassArg, const char *Name,
286 Pass *(*datactor)(const TargetData &)) {
287 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
288 PassInfo::LLC, 0, datactor));
291 /// Register Pass using TargetMachine constructor...
293 RegisterLLC(const char *PassArg, const char *Name,
294 Pass *(*datactor)(TargetMachine &)) {
295 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
301 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
302 /// Analysis groups are used to define an interface (which need not derive from
303 /// Pass) that is required by passes to do their job. Analysis Groups differ
304 /// from normal analyses because any available implementation of the group will
305 /// be used if it is available.
307 /// If no analysis implementing the interface is available, a default
308 /// implementation is created and added. A pass registers itself as the default
309 /// implementation by specifying 'true' as the third template argument of this
312 /// In addition to registering itself as an analysis group member, a pass must
313 /// register itself normally as well. Passes may be members of multiple groups
314 /// and may still be "required" specifically by name.
316 /// The actual interface may also be registered as well (by not specifying the
317 /// second template argument). The interface should be registered to associate
318 /// a nice name with the interface.
320 class RegisterAGBase : public RegisterPassBase {
321 PassInfo *InterfaceInfo;
322 const PassInfo *ImplementationInfo;
323 bool isDefaultImplementation;
325 RegisterAGBase(const std::type_info &Interface,
326 const std::type_info *Pass = 0,
327 bool isDefault = false);
328 void setGroupName(const char *Name);
334 template<typename Interface, typename DefaultImplementationPass = void,
335 bool Default = false>
336 struct RegisterAnalysisGroup : public RegisterAGBase {
337 RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface),
338 &typeid(DefaultImplementationPass),
343 /// Define a specialization of RegisterAnalysisGroup that is used to set the
344 /// name for the analysis group.
346 template<typename Interface>
347 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase {
348 RegisterAnalysisGroup(const char *Name)
349 : RegisterAGBase(typeid(Interface)) {
356 //===---------------------------------------------------------------------------
357 /// PassRegistrationListener class - This class is meant to be derived from by
358 /// clients that are interested in which passes get registered and unregistered
359 /// at runtime (which can be because of the RegisterPass constructors being run
360 /// as the program starts up, or may be because a shared object just got
361 /// loaded). Deriving from the PassRegistationListener class automatically
362 /// registers your object to receive callbacks indicating when passes are loaded
365 struct PassRegistrationListener {
367 /// PassRegistrationListener ctor - Add the current object to the list of
368 /// PassRegistrationListeners...
369 PassRegistrationListener();
371 /// dtor - Remove object from list of listeners...
373 virtual ~PassRegistrationListener();
375 /// Callback functions - These functions are invoked whenever a pass is loaded
376 /// or removed from the current executable.
378 virtual void passRegistered(const PassInfo *P) {}
379 virtual void passUnregistered(const PassInfo *P) {}
381 /// enumeratePasses - Iterate over the registered passes, calling the
382 /// passEnumerate callback on each PassInfo object.
384 void enumeratePasses();
386 /// passEnumerate - Callback function invoked when someone calls
387 /// enumeratePasses on this PassRegistrationListener object.
389 virtual void passEnumerate(const PassInfo *P) {}
393 //===---------------------------------------------------------------------------
394 /// IncludeFile class - This class is used as a hack to make sure that the
395 /// implementation of a header file is included into a tool that uses the
396 /// header. This is solely to overcome problems linking .a files and not
397 /// getting the implementation of passes we need.