blob: 628a67bd639ce29f9cf5e0e2be63628f49532a80 [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.
Matthias Braun9f15a792016-11-18 19:43:18 +0000452 TimingInfo() : TG("pass", "... 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];
Matthias Braun9f15a792016-11-18 19:43:18 +0000475 if (!T) {
476 StringRef PassName = P->getPassName();
477 T = new Timer(PassName, PassName, TG);
478 }
Chris Lattnerec8ef9b2010-03-30 03:57:00 +0000479 return T;
Devang Patel1c3633e2007-01-29 23:10:37 +0000480 }
481};
482
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000483} // End of anon namespace
Devang Patelca58e352006-11-08 10:05:38 +0000484
Dan Gohmand78c4002008-05-13 00:00:25 +0000485static TimingInfo *TheTimeInfo;
486
Devang Patela1514cb2006-12-07 19:39:39 +0000487//===----------------------------------------------------------------------===//
Devang Patelafb1f3622006-12-12 22:35:25 +0000488// PMTopLevelManager implementation
489
Devang Patel4268fc02007-01-16 02:00:38 +0000490/// Initialize top level manager. Create first pass manager.
Dan Gohmane85c6192010-08-16 21:38:42 +0000491PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
492 PMDM->setTopLevelManager(this);
493 addPassManager(PMDM);
494 activeStack.push(PMDM);
Devang Patel4268fc02007-01-16 02:00:38 +0000495}
496
Devang Patelafb1f3622006-12-12 22:35:25 +0000497/// Set pass P as the last user of the given analysis passes.
Dan Gohmanc8da21b2010-10-12 00:12:29 +0000498void
Bill Wendlingea857e12012-05-14 07:53:40 +0000499PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grosserf07426b2011-01-20 21:03:22 +0000500 unsigned PDepth = 0;
501 if (P->getResolver())
502 PDepth = P->getResolver()->getPMDataManager().getDepth();
503
Yaron Keren4849aa32015-06-05 17:48:47 +0000504 for (Pass *AP : AnalysisPasses) {
Devang Patelafb1f3622006-12-12 22:35:25 +0000505 LastUser[AP] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000506
Devang Patel01919d22007-03-08 19:05:01 +0000507 if (P == AP)
508 continue;
509
Tobias Grosserf07426b2011-01-20 21:03:22 +0000510 // Update the last users of passes that are required transitive by AP.
511 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
512 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
513 SmallVector<Pass *, 12> LastUses;
514 SmallVector<Pass *, 12> LastPMUses;
Yaron Keren83009952016-04-28 14:49:44 +0000515 for (AnalysisID ID : IDs) {
516 Pass *AnalysisPass = findAnalysisPass(ID);
Tobias Grosserf07426b2011-01-20 21:03:22 +0000517 assert(AnalysisPass && "Expected analysis pass to exist.");
518 AnalysisResolver *AR = AnalysisPass->getResolver();
519 assert(AR && "Expected analysis resolver to exist.");
520 unsigned APDepth = AR->getPMDataManager().getDepth();
521
522 if (PDepth == APDepth)
523 LastUses.push_back(AnalysisPass);
524 else if (PDepth > APDepth)
525 LastPMUses.push_back(AnalysisPass);
526 }
527
528 setLastUser(LastUses, P);
529
530 // If this pass has a corresponding pass manager, push higher level
531 // analysis to this pass manager.
532 if (P->getResolver())
533 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
534
535
Devang Patelafb1f3622006-12-12 22:35:25 +0000536 // If AP is the last user of other passes then make P last user of
537 // such passes.
Yaron Kerenbd873192016-10-02 19:21:41 +0000538 for (auto LU : LastUser) {
539 if (LU.second == AP)
Devang Patelc68a0b62008-08-12 00:26:16 +0000540 // DenseMap iterator is not invalidated here because
Tobias Grosserf07426b2011-01-20 21:03:22 +0000541 // this is just updating existing entries.
Yaron Kerenbd873192016-10-02 19:21:41 +0000542 LastUser[LU.first] = P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000543 }
544 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000545}
546
547/// Collect passes whose last user is P
Dan Gohman7224bce2010-10-12 00:11:18 +0000548void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patelc68a0b62008-08-12 00:26:16 +0000549 Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000550 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patelc68a0b62008-08-12 00:26:16 +0000551 InversedLastUser.find(P);
552 if (DMI == InversedLastUser.end())
553 return;
554
555 SmallPtrSet<Pass *, 8> &LU = DMI->second;
Craig Topper46276792014-08-24 23:23:06 +0000556 for (Pass *LUP : LU) {
557 LastUses.push_back(LUP);
Devang Patelc68a0b62008-08-12 00:26:16 +0000558 }
559
Devang Patelafb1f3622006-12-12 22:35:25 +0000560}
561
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000562AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
Craig Topperc6207612014-04-09 06:08:46 +0000563 AnalysisUsage *AnUsage = nullptr;
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000564 auto DMI = AnUsageMap.find(P);
Dan Gohmande6188a2010-08-12 23:50:08 +0000565 if (DMI != AnUsageMap.end())
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000566 AnUsage = DMI->second;
567 else {
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000568 // Look up the analysis usage from the pass instance (different instances
569 // of the same pass can produce different results), but unique the
570 // resulting object to reduce memory usage. This helps to greatly reduce
571 // memory usage when we have many instances of only a few pass types
572 // (e.g. instcombine, simplifycfg, etc...) which tend to share a fixed set
573 // of dependencies.
574 AnalysisUsage AU;
575 P->getAnalysisUsage(AU);
576
577 AUFoldingSetNode* Node = nullptr;
578 FoldingSetNodeID ID;
579 AUFoldingSetNode::Profile(ID, AU);
580 void *IP = nullptr;
581 if (auto *N = UniqueAnalysisUsages.FindNodeOrInsertPos(ID, IP))
582 Node = N;
583 else {
Philip Reames000f77d2015-12-04 23:48:19 +0000584 Node = new (AUFoldingSetNodeAllocator.Allocate()) AUFoldingSetNode(AU);
Philip Reamese8aeaeb2015-12-04 20:05:04 +0000585 UniqueAnalysisUsages.InsertNode(Node, IP);
586 }
587 assert(Node && "cached analysis usage must be non null");
588
589 AnUsageMap[P] = &Node->AU;
590 AnUsage = &Node->AU;;
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000591 }
592 return AnUsage;
593}
594
Devang Patelafb1f3622006-12-12 22:35:25 +0000595/// Schedule pass P for execution. Make sure that passes required by
596/// P are run before P is run. Update analysis info maintained by
597/// the manager. Remove dead passes. This is a recursive function.
598void PMTopLevelManager::schedulePass(Pass *P) {
599
Devang Patel3312f752007-01-16 21:43:18 +0000600 // TODO : Allocate function manager for this pass, other wise required set
601 // may be inserted into previous function manager
Devang Patelafb1f3622006-12-12 22:35:25 +0000602
Devang Pateld74ede72007-03-06 01:06:16 +0000603 // Give pass a chance to prepare the stage.
604 P->preparePassManager(activeStack);
605
Devang Patel864970e2008-03-18 00:39:19 +0000606 // If P is an analysis pass and it is available then do not
607 // generate the analysis again. Stale analysis info should not be
608 // available at this point.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000609 const PassInfo *PI = findAnalysisPassInfo(P->getPassID());
Owen Andersona7aed182010-08-06 18:33:48 +0000610 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes0460bb22008-11-04 23:03:58 +0000611 delete P;
Devang Patelaf75ab82008-03-19 00:48:41 +0000612 return;
Nuno Lopes0460bb22008-11-04 23:03:58 +0000613 }
Devang Patel864970e2008-03-18 00:39:19 +0000614
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000615 AnalysisUsage *AnUsage = findAnalysisUsage(P);
616
Devang Patelfdee7032008-08-14 23:07:48 +0000617 bool checkAnalysis = true;
618 while (checkAnalysis) {
619 checkAnalysis = false;
Dan Gohmande6188a2010-08-12 23:50:08 +0000620
Devang Patelfdee7032008-08-14 23:07:48 +0000621 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
622 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
623 E = RequiredSet.end(); I != E; ++I) {
Dan Gohmande6188a2010-08-12 23:50:08 +0000624
Devang Patelfdee7032008-08-14 23:07:48 +0000625 Pass *AnalysisPass = findAnalysisPass(*I);
626 if (!AnalysisPass) {
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000627 const PassInfo *PI = findAnalysisPassInfo(*I);
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000628
Craig Topperc6207612014-04-09 06:08:46 +0000629 if (!PI) {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000630 // Pass P is not in the global PassRegistry
631 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
632 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
633 dbgs() << "Required Passes:" << "\n";
634 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
635 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
636 Pass *AnalysisPass2 = findAnalysisPass(*I2);
637 if (AnalysisPass2) {
638 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper821d6af2013-02-06 06:50:38 +0000639 } else {
Victor Oliveiraaa9ccee2012-07-18 19:59:29 +0000640 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
641 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
642 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
643 }
644 }
645 }
646
Andrew Trick6bbaf132011-06-03 00:48:58 +0000647 assert(PI && "Expected required passes to be initialized");
Owen Andersona7aed182010-08-06 18:33:48 +0000648 AnalysisPass = PI->createPass();
Devang Patelfdee7032008-08-14 23:07:48 +0000649 if (P->getPotentialPassManagerType () ==
650 AnalysisPass->getPotentialPassManagerType())
651 // Schedule analysis pass that is managed by the same pass manager.
652 schedulePass(AnalysisPass);
653 else if (P->getPotentialPassManagerType () >
654 AnalysisPass->getPotentialPassManagerType()) {
655 // Schedule analysis pass that is managed by a new manager.
656 schedulePass(AnalysisPass);
Dan Gohman6304db32010-08-16 22:57:28 +0000657 // Recheck analysis passes to ensure that required analyses that
Devang Patelfdee7032008-08-14 23:07:48 +0000658 // are already checked are still available.
659 checkAnalysis = true;
Craig Topper821d6af2013-02-06 06:50:38 +0000660 } else
Chad Rosierc83fa9e2015-03-20 15:45:14 +0000661 // Do not schedule this analysis. Lower level analysis
Devang Patelfdee7032008-08-14 23:07:48 +0000662 // passes are run on the fly.
663 delete AnalysisPass;
664 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000665 }
666 }
667
668 // Now all required passes are available.
Andrew Trickcbc845f2012-02-01 07:16:20 +0000669 if (ImmutablePass *IP = P->getAsImmutablePass()) {
670 // P is a immutable pass and it will be managed by this
671 // top level manager. Set up analysis resolver to connect them.
672 PMDataManager *DM = getAsPMDataManager();
673 AnalysisResolver *AR = new AnalysisResolver(*DM);
674 P->setResolver(AR);
675 DM->initializeAnalysisImpl(P);
676 addImmutablePass(IP);
677 DM->recordAvailableAnalysis(IP);
678 return;
679 }
680
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000681 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000682 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000683 dbgs(), ("*** IR Dump Before " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000684 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
685 }
686
687 // Add the requested pass to the best available pass manager.
688 P->assignPassManager(activeStack, getTopLevelPassManagerType());
689
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000690 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
Andrew Trickcbc845f2012-02-01 07:16:20 +0000691 Pass *PP = P->createPrinterPass(
Mehdi Amini117296c2016-10-01 02:56:57 +0000692 dbgs(), ("*** IR Dump After " + P->getPassName() + " ***").str());
Andrew Trickcbc845f2012-02-01 07:16:20 +0000693 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
694 }
Devang Patelafb1f3622006-12-12 22:35:25 +0000695}
696
697/// Find the pass that implements Analysis AID. Search immutable
698/// passes and all pass managers. If desired pass is not found
699/// then return NULL.
700Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000701 // For immutable passes we have a direct mapping from ID to pass, so check
702 // that first.
703 if (Pass *P = ImmutablePassMap.lookup(AID))
704 return P;
Devang Patelafb1f3622006-12-12 22:35:25 +0000705
Devang Patelcd6ba152006-12-12 22:50:05 +0000706 // Check pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000707 for (PMDataManager *PassManager : PassManagers)
708 if (Pass *P = PassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000709 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000710
711 // Check other pass managers
Yaron Keren4849aa32015-06-05 17:48:47 +0000712 for (PMDataManager *IndirectPassManager : IndirectPassManagers)
713 if (Pass *P = IndirectPassManager->findAnalysisPass(AID, false))
Dan Gohman844dd0a2010-10-11 23:19:01 +0000714 return P;
Devang Patelcd6ba152006-12-12 22:50:05 +0000715
Craig Topperc6207612014-04-09 06:08:46 +0000716 return nullptr;
Devang Patelafb1f3622006-12-12 22:35:25 +0000717}
718
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000719const PassInfo *PMTopLevelManager::findAnalysisPassInfo(AnalysisID AID) const {
720 const PassInfo *&PI = AnalysisPassInfos[AID];
721 if (!PI)
722 PI = PassRegistry::getPassRegistry()->getPassInfo(AID);
723 else
724 assert(PI == PassRegistry::getPassRegistry()->getPassInfo(AID) &&
725 "The pass info pointer changed for an analysis ID!");
726
727 return PI;
728}
729
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000730void PMTopLevelManager::addImmutablePass(ImmutablePass *P) {
731 P->initializePass();
732 ImmutablePasses.push_back(P);
733
734 // Add this pass to the map from its analysis ID. We clobber any prior runs
735 // of the pass in the map so that the last one added is the one found when
736 // doing lookups.
737 AnalysisID AID = P->getPassID();
738 ImmutablePassMap[AID] = P;
739
740 // Also add any interfaces implemented by the immutable pass to the map for
741 // fast lookup.
742 const PassInfo *PassInf = findAnalysisPassInfo(AID);
743 assert(PassInf && "Expected all immutable passes to be initialized");
Chandler Carruth87275182015-09-10 04:22:36 +0000744 for (const PassInfo *ImmPI : PassInf->getInterfacesImplemented())
Chandler Carruthb1e3a9a2015-09-10 02:31:42 +0000745 ImmutablePassMap[ImmPI->getTypeInfo()] = P;
746}
747
Devang Pateleda56172006-12-12 23:34:33 +0000748// Print passes managed by this top level manager.
Devang Patel991aeba2006-12-15 20:13:01 +0000749void PMTopLevelManager::dumpPasses() const {
Devang Pateleda56172006-12-12 23:34:33 +0000750
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000751 if (PassDebugging < Structure)
Devang Patel67d6a5e2006-12-19 19:46:59 +0000752 return;
753
Devang Pateleda56172006-12-12 23:34:33 +0000754 // Print out the immutable passes
755 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohmanf71c5212010-08-19 01:29:07 +0000756 ImmutablePasses[i]->dumpPassStructure(0);
Devang Pateleda56172006-12-12 23:34:33 +0000757 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000758
Dan Gohmanf71c5212010-08-19 01:29:07 +0000759 // Every class that derives from PMDataManager also derives from Pass
760 // (sometimes indirectly), but there's no inheritance relationship
761 // between PMDataManager and Pass, so we have to getAsPass to get
762 // from a PMDataManager* to a Pass*.
Yaron Keren4849aa32015-06-05 17:48:47 +0000763 for (PMDataManager *Manager : PassManagers)
764 Manager->getAsPass()->dumpPassStructure(1);
Devang Pateleda56172006-12-12 23:34:33 +0000765}
766
Devang Patel991aeba2006-12-15 20:13:01 +0000767void PMTopLevelManager::dumpArguments() const {
Devang Patelcfd70c42006-12-13 22:10:00 +0000768
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000769 if (PassDebugging < Arguments)
Devang Patelcfd70c42006-12-13 22:10:00 +0000770 return;
771
David Greene994e1bb2010-01-05 01:30:02 +0000772 dbgs() << "Pass Arguments: ";
Yaron Keren83009952016-04-28 14:49:44 +0000773 for (ImmutablePass *P : ImmutablePasses)
774 if (const PassInfo *PI = findAnalysisPassInfo(P->getPassID())) {
Andrew Trick6bbaf132011-06-03 00:48:58 +0000775 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohmanf51d06bb2010-11-11 16:32:17 +0000776 if (!PI->isAnalysisGroup())
777 dbgs() << " -" << PI->getPassArgument();
Andrew Trick6bbaf132011-06-03 00:48:58 +0000778 }
Yaron Keren83009952016-04-28 14:49:44 +0000779 for (PMDataManager *PM : PassManagers)
780 PM->dumpPassArguments();
David Greene994e1bb2010-01-05 01:30:02 +0000781 dbgs() << "\n";
Devang Patelcfd70c42006-12-13 22:10:00 +0000782}
783
Devang Patele3068402006-12-21 00:16:50 +0000784void PMTopLevelManager::initializeAllAnalysisInfo() {
Yaron Keren83009952016-04-28 14:49:44 +0000785 for (PMDataManager *PM : PassManagers)
786 PM->initializeAnalysisInfo();
Dan Gohmande6188a2010-08-12 23:50:08 +0000787
Devang Patele3068402006-12-21 00:16:50 +0000788 // Initailize other pass managers
Yaron Keren83009952016-04-28 14:49:44 +0000789 for (PMDataManager *IPM : IndirectPassManagers)
790 IPM->initializeAnalysisInfo();
Devang Patelc68a0b62008-08-12 00:26:16 +0000791
Yaron Kerenbd873192016-10-02 19:21:41 +0000792 for (auto LU : LastUser) {
793 SmallPtrSet<Pass *, 8> &L = InversedLastUser[LU.second];
794 L.insert(LU.first);
Devang Patelc68a0b62008-08-12 00:26:16 +0000795 }
Devang Patele3068402006-12-21 00:16:50 +0000796}
797
Devang Patele7599552007-01-12 18:52:44 +0000798/// Destructor
799PMTopLevelManager::~PMTopLevelManager() {
Yaron Keren83009952016-04-28 14:49:44 +0000800 for (PMDataManager *PM : PassManagers)
801 delete PM;
Dan Gohmande6188a2010-08-12 23:50:08 +0000802
Yaron Keren83009952016-04-28 14:49:44 +0000803 for (ImmutablePass *P : ImmutablePasses)
804 delete P;
Devang Patele7599552007-01-12 18:52:44 +0000805}
806
Devang Patelafb1f3622006-12-12 22:35:25 +0000807//===----------------------------------------------------------------------===//
Devang Pateldbe4a1e2006-12-07 18:36:24 +0000808// PMDataManager implementation
Devang Patelf68a3492006-11-07 22:35:17 +0000809
Devang Patel643676c2006-11-11 01:10:19 +0000810/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patele9976aa2006-12-07 19:33:53 +0000811void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Andersona7aed182010-08-06 18:33:48 +0000812 AnalysisID PI = P->getPassID();
Dan Gohmande6188a2010-08-12 23:50:08 +0000813
Chris Lattner60987362009-03-06 05:53:14 +0000814 AvailableAnalysis[PI] = P;
Dan Gohmande6188a2010-08-12 23:50:08 +0000815
Dan Gohmanb83d1b62010-08-12 23:46:28 +0000816 assert(!AvailableAnalysis.empty());
Devang Patel643676c2006-11-11 01:10:19 +0000817
Dan Gohmande6188a2010-08-12 23:50:08 +0000818 // This pass is the current implementation of all of the interfaces it
819 // implements as well.
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000820 const PassInfo *PInf = TPM->findAnalysisPassInfo(PI);
Craig Topperc6207612014-04-09 06:08:46 +0000821 if (!PInf) return;
Owen Andersona7aed182010-08-06 18:33:48 +0000822 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000823 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Andersona7aed182010-08-06 18:33:48 +0000824 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patel643676c2006-11-11 01:10:19 +0000825}
826
Devang Patel9d9fc902007-03-06 17:52:53 +0000827// Return true if P preserves high level analysis used by other
828// passes managed by this manager
829bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000830 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000831 if (AnUsage->getPreservesAll())
Devang Patel9d9fc902007-03-06 17:52:53 +0000832 return true;
Dan Gohmande6188a2010-08-12 23:50:08 +0000833
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000834 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Yaron Kerenbd873192016-10-02 19:21:41 +0000835 for (Pass *P1 : HigherLevelAnalysis) {
Craig Topperc6207612014-04-09 06:08:46 +0000836 if (P1->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000837 !is_contained(PreservedSet, P1->getPassID()))
Devang Patel01919d22007-03-08 19:05:01 +0000838 return false;
Devang Patel9d9fc902007-03-06 17:52:53 +0000839 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000840
Devang Patel9d9fc902007-03-06 17:52:53 +0000841 return true;
842}
843
Chris Lattner02eb94c2008-08-07 07:34:50 +0000844/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patela273d1c2007-07-19 18:02:32 +0000845void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattner02eb94c2008-08-07 07:34:50 +0000846 // Don't do this unless assertions are enabled.
847#ifdef NDEBUG
848 return;
849#endif
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000850 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
851 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelf68a3492006-11-07 22:35:17 +0000852
Devang Patelef432532007-07-19 05:36:09 +0000853 // Verify preserved analysis
Yaron Kerenbd873192016-10-02 19:21:41 +0000854 for (AnalysisID AID : PreservedSet) {
Dan Gohman4dbb3012009-09-28 00:27:48 +0000855 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattner707431c2010-03-30 04:03:22 +0000856 TimeRegion PassTimer(getPassTimer(AP));
Devang Patela273d1c2007-07-19 18:02:32 +0000857 AP->verifyAnalysis();
Dan Gohman4dbb3012009-09-28 00:27:48 +0000858 }
Devang Patel9dbe4d12008-07-01 17:44:24 +0000859 }
860}
861
Devang Patel67c79a42008-07-01 19:50:56 +0000862/// Remove Analysis not preserved by Pass P
Devang Patela273d1c2007-07-19 18:02:32 +0000863void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000864 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
865 if (AnUsage->getPreservesAll())
Devang Patel2e169c32006-12-07 20:03:49 +0000866 return;
867
Devang Patelec9e1a60a2008-08-11 21:13:39 +0000868 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000869 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patelbe6bd55e2006-12-12 23:07:44 +0000870 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000871 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000872 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000873 !is_contained(PreservedSet, Info->first)) {
Devang Patel349170f2006-11-11 01:24:55 +0000874 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000875 if (PassDebugging >= Details) {
Devang Patelbb4720c2008-06-03 01:02:16 +0000876 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000877 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
878 dbgs() << S->getPassName() << "'\n";
Devang Patelbb4720c2008-06-03 01:02:16 +0000879 }
Dan Gohman193e4c02008-11-06 21:57:17 +0000880 AvailableAnalysis.erase(Info);
Devang Patelbb4720c2008-06-03 01:02:16 +0000881 }
Devang Patel349170f2006-11-11 01:24:55 +0000882 }
Dan Gohmande6188a2010-08-12 23:50:08 +0000883
Devang Patel42dd1e92007-03-06 01:55:46 +0000884 // Check inherited analysis also. If P is not preserving analysis
885 // provided by parent manager then remove it here.
886 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
887
888 if (!InheritedAnalysis[Index])
889 continue;
890
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000891 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patel42dd1e92007-03-06 01:55:46 +0000892 I = InheritedAnalysis[Index]->begin(),
893 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000894 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Craig Topperc6207612014-04-09 06:08:46 +0000895 if (Info->second->getAsImmutablePass() == nullptr &&
David Majnemer0d955d02016-08-11 22:21:41 +0000896 !is_contained(PreservedSet, Info->first)) {
Devang Patel42dd1e92007-03-06 01:55:46 +0000897 // Remove this analysis
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000898 if (PassDebugging >= Details) {
Andreas Neustifter46651412009-12-04 06:58:24 +0000899 Pass *S = Info->second;
David Greene994e1bb2010-01-05 01:30:02 +0000900 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
901 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter46651412009-12-04 06:58:24 +0000902 }
Devang Patel01919d22007-03-08 19:05:01 +0000903 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter46651412009-12-04 06:58:24 +0000904 }
Devang Patel42dd1e92007-03-06 01:55:46 +0000905 }
906 }
Devang Patelf68a3492006-11-07 22:35:17 +0000907}
908
Devang Patelca189262006-11-14 03:05:08 +0000909/// Remove analysis passes that are not used any longer
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000910void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel003a5592007-03-05 20:01:30 +0000911 enum PassDebuggingString DBG_STR) {
Devang Patel17ad0962006-12-08 00:37:52 +0000912
Devang Patel8adae862007-07-20 18:04:54 +0000913 SmallVector<Pass *, 12> DeadPasses;
Devang Patel69e9f6d2007-04-16 20:27:05 +0000914
Devang Patel2ff44922007-04-16 20:39:59 +0000915 // If this is a on the fly manager then it does not have TPM.
Devang Patel69e9f6d2007-04-16 20:27:05 +0000916 if (!TPM)
917 return;
918
Devang Patel17ad0962006-12-08 00:37:52 +0000919 TPM->collectLastUses(DeadPasses, P);
920
Andrew Trickb5e1e6c2013-09-19 06:02:43 +0000921 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene994e1bb2010-01-05 01:30:02 +0000922 dbgs() << " -*- '" << P->getPassName();
923 dbgs() << "' is the last user of following pass instances.";
924 dbgs() << " Free these instances\n";
Evan Cheng93af6ce2008-06-04 09:13:31 +0000925 }
926
Yaron Kerenbd873192016-10-02 19:21:41 +0000927 for (Pass *P : DeadPasses)
928 freePass(P, Msg, DBG_STR);
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000929}
Devang Patel200d3052006-12-13 23:50:44 +0000930
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000931void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000932 enum PassDebuggingString DBG_STR) {
933 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel200d3052006-12-13 23:50:44 +0000934
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000935 {
936 // If the pass crashes releasing memory, remember this.
937 PassManagerPrettyStackEntry X(P);
Chris Lattner707431c2010-03-30 04:03:22 +0000938 TimeRegion PassTimer(getPassTimer(P));
939
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000940 P->releaseMemory();
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000941 }
942
Owen Andersona7aed182010-08-06 18:33:48 +0000943 AnalysisID PI = P->getPassID();
Chandler Carruth5b0d3e32015-01-28 09:47:21 +0000944 if (const PassInfo *PInf = TPM->findAnalysisPassInfo(PI)) {
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000945 // Remove the pass itself (if it is not already removed).
946 AvailableAnalysis.erase(PI);
947
948 // Remove all interfaces this pass implements, for which it is also
949 // listed as the available implementation.
Owen Andersona7aed182010-08-06 18:33:48 +0000950 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson3183ef12010-07-20 16:55:05 +0000951 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +0000952 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Andersona7aed182010-08-06 18:33:48 +0000953 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman5e8ba5d2009-09-27 23:38:27 +0000954 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patelc3e3ca92008-10-06 20:36:36 +0000955 AvailableAnalysis.erase(Pos);
Devang Patelc3e3ca92008-10-06 20:36:36 +0000956 }
Devang Patel17ad0962006-12-08 00:37:52 +0000957 }
Devang Patelca189262006-11-14 03:05:08 +0000958}
959
Dan Gohmande6188a2010-08-12 23:50:08 +0000960/// Add pass P into the PassVector. Update
Devang Patel90b05e02006-11-11 02:04:19 +0000961/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattner60987362009-03-06 05:53:14 +0000962void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Pateld440cd92006-12-08 23:53:00 +0000963 // This manager is going to manage pass P. Set up analysis resolver
964 // to connect them.
Devang Patelb66334b2007-01-05 22:47:07 +0000965 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Pateld440cd92006-12-08 23:53:00 +0000966 P->setResolver(AR);
967
Devang Patelec2b9a72007-03-05 22:57:49 +0000968 // If a FunctionPass F is the last user of ModulePass info M
969 // then the F's manager, not F, records itself as a last user of M.
Devang Patel8adae862007-07-20 18:04:54 +0000970 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelec2b9a72007-03-05 22:57:49 +0000971
Chris Lattner60987362009-03-06 05:53:14 +0000972 if (!ProcessAnalysis) {
973 // Add pass
974 PassVector.push_back(P);
975 return;
Devang Patel90b05e02006-11-11 02:04:19 +0000976 }
Devang Patel8cad70d2006-11-11 01:51:02 +0000977
Chris Lattner60987362009-03-06 05:53:14 +0000978 // At the moment, this pass is the last user of all required passes.
979 SmallVector<Pass *, 12> LastUses;
Chandler Carruth44a13852015-08-19 03:02:12 +0000980 SmallVector<Pass *, 8> UsedPasses;
Chris Lattner60987362009-03-06 05:53:14 +0000981 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
982
983 unsigned PDepth = this->getDepth();
984
Chandler Carruth44a13852015-08-19 03:02:12 +0000985 collectRequiredAndUsedAnalyses(UsedPasses, ReqAnalysisNotAvailable, P);
986 for (Pass *PUsed : UsedPasses) {
Chris Lattner60987362009-03-06 05:53:14 +0000987 unsigned RDepth = 0;
988
Chandler Carruth44a13852015-08-19 03:02:12 +0000989 assert(PUsed->getResolver() && "Analysis Resolver is not set");
990 PMDataManager &DM = PUsed->getResolver()->getPMDataManager();
Chris Lattner60987362009-03-06 05:53:14 +0000991 RDepth = DM.getDepth();
992
993 if (PDepth == RDepth)
Chandler Carruth44a13852015-08-19 03:02:12 +0000994 LastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +0000995 else if (PDepth > RDepth) {
996 // Let the parent claim responsibility of last use
Chandler Carruth44a13852015-08-19 03:02:12 +0000997 TransferLastUses.push_back(PUsed);
Chris Lattner60987362009-03-06 05:53:14 +0000998 // Keep track of higher level analysis used by this manager.
Chandler Carruth44a13852015-08-19 03:02:12 +0000999 HigherLevelAnalysis.push_back(PUsed);
Dan Gohmande6188a2010-08-12 23:50:08 +00001000 } else
Chandler Carruth44a13852015-08-19 03:02:12 +00001001 llvm_unreachable("Unable to accommodate Used Pass");
Chris Lattner60987362009-03-06 05:53:14 +00001002 }
1003
1004 // Set P as P's last user until someone starts using P.
1005 // However, if P is a Pass Manager then it does not need
1006 // to record its last user.
Craig Topperc6207612014-04-09 06:08:46 +00001007 if (!P->getAsPMDataManager())
Chris Lattner60987362009-03-06 05:53:14 +00001008 LastUses.push_back(P);
1009 TPM->setLastUser(LastUses, P);
1010
1011 if (!TransferLastUses.empty()) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001012 Pass *My_PM = getAsPass();
Chris Lattner60987362009-03-06 05:53:14 +00001013 TPM->setLastUser(TransferLastUses, My_PM);
1014 TransferLastUses.clear();
1015 }
1016
Dan Gohman6304db32010-08-16 22:57:28 +00001017 // Now, take care of required analyses that are not available.
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001018 for (AnalysisID ID : ReqAnalysisNotAvailable) {
1019 const PassInfo *PI = TPM->findAnalysisPassInfo(ID);
Owen Andersona7aed182010-08-06 18:33:48 +00001020 Pass *AnalysisPass = PI->createPass();
Chris Lattner60987362009-03-06 05:53:14 +00001021 this->addLowerLevelRequiredPass(P, AnalysisPass);
1022 }
1023
1024 // Take a note of analysis required and made available by this pass.
1025 // Remove the analysis not preserved by this pass
1026 removeNotPreservedAnalysis(P);
1027 recordAvailableAnalysis(P);
1028
Devang Patel8cad70d2006-11-11 01:51:02 +00001029 // Add pass
1030 PassVector.push_back(P);
Devang Patel8cad70d2006-11-11 01:51:02 +00001031}
1032
Devang Patele64d3052007-04-16 20:12:57 +00001033
Chandler Carruth44a13852015-08-19 03:02:12 +00001034/// Populate UP with analysis pass that are used or required by
Devang Patele64d3052007-04-16 20:12:57 +00001035/// pass P and are available. Populate RP_NotAvail with analysis
1036/// pass that are required by pass P but are not available.
Chandler Carruth44a13852015-08-19 03:02:12 +00001037void PMDataManager::collectRequiredAndUsedAnalyses(
1038 SmallVectorImpl<Pass *> &UP, SmallVectorImpl<AnalysisID> &RP_NotAvail,
1039 Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001040 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Chandler Carruth44a13852015-08-19 03:02:12 +00001041
1042 for (const auto &UsedID : AnUsage->getUsedSet())
1043 if (Pass *AnalysisPass = findAnalysisPass(UsedID, true))
1044 UP.push_back(AnalysisPass);
1045
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001046 for (const auto &RequiredID : AnUsage->getRequiredSet())
1047 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001048 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001049 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001050 RP_NotAvail.push_back(RequiredID);
Devang Patelf58183d2006-12-12 23:09:32 +00001051
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001052 for (const auto &RequiredID : AnUsage->getRequiredTransitiveSet())
1053 if (Pass *AnalysisPass = findAnalysisPass(RequiredID, true))
Chandler Carruth44a13852015-08-19 03:02:12 +00001054 UP.push_back(AnalysisPass);
Devang Patele64d3052007-04-16 20:12:57 +00001055 else
Chandler Carruth2f02ea42015-08-18 18:41:53 +00001056 RP_NotAvail.push_back(RequiredID);
Devang Patel1d6267c2006-12-07 23:05:44 +00001057}
1058
Devang Patel07f4f582006-11-14 21:49:36 +00001059// All Required analyses should be available to the pass as it runs! Here
1060// we fill in the AnalysisImpls member of the pass so that it can
1061// successfully use the getAnalysis() method to retrieve the
1062// implementations it needs.
1063//
Devang Pateldbe4a1e2006-12-07 18:36:24 +00001064void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001065 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1066
Chris Lattnercbd160f2008-08-08 05:33:04 +00001067 for (AnalysisUsage::VectorType::const_iterator
Devang Patelec9e1a60a2008-08-11 21:13:39 +00001068 I = AnUsage->getRequiredSet().begin(),
1069 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel640c5bb2006-12-08 22:30:11 +00001070 Pass *Impl = findAnalysisPass(*I, true);
Craig Topperc6207612014-04-09 06:08:46 +00001071 if (!Impl)
Devang Patel56a5c622007-04-16 20:44:16 +00001072 // This may be analysis pass that is initialized on the fly.
1073 // If that is not the case then it will raise an assert when it is used.
1074 continue;
Devang Patelb66334b2007-01-05 22:47:07 +00001075 AnalysisResolver *AR = P->getResolver();
Chris Lattner60987362009-03-06 05:53:14 +00001076 assert(AR && "Analysis Resolver is not set");
Devang Patel984698a2006-12-09 01:11:34 +00001077 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel07f4f582006-11-14 21:49:36 +00001078 }
1079}
1080
Devang Patel640c5bb2006-12-08 22:30:11 +00001081/// Find the pass that implements Analysis AID. If desired pass is not found
1082/// then return NULL.
1083Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1084
1085 // Check if AvailableAnalysis map has one entry.
Michael Ilsemanc33b6ac2013-02-26 01:31:59 +00001086 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel640c5bb2006-12-08 22:30:11 +00001087
1088 if (I != AvailableAnalysis.end())
1089 return I->second;
1090
1091 // Search Parents through TopLevelManager
1092 if (SearchParent)
1093 return TPM->findAnalysisPass(AID);
Dan Gohmande6188a2010-08-12 23:50:08 +00001094
Craig Topperc6207612014-04-09 06:08:46 +00001095 return nullptr;
Devang Patel640c5bb2006-12-08 22:30:11 +00001096}
1097
Devang Patel991aeba2006-12-15 20:13:01 +00001098// Print list of passes that are last used by P.
1099void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1100
Devang Patel8adae862007-07-20 18:04:54 +00001101 SmallVector<Pass *, 12> LUses;
Devang Patel2ff44922007-04-16 20:39:59 +00001102
1103 // If this is a on the fly manager then it does not have TPM.
1104 if (!TPM)
1105 return;
1106
Devang Patel991aeba2006-12-15 20:13:01 +00001107 TPM->collectLastUses(LUses, P);
Dan Gohmande6188a2010-08-12 23:50:08 +00001108
Dan Gohman7224bce2010-10-12 00:11:18 +00001109 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001110 E = LUses.end(); I != E; ++I) {
Eric Christophera13839f2014-02-26 23:27:16 +00001111 dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohmanf71c5212010-08-19 01:29:07 +00001112 (*I)->dumpPassStructure(0);
Devang Patel991aeba2006-12-15 20:13:01 +00001113 }
1114}
1115
1116void PMDataManager::dumpPassArguments() const {
Dan Gohman7224bce2010-10-12 00:11:18 +00001117 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patel991aeba2006-12-15 20:13:01 +00001118 E = PassVector.end(); I != E; ++I) {
Chris Lattner2fa26e52010-01-22 05:24:46 +00001119 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patel991aeba2006-12-15 20:13:01 +00001120 PMD->dumpPassArguments();
1121 else
Owen Andersona7aed182010-08-06 18:33:48 +00001122 if (const PassInfo *PI =
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001123 TPM->findAnalysisPassInfo((*I)->getPassID()))
Devang Patel991aeba2006-12-15 20:13:01 +00001124 if (!PI->isAnalysisGroup())
David Greene994e1bb2010-01-05 01:30:02 +00001125 dbgs() << " -" << PI->getPassArgument();
Devang Patel991aeba2006-12-15 20:13:01 +00001126 }
1127}
1128
Chris Lattnerdd6304f2007-08-10 06:17:04 +00001129void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1130 enum PassDebuggingString S2,
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001131 StringRef Msg) {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001132 if (PassDebugging < Executions)
Devang Patel991aeba2006-12-15 20:13:01 +00001133 return;
Pavel Labathec534e62016-10-25 16:20:07 +00001134 dbgs() << "[" << std::chrono::system_clock::now() << "] " << (void *)this
Chandler Carruth20c56932014-04-27 23:59:25 +00001135 << std::string(getDepth() * 2 + 1, ' ');
Devang Patel003a5592007-03-05 20:01:30 +00001136 switch (S1) {
1137 case EXECUTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001138 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001139 break;
1140 case MODIFICATION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001141 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001142 break;
1143 case FREEING_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001144 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel003a5592007-03-05 20:01:30 +00001145 break;
1146 default:
1147 break;
1148 }
1149 switch (S2) {
1150 case ON_BASICBLOCK_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001151 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001152 break;
1153 case ON_FUNCTION_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001154 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001155 break;
1156 case ON_MODULE_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001157 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001158 break;
Tobias Grosser23c83412010-10-20 01:54:44 +00001159 case ON_REGION_MSG:
1160 dbgs() << "' on Region '" << Msg << "'...\n";
1161 break;
Devang Patel003a5592007-03-05 20:01:30 +00001162 case ON_LOOP_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001163 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001164 break;
1165 case ON_CG_MSG:
David Greene994e1bb2010-01-05 01:30:02 +00001166 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel003a5592007-03-05 20:01:30 +00001167 break;
1168 default:
1169 break;
1170 }
Devang Patel991aeba2006-12-15 20:13:01 +00001171}
1172
Chris Lattner4c1e9542009-03-06 06:45:05 +00001173void PMDataManager::dumpRequiredSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001174 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001175 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001176
Chris Lattner4c493d92008-08-08 15:14:09 +00001177 AnalysisUsage analysisUsage;
1178 P->getAnalysisUsage(analysisUsage);
1179 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1180}
1181
Chris Lattner4c1e9542009-03-06 06:45:05 +00001182void PMDataManager::dumpPreservedSet(const Pass *P) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001183 if (PassDebugging < Details)
Chris Lattner4c493d92008-08-08 15:14:09 +00001184 return;
Dan Gohmande6188a2010-08-12 23:50:08 +00001185
Chris Lattner4c493d92008-08-08 15:14:09 +00001186 AnalysisUsage analysisUsage;
1187 P->getAnalysisUsage(analysisUsage);
1188 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1189}
1190
Chandler Carruth44a13852015-08-19 03:02:12 +00001191void PMDataManager::dumpUsedSet(const Pass *P) const {
1192 if (PassDebugging < Details)
1193 return;
1194
1195 AnalysisUsage analysisUsage;
1196 P->getAnalysisUsage(analysisUsage);
1197 dumpAnalysisUsage("Used", P, analysisUsage.getUsedSet());
1198}
1199
Daniel Dunbarad36e8a2009-11-06 10:58:06 +00001200void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattner4c1e9542009-03-06 06:45:05 +00001201 const AnalysisUsage::VectorType &Set) const {
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001202 assert(PassDebugging >= Details);
Chris Lattner4c493d92008-08-08 15:14:09 +00001203 if (Set.empty())
1204 return;
Roman Divackyad06cee2012-09-05 22:26:57 +00001205 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattner4c1e9542009-03-06 06:45:05 +00001206 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene994e1bb2010-01-05 01:30:02 +00001207 if (i) dbgs() << ',';
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001208 const PassInfo *PInf = TPM->findAnalysisPassInfo(Set[i]);
Andrew Trick6bbaf132011-06-03 00:48:58 +00001209 if (!PInf) {
1210 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1211 // all drivers.
1212 dbgs() << " Uninitialized Pass";
1213 continue;
1214 }
Owen Andersona7aed182010-08-06 18:33:48 +00001215 dbgs() << ' ' << PInf->getPassName();
Chris Lattner4c1e9542009-03-06 06:45:05 +00001216 }
David Greene994e1bb2010-01-05 01:30:02 +00001217 dbgs() << '\n';
Devang Patel991aeba2006-12-15 20:13:01 +00001218}
Devang Patel9bdf7d42006-12-08 23:28:54 +00001219
Devang Patel004937b2007-07-27 20:06:09 +00001220/// Add RequiredPass into list of lower level passes required by pass P.
1221/// RequiredPass is run on the fly by Pass Manager when P requests it
1222/// through getAnalysis interface.
1223/// This should be handled by specific pass manager.
1224void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1225 if (TPM) {
1226 TPM->dumpArguments();
1227 TPM->dumpPasses();
1228 }
Devang Patel8df7cc12008-02-02 01:43:30 +00001229
Dan Gohmande6188a2010-08-12 23:50:08 +00001230 // Module Level pass may required Function Level analysis info
1231 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1232 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel8df7cc12008-02-02 01:43:30 +00001233 // module level pass is requiring lower level analysis info managed by
1234 // lower level pass manager.
1235
1236 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001237 // checks whether any lower level manager will be able to provide this
Devang Patel8df7cc12008-02-02 01:43:30 +00001238 // analysis info on demand or not.
Devang Patelab85d6b2008-06-03 01:20:02 +00001239#ifndef NDEBUG
David Greene994e1bb2010-01-05 01:30:02 +00001240 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1241 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelab85d6b2008-06-03 01:20:02 +00001242#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +00001243 llvm_unreachable("Unable to schedule pass");
Devang Patel004937b2007-07-27 20:06:09 +00001244}
1245
Owen Andersona7aed182010-08-06 18:33:48 +00001246Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topperc514b542012-02-05 22:14:15 +00001247 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmanffdee302010-06-21 18:46:45 +00001248}
1249
Devang Patele7599552007-01-12 18:52:44 +00001250// Destructor
1251PMDataManager::~PMDataManager() {
Dan Gohman7224bce2010-10-12 00:11:18 +00001252 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patele7599552007-01-12 18:52:44 +00001253 E = PassVector.end(); I != E; ++I)
1254 delete *I;
Devang Patele7599552007-01-12 18:52:44 +00001255}
1256
Devang Patel9bdf7d42006-12-08 23:28:54 +00001257//===----------------------------------------------------------------------===//
1258// NOTE: Is this the right place to define this method ?
Duncan Sands5a913d62009-01-28 13:14:17 +00001259// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1260Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patel9bdf7d42006-12-08 23:28:54 +00001261 return PM.findAnalysisPass(ID, dir);
1262}
1263
Dan Gohmande6188a2010-08-12 23:50:08 +00001264Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel92942812007-04-16 20:56:24 +00001265 Function &F) {
1266 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1267}
1268
Devang Patela1514cb2006-12-07 19:39:39 +00001269//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001270// BBPassManager implementation
Devang Patel6e5a1132006-11-07 21:31:57 +00001271
Dan Gohmande6188a2010-08-12 23:50:08 +00001272/// Execute all of the passes scheduled for execution by invoking
1273/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel6e5a1132006-11-07 21:31:57 +00001274/// the function, and if so, return true.
Chris Lattner4c1e9542009-03-06 06:45:05 +00001275bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +00001276 if (F.isDeclaration())
Devang Patel745a6962006-12-12 23:15:28 +00001277 return false;
1278
Devang Patele9585592006-12-08 01:38:28 +00001279 bool Changed = doInitialization(F);
Devang Patel050ec722006-11-14 01:23:29 +00001280
Devang Patel6e5a1132006-11-07 21:31:57 +00001281 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patelabfbe3b2006-12-16 00:56:26 +00001282 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1283 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001284 bool LocalChanged = false;
Devang Patelf6d1d212006-12-14 00:25:06 +00001285
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001286 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001287 dumpRequiredSet(BP);
Devang Patelf6d1d212006-12-14 00:25:06 +00001288
Devang Patelabfbe3b2006-12-16 00:56:26 +00001289 initializeAnalysisImpl(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001290
Chris Lattner4c1e9542009-03-06 06:45:05 +00001291 {
1292 // If the pass crashes, remember this.
1293 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattner707431c2010-03-30 04:03:22 +00001294 TimeRegion PassTimer(getPassTimer(BP));
1295
Dan Gohman74b189f2010-03-01 17:34:28 +00001296 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001297 }
Devang Patel93a197c2006-12-14 00:08:04 +00001298
Dan Gohman74b189f2010-03-01 17:34:28 +00001299 Changed |= LocalChanged;
Dan Gohmande6188a2010-08-12 23:50:08 +00001300 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001301 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001302 I->getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001303 dumpPreservedSet(BP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001304 dumpUsedSet(BP);
Devang Patel93a197c2006-12-14 00:08:04 +00001305
Devang Patela273d1c2007-07-19 18:02:32 +00001306 verifyPreservedAnalysis(BP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001307 removeNotPreservedAnalysis(BP);
1308 recordAvailableAnalysis(BP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001309 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel6e5a1132006-11-07 21:31:57 +00001310 }
Chris Lattnerde2aa652007-08-10 06:22:25 +00001311
Bill Wendling6ce6d262009-12-25 13:50:18 +00001312 return doFinalization(F) || Changed;
Devang Patel6e5a1132006-11-07 21:31:57 +00001313}
1314
Devang Patel475c4532006-12-08 00:59:05 +00001315// Implement doInitialization and doFinalization
Duncan Sands51495602009-02-13 09:42:34 +00001316bool BBPassManager::doInitialization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001317 bool Changed = false;
1318
Chris Lattner4c1e9542009-03-06 06:45:05 +00001319 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1320 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001321
1322 return Changed;
1323}
1324
Duncan Sands51495602009-02-13 09:42:34 +00001325bool BBPassManager::doFinalization(Module &M) {
Devang Patel475c4532006-12-08 00:59:05 +00001326 bool Changed = false;
1327
Pedro Artigas41b98842012-12-05 17:12:22 +00001328 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001329 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel475c4532006-12-08 00:59:05 +00001330
1331 return Changed;
1332}
1333
Duncan Sands51495602009-02-13 09:42:34 +00001334bool BBPassManager::doInitialization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001335 bool Changed = false;
1336
Devang Patelabfbe3b2006-12-16 00:56:26 +00001337 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1338 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001339 Changed |= BP->doInitialization(F);
1340 }
1341
1342 return Changed;
1343}
1344
Duncan Sands51495602009-02-13 09:42:34 +00001345bool BBPassManager::doFinalization(Function &F) {
Devang Patel475c4532006-12-08 00:59:05 +00001346 bool Changed = false;
1347
Devang Patelabfbe3b2006-12-16 00:56:26 +00001348 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1349 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel475c4532006-12-08 00:59:05 +00001350 Changed |= BP->doFinalization(F);
1351 }
1352
1353 return Changed;
1354}
1355
1356
Devang Patela1514cb2006-12-07 19:39:39 +00001357//===----------------------------------------------------------------------===//
Devang Patelb67904d2006-12-13 02:36:01 +00001358// FunctionPassManager implementation
Devang Patela1514cb2006-12-07 19:39:39 +00001359
Devang Patel4e12f862006-11-08 10:44:40 +00001360/// Create new Function pass manager
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001361FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick08966212011-08-29 17:07:00 +00001362 FPM = new FunctionPassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001363 // FPM is the top level manager.
1364 FPM->setTopLevelManager(FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001365
Dan Gohman565df952008-03-13 02:08:36 +00001366 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patel1036b652006-12-12 23:27:37 +00001367 FPM->setResolver(AR);
Devang Patel1f653682006-12-08 18:57:16 +00001368}
1369
Devang Patelb67904d2006-12-13 02:36:01 +00001370FunctionPassManager::~FunctionPassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001371 delete FPM;
1372}
1373
Dan Gohmande6188a2010-08-12 23:50:08 +00001374void FunctionPassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001375 FPM->add(P);
Devang Patel4e12f862006-11-08 10:44:40 +00001376}
1377
Devang Patel9f3083e2006-11-15 19:39:54 +00001378/// run - Execute all of the passes scheduled for execution. Keep
1379/// track of whether any of the passes modifies the function, and if
1380/// so, return true.
1381///
Devang Patelb67904d2006-12-13 02:36:01 +00001382bool FunctionPassManager::run(Function &F) {
Peter Collingbourne7f00d0a2016-11-09 17:49:19 +00001383 handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) {
1384 report_fatal_error("Error reading bitcode file: " + EIB.message());
1385 });
Devang Patel272908d2006-12-08 22:57:48 +00001386 return FPM->run(F);
Devang Patel9f3083e2006-11-15 19:39:54 +00001387}
1388
1389
Devang Patelff631ae2006-11-15 01:27:05 +00001390/// doInitialization - Run all of the initializers for the function passes.
1391///
Devang Patelb67904d2006-12-13 02:36:01 +00001392bool FunctionPassManager::doInitialization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001393 return FPM->doInitialization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001394}
1395
Dan Gohmane6656eb2007-07-30 14:51:13 +00001396/// doFinalization - Run all of the finalizers for the function passes.
Devang Patelff631ae2006-11-15 01:27:05 +00001397///
Devang Patelb67904d2006-12-13 02:36:01 +00001398bool FunctionPassManager::doFinalization() {
Jeffrey Yasskin091217b2010-01-27 20:34:15 +00001399 return FPM->doFinalization(*M);
Devang Patelff631ae2006-11-15 01:27:05 +00001400}
1401
Devang Patela1514cb2006-12-07 19:39:39 +00001402//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001403// FunctionPassManagerImpl implementation
1404//
Duncan Sands51495602009-02-13 09:42:34 +00001405bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001406 bool Changed = false;
1407
Dan Gohman05ebc8f2009-11-23 16:24:18 +00001408 dumpArguments();
1409 dumpPasses();
1410
Yaron Keren4849aa32015-06-05 17:48:47 +00001411 for (ImmutablePass *ImPass : getImmutablePasses())
1412 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001413
Chris Lattner4c1e9542009-03-06 06:45:05 +00001414 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1415 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001416
1417 return Changed;
1418}
1419
Duncan Sands51495602009-02-13 09:42:34 +00001420bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001421 bool Changed = false;
1422
Pedro Artigas41b98842012-12-05 17:12:22 +00001423 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001424 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001425
Yaron Keren4849aa32015-06-05 17:48:47 +00001426 for (ImmutablePass *ImPass : getImmutablePasses())
1427 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001428
Devang Patel67d6a5e2006-12-19 19:46:59 +00001429 return Changed;
1430}
1431
Devang Patelec9c58f2009-04-01 22:34:41 +00001432/// cleanup - After running all passes, clean up pass manager cache.
1433void FPPassManager::cleanup() {
1434 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1435 FunctionPass *FP = getContainedPass(Index);
1436 AnalysisResolver *AR = FP->getResolver();
1437 assert(AR && "Analysis Resolver is not set");
1438 AR->clearAnalysisImpls();
1439 }
1440}
1441
Torok Edwin24c78352009-06-29 18:49:09 +00001442void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1443 if (!wasRun)
1444 return;
1445 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1446 FPPassManager *FPPM = getContainedManager(Index);
1447 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1448 FPPM->getContainedPass(Index)->releaseMemory();
1449 }
1450 }
Torok Edwin896556e2009-06-29 21:05:10 +00001451 wasRun = false;
Torok Edwin24c78352009-06-29 18:49:09 +00001452}
1453
Devang Patel67d6a5e2006-12-19 19:46:59 +00001454// Execute all the passes managed by this top level manager.
1455// Return true if any function is modified by a pass.
1456bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel67d6a5e2006-12-19 19:46:59 +00001457 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001458 TimingInfo::createTheTimeInfo();
1459
Devang Patele3068402006-12-21 00:16:50 +00001460 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001461 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001462 Changed |= getContainedManager(Index)->runOnFunction(F);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001463 F.getContext().yield();
1464 }
Devang Patelec9c58f2009-04-01 22:34:41 +00001465
1466 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1467 getContainedManager(Index)->cleanup();
1468
Torok Edwin24c78352009-06-29 18:49:09 +00001469 wasRun = true;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001470 return Changed;
1471}
1472
1473//===----------------------------------------------------------------------===//
1474// FPPassManager implementation
Devang Patel0c2012f2006-11-07 21:49:50 +00001475
Devang Patel8c78a0b2007-05-03 01:11:54 +00001476char FPPassManager::ID = 0;
Devang Patele7599552007-01-12 18:52:44 +00001477/// Print passes managed by this manager
1478void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramercc863b22011-10-16 16:30:34 +00001479 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patele7599552007-01-12 18:52:44 +00001480 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1481 FunctionPass *FP = getContainedPass(Index);
Dan Gohmanf71c5212010-08-19 01:29:07 +00001482 FP->dumpPassStructure(Offset + 1);
Devang Patele7599552007-01-12 18:52:44 +00001483 dumpLastUses(FP, Offset+1);
1484 }
1485}
1486
1487
Dan Gohmande6188a2010-08-12 23:50:08 +00001488/// Execute all of the passes scheduled for execution by invoking
1489/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel0c2012f2006-11-07 21:49:50 +00001490/// the function, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001491bool FPPassManager::runOnFunction(Function &F) {
Chris Lattner60987362009-03-06 05:53:14 +00001492 if (F.isDeclaration())
1493 return false;
Devang Patel9f3083e2006-11-15 19:39:54 +00001494
1495 bool Changed = false;
Devang Patel745a6962006-12-12 23:15:28 +00001496
Devang Patelcbbf2912008-03-20 01:09:53 +00001497 // Collect inherited analysis from Module level pass manager.
1498 populateInheritedAnalysis(TPM->activeStack);
Devang Patel745a6962006-12-12 23:15:28 +00001499
Devang Patelabfbe3b2006-12-16 00:56:26 +00001500 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1501 FunctionPass *FP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001502 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001503
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001504 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001505 dumpRequiredSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001506
Devang Patelabfbe3b2006-12-16 00:56:26 +00001507 initializeAnalysisImpl(FP);
Eric Christopher3c0d5162012-03-23 03:54:05 +00001508
Chris Lattner4c1e9542009-03-06 06:45:05 +00001509 {
1510 PassManagerPrettyStackEntry X(FP, F);
Chris Lattner707431c2010-03-30 04:03:22 +00001511 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattner4c1e9542009-03-06 06:45:05 +00001512
Dan Gohman74b189f2010-03-01 17:34:28 +00001513 LocalChanged |= FP->runOnFunction(F);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001514 }
Devang Patel93a197c2006-12-14 00:08:04 +00001515
Dan Gohman74b189f2010-03-01 17:34:28 +00001516 Changed |= LocalChanged;
1517 if (LocalChanged)
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001518 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner4c493d92008-08-08 15:14:09 +00001519 dumpPreservedSet(FP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001520 dumpUsedSet(FP);
Devang Patel93a197c2006-12-14 00:08:04 +00001521
Devang Patela273d1c2007-07-19 18:02:32 +00001522 verifyPreservedAnalysis(FP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001523 removeNotPreservedAnalysis(FP);
1524 recordAvailableAnalysis(FP);
Daniel Dunbar9813b0b2009-07-26 07:49:05 +00001525 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel9f3083e2006-11-15 19:39:54 +00001526 }
1527 return Changed;
1528}
1529
Devang Patel67d6a5e2006-12-19 19:46:59 +00001530bool FPPassManager::runOnModule(Module &M) {
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001531 bool Changed = false;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001532
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001533 for (Function &F : M)
1534 Changed |= runOnFunction(F);
Devang Patel67d6a5e2006-12-19 19:46:59 +00001535
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001536 return Changed;
Devang Patel67d6a5e2006-12-19 19:46:59 +00001537}
1538
Duncan Sands51495602009-02-13 09:42:34 +00001539bool FPPassManager::doInitialization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001540 bool Changed = false;
1541
Chris Lattner4c1e9542009-03-06 06:45:05 +00001542 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1543 Changed |= getContainedPass(Index)->doInitialization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001544
Devang Patelff631ae2006-11-15 01:27:05 +00001545 return Changed;
1546}
1547
Duncan Sands51495602009-02-13 09:42:34 +00001548bool FPPassManager::doFinalization(Module &M) {
Devang Patelff631ae2006-11-15 01:27:05 +00001549 bool Changed = false;
Pedro Artigas41b98842012-12-05 17:12:22 +00001550
1551 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattner4c1e9542009-03-06 06:45:05 +00001552 Changed |= getContainedPass(Index)->doFinalization(M);
Andrew Trickdc073ad2013-09-18 23:31:10 +00001553
Devang Patelff631ae2006-11-15 01:27:05 +00001554 return Changed;
1555}
1556
Devang Patela1514cb2006-12-07 19:39:39 +00001557//===----------------------------------------------------------------------===//
Devang Patel67d6a5e2006-12-19 19:46:59 +00001558// MPPassManager implementation
Devang Patel05e1a972006-11-07 22:03:15 +00001559
Dan Gohmande6188a2010-08-12 23:50:08 +00001560/// Execute all of the passes scheduled for execution by invoking
1561/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel05e1a972006-11-07 22:03:15 +00001562/// the module, and if so, return true.
1563bool
Devang Patel67d6a5e2006-12-19 19:46:59 +00001564MPPassManager::runOnModule(Module &M) {
Devang Patel05e1a972006-11-07 22:03:15 +00001565 bool Changed = false;
Devang Patel050ec722006-11-14 01:23:29 +00001566
Torok Edwin24c78352009-06-29 18:49:09 +00001567 // Initialize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001568 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1569 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001570 Changed |= FPP->doInitialization(M);
1571 }
1572
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001573 // Initialize module passes
1574 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1575 Changed |= getContainedPass(Index)->doInitialization(M);
1576
Devang Patelabfbe3b2006-12-16 00:56:26 +00001577 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1578 ModulePass *MP = getContainedPass(Index);
Dan Gohman74b189f2010-03-01 17:34:28 +00001579 bool LocalChanged = false;
Devang Patelabfbe3b2006-12-16 00:56:26 +00001580
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001581 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001582 dumpRequiredSet(MP);
Devang Patel93a197c2006-12-14 00:08:04 +00001583
Devang Patelabfbe3b2006-12-16 00:56:26 +00001584 initializeAnalysisImpl(MP);
Devang Patelb8817b92006-12-14 00:59:42 +00001585
Chris Lattner4c1e9542009-03-06 06:45:05 +00001586 {
1587 PassManagerPrettyStackEntry X(MP, M);
Chris Lattner707431c2010-03-30 04:03:22 +00001588 TimeRegion PassTimer(getPassTimer(MP));
1589
Dan Gohman74b189f2010-03-01 17:34:28 +00001590 LocalChanged |= MP->runOnModule(M);
Chris Lattner4c1e9542009-03-06 06:45:05 +00001591 }
Devang Patel93a197c2006-12-14 00:08:04 +00001592
Dan Gohman74b189f2010-03-01 17:34:28 +00001593 Changed |= LocalChanged;
1594 if (LocalChanged)
Dan Gohman929391a2008-01-29 12:09:55 +00001595 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001596 M.getModuleIdentifier());
Chris Lattner4c493d92008-08-08 15:14:09 +00001597 dumpPreservedSet(MP);
Chandler Carruth44a13852015-08-19 03:02:12 +00001598 dumpUsedSet(MP);
Dan Gohmande6188a2010-08-12 23:50:08 +00001599
Devang Patela273d1c2007-07-19 18:02:32 +00001600 verifyPreservedAnalysis(MP);
Devang Patelabfbe3b2006-12-16 00:56:26 +00001601 removeNotPreservedAnalysis(MP);
1602 recordAvailableAnalysis(MP);
Benjamin Kramerdfcc2852009-12-08 13:07:38 +00001603 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel05e1a972006-11-07 22:03:15 +00001604 }
Torok Edwin24c78352009-06-29 18:49:09 +00001605
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001606 // Finalize module passes
Pedro Artigas41b98842012-12-05 17:12:22 +00001607 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigasd6b092b2012-11-29 17:47:05 +00001608 Changed |= getContainedPass(Index)->doFinalization(M);
1609
Torok Edwin24c78352009-06-29 18:49:09 +00001610 // Finalize on-the-fly passes
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001611 for (auto &OnTheFlyManager : OnTheFlyManagers) {
1612 FunctionPassManagerImpl *FPP = OnTheFlyManager.second;
Torok Edwin24c78352009-06-29 18:49:09 +00001613 // We don't know when is the last time an on-the-fly pass is run,
1614 // so we need to releaseMemory / finalize here
1615 FPP->releaseMemoryOnTheFly();
1616 Changed |= FPP->doFinalization(M);
1617 }
Andrew Trickdc073ad2013-09-18 23:31:10 +00001618
Devang Patel05e1a972006-11-07 22:03:15 +00001619 return Changed;
1620}
1621
Devang Patele64d3052007-04-16 20:12:57 +00001622/// Add RequiredPass into list of lower level passes required by pass P.
1623/// RequiredPass is run on the fly by Pass Manager when P requests it
1624/// through getAnalysis interface.
1625void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattner60987362009-03-06 05:53:14 +00001626 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1627 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001628 assert((P->getPotentialPassManagerType() <
Chris Lattner60987362009-03-06 05:53:14 +00001629 RequiredPass->getPotentialPassManagerType()) &&
1630 "Unable to handle Pass that requires lower level Analysis pass");
Andrew Trick02066f22014-04-08 03:40:34 +00001631 if (!RequiredPass)
1632 return;
Devang Patele64d3052007-04-16 20:12:57 +00001633
Devang Patel68f72b12007-04-26 17:50:19 +00001634 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel69e9f6d2007-04-16 20:27:05 +00001635 if (!FPP) {
Andrew Trick08966212011-08-29 17:07:00 +00001636 FPP = new FunctionPassManagerImpl();
Devang Patel68f72b12007-04-26 17:50:19 +00001637 // FPP is the top level manager.
1638 FPP->setTopLevelManager(FPP);
1639
Devang Patel69e9f6d2007-04-16 20:27:05 +00001640 OnTheFlyManagers[P] = FPP;
1641 }
Chandler Carruth5b0d3e32015-01-28 09:47:21 +00001642 const PassInfo *RequiredPassPI =
1643 TPM->findAnalysisPassInfo(RequiredPass->getPassID());
Devang Patel69e9f6d2007-04-16 20:27:05 +00001644
Craig Topperc6207612014-04-09 06:08:46 +00001645 Pass *FoundPass = nullptr;
Andrew Trick02066f22014-04-08 03:40:34 +00001646 if (RequiredPassPI && RequiredPassPI->isAnalysis()) {
1647 FoundPass =
1648 ((PMTopLevelManager*)FPP)->findAnalysisPass(RequiredPass->getPassID());
Devang Patel6eb3a6b2011-09-13 21:13:29 +00001649 }
Andrew Trick02066f22014-04-08 03:40:34 +00001650 if (!FoundPass) {
1651 FoundPass = RequiredPass;
1652 // This should be guaranteed to add RequiredPass to the passmanager given
Sylvestre Ledru469de192014-08-11 18:04:46 +00001653 // that we checked for an available analysis above.
Andrew Trick02066f22014-04-08 03:40:34 +00001654 FPP->add(RequiredPass);
1655 }
1656 // Register P as the last user of FoundPass or RequiredPass.
1657 SmallVector<Pass *, 1> LU;
1658 LU.push_back(FoundPass);
1659 FPP->setLastUser(LU, P);
Devang Patele64d3052007-04-16 20:12:57 +00001660}
Devang Patel69e9f6d2007-04-16 20:27:05 +00001661
Dan Gohmande6188a2010-08-12 23:50:08 +00001662/// Return function pass corresponding to PassInfo PI, that is
Devang Patel69e9f6d2007-04-16 20:27:05 +00001663/// required by module pass MP. Instantiate analysis pass, by using
1664/// its runOnFunction() for function F.
Owen Andersona7aed182010-08-06 18:33:48 +00001665Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Patel68f72b12007-04-26 17:50:19 +00001666 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattner60987362009-03-06 05:53:14 +00001667 assert(FPP && "Unable to find on the fly pass");
Dan Gohmande6188a2010-08-12 23:50:08 +00001668
Torok Edwin24c78352009-06-29 18:49:09 +00001669 FPP->releaseMemoryOnTheFly();
Devang Patel68f72b12007-04-26 17:50:19 +00001670 FPP->run(F);
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001671 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel69e9f6d2007-04-16 20:27:05 +00001672}
1673
1674
Devang Patela1514cb2006-12-07 19:39:39 +00001675//===----------------------------------------------------------------------===//
1676// PassManagerImpl implementation
Owen Anderson1aa27512012-11-15 00:14:15 +00001677
Devang Patelab97cf42006-12-13 00:09:23 +00001678//
Devang Patelc290c8a2006-11-07 22:23:34 +00001679/// run - Execute all of the passes scheduled for execution. Keep track of
1680/// whether any of the passes modifies the module, and if so, return true.
Devang Patel67d6a5e2006-12-19 19:46:59 +00001681bool PassManagerImpl::run(Module &M) {
Devang Patelc290c8a2006-11-07 22:23:34 +00001682 bool Changed = false;
Devang Patelb8817b92006-12-14 00:59:42 +00001683 TimingInfo::createTheTimeInfo();
1684
Devang Patelcfd70c42006-12-13 22:10:00 +00001685 dumpArguments();
Devang Patel67d6a5e2006-12-19 19:46:59 +00001686 dumpPasses();
Devang Patelf1567a52006-12-13 20:03:48 +00001687
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001688 for (ImmutablePass *ImPass : getImmutablePasses())
1689 Changed |= ImPass->doInitialization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001690
Devang Patele3068402006-12-21 00:16:50 +00001691 initializeAllAnalysisInfo();
Juergen Ributzka34390c72014-05-16 02:33:15 +00001692 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
Chris Lattner4c1e9542009-03-06 06:45:05 +00001693 Changed |= getContainedManager(Index)->runOnModule(M);
Juergen Ributzka34390c72014-05-16 02:33:15 +00001694 M.getContext().yield();
1695 }
Pedro Artigas41b98842012-12-05 17:12:22 +00001696
Yaron Keren3b1e24b2015-06-05 14:15:07 +00001697 for (ImmutablePass *ImPass : getImmutablePasses())
1698 Changed |= ImPass->doFinalization(M);
Pedro Artigas41b98842012-12-05 17:12:22 +00001699
Devang Patelc290c8a2006-11-07 22:23:34 +00001700 return Changed;
1701}
Devang Patel376fefa2006-11-08 10:29:57 +00001702
Devang Patela1514cb2006-12-07 19:39:39 +00001703//===----------------------------------------------------------------------===//
1704// PassManager implementation
1705
Devang Patel376fefa2006-11-08 10:29:57 +00001706/// Create new pass manager
Devang Patelb67904d2006-12-13 02:36:01 +00001707PassManager::PassManager() {
Andrew Trick08966212011-08-29 17:07:00 +00001708 PM = new PassManagerImpl();
Devang Patel9c6290c2006-12-12 22:02:16 +00001709 // PM is the top level manager
1710 PM->setTopLevelManager(PM);
Devang Patel376fefa2006-11-08 10:29:57 +00001711}
1712
Devang Patelb67904d2006-12-13 02:36:01 +00001713PassManager::~PassManager() {
Devang Patelab97cf42006-12-13 00:09:23 +00001714 delete PM;
1715}
1716
Chris Lattner60987362009-03-06 05:53:14 +00001717void PassManager::add(Pass *P) {
Andrew Trickcbc845f2012-02-01 07:16:20 +00001718 PM->add(P);
Devang Patel376fefa2006-11-08 10:29:57 +00001719}
1720
1721/// run - Execute all of the passes scheduled for execution. Keep track of
1722/// whether any of the passes modifies the module, and if so, return true.
Chris Lattner60987362009-03-06 05:53:14 +00001723bool PassManager::run(Module &M) {
Devang Patel376fefa2006-11-08 10:29:57 +00001724 return PM->run(M);
1725}
1726
Devang Patelb8817b92006-12-14 00:59:42 +00001727//===----------------------------------------------------------------------===//
Eli Benderskyb35a2112013-04-03 15:33:45 +00001728// TimingInfo implementation
1729
Andrew Trickb5e1e6c2013-09-19 06:02:43 +00001730bool llvm::TimePassesIsEnabled = false;
1731static cl::opt<bool,true>
1732EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1733 cl::desc("Time each pass, printing elapsed time for each on exit"));
1734
Devang Patelb8817b92006-12-14 00:59:42 +00001735// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
Alp Tokerf907b892013-12-05 05:44:44 +00001736// a non-null value (if the -time-passes option is enabled) or it leaves it
Devang Patelb8817b92006-12-14 00:59:42 +00001737// null. It may be called multiple times.
1738void TimingInfo::createTheTimeInfo() {
1739 if (!TimePassesIsEnabled || TheTimeInfo) return;
1740
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001741 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patelb8817b92006-12-14 00:59:42 +00001742 // This guarantees that the object will be constructed before static globals,
1743 // thus it will be destroyed before them.
1744 static ManagedStatic<TimingInfo> TTI;
1745 TheTimeInfo = &*TTI;
1746}
1747
Devang Patel1c3633e2007-01-29 23:10:37 +00001748/// If TimingInfo is enabled then start pass timer.
Chris Lattner707431c2010-03-30 04:03:22 +00001749Timer *llvm::getPassTimer(Pass *P) {
Dan Gohmande6188a2010-08-12 23:50:08 +00001750 if (TheTimeInfo)
Chris Lattner707431c2010-03-30 04:03:22 +00001751 return TheTimeInfo->getPassTimer(P);
Craig Topperc6207612014-04-09 06:08:46 +00001752 return nullptr;
Devang Patel1c3633e2007-01-29 23:10:37 +00001753}
1754
Devang Patel1c56a632007-01-08 19:29:38 +00001755//===----------------------------------------------------------------------===//
1756// PMStack implementation
1757//
Devang Patelad98d232007-01-11 22:15:30 +00001758
Devang Patel1c56a632007-01-08 19:29:38 +00001759// Pop Pass Manager from the stack and clear its analysis info.
1760void PMStack::pop() {
1761
1762 PMDataManager *Top = this->top();
1763 Top->initializeAnalysisInfo();
1764
1765 S.pop_back();
1766}
1767
1768// Push PM on the stack and set its top level manager.
Dan Gohman11eecd62008-03-13 01:21:31 +00001769void PMStack::push(PMDataManager *PM) {
Chris Lattner60987362009-03-06 05:53:14 +00001770 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick08966212011-08-29 17:07:00 +00001771 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel1c56a632007-01-08 19:29:38 +00001772
Chris Lattner60987362009-03-06 05:53:14 +00001773 if (!this->empty()) {
Andrew Trick08966212011-08-29 17:07:00 +00001774 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1775 && "pushing bad pass manager to PMStack");
Chris Lattner60987362009-03-06 05:53:14 +00001776 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel1c56a632007-01-08 19:29:38 +00001777
Chris Lattner60987362009-03-06 05:53:14 +00001778 assert(TPM && "Unable to find top level manager");
Devang Patel15701b52007-01-11 00:19:00 +00001779 TPM->addIndirectPassManager(PM);
1780 PM->setTopLevelManager(TPM);
Andrew Trick08966212011-08-29 17:07:00 +00001781 PM->setDepth(this->top()->getDepth()+1);
Craig Topper821d6af2013-02-06 06:50:38 +00001782 } else {
Benjamin Kramer6bb5b3c2011-08-29 18:14:15 +00001783 assert((PM->getPassManagerType() == PMT_ModulePassManager
1784 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick08966212011-08-29 17:07:00 +00001785 && "pushing bad pass manager to PMStack");
1786 PM->setDepth(1);
Devang Patel15701b52007-01-11 00:19:00 +00001787 }
1788
Devang Patel15701b52007-01-11 00:19:00 +00001789 S.push_back(PM);
1790}
1791
1792// Dump content of the pass manager stack.
Yaron Kereneb2a2542016-01-29 20:50:44 +00001793LLVM_DUMP_METHOD void PMStack::dump() const {
Yaron Keren4849aa32015-06-05 17:48:47 +00001794 for (PMDataManager *Manager : S)
1795 dbgs() << Manager->getAsPass()->getPassName() << ' ';
Chris Lattner60987362009-03-06 05:53:14 +00001796
Devang Patel15701b52007-01-11 00:19:00 +00001797 if (!S.empty())
Benjamin Kramer4dd515c2011-08-29 18:14:17 +00001798 dbgs() << '\n';
Devang Patel1c56a632007-01-08 19:29:38 +00001799}
1800
Devang Patel1c56a632007-01-08 19:29:38 +00001801/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohmande6188a2010-08-12 23:50:08 +00001802/// add self into that manager.
1803void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001804 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001805 // Find Module Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001806 while (!PMS.empty()) {
Devang Patel23f8aa92007-01-17 21:19:23 +00001807 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1808 if (TopPMType == PreferredType)
1809 break; // We found desired pass manager
1810 else if (TopPMType > PMT_ModulePassManager)
Devang Patel1c56a632007-01-08 19:29:38 +00001811 PMS.pop(); // Pop children pass managers
Devang Patelac99eca2007-01-11 19:59:06 +00001812 else
1813 break;
Devang Patel1c56a632007-01-08 19:29:38 +00001814 }
Devang Patel18ff6362008-09-09 21:38:40 +00001815 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel23f8aa92007-01-17 21:19:23 +00001816 PMS.top()->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001817}
1818
Devang Patel3312f752007-01-16 21:43:18 +00001819/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001820/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001821void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001822 PassManagerType PreferredType) {
Devang Patel1c56a632007-01-08 19:29:38 +00001823
Andrew Trickcbc845f2012-02-01 07:16:20 +00001824 // Find Function Pass Manager
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001825 while (!PMS.empty()) {
Devang Patelac99eca2007-01-11 19:59:06 +00001826 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1827 PMS.pop();
Devang Patel1c56a632007-01-08 19:29:38 +00001828 else
Dan Gohmande6188a2010-08-12 23:50:08 +00001829 break;
Devang Patel3312f752007-01-16 21:43:18 +00001830 }
Devang Patel3312f752007-01-16 21:43:18 +00001831
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001832 // Create new Function Pass Manager if needed.
1833 FPPassManager *FPP;
1834 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1835 FPP = (FPPassManager *)PMS.top();
1836 } else {
Devang Patel3312f752007-01-16 21:43:18 +00001837 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1838 PMDataManager *PMD = PMS.top();
1839
1840 // [1] Create new Function Pass Manager
Andrew Trick08966212011-08-29 17:07:00 +00001841 FPP = new FPPassManager();
Devang Patelcbbf2912008-03-20 01:09:53 +00001842 FPP->populateInheritedAnalysis(PMS);
Devang Patel3312f752007-01-16 21:43:18 +00001843
1844 // [2] Set up new manager's top level manager
1845 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1846 TPM->addIndirectPassManager(FPP);
1847
1848 // [3] Assign manager to manage this new manager. This may create
1849 // and push new managers into PMS
Devang Patela3286902008-09-09 17:56:50 +00001850 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel3312f752007-01-16 21:43:18 +00001851
1852 // [4] Push new manager into PMS
1853 PMS.push(FPP);
Devang Patel1c56a632007-01-08 19:29:38 +00001854 }
1855
Devang Patel3312f752007-01-16 21:43:18 +00001856 // Assign FPP as the manager of this pass.
1857 FPP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001858}
1859
Devang Patel3312f752007-01-16 21:43:18 +00001860/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohmande6188a2010-08-12 23:50:08 +00001861/// in the PM Stack and add self into that manager.
Devang Pateldffca632007-01-17 20:30:17 +00001862void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovfb801512007-04-16 18:10:23 +00001863 PassManagerType PreferredType) {
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001864 BBPassManager *BBP;
Devang Patel1c56a632007-01-08 19:29:38 +00001865
Devang Patel15701b52007-01-11 00:19:00 +00001866 // Basic Pass Manager is a leaf pass manager. It does not handle
1867 // any other pass manager.
Dan Gohmande6188a2010-08-12 23:50:08 +00001868 if (!PMS.empty() &&
Chris Lattner9efd4fc2010-01-22 05:37:10 +00001869 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1870 BBP = (BBPassManager *)PMS.top();
1871 } else {
1872 // If leaf manager is not Basic Block Pass manager then create new
1873 // basic Block Pass manager.
Devang Patel3312f752007-01-16 21:43:18 +00001874 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1875 PMDataManager *PMD = PMS.top();
1876
1877 // [1] Create new Basic Block Manager
Andrew Trick08966212011-08-29 17:07:00 +00001878 BBP = new BBPassManager();
Devang Patel3312f752007-01-16 21:43:18 +00001879
1880 // [2] Set up new manager's top level manager
1881 // Basic Block Pass Manager does not live by itself
1882 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1883 TPM->addIndirectPassManager(BBP);
1884
Devang Patel15701b52007-01-11 00:19:00 +00001885 // [3] Assign manager to manage this new manager. This may create
1886 // and push new managers into PMS
David Greene103d4b42010-05-10 20:24:27 +00001887 BBP->assignPassManager(PMS, PreferredType);
Devang Patel15701b52007-01-11 00:19:00 +00001888
Devang Patel3312f752007-01-16 21:43:18 +00001889 // [4] Push new manager into PMS
1890 PMS.push(BBP);
1891 }
Devang Patel1c56a632007-01-08 19:29:38 +00001892
Devang Patel3312f752007-01-16 21:43:18 +00001893 // Assign BBP as the manager of this pass.
1894 BBP->add(this);
Devang Patel1c56a632007-01-08 19:29:38 +00001895}
1896
Dan Gohmand3a20c92008-03-11 16:41:42 +00001897PassManagerBase::~PassManagerBase() {}