1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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
25 #include "llvm/Config/config.h"
31 //===---------------------------------------------------------------------------
32 /// PassInfo class - An instance of this class exists for every pass known by
33 /// the system, and can be obtained from a live Pass by calling its
34 /// getPassInfo() method. These objects are set up by the RegisterPass<>
35 /// template, defined below.
38 struct StaticPassInfo {
39 typedef Pass* (*NormalCtor_t)();
40 struct InterfaceInfo {
41 const StaticPassInfo *interface;
42 const InterfaceInfo *next;
45 const char *PassName; // Nice name for Pass
46 const char *PassArgument; // Command Line argument to run this pass
48 bool IsCFGOnlyPass; // Pass only looks at the CFG.
49 bool IsAnalysis; // True if an analysis pass.
50 bool IsAnalysisGroup; // True if an analysis group.
51 InterfaceInfo *ItfImpl;// Interfaces implemented by this pass
53 NormalCtor_t NormalCtor;
55 /// getPassName - Return the friendly name for the pass, never returns null
57 const char *getPassName() const { return PassName; }
59 /// getPassArgument - Return the command line option that may be passed to
60 /// 'opt' that will cause this pass to be run. This will return null if there
63 const char *getPassArgument() const { return PassArgument; }
65 /// getTypeInfo - Return the id object for the pass...
67 intptr_t getTypeInfo() const { return PassID; }
69 /// Return true if this PassID implements the specified ID pointer.
70 bool isPassID(void *IDPtr) const {
71 return PassID == (intptr_t)IDPtr;
74 /// isAnalysisGroup - Return true if this is an analysis group, not a normal
77 bool isAnalysisGroup() const { return IsAnalysisGroup; }
78 bool isAnalysis() const { return IsAnalysis; }
80 /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
82 bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
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 NormalCtor_t getNormalCtor() const {
91 void setNormalCtor(NormalCtor_t Ctor) {
95 /// addInterfaceImplemented - This method is called when this pass is
96 /// registered as a member of an analysis group with the RegisterAnalysisGroup
99 void addInterfaceImplemented(const StaticPassInfo *ItfPI) {
100 InterfaceInfo *NewInfo = new InterfaceInfo();
101 NewInfo->interface = ItfPI;
102 NewInfo->next = ItfImpl;
106 /// getInterfacesImplemented - Return a list of all of the analysis group
107 /// interfaces implemented by this pass.
109 const InterfaceInfo *getInterfacesImplemented() const {
113 /// createPass() - Use this method to create an instance of this pass.
114 Pass *createPass() const;
117 class PassInfo : public StaticPassInfo {
119 /// PassInfo ctor - Do not call this directly, this should only be invoked
120 /// through RegisterPass.
121 PassInfo(const char *name, const char *arg, intptr_t pi,
122 NormalCtor_t normal = 0,
123 bool isCFGOnly = false, bool is_analysis = false) {
124 this->PassName = name;
125 this->PassArgument = arg;
127 this->IsCFGOnlyPass = isCFGOnly;
128 this->IsAnalysis = is_analysis;
129 this->IsAnalysisGroup = false;
130 this->NormalCtor = normal;
133 /// PassInfo ctor - Do not call this directly, this should only be invoked
134 /// through RegisterPass. This version is for use by analysis groups; it
135 /// does not auto-register the pass.
136 PassInfo(const char *name, intptr_t pi) {
137 this->PassName = name;
138 this->PassArgument = "";
140 this->IsCFGOnlyPass = false;
141 this->IsAnalysis = false;
142 this->IsAnalysisGroup = true;
143 this->NormalCtor = 0;
148 void unregisterPass();
151 void operator=(const PassInfo &); // do not implement
152 PassInfo(const PassInfo &); // do not implement
156 template<typename PassName>
157 Pass *callDefaultCtor() { return new PassName(); }
159 //===---------------------------------------------------------------------------
160 /// RegisterPass<t> template - This template class is used to notify the system
161 /// that a Pass is available for use, and registers it into the internal
162 /// database maintained by the PassManager. Unless this template is used, opt,
163 /// for example will not be able to see the pass and attempts to create the pass
164 /// will fail. This template is used in the follow manner (at global scope, in
167 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
169 /// This statement will cause your pass to be created by calling the default
170 /// constructor exposed by the pass. If you have a different constructor that
171 /// must be called, create a global constructor function (which takes the
172 /// arguments you need and returns a Pass*) and register your pass like this:
174 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
176 template<typename passName>
177 struct RegisterPass : public PassInfo {
179 // Register Pass using default constructor...
180 RegisterPass(const char *PassArg, const char *Name, bool CFGOnly = false,
181 bool is_analysis = false)
182 : PassInfo(Name, PassArg, intptr_t(&passName::ID),
183 PassInfo::NormalCtor_t(callDefaultCtor<passName>),
184 CFGOnly, is_analysis) {
189 /// RegisterAnalysisGroup - Register a Pass as a member of an analysis _group_.
190 /// Analysis groups are used to define an interface (which need not derive from
191 /// Pass) that is required by passes to do their job. Analysis Groups differ
192 /// from normal analyses because any available implementation of the group will
193 /// be used if it is available.
195 /// If no analysis implementing the interface is available, a default
196 /// implementation is created and added. A pass registers itself as the default
197 /// implementation by specifying 'true' as the second template argument of this
200 /// In addition to registering itself as an analysis group member, a pass must
201 /// register itself normally as well. Passes may be members of multiple groups
202 /// and may still be "required" specifically by name.
204 /// The actual interface may also be registered as well (by not specifying the
205 /// second template argument). The interface should be registered to associate
206 /// a nice name with the interface.
208 class RegisterAGBase : public PassInfo {
210 RegisterAGBase(const char *Name,
211 intptr_t InterfaceID,
213 bool isDefault = false);
216 template<typename Interface, bool Default = false>
217 struct RegisterAnalysisGroup : public RegisterAGBase {
218 explicit RegisterAnalysisGroup(PassInfo &RPB)
219 : RegisterAGBase(RPB.getPassName(),
220 intptr_t(&Interface::ID), RPB.getTypeInfo(),
224 explicit RegisterAnalysisGroup(const char *Name)
225 : RegisterAGBase(Name, intptr_t(&Interface::ID)) {
231 //===---------------------------------------------------------------------------
232 /// PassRegistrationListener class - This class is meant to be derived from by
233 /// clients that are interested in which passes get registered and unregistered
234 /// at runtime (which can be because of the RegisterPass constructors being run
235 /// as the program starts up, or may be because a shared object just got
236 /// loaded). Deriving from the PassRegistationListener class automatically
237 /// registers your object to receive callbacks indicating when passes are loaded
240 struct PassRegistrationListener {
242 /// PassRegistrationListener ctor - Add the current object to the list of
243 /// PassRegistrationListeners...
244 PassRegistrationListener();
246 /// dtor - Remove object from list of listeners...
248 virtual ~PassRegistrationListener();
250 /// Callback functions - These functions are invoked whenever a pass is loaded
251 /// or removed from the current executable.
253 virtual void passRegistered(const StaticPassInfo *) {}
255 /// enumeratePasses - Iterate over the registered passes, calling the
256 /// passEnumerate callback on each PassInfo object.
258 void enumeratePasses();
260 /// passEnumerate - Callback function invoked when someone calls
261 /// enumeratePasses on this PassRegistrationListener object.
263 virtual void passEnumerate(const StaticPassInfo *) {}
267 } // End llvm namespace