1 //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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/PassManager.h"
17 #include "PassManagerT.h" // PassManagerT implementation
18 #include "llvm/Module.h"
19 #include "llvm/ModuleProvider.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/Support/TypeInfo.h"
26 //===----------------------------------------------------------------------===//
27 // AnalysisID Class Implementation
30 // getCFGOnlyAnalyses - A wrapper around the CFGOnlyAnalyses which make it
31 // initializer order independent.
32 static std::vector
<const PassInfo
*> &getCFGOnlyAnalyses() {
33 static std::vector
<const PassInfo
*> CFGOnlyAnalyses
;
34 return CFGOnlyAnalyses
;
37 void RegisterPassBase::setOnlyUsesCFG() {
38 getCFGOnlyAnalyses().push_back(&PIObj
);
41 //===----------------------------------------------------------------------===//
42 // AnalysisResolver Class Implementation
45 AnalysisResolver::~AnalysisResolver() {
47 void AnalysisResolver::setAnalysisResolver(Pass
*P
, AnalysisResolver
*AR
) {
48 assert(P
->Resolver
== 0 && "Pass already in a PassManager!");
52 //===----------------------------------------------------------------------===//
53 // AnalysisUsage Class Implementation
56 // setPreservesCFG - This function should be called to by the pass, iff they do
59 // 1. Add or remove basic blocks from the function
60 // 2. Modify terminator instructions in any way.
62 // This function annotates the AnalysisUsage info object to say that analyses
63 // that only depend on the CFG are preserved by this pass.
65 void AnalysisUsage::setPreservesCFG() {
66 // Since this transformation doesn't modify the CFG, it preserves all analyses
67 // that only depend on the CFG (like dominators, loop info, etc...)
69 Preserved
.insert(Preserved
.end(),
70 getCFGOnlyAnalyses().begin(), getCFGOnlyAnalyses().end());
74 //===----------------------------------------------------------------------===//
75 // PassManager implementation - The PassManager class is a simple Pimpl class
76 // that wraps the PassManagerT template.
78 PassManager::PassManager() : PM(new ModulePassManager()) {}
79 PassManager::~PassManager() { delete PM
; }
80 void PassManager::add(Pass
*P
) {
81 ModulePass
*MP
= dynamic_cast<ModulePass
*>(P
);
82 assert(MP
&& "Not a modulepass?");
85 bool PassManager::run(Module
&M
) { return PM
->runOnModule(M
); }
87 //===----------------------------------------------------------------------===//
88 // FunctionPassManager implementation - The FunctionPassManager class
89 // is a simple Pimpl class that wraps the PassManagerT template. It
90 // is like PassManager, but only deals in FunctionPasses.
92 FunctionPassManager::FunctionPassManager(ModuleProvider
*P
) :
93 PM(new FunctionPassManagerT()), MP(P
) {}
94 FunctionPassManager::~FunctionPassManager() { delete PM
; }
95 void FunctionPassManager::add(FunctionPass
*P
) { PM
->add(P
); }
96 void FunctionPassManager::add(ImmutablePass
*IP
) { PM
->add(IP
); }
97 bool FunctionPassManager::run(Function
&F
) {
99 if (MP
->materializeFunction(&F
, &errstr
)) {
100 std::cerr
<< "Error reading bytecode file: " << errstr
<< "\n";
107 //===----------------------------------------------------------------------===//
108 // TimingInfo Class - This class is used to calculate information about the
109 // amount of time each pass takes to execute. This only happens with
110 // -time-passes is enabled on the command line.
112 bool llvm::TimePassesIsEnabled
= false;
113 static cl::opt
<bool,true>
114 EnableTiming("time-passes", cl::location(TimePassesIsEnabled
),
115 cl::desc("Time each pass, printing elapsed time for each on exit"));
117 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
118 // a non null value (if the -time-passes option is enabled) or it leaves it
119 // null. It may be called multiple times.
120 void TimingInfo::createTheTimeInfo() {
121 if (!TimePassesIsEnabled
|| TheTimeInfo
) return;
123 // Constructed the first time this is called, iff -time-passes is enabled.
124 // This guarantees that the object will be constructed before static globals,
125 // thus it will be destroyed before them.
126 static TimingInfo TTI
;
130 void PMDebug::PrintArgumentInformation(const Pass
*P
) {
131 // Print out passes in pass manager...
132 if (const AnalysisResolver
*PM
= dynamic_cast<const AnalysisResolver
*>(P
)) {
133 for (unsigned i
= 0, e
= PM
->getNumContainedPasses(); i
!= e
; ++i
)
134 PrintArgumentInformation(PM
->getContainedPass(i
));
136 } else { // Normal pass. Print argument information...
137 // Print out arguments for registered passes that are _optimizations_
138 if (const PassInfo
*PI
= P
->getPassInfo())
139 if (PI
->getPassType() & PassInfo::Optimization
)
140 std::cerr
<< " -" << PI
->getPassArgument();
144 void PMDebug::PrintPassInformation(unsigned Depth
, const char *Action
,
145 Pass
*P
, Module
*M
) {
146 if (PassDebugging
>= Executions
) {
147 std::cerr
<< (void*)P
<< std::string(Depth
*2+1, ' ') << Action
<< " '"
149 if (M
) std::cerr
<< "' on Module '" << M
->getModuleIdentifier() << "'\n";
150 std::cerr
<< "'...\n";
154 void PMDebug::PrintPassInformation(unsigned Depth
, const char *Action
,
155 Pass
*P
, Function
*F
) {
156 if (PassDebugging
>= Executions
) {
157 std::cerr
<< (void*)P
<< std::string(Depth
*2+1, ' ') << Action
<< " '"
159 if (F
) std::cerr
<< "' on Function '" << F
->getName();
160 std::cerr
<< "'...\n";
164 void PMDebug::PrintPassInformation(unsigned Depth
, const char *Action
,
165 Pass
*P
, BasicBlock
*BB
) {
166 if (PassDebugging
>= Executions
) {
167 std::cerr
<< (void*)P
<< std::string(Depth
*2+1, ' ') << Action
<< " '"
169 if (BB
) std::cerr
<< "' on BasicBlock '" << BB
->getName();
170 std::cerr
<< "'...\n";
174 void PMDebug::PrintAnalysisSetInfo(unsigned Depth
, const char *Msg
,
175 Pass
*P
, const std::vector
<AnalysisID
> &Set
){
176 if (PassDebugging
>= Details
&& !Set
.empty()) {
177 std::cerr
<< (void*)P
<< std::string(Depth
*2+3, ' ') << Msg
<< " Analyses:";
178 for (unsigned i
= 0; i
!= Set
.size(); ++i
) {
179 if (i
) std::cerr
<< ",";
180 std::cerr
<< " " << Set
[i
]->getPassName();
186 //===----------------------------------------------------------------------===//
187 // Pass Implementation
190 void ModulePass::addToPassManager(ModulePassManager
*PM
, AnalysisUsage
&AU
) {
191 PM
->addPass(this, AU
);
194 bool Pass::mustPreserveAnalysisID(const PassInfo
*AnalysisID
) const {
195 return Resolver
->getAnalysisToUpdate(AnalysisID
) != 0;
198 // dumpPassStructure - Implement the -debug-passes=Structure option
199 void Pass::dumpPassStructure(unsigned Offset
) {
200 std::cerr
<< std::string(Offset
*2, ' ') << getPassName() << "\n";
203 // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
205 const char *Pass::getPassName() const {
206 if (const PassInfo
*PI
= getPassInfo())
207 return PI
->getPassName();
208 return typeid(*this).name();
211 // print - Print out the internal state of the pass. This is called by Analyze
212 // to print out the contents of an analysis. Otherwise it is not necessary to
213 // implement this method.
215 void Pass::print(std::ostream
&O
,const Module
*) const {
216 O
<< "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
219 // dump - call print(std::cerr);
220 void Pass::dump() const {
224 //===----------------------------------------------------------------------===//
225 // ImmutablePass Implementation
227 void ImmutablePass::addToPassManager(ModulePassManager
*PM
,
229 PM
->addPass(this, AU
);
233 //===----------------------------------------------------------------------===//
234 // FunctionPass Implementation
237 // run - On a module, we run this pass by initializing, runOnFunction'ing once
238 // for every function in the module, then by finalizing.
240 bool FunctionPass::runOnModule(Module
&M
) {
241 bool Changed
= doInitialization(M
);
243 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
244 if (!I
->isExternal()) // Passes are not run on external functions!
245 Changed
|= runOnFunction(*I
);
247 return Changed
| doFinalization(M
);
250 // run - On a function, we simply initialize, run the function, then finalize.
252 bool FunctionPass::run(Function
&F
) {
253 if (F
.isExternal()) return false;// Passes are not run on external functions!
255 bool Changed
= doInitialization(*F
.getParent());
256 Changed
|= runOnFunction(F
);
257 return Changed
| doFinalization(*F
.getParent());
260 void FunctionPass::addToPassManager(ModulePassManager
*PM
,
262 PM
->addPass(this, AU
);
265 void FunctionPass::addToPassManager(FunctionPassManagerT
*PM
,
267 PM
->addPass(this, AU
);
270 //===----------------------------------------------------------------------===//
271 // BasicBlockPass Implementation
274 // To run this pass on a function, we simply call runOnBasicBlock once for each
277 bool BasicBlockPass::runOnFunction(Function
&F
) {
278 bool Changed
= doInitialization(F
);
279 for (Function::iterator I
= F
.begin(), E
= F
.end(); I
!= E
; ++I
)
280 Changed
|= runOnBasicBlock(*I
);
281 return Changed
| doFinalization(F
);
284 // To run directly on the basic block, we initialize, runOnBasicBlock, then
287 bool BasicBlockPass::runPass(BasicBlock
&BB
) {
288 Function
&F
= *BB
.getParent();
289 Module
&M
= *F
.getParent();
290 bool Changed
= doInitialization(M
);
291 Changed
|= doInitialization(F
);
292 Changed
|= runOnBasicBlock(BB
);
293 Changed
|= doFinalization(F
);
294 Changed
|= doFinalization(M
);
298 void BasicBlockPass::addToPassManager(FunctionPassManagerT
*PM
,
300 PM
->addPass(this, AU
);
303 void BasicBlockPass::addToPassManager(BasicBlockPassManager
*PM
,
305 PM
->addPass(this, AU
);
309 //===----------------------------------------------------------------------===//
310 // Pass Registration mechanism
312 static std::map
<TypeInfo
, PassInfo
*> *PassInfoMap
= 0;
313 static std::vector
<PassRegistrationListener
*> *Listeners
= 0;
315 // getPassInfo - Return the PassInfo data structure that corresponds to this
317 const PassInfo
*Pass::getPassInfo() const {
318 if (PassInfoCache
) return PassInfoCache
;
319 return lookupPassInfo(typeid(*this));
322 const PassInfo
*Pass::lookupPassInfo(const std::type_info
&TI
) {
323 if (PassInfoMap
== 0) return 0;
324 std::map
<TypeInfo
, PassInfo
*>::iterator I
= PassInfoMap
->find(TI
);
325 return (I
!= PassInfoMap
->end()) ? I
->second
: 0;
328 void RegisterPassBase::registerPass() {
329 if (PassInfoMap
== 0)
330 PassInfoMap
= new std::map
<TypeInfo
, PassInfo
*>();
332 assert(PassInfoMap
->find(PIObj
.getTypeInfo()) == PassInfoMap
->end() &&
333 "Pass already registered!");
334 PassInfoMap
->insert(std::make_pair(TypeInfo(PIObj
.getTypeInfo()), &PIObj
));
336 // Notify any listeners...
338 for (std::vector
<PassRegistrationListener
*>::iterator
339 I
= Listeners
->begin(), E
= Listeners
->end(); I
!= E
; ++I
)
340 (*I
)->passRegistered(&PIObj
);
343 void RegisterPassBase::unregisterPass() {
344 assert(PassInfoMap
&& "Pass registered but not in map!");
345 std::map
<TypeInfo
, PassInfo
*>::iterator I
=
346 PassInfoMap
->find(PIObj
.getTypeInfo());
347 assert(I
!= PassInfoMap
->end() && "Pass registered but not in map!");
349 // Remove pass from the map...
350 PassInfoMap
->erase(I
);
351 if (PassInfoMap
->empty()) {
356 // Notify any listeners...
358 for (std::vector
<PassRegistrationListener
*>::iterator
359 I
= Listeners
->begin(), E
= Listeners
->end(); I
!= E
; ++I
)
360 (*I
)->passUnregistered(&PIObj
);
363 //===----------------------------------------------------------------------===//
364 // Analysis Group Implementation Code
365 //===----------------------------------------------------------------------===//
367 struct AnalysisGroupInfo
{
368 const PassInfo
*DefaultImpl
;
369 std::set
<const PassInfo
*> Implementations
;
370 AnalysisGroupInfo() : DefaultImpl(0) {}
373 static std::map
<const PassInfo
*, AnalysisGroupInfo
> *AnalysisGroupInfoMap
= 0;
375 // RegisterAGBase implementation
377 RegisterAGBase::RegisterAGBase(const std::type_info
&Interface
,
378 const std::type_info
*Pass
, bool isDefault
)
379 : RegisterPassBase(Interface
, PassInfo::AnalysisGroup
),
380 ImplementationInfo(0), isDefaultImplementation(isDefault
) {
382 InterfaceInfo
= const_cast<PassInfo
*>(Pass::lookupPassInfo(Interface
));
383 if (InterfaceInfo
== 0) {
384 // First reference to Interface, register it now.
386 InterfaceInfo
= &PIObj
;
388 assert(InterfaceInfo
->getPassType() == PassInfo::AnalysisGroup
&&
389 "Trying to join an analysis group that is a normal pass!");
392 ImplementationInfo
= Pass::lookupPassInfo(*Pass
);
393 assert(ImplementationInfo
&&
394 "Must register pass before adding to AnalysisGroup!");
396 // Make sure we keep track of the fact that the implementation implements
398 PassInfo
*IIPI
= const_cast<PassInfo
*>(ImplementationInfo
);
399 IIPI
->addInterfaceImplemented(InterfaceInfo
);
401 // Lazily allocate to avoid nasty initialization order dependencies
402 if (AnalysisGroupInfoMap
== 0)
403 AnalysisGroupInfoMap
= new std::map
<const PassInfo
*,AnalysisGroupInfo
>();
405 AnalysisGroupInfo
&AGI
= (*AnalysisGroupInfoMap
)[InterfaceInfo
];
406 assert(AGI
.Implementations
.count(ImplementationInfo
) == 0 &&
407 "Cannot add a pass to the same analysis group more than once!");
408 AGI
.Implementations
.insert(ImplementationInfo
);
410 assert(AGI
.DefaultImpl
== 0 && InterfaceInfo
->getNormalCtor() == 0 &&
411 "Default implementation for analysis group already specified!");
412 assert(ImplementationInfo
->getNormalCtor() &&
413 "Cannot specify pass as default if it does not have a default ctor");
414 AGI
.DefaultImpl
= ImplementationInfo
;
415 InterfaceInfo
->setNormalCtor(ImplementationInfo
->getNormalCtor());
420 void RegisterAGBase::setGroupName(const char *Name
) {
421 assert(InterfaceInfo
->getPassName()[0] == 0 && "Interface Name already set!");
422 InterfaceInfo
->setPassName(Name
);
425 RegisterAGBase::~RegisterAGBase() {
426 if (ImplementationInfo
) {
427 assert(AnalysisGroupInfoMap
&& "Inserted into map, but map doesn't exist?");
428 AnalysisGroupInfo
&AGI
= (*AnalysisGroupInfoMap
)[InterfaceInfo
];
430 assert(AGI
.Implementations
.count(ImplementationInfo
) &&
431 "Pass not a member of analysis group?");
433 if (AGI
.DefaultImpl
== ImplementationInfo
)
436 AGI
.Implementations
.erase(ImplementationInfo
);
438 // Last member of this analysis group? Unregister PassInfo, delete map entry
439 if (AGI
.Implementations
.empty()) {
440 assert(AGI
.DefaultImpl
== 0 &&
441 "Default implementation didn't unregister?");
442 AnalysisGroupInfoMap
->erase(InterfaceInfo
);
443 if (AnalysisGroupInfoMap
->empty()) { // Delete map if empty
444 delete AnalysisGroupInfoMap
;
445 AnalysisGroupInfoMap
= 0;
450 if (InterfaceInfo
== &PIObj
)
455 //===----------------------------------------------------------------------===//
456 // PassRegistrationListener implementation
459 // PassRegistrationListener ctor - Add the current object to the list of
460 // PassRegistrationListeners...
461 PassRegistrationListener::PassRegistrationListener() {
462 if (!Listeners
) Listeners
= new std::vector
<PassRegistrationListener
*>();
463 Listeners
->push_back(this);
466 // dtor - Remove object from list of listeners...
467 PassRegistrationListener::~PassRegistrationListener() {
468 std::vector
<PassRegistrationListener
*>::iterator I
=
469 std::find(Listeners
->begin(), Listeners
->end(), this);
470 assert(Listeners
&& I
!= Listeners
->end() &&
471 "PassRegistrationListener not registered!");
474 if (Listeners
->empty()) {
480 // enumeratePasses - Iterate over the registered passes, calling the
481 // passEnumerate callback on each PassInfo object.
483 void PassRegistrationListener::enumeratePasses() {
485 for (std::map
<TypeInfo
, PassInfo
*>::iterator I
= PassInfoMap
->begin(),
486 E
= PassInfoMap
->end(); I
!= E
; ++I
)
487 passEnumerate(I
->second
);