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/PassRegistry.h"
18 #include "llvm/Assembly/PrintModulePass.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/PassNameParser.h"
21 #include "llvm/Support/raw_ostream.h"
24 //===----------------------------------------------------------------------===//
25 // Pass Implementation
28 // Force out-of-line virtual method.
33 // Force out-of-line virtual method.
34 ModulePass::~ModulePass() { }
36 Pass *ModulePass::createPrinterPass(raw_ostream &O,
37 const std::string &Banner) const {
38 return createPrintModulePass(&O, false, Banner);
41 PassManagerType ModulePass::getPotentialPassManagerType() const {
42 return PMT_ModulePassManager;
45 bool Pass::mustPreserveAnalysisID(char &AID) const {
46 return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
49 // dumpPassStructure - Implement the -debug-pass=Structure option
50 void Pass::dumpPassStructure(unsigned Offset) {
51 dbgs().indent(Offset*2) << getPassName() << "\n";
54 /// getPassName - Return a nice clean name for a pass. This usually
55 /// implemented in terms of the name that is registered by one of the
56 /// Registration templates, but can be overloaded directly.
58 const char *Pass::getPassName() const {
59 AnalysisID AID = getPassID();
60 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
62 return PI->getPassName();
63 return "Unnamed pass: implement Pass::getPassName()";
66 void Pass::preparePassManager(PMStack &) {
67 // By default, don't do anything.
70 PassManagerType Pass::getPotentialPassManagerType() const {
71 // Default implementation.
75 void Pass::getAnalysisUsage(AnalysisUsage &) const {
76 // By default, no analysis results are used, all are invalidated.
79 void Pass::releaseMemory() {
80 // By default, don't do anything.
83 void Pass::verifyAnalysis() const {
84 // By default, don't do anything.
87 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
91 ImmutablePass *Pass::getAsImmutablePass() {
95 PMDataManager *Pass::getAsPMDataManager() {
99 void Pass::setResolver(AnalysisResolver *AR) {
100 assert(!Resolver && "Resolver is already set");
104 // print - Print out the internal state of the pass. This is called by Analyze
105 // to print out the contents of an analysis. Otherwise it is not necessary to
106 // implement this method.
108 void Pass::print(raw_ostream &O,const Module*) const {
109 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
112 // dump - call print(cerr);
113 void Pass::dump() const {
117 //===----------------------------------------------------------------------===//
118 // ImmutablePass Implementation
120 // Force out-of-line virtual method.
121 ImmutablePass::~ImmutablePass() { }
123 void ImmutablePass::initializePass() {
124 // By default, don't do anything.
127 //===----------------------------------------------------------------------===//
128 // FunctionPass Implementation
131 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
132 const std::string &Banner) const {
133 return createPrintFunctionPass(Banner, &O);
136 bool FunctionPass::doInitialization(Module &) {
137 // By default, don't do anything.
141 bool FunctionPass::doFinalization(Module &) {
142 // By default, don't do anything.
146 PassManagerType FunctionPass::getPotentialPassManagerType() const {
147 return PMT_FunctionPassManager;
150 //===----------------------------------------------------------------------===//
151 // BasicBlockPass Implementation
154 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
155 const std::string &Banner) const {
157 llvm_unreachable("BasicBlockPass printing unsupported.");
161 bool BasicBlockPass::doInitialization(Module &) {
162 // By default, don't do anything.
166 bool BasicBlockPass::doInitialization(Function &) {
167 // By default, don't do anything.
171 bool BasicBlockPass::doFinalization(Function &) {
172 // By default, don't do anything.
176 bool BasicBlockPass::doFinalization(Module &) {
177 // By default, don't do anything.
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 *PassInfo::createPass() const {
194 assert((!isAnalysisGroup() || NormalCtor) &&
195 "No default implementation found for analysis group!");
197 "Cannot call createPass on PassInfo without default ctor!");
201 //===----------------------------------------------------------------------===//
202 // Analysis Group Implementation Code
203 //===----------------------------------------------------------------------===//
205 // RegisterAGBase implementation
207 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
208 const void *PassID, bool isDefault)
209 : PassInfo(Name, InterfaceID) {
210 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
214 //===----------------------------------------------------------------------===//
215 // PassRegistrationListener implementation
218 // PassRegistrationListener ctor - Add the current object to the list of
219 // PassRegistrationListeners...
220 PassRegistrationListener::PassRegistrationListener() {
221 PassRegistry::getPassRegistry()->addRegistrationListener(this);
224 // dtor - Remove object from list of listeners...
225 PassRegistrationListener::~PassRegistrationListener() {
226 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
229 // enumeratePasses - Iterate over the registered passes, calling the
230 // passEnumerate callback on each PassInfo object.
232 void PassRegistrationListener::enumeratePasses() {
233 PassRegistry::getPassRegistry()->enumerateWith(this);
236 PassNameParser::~PassNameParser() {}
238 //===----------------------------------------------------------------------===//
239 // AnalysisUsage Class Implementation
243 struct GetCFGOnlyPasses : public PassRegistrationListener {
244 typedef AnalysisUsage::VectorType VectorType;
245 VectorType &CFGOnlyList;
246 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
248 void passEnumerate(const PassInfo *P) {
249 if (P->isCFGOnlyPass())
250 CFGOnlyList.push_back(P->getTypeInfo());
255 // setPreservesCFG - This function should be called to by the pass, iff they do
258 // 1. Add or remove basic blocks from the function
259 // 2. Modify terminator instructions in any way.
261 // This function annotates the AnalysisUsage info object to say that analyses
262 // that only depend on the CFG are preserved by this pass.
264 void AnalysisUsage::setPreservesCFG() {
265 // Since this transformation doesn't modify the CFG, it preserves all analyses
266 // that only depend on the CFG (like dominators, loop info, etc...)
267 GetCFGOnlyPasses(Preserved).enumeratePasses();
270 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
271 const PassInfo *PI = Pass::lookupPassInfo(Arg);
272 // If the pass exists, preserve it. Otherwise silently do nothing.
273 if (PI) Preserved.push_back(PI->getTypeInfo());
277 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
278 Required.push_back(ID);
282 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
283 Required.push_back(&ID);
287 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
288 Required.push_back(&ID);
289 RequiredTransitive.push_back(&ID);