blob: 5e0b59476c4bec4d5902c9dc177e9d34be5643c5 [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"
Eugene Zelenko92334e02017-09-06 23:05:38 +000017#include "llvm/IR/Attributes.h"
18#include "llvm/IR/BasicBlock.h"
Paul Robinsonaf4e64d2014-02-06 00:07:05 +000019#include "llvm/IR/Function.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000020#include "llvm/IR/IRPrintingPasses.h"
Eugene Zelenko92334e02017-09-06 23:05:38 +000021#include "llvm/IR/LLVMContext.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000022#include "llvm/IR/LegacyPassNameParser.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000023#include "llvm/IR/Module.h"
24#include "llvm/IR/OptBisect.h"
Eugene Zelenko92334e02017-09-06 23:05:38 +000025#include "llvm/PassInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/PassRegistry.h"
Eugene Zelenko92334e02017-09-06 23:05:38 +000027#include "llvm/PassSupport.h"
28#include "llvm/Support/Compiler.h"
David Greenedfe4ad72010-01-05 01:30:04 +000029#include "llvm/Support/Debug.h"
Chris Lattner13626022009-08-23 06:03:38 +000030#include "llvm/Support/raw_ostream.h"
Eugene Zelenko92334e02017-09-06 23:05:38 +000031#include <cassert>
32
Chris Lattner189d19f2003-11-21 20:23:48 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Chandler Carruthe96dd892014-04-21 22:55:11 +000035#define DEBUG_TYPE "ir"
36
Chris Lattner7e0dbe62002-05-06 19:31:52 +000037//===----------------------------------------------------------------------===//
Chris Lattnercdd09c22002-01-31 00:45:31 +000038// Pass Implementation
Chris Lattner654b5bc2002-01-22 00:17:48 +000039//
Chris Lattnercdd09c22002-01-31 00:45:31 +000040
Devang Patelf5a994e2006-12-22 22:49:00 +000041// Force out-of-line virtual method.
Andrew Trick808a7a62012-02-03 05:12:30 +000042Pass::~Pass() {
43 delete Resolver;
Devang Patel2c1bba02007-04-26 21:33:42 +000044}
45
46// Force out-of-line virtual method.
Eugene Zelenko92334e02017-09-06 23:05:38 +000047ModulePass::~ModulePass() = default;
Chris Lattner26e4f892002-01-21 07:37:31 +000048
Eugene Zelenko92334e02017-09-06 23:05:38 +000049Pass *ModulePass::createPrinterPass(raw_ostream &OS,
David Greene9b063df2010-04-02 23:17:14 +000050 const std::string &Banner) const {
Eugene Zelenko92334e02017-09-06 23:05:38 +000051 return createPrintModulePass(OS, Banner);
David Greene9b063df2010-04-02 23:17:14 +000052}
53
Dan Gohman1cfbfc82009-12-14 19:43:09 +000054PassManagerType ModulePass::getPotentialPassManagerType() const {
55 return PMT_ModulePassManager;
56}
57
Andrew Kayloraa641a52016-04-22 22:06:11 +000058bool ModulePass::skipModule(Module &M) const {
59 return !M.getContext().getOptBisect().shouldRunPass(this, M);
60}
61
Owen Andersona7aed182010-08-06 18:33:48 +000062bool Pass::mustPreserveAnalysisID(char &AID) const {
Craig Topperc6207612014-04-09 06:08:46 +000063 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
Chris Lattner9ad77572003-03-21 21:41:02 +000064}
65
Joe Abbey96e89f62011-11-21 04:42:21 +000066// dumpPassStructure - Implement the -debug-pass=Structure option
Dan Gohmanf71c5212010-08-19 01:29:07 +000067void Pass::dumpPassStructure(unsigned Offset) {
David Greenedfe4ad72010-01-05 01:30:04 +000068 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner198cf422002-07-30 16:27:02 +000069}
Chris Lattner37104aa2002-04-29 14:57:45 +000070
Dan Gohman94f57c52008-03-14 18:27:04 +000071/// getPassName - Return a nice clean name for a pass. This usually
72/// implemented in terms of the name that is registered by one of the
73/// Registration templates, but can be overloaded directly.
Mehdi Amini117296c2016-10-01 02:56:57 +000074StringRef Pass::getPassName() const {
Owen Andersona7aed182010-08-06 18:33:48 +000075 AnalysisID AID = getPassID();
76 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
77 if (PI)
Chris Lattner071577d2002-07-29 21:02:31 +000078 return PI->getPassName();
Chris Lattner9afb8e42007-10-18 16:11:18 +000079 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner071577d2002-07-29 21:02:31 +000080}
Chris Lattner37104aa2002-04-29 14:57:45 +000081
Dan Gohman1cfbfc82009-12-14 19:43:09 +000082void Pass::preparePassManager(PMStack &) {
83 // By default, don't do anything.
84}
85
86PassManagerType Pass::getPotentialPassManagerType() const {
87 // Default implementation.
Andrew Trick808a7a62012-02-03 05:12:30 +000088 return PMT_Unknown;
Dan Gohman1cfbfc82009-12-14 19:43:09 +000089}
90
91void Pass::getAnalysisUsage(AnalysisUsage &) const {
92 // By default, no analysis results are used, all are invalidated.
93}
94
95void Pass::releaseMemory() {
96 // By default, don't do anything.
97}
98
99void Pass::verifyAnalysis() const {
100 // By default, don't do anything.
101}
102
Owen Andersona7aed182010-08-06 18:33:48 +0000103void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000104 return this;
105}
106
107ImmutablePass *Pass::getAsImmutablePass() {
Craig Topperc6207612014-04-09 06:08:46 +0000108 return nullptr;
Dan Gohmanffdee302010-06-21 18:46:45 +0000109}
110
111PMDataManager *Pass::getAsPMDataManager() {
Craig Topperc6207612014-04-09 06:08:46 +0000112 return nullptr;
Dan Gohmanffdee302010-06-21 18:46:45 +0000113}
114
115void Pass::setResolver(AnalysisResolver *AR) {
116 assert(!Resolver && "Resolver is already set");
117 Resolver = AR;
118}
119
Misha Brukman56a86422003-10-14 21:38:42 +0000120// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman7eb05a12003-08-18 14:43:39 +0000121// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattner26750072002-07-27 01:12:17 +0000122// implement this method.
Eugene Zelenko92334e02017-09-06 23:05:38 +0000123void Pass::print(raw_ostream &OS, const Module *) const {
124 OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
Chris Lattner26750072002-07-27 01:12:17 +0000125}
126
Aaron Ballman615eb472017-10-15 14:32:27 +0000127#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Bill Wendling22e978a2006-12-07 20:04:42 +0000128// dump - call print(cerr);
Yaron Kereneb2a2542016-01-29 20:50:44 +0000129LLVM_DUMP_METHOD void Pass::dump() const {
Craig Topperc6207612014-04-09 06:08:46 +0000130 print(dbgs(), nullptr);
Chris Lattner26750072002-07-27 01:12:17 +0000131}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000132#endif
Chris Lattner26750072002-07-27 01:12:17 +0000133
Chris Lattnercdd09c22002-01-31 00:45:31 +0000134//===----------------------------------------------------------------------===//
Chris Lattneree0788d2002-09-25 21:59:11 +0000135// ImmutablePass Implementation
136//
Devang Patelf5a994e2006-12-22 22:49:00 +0000137// Force out-of-line virtual method.
Eugene Zelenko92334e02017-09-06 23:05:38 +0000138ImmutablePass::~ImmutablePass() = default;
Chris Lattneree0788d2002-09-25 21:59:11 +0000139
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000140void ImmutablePass::initializePass() {
141 // By default, don't do anything.
142}
143
Chris Lattneree0788d2002-09-25 21:59:11 +0000144//===----------------------------------------------------------------------===//
Chris Lattnerc8e66542002-04-27 06:56:12 +0000145// FunctionPass Implementation
Chris Lattner26e4f892002-01-21 07:37:31 +0000146//
Chris Lattnercdd09c22002-01-31 00:45:31 +0000147
Eugene Zelenko92334e02017-09-06 23:05:38 +0000148Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
David Greene9b063df2010-04-02 23:17:14 +0000149 const std::string &Banner) const {
Eugene Zelenko92334e02017-09-06 23:05:38 +0000150 return createPrintFunctionPass(OS, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000151}
152
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000153PassManagerType FunctionPass::getPotentialPassManagerType() const {
154 return PMT_FunctionPassManager;
155}
156
Serge Pavloved5eb932017-01-15 10:23:18 +0000157bool FunctionPass::skipFunction(const Function &F) const {
158 if (!F.getContext().getOptBisect().shouldRunPass(this, F))
Andrew Kayloraa641a52016-04-22 22:06:11 +0000159 return true;
160
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000161 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000162 DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
163 << F.getName() << "\n");
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000164 return true;
165 }
166 return false;
167}
168
Chris Lattnercdd09c22002-01-31 00:45:31 +0000169//===----------------------------------------------------------------------===//
170// BasicBlockPass Implementation
171//
172
Eugene Zelenko92334e02017-09-06 23:05:38 +0000173Pass *BasicBlockPass::createPrinterPass(raw_ostream &OS,
David Greene9b063df2010-04-02 23:17:14 +0000174 const std::string &Banner) const {
Eugene Zelenko92334e02017-09-06 23:05:38 +0000175 return createPrintBasicBlockPass(OS, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000176}
177
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000178bool BasicBlockPass::doInitialization(Function &) {
179 // By default, don't do anything.
180 return false;
181}
182
183bool BasicBlockPass::doFinalization(Function &) {
184 // By default, don't do anything.
185 return false;
186}
187
Andrew Kayloraa641a52016-04-22 22:06:11 +0000188bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
Paul Robinson0c12b1d22014-02-26 01:23:26 +0000189 const Function *F = BB.getParent();
Andrew Kayloraa641a52016-04-22 22:06:11 +0000190 if (!F)
191 return false;
192 if (!F->getContext().getOptBisect().shouldRunPass(this, BB))
193 return true;
194 if (F->hasFnAttribute(Attribute::OptimizeNone)) {
Paul Robinsonaf4e64d2014-02-06 00:07:05 +0000195 // Report this only once per function.
196 if (&BB == &F->getEntryBlock())
197 DEBUG(dbgs() << "Skipping pass '" << getPassName()
198 << "' on function " << F->getName() << "\n");
199 return true;
200 }
201 return false;
202}
203
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000204PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick808a7a62012-02-03 05:12:30 +0000205 return PMT_BasicBlockPassManager;
Dan Gohman1cfbfc82009-12-14 19:43:09 +0000206}
207
Owen Andersona7aed182010-08-06 18:33:48 +0000208const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Anderson41540612010-07-20 21:22:24 +0000209 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner37d3c952002-07-23 18:08:00 +0000210}
211
Owen Anderson81781222010-07-20 08:26:15 +0000212const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Anderson41540612010-07-20 21:22:24 +0000213 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman09984272009-10-08 17:00:02 +0000214}
215
Andrew Trickc9ce9d22012-02-15 03:21:47 +0000216Pass *Pass::createPass(AnalysisID ID) {
217 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trick3a61b782012-02-08 21:22:34 +0000218 if (!PI)
Craig Topperc6207612014-04-09 06:08:46 +0000219 return nullptr;
Andrew Trick3a61b782012-02-08 21:22:34 +0000220 return PI->createPass();
221}
222
Chris Lattner6e041bd2002-08-21 22:17:09 +0000223//===----------------------------------------------------------------------===//
224// Analysis Group Implementation Code
225//===----------------------------------------------------------------------===//
226
Chris Lattner6e041bd2002-08-21 22:17:09 +0000227// RegisterAGBase implementation
Eugene Zelenko92334e02017-09-06 23:05:38 +0000228
Mehdi Aminif20abe52016-10-01 04:03:30 +0000229RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
Owen Andersona7aed182010-08-06 18:33:48 +0000230 const void *PassID, bool isDefault)
Owen Anderson845b14e2010-07-21 17:52:45 +0000231 : PassInfo(Name, InterfaceID) {
232 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
233 *this, isDefault);
Chris Lattner6e041bd2002-08-21 22:17:09 +0000234}
235
Chris Lattner37d3c952002-07-23 18:08:00 +0000236//===----------------------------------------------------------------------===//
237// PassRegistrationListener implementation
238//
239
Chris Lattner37d3c952002-07-23 18:08:00 +0000240// enumeratePasses - Iterate over the registered passes, calling the
241// passEnumerate callback on each PassInfo object.
Chris Lattner37d3c952002-07-23 18:08:00 +0000242void PassRegistrationListener::enumeratePasses() {
Owen Anderson41540612010-07-20 21:22:24 +0000243 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner37d3c952002-07-23 18:08:00 +0000244}
Reid Spencer8edc8be2005-04-25 01:01:35 +0000245
Chris Bieneman799ef372015-01-22 21:01:12 +0000246PassNameParser::PassNameParser(cl::Option &O)
247 : cl::parser<const PassInfo *>(O) {
Zachary Turner39c422d2014-06-12 00:16:36 +0000248 PassRegistry::getPassRegistry()->addRegistrationListener(this);
249}
250
Eugene Zelenko92334e02017-09-06 23:05:38 +0000251// This only gets called during static destruction, in which case the
252// PassRegistry will have already been destroyed by llvm_shutdown(). So
253// attempting to remove the registration listener is an error.
254PassNameParser::~PassNameParser() = default;
Chris Lattner5264fce2010-01-22 06:29:25 +0000255
Chris Lattner23d54052006-12-01 22:21:11 +0000256//===----------------------------------------------------------------------===//
257// AnalysisUsage Class Implementation
258//
259
Chris Lattner1b368a02006-12-01 23:27:45 +0000260namespace {
Andrew Trick808a7a62012-02-03 05:12:30 +0000261
Eugene Zelenko92334e02017-09-06 23:05:38 +0000262struct GetCFGOnlyPasses : public PassRegistrationListener {
263 using VectorType = AnalysisUsage::VectorType;
264
265 VectorType &CFGOnlyList;
266
267 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
268
269 void passEnumerate(const PassInfo *P) override {
270 if (P->isCFGOnlyPass())
271 CFGOnlyList.push_back(P->getTypeInfo());
272 }
273};
274
275} // end anonymous namespace
Chris Lattner1b368a02006-12-01 23:27:45 +0000276
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +0000277// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner23d54052006-12-01 22:21:11 +0000278// not:
279//
280// 1. Add or remove basic blocks from the function
281// 2. Modify terminator instructions in any way.
282//
283// This function annotates the AnalysisUsage info object to say that analyses
284// that only depend on the CFG are preserved by this pass.
Chris Lattner23d54052006-12-01 22:21:11 +0000285void AnalysisUsage::setPreservesCFG() {
286 // Since this transformation doesn't modify the CFG, it preserves all analyses
287 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattner1b368a02006-12-01 23:27:45 +0000288 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner23d54052006-12-01 22:21:11 +0000289}
290
Owen Andersona7aed182010-08-06 18:33:48 +0000291AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
292 const PassInfo *PI = Pass::lookupPassInfo(Arg);
293 // If the pass exists, preserve it. Otherwise silently do nothing.
294 if (PI) Preserved.push_back(PI->getTypeInfo());
295 return *this;
296}
297
298AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmanffdee302010-06-21 18:46:45 +0000299 Required.push_back(ID);
300 return *this;
301}
Chris Lattner23d54052006-12-01 22:21:11 +0000302
Owen Andersona7aed182010-08-06 18:33:48 +0000303AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
304 Required.push_back(&ID);
305 return *this;
306}
307
308AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
309 Required.push_back(&ID);
310 RequiredTransitive.push_back(&ID);
Dan Gohmanffdee302010-06-21 18:46:45 +0000311 return *this;
312}