blob: e6b31b302a952dd52ffb694df6b865cd95f87bd3 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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"
Reid Spencer7c16caa2004-09-01 22:55:40 +000021#include "llvm/Support/TypeInfo.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000022#include <algorithm>
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
Brian Gaeke465a5cc2004-02-28 21:55:18 +000047// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
Chris Lattner37104aa2002-04-29 14:57:45 +000048//
Chris Lattner071577d2002-07-29 21:02:31 +000049const char *Pass::getPassName() const {
50 if (const PassInfo *PI = getPassInfo())
51 return PI->getPassName();
52 return typeid(*this).name();
53}
Chris Lattner37104aa2002-04-29 14:57:45 +000054
Misha Brukman56a86422003-10-14 21:38:42 +000055// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +000056// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +000057// implement this method.
58//
Reid Spencer90839362004-12-07 04:03:45 +000059void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +000060 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
61}
62
Bill Wendling22e978a2006-12-07 20:04:42 +000063// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +000064void Pass::dump() const {
Bill Wendling22e978a2006-12-07 20:04:42 +000065 print(*cerr.stream(), 0);
Chris Lattner26750072002-07-27 01:12:17 +000066}
67
Chris Lattnercdd09c22002-01-31 00:45:31 +000068//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +000069// ImmutablePass Implementation
70//
Devang Patelf5a994e2006-12-22 22:49:00 +000071// Force out-of-line virtual method.
72ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +000073
74//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +000075// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +000076//
Chris Lattnercdd09c22002-01-31 00:45:31 +000077
Chris Lattnerc8e66542002-04-27 06:56:12 +000078// run - On a module, we run this pass by initializing, runOnFunction'ing once
79// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +000080//
Chris Lattner79e523d2004-09-20 04:47:19 +000081bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000082 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +000083
Chris Lattner113f4f42002-06-25 16:13:24 +000084 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000085 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +000086 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +000087
Chris Lattnercdd09c22002-01-31 00:45:31 +000088 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +000089}
90
Chris Lattnerc8e66542002-04-27 06:56:12 +000091// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +000092//
Chris Lattner113f4f42002-06-25 16:13:24 +000093bool FunctionPass::run(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000094 if (F.isDeclaration()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +000095
Chris Lattner79e523d2004-09-20 04:47:19 +000096 bool Changed = doInitialization(*F.getParent());
97 Changed |= runOnFunction(F);
98 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +000099}
Chris Lattnerd013ba92002-01-23 05:49:41 +0000100
Chris Lattnercdd09c22002-01-31 00:45:31 +0000101//===----------------------------------------------------------------------===//
102// BasicBlockPass Implementation
103//
104
Chris Lattnerc8e66542002-04-27 06:56:12 +0000105// To run this pass on a function, we simply call runOnBasicBlock once for each
106// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000107//
Chris Lattner113f4f42002-06-25 16:13:24 +0000108bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000109 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000110 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000111 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000112 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000113}
114
115// To run directly on the basic block, we initialize, runOnBasicBlock, then
116// finalize.
117//
Chris Lattner79e523d2004-09-20 04:47:19 +0000118bool BasicBlockPass::runPass(BasicBlock &BB) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000119 Function &F = *BB.getParent();
120 Module &M = *F.getParent();
Chris Lattner79e523d2004-09-20 04:47:19 +0000121 bool Changed = doInitialization(M);
122 Changed |= doInitialization(F);
123 Changed |= runOnBasicBlock(BB);
124 Changed |= doFinalization(F);
125 Changed |= doFinalization(M);
126 return Changed;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000127}
128
Chris Lattner37d3c952002-07-23 18:08:00 +0000129//===----------------------------------------------------------------------===//
130// Pass Registration mechanism
131//
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000132namespace {
Chris Lattner1b368a02006-12-01 23:27:45 +0000133class PassRegistrar {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000134 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
135 /// pass.
Chris Lattner1b368a02006-12-01 23:27:45 +0000136 std::map<TypeInfo, PassInfo*> PassInfoMap;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000137
138 /// AnalysisGroupInfo - Keep track of information for each analysis group.
139 struct AnalysisGroupInfo {
140 const PassInfo *DefaultImpl;
141 std::set<const PassInfo *> Implementations;
142 AnalysisGroupInfo() : DefaultImpl(0) {}
143 };
144
145 /// AnalysisGroupInfoMap - Information for each analysis group.
146 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
147
Chris Lattner1b368a02006-12-01 23:27:45 +0000148public:
149
150 const PassInfo *GetPassInfo(const std::type_info &TI) const {
151 std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
152 return I != PassInfoMap.end() ? I->second : 0;
153 }
154
155 void RegisterPass(PassInfo &PI) {
156 bool Inserted =
157 PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
158 assert(Inserted && "Pass registered multiple times!");
159 }
160
161 void UnregisterPass(PassInfo &PI) {
162 std::map<TypeInfo, PassInfo*>::iterator I =
163 PassInfoMap.find(PI.getTypeInfo());
164 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
165
166 // Remove pass from the map.
167 PassInfoMap.erase(I);
168 }
169
170 void EnumerateWith(PassRegistrationListener *L) {
171 for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
172 E = PassInfoMap.end(); I != E; ++I)
173 L->passEnumerate(I->second);
174 }
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000175
176
177 /// Analysis Group Mechanisms.
178 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
179 const PassInfo *ImplementationInfo,
180 bool isDefault) {
181 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
182 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
183 "Cannot add a pass to the same analysis group more than once!");
184 AGI.Implementations.insert(ImplementationInfo);
185 if (isDefault) {
186 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
187 "Default implementation for analysis group already specified!");
188 assert(ImplementationInfo->getNormalCtor() &&
189 "Cannot specify pass as default if it does not have a default ctor");
190 AGI.DefaultImpl = ImplementationInfo;
191 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
192 }
193 }
Chris Lattner1b368a02006-12-01 23:27:45 +0000194};
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000195}
Chris Lattner1b368a02006-12-01 23:27:45 +0000196
Chris Lattner37d3c952002-07-23 18:08:00 +0000197static std::vector<PassRegistrationListener*> *Listeners = 0;
198
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000199// FIXME: This should use ManagedStatic to manage the pass registrar.
200// Unfortunately, we can't do this, because passes are registered with static
201// ctors, and having llvm_shutdown clear this map prevents successful
202// ressurection after llvm_shutdown is run.
203static PassRegistrar *getPassRegistrar() {
204 static PassRegistrar *PassRegistrarObj = 0;
205 if (!PassRegistrarObj)
206 PassRegistrarObj = new PassRegistrar();
207 return PassRegistrarObj;
208}
209
Chris Lattner37d3c952002-07-23 18:08:00 +0000210// getPassInfo - Return the PassInfo data structure that corresponds to this
211// pass...
212const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000213 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000214 return lookupPassInfo(typeid(*this));
215}
216
217const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000218 return getPassRegistrar()->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000219}
220
Chris Lattnerc66da832006-01-23 01:01:04 +0000221void RegisterPassBase::registerPass() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000222 getPassRegistrar()->RegisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000223
Chris Lattner1b368a02006-12-01 23:27:45 +0000224 // Notify any listeners.
Chris Lattner37d3c952002-07-23 18:08:00 +0000225 if (Listeners)
226 for (std::vector<PassRegistrationListener*>::iterator
227 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000228 (*I)->passRegistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000229}
230
Chris Lattnerc66da832006-01-23 01:01:04 +0000231void RegisterPassBase::unregisterPass() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000232 getPassRegistrar()->UnregisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000233}
234
Chris Lattner6e041bd2002-08-21 22:17:09 +0000235//===----------------------------------------------------------------------===//
236// Analysis Group Implementation Code
237//===----------------------------------------------------------------------===//
238
Chris Lattner6e041bd2002-08-21 22:17:09 +0000239// RegisterAGBase implementation
240//
241RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
242 const std::type_info *Pass, bool isDefault)
Chris Lattnerfbb46502006-08-27 22:21:55 +0000243 : RegisterPassBase(Interface),
Chris Lattnerc66da832006-01-23 01:01:04 +0000244 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000245
Chris Lattner6e041bd2002-08-21 22:17:09 +0000246 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
Chris Lattnerc66da832006-01-23 01:01:04 +0000247 if (InterfaceInfo == 0) {
248 // First reference to Interface, register it now.
249 registerPass();
250 InterfaceInfo = &PIObj;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000251 }
Chris Lattnerfbb46502006-08-27 22:21:55 +0000252 assert(PIObj.isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000253 "Trying to join an analysis group that is a normal pass!");
254
255 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000256 ImplementationInfo = Pass::lookupPassInfo(*Pass);
257 assert(ImplementationInfo &&
258 "Must register pass before adding to AnalysisGroup!");
259
Chris Lattnerb3708e22002-08-30 20:23:45 +0000260 // Make sure we keep track of the fact that the implementation implements
261 // the interface.
262 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
263 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000264
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000265 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000266 }
267}
268
269void RegisterAGBase::setGroupName(const char *Name) {
270 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
271 InterfaceInfo->setPassName(Name);
272}
273
Chris Lattner6e041bd2002-08-21 22:17:09 +0000274
Chris Lattner37d3c952002-07-23 18:08:00 +0000275//===----------------------------------------------------------------------===//
276// PassRegistrationListener implementation
277//
278
279// PassRegistrationListener ctor - Add the current object to the list of
280// PassRegistrationListeners...
281PassRegistrationListener::PassRegistrationListener() {
282 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
283 Listeners->push_back(this);
284}
285
286// dtor - Remove object from list of listeners...
287PassRegistrationListener::~PassRegistrationListener() {
288 std::vector<PassRegistrationListener*>::iterator I =
289 std::find(Listeners->begin(), Listeners->end(), this);
290 assert(Listeners && I != Listeners->end() &&
291 "PassRegistrationListener not registered!");
292 Listeners->erase(I);
293
294 if (Listeners->empty()) {
295 delete Listeners;
296 Listeners = 0;
297 }
298}
299
300// enumeratePasses - Iterate over the registered passes, calling the
301// passEnumerate callback on each PassInfo object.
302//
303void PassRegistrationListener::enumeratePasses() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000304 getPassRegistrar()->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000305}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000306
Chris Lattner23d54052006-12-01 22:21:11 +0000307//===----------------------------------------------------------------------===//
308// AnalysisUsage Class Implementation
309//
310
Chris Lattner1b368a02006-12-01 23:27:45 +0000311namespace {
312 struct GetCFGOnlyPasses : public PassRegistrationListener {
313 std::vector<AnalysisID> &CFGOnlyList;
314 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
315
316 void passEnumerate(const PassInfo *P) {
317 if (P->isCFGOnlyPass())
318 CFGOnlyList.push_back(P);
319 }
320 };
321}
322
Chris Lattner23d54052006-12-01 22:21:11 +0000323// setPreservesCFG - This function should be called to by the pass, iff they do
324// not:
325//
326// 1. Add or remove basic blocks from the function
327// 2. Modify terminator instructions in any way.
328//
329// This function annotates the AnalysisUsage info object to say that analyses
330// that only depend on the CFG are preserved by this pass.
331//
332void AnalysisUsage::setPreservesCFG() {
333 // Since this transformation doesn't modify the CFG, it preserves all analyses
334 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000335 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000336}
337
338