blob: f1b5f2f108dc14ac89a1d75c30e0d95828e46e20 [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"
Andrew Kayloraa641a52016-04-22 22:06:11 +000020#include "llvm/IR/Module.h"
21#include "llvm/IR/OptBisect.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/PassRegistry.h"
David Greenedfe4ad72010-01-05 01:30:04 +000023#include "llvm/Support/Debug.h"
Chris Lattner13626022009-08-23 06:03:38 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattner189d19f2003-11-21 20:23:48 +000025using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000026
Chandler Carruthe96dd892014-04-21 22:55:11 +000027#define DEBUG_TYPE "ir"
28
Chris Lattner7e0dbe62002-05-06 19:31:52 +000029//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000030// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000031//
Chris Lattnercdd09c22002-01-31 00:45:31 +000032
Devang Patelf5a994e2006-12-22 22:49:00 +000033// Force out-of-line virtual method.
Andrew Trick808a7a62012-02-03 05:12:30 +000034Pass::~Pass() {
35 delete Resolver;
Devang Patel2c1bba02007-04-26 21:33:42 +000036}
37
38// Force out-of-line virtual method.
Devang Patelf5a994e2006-12-22 22:49:00 +000039ModulePass::~ModulePass() { }
Chris Lattner26e4f892002-01-21 07:37:31 +000040
David Greene9b063df2010-04-02 23:17:14 +000041Pass *ModulePass::createPrinterPass(raw_ostream &O,
42 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +000043 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +000044}
45
Dan Gohman1cfbfc82009-12-14 19:43:09 +000046PassManagerType ModulePass::getPotentialPassManagerType() const {
47 return PMT_ModulePassManager;
48}
49
Andrew Kayloraa641a52016-04-22 22:06:11 +000050bool ModulePass::skipModule(Module &M) const {
51 return !M.getContext().getOptBisect().shouldRunPass(this, M);
52}
53
Owen Andersona7aed182010-08-06 18:33:48 +000054bool Pass::mustPreserveAnalysisID(char &AID) const {
Craig Topperc6207612014-04-09 06:08:46 +000055 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
Chris Lattner9ad77572003-03-21 21:41:02 +000056}
57
Joe Abbey96e89f62011-11-21 04:42:21 +000058// dumpPassStructure - Implement the -debug-pass=Structure option
Dan Gohmanf71c5212010-08-19 01:29:07 +000059void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000060 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000061}
Chris Lattner37104aa2002-04-29 14:57:45 +000062
Dan Gohman94f57c52008-03-14 18:27:04 +000063/// getPassName - Return a nice clean name for a pass. This usually
64/// implemented in terms of the name that is registered by one of the
65/// Registration templates, but can be overloaded directly.
66///
Mehdi Amini117296c2016-10-01 02:56:57 +000067StringRef Pass::getPassName() const {
Owen Andersona7aed182010-08-06 18:33:48 +000068 AnalysisID AID = getPassID();
69 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
70 if (PI)
Chris Lattner071577d2002-07-29 21:02:31 +000071 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000072 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000073}
Chris Lattner37104aa2002-04-29 14:57:45 +000074
Dan Gohman1cfbfc82009-12-14 19:43:09 +000075void Pass::preparePassManager(PMStack &) {
76 // By default, don't do anything.
77}
78
79PassManagerType Pass::getPotentialPassManagerType() const {
80 // Default implementation.
Andrew Trick808a7a62012-02-03 05:12:30 +000081 return PMT_Unknown;
Dan Gohman1cfbfc82009-12-14 19:43:09 +000082}
83
84void Pass::getAnalysisUsage(AnalysisUsage &) const {
85 // By default, no analysis results are used, all are invalidated.
86}
87
88void Pass::releaseMemory() {
89 // By default, don't do anything.
90}
91
92void Pass::verifyAnalysis() const {
93 // By default, don't do anything.
94}
95
Owen Andersona7aed182010-08-06 18:33:48 +000096void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmanffdee302010-06-21 18:46:45 +000097 return this;
98}
99
100ImmutablePass *Pass::getAsImmutablePass() {
Craig Topperc6207612014-04-09 06:08:46 +0000101 return nullptr;
Dan Gohmanffdee302010-06-21 18:46:45 +0000102}
103
104PMDataManager *Pass::getAsPMDataManager() {
Craig Topperc6207612014-04-09 06:08:46 +0000105 return nullptr;
Dan Gohmanffdee302010-06-21 18:46:45 +0000106}
107
108void Pass::setResolver(AnalysisResolver *AR) {
109 assert(!Resolver && "Resolver is already set");
110 Resolver = AR;
111}
112
Misha Brukman56a86422003-10-14 21:38:42 +0000113// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000114// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000115// implement this method.
116//
Chris Lattner13626022009-08-23 06:03:38 +0000117void Pass::print(raw_ostream &O,const Module*) const {
Chris Lattner26750072002-07-27 01:12:17 +0000118 O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
119}
120
Matthias Braun8c209aa2017-01-28 02:02:38 +0000121#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Bill Wendling22e978a2006-12-07 20:04:42 +0000122// dump - call print(cerr);
Yaron Kereneb2a2542016-01-29 20:50:44 +0000123LLVM_DUMP_METHOD void Pass::dump() const {
Craig Topperc6207612014-04-09 06:08:46 +0000124 print(dbgs(), nullptr);
Chris Lattner26750072002-07-27 01:12:17 +0000125}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000126#endif
Chris Lattner26750072002-07-27 01:12:17 +0000127
Chris Lattnercdd09c22002-01-31 00:45:31 +0000128//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000129// ImmutablePass Implementation
130//
Devang Patelf5a994e2006-12-22 22:49:00 +0000131// Force out-of-line virtual method.
132ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000133
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000134void ImmutablePass::initializePass() {
135 // By default, don't do anything.
136}
137
Chris Lattneree0788d2002-09-25 21:59:11 +0000138//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000139// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000140//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000141
David Greene9b063df2010-04-02 23:17:14 +0000142Pass *FunctionPass::createPrinterPass(raw_ostream &O,
143 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000144 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000145}
146
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000147PassManagerType FunctionPass::getPotentialPassManagerType() const {
148 return PMT_FunctionPassManager;
149}
150
Serge Pavloved5eb932017-01-15 10:23:18 +0000151bool FunctionPass::skipFunction(const Function &F) const {
152 if (!F.getContext().getOptBisect().shouldRunPass(this, F))
Andrew Kayloraa641a52016-04-22 22:06:11 +0000153 return true;
154
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000155 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000156 DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
157 << F.getName() << "\n");
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000158 return true;
159 }
160 return false;
161}
162
Chris Lattnercdd09c22002-01-31 00:45:31 +0000163//===----------------------------------------------------------------------===//
164// BasicBlockPass Implementation
165//
166
David Greene9b063df2010-04-02 23:17:14 +0000167Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
168 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000169 return createPrintBasicBlockPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000170}
171
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000172bool BasicBlockPass::doInitialization(Function &) {
173 // By default, don't do anything.
174 return false;
175}
176
177bool BasicBlockPass::doFinalization(Function &) {
178 // By default, don't do anything.
179 return false;
180}
181
Andrew Kayloraa641a52016-04-22 22:06:11 +0000182bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
Paul Robinson0c12b1d22014-02-26 01:23:26 +0000183 const Function *F = BB.getParent();
Andrew Kayloraa641a52016-04-22 22:06:11 +0000184 if (!F)
185 return false;
186 if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
187 return true;
188 if (F->hasFnAttribute(Attribute::OptimizeNone)) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000189 // Report this only once per function.
190 if (&BB == &F->getEntryBlock())
191 DEBUG(dbgs() << "Skipping pass '" << getPassName()
192 << "' on function " << F->getName() << "\n");
193 return true;
194 }
195 return false;
196}
197
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000198PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000199 return PMT_BasicBlockPassManager;
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000200}
201
Owen Andersona7aed182010-08-06 18:33:48 +0000202const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000203 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000204}
205
Owen Anderson81781222010-07-20 08:26:15 +0000206const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000207 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000208}
209
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000210Pass *Pass::createPass(AnalysisID ID) {
211 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trick3a61b782012-02-08 21:22:34 +0000212 if (!PI)
Craig Topperc6207612014-04-09 06:08:46 +0000213 return nullptr;
Andrew Trick3a61b782012-02-08 21:22:34 +0000214 return PI->createPass();
215}
216
Chris Lattner6e041bd2002-08-21 22:17:09 +0000217//===----------------------------------------------------------------------===//
218// Analysis Group Implementation Code
219//===----------------------------------------------------------------------===//
220
Chris Lattner6e041bd2002-08-21 22:17:09 +0000221// RegisterAGBase implementation
222//
Mehdi Aminif20abe52016-10-01 04:03:30 +0000223RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
Owen Andersona7aed182010-08-06 18:33:48 +0000224 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000225 : PassInfo(Name, InterfaceID) {
226 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
227 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000228}
229
Chris Lattner37d3c952002-07-23 18:08:00 +0000230//===----------------------------------------------------------------------===//
231// PassRegistrationListener implementation
232//
233
Chris Lattner37d3c952002-07-23 18:08:00 +0000234// enumeratePasses - Iterate over the registered passes, calling the
235// passEnumerate callback on each PassInfo object.
236//
237void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000238 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000239}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000240
Chris Bieneman799ef372015-01-22 21:01:12 +0000241PassNameParser::PassNameParser(cl::Option &O)
242 : cl::parser<const PassInfo *>(O) {
Zachary Turner39c422d2014-06-12 00:16:36 +0000243 PassRegistry::getPassRegistry()->addRegistrationListener(this);
244}
245
246PassNameParser::~PassNameParser() {
247 // This only gets called during static destruction, in which case the
248 // PassRegistry will have already been destroyed by llvm_shutdown(). So
249 // attempting to remove the registration listener is an error.
250}
Chris Lattner5264fce2010-01-22 06:29:25 +0000251
Chris Lattner23d54052006-12-01 22:21:11 +0000252//===----------------------------------------------------------------------===//
253// AnalysisUsage Class Implementation
254//
255
Chris Lattner1b368a02006-12-01 23:27:45 +0000256namespace {
257 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000258 typedef AnalysisUsage::VectorType VectorType;
259 VectorType &CFGOnlyList;
260 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick808a7a62012-02-03 05:12:30 +0000261
Craig Topperf398d7c2014-03-05 06:35:38 +0000262 void passEnumerate(const PassInfo *P) override {
Chris Lattner1b368a02006-12-01 23:27:45 +0000263 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000264 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000265 }
266 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000267}
Chris Lattner1b368a02006-12-01 23:27:45 +0000268
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000269// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner23d54052006-12-01 22:21:11 +0000270// not:
271//
272// 1. Add or remove basic blocks from the function
273// 2. Modify terminator instructions in any way.
274//
275// This function annotates the AnalysisUsage info object to say that analyses
276// that only depend on the CFG are preserved by this pass.
277//
278void AnalysisUsage::setPreservesCFG() {
279 // Since this transformation doesn't modify the CFG, it preserves all analyses
280 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000281 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000282}
283
Owen Andersona7aed182010-08-06 18:33:48 +0000284AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
285 const PassInfo *PI = Pass::lookupPassInfo(Arg);
286 // If the pass exists, preserve it. Otherwise silently do nothing.
287 if (PI) Preserved.push_back(PI->getTypeInfo());
288 return *this;
289}
290
291AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000292 Required.push_back(ID);
293 return *this;
294}
Chris Lattner23d54052006-12-01 22:21:11 +0000295
Owen Andersona7aed182010-08-06 18:33:48 +0000296AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
297 Required.push_back(&ID);
298 return *this;
299}
300
301AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
302 Required.push_back(&ID);
303 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000304 return *this;
305}