blob: 70ec108fa182c3e46b18c0e04842214803912c5d [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"
18#include "llvm/Module.h"
Misha Brukman56a86422003-10-14 21:38:42 +000019#include "llvm/ModuleProvider.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000020#include "llvm/ADT/STLExtras.h"
Chris Lattner1b368a02006-12-01 23:27:45 +000021#include "llvm/Support/ManagedStatic.h"
Owen Andersoneae97722009-06-17 21:16:20 +000022#include "llvm/Support/Threading.h"
23#include "llvm/System/Atomic.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000024#include <algorithm>
Dan Gohman99885692008-03-21 23:51:57 +000025#include <map>
Chris Lattner6e041bd2002-08-21 22:17:09 +000026#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000027using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000028
Chris Lattner7e0dbe62002-05-06 19:31:52 +000029//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000030// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000031//
Chris Lattnercdd09c22002-01-31 00:45:31 +000032
Devang Patelf5a994e2006-12-22 22:49:00 +000033// Force out-of-line virtual method.
Devang Patel2c1bba02007-04-26 21:33:42 +000034Pass::~Pass() {
35 delete Resolver;
36}
37
38// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000039ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000040
Chris Lattner9ad77572003-03-21 21:41:02 +000041bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
Duncan Sands5a913d62009-01-28 13:14:17 +000042 return Resolver->getAnalysisIfAvailable(AnalysisID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000043}
44
Chris Lattner198cf422002-07-30 16:27:02 +000045// dumpPassStructure - Implement the -debug-passes=Structure option
46void Pass::dumpPassStructure(unsigned Offset) {
Bill Wendling22e978a2006-12-07 20:04:42 +000047 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000048}
Chris Lattner37104aa2002-04-29 14:57:45 +000049
Dan Gohman94f57c52008-03-14 18:27:04 +000050/// getPassName - Return a nice clean name for a pass. This usually
51/// implemented in terms of the name that is registered by one of the
52/// Registration templates, but can be overloaded directly.
53///
Chris Lattner071577d2002-07-29 21:02:31 +000054const char *Pass::getPassName() const {
55 if (const PassInfo *PI = getPassInfo())
56 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000057 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000058}
Chris Lattner37104aa2002-04-29 14:57:45 +000059
Misha Brukman56a86422003-10-14 21:38:42 +000060// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +000061// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +000062// implement this method.
63//
Reid Spencer90839362004-12-07 04:03:45 +000064void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +000065 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
66}
67
Bill Wendling22e978a2006-12-07 20:04:42 +000068// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +000069void Pass::dump() const {
Bill Wendling22e978a2006-12-07 20:04:42 +000070 print(*cerr.stream(), 0);
Chris Lattner26750072002-07-27 01:12:17 +000071}
72
Chris Lattnercdd09c22002-01-31 00:45:31 +000073//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +000074// ImmutablePass Implementation
75//
Devang Patelf5a994e2006-12-22 22:49:00 +000076// Force out-of-line virtual method.
77ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +000078
79//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +000080// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +000081//
Chris Lattnercdd09c22002-01-31 00:45:31 +000082
Chris Lattnerc8e66542002-04-27 06:56:12 +000083// run - On a module, we run this pass by initializing, runOnFunction'ing once
84// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +000085//
Chris Lattner79e523d2004-09-20 04:47:19 +000086bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000087 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +000088
Chris Lattner113f4f42002-06-25 16:13:24 +000089 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000090 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +000091 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +000092
Chris Lattnercdd09c22002-01-31 00:45:31 +000093 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +000094}
95
Chris Lattnerc8e66542002-04-27 06:56:12 +000096// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +000097//
Chris Lattner113f4f42002-06-25 16:13:24 +000098bool FunctionPass::run(Function &F) {
Dan Gohman929391a2008-01-29 12:09:55 +000099 // Passes are not run on external functions!
100 if (F.isDeclaration()) return false;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000101
Chris Lattner79e523d2004-09-20 04:47:19 +0000102 bool Changed = doInitialization(*F.getParent());
103 Changed |= runOnFunction(F);
104 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000105}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000106
Chris Lattnercdd09c22002-01-31 00:45:31 +0000107//===----------------------------------------------------------------------===//
108// BasicBlockPass Implementation
109//
110
Chris Lattnerc8e66542002-04-27 06:56:12 +0000111// To run this pass on a function, we simply call runOnBasicBlock once for each
112// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000113//
Chris Lattner113f4f42002-06-25 16:13:24 +0000114bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000115 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000116 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000117 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000118 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000119}
120
Chris Lattner37d3c952002-07-23 18:08:00 +0000121//===----------------------------------------------------------------------===//
122// Pass Registration mechanism
123//
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000124namespace {
Chris Lattner1b368a02006-12-01 23:27:45 +0000125class PassRegistrar {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000126 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
127 /// pass.
Dan Gohman0479aa52008-05-13 02:05:11 +0000128 typedef std::map<intptr_t, const PassInfo*> MapType;
129 MapType PassInfoMap;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000130
131 /// AnalysisGroupInfo - Keep track of information for each analysis group.
132 struct AnalysisGroupInfo {
133 const PassInfo *DefaultImpl;
134 std::set<const PassInfo *> Implementations;
135 AnalysisGroupInfo() : DefaultImpl(0) {}
136 };
137
138 /// AnalysisGroupInfoMap - Information for each analysis group.
139 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
140
Chris Lattner1b368a02006-12-01 23:27:45 +0000141public:
142
Devang Patel0f88f762007-05-02 20:38:25 +0000143 const PassInfo *GetPassInfo(intptr_t TI) const {
Dan Gohman0479aa52008-05-13 02:05:11 +0000144 MapType::const_iterator I = PassInfoMap.find(TI);
Chris Lattner1b368a02006-12-01 23:27:45 +0000145 return I != PassInfoMap.end() ? I->second : 0;
146 }
147
Dan Gohman0479aa52008-05-13 02:05:11 +0000148 void RegisterPass(const PassInfo &PI) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000149 bool Inserted =
Devang Patel0f88f762007-05-02 20:38:25 +0000150 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
Chris Lattner106b0462008-06-21 19:47:03 +0000151 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
Chris Lattner1b368a02006-12-01 23:27:45 +0000152 }
153
Dan Gohman0479aa52008-05-13 02:05:11 +0000154 void UnregisterPass(const PassInfo &PI) {
155 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000156 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
157
158 // Remove pass from the map.
159 PassInfoMap.erase(I);
160 }
161
162 void EnumerateWith(PassRegistrationListener *L) {
Dan Gohman0479aa52008-05-13 02:05:11 +0000163 for (MapType::const_iterator I = PassInfoMap.begin(),
Chris Lattner1b368a02006-12-01 23:27:45 +0000164 E = PassInfoMap.end(); I != E; ++I)
165 L->passEnumerate(I->second);
166 }
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000167
168
169 /// Analysis Group Mechanisms.
170 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
171 const PassInfo *ImplementationInfo,
172 bool isDefault) {
173 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
174 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
175 "Cannot add a pass to the same analysis group more than once!");
176 AGI.Implementations.insert(ImplementationInfo);
177 if (isDefault) {
178 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
179 "Default implementation for analysis group already specified!");
180 assert(ImplementationInfo->getNormalCtor() &&
181 "Cannot specify pass as default if it does not have a default ctor");
182 AGI.DefaultImpl = ImplementationInfo;
183 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
184 }
185 }
Chris Lattner1b368a02006-12-01 23:27:45 +0000186};
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000187}
Chris Lattner1b368a02006-12-01 23:27:45 +0000188
Chris Lattner37d3c952002-07-23 18:08:00 +0000189static std::vector<PassRegistrationListener*> *Listeners = 0;
190
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000191// FIXME: This should use ManagedStatic to manage the pass registrar.
192// Unfortunately, we can't do this, because passes are registered with static
193// ctors, and having llvm_shutdown clear this map prevents successful
194// ressurection after llvm_shutdown is run.
195static PassRegistrar *getPassRegistrar() {
196 static PassRegistrar *PassRegistrarObj = 0;
Owen Andersoneae97722009-06-17 21:16:20 +0000197
198 // Use double-checked locking to safely initialize the registrar when
199 // we're running in multithreaded mode.
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000200 PassRegistrar* tmp = PassRegistrarObj;
201 sys::MemoryFence();
202 if (!tmp) {
Owen Andersoneae97722009-06-17 21:16:20 +0000203 if (llvm_is_multithreaded()) {
204 llvm_acquire_global_lock();
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000205 tmp = PassRegistrarObj;
206 if (!tmp) {
207 tmp = new PassRegistrar();
Owen Andersoneae97722009-06-17 21:16:20 +0000208 sys::MemoryFence();
209 PassRegistrarObj = tmp;
210 }
211 llvm_release_global_lock();
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000212 } else {
Owen Andersoneae97722009-06-17 21:16:20 +0000213 PassRegistrarObj = new PassRegistrar();
Owen Anderson0fd4eae2009-06-18 16:08:27 +0000214 }
Nick Lewycky87c4a052009-06-18 03:01:42 +0000215 }
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000216 return PassRegistrarObj;
217}
218
Chris Lattner37d3c952002-07-23 18:08:00 +0000219// getPassInfo - Return the PassInfo data structure that corresponds to this
220// pass...
221const PassInfo *Pass::getPassInfo() const {
Devang Patel0f88f762007-05-02 20:38:25 +0000222 return lookupPassInfo(PassID);
Chris Lattner4b169632002-08-21 17:08:37 +0000223}
224
Devang Patel0f88f762007-05-02 20:38:25 +0000225const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000226 return getPassRegistrar()->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000227}
228
Dan Gohman0479aa52008-05-13 02:05:11 +0000229void PassInfo::registerPass() {
230 getPassRegistrar()->RegisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000231
Chris Lattner1b368a02006-12-01 23:27:45 +0000232 // Notify any listeners.
Chris Lattner37d3c952002-07-23 18:08:00 +0000233 if (Listeners)
234 for (std::vector<PassRegistrationListener*>::iterator
235 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Dan Gohman0479aa52008-05-13 02:05:11 +0000236 (*I)->passRegistered(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000237}
238
Dan Gohman0479aa52008-05-13 02:05:11 +0000239void PassInfo::unregisterPass() {
240 getPassRegistrar()->UnregisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000241}
242
Chris Lattner6e041bd2002-08-21 22:17:09 +0000243//===----------------------------------------------------------------------===//
244// Analysis Group Implementation Code
245//===----------------------------------------------------------------------===//
246
Chris Lattner6e041bd2002-08-21 22:17:09 +0000247// RegisterAGBase implementation
248//
Dan Gohman0479aa52008-05-13 02:05:11 +0000249RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
Devang Patel0f88f762007-05-02 20:38:25 +0000250 intptr_t PassID, bool isDefault)
Dan Gohman0479aa52008-05-13 02:05:11 +0000251 : PassInfo(Name, InterfaceID),
Chris Lattnerc66da832006-01-23 01:01:04 +0000252 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000253
Devang Patel0f88f762007-05-02 20:38:25 +0000254 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
Chris Lattnerc66da832006-01-23 01:01:04 +0000255 if (InterfaceInfo == 0) {
256 // First reference to Interface, register it now.
257 registerPass();
Dan Gohman0479aa52008-05-13 02:05:11 +0000258 InterfaceInfo = this;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000259 }
Dan Gohman0479aa52008-05-13 02:05:11 +0000260 assert(isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000261 "Trying to join an analysis group that is a normal pass!");
262
Devang Patel0f88f762007-05-02 20:38:25 +0000263 if (PassID) {
264 ImplementationInfo = Pass::lookupPassInfo(PassID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000265 assert(ImplementationInfo &&
266 "Must register pass before adding to AnalysisGroup!");
267
Chris Lattnerb3708e22002-08-30 20:23:45 +0000268 // Make sure we keep track of the fact that the implementation implements
269 // the interface.
270 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
271 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000272
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000273 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000274 }
275}
276
Chris Lattner6e041bd2002-08-21 22:17:09 +0000277
Chris Lattner37d3c952002-07-23 18:08:00 +0000278//===----------------------------------------------------------------------===//
279// PassRegistrationListener implementation
280//
281
282// PassRegistrationListener ctor - Add the current object to the list of
283// PassRegistrationListeners...
284PassRegistrationListener::PassRegistrationListener() {
285 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
286 Listeners->push_back(this);
287}
288
289// dtor - Remove object from list of listeners...
290PassRegistrationListener::~PassRegistrationListener() {
291 std::vector<PassRegistrationListener*>::iterator I =
292 std::find(Listeners->begin(), Listeners->end(), this);
293 assert(Listeners && I != Listeners->end() &&
294 "PassRegistrationListener not registered!");
295 Listeners->erase(I);
296
297 if (Listeners->empty()) {
298 delete Listeners;
299 Listeners = 0;
300 }
301}
302
303// enumeratePasses - Iterate over the registered passes, calling the
304// passEnumerate callback on each PassInfo object.
305//
306void PassRegistrationListener::enumeratePasses() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000307 getPassRegistrar()->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000308}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000309
Chris Lattner23d54052006-12-01 22:21:11 +0000310//===----------------------------------------------------------------------===//
311// AnalysisUsage Class Implementation
312//
313
Chris Lattner1b368a02006-12-01 23:27:45 +0000314namespace {
315 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000316 typedef AnalysisUsage::VectorType VectorType;
317 VectorType &CFGOnlyList;
318 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Chris Lattner1b368a02006-12-01 23:27:45 +0000319
320 void passEnumerate(const PassInfo *P) {
321 if (P->isCFGOnlyPass())
322 CFGOnlyList.push_back(P);
323 }
324 };
325}
326
Chris Lattner23d54052006-12-01 22:21:11 +0000327// setPreservesCFG - This function should be called to by the pass, iff they do
328// not:
329//
330// 1. Add or remove basic blocks from the function
331// 2. Modify terminator instructions in any way.
332//
333// This function annotates the AnalysisUsage info object to say that analyses
334// that only depend on the CFG are preserved by this pass.
335//
336void AnalysisUsage::setPreservesCFG() {
337 // Since this transformation doesn't modify the CFG, it preserves all analyses
338 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000339 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000340}
341
342