blob: fb23fbfbe15e5f52590074541ee216eb855f6636 [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
Owen Andersona7aed182010-08-06 18:33:48 +000039Pass::Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
Dan Gohmanffdee302010-06-21 18:46:45 +000040
Devang Patelf5a994e2006-12-22 22:49:00 +000041// Force out-of-line virtual method.
Devang Patel2c1bba02007-04-26 21:33:42 +000042Pass::~Pass() {
43 delete Resolver;
44}
45
46// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000047ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000048
David Greene9b063df2010-04-02 23:17:14 +000049Pass *ModulePass::createPrinterPass(raw_ostream &O,
50 const std::string &Banner) const {
51 return createPrintModulePass(&O, false, Banner);
52}
53
Dan Gohman1cfbfc82009-12-14 19:43:09 +000054PassManagerType ModulePass::getPotentialPassManagerType() const {
55 return PMT_ModulePassManager;
56}
57
Owen Andersona7aed182010-08-06 18:33:48 +000058bool Pass::mustPreserveAnalysisID(char &AID) const {
59 return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000060}
61
Chris Lattner198cf422002-07-30 16:27:02 +000062// dumpPassStructure - Implement the -debug-passes=Structure option
63void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000064 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000065}
Chris Lattner37104aa2002-04-29 14:57:45 +000066
Dan Gohman94f57c52008-03-14 18:27:04 +000067/// getPassName - Return a nice clean name for a pass. This usually
68/// implemented in terms of the name that is registered by one of the
69/// Registration templates, but can be overloaded directly.
70///
Chris Lattner071577d2002-07-29 21:02:31 +000071const char *Pass::getPassName() const {
Owen Andersona7aed182010-08-06 18:33:48 +000072 AnalysisID AID = getPassID();
73 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
74 if (PI)
Chris Lattner071577d2002-07-29 21:02:31 +000075 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000076 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000077}
Chris Lattner37104aa2002-04-29 14:57:45 +000078
Dan Gohman1cfbfc82009-12-14 19:43:09 +000079void Pass::preparePassManager(PMStack &) {
80 // By default, don't do anything.
81}
82
83PassManagerType Pass::getPotentialPassManagerType() const {
84 // Default implementation.
85 return PMT_Unknown;
86}
87
88void Pass::getAnalysisUsage(AnalysisUsage &) const {
89 // By default, no analysis results are used, all are invalidated.
90}
91
92void Pass::releaseMemory() {
93 // By default, don't do anything.
94}
95
96void Pass::verifyAnalysis() const {
97 // By default, don't do anything.
98}
99
Owen Andersona7aed182010-08-06 18:33:48 +0000100void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000101 return this;
102}
103
104ImmutablePass *Pass::getAsImmutablePass() {
105 return 0;
106}
107
108PMDataManager *Pass::getAsPMDataManager() {
109 return 0;
110}
111
112void Pass::setResolver(AnalysisResolver *AR) {
113 assert(!Resolver && "Resolver is already set");
114 Resolver = AR;
115}
116
Misha Brukman56a86422003-10-14 21:38:42 +0000117// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000118// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000119// implement this method.
120//
Chris Lattner13626022009-08-23 06:03:38 +0000121void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000122 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
123}
124
Bill Wendling22e978a2006-12-07 20:04:42 +0000125// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +0000126void Pass::dump() const {
David Greenedfe4ad72010-01-05 01:30:04 +0000127 print(dbgs(), 0);
Chris Lattner26750072002-07-27 01:12:17 +0000128}
129
Chris Lattnercdd09c22002-01-31 00:45:31 +0000130//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000131// ImmutablePass Implementation
132//
Devang Patelf5a994e2006-12-22 22:49:00 +0000133// Force out-of-line virtual method.
134ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000135
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000136void ImmutablePass::initializePass() {
137 // By default, don't do anything.
138}
139
Chris Lattneree0788d2002-09-25 21:59:11 +0000140//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000141// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000142//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000143
David Greene9b063df2010-04-02 23:17:14 +0000144Pass *FunctionPass::createPrinterPass(raw_ostream &O,
145 const std::string &Banner) const {
146 return createPrintFunctionPass(Banner, &O);
147}
148
Chris Lattnerc8e66542002-04-27 06:56:12 +0000149// run - On a module, we run this pass by initializing, runOnFunction'ing once
150// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000151//
Chris Lattner79e523d2004-09-20 04:47:19 +0000152bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +0000153 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154
Chris Lattner113f4f42002-06-25 16:13:24 +0000155 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +0000156 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +0000157 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000158
Chris Lattnercdd09c22002-01-31 00:45:31 +0000159 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +0000160}
161
Chris Lattnerc8e66542002-04-27 06:56:12 +0000162// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000163//
Chris Lattner113f4f42002-06-25 16:13:24 +0000164bool FunctionPass::run(Function &F) {
Dan Gohman929391a2008-01-29 12:09:55 +0000165 // Passes are not run on external functions!
166 if (F.isDeclaration()) return false;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000167
Chris Lattner79e523d2004-09-20 04:47:19 +0000168 bool Changed = doInitialization(*F.getParent());
169 Changed |= runOnFunction(F);
170 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000171}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000172
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000173bool FunctionPass::doInitialization(Module &) {
174 // By default, don't do anything.
175 return false;
176}
177
178bool FunctionPass::doFinalization(Module &) {
179 // By default, don't do anything.
180 return false;
181}
182
183PassManagerType FunctionPass::getPotentialPassManagerType() const {
184 return PMT_FunctionPassManager;
185}
186
Chris Lattnercdd09c22002-01-31 00:45:31 +0000187//===----------------------------------------------------------------------===//
188// BasicBlockPass Implementation
189//
190
David Greene9b063df2010-04-02 23:17:14 +0000191Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
192 const std::string &Banner) const {
193
194 llvm_unreachable("BasicBlockPass printing unsupported.");
195 return 0;
196}
197
Chris Lattnerc8e66542002-04-27 06:56:12 +0000198// To run this pass on a function, we simply call runOnBasicBlock once for each
199// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000200//
Chris Lattner113f4f42002-06-25 16:13:24 +0000201bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000202 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000203 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000204 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000205 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000206}
207
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000208bool BasicBlockPass::doInitialization(Module &) {
209 // By default, don't do anything.
210 return false;
211}
212
213bool BasicBlockPass::doInitialization(Function &) {
214 // By default, don't do anything.
215 return false;
216}
217
218bool BasicBlockPass::doFinalization(Function &) {
219 // By default, don't do anything.
220 return false;
221}
222
223bool BasicBlockPass::doFinalization(Module &) {
224 // By default, don't do anything.
225 return false;
226}
227
228PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
229 return PMT_BasicBlockPassManager;
230}
231
Owen Andersona7aed182010-08-06 18:33:48 +0000232const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000233 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000234}
235
Owen Anderson81781222010-07-20 08:26:15 +0000236const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000237 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000238}
239
Owen Anderson81781222010-07-20 08:26:15 +0000240Pass *PassInfo::createPass() const {
Dan Gohmanffdee302010-06-21 18:46:45 +0000241 assert((!isAnalysisGroup() || NormalCtor) &&
242 "No default implementation found for analysis group!");
243 assert(NormalCtor &&
244 "Cannot call createPass on PassInfo without default ctor!");
245 return NormalCtor();
246}
247
Chris Lattner6e041bd2002-08-21 22:17:09 +0000248//===----------------------------------------------------------------------===//
249// Analysis Group Implementation Code
250//===----------------------------------------------------------------------===//
251
Chris Lattner6e041bd2002-08-21 22:17:09 +0000252// RegisterAGBase implementation
253//
Owen Andersona7aed182010-08-06 18:33:48 +0000254RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
255 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000256 : PassInfo(Name, InterfaceID) {
257 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
258 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000259}
260
Chris Lattner6e041bd2002-08-21 22:17:09 +0000261
Chris Lattner37d3c952002-07-23 18:08:00 +0000262//===----------------------------------------------------------------------===//
263// PassRegistrationListener implementation
264//
265
266// PassRegistrationListener ctor - Add the current object to the list of
267// PassRegistrationListeners...
268PassRegistrationListener::PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000269 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000270}
271
272// dtor - Remove object from list of listeners...
273PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000274 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000275}
276
277// enumeratePasses - Iterate over the registered passes, calling the
278// passEnumerate callback on each PassInfo object.
279//
280void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000281 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000282}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000283
Chris Lattner5264fce2010-01-22 06:29:25 +0000284PassNameParser::~PassNameParser() {}
285
Chris Lattner23d54052006-12-01 22:21:11 +0000286//===----------------------------------------------------------------------===//
287// AnalysisUsage Class Implementation
288//
289
Chris Lattner1b368a02006-12-01 23:27:45 +0000290namespace {
291 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000292 typedef AnalysisUsage::VectorType VectorType;
293 VectorType &CFGOnlyList;
294 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Chris Lattner1b368a02006-12-01 23:27:45 +0000295
Owen Anderson81781222010-07-20 08:26:15 +0000296 void passEnumerate(const PassInfo *P) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000297 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000298 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000299 }
300 };
301}
302
Chris Lattner23d54052006-12-01 22:21:11 +0000303// setPreservesCFG - This function should be called to by the pass, iff they do
304// not:
305//
306// 1. Add or remove basic blocks from the function
307// 2. Modify terminator instructions in any way.
308//
309// This function annotates the AnalysisUsage info object to say that analyses
310// that only depend on the CFG are preserved by this pass.
311//
312void AnalysisUsage::setPreservesCFG() {
313 // Since this transformation doesn't modify the CFG, it preserves all analyses
314 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000315 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000316}
317
Owen Andersona7aed182010-08-06 18:33:48 +0000318AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
319 const PassInfo *PI = Pass::lookupPassInfo(Arg);
320 // If the pass exists, preserve it. Otherwise silently do nothing.
321 if (PI) Preserved.push_back(PI->getTypeInfo());
322 return *this;
323}
324
325AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000326 Required.push_back(ID);
327 return *this;
328}
Chris Lattner23d54052006-12-01 22:21:11 +0000329
Owen Andersona7aed182010-08-06 18:33:48 +0000330AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
331 Required.push_back(&ID);
332 return *this;
333}
334
335AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
336 Required.push_back(&ID);
337 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000338 return *this;
339}