blob: 3ed4adba4c72790254fe7afb39298358c172fe94 [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;
Alexander Kornienkof00654e2015-06-23 09:49:53 +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 {
Yaron Keren4849aa32015-06-05 17:48:47 +0000296 for (auto &OnTheFlyManager : OnTheFlyManagers) {
297 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000298 delete FPP;
299 }
300 }
301
Dan Gohmande6188a2010-08-12 23:50:08 +0000302 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000303 Pass *createPrinterPass(raw_ostream &O,
304 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000305 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000306 }
307
Devang Patelca58e352006-11-08 10:05:38 +0000308 /// run - Execute all of the passes scheduled for execution. Keep track of
309 /// whether any of the passes modifies the module, and if so, return true.
310 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000311
Pedro Artigase4348b02012-12-03 21:56:57 +0000312 using llvm::Pass::doInitialization;
313 using llvm::Pass::doFinalization;
314
Owen Anderson1aa27512012-11-15 00:14:15 +0000315 /// doInitialization - Run all of the initializers for the module passes.
316 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000317 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000318
319 /// doFinalization - Run all of the finalizers for the module passes.
320 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000321 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000322
Devang Patelf9d96b92006-12-07 19:57:52 +0000323 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000324 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000325 Info.setPreservesAll();
326 }
327
Devang Patele64d3052007-04-16 20:12:57 +0000328 /// Add RequiredPass into list of lower level passes required by pass P.
329 /// RequiredPass is run on the fly by Pass Manager when P requests it
330 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000331 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000332
Dan Gohmande6188a2010-08-12 23:50:08 +0000333 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000334 /// required by module pass MP. Instantiate analysis pass, by using
335 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000336 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000337
Craig Topperf398d7c2014-03-05 06:35:38 +0000338 const char *getPassName() const override {
Devang Patele3858e62007-02-01 22:08:25 +0000339 return "Module Pass Manager";
340 }
341
Craig Topperf398d7c2014-03-05 06:35:38 +0000342 PMDataManager *getAsPMDataManager() override { return this; }
343 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000344
Devang Pateleda56172006-12-12 23:34:33 +0000345 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000346 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000347 dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000348 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
349 ModulePass *MP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000350 MP->dumpPassStructure(Offset + 1);
Dan Gohman83ff1842009-07-01 23:12:33 +0000351 std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I =
352 OnTheFlyManagers.find(MP);
353 if (I != OnTheFlyManagers.end())
354 I->second->dumpPassStructure(Offset + 2);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000355 dumpLastUses(MP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000356 }
357 }
358
Devang Patelabfbe3b2006-12-16 00:56:26 +0000359 ModulePass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000360 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattner60987362009-03-06 05:53:14 +0000361 return static_cast<ModulePass *>(PassVector[N]);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000362 }
363
Craig Topperf398d7c2014-03-05 06:35:38 +0000364 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000365 return PMT_ModulePassManager;
Devang Patel28349ab2007-02-27 15:00:39 +0000366 }
Devang Patel69e9f6d2007-04-16 20:27:05 +0000367
368 private:
369 /// Collection of on the fly FPPassManagers. These managers manage
370 /// function passes that are required by module passes.
Devang Patel68f72b12007-04-26 17:50:19 +0000371 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelca58e352006-11-08 10:05:38 +0000372};
373
Devang Patel8c78a0b2007-05-03 01:11:54 +0000374char MPPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000375} // End anonymous namespace
376
377namespace llvm {
378namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000379//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000380// PassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000381//
Devang Patel09f162c2007-05-01 21:15:47 +0000382
Devang Patel67d6a5e2006-12-19 19:46:59 +0000383/// PassManagerImpl manages MPPassManagers
384class PassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000385 public PMDataManager,
386 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000387 virtual void anchor();
Devang Patel376fefa2006-11-08 10:29:57 +0000388
389public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000390 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000391 explicit PassManagerImpl() :
392 Pass(PT_PassManager, ID), PMDataManager(),
393 PMTopLevelManager(new MPPassManager()) {}
Devang Patel4c36e6b2006-12-07 23:24:58 +0000394
Matthias Braun0880c602014-12-12 01:27:01 +0000395 /// \copydoc PassManager::add()
Devang Patel31217af2006-12-07 21:32:57 +0000396 void add(Pass *P) {
Devang Pateldf6c9ae2006-12-08 22:34:02 +0000397 schedulePass(P);
Devang Patel31217af2006-12-07 21:32:57 +0000398 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000399
400 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000401 Pass *createPrinterPass(raw_ostream &O,
402 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000403 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000404 }
405
Devang Patel376fefa2006-11-08 10:29:57 +0000406 /// run - Execute all of the passes scheduled for execution. Keep track of
407 /// whether any of the passes modifies the module, and if so, return true.
408 bool run(Module &M);
409
Pedro Artigase4348b02012-12-03 21:56:57 +0000410 using llvm::Pass::doInitialization;
411 using llvm::Pass::doFinalization;
412
Owen Anderson1aa27512012-11-15 00:14:15 +0000413 /// doInitialization - Run all of the initializers for the module passes.
414 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000415 bool doInitialization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000416
417 /// doFinalization - Run all of the finalizers for the module passes.
418 ///
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000419 bool doFinalization();
Owen Anderson1aa27512012-11-15 00:14:15 +0000420
Devang Patelf9d96b92006-12-07 19:57:52 +0000421 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000422 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000423 Info.setPreservesAll();
424 }
425
Craig Topperf398d7c2014-03-05 06:35:38 +0000426 PMDataManager *getAsPMDataManager() override { return this; }
427 Pass *getAsPass() override { return this; }
428 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000429 return PMT_ModulePassManager;
430 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000431
Devang Patel67d6a5e2006-12-19 19:46:59 +0000432 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000433 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000434 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
435 return MP;
436 }
Devang Patel376fefa2006-11-08 10:29:57 +0000437};
438
David Blaikiea379b1812011-12-20 02:50:00 +0000439void PassManagerImpl::anchor() {}
440
Devang Patel8c78a0b2007-05-03 01:11:54 +0000441char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000442} // End of legacy namespace
443} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000444
445namespace {
446
447//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000448/// TimingInfo Class - This class is used to calculate information about the
449/// amount of time each pass takes to execute. This only happens when
450/// -time-passes is enabled on the command line.
451///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000452
Owen Anderson5a6960f2009-06-18 20:51:00 +0000453static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000454
Nick Lewycky02d5f772009-10-25 06:33:48 +0000455class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000456 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000457 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000458public:
459 // Use 'create' member to get this.
460 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000461
Devang Patel1c3633e2007-01-29 23:10:37 +0000462 // TimingDtor - Print out information about timing information
463 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000464 // Delete all of the timers, which accumulate their info into the
465 // TimerGroup.
Yaron Keren4849aa32015-06-05 17:48:47 +0000466 for (auto &I : TimingData)
467 delete I.second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000468 // TimerGroup is deleted next, printing the report.
469 }
470
471 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000472 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000473 // null. It may be called multiple times.
474 static void createTheTimeInfo();
475
Chris Lattner707431c2010-03-30 04:03:22 +0000476 /// getPassTimer - Return the timer for the specified pass if it exists.
477 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000478 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000479 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000480
Owen Anderson5c96ef72009-07-07 18:33:04 +0000481 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000482 Timer *&T = TimingData[P];
Craig Topperc6207612014-04-09 06:08:46 +0000483 if (!T)
Chris Lattner707431c2010-03-30 04:03:22 +0000484 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000485 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000486 }
487};
488
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000489} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000490
Dan Gohmand78c4002008-05-13 00:00:25 +0000491static TimingInfo *TheTimeInfo;
492
Devang Patela1514cb2006-12-07 19:39:39 +0000493//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000494// PMTopLevelManager implementation
495
Devang Patel4268fc02007-01-16 02:00:38 +0000496/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000497PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
498 PMDM->setTopLevelManager(this);
499 addPassManager(PMDM);
500 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000501}
502
Devang Patelafb1f3622006-12-12 22:35:25 +0000503/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000504void
Bill Wendlingea857e12012-05-14 07:53:40 +0000505PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000506 unsigned PDepth = 0;
507 if (P->getResolver())
508 PDepth = P->getResolver()->getPMDataManager().getDepth();
509
Yaron Keren4849aa32015-06-05 17:48:47 +0000510 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000511 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000512
Devang Patel01919d22007-03-08 19:05:01 +0000513 if (P == AP)
514 continue;
515
Tobias Grosserf07426b2011-01-20 21:03:22 +0000516 // Update the last users of passes that are required transitive by AP.
517 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
518 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
519 SmallVector<Pass *, 12> LastUses;
520 SmallVector<Pass *, 12> LastPMUses;
521 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
522 E = IDs.end(); I != E; ++I) {
523 Pass *AnalysisPass = findAnalysisPass(*I);
524 assert(AnalysisPass && "Expected analysis pass to exist.");
525 AnalysisResolver *AR = AnalysisPass->getResolver();
526 assert(AR && "Expected analysis resolver to exist.");
527 unsigned APDepth = AR->getPMDataManager().getDepth();
528
529 if (PDepth == APDepth)
530 LastUses.push_back(AnalysisPass);
531 else if (PDepth > APDepth)
532 LastPMUses.push_back(AnalysisPass);
533 }
534
535 setLastUser(LastUses, P);
536
537 // If this pass has a corresponding pass manager, push higher level
538 // analysis to this pass manager.
539 if (P->getResolver())
540 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
541
542
Devang Patelafb1f3622006-12-12 22:35:25 +0000543 // If AP is the last user of other passes then make P last user of
544 // such passes.
Devang Patelc68a0b62008-08-12 00:26:16 +0000545 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patelafb1f3622006-12-12 22:35:25 +0000546 LUE = LastUser.end(); LUI != LUE; ++LUI) {
547 if (LUI->second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000548 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000549 // this is just updating existing entries.
Devang Patelafb1f3622006-12-12 22:35:25 +0000550 LastUser[LUI->first] = P;
551 }
552 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000553}
554
555/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000556void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000557 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000558 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000559 InversedLastUser.find(P);
560 if (DMI == InversedLastUser.end())
561 return;
562
563 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000564 for (Pass *LUP : LU) {
565 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000566 }
567
Devang Patelafb1f3622006-12-12 22:35:25 +0000568}
569
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000570AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000571 AnalysisUsage *AnUsage = nullptr;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000572 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000573 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000574 AnUsage = DMI->second;
575 else {
576 AnUsage = new AnalysisUsage();
577 P->getAnalysisUsage(*AnUsage);
578 AnUsageMap[P] = AnUsage;
579 }
580 return AnUsage;
581}
582
Devang Patelafb1f3622006-12-12 22:35:25 +0000583/// Schedule pass P for execution. Make sure that passes required by
584/// P are run before P is run. Update analysis info maintained by
585/// the manager. Remove dead passes. This is a recursive function.
586void PMTopLevelManager::schedulePass(Pass *P) {
587
Devang Patel3312f752007-01-16 21:43:18 +0000588 // TODO : Allocate function manager for this pass, other wise required set
589 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000590
Devang Pateld74ede72007-03-06 01:06:16 +0000591 // Give pass a chance to prepare the stage.
592 P->preparePassManager(activeStack);
593
Devang Patel864970e2008-03-18 00:39:19 +0000594 // If P is an analysis pass and it is available then do not
595 // generate the analysis again. Stale analysis info should not be
596 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000597 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000598 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000599 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000600 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000601 }
Devang Patel864970e2008-03-18 00:39:19 +0000602
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000603 AnalysisUsage *AnUsage = findAnalysisUsage(P);
604
Devang Patelfdee7032008-08-14 23:07:48 +0000605 bool checkAnalysis = true;
606 while (checkAnalysis) {
607 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000608
Devang Patelfdee7032008-08-14 23:07:48 +0000609 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
610 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
611 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000612
Devang Patelfdee7032008-08-14 23:07:48 +0000613 Pass *AnalysisPass = findAnalysisPass(*I);
614 if (!AnalysisPass) {
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000615 const PassInfo *PI = findAnalysisPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000616
Craig Topperc6207612014-04-09 06:08:46 +0000617 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000618 // Pass P is not in the global PassRegistry
619 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
620 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
621 dbgs() << "Required Passes:" << "\n";
622 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
623 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
624 Pass *AnalysisPass2 = findAnalysisPass(*I2);
625 if (AnalysisPass2) {
626 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000627 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000628 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
629 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
630 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
631 }
632 }
633 }
634
Andrew Trick6bbaf132011-06-03 00:48:58 +0000635 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000636 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000637 if (P->getPotentialPassManagerType () ==
638 AnalysisPass->getPotentialPassManagerType())
639 // Schedule analysis pass that is managed by the same pass manager.
640 schedulePass(AnalysisPass);
641 else if (P->getPotentialPassManagerType () >
642 AnalysisPass->getPotentialPassManagerType()) {
643 // Schedule analysis pass that is managed by a new manager.
644 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000645 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000646 // are already checked are still available.
647 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000648 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000649 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000650 // passes are run on the fly.
651 delete AnalysisPass;
652 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000653 }
654 }
655
656 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000657 if (ImmutablePass *IP = P->getAsImmutablePass()) {
658 // P is a immutable pass and it will be managed by this
659 // top level manager. Set up analysis resolver to connect them.
660 PMDataManager *DM = getAsPMDataManager();
661 AnalysisResolver *AR = new AnalysisResolver(*DM);
662 P->setResolver(AR);
663 DM->initializeAnalysisImpl(P);
664 addImmutablePass(IP);
665 DM->recordAvailableAnalysis(IP);
666 return;
667 }
668
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000669 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000670 Pass *PP = P->createPrinterPass(
671 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
672 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
673 }
674
675 // Add the requested pass to the best available pass manager.
676 P->assignPassManager(activeStack, getTopLevelPassManagerType());
677
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000678 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000679 Pass *PP = P->createPrinterPass(
680 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
681 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
682 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000683}
684
685/// Find the pass that implements Analysis AID. Search immutable
686/// passes and all pass managers. If desired pass is not found
687/// then return NULL.
688Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
689
Devang Patelcd6ba152006-12-12 22:50:05 +0000690 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000691 for (PMDataManager *PassManager : PassManagers)
692 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000693 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000694
695 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000696 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
697 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000698 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000699
Dan Gohman844dd0a2010-10-11 23:19:01 +0000700 // Check the immutable passes. Iterate in reverse order so that we find
701 // the most recently registered passes first.
Yaron Keren4849aa32015-06-05 17:48:47 +0000702 for (auto I = ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E;
703 ++I) {
Owen Andersona7aed182010-08-06 18:33:48 +0000704 AnalysisID PI = (*I)->getPassID();
Devang Patelafb1f3622006-12-12 22:35:25 +0000705 if (PI == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000706 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000707
708 // If Pass not found then check the interfaces implemented by Immutable Pass
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000709 const PassInfo *PassInf = findAnalysisPassInfo(PI);
Andrew Trick6bbaf132011-06-03 00:48:58 +0000710 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman844dd0a2010-10-11 23:19:01 +0000711 const std::vector<const PassInfo*> &ImmPI =
712 PassInf->getInterfacesImplemented();
Yaron Keren4849aa32015-06-05 17:48:47 +0000713 for (const PassInfo *PI : ImmPI)
714 if (PI->getTypeInfo() == AID)
Dan Gohman844dd0a2010-10-11 23:19:01 +0000715 return *I;
Devang Patelafb1f3622006-12-12 22:35:25 +0000716 }
717
Craig Topperc6207612014-04-09 06:08:46 +0000718 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000719}
720
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000721const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
722 const PassInfo *&PI = AnalysisPassInfos[AID];
723 if (!PI)
724 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
725 else
726 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
727 "The pass info pointer changed for an analysis ID!");
728
729 return PI;
730}
731
Devang Pateleda56172006-12-12 23:34:33 +0000732// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000733void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000734
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000735 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000736 return;
737
Devang Pateleda56172006-12-12 23:34:33 +0000738 // Print out the immutable passes
739 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000740 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000741 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000742
Dan Gohmanf71c5212010-08-19 01:29:07 +0000743 // Every class that derives from PMDataManager also derives from Pass
744 // (sometimes indirectly), but there's no inheritance relationship
745 // between PMDataManager and Pass, so we have to getAsPass to get
746 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000747 for (PMDataManager *Manager : PassManagers)
748 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000749}
750
Devang Patel991aeba2006-12-15 20:13:01 +0000751void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000752
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000753 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000754 return;
755
David Greene994e1bb2010-01-05 01:30:02 +0000756 dbgs() << "Pass Arguments: ";
Craig Topper31ee5862013-07-03 15:07:05 +0000757 for (SmallVectorImpl<ImmutablePass *>::const_iterator I =
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000758 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000759 if (const PassInfo *PI = findAnalysisPassInfo((*I)->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000760 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000761 if (!PI->isAnalysisGroup())
762 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000763 }
Craig Topper31ee5862013-07-03 15:07:05 +0000764 for (SmallVectorImpl<PMDataManager *>::const_iterator I =
765 PassManagers.begin(), E = PassManagers.end(); I != E; ++I)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000766 (*I)->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000767 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000768}
769
Devang Patele3068402006-12-21 00:16:50 +0000770void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000771 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattner4c1e9542009-03-06 06:45:05 +0000772 E = PassManagers.end(); I != E; ++I)
773 (*I)->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000774
Devang Patele3068402006-12-21 00:16:50 +0000775 // Initailize other pass managers
Dan Gohman060d5ba2010-10-12 00:15:27 +0000776 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohmande6188a2010-08-12 23:50:08 +0000777 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
778 I != E; ++I)
Devang Patele3068402006-12-21 00:16:50 +0000779 (*I)->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000780
Chris Lattner60987362009-03-06 05:53:14 +0000781 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patelc68a0b62008-08-12 00:26:16 +0000782 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000783 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000784 InversedLastUser.find(DMI->second);
785 if (InvDMI != InversedLastUser.end()) {
786 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
787 L.insert(DMI->first);
788 } else {
789 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
790 InversedLastUser[DMI->second] = L;
791 }
792 }
Devang Patele3068402006-12-21 00:16:50 +0000793}
794
Devang Patele7599552007-01-12 18:52:44 +0000795/// Destructor
796PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman060d5ba2010-10-12 00:15:27 +0000797 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patele7599552007-01-12 18:52:44 +0000798 E = PassManagers.end(); I != E; ++I)
799 delete *I;
Dan Gohmande6188a2010-08-12 23:50:08 +0000800
Dan Gohman060d5ba2010-10-12 00:15:27 +0000801 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patele7599552007-01-12 18:52:44 +0000802 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
803 delete *I;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000804
805 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattner60987362009-03-06 05:53:14 +0000806 DME = AnUsageMap.end(); DMI != DME; ++DMI)
807 delete DMI->second;
Devang Patele7599552007-01-12 18:52:44 +0000808}
809
Devang Patelafb1f3622006-12-12 22:35:25 +0000810//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000811// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000812
Devang Patel643676c2006-11-11 01:10:19 +0000813/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000814void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000815 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000816
Chris Lattner60987362009-03-06 05:53:14 +0000817 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000818
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000819 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000820
Dan Gohmande6188a2010-08-12 23:50:08 +0000821 // This pass is the current implementation of all of the interfaces it
822 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000823 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000824 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000825 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000826 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000827 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000828}
829
Devang Patel9d9fc902007-03-06 17:52:53 +0000830// Return true if P preserves high level analysis used by other
831// passes managed by this manager
832bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000833 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000834 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000835 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000836
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000837 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman060d5ba2010-10-12 00:15:27 +0000838 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel9d9fc902007-03-06 17:52:53 +0000839 E = HigherLevelAnalysis.end(); I != E; ++I) {
840 Pass *P1 = *I;
Craig Topperc6207612014-04-09 06:08:46 +0000841 if (P1->getAsImmutablePass() == nullptr &&
Dan Gohman929391a2008-01-29 12:09:55 +0000842 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohmande6188a2010-08-12 23:50:08 +0000843 P1->getPassID()) ==
Devang Patel01919d22007-03-08 19:05:01 +0000844 PreservedSet.end())
845 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000846 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000847
Devang Patel9d9fc902007-03-06 17:52:53 +0000848 return true;
849}
850
Chris Lattner02eb94c2008-08-07 07:34:50 +0000851/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000852void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000853 // Don't do this unless assertions are enabled.
854#ifdef NDEBUG
855 return;
856#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000857 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
858 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000859
Devang Patelef432532007-07-19 05:36:09 +0000860 // Verify preserved analysis
Chris Lattnercbd160f2008-08-08 05:33:04 +0000861 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patela273d1c2007-07-19 18:02:32 +0000862 E = PreservedSet.end(); I != E; ++I) {
863 AnalysisID AID = *I;
Dan Gohman4dbb3012009-09-28 00:27:48 +0000864 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000865 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000866 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000867 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000868 }
869}
870
Devang Patel67c79a42008-07-01 19:50:56 +0000871/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000872void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000873 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
874 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000875 return;
876
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000877 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000878 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000879 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000880 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000881 if (Info->second->getAsImmutablePass() == nullptr &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000882 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patelbb4720c2008-06-03 01:02:16 +0000883 PreservedSet.end()) {
Devang Patel349170f2006-11-11 01:24:55 +0000884 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000885 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000886 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000887 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
888 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000889 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000890 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000891 }
Devang Patel349170f2006-11-11 01:24:55 +0000892 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000893
Devang Patel42dd1e92007-03-06 01:55:46 +0000894 // Check inherited analysis also. If P is not preserving analysis
895 // provided by parent manager then remove it here.
896 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
897
898 if (!InheritedAnalysis[Index])
899 continue;
900
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000901 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000902 I = InheritedAnalysis[Index]->begin(),
903 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000904 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000905 if (Info->second->getAsImmutablePass() == nullptr &&
Dan Gohmande6188a2010-08-12 23:50:08 +0000906 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter46651412009-12-04 06:58:24 +0000907 PreservedSet.end()) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000908 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000909 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000910 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000911 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
912 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000913 }
Devang Patel01919d22007-03-08 19:05:01 +0000914 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000915 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000916 }
917 }
Devang Patelf68a3492006-11-07 22:35:17 +0000918}
919
Devang Patelca189262006-11-14 03:05:08 +0000920/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000921void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000922 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000923
Devang Patel8adae862007-07-20 18:04:54 +0000924 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000925
Devang Patel2ff44922007-04-16 20:39:59 +0000926 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000927 if (!TPM)
928 return;
929
Devang Patel17ad0962006-12-08 00:37:52 +0000930 TPM->collectLastUses(DeadPasses, P);
931
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000932 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000933 dbgs() << " -*- '" << P->getPassName();
934 dbgs() << "' is the last user of following pass instances.";
935 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000936 }
937
Dan Gohman060d5ba2010-10-12 00:15:27 +0000938 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000939 E = DeadPasses.end(); I != E; ++I)
940 freePass(*I, Msg, DBG_STR);
941}
Devang Patel200d3052006-12-13 23:50:44 +0000942
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000943void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000944 enum PassDebuggingString DBG_STR) {
945 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000946
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000947 {
948 // If the pass crashes releasing memory, remember this.
949 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000950 TimeRegion PassTimer(getPassTimer(P));
951
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000952 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000953 }
954
Owen Andersona7aed182010-08-06 18:33:48 +0000955 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000956 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000957 // Remove the pass itself (if it is not already removed).
958 AvailableAnalysis.erase(PI);
959
960 // Remove all interfaces this pass implements, for which it is also
961 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000962 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000963 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000964 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000965 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000966 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000967 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000968 }
Devang Patel17ad0962006-12-08 00:37:52 +0000969 }
Devang Patelca189262006-11-14 03:05:08 +0000970}
971
Dan Gohmande6188a2010-08-12 23:50:08 +0000972/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000973/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000974void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000975 // This manager is going to manage pass P. Set up analysis resolver
976 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000977 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000978 P->setResolver(AR);
979
Devang Patelec2b9a72007-03-05 22:57:49 +0000980 // If a FunctionPass F is the last user of ModulePass info M
981 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000982 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000983
Chris Lattner60987362009-03-06 05:53:14 +0000984 if (!ProcessAnalysis) {
985 // Add pass
986 PassVector.push_back(P);
987 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000988 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000989
Chris Lattner60987362009-03-06 05:53:14 +0000990 // At the moment, this pass is the last user of all required passes.
991 SmallVector<Pass *, 12> LastUses;
992 SmallVector<Pass *, 8> RequiredPasses;
993 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
994
995 unsigned PDepth = this->getDepth();
996
Dan Gohmande6188a2010-08-12 23:50:08 +0000997 collectRequiredAnalysis(RequiredPasses,
Chris Lattner60987362009-03-06 05:53:14 +0000998 ReqAnalysisNotAvailable, P);
Chandler Carruth2f02ea42015-08-18 18:41:53 +0000999 for (Pass *PRequired : RequiredPasses) {
Chris Lattner60987362009-03-06 05:53:14 +00001000 unsigned RDepth = 0;
1001
1002 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1003 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1004 RDepth = DM.getDepth();
1005
1006 if (PDepth == RDepth)
1007 LastUses.push_back(PRequired);
1008 else if (PDepth > RDepth) {
1009 // Let the parent claim responsibility of last use
1010 TransferLastUses.push_back(PRequired);
1011 // Keep track of higher level analysis used by this manager.
1012 HigherLevelAnalysis.push_back(PRequired);
Dan Gohmande6188a2010-08-12 23:50:08 +00001013 } else
Chris Lattner0ab5e2c2011-04-15 05:18:47 +00001014 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001015 }
1016
1017 // Set P as P's last user until someone starts using P.
1018 // However, if P is a Pass Manager then it does not need
1019 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001020 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001021 LastUses.push_back(P);
1022 TPM->setLastUser(LastUses, P);
1023
1024 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001025 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001026 TPM->setLastUser(TransferLastUses, My_PM);
1027 TransferLastUses.clear();
1028 }
1029
Dan Gohman6304db32010-08-16 22:57:28 +00001030 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001031 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1032 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001033 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001034 this->addLowerLevelRequiredPass(P, AnalysisPass);
1035 }
1036
1037 // Take a note of analysis required and made available by this pass.
1038 // Remove the analysis not preserved by this pass
1039 removeNotPreservedAnalysis(P);
1040 recordAvailableAnalysis(P);
1041
Devang Patel8cad70d2006-11-11 01:51:02 +00001042 // Add pass
1043 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001044}
1045
Devang Patele64d3052007-04-16 20:12:57 +00001046
1047/// Populate RP with analysis pass that are required by
1048/// pass P and are available. Populate RP_NotAvail with analysis
1049/// pass that are required by pass P but are not available.
Dan Gohman7224bce2010-10-12 00:11:18 +00001050void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1051 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patele64d3052007-04-16 20:12:57 +00001052 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001053 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001054 for (const auto &RequiredID : AnUsage->getRequiredSet())
1055 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001056 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001057 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001058 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001059
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001060 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1061 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Dan Gohmande6188a2010-08-12 23:50:08 +00001062 RP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001063 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001064 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001065}
1066
Devang Patel07f4f582006-11-14 21:49:36 +00001067// All Required analyses should be available to the pass as it runs! Here
1068// we fill in the AnalysisImpls member of the pass so that it can
1069// successfully use the getAnalysis() method to retrieve the
1070// implementations it needs.
1071//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001072void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001073 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1074
Chris Lattnercbd160f2008-08-08 05:33:04 +00001075 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001076 I = AnUsage->getRequiredSet().begin(),
1077 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001078 Pass *Impl = findAnalysisPass(*I, true);
Craig Topperc6207612014-04-09 06:08:46 +00001079 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001080 // This may be analysis pass that is initialized on the fly.
1081 // If that is not the case then it will raise an assert when it is used.
1082 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001083 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001084 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001085 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001086 }
1087}
1088
Devang Patel640c5bb2006-12-08 22:30:11 +00001089/// Find the pass that implements Analysis AID. If desired pass is not found
1090/// then return NULL.
1091Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1092
1093 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001094 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001095
1096 if (I != AvailableAnalysis.end())
1097 return I->second;
1098
1099 // Search Parents through TopLevelManager
1100 if (SearchParent)
1101 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001102
Craig Topperc6207612014-04-09 06:08:46 +00001103 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001104}
1105
Devang Patel991aeba2006-12-15 20:13:01 +00001106// Print list of passes that are last used by P.
1107void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1108
Devang Patel8adae862007-07-20 18:04:54 +00001109 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001110
1111 // If this is a on the fly manager then it does not have TPM.
1112 if (!TPM)
1113 return;
1114
Devang Patel991aeba2006-12-15 20:13:01 +00001115 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001116
Dan Gohman7224bce2010-10-12 00:11:18 +00001117 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001118 E = LUses.end(); I != E; ++I) {
Eric Christophera13839f2014-02-26 23:27:16 +00001119 dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001120 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001121 }
1122}
1123
1124void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001125 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001126 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001127 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001128 PMD->dumpPassArguments();
1129 else
Owen Andersona7aed182010-08-06 18:33:48 +00001130 if (const PassInfo *PI =
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001131 TPM->findAnalysisPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001132 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001133 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001134 }
1135}
1136
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001137void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1138 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001139 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001140 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001141 return;
Chandler Carruth20c56932014-04-27 23:59:25 +00001142 dbgs() << "[" << sys::TimeValue::now().str() << "] " << (void *)this
1143 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001144 switch (S1) {
1145 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001146 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001147 break;
1148 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001149 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001150 break;
1151 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001152 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001153 break;
1154 default:
1155 break;
1156 }
1157 switch (S2) {
1158 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001159 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001160 break;
1161 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001162 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001163 break;
1164 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001165 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001166 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001167 case ON_REGION_MSG:
1168 dbgs() << "' on Region '" << Msg << "'...\n";
1169 break;
Devang Patel003a5592007-03-05 20:01:30 +00001170 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001171 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001172 break;
1173 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001174 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001175 break;
1176 default:
1177 break;
1178 }
Devang Patel991aeba2006-12-15 20:13:01 +00001179}
1180
Chris Lattner4c1e9542009-03-06 06:45:05 +00001181void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001182 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001183 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001184
Chris Lattner4c493d92008-08-08 15:14:09 +00001185 AnalysisUsage analysisUsage;
1186 P->getAnalysisUsage(analysisUsage);
1187 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1188}
1189
Chris Lattner4c1e9542009-03-06 06:45:05 +00001190void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001191 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001192 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001193
Chris Lattner4c493d92008-08-08 15:14:09 +00001194 AnalysisUsage analysisUsage;
1195 P->getAnalysisUsage(analysisUsage);
1196 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1197}
1198
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001199void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001200 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001201 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001202 if (Set.empty())
1203 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001204 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001205 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001206 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001207 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001208 if (!PInf) {
1209 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1210 // all drivers.
1211 dbgs() << " Uninitialized Pass";
1212 continue;
1213 }
Owen Andersona7aed182010-08-06 18:33:48 +00001214 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001215 }
David Greene994e1bb2010-01-05 01:30:02 +00001216 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001217}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001218
Devang Patel004937b2007-07-27 20:06:09 +00001219/// Add RequiredPass into list of lower level passes required by pass P.
1220/// RequiredPass is run on the fly by Pass Manager when P requests it
1221/// through getAnalysis interface.
1222/// This should be handled by specific pass manager.
1223void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1224 if (TPM) {
1225 TPM->dumpArguments();
1226 TPM->dumpPasses();
1227 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001228
Dan Gohmande6188a2010-08-12 23:50:08 +00001229 // Module Level pass may required Function Level analysis info
1230 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1231 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001232 // module level pass is requiring lower level analysis info managed by
1233 // lower level pass manager.
1234
1235 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001236 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001237 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001238#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001239 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1240 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001241#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001242 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001243}
1244
Owen Andersona7aed182010-08-06 18:33:48 +00001245Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001246 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001247}
1248
Devang Patele7599552007-01-12 18:52:44 +00001249// Destructor
1250PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001251 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001252 E = PassVector.end(); I != E; ++I)
1253 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001254}
1255
Devang Patel9bdf7d42006-12-08 23:28:54 +00001256//===----------------------------------------------------------------------===//
1257// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001258// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1259Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001260 return PM.findAnalysisPass(ID, dir);
1261}
1262
Dan Gohmande6188a2010-08-12 23:50:08 +00001263Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001264 Function &F) {
1265 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1266}
1267
Devang Patela1514cb2006-12-07 19:39:39 +00001268//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001269// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001270
Dan Gohmande6188a2010-08-12 23:50:08 +00001271/// Execute all of the passes scheduled for execution by invoking
1272/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001273/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001274bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001275 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001276 return false;
1277
Devang Patele9585592006-12-08 01:38:28 +00001278 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001279
Devang Patel6e5a1132006-11-07 21:31:57 +00001280 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001281 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1282 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001283 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001284
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001285 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001286 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001287
Devang Patelabfbe3b2006-12-16 00:56:26 +00001288 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001289
Chris Lattner4c1e9542009-03-06 06:45:05 +00001290 {
1291 // If the pass crashes, remember this.
1292 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001293 TimeRegion PassTimer(getPassTimer(BP));
1294
Dan Gohman74b189f2010-03-01 17:34:28 +00001295 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001296 }
Devang Patel93a197c2006-12-14 00:08:04 +00001297
Dan Gohman74b189f2010-03-01 17:34:28 +00001298 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001299 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001300 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001301 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001302 dumpPreservedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001303
Devang Patela273d1c2007-07-19 18:02:32 +00001304 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001305 removeNotPreservedAnalysis(BP);
1306 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001307 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001308 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001309
Bill Wendling6ce6d262009-12-25 13:50:18 +00001310 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001311}
1312
Devang Patel475c4532006-12-08 00:59:05 +00001313// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001314bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001315 bool Changed = false;
1316
Chris Lattner4c1e9542009-03-06 06:45:05 +00001317 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1318 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001319
1320 return Changed;
1321}
1322
Duncan Sands51495602009-02-13 09:42:34 +00001323bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001324 bool Changed = false;
1325
Pedro Artigas41b98842012-12-05 17:12:22 +00001326 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001327 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001328
1329 return Changed;
1330}
1331
Duncan Sands51495602009-02-13 09:42:34 +00001332bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001333 bool Changed = false;
1334
Devang Patelabfbe3b2006-12-16 00:56:26 +00001335 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1336 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001337 Changed |= BP->doInitialization(F);
1338 }
1339
1340 return Changed;
1341}
1342
Duncan Sands51495602009-02-13 09:42:34 +00001343bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001344 bool Changed = false;
1345
Devang Patelabfbe3b2006-12-16 00:56:26 +00001346 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1347 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001348 Changed |= BP->doFinalization(F);
1349 }
1350
1351 return Changed;
1352}
1353
1354
Devang Patela1514cb2006-12-07 19:39:39 +00001355//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001356// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001357
Devang Patel4e12f862006-11-08 10:44:40 +00001358/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001359FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001360 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001361 // FPM is the top level manager.
1362 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001363
Dan Gohman565df952008-03-13 02:08:36 +00001364 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001365 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001366}
1367
Devang Patelb67904d2006-12-13 02:36:01 +00001368FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001369 delete FPM;
1370}
1371
Dan Gohmande6188a2010-08-12 23:50:08 +00001372void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001373 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001374}
1375
Devang Patel9f3083e2006-11-15 19:39:54 +00001376/// run - Execute all of the passes scheduled for execution. Keep
1377/// track of whether any of the passes modifies the function, and if
1378/// so, return true.
1379///
Devang Patelb67904d2006-12-13 02:36:01 +00001380bool FunctionPassManager::run(Function &F) {
Rafael Espindola246c4fb2014-11-01 16:46:18 +00001381 if (std::error_code EC = F.materialize())
1382 report_fatal_error("Error reading bitcode file: " + EC.message());
Devang Patel272908d2006-12-08 22:57:48 +00001383 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001384}
1385
1386
Devang Patelff631ae2006-11-15 01:27:05 +00001387/// doInitialization - Run all of the initializers for the function passes.
1388///
Devang Patelb67904d2006-12-13 02:36:01 +00001389bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001390 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001391}
1392
Dan Gohmane6656eb2007-07-30 14:51:13 +00001393/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001394///
Devang Patelb67904d2006-12-13 02:36:01 +00001395bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001396 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001397}
1398
Devang Patela1514cb2006-12-07 19:39:39 +00001399//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001400// FunctionPassManagerImpl implementation
1401//
Duncan Sands51495602009-02-13 09:42:34 +00001402bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001403 bool Changed = false;
1404
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001405 dumpArguments();
1406 dumpPasses();
1407
Yaron Keren4849aa32015-06-05 17:48:47 +00001408 for (ImmutablePass *ImPass : getImmutablePasses())
1409 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001410
Chris Lattner4c1e9542009-03-06 06:45:05 +00001411 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1412 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001413
1414 return Changed;
1415}
1416
Duncan Sands51495602009-02-13 09:42:34 +00001417bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001418 bool Changed = false;
1419
Pedro Artigas41b98842012-12-05 17:12:22 +00001420 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001421 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001422
Yaron Keren4849aa32015-06-05 17:48:47 +00001423 for (ImmutablePass *ImPass : getImmutablePasses())
1424 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001425
Devang Patel67d6a5e2006-12-19 19:46:59 +00001426 return Changed;
1427}
1428
Devang Patelec9c58f2009-04-01 22:34:41 +00001429/// cleanup - After running all passes, clean up pass manager cache.
1430void FPPassManager::cleanup() {
1431 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1432 FunctionPass *FP = getContainedPass(Index);
1433 AnalysisResolver *AR = FP->getResolver();
1434 assert(AR && "Analysis Resolver is not set");
1435 AR->clearAnalysisImpls();
1436 }
1437}
1438
Torok Edwin24c78352009-06-29 18:49:09 +00001439void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1440 if (!wasRun)
1441 return;
1442 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1443 FPPassManager *FPPM = getContainedManager(Index);
1444 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1445 FPPM->getContainedPass(Index)->releaseMemory();
1446 }
1447 }
Torok Edwin896556e2009-06-29 21:05:10 +00001448 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001449}
1450
Devang Patel67d6a5e2006-12-19 19:46:59 +00001451// Execute all the passes managed by this top level manager.
1452// Return true if any function is modified by a pass.
1453bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001454 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001455 TimingInfo::createTheTimeInfo();
1456
Devang Patele3068402006-12-21 00:16:50 +00001457 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001458 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001459 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001460 F.getContext().yield();
1461 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001462
1463 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1464 getContainedManager(Index)->cleanup();
1465
Torok Edwin24c78352009-06-29 18:49:09 +00001466 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001467 return Changed;
1468}
1469
1470//===----------------------------------------------------------------------===//
1471// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001472
Devang Patel8c78a0b2007-05-03 01:11:54 +00001473char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001474/// Print passes managed by this manager
1475void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001476 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001477 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1478 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001479 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001480 dumpLastUses(FP, Offset+1);
1481 }
1482}
1483
1484
Dan Gohmande6188a2010-08-12 23:50:08 +00001485/// Execute all of the passes scheduled for execution by invoking
1486/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001487/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001488bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001489 if (F.isDeclaration())
1490 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001491
1492 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001493
Devang Patelcbbf2912008-03-20 01:09:53 +00001494 // Collect inherited analysis from Module level pass manager.
1495 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001496
Devang Patelabfbe3b2006-12-16 00:56:26 +00001497 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1498 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001499 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001500
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001501 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001502 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001503
Devang Patelabfbe3b2006-12-16 00:56:26 +00001504 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001505
Chris Lattner4c1e9542009-03-06 06:45:05 +00001506 {
1507 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001508 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001509
Dan Gohman74b189f2010-03-01 17:34:28 +00001510 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001511 }
Devang Patel93a197c2006-12-14 00:08:04 +00001512
Dan Gohman74b189f2010-03-01 17:34:28 +00001513 Changed |= LocalChanged;
1514 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001515 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001516 dumpPreservedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001517
Devang Patela273d1c2007-07-19 18:02:32 +00001518 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001519 removeNotPreservedAnalysis(FP);
1520 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001521 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001522 }
1523 return Changed;
1524}
1525
Devang Patel67d6a5e2006-12-19 19:46:59 +00001526bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001527 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001528
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001529 for (Function &F : M)
1530 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001531
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001532 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001533}
1534
Duncan Sands51495602009-02-13 09:42:34 +00001535bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001536 bool Changed = false;
1537
Chris Lattner4c1e9542009-03-06 06:45:05 +00001538 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1539 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001540
Devang Patelff631ae2006-11-15 01:27:05 +00001541 return Changed;
1542}
1543
Duncan Sands51495602009-02-13 09:42:34 +00001544bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001545 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001546
1547 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001548 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001549
Devang Patelff631ae2006-11-15 01:27:05 +00001550 return Changed;
1551}
1552
Devang Patela1514cb2006-12-07 19:39:39 +00001553//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001554// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001555
Dan Gohmande6188a2010-08-12 23:50:08 +00001556/// Execute all of the passes scheduled for execution by invoking
1557/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001558/// the module, and if so, return true.
1559bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001560MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001561 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001562
Torok Edwin24c78352009-06-29 18:49:09 +00001563 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001564 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1565 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001566 Changed |= FPP->doInitialization(M);
1567 }
1568
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001569 // Initialize module passes
1570 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1571 Changed |= getContainedPass(Index)->doInitialization(M);
1572
Devang Patelabfbe3b2006-12-16 00:56:26 +00001573 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1574 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001575 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001576
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001577 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001578 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001579
Devang Patelabfbe3b2006-12-16 00:56:26 +00001580 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001581
Chris Lattner4c1e9542009-03-06 06:45:05 +00001582 {
1583 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001584 TimeRegion PassTimer(getPassTimer(MP));
1585
Dan Gohman74b189f2010-03-01 17:34:28 +00001586 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001587 }
Devang Patel93a197c2006-12-14 00:08:04 +00001588
Dan Gohman74b189f2010-03-01 17:34:28 +00001589 Changed |= LocalChanged;
1590 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001591 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001592 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001593 dumpPreservedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001594
Devang Patela273d1c2007-07-19 18:02:32 +00001595 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001596 removeNotPreservedAnalysis(MP);
1597 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001598 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001599 }
Torok Edwin24c78352009-06-29 18:49:09 +00001600
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001601 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001602 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001603 Changed |= getContainedPass(Index)->doFinalization(M);
1604
Torok Edwin24c78352009-06-29 18:49:09 +00001605 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001606 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1607 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001608 // We don't know when is the last time an on-the-fly pass is run,
1609 // so we need to releaseMemory / finalize here
1610 FPP->releaseMemoryOnTheFly();
1611 Changed |= FPP->doFinalization(M);
1612 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001613
Devang Patel05e1a972006-11-07 22:03:15 +00001614 return Changed;
1615}
1616
Devang Patele64d3052007-04-16 20:12:57 +00001617/// Add RequiredPass into list of lower level passes required by pass P.
1618/// RequiredPass is run on the fly by Pass Manager when P requests it
1619/// through getAnalysis interface.
1620void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001621 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1622 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001623 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001624 RequiredPass->getPotentialPassManagerType()) &&
1625 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001626 if (!RequiredPass)
1627 return;
Devang Patele64d3052007-04-16 20:12:57 +00001628
Devang Patel68f72b12007-04-26 17:50:19 +00001629 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001630 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001631 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001632 // FPP is the top level manager.
1633 FPP->setTopLevelManager(FPP);
1634
Devang Patel69e9f6d2007-04-16 20:27:05 +00001635 OnTheFlyManagers[P] = FPP;
1636 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001637 const PassInfo *RequiredPassPI =
1638 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001639
Craig Topperc6207612014-04-09 06:08:46 +00001640 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001641 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1642 FoundPass =
1643 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001644 }
Andrew Trick02066f22014-04-08 03:40:34 +00001645 if (!FoundPass) {
1646 FoundPass = RequiredPass;
1647 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001648 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001649 FPP->add(RequiredPass);
1650 }
1651 // Register P as the last user of FoundPass or RequiredPass.
1652 SmallVector<Pass *, 1> LU;
1653 LU.push_back(FoundPass);
1654 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001655}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001656
Dan Gohmande6188a2010-08-12 23:50:08 +00001657/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001658/// required by module pass MP. Instantiate analysis pass, by using
1659/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001660Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001661 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001662 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001663
Torok Edwin24c78352009-06-29 18:49:09 +00001664 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001665 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001666 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001667}
1668
1669
Devang Patela1514cb2006-12-07 19:39:39 +00001670//===----------------------------------------------------------------------===//
1671// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001672
Devang Patelab97cf42006-12-13 00:09:23 +00001673//
Devang Patelc290c8a2006-11-07 22:23:34 +00001674/// run - Execute all of the passes scheduled for execution. Keep track of
1675/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001676bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001677 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001678 TimingInfo::createTheTimeInfo();
1679
Devang Patelcfd70c42006-12-13 22:10:00 +00001680 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001681 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001682
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001683 for (ImmutablePass *ImPass : getImmutablePasses())
1684 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001685
Devang Patele3068402006-12-21 00:16:50 +00001686 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001687 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001688 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001689 M.getContext().yield();
1690 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001691
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001692 for (ImmutablePass *ImPass : getImmutablePasses())
1693 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001694
Devang Patelc290c8a2006-11-07 22:23:34 +00001695 return Changed;
1696}
Devang Patel376fefa2006-11-08 10:29:57 +00001697
Devang Patela1514cb2006-12-07 19:39:39 +00001698//===----------------------------------------------------------------------===//
1699// PassManager implementation
1700
Devang Patel376fefa2006-11-08 10:29:57 +00001701/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001702PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001703 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001704 // PM is the top level manager
1705 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001706}
1707
Devang Patelb67904d2006-12-13 02:36:01 +00001708PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001709 delete PM;
1710}
1711
Chris Lattner60987362009-03-06 05:53:14 +00001712void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001713 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001714}
1715
1716/// run - Execute all of the passes scheduled for execution. Keep track of
1717/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001718bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001719 return PM->run(M);
1720}
1721
Devang Patelb8817b92006-12-14 00:59:42 +00001722//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001723// TimingInfo implementation
1724
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001725bool llvm::TimePassesIsEnabled = false;
1726static cl::opt<bool,true>
1727EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1728 cl::desc("Time each pass, printing elapsed time for each on exit"));
1729
Devang Patelb8817b92006-12-14 00:59:42 +00001730// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001731// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001732// null. It may be called multiple times.
1733void TimingInfo::createTheTimeInfo() {
1734 if (!TimePassesIsEnabled || TheTimeInfo) return;
1735
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001736 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001737 // This guarantees that the object will be constructed before static globals,
1738 // thus it will be destroyed before them.
1739 static ManagedStatic<TimingInfo> TTI;
1740 TheTimeInfo = &*TTI;
1741}
1742
Devang Patel1c3633e2007-01-29 23:10:37 +00001743/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001744Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001745 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001746 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001747 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001748}
1749
Devang Patel1c56a632007-01-08 19:29:38 +00001750//===----------------------------------------------------------------------===//
1751// PMStack implementation
1752//
Devang Patelad98d232007-01-11 22:15:30 +00001753
Devang Patel1c56a632007-01-08 19:29:38 +00001754// Pop Pass Manager from the stack and clear its analysis info.
1755void PMStack::pop() {
1756
1757 PMDataManager *Top = this->top();
1758 Top->initializeAnalysisInfo();
1759
1760 S.pop_back();
1761}
1762
1763// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001764void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001765 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001766 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001767
Chris Lattner60987362009-03-06 05:53:14 +00001768 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001769 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1770 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001771 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001772
Chris Lattner60987362009-03-06 05:53:14 +00001773 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001774 TPM->addIndirectPassManager(PM);
1775 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001776 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001777 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001778 assert((PM->getPassManagerType() == PMT_ModulePassManager
1779 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001780 && "pushing bad pass manager to PMStack");
1781 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001782 }
1783
Devang Patel15701b52007-01-11 00:19:00 +00001784 S.push_back(PM);
1785}
1786
1787// Dump content of the pass manager stack.
Dan Gohman027ad432010-08-07 01:04:15 +00001788void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001789 for (PMDataManager *Manager : S)
1790 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001791
Devang Patel15701b52007-01-11 00:19:00 +00001792 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001793 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001794}
1795
Devang Patel1c56a632007-01-08 19:29:38 +00001796/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001797/// add self into that manager.
1798void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001799 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001800 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001801 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001802 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1803 if (TopPMType == PreferredType)
1804 break; // We found desired pass manager
1805 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001806 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001807 else
1808 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001809 }
Devang Patel18ff6362008-09-09 21:38:40 +00001810 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001811 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001812}
1813
Devang Patel3312f752007-01-16 21:43:18 +00001814/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001815/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001816void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001817 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001818
Andrew Trickcbc845f2012-02-01 07:16:20 +00001819 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001820 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001821 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1822 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001823 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001824 break;
Devang Patel3312f752007-01-16 21:43:18 +00001825 }
Devang Patel3312f752007-01-16 21:43:18 +00001826
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001827 // Create new Function Pass Manager if needed.
1828 FPPassManager *FPP;
1829 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1830 FPP = (FPPassManager *)PMS.top();
1831 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001832 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1833 PMDataManager *PMD = PMS.top();
1834
1835 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001836 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001837 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001838
1839 // [2] Set up new manager's top level manager
1840 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1841 TPM->addIndirectPassManager(FPP);
1842
1843 // [3] Assign manager to manage this new manager. This may create
1844 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001845 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001846
1847 // [4] Push new manager into PMS
1848 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001849 }
1850
Devang Patel3312f752007-01-16 21:43:18 +00001851 // Assign FPP as the manager of this pass.
1852 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001853}
1854
Devang Patel3312f752007-01-16 21:43:18 +00001855/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001856/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001857void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001858 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001859 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001860
Devang Patel15701b52007-01-11 00:19:00 +00001861 // Basic Pass Manager is a leaf pass manager. It does not handle
1862 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001863 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001864 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1865 BBP = (BBPassManager *)PMS.top();
1866 } else {
1867 // If leaf manager is not Basic Block Pass manager then create new
1868 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001869 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1870 PMDataManager *PMD = PMS.top();
1871
1872 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001873 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001874
1875 // [2] Set up new manager's top level manager
1876 // Basic Block Pass Manager does not live by itself
1877 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1878 TPM->addIndirectPassManager(BBP);
1879
Devang Patel15701b52007-01-11 00:19:00 +00001880 // [3] Assign manager to manage this new manager. This may create
1881 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001882 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001883
Devang Patel3312f752007-01-16 21:43:18 +00001884 // [4] Push new manager into PMS
1885 PMS.push(BBP);
1886 }
Devang Patel1c56a632007-01-08 19:29:38 +00001887
Devang Patel3312f752007-01-16 21:43:18 +00001888 // Assign BBP as the manager of this pass.
1889 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001890}
1891
Dan Gohmand3a20c92008-03-11 16:41:42 +00001892PassManagerBase::~PassManagerBase() {}