blob: b593d475ca0efe3119cb0edf5b5779d1f57a4de4 [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
192static ManagedStatic<PassRegistrar> PassRegistrarObj;
Chris Lattner37d3c952002-07-23 18:08:00 +0000193static std::vector<PassRegistrationListener*> *Listeners = 0;
194
195// getPassInfo - Return the PassInfo data structure that corresponds to this
196// pass...
197const PassInfo *Pass::getPassInfo() const {
Chris Lattner071577d2002-07-29 21:02:31 +0000198 if (PassInfoCache) return PassInfoCache;
Chris Lattner4b169632002-08-21 17:08:37 +0000199 return lookupPassInfo(typeid(*this));
200}
201
202const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000203 return PassRegistrarObj->GetPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000204}
205
Chris Lattnerc66da832006-01-23 01:01:04 +0000206void RegisterPassBase::registerPass() {
Chris Lattner1b368a02006-12-01 23:27:45 +0000207 PassRegistrarObj->RegisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000208
Chris Lattner1b368a02006-12-01 23:27:45 +0000209 // Notify any listeners.
Chris Lattner37d3c952002-07-23 18:08:00 +0000210 if (Listeners)
211 for (std::vector<PassRegistrationListener*>::iterator
212 I = Listeners->begin(), E = Listeners->end(); I != E; ++I)
Chris Lattnerc66da832006-01-23 01:01:04 +0000213 (*I)->passRegistered(&PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000214}
215
Chris Lattnerc66da832006-01-23 01:01:04 +0000216void RegisterPassBase::unregisterPass() {
Chris Lattner1b368a02006-12-01 23:27:45 +0000217 PassRegistrarObj->UnregisterPass(PIObj);
Chris Lattner37d3c952002-07-23 18:08:00 +0000218}
219
Chris Lattner6e041bd2002-08-21 22:17:09 +0000220//===----------------------------------------------------------------------===//
221// Analysis Group Implementation Code
222//===----------------------------------------------------------------------===//
223
Chris Lattner6e041bd2002-08-21 22:17:09 +0000224// RegisterAGBase implementation
225//
226RegisterAGBase::RegisterAGBase(const std::type_info &Interface,
227 const std::type_info *Pass, bool isDefault)
Chris Lattnerfbb46502006-08-27 22:21:55 +0000228 : RegisterPassBase(Interface),
Chris Lattnerc66da832006-01-23 01:01:04 +0000229 ImplementationInfo(0), isDefaultImplementation(isDefault) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000230
Chris Lattner6e041bd2002-08-21 22:17:09 +0000231 InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface));
Chris Lattnerc66da832006-01-23 01:01:04 +0000232 if (InterfaceInfo == 0) {
233 // First reference to Interface, register it now.
234 registerPass();
235 InterfaceInfo = &PIObj;
Chris Lattner6e041bd2002-08-21 22:17:09 +0000236 }
Chris Lattnerfbb46502006-08-27 22:21:55 +0000237 assert(PIObj.isAnalysisGroup() &&
Chris Lattner6e041bd2002-08-21 22:17:09 +0000238 "Trying to join an analysis group that is a normal pass!");
239
240 if (Pass) {
Chris Lattner6e041bd2002-08-21 22:17:09 +0000241 ImplementationInfo = Pass::lookupPassInfo(*Pass);
242 assert(ImplementationInfo &&
243 "Must register pass before adding to AnalysisGroup!");
244
Chris Lattnerb3708e22002-08-30 20:23:45 +0000245 // Make sure we keep track of the fact that the implementation implements
246 // the interface.
247 PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo);
248 IIPI->addInterfaceImplemented(InterfaceInfo);
Chris Lattner4d9fc5e2006-12-01 23:46:50 +0000249
250 PassRegistrarObj->RegisterAnalysisGroup(InterfaceInfo, IIPI, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000251 }
252}
253
254void RegisterAGBase::setGroupName(const char *Name) {
255 assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!");
256 InterfaceInfo->setPassName(Name);
257}
258
Chris Lattner6e041bd2002-08-21 22:17:09 +0000259
Chris Lattner37d3c952002-07-23 18:08:00 +0000260//===----------------------------------------------------------------------===//
261// PassRegistrationListener implementation
262//
263
264// PassRegistrationListener ctor - Add the current object to the list of
265// PassRegistrationListeners...
266PassRegistrationListener::PassRegistrationListener() {
267 if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>();
268 Listeners->push_back(this);
269}
270
271// dtor - Remove object from list of listeners...
272PassRegistrationListener::~PassRegistrationListener() {
273 std::vector<PassRegistrationListener*>::iterator I =
274 std::find(Listeners->begin(), Listeners->end(), this);
275 assert(Listeners && I != Listeners->end() &&
276 "PassRegistrationListener not registered!");
277 Listeners->erase(I);
278
279 if (Listeners->empty()) {
280 delete Listeners;
281 Listeners = 0;
282 }
283}
284
285// enumeratePasses - Iterate over the registered passes, calling the
286// passEnumerate callback on each PassInfo object.
287//
288void PassRegistrationListener::enumeratePasses() {
Chris Lattner1b368a02006-12-01 23:27:45 +0000289 PassRegistrarObj->EnumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000290}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000291
Chris Lattner23d54052006-12-01 22:21:11 +0000292//===----------------------------------------------------------------------===//
293// AnalysisUsage Class Implementation
294//
295
Chris Lattner1b368a02006-12-01 23:27:45 +0000296namespace {
297 struct GetCFGOnlyPasses : public PassRegistrationListener {
298 std::vector<AnalysisID> &CFGOnlyList;
299 GetCFGOnlyPasses(std::vector<AnalysisID> &L) : CFGOnlyList(L) {}
300
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