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/StringMap.h"
21 #include "llvm/Assembly/PrintModulePass.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/PassNameParser.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"
30 //===----------------------------------------------------------------------===//
31 // Pass Implementation
34 Pass::Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
36 // Force out-of-line virtual method.
41 // Force out-of-line virtual method.
42 ModulePass::~ModulePass() { }
44 Pass *ModulePass::createPrinterPass(raw_ostream &O,
45 const std::string &Banner) const {
46 return createPrintModulePass(&O, false, Banner);
49 PassManagerType ModulePass::getPotentialPassManagerType() const {
50 return PMT_ModulePassManager;
53 bool Pass::mustPreserveAnalysisID(char &AID) const {
54 return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
57 // dumpPassStructure - Implement the -debug-passes=Structure option
58 void Pass::dumpPassStructure(unsigned Offset) {
59 dbgs().indent(Offset*2) << getPassName() << "\n";
62 /// getPassName - Return a nice clean name for a pass. This usually
63 /// implemented in terms of the name that is registered by one of the
64 /// Registration templates, but can be overloaded directly.
66 const char *Pass::getPassName() const {
67 AnalysisID AID = getPassID();
68 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
70 return PI->getPassName();
71 return "Unnamed pass: implement Pass::getPassName()";
74 void Pass::preparePassManager(PMStack &) {
75 // By default, don't do anything.
78 PassManagerType Pass::getPotentialPassManagerType() const {
79 // Default implementation.
83 void Pass::getAnalysisUsage(AnalysisUsage &) const {
84 // By default, no analysis results are used, all are invalidated.
87 void Pass::releaseMemory() {
88 // By default, don't do anything.
91 void Pass::verifyAnalysis() const {
92 // By default, don't do anything.
95 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
99 ImmutablePass *Pass::getAsImmutablePass() {
103 PMDataManager *Pass::getAsPMDataManager() {
107 void Pass::setResolver(AnalysisResolver *AR) {
108 assert(!Resolver && "Resolver is already set");
112 // print - Print out the internal state of the pass. This is called by Analyze
113 // to print out the contents of an analysis. Otherwise it is not necessary to
114 // implement this method.
116 void Pass::print(raw_ostream &O,const Module*) const {
117 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
120 // dump - call print(cerr);
121 void Pass::dump() const {
125 //===----------------------------------------------------------------------===//
126 // ImmutablePass Implementation
128 // Force out-of-line virtual method.
129 ImmutablePass::~ImmutablePass() { }
131 void ImmutablePass::initializePass() {
132 // By default, don't do anything.
135 //===----------------------------------------------------------------------===//
136 // FunctionPass Implementation
139 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
140 const std::string &Banner) const {
141 return createPrintFunctionPass(Banner, &O);
144 bool FunctionPass::doInitialization(Module &) {
145 // By default, don't do anything.
149 bool FunctionPass::doFinalization(Module &) {
150 // By default, don't do anything.
154 PassManagerType FunctionPass::getPotentialPassManagerType() const {
155 return PMT_FunctionPassManager;
158 //===----------------------------------------------------------------------===//
159 // BasicBlockPass Implementation
162 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
163 const std::string &Banner) const {
165 llvm_unreachable("BasicBlockPass printing unsupported.");
169 // To run this pass on a function, we simply call runOnBasicBlock once for each
172 bool BasicBlockPass::runOnFunction(Function &F) {
173 bool Changed = doInitialization(F);
174 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
175 Changed |= runOnBasicBlock(*I);
176 return Changed | doFinalization(F);
179 bool BasicBlockPass::doInitialization(Module &) {
180 // By default, don't do anything.
184 bool BasicBlockPass::doInitialization(Function &) {
185 // By default, don't do anything.
189 bool BasicBlockPass::doFinalization(Function &) {
190 // By default, don't do anything.
194 bool BasicBlockPass::doFinalization(Module &) {
195 // By default, don't do anything.
199 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
200 return PMT_BasicBlockPassManager;
203 const PassInfo *Pass::lookupPassInfo(const void *TI) {
204 return PassRegistry::getPassRegistry()->getPassInfo(TI);
207 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
208 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
211 Pass *PassInfo::createPass() const {
212 assert((!isAnalysisGroup() || NormalCtor) &&
213 "No default implementation found for analysis group!");
215 "Cannot call createPass on PassInfo without default ctor!");
219 //===----------------------------------------------------------------------===//
220 // Analysis Group Implementation Code
221 //===----------------------------------------------------------------------===//
223 // RegisterAGBase implementation
225 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
226 const void *PassID, bool isDefault)
227 : PassInfo(Name, InterfaceID) {
228 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
233 //===----------------------------------------------------------------------===//
234 // PassRegistrationListener implementation
237 // PassRegistrationListener ctor - Add the current object to the list of
238 // PassRegistrationListeners...
239 PassRegistrationListener::PassRegistrationListener() {
240 PassRegistry::getPassRegistry()->addRegistrationListener(this);
243 // dtor - Remove object from list of listeners...
244 PassRegistrationListener::~PassRegistrationListener() {
245 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
248 // enumeratePasses - Iterate over the registered passes, calling the
249 // passEnumerate callback on each PassInfo object.
251 void PassRegistrationListener::enumeratePasses() {
252 PassRegistry::getPassRegistry()->enumerateWith(this);
255 PassNameParser::~PassNameParser() {}
257 //===----------------------------------------------------------------------===//
258 // AnalysisUsage Class Implementation
262 struct GetCFGOnlyPasses : public PassRegistrationListener {
263 typedef AnalysisUsage::VectorType VectorType;
264 VectorType &CFGOnlyList;
265 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
267 void passEnumerate(const PassInfo *P) {
268 if (P->isCFGOnlyPass())
269 CFGOnlyList.push_back(P->getTypeInfo());
274 // setPreservesCFG - This function should be called to by the pass, iff they do
277 // 1. Add or remove basic blocks from the function
278 // 2. Modify terminator instructions in any way.
280 // This function annotates the AnalysisUsage info object to say that analyses
281 // that only depend on the CFG are preserved by this pass.
283 void AnalysisUsage::setPreservesCFG() {
284 // Since this transformation doesn't modify the CFG, it preserves all analyses
285 // that only depend on the CFG (like dominators, loop info, etc...)
286 GetCFGOnlyPasses(Preserved).enumeratePasses();
289 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
290 const PassInfo *PI = Pass::lookupPassInfo(Arg);
291 // If the pass exists, preserve it. Otherwise silently do nothing.
292 if (PI) Preserved.push_back(PI->getTypeInfo());
296 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
297 Required.push_back(ID);
301 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
302 Required.push_back(&ID);
306 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
307 Required.push_back(&ID);
308 RequiredTransitive.push_back(&ID);