1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
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 implements the LLVM Pass infrastructure. It is primarily
11 // responsible with ensuring that passes are executed and batched together
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Pass.h"
17 #include "llvm/PassManager.h"
18 #include "llvm/Module.h"
19 #include "llvm/ModuleProvider.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/ManagedStatic.h"
27 //===----------------------------------------------------------------------===//
28 // Pass Implementation
31 // Force out-of-line virtual method.
36 // Force out-of-line virtual method.
37 ModulePass::~ModulePass() { }
39 bool Pass::mustPreserveAnalysisID(const PassInfo
*AnalysisID
) const {
40 return Resolver
->getAnalysisIfAvailable(AnalysisID
, true) != 0;
43 // dumpPassStructure - Implement the -debug-passes=Structure option
44 void Pass::dumpPassStructure(unsigned Offset
) {
45 cerr
<< std::string(Offset
*2, ' ') << getPassName() << "\n";
48 /// getPassName - Return a nice clean name for a pass. This usually
49 /// implemented in terms of the name that is registered by one of the
50 /// Registration templates, but can be overloaded directly.
52 const char *Pass::getPassName() const {
53 if (const PassInfo
*PI
= getPassInfo())
54 return PI
->getPassName();
55 return "Unnamed pass: implement Pass::getPassName()";
58 // print - Print out the internal state of the pass. This is called by Analyze
59 // to print out the contents of an analysis. Otherwise it is not necessary to
60 // implement this method.
62 void Pass::print(std::ostream
&O
,const Module
*) const {
63 O
<< "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
66 // dump - call print(cerr);
67 void Pass::dump() const {
68 print(*cerr
.stream(), 0);
71 //===----------------------------------------------------------------------===//
72 // ImmutablePass Implementation
74 // Force out-of-line virtual method.
75 ImmutablePass::~ImmutablePass() { }
77 //===----------------------------------------------------------------------===//
78 // FunctionPass Implementation
81 // run - On a module, we run this pass by initializing, runOnFunction'ing once
82 // for every function in the module, then by finalizing.
84 bool FunctionPass::runOnModule(Module
&M
) {
85 bool Changed
= doInitialization(M
);
87 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
88 if (!I
->isDeclaration()) // Passes are not run on external functions!
89 Changed
|= runOnFunction(*I
);
91 return Changed
| doFinalization(M
);
94 // run - On a function, we simply initialize, run the function, then finalize.
96 bool FunctionPass::run(Function
&F
) {
97 // Passes are not run on external functions!
98 if (F
.isDeclaration()) return false;
100 bool Changed
= doInitialization(*F
.getParent());
101 Changed
|= runOnFunction(F
);
102 return Changed
| doFinalization(*F
.getParent());
105 //===----------------------------------------------------------------------===//
106 // BasicBlockPass Implementation
109 // To run this pass on a function, we simply call runOnBasicBlock once for each
112 bool BasicBlockPass::runOnFunction(Function
&F
) {
113 bool Changed
= doInitialization(F
);
114 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
)
115 Changed
|= runOnBasicBlock(*I
);
116 return Changed
| doFinalization(F
);
119 //===----------------------------------------------------------------------===//
120 // Pass Registration mechanism
123 class PassRegistrar
{
124 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
126 typedef std::map
<intptr_t, const PassInfo
*> MapType
;
129 /// AnalysisGroupInfo - Keep track of information for each analysis group.
130 struct AnalysisGroupInfo
{
131 const PassInfo
*DefaultImpl
;
132 std::set
<const PassInfo
*> Implementations
;
133 AnalysisGroupInfo() : DefaultImpl(0) {}
136 /// AnalysisGroupInfoMap - Information for each analysis group.
137 std::map
<const PassInfo
*, AnalysisGroupInfo
> AnalysisGroupInfoMap
;
141 const PassInfo
*GetPassInfo(intptr_t TI
) const {
142 MapType::const_iterator I
= PassInfoMap
.find(TI
);
143 return I
!= PassInfoMap
.end() ? I
->second
: 0;
146 void RegisterPass(const PassInfo
&PI
) {
148 PassInfoMap
.insert(std::make_pair(PI
.getTypeInfo(),&PI
)).second
;
149 assert(Inserted
&& "Pass registered multiple times!"); Inserted
=Inserted
;
152 void UnregisterPass(const PassInfo
&PI
) {
153 MapType::iterator I
= PassInfoMap
.find(PI
.getTypeInfo());
154 assert(I
!= PassInfoMap
.end() && "Pass registered but not in map!");
156 // Remove pass from the map.
157 PassInfoMap
.erase(I
);
160 void EnumerateWith(PassRegistrationListener
*L
) {
161 for (MapType::const_iterator I
= PassInfoMap
.begin(),
162 E
= PassInfoMap
.end(); I
!= E
; ++I
)
163 L
->passEnumerate(I
->second
);
167 /// Analysis Group Mechanisms.
168 void RegisterAnalysisGroup(PassInfo
*InterfaceInfo
,
169 const PassInfo
*ImplementationInfo
,
171 AnalysisGroupInfo
&AGI
= AnalysisGroupInfoMap
[InterfaceInfo
];
172 assert(AGI
.Implementations
.count(ImplementationInfo
) == 0 &&
173 "Cannot add a pass to the same analysis group more than once!");
174 AGI
.Implementations
.insert(ImplementationInfo
);
176 assert(AGI
.DefaultImpl
== 0 && InterfaceInfo
->getNormalCtor() == 0 &&
177 "Default implementation for analysis group already specified!");
178 assert(ImplementationInfo
->getNormalCtor() &&
179 "Cannot specify pass as default if it does not have a default ctor");
180 AGI
.DefaultImpl
= ImplementationInfo
;
181 InterfaceInfo
->setNormalCtor(ImplementationInfo
->getNormalCtor());
187 static std::vector
<PassRegistrationListener
*> *Listeners
= 0;
189 // FIXME: This should use ManagedStatic to manage the pass registrar.
190 // Unfortunately, we can't do this, because passes are registered with static
191 // ctors, and having llvm_shutdown clear this map prevents successful
192 // ressurection after llvm_shutdown is run.
193 static PassRegistrar
*getPassRegistrar() {
194 static PassRegistrar
*PassRegistrarObj
= 0;
195 if (!PassRegistrarObj
)
196 PassRegistrarObj
= new PassRegistrar();
197 return PassRegistrarObj
;
200 // getPassInfo - Return the PassInfo data structure that corresponds to this
202 const PassInfo
*Pass::getPassInfo() const {
203 return lookupPassInfo(PassID
);
206 const PassInfo
*Pass::lookupPassInfo(intptr_t TI
) {
207 return getPassRegistrar()->GetPassInfo(TI
);
210 void PassInfo::registerPass() {
211 getPassRegistrar()->RegisterPass(*this);
213 // Notify any listeners.
215 for (std::vector
<PassRegistrationListener
*>::iterator
216 I
= Listeners
->begin(), E
= Listeners
->end(); I
!= E
; ++I
)
217 (*I
)->passRegistered(this);
220 void PassInfo::unregisterPass() {
221 getPassRegistrar()->UnregisterPass(*this);
224 //===----------------------------------------------------------------------===//
225 // Analysis Group Implementation Code
226 //===----------------------------------------------------------------------===//
228 // RegisterAGBase implementation
230 RegisterAGBase::RegisterAGBase(const char *Name
, intptr_t InterfaceID
,
231 intptr_t PassID
, bool isDefault
)
232 : PassInfo(Name
, InterfaceID
),
233 ImplementationInfo(0), isDefaultImplementation(isDefault
) {
235 InterfaceInfo
= const_cast<PassInfo
*>(Pass::lookupPassInfo(InterfaceID
));
236 if (InterfaceInfo
== 0) {
237 // First reference to Interface, register it now.
239 InterfaceInfo
= this;
241 assert(isAnalysisGroup() &&
242 "Trying to join an analysis group that is a normal pass!");
245 ImplementationInfo
= Pass::lookupPassInfo(PassID
);
246 assert(ImplementationInfo
&&
247 "Must register pass before adding to AnalysisGroup!");
249 // Make sure we keep track of the fact that the implementation implements
251 PassInfo
*IIPI
= const_cast<PassInfo
*>(ImplementationInfo
);
252 IIPI
->addInterfaceImplemented(InterfaceInfo
);
254 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo
, IIPI
, isDefault
);
259 //===----------------------------------------------------------------------===//
260 // PassRegistrationListener implementation
263 // PassRegistrationListener ctor - Add the current object to the list of
264 // PassRegistrationListeners...
265 PassRegistrationListener::PassRegistrationListener() {
266 if (!Listeners
) Listeners
= new std::vector
<PassRegistrationListener
*>();
267 Listeners
->push_back(this);
270 // dtor - Remove object from list of listeners...
271 PassRegistrationListener::~PassRegistrationListener() {
272 std::vector
<PassRegistrationListener
*>::iterator I
=
273 std::find(Listeners
->begin(), Listeners
->end(), this);
274 assert(Listeners
&& I
!= Listeners
->end() &&
275 "PassRegistrationListener not registered!");
278 if (Listeners
->empty()) {
284 // enumeratePasses - Iterate over the registered passes, calling the
285 // passEnumerate callback on each PassInfo object.
287 void PassRegistrationListener::enumeratePasses() {
288 getPassRegistrar()->EnumerateWith(this);
291 //===----------------------------------------------------------------------===//
292 // AnalysisUsage Class Implementation
296 struct GetCFGOnlyPasses
: public PassRegistrationListener
{
297 typedef AnalysisUsage::VectorType VectorType
;
298 VectorType
&CFGOnlyList
;
299 GetCFGOnlyPasses(VectorType
&L
) : CFGOnlyList(L
) {}
301 void passEnumerate(const PassInfo
*P
) {
302 if (P
->isCFGOnlyPass())
303 CFGOnlyList
.push_back(P
);
308 // setPreservesCFG - This function should be called to by the pass, iff they do
311 // 1. Add or remove basic blocks from the function
312 // 2. Modify terminator instructions in any way.
314 // This function annotates the AnalysisUsage info object to say that analyses
315 // that only depend on the CFG are preserved by this pass.
317 void AnalysisUsage::setPreservesCFG() {
318 // Since this transformation doesn't modify the CFG, it preserves all analyses
319 // that only depend on the CFG (like dominators, loop info, etc...)
320 GetCFGOnlyPasses(Preserved
).enumeratePasses();