blob: df45460a6ccaca930e41b2a7ca7f5e04b3cc1f35 [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
Chris Lattner6e041bd2002-08-21 22:17:09 +0000202//===----------------------------------------------------------------------===//
203// Analysis Group Implementation Code
204//===----------------------------------------------------------------------===//
205
Chris Lattner6e041bd2002-08-21 22:17:09 +0000206// RegisterAGBase implementation
207//
Owen Andersona7aed182010-08-06 18:33:48 +0000208RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
209 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000210 : PassInfo(Name, InterfaceID) {
211 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
212 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000213}
214
Chris Lattner37d3c952002-07-23 18:08:00 +0000215//===----------------------------------------------------------------------===//
216// PassRegistrationListener implementation
217//
218
Chris Lattner37d3c952002-07-23 18:08:00 +0000219// enumeratePasses - Iterate over the registered passes, calling the
220// passEnumerate callback on each PassInfo object.
221//
222void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000223 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000224}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000225
Chris Bieneman799ef372015-01-22 21:01:12 +0000226PassNameParser::PassNameParser(cl::Option &O)
227 : cl::parser<const PassInfo *>(O) {
Zachary Turner39c422d2014-06-12 00:16:36 +0000228 PassRegistry::getPassRegistry()->addRegistrationListener(this);
229}
230
231PassNameParser::~PassNameParser() {
232 // This only gets called during static destruction, in which case the
233 // PassRegistry will have already been destroyed by llvm_shutdown(). So
234 // attempting to remove the registration listener is an error.
235}
Chris Lattner5264fce2010-01-22 06:29:25 +0000236
Chris Lattner23d54052006-12-01 22:21:11 +0000237//===----------------------------------------------------------------------===//
238// AnalysisUsage Class Implementation
239//
240
Chris Lattner1b368a02006-12-01 23:27:45 +0000241namespace {
242 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000243 typedef AnalysisUsage::VectorType VectorType;
244 VectorType &CFGOnlyList;
245 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick808a7a62012-02-03 05:12:30 +0000246
Craig Topperf398d7c2014-03-05 06:35:38 +0000247 void passEnumerate(const PassInfo *P) override {
Chris Lattner1b368a02006-12-01 23:27:45 +0000248 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000249 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000250 }
251 };
252}
253
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000254// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner23d54052006-12-01 22:21:11 +0000255// not:
256//
257// 1. Add or remove basic blocks from the function
258// 2. Modify terminator instructions in any way.
259//
260// This function annotates the AnalysisUsage info object to say that analyses
261// that only depend on the CFG are preserved by this pass.
262//
263void AnalysisUsage::setPreservesCFG() {
264 // Since this transformation doesn't modify the CFG, it preserves all analyses
265 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000266 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000267}
268
Owen Andersona7aed182010-08-06 18:33:48 +0000269AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
270 const PassInfo *PI = Pass::lookupPassInfo(Arg);
271 // If the pass exists, preserve it. Otherwise silently do nothing.
272 if (PI) Preserved.push_back(PI->getTypeInfo());
273 return *this;
274}
275
276AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000277 Required.push_back(ID);
278 return *this;
279}
Chris Lattner23d54052006-12-01 22:21:11 +0000280
Owen Andersona7aed182010-08-06 18:33:48 +0000281AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
282 Required.push_back(&ID);
283 return *this;
284}
285
286AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
287 Required.push_back(&ID);
288 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000289 return *this;
290}