blob: d5b2ef6d03b9608630df2f061a4e4ec79f8ced1b [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"
Pavel Labathec534e62016-10-25 16:20:07 +000015#include "llvm/IR/IRPrintingPasses.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000017#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000018#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/IR/Module.h"
Pavel Labathec534e62016-10-25 16:20:07 +000020#include "llvm/Support/Chrono.h"
Devang Patelf1567a52006-12-13 20:03:48 +000021#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000022#include "llvm/Support/Debug.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000023#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000024#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000025#include "llvm/Support/Mutex.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Support/Timer.h"
27#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000028#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000029#include <map>
Weiming Zhao0f1762c2016-01-06 22:55:03 +000030#include <unordered_set>
Dan Gohman8c43e412007-10-03 19:04:09 +000031using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000032using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000033
Devang Patele7599552007-01-12 18:52:44 +000034// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000035
Devang Patelf1567a52006-12-13 20:03:48 +000036//===----------------------------------------------------------------------===//
37// Pass debugging information. Often it is useful to find out what pass is
38// running when a crash occurs in a utility. When this library is compiled with
39// debugging on, a command line option (--debug-pass) is enabled that causes the
40// pass name to be printed before it executes.
41//
42
Chandler Carruth7caea412013-11-09 12:26:54 +000043namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000044// Different debug levels that can be enabled...
45enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000046 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000047};
Chandler Carruth7caea412013-11-09 12:26:54 +000048}
Devang Patel03fb5872006-12-13 21:13:31 +000049
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000050static cl::opt<enum PassDebugLevel>
51PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000052 cl::desc("Print PassManager debugging information"),
53 cl::values(
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000054 clEnumVal(Disabled , "disable debug output"),
55 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
56 clEnumVal(Structure , "print pass structure before run()"),
57 clEnumVal(Executions, "print pass name before it is executed"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000058 clEnumVal(Details , "print pass details when it is executed")));
David Greene9b063df2010-04-02 23:17:14 +000059
Chandler Carruth7caea412013-11-09 12:26:54 +000060namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000061typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
62PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000063}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000064
65// Print IR out before/after specified passes.
66static PassOptionList
67PrintBefore("print-before",
68 llvm::cl::desc("Print IR before specified passes"),
69 cl::Hidden);
70
71static PassOptionList
72PrintAfter("print-after",
73 llvm::cl::desc("Print IR after specified passes"),
74 cl::Hidden);
75
76static cl::opt<bool>
77PrintBeforeAll("print-before-all",
78 llvm::cl::desc("Print IR before each pass"),
79 cl::init(false));
80static cl::opt<bool>
81PrintAfterAll("print-after-all",
82 llvm::cl::desc("Print IR after each pass"),
83 cl::init(false));
84
Weiming Zhao0f1762c2016-01-06 22:55:03 +000085static cl::list<std::string>
86 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
87 cl::desc("Only print IR for functions whose name "
88 "match this for all print-[before|after][-all] "
89 "options"),
90 cl::CommaSeparated);
91
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000092/// This is a helper to determine whether to print IR before or
93/// after a pass.
94
95static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
96 PassOptionList &PassesToPrint) {
Chris Bieneman664294c2015-04-29 21:45:22 +000097 for (auto *PassInf : PassesToPrint) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000098 if (PassInf)
99 if (PassInf->getPassArgument() == PI->getPassArgument()) {
100 return true;
101 }
David Greene9b063df2010-04-02 23:17:14 +0000102 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000103 return false;
104}
Dan Gohmande6188a2010-08-12 23:50:08 +0000105
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000106/// This is a utility to check whether a pass should have IR dumped
107/// before it.
108static bool ShouldPrintBeforePass(const PassInfo *PI) {
109 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
110}
David Greene9b063df2010-04-02 23:17:14 +0000111
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000112/// This is a utility to check whether a pass should have IR dumped
113/// after it.
114static bool ShouldPrintAfterPass(const PassInfo *PI) {
115 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000116}
117
Weiming Zhao0f1762c2016-01-06 22:55:03 +0000118bool llvm::isFunctionInPrintList(StringRef FunctionName) {
119 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
120 PrintFuncsList.end());
121 return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
122}
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000123/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
124/// or higher is specified.
125bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000126 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000127}
128
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000129
130
131
Chris Lattner4c1e9542009-03-06 06:45:05 +0000132void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000133 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000134 OS << "Releasing pass '";
135 else
136 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000137
Chris Lattner4c1e9542009-03-06 06:45:05 +0000138 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000139
Chris Lattner4c1e9542009-03-06 06:45:05 +0000140 if (M) {
141 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
142 return;
143 }
Craig Topperc6207612014-04-09 06:08:46 +0000144 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000145 OS << '\n';
146 return;
147 }
148
Dan Gohman79fc0e92009-03-10 18:47:59 +0000149 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000150 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000151 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000152 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000153 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000154 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000155 OS << "value";
156
157 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000158 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000159 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000160}
161
162
Devang Patelffca9102006-12-15 19:39:30 +0000163namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000164//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000165// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000166//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000167/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000168/// pass together and sequence them to process one basic block before
169/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000170class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000171
172public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000173 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000174 explicit BBPassManager()
175 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000176
Devang Patelca58e352006-11-08 10:05:38 +0000177 /// Execute all of the passes scheduled for execution. Keep track of
178 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000179 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000180
Devang Patelf9d96b92006-12-07 19:57:52 +0000181 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000182 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000183 Info.setPreservesAll();
184 }
185
Craig Topperf398d7c2014-03-05 06:35:38 +0000186 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000187 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000188 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000189 bool doFinalization(Function &F);
190
Craig Topperf398d7c2014-03-05 06:35:38 +0000191 PMDataManager *getAsPMDataManager() override { return this; }
192 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000193
Mehdi Amini117296c2016-10-01 02:56:57 +0000194 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000195
Devang Pateleda56172006-12-12 23:34:33 +0000196 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000197 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000198 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000199 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
200 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000201 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000202 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000203 }
204 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000205
206 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000207 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000208 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
209 return BP;
210 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000211
Craig Topperf398d7c2014-03-05 06:35:38 +0000212 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000213 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000214 }
Devang Patelca58e352006-11-08 10:05:38 +0000215};
216
Devang Patel8c78a0b2007-05-03 01:11:54 +0000217char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000218} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000219
Devang Patele7599552007-01-12 18:52:44 +0000220namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000221namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000222//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000223// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000224//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000225/// FunctionPassManagerImpl manages FPPassManagers
226class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000227 public PMDataManager,
228 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000229 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000230private:
231 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000232public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000233 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000234 explicit FunctionPassManagerImpl() :
235 Pass(PT_PassManager, ID), PMDataManager(),
236 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000237
Matthias Braun0880c602014-12-12 01:27:01 +0000238 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000239 void add(Pass *P) {
240 schedulePass(P);
241 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000242
243 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000244 Pass *createPrinterPass(raw_ostream &O,
245 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000246 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000247 }
248
Torok Edwin24c78352009-06-29 18:49:09 +0000249 // Prepare for running an on the fly pass, freeing memory if needed
250 // from a previous run.
251 void releaseMemoryOnTheFly();
252
Devang Patel67d6a5e2006-12-19 19:46:59 +0000253 /// run - Execute all of the passes scheduled for execution. Keep track of
254 /// whether any of the passes modifies the module, and if so, return true.
255 bool run(Function &F);
256
257 /// doInitialization - Run all of the initializers for the function passes.
258 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000259 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000260
Dan Gohmane6656eb2007-07-30 14:51:13 +0000261 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000262 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000263 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000264
Dan Gohmande6188a2010-08-12 23:50:08 +0000265
Craig Topperf398d7c2014-03-05 06:35:38 +0000266 PMDataManager *getAsPMDataManager() override { return this; }
267 Pass *getAsPass() override { return this; }
268 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000269 return PMT_FunctionPassManager;
270 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000271
Devang Patel67d6a5e2006-12-19 19:46:59 +0000272 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000273 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000274 Info.setPreservesAll();
275 }
276
Devang Patel67d6a5e2006-12-19 19:46:59 +0000277 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000278 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000279 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
280 return FP;
281 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000282};
283
David Blaikiea379b1812011-12-20 02:50:00 +0000284void FunctionPassManagerImpl::anchor() {}
285
Devang Patel8c78a0b2007-05-03 01:11:54 +0000286char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000287} // End of legacy namespace
288} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000289
Chandler Carruth7caea412013-11-09 12:26:54 +0000290namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000291//===----------------------------------------------------------------------===//
292// MPPassManager
293//
294/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000295/// It batches all Module passes and function pass managers together and
296/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000297class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000298public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000299 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000300 explicit MPPassManager() :
301 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000302
303 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000304 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000305 for (auto &OnTheFlyManager : OnTheFlyManagers) {
306 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000307 delete FPP;
308 }
309 }
310
Dan Gohmande6188a2010-08-12 23:50:08 +0000311 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000312 Pass *createPrinterPass(raw_ostream &O,
313 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000314 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000315 }
316
Devang Patelca58e352006-11-08 10:05:38 +0000317 /// run - Execute all of the passes scheduled for execution. Keep track of
318 /// whether any of the passes modifies the module, and if so, return true.
319 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000320
Pedro Artigase4348b02012-12-03 21:56:57 +0000321 using llvm::Pass::doInitialization;
322 using llvm::Pass::doFinalization;
323
Devang Patelf9d96b92006-12-07 19:57:52 +0000324 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000325 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000326 Info.setPreservesAll();
327 }
328
Devang Patele64d3052007-04-16 20:12:57 +0000329 /// Add RequiredPass into list of lower level passes required by pass P.
330 /// RequiredPass is run on the fly by Pass Manager when P requests it
331 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000332 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000333
Dan Gohmande6188a2010-08-12 23:50:08 +0000334 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000335 /// required by module pass MP. Instantiate analysis pass, by using
336 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000337 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000338
Mehdi Amini117296c2016-10-01 02:56:57 +0000339 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000340
Craig Topperf398d7c2014-03-05 06:35:38 +0000341 PMDataManager *getAsPMDataManager() override { return this; }
342 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000343
Devang Pateleda56172006-12-12 23:34:33 +0000344 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000345 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000346 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000347 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
348 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000349 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000350 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
351 OnTheFlyManagers.find(MP);
352 if (I != OnTheFlyManagers.end())
353 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000354 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000355 }
356 }
357
Devang Patelabfbe3b2006-12-16 00:56:26 +0000358 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000359 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000360 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000361 }
362
Craig Topperf398d7c2014-03-05 06:35:38 +0000363 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000364 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000365 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000366
367 private:
368 /// Collection of on the fly FPPassManagers. These managers manage
369 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000370 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000371};
372
Devang Patel8c78a0b2007-05-03 01:11:54 +0000373char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000374} // End anonymous namespace
375
376namespace llvm {
377namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000378//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000379// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000380//
Devang Patel09f162c2007-05-01 21:15:47 +0000381
Devang Patel67d6a5e2006-12-19 19:46:59 +0000382/// PassManagerImpl manages MPPassManagers
383class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000384 public PMDataManager,
385 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000386 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000387
388public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000389 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000390 explicit PassManagerImpl() :
391 Pass(PT_PassManager, ID), PMDataManager(),
392 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000393
Matthias Braun0880c602014-12-12 01:27:01 +0000394 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000395 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000396 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000397 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000398
399 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000400 Pass *createPrinterPass(raw_ostream &O,
401 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000402 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000403 }
404
Devang Patel376fefa2006-11-08 10:29:57 +0000405 /// run - Execute all of the passes scheduled for execution. Keep track of
406 /// whether any of the passes modifies the module, and if so, return true.
407 bool run(Module &M);
408
Pedro Artigase4348b02012-12-03 21:56:57 +0000409 using llvm::Pass::doInitialization;
410 using llvm::Pass::doFinalization;
411
Devang Patelf9d96b92006-12-07 19:57:52 +0000412 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000413 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000414 Info.setPreservesAll();
415 }
416
Craig Topperf398d7c2014-03-05 06:35:38 +0000417 PMDataManager *getAsPMDataManager() override { return this; }
418 Pass *getAsPass() override { return this; }
419 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000420 return PMT_ModulePassManager;
421 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000422
Devang Patel67d6a5e2006-12-19 19:46:59 +0000423 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000424 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000425 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
426 return MP;
427 }
Devang Patel376fefa2006-11-08 10:29:57 +0000428};
429
David Blaikiea379b1812011-12-20 02:50:00 +0000430void PassManagerImpl::anchor() {}
431
Devang Patel8c78a0b2007-05-03 01:11:54 +0000432char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000433} // End of legacy namespace
434} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000435
436namespace {
437
438//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000439/// TimingInfo Class - This class is used to calculate information about the
440/// amount of time each pass takes to execute. This only happens when
441/// -time-passes is enabled on the command line.
442///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000443
Owen Anderson5a6960f2009-06-18 20:51:00 +0000444static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000445
Nick Lewycky02d5f772009-10-25 06:33:48 +0000446class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000447 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000448 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000449public:
450 // Use 'create' member to get this.
451 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000452
Devang Patel1c3633e2007-01-29 23:10:37 +0000453 // TimingDtor - Print out information about timing information
454 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000455 // Delete all of the timers, which accumulate their info into the
456 // TimerGroup.
Yaron Keren4849aa32015-06-05 17:48:47 +0000457 for (auto &I : TimingData)
458 delete I.second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000459 // TimerGroup is deleted next, printing the report.
460 }
461
462 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000463 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000464 // null. It may be called multiple times.
465 static void createTheTimeInfo();
466
Chris Lattner707431c2010-03-30 04:03:22 +0000467 /// getPassTimer - Return the timer for the specified pass if it exists.
468 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000469 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000470 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000471
Owen Anderson5c96ef72009-07-07 18:33:04 +0000472 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000473 Timer *&T = TimingData[P];
Craig Topperc6207612014-04-09 06:08:46 +0000474 if (!T)
Chris Lattner707431c2010-03-30 04:03:22 +0000475 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000476 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000477 }
478};
479
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000480} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000481
Dan Gohmand78c4002008-05-13 00:00:25 +0000482static TimingInfo *TheTimeInfo;
483
Devang Patela1514cb2006-12-07 19:39:39 +0000484//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000485// PMTopLevelManager implementation
486
Devang Patel4268fc02007-01-16 02:00:38 +0000487/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000488PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
489 PMDM->setTopLevelManager(this);
490 addPassManager(PMDM);
491 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000492}
493
Devang Patelafb1f3622006-12-12 22:35:25 +0000494/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000495void
Bill Wendlingea857e12012-05-14 07:53:40 +0000496PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000497 unsigned PDepth = 0;
498 if (P->getResolver())
499 PDepth = P->getResolver()->getPMDataManager().getDepth();
500
Yaron Keren4849aa32015-06-05 17:48:47 +0000501 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000502 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000503
Devang Patel01919d22007-03-08 19:05:01 +0000504 if (P == AP)
505 continue;
506
Tobias Grosserf07426b2011-01-20 21:03:22 +0000507 // Update the last users of passes that are required transitive by AP.
508 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
509 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
510 SmallVector<Pass *, 12> LastUses;
511 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000512 for (AnalysisID ID : IDs) {
513 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000514 assert(AnalysisPass && "Expected analysis pass to exist.");
515 AnalysisResolver *AR = AnalysisPass->getResolver();
516 assert(AR && "Expected analysis resolver to exist.");
517 unsigned APDepth = AR->getPMDataManager().getDepth();
518
519 if (PDepth == APDepth)
520 LastUses.push_back(AnalysisPass);
521 else if (PDepth > APDepth)
522 LastPMUses.push_back(AnalysisPass);
523 }
524
525 setLastUser(LastUses, P);
526
527 // If this pass has a corresponding pass manager, push higher level
528 // analysis to this pass manager.
529 if (P->getResolver())
530 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
531
532
Devang Patelafb1f3622006-12-12 22:35:25 +0000533 // If AP is the last user of other passes then make P last user of
534 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000535 for (auto LU : LastUser) {
536 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000537 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000538 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000539 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000540 }
541 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000542}
543
544/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000545void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000546 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000547 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000548 InversedLastUser.find(P);
549 if (DMI == InversedLastUser.end())
550 return;
551
552 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000553 for (Pass *LUP : LU) {
554 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000555 }
556
Devang Patelafb1f3622006-12-12 22:35:25 +0000557}
558
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000559AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000560 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000561 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000562 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000563 AnUsage = DMI->second;
564 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000565 // Look up the analysis usage from the pass instance (different instances
566 // of the same pass can produce different results), but unique the
567 // resulting object to reduce memory usage. This helps to greatly reduce
568 // memory usage when we have many instances of only a few pass types
569 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
570 // of dependencies.
571 AnalysisUsage AU;
572 P->getAnalysisUsage(AU);
573
574 AUFoldingSetNode* Node = nullptr;
575 FoldingSetNodeID ID;
576 AUFoldingSetNode::Profile(ID, AU);
577 void *IP = nullptr;
578 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
579 Node = N;
580 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000581 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000582 UniqueAnalysisUsages.InsertNode(Node, IP);
583 }
584 assert(Node && "cached analysis usage must be non null");
585
586 AnUsageMap[P] = &Node->AU;
587 AnUsage = &Node->AU;;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000588 }
589 return AnUsage;
590}
591
Devang Patelafb1f3622006-12-12 22:35:25 +0000592/// Schedule pass P for execution. Make sure that passes required by
593/// P are run before P is run. Update analysis info maintained by
594/// the manager. Remove dead passes. This is a recursive function.
595void PMTopLevelManager::schedulePass(Pass *P) {
596
Devang Patel3312f752007-01-16 21:43:18 +0000597 // TODO : Allocate function manager for this pass, other wise required set
598 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000599
Devang Pateld74ede72007-03-06 01:06:16 +0000600 // Give pass a chance to prepare the stage.
601 P->preparePassManager(activeStack);
602
Devang Patel864970e2008-03-18 00:39:19 +0000603 // If P is an analysis pass and it is available then do not
604 // generate the analysis again. Stale analysis info should not be
605 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000606 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000607 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000608 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000609 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000610 }
Devang Patel864970e2008-03-18 00:39:19 +0000611
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000612 AnalysisUsage *AnUsage = findAnalysisUsage(P);
613
Devang Patelfdee7032008-08-14 23:07:48 +0000614 bool checkAnalysis = true;
615 while (checkAnalysis) {
616 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000617
Devang Patelfdee7032008-08-14 23:07:48 +0000618 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
619 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
620 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000621
Devang Patelfdee7032008-08-14 23:07:48 +0000622 Pass *AnalysisPass = findAnalysisPass(*I);
623 if (!AnalysisPass) {
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000624 const PassInfo *PI = findAnalysisPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000625
Craig Topperc6207612014-04-09 06:08:46 +0000626 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000627 // Pass P is not in the global PassRegistry
628 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
629 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
630 dbgs() << "Required Passes:" << "\n";
631 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
632 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
633 Pass *AnalysisPass2 = findAnalysisPass(*I2);
634 if (AnalysisPass2) {
635 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000636 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000637 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
638 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
639 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
640 }
641 }
642 }
643
Andrew Trick6bbaf132011-06-03 00:48:58 +0000644 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000645 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000646 if (P->getPotentialPassManagerType () ==
647 AnalysisPass->getPotentialPassManagerType())
648 // Schedule analysis pass that is managed by the same pass manager.
649 schedulePass(AnalysisPass);
650 else if (P->getPotentialPassManagerType () >
651 AnalysisPass->getPotentialPassManagerType()) {
652 // Schedule analysis pass that is managed by a new manager.
653 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000654 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000655 // are already checked are still available.
656 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000657 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000658 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000659 // passes are run on the fly.
660 delete AnalysisPass;
661 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000662 }
663 }
664
665 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000666 if (ImmutablePass *IP = P->getAsImmutablePass()) {
667 // P is a immutable pass and it will be managed by this
668 // top level manager. Set up analysis resolver to connect them.
669 PMDataManager *DM = getAsPMDataManager();
670 AnalysisResolver *AR = new AnalysisResolver(*DM);
671 P->setResolver(AR);
672 DM->initializeAnalysisImpl(P);
673 addImmutablePass(IP);
674 DM->recordAvailableAnalysis(IP);
675 return;
676 }
677
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000678 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000679 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000680 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000681 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
682 }
683
684 // Add the requested pass to the best available pass manager.
685 P->assignPassManager(activeStack, getTopLevelPassManagerType());
686
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000687 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000688 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000689 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000690 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
691 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000692}
693
694/// Find the pass that implements Analysis AID. Search immutable
695/// passes and all pass managers. If desired pass is not found
696/// then return NULL.
697Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000698 // For immutable passes we have a direct mapping from ID to pass, so check
699 // that first.
700 if (Pass *P = ImmutablePassMap.lookup(AID))
701 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000702
Devang Patelcd6ba152006-12-12 22:50:05 +0000703 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000704 for (PMDataManager *PassManager : PassManagers)
705 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000706 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000707
708 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000709 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
710 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000711 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000712
Craig Topperc6207612014-04-09 06:08:46 +0000713 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000714}
715
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000716const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
717 const PassInfo *&PI = AnalysisPassInfos[AID];
718 if (!PI)
719 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
720 else
721 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
722 "The pass info pointer changed for an analysis ID!");
723
724 return PI;
725}
726
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000727void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
728 P->initializePass();
729 ImmutablePasses.push_back(P);
730
731 // Add this pass to the map from its analysis ID. We clobber any prior runs
732 // of the pass in the map so that the last one added is the one found when
733 // doing lookups.
734 AnalysisID AID = P->getPassID();
735 ImmutablePassMap[AID] = P;
736
737 // Also add any interfaces implemented by the immutable pass to the map for
738 // fast lookup.
739 const PassInfo *PassInf = findAnalysisPassInfo(AID);
740 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000741 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000742 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
743}
744
Devang Pateleda56172006-12-12 23:34:33 +0000745// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000746void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000747
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000748 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000749 return;
750
Devang Pateleda56172006-12-12 23:34:33 +0000751 // Print out the immutable passes
752 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000753 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000754 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000755
Dan Gohmanf71c5212010-08-19 01:29:07 +0000756 // Every class that derives from PMDataManager also derives from Pass
757 // (sometimes indirectly), but there's no inheritance relationship
758 // between PMDataManager and Pass, so we have to getAsPass to get
759 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000760 for (PMDataManager *Manager : PassManagers)
761 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000762}
763
Devang Patel991aeba2006-12-15 20:13:01 +0000764void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000765
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000766 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000767 return;
768
David Greene994e1bb2010-01-05 01:30:02 +0000769 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000770 for (ImmutablePass *P : ImmutablePasses)
771 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000772 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000773 if (!PI->isAnalysisGroup())
774 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000775 }
Yaron Keren83009952016-04-28 14:49:44 +0000776 for (PMDataManager *PM : PassManagers)
777 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000778 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000779}
780
Devang Patele3068402006-12-21 00:16:50 +0000781void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000782 for (PMDataManager *PM : PassManagers)
783 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000784
Devang Patele3068402006-12-21 00:16:50 +0000785 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000786 for (PMDataManager *IPM : IndirectPassManagers)
787 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000788
Yaron Kerenbd873192016-10-02 19:21:41 +0000789 for (auto LU : LastUser) {
790 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
791 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000792 }
Devang Patele3068402006-12-21 00:16:50 +0000793}
794
Devang Patele7599552007-01-12 18:52:44 +0000795/// Destructor
796PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000797 for (PMDataManager *PM : PassManagers)
798 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000799
Yaron Keren83009952016-04-28 14:49:44 +0000800 for (ImmutablePass *P : ImmutablePasses)
801 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000802}
803
Devang Patelafb1f3622006-12-12 22:35:25 +0000804//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000805// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000806
Devang Patel643676c2006-11-11 01:10:19 +0000807/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000808void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000809 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000810
Chris Lattner60987362009-03-06 05:53:14 +0000811 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000812
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000813 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000814
Dan Gohmande6188a2010-08-12 23:50:08 +0000815 // This pass is the current implementation of all of the interfaces it
816 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000817 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000818 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000819 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000820 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000821 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000822}
823
Devang Patel9d9fc902007-03-06 17:52:53 +0000824// Return true if P preserves high level analysis used by other
825// passes managed by this manager
826bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000827 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000828 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000829 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000830
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000831 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000832 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000833 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000834 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000835 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000836 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000837
Devang Patel9d9fc902007-03-06 17:52:53 +0000838 return true;
839}
840
Chris Lattner02eb94c2008-08-07 07:34:50 +0000841/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000842void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000843 // Don't do this unless assertions are enabled.
844#ifdef NDEBUG
845 return;
846#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000847 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
848 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000849
Devang Patelef432532007-07-19 05:36:09 +0000850 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000851 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000852 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000853 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000854 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000855 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000856 }
857}
858
Devang Patel67c79a42008-07-01 19:50:56 +0000859/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000860void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000861 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
862 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000863 return;
864
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000865 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000866 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000867 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000868 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000869 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000870 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000871 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000872 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000873 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000874 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
875 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000876 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000877 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000878 }
Devang Patel349170f2006-11-11 01:24:55 +0000879 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000880
Devang Patel42dd1e92007-03-06 01:55:46 +0000881 // Check inherited analysis also. If P is not preserving analysis
882 // provided by parent manager then remove it here.
883 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
884
885 if (!InheritedAnalysis[Index])
886 continue;
887
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000888 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000889 I = InheritedAnalysis[Index]->begin(),
890 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000891 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000892 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000893 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000894 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000895 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000896 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000897 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
898 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000899 }
Devang Patel01919d22007-03-08 19:05:01 +0000900 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000901 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000902 }
903 }
Devang Patelf68a3492006-11-07 22:35:17 +0000904}
905
Devang Patelca189262006-11-14 03:05:08 +0000906/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000907void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000908 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000909
Devang Patel8adae862007-07-20 18:04:54 +0000910 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000911
Devang Patel2ff44922007-04-16 20:39:59 +0000912 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000913 if (!TPM)
914 return;
915
Devang Patel17ad0962006-12-08 00:37:52 +0000916 TPM->collectLastUses(DeadPasses, P);
917
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000918 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000919 dbgs() << " -*- '" << P->getPassName();
920 dbgs() << "' is the last user of following pass instances.";
921 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000922 }
923
Yaron Kerenbd873192016-10-02 19:21:41 +0000924 for (Pass *P : DeadPasses)
925 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000926}
Devang Patel200d3052006-12-13 23:50:44 +0000927
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000928void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000929 enum PassDebuggingString DBG_STR) {
930 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000931
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000932 {
933 // If the pass crashes releasing memory, remember this.
934 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000935 TimeRegion PassTimer(getPassTimer(P));
936
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000937 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000938 }
939
Owen Andersona7aed182010-08-06 18:33:48 +0000940 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000941 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000942 // Remove the pass itself (if it is not already removed).
943 AvailableAnalysis.erase(PI);
944
945 // Remove all interfaces this pass implements, for which it is also
946 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000947 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000948 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000949 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000950 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000951 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000952 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000953 }
Devang Patel17ad0962006-12-08 00:37:52 +0000954 }
Devang Patelca189262006-11-14 03:05:08 +0000955}
956
Dan Gohmande6188a2010-08-12 23:50:08 +0000957/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000958/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000959void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000960 // This manager is going to manage pass P. Set up analysis resolver
961 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000962 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000963 P->setResolver(AR);
964
Devang Patelec2b9a72007-03-05 22:57:49 +0000965 // If a FunctionPass F is the last user of ModulePass info M
966 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000967 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000968
Chris Lattner60987362009-03-06 05:53:14 +0000969 if (!ProcessAnalysis) {
970 // Add pass
971 PassVector.push_back(P);
972 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000973 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000974
Chris Lattner60987362009-03-06 05:53:14 +0000975 // At the moment, this pass is the last user of all required passes.
976 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +0000977 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +0000978 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
979
980 unsigned PDepth = this->getDepth();
981
Chandler Carruth44a13852015-08-19 03:02:12 +0000982 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
983 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +0000984 unsigned RDepth = 0;
985
Chandler Carruth44a13852015-08-19 03:02:12 +0000986 assert(PUsed->getResolver() && "Analysis Resolver is not set");
987 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +0000988 RDepth = DM.getDepth();
989
990 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +0000991 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +0000992 else if (PDepth > RDepth) {
993 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +0000994 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +0000995 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +0000996 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +0000997 } else
Chandler Carruth44a13852015-08-19 03:02:12 +0000998 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +0000999 }
1000
1001 // Set P as P's last user until someone starts using P.
1002 // However, if P is a Pass Manager then it does not need
1003 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001004 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001005 LastUses.push_back(P);
1006 TPM->setLastUser(LastUses, P);
1007
1008 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001009 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001010 TPM->setLastUser(TransferLastUses, My_PM);
1011 TransferLastUses.clear();
1012 }
1013
Dan Gohman6304db32010-08-16 22:57:28 +00001014 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001015 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1016 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001017 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001018 this->addLowerLevelRequiredPass(P, AnalysisPass);
1019 }
1020
1021 // Take a note of analysis required and made available by this pass.
1022 // Remove the analysis not preserved by this pass
1023 removeNotPreservedAnalysis(P);
1024 recordAvailableAnalysis(P);
1025
Devang Patel8cad70d2006-11-11 01:51:02 +00001026 // Add pass
1027 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001028}
1029
Devang Patele64d3052007-04-16 20:12:57 +00001030
Chandler Carruth44a13852015-08-19 03:02:12 +00001031/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001032/// pass P and are available. Populate RP_NotAvail with analysis
1033/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001034void PMDataManager::collectRequiredAndUsedAnalyses(
1035 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1036 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001037 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001038
1039 for (const auto &UsedID : AnUsage->getUsedSet())
1040 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1041 UP.push_back(AnalysisPass);
1042
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001043 for (const auto &RequiredID : AnUsage->getRequiredSet())
1044 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001045 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001046 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001047 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001048
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001049 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1050 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001051 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001052 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001053 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001054}
1055
Devang Patel07f4f582006-11-14 21:49:36 +00001056// All Required analyses should be available to the pass as it runs! Here
1057// we fill in the AnalysisImpls member of the pass so that it can
1058// successfully use the getAnalysis() method to retrieve the
1059// implementations it needs.
1060//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001061void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001062 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1063
Chris Lattnercbd160f2008-08-08 05:33:04 +00001064 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001065 I = AnUsage->getRequiredSet().begin(),
1066 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001067 Pass *Impl = findAnalysisPass(*I, true);
Craig Topperc6207612014-04-09 06:08:46 +00001068 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001069 // This may be analysis pass that is initialized on the fly.
1070 // If that is not the case then it will raise an assert when it is used.
1071 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001072 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001073 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001074 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001075 }
1076}
1077
Devang Patel640c5bb2006-12-08 22:30:11 +00001078/// Find the pass that implements Analysis AID. If desired pass is not found
1079/// then return NULL.
1080Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1081
1082 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001083 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001084
1085 if (I != AvailableAnalysis.end())
1086 return I->second;
1087
1088 // Search Parents through TopLevelManager
1089 if (SearchParent)
1090 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001091
Craig Topperc6207612014-04-09 06:08:46 +00001092 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001093}
1094
Devang Patel991aeba2006-12-15 20:13:01 +00001095// Print list of passes that are last used by P.
1096void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1097
Devang Patel8adae862007-07-20 18:04:54 +00001098 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001099
1100 // If this is a on the fly manager then it does not have TPM.
1101 if (!TPM)
1102 return;
1103
Devang Patel991aeba2006-12-15 20:13:01 +00001104 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001105
Dan Gohman7224bce2010-10-12 00:11:18 +00001106 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001107 E = LUses.end(); I != E; ++I) {
Eric Christophera13839f2014-02-26 23:27:16 +00001108 dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001109 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001110 }
1111}
1112
1113void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001114 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001115 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001116 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001117 PMD->dumpPassArguments();
1118 else
Owen Andersona7aed182010-08-06 18:33:48 +00001119 if (const PassInfo *PI =
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001120 TPM->findAnalysisPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001121 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001122 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001123 }
1124}
1125
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001126void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1127 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001128 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001129 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001130 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001131 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001132 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001133 switch (S1) {
1134 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001135 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001136 break;
1137 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001138 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001139 break;
1140 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001141 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001142 break;
1143 default:
1144 break;
1145 }
1146 switch (S2) {
1147 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001148 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001149 break;
1150 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001151 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001152 break;
1153 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001154 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001155 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001156 case ON_REGION_MSG:
1157 dbgs() << "' on Region '" << Msg << "'...\n";
1158 break;
Devang Patel003a5592007-03-05 20:01:30 +00001159 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001160 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001161 break;
1162 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001163 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001164 break;
1165 default:
1166 break;
1167 }
Devang Patel991aeba2006-12-15 20:13:01 +00001168}
1169
Chris Lattner4c1e9542009-03-06 06:45:05 +00001170void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001171 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001172 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001173
Chris Lattner4c493d92008-08-08 15:14:09 +00001174 AnalysisUsage analysisUsage;
1175 P->getAnalysisUsage(analysisUsage);
1176 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1177}
1178
Chris Lattner4c1e9542009-03-06 06:45:05 +00001179void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001180 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001181 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001182
Chris Lattner4c493d92008-08-08 15:14:09 +00001183 AnalysisUsage analysisUsage;
1184 P->getAnalysisUsage(analysisUsage);
1185 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1186}
1187
Chandler Carruth44a13852015-08-19 03:02:12 +00001188void PMDataManager::dumpUsedSet(const Pass *P) const {
1189 if (PassDebugging < Details)
1190 return;
1191
1192 AnalysisUsage analysisUsage;
1193 P->getAnalysisUsage(analysisUsage);
1194 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1195}
1196
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001197void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001198 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001199 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001200 if (Set.empty())
1201 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001202 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001203 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001204 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001205 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001206 if (!PInf) {
1207 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1208 // all drivers.
1209 dbgs() << " Uninitialized Pass";
1210 continue;
1211 }
Owen Andersona7aed182010-08-06 18:33:48 +00001212 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001213 }
David Greene994e1bb2010-01-05 01:30:02 +00001214 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001215}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001216
Devang Patel004937b2007-07-27 20:06:09 +00001217/// Add RequiredPass into list of lower level passes required by pass P.
1218/// RequiredPass is run on the fly by Pass Manager when P requests it
1219/// through getAnalysis interface.
1220/// This should be handled by specific pass manager.
1221void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1222 if (TPM) {
1223 TPM->dumpArguments();
1224 TPM->dumpPasses();
1225 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001226
Dan Gohmande6188a2010-08-12 23:50:08 +00001227 // Module Level pass may required Function Level analysis info
1228 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1229 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001230 // module level pass is requiring lower level analysis info managed by
1231 // lower level pass manager.
1232
1233 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001234 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001235 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001236#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001237 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1238 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001239#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001240 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001241}
1242
Owen Andersona7aed182010-08-06 18:33:48 +00001243Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001244 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001245}
1246
Devang Patele7599552007-01-12 18:52:44 +00001247// Destructor
1248PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001249 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001250 E = PassVector.end(); I != E; ++I)
1251 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001252}
1253
Devang Patel9bdf7d42006-12-08 23:28:54 +00001254//===----------------------------------------------------------------------===//
1255// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001256// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1257Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001258 return PM.findAnalysisPass(ID, dir);
1259}
1260
Dan Gohmande6188a2010-08-12 23:50:08 +00001261Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001262 Function &F) {
1263 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1264}
1265
Devang Patela1514cb2006-12-07 19:39:39 +00001266//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001267// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001268
Dan Gohmande6188a2010-08-12 23:50:08 +00001269/// Execute all of the passes scheduled for execution by invoking
1270/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001271/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001272bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001273 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001274 return false;
1275
Devang Patele9585592006-12-08 01:38:28 +00001276 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001277
Devang Patel6e5a1132006-11-07 21:31:57 +00001278 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001279 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1280 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001281 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001282
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001283 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001284 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001285
Devang Patelabfbe3b2006-12-16 00:56:26 +00001286 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001287
Chris Lattner4c1e9542009-03-06 06:45:05 +00001288 {
1289 // If the pass crashes, remember this.
1290 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001291 TimeRegion PassTimer(getPassTimer(BP));
1292
Dan Gohman74b189f2010-03-01 17:34:28 +00001293 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001294 }
Devang Patel93a197c2006-12-14 00:08:04 +00001295
Dan Gohman74b189f2010-03-01 17:34:28 +00001296 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001297 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001298 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001299 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001300 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001301 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001302
Devang Patela273d1c2007-07-19 18:02:32 +00001303 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001304 removeNotPreservedAnalysis(BP);
1305 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001306 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001307 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001308
Bill Wendling6ce6d262009-12-25 13:50:18 +00001309 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001310}
1311
Devang Patel475c4532006-12-08 00:59:05 +00001312// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001313bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001314 bool Changed = false;
1315
Chris Lattner4c1e9542009-03-06 06:45:05 +00001316 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1317 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001318
1319 return Changed;
1320}
1321
Duncan Sands51495602009-02-13 09:42:34 +00001322bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001323 bool Changed = false;
1324
Pedro Artigas41b98842012-12-05 17:12:22 +00001325 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001326 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001327
1328 return Changed;
1329}
1330
Duncan Sands51495602009-02-13 09:42:34 +00001331bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001332 bool Changed = false;
1333
Devang Patelabfbe3b2006-12-16 00:56:26 +00001334 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1335 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001336 Changed |= BP->doInitialization(F);
1337 }
1338
1339 return Changed;
1340}
1341
Duncan Sands51495602009-02-13 09:42:34 +00001342bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001343 bool Changed = false;
1344
Devang Patelabfbe3b2006-12-16 00:56:26 +00001345 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1346 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001347 Changed |= BP->doFinalization(F);
1348 }
1349
1350 return Changed;
1351}
1352
1353
Devang Patela1514cb2006-12-07 19:39:39 +00001354//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001355// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001356
Devang Patel4e12f862006-11-08 10:44:40 +00001357/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001358FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001359 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001360 // FPM is the top level manager.
1361 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001362
Dan Gohman565df952008-03-13 02:08:36 +00001363 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001364 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001365}
1366
Devang Patelb67904d2006-12-13 02:36:01 +00001367FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001368 delete FPM;
1369}
1370
Dan Gohmande6188a2010-08-12 23:50:08 +00001371void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001372 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001373}
1374
Devang Patel9f3083e2006-11-15 19:39:54 +00001375/// run - Execute all of the passes scheduled for execution. Keep
1376/// track of whether any of the passes modifies the function, and if
1377/// so, return true.
1378///
Devang Patelb67904d2006-12-13 02:36:01 +00001379bool FunctionPassManager::run(Function &F) {
Rafael Espindola246c4fb2014-11-01 16:46:18 +00001380 if (std::error_code EC = F.materialize())
1381 report_fatal_error("Error reading bitcode file: " + EC.message());
Devang Patel272908d2006-12-08 22:57:48 +00001382 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001383}
1384
1385
Devang Patelff631ae2006-11-15 01:27:05 +00001386/// doInitialization - Run all of the initializers for the function passes.
1387///
Devang Patelb67904d2006-12-13 02:36:01 +00001388bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001389 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001390}
1391
Dan Gohmane6656eb2007-07-30 14:51:13 +00001392/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001393///
Devang Patelb67904d2006-12-13 02:36:01 +00001394bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001395 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001396}
1397
Devang Patela1514cb2006-12-07 19:39:39 +00001398//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001399// FunctionPassManagerImpl implementation
1400//
Duncan Sands51495602009-02-13 09:42:34 +00001401bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001402 bool Changed = false;
1403
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001404 dumpArguments();
1405 dumpPasses();
1406
Yaron Keren4849aa32015-06-05 17:48:47 +00001407 for (ImmutablePass *ImPass : getImmutablePasses())
1408 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001409
Chris Lattner4c1e9542009-03-06 06:45:05 +00001410 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1411 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001412
1413 return Changed;
1414}
1415
Duncan Sands51495602009-02-13 09:42:34 +00001416bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001417 bool Changed = false;
1418
Pedro Artigas41b98842012-12-05 17:12:22 +00001419 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001420 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001421
Yaron Keren4849aa32015-06-05 17:48:47 +00001422 for (ImmutablePass *ImPass : getImmutablePasses())
1423 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001424
Devang Patel67d6a5e2006-12-19 19:46:59 +00001425 return Changed;
1426}
1427
Devang Patelec9c58f2009-04-01 22:34:41 +00001428/// cleanup - After running all passes, clean up pass manager cache.
1429void FPPassManager::cleanup() {
1430 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1431 FunctionPass *FP = getContainedPass(Index);
1432 AnalysisResolver *AR = FP->getResolver();
1433 assert(AR && "Analysis Resolver is not set");
1434 AR->clearAnalysisImpls();
1435 }
1436}
1437
Torok Edwin24c78352009-06-29 18:49:09 +00001438void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1439 if (!wasRun)
1440 return;
1441 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1442 FPPassManager *FPPM = getContainedManager(Index);
1443 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1444 FPPM->getContainedPass(Index)->releaseMemory();
1445 }
1446 }
Torok Edwin896556e2009-06-29 21:05:10 +00001447 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001448}
1449
Devang Patel67d6a5e2006-12-19 19:46:59 +00001450// Execute all the passes managed by this top level manager.
1451// Return true if any function is modified by a pass.
1452bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001453 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001454 TimingInfo::createTheTimeInfo();
1455
Devang Patele3068402006-12-21 00:16:50 +00001456 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001457 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001458 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001459 F.getContext().yield();
1460 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001461
1462 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1463 getContainedManager(Index)->cleanup();
1464
Torok Edwin24c78352009-06-29 18:49:09 +00001465 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001466 return Changed;
1467}
1468
1469//===----------------------------------------------------------------------===//
1470// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001471
Devang Patel8c78a0b2007-05-03 01:11:54 +00001472char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001473/// Print passes managed by this manager
1474void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001475 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001476 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1477 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001478 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001479 dumpLastUses(FP, Offset+1);
1480 }
1481}
1482
1483
Dan Gohmande6188a2010-08-12 23:50:08 +00001484/// Execute all of the passes scheduled for execution by invoking
1485/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001486/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001487bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001488 if (F.isDeclaration())
1489 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001490
1491 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001492
Devang Patelcbbf2912008-03-20 01:09:53 +00001493 // Collect inherited analysis from Module level pass manager.
1494 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001495
Devang Patelabfbe3b2006-12-16 00:56:26 +00001496 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1497 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001498 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001499
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001500 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001501 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001502
Devang Patelabfbe3b2006-12-16 00:56:26 +00001503 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001504
Chris Lattner4c1e9542009-03-06 06:45:05 +00001505 {
1506 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001507 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001508
Dan Gohman74b189f2010-03-01 17:34:28 +00001509 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001510 }
Devang Patel93a197c2006-12-14 00:08:04 +00001511
Dan Gohman74b189f2010-03-01 17:34:28 +00001512 Changed |= LocalChanged;
1513 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001514 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001515 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001516 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001517
Devang Patela273d1c2007-07-19 18:02:32 +00001518 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001519 removeNotPreservedAnalysis(FP);
1520 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001521 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001522 }
1523 return Changed;
1524}
1525
Devang Patel67d6a5e2006-12-19 19:46:59 +00001526bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001527 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001528
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001529 for (Function &F : M)
1530 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001531
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001532 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001533}
1534
Duncan Sands51495602009-02-13 09:42:34 +00001535bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001536 bool Changed = false;
1537
Chris Lattner4c1e9542009-03-06 06:45:05 +00001538 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1539 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001540
Devang Patelff631ae2006-11-15 01:27:05 +00001541 return Changed;
1542}
1543
Duncan Sands51495602009-02-13 09:42:34 +00001544bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001545 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001546
1547 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001548 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001549
Devang Patelff631ae2006-11-15 01:27:05 +00001550 return Changed;
1551}
1552
Devang Patela1514cb2006-12-07 19:39:39 +00001553//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001554// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001555
Dan Gohmande6188a2010-08-12 23:50:08 +00001556/// Execute all of the passes scheduled for execution by invoking
1557/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001558/// the module, and if so, return true.
1559bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001560MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001561 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001562
Torok Edwin24c78352009-06-29 18:49:09 +00001563 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001564 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1565 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001566 Changed |= FPP->doInitialization(M);
1567 }
1568
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001569 // Initialize module passes
1570 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1571 Changed |= getContainedPass(Index)->doInitialization(M);
1572
Devang Patelabfbe3b2006-12-16 00:56:26 +00001573 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1574 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001575 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001576
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001577 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001578 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001579
Devang Patelabfbe3b2006-12-16 00:56:26 +00001580 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001581
Chris Lattner4c1e9542009-03-06 06:45:05 +00001582 {
1583 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001584 TimeRegion PassTimer(getPassTimer(MP));
1585
Dan Gohman74b189f2010-03-01 17:34:28 +00001586 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001587 }
Devang Patel93a197c2006-12-14 00:08:04 +00001588
Dan Gohman74b189f2010-03-01 17:34:28 +00001589 Changed |= LocalChanged;
1590 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001591 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001592 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001593 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001594 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001595
Devang Patela273d1c2007-07-19 18:02:32 +00001596 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001597 removeNotPreservedAnalysis(MP);
1598 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001599 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001600 }
Torok Edwin24c78352009-06-29 18:49:09 +00001601
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001602 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001603 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001604 Changed |= getContainedPass(Index)->doFinalization(M);
1605
Torok Edwin24c78352009-06-29 18:49:09 +00001606 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001607 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1608 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001609 // We don't know when is the last time an on-the-fly pass is run,
1610 // so we need to releaseMemory / finalize here
1611 FPP->releaseMemoryOnTheFly();
1612 Changed |= FPP->doFinalization(M);
1613 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001614
Devang Patel05e1a972006-11-07 22:03:15 +00001615 return Changed;
1616}
1617
Devang Patele64d3052007-04-16 20:12:57 +00001618/// Add RequiredPass into list of lower level passes required by pass P.
1619/// RequiredPass is run on the fly by Pass Manager when P requests it
1620/// through getAnalysis interface.
1621void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001622 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1623 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001624 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001625 RequiredPass->getPotentialPassManagerType()) &&
1626 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001627 if (!RequiredPass)
1628 return;
Devang Patele64d3052007-04-16 20:12:57 +00001629
Devang Patel68f72b12007-04-26 17:50:19 +00001630 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001631 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001632 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001633 // FPP is the top level manager.
1634 FPP->setTopLevelManager(FPP);
1635
Devang Patel69e9f6d2007-04-16 20:27:05 +00001636 OnTheFlyManagers[P] = FPP;
1637 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001638 const PassInfo *RequiredPassPI =
1639 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001640
Craig Topperc6207612014-04-09 06:08:46 +00001641 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001642 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1643 FoundPass =
1644 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001645 }
Andrew Trick02066f22014-04-08 03:40:34 +00001646 if (!FoundPass) {
1647 FoundPass = RequiredPass;
1648 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001649 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001650 FPP->add(RequiredPass);
1651 }
1652 // Register P as the last user of FoundPass or RequiredPass.
1653 SmallVector<Pass *, 1> LU;
1654 LU.push_back(FoundPass);
1655 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001656}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001657
Dan Gohmande6188a2010-08-12 23:50:08 +00001658/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001659/// required by module pass MP. Instantiate analysis pass, by using
1660/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001661Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001662 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001663 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001664
Torok Edwin24c78352009-06-29 18:49:09 +00001665 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001666 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001667 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001668}
1669
1670
Devang Patela1514cb2006-12-07 19:39:39 +00001671//===----------------------------------------------------------------------===//
1672// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001673
Devang Patelab97cf42006-12-13 00:09:23 +00001674//
Devang Patelc290c8a2006-11-07 22:23:34 +00001675/// run - Execute all of the passes scheduled for execution. Keep track of
1676/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001677bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001678 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001679 TimingInfo::createTheTimeInfo();
1680
Devang Patelcfd70c42006-12-13 22:10:00 +00001681 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001682 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001683
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001684 for (ImmutablePass *ImPass : getImmutablePasses())
1685 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001686
Devang Patele3068402006-12-21 00:16:50 +00001687 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001688 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001689 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001690 M.getContext().yield();
1691 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001692
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001693 for (ImmutablePass *ImPass : getImmutablePasses())
1694 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001695
Devang Patelc290c8a2006-11-07 22:23:34 +00001696 return Changed;
1697}
Devang Patel376fefa2006-11-08 10:29:57 +00001698
Devang Patela1514cb2006-12-07 19:39:39 +00001699//===----------------------------------------------------------------------===//
1700// PassManager implementation
1701
Devang Patel376fefa2006-11-08 10:29:57 +00001702/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001703PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001704 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001705 // PM is the top level manager
1706 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001707}
1708
Devang Patelb67904d2006-12-13 02:36:01 +00001709PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001710 delete PM;
1711}
1712
Chris Lattner60987362009-03-06 05:53:14 +00001713void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001714 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001715}
1716
1717/// run - Execute all of the passes scheduled for execution. Keep track of
1718/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001719bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001720 return PM->run(M);
1721}
1722
Devang Patelb8817b92006-12-14 00:59:42 +00001723//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001724// TimingInfo implementation
1725
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001726bool llvm::TimePassesIsEnabled = false;
1727static cl::opt<bool,true>
1728EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1729 cl::desc("Time each pass, printing elapsed time for each on exit"));
1730
Devang Patelb8817b92006-12-14 00:59:42 +00001731// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001732// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001733// null. It may be called multiple times.
1734void TimingInfo::createTheTimeInfo() {
1735 if (!TimePassesIsEnabled || TheTimeInfo) return;
1736
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001737 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001738 // This guarantees that the object will be constructed before static globals,
1739 // thus it will be destroyed before them.
1740 static ManagedStatic<TimingInfo> TTI;
1741 TheTimeInfo = &*TTI;
1742}
1743
Devang Patel1c3633e2007-01-29 23:10:37 +00001744/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001745Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001746 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001747 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001748 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001749}
1750
Devang Patel1c56a632007-01-08 19:29:38 +00001751//===----------------------------------------------------------------------===//
1752// PMStack implementation
1753//
Devang Patelad98d232007-01-11 22:15:30 +00001754
Devang Patel1c56a632007-01-08 19:29:38 +00001755// Pop Pass Manager from the stack and clear its analysis info.
1756void PMStack::pop() {
1757
1758 PMDataManager *Top = this->top();
1759 Top->initializeAnalysisInfo();
1760
1761 S.pop_back();
1762}
1763
1764// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001765void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001766 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001767 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001768
Chris Lattner60987362009-03-06 05:53:14 +00001769 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001770 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1771 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001772 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001773
Chris Lattner60987362009-03-06 05:53:14 +00001774 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001775 TPM->addIndirectPassManager(PM);
1776 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001777 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001778 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001779 assert((PM->getPassManagerType() == PMT_ModulePassManager
1780 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001781 && "pushing bad pass manager to PMStack");
1782 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001783 }
1784
Devang Patel15701b52007-01-11 00:19:00 +00001785 S.push_back(PM);
1786}
1787
1788// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001789LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001790 for (PMDataManager *Manager : S)
1791 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001792
Devang Patel15701b52007-01-11 00:19:00 +00001793 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001794 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001795}
1796
Devang Patel1c56a632007-01-08 19:29:38 +00001797/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001798/// add self into that manager.
1799void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001800 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001801 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001802 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001803 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1804 if (TopPMType == PreferredType)
1805 break; // We found desired pass manager
1806 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001807 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001808 else
1809 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001810 }
Devang Patel18ff6362008-09-09 21:38:40 +00001811 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001812 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001813}
1814
Devang Patel3312f752007-01-16 21:43:18 +00001815/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001816/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001817void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001818 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001819
Andrew Trickcbc845f2012-02-01 07:16:20 +00001820 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001821 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001822 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1823 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001824 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001825 break;
Devang Patel3312f752007-01-16 21:43:18 +00001826 }
Devang Patel3312f752007-01-16 21:43:18 +00001827
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001828 // Create new Function Pass Manager if needed.
1829 FPPassManager *FPP;
1830 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1831 FPP = (FPPassManager *)PMS.top();
1832 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001833 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1834 PMDataManager *PMD = PMS.top();
1835
1836 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001837 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001838 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001839
1840 // [2] Set up new manager's top level manager
1841 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1842 TPM->addIndirectPassManager(FPP);
1843
1844 // [3] Assign manager to manage this new manager. This may create
1845 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001846 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001847
1848 // [4] Push new manager into PMS
1849 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001850 }
1851
Devang Patel3312f752007-01-16 21:43:18 +00001852 // Assign FPP as the manager of this pass.
1853 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001854}
1855
Devang Patel3312f752007-01-16 21:43:18 +00001856/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001857/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001858void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001859 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001860 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001861
Devang Patel15701b52007-01-11 00:19:00 +00001862 // Basic Pass Manager is a leaf pass manager. It does not handle
1863 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001864 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001865 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1866 BBP = (BBPassManager *)PMS.top();
1867 } else {
1868 // If leaf manager is not Basic Block Pass manager then create new
1869 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001870 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1871 PMDataManager *PMD = PMS.top();
1872
1873 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001874 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001875
1876 // [2] Set up new manager's top level manager
1877 // Basic Block Pass Manager does not live by itself
1878 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1879 TPM->addIndirectPassManager(BBP);
1880
Devang Patel15701b52007-01-11 00:19:00 +00001881 // [3] Assign manager to manage this new manager. This may create
1882 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001883 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001884
Devang Patel3312f752007-01-16 21:43:18 +00001885 // [4] Push new manager into PMS
1886 PMS.push(BBP);
1887 }
Devang Patel1c56a632007-01-08 19:29:38 +00001888
Devang Patel3312f752007-01-16 21:43:18 +00001889 // Assign BBP as the manager of this pass.
1890 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001891}
1892
Dan Gohmand3a20c92008-03-11 16:41:42 +00001893PassManagerBase::~PassManagerBase() {}