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 Pass::Pass(PassKind K
, char &pid
) : Resolver(0), PassID(&pid
), Kind(K
) { }
30 // Force out-of-line virtual method.
35 // Force out-of-line virtual method.
36 ModulePass::~ModulePass() { }
38 Pass
*ModulePass::createPrinterPass(raw_ostream
&O
,
39 const std::string
&Banner
) const {
40 return createPrintModulePass(&O
, false, Banner
);
43 PassManagerType
ModulePass::getPotentialPassManagerType() const {
44 return PMT_ModulePassManager
;
47 bool Pass::mustPreserveAnalysisID(char &AID
) const {
48 return Resolver
->getAnalysisIfAvailable(&AID
, true) != 0;
51 // dumpPassStructure - Implement the -debug-passes=Structure option
52 void Pass::dumpPassStructure(unsigned Offset
) {
53 dbgs().indent(Offset
*2) << getPassName() << "\n";
56 /// getPassName - Return a nice clean name for a pass. This usually
57 /// implemented in terms of the name that is registered by one of the
58 /// Registration templates, but can be overloaded directly.
60 const char *Pass::getPassName() const {
61 AnalysisID AID
= getPassID();
62 const PassInfo
*PI
= PassRegistry::getPassRegistry()->getPassInfo(AID
);
64 return PI
->getPassName();
65 return "Unnamed pass: implement Pass::getPassName()";
68 void Pass::preparePassManager(PMStack
&) {
69 // By default, don't do anything.
72 PassManagerType
Pass::getPotentialPassManagerType() const {
73 // Default implementation.
77 void Pass::getAnalysisUsage(AnalysisUsage
&) const {
78 // By default, no analysis results are used, all are invalidated.
81 void Pass::releaseMemory() {
82 // By default, don't do anything.
85 void Pass::verifyAnalysis() const {
86 // By default, don't do anything.
89 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID
) {
93 ImmutablePass
*Pass::getAsImmutablePass() {
97 PMDataManager
*Pass::getAsPMDataManager() {
101 void Pass::setResolver(AnalysisResolver
*AR
) {
102 assert(!Resolver
&& "Resolver is already set");
106 // print - Print out the internal state of the pass. This is called by Analyze
107 // to print out the contents of an analysis. Otherwise it is not necessary to
108 // implement this method.
110 void Pass::print(raw_ostream
&O
,const Module
*) const {
111 O
<< "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
114 // dump - call print(cerr);
115 void Pass::dump() const {
119 //===----------------------------------------------------------------------===//
120 // ImmutablePass Implementation
122 // Force out-of-line virtual method.
123 ImmutablePass::~ImmutablePass() { }
125 void ImmutablePass::initializePass() {
126 // By default, don't do anything.
129 //===----------------------------------------------------------------------===//
130 // FunctionPass Implementation
133 Pass
*FunctionPass::createPrinterPass(raw_ostream
&O
,
134 const std::string
&Banner
) const {
135 return createPrintFunctionPass(Banner
, &O
);
138 bool FunctionPass::doInitialization(Module
&) {
139 // By default, don't do anything.
143 bool FunctionPass::doFinalization(Module
&) {
144 // By default, don't do anything.
148 PassManagerType
FunctionPass::getPotentialPassManagerType() const {
149 return PMT_FunctionPassManager
;
152 //===----------------------------------------------------------------------===//
153 // BasicBlockPass Implementation
156 Pass
*BasicBlockPass::createPrinterPass(raw_ostream
&O
,
157 const std::string
&Banner
) const {
159 llvm_unreachable("BasicBlockPass printing unsupported.");
163 bool BasicBlockPass::doInitialization(Module
&) {
164 // By default, don't do anything.
168 bool BasicBlockPass::doInitialization(Function
&) {
169 // By default, don't do anything.
173 bool BasicBlockPass::doFinalization(Function
&) {
174 // By default, don't do anything.
178 bool BasicBlockPass::doFinalization(Module
&) {
179 // By default, don't do anything.
183 PassManagerType
BasicBlockPass::getPotentialPassManagerType() const {
184 return PMT_BasicBlockPassManager
;
187 const PassInfo
*Pass::lookupPassInfo(const void *TI
) {
188 return PassRegistry::getPassRegistry()->getPassInfo(TI
);
191 const PassInfo
*Pass::lookupPassInfo(StringRef Arg
) {
192 return PassRegistry::getPassRegistry()->getPassInfo(Arg
);
195 Pass
*PassInfo::createPass() const {
196 assert((!isAnalysisGroup() || NormalCtor
) &&
197 "No default implementation found for analysis group!");
199 "Cannot call createPass on PassInfo without default ctor!");
203 //===----------------------------------------------------------------------===//
204 // Analysis Group Implementation Code
205 //===----------------------------------------------------------------------===//
207 // RegisterAGBase implementation
209 RegisterAGBase::RegisterAGBase(const char *Name
, const void *InterfaceID
,
210 const void *PassID
, bool isDefault
)
211 : PassInfo(Name
, InterfaceID
) {
212 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID
, PassID
,
216 //===----------------------------------------------------------------------===//
217 // PassRegistrationListener implementation
220 // PassRegistrationListener ctor - Add the current object to the list of
221 // PassRegistrationListeners...
222 PassRegistrationListener::PassRegistrationListener() {
223 PassRegistry::getPassRegistry()->addRegistrationListener(this);
226 // dtor - Remove object from list of listeners...
227 PassRegistrationListener::~PassRegistrationListener() {
228 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
231 // enumeratePasses - Iterate over the registered passes, calling the
232 // passEnumerate callback on each PassInfo object.
234 void PassRegistrationListener::enumeratePasses() {
235 PassRegistry::getPassRegistry()->enumerateWith(this);
238 PassNameParser::~PassNameParser() {}
240 //===----------------------------------------------------------------------===//
241 // AnalysisUsage Class Implementation
245 struct GetCFGOnlyPasses
: public PassRegistrationListener
{
246 typedef AnalysisUsage::VectorType VectorType
;
247 VectorType
&CFGOnlyList
;
248 GetCFGOnlyPasses(VectorType
&L
) : CFGOnlyList(L
) {}
250 void passEnumerate(const PassInfo
*P
) {
251 if (P
->isCFGOnlyPass())
252 CFGOnlyList
.push_back(P
->getTypeInfo());
257 // setPreservesCFG - This function should be called to by the pass, iff they do
260 // 1. Add or remove basic blocks from the function
261 // 2. Modify terminator instructions in any way.
263 // This function annotates the AnalysisUsage info object to say that analyses
264 // that only depend on the CFG are preserved by this pass.
266 void AnalysisUsage::setPreservesCFG() {
267 // Since this transformation doesn't modify the CFG, it preserves all analyses
268 // that only depend on the CFG (like dominators, loop info, etc...)
269 GetCFGOnlyPasses(Preserved
).enumeratePasses();
272 AnalysisUsage
&AnalysisUsage::addPreserved(StringRef Arg
) {
273 const PassInfo
*PI
= Pass::lookupPassInfo(Arg
);
274 // If the pass exists, preserve it. Otherwise silently do nothing.
275 if (PI
) Preserved
.push_back(PI
->getTypeInfo());
279 AnalysisUsage
&AnalysisUsage::addRequiredID(const void *ID
) {
280 Required
.push_back(ID
);
284 AnalysisUsage
&AnalysisUsage::addRequiredID(char &ID
) {
285 Required
.push_back(&ID
);
289 AnalysisUsage
&AnalysisUsage::addRequiredTransitiveID(char &ID
) {
290 Required
.push_back(&ID
);
291 RequiredTransitive
.push_back(&ID
);