blob: bb55d2af7cf8d5b5ac7e6ac151cfb537c3baac20 [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
Chandler Carruthe96dd892014-04-21 22:55:11 +000025#define DEBUG_TYPE "ir"
26
Chris Lattner7e0dbe62002-05-06 19:31:52 +000027//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000028// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000029//
Chris Lattnercdd09c22002-01-31 00:45:31 +000030
Devang Patelf5a994e2006-12-22 22:49:00 +000031// Force out-of-line virtual method.
Andrew Trick808a7a62012-02-03 05:12:30 +000032Pass::~Pass() {
33 delete Resolver;
Devang Patel2c1bba02007-04-26 21:33:42 +000034}
35
36// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000037ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000038
David Greene9b063df2010-04-02 23:17:14 +000039Pass *ModulePass::createPrinterPass(raw_ostream &O,
40 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +000041 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +000042}
43
Dan Gohman1cfbfc82009-12-14 19:43:09 +000044PassManagerType ModulePass::getPotentialPassManagerType() const {
45 return PMT_ModulePassManager;
46}
47
Owen Andersona7aed182010-08-06 18:33:48 +000048bool Pass::mustPreserveAnalysisID(char &AID) const {
Craig Topperc6207612014-04-09 06:08:46 +000049 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
Chris Lattner9ad77572003-03-21 21:41:02 +000050}
51
Joe Abbey96e89f62011-11-21 04:42:21 +000052// dumpPassStructure - Implement the -debug-pass=Structure option
Dan Gohmanf71c5212010-08-19 01:29:07 +000053void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000054 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000055}
Chris Lattner37104aa2002-04-29 14:57:45 +000056
Dan Gohman94f57c52008-03-14 18:27:04 +000057/// getPassName - Return a nice clean name for a pass. This usually
58/// implemented in terms of the name that is registered by one of the
59/// Registration templates, but can be overloaded directly.
60///
Chris Lattner071577d2002-07-29 21:02:31 +000061const char *Pass::getPassName() const {
Owen Andersona7aed182010-08-06 18:33:48 +000062 AnalysisID AID = getPassID();
63 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
64 if (PI)
Chris Lattner071577d2002-07-29 21:02:31 +000065 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000066 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000067}
Chris Lattner37104aa2002-04-29 14:57:45 +000068
Dan Gohman1cfbfc82009-12-14 19:43:09 +000069void Pass::preparePassManager(PMStack &) {
70 // By default, don't do anything.
71}
72
73PassManagerType Pass::getPotentialPassManagerType() const {
74 // Default implementation.
Andrew Trick808a7a62012-02-03 05:12:30 +000075 return PMT_Unknown;
Dan Gohman1cfbfc82009-12-14 19:43:09 +000076}
77
78void Pass::getAnalysisUsage(AnalysisUsage &) const {
79 // By default, no analysis results are used, all are invalidated.
80}
81
82void Pass::releaseMemory() {
83 // By default, don't do anything.
84}
85
86void Pass::verifyAnalysis() const {
87 // By default, don't do anything.
88}
89
Owen Andersona7aed182010-08-06 18:33:48 +000090void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmanffdee302010-06-21 18:46:45 +000091 return this;
92}
93
94ImmutablePass *Pass::getAsImmutablePass() {
Craig Topperc6207612014-04-09 06:08:46 +000095 return nullptr;
Dan Gohmanffdee302010-06-21 18:46:45 +000096}
97
98PMDataManager *Pass::getAsPMDataManager() {
Craig Topperc6207612014-04-09 06:08:46 +000099 return nullptr;
Dan Gohmanffdee302010-06-21 18:46:45 +0000100}
101
102void Pass::setResolver(AnalysisResolver *AR) {
103 assert(!Resolver && "Resolver is already set");
104 Resolver = AR;
105}
106
Misha Brukman56a86422003-10-14 21:38:42 +0000107// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000108// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000109// implement this method.
110//
Chris Lattner13626022009-08-23 06:03:38 +0000111void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000112 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
113}
114
Bill Wendling22e978a2006-12-07 20:04:42 +0000115// dump - call print(cerr);
Chris Lattner26750072002-07-27 01:12:17 +0000116void Pass::dump() const {
Craig Topperc6207612014-04-09 06:08:46 +0000117 print(dbgs(), nullptr);
Chris Lattner26750072002-07-27 01:12:17 +0000118}
119
Chris Lattnercdd09c22002-01-31 00:45:31 +0000120//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000121// ImmutablePass Implementation
122//
Devang Patelf5a994e2006-12-22 22:49:00 +0000123// Force out-of-line virtual method.
124ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000125
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000126void ImmutablePass::initializePass() {
127 // By default, don't do anything.
128}
129
Chris Lattneree0788d2002-09-25 21:59:11 +0000130//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000131// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000132//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000133
David Greene9b063df2010-04-02 23:17:14 +0000134Pass *FunctionPass::createPrinterPass(raw_ostream &O,
135 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000136 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000137}
138
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000139PassManagerType FunctionPass::getPotentialPassManagerType() const {
140 return PMT_FunctionPassManager;
141}
142
Paul Robinson0c12b1d22014-02-26 01:23:26 +0000143bool FunctionPass::skipOptnoneFunction(const Function &F) const {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000144 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
145 DEBUG(dbgs() << "Skipping pass '" << getPassName()
146 << "' on function " << F.getName() << "\n");
147 return true;
148 }
149 return false;
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 {
Chandler Carruth9d805132014-01-12 11:30:46 +0000158 return createPrintBasicBlockPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000159}
160
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000161bool BasicBlockPass::doInitialization(Function &) {
162 // By default, don't do anything.
163 return false;
164}
165
166bool BasicBlockPass::doFinalization(Function &) {
167 // By default, don't do anything.
168 return false;
169}
170
Paul Robinson0c12b1d22014-02-26 01:23:26 +0000171bool BasicBlockPass::skipOptnoneFunction(const BasicBlock &BB) const {
172 const Function *F = BB.getParent();
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000173 if (F && F->hasFnAttribute(Attribute::OptimizeNone)) {
174 // Report this only once per function.
175 if (&BB == &F->getEntryBlock())
176 DEBUG(dbgs() << "Skipping pass '" << getPassName()
177 << "' on function " << F->getName() << "\n");
178 return true;
179 }
180 return false;
181}
182
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000183PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000184 return PMT_BasicBlockPassManager;
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000185}
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
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000195Pass *Pass::createPass(AnalysisID ID) {
196 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trick3a61b782012-02-08 21:22:34 +0000197 if (!PI)
Craig Topperc6207612014-04-09 06:08:46 +0000198 return nullptr;
Andrew Trick3a61b782012-02-08 21:22:34 +0000199 return PI->createPass();
200}
201
Owen Anderson81781222010-07-20 08:26:15 +0000202Pass *PassInfo::createPass() const {
Dan Gohmanffdee302010-06-21 18:46:45 +0000203 assert((!isAnalysisGroup() || NormalCtor) &&
204 "No default implementation found for analysis group!");
205 assert(NormalCtor &&
206 "Cannot call createPass on PassInfo without default ctor!");
207 return NormalCtor();
208}
209
Chris Lattner6e041bd2002-08-21 22:17:09 +0000210//===----------------------------------------------------------------------===//
211// Analysis Group Implementation Code
212//===----------------------------------------------------------------------===//
213
Chris Lattner6e041bd2002-08-21 22:17:09 +0000214// RegisterAGBase implementation
215//
Owen Andersona7aed182010-08-06 18:33:48 +0000216RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
217 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000218 : PassInfo(Name, InterfaceID) {
219 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
220 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000221}
222
Chris Lattner37d3c952002-07-23 18:08:00 +0000223//===----------------------------------------------------------------------===//
224// PassRegistrationListener implementation
225//
226
227// PassRegistrationListener ctor - Add the current object to the list of
228// PassRegistrationListeners...
229PassRegistrationListener::PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000230 PassRegistry::getPassRegistry()->addRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000231}
232
233// dtor - Remove object from list of listeners...
234PassRegistrationListener::~PassRegistrationListener() {
Owen Anderson7fc9fe72010-07-20 23:41:56 +0000235 PassRegistry::getPassRegistry()->removeRegistrationListener(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000236}
237
238// enumeratePasses - Iterate over the registered passes, calling the
239// passEnumerate callback on each PassInfo object.
240//
241void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000242 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000243}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000244
Chris Lattner5264fce2010-01-22 06:29:25 +0000245PassNameParser::~PassNameParser() {}
246
Chris Lattner23d54052006-12-01 22:21:11 +0000247//===----------------------------------------------------------------------===//
248// AnalysisUsage Class Implementation
249//
250
Chris Lattner1b368a02006-12-01 23:27:45 +0000251namespace {
252 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000253 typedef AnalysisUsage::VectorType VectorType;
254 VectorType &CFGOnlyList;
255 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick808a7a62012-02-03 05:12:30 +0000256
Craig Topperf398d7c2014-03-05 06:35:38 +0000257 void passEnumerate(const PassInfo *P) override {
Chris Lattner1b368a02006-12-01 23:27:45 +0000258 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000259 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000260 }
261 };
262}
263
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000264// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner23d54052006-12-01 22:21:11 +0000265// not:
266//
267// 1. Add or remove basic blocks from the function
268// 2. Modify terminator instructions in any way.
269//
270// This function annotates the AnalysisUsage info object to say that analyses
271// that only depend on the CFG are preserved by this pass.
272//
273void AnalysisUsage::setPreservesCFG() {
274 // Since this transformation doesn't modify the CFG, it preserves all analyses
275 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000276 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000277}
278
Owen Andersona7aed182010-08-06 18:33:48 +0000279AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
280 const PassInfo *PI = Pass::lookupPassInfo(Arg);
281 // If the pass exists, preserve it. Otherwise silently do nothing.
282 if (PI) Preserved.push_back(PI->getTypeInfo());
283 return *this;
284}
285
286AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000287 Required.push_back(ID);
288 return *this;
289}
Chris Lattner23d54052006-12-01 22:21:11 +0000290
Owen Andersona7aed182010-08-06 18:33:48 +0000291AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
292 Required.push_back(&ID);
293 return *this;
294}
295
296AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
297 Required.push_back(&ID);
298 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000299 return *this;
300}