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/IR/LegacyPassNameParser.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/IR/OptBisect.h"
22 #include "llvm/PassRegistry.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "ir"
29 //===----------------------------------------------------------------------===//
30 // Pass Implementation
33 // Force out-of-line virtual method.
38 // Force out-of-line virtual method.
39 ModulePass::~ModulePass() { }
41 Pass
*ModulePass::createPrinterPass(raw_ostream
&O
,
42 const std::string
&Banner
) const {
43 return createPrintModulePass(O
, Banner
);
46 PassManagerType
ModulePass::getPotentialPassManagerType() const {
47 return PMT_ModulePassManager
;
50 bool ModulePass::skipModule(Module
&M
) const {
51 return !M
.getContext().getOptBisect().shouldRunPass(this, M
);
54 bool Pass::mustPreserveAnalysisID(char &AID
) const {
55 return Resolver
->getAnalysisIfAvailable(&AID
, true) != nullptr;
58 // dumpPassStructure - Implement the -debug-pass=Structure option
59 void Pass::dumpPassStructure(unsigned Offset
) {
60 dbgs().indent(Offset
*2) << getPassName() << "\n";
63 /// getPassName - Return a nice clean name for a pass. This usually
64 /// implemented in terms of the name that is registered by one of the
65 /// Registration templates, but can be overloaded directly.
67 StringRef
Pass::getPassName() const {
68 AnalysisID AID
= getPassID();
69 const PassInfo
*PI
= PassRegistry::getPassRegistry()->getPassInfo(AID
);
71 return PI
->getPassName();
72 return "Unnamed pass: implement Pass::getPassName()";
75 void Pass::preparePassManager(PMStack
&) {
76 // By default, don't do anything.
79 PassManagerType
Pass::getPotentialPassManagerType() const {
80 // Default implementation.
84 void Pass::getAnalysisUsage(AnalysisUsage
&) const {
85 // By default, no analysis results are used, all are invalidated.
88 void Pass::releaseMemory() {
89 // By default, don't do anything.
92 void Pass::verifyAnalysis() const {
93 // By default, don't do anything.
96 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID
) {
100 ImmutablePass
*Pass::getAsImmutablePass() {
104 PMDataManager
*Pass::getAsPMDataManager() {
108 void Pass::setResolver(AnalysisResolver
*AR
) {
109 assert(!Resolver
&& "Resolver is already set");
113 // print - Print out the internal state of the pass. This is called by Analyze
114 // to print out the contents of an analysis. Otherwise it is not necessary to
115 // implement this method.
117 void Pass::print(raw_ostream
&O
,const Module
*) const {
118 O
<< "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
121 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
122 // dump - call print(cerr);
123 LLVM_DUMP_METHOD
void Pass::dump() const {
124 print(dbgs(), nullptr);
128 //===----------------------------------------------------------------------===//
129 // ImmutablePass Implementation
131 // Force out-of-line virtual method.
132 ImmutablePass::~ImmutablePass() { }
134 void ImmutablePass::initializePass() {
135 // By default, don't do anything.
138 //===----------------------------------------------------------------------===//
139 // FunctionPass Implementation
142 Pass
*FunctionPass::createPrinterPass(raw_ostream
&O
,
143 const std::string
&Banner
) const {
144 return createPrintFunctionPass(O
, Banner
);
147 PassManagerType
FunctionPass::getPotentialPassManagerType() const {
148 return PMT_FunctionPassManager
;
151 bool FunctionPass::skipFunction(const Function
&F
) const {
152 if (!F
.getContext().getOptBisect().shouldRunPass(this, F
))
155 if (F
.hasFnAttribute(Attribute::OptimizeNone
)) {
156 DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
157 << F
.getName() << "\n");
163 //===----------------------------------------------------------------------===//
164 // BasicBlockPass Implementation
167 Pass
*BasicBlockPass::createPrinterPass(raw_ostream
&O
,
168 const std::string
&Banner
) const {
169 return createPrintBasicBlockPass(O
, Banner
);
172 bool BasicBlockPass::doInitialization(Function
&) {
173 // By default, don't do anything.
177 bool BasicBlockPass::doFinalization(Function
&) {
178 // By default, don't do anything.
182 bool BasicBlockPass::skipBasicBlock(const BasicBlock
&BB
) const {
183 const Function
*F
= BB
.getParent();
186 if (!F
->getContext().getOptBisect().shouldRunPass(this, BB
))
188 if (F
->hasFnAttribute(Attribute::OptimizeNone
)) {
189 // Report this only once per function.
190 if (&BB
== &F
->getEntryBlock())
191 DEBUG(dbgs() << "Skipping pass '" << getPassName()
192 << "' on function " << F
->getName() << "\n");
198 PassManagerType
BasicBlockPass::getPotentialPassManagerType() const {
199 return PMT_BasicBlockPassManager
;
202 const PassInfo
*Pass::lookupPassInfo(const void *TI
) {
203 return PassRegistry::getPassRegistry()->getPassInfo(TI
);
206 const PassInfo
*Pass::lookupPassInfo(StringRef Arg
) {
207 return PassRegistry::getPassRegistry()->getPassInfo(Arg
);
210 Pass
*Pass::createPass(AnalysisID ID
) {
211 const PassInfo
*PI
= PassRegistry::getPassRegistry()->getPassInfo(ID
);
214 return PI
->createPass();
217 //===----------------------------------------------------------------------===//
218 // Analysis Group Implementation Code
219 //===----------------------------------------------------------------------===//
221 // RegisterAGBase implementation
223 RegisterAGBase::RegisterAGBase(StringRef Name
, const void *InterfaceID
,
224 const void *PassID
, bool isDefault
)
225 : PassInfo(Name
, InterfaceID
) {
226 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID
, PassID
,
230 //===----------------------------------------------------------------------===//
231 // PassRegistrationListener implementation
234 // enumeratePasses - Iterate over the registered passes, calling the
235 // passEnumerate callback on each PassInfo object.
237 void PassRegistrationListener::enumeratePasses() {
238 PassRegistry::getPassRegistry()->enumerateWith(this);
241 PassNameParser::PassNameParser(cl::Option
&O
)
242 : cl::parser
<const PassInfo
*>(O
) {
243 PassRegistry::getPassRegistry()->addRegistrationListener(this);
246 PassNameParser::~PassNameParser() {
247 // This only gets called during static destruction, in which case the
248 // PassRegistry will have already been destroyed by llvm_shutdown(). So
249 // attempting to remove the registration listener is an error.
252 //===----------------------------------------------------------------------===//
253 // AnalysisUsage Class Implementation
257 struct GetCFGOnlyPasses
: public PassRegistrationListener
{
258 typedef AnalysisUsage::VectorType VectorType
;
259 VectorType
&CFGOnlyList
;
260 GetCFGOnlyPasses(VectorType
&L
) : CFGOnlyList(L
) {}
262 void passEnumerate(const PassInfo
*P
) override
{
263 if (P
->isCFGOnlyPass())
264 CFGOnlyList
.push_back(P
->getTypeInfo());
269 // setPreservesCFG - This function should be called to by the pass, iff they do
272 // 1. Add or remove basic blocks from the function
273 // 2. Modify terminator instructions in any way.
275 // This function annotates the AnalysisUsage info object to say that analyses
276 // that only depend on the CFG are preserved by this pass.
278 void AnalysisUsage::setPreservesCFG() {
279 // Since this transformation doesn't modify the CFG, it preserves all analyses
280 // that only depend on the CFG (like dominators, loop info, etc...)
281 GetCFGOnlyPasses(Preserved
).enumeratePasses();
284 AnalysisUsage
&AnalysisUsage::addPreserved(StringRef Arg
) {
285 const PassInfo
*PI
= Pass::lookupPassInfo(Arg
);
286 // If the pass exists, preserve it. Otherwise silently do nothing.
287 if (PI
) Preserved
.push_back(PI
->getTypeInfo());
291 AnalysisUsage
&AnalysisUsage::addRequiredID(const void *ID
) {
292 Required
.push_back(ID
);
296 AnalysisUsage
&AnalysisUsage::addRequiredID(char &ID
) {
297 Required
.push_back(&ID
);
301 AnalysisUsage
&AnalysisUsage::addRequiredTransitiveID(char &ID
) {
302 Required
.push_back(&ID
);
303 RequiredTransitive
.push_back(&ID
);