blob: 3890478f53e3386911c0dfa795855e96c9d47742 [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
14
Juergen Ributzka34390c72014-05-16 02:33:15 +000015#include "llvm/IR/LLVMContext.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000016#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000017#include "llvm/IR/LegacyPassManager.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000018#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000019#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000020#include "llvm/IR/Module.h"
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 Carruth20c56932014-04-27 23:59:25 +000026#include "llvm/Support/TimeValue.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/Timer.h"
28#include "llvm/Support/raw_ostream.h"
Jeff Cohenb622c112007-03-05 00:00:42 +000029#include <algorithm>
Devang Patelf60b5d92006-11-14 01:59:59 +000030#include <map>
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"),
58 clEnumVal(Details , "print pass details when it is executed"),
59 clEnumValEnd));
David Greene9b063df2010-04-02 23:17:14 +000060
Chandler Carruth7caea412013-11-09 12:26:54 +000061namespace {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000062typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
63PassOptionList;
Chandler Carruth7caea412013-11-09 12:26:54 +000064}
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000065
66// Print IR out before/after specified passes.
67static PassOptionList
68PrintBefore("print-before",
69 llvm::cl::desc("Print IR before specified passes"),
70 cl::Hidden);
71
72static PassOptionList
73PrintAfter("print-after",
74 llvm::cl::desc("Print IR after specified passes"),
75 cl::Hidden);
76
77static cl::opt<bool>
78PrintBeforeAll("print-before-all",
79 llvm::cl::desc("Print IR before each pass"),
80 cl::init(false));
81static cl::opt<bool>
82PrintAfterAll("print-after-all",
83 llvm::cl::desc("Print IR after each pass"),
84 cl::init(false));
85
86/// This is a helper to determine whether to print IR before or
87/// after a pass.
88
89static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
90 PassOptionList &PassesToPrint) {
Chris Bieneman664294c2015-04-29 21:45:22 +000091 for (auto *PassInf : PassesToPrint) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000092 if (PassInf)
93 if (PassInf->getPassArgument() == PI->getPassArgument()) {
94 return true;
95 }
David Greene9b063df2010-04-02 23:17:14 +000096 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000097 return false;
98}
Dan Gohmande6188a2010-08-12 23:50:08 +000099
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000100/// This is a utility to check whether a pass should have IR dumped
101/// before it.
102static bool ShouldPrintBeforePass(const PassInfo *PI) {
103 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
104}
David Greene9b063df2010-04-02 23:17:14 +0000105
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000106/// This is a utility to check whether a pass should have IR dumped
107/// after it.
108static bool ShouldPrintAfterPass(const PassInfo *PI) {
109 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000110}
111
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000112/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
113/// or higher is specified.
114bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000115 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000116}
117
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000118
119
120
Chris Lattner4c1e9542009-03-06 06:45:05 +0000121void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000122 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000123 OS << "Releasing pass '";
124 else
125 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000126
Chris Lattner4c1e9542009-03-06 06:45:05 +0000127 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000128
Chris Lattner4c1e9542009-03-06 06:45:05 +0000129 if (M) {
130 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
131 return;
132 }
Craig Topperc6207612014-04-09 06:08:46 +0000133 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000134 OS << '\n';
135 return;
136 }
137
Dan Gohman79fc0e92009-03-10 18:47:59 +0000138 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000139 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000140 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000141 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000142 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000143 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000144 OS << "value";
145
146 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000147 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000148 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000149}
150
151
Devang Patelffca9102006-12-15 19:39:30 +0000152namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000153//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000154// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000155//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000156/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000157/// pass together and sequence them to process one basic block before
158/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000159class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000160
161public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000162 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000163 explicit BBPassManager()
164 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000165
Devang Patelca58e352006-11-08 10:05:38 +0000166 /// Execute all of the passes scheduled for execution. Keep track of
167 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000168 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000169
Devang Patelf9d96b92006-12-07 19:57:52 +0000170 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000171 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000172 Info.setPreservesAll();
173 }
174
Craig Topperf398d7c2014-03-05 06:35:38 +0000175 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000176 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000177 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000178 bool doFinalization(Function &F);
179
Craig Topperf398d7c2014-03-05 06:35:38 +0000180 PMDataManager *getAsPMDataManager() override { return this; }
181 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000182
Craig Topperf398d7c2014-03-05 06:35:38 +0000183 const char *getPassName() const override {
Dan Gohman1e9860a2008-03-13 01:58:48 +0000184 return "BasicBlock Pass Manager";
Devang Patele3858e62007-02-01 22:08:25 +0000185 }
186
Devang Pateleda56172006-12-12 23:34:33 +0000187 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000188 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000189 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000190 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
191 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000192 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000193 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000194 }
195 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000196
197 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000198 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000199 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
200 return BP;
201 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000202
Craig Topperf398d7c2014-03-05 06:35:38 +0000203 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000204 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000205 }
Devang Patelca58e352006-11-08 10:05:38 +0000206};
207
Devang Patel8c78a0b2007-05-03 01:11:54 +0000208char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000209} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000210
Devang Patele7599552007-01-12 18:52:44 +0000211namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000212namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000213//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000214// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000215//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000216/// FunctionPassManagerImpl manages FPPassManagers
217class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000218 public PMDataManager,
219 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000220 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000221private:
222 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000223public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000224 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000225 explicit FunctionPassManagerImpl() :
226 Pass(PT_PassManager, ID), PMDataManager(),
227 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000228
Matthias Braun0880c602014-12-12 01:27:01 +0000229 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000230 void add(Pass *P) {
231 schedulePass(P);
232 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000233
234 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000235 Pass *createPrinterPass(raw_ostream &O,
236 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000237 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000238 }
239
Torok Edwin24c78352009-06-29 18:49:09 +0000240 // Prepare for running an on the fly pass, freeing memory if needed
241 // from a previous run.
242 void releaseMemoryOnTheFly();
243
Devang Patel67d6a5e2006-12-19 19:46:59 +0000244 /// run - Execute all of the passes scheduled for execution. Keep track of
245 /// whether any of the passes modifies the module, and if so, return true.
246 bool run(Function &F);
247
248 /// doInitialization - Run all of the initializers for the function passes.
249 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000250 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000251
Dan Gohmane6656eb2007-07-30 14:51:13 +0000252 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000253 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000254 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000255
Dan Gohmande6188a2010-08-12 23:50:08 +0000256
Craig Topperf398d7c2014-03-05 06:35:38 +0000257 PMDataManager *getAsPMDataManager() override { return this; }
258 Pass *getAsPass() override { return this; }
259 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000260 return PMT_FunctionPassManager;
261 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000262
Devang Patel67d6a5e2006-12-19 19:46:59 +0000263 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000264 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000265 Info.setPreservesAll();
266 }
267
Devang Patel67d6a5e2006-12-19 19:46:59 +0000268 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000269 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000270 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
271 return FP;
272 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000273};
274
David Blaikiea379b1812011-12-20 02:50:00 +0000275void FunctionPassManagerImpl::anchor() {}
276
Devang Patel8c78a0b2007-05-03 01:11:54 +0000277char FunctionPassManagerImpl::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000278} // End of legacy namespace
279} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000280
Chandler Carruth7caea412013-11-09 12:26:54 +0000281namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000282//===----------------------------------------------------------------------===//
283// MPPassManager
284//
285/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000286/// It batches all Module passes and function pass managers together and
287/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000288class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000289public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000290 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000291 explicit MPPassManager() :
292 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000293
294 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000295 ~MPPassManager() override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000296 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel2ff44922007-04-16 20:39:59 +0000297 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
298 I != E; ++I) {
Devang Patel68f72b12007-04-26 17:50:19 +0000299 FunctionPassManagerImpl *FPP = I->second;
Devang Patel2ff44922007-04-16 20:39:59 +0000300 delete FPP;
301 }
302 }
303
Dan Gohmande6188a2010-08-12 23:50:08 +0000304 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000305 Pass *createPrinterPass(raw_ostream &O,
306 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000307 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000308 }
309
Devang Patelca58e352006-11-08 10:05:38 +0000310 /// run - Execute all of the passes scheduled for execution. Keep track of
311 /// whether any of the passes modifies the module, and if so, return true.
312 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000313
Pedro Artigase4348b02012-12-03 21:56:57 +0000314 using llvm::Pass::doInitialization;
315 using llvm::Pass::doFinalization;
316
Owen Anderson1aa27512012-11-15 00:14:15 +0000317 /// doInitialization - Run all of the initializers for the module passes.
318 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000319 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000320
321 /// doFinalization - Run all of the finalizers for the module passes.
322 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000323 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000324
Devang Patelf9d96b92006-12-07 19:57:52 +0000325 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000326 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000327 Info.setPreservesAll();
328 }
329
Devang Patele64d3052007-04-16 20:12:57 +0000330 /// Add RequiredPass into list of lower level passes required by pass P.
331 /// RequiredPass is run on the fly by Pass Manager when P requests it
332 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000333 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000334
Dan Gohmande6188a2010-08-12 23:50:08 +0000335 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000336 /// required by module pass MP. Instantiate analysis pass, by using
337 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000338 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000339
Craig Topperf398d7c2014-03-05 06:35:38 +0000340 const char *getPassName() const override {
Devang Patele3858e62007-02-01 22:08:25 +0000341 return "Module Pass Manager";
342 }
343
Craig Topperf398d7c2014-03-05 06:35:38 +0000344 PMDataManager *getAsPMDataManager() override { return this; }
345 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000346
Devang Pateleda56172006-12-12 23:34:33 +0000347 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000348 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000349 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000350 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
351 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000352 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000353 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
354 OnTheFlyManagers.find(MP);
355 if (I != OnTheFlyManagers.end())
356 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000357 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000358 }
359 }
360
Devang Patelabfbe3b2006-12-16 00:56:26 +0000361 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000362 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000363 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000364 }
365
Craig Topperf398d7c2014-03-05 06:35:38 +0000366 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000367 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000368 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000369
370 private:
371 /// Collection of on the fly FPPassManagers. These managers manage
372 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000373 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000374};
375
Devang Patel8c78a0b2007-05-03 01:11:54 +0000376char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000377} // End anonymous namespace
378
379namespace llvm {
380namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000381//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000382// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000383//
Devang Patel09f162c2007-05-01 21:15:47 +0000384
Devang Patel67d6a5e2006-12-19 19:46:59 +0000385/// PassManagerImpl manages MPPassManagers
386class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000387 public PMDataManager,
388 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000389 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000390
391public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000392 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000393 explicit PassManagerImpl() :
394 Pass(PT_PassManager, ID), PMDataManager(),
395 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000396
Matthias Braun0880c602014-12-12 01:27:01 +0000397 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000398 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000399 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000400 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000401
402 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000403 Pass *createPrinterPass(raw_ostream &O,
404 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000405 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000406 }
407
Devang Patel376fefa2006-11-08 10:29:57 +0000408 /// run - Execute all of the passes scheduled for execution. Keep track of
409 /// whether any of the passes modifies the module, and if so, return true.
410 bool run(Module &M);
411
Pedro Artigase4348b02012-12-03 21:56:57 +0000412 using llvm::Pass::doInitialization;
413 using llvm::Pass::doFinalization;
414
Owen Anderson1aa27512012-11-15 00:14:15 +0000415 /// doInitialization - Run all of the initializers for the module passes.
416 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000417 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000418
419 /// doFinalization - Run all of the finalizers for the module passes.
420 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000421 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000422
Devang Patelf9d96b92006-12-07 19:57:52 +0000423 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000424 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000425 Info.setPreservesAll();
426 }
427
Craig Topperf398d7c2014-03-05 06:35:38 +0000428 PMDataManager *getAsPMDataManager() override { return this; }
429 Pass *getAsPass() override { return this; }
430 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000431 return PMT_ModulePassManager;
432 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000433
Devang Patel67d6a5e2006-12-19 19:46:59 +0000434 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000435 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000436 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
437 return MP;
438 }
Devang Patel376fefa2006-11-08 10:29:57 +0000439};
440
David Blaikiea379b1812011-12-20 02:50:00 +0000441void PassManagerImpl::anchor() {}
442
Devang Patel8c78a0b2007-05-03 01:11:54 +0000443char PassManagerImpl::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000444} // End of legacy namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000445} // End of llvm namespace
446
447namespace {
448
449//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000450/// TimingInfo Class - This class is used to calculate information about the
451/// amount of time each pass takes to execute. This only happens when
452/// -time-passes is enabled on the command line.
453///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000454
Owen Anderson5a6960f2009-06-18 20:51:00 +0000455static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000456
Nick Lewycky02d5f772009-10-25 06:33:48 +0000457class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000458 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000459 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000460public:
461 // Use 'create' member to get this.
462 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000463
Devang Patel1c3633e2007-01-29 23:10:37 +0000464 // TimingDtor - Print out information about timing information
465 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000466 // Delete all of the timers, which accumulate their info into the
467 // TimerGroup.
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000468 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
Chris Lattner707431c2010-03-30 04:03:22 +0000469 E = TimingData.end(); I != E; ++I)
470 delete I->second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000471 // TimerGroup is deleted next, printing the report.
472 }
473
474 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000475 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000476 // null. It may be called multiple times.
477 static void createTheTimeInfo();
478
Chris Lattner707431c2010-03-30 04:03:22 +0000479 /// getPassTimer - Return the timer for the specified pass if it exists.
480 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000481 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000482 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000483
Owen Anderson5c96ef72009-07-07 18:33:04 +0000484 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000485 Timer *&T = TimingData[P];
Craig Topperc6207612014-04-09 06:08:46 +0000486 if (!T)
Chris Lattner707431c2010-03-30 04:03:22 +0000487 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000488 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000489 }
490};
491
Devang Patel1c3633e2007-01-29 23:10:37 +0000492} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000493
Dan Gohmand78c4002008-05-13 00:00:25 +0000494static TimingInfo *TheTimeInfo;
495
Devang Patela1514cb2006-12-07 19:39:39 +0000496//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000497// PMTopLevelManager implementation
498
Devang Patel4268fc02007-01-16 02:00:38 +0000499/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000500PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
501 PMDM->setTopLevelManager(this);
502 addPassManager(PMDM);
503 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000504}
505
Devang Patelafb1f3622006-12-12 22:35:25 +0000506/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000507void
Bill Wendlingea857e12012-05-14 07:53:40 +0000508PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000509 unsigned PDepth = 0;
510 if (P->getResolver())
511 PDepth = P->getResolver()->getPMDataManager().getDepth();
512
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000513 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000514 E = AnalysisPasses.end(); I != E; ++I) {
515 Pass *AP = *I;
516 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000517
Devang Patel01919d22007-03-08 19:05:01 +0000518 if (P == AP)
519 continue;
520
Tobias Grosserf07426b2011-01-20 21:03:22 +0000521 // Update the last users of passes that are required transitive by AP.
522 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
523 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
524 SmallVector<Pass *, 12> LastUses;
525 SmallVector<Pass *, 12> LastPMUses;
526 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
527 E = IDs.end(); I != E; ++I) {
528 Pass *AnalysisPass = findAnalysisPass(*I);
529 assert(AnalysisPass && "Expected analysis pass to exist.");
530 AnalysisResolver *AR = AnalysisPass->getResolver();
531 assert(AR && "Expected analysis resolver to exist.");
532 unsigned APDepth = AR->getPMDataManager().getDepth();
533
534 if (PDepth == APDepth)
535 LastUses.push_back(AnalysisPass);
536 else if (PDepth > APDepth)
537 LastPMUses.push_back(AnalysisPass);
538 }
539
540 setLastUser(LastUses, P);
541
542 // If this pass has a corresponding pass manager, push higher level
543 // analysis to this pass manager.
544 if (P->getResolver())
545 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
546
547
Devang Patelafb1f3622006-12-12 22:35:25 +0000548 // If AP is the last user of other passes then make P last user of
549 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000550 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000551 LUE = LastUser.end(); LUI != LUE; ++LUI) {
552 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000553 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000554 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000555 LastUser[LUI->first] = P;
556 }
557 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000558}
559
560/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000561void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000562 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000563 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000564 InversedLastUser.find(P);
565 if (DMI == InversedLastUser.end())
566 return;
567
568 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000569 for (Pass *LUP : LU) {
570 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000571 }
572
Devang Patelafb1f3622006-12-12 22:35:25 +0000573}
574
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000575AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000576 AnalysisUsage *AnUsage = nullptr;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000577 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000578 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000579 AnUsage = DMI->second;
580 else {
581 AnUsage = new AnalysisUsage();
582 P->getAnalysisUsage(*AnUsage);
583 AnUsageMap[P] = AnUsage;
584 }
585 return AnUsage;
586}
587
Devang Patelafb1f3622006-12-12 22:35:25 +0000588/// Schedule pass P for execution. Make sure that passes required by
589/// P are run before P is run. Update analysis info maintained by
590/// the manager. Remove dead passes. This is a recursive function.
591void PMTopLevelManager::schedulePass(Pass *P) {
592
Devang Patel3312f752007-01-16 21:43:18 +0000593 // TODO : Allocate function manager for this pass, other wise required set
594 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000595
Devang Pateld74ede72007-03-06 01:06:16 +0000596 // Give pass a chance to prepare the stage.
597 P->preparePassManager(activeStack);
598
Devang Patel864970e2008-03-18 00:39:19 +0000599 // If P is an analysis pass and it is available then do not
600 // generate the analysis again. Stale analysis info should not be
601 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000602 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000603 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000604 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000605 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000606 }
Devang Patel864970e2008-03-18 00:39:19 +0000607
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000608 AnalysisUsage *AnUsage = findAnalysisUsage(P);
609
Devang Patelfdee7032008-08-14 23:07:48 +0000610 bool checkAnalysis = true;
611 while (checkAnalysis) {
612 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000613
Devang Patelfdee7032008-08-14 23:07:48 +0000614 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
615 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
616 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000617
Devang Patelfdee7032008-08-14 23:07:48 +0000618 Pass *AnalysisPass = findAnalysisPass(*I);
619 if (!AnalysisPass) {
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000620 const PassInfo *PI = findAnalysisPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000621
Craig Topperc6207612014-04-09 06:08:46 +0000622 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000623 // Pass P is not in the global PassRegistry
624 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
625 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
626 dbgs() << "Required Passes:" << "\n";
627 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
628 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
629 Pass *AnalysisPass2 = findAnalysisPass(*I2);
630 if (AnalysisPass2) {
631 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000632 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000633 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
634 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
635 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
636 }
637 }
638 }
639
Andrew Trick6bbaf132011-06-03 00:48:58 +0000640 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000641 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000642 if (P->getPotentialPassManagerType () ==
643 AnalysisPass->getPotentialPassManagerType())
644 // Schedule analysis pass that is managed by the same pass manager.
645 schedulePass(AnalysisPass);
646 else if (P->getPotentialPassManagerType () >
647 AnalysisPass->getPotentialPassManagerType()) {
648 // Schedule analysis pass that is managed by a new manager.
649 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000650 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000651 // are already checked are still available.
652 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000653 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000654 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000655 // passes are run on the fly.
656 delete AnalysisPass;
657 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000658 }
659 }
660
661 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000662 if (ImmutablePass *IP = P->getAsImmutablePass()) {
663 // P is a immutable pass and it will be managed by this
664 // top level manager. Set up analysis resolver to connect them.
665 PMDataManager *DM = getAsPMDataManager();
666 AnalysisResolver *AR = new AnalysisResolver(*DM);
667 P->setResolver(AR);
668 DM->initializeAnalysisImpl(P);
669 addImmutablePass(IP);
670 DM->recordAvailableAnalysis(IP);
671 return;
672 }
673
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000674 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000675 Pass *PP = P->createPrinterPass(
676 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
677 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
678 }
679
680 // Add the requested pass to the best available pass manager.
681 P->assignPassManager(activeStack, getTopLevelPassManagerType());
682
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000683 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000684 Pass *PP = P->createPrinterPass(
685 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
686 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
687 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000688}
689
690/// Find the pass that implements Analysis AID. Search immutable
691/// passes and all pass managers. If desired pass is not found
692/// then return NULL.
693Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
694
Devang Patelcd6ba152006-12-12 22:50:05 +0000695 // Check pass managers
Dan Gohman7224bce2010-10-12 00:11:18 +0000696 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000697 E = PassManagers.end(); I != E; ++I)
698 if (Pass *P = (*I)->findAnalysisPass(AID, false))
699 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000700
701 // Check other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000702 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattner60987362009-03-06 05:53:14 +0000703 I = IndirectPassManagers.begin(),
Dan Gohman844dd0a2010-10-11 23:19:01 +0000704 E = IndirectPassManagers.end(); I != E; ++I)
705 if (Pass *P = (*I)->findAnalysisPass(AID, false))
706 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000707
Dan Gohman844dd0a2010-10-11 23:19:01 +0000708 // Check the immutable passes. Iterate in reverse order so that we find
709 // the most recently registered passes first.
Craig Topper31ee5862013-07-03 15:07:05 +0000710 for (SmallVectorImpl<ImmutablePass *>::reverse_iterator I =
Dan Gohman844dd0a2010-10-11 23:19:01 +0000711 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000712 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000713 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000714 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000715
716 // If Pass not found then check the interfaces implemented by Immutable Pass
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000717 const PassInfo *PassInf = findAnalysisPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000718 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000719 const std::vector<const PassInfo*> &ImmPI =
720 PassInf->getInterfacesImplemented();
721 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
722 EE = ImmPI.end(); II != EE; ++II) {
723 if ((*II)->getTypeInfo() == AID)
724 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000725 }
726 }
727
Craig Topperc6207612014-04-09 06:08:46 +0000728 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000729}
730
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000731const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
732 const PassInfo *&PI = AnalysisPassInfos[AID];
733 if (!PI)
734 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
735 else
736 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
737 "The pass info pointer changed for an analysis ID!");
738
739 return PI;
740}
741
Devang Pateleda56172006-12-12 23:34:33 +0000742// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000743void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000744
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000745 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000746 return;
747
Devang Pateleda56172006-12-12 23:34:33 +0000748 // Print out the immutable passes
749 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000750 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000751 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000752
Dan Gohmanf71c5212010-08-19 01:29:07 +0000753 // Every class that derives from PMDataManager also derives from Pass
754 // (sometimes indirectly), but there's no inheritance relationship
755 // between PMDataManager and Pass, so we have to getAsPass to get
756 // from a PMDataManager* to a Pass*.
Craig Topper31ee5862013-07-03 15:07:05 +0000757 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
758 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Dan Gohmanf71c5212010-08-19 01:29:07 +0000759 (*I)->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000760}
761
Devang Patel991aeba2006-12-15 20:13:01 +0000762void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000763
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000764 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000765 return;
766
David Greene994e1bb2010-01-05 01:30:02 +0000767 dbgs() << "Pass Arguments: ";
Craig Topper31ee5862013-07-03 15:07:05 +0000768 for (SmallVectorImpl<ImmutablePass *>::const_iterator I =
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000769 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000770 if (const PassInfo *PI = findAnalysisPassInfo((*I)->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000771 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000772 if (!PI->isAnalysisGroup())
773 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000774 }
Craig Topper31ee5862013-07-03 15:07:05 +0000775 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
776 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000777 (*I)->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() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000782 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000783 E = PassManagers.end(); I != E; ++I)
784 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000785
Devang Patele3068402006-12-21 00:16:50 +0000786 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000787 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000788 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
789 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000790 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000791
Chris Lattner60987362009-03-06 05:53:14 +0000792 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000793 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000794 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000795 InversedLastUser.find(DMI->second);
796 if (InvDMI != InversedLastUser.end()) {
797 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
798 L.insert(DMI->first);
799 } else {
800 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
801 InversedLastUser[DMI->second] = L;
802 }
803 }
Devang Patele3068402006-12-21 00:16:50 +0000804}
805
Devang Patele7599552007-01-12 18:52:44 +0000806/// Destructor
807PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000808 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000809 E = PassManagers.end(); I != E; ++I)
810 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000811
Dan Gohman060d5ba2010-10-12 00:15:27 +0000812 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000813 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
814 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000815
816 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000817 DME = AnUsageMap.end(); DMI != DME; ++DMI)
818 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000819}
820
Devang Patelafb1f3622006-12-12 22:35:25 +0000821//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000822// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000823
Devang Patel643676c2006-11-11 01:10:19 +0000824/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000825void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000826 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000827
Chris Lattner60987362009-03-06 05:53:14 +0000828 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000829
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000830 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000831
Dan Gohmande6188a2010-08-12 23:50:08 +0000832 // This pass is the current implementation of all of the interfaces it
833 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000834 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000835 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000836 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000837 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000838 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000839}
840
Devang Patel9d9fc902007-03-06 17:52:53 +0000841// Return true if P preserves high level analysis used by other
842// passes managed by this manager
843bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000844 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000845 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000846 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000847
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000848 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000849 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000850 E = HigherLevelAnalysis.end(); I != E; ++I) {
851 Pass *P1 = *I;
Craig Topperc6207612014-04-09 06:08:46 +0000852 if (P1->getAsImmutablePass() == nullptr &&
Dan Gohman929391a2008-01-29 12:09:55 +0000853 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000854 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000855 PreservedSet.end())
856 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000857 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000858
Devang Patel9d9fc902007-03-06 17:52:53 +0000859 return true;
860}
861
Chris Lattner02eb94c2008-08-07 07:34:50 +0000862/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000863void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000864 // Don't do this unless assertions are enabled.
865#ifdef NDEBUG
866 return;
867#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000868 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
869 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000870
Devang Patelef432532007-07-19 05:36:09 +0000871 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000872 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000873 E = PreservedSet.end(); I != E; ++I) {
874 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000875 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000876 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000877 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000878 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000879 }
880}
881
Devang Patel67c79a42008-07-01 19:50:56 +0000882/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000883void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000884 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
885 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000886 return;
887
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000888 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000889 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000890 E = AvailableAnalysis.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 &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000893 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000894 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000895 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000896 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000897 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000898 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
899 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000900 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000901 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000902 }
Devang Patel349170f2006-11-11 01:24:55 +0000903 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000904
Devang Patel42dd1e92007-03-06 01:55:46 +0000905 // Check inherited analysis also. If P is not preserving analysis
906 // provided by parent manager then remove it here.
907 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
908
909 if (!InheritedAnalysis[Index])
910 continue;
911
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000912 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000913 I = InheritedAnalysis[Index]->begin(),
914 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000915 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000916 if (Info->second->getAsImmutablePass() == nullptr &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000917 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000918 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000919 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000920 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000921 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000922 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
923 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000924 }
Devang Patel01919d22007-03-08 19:05:01 +0000925 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000926 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000927 }
928 }
Devang Patelf68a3492006-11-07 22:35:17 +0000929}
930
Devang Patelca189262006-11-14 03:05:08 +0000931/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000932void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000933 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000934
Devang Patel8adae862007-07-20 18:04:54 +0000935 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000936
Devang Patel2ff44922007-04-16 20:39:59 +0000937 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000938 if (!TPM)
939 return;
940
Devang Patel17ad0962006-12-08 00:37:52 +0000941 TPM->collectLastUses(DeadPasses, P);
942
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000943 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000944 dbgs() << " -*- '" << P->getPassName();
945 dbgs() << "' is the last user of following pass instances.";
946 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000947 }
948
Dan Gohman060d5ba2010-10-12 00:15:27 +0000949 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000950 E = DeadPasses.end(); I != E; ++I)
951 freePass(*I, Msg, DBG_STR);
952}
Devang Patel200d3052006-12-13 23:50:44 +0000953
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000954void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000955 enum PassDebuggingString DBG_STR) {
956 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000957
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000958 {
959 // If the pass crashes releasing memory, remember this.
960 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000961 TimeRegion PassTimer(getPassTimer(P));
962
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000963 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000964 }
965
Owen Andersona7aed182010-08-06 18:33:48 +0000966 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000967 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000968 // Remove the pass itself (if it is not already removed).
969 AvailableAnalysis.erase(PI);
970
971 // Remove all interfaces this pass implements, for which it is also
972 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000973 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000974 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000975 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000976 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000977 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000978 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000979 }
Devang Patel17ad0962006-12-08 00:37:52 +0000980 }
Devang Patelca189262006-11-14 03:05:08 +0000981}
982
Dan Gohmande6188a2010-08-12 23:50:08 +0000983/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000984/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000985void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000986 // This manager is going to manage pass P. Set up analysis resolver
987 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000988 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000989 P->setResolver(AR);
990
Devang Patelec2b9a72007-03-05 22:57:49 +0000991 // If a FunctionPass F is the last user of ModulePass info M
992 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000993 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000994
Chris Lattner60987362009-03-06 05:53:14 +0000995 if (!ProcessAnalysis) {
996 // Add pass
997 PassVector.push_back(P);
998 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000999 }
Devang Patel8cad70d2006-11-11 01:51:02 +00001000
Chris Lattner60987362009-03-06 05:53:14 +00001001 // At the moment, this pass is the last user of all required passes.
1002 SmallVector<Pass *, 12> LastUses;
1003 SmallVector<Pass *, 8> RequiredPasses;
1004 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
1005
1006 unsigned PDepth = this->getDepth();
1007
Dan Gohmande6188a2010-08-12 23:50:08 +00001008 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +00001009 ReqAnalysisNotAvailable, P);
Dan Gohman060d5ba2010-10-12 00:15:27 +00001010 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001011 E = RequiredPasses.end(); I != E; ++I) {
1012 Pass *PRequired = *I;
1013 unsigned RDepth = 0;
1014
1015 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1016 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1017 RDepth = DM.getDepth();
1018
1019 if (PDepth == RDepth)
1020 LastUses.push_back(PRequired);
1021 else if (PDepth > RDepth) {
1022 // Let the parent claim responsibility of last use
1023 TransferLastUses.push_back(PRequired);
1024 // Keep track of higher level analysis used by this manager.
1025 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +00001026 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001027 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001028 }
1029
1030 // Set P as P's last user until someone starts using P.
1031 // However, if P is a Pass Manager then it does not need
1032 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001033 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001034 LastUses.push_back(P);
1035 TPM->setLastUser(LastUses, P);
1036
1037 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001038 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001039 TPM->setLastUser(TransferLastUses, My_PM);
1040 TransferLastUses.clear();
1041 }
1042
Dan Gohman6304db32010-08-16 22:57:28 +00001043 // Now, take care of required analyses that are not available.
Dan Gohman060d5ba2010-10-12 00:15:27 +00001044 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +00001045 I = ReqAnalysisNotAvailable.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001046 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001047 const PassInfo *PI = TPM->findAnalysisPassInfo(*I);
Owen Andersona7aed182010-08-06 18:33:48 +00001048 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001049 this->addLowerLevelRequiredPass(P, AnalysisPass);
1050 }
1051
1052 // Take a note of analysis required and made available by this pass.
1053 // Remove the analysis not preserved by this pass
1054 removeNotPreservedAnalysis(P);
1055 recordAvailableAnalysis(P);
1056
Devang Patel8cad70d2006-11-11 01:51:02 +00001057 // Add pass
1058 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001059}
1060
Devang Patele64d3052007-04-16 20:12:57 +00001061
1062/// Populate RP with analysis pass that are required by
1063/// pass P and are available. Populate RP_NotAvail with analysis
1064/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001065void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1066 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001067 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001068 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1069 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohmande6188a2010-08-12 23:50:08 +00001070 for (AnalysisUsage::VectorType::const_iterator
Chris Lattner60987362009-03-06 05:53:14 +00001071 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001072 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001073 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001074 else
Chris Lattner60987362009-03-06 05:53:14 +00001075 RP_NotAvail.push_back(*I);
Devang Patel1d6267c2006-12-07 23:05:44 +00001076 }
Devang Patelf58183d2006-12-12 23:09:32 +00001077
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001078 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnercbd160f2008-08-08 05:33:04 +00001079 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patelf58183d2006-12-12 23:09:32 +00001080 E = IDs.end(); I != E; ++I) {
Devang Patele64d3052007-04-16 20:12:57 +00001081 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001082 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001083 else
Chris Lattner60987362009-03-06 05:53:14 +00001084 RP_NotAvail.push_back(*I);
Devang Patelf58183d2006-12-12 23:09:32 +00001085 }
Devang Patel1d6267c2006-12-07 23:05:44 +00001086}
1087
Devang Patel07f4f582006-11-14 21:49:36 +00001088// All Required analyses should be available to the pass as it runs! Here
1089// we fill in the AnalysisImpls member of the pass so that it can
1090// successfully use the getAnalysis() method to retrieve the
1091// implementations it needs.
1092//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001093void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001094 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1095
Chris Lattnercbd160f2008-08-08 05:33:04 +00001096 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001097 I = AnUsage->getRequiredSet().begin(),
1098 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001099 Pass *Impl = findAnalysisPass(*I, true);
Craig Topperc6207612014-04-09 06:08:46 +00001100 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001101 // This may be analysis pass that is initialized on the fly.
1102 // If that is not the case then it will raise an assert when it is used.
1103 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001104 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001105 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001106 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001107 }
1108}
1109
Devang Patel640c5bb2006-12-08 22:30:11 +00001110/// Find the pass that implements Analysis AID. If desired pass is not found
1111/// then return NULL.
1112Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1113
1114 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001115 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001116
1117 if (I != AvailableAnalysis.end())
1118 return I->second;
1119
1120 // Search Parents through TopLevelManager
1121 if (SearchParent)
1122 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001123
Craig Topperc6207612014-04-09 06:08:46 +00001124 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001125}
1126
Devang Patel991aeba2006-12-15 20:13:01 +00001127// Print list of passes that are last used by P.
1128void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1129
Devang Patel8adae862007-07-20 18:04:54 +00001130 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001131
1132 // If this is a on the fly manager then it does not have TPM.
1133 if (!TPM)
1134 return;
1135
Devang Patel991aeba2006-12-15 20:13:01 +00001136 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001137
Dan Gohman7224bce2010-10-12 00:11:18 +00001138 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001139 E = LUses.end(); I != E; ++I) {
Eric Christophera13839f2014-02-26 23:27:16 +00001140 dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001141 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001142 }
1143}
1144
1145void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001146 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001147 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001148 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001149 PMD->dumpPassArguments();
1150 else
Owen Andersona7aed182010-08-06 18:33:48 +00001151 if (const PassInfo *PI =
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001152 TPM->findAnalysisPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001153 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001154 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001155 }
1156}
1157
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001158void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1159 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001160 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001161 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001162 return;
Chandler Carruth20c56932014-04-27 23:59:25 +00001163 dbgs() << "[" << sys::TimeValue::now().str() << "] " << (void *)this
1164 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001165 switch (S1) {
1166 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001167 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001168 break;
1169 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001170 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001171 break;
1172 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001173 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001174 break;
1175 default:
1176 break;
1177 }
1178 switch (S2) {
1179 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001180 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001181 break;
1182 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001183 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001184 break;
1185 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001186 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001187 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001188 case ON_REGION_MSG:
1189 dbgs() << "' on Region '" << Msg << "'...\n";
1190 break;
Devang Patel003a5592007-03-05 20:01:30 +00001191 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001192 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001193 break;
1194 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001195 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001196 break;
1197 default:
1198 break;
1199 }
Devang Patel991aeba2006-12-15 20:13:01 +00001200}
1201
Chris Lattner4c1e9542009-03-06 06:45:05 +00001202void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001203 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001204 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001205
Chris Lattner4c493d92008-08-08 15:14:09 +00001206 AnalysisUsage analysisUsage;
1207 P->getAnalysisUsage(analysisUsage);
1208 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1209}
1210
Chris Lattner4c1e9542009-03-06 06:45:05 +00001211void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001212 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001213 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001214
Chris Lattner4c493d92008-08-08 15:14:09 +00001215 AnalysisUsage analysisUsage;
1216 P->getAnalysisUsage(analysisUsage);
1217 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1218}
1219
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001220void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001221 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001222 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001223 if (Set.empty())
1224 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001225 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001226 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001227 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001228 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001229 if (!PInf) {
1230 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1231 // all drivers.
1232 dbgs() << " Uninitialized Pass";
1233 continue;
1234 }
Owen Andersona7aed182010-08-06 18:33:48 +00001235 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001236 }
David Greene994e1bb2010-01-05 01:30:02 +00001237 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001238}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001239
Devang Patel004937b2007-07-27 20:06:09 +00001240/// Add RequiredPass into list of lower level passes required by pass P.
1241/// RequiredPass is run on the fly by Pass Manager when P requests it
1242/// through getAnalysis interface.
1243/// This should be handled by specific pass manager.
1244void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1245 if (TPM) {
1246 TPM->dumpArguments();
1247 TPM->dumpPasses();
1248 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001249
Dan Gohmande6188a2010-08-12 23:50:08 +00001250 // Module Level pass may required Function Level analysis info
1251 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1252 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001253 // module level pass is requiring lower level analysis info managed by
1254 // lower level pass manager.
1255
1256 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001257 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001258 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001259#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001260 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1261 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001262#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001263 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001264}
1265
Owen Andersona7aed182010-08-06 18:33:48 +00001266Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001267 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001268}
1269
Devang Patele7599552007-01-12 18:52:44 +00001270// Destructor
1271PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001272 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001273 E = PassVector.end(); I != E; ++I)
1274 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001275}
1276
Devang Patel9bdf7d42006-12-08 23:28:54 +00001277//===----------------------------------------------------------------------===//
1278// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001279// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1280Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001281 return PM.findAnalysisPass(ID, dir);
1282}
1283
Dan Gohmande6188a2010-08-12 23:50:08 +00001284Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001285 Function &F) {
1286 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1287}
1288
Devang Patela1514cb2006-12-07 19:39:39 +00001289//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001290// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001291
Dan Gohmande6188a2010-08-12 23:50:08 +00001292/// Execute all of the passes scheduled for execution by invoking
1293/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001294/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001295bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001296 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001297 return false;
1298
Devang Patele9585592006-12-08 01:38:28 +00001299 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001300
Devang Patel6e5a1132006-11-07 21:31:57 +00001301 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001302 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1303 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001304 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001305
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001306 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001307 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001308
Devang Patelabfbe3b2006-12-16 00:56:26 +00001309 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001310
Chris Lattner4c1e9542009-03-06 06:45:05 +00001311 {
1312 // If the pass crashes, remember this.
1313 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001314 TimeRegion PassTimer(getPassTimer(BP));
1315
Dan Gohman74b189f2010-03-01 17:34:28 +00001316 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001317 }
Devang Patel93a197c2006-12-14 00:08:04 +00001318
Dan Gohman74b189f2010-03-01 17:34:28 +00001319 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001320 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001321 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001322 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001323 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001324
Devang Patela273d1c2007-07-19 18:02:32 +00001325 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001326 removeNotPreservedAnalysis(BP);
1327 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001328 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001329 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001330
Bill Wendling6ce6d262009-12-25 13:50:18 +00001331 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001332}
1333
Devang Patel475c4532006-12-08 00:59:05 +00001334// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001335bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001336 bool Changed = false;
1337
Chris Lattner4c1e9542009-03-06 06:45:05 +00001338 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1339 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001340
1341 return Changed;
1342}
1343
Duncan Sands51495602009-02-13 09:42:34 +00001344bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001345 bool Changed = false;
1346
Pedro Artigas41b98842012-12-05 17:12:22 +00001347 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001348 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001349
1350 return Changed;
1351}
1352
Duncan Sands51495602009-02-13 09:42:34 +00001353bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001354 bool Changed = false;
1355
Devang Patelabfbe3b2006-12-16 00:56:26 +00001356 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1357 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001358 Changed |= BP->doInitialization(F);
1359 }
1360
1361 return Changed;
1362}
1363
Duncan Sands51495602009-02-13 09:42:34 +00001364bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001365 bool Changed = false;
1366
Devang Patelabfbe3b2006-12-16 00:56:26 +00001367 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1368 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001369 Changed |= BP->doFinalization(F);
1370 }
1371
1372 return Changed;
1373}
1374
1375
Devang Patela1514cb2006-12-07 19:39:39 +00001376//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001377// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001378
Devang Patel4e12f862006-11-08 10:44:40 +00001379/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001380FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001381 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001382 // FPM is the top level manager.
1383 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001384
Dan Gohman565df952008-03-13 02:08:36 +00001385 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001386 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001387}
1388
Devang Patelb67904d2006-12-13 02:36:01 +00001389FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001390 delete FPM;
1391}
1392
Dan Gohmande6188a2010-08-12 23:50:08 +00001393void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001394 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001395}
1396
Devang Patel9f3083e2006-11-15 19:39:54 +00001397/// run - Execute all of the passes scheduled for execution. Keep
1398/// track of whether any of the passes modifies the function, and if
1399/// so, return true.
1400///
Devang Patelb67904d2006-12-13 02:36:01 +00001401bool FunctionPassManager::run(Function &F) {
Rafael Espindola246c4fb2014-11-01 16:46:18 +00001402 if (std::error_code EC = F.materialize())
1403 report_fatal_error("Error reading bitcode file: " + EC.message());
Devang Patel272908d2006-12-08 22:57:48 +00001404 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001405}
1406
1407
Devang Patelff631ae2006-11-15 01:27:05 +00001408/// doInitialization - Run all of the initializers for the function passes.
1409///
Devang Patelb67904d2006-12-13 02:36:01 +00001410bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001411 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001412}
1413
Dan Gohmane6656eb2007-07-30 14:51:13 +00001414/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001415///
Devang Patelb67904d2006-12-13 02:36:01 +00001416bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001417 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001418}
1419
Devang Patela1514cb2006-12-07 19:39:39 +00001420//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001421// FunctionPassManagerImpl implementation
1422//
Duncan Sands51495602009-02-13 09:42:34 +00001423bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001424 bool Changed = false;
1425
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001426 dumpArguments();
1427 dumpPasses();
1428
Pedro Artigas41b98842012-12-05 17:12:22 +00001429 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1430 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1431 E = IPV.end(); I != E; ++I) {
1432 Changed |= (*I)->doInitialization(M);
1433 }
1434
Chris Lattner4c1e9542009-03-06 06:45:05 +00001435 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1436 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001437
1438 return Changed;
1439}
1440
Duncan Sands51495602009-02-13 09:42:34 +00001441bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001442 bool Changed = false;
1443
Pedro Artigas41b98842012-12-05 17:12:22 +00001444 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001445 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001446
Pedro Artigas41b98842012-12-05 17:12:22 +00001447 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1448 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1449 E = IPV.end(); I != E; ++I) {
1450 Changed |= (*I)->doFinalization(M);
1451 }
1452
Devang Patel67d6a5e2006-12-19 19:46:59 +00001453 return Changed;
1454}
1455
Devang Patelec9c58f2009-04-01 22:34:41 +00001456/// cleanup - After running all passes, clean up pass manager cache.
1457void FPPassManager::cleanup() {
1458 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1459 FunctionPass *FP = getContainedPass(Index);
1460 AnalysisResolver *AR = FP->getResolver();
1461 assert(AR && "Analysis Resolver is not set");
1462 AR->clearAnalysisImpls();
1463 }
1464}
1465
Torok Edwin24c78352009-06-29 18:49:09 +00001466void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1467 if (!wasRun)
1468 return;
1469 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1470 FPPassManager *FPPM = getContainedManager(Index);
1471 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1472 FPPM->getContainedPass(Index)->releaseMemory();
1473 }
1474 }
Torok Edwin896556e2009-06-29 21:05:10 +00001475 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001476}
1477
Devang Patel67d6a5e2006-12-19 19:46:59 +00001478// Execute all the passes managed by this top level manager.
1479// Return true if any function is modified by a pass.
1480bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001481 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001482 TimingInfo::createTheTimeInfo();
1483
Devang Patele3068402006-12-21 00:16:50 +00001484 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001485 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001486 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001487 F.getContext().yield();
1488 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001489
1490 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1491 getContainedManager(Index)->cleanup();
1492
Torok Edwin24c78352009-06-29 18:49:09 +00001493 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001494 return Changed;
1495}
1496
1497//===----------------------------------------------------------------------===//
1498// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001499
Devang Patel8c78a0b2007-05-03 01:11:54 +00001500char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001501/// Print passes managed by this manager
1502void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001503 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001504 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1505 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001506 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001507 dumpLastUses(FP, Offset+1);
1508 }
1509}
1510
1511
Dan Gohmande6188a2010-08-12 23:50:08 +00001512/// Execute all of the passes scheduled for execution by invoking
1513/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001514/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001515bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001516 if (F.isDeclaration())
1517 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001518
1519 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001520
Devang Patelcbbf2912008-03-20 01:09:53 +00001521 // Collect inherited analysis from Module level pass manager.
1522 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001523
Devang Patelabfbe3b2006-12-16 00:56:26 +00001524 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1525 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001526 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001527
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001528 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001529 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001530
Devang Patelabfbe3b2006-12-16 00:56:26 +00001531 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001532
Chris Lattner4c1e9542009-03-06 06:45:05 +00001533 {
1534 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001535 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001536
Dan Gohman74b189f2010-03-01 17:34:28 +00001537 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001538 }
Devang Patel93a197c2006-12-14 00:08:04 +00001539
Dan Gohman74b189f2010-03-01 17:34:28 +00001540 Changed |= LocalChanged;
1541 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001542 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001543 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001544
Devang Patela273d1c2007-07-19 18:02:32 +00001545 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001546 removeNotPreservedAnalysis(FP);
1547 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001548 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001549 }
1550 return Changed;
1551}
1552
Devang Patel67d6a5e2006-12-19 19:46:59 +00001553bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001554 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001555
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001556 for (Function &F : M)
1557 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001559 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001560}
1561
Duncan Sands51495602009-02-13 09:42:34 +00001562bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001563 bool Changed = false;
1564
Chris Lattner4c1e9542009-03-06 06:45:05 +00001565 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1566 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001567
Devang Patelff631ae2006-11-15 01:27:05 +00001568 return Changed;
1569}
1570
Duncan Sands51495602009-02-13 09:42:34 +00001571bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001572 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001573
1574 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001575 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001576
Devang Patelff631ae2006-11-15 01:27:05 +00001577 return Changed;
1578}
1579
Devang Patela1514cb2006-12-07 19:39:39 +00001580//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001581// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001582
Dan Gohmande6188a2010-08-12 23:50:08 +00001583/// Execute all of the passes scheduled for execution by invoking
1584/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001585/// the module, and if so, return true.
1586bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001587MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001588 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001589
Torok Edwin24c78352009-06-29 18:49:09 +00001590 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001591 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1592 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001593 Changed |= FPP->doInitialization(M);
1594 }
1595
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001596 // Initialize module passes
1597 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1598 Changed |= getContainedPass(Index)->doInitialization(M);
1599
Devang Patelabfbe3b2006-12-16 00:56:26 +00001600 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1601 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001602 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001603
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001604 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001605 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001606
Devang Patelabfbe3b2006-12-16 00:56:26 +00001607 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001608
Chris Lattner4c1e9542009-03-06 06:45:05 +00001609 {
1610 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001611 TimeRegion PassTimer(getPassTimer(MP));
1612
Dan Gohman74b189f2010-03-01 17:34:28 +00001613 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001614 }
Devang Patel93a197c2006-12-14 00:08:04 +00001615
Dan Gohman74b189f2010-03-01 17:34:28 +00001616 Changed |= LocalChanged;
1617 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001618 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001619 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001620 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001621
Devang Patela273d1c2007-07-19 18:02:32 +00001622 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001623 removeNotPreservedAnalysis(MP);
1624 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001625 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001626 }
Torok Edwin24c78352009-06-29 18:49:09 +00001627
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001628 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001629 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001630 Changed |= getContainedPass(Index)->doFinalization(M);
1631
Torok Edwin24c78352009-06-29 18:49:09 +00001632 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001633 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1634 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001635 // We don't know when is the last time an on-the-fly pass is run,
1636 // so we need to releaseMemory / finalize here
1637 FPP->releaseMemoryOnTheFly();
1638 Changed |= FPP->doFinalization(M);
1639 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001640
Devang Patel05e1a972006-11-07 22:03:15 +00001641 return Changed;
1642}
1643
Devang Patele64d3052007-04-16 20:12:57 +00001644/// Add RequiredPass into list of lower level passes required by pass P.
1645/// RequiredPass is run on the fly by Pass Manager when P requests it
1646/// through getAnalysis interface.
1647void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001648 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1649 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001650 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001651 RequiredPass->getPotentialPassManagerType()) &&
1652 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001653 if (!RequiredPass)
1654 return;
Devang Patele64d3052007-04-16 20:12:57 +00001655
Devang Patel68f72b12007-04-26 17:50:19 +00001656 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001657 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001658 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001659 // FPP is the top level manager.
1660 FPP->setTopLevelManager(FPP);
1661
Devang Patel69e9f6d2007-04-16 20:27:05 +00001662 OnTheFlyManagers[P] = FPP;
1663 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001664 const PassInfo *RequiredPassPI =
1665 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001666
Craig Topperc6207612014-04-09 06:08:46 +00001667 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001668 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1669 FoundPass =
1670 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001671 }
Andrew Trick02066f22014-04-08 03:40:34 +00001672 if (!FoundPass) {
1673 FoundPass = RequiredPass;
1674 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001675 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001676 FPP->add(RequiredPass);
1677 }
1678 // Register P as the last user of FoundPass or RequiredPass.
1679 SmallVector<Pass *, 1> LU;
1680 LU.push_back(FoundPass);
1681 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001682}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001683
Dan Gohmande6188a2010-08-12 23:50:08 +00001684/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001685/// required by module pass MP. Instantiate analysis pass, by using
1686/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001687Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001688 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001689 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001690
Torok Edwin24c78352009-06-29 18:49:09 +00001691 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001692 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001693 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001694}
1695
1696
Devang Patela1514cb2006-12-07 19:39:39 +00001697//===----------------------------------------------------------------------===//
1698// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001699
Devang Patelab97cf42006-12-13 00:09:23 +00001700//
Devang Patelc290c8a2006-11-07 22:23:34 +00001701/// run - Execute all of the passes scheduled for execution. Keep track of
1702/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001703bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001704 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001705 TimingInfo::createTheTimeInfo();
1706
Devang Patelcfd70c42006-12-13 22:10:00 +00001707 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001708 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001709
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001710 for (ImmutablePass *ImPass : getImmutablePasses())
1711 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001712
Devang Patele3068402006-12-21 00:16:50 +00001713 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001714 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001715 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001716 M.getContext().yield();
1717 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001718
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001719 for (ImmutablePass *ImPass : getImmutablePasses())
1720 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001721
Devang Patelc290c8a2006-11-07 22:23:34 +00001722 return Changed;
1723}
Devang Patel376fefa2006-11-08 10:29:57 +00001724
Devang Patela1514cb2006-12-07 19:39:39 +00001725//===----------------------------------------------------------------------===//
1726// PassManager implementation
1727
Devang Patel376fefa2006-11-08 10:29:57 +00001728/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001729PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001730 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001731 // PM is the top level manager
1732 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001733}
1734
Devang Patelb67904d2006-12-13 02:36:01 +00001735PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001736 delete PM;
1737}
1738
Chris Lattner60987362009-03-06 05:53:14 +00001739void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001740 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001741}
1742
1743/// run - Execute all of the passes scheduled for execution. Keep track of
1744/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001745bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001746 return PM->run(M);
1747}
1748
Devang Patelb8817b92006-12-14 00:59:42 +00001749//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001750// TimingInfo implementation
1751
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001752bool llvm::TimePassesIsEnabled = false;
1753static cl::opt<bool,true>
1754EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1755 cl::desc("Time each pass, printing elapsed time for each on exit"));
1756
Devang Patelb8817b92006-12-14 00:59:42 +00001757// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001758// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001759// null. It may be called multiple times.
1760void TimingInfo::createTheTimeInfo() {
1761 if (!TimePassesIsEnabled || TheTimeInfo) return;
1762
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001763 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001764 // This guarantees that the object will be constructed before static globals,
1765 // thus it will be destroyed before them.
1766 static ManagedStatic<TimingInfo> TTI;
1767 TheTimeInfo = &*TTI;
1768}
1769
Devang Patel1c3633e2007-01-29 23:10:37 +00001770/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001771Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001772 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001773 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001774 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001775}
1776
Devang Patel1c56a632007-01-08 19:29:38 +00001777//===----------------------------------------------------------------------===//
1778// PMStack implementation
1779//
Devang Patelad98d232007-01-11 22:15:30 +00001780
Devang Patel1c56a632007-01-08 19:29:38 +00001781// Pop Pass Manager from the stack and clear its analysis info.
1782void PMStack::pop() {
1783
1784 PMDataManager *Top = this->top();
1785 Top->initializeAnalysisInfo();
1786
1787 S.pop_back();
1788}
1789
1790// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001791void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001792 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001793 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001794
Chris Lattner60987362009-03-06 05:53:14 +00001795 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001796 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1797 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001798 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001799
Chris Lattner60987362009-03-06 05:53:14 +00001800 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001801 TPM->addIndirectPassManager(PM);
1802 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001803 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001804 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001805 assert((PM->getPassManagerType() == PMT_ModulePassManager
1806 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001807 && "pushing bad pass manager to PMStack");
1808 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001809 }
1810
Devang Patel15701b52007-01-11 00:19:00 +00001811 S.push_back(PM);
1812}
1813
1814// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001815void PMStack::dump() const {
1816 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattner60987362009-03-06 05:53:14 +00001817 E = S.end(); I != E; ++I)
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001818 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001819
Devang Patel15701b52007-01-11 00:19:00 +00001820 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001821 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001822}
1823
Devang Patel1c56a632007-01-08 19:29:38 +00001824/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001825/// add self into that manager.
1826void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001827 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001828 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001829 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001830 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1831 if (TopPMType == PreferredType)
1832 break; // We found desired pass manager
1833 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001834 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001835 else
1836 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001837 }
Devang Patel18ff6362008-09-09 21:38:40 +00001838 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001839 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001840}
1841
Devang Patel3312f752007-01-16 21:43:18 +00001842/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001843/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001844void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001845 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001846
Andrew Trickcbc845f2012-02-01 07:16:20 +00001847 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001848 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001849 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1850 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001851 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001852 break;
Devang Patel3312f752007-01-16 21:43:18 +00001853 }
Devang Patel3312f752007-01-16 21:43:18 +00001854
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001855 // Create new Function Pass Manager if needed.
1856 FPPassManager *FPP;
1857 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1858 FPP = (FPPassManager *)PMS.top();
1859 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001860 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1861 PMDataManager *PMD = PMS.top();
1862
1863 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001864 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001865 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001866
1867 // [2] Set up new manager's top level manager
1868 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1869 TPM->addIndirectPassManager(FPP);
1870
1871 // [3] Assign manager to manage this new manager. This may create
1872 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001873 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001874
1875 // [4] Push new manager into PMS
1876 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001877 }
1878
Devang Patel3312f752007-01-16 21:43:18 +00001879 // Assign FPP as the manager of this pass.
1880 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001881}
1882
Devang Patel3312f752007-01-16 21:43:18 +00001883/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001884/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001885void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001886 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001887 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001888
Devang Patel15701b52007-01-11 00:19:00 +00001889 // Basic Pass Manager is a leaf pass manager. It does not handle
1890 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001891 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001892 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1893 BBP = (BBPassManager *)PMS.top();
1894 } else {
1895 // If leaf manager is not Basic Block Pass manager then create new
1896 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001897 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1898 PMDataManager *PMD = PMS.top();
1899
1900 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001901 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001902
1903 // [2] Set up new manager's top level manager
1904 // Basic Block Pass Manager does not live by itself
1905 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1906 TPM->addIndirectPassManager(BBP);
1907
Devang Patel15701b52007-01-11 00:19:00 +00001908 // [3] Assign manager to manage this new manager. This may create
1909 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001910 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001911
Devang Patel3312f752007-01-16 21:43:18 +00001912 // [4] Push new manager into PMS
1913 PMS.push(BBP);
1914 }
Devang Patel1c56a632007-01-08 19:29:38 +00001915
Devang Patel3312f752007-01-16 21:43:18 +00001916 // Assign BBP as the manager of this pass.
1917 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001918}
1919
Dan Gohmand3a20c92008-03-11 16:41:42 +00001920PassManagerBase::~PassManagerBase() {}