blob: ec448e6420d3bf4d017dcbe9639ed7b4b3460a6c [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"
David Greene9b063df2010-04-02 23:17:14 +000017#include "llvm/Assembly/PrintModulePass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/PassRegistry.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 +0000136PassManagerType FunctionPass::getPotentialPassManagerType() const {
137 return PMT_FunctionPassManager;
138}
139
Chris Lattnercdd09c22002-01-31 00:45:31 +0000140//===----------------------------------------------------------------------===//
141// BasicBlockPass Implementation
142//
143
David Greene9b063df2010-04-02 23:17:14 +0000144Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
145 const std::string &Banner) const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000146
David Greene9b063df2010-04-02 23:17:14 +0000147 llvm_unreachable("BasicBlockPass printing unsupported.");
David Greene9b063df2010-04-02 23:17:14 +0000148}
149
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000150bool BasicBlockPass::doInitialization(Function &) {
151 // By default, don't do anything.
152 return false;
153}
154
155bool BasicBlockPass::doFinalization(Function &) {
156 // By default, don't do anything.
157 return false;
158}
159
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000160PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000161 return PMT_BasicBlockPassManager;
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000162}
163
Owen Andersona7aed182010-08-06 18:33:48 +0000164const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000165 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000166}
167
Owen Anderson81781222010-07-20 08:26:15 +0000168const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000169 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000170}
171
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000172Pass *Pass::createPass(AnalysisID ID) {
173 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trick3a61b782012-02-08 21:22:34 +0000174 if (!PI)
175 return NULL;
176 return PI->createPass();
177}
178
Owen Anderson81781222010-07-20 08:26:15 +0000179Pass *PassInfo::createPass() const {
Dan Gohmanffdee302010-06-21 18:46:45 +0000180 assert((!isAnalysisGroup() || NormalCtor) &&
181 "No default implementation found for analysis group!");
182 assert(NormalCtor &&
183 "Cannot call createPass on PassInfo without default ctor!");
184 return NormalCtor();
185}
186
Chris Lattner6e041bd2002-08-21 22:17:09 +0000187//===----------------------------------------------------------------------===//
188// Analysis Group Implementation Code
189//===----------------------------------------------------------------------===//
190
Chris Lattner6e041bd2002-08-21 22:17:09 +0000191// RegisterAGBase implementation
192//
Owen Andersona7aed182010-08-06 18:33:48 +0000193RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
194 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000195 : PassInfo(Name, InterfaceID) {
196 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
197 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000198}
199
Chris Lattner37d3c952002-07-23 18:08:00 +0000200//===----------------------------------------------------------------------===//
201// PassRegistrationListener implementation
202//
203
204// PassRegistrationListener ctor - Add the current object to the list of
205// PassRegistrationListeners...
206PassRegistrationListener::PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000207 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000208}
209
210// dtor - Remove object from list of listeners...
211PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000212 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000213}
214
215// enumeratePasses - Iterate over the registered passes, calling the
216// passEnumerate callback on each PassInfo object.
217//
218void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000219 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000220}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000221
Chris Lattner5264fce2010-01-22 06:29:25 +0000222PassNameParser::~PassNameParser() {}
223
Chris Lattner23d54052006-12-01 22:21:11 +0000224//===----------------------------------------------------------------------===//
225// AnalysisUsage Class Implementation
226//
227
Chris Lattner1b368a02006-12-01 23:27:45 +0000228namespace {
229 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000230 typedef AnalysisUsage::VectorType VectorType;
231 VectorType &CFGOnlyList;
232 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick808a7a62012-02-03 05:12:30 +0000233
Owen Anderson81781222010-07-20 08:26:15 +0000234 void passEnumerate(const PassInfo *P) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000235 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000236 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000237 }
238 };
239}
240
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000241// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner23d54052006-12-01 22:21:11 +0000242// not:
243//
244// 1. Add or remove basic blocks from the function
245// 2. Modify terminator instructions in any way.
246//
247// This function annotates the AnalysisUsage info object to say that analyses
248// that only depend on the CFG are preserved by this pass.
249//
250void AnalysisUsage::setPreservesCFG() {
251 // Since this transformation doesn't modify the CFG, it preserves all analyses
252 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000253 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000254}
255
Owen Andersona7aed182010-08-06 18:33:48 +0000256AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
257 const PassInfo *PI = Pass::lookupPassInfo(Arg);
258 // If the pass exists, preserve it. Otherwise silently do nothing.
259 if (PI) Preserved.push_back(PI->getTypeInfo());
260 return *this;
261}
262
263AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000264 Required.push_back(ID);
265 return *this;
266}
Chris Lattner23d54052006-12-01 22:21:11 +0000267
Owen Andersona7aed182010-08-06 18:33:48 +0000268AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
269 Required.push_back(&ID);
270 return *this;
271}
272
273AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
274 Required.push_back(&ID);
275 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000276 return *this;
277}