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(); }
174 template<typename PassName>
175 Pass *callTargetDataCtor(const TargetData &TD) { return new PassName(TD); }
177 template<typename PassName>
178 struct RegisterPass : public RegisterPassBase {
180 // Register Pass using default constructor...
181 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
182 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
183 callDefaultCtor<PassName>));
186 // Register Pass using default constructor explicitly...
187 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
189 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor));
192 // Register Pass using TargetData constructor...
193 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
194 Pass *(*datactor)(const TargetData &)) {
195 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
199 // Register Pass using TargetMachine constructor...
200 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
201 Pass *(*targetctor)(TargetMachine &)) {
202 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
206 // Generic constructor version that has an unknown ctor type...
207 template<typename CtorType>
208 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
210 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0));
214 /// RegisterOpt - Register something that is to show up in Opt, this is just a
215 /// shortcut for specifying RegisterPass...
217 template<typename PassName>
218 struct RegisterOpt : public RegisterPassBase {
219 RegisterOpt(const char *PassArg, const char *Name) {
220 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
221 PassInfo::Optimization,
222 callDefaultCtor<PassName>));
225 /// Register Pass using default constructor explicitly...
227 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)()) {
228 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
229 PassInfo::Optimization, ctor));
232 /// Register Pass using TargetData constructor...
234 RegisterOpt(const char *PassArg, const char *Name,
235 Pass *(*datactor)(const TargetData &)) {
236 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
237 PassInfo::Optimization, 0, datactor));
240 /// Register Pass using TargetMachine constructor...
242 RegisterOpt(const char *PassArg, const char *Name,
243 Pass *(*targetctor)(TargetMachine &)) {
244 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
245 PassInfo::Optimization, 0, 0, targetctor));
249 /// RegisterAnalysis - Register something that is to show up in Analysis, this
250 /// is just a shortcut for specifying RegisterPass... Analyses take a special
251 /// argument that, when set to true, tells the system that the analysis ONLY
252 /// depends on the shape of the CFG, so if a transformation preserves the CFG
253 /// that the analysis is not invalidated.
255 template<typename PassName>
256 struct RegisterAnalysis : public RegisterPassBase {
257 RegisterAnalysis(const char *PassArg, const char *Name,
258 bool CFGOnly = false) {
259 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
261 callDefaultCtor<PassName>));
267 /// RegisterLLC - Register something that is to show up in LLC, this is just a
268 /// shortcut for specifying RegisterPass...
270 template<typename PassName>
271 struct RegisterLLC : public RegisterPassBase {
272 RegisterLLC(const char *PassArg, const char *Name) {
273 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
275 callDefaultCtor<PassName>));
278 /// Register Pass using default constructor explicitly...
280 RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
281 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
282 PassInfo::LLC, ctor));
285 /// Register Pass using TargetData constructor...
287 RegisterLLC(const char *PassArg, const char *Name,
288 Pass *(*datactor)(const TargetData &)) {
289 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
290 PassInfo::LLC, 0, datactor));
293 /// Register Pass using TargetMachine constructor...
295 RegisterLLC(const char *PassArg, const char *Name,
296 Pass *(*datactor)(TargetMachine &)) {
297 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
303 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
304 /// Analysis groups are used to define an interface (which need not derive from
305 /// Pass) that is required by passes to do their job. Analysis Groups differ
306 /// from normal analyses because any available implementation of the group will
307 /// be used if it is available.
309 /// If no analysis implementing the interface is available, a default
310 /// implementation is created and added. A pass registers itself as the default
311 /// implementation by specifying 'true' as the third template argument of this
314 /// In addition to registering itself as an analysis group member, a pass must
315 /// register itself normally as well. Passes may be members of multiple groups
316 /// and may still be "required" specifically by name.
318 /// The actual interface may also be registered as well (by not specifying the
319 /// second template argument). The interface should be registered to associate
320 /// a nice name with the interface.
322 class RegisterAGBase : public RegisterPassBase {
323 PassInfo *InterfaceInfo;
324 const PassInfo *ImplementationInfo;
325 bool isDefaultImplementation;
327 RegisterAGBase(const std::type_info &Interface,
328 const std::type_info *Pass = 0,
329 bool isDefault = false);
330 void setGroupName(const char *Name);
336 template<typename Interface, typename DefaultImplementationPass = void,
337 bool Default = false>
338 struct RegisterAnalysisGroup : public RegisterAGBase {
339 RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface),
340 &typeid(DefaultImplementationPass),
345 /// Define a specialization of RegisterAnalysisGroup that is used to set the
346 /// name for the analysis group.
348 template<typename Interface>
349 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase {
350 RegisterAnalysisGroup(const char *Name)
351 : RegisterAGBase(typeid(Interface)) {
358 //===---------------------------------------------------------------------------
359 /// PassRegistrationListener class - This class is meant to be derived from by
360 /// clients that are interested in which passes get registered and unregistered
361 /// at runtime (which can be because of the RegisterPass constructors being run
362 /// as the program starts up, or may be because a shared object just got
363 /// loaded). Deriving from the PassRegistationListener class automatically
364 /// registers your object to receive callbacks indicating when passes are loaded
367 struct PassRegistrationListener {
369 /// PassRegistrationListener ctor - Add the current object to the list of
370 /// PassRegistrationListeners...
371 PassRegistrationListener();
373 /// dtor - Remove object from list of listeners...
375 virtual ~PassRegistrationListener();
377 /// Callback functions - These functions are invoked whenever a pass is loaded
378 /// or removed from the current executable.
380 virtual void passRegistered(const PassInfo *P) {}
381 virtual void passUnregistered(const PassInfo *P) {}
383 /// enumeratePasses - Iterate over the registered passes, calling the
384 /// passEnumerate callback on each PassInfo object.
386 void enumeratePasses();
388 /// passEnumerate - Callback function invoked when someone calls
389 /// enumeratePasses on this PassRegistrationListener object.
391 virtual void passEnumerate(const PassInfo *P) {}
395 //===---------------------------------------------------------------------------
396 /// IncludeFile class - This class is used as a hack to make sure that the
397 /// implementation of a header file is included into a tool that uses the
398 /// header. This is solely to overcome problems linking .a files and not
399 /// getting the implementation of passes we need.