1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines stuff that is used to define and "use" Passes. This file
11 // is automatically #included by Pass.h, so:
13 // NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
15 // Instead, #include Pass.h.
17 // This file defines Pass registration code and classes used for it.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_PASS_SUPPORT_H
22 #define LLVM_PASS_SUPPORT_H
24 // No need to include Pass.h, we are being included by it!
30 //===---------------------------------------------------------------------------
31 /// PassInfo class - An instance of this class exists for every pass known by
32 /// the system, and can be obtained from a live Pass by calling its
33 /// getPassInfo() method. These objects are set up by the RegisterPass<>
34 /// template, defined below.
37 const char *PassName; // Nice name for Pass
38 const char *PassArgument; // Command Line argument to run this pass
39 const std::type_info &TypeInfo; // type_info object for this Pass class
40 unsigned char PassType; // Set of enums values below...
41 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
43 Pass *(*NormalCtor)(); // No argument ctor
44 Pass *(*TargetCtor)(TargetMachine&); // Ctor taking TargetMachine object...
47 /// PassType - Define symbolic constants that can be used to test to see if
48 /// this pass should be listed by analyze or opt. Passes can use none, one or
49 /// many of these flags or'd together. It is not legal to combine the
50 /// AnalysisGroup flag with others.
53 Analysis = 1, Optimization = 2, LLC = 4, AnalysisGroup = 8
56 /// PassInfo ctor - Do not call this directly, this should only be invoked
57 /// through RegisterPass.
58 PassInfo(const char *name, const char *arg, const std::type_info &ti,
59 unsigned pt, Pass *(*normal)() = 0,
60 Pass *(*targetctor)(TargetMachine &) = 0)
61 : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt),
62 NormalCtor(normal), TargetCtor(targetctor) {
65 /// getPassName - Return the friendly name for the pass, never returns null
67 const char *getPassName() const { return PassName; }
68 void setPassName(const char *Name) { PassName = Name; }
70 /// getPassArgument - Return the command line option that may be passed to
71 /// 'opt' that will cause this pass to be run. This will return null if there
74 const char *getPassArgument() const { return PassArgument; }
76 /// getTypeInfo - Return the type_info object for the pass...
78 const std::type_info &getTypeInfo() const { return TypeInfo; }
80 /// getPassType - Return the PassType of a pass. Note that this can be
81 /// several different types or'd together. This is _strictly_ for use by opt,
82 /// analyze and llc for deciding which passes to use as command line options.
84 unsigned getPassType() const { return PassType; }
86 /// getNormalCtor - Return a pointer to a function, that when called, creates
87 /// an instance of the pass and returns it. This pointer may be null if there
88 /// is no default constructor for the pass.
90 Pass *(*getNormalCtor() const)() {
93 void setNormalCtor(Pass *(*Ctor)()) {
97 /// createPass() - Use this method to create an instance of this pass.
98 Pass *createPass() const {
99 assert((PassType != AnalysisGroup || NormalCtor) &&
100 "No default implementation found for analysis group!");
102 "Cannot call createPass on PassInfo without default ctor!");
106 /// getTargetCtor - Return a pointer to a function that creates an instance of
107 /// the pass and returns it. This returns a constructor for a version of the
108 /// pass that takes a TargetMachine object as a parameter.
110 Pass *(*getTargetCtor() const)(TargetMachine &) {
114 /// addInterfaceImplemented - This method is called when this pass is
115 /// registered as a member of an analysis group with the RegisterAnalysisGroup
118 void addInterfaceImplemented(const PassInfo *ItfPI) {
119 ItfImpl.push_back(ItfPI);
122 /// getInterfacesImplemented - Return a list of all of the analysis group
123 /// interfaces implemented by this pass.
125 const std::vector<const PassInfo*> &getInterfacesImplemented() const {
131 //===---------------------------------------------------------------------------
132 /// RegisterPass<t> template - This template class is used to notify the system
133 /// that a Pass is available for use, and registers it into the internal
134 /// database maintained by the PassManager. Unless this template is used, opt,
135 /// for example will not be able to see the pass and attempts to create the pass
136 /// will fail. This template is used in the follow manner (at global scope, in
139 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
141 /// This statement will cause your pass to be created by calling the default
142 /// constructor exposed by the pass. If you have a different constructor that
143 /// must be called, create a global constructor function (which takes the
144 /// arguments you need and returns a Pass*) and register your pass like this:
146 /// Pass *createMyPass(foo &opt) { return new MyPass(opt); }
147 /// static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
149 struct RegisterPassBase {
150 /// getPassInfo - Get the pass info for the registered class...
152 const PassInfo *getPassInfo() const { return PIObj; }
154 RegisterPassBase() : PIObj(0) {}
155 ~RegisterPassBase() { // Intentionally non-virtual...
156 if (PIObj) unregisterPass(PIObj);
160 PassInfo *PIObj; // The PassInfo object for this pass
161 void registerPass(PassInfo *);
162 void unregisterPass(PassInfo *);
164 /// setOnlyUsesCFG - Notice that this pass only depends on the CFG, so
165 /// transformations that do not modify the CFG do not invalidate this pass.
167 void setOnlyUsesCFG();
170 template<typename PassName>
171 Pass *callDefaultCtor() { return new PassName(); }
173 template<typename PassName>
174 struct RegisterPass : public RegisterPassBase {
176 // Register Pass using default constructor...
177 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
178 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
179 callDefaultCtor<PassName>));
182 // Register Pass using default constructor explicitly...
183 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
185 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor));
188 // Register Pass using TargetMachine constructor...
189 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
190 Pass *(*targetctor)(TargetMachine &)) {
191 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
195 // Generic constructor version that has an unknown ctor type...
196 template<typename CtorType>
197 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
199 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0));
203 /// RegisterOpt - Register something that is to show up in Opt, this is just a
204 /// shortcut for specifying RegisterPass...
206 template<typename PassName>
207 struct RegisterOpt : public RegisterPassBase {
208 RegisterOpt(const char *PassArg, const char *Name, bool CFGOnly = false) {
209 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
210 PassInfo::Optimization,
211 callDefaultCtor<PassName>));
212 if (CFGOnly) setOnlyUsesCFG();
215 /// Register Pass using default constructor explicitly...
217 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)(),
218 bool CFGOnly = false) {
219 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
220 PassInfo::Optimization, ctor));
221 if (CFGOnly) setOnlyUsesCFG();
224 /// Register FunctionPass using default constructor explicitly...
226 RegisterOpt(const char *PassArg, const char *Name, FunctionPass *(*ctor)(),
227 bool CFGOnly = false) {
228 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
229 PassInfo::Optimization,
230 static_cast<Pass*(*)()>(ctor)));
231 if (CFGOnly) setOnlyUsesCFG();
234 /// Register Pass using TargetMachine constructor...
236 RegisterOpt(const char *PassArg, const char *Name,
237 Pass *(*targetctor)(TargetMachine &), bool CFGOnly = false) {
238 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
239 PassInfo::Optimization, 0, targetctor));
240 if (CFGOnly) setOnlyUsesCFG();
243 /// Register FunctionPass using TargetMachine constructor...
245 RegisterOpt(const char *PassArg, const char *Name,
246 FunctionPass *(*targetctor)(TargetMachine &),
247 bool CFGOnly = false) {
248 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
249 PassInfo::Optimization, 0,
250 static_cast<Pass*(*)(TargetMachine&)>(targetctor)));
251 if (CFGOnly) setOnlyUsesCFG();
255 /// RegisterAnalysis - Register something that is to show up in Analysis, this
256 /// is just a shortcut for specifying RegisterPass... Analyses take a special
257 /// argument that, when set to true, tells the system that the analysis ONLY
258 /// depends on the shape of the CFG, so if a transformation preserves the CFG
259 /// that the analysis is not invalidated.
261 template<typename PassName>
262 struct RegisterAnalysis : public RegisterPassBase {
263 RegisterAnalysis(const char *PassArg, const char *Name,
264 bool CFGOnly = false) {
265 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
267 callDefaultCtor<PassName>));
268 if (CFGOnly) setOnlyUsesCFG();
272 /// RegisterLLC - Register something that is to show up in LLC, this is just a
273 /// shortcut for specifying RegisterPass...
275 template<typename PassName>
276 struct RegisterLLC : public RegisterPassBase {
277 RegisterLLC(const char *PassArg, const char *Name) {
278 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
280 callDefaultCtor<PassName>));
283 /// Register Pass using default constructor explicitly...
285 RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
286 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
287 PassInfo::LLC, ctor));
290 /// Register Pass using TargetMachine constructor...
292 RegisterLLC(const char *PassArg, const char *Name,
293 Pass *(*datactor)(TargetMachine &)) {
294 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
300 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
301 /// Analysis groups are used to define an interface (which need not derive from
302 /// Pass) that is required by passes to do their job. Analysis Groups differ
303 /// from normal analyses because any available implementation of the group will
304 /// be used if it is available.
306 /// If no analysis implementing the interface is available, a default
307 /// implementation is created and added. A pass registers itself as the default
308 /// implementation by specifying 'true' as the third template argument of this
311 /// In addition to registering itself as an analysis group member, a pass must
312 /// register itself normally as well. Passes may be members of multiple groups
313 /// and may still be "required" specifically by name.
315 /// The actual interface may also be registered as well (by not specifying the
316 /// second template argument). The interface should be registered to associate
317 /// a nice name with the interface.
319 class RegisterAGBase : public RegisterPassBase {
320 PassInfo *InterfaceInfo;
321 const PassInfo *ImplementationInfo;
322 bool isDefaultImplementation;
324 RegisterAGBase(const std::type_info &Interface,
325 const std::type_info *Pass = 0,
326 bool isDefault = false);
327 void setGroupName(const char *Name);
333 template<typename Interface, typename DefaultImplementationPass = void,
334 bool Default = false>
335 struct RegisterAnalysisGroup : public RegisterAGBase {
336 RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface),
337 &typeid(DefaultImplementationPass),
342 /// Define a specialization of RegisterAnalysisGroup that is used to set the
343 /// name for the analysis group.
345 template<typename Interface>
346 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase {
347 RegisterAnalysisGroup(const char *Name)
348 : RegisterAGBase(typeid(Interface)) {
355 //===---------------------------------------------------------------------------
356 /// PassRegistrationListener class - This class is meant to be derived from by
357 /// clients that are interested in which passes get registered and unregistered
358 /// at runtime (which can be because of the RegisterPass constructors being run
359 /// as the program starts up, or may be because a shared object just got
360 /// loaded). Deriving from the PassRegistationListener class automatically
361 /// registers your object to receive callbacks indicating when passes are loaded
364 struct PassRegistrationListener {
366 /// PassRegistrationListener ctor - Add the current object to the list of
367 /// PassRegistrationListeners...
368 PassRegistrationListener();
370 /// dtor - Remove object from list of listeners...
372 virtual ~PassRegistrationListener();
374 /// Callback functions - These functions are invoked whenever a pass is loaded
375 /// or removed from the current executable.
377 virtual void passRegistered(const PassInfo *P) {}
378 virtual void passUnregistered(const PassInfo *P) {}
380 /// enumeratePasses - Iterate over the registered passes, calling the
381 /// passEnumerate callback on each PassInfo object.
383 void enumeratePasses();
385 /// passEnumerate - Callback function invoked when someone calls
386 /// enumeratePasses on this PassRegistrationListener object.
388 virtual void passEnumerate(const PassInfo *P) {}
392 //===---------------------------------------------------------------------------
393 /// IncludeFile class - This class is used as a hack to make sure that the
394 /// implementation of a header file is included into a tool that uses the
395 /// header. This is solely to overcome problems linking .a files and not
396 /// getting the implementation of passes we need.
402 } // End llvm namespace