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/LLVMContext.h"
21 #include "llvm/PassRegistry.h"
22 #include "llvm/Support/Debug.h"
23 #include "llvm/Support/raw_ostream.h"
26 #define DEBUG_TYPE "ir"
28 //===----------------------------------------------------------------------===//
29 // Pass Implementation
32 // Force out-of-line virtual method.
37 // Force out-of-line virtual method.
38 ModulePass::~ModulePass() { }
40 Pass *ModulePass::createPrinterPass(raw_ostream &O,
41 const std::string &Banner) const {
42 return createPrintModulePass(O, Banner);
45 PassManagerType ModulePass::getPotentialPassManagerType() const {
46 return PMT_ModulePassManager;
49 bool Pass::mustPreserveAnalysisID(char &AID) const {
50 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
53 // dumpPassStructure - Implement the -debug-pass=Structure option
54 void Pass::dumpPassStructure(unsigned Offset) {
55 dbgs().indent(Offset*2) << getPassName() << "\n";
58 /// getPassName - Return a nice clean name for a pass. This usually
59 /// implemented in terms of the name that is registered by one of the
60 /// Registration templates, but can be overloaded directly.
62 const char *Pass::getPassName() const {
63 AnalysisID AID = getPassID();
64 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
66 return PI->getPassName();
67 return "Unnamed pass: implement Pass::getPassName()";
70 void Pass::preparePassManager(PMStack &) {
71 // By default, don't do anything.
74 PassManagerType Pass::getPotentialPassManagerType() const {
75 // Default implementation.
79 void Pass::getAnalysisUsage(AnalysisUsage &) const {
80 // By default, no analysis results are used, all are invalidated.
83 void Pass::releaseMemory() {
84 // By default, don't do anything.
87 void Pass::verifyAnalysis() const {
88 // By default, don't do anything.
91 void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
95 ImmutablePass *Pass::getAsImmutablePass() {
99 PMDataManager *Pass::getAsPMDataManager() {
103 void Pass::setResolver(AnalysisResolver *AR) {
104 assert(!Resolver && "Resolver is already set");
108 // print - Print out the internal state of the pass. This is called by Analyze
109 // to print out the contents of an analysis. Otherwise it is not necessary to
110 // implement this method.
112 void Pass::print(raw_ostream &O,const Module*) const {
113 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
116 // dump - call print(cerr);
117 void Pass::dump() const {
118 print(dbgs(), nullptr);
121 //===----------------------------------------------------------------------===//
122 // ImmutablePass Implementation
124 // Force out-of-line virtual method.
125 ImmutablePass::~ImmutablePass() { }
127 void ImmutablePass::initializePass() {
128 // By default, don't do anything.
131 //===----------------------------------------------------------------------===//
132 // FunctionPass Implementation
135 Pass *FunctionPass::createPrinterPass(raw_ostream &O,
136 const std::string &Banner) const {
137 return createPrintFunctionPass(O, Banner);
140 PassManagerType FunctionPass::getPotentialPassManagerType() const {
141 return PMT_FunctionPassManager;
144 bool FunctionPass::skipOptnoneFunction(const Function &F) const {
145 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
146 DEBUG(dbgs() << "Skipping pass '" << getPassName()
147 << "' on function " << F.getName() << "\n");
153 //===----------------------------------------------------------------------===//
154 // BasicBlockPass Implementation
157 Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
158 const std::string &Banner) const {
159 return createPrintBasicBlockPass(O, Banner);
162 bool BasicBlockPass::doInitialization(Function &) {
163 // By default, don't do anything.
167 bool BasicBlockPass::doFinalization(Function &) {
168 // By default, don't do anything.
172 bool BasicBlockPass::skipOptnoneFunction(const BasicBlock &BB) const {
173 const Function *F = BB.getParent();
174 if (F && F->hasFnAttribute(Attribute::OptimizeNone)) {
175 // Report this only once per function.
176 if (&BB == &F->getEntryBlock())
177 DEBUG(dbgs() << "Skipping pass '" << getPassName()
178 << "' on function " << F->getName() << "\n");
184 PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
185 return PMT_BasicBlockPassManager;
188 const PassInfo *Pass::lookupPassInfo(const void *TI) {
189 return PassRegistry::getPassRegistry()->getPassInfo(TI);
192 const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
193 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
196 Pass *Pass::createPass(AnalysisID ID) {
197 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
200 return PI->createPass();
203 Pass *PassInfo::createPass() const {
204 assert((!isAnalysisGroup() || NormalCtor) &&
205 "No default implementation found for analysis group!");
207 "Cannot call createPass on PassInfo without default ctor!");
211 //===----------------------------------------------------------------------===//
212 // Analysis Group Implementation Code
213 //===----------------------------------------------------------------------===//
215 // RegisterAGBase implementation
217 RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
218 const void *PassID, bool isDefault)
219 : PassInfo(Name, InterfaceID) {
220 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
224 //===----------------------------------------------------------------------===//
225 // PassRegistrationListener implementation
228 // PassRegistrationListener ctor - Add the current object to the list of
229 // PassRegistrationListeners...
230 PassRegistrationListener::PassRegistrationListener() {
231 PassRegistry::getPassRegistry()->addRegistrationListener(this);
234 // dtor - Remove object from list of listeners...
235 PassRegistrationListener::~PassRegistrationListener() {
236 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
239 // enumeratePasses - Iterate over the registered passes, calling the
240 // passEnumerate callback on each PassInfo object.
242 void PassRegistrationListener::enumeratePasses() {
243 PassRegistry::getPassRegistry()->enumerateWith(this);
246 //===----------------------------------------------------------------------===//
247 // PassRunListener implementation
250 // PassRunListener ctor - Add the current object to the list of
251 // PassRunListeners...
252 PassRunListener::PassRunListener(LLVMContext *C) {
253 C->addRunListener(this);
256 PassRunListener::~PassRunListener() {}
258 PassNameParser::~PassNameParser() {}
260 //===----------------------------------------------------------------------===//
261 // AnalysisUsage Class Implementation
265 struct GetCFGOnlyPasses : public PassRegistrationListener {
266 typedef AnalysisUsage::VectorType VectorType;
267 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());
277 // setPreservesCFG - This function should be called to by the pass, iff they do
280 // 1. Add or remove basic blocks from the function
281 // 2. Modify terminator instructions in any way.
283 // This function annotates the AnalysisUsage info object to say that analyses
284 // 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);