blob: 35cbe906b70147d835392779b17946f495884cbd [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"
Jeff Cohenb622c112007-03-05 00:00:42 +000022#include <algorithm>
Dan Gohman99885692008-03-21 23:51:57 +000023#include <map>
Chris Lattner6e041bd2002-08-21 22:17:09 +000024#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chris Lattner7e0dbe62002-05-06 19:31:52 +000027//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000028// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000029//
Chris Lattnercdd09c22002-01-31 00:45:31 +000030
Devang Patelf5a994e2006-12-22 22:49:00 +000031// Force out-of-line virtual method.
Devang Patel2c1bba02007-04-26 21:33:42 +000032Pass::~Pass() {
33 delete Resolver;
34}
35
36// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000037ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000038
Chris Lattner9ad77572003-03-21 21:41:02 +000039bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
Devang Patelb66334b2007-01-05 22:47:07 +000040 return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000041}
42
Chris Lattner198cf422002-07-30 16:27:02 +000043// dumpPassStructure - Implement the -debug-passes=Structure option
44void Pass::dumpPassStructure(unsigned Offset) {
Bill Wendling22e978a2006-12-07 20:04:42 +000045 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000046}
Chris Lattner37104aa2002-04-29 14:57:45 +000047
Dan Gohman94f57c52008-03-14 18:27:04 +000048/// getPassName - Return a nice clean name for a pass. This usually
49/// implemented in terms of the name that is registered by one of the
50/// Registration templates, but can be overloaded directly.
51///
Chris Lattner071577d2002-07-29 21:02:31 +000052const char *Pass::getPassName() const {
53 if (const PassInfo *PI = getPassInfo())
54 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000055 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000056}
Chris Lattner37104aa2002-04-29 14:57:45 +000057
Misha Brukman56a86422003-10-14 21:38:42 +000058// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +000059// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +000060// implement this method.
61//
Reid Spencer90839362004-12-07 04:03:45 +000062void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +000063 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
64}
65
Bill Wendling22e978a2006-12-07 20:04:42 +000066// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +000067void Pass::dump() const {
Bill Wendling22e978a2006-12-07 20:04:42 +000068 print(*cerr.stream(), 0);
Chris Lattner26750072002-07-27 01:12:17 +000069}
70
Chris Lattnercdd09c22002-01-31 00:45:31 +000071//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +000072// ImmutablePass Implementation
73//
Devang Patelf5a994e2006-12-22 22:49:00 +000074// Force out-of-line virtual method.
75ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +000076
77//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +000078// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +000079//
Chris Lattnercdd09c22002-01-31 00:45:31 +000080
Chris Lattnerc8e66542002-04-27 06:56:12 +000081// run - On a module, we run this pass by initializing, runOnFunction'ing once
82// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +000083//
Chris Lattner79e523d2004-09-20 04:47:19 +000084bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000085 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +000086
Chris Lattner113f4f42002-06-25 16:13:24 +000087 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000088 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +000089 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +000090
Chris Lattnercdd09c22002-01-31 00:45:31 +000091 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +000092}
93
Chris Lattnerc8e66542002-04-27 06:56:12 +000094// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +000095//
Chris Lattner113f4f42002-06-25 16:13:24 +000096bool FunctionPass::run(Function &F) {
Dan Gohman929391a2008-01-29 12:09:55 +000097 // Passes are not run on external functions!
98 if (F.isDeclaration()) return false;
Chris Lattnercdd09c22002-01-31 00:45:31 +000099
Chris Lattner79e523d2004-09-20 04:47:19 +0000100 bool Changed = doInitialization(*F.getParent());
101 Changed |= runOnFunction(F);
102 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +0000103}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000104
Chris Lattnercdd09c22002-01-31 00:45:31 +0000105//===----------------------------------------------------------------------===//
106// BasicBlockPass Implementation
107//
108
Chris Lattnerc8e66542002-04-27 06:56:12 +0000109// To run this pass on a function, we simply call runOnBasicBlock once for each
110// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000111//
Chris Lattner113f4f42002-06-25 16:13:24 +0000112bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000113 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000114 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000115 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000116 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000117}
118
Chris Lattner37d3c952002-07-23 18:08:00 +0000119//===----------------------------------------------------------------------===//
120// Pass Registration mechanism
121//
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000122namespace {
Chris Lattner1b368a02006-12-01 23:27:45 +0000123class PassRegistrar {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000124 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
125 /// pass.
Dan Gohman0479aa52008-05-13 02:05:11 +0000126 typedef std::map<intptr_t, const PassInfo*> MapType;
127 MapType PassInfoMap;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000128
129 /// AnalysisGroupInfo - Keep track of information for each analysis group.
130 struct AnalysisGroupInfo {
131 const PassInfo *DefaultImpl;
132 std::set<const PassInfo *> Implementations;
133 AnalysisGroupInfo() : DefaultImpl(0) {}
134 };
135
136 /// AnalysisGroupInfoMap - Information for each analysis group.
137 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
138
Chris Lattner1b368a02006-12-01 23:27:45 +0000139public:
140
Devang Patel0f88f762007-05-02 20:38:25 +0000141 const PassInfo *GetPassInfo(intptr_t TI) const {
Dan Gohman0479aa52008-05-13 02:05:11 +0000142 MapType::const_iterator I = PassInfoMap.find(TI);
Chris Lattner1b368a02006-12-01 23:27:45 +0000143 return I != PassInfoMap.end() ? I->second : 0;
144 }
145
Dan Gohman0479aa52008-05-13 02:05:11 +0000146 void RegisterPass(const PassInfo &PI) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000147 bool Inserted =
Devang Patel0f88f762007-05-02 20:38:25 +0000148 PassInfoMap.insert(std::make_pair(PI.getTypeInfo(),&PI)).second;
Chris Lattner106b0462008-06-21 19:47:03 +0000149 assert(Inserted && "Pass registered multiple times!"); Inserted=Inserted;
Chris Lattner1b368a02006-12-01 23:27:45 +0000150 }
151
Dan Gohman0479aa52008-05-13 02:05:11 +0000152 void UnregisterPass(const PassInfo &PI) {
153 MapType::iterator I = PassInfoMap.find(PI.getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000154 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
155
156 // Remove pass from the map.
157 PassInfoMap.erase(I);
158 }
159
160 void EnumerateWith(PassRegistrationListener *L) {
Dan Gohman0479aa52008-05-13 02:05:11 +0000161 for (MapType::const_iterator I = PassInfoMap.begin(),
Chris Lattner1b368a02006-12-01 23:27:45 +0000162 E = PassInfoMap.end(); I != E; ++I)
163 L->passEnumerate(I->second);
164 }
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000165
166
167 /// Analysis Group Mechanisms.
168 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
169 const PassInfo *ImplementationInfo,
170 bool isDefault) {
171 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
172 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
173 "Cannot add a pass to the same analysis group more than once!");
174 AGI.Implementations.insert(ImplementationInfo);
175 if (isDefault) {
176 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
177 "Default implementation for analysis group already specified!");
178 assert(ImplementationInfo->getNormalCtor() &&
179 "Cannot specify pass as default if it does not have a default ctor");
180 AGI.DefaultImpl = ImplementationInfo;
181 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
182 }
183 }
Chris Lattner1b368a02006-12-01 23:27:45 +0000184};
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000185}
Chris Lattner1b368a02006-12-01 23:27:45 +0000186
Chris Lattner37d3c952002-07-23 18:08:00 +0000187static std::vector<PassRegistrationListener*> *Listeners = 0;
188
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000189// FIXME: This should use ManagedStatic to manage the pass registrar.
190// Unfortunately, we can't do this, because passes are registered with static
191// ctors, and having llvm_shutdown clear this map prevents successful
192// ressurection after llvm_shutdown is run.
193static PassRegistrar *getPassRegistrar() {
194 static PassRegistrar *PassRegistrarObj = 0;
195 if (!PassRegistrarObj)
196 PassRegistrarObj = new PassRegistrar();
197 return PassRegistrarObj;
198}
199
Chris Lattner37d3c952002-07-23 18:08:00 +0000200// getPassInfo - Return the PassInfo data structure that corresponds to this
201// pass...
202const PassInfo *Pass::getPassInfo() const {
Devang Patel0f88f762007-05-02 20:38:25 +0000203 return lookupPassInfo(PassID);
Chris Lattner4b169632002-08-21 17:08:37 +0000204}
205
Devang Patel0f88f762007-05-02 20:38:25 +0000206const PassInfo *Pass::lookupPassInfo(intptr_t TI) {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000207 return getPassRegistrar()->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000208}
209
Dan Gohman0479aa52008-05-13 02:05:11 +0000210void PassInfo::registerPass() {
211 getPassRegistrar()->RegisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000212
Chris Lattner1b368a02006-12-01 23:27:45 +0000213 // Notify any listeners.
Chris Lattner37d3c952002-07-23 18:08:00 +0000214 if (Listeners)
215 for (std::vector<PassRegistrationListener*>::iterator
216 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Dan Gohman0479aa52008-05-13 02:05:11 +0000217 (*I)->passRegistered(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000218}
219
Dan Gohman0479aa52008-05-13 02:05:11 +0000220void PassInfo::unregisterPass() {
221 getPassRegistrar()->UnregisterPass(*this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000222}
223
Chris Lattner6e041bd2002-08-21 22:17:09 +0000224//===----------------------------------------------------------------------===//
225// Analysis Group Implementation Code
226//===----------------------------------------------------------------------===//
227
Chris Lattner6e041bd2002-08-21 22:17:09 +0000228// RegisterAGBase implementation
229//
Dan Gohman0479aa52008-05-13 02:05:11 +0000230RegisterAGBase::RegisterAGBase(const char *Name, intptr_t InterfaceID,
Devang Patel0f88f762007-05-02 20:38:25 +0000231 intptr_t PassID, bool isDefault)
Dan Gohman0479aa52008-05-13 02:05:11 +0000232 : PassInfo(Name, InterfaceID),
Chris Lattnerc66da832006-01-23 01:01:04 +0000233 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000234
Devang Patel0f88f762007-05-02 20:38:25 +0000235 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(InterfaceID));
Chris Lattnerc66da832006-01-23 01:01:04 +0000236 if (InterfaceInfo == 0) {
237 // First reference to Interface, register it now.
238 registerPass();
Dan Gohman0479aa52008-05-13 02:05:11 +0000239 InterfaceInfo = this;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000240 }
Dan Gohman0479aa52008-05-13 02:05:11 +0000241 assert(isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000242 "Trying to join an analysis group that is a normal pass!");
243
Devang Patel0f88f762007-05-02 20:38:25 +0000244 if (PassID) {
245 ImplementationInfo = Pass::lookupPassInfo(PassID);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000246 assert(ImplementationInfo &&
247 "Must register pass before adding to AnalysisGroup!");
248
Chris Lattnerb3708e22002-08-30 20:23:45 +0000249 // Make sure we keep track of the fact that the implementation implements
250 // the interface.
251 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
252 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000253
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000254 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000255 }
256}
257
Chris Lattner6e041bd2002-08-21 22:17:09 +0000258
Chris Lattner37d3c952002-07-23 18:08:00 +0000259//===----------------------------------------------------------------------===//
260// PassRegistrationListener implementation
261//
262
263// PassRegistrationListener ctor - Add the current object to the list of
264// PassRegistrationListeners...
265PassRegistrationListener::PassRegistrationListener() {
266 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
267 Listeners->push_back(this);
268}
269
270// dtor - Remove object from list of listeners...
271PassRegistrationListener::~PassRegistrationListener() {
272 std::vector<PassRegistrationListener*>::iterator I =
273 std::find(Listeners->begin(), Listeners->end(), this);
274 assert(Listeners && I != Listeners->end() &&
275 "PassRegistrationListener not registered!");
276 Listeners->erase(I);
277
278 if (Listeners->empty()) {
279 delete Listeners;
280 Listeners = 0;
281 }
282}
283
284// enumeratePasses - Iterate over the registered passes, calling the
285// passEnumerate callback on each PassInfo object.
286//
287void PassRegistrationListener::enumeratePasses() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000288 getPassRegistrar()->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000289}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000290
Chris Lattner23d54052006-12-01 22:21:11 +0000291//===----------------------------------------------------------------------===//
292// AnalysisUsage Class Implementation
293//
294
Chris Lattner1b368a02006-12-01 23:27:45 +0000295namespace {
296 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000297 typedef AnalysisUsage::VectorType VectorType;
298 VectorType &CFGOnlyList;
299 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Chris Lattner1b368a02006-12-01 23:27:45 +0000300
301 void passEnumerate(const PassInfo *P) {
302 if (P->isCFGOnlyPass())
303 CFGOnlyList.push_back(P);
304 }
305 };
306}
307
Chris Lattner23d54052006-12-01 22:21:11 +0000308// setPreservesCFG - This function should be called to by the pass, iff they do
309// not:
310//
311// 1. Add or remove basic blocks from the function
312// 2. Modify terminator instructions in any way.
313//
314// This function annotates the AnalysisUsage info object to say that analyses
315// that only depend on the CFG are preserved by this pass.
316//
317void AnalysisUsage::setPreservesCFG() {
318 // Since this transformation doesn't modify the CFG, it preserves all analyses
319 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000320 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000321}
322
323