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 // dump - call print(cerr);
122 LLVM_DUMP_METHOD void Pass::dump() const {
123 print(dbgs(), nullptr);
126 //===----------------------------------------------------------------------===//
127 // ImmutablePass Implementation
129 // Force out-of-line virtual method.
130 ImmutablePass::~ImmutablePass() { }
132 void ImmutablePass::initializePass() {
133 // By default, don't do anything.
136 //===----------------------------------------------------------------------===//
137 // FunctionPass Implementation
140 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
141 const std::string &Banner) const {
142 return createPrintFunctionPass(O, Banner);
145 PassManagerType FunctionPass::getPotentialPassManagerType() const {
146 return PMT_FunctionPassManager;
149 bool FunctionPass::skipFunction(const Function &F) const {
150 if (!F.getContext().getOptBisect().shouldRunPass(this, F))
153 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
154 DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
155 << F.getName() << "\n");
161 //===----------------------------------------------------------------------===//
162 // BasicBlockPass Implementation
165 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
166 const std::string &Banner) const {
167 return createPrintBasicBlockPass(O, Banner);
170 bool BasicBlockPass::doInitialization(Function &) {
171 // By default, don't do anything.
175 bool BasicBlockPass::doFinalization(Function &) {
176 // By default, don't do anything.
180 bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
181 const Function *F = BB.getParent();
184 if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
186 if (F->hasFnAttribute(Attribute::OptimizeNone)) {
187 // Report this only once per function.
188 if (&BB == &F->getEntryBlock())
189 DEBUG(dbgs() << "Skipping pass '" << getPassName()
190 << "' on function " << F->getName() << "\n");
196 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
197 return PMT_BasicBlockPassManager;
200 const PassInfo *Pass::lookupPassInfo(const void *TI) {
201 return PassRegistry::getPassRegistry()->getPassInfo(TI);
204 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
205 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
208 Pass *Pass::createPass(AnalysisID ID) {
209 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
212 return PI->createPass();
215 //===----------------------------------------------------------------------===//
216 // Analysis Group Implementation Code
217 //===----------------------------------------------------------------------===//
219 // RegisterAGBase implementation
221 RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
222 const void *PassID, bool isDefault)
223 : PassInfo(Name, InterfaceID) {
224 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
228 //===----------------------------------------------------------------------===//
229 // PassRegistrationListener implementation
232 // enumeratePasses - Iterate over the registered passes, calling the
233 // passEnumerate callback on each PassInfo object.
235 void PassRegistrationListener::enumeratePasses() {
236 PassRegistry::getPassRegistry()->enumerateWith(this);
239 PassNameParser::PassNameParser(cl::Option &O)
240 : cl::parser<const PassInfo *>(O) {
241 PassRegistry::getPassRegistry()->addRegistrationListener(this);
244 PassNameParser::~PassNameParser() {
245 // This only gets called during static destruction, in which case the
246 // PassRegistry will have already been destroyed by llvm_shutdown(). So
247 // attempting to remove the registration listener is an error.
250 //===----------------------------------------------------------------------===//
251 // AnalysisUsage Class Implementation
255 struct GetCFGOnlyPasses : public PassRegistrationListener {
256 typedef AnalysisUsage::VectorType VectorType;
257 VectorType &CFGOnlyList;
258 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
260 void passEnumerate(const PassInfo *P) override {
261 if (P->isCFGOnlyPass())
262 CFGOnlyList.push_back(P->getTypeInfo());
267 // setPreservesCFG - This function should be called to by the pass, iff they do
270 // 1. Add or remove basic blocks from the function
271 // 2. Modify terminator instructions in any way.
273 // This function annotates the AnalysisUsage info object to say that analyses
274 // that only depend on the CFG are preserved by this pass.
276 void AnalysisUsage::setPreservesCFG() {
277 // Since this transformation doesn't modify the CFG, it preserves all analyses
278 // that only depend on the CFG (like dominators, loop info, etc...)
279 GetCFGOnlyPasses(Preserved).enumeratePasses();
282 AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
283 const PassInfo *PI = Pass::lookupPassInfo(Arg);
284 // If the pass exists, preserve it. Otherwise silently do nothing.
285 if (PI) Preserved.push_back(PI->getTypeInfo());
289 AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
290 Required.push_back(ID);
294 AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
295 Required.push_back(&ID);
299 AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
300 Required.push_back(&ID);
301 RequiredTransitive.push_back(&ID);