blob: 2e27015c3ac37aaf40566537e4868a3d5f2bb0c8 [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"
Chris Lattner6e041bd2002-08-21 22:17:09 +000022#include <set>
Chris Lattner189d19f2003-11-21 20:23:48 +000023using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000024
Chris Lattner7e0dbe62002-05-06 19:31:52 +000025//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000026// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000027//
Chris Lattnercdd09c22002-01-31 00:45:31 +000028
Devang Patelf5a994e2006-12-22 22:49:00 +000029// Force out-of-line virtual method.
30ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000031
Chris Lattner9ad77572003-03-21 21:41:02 +000032bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const {
Devang Patelb66334b2007-01-05 22:47:07 +000033 return Resolver->getAnalysisToUpdate(AnalysisID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000034}
35
Chris Lattner198cf422002-07-30 16:27:02 +000036// dumpPassStructure - Implement the -debug-passes=Structure option
37void Pass::dumpPassStructure(unsigned Offset) {
Bill Wendling22e978a2006-12-07 20:04:42 +000038 cerr << std::string(Offset*2, ' ') << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000039}
Chris Lattner37104aa2002-04-29 14:57:45 +000040
Brian Gaeke465a5cc2004-02-28 21:55:18 +000041// getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass.
Chris Lattner37104aa2002-04-29 14:57:45 +000042//
Chris Lattner071577d2002-07-29 21:02:31 +000043const char *Pass::getPassName() const {
44 if (const PassInfo *PI = getPassInfo())
45 return PI->getPassName();
46 return typeid(*this).name();
47}
Chris Lattner37104aa2002-04-29 14:57:45 +000048
Misha Brukman56a86422003-10-14 21:38:42 +000049// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +000050// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +000051// implement this method.
52//
Reid Spencer90839362004-12-07 04:03:45 +000053void Pass::print(std::ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +000054 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
55}
56
Bill Wendling22e978a2006-12-07 20:04:42 +000057// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +000058void Pass::dump() const {
Bill Wendling22e978a2006-12-07 20:04:42 +000059 print(*cerr.stream(), 0);
Chris Lattner26750072002-07-27 01:12:17 +000060}
61
Chris Lattnercdd09c22002-01-31 00:45:31 +000062//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +000063// ImmutablePass Implementation
64//
Devang Patelf5a994e2006-12-22 22:49:00 +000065// Force out-of-line virtual method.
66ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +000067
68//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +000069// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +000070//
Chris Lattnercdd09c22002-01-31 00:45:31 +000071
Chris Lattnerc8e66542002-04-27 06:56:12 +000072// run - On a module, we run this pass by initializing, runOnFunction'ing once
73// for every function in the module, then by finalizing.
Chris Lattnercdd09c22002-01-31 00:45:31 +000074//
Chris Lattner79e523d2004-09-20 04:47:19 +000075bool FunctionPass::runOnModule(Module &M) {
Chris Lattnercdd09c22002-01-31 00:45:31 +000076 bool Changed = doInitialization(M);
Misha Brukmanb1c93172005-04-21 23:48:37 +000077
Chris Lattner113f4f42002-06-25 16:13:24 +000078 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000079 if (!I->isDeclaration()) // Passes are not run on external functions!
Chris Lattnerc8e66542002-04-27 06:56:12 +000080 Changed |= runOnFunction(*I);
Misha Brukmanb1c93172005-04-21 23:48:37 +000081
Chris Lattnercdd09c22002-01-31 00:45:31 +000082 return Changed | doFinalization(M);
Chris Lattner26e4f892002-01-21 07:37:31 +000083}
84
Chris Lattnerc8e66542002-04-27 06:56:12 +000085// run - On a function, we simply initialize, run the function, then finalize.
Chris Lattnercdd09c22002-01-31 00:45:31 +000086//
Chris Lattner113f4f42002-06-25 16:13:24 +000087bool FunctionPass::run(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000088 if (F.isDeclaration()) return false;// Passes are not run on external functions!
Chris Lattnercdd09c22002-01-31 00:45:31 +000089
Chris Lattner79e523d2004-09-20 04:47:19 +000090 bool Changed = doInitialization(*F.getParent());
91 Changed |= runOnFunction(F);
92 return Changed | doFinalization(*F.getParent());
Chris Lattner26e4f892002-01-21 07:37:31 +000093}
Chris Lattnerd013ba92002-01-23 05:49:41 +000094
Chris Lattnercdd09c22002-01-31 00:45:31 +000095//===----------------------------------------------------------------------===//
96// BasicBlockPass Implementation
97//
98
Chris Lattnerc8e66542002-04-27 06:56:12 +000099// To run this pass on a function, we simply call runOnBasicBlock once for each
100// function.
Chris Lattnercdd09c22002-01-31 00:45:31 +0000101//
Chris Lattner113f4f42002-06-25 16:13:24 +0000102bool BasicBlockPass::runOnFunction(Function &F) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000103 bool Changed = doInitialization(F);
Chris Lattner113f4f42002-06-25 16:13:24 +0000104 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Chris Lattnercdd09c22002-01-31 00:45:31 +0000105 Changed |= runOnBasicBlock(*I);
Chris Lattnerbae3c672002-09-12 17:06:40 +0000106 return Changed | doFinalization(F);
Chris Lattnercdd09c22002-01-31 00:45:31 +0000107}
108
109// To run directly on the basic block, we initialize, runOnBasicBlock, then
110// finalize.
111//
Chris Lattner79e523d2004-09-20 04:47:19 +0000112bool BasicBlockPass::runPass(BasicBlock &BB) {
Chris Lattnerbae3c672002-09-12 17:06:40 +0000113 Function &F = *BB.getParent();
114 Module &M = *F.getParent();
Chris Lattner79e523d2004-09-20 04:47:19 +0000115 bool Changed = doInitialization(M);
116 Changed |= doInitialization(F);
117 Changed |= runOnBasicBlock(BB);
118 Changed |= doFinalization(F);
119 Changed |= doFinalization(M);
120 return Changed;
Chris Lattnercdd09c22002-01-31 00:45:31 +0000121}
122
Chris Lattner37d3c952002-07-23 18:08:00 +0000123//===----------------------------------------------------------------------===//
124// Pass Registration mechanism
125//
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000126namespace {
Chris Lattner1b368a02006-12-01 23:27:45 +0000127class PassRegistrar {
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000128 /// PassInfoMap - Keep track of the passinfo object for each registered llvm
129 /// pass.
Chris Lattner1b368a02006-12-01 23:27:45 +0000130 std::map<TypeInfo, PassInfo*> PassInfoMap;
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000131
132 /// AnalysisGroupInfo - Keep track of information for each analysis group.
133 struct AnalysisGroupInfo {
134 const PassInfo *DefaultImpl;
135 std::set<const PassInfo *> Implementations;
136 AnalysisGroupInfo() : DefaultImpl(0) {}
137 };
138
139 /// AnalysisGroupInfoMap - Information for each analysis group.
140 std::map<const PassInfo *, AnalysisGroupInfo> AnalysisGroupInfoMap;
141
Chris Lattner1b368a02006-12-01 23:27:45 +0000142public:
143
144 const PassInfo *GetPassInfo(const std::type_info &TI) const {
145 std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.find(TI);
146 return I != PassInfoMap.end() ? I->second : 0;
147 }
148
149 void RegisterPass(PassInfo &PI) {
150 bool Inserted =
151 PassInfoMap.insert(std::make_pair(TypeInfo(PI.getTypeInfo()),&PI)).second;
152 assert(Inserted && "Pass registered multiple times!");
153 }
154
155 void UnregisterPass(PassInfo &PI) {
156 std::map<TypeInfo, PassInfo*>::iterator I =
157 PassInfoMap.find(PI.getTypeInfo());
158 assert(I != PassInfoMap.end() && "Pass registered but not in map!");
159
160 // Remove pass from the map.
161 PassInfoMap.erase(I);
162 }
163
164 void EnumerateWith(PassRegistrationListener *L) {
165 for (std::map<TypeInfo, PassInfo*>::const_iterator I = PassInfoMap.begin(),
166 E = PassInfoMap.end(); I != E; ++I)
167 L->passEnumerate(I->second);
168 }
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000169
170
171 /// Analysis Group Mechanisms.
172 void RegisterAnalysisGroup(PassInfo *InterfaceInfo,
173 const PassInfo *ImplementationInfo,
174 bool isDefault) {
175 AnalysisGroupInfo &AGI = AnalysisGroupInfoMap[InterfaceInfo];
176 assert(AGI.Implementations.count(ImplementationInfo) == 0 &&
177 "Cannot add a pass to the same analysis group more than once!");
178 AGI.Implementations.insert(ImplementationInfo);
179 if (isDefault) {
180 assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 &&
181 "Default implementation for analysis group already specified!");
182 assert(ImplementationInfo->getNormalCtor() &&
183 "Cannot specify pass as default if it does not have a default ctor");
184 AGI.DefaultImpl = ImplementationInfo;
185 InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor());
186 }
187 }
Chris Lattner1b368a02006-12-01 23:27:45 +0000188};
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000189}
Chris Lattner1b368a02006-12-01 23:27:45 +0000190
191static ManagedStatic<PassRegistrar> PassRegistrarObj;
Chris Lattner37d3c952002-07-23 18:08:00 +0000192static std::vector<PassRegistrationListener*> *Listeners = 0;
193
194// getPassInfo - Return the PassInfo data structure that corresponds to this
195// pass...
196const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000197 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000198 return lookupPassInfo(typeid(*this));
199}
200
201const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000202 return PassRegistrarObj->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000203}
204
Chris Lattnerc66da832006-01-23 01:01:04 +0000205void RegisterPassBase::registerPass() {
Chris Lattner1b368a02006-12-01 23:27:45 +0000206 PassRegistrarObj->RegisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000207
Chris Lattner1b368a02006-12-01 23:27:45 +0000208 // Notify any listeners.
Chris Lattner37d3c952002-07-23 18:08:00 +0000209 if (Listeners)
210 for (std::vector<PassRegistrationListener*>::iterator
211 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000212 (*I)->passRegistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000213}
214
Chris Lattnerc66da832006-01-23 01:01:04 +0000215void RegisterPassBase::unregisterPass() {
Chris Lattner1b368a02006-12-01 23:27:45 +0000216 PassRegistrarObj->UnregisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000217}
218
Chris Lattner6e041bd2002-08-21 22:17:09 +0000219//===----------------------------------------------------------------------===//
220// Analysis Group Implementation Code
221//===----------------------------------------------------------------------===//
222
Chris Lattner6e041bd2002-08-21 22:17:09 +0000223// RegisterAGBase implementation
224//
225RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
226 const std::type_info *Pass, bool isDefault)
Chris Lattnerfbb46502006-08-27 22:21:55 +0000227 : RegisterPassBase(Interface),
Chris Lattnerc66da832006-01-23 01:01:04 +0000228 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000229
Chris Lattner6e041bd2002-08-21 22:17:09 +0000230 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
Chris Lattnerc66da832006-01-23 01:01:04 +0000231 if (InterfaceInfo == 0) {
232 // First reference to Interface, register it now.
233 registerPass();
234 InterfaceInfo = &PIObj;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000235 }
Chris Lattnerfbb46502006-08-27 22:21:55 +0000236 assert(PIObj.isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000237 "Trying to join an analysis group that is a normal pass!");
238
239 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000240 ImplementationInfo = Pass::lookupPassInfo(*Pass);
241 assert(ImplementationInfo &&
242 "Must register pass before adding to AnalysisGroup!");
243
Chris Lattnerb3708e22002-08-30 20:23:45 +0000244 // Make sure we keep track of the fact that the implementation implements
245 // the interface.
246 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
247 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000248
249 PassRegistrarObj->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000250 }
251}
252
253void RegisterAGBase::setGroupName(const char *Name) {
254 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
255 InterfaceInfo->setPassName(Name);
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 Lattner1b368a02006-12-01 23:27:45 +0000288 PassRegistrarObj->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 {
297 std::vector<AnalysisID> &CFGOnlyList;
298 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
299
300 void passEnumerate(const PassInfo *P) {
301 if (P->isCFGOnlyPass())
302 CFGOnlyList.push_back(P);
303 }
304 };
305}
306
Chris Lattner23d54052006-12-01 22:21:11 +0000307// setPreservesCFG - This function should be called to by the pass, iff they do
308// not:
309//
310// 1. Add or remove basic blocks from the function
311// 2. Modify terminator instructions in any way.
312//
313// This function annotates the AnalysisUsage info object to say that analyses
314// that only depend on the CFG are preserved by this pass.
315//
316void AnalysisUsage::setPreservesCFG() {
317 // Since this transformation doesn't modify the CFG, it preserves all analyses
318 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000319 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000320}
321
322