blob: 69299fea7634ca37ab3c13cdb3962f4727b933bd [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///
Chris Lattner071577d2002-07-29 21:02:31 +000067const char *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
Bill Wendling22e978a2006-12-07 20:04:42 +0000121// dump - call print(cerr);
Yaron Kereneb2a2542016-01-29 20:50:44 +0000122LLVM_DUMP_METHOD void Pass::dump() const {
Craig Topperc6207612014-04-09 06:08:46 +0000123 print(dbgs(), nullptr);
Chris Lattner26750072002-07-27 01:12:17 +0000124}
125
Chris Lattnercdd09c22002-01-31 00:45:31 +0000126//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000127// ImmutablePass Implementation
128//
Devang Patelf5a994e2006-12-22 22:49:00 +0000129// Force out-of-line virtual method.
130ImmutablePass::~ImmutablePass() { }
Chris Lattneree0788d2002-09-25 21:59:11 +0000131
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000132void ImmutablePass::initializePass() {
133 // By default, don't do anything.
134}
135
Chris Lattneree0788d2002-09-25 21:59:11 +0000136//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000137// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000138//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000139
David Greene9b063df2010-04-02 23:17:14 +0000140Pass *FunctionPass::createPrinterPass(raw_ostream &O,
141 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000142 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000143}
144
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000145PassManagerType FunctionPass::getPotentialPassManagerType() const {
146 return PMT_FunctionPassManager;
147}
148
Andrew Kayloraa641a52016-04-22 22:06:11 +0000149bool FunctionPass::skipFunction(const Function &F) const {
150 if (!F.getContext().getOptBisect().shouldRunPass(this, F))
151 return true;
152
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000153 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000154 DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
155 << F.getName() << "\n");
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000156 return true;
157 }
158 return false;
159}
160
Chris Lattnercdd09c22002-01-31 00:45:31 +0000161//===----------------------------------------------------------------------===//
162// BasicBlockPass Implementation
163//
164
David Greene9b063df2010-04-02 23:17:14 +0000165Pass *BasicBlockPass::createPrinterPass(raw_ostream &O,
166 const std::string &Banner) const {
Chandler Carruth9d805132014-01-12 11:30:46 +0000167 return createPrintBasicBlockPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000168}
169
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000170bool BasicBlockPass::doInitialization(Function &) {
171 // By default, don't do anything.
172 return false;
173}
174
175bool BasicBlockPass::doFinalization(Function &) {
176 // By default, don't do anything.
177 return false;
178}
179
Andrew Kayloraa641a52016-04-22 22:06:11 +0000180bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
Paul Robinson0c12b1d22014-02-26 01:23:26 +0000181 const Function *F = BB.getParent();
Andrew Kayloraa641a52016-04-22 22:06:11 +0000182 if (!F)
183 return false;
184 if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
185 return true;
186 if (F->hasFnAttribute(Attribute::OptimizeNone)) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000187 // Report this only once per function.
188 if (&BB == &F->getEntryBlock())
189 DEBUG(dbgs() << "Skipping pass '" << getPassName()
190 << "' on function " << F->getName() << "\n");
191 return true;
192 }
193 return false;
194}
195
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000196PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000197 return PMT_BasicBlockPassManager;
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000198}
199
Owen Andersona7aed182010-08-06 18:33:48 +0000200const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000201 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000202}
203
Owen Anderson81781222010-07-20 08:26:15 +0000204const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000205 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000206}
207
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000208Pass *Pass::createPass(AnalysisID ID) {
209 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trick3a61b782012-02-08 21:22:34 +0000210 if (!PI)
Craig Topperc6207612014-04-09 06:08:46 +0000211 return nullptr;
Andrew Trick3a61b782012-02-08 21:22:34 +0000212 return PI->createPass();
213}
214
Chris Lattner6e041bd2002-08-21 22:17:09 +0000215//===----------------------------------------------------------------------===//
216// Analysis Group Implementation Code
217//===----------------------------------------------------------------------===//
218
Chris Lattner6e041bd2002-08-21 22:17:09 +0000219// RegisterAGBase implementation
220//
Owen Andersona7aed182010-08-06 18:33:48 +0000221RegisterAGBase::RegisterAGBase(const char *Name, const void *InterfaceID,
222 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000223 : PassInfo(Name, InterfaceID) {
224 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
225 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000226}
227
Chris Lattner37d3c952002-07-23 18:08:00 +0000228//===----------------------------------------------------------------------===//
229// PassRegistrationListener implementation
230//
231
Chris Lattner37d3c952002-07-23 18:08:00 +0000232// 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 Bieneman799ef372015-01-22 21:01:12 +0000239PassNameParser::PassNameParser(cl::Option &O)
240 : cl::parser<const PassInfo *>(O) {
Zachary Turner39c422d2014-06-12 00:16:36 +0000241 PassRegistry::getPassRegistry()->addRegistrationListener(this);
242}
243
244PassNameParser::~PassNameParser() {
245 // This only gets called during static destruction, in which case the
246 // PassRegistry will have already been destroyed by llvm_shutdown(). So
247 // attempting to remove the registration listener is an error.
248}
Chris Lattner5264fce2010-01-22 06:29:25 +0000249
Chris Lattner23d54052006-12-01 22:21:11 +0000250//===----------------------------------------------------------------------===//
251// AnalysisUsage Class Implementation
252//
253
Chris Lattner1b368a02006-12-01 23:27:45 +0000254namespace {
255 struct GetCFGOnlyPasses : public PassRegistrationListener {
Chris Lattnercbd160f2008-08-08 05:33:04 +0000256 typedef AnalysisUsage::VectorType VectorType;
257 VectorType &CFGOnlyList;
258 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
Andrew Trick808a7a62012-02-03 05:12:30 +0000259
Craig Topperf398d7c2014-03-05 06:35:38 +0000260 void passEnumerate(const PassInfo *P) override {
Chris Lattner1b368a02006-12-01 23:27:45 +0000261 if (P->isCFGOnlyPass())
Owen Andersona7aed182010-08-06 18:33:48 +0000262 CFGOnlyList.push_back(P->getTypeInfo());
Chris Lattner1b368a02006-12-01 23:27:45 +0000263 }
264 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000265}
Chris Lattner1b368a02006-12-01 23:27:45 +0000266
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000267// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner23d54052006-12-01 22:21:11 +0000268// not:
269//
270// 1. Add or remove basic blocks from the function
271// 2. Modify terminator instructions in any way.
272//
273// This function annotates the AnalysisUsage info object to say that analyses
274// that only depend on the CFG are preserved by this pass.
275//
276void AnalysisUsage::setPreservesCFG() {
277 // Since this transformation doesn't modify the CFG, it preserves all analyses
278 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000279 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000280}
281
Owen Andersona7aed182010-08-06 18:33:48 +0000282AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
283 const PassInfo *PI = Pass::lookupPassInfo(Arg);
284 // If the pass exists, preserve it. Otherwise silently do nothing.
285 if (PI) Preserved.push_back(PI->getTypeInfo());
286 return *this;
287}
288
289AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000290 Required.push_back(ID);
291 return *this;
292}
Chris Lattner23d54052006-12-01 22:21:11 +0000293
Owen Andersona7aed182010-08-06 18:33:48 +0000294AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
295 Required.push_back(&ID);
296 return *this;
297}
298
299AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
300 Required.push_back(&ID);
301 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000302 return *this;
303}