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
32 unsigned char PassType; // Set of enums values below...
34 Pass *(*NormalCtor)(); // No argument ctor
35 Pass *(*DataCtor)(const TargetData&);// Ctor taking TargetData object...
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.
43 Analysis = 1, Optimization = 2
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) {
54 // getPassName - Return the friendly name for the pass, never returns null
55 const char *getPassName() const { return PassName; }
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
61 const char *getPassArgument() const { return PassArgument; }
63 // getTypeInfo - Return the type_info object for the pass...
64 const std::type_info &getTypeInfo() const { return TypeInfo; }
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.
70 unsigned getPassType() const { return PassType; }
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.
76 Pass *(*getNormalCtor() const)() {
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.
84 Pass *(*getDataCtor() const)(const TargetData &) {
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
98 // static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
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:
105 // Pass *createMyPass(foo &opt) { return new MyPass(opt); }
106 // static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
108 struct RegisterPassBase {
109 // getPassInfo - Get the pass info for the registered class...
110 const PassInfo *getPassInfo() const { return PIObj; }
112 ~RegisterPassBase(); // Intentionally non-virtual...
115 PassInfo *PIObj; // The PassInfo object for this pass
116 void registerPass(PassInfo *);
119 template<typename PassName>
120 Pass *callDefaultCtor() { return new PassName(); }
122 template<typename PassName>
123 struct RegisterPass : public RegisterPassBase {
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));
131 // Register Pass using default constructor explicitly...
132 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
134 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor,0));
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,
144 // Generic constructor version that has an unknown ctor type...
145 template<typename CtorType>
146 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
148 registerPass(new PassInfo(Name, PassArg, typeid(PassName), 0, 0));
152 // RegisterOpt - Register something that is to show up in Opt, this is just a
153 // shortcut for specifying RegisterPass...
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));
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));
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));
177 // RegisterAnalysis - Register something that is to show up in Analysis, this is
178 // just a shortcut for specifying RegisterPass...
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),
185 callDefaultCtor<PassName>, 0));
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));
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));
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.
211 struct PassRegistrationListener {
213 // PassRegistrationListener ctor - Add the current object to the list of
214 // PassRegistrationListeners...
215 PassRegistrationListener();
217 // dtor - Remove object from list of listeners...
218 virtual ~PassRegistrationListener();
220 // Callback functions - These functions are invoked whenever a pass is loaded
221 // or removed from the current executable.
223 virtual void passRegistered(const PassInfo *P) {}
224 virtual void passUnregistered(const PassInfo *P) {}
226 // enumeratePasses - Iterate over the registered passes, calling the
227 // passEnumerate callback on each PassInfo object.
229 void enumeratePasses();
231 // passEnumerate - Callback function invoked when someone calls
232 // enumeratePasses on this PassRegistrationListener object.
234 virtual void passEnumerate(const PassInfo *P) {}