blob: 05d2efa850aa7409c935810b9260f695e9738e05 [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
Owen Anderson81781222010-07-20 08:26:15 +0000192Pass *PassInfo::createPass() const {
Dan Gohmanffdee302010-06-21 18:46:45 +0000193 assert((!isAnalysisGroup() || NormalCtor) &&
194 "No default implementation found for analysis group!");
195 assert(NormalCtor &&
196 "Cannot call createPass on PassInfo without default ctor!");
197 return NormalCtor();
198}
199
Chris Lattner6e041bd2002-08-21 22:17:09 +0000200//===----------------------------------------------------------------------===//
201// Analysis Group Implementation Code
202//===----------------------------------------------------------------------===//
203
Chris Lattner6e041bd2002-08-21 22:17:09 +0000204// RegisterAGBase implementation
205//
Owen Andersona7aed182010-08-06 18:33:48 +0000206RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
207 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000208 : PassInfo(Name, InterfaceID) {
209 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
210 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000211}
212
Chris Lattner37d3c952002-07-23 18:08:00 +0000213//===----------------------------------------------------------------------===//
214// PassRegistrationListener implementation
215//
216
217// PassRegistrationListener ctor - Add the current object to the list of
218// PassRegistrationListeners...
219PassRegistrationListener::PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000220 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000221}
222
223// dtor - Remove object from list of listeners...
224PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000225 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000226}
227
228// enumeratePasses - Iterate over the registered passes, calling the
229// passEnumerate callback on each PassInfo object.
230//
231void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000232 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000233}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000234
Chris Lattner5264fce2010-01-22 06:29:25 +0000235PassNameParser::~PassNameParser() {}
236
Chris Lattner23d54052006-12-01 22:21:11 +0000237//===----------------------------------------------------------------------===//
238// AnalysisUsage Class Implementation
239//
240
Chris Lattner1b368a02006-12-01 23:27:45 +0000241namespace {
242 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000243 typedef AnalysisUsage::VectorType VectorType;
244 VectorType &CFGOnlyList;
245 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick808a7a62012-02-03 05:12:30 +0000246
Owen Anderson81781222010-07-20 08:26:15 +0000247 void passEnumerate(const PassInfo *P) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000248 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000249 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000250 }
251 };
252}
253
Chris Lattner23d54052006-12-01 22:21:11 +0000254// setPreservesCFG - This function should be called to by the pass, iff they do
255// not:
256//
257// 1. Add or remove basic blocks from the function
258// 2. Modify terminator instructions in any way.
259//
260// This function annotates the AnalysisUsage info object to say that analyses
261// that only depend on the CFG are preserved by this pass.
262//
263void AnalysisUsage::setPreservesCFG() {
264 // Since this transformation doesn't modify the CFG, it preserves all analyses
265 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000266 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000267}
268
Owen Andersona7aed182010-08-06 18:33:48 +0000269AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
270 const PassInfo *PI = Pass::lookupPassInfo(Arg);
271 // If the pass exists, preserve it. Otherwise silently do nothing.
272 if (PI) Preserved.push_back(PI->getTypeInfo());
273 return *this;
274}
275
276AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000277 Required.push_back(ID);
278 return *this;
279}
Chris Lattner23d54052006-12-01 22:21:11 +0000280
Owen Andersona7aed182010-08-06 18:33:48 +0000281AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
282 Required.push_back(&ID);
283 return *this;
284}
285
286AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
287 Required.push_back(&ID);
288 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000289 return *this;
290}