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/ADT/StringMap.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/System/Atomic.h"
26 #include "llvm/System/Mutex.h"
27 #include "llvm/System/Threading.h"
33 //===----------------------------------------------------------------------===//
34 // Pass Implementation
37 // Force out-of-line virtual method.
42 // Force out-of-line virtual method.
43 ModulePass::~ModulePass() { }
45 PassManagerType ModulePass::getPotentialPassManagerType() const {
46 return PMT_ModulePassManager;
49 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
50 return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0;
53 // dumpPassStructure - Implement the -debug-passes=Structure option
54 void Pass::dumpPassStructure(unsigned Offset) {
55 dbgs().indent(Offset*2) << getPassName() << "\n";
58 /// getPassName - Return a nice clean name for a pass. This usually
59 /// implemented in terms of the name that is registered by one of the
60 /// Registration templates, but can be overloaded directly.
62 const char *Pass::getPassName() const {
63 if (const PassInfo *PI = getPassInfo())
64 return PI->getPassName();
65 return "Unnamed pass: implement Pass::getPassName()";
68 void Pass::preparePassManager(PMStack &) {
69 // By default, don't do anything.
72 PassManagerType Pass::getPotentialPassManagerType() const {
73 // Default implementation.
77 void Pass::getAnalysisUsage(AnalysisUsage &) const {
78 // By default, no analysis results are used, all are invalidated.
81 void Pass::releaseMemory() {
82 // By default, don't do anything.
85 void Pass::verifyAnalysis() const {
86 // By default, don't do anything.
89 // print - Print out the internal state of the pass. This is called by Analyze
90 // to print out the contents of an analysis. Otherwise it is not necessary to
91 // implement this method.
93 void Pass::print(raw_ostream &O,const Module*) const {
94 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
97 // dump - call print(cerr);
98 void Pass::dump() const {
102 //===----------------------------------------------------------------------===//
103 // ImmutablePass Implementation
105 // Force out-of-line virtual method.
106 ImmutablePass::~ImmutablePass() { }
108 void ImmutablePass::initializePass() {
109 // By default, don't do anything.
112 //===----------------------------------------------------------------------===//
113 // FunctionPass Implementation
116 // run - On a module, we run this pass by initializing, runOnFunction'ing once
117 // for every function in the module, then by finalizing.
119 bool FunctionPass::runOnModule(Module &M) {
120 bool Changed = doInitialization(M);
122 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
123 if (!I->isDeclaration()) // Passes are not run on external functions!
124 Changed |= runOnFunction(*I);
126 return Changed | doFinalization(M);
129 // run - On a function, we simply initialize, run the function, then finalize.
131 bool FunctionPass::run(Function &F) {
132 // Passes are not run on external functions!
133 if (F.isDeclaration()) return false;
135 bool Changed = doInitialization(*F.getParent());
136 Changed |= runOnFunction(F);
137 return Changed | doFinalization(*F.getParent());
140 bool FunctionPass::doInitialization(Module &) {
141 // By default, don't do anything.
145 bool FunctionPass::doFinalization(Module &) {
146 // By default, don't do anything.
150 PassManagerType FunctionPass::getPotentialPassManagerType() const {
151 return PMT_FunctionPassManager;
154 //===----------------------------------------------------------------------===//
155 // BasicBlockPass Implementation
158 // To run this pass on a function, we simply call runOnBasicBlock once for each
161 bool BasicBlockPass::runOnFunction(Function &F) {
162 bool Changed = doInitialization(F);
163 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
164 Changed |= runOnBasicBlock(*I);
165 return Changed | doFinalization(F);
168 bool BasicBlockPass::doInitialization(Module &) {
169 // By default, don't do anything.
173 bool BasicBlockPass::doInitialization(Function &) {
174 // By default, don't do anything.
178 bool BasicBlockPass::doFinalization(Function &) {
179 // By default, don't do anything.
183 bool BasicBlockPass::doFinalization(Module &) {
184 // By default, don't do anything.
188 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
189 return PMT_BasicBlockPassManager;
192 //===----------------------------------------------------------------------===//
193 // Pass Registration mechanism
196 class PassRegistrar {
197 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
199 typedef std::map<intptr_t, const PassInfo*> MapType;
202 typedef StringMap<const PassInfo*> StringMapType;
203 StringMapType PassInfoStringMap;
205 /// AnalysisGroupInfo - Keep track of information for each analysis group.
206 struct AnalysisGroupInfo {
207 std::set<const PassInfo *> Implementations;
210 /// AnalysisGroupInfoMap - Information for each analysis group.
211 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
215 const PassInfo *GetPassInfo(intptr_t TI) const {
216 MapType::const_iterator I = PassInfoMap.find(TI);
217 return I != PassInfoMap.end() ? I->second : 0;
220 const PassInfo *GetPassInfo(StringRef Arg) const {
221 StringMapType::const_iterator I = PassInfoStringMap.find(Arg);
222 return I != PassInfoStringMap.end() ? I->second : 0;
225 void RegisterPass(const PassInfo &PI) {
227 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
228 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
229 PassInfoStringMap[PI.getPassArgument()] = &PI;
232 void UnregisterPass(const PassInfo &PI) {
233 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
234 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
236 // Remove pass from the map.
237 PassInfoMap.erase(I);
238 PassInfoStringMap.erase(PI.getPassArgument());
241 void EnumerateWith(PassRegistrationListener *L) {
242 for (MapType::const_iterator I = PassInfoMap.begin(),
243 E = PassInfoMap.end(); I != E; ++I)
244 L->passEnumerate(I->second);
248 /// Analysis Group Mechanisms.
249 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
250 const PassInfo *ImplementationInfo,
252 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
253 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
254 "Cannot add a pass to the same analysis group more than once!");
255 AGI.Implementations.insert(ImplementationInfo);
257 assert(InterfaceInfo->getNormalCtor() == 0 &&
258 "Default implementation for analysis group already specified!");
259 assert(ImplementationInfo->getNormalCtor() &&
260 "Cannot specify pass as default if it does not have a default ctor");
261 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
267 static std::vector<PassRegistrationListener*> *Listeners = 0;
268 static sys::SmartMutex<true> ListenersLock;
270 // FIXME: This should use ManagedStatic to manage the pass registrar.
271 // Unfortunately, we can't do this, because passes are registered with static
272 // ctors, and having llvm_shutdown clear this map prevents successful
273 // ressurection after llvm_shutdown is run.
274 static PassRegistrar *getPassRegistrar() {
275 static PassRegistrar *PassRegistrarObj = 0;
277 // Use double-checked locking to safely initialize the registrar when
278 // we're running in multithreaded mode.
279 PassRegistrar* tmp = PassRegistrarObj;
280 if (llvm_is_multithreaded()) {
283 llvm_acquire_global_lock();
284 tmp = PassRegistrarObj;
286 tmp = new PassRegistrar();
288 PassRegistrarObj = tmp;
290 llvm_release_global_lock();
293 PassRegistrarObj = new PassRegistrar();
296 return PassRegistrarObj;
299 // getPassInfo - Return the PassInfo data structure that corresponds to this
301 const PassInfo *Pass::getPassInfo() const {
302 return lookupPassInfo(PassID);
305 const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
306 return getPassRegistrar()->GetPassInfo(TI);
309 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
310 return getPassRegistrar()->GetPassInfo(Arg);
313 void PassInfo::registerPass() {
314 getPassRegistrar()->RegisterPass(*this);
316 // Notify any listeners.
317 sys::SmartScopedLock<true> Lock(ListenersLock);
319 for (std::vector<PassRegistrationListener*>::iterator
320 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
321 (*I)->passRegistered(this);
324 void PassInfo::unregisterPass() {
325 getPassRegistrar()->UnregisterPass(*this);
328 //===----------------------------------------------------------------------===//
329 // Analysis Group Implementation Code
330 //===----------------------------------------------------------------------===//
332 // RegisterAGBase implementation
334 RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
335 intptr_t PassID, bool isDefault)
336 : PassInfo(Name, InterfaceID) {
338 PassInfo *InterfaceInfo =
339 const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
340 if (InterfaceInfo == 0) {
341 // First reference to Interface, register it now.
343 InterfaceInfo = this;
345 assert(isAnalysisGroup() &&
346 "Trying to join an analysis group that is a normal pass!");
349 const PassInfo *ImplementationInfo = Pass::lookupPassInfo(PassID);
350 assert(ImplementationInfo &&
351 "Must register pass before adding to AnalysisGroup!");
353 // Make sure we keep track of the fact that the implementation implements
355 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
356 IIPI->addInterfaceImplemented(InterfaceInfo);
358 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
363 //===----------------------------------------------------------------------===//
364 // PassRegistrationListener implementation
367 // PassRegistrationListener ctor - Add the current object to the list of
368 // PassRegistrationListeners...
369 PassRegistrationListener::PassRegistrationListener() {
370 sys::SmartScopedLock<true> Lock(ListenersLock);
371 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
372 Listeners->push_back(this);
375 // dtor - Remove object from list of listeners...
376 PassRegistrationListener::~PassRegistrationListener() {
377 sys::SmartScopedLock<true> Lock(ListenersLock);
378 std::vector<PassRegistrationListener*>::iterator I =
379 std::find(Listeners->begin(), Listeners->end(), this);
380 assert(Listeners && I != Listeners->end() &&
381 "PassRegistrationListener not registered!");
384 if (Listeners->empty()) {
390 // enumeratePasses - Iterate over the registered passes, calling the
391 // passEnumerate callback on each PassInfo object.
393 void PassRegistrationListener::enumeratePasses() {
394 getPassRegistrar()->EnumerateWith(this);
397 //===----------------------------------------------------------------------===//
398 // AnalysisUsage Class Implementation
402 struct GetCFGOnlyPasses : public PassRegistrationListener {
403 typedef AnalysisUsage::VectorType VectorType;
404 VectorType &CFGOnlyList;
405 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
407 void passEnumerate(const PassInfo *P) {
408 if (P->isCFGOnlyPass())
409 CFGOnlyList.push_back(P);
414 // setPreservesCFG - This function should be called to by the pass, iff they do
417 // 1. Add or remove basic blocks from the function
418 // 2. Modify terminator instructions in any way.
420 // This function annotates the AnalysisUsage info object to say that analyses
421 // that only depend on the CFG are preserved by this pass.
423 void AnalysisUsage::setPreservesCFG() {
424 // Since this transformation doesn't modify the CFG, it preserves all analyses
425 // that only depend on the CFG (like dominators, loop info, etc...)
426 GetCFGOnlyPasses(Preserved).enumeratePasses();