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!
28 //===---------------------------------------------------------------------------
29 /// PassInfo class - An instance of this class exists for every pass known by
30 /// the system, and can be obtained from a live Pass by calling its
31 /// getPassInfo() method. These objects are set up by the RegisterPass<>
32 /// template, defined below.
35 const char *PassName; // Nice name for Pass
36 const char *PassArgument; // Command Line argument to run this pass
37 const std::type_info &TypeInfo; // type_info object for this Pass class
38 unsigned char PassType; // Set of enums values below...
39 std::vector<const PassInfo*> ItfImpl;// Interfaces implemented by this pass
41 Pass *(*NormalCtor)(); // No argument ctor
42 Pass *(*TargetCtor)(TargetMachine&); // Ctor taking TargetMachine object...
45 /// PassType - Define symbolic constants that can be used to test to see if
46 /// this pass should be listed by analyze or opt. Passes can use none, one or
47 /// many of these flags or'd together. It is not legal to combine the
48 /// AnalysisGroup flag with others.
51 Analysis = 1, Optimization = 2, LLC = 4, AnalysisGroup = 8
54 /// PassInfo ctor - Do not call this directly, this should only be invoked
55 /// through RegisterPass.
56 PassInfo(const char *name, const char *arg, const std::type_info &ti,
57 unsigned pt, Pass *(*normal)() = 0,
58 Pass *(*targetctor)(TargetMachine &) = 0)
59 : PassName(name), PassArgument(arg), TypeInfo(ti), PassType(pt),
60 NormalCtor(normal), TargetCtor(targetctor) {
63 /// getPassName - Return the friendly name for the pass, never returns null
65 const char *getPassName() const { return PassName; }
66 void setPassName(const char *Name) { PassName = Name; }
68 /// getPassArgument - Return the command line option that may be passed to
69 /// 'opt' that will cause this pass to be run. This will return null if there
72 const char *getPassArgument() const { return PassArgument; }
74 /// getTypeInfo - Return the type_info object for the pass...
76 const std::type_info &getTypeInfo() const { return TypeInfo; }
78 /// getPassType - Return the PassType of a pass. Note that this can be
79 /// several different types or'd together. This is _strictly_ for use by opt,
80 /// analyze and llc for deciding which passes to use as command line options.
82 unsigned getPassType() const { return PassType; }
84 /// getNormalCtor - Return a pointer to a function, that when called, creates
85 /// an instance of the pass and returns it. This pointer may be null if there
86 /// is no default constructor for the pass.
88 Pass *(*getNormalCtor() const)() {
91 void setNormalCtor(Pass *(*Ctor)()) {
95 /// createPass() - Use this method to create an instance of this pass.
96 Pass *createPass() const {
97 assert((PassType != AnalysisGroup || NormalCtor) &&
98 "No default implementation found for analysis group!");
100 "Cannot call createPass on PassInfo without default ctor!");
104 /// getTargetCtor - Return a pointer to a function that creates an instance of
105 /// the pass and returns it. This returns a constructor for a version of the
106 /// pass that takes a TargetMachine object as a parameter.
108 Pass *(*getTargetCtor() const)(TargetMachine &) {
112 /// addInterfaceImplemented - This method is called when this pass is
113 /// registered as a member of an analysis group with the RegisterAnalysisGroup
116 void addInterfaceImplemented(const PassInfo *ItfPI) {
117 ItfImpl.push_back(ItfPI);
120 /// getInterfacesImplemented - Return a list of all of the analysis group
121 /// interfaces implemented by this pass.
123 const std::vector<const PassInfo*> &getInterfacesImplemented() const {
129 //===---------------------------------------------------------------------------
130 /// RegisterPass<t> template - This template class is used to notify the system
131 /// that a Pass is available for use, and registers it into the internal
132 /// database maintained by the PassManager. Unless this template is used, opt,
133 /// for example will not be able to see the pass and attempts to create the pass
134 /// will fail. This template is used in the follow manner (at global scope, in
137 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
139 /// This statement will cause your pass to be created by calling the default
140 /// constructor exposed by the pass. If you have a different constructor that
141 /// must be called, create a global constructor function (which takes the
142 /// arguments you need and returns a Pass*) and register your pass like this:
144 /// Pass *createMyPass(foo &opt) { return new MyPass(opt); }
145 /// static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
147 struct RegisterPassBase {
148 /// getPassInfo - Get the pass info for the registered class...
150 const PassInfo *getPassInfo() const { return PIObj; }
152 RegisterPassBase() : PIObj(0) {}
153 ~RegisterPassBase() { // Intentionally non-virtual...
154 if (PIObj) unregisterPass(PIObj);
158 PassInfo *PIObj; // The PassInfo object for this pass
159 void registerPass(PassInfo *);
160 void unregisterPass(PassInfo *);
162 /// setOnlyUsesCFG - Notice that this pass only depends on the CFG, so
163 /// transformations that do not modify the CFG do not invalidate this pass.
165 void setOnlyUsesCFG();
168 template<typename PassName>
169 Pass *callDefaultCtor() { return new PassName(); }
171 template<typename PassName>
172 struct RegisterPass : public RegisterPassBase {
174 // Register Pass using default constructor...
175 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy = 0) {
176 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
177 callDefaultCtor<PassName>));
180 // Register Pass using default constructor explicitly...
181 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
183 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, ctor));
186 // Register Pass using TargetMachine constructor...
187 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
188 Pass *(*targetctor)(TargetMachine &)) {
189 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy,
193 // Generic constructor version that has an unknown ctor type...
194 template<typename CtorType>
195 RegisterPass(const char *PassArg, const char *Name, unsigned PassTy,
197 registerPass(new PassInfo(Name, PassArg, typeid(PassName), PassTy, 0));
201 /// RegisterOpt - Register something that is to show up in Opt, this is just a
202 /// shortcut for specifying RegisterPass...
204 template<typename PassName>
205 struct RegisterOpt : public RegisterPassBase {
206 RegisterOpt(const char *PassArg, const char *Name, bool CFGOnly = false) {
207 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
208 PassInfo::Optimization,
209 callDefaultCtor<PassName>));
210 if (CFGOnly) setOnlyUsesCFG();
213 /// Register Pass using default constructor explicitly...
215 RegisterOpt(const char *PassArg, const char *Name, Pass *(*ctor)(),
216 bool CFGOnly = false) {
217 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
218 PassInfo::Optimization, ctor));
219 if (CFGOnly) setOnlyUsesCFG();
222 /// Register Pass using TargetMachine constructor...
224 RegisterOpt(const char *PassArg, const char *Name,
225 Pass *(*targetctor)(TargetMachine &), bool CFGOnly = false) {
226 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
227 PassInfo::Optimization, 0, targetctor));
228 if (CFGOnly) setOnlyUsesCFG();
232 /// RegisterAnalysis - Register something that is to show up in Analysis, this
233 /// is just a shortcut for specifying RegisterPass... Analyses take a special
234 /// argument that, when set to true, tells the system that the analysis ONLY
235 /// depends on the shape of the CFG, so if a transformation preserves the CFG
236 /// that the analysis is not invalidated.
238 template<typename PassName>
239 struct RegisterAnalysis : public RegisterPassBase {
240 RegisterAnalysis(const char *PassArg, const char *Name,
241 bool CFGOnly = false) {
242 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
244 callDefaultCtor<PassName>));
245 if (CFGOnly) setOnlyUsesCFG();
249 /// RegisterLLC - Register something that is to show up in LLC, this is just a
250 /// shortcut for specifying RegisterPass...
252 template<typename PassName>
253 struct RegisterLLC : public RegisterPassBase {
254 RegisterLLC(const char *PassArg, const char *Name) {
255 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
257 callDefaultCtor<PassName>));
260 /// Register Pass using default constructor explicitly...
262 RegisterLLC(const char *PassArg, const char *Name, Pass *(*ctor)()) {
263 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
264 PassInfo::LLC, ctor));
267 /// Register Pass using TargetMachine constructor...
269 RegisterLLC(const char *PassArg, const char *Name,
270 Pass *(*datactor)(TargetMachine &)) {
271 registerPass(new PassInfo(Name, PassArg, typeid(PassName),
277 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
278 /// Analysis groups are used to define an interface (which need not derive from
279 /// Pass) that is required by passes to do their job. Analysis Groups differ
280 /// from normal analyses because any available implementation of the group will
281 /// be used if it is available.
283 /// If no analysis implementing the interface is available, a default
284 /// implementation is created and added. A pass registers itself as the default
285 /// implementation by specifying 'true' as the third template argument of this
288 /// In addition to registering itself as an analysis group member, a pass must
289 /// register itself normally as well. Passes may be members of multiple groups
290 /// and may still be "required" specifically by name.
292 /// The actual interface may also be registered as well (by not specifying the
293 /// second template argument). The interface should be registered to associate
294 /// a nice name with the interface.
296 class RegisterAGBase : public RegisterPassBase {
297 PassInfo *InterfaceInfo;
298 const PassInfo *ImplementationInfo;
299 bool isDefaultImplementation;
301 RegisterAGBase(const std::type_info &Interface,
302 const std::type_info *Pass = 0,
303 bool isDefault = false);
304 void setGroupName(const char *Name);
310 template<typename Interface, typename DefaultImplementationPass = void,
311 bool Default = false>
312 struct RegisterAnalysisGroup : public RegisterAGBase {
313 RegisterAnalysisGroup() : RegisterAGBase(typeid(Interface),
314 &typeid(DefaultImplementationPass),
319 /// Define a specialization of RegisterAnalysisGroup that is used to set the
320 /// name for the analysis group.
322 template<typename Interface>
323 struct RegisterAnalysisGroup<Interface, void, false> : public RegisterAGBase {
324 RegisterAnalysisGroup(const char *Name)
325 : RegisterAGBase(typeid(Interface)) {
332 //===---------------------------------------------------------------------------
333 /// PassRegistrationListener class - This class is meant to be derived from by
334 /// clients that are interested in which passes get registered and unregistered
335 /// at runtime (which can be because of the RegisterPass constructors being run
336 /// as the program starts up, or may be because a shared object just got
337 /// loaded). Deriving from the PassRegistationListener class automatically
338 /// registers your object to receive callbacks indicating when passes are loaded
341 struct PassRegistrationListener {
343 /// PassRegistrationListener ctor - Add the current object to the list of
344 /// PassRegistrationListeners...
345 PassRegistrationListener();
347 /// dtor - Remove object from list of listeners...
349 virtual ~PassRegistrationListener();
351 /// Callback functions - These functions are invoked whenever a pass is loaded
352 /// or removed from the current executable.
354 virtual void passRegistered(const PassInfo *P) {}
355 virtual void passUnregistered(const PassInfo *P) {}
357 /// enumeratePasses - Iterate over the registered passes, calling the
358 /// passEnumerate callback on each PassInfo object.
360 void enumeratePasses();
362 /// passEnumerate - Callback function invoked when someone calls
363 /// enumeratePasses on this PassRegistrationListener object.
365 virtual void passEnumerate(const PassInfo *P) {}
369 //===---------------------------------------------------------------------------
370 /// IncludeFile class - This class is used as a hack to make sure that the
371 /// implementation of a header file is included into a tool that uses the
372 /// header. This is solely to overcome problems linking .a files and not
373 /// getting the implementation of passes we need.