blob: 2d483a09fc1f1381ced2eba8049733cd6306663f [file] [log] [blame]
Chris Lattnerab16a652003-10-13 05:33:01 +00001//===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner26e4f892002-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 Gohman643b3a02008-05-23 20:40:06 +000016#include "llvm/Pass.h"
Owen Anderson1e8ae642010-07-20 18:39:06 +000017#include "llvm/PassRegistry.h"
David Greene9b063df2010-04-02 23:17:14 +000018#include "llvm/Assembly/PrintModulePass.h"
David Greenedfe4ad72010-01-05 01:30:04 +000019#include "llvm/Support/Debug.h"
Chris Lattner5264fce2010-01-22 06:29:25 +000020#include "llvm/Support/PassNameParser.h"
Chris Lattner13626022009-08-23 06:03:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner7e0dbe62002-05-06 19:31:52 +000024//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000025// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000026//
Chris Lattnercdd09c22002-01-31 00:45:31 +000027
Devang Patelf5a994e2006-12-22 22:49:00 +000028// Force out-of-line virtual method.
Andrew Trick808a7a62012-02-03 05:12:30 +000029Pass::~Pass() {
30 delete Resolver;
Devang Patel2c1bba02007-04-26 21:33:42 +000031}
32
33// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000034ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000035
David Greene9b063df2010-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 Gohman1cfbfc82009-12-14 19:43:09 +000041PassManagerType ModulePass::getPotentialPassManagerType() const {
42 return PMT_ModulePassManager;
43}
44
Owen Andersona7aed182010-08-06 18:33:48 +000045bool Pass::mustPreserveAnalysisID(char &AID) const {
46 return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000047}
48
Joe Abbey96e89f62011-11-21 04:42:21 +000049// dumpPassStructure - Implement the -debug-pass=Structure option
Dan Gohmanf71c5212010-08-19 01:29:07 +000050void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000051 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000052}
Chris Lattner37104aa2002-04-29 14:57:45 +000053
Dan Gohman94f57c52008-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 Lattner071577d2002-07-29 21:02:31 +000058const char *Pass::getPassName() const {
Owen Andersona7aed182010-08-06 18:33:48 +000059 AnalysisID AID = getPassID();
60 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
61 if (PI)
Chris Lattner071577d2002-07-29 21:02:31 +000062 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000063 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000064}
Chris Lattner37104aa2002-04-29 14:57:45 +000065
Dan Gohman1cfbfc82009-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 Trick808a7a62012-02-03 05:12:30 +000072 return PMT_Unknown;
Dan Gohman1cfbfc82009-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 Andersona7aed182010-08-06 18:33:48 +000087void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmanffdee302010-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 Brukman56a86422003-10-14 21:38:42 +0000104// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000105// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000106// implement this method.
107//
Chris Lattner13626022009-08-23 06:03:38 +0000108void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000109 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
110}
111
Bill Wendling22e978a2006-12-07 20:04:42 +0000112// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +0000113void Pass::dump() const {
David Greenedfe4ad72010-01-05 01:30:04 +0000114 print(dbgs(), 0);
Chris Lattner26750072002-07-27 01:12:17 +0000115}
116
Chris Lattnercdd09c22002-01-31 00:45:31 +0000117//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000118// ImmutablePass Implementation
119//
Devang Patelf5a994e2006-12-22 22:49:00 +0000120// Force out-of-line virtual method.
121ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000122
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000123void ImmutablePass::initializePass() {
124 // By default, don't do anything.
125}
126
Chris Lattneree0788d2002-09-25 21:59:11 +0000127//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000128// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000129//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000130
David Greene9b063df2010-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 Gohman1cfbfc82009-12-14 19:43:09 +0000136bool FunctionPass::doInitialization(Module &) {
137 // By default, don't do anything.
138 return false;
139}
140
141bool FunctionPass::doFinalization(Module &) {
142 // By default, don't do anything.
143 return false;
144}
145
146PassManagerType FunctionPass::getPotentialPassManagerType() const {
147 return PMT_FunctionPassManager;
148}
149
Chris Lattnercdd09c22002-01-31 00:45:31 +0000150//===----------------------------------------------------------------------===//
151// BasicBlockPass Implementation
152//
153
David Greene9b063df2010-04-02 23:17:14 +0000154Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
155 const std::string &Banner) const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000156
David Greene9b063df2010-04-02 23:17:14 +0000157 llvm_unreachable("BasicBlockPass printing unsupported.");
David Greene9b063df2010-04-02 23:17:14 +0000158}
159
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000160bool BasicBlockPass::doInitialization(Module &) {
161 // By default, don't do anything.
162 return false;
163}
164
165bool BasicBlockPass::doInitialization(Function &) {
166 // By default, don't do anything.
167 return false;
168}
169
170bool BasicBlockPass::doFinalization(Function &) {
171 // By default, don't do anything.
172 return false;
173}
174
175bool BasicBlockPass::doFinalization(Module &) {
176 // By default, don't do anything.
177 return false;
178}
179
180PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000181 return PMT_BasicBlockPassManager;
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000182}
183
Owen Andersona7aed182010-08-06 18:33:48 +0000184const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000185 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000186}
187
Owen Anderson81781222010-07-20 08:26:15 +0000188const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000189 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000190}
191
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000192Pass *Pass::createPass(AnalysisID ID) {
193 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trick3a61b782012-02-08 21:22:34 +0000194 if (!PI)
195 return NULL;
196 return PI->createPass();
197}
198
Owen Anderson81781222010-07-20 08:26:15 +0000199Pass *PassInfo::createPass() const {
Dan Gohmanffdee302010-06-21 18:46:45 +0000200 assert((!isAnalysisGroup() || NormalCtor) &&
201 "No default implementation found for analysis group!");
202 assert(NormalCtor &&
203 "Cannot call createPass on PassInfo without default ctor!");
204 return NormalCtor();
205}
206
Chris Lattner6e041bd2002-08-21 22:17:09 +0000207//===----------------------------------------------------------------------===//
208// Analysis Group Implementation Code
209//===----------------------------------------------------------------------===//
210
Chris Lattner6e041bd2002-08-21 22:17:09 +0000211// RegisterAGBase implementation
212//
Owen Andersona7aed182010-08-06 18:33:48 +0000213RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
214 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000215 : PassInfo(Name, InterfaceID) {
216 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
217 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000218}
219
Chris Lattner37d3c952002-07-23 18:08:00 +0000220//===----------------------------------------------------------------------===//
221// PassRegistrationListener implementation
222//
223
224// PassRegistrationListener ctor - Add the current object to the list of
225// PassRegistrationListeners...
226PassRegistrationListener::PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000227 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000228}
229
230// dtor - Remove object from list of listeners...
231PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000232 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000233}
234
235// enumeratePasses - Iterate over the registered passes, calling the
236// passEnumerate callback on each PassInfo object.
237//
238void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000239 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000240}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000241
Chris Lattner5264fce2010-01-22 06:29:25 +0000242PassNameParser::~PassNameParser() {}
243
Chris Lattner23d54052006-12-01 22:21:11 +0000244//===----------------------------------------------------------------------===//
245// AnalysisUsage Class Implementation
246//
247
Chris Lattner1b368a02006-12-01 23:27:45 +0000248namespace {
249 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000250 typedef AnalysisUsage::VectorType VectorType;
251 VectorType &CFGOnlyList;
252 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick808a7a62012-02-03 05:12:30 +0000253
Owen Anderson81781222010-07-20 08:26:15 +0000254 void passEnumerate(const PassInfo *P) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000255 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000256 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000257 }
258 };
259}
260
Sylvestre Ledru721cffd2012-09-27 09:59:43 +0000261// setPreservesCFG - This function should be called to by the pass, if they do
Chris Lattner23d54052006-12-01 22:21:11 +0000262// not:
263//
264// 1. Add or remove basic blocks from the function
265// 2. Modify terminator instructions in any way.
266//
267// This function annotates the AnalysisUsage info object to say that analyses
268// that only depend on the CFG are preserved by this pass.
269//
270void AnalysisUsage::setPreservesCFG() {
271 // Since this transformation doesn't modify the CFG, it preserves all analyses
272 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000273 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000274}
275
Owen Andersona7aed182010-08-06 18:33:48 +0000276AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
277 const PassInfo *PI = Pass::lookupPassInfo(Arg);
278 // If the pass exists, preserve it. Otherwise silently do nothing.
279 if (PI) Preserved.push_back(PI->getTypeInfo());
280 return *this;
281}
282
283AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000284 Required.push_back(ID);
285 return *this;
286}
Chris Lattner23d54052006-12-01 22:21:11 +0000287
Owen Andersona7aed182010-08-06 18:33:48 +0000288AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
289 Required.push_back(&ID);
290 return *this;
291}
292
293AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
294 Required.push_back(&ID);
295 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000296 return *this;
297}