blob: be49ae41a183c3a71630006c16431951707eef7d [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.
31ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000032
Chris Lattner9ad77572003-03-21 21:41:02 +000033bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
Devang Patelb66334b2007-01-05 22:47:07 +000034 return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000035}
36
Chris Lattner198cf422002-07-30 16:27:02 +000037// dumpPassStructure - Implement the -debug-passes=Structure option
38void Pass::dumpPassStructure(unsigned Offset) {
Bill Wendling22e978a2006-12-07 20:04:42 +000039 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000040}
Chris Lattner37104aa2002-04-29 14:57:45 +000041
Brian Gaeke465a5cc2004-02-28 21:55:18 +000042// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
Chris Lattner37104aa2002-04-29 14:57:45 +000043//
Chris Lattner071577d2002-07-29 21:02:31 +000044const char *Pass::getPassName() const {
45 if (const PassInfo *PI = getPassInfo())
46 return PI->getPassName();
47 return typeid(*this).name();
48}
Chris Lattner37104aa2002-04-29 14:57:45 +000049
Misha Brukman56a86422003-10-14 21:38:42 +000050// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +000051// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +000052// implement this method.
53//
Reid Spencer90839362004-12-07 04:03:45 +000054void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +000055 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
56}
57
Bill Wendling22e978a2006-12-07 20:04:42 +000058// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +000059void Pass::dump() const {
Bill Wendling22e978a2006-12-07 20:04:42 +000060 print(*cerr.stream(), 0);
Chris Lattner26750072002-07-27 01:12:17 +000061}
62
Chris Lattnercdd09c22002-01-31 00:45:31 +000063//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +000064// ImmutablePass Implementation
65//
Devang Patelf5a994e2006-12-22 22:49:00 +000066// Force out-of-line virtual method.
67ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +000068
69//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +000070// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +000071//
Chris Lattnercdd09c22002-01-31 00:45:31 +000072
Chris Lattnerc8e66542002-04-27 06:56:12 +000073// run - On a module, we run this pass by initializing, runOnFunction'ing once
74// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +000075//
Chris Lattner79e523d2004-09-20 04:47:19 +000076bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000077 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +000078
Chris Lattner113f4f42002-06-25 16:13:24 +000079 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000080 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +000081 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +000082
Chris Lattnercdd09c22002-01-31 00:45:31 +000083 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +000084}
85
Chris Lattnerc8e66542002-04-27 06:56:12 +000086// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +000087//
Chris Lattner113f4f42002-06-25 16:13:24 +000088bool FunctionPass::run(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000089 if (F.isDeclaration()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +000090
Chris Lattner79e523d2004-09-20 04:47:19 +000091 bool Changed = doInitialization(*F.getParent());
92 Changed |= runOnFunction(F);
93 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +000094}
Chris Lattnerd013ba92002-01-23 05:49:41 +000095
Chris Lattnercdd09c22002-01-31 00:45:31 +000096//===----------------------------------------------------------------------===//
97// BasicBlockPass Implementation
98//
99
Chris Lattnerc8e66542002-04-27 06:56:12 +0000100// To run this pass on a function, we simply call runOnBasicBlock once for each
101// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000102//
Chris Lattner113f4f42002-06-25 16:13:24 +0000103bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000104 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000105 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000106 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000107 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000108}
109
110// To run directly on the basic block, we initialize, runOnBasicBlock, then
111// finalize.
112//
Chris Lattner79e523d2004-09-20 04:47:19 +0000113bool BasicBlockPass::runPass(BasicBlock &BB) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000114 Function &F = *BB.getParent();
115 Module &M = *F.getParent();
Chris Lattner79e523d2004-09-20 04:47:19 +0000116 bool Changed = doInitialization(M);
117 Changed |= doInitialization(F);
118 Changed |= runOnBasicBlock(BB);
119 Changed |= doFinalization(F);
120 Changed |= doFinalization(M);
121 return Changed;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000122}
123
Chris Lattner37d3c952002-07-23 18:08:00 +0000124//===----------------------------------------------------------------------===//
125// Pass Registration mechanism
126//
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000127namespace {
Chris Lattner1b368a02006-12-01 23:27:45 +0000128class PassRegistrar {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000129 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
130 /// pass.
Chris Lattner1b368a02006-12-01 23:27:45 +0000131 std::map<TypeInfo, PassInfo*> PassInfoMap;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000132
133 /// AnalysisGroupInfo - Keep track of information for each analysis group.
134 struct AnalysisGroupInfo {
135 const PassInfo *DefaultImpl;
136 std::set<const PassInfo *> Implementations;
137 AnalysisGroupInfo() : DefaultImpl(0) {}
138 };
139
140 /// AnalysisGroupInfoMap - Information for each analysis group.
141 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
142
Chris Lattner1b368a02006-12-01 23:27:45 +0000143public:
144
145 const PassInfo *GetPassInfo(const std::type_info &TI) const {
146 std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
147 return I != PassInfoMap.end() ? I->second : 0;
148 }
149
150 void RegisterPass(PassInfo &PI) {
151 bool Inserted =
152 PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
153 assert(Inserted && "Pass registered multiple times!");
154 }
155
156 void UnregisterPass(PassInfo &PI) {
157 std::map<TypeInfo, PassInfo*>::iterator I =
158 PassInfoMap.find(PI.getTypeInfo());
159 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
160
161 // Remove pass from the map.
162 PassInfoMap.erase(I);
163 }
164
165 void EnumerateWith(PassRegistrationListener *L) {
166 for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
167 E = PassInfoMap.end(); I != E; ++I)
168 L->passEnumerate(I->second);
169 }
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000170
171
172 /// Analysis Group Mechanisms.
173 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
174 const PassInfo *ImplementationInfo,
175 bool isDefault) {
176 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
177 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
178 "Cannot add a pass to the same analysis group more than once!");
179 AGI.Implementations.insert(ImplementationInfo);
180 if (isDefault) {
181 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
182 "Default implementation for analysis group already specified!");
183 assert(ImplementationInfo->getNormalCtor() &&
184 "Cannot specify pass as default if it does not have a default ctor");
185 AGI.DefaultImpl = ImplementationInfo;
186 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
187 }
188 }
Chris Lattner1b368a02006-12-01 23:27:45 +0000189};
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000190}
Chris Lattner1b368a02006-12-01 23:27:45 +0000191
Chris Lattner37d3c952002-07-23 18:08:00 +0000192static std::vector<PassRegistrationListener*> *Listeners = 0;
193
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000194// FIXME: This should use ManagedStatic to manage the pass registrar.
195// Unfortunately, we can't do this, because passes are registered with static
196// ctors, and having llvm_shutdown clear this map prevents successful
197// ressurection after llvm_shutdown is run.
198static PassRegistrar *getPassRegistrar() {
199 static PassRegistrar *PassRegistrarObj = 0;
200 if (!PassRegistrarObj)
201 PassRegistrarObj = new PassRegistrar();
202 return PassRegistrarObj;
203}
204
Chris Lattner37d3c952002-07-23 18:08:00 +0000205// getPassInfo - Return the PassInfo data structure that corresponds to this
206// pass...
207const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000208 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000209 return lookupPassInfo(typeid(*this));
210}
211
212const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000213 return getPassRegistrar()->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000214}
215
Chris Lattnerc66da832006-01-23 01:01:04 +0000216void RegisterPassBase::registerPass() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000217 getPassRegistrar()->RegisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000218
Chris Lattner1b368a02006-12-01 23:27:45 +0000219 // Notify any listeners.
Chris Lattner37d3c952002-07-23 18:08:00 +0000220 if (Listeners)
221 for (std::vector<PassRegistrationListener*>::iterator
222 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000223 (*I)->passRegistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000224}
225
Chris Lattnerc66da832006-01-23 01:01:04 +0000226void RegisterPassBase::unregisterPass() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000227 getPassRegistrar()->UnregisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000228}
229
Chris Lattner6e041bd2002-08-21 22:17:09 +0000230//===----------------------------------------------------------------------===//
231// Analysis Group Implementation Code
232//===----------------------------------------------------------------------===//
233
Chris Lattner6e041bd2002-08-21 22:17:09 +0000234// RegisterAGBase implementation
235//
236RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
237 const std::type_info *Pass, bool isDefault)
Chris Lattnerfbb46502006-08-27 22:21:55 +0000238 : RegisterPassBase(Interface),
Chris Lattnerc66da832006-01-23 01:01:04 +0000239 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000240
Chris Lattner6e041bd2002-08-21 22:17:09 +0000241 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
Chris Lattnerc66da832006-01-23 01:01:04 +0000242 if (InterfaceInfo == 0) {
243 // First reference to Interface, register it now.
244 registerPass();
245 InterfaceInfo = &PIObj;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000246 }
Chris Lattnerfbb46502006-08-27 22:21:55 +0000247 assert(PIObj.isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000248 "Trying to join an analysis group that is a normal pass!");
249
250 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000251 ImplementationInfo = Pass::lookupPassInfo(*Pass);
252 assert(ImplementationInfo &&
253 "Must register pass before adding to AnalysisGroup!");
254
Chris Lattnerb3708e22002-08-30 20:23:45 +0000255 // Make sure we keep track of the fact that the implementation implements
256 // the interface.
257 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
258 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000259
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000260 getPassRegistrar()->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000261 }
262}
263
264void RegisterAGBase::setGroupName(const char *Name) {
265 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
266 InterfaceInfo->setPassName(Name);
267}
268
Chris Lattner6e041bd2002-08-21 22:17:09 +0000269
Chris Lattner37d3c952002-07-23 18:08:00 +0000270//===----------------------------------------------------------------------===//
271// PassRegistrationListener implementation
272//
273
274// PassRegistrationListener ctor - Add the current object to the list of
275// PassRegistrationListeners...
276PassRegistrationListener::PassRegistrationListener() {
277 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
278 Listeners->push_back(this);
279}
280
281// dtor - Remove object from list of listeners...
282PassRegistrationListener::~PassRegistrationListener() {
283 std::vector<PassRegistrationListener*>::iterator I =
284 std::find(Listeners->begin(), Listeners->end(), this);
285 assert(Listeners && I != Listeners->end() &&
286 "PassRegistrationListener not registered!");
287 Listeners->erase(I);
288
289 if (Listeners->empty()) {
290 delete Listeners;
291 Listeners = 0;
292 }
293}
294
295// enumeratePasses - Iterate over the registered passes, calling the
296// passEnumerate callback on each PassInfo object.
297//
298void PassRegistrationListener::enumeratePasses() {
Chris Lattnera5ebb6a2007-04-21 00:12:18 +0000299 getPassRegistrar()->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000300}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000301
Chris Lattner23d54052006-12-01 22:21:11 +0000302//===----------------------------------------------------------------------===//
303// AnalysisUsage Class Implementation
304//
305
Chris Lattner1b368a02006-12-01 23:27:45 +0000306namespace {
307 struct GetCFGOnlyPasses : public PassRegistrationListener {
308 std::vector<AnalysisID> &CFGOnlyList;
309 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
310
311 void passEnumerate(const PassInfo *P) {
312 if (P->isCFGOnlyPass())
313 CFGOnlyList.push_back(P);
314 }
315 };
316}
317
Chris Lattner23d54052006-12-01 22:21:11 +0000318// setPreservesCFG - This function should be called to by the pass, iff they do
319// not:
320//
321// 1. Add or remove basic blocks from the function
322// 2. Modify terminator instructions in any way.
323//
324// This function annotates the AnalysisUsage info object to say that analyses
325// that only depend on the CFG are preserved by this pass.
326//
327void AnalysisUsage::setPreservesCFG() {
328 // Since this transformation doesn't modify the CFG, it preserves all analyses
329 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000330 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000331}
332
333