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/IR/Function.h"
18 #include "llvm/IR/IRPrintingPasses.h"
19 #include "llvm/PassRegistry.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/PassNameParser.h"
22 #include "llvm/Support/raw_ostream.h"
25 //===----------------------------------------------------------------------===//
26 // Pass Implementation
29 // Force out-of-line virtual method.
34 // Force out-of-line virtual method.
35 ModulePass::~ModulePass() { }
37 Pass *ModulePass::createPrinterPass(raw_ostream &O,
38 const std::string &Banner) const {
39 return createPrintModulePass(O, Banner);
42 PassManagerType ModulePass::getPotentialPassManagerType() const {
43 return PMT_ModulePassManager;
46 bool Pass::mustPreserveAnalysisID(char &AID) const {
47 return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
50 // dumpPassStructure - Implement the -debug-pass=Structure option
51 void Pass::dumpPassStructure(unsigned Offset) {
52 dbgs().indent(Offset*2) << getPassName() << "\n";
55 /// getPassName - Return a nice clean name for a pass. This usually
56 /// implemented in terms of the name that is registered by one of the
57 /// Registration templates, but can be overloaded directly.
59 const char *Pass::getPassName() const {
60 AnalysisID AID = getPassID();
61 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
63 return PI->getPassName();
64 return "Unnamed pass: implement Pass::getPassName()";
67 void Pass::preparePassManager(PMStack &) {
68 // By default, don't do anything.
71 PassManagerType Pass::getPotentialPassManagerType() const {
72 // Default implementation.
76 void Pass::getAnalysisUsage(AnalysisUsage &) const {
77 // By default, no analysis results are used, all are invalidated.
80 void Pass::releaseMemory() {
81 // By default, don't do anything.
84 void Pass::verifyAnalysis() const {
85 // By default, don't do anything.
88 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
92 ImmutablePass *Pass::getAsImmutablePass() {
96 PMDataManager *Pass::getAsPMDataManager() {
100 void Pass::setResolver(AnalysisResolver *AR) {
101 assert(!Resolver && "Resolver is already set");
105 // print - Print out the internal state of the pass. This is called by Analyze
106 // to print out the contents of an analysis. Otherwise it is not necessary to
107 // implement this method.
109 void Pass::print(raw_ostream &O,const Module*) const {
110 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
113 // dump - call print(cerr);
114 void Pass::dump() const {
118 //===----------------------------------------------------------------------===//
119 // ImmutablePass Implementation
121 // Force out-of-line virtual method.
122 ImmutablePass::~ImmutablePass() { }
124 void ImmutablePass::initializePass() {
125 // By default, don't do anything.
128 //===----------------------------------------------------------------------===//
129 // FunctionPass Implementation
132 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
133 const std::string &Banner) const {
134 return createPrintFunctionPass(O, Banner);
137 PassManagerType FunctionPass::getPotentialPassManagerType() const {
138 return PMT_FunctionPassManager;
141 bool FunctionPass::skipOptnoneFunction(Function &F) const {
142 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
143 DEBUG(dbgs() << "Skipping pass '" << getPassName()
144 << "' on function " << F.getName() << "\n");
150 //===----------------------------------------------------------------------===//
151 // BasicBlockPass Implementation
154 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
155 const std::string &Banner) const {
156 return createPrintBasicBlockPass(O, Banner);
159 bool BasicBlockPass::doInitialization(Function &) {
160 // By default, don't do anything.
164 bool BasicBlockPass::doFinalization(Function &) {
165 // By default, don't do anything.
169 bool BasicBlockPass::skipOptnoneFunction(BasicBlock &BB) const {
170 Function *F = BB.getParent();
171 if (F && F->hasFnAttribute(Attribute::OptimizeNone)) {
172 // Report this only once per function.
173 if (&BB == &F->getEntryBlock())
174 DEBUG(dbgs() << "Skipping pass '" << getPassName()
175 << "' on function " << F->getName() << "\n");
181 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
182 return PMT_BasicBlockPassManager;
185 const PassInfo *Pass::lookupPassInfo(const void *TI) {
186 return PassRegistry::getPassRegistry()->getPassInfo(TI);
189 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
190 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
193 Pass *Pass::createPass(AnalysisID ID) {
194 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
197 return PI->createPass();
200 Pass *PassInfo::createPass() const {
201 assert((!isAnalysisGroup() || NormalCtor) &&
202 "No default implementation found for analysis group!");
204 "Cannot call createPass on PassInfo without default ctor!");
208 //===----------------------------------------------------------------------===//
209 // Analysis Group Implementation Code
210 //===----------------------------------------------------------------------===//
212 // RegisterAGBase implementation
214 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
215 const void *PassID, bool isDefault)
216 : PassInfo(Name, InterfaceID) {
217 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
221 //===----------------------------------------------------------------------===//
222 // PassRegistrationListener implementation
225 // PassRegistrationListener ctor - Add the current object to the list of
226 // PassRegistrationListeners...
227 PassRegistrationListener::PassRegistrationListener() {
228 PassRegistry::getPassRegistry()->addRegistrationListener(this);
231 // dtor - Remove object from list of listeners...
232 PassRegistrationListener::~PassRegistrationListener() {
233 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
236 // enumeratePasses - Iterate over the registered passes, calling the
237 // passEnumerate callback on each PassInfo object.
239 void PassRegistrationListener::enumeratePasses() {
240 PassRegistry::getPassRegistry()->enumerateWith(this);
243 PassNameParser::~PassNameParser() {}
245 //===----------------------------------------------------------------------===//
246 // AnalysisUsage Class Implementation
250 struct GetCFGOnlyPasses : public PassRegistrationListener {
251 typedef AnalysisUsage::VectorType VectorType;
252 VectorType &CFGOnlyList;
253 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
255 void passEnumerate(const PassInfo *P) {
256 if (P->isCFGOnlyPass())
257 CFGOnlyList.push_back(P->getTypeInfo());
262 // setPreservesCFG - This function should be called to by the pass, iff they do
265 // 1. Add or remove basic blocks from the function
266 // 2. Modify terminator instructions in any way.
268 // This function annotates the AnalysisUsage info object to say that analyses
269 // that only depend on the CFG are preserved by this pass.
271 void AnalysisUsage::setPreservesCFG() {
272 // Since this transformation doesn't modify the CFG, it preserves all analyses
273 // that only depend on the CFG (like dominators, loop info, etc...)
274 GetCFGOnlyPasses(Preserved).enumeratePasses();
277 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
278 const PassInfo *PI = Pass::lookupPassInfo(Arg);
279 // If the pass exists, preserve it. Otherwise silently do nothing.
280 if (PI) Preserved.push_back(PI->getTypeInfo());
284 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
285 Required.push_back(ID);
289 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
290 Required.push_back(&ID);
294 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
295 Required.push_back(&ID);
296 RequiredTransitive.push_back(&ID);