blob: 0f1446fabadaf39f3222e85e150458405599c436 [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"
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000017#include "llvm/IR/Function.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000018#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000019#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/PassRegistry.h"
David Greenedfe4ad72010-01-05 01:30:04 +000021#include "llvm/Support/Debug.h"
Chris Lattner13626022009-08-23 06:03:38 +000022#include "llvm/Support/raw_ostream.h"
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.
Andrew Trick808a7a62012-02-03 05:12:30 +000030Pass::~Pass() {
31 delete Resolver;
Devang Patel2c1bba02007-04-26 21:33:42 +000032}
33
34// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000035ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000036
David Greene9b063df2010-04-02 23:17:14 +000037Pass *ModulePass::createPrinterPass(raw_ostream &O,
38 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +000039 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +000040}
41
Dan Gohman1cfbfc82009-12-14 19:43:09 +000042PassManagerType ModulePass::getPotentialPassManagerType() const {
43 return PMT_ModulePassManager;
44}
45
Owen Andersona7aed182010-08-06 18:33:48 +000046bool Pass::mustPreserveAnalysisID(char &AID) const {
Craig Topperc6207612014-04-09 06:08:46 +000047 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
Chris Lattner9ad77572003-03-21 21:41:02 +000048}
49
Joe Abbey96e89f62011-11-21 04:42:21 +000050// dumpPassStructure - Implement the -debug-pass=Structure option
Dan Gohmanf71c5212010-08-19 01:29:07 +000051void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000052 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000053}
Chris Lattner37104aa2002-04-29 14:57:45 +000054
Dan Gohman94f57c52008-03-14 18:27:04 +000055/// getPassName - Return a nice clean name for a pass. This usually
56/// implemented in terms of the name that is registered by one of the
57/// Registration templates, but can be overloaded directly.
58///
Chris Lattner071577d2002-07-29 21:02:31 +000059const char *Pass::getPassName() const {
Owen Andersona7aed182010-08-06 18:33:48 +000060 AnalysisID AID = getPassID();
61 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
62 if (PI)
Chris Lattner071577d2002-07-29 21:02:31 +000063 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000064 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000065}
Chris Lattner37104aa2002-04-29 14:57:45 +000066
Dan Gohman1cfbfc82009-12-14 19:43:09 +000067void Pass::preparePassManager(PMStack &) {
68 // By default, don't do anything.
69}
70
71PassManagerType Pass::getPotentialPassManagerType() const {
72 // Default implementation.
Andrew Trick808a7a62012-02-03 05:12:30 +000073 return PMT_Unknown;
Dan Gohman1cfbfc82009-12-14 19:43:09 +000074}
75
76void Pass::getAnalysisUsage(AnalysisUsage &) const {
77 // By default, no analysis results are used, all are invalidated.
78}
79
80void Pass::releaseMemory() {
81 // By default, don't do anything.
82}
83
84void Pass::verifyAnalysis() const {
85 // By default, don't do anything.
86}
87
Owen Andersona7aed182010-08-06 18:33:48 +000088void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmanffdee302010-06-21 18:46:45 +000089 return this;
90}
91
92ImmutablePass *Pass::getAsImmutablePass() {
Craig Topperc6207612014-04-09 06:08:46 +000093 return nullptr;
Dan Gohmanffdee302010-06-21 18:46:45 +000094}
95
96PMDataManager *Pass::getAsPMDataManager() {
Craig Topperc6207612014-04-09 06:08:46 +000097 return nullptr;
Dan Gohmanffdee302010-06-21 18:46:45 +000098}
99
100void Pass::setResolver(AnalysisResolver *AR) {
101 assert(!Resolver && "Resolver is already set");
102 Resolver = AR;
103}
104
Misha Brukman56a86422003-10-14 21:38:42 +0000105// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000106// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000107// implement this method.
108//
Chris Lattner13626022009-08-23 06:03:38 +0000109void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000110 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
111}
112
Bill Wendling22e978a2006-12-07 20:04:42 +0000113// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +0000114void Pass::dump() const {
Craig Topperc6207612014-04-09 06:08:46 +0000115 print(dbgs(), nullptr);
Chris Lattner26750072002-07-27 01:12:17 +0000116}
117
Chris Lattnercdd09c22002-01-31 00:45:31 +0000118//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000119// ImmutablePass Implementation
120//
Devang Patelf5a994e2006-12-22 22:49:00 +0000121// Force out-of-line virtual method.
122ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000123
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000124void ImmutablePass::initializePass() {
125 // By default, don't do anything.
126}
127
Chris Lattneree0788d2002-09-25 21:59:11 +0000128//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000129// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000130//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000131
David Greene9b063df2010-04-02 23:17:14 +0000132Pass *FunctionPass::createPrinterPass(raw_ostream &O,
133 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000134 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000135}
136
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000137PassManagerType FunctionPass::getPotentialPassManagerType() const {
138 return PMT_FunctionPassManager;
139}
140
Paul Robinson0c12b1d22014-02-26 01:23:26 +0000141bool FunctionPass::skipOptnoneFunction(const Function &F) const {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000142 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
143 DEBUG(dbgs() << "Skipping pass '" << getPassName()
144 << "' on function " << F.getName() << "\n");
145 return true;
146 }
147 return false;
148}
149
Chris Lattnercdd09c22002-01-31 00:45:31 +0000150//===----------------------------------------------------------------------===//
151// BasicBlockPass Implementation
152//
153
David Greene9b063df2010-04-02 23:17:14 +0000154Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
155 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000156 return createPrintBasicBlockPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000157}
158
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000159bool BasicBlockPass::doInitialization(Function &) {
160 // By default, don't do anything.
161 return false;
162}
163
164bool BasicBlockPass::doFinalization(Function &) {
165 // By default, don't do anything.
166 return false;
167}
168
Paul Robinson0c12b1d22014-02-26 01:23:26 +0000169bool BasicBlockPass::skipOptnoneFunction(const BasicBlock &BB) const {
170 const Function *F = BB.getParent();
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000171 if (F && F->hasFnAttribute(Attribute::OptimizeNone)) {
172 // Report this only once per function.
173 if (&BB == &F->getEntryBlock())
174 DEBUG(dbgs() << "Skipping pass '" << getPassName()
175 << "' on function " << F->getName() << "\n");
176 return true;
177 }
178 return false;
179}
180
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000181PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000182 return PMT_BasicBlockPassManager;
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000183}
184
Owen Andersona7aed182010-08-06 18:33:48 +0000185const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000186 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000187}
188
Owen Anderson81781222010-07-20 08:26:15 +0000189const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000190 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000191}
192
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000193Pass *Pass::createPass(AnalysisID ID) {
194 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trick3a61b782012-02-08 21:22:34 +0000195 if (!PI)
Craig Topperc6207612014-04-09 06:08:46 +0000196 return nullptr;
Andrew Trick3a61b782012-02-08 21:22:34 +0000197 return PI->createPass();
198}
199
Owen Anderson81781222010-07-20 08:26:15 +0000200Pass *PassInfo::createPass() const {
Dan Gohmanffdee302010-06-21 18:46:45 +0000201 assert((!isAnalysisGroup() || NormalCtor) &&
202 "No default implementation found for analysis group!");
203 assert(NormalCtor &&
204 "Cannot call createPass on PassInfo without default ctor!");
205 return NormalCtor();
206}
207
Chris Lattner6e041bd2002-08-21 22:17:09 +0000208//===----------------------------------------------------------------------===//
209// Analysis Group Implementation Code
210//===----------------------------------------------------------------------===//
211
Chris Lattner6e041bd2002-08-21 22:17:09 +0000212// RegisterAGBase implementation
213//
Owen Andersona7aed182010-08-06 18:33:48 +0000214RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
215 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000216 : PassInfo(Name, InterfaceID) {
217 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
218 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000219}
220
Chris Lattner37d3c952002-07-23 18:08:00 +0000221//===----------------------------------------------------------------------===//
222// PassRegistrationListener implementation
223//
224
225// PassRegistrationListener ctor - Add the current object to the list of
226// PassRegistrationListeners...
227PassRegistrationListener::PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000228 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000229}
230
231// dtor - Remove object from list of listeners...
232PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000233 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000234}
235
236// enumeratePasses - Iterate over the registered passes, calling the
237// passEnumerate callback on each PassInfo object.
238//
239void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000240 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000241}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000242
Chris Lattner5264fce2010-01-22 06:29:25 +0000243PassNameParser::~PassNameParser() {}
244
Chris Lattner23d54052006-12-01 22:21:11 +0000245//===----------------------------------------------------------------------===//
246// AnalysisUsage Class Implementation
247//
248
Chris Lattner1b368a02006-12-01 23:27:45 +0000249namespace {
250 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000251 typedef AnalysisUsage::VectorType VectorType;
252 VectorType &CFGOnlyList;
253 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick808a7a62012-02-03 05:12:30 +0000254
Craig Topperf398d7c2014-03-05 06:35:38 +0000255 void passEnumerate(const PassInfo *P) override {
Chris Lattner1b368a02006-12-01 23:27:45 +0000256 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000257 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000258 }
259 };
260}
261
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000262// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner23d54052006-12-01 22:21:11 +0000263// not:
264//
265// 1. Add or remove basic blocks from the function
266// 2. Modify terminator instructions in any way.
267//
268// This function annotates the AnalysisUsage info object to say that analyses
269// that only depend on the CFG are preserved by this pass.
270//
271void AnalysisUsage::setPreservesCFG() {
272 // Since this transformation doesn't modify the CFG, it preserves all analyses
273 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000274 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000275}
276
Owen Andersona7aed182010-08-06 18:33:48 +0000277AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
278 const PassInfo *PI = Pass::lookupPassInfo(Arg);
279 // If the pass exists, preserve it. Otherwise silently do nothing.
280 if (PI) Preserved.push_back(PI->getTypeInfo());
281 return *this;
282}
283
284AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000285 Required.push_back(ID);
286 return *this;
287}
Chris Lattner23d54052006-12-01 22:21:11 +0000288
Owen Andersona7aed182010-08-06 18:33:48 +0000289AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
290 Required.push_back(&ID);
291 return *this;
292}
293
294AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
295 Required.push_back(&ID);
296 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000297 return *this;
298}