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, AnalysisGroup = 4
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 char 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(const char *Name, const char *Arg, const std::type_info &TI,
155 unsigned char PT, Pass *(*Normal)() = 0,
156 Pass *(*TargetCtor)(TargetMachine &) = 0)
157 : PIObj(Name, Arg, TI, PT, Normal, TargetCtor) {
160 RegisterPassBase(const std::type_info &TI, unsigned char PT)
161 : PIObj("", "", TI, PT, 0, 0) {
162 // This ctor may only be used for analysis groups: it does not auto-register
164 assert(PT == PassInfo::AnalysisGroup && "Not an AnalysisGroup!");
167 ~RegisterPassBase() { // Intentionally non-virtual.
168 // Analysis groups are registered/unregistered by their dtor.
169 if (PIObj.getPassType() != PassInfo::AnalysisGroup)
174 PassInfo PIObj; // The PassInfo object for this pass
176 void unregisterPass();
178 /// setOnlyUsesCFG - Notice that this pass only depends on the CFG, so
179 /// transformations that do not modify the CFG do not invalidate this pass.
181 void setOnlyUsesCFG();
184 template<typename PassName>
185 Pass *callDefaultCtor() { return new PassName(); }
187 template<typename PassName>
188 struct RegisterPass : public RegisterPassBase {
190 // Register Pass using default constructor...
191 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy = 0)
192 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy,
193 callDefaultCtor<PassName>) {}
195 // Register Pass using default constructor explicitly...
196 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy,
198 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, ctor) {}
200 // Register Pass using TargetMachine constructor...
201 RegisterPass(const char *PassArg, const char *Name, unsigned char PassTy,
202 Pass *(*targetctor)(TargetMachine &))
203 : RegisterPassBase(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 char PassTy,
210 : RegisterPassBase(Name, PassArg, typeid(PassName), PassTy, 0) {}
213 /// RegisterOpt - Register something that is to show up in Opt, this is just a
214 /// shortcut for specifying RegisterPass...
216 template<typename PassName>
217 struct RegisterOpt : public RegisterPassBase {
218 RegisterOpt(const char *PassArg, const char *Name, bool CFGOnly = false)
219 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization,
220 callDefaultCtor<PassName>) {
221 if (CFGOnly) setOnlyUsesCFG();
224 /// Register Pass using default constructor explicitly...
226 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)(),
227 bool CFGOnly = false)
228 : RegisterPassBase(Name, PassArg, typeid(PassName),
229 PassInfo::Optimization, ctor) {
230 if (CFGOnly) setOnlyUsesCFG();
233 /// Register FunctionPass using default constructor explicitly...
235 RegisterOpt(const char *PassArg, const char *Name, FunctionPass *(*ctor)(),
236 bool CFGOnly = false)
237 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization,
238 static_cast<Pass*(*)()>(ctor)) {
239 if (CFGOnly) setOnlyUsesCFG();
242 /// Register Pass using TargetMachine constructor...
244 RegisterOpt(const char *PassArg, const char *Name,
245 Pass *(*targetctor)(TargetMachine &), bool CFGOnly = false)
246 : RegisterPassBase(Name, PassArg, typeid(PassName),
247 PassInfo::Optimization, 0, targetctor) {
248 if (CFGOnly) setOnlyUsesCFG();
251 /// Register FunctionPass using TargetMachine constructor...
253 RegisterOpt(const char *PassArg, const char *Name,
254 FunctionPass *(*targetctor)(TargetMachine &),
255 bool CFGOnly = false)
256 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Optimization, 0,
257 static_cast<Pass*(*)(TargetMachine&)>(targetctor)) {
258 if (CFGOnly) setOnlyUsesCFG();
262 /// RegisterAnalysis - Register something that is to show up in Analysis, this
263 /// is just a shortcut for specifying RegisterPass... Analyses take a special
264 /// argument that, when set to true, tells the system that the analysis ONLY
265 /// depends on the shape of the CFG, so if a transformation preserves the CFG
266 /// that the analysis is not invalidated.
268 template<typename PassName>
269 struct RegisterAnalysis : public RegisterPassBase {
270 RegisterAnalysis(const char *PassArg, const char *Name,
271 bool CFGOnly = false)
272 : RegisterPassBase(Name, PassArg, typeid(PassName), PassInfo::Analysis,
273 callDefaultCtor<PassName>) {
274 if (CFGOnly) setOnlyUsesCFG();
279 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
280 /// Analysis groups are used to define an interface (which need not derive from
281 /// Pass) that is required by passes to do their job. Analysis Groups differ
282 /// from normal analyses because any available implementation of the group will
283 /// be used if it is available.
285 /// If no analysis implementing the interface is available, a default
286 /// implementation is created and added. A pass registers itself as the default
287 /// implementation by specifying 'true' as the third template argument of this
290 /// In addition to registering itself as an analysis group member, a pass must
291 /// register itself normally as well. Passes may be members of multiple groups
292 /// and may still be "required" specifically by name.
294 /// The actual interface may also be registered as well (by not specifying the
295 /// second template argument). The interface should be registered to associate
296 /// a nice name with the interface.
298 class RegisterAGBase : public RegisterPassBase {
299 PassInfo *InterfaceInfo;
300 const PassInfo *ImplementationInfo;
301 bool isDefaultImplementation;
303 RegisterAGBase(const std::type_info &Interface,
304 const std::type_info *Pass = 0,
305 bool isDefault = false);
306 void setGroupName(const char *Name);
312 template<typename Interface, typename DefaultImplementationPass = void,
313 bool Default = false>
314 struct RegisterAnalysisGroup : public RegisterAGBase {
315 RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface),
316 &typeid(DefaultImplementationPass),
321 /// Define a specialization of RegisterAnalysisGroup that is used to set the
322 /// name for the analysis group.
324 template<typename Interface>
325 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase {
326 RegisterAnalysisGroup(const char *Name)
327 : RegisterAGBase(typeid(Interface)) {
334 //===---------------------------------------------------------------------------
335 /// PassRegistrationListener class - This class is meant to be derived from by
336 /// clients that are interested in which passes get registered and unregistered
337 /// at runtime (which can be because of the RegisterPass constructors being run
338 /// as the program starts up, or may be because a shared object just got
339 /// loaded). Deriving from the PassRegistationListener class automatically
340 /// registers your object to receive callbacks indicating when passes are loaded
343 struct PassRegistrationListener {
345 /// PassRegistrationListener ctor - Add the current object to the list of
346 /// PassRegistrationListeners...
347 PassRegistrationListener();
349 /// dtor - Remove object from list of listeners...
351 virtual ~PassRegistrationListener();
353 /// Callback functions - These functions are invoked whenever a pass is loaded
354 /// or removed from the current executable.
356 virtual void passRegistered(const PassInfo *P) {}
357 virtual void passUnregistered(const PassInfo *P) {}
359 /// enumeratePasses - Iterate over the registered passes, calling the
360 /// passEnumerate callback on each PassInfo object.
362 void enumeratePasses();
364 /// passEnumerate - Callback function invoked when someone calls
365 /// enumeratePasses on this PassRegistrationListener object.
367 virtual void passEnumerate(const PassInfo *P) {}
371 //===---------------------------------------------------------------------------
372 /// IncludeFile class - This class is used as a hack to make sure that the
373 /// implementation of a header file is included into a tool that uses the
374 /// header. This is solely to overcome problems linking .a files and not
375 /// getting the implementation of passes we need.
381 } // End llvm namespace