blob: 458aeb8a38f9506ad24281a7d06f5eddebdce3e6 [file] [log] [blame]
Chandler Carruth7caea412013-11-09 12:26:54 +00001//===- LegacyPassManager.cpp - LLVM Pass Infrastructure Implementation ----===//
Devang Patel6e5a1132006-11-07 21:31:57 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Devang Patel6e5a1132006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruth7caea412013-11-09 12:26:54 +000010// This file implements the legacy LLVM Pass Manager infrastructure.
Devang Patel6e5a1132006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth7caea412013-11-09 12:26:54 +000014#include "llvm/IR/LegacyPassManager.h"
Pavel Labathec534e62016-10-25 16:20:07 +000015#include "llvm/IR/IRPrintingPasses.h"
16#include "llvm/IR/LLVMContext.h"
Chandler Carruth7caea412013-11-09 12:26:54 +000017#include "llvm/IR/LegacyPassManagers.h"
Chandler Carruth1b69ed82014-03-04 12:32:42 +000018#include "llvm/IR/LegacyPassNameParser.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000019#include "llvm/IR/Module.h"
Pavel Labathec534e62016-10-25 16:20:07 +000020#include "llvm/Support/Chrono.h"
Devang Patelf1567a52006-12-13 20:03:48 +000021#include "llvm/Support/CommandLine.h"
David Greene994e1bb2010-01-05 01:30:02 +000022#include "llvm/Support/Debug.h"
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +000023#include "llvm/Support/Error.h"
Torok Edwin6dd27302009-07-08 18:01:40 +000024#include "llvm/Support/ErrorHandling.h"
Devang Patelb8817b92006-12-14 00:59:42 +000025#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000026#include "llvm/Support/Mutex.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>
Weiming Zhao0f1762c2016-01-06 22:55:03 +000031#include <unordered_set>
Dan Gohman8c43e412007-10-03 19:04:09 +000032using namespace llvm;
Chandler Carruth7caea412013-11-09 12:26:54 +000033using namespace llvm::legacy;
Devang Patelffca9102006-12-15 19:39:30 +000034
Devang Patele7599552007-01-12 18:52:44 +000035// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patel6fea2852006-12-07 18:23:30 +000036
Devang Patelf1567a52006-12-13 20:03:48 +000037//===----------------------------------------------------------------------===//
38// Pass debugging information. Often it is useful to find out what pass is
39// running when a crash occurs in a utility. When this library is compiled with
40// debugging on, a command line option (--debug-pass) is enabled that causes the
41// pass name to be printed before it executes.
42//
43
Chandler Carruth7caea412013-11-09 12:26:54 +000044namespace {
Devang Patel03fb5872006-12-13 21:13:31 +000045// Different debug levels that can be enabled...
46enum PassDebugLevel {
Dmitri Gribenko3238fb72013-05-05 00:40:33 +000047 Disabled, Arguments, Structure, Executions, Details
Devang Patel03fb5872006-12-13 21:13:31 +000048};
Chandler Carruth7caea412013-11-09 12:26:54 +000049}
Devang Patel03fb5872006-12-13 21:13:31 +000050
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000051static cl::opt<enum PassDebugLevel>
52PassDebugging("debug-pass", cl::Hidden,
Devang Patelf1567a52006-12-13 20:03:48 +000053 cl::desc("Print PassManager debugging information"),
54 cl::values(
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000055 clEnumVal(Disabled , "disable debug output"),
56 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
57 clEnumVal(Structure , "print pass structure before run()"),
58 clEnumVal(Executions, "print pass name before it is executed"),
Mehdi Amini732afdd2016-10-08 19:41:06 +000059 clEnumVal(Details , "print pass details when it is executed")));
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
Weiming Zhao0f1762c2016-01-06 22:55:03 +000086static cl::list<std::string>
87 PrintFuncsList("filter-print-funcs", cl::value_desc("function names"),
88 cl::desc("Only print IR for functions whose name "
89 "match this for all print-[before|after][-all] "
90 "options"),
91 cl::CommaSeparated);
92
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000093/// This is a helper to determine whether to print IR before or
94/// after a pass.
95
96static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
97 PassOptionList &PassesToPrint) {
Chris Bieneman664294c2015-04-29 21:45:22 +000098 for (auto *PassInf : PassesToPrint) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +000099 if (PassInf)
100 if (PassInf->getPassArgument() == PI->getPassArgument()) {
101 return true;
102 }
David Greene9b063df2010-04-02 23:17:14 +0000103 }
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000104 return false;
105}
Dan Gohmande6188a2010-08-12 23:50:08 +0000106
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000107/// This is a utility to check whether a pass should have IR dumped
108/// before it.
109static bool ShouldPrintBeforePass(const PassInfo *PI) {
110 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
111}
David Greene9b063df2010-04-02 23:17:14 +0000112
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000113/// This is a utility to check whether a pass should have IR dumped
114/// after it.
115static bool ShouldPrintAfterPass(const PassInfo *PI) {
116 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene9b063df2010-04-02 23:17:14 +0000117}
118
Weiming Zhao0f1762c2016-01-06 22:55:03 +0000119bool llvm::isFunctionInPrintList(StringRef FunctionName) {
120 static std::unordered_set<std::string> PrintFuncNames(PrintFuncsList.begin(),
121 PrintFuncsList.end());
122 return PrintFuncNames.empty() || PrintFuncNames.count(FunctionName);
123}
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000124/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
125/// or higher is specified.
126bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000127 return PassDebugging >= Executions;
Chris Lattnerd4d966f2009-09-15 05:03:04 +0000128}
129
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000130
131
132
Chris Lattner4c1e9542009-03-06 06:45:05 +0000133void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
Craig Topperc6207612014-04-09 06:08:46 +0000134 if (!V && !M)
Chris Lattner4c1e9542009-03-06 06:45:05 +0000135 OS << "Releasing pass '";
136 else
137 OS << "Running pass '";
Dan Gohmande6188a2010-08-12 23:50:08 +0000138
Chris Lattner4c1e9542009-03-06 06:45:05 +0000139 OS << P->getPassName() << "'";
Dan Gohmande6188a2010-08-12 23:50:08 +0000140
Chris Lattner4c1e9542009-03-06 06:45:05 +0000141 if (M) {
142 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
143 return;
144 }
Craig Topperc6207612014-04-09 06:08:46 +0000145 if (!V) {
Chris Lattner4c1e9542009-03-06 06:45:05 +0000146 OS << '\n';
147 return;
148 }
149
Dan Gohman79fc0e92009-03-10 18:47:59 +0000150 OS << " on ";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000151 if (isa<Function>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000152 OS << "function";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000153 else if (isa<BasicBlock>(V))
Dan Gohman79fc0e92009-03-10 18:47:59 +0000154 OS << "basic block";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000155 else
Dan Gohman79fc0e92009-03-10 18:47:59 +0000156 OS << "value";
157
158 OS << " '";
Chandler Carruthd48cdbf2014-01-09 02:29:41 +0000159 V->printAsOperand(OS, /*PrintTy=*/false, M);
Dan Gohman79fc0e92009-03-10 18:47:59 +0000160 OS << "'\n";
Chris Lattner4c1e9542009-03-06 06:45:05 +0000161}
162
163
Devang Patelffca9102006-12-15 19:39:30 +0000164namespace {
Devang Patelf33f3eb2006-12-07 19:21:29 +0000165//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000166// BBPassManager
Devang Patel10c2ca62006-12-12 22:47:13 +0000167//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000168/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelca58e352006-11-08 10:05:38 +0000169/// pass together and sequence them to process one basic block before
170/// processing next basic block.
Nick Lewycky02d5f772009-10-25 06:33:48 +0000171class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelca58e352006-11-08 10:05:38 +0000172
173public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000174 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000175 explicit BBPassManager()
176 : PMDataManager(), FunctionPass(ID) {}
Devang Patelca58e352006-11-08 10:05:38 +0000177
Devang Patelca58e352006-11-08 10:05:38 +0000178 /// Execute all of the passes scheduled for execution. Keep track of
179 /// whether any of the passes modifies the function, and if so, return true.
Craig Topperf398d7c2014-03-05 06:35:38 +0000180 bool runOnFunction(Function &F) override;
Devang Patelca58e352006-11-08 10:05:38 +0000181
Devang Patelf9d96b92006-12-07 19:57:52 +0000182 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000183 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000184 Info.setPreservesAll();
185 }
186
Craig Topperf398d7c2014-03-05 06:35:38 +0000187 bool doInitialization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000188 bool doInitialization(Function &F);
Craig Topperf398d7c2014-03-05 06:35:38 +0000189 bool doFinalization(Module &M) override;
Devang Patel475c4532006-12-08 00:59:05 +0000190 bool doFinalization(Function &F);
191
Craig Topperf398d7c2014-03-05 06:35:38 +0000192 PMDataManager *getAsPMDataManager() override { return this; }
193 Pass *getAsPass() override { return this; }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000194
Mehdi Amini117296c2016-10-01 02:56:57 +0000195 StringRef getPassName() const override { return "BasicBlock Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000196
Devang Pateleda56172006-12-12 23:34:33 +0000197 // Print passes managed by this manager
Craig Topperf398d7c2014-03-05 06:35:38 +0000198 void dumpPassStructure(unsigned Offset) override {
Eric Christophera13839f2014-02-26 23:27:16 +0000199 dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patelabfbe3b2006-12-16 00:56:26 +0000200 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
201 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +0000202 BP->dumpPassStructure(Offset + 1);
Devang Patelabfbe3b2006-12-16 00:56:26 +0000203 dumpLastUses(BP, Offset+1);
Devang Pateleda56172006-12-12 23:34:33 +0000204 }
205 }
Devang Patelabfbe3b2006-12-16 00:56:26 +0000206
207 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng66dbd3f2012-11-13 02:56:38 +0000208 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patelabfbe3b2006-12-16 00:56:26 +0000209 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
210 return BP;
211 }
Devang Patel3b3f8992007-01-11 01:10:25 +0000212
Craig Topperf398d7c2014-03-05 06:35:38 +0000213 PassManagerType getPassManagerType() const override {
Dan Gohmande6188a2010-08-12 23:50:08 +0000214 return PMT_BasicBlockPassManager;
Devang Patel3b3f8992007-01-11 01:10:25 +0000215 }
Devang Patelca58e352006-11-08 10:05:38 +0000216};
217
Devang Patel8c78a0b2007-05-03 01:11:54 +0000218char BBPassManager::ID = 0;
Chandler Carruth7caea412013-11-09 12:26:54 +0000219} // End anonymous namespace
Devang Patel67d6a5e2006-12-19 19:46:59 +0000220
Devang Patele7599552007-01-12 18:52:44 +0000221namespace llvm {
Chandler Carruth7caea412013-11-09 12:26:54 +0000222namespace legacy {
Devang Patel10c2ca62006-12-12 22:47:13 +0000223//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000224// FunctionPassManagerImpl
Devang Patel10c2ca62006-12-12 22:47:13 +0000225//
Devang Patel67d6a5e2006-12-19 19:46:59 +0000226/// FunctionPassManagerImpl manages FPPassManagers
227class FunctionPassManagerImpl : public Pass,
Devang Patelad98d232007-01-11 22:15:30 +0000228 public PMDataManager,
229 public PMTopLevelManager {
David Blaikiea379b1812011-12-20 02:50:00 +0000230 virtual void anchor();
Torok Edwin24c78352009-06-29 18:49:09 +0000231private:
232 bool wasRun;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000233public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000234 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000235 explicit FunctionPassManagerImpl() :
236 Pass(PT_PassManager, ID), PMDataManager(),
237 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel67d6a5e2006-12-19 19:46:59 +0000238
Matthias Braun0880c602014-12-12 01:27:01 +0000239 /// \copydoc FunctionPassManager::add()
Devang Patel67d6a5e2006-12-19 19:46:59 +0000240 void add(Pass *P) {
241 schedulePass(P);
242 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000243
244 /// createPrinterPass - Get a function printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000245 Pass *createPrinterPass(raw_ostream &O,
246 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000247 return createPrintFunctionPass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000248 }
249
Torok Edwin24c78352009-06-29 18:49:09 +0000250 // Prepare for running an on the fly pass, freeing memory if needed
251 // from a previous run.
252 void releaseMemoryOnTheFly();
253
Devang Patel67d6a5e2006-12-19 19:46:59 +0000254 /// run - Execute all of the passes scheduled for execution. Keep track of
255 /// whether any of the passes modifies the module, and if so, return true.
256 bool run(Function &F);
257
258 /// doInitialization - Run all of the initializers for the function passes.
259 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000260 bool doInitialization(Module &M) override;
Dan Gohmande6188a2010-08-12 23:50:08 +0000261
Dan Gohmane6656eb2007-07-30 14:51:13 +0000262 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000263 ///
Craig Topperf398d7c2014-03-05 06:35:38 +0000264 bool doFinalization(Module &M) override;
Devang Patel67d6a5e2006-12-19 19:46:59 +0000265
Dan Gohmande6188a2010-08-12 23:50:08 +0000266
Craig Topperf398d7c2014-03-05 06:35:38 +0000267 PMDataManager *getAsPMDataManager() override { return this; }
268 Pass *getAsPass() override { return this; }
269 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000270 return PMT_FunctionPassManager;
271 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000272
Devang Patel67d6a5e2006-12-19 19:46:59 +0000273 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000274 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000275 Info.setPreservesAll();
276 }
277
Devang Patel67d6a5e2006-12-19 19:46:59 +0000278 FPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000279 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000280 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
281 return FP;
282 }
Devang Patel67d6a5e2006-12-19 19:46:59 +0000283};
284
David Blaikiea379b1812011-12-20 02:50:00 +0000285void FunctionPassManagerImpl::anchor() {}
286
Devang Patel8c78a0b2007-05-03 01:11:54 +0000287char FunctionPassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000288} // End of legacy namespace
289} // End of llvm namespace
Dan Gohmande6188a2010-08-12 23:50:08 +0000290
Chandler Carruth7caea412013-11-09 12:26:54 +0000291namespace {
Devang Patel67d6a5e2006-12-19 19:46:59 +0000292//===----------------------------------------------------------------------===//
293// MPPassManager
294//
295/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmandfdf2c02008-03-11 16:18:48 +0000296/// It batches all Module passes and function pass managers together and
297/// sequences them to process one module.
Devang Patel67d6a5e2006-12-19 19:46:59 +0000298class MPPassManager : public Pass, public PMDataManager {
Devang Patelca58e352006-11-08 10:05:38 +0000299public:
Devang Patel8c78a0b2007-05-03 01:11:54 +0000300 static char ID;
Andrew Trick08966212011-08-29 17:07:00 +0000301 explicit MPPassManager() :
302 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel2ff44922007-04-16 20:39:59 +0000303
304 // Delete on the fly managers.
Alexander Kornienkof817c1c2015-04-11 02:11:45 +0000305 ~MPPassManager() override {
Yaron Keren4849aa32015-06-05 17:48:47 +0000306 for (auto &OnTheFlyManager : OnTheFlyManagers) {
307 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Devang Patel2ff44922007-04-16 20:39:59 +0000308 delete FPP;
309 }
310 }
311
Dan Gohmande6188a2010-08-12 23:50:08 +0000312 /// createPrinterPass - Get a module printer pass.
Craig Topperf398d7c2014-03-05 06:35:38 +0000313 Pass *createPrinterPass(raw_ostream &O,
314 const std::string &Banner) const override {
Chandler Carruth9d805132014-01-12 11:30:46 +0000315 return createPrintModulePass(O, Banner);
David Greene9b063df2010-04-02 23:17:14 +0000316 }
317
Devang Patelca58e352006-11-08 10:05:38 +0000318 /// run - Execute all of the passes scheduled for execution. Keep track of
319 /// whether any of the passes modifies the module, and if so, return true.
320 bool runOnModule(Module &M);
Devang Patelebba9702006-11-13 22:40:09 +0000321
Pedro Artigase4348b02012-12-03 21:56:57 +0000322 using llvm::Pass::doInitialization;
323 using llvm::Pass::doFinalization;
324
Devang Patelf9d96b92006-12-07 19:57:52 +0000325 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000326 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000327 Info.setPreservesAll();
328 }
329
Devang Patele64d3052007-04-16 20:12:57 +0000330 /// Add RequiredPass into list of lower level passes required by pass P.
331 /// RequiredPass is run on the fly by Pass Manager when P requests it
332 /// through getAnalysis interface.
Craig Topperf398d7c2014-03-05 06:35:38 +0000333 void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) override;
Devang Patele64d3052007-04-16 20:12:57 +0000334
Dan Gohmande6188a2010-08-12 23:50:08 +0000335 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +0000336 /// required by module pass MP. Instantiate analysis pass, by using
337 /// its runOnFunction() for function F.
Craig Topperf398d7c2014-03-05 06:35:38 +0000338 Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F) override;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000339
Mehdi Amini117296c2016-10-01 02:56:57 +0000340 StringRef getPassName() const override { return "Module Pass Manager"; }
Devang Patele3858e62007-02-01 22:08:25 +0000341
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
Devang Patelf9d96b92006-12-07 19:57:52 +0000413 /// Pass Manager itself does not invalidate any analysis info.
Craig Topperf398d7c2014-03-05 06:35:38 +0000414 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patelf9d96b92006-12-07 19:57:52 +0000415 Info.setPreservesAll();
416 }
417
Craig Topperf398d7c2014-03-05 06:35:38 +0000418 PMDataManager *getAsPMDataManager() override { return this; }
419 Pass *getAsPass() override { return this; }
420 PassManagerType getTopLevelPassManagerType() override {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000421 return PMT_ModulePassManager;
422 }
Chris Lattner2fa26e52010-01-22 05:24:46 +0000423
Devang Patel67d6a5e2006-12-19 19:46:59 +0000424 MPPassManager *getContainedManager(unsigned N) {
Chris Lattner60987362009-03-06 05:53:14 +0000425 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel67d6a5e2006-12-19 19:46:59 +0000426 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
427 return MP;
428 }
Devang Patel376fefa2006-11-08 10:29:57 +0000429};
430
David Blaikiea379b1812011-12-20 02:50:00 +0000431void PassManagerImpl::anchor() {}
432
Devang Patel8c78a0b2007-05-03 01:11:54 +0000433char PassManagerImpl::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000434} // End of legacy namespace
435} // End of llvm namespace
Devang Patel1c3633e2007-01-29 23:10:37 +0000436
437namespace {
438
439//===----------------------------------------------------------------------===//
Chris Lattner4c1e9542009-03-06 06:45:05 +0000440/// TimingInfo Class - This class is used to calculate information about the
441/// amount of time each pass takes to execute. This only happens when
442/// -time-passes is enabled on the command line.
443///
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000444
Owen Anderson5a6960f2009-06-18 20:51:00 +0000445static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Anderson0dd39fd2009-06-17 21:28:54 +0000446
Nick Lewycky02d5f772009-10-25 06:33:48 +0000447class TimingInfo {
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000448 DenseMap<Pass*, Timer*> TimingData;
Devang Patel1c3633e2007-01-29 23:10:37 +0000449 TimerGroup TG;
Devang Patel1c3633e2007-01-29 23:10:37 +0000450public:
451 // Use 'create' member to get this.
452 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohmande6188a2010-08-12 23:50:08 +0000453
Devang Patel1c3633e2007-01-29 23:10:37 +0000454 // TimingDtor - Print out information about timing information
455 ~TimingInfo() {
Chris Lattner707431c2010-03-30 04:03:22 +0000456 // Delete all of the timers, which accumulate their info into the
457 // TimerGroup.
Yaron Keren4849aa32015-06-05 17:48:47 +0000458 for (auto &I : TimingData)
459 delete I.second;
Devang Patel1c3633e2007-01-29 23:10:37 +0000460 // TimerGroup is deleted next, printing the report.
461 }
462
463 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
Alp Tokerf907b892013-12-05 05:44:44 +0000464 // to a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patel1c3633e2007-01-29 23:10:37 +0000465 // null. It may be called multiple times.
466 static void createTheTimeInfo();
467
Chris Lattner707431c2010-03-30 04:03:22 +0000468 /// getPassTimer - Return the timer for the specified pass if it exists.
469 Timer *getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000470 if (P->getAsPMDataManager())
Craig Topperc6207612014-04-09 06:08:46 +0000471 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +0000472
Owen Anderson5c96ef72009-07-07 18:33:04 +0000473 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesen4c2094b2012-12-03 17:31:11 +0000474 Timer *&T = TimingData[P];
Craig Topperc6207612014-04-09 06:08:46 +0000475 if (!T)
Chris Lattner707431c2010-03-30 04:03:22 +0000476 T = new Timer(P->getPassName(), TG);
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000477 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000478 }
479};
480
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000481} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000482
Dan Gohmand78c4002008-05-13 00:00:25 +0000483static TimingInfo *TheTimeInfo;
484
Devang Patela1514cb2006-12-07 19:39:39 +0000485//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000486// PMTopLevelManager implementation
487
Devang Patel4268fc02007-01-16 02:00:38 +0000488/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000489PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
490 PMDM->setTopLevelManager(this);
491 addPassManager(PMDM);
492 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000493}
494
Devang Patelafb1f3622006-12-12 22:35:25 +0000495/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000496void
Bill Wendlingea857e12012-05-14 07:53:40 +0000497PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000498 unsigned PDepth = 0;
499 if (P->getResolver())
500 PDepth = P->getResolver()->getPMDataManager().getDepth();
501
Yaron Keren4849aa32015-06-05 17:48:47 +0000502 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000503 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000504
Devang Patel01919d22007-03-08 19:05:01 +0000505 if (P == AP)
506 continue;
507
Tobias Grosserf07426b2011-01-20 21:03:22 +0000508 // Update the last users of passes that are required transitive by AP.
509 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
510 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
511 SmallVector<Pass *, 12> LastUses;
512 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000513 for (AnalysisID ID : IDs) {
514 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000515 assert(AnalysisPass && "Expected analysis pass to exist.");
516 AnalysisResolver *AR = AnalysisPass->getResolver();
517 assert(AR && "Expected analysis resolver to exist.");
518 unsigned APDepth = AR->getPMDataManager().getDepth();
519
520 if (PDepth == APDepth)
521 LastUses.push_back(AnalysisPass);
522 else if (PDepth > APDepth)
523 LastPMUses.push_back(AnalysisPass);
524 }
525
526 setLastUser(LastUses, P);
527
528 // If this pass has a corresponding pass manager, push higher level
529 // analysis to this pass manager.
530 if (P->getResolver())
531 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
532
533
Devang Patelafb1f3622006-12-12 22:35:25 +0000534 // If AP is the last user of other passes then make P last user of
535 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000536 for (auto LU : LastUser) {
537 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000538 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000539 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000540 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000541 }
542 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000543}
544
545/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000546void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000547 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000548 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000549 InversedLastUser.find(P);
550 if (DMI == InversedLastUser.end())
551 return;
552
553 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000554 for (Pass *LUP : LU) {
555 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000556 }
557
Devang Patelafb1f3622006-12-12 22:35:25 +0000558}
559
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000560AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000561 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000562 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000563 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000564 AnUsage = DMI->second;
565 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000566 // Look up the analysis usage from the pass instance (different instances
567 // of the same pass can produce different results), but unique the
568 // resulting object to reduce memory usage. This helps to greatly reduce
569 // memory usage when we have many instances of only a few pass types
570 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
571 // of dependencies.
572 AnalysisUsage AU;
573 P->getAnalysisUsage(AU);
574
575 AUFoldingSetNode* Node = nullptr;
576 FoldingSetNodeID ID;
577 AUFoldingSetNode::Profile(ID, AU);
578 void *IP = nullptr;
579 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
580 Node = N;
581 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000582 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000583 UniqueAnalysisUsages.InsertNode(Node, IP);
584 }
585 assert(Node && "cached analysis usage must be non null");
586
587 AnUsageMap[P] = &Node->AU;
588 AnUsage = &Node->AU;;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000589 }
590 return AnUsage;
591}
592
Devang Patelafb1f3622006-12-12 22:35:25 +0000593/// Schedule pass P for execution. Make sure that passes required by
594/// P are run before P is run. Update analysis info maintained by
595/// the manager. Remove dead passes. This is a recursive function.
596void PMTopLevelManager::schedulePass(Pass *P) {
597
Devang Patel3312f752007-01-16 21:43:18 +0000598 // TODO : Allocate function manager for this pass, other wise required set
599 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000600
Devang Pateld74ede72007-03-06 01:06:16 +0000601 // Give pass a chance to prepare the stage.
602 P->preparePassManager(activeStack);
603
Devang Patel864970e2008-03-18 00:39:19 +0000604 // If P is an analysis pass and it is available then do not
605 // generate the analysis again. Stale analysis info should not be
606 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000607 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000608 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000609 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000610 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000611 }
Devang Patel864970e2008-03-18 00:39:19 +0000612
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000613 AnalysisUsage *AnUsage = findAnalysisUsage(P);
614
Devang Patelfdee7032008-08-14 23:07:48 +0000615 bool checkAnalysis = true;
616 while (checkAnalysis) {
617 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000618
Devang Patelfdee7032008-08-14 23:07:48 +0000619 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
620 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
621 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000622
Devang Patelfdee7032008-08-14 23:07:48 +0000623 Pass *AnalysisPass = findAnalysisPass(*I);
624 if (!AnalysisPass) {
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000625 const PassInfo *PI = findAnalysisPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000626
Craig Topperc6207612014-04-09 06:08:46 +0000627 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000628 // Pass P is not in the global PassRegistry
629 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
630 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
631 dbgs() << "Required Passes:" << "\n";
632 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
633 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
634 Pass *AnalysisPass2 = findAnalysisPass(*I2);
635 if (AnalysisPass2) {
636 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000637 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000638 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
639 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
640 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
641 }
642 }
643 }
644
Andrew Trick6bbaf132011-06-03 00:48:58 +0000645 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000646 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000647 if (P->getPotentialPassManagerType () ==
648 AnalysisPass->getPotentialPassManagerType())
649 // Schedule analysis pass that is managed by the same pass manager.
650 schedulePass(AnalysisPass);
651 else if (P->getPotentialPassManagerType () >
652 AnalysisPass->getPotentialPassManagerType()) {
653 // Schedule analysis pass that is managed by a new manager.
654 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000655 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000656 // are already checked are still available.
657 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000658 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000659 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000660 // passes are run on the fly.
661 delete AnalysisPass;
662 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000663 }
664 }
665
666 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000667 if (ImmutablePass *IP = P->getAsImmutablePass()) {
668 // P is a immutable pass and it will be managed by this
669 // top level manager. Set up analysis resolver to connect them.
670 PMDataManager *DM = getAsPMDataManager();
671 AnalysisResolver *AR = new AnalysisResolver(*DM);
672 P->setResolver(AR);
673 DM->initializeAnalysisImpl(P);
674 addImmutablePass(IP);
675 DM->recordAvailableAnalysis(IP);
676 return;
677 }
678
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000679 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000680 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000681 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000682 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
683 }
684
685 // Add the requested pass to the best available pass manager.
686 P->assignPassManager(activeStack, getTopLevelPassManagerType());
687
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000688 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000689 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000690 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000691 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
692 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000693}
694
695/// Find the pass that implements Analysis AID. Search immutable
696/// passes and all pass managers. If desired pass is not found
697/// then return NULL.
698Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000699 // For immutable passes we have a direct mapping from ID to pass, so check
700 // that first.
701 if (Pass *P = ImmutablePassMap.lookup(AID))
702 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000703
Devang Patelcd6ba152006-12-12 22:50:05 +0000704 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000705 for (PMDataManager *PassManager : PassManagers)
706 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000707 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000708
709 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000710 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
711 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000712 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000713
Craig Topperc6207612014-04-09 06:08:46 +0000714 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000715}
716
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000717const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
718 const PassInfo *&PI = AnalysisPassInfos[AID];
719 if (!PI)
720 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
721 else
722 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
723 "The pass info pointer changed for an analysis ID!");
724
725 return PI;
726}
727
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000728void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
729 P->initializePass();
730 ImmutablePasses.push_back(P);
731
732 // Add this pass to the map from its analysis ID. We clobber any prior runs
733 // of the pass in the map so that the last one added is the one found when
734 // doing lookups.
735 AnalysisID AID = P->getPassID();
736 ImmutablePassMap[AID] = P;
737
738 // Also add any interfaces implemented by the immutable pass to the map for
739 // fast lookup.
740 const PassInfo *PassInf = findAnalysisPassInfo(AID);
741 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000742 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000743 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
744}
745
Devang Pateleda56172006-12-12 23:34:33 +0000746// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000747void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000748
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000749 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000750 return;
751
Devang Pateleda56172006-12-12 23:34:33 +0000752 // Print out the immutable passes
753 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000754 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000755 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000756
Dan Gohmanf71c5212010-08-19 01:29:07 +0000757 // Every class that derives from PMDataManager also derives from Pass
758 // (sometimes indirectly), but there's no inheritance relationship
759 // between PMDataManager and Pass, so we have to getAsPass to get
760 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000761 for (PMDataManager *Manager : PassManagers)
762 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000763}
764
Devang Patel991aeba2006-12-15 20:13:01 +0000765void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000766
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000767 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000768 return;
769
David Greene994e1bb2010-01-05 01:30:02 +0000770 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000771 for (ImmutablePass *P : ImmutablePasses)
772 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000773 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000774 if (!PI->isAnalysisGroup())
775 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000776 }
Yaron Keren83009952016-04-28 14:49:44 +0000777 for (PMDataManager *PM : PassManagers)
778 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000779 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000780}
781
Devang Patele3068402006-12-21 00:16:50 +0000782void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000783 for (PMDataManager *PM : PassManagers)
784 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000785
Devang Patele3068402006-12-21 00:16:50 +0000786 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000787 for (PMDataManager *IPM : IndirectPassManagers)
788 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000789
Yaron Kerenbd873192016-10-02 19:21:41 +0000790 for (auto LU : LastUser) {
791 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
792 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000793 }
Devang Patele3068402006-12-21 00:16:50 +0000794}
795
Devang Patele7599552007-01-12 18:52:44 +0000796/// Destructor
797PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000798 for (PMDataManager *PM : PassManagers)
799 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000800
Yaron Keren83009952016-04-28 14:49:44 +0000801 for (ImmutablePass *P : ImmutablePasses)
802 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000803}
804
Devang Patelafb1f3622006-12-12 22:35:25 +0000805//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000806// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000807
Devang Patel643676c2006-11-11 01:10:19 +0000808/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000809void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000810 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000811
Chris Lattner60987362009-03-06 05:53:14 +0000812 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000813
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000814 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000815
Dan Gohmande6188a2010-08-12 23:50:08 +0000816 // This pass is the current implementation of all of the interfaces it
817 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000818 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000819 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000820 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000821 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000822 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000823}
824
Devang Patel9d9fc902007-03-06 17:52:53 +0000825// Return true if P preserves high level analysis used by other
826// passes managed by this manager
827bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000828 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000829 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000830 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000831
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000832 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000833 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000834 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000835 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000836 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000837 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000838
Devang Patel9d9fc902007-03-06 17:52:53 +0000839 return true;
840}
841
Chris Lattner02eb94c2008-08-07 07:34:50 +0000842/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000843void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000844 // Don't do this unless assertions are enabled.
845#ifdef NDEBUG
846 return;
847#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000848 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
849 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000850
Devang Patelef432532007-07-19 05:36:09 +0000851 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000852 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000853 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000854 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000855 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000856 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000857 }
858}
859
Devang Patel67c79a42008-07-01 19:50:56 +0000860/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000861void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000862 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
863 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000864 return;
865
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000866 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000867 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000868 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000869 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000870 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000871 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000872 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000873 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000874 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000875 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
876 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000877 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000878 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000879 }
Devang Patel349170f2006-11-11 01:24:55 +0000880 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000881
Devang Patel42dd1e92007-03-06 01:55:46 +0000882 // Check inherited analysis also. If P is not preserving analysis
883 // provided by parent manager then remove it here.
884 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
885
886 if (!InheritedAnalysis[Index])
887 continue;
888
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000889 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000890 I = InheritedAnalysis[Index]->begin(),
891 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000892 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000893 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000894 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000895 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000896 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000897 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000898 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
899 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000900 }
Devang Patel01919d22007-03-08 19:05:01 +0000901 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000902 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000903 }
904 }
Devang Patelf68a3492006-11-07 22:35:17 +0000905}
906
Devang Patelca189262006-11-14 03:05:08 +0000907/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000908void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000909 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000910
Devang Patel8adae862007-07-20 18:04:54 +0000911 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000912
Devang Patel2ff44922007-04-16 20:39:59 +0000913 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000914 if (!TPM)
915 return;
916
Devang Patel17ad0962006-12-08 00:37:52 +0000917 TPM->collectLastUses(DeadPasses, P);
918
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000919 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000920 dbgs() << " -*- '" << P->getPassName();
921 dbgs() << "' is the last user of following pass instances.";
922 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000923 }
924
Yaron Kerenbd873192016-10-02 19:21:41 +0000925 for (Pass *P : DeadPasses)
926 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000927}
Devang Patel200d3052006-12-13 23:50:44 +0000928
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000929void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000930 enum PassDebuggingString DBG_STR) {
931 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000932
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000933 {
934 // If the pass crashes releasing memory, remember this.
935 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000936 TimeRegion PassTimer(getPassTimer(P));
937
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000938 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000939 }
940
Owen Andersona7aed182010-08-06 18:33:48 +0000941 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000942 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000943 // Remove the pass itself (if it is not already removed).
944 AvailableAnalysis.erase(PI);
945
946 // Remove all interfaces this pass implements, for which it is also
947 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000948 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000949 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000950 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000951 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000952 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000953 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000954 }
Devang Patel17ad0962006-12-08 00:37:52 +0000955 }
Devang Patelca189262006-11-14 03:05:08 +0000956}
957
Dan Gohmande6188a2010-08-12 23:50:08 +0000958/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000959/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000960void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000961 // This manager is going to manage pass P. Set up analysis resolver
962 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000963 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000964 P->setResolver(AR);
965
Devang Patelec2b9a72007-03-05 22:57:49 +0000966 // If a FunctionPass F is the last user of ModulePass info M
967 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000968 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000969
Chris Lattner60987362009-03-06 05:53:14 +0000970 if (!ProcessAnalysis) {
971 // Add pass
972 PassVector.push_back(P);
973 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000974 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000975
Chris Lattner60987362009-03-06 05:53:14 +0000976 // At the moment, this pass is the last user of all required passes.
977 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +0000978 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +0000979 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
980
981 unsigned PDepth = this->getDepth();
982
Chandler Carruth44a13852015-08-19 03:02:12 +0000983 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
984 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +0000985 unsigned RDepth = 0;
986
Chandler Carruth44a13852015-08-19 03:02:12 +0000987 assert(PUsed->getResolver() && "Analysis Resolver is not set");
988 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +0000989 RDepth = DM.getDepth();
990
991 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +0000992 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +0000993 else if (PDepth > RDepth) {
994 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +0000995 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +0000996 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +0000997 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +0000998 } else
Chandler Carruth44a13852015-08-19 03:02:12 +0000999 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001000 }
1001
1002 // Set P as P's last user until someone starts using P.
1003 // However, if P is a Pass Manager then it does not need
1004 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001005 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001006 LastUses.push_back(P);
1007 TPM->setLastUser(LastUses, P);
1008
1009 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001010 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001011 TPM->setLastUser(TransferLastUses, My_PM);
1012 TransferLastUses.clear();
1013 }
1014
Dan Gohman6304db32010-08-16 22:57:28 +00001015 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001016 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1017 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001018 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001019 this->addLowerLevelRequiredPass(P, AnalysisPass);
1020 }
1021
1022 // Take a note of analysis required and made available by this pass.
1023 // Remove the analysis not preserved by this pass
1024 removeNotPreservedAnalysis(P);
1025 recordAvailableAnalysis(P);
1026
Devang Patel8cad70d2006-11-11 01:51:02 +00001027 // Add pass
1028 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001029}
1030
Devang Patele64d3052007-04-16 20:12:57 +00001031
Chandler Carruth44a13852015-08-19 03:02:12 +00001032/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001033/// pass P and are available. Populate RP_NotAvail with analysis
1034/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001035void PMDataManager::collectRequiredAndUsedAnalyses(
1036 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1037 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001038 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001039
1040 for (const auto &UsedID : AnUsage->getUsedSet())
1041 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1042 UP.push_back(AnalysisPass);
1043
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001044 for (const auto &RequiredID : AnUsage->getRequiredSet())
1045 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001046 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001047 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001048 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001049
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001050 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1051 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001052 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001053 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001054 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001055}
1056
Devang Patel07f4f582006-11-14 21:49:36 +00001057// All Required analyses should be available to the pass as it runs! Here
1058// we fill in the AnalysisImpls member of the pass so that it can
1059// successfully use the getAnalysis() method to retrieve the
1060// implementations it needs.
1061//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001062void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001063 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1064
Chris Lattnercbd160f2008-08-08 05:33:04 +00001065 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001066 I = AnUsage->getRequiredSet().begin(),
1067 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001068 Pass *Impl = findAnalysisPass(*I, true);
Craig Topperc6207612014-04-09 06:08:46 +00001069 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001070 // This may be analysis pass that is initialized on the fly.
1071 // If that is not the case then it will raise an assert when it is used.
1072 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001073 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001074 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001075 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001076 }
1077}
1078
Devang Patel640c5bb2006-12-08 22:30:11 +00001079/// Find the pass that implements Analysis AID. If desired pass is not found
1080/// then return NULL.
1081Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1082
1083 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001084 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001085
1086 if (I != AvailableAnalysis.end())
1087 return I->second;
1088
1089 // Search Parents through TopLevelManager
1090 if (SearchParent)
1091 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001092
Craig Topperc6207612014-04-09 06:08:46 +00001093 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001094}
1095
Devang Patel991aeba2006-12-15 20:13:01 +00001096// Print list of passes that are last used by P.
1097void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1098
Devang Patel8adae862007-07-20 18:04:54 +00001099 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001100
1101 // If this is a on the fly manager then it does not have TPM.
1102 if (!TPM)
1103 return;
1104
Devang Patel991aeba2006-12-15 20:13:01 +00001105 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001106
Dan Gohman7224bce2010-10-12 00:11:18 +00001107 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001108 E = LUses.end(); I != E; ++I) {
Eric Christophera13839f2014-02-26 23:27:16 +00001109 dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001110 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001111 }
1112}
1113
1114void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001115 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001116 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001117 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001118 PMD->dumpPassArguments();
1119 else
Owen Andersona7aed182010-08-06 18:33:48 +00001120 if (const PassInfo *PI =
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001121 TPM->findAnalysisPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001122 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001123 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001124 }
1125}
1126
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001127void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1128 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001129 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001130 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001131 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001132 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001133 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001134 switch (S1) {
1135 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001136 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001137 break;
1138 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001139 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001140 break;
1141 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001142 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001143 break;
1144 default:
1145 break;
1146 }
1147 switch (S2) {
1148 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001149 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001150 break;
1151 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001152 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001153 break;
1154 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001155 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001156 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001157 case ON_REGION_MSG:
1158 dbgs() << "' on Region '" << Msg << "'...\n";
1159 break;
Devang Patel003a5592007-03-05 20:01:30 +00001160 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001161 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001162 break;
1163 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001164 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001165 break;
1166 default:
1167 break;
1168 }
Devang Patel991aeba2006-12-15 20:13:01 +00001169}
1170
Chris Lattner4c1e9542009-03-06 06:45:05 +00001171void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001172 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001173 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001174
Chris Lattner4c493d92008-08-08 15:14:09 +00001175 AnalysisUsage analysisUsage;
1176 P->getAnalysisUsage(analysisUsage);
1177 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1178}
1179
Chris Lattner4c1e9542009-03-06 06:45:05 +00001180void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001181 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001182 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001183
Chris Lattner4c493d92008-08-08 15:14:09 +00001184 AnalysisUsage analysisUsage;
1185 P->getAnalysisUsage(analysisUsage);
1186 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1187}
1188
Chandler Carruth44a13852015-08-19 03:02:12 +00001189void PMDataManager::dumpUsedSet(const Pass *P) const {
1190 if (PassDebugging < Details)
1191 return;
1192
1193 AnalysisUsage analysisUsage;
1194 P->getAnalysisUsage(analysisUsage);
1195 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1196}
1197
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001198void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001199 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001200 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001201 if (Set.empty())
1202 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001203 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001204 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001205 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001206 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001207 if (!PInf) {
1208 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1209 // all drivers.
1210 dbgs() << " Uninitialized Pass";
1211 continue;
1212 }
Owen Andersona7aed182010-08-06 18:33:48 +00001213 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001214 }
David Greene994e1bb2010-01-05 01:30:02 +00001215 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001216}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001217
Devang Patel004937b2007-07-27 20:06:09 +00001218/// Add RequiredPass into list of lower level passes required by pass P.
1219/// RequiredPass is run on the fly by Pass Manager when P requests it
1220/// through getAnalysis interface.
1221/// This should be handled by specific pass manager.
1222void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1223 if (TPM) {
1224 TPM->dumpArguments();
1225 TPM->dumpPasses();
1226 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001227
Dan Gohmande6188a2010-08-12 23:50:08 +00001228 // Module Level pass may required Function Level analysis info
1229 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1230 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001231 // module level pass is requiring lower level analysis info managed by
1232 // lower level pass manager.
1233
1234 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001235 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001236 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001237#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001238 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1239 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001240#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001241 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001242}
1243
Owen Andersona7aed182010-08-06 18:33:48 +00001244Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001245 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001246}
1247
Devang Patele7599552007-01-12 18:52:44 +00001248// Destructor
1249PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001250 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001251 E = PassVector.end(); I != E; ++I)
1252 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001253}
1254
Devang Patel9bdf7d42006-12-08 23:28:54 +00001255//===----------------------------------------------------------------------===//
1256// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001257// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1258Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001259 return PM.findAnalysisPass(ID, dir);
1260}
1261
Dan Gohmande6188a2010-08-12 23:50:08 +00001262Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001263 Function &F) {
1264 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1265}
1266
Devang Patela1514cb2006-12-07 19:39:39 +00001267//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001268// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001269
Dan Gohmande6188a2010-08-12 23:50:08 +00001270/// Execute all of the passes scheduled for execution by invoking
1271/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001272/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001273bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001274 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001275 return false;
1276
Devang Patele9585592006-12-08 01:38:28 +00001277 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001278
Devang Patel6e5a1132006-11-07 21:31:57 +00001279 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001280 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1281 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001282 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001283
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001284 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001285 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001286
Devang Patelabfbe3b2006-12-16 00:56:26 +00001287 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001288
Chris Lattner4c1e9542009-03-06 06:45:05 +00001289 {
1290 // If the pass crashes, remember this.
1291 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001292 TimeRegion PassTimer(getPassTimer(BP));
1293
Dan Gohman74b189f2010-03-01 17:34:28 +00001294 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001295 }
Devang Patel93a197c2006-12-14 00:08:04 +00001296
Dan Gohman74b189f2010-03-01 17:34:28 +00001297 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001298 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001299 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001300 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001301 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001302 dumpUsedSet(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) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001381 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1382 report_fatal_error("Error reading bitcode file: " + EIB.message());
1383 });
Devang Patel272908d2006-12-08 22:57:48 +00001384 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001385}
1386
1387
Devang Patelff631ae2006-11-15 01:27:05 +00001388/// doInitialization - Run all of the initializers for the function passes.
1389///
Devang Patelb67904d2006-12-13 02:36:01 +00001390bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001391 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001392}
1393
Dan Gohmane6656eb2007-07-30 14:51:13 +00001394/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001395///
Devang Patelb67904d2006-12-13 02:36:01 +00001396bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001397 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001398}
1399
Devang Patela1514cb2006-12-07 19:39:39 +00001400//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001401// FunctionPassManagerImpl implementation
1402//
Duncan Sands51495602009-02-13 09:42:34 +00001403bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001404 bool Changed = false;
1405
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001406 dumpArguments();
1407 dumpPasses();
1408
Yaron Keren4849aa32015-06-05 17:48:47 +00001409 for (ImmutablePass *ImPass : getImmutablePasses())
1410 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001411
Chris Lattner4c1e9542009-03-06 06:45:05 +00001412 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1413 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001414
1415 return Changed;
1416}
1417
Duncan Sands51495602009-02-13 09:42:34 +00001418bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001419 bool Changed = false;
1420
Pedro Artigas41b98842012-12-05 17:12:22 +00001421 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001422 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001423
Yaron Keren4849aa32015-06-05 17:48:47 +00001424 for (ImmutablePass *ImPass : getImmutablePasses())
1425 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001426
Devang Patel67d6a5e2006-12-19 19:46:59 +00001427 return Changed;
1428}
1429
Devang Patelec9c58f2009-04-01 22:34:41 +00001430/// cleanup - After running all passes, clean up pass manager cache.
1431void FPPassManager::cleanup() {
1432 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1433 FunctionPass *FP = getContainedPass(Index);
1434 AnalysisResolver *AR = FP->getResolver();
1435 assert(AR && "Analysis Resolver is not set");
1436 AR->clearAnalysisImpls();
1437 }
1438}
1439
Torok Edwin24c78352009-06-29 18:49:09 +00001440void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1441 if (!wasRun)
1442 return;
1443 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1444 FPPassManager *FPPM = getContainedManager(Index);
1445 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1446 FPPM->getContainedPass(Index)->releaseMemory();
1447 }
1448 }
Torok Edwin896556e2009-06-29 21:05:10 +00001449 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001450}
1451
Devang Patel67d6a5e2006-12-19 19:46:59 +00001452// Execute all the passes managed by this top level manager.
1453// Return true if any function is modified by a pass.
1454bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001455 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001456 TimingInfo::createTheTimeInfo();
1457
Devang Patele3068402006-12-21 00:16:50 +00001458 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001459 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001460 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001461 F.getContext().yield();
1462 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001463
1464 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1465 getContainedManager(Index)->cleanup();
1466
Torok Edwin24c78352009-06-29 18:49:09 +00001467 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001468 return Changed;
1469}
1470
1471//===----------------------------------------------------------------------===//
1472// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001473
Devang Patel8c78a0b2007-05-03 01:11:54 +00001474char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001475/// Print passes managed by this manager
1476void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001477 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001478 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1479 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001480 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001481 dumpLastUses(FP, Offset+1);
1482 }
1483}
1484
1485
Dan Gohmande6188a2010-08-12 23:50:08 +00001486/// Execute all of the passes scheduled for execution by invoking
1487/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001488/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001489bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001490 if (F.isDeclaration())
1491 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001492
1493 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001494
Devang Patelcbbf2912008-03-20 01:09:53 +00001495 // Collect inherited analysis from Module level pass manager.
1496 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001497
Devang Patelabfbe3b2006-12-16 00:56:26 +00001498 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1499 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001500 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001501
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001502 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001503 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001504
Devang Patelabfbe3b2006-12-16 00:56:26 +00001505 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001506
Chris Lattner4c1e9542009-03-06 06:45:05 +00001507 {
1508 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001509 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001510
Dan Gohman74b189f2010-03-01 17:34:28 +00001511 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001512 }
Devang Patel93a197c2006-12-14 00:08:04 +00001513
Dan Gohman74b189f2010-03-01 17:34:28 +00001514 Changed |= LocalChanged;
1515 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001516 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001517 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001518 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001519
Devang Patela273d1c2007-07-19 18:02:32 +00001520 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001521 removeNotPreservedAnalysis(FP);
1522 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001523 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001524 }
1525 return Changed;
1526}
1527
Devang Patel67d6a5e2006-12-19 19:46:59 +00001528bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001529 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001530
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001531 for (Function &F : M)
1532 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001533
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001534 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001535}
1536
Duncan Sands51495602009-02-13 09:42:34 +00001537bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001538 bool Changed = false;
1539
Chris Lattner4c1e9542009-03-06 06:45:05 +00001540 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1541 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001542
Devang Patelff631ae2006-11-15 01:27:05 +00001543 return Changed;
1544}
1545
Duncan Sands51495602009-02-13 09:42:34 +00001546bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001547 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001548
1549 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001550 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001551
Devang Patelff631ae2006-11-15 01:27:05 +00001552 return Changed;
1553}
1554
Devang Patela1514cb2006-12-07 19:39:39 +00001555//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001556// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001557
Dan Gohmande6188a2010-08-12 23:50:08 +00001558/// Execute all of the passes scheduled for execution by invoking
1559/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001560/// the module, and if so, return true.
1561bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001562MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001563 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001564
Torok Edwin24c78352009-06-29 18:49:09 +00001565 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001566 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1567 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001568 Changed |= FPP->doInitialization(M);
1569 }
1570
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001571 // Initialize module passes
1572 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1573 Changed |= getContainedPass(Index)->doInitialization(M);
1574
Devang Patelabfbe3b2006-12-16 00:56:26 +00001575 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1576 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001577 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001578
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001579 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001580 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001581
Devang Patelabfbe3b2006-12-16 00:56:26 +00001582 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001583
Chris Lattner4c1e9542009-03-06 06:45:05 +00001584 {
1585 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001586 TimeRegion PassTimer(getPassTimer(MP));
1587
Dan Gohman74b189f2010-03-01 17:34:28 +00001588 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001589 }
Devang Patel93a197c2006-12-14 00:08:04 +00001590
Dan Gohman74b189f2010-03-01 17:34:28 +00001591 Changed |= LocalChanged;
1592 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001593 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001594 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001595 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001596 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001597
Devang Patela273d1c2007-07-19 18:02:32 +00001598 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001599 removeNotPreservedAnalysis(MP);
1600 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001601 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001602 }
Torok Edwin24c78352009-06-29 18:49:09 +00001603
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001604 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001605 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001606 Changed |= getContainedPass(Index)->doFinalization(M);
1607
Torok Edwin24c78352009-06-29 18:49:09 +00001608 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001609 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1610 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001611 // We don't know when is the last time an on-the-fly pass is run,
1612 // so we need to releaseMemory / finalize here
1613 FPP->releaseMemoryOnTheFly();
1614 Changed |= FPP->doFinalization(M);
1615 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001616
Devang Patel05e1a972006-11-07 22:03:15 +00001617 return Changed;
1618}
1619
Devang Patele64d3052007-04-16 20:12:57 +00001620/// Add RequiredPass into list of lower level passes required by pass P.
1621/// RequiredPass is run on the fly by Pass Manager when P requests it
1622/// through getAnalysis interface.
1623void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001624 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1625 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001626 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001627 RequiredPass->getPotentialPassManagerType()) &&
1628 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001629 if (!RequiredPass)
1630 return;
Devang Patele64d3052007-04-16 20:12:57 +00001631
Devang Patel68f72b12007-04-26 17:50:19 +00001632 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001633 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001634 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001635 // FPP is the top level manager.
1636 FPP->setTopLevelManager(FPP);
1637
Devang Patel69e9f6d2007-04-16 20:27:05 +00001638 OnTheFlyManagers[P] = FPP;
1639 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001640 const PassInfo *RequiredPassPI =
1641 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001642
Craig Topperc6207612014-04-09 06:08:46 +00001643 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001644 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1645 FoundPass =
1646 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001647 }
Andrew Trick02066f22014-04-08 03:40:34 +00001648 if (!FoundPass) {
1649 FoundPass = RequiredPass;
1650 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001651 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001652 FPP->add(RequiredPass);
1653 }
1654 // Register P as the last user of FoundPass or RequiredPass.
1655 SmallVector<Pass *, 1> LU;
1656 LU.push_back(FoundPass);
1657 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001658}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001659
Dan Gohmande6188a2010-08-12 23:50:08 +00001660/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001661/// required by module pass MP. Instantiate analysis pass, by using
1662/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001663Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001664 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001665 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001666
Torok Edwin24c78352009-06-29 18:49:09 +00001667 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001668 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001669 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001670}
1671
1672
Devang Patela1514cb2006-12-07 19:39:39 +00001673//===----------------------------------------------------------------------===//
1674// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001675
Devang Patelab97cf42006-12-13 00:09:23 +00001676//
Devang Patelc290c8a2006-11-07 22:23:34 +00001677/// run - Execute all of the passes scheduled for execution. Keep track of
1678/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001679bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001680 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001681 TimingInfo::createTheTimeInfo();
1682
Devang Patelcfd70c42006-12-13 22:10:00 +00001683 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001684 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001685
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001686 for (ImmutablePass *ImPass : getImmutablePasses())
1687 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001688
Devang Patele3068402006-12-21 00:16:50 +00001689 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001690 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001691 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001692 M.getContext().yield();
1693 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001694
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001695 for (ImmutablePass *ImPass : getImmutablePasses())
1696 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001697
Devang Patelc290c8a2006-11-07 22:23:34 +00001698 return Changed;
1699}
Devang Patel376fefa2006-11-08 10:29:57 +00001700
Devang Patela1514cb2006-12-07 19:39:39 +00001701//===----------------------------------------------------------------------===//
1702// PassManager implementation
1703
Devang Patel376fefa2006-11-08 10:29:57 +00001704/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001705PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001706 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001707 // PM is the top level manager
1708 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001709}
1710
Devang Patelb67904d2006-12-13 02:36:01 +00001711PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001712 delete PM;
1713}
1714
Chris Lattner60987362009-03-06 05:53:14 +00001715void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001716 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001717}
1718
1719/// run - Execute all of the passes scheduled for execution. Keep track of
1720/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001721bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001722 return PM->run(M);
1723}
1724
Devang Patelb8817b92006-12-14 00:59:42 +00001725//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001726// TimingInfo implementation
1727
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001728bool llvm::TimePassesIsEnabled = false;
1729static cl::opt<bool,true>
1730EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1731 cl::desc("Time each pass, printing elapsed time for each on exit"));
1732
Devang Patelb8817b92006-12-14 00:59:42 +00001733// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001734// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001735// null. It may be called multiple times.
1736void TimingInfo::createTheTimeInfo() {
1737 if (!TimePassesIsEnabled || TheTimeInfo) return;
1738
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001739 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001740 // This guarantees that the object will be constructed before static globals,
1741 // thus it will be destroyed before them.
1742 static ManagedStatic<TimingInfo> TTI;
1743 TheTimeInfo = &*TTI;
1744}
1745
Devang Patel1c3633e2007-01-29 23:10:37 +00001746/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001747Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001748 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001749 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001750 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001751}
1752
Devang Patel1c56a632007-01-08 19:29:38 +00001753//===----------------------------------------------------------------------===//
1754// PMStack implementation
1755//
Devang Patelad98d232007-01-11 22:15:30 +00001756
Devang Patel1c56a632007-01-08 19:29:38 +00001757// Pop Pass Manager from the stack and clear its analysis info.
1758void PMStack::pop() {
1759
1760 PMDataManager *Top = this->top();
1761 Top->initializeAnalysisInfo();
1762
1763 S.pop_back();
1764}
1765
1766// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001767void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001768 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001769 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001770
Chris Lattner60987362009-03-06 05:53:14 +00001771 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001772 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1773 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001774 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001775
Chris Lattner60987362009-03-06 05:53:14 +00001776 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001777 TPM->addIndirectPassManager(PM);
1778 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001779 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001780 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001781 assert((PM->getPassManagerType() == PMT_ModulePassManager
1782 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001783 && "pushing bad pass manager to PMStack");
1784 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001785 }
1786
Devang Patel15701b52007-01-11 00:19:00 +00001787 S.push_back(PM);
1788}
1789
1790// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001791LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001792 for (PMDataManager *Manager : S)
1793 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001794
Devang Patel15701b52007-01-11 00:19:00 +00001795 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001796 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001797}
1798
Devang Patel1c56a632007-01-08 19:29:38 +00001799/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001800/// add self into that manager.
1801void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001802 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001803 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001804 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001805 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1806 if (TopPMType == PreferredType)
1807 break; // We found desired pass manager
1808 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001809 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001810 else
1811 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001812 }
Devang Patel18ff6362008-09-09 21:38:40 +00001813 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001814 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001815}
1816
Devang Patel3312f752007-01-16 21:43:18 +00001817/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001818/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001819void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001820 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001821
Andrew Trickcbc845f2012-02-01 07:16:20 +00001822 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001823 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001824 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1825 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001826 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001827 break;
Devang Patel3312f752007-01-16 21:43:18 +00001828 }
Devang Patel3312f752007-01-16 21:43:18 +00001829
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001830 // Create new Function Pass Manager if needed.
1831 FPPassManager *FPP;
1832 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1833 FPP = (FPPassManager *)PMS.top();
1834 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001835 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1836 PMDataManager *PMD = PMS.top();
1837
1838 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001839 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001840 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001841
1842 // [2] Set up new manager's top level manager
1843 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1844 TPM->addIndirectPassManager(FPP);
1845
1846 // [3] Assign manager to manage this new manager. This may create
1847 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001848 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001849
1850 // [4] Push new manager into PMS
1851 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001852 }
1853
Devang Patel3312f752007-01-16 21:43:18 +00001854 // Assign FPP as the manager of this pass.
1855 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001856}
1857
Devang Patel3312f752007-01-16 21:43:18 +00001858/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001859/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001860void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001861 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001862 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001863
Devang Patel15701b52007-01-11 00:19:00 +00001864 // Basic Pass Manager is a leaf pass manager. It does not handle
1865 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001866 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001867 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1868 BBP = (BBPassManager *)PMS.top();
1869 } else {
1870 // If leaf manager is not Basic Block Pass manager then create new
1871 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001872 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1873 PMDataManager *PMD = PMS.top();
1874
1875 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001876 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001877
1878 // [2] Set up new manager's top level manager
1879 // Basic Block Pass Manager does not live by itself
1880 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1881 TPM->addIndirectPassManager(BBP);
1882
Devang Patel15701b52007-01-11 00:19:00 +00001883 // [3] Assign manager to manage this new manager. This may create
1884 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001885 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001886
Devang Patel3312f752007-01-16 21:43:18 +00001887 // [4] Push new manager into PMS
1888 PMS.push(BBP);
1889 }
Devang Patel1c56a632007-01-08 19:29:38 +00001890
Devang Patel3312f752007-01-16 21:43:18 +00001891 // Assign BBP as the manager of this pass.
1892 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001893}
1894
Dan Gohmand3a20c92008-03-11 16:41:42 +00001895PassManagerBase::~PassManagerBase() {}