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!
23 //===---------------------------------------------------------------------------
24 // PassInfo class - An instance of this class exists for every pass known by the
25 // system, and can be obtained from a live Pass by calling its getPassInfo()
26 // method. These objects are set up by the RegisterPass<> template, defined
30 const char *PassName; // Nice name for Pass
31 const char *PassArgument; // Command Line argument to run this pass
32 const std::type_info &TypeInfo; // type_info object for this Pass class
33 unsigned char PassType; // Set of enums values below...
35 Pass *(*NormalCtor)(); // No argument ctor
36 Pass *(*DataCtor)(const TargetData&);// Ctor taking TargetData object...
39 // PassType - Define symbolic constants that can be used to test to see if
40 // this pass should be listed by analyze or opt. Passes can use none, one or
41 // many of these flags or'd together.
44 Analysis = 1, Optimization = 2, LLC = 4
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) {
55 // getPassName - Return the friendly name for the pass, never returns null
56 const char *getPassName() const { return PassName; }
58 // getPassArgument - Return the command line option that may be passed to
59 // 'opt' that will cause this pass to be run. This will return null if there
62 const char *getPassArgument() const { return PassArgument; }
64 // getTypeInfo - Return the type_info object for the pass...
65 const std::type_info &getTypeInfo() const { return TypeInfo; }
67 // getPassType - Return the PassType of a pass. Note that this can be several
68 // different types or'd together. This is _strictly_ for use by opt, analyze
69 // and llc for deciding which passes to use as command line options.
71 unsigned getPassType() const { return PassType; }
73 // getNormalCtor - Return a pointer to a function, that when called, creates
74 // an instance of the pass and returns it. This pointer may be null if there
75 // is no default constructor for the pass.
77 Pass *(*getNormalCtor() const)() {
81 // createPass() - Use this
82 Pass *createPass() const {
84 "Cannot call createPass on PassInfo without default ctor!");
88 // getDataCtor - Return a pointer to a function that creates an instance of
89 // the pass and returns it. This returns a constructor for a version of the
90 // pass that takes a TArgetData object as a parameter.
92 Pass *(*getDataCtor() const)(const TargetData &) {
98 //===---------------------------------------------------------------------------
99 // RegisterPass<t> template - This template class is used to notify the system
100 // that a Pass is available for use, and registers it into the internal database
101 // maintained by the PassManager. Unless this template is used, opt, for
102 // example will not be able to see the pass and attempts to create the pass will
103 // fail. This template is used in the follow manner (at global scope, in your
106 // static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
108 // This statement will cause your pass to be created by calling the default
109 // constructor exposed by the pass. If you have a different constructor that
110 // must be called, create a global constructor function (which takes the
111 // arguments you need and returns a Pass*) and register your pass like this:
113 // Pass *createMyPass(foo &opt) { return new MyPass(opt); }
114 // static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
116 struct RegisterPassBase {
117 // getPassInfo - Get the pass info for the registered class...
118 const PassInfo *getPassInfo() const { return PIObj; }
120 ~RegisterPassBase(); // Intentionally non-virtual...
122 inline operator PassInfo* () const { return PIObj; }
125 PassInfo *PIObj; // The PassInfo object for this pass
126 void registerPass(PassInfo *);
128 // setPreservesCFG - Notice that this pass only depends on the CFG, so
129 // transformations that do not modify the CFG do not invalidate this pass.
131 void setPreservesCFG();
134 template<typename PassName>
135 Pass *callDefaultCtor() { return new PassName(); }
137 template<typename PassName>
138 struct RegisterPass : public RegisterPassBase {
140 // Register Pass using default constructor...
141 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
142 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
143 callDefaultCtor<PassName>, 0));
146 // Register Pass using default constructor explicitly...
147 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
149 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor,0));
152 // Register Pass using TargetData constructor...
153 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
154 Pass *(*datactor)(const TargetData &)) {
155 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
159 // Generic constructor version that has an unknown ctor type...
160 template<typename CtorType>
161 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
163 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0, 0));
167 // RegisterOpt - Register something that is to show up in Opt, this is just a
168 // shortcut for specifying RegisterPass...
170 template<typename PassName>
171 struct RegisterOpt : public RegisterPassBase {
172 RegisterOpt(const char *PassArg, const char *Name) {
173 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
174 PassInfo::Optimization,
175 callDefaultCtor<PassName>, 0));
178 // Register Pass using default constructor explicitly...
179 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) {
180 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
181 PassInfo::Optimization, ctor, 0));
184 // Register Pass using TargetData constructor...
185 RegisterOpt(const char *PassArg, const char *Name,
186 Pass *(*datactor)(const TargetData &)) {
187 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
188 PassInfo::Optimization, 0, datactor));
192 // RegisterAnalysis - Register something that is to show up in Analysis, this is
193 // just a shortcut for specifying RegisterPass... Analyses take a special
194 // argument that, when set to true, tells the system that the analysis ONLY
195 // depends on the shape of the CFG, so if a transformation preserves the CFG
196 // that the analysis is not invalidated.
198 template<typename PassName>
199 struct RegisterAnalysis : public RegisterPassBase {
200 RegisterAnalysis(const char *PassArg, const char *Name,
201 bool CFGOnly = false) {
202 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
204 callDefaultCtor<PassName>, 0));
210 // RegisterLLC - Register something that is to show up in LLC, this is just a
211 // shortcut for specifying RegisterPass...
213 template<typename PassName>
214 struct RegisterLLC : public RegisterPassBase {
215 RegisterLLC(const char *PassArg, const char *Name) {
216 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
218 callDefaultCtor<PassName>, 0));
221 // Register Pass using default constructor explicitly...
222 RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
223 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
224 PassInfo::LLC, ctor, 0));
227 // Register Pass using TargetData constructor...
228 RegisterLLC(const char *PassArg, const char *Name,
229 Pass *(*datactor)(const TargetData &)) {
230 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
231 PassInfo::LLC, 0, datactor));
234 // Register Pass using TargetMachine constructor...
235 RegisterLLC(const char *PassArg, const char *Name,
236 Pass *(*datactor)(TargetMachine &)) {
237 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
238 PassInfo::LLC, 0, 0));
243 //===---------------------------------------------------------------------------
244 // PassRegistrationListener class - This class is meant to be derived from by
245 // clients that are interested in which passes get registered and unregistered
246 // at runtime (which can be because of the RegisterPass constructors being run
247 // as the program starts up, or may be because a shared object just got loaded).
248 // Deriving from the PassRegistationListener class automatically registers your
249 // object to receive callbacks indicating when passes are loaded and removed.
251 struct PassRegistrationListener {
253 // PassRegistrationListener ctor - Add the current object to the list of
254 // PassRegistrationListeners...
255 PassRegistrationListener();
257 // dtor - Remove object from list of listeners...
258 virtual ~PassRegistrationListener();
260 // Callback functions - These functions are invoked whenever a pass is loaded
261 // or removed from the current executable.
263 virtual void passRegistered(const PassInfo *P) {}
264 virtual void passUnregistered(const PassInfo *P) {}
266 // enumeratePasses - Iterate over the registered passes, calling the
267 // passEnumerate callback on each PassInfo object.
269 void enumeratePasses();
271 // passEnumerate - Callback function invoked when someone calls
272 // enumeratePasses on this PassRegistrationListener object.
274 virtual void passEnumerate(const PassInfo *P) {}