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, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
41 // Force out-of-line virtual method.
46 // Force out-of-line virtual method.
47 ModulePass::~ModulePass() { }
49 Pass *ModulePass::createPrinterPass(raw_ostream &O,
50 const std::string &Banner) const {
51 return createPrintModulePass(&O, false, Banner);
54 PassManagerType ModulePass::getPotentialPassManagerType() const {
55 return PMT_ModulePassManager;
58 bool Pass::mustPreserveAnalysisID(char &AID) const {
59 return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
62 // dumpPassStructure - Implement the -debug-passes=Structure option
63 void Pass::dumpPassStructure(unsigned Offset) {
64 dbgs().indent(Offset*2) << getPassName() << "\n";
67 /// getPassName - Return a nice clean name for a pass. This usually
68 /// implemented in terms of the name that is registered by one of the
69 /// Registration templates, but can be overloaded directly.
71 const char *Pass::getPassName() const {
72 AnalysisID AID = getPassID();
73 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
75 return PI->getPassName();
76 return "Unnamed pass: implement Pass::getPassName()";
79 void Pass::preparePassManager(PMStack &) {
80 // By default, don't do anything.
83 PassManagerType Pass::getPotentialPassManagerType() const {
84 // Default implementation.
88 void Pass::getAnalysisUsage(AnalysisUsage &) const {
89 // By default, no analysis results are used, all are invalidated.
92 void Pass::releaseMemory() {
93 // By default, don't do anything.
96 void Pass::verifyAnalysis() const {
97 // By default, don't do anything.
100 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
104 ImmutablePass *Pass::getAsImmutablePass() {
108 PMDataManager *Pass::getAsPMDataManager() {
112 void Pass::setResolver(AnalysisResolver *AR) {
113 assert(!Resolver && "Resolver is already set");
117 // print - Print out the internal state of the pass. This is called by Analyze
118 // to print out the contents of an analysis. Otherwise it is not necessary to
119 // implement this method.
121 void Pass::print(raw_ostream &O,const Module*) const {
122 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
125 // dump - call print(cerr);
126 void Pass::dump() const {
130 //===----------------------------------------------------------------------===//
131 // ImmutablePass Implementation
133 // Force out-of-line virtual method.
134 ImmutablePass::~ImmutablePass() { }
136 void ImmutablePass::initializePass() {
137 // By default, don't do anything.
140 //===----------------------------------------------------------------------===//
141 // FunctionPass Implementation
144 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
145 const std::string &Banner) const {
146 return createPrintFunctionPass(Banner, &O);
149 // run - On a module, we run this pass by initializing, runOnFunction'ing once
150 // for every function in the module, then by finalizing.
152 bool FunctionPass::runOnModule(Module &M) {
153 bool Changed = doInitialization(M);
155 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
156 if (!I->isDeclaration()) // Passes are not run on external functions!
157 Changed |= runOnFunction(*I);
159 return Changed | doFinalization(M);
162 // run - On a function, we simply initialize, run the function, then finalize.
164 bool FunctionPass::run(Function &F) {
165 // Passes are not run on external functions!
166 if (F.isDeclaration()) return false;
168 bool Changed = doInitialization(*F.getParent());
169 Changed |= runOnFunction(F);
170 return Changed | doFinalization(*F.getParent());
173 bool FunctionPass::doInitialization(Module &) {
174 // By default, don't do anything.
178 bool FunctionPass::doFinalization(Module &) {
179 // By default, don't do anything.
183 PassManagerType FunctionPass::getPotentialPassManagerType() const {
184 return PMT_FunctionPassManager;
187 //===----------------------------------------------------------------------===//
188 // BasicBlockPass Implementation
191 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
192 const std::string &Banner) const {
194 llvm_unreachable("BasicBlockPass printing unsupported.");
198 // To run this pass on a function, we simply call runOnBasicBlock once for each
201 bool BasicBlockPass::runOnFunction(Function &F) {
202 bool Changed = doInitialization(F);
203 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
204 Changed |= runOnBasicBlock(*I);
205 return Changed | doFinalization(F);
208 bool BasicBlockPass::doInitialization(Module &) {
209 // By default, don't do anything.
213 bool BasicBlockPass::doInitialization(Function &) {
214 // By default, don't do anything.
218 bool BasicBlockPass::doFinalization(Function &) {
219 // By default, don't do anything.
223 bool BasicBlockPass::doFinalization(Module &) {
224 // By default, don't do anything.
228 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
229 return PMT_BasicBlockPassManager;
232 const PassInfo *Pass::lookupPassInfo(const void *TI) {
233 return PassRegistry::getPassRegistry()->getPassInfo(TI);
236 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
237 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
240 Pass *PassInfo::createPass() const {
241 assert((!isAnalysisGroup() || NormalCtor) &&
242 "No default implementation found for analysis group!");
244 "Cannot call createPass on PassInfo without default ctor!");
248 //===----------------------------------------------------------------------===//
249 // Analysis Group Implementation Code
250 //===----------------------------------------------------------------------===//
252 // RegisterAGBase implementation
254 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
255 const void *PassID, bool isDefault)
256 : PassInfo(Name, InterfaceID) {
257 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
262 //===----------------------------------------------------------------------===//
263 // PassRegistrationListener implementation
266 // PassRegistrationListener ctor - Add the current object to the list of
267 // PassRegistrationListeners...
268 PassRegistrationListener::PassRegistrationListener() {
269 PassRegistry::getPassRegistry()->addRegistrationListener(this);
272 // dtor - Remove object from list of listeners...
273 PassRegistrationListener::~PassRegistrationListener() {
274 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
277 // enumeratePasses - Iterate over the registered passes, calling the
278 // passEnumerate callback on each PassInfo object.
280 void PassRegistrationListener::enumeratePasses() {
281 PassRegistry::getPassRegistry()->enumerateWith(this);
284 PassNameParser::~PassNameParser() {}
286 //===----------------------------------------------------------------------===//
287 // AnalysisUsage Class Implementation
291 struct GetCFGOnlyPasses : public PassRegistrationListener {
292 typedef AnalysisUsage::VectorType VectorType;
293 VectorType &CFGOnlyList;
294 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
296 void passEnumerate(const PassInfo *P) {
297 if (P->isCFGOnlyPass())
298 CFGOnlyList.push_back(P->getTypeInfo());
303 // setPreservesCFG - This function should be called to by the pass, iff they do
306 // 1. Add or remove basic blocks from the function
307 // 2. Modify terminator instructions in any way.
309 // This function annotates the AnalysisUsage info object to say that analyses
310 // that only depend on the CFG are preserved by this pass.
312 void AnalysisUsage::setPreservesCFG() {
313 // Since this transformation doesn't modify the CFG, it preserves all analyses
314 // that only depend on the CFG (like dominators, loop info, etc...)
315 GetCFGOnlyPasses(Preserved).enumeratePasses();
318 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
319 const PassInfo *PI = Pass::lookupPassInfo(Arg);
320 // If the pass exists, preserve it. Otherwise silently do nothing.
321 if (PI) Preserved.push_back(PI->getTypeInfo());
325 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
326 Required.push_back(ID);
330 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
331 Required.push_back(&ID);
335 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
336 Required.push_back(&ID);
337 RequiredTransitive.push_back(&ID);