Break part of Pass.h out into PassAnalysisSupport.h
[oota-llvm.git] / include / llvm / PassSupport.h
1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
2 //
3 // This file defines stuff that is used to define and "use" Passes.  This file
4 // is automatically #included by Pass.h, so:
5 //
6 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
7 //
8 // Instead, #include Pass.h.
9 //
10 // This file defines Pass registration code and classes used for it.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_PASS_SUPPORT_H
15 #define LLVM_PASS_SUPPORT_H
16
17 // No need to include Pass.h, we are being included by it!
18
19 #include <typeinfo>
20 class TargetData;
21
22 //===---------------------------------------------------------------------------
23 // PassInfo class - An instance of this class exists for every pass known by the
24 // system, and can be obtained from a live Pass by calling its getPassInfo()
25 // method.  These objects are set up by the RegisterPass<> template, defined
26 // below.
27 //
28 class PassInfo {
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
33   Pass *(*NormalCtor)();               // No argument ctor
34   Pass *(*DataCtor)(const TargetData&);// Ctor taking TargetData object...
35
36 public:
37   // PassInfo ctor - Do not call this directly, this should only be invoked
38   // through RegisterPass.
39   PassInfo(const char *name, const char *arg, const std::type_info &ti, 
40            Pass *(*normal)(), Pass *(*data)(const TargetData &))
41     : PassName(name), PassArgument(arg), TypeInfo(ti), NormalCtor(normal), 
42       DataCtor(data) {
43   }
44
45   // getPassName - Return the friendly name for the pass, never returns null
46   const char *getPassName() const { return PassName; }
47
48   // getPassArgument - Return the command line option that may be passed to
49   // 'opt' that will cause this pass to be run.  This will return null if there
50   // is no argument.
51   //
52   const char *getPassArgument() const { return PassArgument; }
53
54   // getTypeInfo - Return the type_info object for the pass...
55   const std::type_info &getTypeInfo() const { return TypeInfo; }
56
57   // getNormalCtor - Return a pointer to a function, that when called, creates
58   // an instance of the pass and returns it.  This pointer may be null if there
59   // is no default constructor for the pass.
60   
61   Pass *(*getNormalCtor() const)() {
62     return NormalCtor;
63   }
64
65   // getDataCtor - Return a pointer to a function that creates an instance of
66   // the pass and returns it.  This returns a constructor for a version of the
67   // pass that takes a TArgetData object as a parameter.
68   //
69   Pass *(*getDataCtor() const)(const TargetData &) {
70     return DataCtor;
71   }
72 };
73
74
75 //===---------------------------------------------------------------------------
76 // RegisterPass<t> template - This template class is used to notify the system
77 // that a Pass is available for use, and registers it into the internal database
78 // maintained by the PassManager.  Unless this template is used, opt, for
79 // example will not be able to see the pass and attempts to create the pass will
80 // fail. This template is used in the follow manner (at global scope, in your
81 // .cpp file):
82 // 
83 // static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
84 //
85 // This statement will cause your pass to be created by calling the default
86 // constructor exposed by the pass.  If you have a different constructor that
87 // must be called, create a global constructor function (which takes the
88 // arguments you need and returns a Pass*) and register your pass like this:
89 //
90 // Pass *createMyPass(foo &opt) { return new MyPass(opt); }
91 // static RegisterPass<PassClassName> tmp("passopt", "My Name", createMyPass);
92 // 
93 struct RegisterPassBase {
94   // getPassInfo - Get the pass info for the registered class...
95   const PassInfo *getPassInfo() const { return PIObj; }
96
97   ~RegisterPassBase();   // Intentionally non-virtual...
98
99 protected:
100   PassInfo *PIObj;       // The PassInfo object for this pass
101   void registerPass(PassInfo *);
102 };
103
104 template<typename PassName>
105 Pass *callDefaultCtor() { return new PassName(); }
106
107 template<typename PassName>
108 struct RegisterPass : public RegisterPassBase {
109   
110   // Register Pass using default constructor...
111   RegisterPass(const char *PassArg, const char *Name) {
112     registerPass(new PassInfo(Name, PassArg, typeid(PassName),
113                               callDefaultCtor<PassName>, 0));
114   }
115
116   // Register Pass using default constructor explicitly...
117   RegisterPass(const char *PassArg, const char *Name,
118                Pass *(*ctor)()) {
119     registerPass(new PassInfo(Name, PassArg, typeid(PassName), ctor, 0));
120   }
121
122   // Register Pass using TargetData constructor...
123   RegisterPass(const char *PassArg, const char *Name,
124                Pass *(*datactor)(const TargetData &)) {
125     registerPass(new PassInfo(Name, PassArg, typeid(PassName), 0, datactor));
126   }
127
128   // Generic constructor version that has an unknown ctor type...
129   template<typename CtorType>
130   RegisterPass(const char *PassArg, const char *Name, CtorType *Fn) {
131     registerPass(new PassInfo(Name, PassArg, typeid(PassName), 0, 0));
132   }
133 };
134
135
136 //===---------------------------------------------------------------------------
137 // PassRegistrationListener class - This class is meant to be derived from by
138 // clients that are interested in which passes get registered and unregistered
139 // at runtime (which can be because of the RegisterPass constructors being run
140 // as the program starts up, or may be because a shared object just got loaded).
141 // Deriving from the PassRegistationListener class automatically registers your
142 // object to receive callbacks indicating when passes are loaded and removed.
143 //
144 struct PassRegistrationListener {
145
146   // PassRegistrationListener ctor - Add the current object to the list of
147   // PassRegistrationListeners...
148   PassRegistrationListener();
149
150   // dtor - Remove object from list of listeners...
151   virtual ~PassRegistrationListener();
152
153   // Callback functions - These functions are invoked whenever a pass is loaded
154   // or removed from the current executable.
155   //
156   virtual void passRegistered(const PassInfo *P) {}
157   virtual void passUnregistered(const PassInfo *P) {}
158
159   // enumeratePasses - Iterate over the registered passes, calling the
160   // passEnumerate callback on each PassInfo object.
161   //
162   void enumeratePasses();
163
164   // passEnumerate - Callback function invoked when someone calls
165   // enumeratePasses on this PassRegistrationListener object.
166   //
167   virtual void passEnumerate(const PassInfo *P) {}
168 };
169
170 #endif