blob: a7d7f61dd76225104acad4ae6c57b5e671059830 [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"
Owen Anderson1e8ae642010-07-20 18:39:06 +000017#include "llvm/PassRegistry.h"
David Greene9b063df2010-04-02 23:17:14 +000018#include "llvm/Assembly/PrintModulePass.h"
David Greenedfe4ad72010-01-05 01:30:04 +000019#include "llvm/Support/Debug.h"
Chris Lattner5264fce2010-01-22 06:29:25 +000020#include "llvm/Support/PassNameParser.h"
Chris Lattner13626022009-08-23 06:03:38 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Chris Lattner7e0dbe62002-05-06 19:31:52 +000024//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000025// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000026//
Chris Lattnercdd09c22002-01-31 00:45:31 +000027
Owen Andersona7aed182010-08-06 18:33:48 +000028Pass::Pass(PassKind K, char &pid) : Resolver(0), PassID(&pid), Kind(K) { }
Dan Gohmanffdee302010-06-21 18:46:45 +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
David Greene9b063df2010-04-02 23:17:14 +000038Pass *ModulePass::createPrinterPass(raw_ostream &O,
39 const std::string &Banner) const {
40 return createPrintModulePass(&O, false, Banner);
41}
42
Dan Gohman1cfbfc82009-12-14 19:43:09 +000043PassManagerType ModulePass::getPotentialPassManagerType() const {
44 return PMT_ModulePassManager;
45}
46
Owen Andersona7aed182010-08-06 18:33:48 +000047bool Pass::mustPreserveAnalysisID(char &AID) const {
48 return Resolver->getAnalysisIfAvailable(&AID, true) != 0;
Chris Lattner9ad77572003-03-21 21:41:02 +000049}
50
Dan Gohmanf71c5212010-08-19 01:29:07 +000051// dumpPassStructure - Implement the -debug-passes=Structure option
52void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000053 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000054}
Chris Lattner37104aa2002-04-29 14:57:45 +000055
Dan Gohman94f57c52008-03-14 18:27:04 +000056/// getPassName - Return a nice clean name for a pass. This usually
57/// implemented in terms of the name that is registered by one of the
58/// Registration templates, but can be overloaded directly.
59///
Chris Lattner071577d2002-07-29 21:02:31 +000060const char *Pass::getPassName() const {
Owen Andersona7aed182010-08-06 18:33:48 +000061 AnalysisID AID = getPassID();
62 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
63 if (PI)
Chris Lattner071577d2002-07-29 21:02:31 +000064 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000065 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000066}
Chris Lattner37104aa2002-04-29 14:57:45 +000067
Dan Gohman1cfbfc82009-12-14 19:43:09 +000068void Pass::preparePassManager(PMStack &) {
69 // By default, don't do anything.
70}
71
72PassManagerType Pass::getPotentialPassManagerType() const {
73 // Default implementation.
74 return PMT_Unknown;
75}
76
77void Pass::getAnalysisUsage(AnalysisUsage &) const {
78 // By default, no analysis results are used, all are invalidated.
79}
80
81void Pass::releaseMemory() {
82 // By default, don't do anything.
83}
84
85void Pass::verifyAnalysis() const {
86 // By default, don't do anything.
87}
88
Owen Andersona7aed182010-08-06 18:33:48 +000089void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmanffdee302010-06-21 18:46:45 +000090 return this;
91}
92
93ImmutablePass *Pass::getAsImmutablePass() {
94 return 0;
95}
96
97PMDataManager *Pass::getAsPMDataManager() {
98 return 0;
99}
100
101void Pass::setResolver(AnalysisResolver *AR) {
102 assert(!Resolver && "Resolver is already set");
103 Resolver = AR;
104}
105
Misha Brukman56a86422003-10-14 21:38:42 +0000106// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000107// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000108// implement this method.
109//
Chris Lattner13626022009-08-23 06:03:38 +0000110void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000111 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
112}
113
Bill Wendling22e978a2006-12-07 20:04:42 +0000114// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +0000115void Pass::dump() const {
David Greenedfe4ad72010-01-05 01:30:04 +0000116 print(dbgs(), 0);
Chris Lattner26750072002-07-27 01:12:17 +0000117}
118
Chris Lattnercdd09c22002-01-31 00:45:31 +0000119//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000120// ImmutablePass Implementation
121//
Devang Patelf5a994e2006-12-22 22:49:00 +0000122// Force out-of-line virtual method.
123ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000124
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000125void ImmutablePass::initializePass() {
126 // By default, don't do anything.
127}
128
Chris Lattneree0788d2002-09-25 21:59:11 +0000129//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000130// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000131//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000132
David Greene9b063df2010-04-02 23:17:14 +0000133Pass *FunctionPass::createPrinterPass(raw_ostream &O,
134 const std::string &Banner) const {
135 return createPrintFunctionPass(Banner, &O);
136}
137
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000138bool FunctionPass::doInitialization(Module &) {
139 // By default, don't do anything.
140 return false;
141}
142
143bool FunctionPass::doFinalization(Module &) {
144 // By default, don't do anything.
145 return false;
146}
147
148PassManagerType FunctionPass::getPotentialPassManagerType() const {
149 return PMT_FunctionPassManager;
150}
151
Chris Lattnercdd09c22002-01-31 00:45:31 +0000152//===----------------------------------------------------------------------===//
153// BasicBlockPass Implementation
154//
155
David Greene9b063df2010-04-02 23:17:14 +0000156Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
157 const std::string &Banner) const {
158
159 llvm_unreachable("BasicBlockPass printing unsupported.");
160 return 0;
161}
162
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000163bool BasicBlockPass::doInitialization(Module &) {
164 // By default, don't do anything.
165 return false;
166}
167
168bool BasicBlockPass::doInitialization(Function &) {
169 // By default, don't do anything.
170 return false;
171}
172
173bool BasicBlockPass::doFinalization(Function &) {
174 // By default, don't do anything.
175 return false;
176}
177
178bool BasicBlockPass::doFinalization(Module &) {
179 // By default, don't do anything.
180 return false;
181}
182
183PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
184 return PMT_BasicBlockPassManager;
185}
186
Owen Andersona7aed182010-08-06 18:33:48 +0000187const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000188 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000189}
190
Owen Anderson81781222010-07-20 08:26:15 +0000191const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000192 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000193}
194
Owen Anderson81781222010-07-20 08:26:15 +0000195Pass *PassInfo::createPass() const {
Dan Gohmanffdee302010-06-21 18:46:45 +0000196 assert((!isAnalysisGroup() || NormalCtor) &&
197 "No default implementation found for analysis group!");
198 assert(NormalCtor &&
199 "Cannot call createPass on PassInfo without default ctor!");
200 return NormalCtor();
201}
202
Chris Lattner6e041bd2002-08-21 22:17:09 +0000203//===----------------------------------------------------------------------===//
204// Analysis Group Implementation Code
205//===----------------------------------------------------------------------===//
206
Chris Lattner6e041bd2002-08-21 22:17:09 +0000207// RegisterAGBase implementation
208//
Owen Andersona7aed182010-08-06 18:33:48 +0000209RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
210 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000211 : PassInfo(Name, InterfaceID) {
212 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
213 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000214}
215
Chris Lattner6e041bd2002-08-21 22:17:09 +0000216
Chris Lattner37d3c952002-07-23 18:08:00 +0000217//===----------------------------------------------------------------------===//
218// PassRegistrationListener implementation
219//
220
221// PassRegistrationListener ctor - Add the current object to the list of
222// PassRegistrationListeners...
223PassRegistrationListener::PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000224 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000225}
226
227// dtor - Remove object from list of listeners...
228PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000229 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000230}
231
232// enumeratePasses - Iterate over the registered passes, calling the
233// passEnumerate callback on each PassInfo object.
234//
235void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000236 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000237}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000238
Chris Lattner5264fce2010-01-22 06:29:25 +0000239PassNameParser::~PassNameParser() {}
240
Chris Lattner23d54052006-12-01 22:21:11 +0000241//===----------------------------------------------------------------------===//
242// AnalysisUsage Class Implementation
243//
244
Chris Lattner1b368a02006-12-01 23:27:45 +0000245namespace {
246 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000247 typedef AnalysisUsage::VectorType VectorType;
248 VectorType &CFGOnlyList;
249 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Chris Lattner1b368a02006-12-01 23:27:45 +0000250
Owen Anderson81781222010-07-20 08:26:15 +0000251 void passEnumerate(const PassInfo *P) {
Chris Lattner1b368a02006-12-01 23:27:45 +0000252 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000253 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000254 }
255 };
256}
257
Chris Lattner23d54052006-12-01 22:21:11 +0000258// setPreservesCFG - This function should be called to by the pass, iff they do
259// not:
260//
261// 1. Add or remove basic blocks from the function
262// 2. Modify terminator instructions in any way.
263//
264// This function annotates the AnalysisUsage info object to say that analyses
265// that only depend on the CFG are preserved by this pass.
266//
267void AnalysisUsage::setPreservesCFG() {
268 // Since this transformation doesn't modify the CFG, it preserves all analyses
269 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000270 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000271}
272
Owen Andersona7aed182010-08-06 18:33:48 +0000273AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
274 const PassInfo *PI = Pass::lookupPassInfo(Arg);
275 // If the pass exists, preserve it. Otherwise silently do nothing.
276 if (PI) Preserved.push_back(PI->getTypeInfo());
277 return *this;
278}
279
280AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000281 Required.push_back(ID);
282 return *this;
283}
Chris Lattner23d54052006-12-01 22:21:11 +0000284
Owen Andersona7aed182010-08-06 18:33:48 +0000285AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
286 Required.push_back(&ID);
287 return *this;
288}
289
290AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
291 Required.push_back(&ID);
292 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000293 return *this;
294}