blob: 7fc48282380b7b10b683932899e3881bcffa474b [file] [log] [blame]
Chris Lattner3e58d9b2003-10-13 05:33:01 +00001//===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd6953282002-01-21 07:37:31 +00009//
10// This file implements the LLVM Pass infrastructure. It is primarily
11// responsible with ensuring that passes are executed and batched together
12// optimally.
13//
14//===----------------------------------------------------------------------===//
15
Dan Gohmanee335e32008-05-23 20:40:06 +000016#include "llvm/Pass.h"
David Greene5c8aa952010-04-02 23:17:14 +000017#include "llvm/Assembly/PrintModulePass.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000018#include "llvm/PassRegistry.h"
David Greene840d41e2010-01-05 01:30:04 +000019#include "llvm/Support/Debug.h"
Chris Lattner6eeb7342010-01-22 06:29:25 +000020#include "llvm/Support/PassNameParser.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner31f84992003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner27ad1372002-05-06 19:31:52 +000024//===----------------------------------------------------------------------===//
Chris Lattner41300862002-01-31 00:45:31 +000025// Pass Implementation
Chris Lattner35f07eb2002-01-22 00:17:48 +000026//
Chris Lattner41300862002-01-31 00:45:31 +000027
Devang Patelc7d0f4b2006-12-22 22:49:00 +000028// Force out-of-line virtual method.
Andrew Trick8247e0d2012-02-03 05:12:30 +000029Pass::~Pass() {
30 delete Resolver;
Devang Patel6e21ff02007-04-26 21:33:42 +000031}
32
33// Force out-of-line virtual method.
Devang Patelc7d0f4b2006-12-22 22:49:00 +000034ModulePass::~ModulePass() { }
Chris Lattnerd6953282002-01-21 07:37:31 +000035
David Greene5c8aa952010-04-02 23:17:14 +000036Pass *ModulePass::createPrinterPass(raw_ostream &O,
37 const std::string &Banner) const {
38 return createPrintModulePass(&O, false, Banner);
39}
40
Dan Gohmaneda23fa2009-12-14 19:43:09 +000041PassManagerType ModulePass::getPotentialPassManagerType() const {
42 return PMT_ModulePassManager;
43}
44
Owen Anderson90c579d2010-08-06 18:33:48 +000045bool Pass::mustPreserveAnalysisID(char &AID) const {
46 return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
Chris Lattnerf1ab4542003-03-21 21:41:02 +000047}
48
Joe Abbey62faf772011-11-21 04:42:21 +000049// dumpPassStructure - Implement the -debug-pass=Structure option
Dan Gohman8a757ae2010-08-19 01:29:07 +000050void Pass::dumpPassStructure(unsigned Offset) {
David Greene840d41e2010-01-05 01:30:04 +000051 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner37d66c42002-07-30 16:27:02 +000052}
Chris Lattner96c466b2002-04-29 14:57:45 +000053
Dan Gohmanb973d5f2008-03-14 18:27:04 +000054/// getPassName - Return a nice clean name for a pass. This usually
55/// implemented in terms of the name that is registered by one of the
56/// Registration templates, but can be overloaded directly.
57///
Chris Lattner44050fb2002-07-29 21:02:31 +000058const char *Pass::getPassName() const {
Owen Anderson90c579d2010-08-06 18:33:48 +000059 AnalysisID AID = getPassID();
60 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
61 if (PI)
Chris Lattner44050fb2002-07-29 21:02:31 +000062 return PI->getPassName();
Chris Lattnerfe3e3f42007-10-18 16:11:18 +000063 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner44050fb2002-07-29 21:02:31 +000064}
Chris Lattner96c466b2002-04-29 14:57:45 +000065
Dan Gohmaneda23fa2009-12-14 19:43:09 +000066void Pass::preparePassManager(PMStack &) {
67 // By default, don't do anything.
68}
69
70PassManagerType Pass::getPotentialPassManagerType() const {
71 // Default implementation.
Andrew Trick8247e0d2012-02-03 05:12:30 +000072 return PMT_Unknown;
Dan Gohmaneda23fa2009-12-14 19:43:09 +000073}
74
75void Pass::getAnalysisUsage(AnalysisUsage &) const {
76 // By default, no analysis results are used, all are invalidated.
77}
78
79void Pass::releaseMemory() {
80 // By default, don't do anything.
81}
82
83void Pass::verifyAnalysis() const {
84 // By default, don't do anything.
85}
86
Owen Anderson90c579d2010-08-06 18:33:48 +000087void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmane407c1d2010-06-21 18:46:45 +000088 return this;
89}
90
91ImmutablePass *Pass::getAsImmutablePass() {
92 return 0;
93}
94
95PMDataManager *Pass::getAsPMDataManager() {
96 return 0;
97}
98
99void Pass::setResolver(AnalysisResolver *AR) {
100 assert(!Resolver && "Resolver is already set");
101 Resolver = AR;
102}
103
Misha Brukman69c856a2003-10-14 21:38:42 +0000104// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman5560c9d2003-08-18 14:43:39 +0000105// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattnera59cbb22002-07-27 01:12:17 +0000106// implement this method.
107//
Chris Lattner45cfe542009-08-23 06:03:38 +0000108void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattnera59cbb22002-07-27 01:12:17 +0000109 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
110}
111
Bill Wendling832171c2006-12-07 20:04:42 +0000112// dump - call print(cerr);
Chris Lattnera59cbb22002-07-27 01:12:17 +0000113void Pass::dump() const {
David Greene840d41e2010-01-05 01:30:04 +0000114 print(dbgs(), 0);
Chris Lattnera59cbb22002-07-27 01:12:17 +0000115}
116
Chris Lattner41300862002-01-31 00:45:31 +0000117//===----------------------------------------------------------------------===//
Chris Lattner70b4f3e2002-09-25 21:59:11 +0000118// ImmutablePass Implementation
119//
Devang Patelc7d0f4b2006-12-22 22:49:00 +0000120// Force out-of-line virtual method.
121ImmutablePass::~ImmutablePass() { }
Chris Lattner70b4f3e2002-09-25 21:59:11 +0000122
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000123void ImmutablePass::initializePass() {
124 // By default, don't do anything.
125}
126
Chris Lattner70b4f3e2002-09-25 21:59:11 +0000127//===----------------------------------------------------------------------===//
Chris Lattnerf57b8452002-04-27 06:56:12 +0000128// FunctionPass Implementation
Chris Lattnerd6953282002-01-21 07:37:31 +0000129//
Chris Lattner41300862002-01-31 00:45:31 +0000130
David Greene5c8aa952010-04-02 23:17:14 +0000131Pass *FunctionPass::createPrinterPass(raw_ostream &O,
132 const std::string &Banner) const {
133 return createPrintFunctionPass(Banner, &O);
134}
135
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000136PassManagerType FunctionPass::getPotentialPassManagerType() const {
137 return PMT_FunctionPassManager;
138}
139
Chris Lattner41300862002-01-31 00:45:31 +0000140//===----------------------------------------------------------------------===//
141// BasicBlockPass Implementation
142//
143
David Greene5c8aa952010-04-02 23:17:14 +0000144Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
145 const std::string &Banner) const {
Sergei Larin68b2faf2013-02-08 23:37:41 +0000146 return createPrintBasicBlockPass(&O, false, Banner);
David Greene5c8aa952010-04-02 23:17:14 +0000147}
148
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000149bool BasicBlockPass::doInitialization(Function &) {
150 // By default, don't do anything.
151 return false;
152}
153
154bool BasicBlockPass::doFinalization(Function &) {
155 // By default, don't do anything.
156 return false;
157}
158
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000159PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick8247e0d2012-02-03 05:12:30 +0000160 return PMT_BasicBlockPassManager;
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000161}
162
Owen Anderson90c579d2010-08-06 18:33:48 +0000163const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Andersonaac07ea2010-07-20 21:22:24 +0000164 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner54bbdb42002-07-23 18:08:00 +0000165}
166
Owen Anderson8be32912010-07-20 08:26:15 +0000167const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Andersonaac07ea2010-07-20 21:22:24 +0000168 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman8a261e42009-10-08 17:00:02 +0000169}
170
Andrew Trick5e108ee2012-02-15 03:21:47 +0000171Pass *Pass::createPass(AnalysisID ID) {
172 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trickebe18ef2012-02-08 21:22:34 +0000173 if (!PI)
174 return NULL;
175 return PI->createPass();
176}
177
Owen Anderson8be32912010-07-20 08:26:15 +0000178Pass *PassInfo::createPass() const {
Dan Gohmane407c1d2010-06-21 18:46:45 +0000179 assert((!isAnalysisGroup() || NormalCtor) &&
180 "No default implementation found for analysis group!");
181 assert(NormalCtor &&
182 "Cannot call createPass on PassInfo without default ctor!");
183 return NormalCtor();
184}
185
Chris Lattner789bc842002-08-21 22:17:09 +0000186//===----------------------------------------------------------------------===//
187// Analysis Group Implementation Code
188//===----------------------------------------------------------------------===//
189
Chris Lattner789bc842002-08-21 22:17:09 +0000190// RegisterAGBase implementation
191//
Owen Anderson90c579d2010-08-06 18:33:48 +0000192RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
193 const void *PassID, bool isDefault)
Owen Anderson96509832010-07-21 17:52:45 +0000194 : PassInfo(Name, InterfaceID) {
195 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
196 *this, isDefault);
Chris Lattner789bc842002-08-21 22:17:09 +0000197}
198
Chris Lattner54bbdb42002-07-23 18:08:00 +0000199//===----------------------------------------------------------------------===//
200// PassRegistrationListener implementation
201//
202
203// PassRegistrationListener ctor - Add the current object to the list of
204// PassRegistrationListeners...
205PassRegistrationListener::PassRegistrationListener() {
Owen Anderson53967352010-07-20 23:41:56 +0000206 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner54bbdb42002-07-23 18:08:00 +0000207}
208
209// dtor - Remove object from list of listeners...
210PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson53967352010-07-20 23:41:56 +0000211 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner54bbdb42002-07-23 18:08:00 +0000212}
213
214// enumeratePasses - Iterate over the registered passes, calling the
215// passEnumerate callback on each PassInfo object.
216//
217void PassRegistrationListener::enumeratePasses() {
Owen Andersonaac07ea2010-07-20 21:22:24 +0000218 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner54bbdb42002-07-23 18:08:00 +0000219}
Reid Spencere8f38482005-04-25 01:01:35 +0000220
Chris Lattner6eeb7342010-01-22 06:29:25 +0000221PassNameParser::~PassNameParser() {}
222
Chris Lattner947c7682006-12-01 22:21:11 +0000223//===----------------------------------------------------------------------===//
224// AnalysisUsage Class Implementation
225//
226
Chris Lattnerbaf2ecd2006-12-01 23:27:45 +0000227namespace {
228 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnerfc65d382008-08-08 05:33:04 +0000229 typedef AnalysisUsage::VectorType VectorType;
230 VectorType &CFGOnlyList;
231 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick8247e0d2012-02-03 05:12:30 +0000232
Owen Anderson8be32912010-07-20 08:26:15 +0000233 void passEnumerate(const PassInfo *P) {
Chris Lattnerbaf2ecd2006-12-01 23:27:45 +0000234 if (P->isCFGOnlyPass())
Owen Anderson90c579d2010-08-06 18:33:48 +0000235 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattnerbaf2ecd2006-12-01 23:27:45 +0000236 }
237 };
238}
239
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000240// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner947c7682006-12-01 22:21:11 +0000241// not:
242//
243// 1. Add or remove basic blocks from the function
244// 2. Modify terminator instructions in any way.
245//
246// This function annotates the AnalysisUsage info object to say that analyses
247// that only depend on the CFG are preserved by this pass.
248//
249void AnalysisUsage::setPreservesCFG() {
250 // Since this transformation doesn't modify the CFG, it preserves all analyses
251 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattnerbaf2ecd2006-12-01 23:27:45 +0000252 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner947c7682006-12-01 22:21:11 +0000253}
254
Owen Anderson90c579d2010-08-06 18:33:48 +0000255AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
256 const PassInfo *PI = Pass::lookupPassInfo(Arg);
257 // If the pass exists, preserve it. Otherwise silently do nothing.
258 if (PI) Preserved.push_back(PI->getTypeInfo());
259 return *this;
260}
261
262AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmane407c1d2010-06-21 18:46:45 +0000263 Required.push_back(ID);
264 return *this;
265}
Chris Lattner947c7682006-12-01 22:21:11 +0000266
Owen Anderson90c579d2010-08-06 18:33:48 +0000267AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
268 Required.push_back(&ID);
269 return *this;
270}
271
272AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
273 Required.push_back(&ID);
274 RequiredTransitive.push_back(&ID);
Dan Gohmane407c1d2010-06-21 18:46:45 +0000275 return *this;
276}