blob: a1dc17882493ff9a297451edf0be4eca641cbeeb [file] [log] [blame]
Chris Lattner3e58d9b2003-10-13 05:33:01 +00001//===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerd6953282002-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 Gohmanee335e32008-05-23 20:40:06 +000016#include "llvm/Pass.h"
Nico Weber0f38c602018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Eugene Zelenko9797e4a2017-09-06 23:05:38 +000018#include "llvm/IR/Attributes.h"
19#include "llvm/IR/BasicBlock.h"
Paul Robinson2684ddd2014-02-06 00:07:05 +000020#include "llvm/IR/Function.h"
Chandler Carruth8a5351f2014-01-12 11:10:32 +000021#include "llvm/IR/IRPrintingPasses.h"
Eugene Zelenko9797e4a2017-09-06 23:05:38 +000022#include "llvm/IR/LLVMContext.h"
Chandler Carruthf7591dd2014-03-04 12:32:42 +000023#include "llvm/IR/LegacyPassNameParser.h"
Andrew Kaylor1e455c52016-04-22 22:06:11 +000024#include "llvm/IR/Module.h"
25#include "llvm/IR/OptBisect.h"
Eugene Zelenko9797e4a2017-09-06 23:05:38 +000026#include "llvm/PassInfo.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/PassRegistry.h"
Eugene Zelenko9797e4a2017-09-06 23:05:38 +000028#include "llvm/PassSupport.h"
29#include "llvm/Support/Compiler.h"
David Greene840d41e2010-01-05 01:30:04 +000030#include "llvm/Support/Debug.h"
Chris Lattner45cfe542009-08-23 06:03:38 +000031#include "llvm/Support/raw_ostream.h"
Eugene Zelenko9797e4a2017-09-06 23:05:38 +000032#include <cassert>
33
Chris Lattner31f84992003-11-21 20:23:48 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Chandler Carruth283b3992014-04-21 22:55:11 +000036#define DEBUG_TYPE "ir"
37
Chris Lattner27ad1372002-05-06 19:31:52 +000038//===----------------------------------------------------------------------===//
Chris Lattner41300862002-01-31 00:45:31 +000039// Pass Implementation
Chris Lattner35f07eb2002-01-22 00:17:48 +000040//
Chris Lattner41300862002-01-31 00:45:31 +000041
Devang Patelc7d0f4b2006-12-22 22:49:00 +000042// Force out-of-line virtual method.
Andrew Trick8247e0d2012-02-03 05:12:30 +000043Pass::~Pass() {
44 delete Resolver;
Devang Patel6e21ff02007-04-26 21:33:42 +000045}
46
47// Force out-of-line virtual method.
Eugene Zelenko9797e4a2017-09-06 23:05:38 +000048ModulePass::~ModulePass() = default;
Chris Lattnerd6953282002-01-21 07:37:31 +000049
Eugene Zelenko9797e4a2017-09-06 23:05:38 +000050Pass *ModulePass::createPrinterPass(raw_ostream &OS,
David Greene5c8aa952010-04-02 23:17:14 +000051 const std::string &Banner) const {
Eugene Zelenko9797e4a2017-09-06 23:05:38 +000052 return createPrintModulePass(OS, Banner);
David Greene5c8aa952010-04-02 23:17:14 +000053}
54
Dan Gohmaneda23fa2009-12-14 19:43:09 +000055PassManagerType ModulePass::getPotentialPassManagerType() const {
56 return PMT_ModulePassManager;
57}
58
Andrew Kaylor1e455c52016-04-22 22:06:11 +000059bool ModulePass::skipModule(Module &M) const {
Fedor Sergeev558f76f2018-03-27 16:57:20 +000060 return !M.getContext().getOptPassGate().shouldRunPass(this, M);
Andrew Kaylor1e455c52016-04-22 22:06:11 +000061}
62
Owen Anderson90c579d2010-08-06 18:33:48 +000063bool Pass::mustPreserveAnalysisID(char &AID) const {
Craig Topperec0f0bc2014-04-09 06:08:46 +000064 return Resolver->getAnalysisIfAvailable(&AID, true) != nullptr;
Chris Lattnerf1ab4542003-03-21 21:41:02 +000065}
66
Joe Abbey62faf772011-11-21 04:42:21 +000067// dumpPassStructure - Implement the -debug-pass=Structure option
Dan Gohman8a757ae2010-08-19 01:29:07 +000068void Pass::dumpPassStructure(unsigned Offset) {
David Greene840d41e2010-01-05 01:30:04 +000069 dbgs().indent(Offset*2) << getPassName() << "\n";
Chris Lattner37d66c42002-07-30 16:27:02 +000070}
Chris Lattner96c466b2002-04-29 14:57:45 +000071
Dan Gohmanb973d5f2008-03-14 18:27:04 +000072/// getPassName - Return a nice clean name for a pass. This usually
73/// implemented in terms of the name that is registered by one of the
74/// Registration templates, but can be overloaded directly.
Mehdi Amini67f335d2016-10-01 02:56:57 +000075StringRef Pass::getPassName() const {
Owen Anderson90c579d2010-08-06 18:33:48 +000076 AnalysisID AID = getPassID();
77 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
78 if (PI)
Chris Lattner44050fb2002-07-29 21:02:31 +000079 return PI->getPassName();
Chris Lattnerfe3e3f42007-10-18 16:11:18 +000080 return "Unnamed pass: implement Pass::getPassName()";
Chris Lattner44050fb2002-07-29 21:02:31 +000081}
Chris Lattner96c466b2002-04-29 14:57:45 +000082
Dan Gohmaneda23fa2009-12-14 19:43:09 +000083void Pass::preparePassManager(PMStack &) {
84 // By default, don't do anything.
85}
86
87PassManagerType Pass::getPotentialPassManagerType() const {
88 // Default implementation.
Andrew Trick8247e0d2012-02-03 05:12:30 +000089 return PMT_Unknown;
Dan Gohmaneda23fa2009-12-14 19:43:09 +000090}
91
92void Pass::getAnalysisUsage(AnalysisUsage &) const {
93 // By default, no analysis results are used, all are invalidated.
94}
95
96void Pass::releaseMemory() {
97 // By default, don't do anything.
98}
99
100void Pass::verifyAnalysis() const {
101 // By default, don't do anything.
102}
103
Owen Anderson90c579d2010-08-06 18:33:48 +0000104void *Pass::getAdjustedAnalysisPointer(AnalysisID AID) {
Dan Gohmane407c1d2010-06-21 18:46:45 +0000105 return this;
106}
107
108ImmutablePass *Pass::getAsImmutablePass() {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000109 return nullptr;
Dan Gohmane407c1d2010-06-21 18:46:45 +0000110}
111
112PMDataManager *Pass::getAsPMDataManager() {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000113 return nullptr;
Dan Gohmane407c1d2010-06-21 18:46:45 +0000114}
115
116void Pass::setResolver(AnalysisResolver *AR) {
117 assert(!Resolver && "Resolver is already set");
118 Resolver = AR;
119}
120
Misha Brukman69c856a2003-10-14 21:38:42 +0000121// print - Print out the internal state of the pass. This is called by Analyze
Misha Brukman5560c9d2003-08-18 14:43:39 +0000122// to print out the contents of an analysis. Otherwise it is not necessary to
Chris Lattnera59cbb22002-07-27 01:12:17 +0000123// implement this method.
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000124void Pass::print(raw_ostream &OS, const Module *) const {
125 OS << "Pass::print not implemented for pass: '" << getPassName() << "'!\n";
Chris Lattnera59cbb22002-07-27 01:12:17 +0000126}
127
Aaron Ballman1d03d382017-10-15 14:32:27 +0000128#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Bill Wendling832171c2006-12-07 20:04:42 +0000129// dump - call print(cerr);
Yaron Keren55307982016-01-29 20:50:44 +0000130LLVM_DUMP_METHOD void Pass::dump() const {
Craig Topperec0f0bc2014-04-09 06:08:46 +0000131 print(dbgs(), nullptr);
Chris Lattnera59cbb22002-07-27 01:12:17 +0000132}
Matthias Braun88d20752017-01-28 02:02:38 +0000133#endif
Chris Lattnera59cbb22002-07-27 01:12:17 +0000134
Chris Lattner41300862002-01-31 00:45:31 +0000135//===----------------------------------------------------------------------===//
Chris Lattner70b4f3e2002-09-25 21:59:11 +0000136// ImmutablePass Implementation
137//
Devang Patelc7d0f4b2006-12-22 22:49:00 +0000138// Force out-of-line virtual method.
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000139ImmutablePass::~ImmutablePass() = default;
Chris Lattner70b4f3e2002-09-25 21:59:11 +0000140
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000141void ImmutablePass::initializePass() {
142 // By default, don't do anything.
143}
144
Chris Lattner70b4f3e2002-09-25 21:59:11 +0000145//===----------------------------------------------------------------------===//
Chris Lattnerf57b8452002-04-27 06:56:12 +0000146// FunctionPass Implementation
Chris Lattnerd6953282002-01-21 07:37:31 +0000147//
Chris Lattner41300862002-01-31 00:45:31 +0000148
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000149Pass *FunctionPass::createPrinterPass(raw_ostream &OS,
David Greene5c8aa952010-04-02 23:17:14 +0000150 const std::string &Banner) const {
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000151 return createPrintFunctionPass(OS, Banner);
David Greene5c8aa952010-04-02 23:17:14 +0000152}
153
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000154PassManagerType FunctionPass::getPotentialPassManagerType() const {
155 return PMT_FunctionPassManager;
156}
157
Serge Pavlov69a1a202017-01-15 10:23:18 +0000158bool FunctionPass::skipFunction(const Function &F) const {
Fedor Sergeev558f76f2018-03-27 16:57:20 +0000159 if (!F.getContext().getOptPassGate().shouldRunPass(this, F))
Andrew Kaylor1e455c52016-04-22 22:06:11 +0000160 return true;
161
Paul Robinson2684ddd2014-02-06 00:07:05 +0000162 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000163 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName() << "' on function "
164 << F.getName() << "\n");
Paul Robinson2684ddd2014-02-06 00:07:05 +0000165 return true;
166 }
167 return false;
168}
169
Chris Lattner41300862002-01-31 00:45:31 +0000170//===----------------------------------------------------------------------===//
171// BasicBlockPass Implementation
172//
173
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000174Pass *BasicBlockPass::createPrinterPass(raw_ostream &OS,
David Greene5c8aa952010-04-02 23:17:14 +0000175 const std::string &Banner) const {
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000176 return createPrintBasicBlockPass(OS, Banner);
David Greene5c8aa952010-04-02 23:17:14 +0000177}
178
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000179bool BasicBlockPass::doInitialization(Function &) {
180 // By default, don't do anything.
181 return false;
182}
183
184bool BasicBlockPass::doFinalization(Function &) {
185 // By default, don't do anything.
186 return false;
187}
188
Andrew Kaylor1e455c52016-04-22 22:06:11 +0000189bool BasicBlockPass::skipBasicBlock(const BasicBlock &BB) const {
Paul Robinsoncf84b5b2014-02-26 01:23:26 +0000190 const Function *F = BB.getParent();
Andrew Kaylor1e455c52016-04-22 22:06:11 +0000191 if (!F)
192 return false;
Fedor Sergeev558f76f2018-03-27 16:57:20 +0000193 if (!F->getContext().getOptPassGate().shouldRunPass(this, BB))
Andrew Kaylor1e455c52016-04-22 22:06:11 +0000194 return true;
195 if (F->hasFnAttribute(Attribute::OptimizeNone)) {
Paul Robinson2684ddd2014-02-06 00:07:05 +0000196 // Report this only once per function.
197 if (&BB == &F->getEntryBlock())
Nicola Zaghen0818e782018-05-14 12:53:11 +0000198 LLVM_DEBUG(dbgs() << "Skipping pass '" << getPassName()
199 << "' on function " << F->getName() << "\n");
Paul Robinson2684ddd2014-02-06 00:07:05 +0000200 return true;
201 }
202 return false;
203}
204
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000205PassManagerType BasicBlockPass::getPotentialPassManagerType() const {
Andrew Trick8247e0d2012-02-03 05:12:30 +0000206 return PMT_BasicBlockPassManager;
Dan Gohmaneda23fa2009-12-14 19:43:09 +0000207}
208
Owen Anderson90c579d2010-08-06 18:33:48 +0000209const PassInfo *Pass::lookupPassInfo(const void *TI) {
Owen Andersonaac07ea2010-07-20 21:22:24 +0000210 return PassRegistry::getPassRegistry()->getPassInfo(TI);
Chris Lattner54bbdb42002-07-23 18:08:00 +0000211}
212
Owen Anderson8be32912010-07-20 08:26:15 +0000213const PassInfo *Pass::lookupPassInfo(StringRef Arg) {
Owen Andersonaac07ea2010-07-20 21:22:24 +0000214 return PassRegistry::getPassRegistry()->getPassInfo(Arg);
Dan Gohman8a261e42009-10-08 17:00:02 +0000215}
216
Andrew Trick5e108ee2012-02-15 03:21:47 +0000217Pass *Pass::createPass(AnalysisID ID) {
218 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);
Andrew Trickebe18ef2012-02-08 21:22:34 +0000219 if (!PI)
Craig Topperec0f0bc2014-04-09 06:08:46 +0000220 return nullptr;
Andrew Trickebe18ef2012-02-08 21:22:34 +0000221 return PI->createPass();
222}
223
Chris Lattner789bc842002-08-21 22:17:09 +0000224//===----------------------------------------------------------------------===//
225// Analysis Group Implementation Code
226//===----------------------------------------------------------------------===//
227
Chris Lattner789bc842002-08-21 22:17:09 +0000228// RegisterAGBase implementation
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000229
Mehdi Amini6bd185b2016-10-01 04:03:30 +0000230RegisterAGBase::RegisterAGBase(StringRef Name, const void *InterfaceID,
Owen Anderson90c579d2010-08-06 18:33:48 +0000231 const void *PassID, bool isDefault)
Owen Anderson96509832010-07-21 17:52:45 +0000232 : PassInfo(Name, InterfaceID) {
233 PassRegistry::getPassRegistry()->registerAnalysisGroup(InterfaceID, PassID,
234 *this, isDefault);
Chris Lattner789bc842002-08-21 22:17:09 +0000235}
236
Chris Lattner54bbdb42002-07-23 18:08:00 +0000237//===----------------------------------------------------------------------===//
238// PassRegistrationListener implementation
239//
240
Chris Lattner54bbdb42002-07-23 18:08:00 +0000241// enumeratePasses - Iterate over the registered passes, calling the
242// passEnumerate callback on each PassInfo object.
Chris Lattner54bbdb42002-07-23 18:08:00 +0000243void PassRegistrationListener::enumeratePasses() {
Owen Andersonaac07ea2010-07-20 21:22:24 +0000244 PassRegistry::getPassRegistry()->enumerateWith(this);
Chris Lattner54bbdb42002-07-23 18:08:00 +0000245}
Reid Spencere8f38482005-04-25 01:01:35 +0000246
Chris Bieneman40a21862015-01-22 21:01:12 +0000247PassNameParser::PassNameParser(cl::Option &O)
248 : cl::parser<const PassInfo *>(O) {
Zachary Turnera4dc93e2014-06-12 00:16:36 +0000249 PassRegistry::getPassRegistry()->addRegistrationListener(this);
250}
251
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000252// This only gets called during static destruction, in which case the
253// PassRegistry will have already been destroyed by llvm_shutdown(). So
254// attempting to remove the registration listener is an error.
255PassNameParser::~PassNameParser() = default;
Chris Lattner6eeb7342010-01-22 06:29:25 +0000256
Chris Lattner947c7682006-12-01 22:21:11 +0000257//===----------------------------------------------------------------------===//
258// AnalysisUsage Class Implementation
259//
260
Chris Lattnerbaf2ecd2006-12-01 23:27:45 +0000261namespace {
Andrew Trick8247e0d2012-02-03 05:12:30 +0000262
Eugene Zelenko9797e4a2017-09-06 23:05:38 +0000263struct GetCFGOnlyPasses : public PassRegistrationListener {
264 using VectorType = AnalysisUsage::VectorType;
265
266 VectorType &CFGOnlyList;
267
268 GetCFGOnlyPasses(VectorType &L) : CFGOnlyList(L) {}
269
270 void passEnumerate(const PassInfo *P) override {
271 if (P->isCFGOnlyPass())
272 CFGOnlyList.push_back(P->getTypeInfo());
273 }
274};
275
276} // end anonymous namespace
Chris Lattnerbaf2ecd2006-12-01 23:27:45 +0000277
Sylvestre Ledru94c22712012-09-27 10:14:43 +0000278// setPreservesCFG - This function should be called to by the pass, iff they do
Chris Lattner947c7682006-12-01 22:21:11 +0000279// not:
280//
281// 1. Add or remove basic blocks from the function
282// 2. Modify terminator instructions in any way.
283//
284// This function annotates the AnalysisUsage info object to say that analyses
285// that only depend on the CFG are preserved by this pass.
Chris Lattner947c7682006-12-01 22:21:11 +0000286void AnalysisUsage::setPreservesCFG() {
287 // Since this transformation doesn't modify the CFG, it preserves all analyses
288 // that only depend on the CFG (like dominators, loop info, etc...)
Chris Lattnerbaf2ecd2006-12-01 23:27:45 +0000289 GetCFGOnlyPasses(Preserved).enumeratePasses();
Chris Lattner947c7682006-12-01 22:21:11 +0000290}
291
Owen Anderson90c579d2010-08-06 18:33:48 +0000292AnalysisUsage &AnalysisUsage::addPreserved(StringRef Arg) {
293 const PassInfo *PI = Pass::lookupPassInfo(Arg);
294 // If the pass exists, preserve it. Otherwise silently do nothing.
295 if (PI) Preserved.push_back(PI->getTypeInfo());
296 return *this;
297}
298
299AnalysisUsage &AnalysisUsage::addRequiredID(const void *ID) {
Dan Gohmane407c1d2010-06-21 18:46:45 +0000300 Required.push_back(ID);
301 return *this;
302}
Chris Lattner947c7682006-12-01 22:21:11 +0000303
Owen Anderson90c579d2010-08-06 18:33:48 +0000304AnalysisUsage &AnalysisUsage::addRequiredID(char &ID) {
305 Required.push_back(&ID);
306 return *this;
307}
308
309AnalysisUsage &AnalysisUsage::addRequiredTransitiveID(char &ID) {
310 Required.push_back(&ID);
311 RequiredTransitive.push_back(&ID);
Dan Gohmane407c1d2010-06-21 18:46:45 +0000312 return *this;
313}