Reapply r108794, a fix for the failing test from last time.
[oota-llvm.git] / include / llvm / PassSupport.h
1 //===- llvm/PassSupport.h - Pass Support code -------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines stuff that is used to define and "use" Passes.  This file
11 // is automatically #included by Pass.h, so:
12 //
13 //           NO .CPP FILES SHOULD INCLUDE THIS FILE DIRECTLY
14 //
15 // Instead, #include Pass.h.
16 //
17 // This file defines Pass registration code and classes used for it.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_PASS_SUPPORT_H
22 #define LLVM_PASS_SUPPORT_H
23
24 #include "Pass.h"
25 #include "llvm/Config/config.h"
26
27 namespace llvm {
28
29 class TargetMachine;
30
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.
36 ///
37
38 struct StaticPassInfo {
39   typedef Pass* (*NormalCtor_t)();
40   struct InterfaceInfo {
41     const StaticPassInfo *interface;
42     const InterfaceInfo *next;
43   };
44
45   const char *PassName;     // Nice name for Pass
46   const char *PassArgument; // Command Line argument to run this pass
47   intptr_t  PassID;      
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
52
53   NormalCtor_t NormalCtor;
54     
55   /// getPassName - Return the friendly name for the pass, never returns null
56   ///
57   const char *getPassName() const { return PassName; }
58
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
61   /// is no argument.
62   ///
63   const char *getPassArgument() const { return PassArgument; }
64
65   /// getTypeInfo - Return the id object for the pass...
66   /// TODO : Rename
67   intptr_t getTypeInfo() const { return PassID; }
68
69   /// Return true if this PassID implements the specified ID pointer.
70   bool isPassID(void *IDPtr) const {
71     return PassID == (intptr_t)IDPtr;
72   }
73
74   /// isAnalysisGroup - Return true if this is an analysis group, not a normal
75   /// pass.
76   ///
77   bool isAnalysisGroup() const { return IsAnalysisGroup; }
78   bool isAnalysis() const { return IsAnalysis; }
79
80   /// isCFGOnlyPass - return true if this pass only looks at the CFG for the
81   /// function.
82   bool isCFGOnlyPass() const { return IsCFGOnlyPass; }
83
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.
87   ///
88   NormalCtor_t getNormalCtor() const {
89     return NormalCtor;
90   }
91   void setNormalCtor(NormalCtor_t Ctor) {
92     NormalCtor = Ctor;
93   }
94
95   /// addInterfaceImplemented - This method is called when this pass is
96   /// registered as a member of an analysis group with the RegisterAnalysisGroup
97   /// template.
98   ///
99   void addInterfaceImplemented(const StaticPassInfo *ItfPI) {
100     InterfaceInfo *NewInfo = new InterfaceInfo();
101     NewInfo->interface = ItfPI;
102     NewInfo->next = ItfImpl;
103     ItfImpl = NewInfo;
104   }
105
106   /// getInterfacesImplemented - Return a list of all of the analysis group
107   /// interfaces implemented by this pass.
108   ///
109   const InterfaceInfo *getInterfacesImplemented() const {
110     return ItfImpl;
111   }
112
113   /// createPass() - Use this method to create an instance of this pass.
114   Pass *createPass() const;
115 };
116
117 class PassInfo : public StaticPassInfo {
118 public:
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;
126     this->PassID = pi;
127     this->IsCFGOnlyPass = isCFGOnly;
128     this->IsAnalysis = is_analysis;
129     this->IsAnalysisGroup = false;
130     this->NormalCtor = normal;
131     registerPass();
132   }
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 = "";
139     this->PassID = pi;
140     this->IsCFGOnlyPass = false;
141     this->IsAnalysis = false;
142     this->IsAnalysisGroup = true;
143     this->NormalCtor = 0;
144   }
145
146 protected:
147   void registerPass();
148   void unregisterPass();
149
150 private:
151   void operator=(const PassInfo &); // do not implement
152   PassInfo(const PassInfo &);       // do not implement
153 };
154
155
156 template<typename PassName>
157 Pass *callDefaultCtor() { return new PassName(); }
158
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
165 /// your .cpp file):
166 ///
167 /// static RegisterPass<YourPassClassName> tmp("passopt", "My Pass Name");
168 ///
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:
173 ///
174 /// static RegisterPass<PassClassName> tmp("passopt", "My Name");
175 ///
176 template<typename passName>
177 struct RegisterPass : public PassInfo {
178
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) {
185   }
186 };
187
188
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.
194 ///
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
198 /// class.
199 ///
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.
203 ///
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.
207 ///
208 class RegisterAGBase : public PassInfo {
209 protected:
210   RegisterAGBase(const char *Name,
211                  intptr_t InterfaceID,
212                  intptr_t PassID = 0,
213                  bool isDefault = false);
214 };
215
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(),
221                      Default) {
222   }
223
224   explicit RegisterAnalysisGroup(const char *Name)
225     : RegisterAGBase(Name, intptr_t(&Interface::ID)) {
226   }
227 };
228
229
230
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
238 /// and removed.
239 ///
240 struct PassRegistrationListener {
241
242   /// PassRegistrationListener ctor - Add the current object to the list of
243   /// PassRegistrationListeners...
244   PassRegistrationListener();
245
246   /// dtor - Remove object from list of listeners...
247   ///
248   virtual ~PassRegistrationListener();
249
250   /// Callback functions - These functions are invoked whenever a pass is loaded
251   /// or removed from the current executable.
252   ///
253   virtual void passRegistered(const StaticPassInfo *) {}
254
255   /// enumeratePasses - Iterate over the registered passes, calling the
256   /// passEnumerate callback on each PassInfo object.
257   ///
258   void enumeratePasses();
259
260   /// passEnumerate - Callback function invoked when someone calls
261   /// enumeratePasses on this PassRegistrationListener object.
262   ///
263   virtual void passEnumerate(const StaticPassInfo *) {}
264 };
265
266
267 } // End llvm namespace
268
269 #endif