LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Pass.cpp
Go to the documentation of this file.
1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
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 implements the LLVM Pass infrastructure. It is primarily
11 // responsible with ensuring that passes are executed and batched together
12 // optimally.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Pass.h"
18 #include "llvm/PassRegistry.h"
19 #include "llvm/Support/Debug.h"
22 using namespace llvm;
23 
24 //===----------------------------------------------------------------------===//
25 // Pass Implementation
26 //
27 
28 // Force out-of-line virtual method.
30  delete Resolver;
31 }
32 
33 // Force out-of-line virtual method.
35 
37  const std::string &Banner) const {
38  return createPrintModulePass(&O, false, Banner);
39 }
40 
42  return PMT_ModulePassManager;
43 }
44 
45 bool Pass::mustPreserveAnalysisID(char &AID) const {
46  return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
47 }
48 
49 // dumpPassStructure - Implement the -debug-pass=Structure option
50 void Pass::dumpPassStructure(unsigned Offset) {
51  dbgs().indent(Offset*2) << getPassName() << "\n";
52 }
53 
54 /// getPassName - Return a nice clean name for a pass. This usually
55 /// implemented in terms of the name that is registered by one of the
56 /// Registration templates, but can be overloaded directly.
57 ///
58 const char *Pass::getPassName() const {
59  AnalysisID AID = getPassID();
61  if (PI)
62  return PI->getPassName();
63  return "Unnamed pass: implement Pass::getPassName()";
64 }
65 
67  // By default, don't do anything.
68 }
69 
71  // Default implementation.
72  return PMT_Unknown;
73 }
74 
76  // By default, no analysis results are used, all are invalidated.
77 }
78 
80  // By default, don't do anything.
81 }
82 
83 void Pass::verifyAnalysis() const {
84  // By default, don't do anything.
85 }
86 
88  return this;
89 }
90 
92  return 0;
93 }
94 
96  return 0;
97 }
98 
100  assert(!Resolver && "Resolver is already set");
101  Resolver = AR;
102 }
103 
104 // print - Print out the internal state of the pass. This is called by Analyze
105 // to print out the contents of an analysis. Otherwise it is not necessary to
106 // implement this method.
107 //
108 void Pass::print(raw_ostream &O,const Module*) const {
109  O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
110 }
111 
112 // dump - call print(cerr);
113 void Pass::dump() const {
114  print(dbgs(), 0);
115 }
116 
117 //===----------------------------------------------------------------------===//
118 // ImmutablePass Implementation
119 //
120 // Force out-of-line virtual method.
122 
124  // By default, don't do anything.
125 }
126 
127 //===----------------------------------------------------------------------===//
128 // FunctionPass Implementation
129 //
130 
132  const std::string &Banner) const {
133  return createPrintFunctionPass(Banner, &O);
134 }
135 
138 }
139 
140 //===----------------------------------------------------------------------===//
141 // BasicBlockPass Implementation
142 //
143 
145  const std::string &Banner) const {
146  return createPrintBasicBlockPass(&O, false, Banner);
147 }
148 
150  // By default, don't do anything.
151  return false;
152 }
153 
155  // By default, don't do anything.
156  return false;
157 }
158 
161 }
162 
163 const PassInfo *Pass::lookupPassInfo(const void *TI) {
165 }
166 
169 }
170 
173  if (!PI)
174  return NULL;
175  return PI->createPass();
176 }
177 
179  assert((!isAnalysisGroup() || NormalCtor) &&
180  "No default implementation found for analysis group!");
181  assert(NormalCtor &&
182  "Cannot call createPass on PassInfo without default ctor!");
183  return NormalCtor();
184 }
185 
186 //===----------------------------------------------------------------------===//
187 // Analysis Group Implementation Code
188 //===----------------------------------------------------------------------===//
189 
190 // RegisterAGBase implementation
191 //
192 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
193  const void *PassID, bool isDefault)
194  : PassInfo(Name, InterfaceID) {
196  *this, isDefault);
197 }
198 
199 //===----------------------------------------------------------------------===//
200 // PassRegistrationListener implementation
201 //
202 
203 // PassRegistrationListener ctor - Add the current object to the list of
204 // PassRegistrationListeners...
207 }
208 
209 // dtor - Remove object from list of listeners...
212 }
213 
214 // enumeratePasses - Iterate over the registered passes, calling the
215 // passEnumerate callback on each PassInfo object.
216 //
219 }
220 
222 
223 //===----------------------------------------------------------------------===//
224 // AnalysisUsage Class Implementation
225 //
226 
227 namespace {
228  struct GetCFGOnlyPasses : public PassRegistrationListener {
230  VectorType &CFGOnlyList;
231  GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
232 
233  void passEnumerate(const PassInfo *P) {
234  if (P->isCFGOnlyPass())
235  CFGOnlyList.push_back(P->getTypeInfo());
236  }
237  };
238 }
239 
240 // setPreservesCFG - This function should be called to by the pass, iff they do
241 // not:
242 //
243 // 1. Add or remove basic blocks from the function
244 // 2. Modify terminator instructions in any way.
245 //
246 // This function annotates the AnalysisUsage info object to say that analyses
247 // that only depend on the CFG are preserved by this pass.
248 //
250  // Since this transformation doesn't modify the CFG, it preserves all analyses
251  // that only depend on the CFG (like dominators, loop info, etc...)
252  GetCFGOnlyPasses(Preserved).enumeratePasses();
253 }
254 
256  const PassInfo *PI = Pass::lookupPassInfo(Arg);
257  // If the pass exists, preserve it. Otherwise silently do nothing.
258  if (PI) Preserved.push_back(PI->getTypeInfo());
259  return *this;
260 }
261 
263  Required.push_back(ID);
264  return *this;
265 }
266 
268  Required.push_back(&ID);
269  return *this;
270 }
271 
273  Required.push_back(&ID);
274  RequiredTransitive.push_back(&ID);
275  return *this;
276 }
void push_back(const T &Elt)
Definition: SmallVector.h:236
void registerAnalysisGroup(const void *InterfaceID, const void *PassID, PassInfo &Registeree, bool isDefault, bool ShouldFree=false)
registerAnalysisGroup - Register an analysis group (or a pass implementing
PassManagerType
Definition: Pass.h:55
BBPassManager.
Definition: Pass.h:62
AnalysisUsage & addPreserved()
static PassRegistry * getPassRegistry()
void enumerateWith(PassRegistrationListener *L)
const char * getPassName() const
Definition: PassSupport.h:72
virtual PMDataManager * getAsPMDataManager()
Definition: Pass.cpp:95
virtual void dumpPassStructure(unsigned Offset=0)
Definition: Pass.cpp:50
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
AnalysisID getPassID() const
getPassID - Return the PassID number that corresponds to this pass.
Definition: Pass.h:103
virtual void releaseMemory()
Definition: Pass.cpp:79
virtual void verifyAnalysis() const
Definition: Pass.cpp:83
virtual void getAnalysisUsage(AnalysisUsage &) const
Definition: Pass.cpp:75
virtual bool doInitialization(Function &)
Definition: Pass.cpp:149
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
virtual const char * getPassName() const
Definition: Pass.cpp:58
bool isCFGOnlyPass() const
Definition: PassSupport.h:97
const void * getTypeInfo() const
Definition: PassSupport.h:82
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const
createPrinterPass - Get a module printer pass.
Definition: Pass.cpp:36
ModulePass * createPrintModulePass(raw_ostream *OS, bool DeleteStream=false, const std::string &Banner="")
virtual void preparePassManager(PMStack &)
Check if available pass managers are suitable for this pass or not.
Definition: Pass.cpp:66
virtual void initializePass()
Definition: Pass.cpp:123
void setResolver(AnalysisResolver *AR)
Definition: Pass.cpp:99
virtual ~ImmutablePass()
Definition: Pass.cpp:121
bool isAnalysisGroup() const
Definition: PassSupport.h:92
void removeRegistrationListener(PassRegistrationListener *L)
bool mustPreserveAnalysisID(char &AID) const
Definition: Pass.cpp:45
MPPassManager.
Definition: Pass.h:57
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:41
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
Pass * getAnalysisIfAvailable(AnalysisID ID, bool Direction) const
virtual ~ModulePass()
Definition: Pass.cpp:34
virtual bool doFinalization(Function &)
Definition: Pass.cpp:154
virtual ImmutablePass * getAsImmutablePass()
Definition: Pass.cpp:91
static const PassInfo * lookupPassInfo(const void *TI)
Definition: Pass.cpp:163
#define P(N)
virtual ~PassRegistrationListener()
Definition: Pass.cpp:210
Pass * createPass() const
createPass() - Use this method to create an instance of this pass.
Definition: Pass.cpp:178
void addRegistrationListener(PassRegistrationListener *L)
FunctionPass * createPrintFunctionPass(const std::string &Banner, raw_ostream *OS, bool DeleteStream=false)
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const
createPrinterPass - Get a basic block printer pass.
Definition: Pass.cpp:144
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:136
FPPassManager.
Definition: Pass.h:59
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:262
void setPreservesCFG()
Definition: Pass.cpp:249
virtual ~PassNameParser()
Definition: Pass.cpp:221
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
Definition: Debug.cpp:101
BasicBlockPass * createPrintBasicBlockPass(raw_ostream *OS, bool DeleteStream=false, const std::string &Banner="")
virtual ~Pass()
Definition: Pass.cpp:29
AnalysisUsage & addRequiredTransitiveID(char &ID)
Definition: Pass.cpp:272
const void * AnalysisID
Definition: Pass.h:47
const PassInfo * getPassInfo(const void *TI) const
static Pass * createPass(AnalysisID ID)
Definition: Pass.cpp:171
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:159
void dump() const
Definition: Pass.cpp:113
virtual void * getAdjustedAnalysisPointer(AnalysisID ID)
Definition: Pass.cpp:87
virtual void print(raw_ostream &O, const Module *M) const
Definition: Pass.cpp:108
Pass * createPrinterPass(raw_ostream &O, const std::string &Banner) const
createPrinterPass - Get a function printer pass.
Definition: Pass.cpp:131
RegisterAGBase(const char *Name, const void *InterfaceID, const void *PassID=0, bool isDefault=false)
Definition: Pass.cpp:192
virtual PassManagerType getPotentialPassManagerType() const
Return what kind of Pass Manager can manage this pass.
Definition: Pass.cpp:70