blob: 5a176d59f9b868ac927d991ccc3a8fd365311fb9 [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"
Chris Lattnercdd09c22002-01-31 00:45:31 +000017#include "llvm/PassManager.h"
Owen Anderson1e8ae642010-07-20 18:39:06 +000018#include "llvm/PassRegistry.h"
Chris Lattnercdd09c22002-01-31 00:45:31 +000019#include "llvm/Module.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/STLExtras.h"
Devang Patel1155fdf2009-10-22 19:36:54 +000021#include "llvm/ADT/StringMap.h"
David Greene9b063df2010-04-02 23:17:14 +000022#include "llvm/Assembly/PrintModulePass.h"
David Greenedfe4ad72010-01-05 01:30:04 +000023#include "llvm/Support/Debug.h"
Chris Lattner1b368a02006-12-01 23:27:45 +000024#include "llvm/Support/ManagedStatic.h"
Chris Lattner5264fce2010-01-22 06:29:25 +000025#include "llvm/Support/PassNameParser.h"
Chris Lattner13626022009-08-23 06:03:38 +000026#include "llvm/Support/raw_ostream.h"
Owen Andersoneae97722009-06-17 21:16:20 +000027#include "llvm/System/Atomic.h"
Owen Andersonecdab542009-06-24 00:25:42 +000028#include "llvm/System/Mutex.h"
Owen Anderson7d42b952009-06-18 16:54:52 +000029#include "llvm/System/Threading.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000030#include <algorithm>
Dan Gohman99885692008-03-21 23:51:57 +000031#include <map>
Chris Lattner6e041bd2002-08-21 22:17:09 +000032#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chris Lattner7e0dbe62002-05-06 19:31:52 +000035//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000036// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000037//
Chris Lattnercdd09c22002-01-31 00:45:31 +000038
Dan Gohmanffdee302010-06-21 18:46:45 +000039Pass::Pass(PassKind K, intptr_t pid) : Resolver(0), PassID(pid), Kind(K) {
40 assert(pid && "pid cannot be 0");
41}
42
43Pass::Pass(PassKind K, const void *pid)
44 : Resolver(0), PassID((intptr_t)pid), Kind(K) {
45 assert(pid && "pid cannot be 0");
46}
47
Devang Patelf5a994e2006-12-22 22:49:00 +000048// Force out-of-line virtual method.
Devang Patel2c1bba02007-04-26 21:33:42 +000049Pass::~Pass() {
50 delete Resolver;
51}
52
53// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000054ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000055
David Greene9b063df2010-04-02 23:17:14 +000056Pass *ModulePass::createPrinterPass(raw_ostream &O,
57 const std::string &Banner) const {
58 return createPrintModulePass(&O, false, Banner);
59}
60
Dan Gohman1cfbfc82009-12-14 19:43:09 +000061PassManagerType ModulePass::getPotentialPassManagerType() const {
62 return PMT_ModulePassManager;
63}
64
Chris Lattner9ad77572003-03-21 21:41:02 +000065bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
Duncan Sands5a913d62009-01-28 13:14:17 +000066 return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000067}
68
Chris Lattner198cf422002-07-30 16:27:02 +000069// dumpPassStructure - Implement the -debug-passes=Structure option
70void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000071 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000072}
Chris Lattner37104aa2002-04-29 14:57:45 +000073
Dan Gohman94f57c52008-03-14 18:27:04 +000074/// getPassName - Return a nice clean name for a pass. This usually
75/// implemented in terms of the name that is registered by one of the
76/// Registration templates, but can be overloaded directly.
77///
Chris Lattner071577d2002-07-29 21:02:31 +000078const char *Pass::getPassName() const {
Owen Anderson81781222010-07-20 08:26:15 +000079 if (const PassInfo *PI = getPassInfo())
Chris Lattner071577d2002-07-29 21:02:31 +000080 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000081 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000082}
Chris Lattner37104aa2002-04-29 14:57:45 +000083
Dan Gohman1cfbfc82009-12-14 19:43:09 +000084void Pass::preparePassManager(PMStack &) {
85 // By default, don't do anything.
86}
87
88PassManagerType Pass::getPotentialPassManagerType() const {
89 // Default implementation.
90 return PMT_Unknown;
91}
92
93void Pass::getAnalysisUsage(AnalysisUsage &) const {
94 // By default, no analysis results are used, all are invalidated.
95}
96
97void Pass::releaseMemory() {
98 // By default, don't do anything.
99}
100
101void Pass::verifyAnalysis() const {
102 // By default, don't do anything.
103}
104
Owen Anderson81781222010-07-20 08:26:15 +0000105void *Pass::getAdjustedAnalysisPointer(const PassInfo *) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000106 return this;
107}
108
109ImmutablePass *Pass::getAsImmutablePass() {
110 return 0;
111}
112
113PMDataManager *Pass::getAsPMDataManager() {
114 return 0;
115}
116
117void Pass::setResolver(AnalysisResolver *AR) {
118 assert(!Resolver && "Resolver is already set");
119 Resolver = AR;
120}
121
Misha Brukman56a86422003-10-14 21:38:42 +0000122// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000123// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000124// implement this method.
125//
Chris Lattner13626022009-08-23 06:03:38 +0000126void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000127 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
128}
129
Bill Wendling22e978a2006-12-07 20:04:42 +0000130// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +0000131void Pass::dump() const {
David Greenedfe4ad72010-01-05 01:30:04 +0000132 print(dbgs(), 0);
Chris Lattner26750072002-07-27 01:12:17 +0000133}
134
Chris Lattnercdd09c22002-01-31 00:45:31 +0000135//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000136// ImmutablePass Implementation
137//
Devang Patelf5a994e2006-12-22 22:49:00 +0000138// Force out-of-line virtual method.
139ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000140
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000141void ImmutablePass::initializePass() {
142 // By default, don't do anything.
143}
144
Chris Lattneree0788d2002-09-25 21:59:11 +0000145//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000146// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000147//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000148
David Greene9b063df2010-04-02 23:17:14 +0000149Pass *FunctionPass::createPrinterPass(raw_ostream &O,
150 const std::string &Banner) const {
151 return createPrintFunctionPass(Banner, &O);
152}
153
Chris Lattnerc8e66542002-04-27 06:56:12 +0000154// run - On a module, we run this pass by initializing, runOnFunction'ing once
155// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000156//
Chris Lattner79e523d2004-09-20 04:47:19 +0000157bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000158 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000159
Chris Lattner113f4f42002-06-25 16:13:24 +0000160 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000161 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000162 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000163
Chris Lattnercdd09c22002-01-31 00:45:31 +0000164 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000165}
166
Chris Lattnerc8e66542002-04-27 06:56:12 +0000167// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000168//
Chris Lattner113f4f42002-06-25 16:13:24 +0000169bool FunctionPass::run(Function &F) {
Dan Gohman929391a2008-01-29 12:09:55 +0000170 // Passes are not run on external functions!
171 if (F.isDeclaration()) return false;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000172
Chris Lattner79e523d2004-09-20 04:47:19 +0000173 bool Changed = doInitialization(*F.getParent());
174 Changed |= runOnFunction(F);
175 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000176}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000177
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000178bool FunctionPass::doInitialization(Module &) {
179 // By default, don't do anything.
180 return false;
181}
182
183bool FunctionPass::doFinalization(Module &) {
184 // By default, don't do anything.
185 return false;
186}
187
188PassManagerType FunctionPass::getPotentialPassManagerType() const {
189 return PMT_FunctionPassManager;
190}
191
Chris Lattnercdd09c22002-01-31 00:45:31 +0000192//===----------------------------------------------------------------------===//
193// BasicBlockPass Implementation
194//
195
David Greene9b063df2010-04-02 23:17:14 +0000196Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
197 const std::string &Banner) const {
198
199 llvm_unreachable("BasicBlockPass printing unsupported.");
200 return 0;
201}
202
Chris Lattnerc8e66542002-04-27 06:56:12 +0000203// To run this pass on a function, we simply call runOnBasicBlock once for each
204// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000205//
Chris Lattner113f4f42002-06-25 16:13:24 +0000206bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000207 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000208 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000209 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000210 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000211}
212
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000213bool BasicBlockPass::doInitialization(Module &) {
214 // By default, don't do anything.
215 return false;
216}
217
218bool BasicBlockPass::doInitialization(Function &) {
219 // By default, don't do anything.
220 return false;
221}
222
223bool BasicBlockPass::doFinalization(Function &) {
224 // By default, don't do anything.
225 return false;
226}
227
228bool BasicBlockPass::doFinalization(Module &) {
229 // By default, don't do anything.
230 return false;
231}
232
233PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
234 return PMT_BasicBlockPassManager;
235}
236
Chris Lattner37d3c952002-07-23 18:08:00 +0000237// getPassInfo - Return the PassInfo data structure that corresponds to this
238// pass...
Owen Anderson81781222010-07-20 08:26:15 +0000239const PassInfo *Pass::getPassInfo() const {
Devang Patel0f88f762007-05-02 20:38:25 +0000240 return lookupPassInfo(PassID);
Chris Lattner4b169632002-08-21 17:08:37 +0000241}
242
Owen Anderson81781222010-07-20 08:26:15 +0000243const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000244 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000245}
246
Owen Anderson81781222010-07-20 08:26:15 +0000247const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000248 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000249}
250
Owen Anderson81781222010-07-20 08:26:15 +0000251Pass *PassInfo::createPass() const {
Dan Gohmanffdee302010-06-21 18:46:45 +0000252 assert((!isAnalysisGroup() || NormalCtor) &&
253 "No default implementation found for analysis group!");
254 assert(NormalCtor &&
255 "Cannot call createPass on PassInfo without default ctor!");
256 return NormalCtor();
257}
258
Chris Lattner6e041bd2002-08-21 22:17:09 +0000259//===----------------------------------------------------------------------===//
260// Analysis Group Implementation Code
261//===----------------------------------------------------------------------===//
262
Chris Lattner6e041bd2002-08-21 22:17:09 +0000263// RegisterAGBase implementation
264//
Dan Gohman0479aa52008-05-13 02:05:11 +0000265RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
Devang Patel0f88f762007-05-02 20:38:25 +0000266 intptr_t PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000267 : PassInfo(Name, InterfaceID) {
268 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
269 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000270}
271
Chris Lattner6e041bd2002-08-21 22:17:09 +0000272
Chris Lattner37d3c952002-07-23 18:08:00 +0000273//===----------------------------------------------------------------------===//
274// PassRegistrationListener implementation
275//
276
277// PassRegistrationListener ctor - Add the current object to the list of
278// PassRegistrationListeners...
279PassRegistrationListener::PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000280 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000281}
282
283// dtor - Remove object from list of listeners...
284PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000285 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000286}
287
288// enumeratePasses - Iterate over the registered passes, calling the
289// passEnumerate callback on each PassInfo object.
290//
291void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000292 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000293}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000294
Chris Lattner5264fce2010-01-22 06:29:25 +0000295PassNameParser::~PassNameParser() {}
296
Chris Lattner23d54052006-12-01 22:21:11 +0000297//===----------------------------------------------------------------------===//
298// AnalysisUsage Class Implementation
299//
300
Chris Lattner1b368a02006-12-01 23:27:45 +0000301namespace {
302 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000303 typedef AnalysisUsage::VectorType VectorType;
304 VectorType &CFGOnlyList;
305 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Chris Lattner1b368a02006-12-01 23:27:45 +0000306
Owen Anderson81781222010-07-20 08:26:15 +0000307 void passEnumerate(const PassInfo *P) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000308 if (P->isCFGOnlyPass())
309 CFGOnlyList.push_back(P);
310 }
311 };
312}
313
Chris Lattner23d54052006-12-01 22:21:11 +0000314// setPreservesCFG - This function should be called to by the pass, iff they do
315// not:
316//
317// 1. Add or remove basic blocks from the function
318// 2. Modify terminator instructions in any way.
319//
320// This function annotates the AnalysisUsage info object to say that analyses
321// that only depend on the CFG are preserved by this pass.
322//
323void AnalysisUsage::setPreservesCFG() {
324 // Since this transformation doesn't modify the CFG, it preserves all analyses
325 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000326 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000327}
328
Dan Gohmanffdee302010-06-21 18:46:45 +0000329AnalysisUsage &AnalysisUsage::addRequiredID(AnalysisID ID) {
330 assert(ID && "Pass class not registered!");
331 Required.push_back(ID);
332 return *this;
333}
Chris Lattner23d54052006-12-01 22:21:11 +0000334
Dan Gohmanffdee302010-06-21 18:46:45 +0000335AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(AnalysisID ID) {
336 assert(ID && "Pass class not registered!");
337 Required.push_back(ID);
338 RequiredTransitive.push_back(ID);
339 return *this;
340}