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 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
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
33 Pass *(*NormalCtor)(); // No argument ctor
34 Pass *(*DataCtor)(const TargetData&);// Ctor taking TargetData object...
37 // PassInfo ctor - Do not call this directly, this should only be invoked
38 // through RegisterPass.
39 PassInfo(const char *name, const char *arg, const std::type_info &ti,
40 Pass *(*normal)(), Pass *(*data)(const TargetData &))
41 : PassName(name), PassArgument(arg), TypeInfo(ti), NormalCtor(normal),
45 // getPassName - Return the friendly name for the pass, never returns null
46 const char *getPassName() const { return PassName; }
48 // getPassArgument - Return the command line option that may be passed to
49 // 'opt' that will cause this pass to be run. This will return null if there
52 const char *getPassArgument() const { return PassArgument; }
54 // getTypeInfo - Return the type_info object for the pass...
55 const std::type_info &getTypeInfo() const { return TypeInfo; }
57 // getNormalCtor - Return a pointer to a function, that when called, creates
58 // an instance of the pass and returns it. This pointer may be null if there
59 // is no default constructor for the pass.
61 Pass *(*getNormalCtor() const)() {
65 // getDataCtor - Return a pointer to a function that creates an instance of
66 // the pass and returns it. This returns a constructor for a version of the
67 // pass that takes a TArgetData object as a parameter.
69 Pass *(*getDataCtor() const)(const TargetData &) {
75 //===---------------------------------------------------------------------------
76 // RegisterPass<t> template - This template class is used to notify the system
77 // that a Pass is available for use, and registers it into the internal database
78 // maintained by the PassManager. Unless this template is used, opt, for
79 // example will not be able to see the pass and attempts to create the pass will
80 // fail. This template is used in the follow manner (at global scope, in your
83 // static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
85 // This statement will cause your pass to be created by calling the default
86 // constructor exposed by the pass. If you have a different constructor that
87 // must be called, create a global constructor function (which takes the
88 // arguments you need and returns a Pass*) and register your pass like this:
90 // Pass *createMyPass(foo &opt) { return new MyPass(opt); }
91 // static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
93 struct RegisterPassBase {
94 // getPassInfo - Get the pass info for the registered class...
95 const PassInfo *getPassInfo() const { return PIObj; }
97 ~RegisterPassBase(); // Intentionally non-virtual...
100 PassInfo *PIObj; // The PassInfo object for this pass
101 void registerPass(PassInfo *);
104 template<typename PassName>
105 Pass *callDefaultCtor() { return new PassName(); }
107 template<typename PassName>
108 struct RegisterPass : public RegisterPassBase {
110 // Register Pass using default constructor...
111 RegisterPass(const char *PassArg, const char *Name) {
112 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
113 callDefaultCtor<PassName>, 0));
116 // Register Pass using default constructor explicitly...
117 RegisterPass(const char *PassArg, const char *Name,
119 registerPass(new PassInfo(Name, PassArg, typeid(PassName), ctor, 0));
122 // Register Pass using TargetData constructor...
123 RegisterPass(const char *PassArg, const char *Name,
124 Pass *(*datactor)(const TargetData &)) {
125 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 0, datactor));
128 // Generic constructor version that has an unknown ctor type...
129 template<typename CtorType>
130 RegisterPass(const char *PassArg, const char *Name, CtorType *Fn) {
131 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 0, 0));
136 //===---------------------------------------------------------------------------
137 // PassRegistrationListener class - This class is meant to be derived from by
138 // clients that are interested in which passes get registered and unregistered
139 // at runtime (which can be because of the RegisterPass constructors being run
140 // as the program starts up, or may be because a shared object just got loaded).
141 // Deriving from the PassRegistationListener class automatically registers your
142 // object to receive callbacks indicating when passes are loaded and removed.
144 struct PassRegistrationListener {
146 // PassRegistrationListener ctor - Add the current object to the list of
147 // PassRegistrationListeners...
148 PassRegistrationListener();
150 // dtor - Remove object from list of listeners...
151 virtual ~PassRegistrationListener();
153 // Callback functions - These functions are invoked whenever a pass is loaded
154 // or removed from the current executable.
156 virtual void passRegistered(const PassInfo *P) {}
157 virtual void passUnregistered(const PassInfo *P) {}
159 // enumeratePasses - Iterate over the registered passes, calling the
160 // passEnumerate callback on each PassInfo object.
162 void enumeratePasses();
164 // passEnumerate - Callback function invoked when someone calls
165 // enumeratePasses on this PassRegistrationListener object.
167 virtual void passEnumerate(const PassInfo *P) {}