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/Config/llvm-config.h"
18 #include "llvm/IR/Attributes.h"
19 #include "llvm/IR/BasicBlock.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/IRPrintingPasses.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/LegacyPassNameParser.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/OptBisect.h"
26 #include "llvm/PassInfo.h"
27 #include "llvm/PassRegistry.h"
28 #include "llvm/PassSupport.h"
29 #include "llvm/Support/Compiler.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/raw_ostream.h"
36 #define DEBUG_TYPE "ir"
38 //===----------------------------------------------------------------------===//
39 // Pass Implementation
42 // Force out-of-line virtual method.
47 // Force out-of-line virtual method.
48 ModulePass::~ModulePass() = default;
50 Pass
*ModulePass::createPrinterPass(raw_ostream
&OS
,
51 const std::string
&Banner
) const {
52 return createPrintModulePass(OS
, Banner
);
55 PassManagerType
ModulePass::getPotentialPassManagerType() const {
56 return PMT_ModulePassManager
;
59 bool ModulePass::skipModule(Module
&M
) const {
60 return !M
.getContext().getOptPassGate().shouldRunPass(this, M
);
63 bool Pass::mustPreserveAnalysisID(char &AID
) const {
64 return Resolver
->getAnalysisIfAvailable(&AID
, true) != nullptr;
67 // dumpPassStructure - Implement the -debug-pass=Structure option
68 void Pass::dumpPassStructure(unsigned Offset
) {
69 dbgs().indent(Offset
*2) << getPassName() << "\n";
72 /// getPassName - Return a nice clean name for a pass. This usually
73 /// implemented in terms of the name that is registered by one of the
74 /// Registration templates, but can be overloaded directly.
75 StringRef
Pass::getPassName() const {
76 AnalysisID AID
= getPassID();
77 const PassInfo
*PI
= PassRegistry::getPassRegistry()->getPassInfo(AID
);
79 return PI
->getPassName();
80 return "Unnamed pass: implement Pass::getPassName()";
83 void Pass::preparePassManager(PMStack
&) {
84 // By default, don't do anything.
87 PassManagerType
Pass::getPotentialPassManagerType() const {
88 // Default implementation.
92 void Pass::getAnalysisUsage(AnalysisUsage
&) const {
93 // By default, no analysis results are used, all are invalidated.
96 void Pass::releaseMemory() {
97 // By default, don't do anything.
100 void Pass::verifyAnalysis() const {
101 // By default, don't do anything.
104 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID
) {
108 ImmutablePass
*Pass::getAsImmutablePass() {
112 PMDataManager
*Pass::getAsPMDataManager() {
116 void Pass::setResolver(AnalysisResolver
*AR
) {
117 assert(!Resolver
&& "Resolver is already set");
121 // print - Print out the internal state of the pass. This is called by Analyze
122 // to print out the contents of an analysis. Otherwise it is not necessary to
123 // implement this method.
124 void Pass::print(raw_ostream
&OS
, const Module
*) const {
125 OS
<< "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
128 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
129 // dump - call print(cerr);
130 LLVM_DUMP_METHOD
void Pass::dump() const {
131 print(dbgs(), nullptr);
135 //===----------------------------------------------------------------------===//
136 // ImmutablePass Implementation
138 // Force out-of-line virtual method.
139 ImmutablePass::~ImmutablePass() = default;
141 void ImmutablePass::initializePass() {
142 // By default, don't do anything.
145 //===----------------------------------------------------------------------===//
146 // FunctionPass Implementation
149 Pass
*FunctionPass::createPrinterPass(raw_ostream
&OS
,
150 const std::string
&Banner
) const {
151 return createPrintFunctionPass(OS
, Banner
);
154 PassManagerType
FunctionPass::getPotentialPassManagerType() const {
155 return PMT_FunctionPassManager
;
158 bool FunctionPass::skipFunction(const Function
&F
) const {
159 if (!F
.getContext().getOptPassGate().shouldRunPass(this, F
))
162 if (F
.hasFnAttribute(Attribute::OptimizeNone
)) {
163 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
164 << F
.getName() << "\n");
170 //===----------------------------------------------------------------------===//
171 // BasicBlockPass Implementation
174 Pass
*BasicBlockPass::createPrinterPass(raw_ostream
&OS
,
175 const std::string
&Banner
) const {
176 return createPrintBasicBlockPass(OS
, Banner
);
179 bool BasicBlockPass::doInitialization(Function
&) {
180 // By default, don't do anything.
184 bool BasicBlockPass::doFinalization(Function
&) {
185 // By default, don't do anything.
189 bool BasicBlockPass::skipBasicBlock(const BasicBlock
&BB
) const {
190 const Function
*F
= BB
.getParent();
193 if (!F
->getContext().getOptPassGate().shouldRunPass(this, BB
))
195 if (F
->hasFnAttribute(Attribute::OptimizeNone
)) {
196 // Report this only once per function.
197 if (&BB
== &F
->getEntryBlock())
198 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName()
199 << "' on function " << F
->getName() << "\n");
205 PassManagerType
BasicBlockPass::getPotentialPassManagerType() const {
206 return PMT_BasicBlockPassManager
;
209 const PassInfo
*Pass::lookupPassInfo(const void *TI
) {
210 return PassRegistry::getPassRegistry()->getPassInfo(TI
);
213 const PassInfo
*Pass::lookupPassInfo(StringRef Arg
) {
214 return PassRegistry::getPassRegistry()->getPassInfo(Arg
);
217 Pass
*Pass::createPass(AnalysisID ID
) {
218 const PassInfo
*PI
= PassRegistry::getPassRegistry()->getPassInfo(ID
);
221 return PI
->createPass();
224 //===----------------------------------------------------------------------===//
225 // Analysis Group Implementation Code
226 //===----------------------------------------------------------------------===//
228 // RegisterAGBase implementation
230 RegisterAGBase::RegisterAGBase(StringRef Name
, const void *InterfaceID
,
231 const void *PassID
, bool isDefault
)
232 : PassInfo(Name
, InterfaceID
) {
233 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID
, PassID
,
237 //===----------------------------------------------------------------------===//
238 // PassRegistrationListener implementation
241 // enumeratePasses - Iterate over the registered passes, calling the
242 // passEnumerate callback on each PassInfo object.
243 void PassRegistrationListener::enumeratePasses() {
244 PassRegistry::getPassRegistry()->enumerateWith(this);
247 PassNameParser::PassNameParser(cl::Option
&O
)
248 : cl::parser
<const PassInfo
*>(O
) {
249 PassRegistry::getPassRegistry()->addRegistrationListener(this);
252 // This only gets called during static destruction, in which case the
253 // PassRegistry will have already been destroyed by llvm_shutdown(). So
254 // attempting to remove the registration listener is an error.
255 PassNameParser::~PassNameParser() = default;
257 //===----------------------------------------------------------------------===//
258 // AnalysisUsage Class Implementation
263 struct GetCFGOnlyPasses
: public PassRegistrationListener
{
264 using VectorType
= AnalysisUsage::VectorType
;
266 VectorType
&CFGOnlyList
;
268 GetCFGOnlyPasses(VectorType
&L
) : CFGOnlyList(L
) {}
270 void passEnumerate(const PassInfo
*P
) override
{
271 if (P
->isCFGOnlyPass())
272 CFGOnlyList
.push_back(P
->getTypeInfo());
276 } // end anonymous namespace
278 // setPreservesCFG - This function should be called to by the pass, iff they do
281 // 1. Add or remove basic blocks from the function
282 // 2. Modify terminator instructions in any way.
284 // This function annotates the AnalysisUsage info object to say that analyses
285 // that only depend on the CFG are preserved by this pass.
286 void AnalysisUsage::setPreservesCFG() {
287 // Since this transformation doesn't modify the CFG, it preserves all analyses
288 // that only depend on the CFG (like dominators, loop info, etc...)
289 GetCFGOnlyPasses(Preserved
).enumeratePasses();
292 AnalysisUsage
&AnalysisUsage::addPreserved(StringRef Arg
) {
293 const PassInfo
*PI
= Pass::lookupPassInfo(Arg
);
294 // If the pass exists, preserve it. Otherwise silently do nothing.
295 if (PI
) Preserved
.push_back(PI
->getTypeInfo());
299 AnalysisUsage
&AnalysisUsage::addRequiredID(const void *ID
) {
300 Required
.push_back(ID
);
304 AnalysisUsage
&AnalysisUsage::addRequiredID(char &ID
) {
305 Required
.push_back(&ID
);
309 AnalysisUsage
&AnalysisUsage::addRequiredTransitiveID(char &ID
) {
310 Required
.push_back(&ID
);
311 RequiredTransitive
.push_back(&ID
);