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/PassRegistry.h"
19 #include "llvm/Module.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/Assembly/PrintModulePass.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Support/PassNameParser.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/System/Atomic.h"
28 #include "llvm/System/Mutex.h"
29 #include "llvm/System/Threading.h"
35 //===----------------------------------------------------------------------===//
36 // Pass Implementation
39 Pass::Pass(PassKind K, intptr_t pid) : Resolver(0), PassID(pid), Kind(K) {
40 assert(pid && "pid cannot be 0");
43 Pass::Pass(PassKind K, const void *pid)
44 : Resolver(0), PassID((intptr_t)pid), Kind(K) {
45 assert(pid && "pid cannot be 0");
48 // Force out-of-line virtual method.
53 // Force out-of-line virtual method.
54 ModulePass::~ModulePass() { }
56 Pass *ModulePass::createPrinterPass(raw_ostream &O,
57 const std::string &Banner) const {
58 return createPrintModulePass(&O, false, Banner);
61 PassManagerType ModulePass::getPotentialPassManagerType() const {
62 return PMT_ModulePassManager;
65 bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
66 return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0;
69 // dumpPassStructure - Implement the -debug-passes=Structure option
70 void Pass::dumpPassStructure(unsigned Offset) {
71 dbgs().indent(Offset*2) << getPassName() << "\n";
74 /// getPassName - Return a nice clean name for a pass. This usually
75 /// implemented in terms of the name that is registered by one of the
76 /// Registration templates, but can be overloaded directly.
78 const char *Pass::getPassName() const {
79 if (const PassInfo *PI = getPassInfo())
80 return PI->getPassName();
81 return "Unnamed pass: implement Pass::getPassName()";
84 void Pass::preparePassManager(PMStack &) {
85 // By default, don't do anything.
88 PassManagerType Pass::getPotentialPassManagerType() const {
89 // Default implementation.
93 void Pass::getAnalysisUsage(AnalysisUsage &) const {
94 // By default, no analysis results are used, all are invalidated.
97 void Pass::releaseMemory() {
98 // By default, don't do anything.
101 void Pass::verifyAnalysis() const {
102 // By default, don't do anything.
105 void *Pass::getAdjustedAnalysisPointer(const PassInfo *) {
109 ImmutablePass *Pass::getAsImmutablePass() {
113 PMDataManager *Pass::getAsPMDataManager() {
117 void Pass::setResolver(AnalysisResolver *AR) {
118 assert(!Resolver && "Resolver is already set");
122 // print - Print out the internal state of the pass. This is called by Analyze
123 // to print out the contents of an analysis. Otherwise it is not necessary to
124 // implement this method.
126 void Pass::print(raw_ostream &O,const Module*) const {
127 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
130 // dump - call print(cerr);
131 void Pass::dump() const {
135 //===----------------------------------------------------------------------===//
136 // ImmutablePass Implementation
138 // Force out-of-line virtual method.
139 ImmutablePass::~ImmutablePass() { }
141 void ImmutablePass::initializePass() {
142 // By default, don't do anything.
145 //===----------------------------------------------------------------------===//
146 // FunctionPass Implementation
149 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
150 const std::string &Banner) const {
151 return createPrintFunctionPass(Banner, &O);
154 // run - On a module, we run this pass by initializing, runOnFunction'ing once
155 // for every function in the module, then by finalizing.
157 bool FunctionPass::runOnModule(Module &M) {
158 bool Changed = doInitialization(M);
160 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
161 if (!I->isDeclaration()) // Passes are not run on external functions!
162 Changed |= runOnFunction(*I);
164 return Changed | doFinalization(M);
167 // run - On a function, we simply initialize, run the function, then finalize.
169 bool FunctionPass::run(Function &F) {
170 // Passes are not run on external functions!
171 if (F.isDeclaration()) return false;
173 bool Changed = doInitialization(*F.getParent());
174 Changed |= runOnFunction(F);
175 return Changed | doFinalization(*F.getParent());
178 bool FunctionPass::doInitialization(Module &) {
179 // By default, don't do anything.
183 bool FunctionPass::doFinalization(Module &) {
184 // By default, don't do anything.
188 PassManagerType FunctionPass::getPotentialPassManagerType() const {
189 return PMT_FunctionPassManager;
192 //===----------------------------------------------------------------------===//
193 // BasicBlockPass Implementation
196 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
197 const std::string &Banner) const {
199 llvm_unreachable("BasicBlockPass printing unsupported.");
203 // To run this pass on a function, we simply call runOnBasicBlock once for each
206 bool BasicBlockPass::runOnFunction(Function &F) {
207 bool Changed = doInitialization(F);
208 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
209 Changed |= runOnBasicBlock(*I);
210 return Changed | doFinalization(F);
213 bool BasicBlockPass::doInitialization(Module &) {
214 // By default, don't do anything.
218 bool BasicBlockPass::doInitialization(Function &) {
219 // By default, don't do anything.
223 bool BasicBlockPass::doFinalization(Function &) {
224 // By default, don't do anything.
228 bool BasicBlockPass::doFinalization(Module &) {
229 // By default, don't do anything.
233 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
234 return PMT_BasicBlockPassManager;
237 //===----------------------------------------------------------------------===//
238 // Pass Registration mechanism
241 static std::vector<PassRegistrationListener*> *Listeners = 0;
242 static sys::SmartMutex<true> ListenersLock;
244 static PassRegistry *PassRegistryObj = 0;
245 static PassRegistry *getPassRegistry() {
246 // Use double-checked locking to safely initialize the registrar when
247 // we're running in multithreaded mode.
248 PassRegistry* tmp = PassRegistryObj;
249 if (llvm_is_multithreaded()) {
252 llvm_acquire_global_lock();
253 tmp = PassRegistryObj;
255 tmp = new PassRegistry();
257 PassRegistryObj = tmp;
259 llvm_release_global_lock();
262 PassRegistryObj = new PassRegistry();
265 return PassRegistryObj;
270 // FIXME: We use ManagedCleanup to erase the pass registrar on shutdown.
271 // Unfortunately, passes are registered with static ctors, and having
272 // llvm_shutdown clear this map prevents successful ressurection after
273 // llvm_shutdown is run. Ideally we should find a solution so that we don't
274 // leak the map, AND can still resurrect after shutdown.
275 void cleanupPassRegistry(void*) {
276 if (PassRegistryObj) {
277 delete PassRegistryObj;
281 ManagedCleanup<&cleanupPassRegistry> registryCleanup ATTRIBUTE_USED;
285 // getPassInfo - Return the PassInfo data structure that corresponds to this
287 const PassInfo *Pass::getPassInfo() const {
288 return lookupPassInfo(PassID);
291 const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
292 return getPassRegistry()->getPassInfo(TI);
295 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
296 return getPassRegistry()->getPassInfo(Arg);
299 void PassInfo::registerPass() {
300 getPassRegistry()->registerPass(*this);
302 // Notify any listeners.
303 sys::SmartScopedLock<true> Lock(ListenersLock);
305 for (std::vector<PassRegistrationListener*>::iterator
306 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
307 (*I)->passRegistered(this);
310 void PassInfo::unregisterPass() {
311 getPassRegistry()->unregisterPass(*this);
314 Pass *PassInfo::createPass() const {
315 assert((!isAnalysisGroup() || NormalCtor) &&
316 "No default implementation found for analysis group!");
318 "Cannot call createPass on PassInfo without default ctor!");
322 //===----------------------------------------------------------------------===//
323 // Analysis Group Implementation Code
324 //===----------------------------------------------------------------------===//
326 // RegisterAGBase implementation
328 RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
329 intptr_t PassID, bool isDefault)
330 : PassInfo(Name, InterfaceID) {
332 PassInfo *InterfaceInfo =
333 const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
334 if (InterfaceInfo == 0) {
335 // First reference to Interface, register it now.
337 InterfaceInfo = this;
339 assert(isAnalysisGroup() &&
340 "Trying to join an analysis group that is a normal pass!");
343 const PassInfo *ImplementationInfo = Pass::lookupPassInfo(PassID);
344 assert(ImplementationInfo &&
345 "Must register pass before adding to AnalysisGroup!");
347 // Make sure we keep track of the fact that the implementation implements
349 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
350 IIPI->addInterfaceImplemented(InterfaceInfo);
352 getPassRegistry()->registerAnalysisGroup(InterfaceInfo, IIPI, isDefault);
357 //===----------------------------------------------------------------------===//
358 // PassRegistrationListener implementation
361 // PassRegistrationListener ctor - Add the current object to the list of
362 // PassRegistrationListeners...
363 PassRegistrationListener::PassRegistrationListener() {
364 sys::SmartScopedLock<true> Lock(ListenersLock);
365 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
366 Listeners->push_back(this);
369 // dtor - Remove object from list of listeners...
370 PassRegistrationListener::~PassRegistrationListener() {
371 sys::SmartScopedLock<true> Lock(ListenersLock);
372 std::vector<PassRegistrationListener*>::iterator I =
373 std::find(Listeners->begin(), Listeners->end(), this);
374 assert(Listeners && I != Listeners->end() &&
375 "PassRegistrationListener not registered!");
378 if (Listeners->empty()) {
384 // enumeratePasses - Iterate over the registered passes, calling the
385 // passEnumerate callback on each PassInfo object.
387 void PassRegistrationListener::enumeratePasses() {
388 getPassRegistry()->enumerateWith(this);
391 PassNameParser::~PassNameParser() {}
393 //===----------------------------------------------------------------------===//
394 // AnalysisUsage Class Implementation
398 struct GetCFGOnlyPasses : public PassRegistrationListener {
399 typedef AnalysisUsage::VectorType VectorType;
400 VectorType &CFGOnlyList;
401 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
403 void passEnumerate(const PassInfo *P) {
404 if (P->isCFGOnlyPass())
405 CFGOnlyList.push_back(P);
410 // setPreservesCFG - This function should be called to by the pass, iff they do
413 // 1. Add or remove basic blocks from the function
414 // 2. Modify terminator instructions in any way.
416 // This function annotates the AnalysisUsage info object to say that analyses
417 // that only depend on the CFG are preserved by this pass.
419 void AnalysisUsage::setPreservesCFG() {
420 // Since this transformation doesn't modify the CFG, it preserves all analyses
421 // that only depend on the CFG (like dominators, loop info, etc...)
422 GetCFGOnlyPasses(Preserved).enumeratePasses();
425 AnalysisUsage &AnalysisUsage::addRequiredID(AnalysisID ID) {
426 assert(ID && "Pass class not registered!");
427 Required.push_back(ID);
431 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(AnalysisID ID) {
432 assert(ID && "Pass class not registered!");
433 Required.push_back(ID);
434 RequiredTransitive.push_back(ID);