blob: e0fe6222b5010462990dff1a4460df0835c26df5 [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
Chris Lattnercdd09c22002-01-31 00:45:31 +000016#include "llvm/PassManager.h"
17#include "llvm/Module.h"
Misha Brukman56a86422003-10-14 21:38:42 +000018#include "llvm/ModuleProvider.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000019#include "llvm/ADT/STLExtras.h"
Chris Lattner1b368a02006-12-01 23:27:45 +000020#include "llvm/Support/ManagedStatic.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000021#include <algorithm>
Dan Gohman99885692008-03-21 23:51:57 +000022#include <map>
Chris Lattner6e041bd2002-08-21 22:17:09 +000023#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner7e0dbe62002-05-06 19:31:52 +000026//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000027// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000028//
Chris Lattnercdd09c22002-01-31 00:45:31 +000029
Devang Patelf5a994e2006-12-22 22:49:00 +000030// Force out-of-line virtual method.
Devang Patel2c1bba02007-04-26 21:33:42 +000031Pass::~Pass() {
32 delete Resolver;
33}
34
35// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000036ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000037
Chris Lattner9ad77572003-03-21 21:41:02 +000038bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
Devang Patelb66334b2007-01-05 22:47:07 +000039 return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000040}
41
Chris Lattner198cf422002-07-30 16:27:02 +000042// dumpPassStructure - Implement the -debug-passes=Structure option
43void Pass::dumpPassStructure(unsigned Offset) {
Bill Wendling22e978a2006-12-07 20:04:42 +000044 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000045}
Chris Lattner37104aa2002-04-29 14:57:45 +000046
Dan Gohman94f57c52008-03-14 18:27:04 +000047/// getPassName - Return a nice clean name for a pass. This usually
48/// implemented in terms of the name that is registered by one of the
49/// Registration templates, but can be overloaded directly.
50///
Chris Lattner071577d2002-07-29 21:02:31 +000051const char *Pass::getPassName() const {
52 if (const PassInfo *PI = getPassInfo())
53 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000054 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000055}
Chris Lattner37104aa2002-04-29 14:57:45 +000056
Misha Brukman56a86422003-10-14 21:38:42 +000057// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +000058// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +000059// implement this method.
60//
Reid Spencer90839362004-12-07 04:03:45 +000061void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +000062 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
63}
64
Bill Wendling22e978a2006-12-07 20:04:42 +000065// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +000066void Pass::dump() const {
Bill Wendling22e978a2006-12-07 20:04:42 +000067 print(*cerr.stream(), 0);
Chris Lattner26750072002-07-27 01:12:17 +000068}
69
Chris Lattnercdd09c22002-01-31 00:45:31 +000070//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +000071// ImmutablePass Implementation
72//
Devang Patelf5a994e2006-12-22 22:49:00 +000073// Force out-of-line virtual method.
74ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +000075
76//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +000077// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +000078//
Chris Lattnercdd09c22002-01-31 00:45:31 +000079
Chris Lattnerc8e66542002-04-27 06:56:12 +000080// run - On a module, we run this pass by initializing, runOnFunction'ing once
81// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +000082//
Chris Lattner79e523d2004-09-20 04:47:19 +000083bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000084 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +000085
Chris Lattner113f4f42002-06-25 16:13:24 +000086 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000087 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +000088 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +000089
Chris Lattnercdd09c22002-01-31 00:45:31 +000090 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +000091}
92
Chris Lattnerc8e66542002-04-27 06:56:12 +000093// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +000094//
Chris Lattner113f4f42002-06-25 16:13:24 +000095bool FunctionPass::run(Function &F) {
Dan Gohman929391a2008-01-29 12:09:55 +000096 // Passes are not run on external functions!
97 if (F.isDeclaration()) return false;
Chris Lattnercdd09c22002-01-31 00:45:31 +000098
Chris Lattner79e523d2004-09-20 04:47:19 +000099 bool Changed = doInitialization(*F.getParent());
100 Changed |= runOnFunction(F);
101 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000102}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000103
Chris Lattnercdd09c22002-01-31 00:45:31 +0000104//===----------------------------------------------------------------------===//
105// BasicBlockPass Implementation
106//
107
Chris Lattnerc8e66542002-04-27 06:56:12 +0000108// To run this pass on a function, we simply call runOnBasicBlock once for each
109// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000110//
Chris Lattner113f4f42002-06-25 16:13:24 +0000111bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000112 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000113 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000114 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000115 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000116}
117
Chris Lattner37d3c952002-07-23 18:08:00 +0000118//===----------------------------------------------------------------------===//
119// Pass Registration mechanism
120//
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000121namespace {
Chris Lattner1b368a02006-12-01 23:27:45 +0000122class PassRegistrar {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000123 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
124 /// pass.
Dan Gohman0479aa52008-05-13 02:05:11 +0000125 typedef std::map<intptr_t, const PassInfo*> MapType;
126 MapType PassInfoMap;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000127
128 /// AnalysisGroupInfo - Keep track of information for each analysis group.
129 struct AnalysisGroupInfo {
130 const PassInfo *DefaultImpl;
131 std::set<const PassInfo *> Implementations;
132 AnalysisGroupInfo() : DefaultImpl(0) {}
133 };
134
135 /// AnalysisGroupInfoMap - Information for each analysis group.
136 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
137
Chris Lattner1b368a02006-12-01 23:27:45 +0000138public:
139
Devang Patel0f88f762007-05-02 20:38:25 +0000140 const PassInfo *GetPassInfo(intptr_t TI) const {
Dan Gohman0479aa52008-05-13 02:05:11 +0000141 MapType::const_iterator I = PassInfoMap.find(TI);
Chris Lattner1b368a02006-12-01 23:27:45 +0000142 return I != PassInfoMap.end() ? I->second : 0;
143 }
144
Dan Gohman0479aa52008-05-13 02:05:11 +0000145 void RegisterPass(const PassInfo &PI) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000146 bool Inserted =
Devang Patel0f88f762007-05-02 20:38:25 +0000147 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
Chris Lattner11dfd352007-05-02 04:25:31 +0000148 assert(Inserted && "Pass registered multiple times!");
Chris Lattner1b368a02006-12-01 23:27:45 +0000149 }
150
Dan Gohman0479aa52008-05-13 02:05:11 +0000151 void UnregisterPass(const PassInfo &PI) {
152 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000153 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
154
155 // Remove pass from the map.
156 PassInfoMap.erase(I);
157 }
158
159 void EnumerateWith(PassRegistrationListener *L) {
Dan Gohman0479aa52008-05-13 02:05:11 +0000160 for (MapType::const_iterator I = PassInfoMap.begin(),
Chris Lattner1b368a02006-12-01 23:27:45 +0000161 E = PassInfoMap.end(); I != E; ++I)
162 L->passEnumerate(I->second);
163 }
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000164
165
166 /// Analysis Group Mechanisms.
167 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
168 const PassInfo *ImplementationInfo,
169 bool isDefault) {
170 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
171 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
172 "Cannot add a pass to the same analysis group more than once!");
173 AGI.Implementations.insert(ImplementationInfo);
174 if (isDefault) {
175 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
176 "Default implementation for analysis group already specified!");
177 assert(ImplementationInfo->getNormalCtor() &&
178 "Cannot specify pass as default if it does not have a default ctor");
179 AGI.DefaultImpl = ImplementationInfo;
180 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
181 }
182 }
Chris Lattner1b368a02006-12-01 23:27:45 +0000183};
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000184}
Chris Lattner1b368a02006-12-01 23:27:45 +0000185
Chris Lattner37d3c952002-07-23 18:08:00 +0000186static std::vector<PassRegistrationListener*> *Listeners = 0;
187
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000188// FIXME: This should use ManagedStatic to manage the pass registrar.
189// Unfortunately, we can't do this, because passes are registered with static
190// ctors, and having llvm_shutdown clear this map prevents successful
191// ressurection after llvm_shutdown is run.
192static PassRegistrar *getPassRegistrar() {
193 static PassRegistrar *PassRegistrarObj = 0;
194 if (!PassRegistrarObj)
195 PassRegistrarObj = new PassRegistrar();
196 return PassRegistrarObj;
197}
198
Chris Lattner37d3c952002-07-23 18:08:00 +0000199// getPassInfo - Return the PassInfo data structure that corresponds to this
200// pass...
201const PassInfo *Pass::getPassInfo() const {
Devang Patel0f88f762007-05-02 20:38:25 +0000202 return lookupPassInfo(PassID);
Chris Lattner4b169632002-08-21 17:08:37 +0000203}
204
Devang Patel0f88f762007-05-02 20:38:25 +0000205const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000206 return getPassRegistrar()->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000207}
208
Dan Gohman0479aa52008-05-13 02:05:11 +0000209void PassInfo::registerPass() {
210 getPassRegistrar()->RegisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000211
Chris Lattner1b368a02006-12-01 23:27:45 +0000212 // Notify any listeners.
Chris Lattner37d3c952002-07-23 18:08:00 +0000213 if (Listeners)
214 for (std::vector<PassRegistrationListener*>::iterator
215 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Dan Gohman0479aa52008-05-13 02:05:11 +0000216 (*I)->passRegistered(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000217}
218
Dan Gohman0479aa52008-05-13 02:05:11 +0000219void PassInfo::unregisterPass() {
220 getPassRegistrar()->UnregisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000221}
222
Chris Lattner6e041bd2002-08-21 22:17:09 +0000223//===----------------------------------------------------------------------===//
224// Analysis Group Implementation Code
225//===----------------------------------------------------------------------===//
226
Chris Lattner6e041bd2002-08-21 22:17:09 +0000227// RegisterAGBase implementation
228//
Dan Gohman0479aa52008-05-13 02:05:11 +0000229RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
Devang Patel0f88f762007-05-02 20:38:25 +0000230 intptr_t PassID, bool isDefault)
Dan Gohman0479aa52008-05-13 02:05:11 +0000231 : PassInfo(Name, InterfaceID),
Chris Lattnerc66da832006-01-23 01:01:04 +0000232 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000233
Devang Patel0f88f762007-05-02 20:38:25 +0000234 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
Chris Lattnerc66da832006-01-23 01:01:04 +0000235 if (InterfaceInfo == 0) {
236 // First reference to Interface, register it now.
237 registerPass();
Dan Gohman0479aa52008-05-13 02:05:11 +0000238 InterfaceInfo = this;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000239 }
Dan Gohman0479aa52008-05-13 02:05:11 +0000240 assert(isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000241 "Trying to join an analysis group that is a normal pass!");
242
Devang Patel0f88f762007-05-02 20:38:25 +0000243 if (PassID) {
244 ImplementationInfo = Pass::lookupPassInfo(PassID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000245 assert(ImplementationInfo &&
246 "Must register pass before adding to AnalysisGroup!");
247
Chris Lattnerb3708e22002-08-30 20:23:45 +0000248 // Make sure we keep track of the fact that the implementation implements
249 // the interface.
250 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
251 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000252
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000253 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000254 }
255}
256
Chris Lattner6e041bd2002-08-21 22:17:09 +0000257
Chris Lattner37d3c952002-07-23 18:08:00 +0000258//===----------------------------------------------------------------------===//
259// PassRegistrationListener implementation
260//
261
262// PassRegistrationListener ctor - Add the current object to the list of
263// PassRegistrationListeners...
264PassRegistrationListener::PassRegistrationListener() {
265 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
266 Listeners->push_back(this);
267}
268
269// dtor - Remove object from list of listeners...
270PassRegistrationListener::~PassRegistrationListener() {
271 std::vector<PassRegistrationListener*>::iterator I =
272 std::find(Listeners->begin(), Listeners->end(), this);
273 assert(Listeners && I != Listeners->end() &&
274 "PassRegistrationListener not registered!");
275 Listeners->erase(I);
276
277 if (Listeners->empty()) {
278 delete Listeners;
279 Listeners = 0;
280 }
281}
282
283// enumeratePasses - Iterate over the registered passes, calling the
284// passEnumerate callback on each PassInfo object.
285//
286void PassRegistrationListener::enumeratePasses() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000287 getPassRegistrar()->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000288}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000289
Chris Lattner23d54052006-12-01 22:21:11 +0000290//===----------------------------------------------------------------------===//
291// AnalysisUsage Class Implementation
292//
293
Chris Lattner1b368a02006-12-01 23:27:45 +0000294namespace {
295 struct GetCFGOnlyPasses : public PassRegistrationListener {
296 std::vector<AnalysisID> &CFGOnlyList;
297 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
298
299 void passEnumerate(const PassInfo *P) {
300 if (P->isCFGOnlyPass())
301 CFGOnlyList.push_back(P);
302 }
303 };
304}
305
Chris Lattner23d54052006-12-01 22:21:11 +0000306// setPreservesCFG - This function should be called to by the pass, iff they do
307// not:
308//
309// 1. Add or remove basic blocks from the function
310// 2. Modify terminator instructions in any way.
311//
312// This function annotates the AnalysisUsage info object to say that analyses
313// that only depend on the CFG are preserved by this pass.
314//
315void AnalysisUsage::setPreservesCFG() {
316 // Since this transformation doesn't modify the CFG, it preserves all analyses
317 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000318 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000319}
320
321