blob: 8bd9ed6ef0faa1c7f2612644cdd5edc43d2d68de [file] [log] [blame]
Chandler Carruth7caea412013-11-09 12:26:54 +00001//===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===//
Devang Patel6e5a1132006-11-07 21:31:57 +00002//
3// 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.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruth7caea412013-11-09 12:26:54 +000010// This file implements the legacy LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth7caea412013-11-09 12:26:54 +000014#include "llvm/IR/LegacyPassManager.h"
James Henderson852f6fd2017-05-16 09:43:21 +000015#include "llvm/ADT/Statistic.h"
Pavel Labathec534e62016-10-25 16:20:07 +000016#include "llvm/IR/IRPrintingPasses.h"
17#include "llvm/IR/LLVMContext.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000018#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000019#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/IR/Module.h"
Pavel Labathec534e62016-10-25 16:20:07 +000021#include "llvm/Support/Chrono.h"
Devang Patelf1567a52006-12-13 20:03:48 +000022#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000023#include "llvm/Support/Debug.h"
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000024#include "llvm/Support/Error.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000025#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000026#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000027#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Support/Timer.h"
29#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000030#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000031#include <map>
Weiming Zhao0f1762c2016-01-06 22:55:03 +000032#include <unordered_set>
Dan Gohman8c43e412007-10-03 19:04:09 +000033using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000034using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000035
Devang Patele7599552007-01-12 18:52:44 +000036// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000037
Devang Patelf1567a52006-12-13 20:03:48 +000038//===----------------------------------------------------------------------===//
39// Pass debugging information. Often it is useful to find out what pass is
40// running when a crash occurs in a utility. When this library is compiled with
41// debugging on, a command line option (--debug-pass) is enabled that causes the
42// pass name to be printed before it executes.
43//
44
Chandler Carruth7caea412013-11-09 12:26:54 +000045namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000046// Different debug levels that can be enabled...
47enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000048 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000049};
Chandler Carruth7caea412013-11-09 12:26:54 +000050}
Devang Patel03fb5872006-12-13 21:13:31 +000051
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000052static cl::opt<enum PassDebugLevel>
53PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000054 cl::desc("Print PassManager debugging information"),
55 cl::values(
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000056 clEnumVal(Disabled , "disable debug output"),
57 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
58 clEnumVal(Structure , "print pass structure before run()"),
59 clEnumVal(Executions, "print pass name before it is executed"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000060 clEnumVal(Details , "print pass details when it is executed")));
David Greene9b063df2010-04-02 23:17:14 +000061
Chandler Carruth7caea412013-11-09 12:26:54 +000062namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000063typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
64PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000065}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000066
67// Print IR out before/after specified passes.
68static PassOptionList
69PrintBefore("print-before",
70 llvm::cl::desc("Print IR before specified passes"),
71 cl::Hidden);
72
73static PassOptionList
74PrintAfter("print-after",
75 llvm::cl::desc("Print IR after specified passes"),
76 cl::Hidden);
77
Zachary Turner8065f0b2017-12-01 00:53:10 +000078static cl::opt<bool> PrintBeforeAll("print-before-all",
79 llvm::cl::desc("Print IR before each pass"),
80 cl::init(false), cl::Hidden);
81static cl::opt<bool> PrintAfterAll("print-after-all",
82 llvm::cl::desc("Print IR after each pass"),
83 cl::init(false), cl::Hidden);
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000084
Fedor Sergeev94dca7c2017-12-01 17:42:46 +000085static cl::opt<bool>
86 PrintModuleScope("print-module-scope",
87 cl::desc("When printing IR for print-[before|after]{-all} "
88 "always print a module IR"),
89 cl::init(false));
90
Weiming Zhao0f1762c2016-01-06 22:55:03 +000091static cl::list<std::string>
92 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
93 cl::desc("Only print IR for functions whose name "
94 "match this for all print-[before|after][-all] "
95 "options"),
Zachary Turner8065f0b2017-12-01 00:53:10 +000096 cl::CommaSeparated, cl::Hidden);
Weiming Zhao0f1762c2016-01-06 22:55:03 +000097
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000098/// This is a helper to determine whether to print IR before or
99/// after a pass.
100
101static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
102 PassOptionList &PassesToPrint) {
Chris Bieneman664294c2015-04-29 21:45:22 +0000103 for (auto *PassInf : PassesToPrint) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000104 if (PassInf)
105 if (PassInf->getPassArgument() == PI->getPassArgument()) {
106 return true;
107 }
David Greene9b063df2010-04-02 23:17:14 +0000108 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000109 return false;
110}
Dan Gohmande6188a2010-08-12 23:50:08 +0000111
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000112/// This is a utility to check whether a pass should have IR dumped
113/// before it.
114static bool ShouldPrintBeforePass(const PassInfo *PI) {
115 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
116}
David Greene9b063df2010-04-02 23:17:14 +0000117
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000118/// This is a utility to check whether a pass should have IR dumped
119/// after it.
120static bool ShouldPrintAfterPass(const PassInfo *PI) {
121 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000122}
123
Fedor Sergeev94dca7c2017-12-01 17:42:46 +0000124bool llvm::forcePrintModuleIR() { return PrintModuleScope; }
125
Weiming Zhao0f1762c2016-01-06 22:55:03 +0000126bool llvm::isFunctionInPrintList(StringRef FunctionName) {
127 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
128 PrintFuncsList.end());
129 return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
130}
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000131/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
132/// or higher is specified.
133bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000134 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000135}
136
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000137
138
139
Chris Lattner4c1e9542009-03-06 06:45:05 +0000140void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000141 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000142 OS << "Releasing pass '";
143 else
144 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000145
Chris Lattner4c1e9542009-03-06 06:45:05 +0000146 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000147
Chris Lattner4c1e9542009-03-06 06:45:05 +0000148 if (M) {
149 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
150 return;
151 }
Craig Topperc6207612014-04-09 06:08:46 +0000152 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000153 OS << '\n';
154 return;
155 }
156
Dan Gohman79fc0e92009-03-10 18:47:59 +0000157 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000158 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000159 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000160 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000161 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000162 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000163 OS << "value";
164
165 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000166 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000167 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000168}
169
170
Devang Patelffca9102006-12-15 19:39:30 +0000171namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000172//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000173// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000174//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000175/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000176/// pass together and sequence them to process one basic block before
177/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000178class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000179
180public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000181 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000182 explicit BBPassManager()
183 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000184
Devang Patelca58e352006-11-08 10:05:38 +0000185 /// Execute all of the passes scheduled for execution. Keep track of
186 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000187 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000188
Devang Patelf9d96b92006-12-07 19:57:52 +0000189 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000190 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000191 Info.setPreservesAll();
192 }
193
Craig Topperf398d7c2014-03-05 06:35:38 +0000194 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000195 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000196 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000197 bool doFinalization(Function &F);
198
Craig Topperf398d7c2014-03-05 06:35:38 +0000199 PMDataManager *getAsPMDataManager() override { return this; }
200 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000201
Mehdi Amini117296c2016-10-01 02:56:57 +0000202 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000203
Devang Pateleda56172006-12-12 23:34:33 +0000204 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000205 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000206 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000207 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
208 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000209 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000210 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000211 }
212 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000213
214 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000215 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000216 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
217 return BP;
218 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000219
Craig Topperf398d7c2014-03-05 06:35:38 +0000220 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000221 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000222 }
Devang Patelca58e352006-11-08 10:05:38 +0000223};
224
Devang Patel8c78a0b2007-05-03 01:11:54 +0000225char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000226} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000227
Devang Patele7599552007-01-12 18:52:44 +0000228namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000229namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000230//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000231// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000232//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000233/// FunctionPassManagerImpl manages FPPassManagers
234class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000235 public PMDataManager,
236 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000237 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000238private:
239 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000240public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000241 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000242 explicit FunctionPassManagerImpl() :
243 Pass(PT_PassManager, ID), PMDataManager(),
244 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000245
Matthias Braun0880c602014-12-12 01:27:01 +0000246 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000247 void add(Pass *P) {
248 schedulePass(P);
249 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000250
251 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000252 Pass *createPrinterPass(raw_ostream &O,
253 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000254 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000255 }
256
Torok Edwin24c78352009-06-29 18:49:09 +0000257 // Prepare for running an on the fly pass, freeing memory if needed
258 // from a previous run.
259 void releaseMemoryOnTheFly();
260
Devang Patel67d6a5e2006-12-19 19:46:59 +0000261 /// run - Execute all of the passes scheduled for execution. Keep track of
262 /// whether any of the passes modifies the module, and if so, return true.
263 bool run(Function &F);
264
265 /// doInitialization - Run all of the initializers for the function passes.
266 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000267 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000268
Dan Gohmane6656eb2007-07-30 14:51:13 +0000269 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000270 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000271 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000272
Dan Gohmande6188a2010-08-12 23:50:08 +0000273
Craig Topperf398d7c2014-03-05 06:35:38 +0000274 PMDataManager *getAsPMDataManager() override { return this; }
275 Pass *getAsPass() override { return this; }
276 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000277 return PMT_FunctionPassManager;
278 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000279
Devang Patel67d6a5e2006-12-19 19:46:59 +0000280 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000281 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000282 Info.setPreservesAll();
283 }
284
Devang Patel67d6a5e2006-12-19 19:46:59 +0000285 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000286 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000287 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
288 return FP;
289 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000290};
291
David Blaikiea379b1812011-12-20 02:50:00 +0000292void FunctionPassManagerImpl::anchor() {}
293
Devang Patel8c78a0b2007-05-03 01:11:54 +0000294char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000295} // End of legacy namespace
296} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000297
Chandler Carruth7caea412013-11-09 12:26:54 +0000298namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000299//===----------------------------------------------------------------------===//
300// MPPassManager
301//
302/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000303/// It batches all Module passes and function pass managers together and
304/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000305class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000306public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000307 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000308 explicit MPPassManager() :
309 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000310
311 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000312 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000313 for (auto &OnTheFlyManager : OnTheFlyManagers) {
314 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000315 delete FPP;
316 }
317 }
318
Dan Gohmande6188a2010-08-12 23:50:08 +0000319 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000320 Pass *createPrinterPass(raw_ostream &O,
321 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000322 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000323 }
324
Devang Patelca58e352006-11-08 10:05:38 +0000325 /// run - Execute all of the passes scheduled for execution. Keep track of
326 /// whether any of the passes modifies the module, and if so, return true.
327 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000328
Pedro Artigase4348b02012-12-03 21:56:57 +0000329 using llvm::Pass::doInitialization;
330 using llvm::Pass::doFinalization;
331
Devang Patelf9d96b92006-12-07 19:57:52 +0000332 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000333 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000334 Info.setPreservesAll();
335 }
336
Devang Patele64d3052007-04-16 20:12:57 +0000337 /// Add RequiredPass into list of lower level passes required by pass P.
338 /// RequiredPass is run on the fly by Pass Manager when P requests it
339 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000340 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000341
Dan Gohmande6188a2010-08-12 23:50:08 +0000342 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000343 /// required by module pass MP. Instantiate analysis pass, by using
344 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000345 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000346
Mehdi Amini117296c2016-10-01 02:56:57 +0000347 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000348
Craig Topperf398d7c2014-03-05 06:35:38 +0000349 PMDataManager *getAsPMDataManager() override { return this; }
350 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000351
Devang Pateleda56172006-12-12 23:34:33 +0000352 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000353 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000354 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000355 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
356 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000357 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000358 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
359 OnTheFlyManagers.find(MP);
360 if (I != OnTheFlyManagers.end())
361 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000362 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000363 }
364 }
365
Devang Patelabfbe3b2006-12-16 00:56:26 +0000366 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000367 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000368 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000369 }
370
Craig Topperf398d7c2014-03-05 06:35:38 +0000371 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000372 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000373 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000374
375 private:
376 /// Collection of on the fly FPPassManagers. These managers manage
377 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000378 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000379};
380
Devang Patel8c78a0b2007-05-03 01:11:54 +0000381char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000382} // End anonymous namespace
383
384namespace llvm {
385namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000386//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000387// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000388//
Devang Patel09f162c2007-05-01 21:15:47 +0000389
Devang Patel67d6a5e2006-12-19 19:46:59 +0000390/// PassManagerImpl manages MPPassManagers
391class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000392 public PMDataManager,
393 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000394 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000395
396public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000397 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000398 explicit PassManagerImpl() :
399 Pass(PT_PassManager, ID), PMDataManager(),
400 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000401
Matthias Braun0880c602014-12-12 01:27:01 +0000402 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000403 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000404 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000405 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000406
407 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000408 Pass *createPrinterPass(raw_ostream &O,
409 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000410 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000411 }
412
Devang Patel376fefa2006-11-08 10:29:57 +0000413 /// run - Execute all of the passes scheduled for execution. Keep track of
414 /// whether any of the passes modifies the module, and if so, return true.
415 bool run(Module &M);
416
Pedro Artigase4348b02012-12-03 21:56:57 +0000417 using llvm::Pass::doInitialization;
418 using llvm::Pass::doFinalization;
419
Devang Patelf9d96b92006-12-07 19:57:52 +0000420 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000421 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000422 Info.setPreservesAll();
423 }
424
Craig Topperf398d7c2014-03-05 06:35:38 +0000425 PMDataManager *getAsPMDataManager() override { return this; }
426 Pass *getAsPass() override { return this; }
427 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000428 return PMT_ModulePassManager;
429 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000430
Devang Patel67d6a5e2006-12-19 19:46:59 +0000431 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000432 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000433 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
434 return MP;
435 }
Devang Patel376fefa2006-11-08 10:29:57 +0000436};
437
David Blaikiea379b1812011-12-20 02:50:00 +0000438void PassManagerImpl::anchor() {}
439
Devang Patel8c78a0b2007-05-03 01:11:54 +0000440char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000441} // End of legacy namespace
442} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000443
444namespace {
445
446//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000447/// TimingInfo Class - This class is used to calculate information about the
448/// amount of time each pass takes to execute. This only happens when
449/// -time-passes is enabled on the command line.
450///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000451
Owen Anderson5a6960f2009-06-18 20:51:00 +0000452static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000453
Nick Lewycky02d5f772009-10-25 06:33:48 +0000454class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000455 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000456 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000457public:
458 // Use 'create' member to get this.
Matthias Braun9f15a792016-11-18 19:43:18 +0000459 TimingInfo() : TG("pass", "... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000460
Devang Patel1c3633e2007-01-29 23:10:37 +0000461 // TimingDtor - Print out information about timing information
462 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000463 // Delete all of the timers, which accumulate their info into the
464 // TimerGroup.
Yaron Keren4849aa32015-06-05 17:48:47 +0000465 for (auto &I : TimingData)
466 delete I.second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000467 // TimerGroup is deleted next, printing the report.
468 }
469
470 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000471 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000472 // null. It may be called multiple times.
473 static void createTheTimeInfo();
474
James Henderson852f6fd2017-05-16 09:43:21 +0000475 // print - Prints out timing information and then resets the timers.
476 void print() {
477 TG.print(*CreateInfoOutputFile());
478 }
479
Chris Lattner707431c2010-03-30 04:03:22 +0000480 /// getPassTimer - Return the timer for the specified pass if it exists.
481 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000482 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000483 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000484
Owen Anderson5c96ef72009-07-07 18:33:04 +0000485 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000486 Timer *&T = TimingData[P];
Matthias Braun9f15a792016-11-18 19:43:18 +0000487 if (!T) {
488 StringRef PassName = P->getPassName();
489 T = new Timer(PassName, PassName, TG);
490 }
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000491 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000492 }
493};
494
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000495} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000496
Dan Gohmand78c4002008-05-13 00:00:25 +0000497static TimingInfo *TheTimeInfo;
498
Devang Patela1514cb2006-12-07 19:39:39 +0000499//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000500// PMTopLevelManager implementation
501
Devang Patel4268fc02007-01-16 02:00:38 +0000502/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000503PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
504 PMDM->setTopLevelManager(this);
505 addPassManager(PMDM);
506 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000507}
508
Devang Patelafb1f3622006-12-12 22:35:25 +0000509/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000510void
Bill Wendlingea857e12012-05-14 07:53:40 +0000511PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000512 unsigned PDepth = 0;
513 if (P->getResolver())
514 PDepth = P->getResolver()->getPMDataManager().getDepth();
515
Yaron Keren4849aa32015-06-05 17:48:47 +0000516 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000517 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000518
Devang Patel01919d22007-03-08 19:05:01 +0000519 if (P == AP)
520 continue;
521
Tobias Grosserf07426b2011-01-20 21:03:22 +0000522 // Update the last users of passes that are required transitive by AP.
523 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
524 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
525 SmallVector<Pass *, 12> LastUses;
526 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000527 for (AnalysisID ID : IDs) {
528 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000529 assert(AnalysisPass && "Expected analysis pass to exist.");
530 AnalysisResolver *AR = AnalysisPass->getResolver();
531 assert(AR && "Expected analysis resolver to exist.");
532 unsigned APDepth = AR->getPMDataManager().getDepth();
533
534 if (PDepth == APDepth)
535 LastUses.push_back(AnalysisPass);
536 else if (PDepth > APDepth)
537 LastPMUses.push_back(AnalysisPass);
538 }
539
540 setLastUser(LastUses, P);
541
542 // If this pass has a corresponding pass manager, push higher level
543 // analysis to this pass manager.
544 if (P->getResolver())
545 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
546
547
Devang Patelafb1f3622006-12-12 22:35:25 +0000548 // If AP is the last user of other passes then make P last user of
549 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000550 for (auto LU : LastUser) {
551 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000552 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000553 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000554 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000555 }
556 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000557}
558
559/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000560void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000561 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000562 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000563 InversedLastUser.find(P);
564 if (DMI == InversedLastUser.end())
565 return;
566
567 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000568 for (Pass *LUP : LU) {
569 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000570 }
571
Devang Patelafb1f3622006-12-12 22:35:25 +0000572}
573
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000574AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000575 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000576 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000577 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000578 AnUsage = DMI->second;
579 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000580 // Look up the analysis usage from the pass instance (different instances
581 // of the same pass can produce different results), but unique the
582 // resulting object to reduce memory usage. This helps to greatly reduce
583 // memory usage when we have many instances of only a few pass types
584 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
585 // of dependencies.
586 AnalysisUsage AU;
587 P->getAnalysisUsage(AU);
588
589 AUFoldingSetNode* Node = nullptr;
590 FoldingSetNodeID ID;
591 AUFoldingSetNode::Profile(ID, AU);
592 void *IP = nullptr;
593 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
594 Node = N;
595 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000596 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000597 UniqueAnalysisUsages.InsertNode(Node, IP);
598 }
599 assert(Node && "cached analysis usage must be non null");
600
601 AnUsageMap[P] = &Node->AU;
Mandeep Singh Grang5e1697e2017-06-06 05:08:36 +0000602 AnUsage = &Node->AU;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000603 }
604 return AnUsage;
605}
606
Devang Patelafb1f3622006-12-12 22:35:25 +0000607/// Schedule pass P for execution. Make sure that passes required by
608/// P are run before P is run. Update analysis info maintained by
609/// the manager. Remove dead passes. This is a recursive function.
610void PMTopLevelManager::schedulePass(Pass *P) {
611
Devang Patel3312f752007-01-16 21:43:18 +0000612 // TODO : Allocate function manager for this pass, other wise required set
613 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000614
Devang Pateld74ede72007-03-06 01:06:16 +0000615 // Give pass a chance to prepare the stage.
616 P->preparePassManager(activeStack);
617
Devang Patel864970e2008-03-18 00:39:19 +0000618 // If P is an analysis pass and it is available then do not
619 // generate the analysis again. Stale analysis info should not be
620 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000621 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000622 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000623 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000624 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000625 }
Devang Patel864970e2008-03-18 00:39:19 +0000626
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000627 AnalysisUsage *AnUsage = findAnalysisUsage(P);
628
Devang Patelfdee7032008-08-14 23:07:48 +0000629 bool checkAnalysis = true;
630 while (checkAnalysis) {
631 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000632
Devang Patelfdee7032008-08-14 23:07:48 +0000633 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000634 for (const AnalysisID ID : RequiredSet) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000635
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000636 Pass *AnalysisPass = findAnalysisPass(ID);
Devang Patelfdee7032008-08-14 23:07:48 +0000637 if (!AnalysisPass) {
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000638 const PassInfo *PI = findAnalysisPassInfo(ID);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000639
Craig Topperc6207612014-04-09 06:08:46 +0000640 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000641 // Pass P is not in the global PassRegistry
642 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
643 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
644 dbgs() << "Required Passes:" << "\n";
Florian Hahn03c3a1a2017-07-13 10:52:00 +0000645 for (const AnalysisID ID2 : RequiredSet) {
646 if (ID == ID2)
647 break;
648 Pass *AnalysisPass2 = findAnalysisPass(ID2);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000649 if (AnalysisPass2) {
650 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000651 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000652 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
653 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
654 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
655 }
656 }
657 }
658
Andrew Trick6bbaf132011-06-03 00:48:58 +0000659 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000660 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000661 if (P->getPotentialPassManagerType () ==
662 AnalysisPass->getPotentialPassManagerType())
663 // Schedule analysis pass that is managed by the same pass manager.
664 schedulePass(AnalysisPass);
665 else if (P->getPotentialPassManagerType () >
666 AnalysisPass->getPotentialPassManagerType()) {
667 // Schedule analysis pass that is managed by a new manager.
668 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000669 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000670 // are already checked are still available.
671 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000672 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000673 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000674 // passes are run on the fly.
675 delete AnalysisPass;
676 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000677 }
678 }
679
680 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000681 if (ImmutablePass *IP = P->getAsImmutablePass()) {
682 // P is a immutable pass and it will be managed by this
683 // top level manager. Set up analysis resolver to connect them.
684 PMDataManager *DM = getAsPMDataManager();
685 AnalysisResolver *AR = new AnalysisResolver(*DM);
686 P->setResolver(AR);
687 DM->initializeAnalysisImpl(P);
688 addImmutablePass(IP);
689 DM->recordAvailableAnalysis(IP);
690 return;
691 }
692
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000693 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000694 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000695 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000696 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
697 }
698
699 // Add the requested pass to the best available pass manager.
700 P->assignPassManager(activeStack, getTopLevelPassManagerType());
701
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000702 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000703 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000704 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000705 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
706 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000707}
708
709/// Find the pass that implements Analysis AID. Search immutable
710/// passes and all pass managers. If desired pass is not found
711/// then return NULL.
712Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000713 // For immutable passes we have a direct mapping from ID to pass, so check
714 // that first.
715 if (Pass *P = ImmutablePassMap.lookup(AID))
716 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000717
Devang Patelcd6ba152006-12-12 22:50:05 +0000718 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000719 for (PMDataManager *PassManager : PassManagers)
720 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000721 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000722
723 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000724 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
725 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000726 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000727
Craig Topperc6207612014-04-09 06:08:46 +0000728 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000729}
730
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000731const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
732 const PassInfo *&PI = AnalysisPassInfos[AID];
733 if (!PI)
734 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
735 else
736 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
737 "The pass info pointer changed for an analysis ID!");
738
739 return PI;
740}
741
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000742void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
743 P->initializePass();
744 ImmutablePasses.push_back(P);
745
746 // Add this pass to the map from its analysis ID. We clobber any prior runs
747 // of the pass in the map so that the last one added is the one found when
748 // doing lookups.
749 AnalysisID AID = P->getPassID();
750 ImmutablePassMap[AID] = P;
751
752 // Also add any interfaces implemented by the immutable pass to the map for
753 // fast lookup.
754 const PassInfo *PassInf = findAnalysisPassInfo(AID);
755 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000756 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000757 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
758}
759
Devang Pateleda56172006-12-12 23:34:33 +0000760// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000761void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000762
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000763 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000764 return;
765
Devang Pateleda56172006-12-12 23:34:33 +0000766 // Print out the immutable passes
767 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000768 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000769 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000770
Dan Gohmanf71c5212010-08-19 01:29:07 +0000771 // Every class that derives from PMDataManager also derives from Pass
772 // (sometimes indirectly), but there's no inheritance relationship
773 // between PMDataManager and Pass, so we have to getAsPass to get
774 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000775 for (PMDataManager *Manager : PassManagers)
776 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000777}
778
Devang Patel991aeba2006-12-15 20:13:01 +0000779void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000780
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000781 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000782 return;
783
David Greene994e1bb2010-01-05 01:30:02 +0000784 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000785 for (ImmutablePass *P : ImmutablePasses)
786 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000787 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000788 if (!PI->isAnalysisGroup())
789 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000790 }
Yaron Keren83009952016-04-28 14:49:44 +0000791 for (PMDataManager *PM : PassManagers)
792 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000793 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000794}
795
Devang Patele3068402006-12-21 00:16:50 +0000796void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000797 for (PMDataManager *PM : PassManagers)
798 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000799
Devang Patele3068402006-12-21 00:16:50 +0000800 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000801 for (PMDataManager *IPM : IndirectPassManagers)
802 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000803
Yaron Kerenbd873192016-10-02 19:21:41 +0000804 for (auto LU : LastUser) {
805 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
806 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000807 }
Devang Patele3068402006-12-21 00:16:50 +0000808}
809
Devang Patele7599552007-01-12 18:52:44 +0000810/// Destructor
811PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000812 for (PMDataManager *PM : PassManagers)
813 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000814
Yaron Keren83009952016-04-28 14:49:44 +0000815 for (ImmutablePass *P : ImmutablePasses)
816 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000817}
818
Devang Patelafb1f3622006-12-12 22:35:25 +0000819//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000820// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000821
Devang Patel643676c2006-11-11 01:10:19 +0000822/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000823void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000824 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000825
Chris Lattner60987362009-03-06 05:53:14 +0000826 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000827
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000828 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000829
Dan Gohmande6188a2010-08-12 23:50:08 +0000830 // This pass is the current implementation of all of the interfaces it
831 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000832 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000833 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000834 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000835 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000836 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000837}
838
Devang Patel9d9fc902007-03-06 17:52:53 +0000839// Return true if P preserves high level analysis used by other
840// passes managed by this manager
841bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000842 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000843 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000844 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000845
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000846 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000847 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000848 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000849 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000850 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000851 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000852
Devang Patel9d9fc902007-03-06 17:52:53 +0000853 return true;
854}
855
Chris Lattner02eb94c2008-08-07 07:34:50 +0000856/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000857void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000858 // Don't do this unless assertions are enabled.
859#ifdef NDEBUG
860 return;
861#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000862 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
863 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000864
Devang Patelef432532007-07-19 05:36:09 +0000865 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000866 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000867 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000868 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000869 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000870 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000871 }
872}
873
Devang Patel67c79a42008-07-01 19:50:56 +0000874/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000875void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000876 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
877 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000878 return;
879
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000880 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000881 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000882 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000883 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000884 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000885 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000886 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000887 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000888 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000889 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
890 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000891 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000892 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000893 }
Devang Patel349170f2006-11-11 01:24:55 +0000894 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000895
Devang Patel42dd1e92007-03-06 01:55:46 +0000896 // Check inherited analysis also. If P is not preserving analysis
897 // provided by parent manager then remove it here.
898 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
899
900 if (!InheritedAnalysis[Index])
901 continue;
902
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000903 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000904 I = InheritedAnalysis[Index]->begin(),
905 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000906 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000907 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000908 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000909 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000910 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000911 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000912 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
913 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000914 }
Devang Patel01919d22007-03-08 19:05:01 +0000915 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000916 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000917 }
918 }
Devang Patelf68a3492006-11-07 22:35:17 +0000919}
920
Devang Patelca189262006-11-14 03:05:08 +0000921/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000922void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000923 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000924
Devang Patel8adae862007-07-20 18:04:54 +0000925 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000926
Devang Patel2ff44922007-04-16 20:39:59 +0000927 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000928 if (!TPM)
929 return;
930
Devang Patel17ad0962006-12-08 00:37:52 +0000931 TPM->collectLastUses(DeadPasses, P);
932
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000933 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000934 dbgs() << " -*- '" << P->getPassName();
935 dbgs() << "' is the last user of following pass instances.";
936 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000937 }
938
Yaron Kerenbd873192016-10-02 19:21:41 +0000939 for (Pass *P : DeadPasses)
940 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000941}
Devang Patel200d3052006-12-13 23:50:44 +0000942
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000943void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000944 enum PassDebuggingString DBG_STR) {
945 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000946
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000947 {
948 // If the pass crashes releasing memory, remember this.
949 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000950 TimeRegion PassTimer(getPassTimer(P));
951
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000952 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000953 }
954
Owen Andersona7aed182010-08-06 18:33:48 +0000955 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000956 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000957 // Remove the pass itself (if it is not already removed).
958 AvailableAnalysis.erase(PI);
959
960 // Remove all interfaces this pass implements, for which it is also
961 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000962 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000963 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000964 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000965 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000966 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000967 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000968 }
Devang Patel17ad0962006-12-08 00:37:52 +0000969 }
Devang Patelca189262006-11-14 03:05:08 +0000970}
971
Dan Gohmande6188a2010-08-12 23:50:08 +0000972/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000973/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000974void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000975 // This manager is going to manage pass P. Set up analysis resolver
976 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000977 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000978 P->setResolver(AR);
979
Devang Patelec2b9a72007-03-05 22:57:49 +0000980 // If a FunctionPass F is the last user of ModulePass info M
981 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000982 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000983
Chris Lattner60987362009-03-06 05:53:14 +0000984 if (!ProcessAnalysis) {
985 // Add pass
986 PassVector.push_back(P);
987 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000988 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000989
Chris Lattner60987362009-03-06 05:53:14 +0000990 // At the moment, this pass is the last user of all required passes.
991 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +0000992 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +0000993 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
994
995 unsigned PDepth = this->getDepth();
996
Chandler Carruth44a13852015-08-19 03:02:12 +0000997 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
998 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +0000999 unsigned RDepth = 0;
1000
Chandler Carruth44a13852015-08-19 03:02:12 +00001001 assert(PUsed->getResolver() && "Analysis Resolver is not set");
1002 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +00001003 RDepth = DM.getDepth();
1004
1005 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +00001006 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001007 else if (PDepth > RDepth) {
1008 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +00001009 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +00001010 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +00001011 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001012 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001013 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001014 }
1015
1016 // Set P as P's last user until someone starts using P.
1017 // However, if P is a Pass Manager then it does not need
1018 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001019 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001020 LastUses.push_back(P);
1021 TPM->setLastUser(LastUses, P);
1022
1023 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001024 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001025 TPM->setLastUser(TransferLastUses, My_PM);
1026 TransferLastUses.clear();
1027 }
1028
Dan Gohman6304db32010-08-16 22:57:28 +00001029 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001030 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1031 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001032 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001033 this->addLowerLevelRequiredPass(P, AnalysisPass);
1034 }
1035
1036 // Take a note of analysis required and made available by this pass.
1037 // Remove the analysis not preserved by this pass
1038 removeNotPreservedAnalysis(P);
1039 recordAvailableAnalysis(P);
1040
Devang Patel8cad70d2006-11-11 01:51:02 +00001041 // Add pass
1042 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001043}
1044
Devang Patele64d3052007-04-16 20:12:57 +00001045
Chandler Carruth44a13852015-08-19 03:02:12 +00001046/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001047/// pass P and are available. Populate RP_NotAvail with analysis
1048/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001049void PMDataManager::collectRequiredAndUsedAnalyses(
1050 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1051 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001052 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001053
1054 for (const auto &UsedID : AnUsage->getUsedSet())
1055 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1056 UP.push_back(AnalysisPass);
1057
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001058 for (const auto &RequiredID : AnUsage->getRequiredSet())
1059 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001060 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001061 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001062 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001063
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001064 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1065 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001066 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001067 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001068 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001069}
1070
Devang Patel07f4f582006-11-14 21:49:36 +00001071// All Required analyses should be available to the pass as it runs! Here
1072// we fill in the AnalysisImpls member of the pass so that it can
1073// successfully use the getAnalysis() method to retrieve the
1074// implementations it needs.
1075//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001076void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001077 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1078
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001079 for (const AnalysisID ID : AnUsage->getRequiredSet()) {
1080 Pass *Impl = findAnalysisPass(ID, true);
Craig Topperc6207612014-04-09 06:08:46 +00001081 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001082 // This may be analysis pass that is initialized on the fly.
1083 // If that is not the case then it will raise an assert when it is used.
1084 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001085 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001086 assert(AR && "Analysis Resolver is not set");
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001087 AR->addAnalysisImplsPair(ID, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001088 }
1089}
1090
Devang Patel640c5bb2006-12-08 22:30:11 +00001091/// Find the pass that implements Analysis AID. If desired pass is not found
1092/// then return NULL.
1093Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1094
1095 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001096 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001097
1098 if (I != AvailableAnalysis.end())
1099 return I->second;
1100
1101 // Search Parents through TopLevelManager
1102 if (SearchParent)
1103 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001104
Craig Topperc6207612014-04-09 06:08:46 +00001105 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001106}
1107
Devang Patel991aeba2006-12-15 20:13:01 +00001108// Print list of passes that are last used by P.
1109void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1110
Devang Patel8adae862007-07-20 18:04:54 +00001111 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001112
1113 // If this is a on the fly manager then it does not have TPM.
1114 if (!TPM)
1115 return;
1116
Devang Patel991aeba2006-12-15 20:13:01 +00001117 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001118
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001119 for (Pass *P : LUses) {
Eric Christophera13839f2014-02-26 23:27:16 +00001120 dbgs() << "--" << std::string(Offset*2, ' ');
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001121 P->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001122 }
1123}
1124
1125void PMDataManager::dumpPassArguments() const {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001126 for (Pass *P : PassVector) {
1127 if (PMDataManager *PMD = P->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001128 PMD->dumpPassArguments();
1129 else
Owen Andersona7aed182010-08-06 18:33:48 +00001130 if (const PassInfo *PI =
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001131 TPM->findAnalysisPassInfo(P->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001132 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001133 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001134 }
1135}
1136
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001137void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1138 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001139 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001140 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001141 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001142 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001143 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001144 switch (S1) {
1145 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001146 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001147 break;
1148 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001149 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001150 break;
1151 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001152 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001153 break;
1154 default:
1155 break;
1156 }
1157 switch (S2) {
1158 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001159 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001160 break;
1161 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001162 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001163 break;
1164 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001165 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001166 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001167 case ON_REGION_MSG:
1168 dbgs() << "' on Region '" << Msg << "'...\n";
1169 break;
Devang Patel003a5592007-03-05 20:01:30 +00001170 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001171 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001172 break;
1173 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001174 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001175 break;
1176 default:
1177 break;
1178 }
Devang Patel991aeba2006-12-15 20:13:01 +00001179}
1180
Chris Lattner4c1e9542009-03-06 06:45:05 +00001181void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001182 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001183 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001184
Chris Lattner4c493d92008-08-08 15:14:09 +00001185 AnalysisUsage analysisUsage;
1186 P->getAnalysisUsage(analysisUsage);
1187 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1188}
1189
Chris Lattner4c1e9542009-03-06 06:45:05 +00001190void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001191 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001192 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001193
Chris Lattner4c493d92008-08-08 15:14:09 +00001194 AnalysisUsage analysisUsage;
1195 P->getAnalysisUsage(analysisUsage);
1196 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1197}
1198
Chandler Carruth44a13852015-08-19 03:02:12 +00001199void PMDataManager::dumpUsedSet(const Pass *P) const {
1200 if (PassDebugging < Details)
1201 return;
1202
1203 AnalysisUsage analysisUsage;
1204 P->getAnalysisUsage(analysisUsage);
1205 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1206}
1207
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001208void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001209 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001210 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001211 if (Set.empty())
1212 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001213 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001214 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001215 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001216 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001217 if (!PInf) {
1218 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1219 // all drivers.
1220 dbgs() << " Uninitialized Pass";
1221 continue;
1222 }
Owen Andersona7aed182010-08-06 18:33:48 +00001223 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001224 }
David Greene994e1bb2010-01-05 01:30:02 +00001225 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001226}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001227
Devang Patel004937b2007-07-27 20:06:09 +00001228/// Add RequiredPass into list of lower level passes required by pass P.
1229/// RequiredPass is run on the fly by Pass Manager when P requests it
1230/// through getAnalysis interface.
1231/// This should be handled by specific pass manager.
1232void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1233 if (TPM) {
1234 TPM->dumpArguments();
1235 TPM->dumpPasses();
1236 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001237
Dan Gohmande6188a2010-08-12 23:50:08 +00001238 // Module Level pass may required Function Level analysis info
1239 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1240 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001241 // module level pass is requiring lower level analysis info managed by
1242 // lower level pass manager.
1243
1244 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001245 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001246 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001247#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001248 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1249 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001250#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001251 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001252}
1253
Owen Andersona7aed182010-08-06 18:33:48 +00001254Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001255 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001256}
1257
Devang Patele7599552007-01-12 18:52:44 +00001258// Destructor
1259PMDataManager::~PMDataManager() {
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001260 for (Pass *P : PassVector)
1261 delete P;
Devang Patele7599552007-01-12 18:52:44 +00001262}
1263
Devang Patel9bdf7d42006-12-08 23:28:54 +00001264//===----------------------------------------------------------------------===//
1265// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001266// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1267Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001268 return PM.findAnalysisPass(ID, dir);
1269}
1270
Dan Gohmande6188a2010-08-12 23:50:08 +00001271Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001272 Function &F) {
1273 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1274}
1275
Devang Patela1514cb2006-12-07 19:39:39 +00001276//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001277// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001278
Dan Gohmande6188a2010-08-12 23:50:08 +00001279/// Execute all of the passes scheduled for execution by invoking
1280/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001281/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001282bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001283 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001284 return false;
1285
Devang Patele9585592006-12-08 01:38:28 +00001286 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001287
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001288 for (BasicBlock &BB : F)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001289 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1290 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001291 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001292
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001293 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001294 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001295
Devang Patelabfbe3b2006-12-16 00:56:26 +00001296 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001297
Chris Lattner4c1e9542009-03-06 06:45:05 +00001298 {
1299 // If the pass crashes, remember this.
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001300 PassManagerPrettyStackEntry X(BP, BB);
Chris Lattner707431c2010-03-30 04:03:22 +00001301 TimeRegion PassTimer(getPassTimer(BP));
1302
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001303 LocalChanged |= BP->runOnBasicBlock(BB);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001304 }
Devang Patel93a197c2006-12-14 00:08:04 +00001305
Dan Gohman74b189f2010-03-01 17:34:28 +00001306 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001307 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001308 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001309 BB.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001310 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001311 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001312
Devang Patela273d1c2007-07-19 18:02:32 +00001313 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001314 removeNotPreservedAnalysis(BP);
1315 recordAvailableAnalysis(BP);
Florian Hahn03c3a1a2017-07-13 10:52:00 +00001316 removeDeadPasses(BP, BB.getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001317 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001318
Bill Wendling6ce6d262009-12-25 13:50:18 +00001319 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001320}
1321
Devang Patel475c4532006-12-08 00:59:05 +00001322// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001323bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001324 bool Changed = false;
1325
Chris Lattner4c1e9542009-03-06 06:45:05 +00001326 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1327 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001328
1329 return Changed;
1330}
1331
Duncan Sands51495602009-02-13 09:42:34 +00001332bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001333 bool Changed = false;
1334
Pedro Artigas41b98842012-12-05 17:12:22 +00001335 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001336 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001337
1338 return Changed;
1339}
1340
Duncan Sands51495602009-02-13 09:42:34 +00001341bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001342 bool Changed = false;
1343
Devang Patelabfbe3b2006-12-16 00:56:26 +00001344 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1345 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001346 Changed |= BP->doInitialization(F);
1347 }
1348
1349 return Changed;
1350}
1351
Duncan Sands51495602009-02-13 09:42:34 +00001352bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001353 bool Changed = false;
1354
Devang Patelabfbe3b2006-12-16 00:56:26 +00001355 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1356 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001357 Changed |= BP->doFinalization(F);
1358 }
1359
1360 return Changed;
1361}
1362
1363
Devang Patela1514cb2006-12-07 19:39:39 +00001364//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001365// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001366
Devang Patel4e12f862006-11-08 10:44:40 +00001367/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001368FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001369 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001370 // FPM is the top level manager.
1371 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001372
Dan Gohman565df952008-03-13 02:08:36 +00001373 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001374 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001375}
1376
Devang Patelb67904d2006-12-13 02:36:01 +00001377FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001378 delete FPM;
1379}
1380
Dan Gohmande6188a2010-08-12 23:50:08 +00001381void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001382 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001383}
1384
Devang Patel9f3083e2006-11-15 19:39:54 +00001385/// run - Execute all of the passes scheduled for execution. Keep
1386/// track of whether any of the passes modifies the function, and if
1387/// so, return true.
1388///
Devang Patelb67904d2006-12-13 02:36:01 +00001389bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001390 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1391 report_fatal_error("Error reading bitcode file: " + EIB.message());
1392 });
Devang Patel272908d2006-12-08 22:57:48 +00001393 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001394}
1395
1396
Devang Patelff631ae2006-11-15 01:27:05 +00001397/// doInitialization - Run all of the initializers for the function passes.
1398///
Devang Patelb67904d2006-12-13 02:36:01 +00001399bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001400 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001401}
1402
Dan Gohmane6656eb2007-07-30 14:51:13 +00001403/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001404///
Devang Patelb67904d2006-12-13 02:36:01 +00001405bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001406 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001407}
1408
Devang Patela1514cb2006-12-07 19:39:39 +00001409//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001410// FunctionPassManagerImpl implementation
1411//
Duncan Sands51495602009-02-13 09:42:34 +00001412bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001413 bool Changed = false;
1414
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001415 dumpArguments();
1416 dumpPasses();
1417
Yaron Keren4849aa32015-06-05 17:48:47 +00001418 for (ImmutablePass *ImPass : getImmutablePasses())
1419 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001420
Chris Lattner4c1e9542009-03-06 06:45:05 +00001421 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1422 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001423
1424 return Changed;
1425}
1426
Duncan Sands51495602009-02-13 09:42:34 +00001427bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001428 bool Changed = false;
1429
Pedro Artigas41b98842012-12-05 17:12:22 +00001430 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001431 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001432
Yaron Keren4849aa32015-06-05 17:48:47 +00001433 for (ImmutablePass *ImPass : getImmutablePasses())
1434 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001435
Devang Patel67d6a5e2006-12-19 19:46:59 +00001436 return Changed;
1437}
1438
Devang Patelec9c58f2009-04-01 22:34:41 +00001439/// cleanup - After running all passes, clean up pass manager cache.
1440void FPPassManager::cleanup() {
1441 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1442 FunctionPass *FP = getContainedPass(Index);
1443 AnalysisResolver *AR = FP->getResolver();
1444 assert(AR && "Analysis Resolver is not set");
1445 AR->clearAnalysisImpls();
1446 }
1447}
1448
Torok Edwin24c78352009-06-29 18:49:09 +00001449void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1450 if (!wasRun)
1451 return;
1452 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1453 FPPassManager *FPPM = getContainedManager(Index);
1454 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1455 FPPM->getContainedPass(Index)->releaseMemory();
1456 }
1457 }
Torok Edwin896556e2009-06-29 21:05:10 +00001458 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001459}
1460
Devang Patel67d6a5e2006-12-19 19:46:59 +00001461// Execute all the passes managed by this top level manager.
1462// Return true if any function is modified by a pass.
1463bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001464 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001465 TimingInfo::createTheTimeInfo();
1466
Devang Patele3068402006-12-21 00:16:50 +00001467 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001468 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001469 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001470 F.getContext().yield();
1471 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001472
1473 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1474 getContainedManager(Index)->cleanup();
1475
Torok Edwin24c78352009-06-29 18:49:09 +00001476 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001477 return Changed;
1478}
1479
1480//===----------------------------------------------------------------------===//
1481// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001482
Devang Patel8c78a0b2007-05-03 01:11:54 +00001483char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001484/// Print passes managed by this manager
1485void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001486 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001487 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1488 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001489 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001490 dumpLastUses(FP, Offset+1);
1491 }
1492}
1493
1494
Dan Gohmande6188a2010-08-12 23:50:08 +00001495/// Execute all of the passes scheduled for execution by invoking
1496/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001497/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001498bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001499 if (F.isDeclaration())
1500 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001501
1502 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001503
Devang Patelcbbf2912008-03-20 01:09:53 +00001504 // Collect inherited analysis from Module level pass manager.
1505 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001506
Devang Patelabfbe3b2006-12-16 00:56:26 +00001507 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1508 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001509 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001510
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001511 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001512 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001513
Devang Patelabfbe3b2006-12-16 00:56:26 +00001514 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001515
Chris Lattner4c1e9542009-03-06 06:45:05 +00001516 {
1517 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001518 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001519
Dan Gohman74b189f2010-03-01 17:34:28 +00001520 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001521 }
Devang Patel93a197c2006-12-14 00:08:04 +00001522
Dan Gohman74b189f2010-03-01 17:34:28 +00001523 Changed |= LocalChanged;
1524 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001525 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001526 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001527 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001528
Devang Patela273d1c2007-07-19 18:02:32 +00001529 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001530 removeNotPreservedAnalysis(FP);
1531 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001532 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001533 }
1534 return Changed;
1535}
1536
Devang Patel67d6a5e2006-12-19 19:46:59 +00001537bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001538 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001539
Serge Pavloved5eb932017-01-15 10:23:18 +00001540 for (Function &F : M)
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001541 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001542
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001543 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001544}
1545
Duncan Sands51495602009-02-13 09:42:34 +00001546bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001547 bool Changed = false;
1548
Chris Lattner4c1e9542009-03-06 06:45:05 +00001549 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1550 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001551
Devang Patelff631ae2006-11-15 01:27:05 +00001552 return Changed;
1553}
1554
Duncan Sands51495602009-02-13 09:42:34 +00001555bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001556 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001557
1558 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001559 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001560
Devang Patelff631ae2006-11-15 01:27:05 +00001561 return Changed;
1562}
1563
Devang Patela1514cb2006-12-07 19:39:39 +00001564//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001565// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001566
Dan Gohmande6188a2010-08-12 23:50:08 +00001567/// Execute all of the passes scheduled for execution by invoking
1568/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001569/// the module, and if so, return true.
1570bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001571MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001572 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001573
Torok Edwin24c78352009-06-29 18:49:09 +00001574 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001575 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1576 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001577 Changed |= FPP->doInitialization(M);
1578 }
1579
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001580 // Initialize module passes
1581 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1582 Changed |= getContainedPass(Index)->doInitialization(M);
1583
Devang Patelabfbe3b2006-12-16 00:56:26 +00001584 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1585 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001586 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001587
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001588 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001589 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001590
Devang Patelabfbe3b2006-12-16 00:56:26 +00001591 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001592
Chris Lattner4c1e9542009-03-06 06:45:05 +00001593 {
1594 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001595 TimeRegion PassTimer(getPassTimer(MP));
1596
Dan Gohman74b189f2010-03-01 17:34:28 +00001597 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001598 }
Devang Patel93a197c2006-12-14 00:08:04 +00001599
Dan Gohman74b189f2010-03-01 17:34:28 +00001600 Changed |= LocalChanged;
1601 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001602 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001603 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001604 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001605 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001606
Devang Patela273d1c2007-07-19 18:02:32 +00001607 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001608 removeNotPreservedAnalysis(MP);
1609 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001610 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001611 }
Torok Edwin24c78352009-06-29 18:49:09 +00001612
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001613 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001614 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001615 Changed |= getContainedPass(Index)->doFinalization(M);
1616
Torok Edwin24c78352009-06-29 18:49:09 +00001617 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001618 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1619 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001620 // We don't know when is the last time an on-the-fly pass is run,
1621 // so we need to releaseMemory / finalize here
1622 FPP->releaseMemoryOnTheFly();
1623 Changed |= FPP->doFinalization(M);
1624 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001625
Devang Patel05e1a972006-11-07 22:03:15 +00001626 return Changed;
1627}
1628
Devang Patele64d3052007-04-16 20:12:57 +00001629/// Add RequiredPass into list of lower level passes required by pass P.
1630/// RequiredPass is run on the fly by Pass Manager when P requests it
1631/// through getAnalysis interface.
1632void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001633 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1634 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001635 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001636 RequiredPass->getPotentialPassManagerType()) &&
1637 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001638 if (!RequiredPass)
1639 return;
Devang Patele64d3052007-04-16 20:12:57 +00001640
Devang Patel68f72b12007-04-26 17:50:19 +00001641 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001642 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001643 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001644 // FPP is the top level manager.
1645 FPP->setTopLevelManager(FPP);
1646
Devang Patel69e9f6d2007-04-16 20:27:05 +00001647 OnTheFlyManagers[P] = FPP;
1648 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001649 const PassInfo *RequiredPassPI =
1650 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001651
Craig Topperc6207612014-04-09 06:08:46 +00001652 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001653 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1654 FoundPass =
1655 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001656 }
Andrew Trick02066f22014-04-08 03:40:34 +00001657 if (!FoundPass) {
1658 FoundPass = RequiredPass;
1659 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001660 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001661 FPP->add(RequiredPass);
1662 }
1663 // Register P as the last user of FoundPass or RequiredPass.
1664 SmallVector<Pass *, 1> LU;
1665 LU.push_back(FoundPass);
1666 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001667}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001668
Dan Gohmande6188a2010-08-12 23:50:08 +00001669/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001670/// required by module pass MP. Instantiate analysis pass, by using
1671/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001672Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001673 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001674 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001675
Torok Edwin24c78352009-06-29 18:49:09 +00001676 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001677 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001678 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001679}
1680
1681
Devang Patela1514cb2006-12-07 19:39:39 +00001682//===----------------------------------------------------------------------===//
1683// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001684
Devang Patelab97cf42006-12-13 00:09:23 +00001685//
Devang Patelc290c8a2006-11-07 22:23:34 +00001686/// run - Execute all of the passes scheduled for execution. Keep track of
1687/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001688bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001689 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001690 TimingInfo::createTheTimeInfo();
1691
Devang Patelcfd70c42006-12-13 22:10:00 +00001692 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001693 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001694
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001695 for (ImmutablePass *ImPass : getImmutablePasses())
1696 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001697
Devang Patele3068402006-12-21 00:16:50 +00001698 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001699 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Serge Pavloved5eb932017-01-15 10:23:18 +00001700 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001701 M.getContext().yield();
1702 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001703
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001704 for (ImmutablePass *ImPass : getImmutablePasses())
1705 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001706
Devang Patelc290c8a2006-11-07 22:23:34 +00001707 return Changed;
1708}
Devang Patel376fefa2006-11-08 10:29:57 +00001709
Devang Patela1514cb2006-12-07 19:39:39 +00001710//===----------------------------------------------------------------------===//
1711// PassManager implementation
1712
Devang Patel376fefa2006-11-08 10:29:57 +00001713/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001714PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001715 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001716 // PM is the top level manager
1717 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001718}
1719
Devang Patelb67904d2006-12-13 02:36:01 +00001720PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001721 delete PM;
1722}
1723
Chris Lattner60987362009-03-06 05:53:14 +00001724void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001725 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001726}
1727
1728/// run - Execute all of the passes scheduled for execution. Keep track of
1729/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001730bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001731 return PM->run(M);
1732}
1733
Devang Patelb8817b92006-12-14 00:59:42 +00001734//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001735// TimingInfo implementation
1736
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001737bool llvm::TimePassesIsEnabled = false;
Zachary Turner8065f0b2017-12-01 00:53:10 +00001738static cl::opt<bool, true> EnableTiming(
1739 "time-passes", cl::location(TimePassesIsEnabled), cl::Hidden,
1740 cl::desc("Time each pass, printing elapsed time for each on exit"));
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001741
Devang Patelb8817b92006-12-14 00:59:42 +00001742// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001743// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001744// null. It may be called multiple times.
1745void TimingInfo::createTheTimeInfo() {
1746 if (!TimePassesIsEnabled || TheTimeInfo) return;
1747
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001748 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001749 // This guarantees that the object will be constructed before static globals,
1750 // thus it will be destroyed before them.
1751 static ManagedStatic<TimingInfo> TTI;
1752 TheTimeInfo = &*TTI;
1753}
1754
Devang Patel1c3633e2007-01-29 23:10:37 +00001755/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001756Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001757 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001758 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001759 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001760}
1761
James Henderson852f6fd2017-05-16 09:43:21 +00001762/// If timing is enabled, report the times collected up to now and then reset
1763/// them.
1764void llvm::reportAndResetTimings() {
1765 if (TheTimeInfo)
1766 TheTimeInfo->print();
1767}
1768
Devang Patel1c56a632007-01-08 19:29:38 +00001769//===----------------------------------------------------------------------===//
1770// PMStack implementation
1771//
Devang Patelad98d232007-01-11 22:15:30 +00001772
Devang Patel1c56a632007-01-08 19:29:38 +00001773// Pop Pass Manager from the stack and clear its analysis info.
1774void PMStack::pop() {
1775
1776 PMDataManager *Top = this->top();
1777 Top->initializeAnalysisInfo();
1778
1779 S.pop_back();
1780}
1781
1782// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001783void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001784 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001785 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001786
Chris Lattner60987362009-03-06 05:53:14 +00001787 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001788 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1789 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001790 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001791
Chris Lattner60987362009-03-06 05:53:14 +00001792 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001793 TPM->addIndirectPassManager(PM);
1794 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001795 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001796 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001797 assert((PM->getPassManagerType() == PMT_ModulePassManager
1798 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001799 && "pushing bad pass manager to PMStack");
1800 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001801 }
1802
Devang Patel15701b52007-01-11 00:19:00 +00001803 S.push_back(PM);
1804}
1805
1806// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001807LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001808 for (PMDataManager *Manager : S)
1809 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001810
Devang Patel15701b52007-01-11 00:19:00 +00001811 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001812 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001813}
1814
Devang Patel1c56a632007-01-08 19:29:38 +00001815/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001816/// add self into that manager.
1817void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001818 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001819 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001820 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001821 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1822 if (TopPMType == PreferredType)
1823 break; // We found desired pass manager
1824 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001825 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001826 else
1827 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001828 }
Devang Patel18ff6362008-09-09 21:38:40 +00001829 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001830 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001831}
1832
Devang Patel3312f752007-01-16 21:43:18 +00001833/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001834/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001835void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001836 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001837
Andrew Trickcbc845f2012-02-01 07:16:20 +00001838 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001839 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001840 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1841 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001842 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001843 break;
Devang Patel3312f752007-01-16 21:43:18 +00001844 }
Devang Patel3312f752007-01-16 21:43:18 +00001845
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001846 // Create new Function Pass Manager if needed.
1847 FPPassManager *FPP;
1848 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1849 FPP = (FPPassManager *)PMS.top();
1850 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001851 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1852 PMDataManager *PMD = PMS.top();
1853
1854 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001855 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001856 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001857
1858 // [2] Set up new manager's top level manager
1859 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1860 TPM->addIndirectPassManager(FPP);
1861
1862 // [3] Assign manager to manage this new manager. This may create
1863 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001864 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001865
1866 // [4] Push new manager into PMS
1867 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001868 }
1869
Devang Patel3312f752007-01-16 21:43:18 +00001870 // Assign FPP as the manager of this pass.
1871 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001872}
1873
Devang Patel3312f752007-01-16 21:43:18 +00001874/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001875/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001876void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001877 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001878 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001879
Devang Patel15701b52007-01-11 00:19:00 +00001880 // Basic Pass Manager is a leaf pass manager. It does not handle
1881 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001882 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001883 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1884 BBP = (BBPassManager *)PMS.top();
1885 } else {
1886 // If leaf manager is not Basic Block Pass manager then create new
1887 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001888 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1889 PMDataManager *PMD = PMS.top();
1890
1891 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001892 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001893
1894 // [2] Set up new manager's top level manager
1895 // Basic Block Pass Manager does not live by itself
1896 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1897 TPM->addIndirectPassManager(BBP);
1898
Devang Patel15701b52007-01-11 00:19:00 +00001899 // [3] Assign manager to manage this new manager. This may create
1900 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001901 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001902
Devang Patel3312f752007-01-16 21:43:18 +00001903 // [4] Push new manager into PMS
1904 PMS.push(BBP);
1905 }
Devang Patel1c56a632007-01-08 19:29:38 +00001906
Devang Patel3312f752007-01-16 21:43:18 +00001907 // Assign BBP as the manager of this pass.
1908 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001909}
1910
Dan Gohmand3a20c92008-03-11 16:41:42 +00001911PassManagerBase::~PassManagerBase() {}