blob: 3c968aac164f58a09a83cbba7f56b5288e8102ec [file] [log] [blame]
Devang Patel55fd43f2006-11-07 21:31:57 +00001//===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Patel55fd43f2006-11-07 21:31:57 +00007//
8//===----------------------------------------------------------------------===//
9//
Dan Gohman95df6192010-08-12 23:50:08 +000010// This file implements the LLVM Pass Manager infrastructure.
Devang Patel55fd43f2006-11-07 21:31:57 +000011//
12//===----------------------------------------------------------------------===//
13
14
Devang Patelab7752c2007-01-12 18:52:44 +000015#include "llvm/PassManagers.h"
David Greene5c8aa952010-04-02 23:17:14 +000016#include "llvm/Assembly/PrintModulePass.h"
Dan Gohman9450b0e2009-09-28 00:27:48 +000017#include "llvm/Assembly/Writer.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000019#include "llvm/PassManager.h"
Devang Patel45dc02d2006-12-13 20:03:48 +000020#include "llvm/Support/CommandLine.h"
David Greene170c48a2010-01-05 01:30:02 +000021#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000022#include "llvm/Support/ErrorHandling.h"
Devang Patel8e58a1b2006-12-14 00:59:42 +000023#include "llvm/Support/ManagedStatic.h"
Michael J. Spencer1f6efa32010-11-29 18:16:10 +000024#include "llvm/Support/Mutex.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/Support/PassNameParser.h"
26#include "llvm/Support/Timer.h"
27#include "llvm/Support/raw_ostream.h"
Jeff Cohenca5183d2007-03-05 00:00:42 +000028#include <algorithm>
Devang Patelb899eed2006-11-14 01:59:59 +000029#include <map>
Dan Gohman2bb7d062007-10-03 19:04:09 +000030using namespace llvm;
Devang Patelc2ff9622006-12-15 19:39:30 +000031
Devang Patelab7752c2007-01-12 18:52:44 +000032// See PassManagers.h for Pass Manager infrastructure overview.
Devang Patele77242c2006-12-07 18:23:30 +000033
Devang Patel45dc02d2006-12-13 20:03:48 +000034namespace llvm {
35
36//===----------------------------------------------------------------------===//
37// Pass debugging information. Often it is useful to find out what pass is
38// running when a crash occurs in a utility. When this library is compiled with
39// debugging on, a command line option (--debug-pass) is enabled that causes the
40// pass name to be printed before it executes.
41//
42
Devang Patele8ff1ce2006-12-13 21:13:31 +000043// Different debug levels that can be enabled...
44enum PassDebugLevel {
45 None, Arguments, Structure, Executions, Details
46};
47
Devang Patel45dc02d2006-12-13 20:03:48 +000048static cl::opt<enum PassDebugLevel>
Devang Patel26426942007-01-17 20:33:36 +000049PassDebugging("debug-pass", cl::Hidden,
Devang Patel45dc02d2006-12-13 20:03:48 +000050 cl::desc("Print PassManager debugging information"),
51 cl::values(
Devang Patele8ff1ce2006-12-13 21:13:31 +000052 clEnumVal(None , "disable debug output"),
53 clEnumVal(Arguments , "print pass arguments to pass to 'opt'"),
54 clEnumVal(Structure , "print pass structure before run()"),
55 clEnumVal(Executions, "print pass name before it is executed"),
56 clEnumVal(Details , "print pass details when it is executed"),
Devang Patel45dc02d2006-12-13 20:03:48 +000057 clEnumValEnd));
David Greene5c8aa952010-04-02 23:17:14 +000058
59typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser>
60PassOptionList;
61
62// Print IR out before/after specified passes.
63static PassOptionList
64PrintBefore("print-before",
Eric Christopherf607abd2011-03-09 19:46:51 +000065 llvm::cl::desc("Print IR before specified passes"),
66 cl::Hidden);
David Greene5c8aa952010-04-02 23:17:14 +000067
68static PassOptionList
69PrintAfter("print-after",
Eric Christopherf607abd2011-03-09 19:46:51 +000070 llvm::cl::desc("Print IR after specified passes"),
71 cl::Hidden);
David Greene5c8aa952010-04-02 23:17:14 +000072
73static cl::opt<bool>
74PrintBeforeAll("print-before-all",
75 llvm::cl::desc("Print IR before each pass"),
76 cl::init(false));
77static cl::opt<bool>
78PrintAfterAll("print-after-all",
79 llvm::cl::desc("Print IR after each pass"),
80 cl::init(false));
81
82/// This is a helper to determine whether to print IR before or
83/// after a pass.
84
Andrew Trick11e43292012-02-01 07:16:20 +000085static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI,
David Greene5c8aa952010-04-02 23:17:14 +000086 PassOptionList &PassesToPrint) {
Andrew Trick11e43292012-02-01 07:16:20 +000087 for (unsigned i = 0, ie = PassesToPrint.size(); i < ie; ++i) {
88 const llvm::PassInfo *PassInf = PassesToPrint[i];
89 if (PassInf)
90 if (PassInf->getPassArgument() == PI->getPassArgument()) {
91 return true;
92 }
David Greene5c8aa952010-04-02 23:17:14 +000093 }
94 return false;
95}
Dan Gohman95df6192010-08-12 23:50:08 +000096
David Greene5c8aa952010-04-02 23:17:14 +000097/// This is a utility to check whether a pass should have IR dumped
98/// before it.
Andrew Trick11e43292012-02-01 07:16:20 +000099static bool ShouldPrintBeforePass(const PassInfo *PI) {
100 return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore);
David Greene5c8aa952010-04-02 23:17:14 +0000101}
102
103/// This is a utility to check whether a pass should have IR dumped
104/// after it.
Andrew Trick11e43292012-02-01 07:16:20 +0000105static bool ShouldPrintAfterPass(const PassInfo *PI) {
106 return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter);
David Greene5c8aa952010-04-02 23:17:14 +0000107}
108
Devang Patel45dc02d2006-12-13 20:03:48 +0000109} // End of llvm namespace
110
Chris Lattner9554c612009-09-15 05:03:04 +0000111/// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions
112/// or higher is specified.
113bool PMDataManager::isPassDebuggingExecutionsOrMore() const {
114 return PassDebugging >= Executions;
115}
116
117
118
119
Chris Lattnerd6f16582009-03-06 06:45:05 +0000120void PassManagerPrettyStackEntry::print(raw_ostream &OS) const {
121 if (V == 0 && M == 0)
122 OS << "Releasing pass '";
123 else
124 OS << "Running pass '";
Dan Gohman95df6192010-08-12 23:50:08 +0000125
Chris Lattnerd6f16582009-03-06 06:45:05 +0000126 OS << P->getPassName() << "'";
Dan Gohman95df6192010-08-12 23:50:08 +0000127
Chris Lattnerd6f16582009-03-06 06:45:05 +0000128 if (M) {
129 OS << " on module '" << M->getModuleIdentifier() << "'.\n";
130 return;
131 }
132 if (V == 0) {
133 OS << '\n';
134 return;
135 }
136
Dan Gohmanac57b122009-03-10 18:47:59 +0000137 OS << " on ";
Chris Lattnerd6f16582009-03-06 06:45:05 +0000138 if (isa<Function>(V))
Dan Gohmanac57b122009-03-10 18:47:59 +0000139 OS << "function";
Chris Lattnerd6f16582009-03-06 06:45:05 +0000140 else if (isa<BasicBlock>(V))
Dan Gohmanac57b122009-03-10 18:47:59 +0000141 OS << "basic block";
Chris Lattnerd6f16582009-03-06 06:45:05 +0000142 else
Dan Gohmanac57b122009-03-10 18:47:59 +0000143 OS << "value";
144
145 OS << " '";
146 WriteAsOperand(OS, V, /*PrintTy=*/false, M);
147 OS << "'\n";
Chris Lattnerd6f16582009-03-06 06:45:05 +0000148}
149
150
Devang Patelc2ff9622006-12-15 19:39:30 +0000151namespace {
Devang Patel1b8d0152006-12-12 22:35:25 +0000152
Devang Patel3f5d2b52006-12-07 19:21:29 +0000153//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000154// BBPassManager
Devang Patel7e601a72006-12-12 22:47:13 +0000155//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000156/// BBPassManager manages BasicBlockPass. It batches all the
Devang Patelc67c9382006-11-08 10:05:38 +0000157/// pass together and sequence them to process one basic block before
158/// processing next basic block.
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000159class BBPassManager : public PMDataManager, public FunctionPass {
Devang Patelc67c9382006-11-08 10:05:38 +0000160
161public:
Devang Patel19974732007-05-03 01:11:54 +0000162 static char ID;
Andrew Trick0e122d12011-08-29 17:07:00 +0000163 explicit BBPassManager()
164 : PMDataManager(), FunctionPass(ID) {}
Devang Patelc67c9382006-11-08 10:05:38 +0000165
Devang Patelc67c9382006-11-08 10:05:38 +0000166 /// Execute all of the passes scheduled for execution. Keep track of
167 /// whether any of the passes modifies the function, and if so, return true.
168 bool runOnFunction(Function &F);
169
Devang Patel66d72e12006-12-07 19:57:52 +0000170 /// Pass Manager itself does not invalidate any analysis info.
171 void getAnalysisUsage(AnalysisUsage &Info) const {
172 Info.setPreservesAll();
173 }
174
Devang Patel964e45e2006-12-08 00:59:05 +0000175 bool doInitialization(Module &M);
176 bool doInitialization(Function &F);
177 bool doFinalization(Module &M);
178 bool doFinalization(Function &F);
179
Chris Lattner3660eca2010-01-22 05:24:46 +0000180 virtual PMDataManager *getAsPMDataManager() { return this; }
181 virtual Pass *getAsPass() { return this; }
182
Devang Patele27ae7e2007-02-01 22:08:25 +0000183 virtual const char *getPassName() const {
Dan Gohman9769cee2008-03-13 01:58:48 +0000184 return "BasicBlock Pass Manager";
Devang Patele27ae7e2007-02-01 22:08:25 +0000185 }
186
Devang Patelebc09222006-12-12 23:34:33 +0000187 // Print passes managed by this manager
188 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer3dedf7e2011-08-29 18:14:17 +0000189 llvm::dbgs().indent(Offset*2) << "BasicBlockPass Manager\n";
Devang Patel1554c852006-12-16 00:56:26 +0000190 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
191 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman8a757ae2010-08-19 01:29:07 +0000192 BP->dumpPassStructure(Offset + 1);
Devang Patel1554c852006-12-16 00:56:26 +0000193 dumpLastUses(BP, Offset+1);
Devang Patelebc09222006-12-12 23:34:33 +0000194 }
195 }
Devang Patel1554c852006-12-16 00:56:26 +0000196
197 BasicBlockPass *getContainedPass(unsigned N) {
Evan Cheng310fa652012-11-13 02:56:38 +0000198 assert(N < PassVector.size() && "Pass number out of range!");
Devang Patel1554c852006-12-16 00:56:26 +0000199 BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]);
200 return BP;
201 }
Devang Patel25919cb2007-01-11 01:10:25 +0000202
Dan Gohman95df6192010-08-12 23:50:08 +0000203 virtual PassManagerType getPassManagerType() const {
204 return PMT_BasicBlockPassManager;
Devang Patel25919cb2007-01-11 01:10:25 +0000205 }
Devang Patelc67c9382006-11-08 10:05:38 +0000206};
207
Devang Patel19974732007-05-03 01:11:54 +0000208char BBPassManager::ID = 0;
Devang Patelab7752c2007-01-12 18:52:44 +0000209}
Devang Patel5f4ddf52006-12-19 19:46:59 +0000210
Devang Patelab7752c2007-01-12 18:52:44 +0000211namespace llvm {
Devang Patelc67c9382006-11-08 10:05:38 +0000212
Devang Patel7e601a72006-12-12 22:47:13 +0000213//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000214// FunctionPassManagerImpl
Devang Patel7e601a72006-12-12 22:47:13 +0000215//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000216/// FunctionPassManagerImpl manages FPPassManagers
217class FunctionPassManagerImpl : public Pass,
Devang Patel36bcb822007-01-11 22:15:30 +0000218 public PMDataManager,
219 public PMTopLevelManager {
David Blaikie2d24e2a2011-12-20 02:50:00 +0000220 virtual void anchor();
Torok Edwin1970a892009-06-29 18:49:09 +0000221private:
222 bool wasRun;
Devang Patel5f4ddf52006-12-19 19:46:59 +0000223public:
Devang Patel19974732007-05-03 01:11:54 +0000224 static char ID;
Andrew Trick0e122d12011-08-29 17:07:00 +0000225 explicit FunctionPassManagerImpl() :
226 Pass(PT_PassManager, ID), PMDataManager(),
227 PMTopLevelManager(new FPPassManager()), wasRun(false) {}
Devang Patel5f4ddf52006-12-19 19:46:59 +0000228
229 /// add - Add a pass to the queue of passes to run. This passes ownership of
230 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
231 /// will be destroyed as well, so there is no need to delete the pass. This
232 /// implies that all passes MUST be allocated with 'new'.
233 void add(Pass *P) {
234 schedulePass(P);
235 }
Dan Gohman95df6192010-08-12 23:50:08 +0000236
237 /// createPrinterPass - Get a function printer pass.
David Greene5c8aa952010-04-02 23:17:14 +0000238 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
239 return createPrintFunctionPass(Banner, &O);
240 }
241
Torok Edwin1970a892009-06-29 18:49:09 +0000242 // Prepare for running an on the fly pass, freeing memory if needed
243 // from a previous run.
244 void releaseMemoryOnTheFly();
245
Devang Patel5f4ddf52006-12-19 19:46:59 +0000246 /// run - Execute all of the passes scheduled for execution. Keep track of
247 /// whether any of the passes modifies the module, and if so, return true.
248 bool run(Function &F);
249
250 /// doInitialization - Run all of the initializers for the function passes.
251 ///
252 bool doInitialization(Module &M);
Dan Gohman95df6192010-08-12 23:50:08 +0000253
Dan Gohman209ee182007-07-30 14:51:13 +0000254 /// doFinalization - Run all of the finalizers for the function passes.
Devang Patel5f4ddf52006-12-19 19:46:59 +0000255 ///
256 bool doFinalization(Module &M);
257
Dan Gohman95df6192010-08-12 23:50:08 +0000258
Chris Lattner3660eca2010-01-22 05:24:46 +0000259 virtual PMDataManager *getAsPMDataManager() { return this; }
260 virtual Pass *getAsPass() { return this; }
Andrew Trick11e43292012-02-01 07:16:20 +0000261 virtual PassManagerType getTopLevelPassManagerType() {
262 return PMT_FunctionPassManager;
263 }
Chris Lattner3660eca2010-01-22 05:24:46 +0000264
Devang Patel5f4ddf52006-12-19 19:46:59 +0000265 /// Pass Manager itself does not invalidate any analysis info.
266 void getAnalysisUsage(AnalysisUsage &Info) const {
267 Info.setPreservesAll();
268 }
269
Devang Patel5f4ddf52006-12-19 19:46:59 +0000270 FPPassManager *getContainedManager(unsigned N) {
Chris Lattnerf9574362009-03-06 05:53:14 +0000271 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel5f4ddf52006-12-19 19:46:59 +0000272 FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]);
273 return FP;
274 }
Devang Patel5f4ddf52006-12-19 19:46:59 +0000275};
276
David Blaikie2d24e2a2011-12-20 02:50:00 +0000277void FunctionPassManagerImpl::anchor() {}
278
Devang Patel19974732007-05-03 01:11:54 +0000279char FunctionPassManagerImpl::ID = 0;
Dan Gohman95df6192010-08-12 23:50:08 +0000280
Devang Patel5f4ddf52006-12-19 19:46:59 +0000281//===----------------------------------------------------------------------===//
282// MPPassManager
283//
284/// MPPassManager manages ModulePasses and function pass managers.
Dan Gohmanb4b28232008-03-11 16:18:48 +0000285/// It batches all Module passes and function pass managers together and
286/// sequences them to process one module.
Devang Patel5f4ddf52006-12-19 19:46:59 +0000287class MPPassManager : public Pass, public PMDataManager {
Devang Patelc67c9382006-11-08 10:05:38 +0000288public:
Devang Patel19974732007-05-03 01:11:54 +0000289 static char ID;
Andrew Trick0e122d12011-08-29 17:07:00 +0000290 explicit MPPassManager() :
291 Pass(PT_PassManager, ID), PMDataManager() { }
Devang Patel693941b2007-04-16 20:39:59 +0000292
293 // Delete on the fly managers.
294 virtual ~MPPassManager() {
Dan Gohman95df6192010-08-12 23:50:08 +0000295 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
Devang Patel693941b2007-04-16 20:39:59 +0000296 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
297 I != E; ++I) {
Devang Pateldfa1ec32007-04-26 17:50:19 +0000298 FunctionPassManagerImpl *FPP = I->second;
Devang Patel693941b2007-04-16 20:39:59 +0000299 delete FPP;
300 }
301 }
302
Dan Gohman95df6192010-08-12 23:50:08 +0000303 /// createPrinterPass - Get a module printer pass.
David Greene5c8aa952010-04-02 23:17:14 +0000304 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
305 return createPrintModulePass(&O, false, Banner);
306 }
307
Devang Patelc67c9382006-11-08 10:05:38 +0000308 /// run - Execute all of the passes scheduled for execution. Keep track of
309 /// whether any of the passes modifies the module, and if so, return true.
310 bool runOnModule(Module &M);
Devang Patelbe6d5152006-11-13 22:40:09 +0000311
Pedro Artigas49eb6282012-12-03 21:56:57 +0000312 using llvm::Pass::doInitialization;
313 using llvm::Pass::doFinalization;
314
Owen Anderson40b6fdb2012-11-15 00:14:15 +0000315 /// doInitialization - Run all of the initializers for the module passes.
316 ///
Dmitri Gribenko79c07d22012-11-15 16:51:49 +0000317 bool doInitialization();
Owen Anderson40b6fdb2012-11-15 00:14:15 +0000318
319 /// doFinalization - Run all of the finalizers for the module passes.
320 ///
Dmitri Gribenko79c07d22012-11-15 16:51:49 +0000321 bool doFinalization();
Owen Anderson40b6fdb2012-11-15 00:14:15 +0000322
Devang Patel66d72e12006-12-07 19:57:52 +0000323 /// Pass Manager itself does not invalidate any analysis info.
324 void getAnalysisUsage(AnalysisUsage &Info) const {
325 Info.setPreservesAll();
326 }
327
Devang Patel569a6fd2007-04-16 20:12:57 +0000328 /// Add RequiredPass into list of lower level passes required by pass P.
329 /// RequiredPass is run on the fly by Pass Manager when P requests it
330 /// through getAnalysis interface.
331 virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass);
332
Dan Gohman95df6192010-08-12 23:50:08 +0000333 /// Return function pass corresponding to PassInfo PI, that is
Devang Patel0ed8df32007-04-16 20:27:05 +0000334 /// required by module pass MP. Instantiate analysis pass, by using
335 /// its runOnFunction() for function F.
Owen Anderson90c579d2010-08-06 18:33:48 +0000336 virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F);
Devang Patel0ed8df32007-04-16 20:27:05 +0000337
Devang Patele27ae7e2007-02-01 22:08:25 +0000338 virtual const char *getPassName() const {
339 return "Module Pass Manager";
340 }
341
Chris Lattner3660eca2010-01-22 05:24:46 +0000342 virtual PMDataManager *getAsPMDataManager() { return this; }
343 virtual Pass *getAsPass() { return this; }
344
Devang Patelebc09222006-12-12 23:34:33 +0000345 // Print passes managed by this manager
346 void dumpPassStructure(unsigned Offset) {
Benjamin Kramer3dedf7e2011-08-29 18:14:17 +0000347 llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n";
Devang Patel1554c852006-12-16 00:56:26 +0000348 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
349 ModulePass *MP = getContainedPass(Index);
Dan Gohman8a757ae2010-08-19 01:29:07 +0000350 MP->dumpPassStructure(Offset + 1);
Dan Gohman82c32c42009-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 Patel1554c852006-12-16 00:56:26 +0000355 dumpLastUses(MP, Offset+1);
Devang Patelebc09222006-12-12 23:34:33 +0000356 }
357 }
358
Devang Patel1554c852006-12-16 00:56:26 +0000359 ModulePass *getContainedPass(unsigned N) {
Evan Cheng310fa652012-11-13 02:56:38 +0000360 assert(N < PassVector.size() && "Pass number out of range!");
Chris Lattnerf9574362009-03-06 05:53:14 +0000361 return static_cast<ModulePass *>(PassVector[N]);
Devang Patel1554c852006-12-16 00:56:26 +0000362 }
363
Dan Gohman95df6192010-08-12 23:50:08 +0000364 virtual PassManagerType getPassManagerType() const {
365 return PMT_ModulePassManager;
Devang Patel84da80d2007-02-27 15:00:39 +0000366 }
Devang Patel0ed8df32007-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 Pateldfa1ec32007-04-26 17:50:19 +0000371 std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers;
Devang Patelc67c9382006-11-08 10:05:38 +0000372};
373
Devang Patel19974732007-05-03 01:11:54 +0000374char MPPassManager::ID = 0;
Devang Patel7e601a72006-12-12 22:47:13 +0000375//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +0000376// PassManagerImpl
Devang Patel7e601a72006-12-12 22:47:13 +0000377//
Devang Patel794fd752007-05-01 21:15:47 +0000378
Devang Patel5f4ddf52006-12-19 19:46:59 +0000379/// PassManagerImpl manages MPPassManagers
380class PassManagerImpl : public Pass,
Devang Patel36bcb822007-01-11 22:15:30 +0000381 public PMDataManager,
382 public PMTopLevelManager {
David Blaikie2d24e2a2011-12-20 02:50:00 +0000383 virtual void anchor();
Devang Patel5a39b2e2006-11-08 10:29:57 +0000384
385public:
Devang Patel19974732007-05-03 01:11:54 +0000386 static char ID;
Andrew Trick0e122d12011-08-29 17:07:00 +0000387 explicit PassManagerImpl() :
388 Pass(PT_PassManager, ID), PMDataManager(),
389 PMTopLevelManager(new MPPassManager()) {}
Devang Patelf72d29c2006-12-07 23:24:58 +0000390
Devang Patel5a39b2e2006-11-08 10:29:57 +0000391 /// add - Add a pass to the queue of passes to run. This passes ownership of
392 /// the Pass to the PassManager. When the PassManager is destroyed, the pass
393 /// will be destroyed as well, so there is no need to delete the pass. This
394 /// implies that all passes MUST be allocated with 'new'.
Devang Patel877bfbb2006-12-07 21:32:57 +0000395 void add(Pass *P) {
Devang Patele61b7472006-12-08 22:34:02 +0000396 schedulePass(P);
Devang Patel877bfbb2006-12-07 21:32:57 +0000397 }
Dan Gohman95df6192010-08-12 23:50:08 +0000398
399 /// createPrinterPass - Get a module printer pass.
David Greene5c8aa952010-04-02 23:17:14 +0000400 Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const {
401 return createPrintModulePass(&O, false, Banner);
402 }
403
Devang Patel5a39b2e2006-11-08 10:29:57 +0000404 /// run - Execute all of the passes scheduled for execution. Keep track of
405 /// whether any of the passes modifies the module, and if so, return true.
406 bool run(Module &M);
407
Pedro Artigas49eb6282012-12-03 21:56:57 +0000408 using llvm::Pass::doInitialization;
409 using llvm::Pass::doFinalization;
410
Owen Anderson40b6fdb2012-11-15 00:14:15 +0000411 /// doInitialization - Run all of the initializers for the module passes.
412 ///
Dmitri Gribenko79c07d22012-11-15 16:51:49 +0000413 bool doInitialization();
Owen Anderson40b6fdb2012-11-15 00:14:15 +0000414
415 /// doFinalization - Run all of the finalizers for the module passes.
416 ///
Dmitri Gribenko79c07d22012-11-15 16:51:49 +0000417 bool doFinalization();
Owen Anderson40b6fdb2012-11-15 00:14:15 +0000418
Devang Patel66d72e12006-12-07 19:57:52 +0000419 /// Pass Manager itself does not invalidate any analysis info.
420 void getAnalysisUsage(AnalysisUsage &Info) const {
421 Info.setPreservesAll();
422 }
423
Chris Lattner3660eca2010-01-22 05:24:46 +0000424 virtual PMDataManager *getAsPMDataManager() { return this; }
425 virtual Pass *getAsPass() { return this; }
Andrew Trick11e43292012-02-01 07:16:20 +0000426 virtual PassManagerType getTopLevelPassManagerType() {
427 return PMT_ModulePassManager;
428 }
Chris Lattner3660eca2010-01-22 05:24:46 +0000429
Devang Patel5f4ddf52006-12-19 19:46:59 +0000430 MPPassManager *getContainedManager(unsigned N) {
Chris Lattnerf9574362009-03-06 05:53:14 +0000431 assert(N < PassManagers.size() && "Pass number out of range!");
Devang Patel5f4ddf52006-12-19 19:46:59 +0000432 MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]);
433 return MP;
434 }
Devang Patel5a39b2e2006-11-08 10:29:57 +0000435};
436
David Blaikie2d24e2a2011-12-20 02:50:00 +0000437void PassManagerImpl::anchor() {}
438
Devang Patel19974732007-05-03 01:11:54 +0000439char PassManagerImpl::ID = 0;
Devang Patelc874eb52007-01-29 23:10:37 +0000440} // End of llvm namespace
441
442namespace {
443
444//===----------------------------------------------------------------------===//
Chris Lattnerd6f16582009-03-06 06:45:05 +0000445/// TimingInfo Class - This class is used to calculate information about the
446/// amount of time each pass takes to execute. This only happens when
447/// -time-passes is enabled on the command line.
448///
Owen Andersonf005a642009-06-17 21:28:54 +0000449
Owen Anderson3c8031d2009-06-18 20:51:00 +0000450static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex;
Owen Andersonf005a642009-06-17 21:28:54 +0000451
Nick Lewycky6726b6d2009-10-25 06:33:48 +0000452class TimingInfo {
Jakob Stoklund Olesend30f1682012-12-03 17:31:11 +0000453 DenseMap<Pass*, Timer*> TimingData;
Devang Patelc874eb52007-01-29 23:10:37 +0000454 TimerGroup TG;
Devang Patelc874eb52007-01-29 23:10:37 +0000455public:
456 // Use 'create' member to get this.
457 TimingInfo() : TG("... Pass execution timing report ...") {}
Dan Gohman95df6192010-08-12 23:50:08 +0000458
Devang Patelc874eb52007-01-29 23:10:37 +0000459 // TimingDtor - Print out information about timing information
460 ~TimingInfo() {
Chris Lattnera782e752010-03-30 04:03:22 +0000461 // Delete all of the timers, which accumulate their info into the
462 // TimerGroup.
Jakob Stoklund Olesend30f1682012-12-03 17:31:11 +0000463 for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(),
Chris Lattnera782e752010-03-30 04:03:22 +0000464 E = TimingData.end(); I != E; ++I)
465 delete I->second;
Devang Patelc874eb52007-01-29 23:10:37 +0000466 // TimerGroup is deleted next, printing the report.
467 }
468
469 // createTheTimeInfo - This method either initializes the TheTimeInfo pointer
470 // to a non null value (if the -time-passes option is enabled) or it leaves it
471 // null. It may be called multiple times.
472 static void createTheTimeInfo();
473
Chris Lattnera782e752010-03-30 04:03:22 +0000474 /// getPassTimer - Return the timer for the specified pass if it exists.
475 Timer *getPassTimer(Pass *P) {
Dan Gohman95df6192010-08-12 23:50:08 +0000476 if (P->getAsPMDataManager())
Dan Gohman5c12ada2009-09-28 00:07:05 +0000477 return 0;
Devang Patelc874eb52007-01-29 23:10:37 +0000478
Owen Andersona9d1f2c2009-07-07 18:33:04 +0000479 sys::SmartScopedLock<true> Lock(*TimingInfoMutex);
Jakob Stoklund Olesend30f1682012-12-03 17:31:11 +0000480 Timer *&T = TimingData[P];
Chris Lattnera782e752010-03-30 04:03:22 +0000481 if (T == 0)
482 T = new Timer(P->getPassName(), TG);
Chris Lattner0d2725a2010-03-30 03:57:00 +0000483 return T;
Devang Patelc874eb52007-01-29 23:10:37 +0000484 }
485};
486
Devang Patelc874eb52007-01-29 23:10:37 +0000487} // End of anon namespace
Devang Patelc67c9382006-11-08 10:05:38 +0000488
Dan Gohman844731a2008-05-13 00:00:25 +0000489static TimingInfo *TheTimeInfo;
490
Devang Patel06e86562006-12-07 19:39:39 +0000491//===----------------------------------------------------------------------===//
Devang Patel1b8d0152006-12-12 22:35:25 +0000492// PMTopLevelManager implementation
493
Devang Patel8f3f3d12007-01-16 02:00:38 +0000494/// Initialize top level manager. Create first pass manager.
Dan Gohman7578ea82010-08-16 21:38:42 +0000495PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) {
496 PMDM->setTopLevelManager(this);
497 addPassManager(PMDM);
498 activeStack.push(PMDM);
Devang Patel8f3f3d12007-01-16 02:00:38 +0000499}
500
Devang Patel1b8d0152006-12-12 22:35:25 +0000501/// Set pass P as the last user of the given analysis passes.
Dan Gohman568a63d2010-10-12 00:12:29 +0000502void
Bill Wendlingc6db6b62012-05-14 07:53:40 +0000503PMTopLevelManager::setLastUser(ArrayRef<Pass*> AnalysisPasses, Pass *P) {
Tobias Grossere9069212011-01-20 21:03:22 +0000504 unsigned PDepth = 0;
505 if (P->getResolver())
506 PDepth = P->getResolver()->getPMDataManager().getDepth();
507
Dan Gohman568a63d2010-10-12 00:12:29 +0000508 for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(),
Devang Patel1b8d0152006-12-12 22:35:25 +0000509 E = AnalysisPasses.end(); I != E; ++I) {
510 Pass *AP = *I;
511 LastUser[AP] = P;
Dan Gohman95df6192010-08-12 23:50:08 +0000512
Devang Pateld46825c2007-03-08 19:05:01 +0000513 if (P == AP)
514 continue;
515
Tobias Grossere9069212011-01-20 21:03:22 +0000516 // Update the last users of passes that are required transitive by AP.
517 AnalysisUsage *AnUsage = findAnalysisUsage(AP);
518 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
519 SmallVector<Pass *, 12> LastUses;
520 SmallVector<Pass *, 12> LastPMUses;
521 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
522 E = IDs.end(); I != E; ++I) {
523 Pass *AnalysisPass = findAnalysisPass(*I);
524 assert(AnalysisPass && "Expected analysis pass to exist.");
525 AnalysisResolver *AR = AnalysisPass->getResolver();
526 assert(AR && "Expected analysis resolver to exist.");
527 unsigned APDepth = AR->getPMDataManager().getDepth();
528
529 if (PDepth == APDepth)
530 LastUses.push_back(AnalysisPass);
531 else if (PDepth > APDepth)
532 LastPMUses.push_back(AnalysisPass);
533 }
534
535 setLastUser(LastUses, P);
536
537 // If this pass has a corresponding pass manager, push higher level
538 // analysis to this pass manager.
539 if (P->getResolver())
540 setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass());
541
542
Devang Patel1b8d0152006-12-12 22:35:25 +0000543 // If AP is the last user of other passes then make P last user of
544 // such passes.
Devang Patel721e59c2008-08-12 00:26:16 +0000545 for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
Devang Patel1b8d0152006-12-12 22:35:25 +0000546 LUE = LastUser.end(); LUI != LUE; ++LUI) {
547 if (LUI->second == AP)
Devang Patel721e59c2008-08-12 00:26:16 +0000548 // DenseMap iterator is not invalidated here because
Tobias Grossere9069212011-01-20 21:03:22 +0000549 // this is just updating existing entries.
Devang Patel1b8d0152006-12-12 22:35:25 +0000550 LastUser[LUI->first] = P;
551 }
552 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000553}
554
555/// Collect passes whose last user is P
Dan Gohmanebb18342010-10-12 00:11:18 +0000556void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses,
Devang Patel721e59c2008-08-12 00:26:16 +0000557 Pass *P) {
Dan Gohman95df6192010-08-12 23:50:08 +0000558 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI =
Devang Patel721e59c2008-08-12 00:26:16 +0000559 InversedLastUser.find(P);
560 if (DMI == InversedLastUser.end())
561 return;
562
563 SmallPtrSet<Pass *, 8> &LU = DMI->second;
564 for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
565 E = LU.end(); I != E; ++I) {
566 LastUses.push_back(*I);
567 }
568
Devang Patel1b8d0152006-12-12 22:35:25 +0000569}
570
Devang Patel3b8a9062008-08-11 21:13:39 +0000571AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
572 AnalysisUsage *AnUsage = NULL;
573 DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
Dan Gohman95df6192010-08-12 23:50:08 +0000574 if (DMI != AnUsageMap.end())
Devang Patel3b8a9062008-08-11 21:13:39 +0000575 AnUsage = DMI->second;
576 else {
577 AnUsage = new AnalysisUsage();
578 P->getAnalysisUsage(*AnUsage);
579 AnUsageMap[P] = AnUsage;
580 }
581 return AnUsage;
582}
583
Devang Patel1b8d0152006-12-12 22:35:25 +0000584/// Schedule pass P for execution. Make sure that passes required by
585/// P are run before P is run. Update analysis info maintained by
586/// the manager. Remove dead passes. This is a recursive function.
587void PMTopLevelManager::schedulePass(Pass *P) {
588
Devang Patel9d133e12007-01-16 21:43:18 +0000589 // TODO : Allocate function manager for this pass, other wise required set
590 // may be inserted into previous function manager
Devang Patel1b8d0152006-12-12 22:35:25 +0000591
Devang Patel22a1cf92007-03-06 01:06:16 +0000592 // Give pass a chance to prepare the stage.
593 P->preparePassManager(activeStack);
594
Devang Patel1cee94f2008-03-18 00:39:19 +0000595 // If P is an analysis pass and it is available then do not
596 // generate the analysis again. Stale analysis info should not be
597 // available at this point.
Owen Anderson90c579d2010-08-06 18:33:48 +0000598 const PassInfo *PI =
599 PassRegistry::getPassRegistry()->getPassInfo(P->getPassID());
600 if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) {
Nuno Lopes641397f2008-11-04 23:03:58 +0000601 delete P;
Devang Patelc7fe32e2008-03-19 00:48:41 +0000602 return;
Nuno Lopes641397f2008-11-04 23:03:58 +0000603 }
Devang Patel1cee94f2008-03-18 00:39:19 +0000604
Devang Patel3b8a9062008-08-11 21:13:39 +0000605 AnalysisUsage *AnUsage = findAnalysisUsage(P);
606
Devang Patel488dc672008-08-14 23:07:48 +0000607 bool checkAnalysis = true;
608 while (checkAnalysis) {
609 checkAnalysis = false;
Dan Gohman95df6192010-08-12 23:50:08 +0000610
Devang Patel488dc672008-08-14 23:07:48 +0000611 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
612 for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
613 E = RequiredSet.end(); I != E; ++I) {
Dan Gohman95df6192010-08-12 23:50:08 +0000614
Devang Patel488dc672008-08-14 23:07:48 +0000615 Pass *AnalysisPass = findAnalysisPass(*I);
616 if (!AnalysisPass) {
Owen Anderson90c579d2010-08-06 18:33:48 +0000617 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
Victor Oliveira1ef3b6c2012-07-18 19:59:29 +0000618
619 if (PI == NULL) {
620 // Pass P is not in the global PassRegistry
621 dbgs() << "Pass '" << P->getPassName() << "' is not initialized." << "\n";
622 dbgs() << "Verify if there is a pass dependency cycle." << "\n";
623 dbgs() << "Required Passes:" << "\n";
624 for (AnalysisUsage::VectorType::const_iterator I2 = RequiredSet.begin(),
625 E = RequiredSet.end(); I2 != E && I2 != I; ++I2) {
626 Pass *AnalysisPass2 = findAnalysisPass(*I2);
627 if (AnalysisPass2) {
628 dbgs() << "\t" << AnalysisPass2->getPassName() << "\n";
Craig Topper97fe3d92013-02-06 06:50:38 +0000629 } else {
Victor Oliveira1ef3b6c2012-07-18 19:59:29 +0000630 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
631 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
632 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
633 }
634 }
635 }
636
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000637 assert(PI && "Expected required passes to be initialized");
Owen Anderson90c579d2010-08-06 18:33:48 +0000638 AnalysisPass = PI->createPass();
Devang Patel488dc672008-08-14 23:07:48 +0000639 if (P->getPotentialPassManagerType () ==
640 AnalysisPass->getPotentialPassManagerType())
641 // Schedule analysis pass that is managed by the same pass manager.
642 schedulePass(AnalysisPass);
643 else if (P->getPotentialPassManagerType () >
644 AnalysisPass->getPotentialPassManagerType()) {
645 // Schedule analysis pass that is managed by a new manager.
646 schedulePass(AnalysisPass);
Dan Gohman3620ff92010-08-16 22:57:28 +0000647 // Recheck analysis passes to ensure that required analyses that
Devang Patel488dc672008-08-14 23:07:48 +0000648 // are already checked are still available.
649 checkAnalysis = true;
Craig Topper97fe3d92013-02-06 06:50:38 +0000650 } else
Dan Gohman95df6192010-08-12 23:50:08 +0000651 // Do not schedule this analysis. Lower level analsyis
Devang Patel488dc672008-08-14 23:07:48 +0000652 // passes are run on the fly.
653 delete AnalysisPass;
654 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000655 }
656 }
657
658 // Now all required passes are available.
Andrew Trick11e43292012-02-01 07:16:20 +0000659 if (ImmutablePass *IP = P->getAsImmutablePass()) {
660 // P is a immutable pass and it will be managed by this
661 // top level manager. Set up analysis resolver to connect them.
662 PMDataManager *DM = getAsPMDataManager();
663 AnalysisResolver *AR = new AnalysisResolver(*DM);
664 P->setResolver(AR);
665 DM->initializeAnalysisImpl(P);
666 addImmutablePass(IP);
667 DM->recordAvailableAnalysis(IP);
668 return;
669 }
670
671 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
672 Pass *PP = P->createPrinterPass(
673 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
674 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
675 }
676
677 // Add the requested pass to the best available pass manager.
678 P->assignPassManager(activeStack, getTopLevelPassManagerType());
679
680 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
681 Pass *PP = P->createPrinterPass(
682 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
683 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
684 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000685}
686
687/// Find the pass that implements Analysis AID. Search immutable
688/// passes and all pass managers. If desired pass is not found
689/// then return NULL.
690Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
691
Devang Pateld0fa16c2006-12-12 22:50:05 +0000692 // Check pass managers
Dan Gohmanebb18342010-10-12 00:11:18 +0000693 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman7c347302010-10-11 23:19:01 +0000694 E = PassManagers.end(); I != E; ++I)
695 if (Pass *P = (*I)->findAnalysisPass(AID, false))
696 return P;
Devang Pateld0fa16c2006-12-12 22:50:05 +0000697
698 // Check other pass managers
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000699 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattnerf9574362009-03-06 05:53:14 +0000700 I = IndirectPassManagers.begin(),
Dan Gohman7c347302010-10-11 23:19:01 +0000701 E = IndirectPassManagers.end(); I != E; ++I)
702 if (Pass *P = (*I)->findAnalysisPass(AID, false))
703 return P;
Devang Pateld0fa16c2006-12-12 22:50:05 +0000704
Dan Gohman7c347302010-10-11 23:19:01 +0000705 // Check the immutable passes. Iterate in reverse order so that we find
706 // the most recently registered passes first.
707 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
708 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Anderson90c579d2010-08-06 18:33:48 +0000709 AnalysisID PI = (*I)->getPassID();
Devang Patel1b8d0152006-12-12 22:35:25 +0000710 if (PI == AID)
Dan Gohman7c347302010-10-11 23:19:01 +0000711 return *I;
Devang Patel1b8d0152006-12-12 22:35:25 +0000712
713 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman7c347302010-10-11 23:19:01 +0000714 const PassInfo *PassInf =
715 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000716 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman7c347302010-10-11 23:19:01 +0000717 const std::vector<const PassInfo*> &ImmPI =
718 PassInf->getInterfacesImplemented();
719 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
720 EE = ImmPI.end(); II != EE; ++II) {
721 if ((*II)->getTypeInfo() == AID)
722 return *I;
Devang Patel1b8d0152006-12-12 22:35:25 +0000723 }
724 }
725
Dan Gohman7c347302010-10-11 23:19:01 +0000726 return 0;
Devang Patel1b8d0152006-12-12 22:35:25 +0000727}
728
Devang Patelebc09222006-12-12 23:34:33 +0000729// Print passes managed by this top level manager.
Devang Patela52035a2006-12-15 20:13:01 +0000730void PMTopLevelManager::dumpPasses() const {
Devang Patelebc09222006-12-12 23:34:33 +0000731
Devang Patel26426942007-01-17 20:33:36 +0000732 if (PassDebugging < Structure)
Devang Patel5f4ddf52006-12-19 19:46:59 +0000733 return;
734
Devang Patelebc09222006-12-12 23:34:33 +0000735 // Print out the immutable passes
736 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohman8a757ae2010-08-19 01:29:07 +0000737 ImmutablePasses[i]->dumpPassStructure(0);
Devang Patelebc09222006-12-12 23:34:33 +0000738 }
Dan Gohman95df6192010-08-12 23:50:08 +0000739
Dan Gohman8a757ae2010-08-19 01:29:07 +0000740 // Every class that derives from PMDataManager also derives from Pass
741 // (sometimes indirectly), but there's no inheritance relationship
742 // between PMDataManager and Pass, so we have to getAsPass to get
743 // from a PMDataManager* to a Pass*.
Devang Patel78766ff2008-08-12 15:44:31 +0000744 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Patelebc09222006-12-12 23:34:33 +0000745 E = PassManagers.end(); I != E; ++I)
Dan Gohman8a757ae2010-08-19 01:29:07 +0000746 (*I)->getAsPass()->dumpPassStructure(1);
Devang Patelebc09222006-12-12 23:34:33 +0000747}
748
Devang Patela52035a2006-12-15 20:13:01 +0000749void PMTopLevelManager::dumpArguments() const {
Devang Patelc32cf542006-12-13 22:10:00 +0000750
Devang Patel26426942007-01-17 20:33:36 +0000751 if (PassDebugging < Arguments)
Devang Patelc32cf542006-12-13 22:10:00 +0000752 return;
753
David Greene170c48a2010-01-05 01:30:02 +0000754 dbgs() << "Pass Arguments: ";
Dan Gohman67a84f12010-11-11 16:32:17 +0000755 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
756 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
757 if (const PassInfo *PI =
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000758 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
759 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohman67a84f12010-11-11 16:32:17 +0000760 if (!PI->isAnalysisGroup())
761 dbgs() << " -" << PI->getPassArgument();
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000762 }
Devang Patel78766ff2008-08-12 15:44:31 +0000763 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattnerd6f16582009-03-06 06:45:05 +0000764 E = PassManagers.end(); I != E; ++I)
765 (*I)->dumpPassArguments();
David Greene170c48a2010-01-05 01:30:02 +0000766 dbgs() << "\n";
Devang Patelc32cf542006-12-13 22:10:00 +0000767}
768
Devang Patel1336a6b2006-12-21 00:16:50 +0000769void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000770 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattnerd6f16582009-03-06 06:45:05 +0000771 E = PassManagers.end(); I != E; ++I)
772 (*I)->initializeAnalysisInfo();
Dan Gohman95df6192010-08-12 23:50:08 +0000773
Devang Patel1336a6b2006-12-21 00:16:50 +0000774 // Initailize other pass managers
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000775 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohman95df6192010-08-12 23:50:08 +0000776 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
777 I != E; ++I)
Devang Patel1336a6b2006-12-21 00:16:50 +0000778 (*I)->initializeAnalysisInfo();
Devang Patel721e59c2008-08-12 00:26:16 +0000779
Chris Lattnerf9574362009-03-06 05:53:14 +0000780 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patel721e59c2008-08-12 00:26:16 +0000781 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohman95df6192010-08-12 23:50:08 +0000782 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patel721e59c2008-08-12 00:26:16 +0000783 InversedLastUser.find(DMI->second);
784 if (InvDMI != InversedLastUser.end()) {
785 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
786 L.insert(DMI->first);
787 } else {
788 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
789 InversedLastUser[DMI->second] = L;
790 }
791 }
Devang Patel1336a6b2006-12-21 00:16:50 +0000792}
793
Devang Patelab7752c2007-01-12 18:52:44 +0000794/// Destructor
795PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000796 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patelab7752c2007-01-12 18:52:44 +0000797 E = PassManagers.end(); I != E; ++I)
798 delete *I;
Dan Gohman95df6192010-08-12 23:50:08 +0000799
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000800 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patelab7752c2007-01-12 18:52:44 +0000801 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
802 delete *I;
Devang Patel3b8a9062008-08-11 21:13:39 +0000803
804 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattnerf9574362009-03-06 05:53:14 +0000805 DME = AnUsageMap.end(); DMI != DME; ++DMI)
806 delete DMI->second;
Devang Patelab7752c2007-01-12 18:52:44 +0000807}
808
Devang Patel1b8d0152006-12-12 22:35:25 +0000809//===----------------------------------------------------------------------===//
Devang Patel419f0e92006-12-07 18:36:24 +0000810// PMDataManager implementation
Devang Patel889739c2006-11-07 22:35:17 +0000811
Devang Patelb8526162006-11-11 01:10:19 +0000812/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patelf32b4dd2006-12-07 19:33:53 +0000813void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Anderson90c579d2010-08-06 18:33:48 +0000814 AnalysisID PI = P->getPassID();
Dan Gohman95df6192010-08-12 23:50:08 +0000815
Chris Lattnerf9574362009-03-06 05:53:14 +0000816 AvailableAnalysis[PI] = P;
Dan Gohman95df6192010-08-12 23:50:08 +0000817
Dan Gohman9e2f6282010-08-12 23:46:28 +0000818 assert(!AvailableAnalysis.empty());
Devang Patelb8526162006-11-11 01:10:19 +0000819
Dan Gohman95df6192010-08-12 23:50:08 +0000820 // This pass is the current implementation of all of the interfaces it
821 // implements as well.
Owen Anderson90c579d2010-08-06 18:33:48 +0000822 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
823 if (PInf == 0) return;
824 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson2dcacab2010-07-20 16:55:05 +0000825 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Anderson90c579d2010-08-06 18:33:48 +0000826 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patelb8526162006-11-11 01:10:19 +0000827}
828
Devang Patel7b65dd92007-03-06 17:52:53 +0000829// Return true if P preserves high level analysis used by other
830// passes managed by this manager
831bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +0000832 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel3b8a9062008-08-11 21:13:39 +0000833 if (AnUsage->getPreservesAll())
Devang Patel7b65dd92007-03-06 17:52:53 +0000834 return true;
Dan Gohman95df6192010-08-12 23:50:08 +0000835
Devang Patel3b8a9062008-08-11 21:13:39 +0000836 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000837 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel7b65dd92007-03-06 17:52:53 +0000838 E = HigherLevelAnalysis.end(); I != E; ++I) {
839 Pass *P1 = *I;
Chris Lattner5e664b82010-01-22 04:55:08 +0000840 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman97cf759b2008-01-29 12:09:55 +0000841 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohman95df6192010-08-12 23:50:08 +0000842 P1->getPassID()) ==
Devang Pateld46825c2007-03-08 19:05:01 +0000843 PreservedSet.end())
844 return false;
Devang Patel7b65dd92007-03-06 17:52:53 +0000845 }
Dan Gohman95df6192010-08-12 23:50:08 +0000846
Devang Patel7b65dd92007-03-06 17:52:53 +0000847 return true;
848}
849
Chris Lattnere2c5ecd2008-08-07 07:34:50 +0000850/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patel58e0ef12007-07-19 18:02:32 +0000851void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattnere2c5ecd2008-08-07 07:34:50 +0000852 // Don't do this unless assertions are enabled.
853#ifdef NDEBUG
854 return;
855#endif
Devang Patel3b8a9062008-08-11 21:13:39 +0000856 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
857 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel889739c2006-11-07 22:35:17 +0000858
Devang Patel9750b5d2007-07-19 05:36:09 +0000859 // Verify preserved analysis
Chris Lattnerfc65d382008-08-08 05:33:04 +0000860 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patel58e0ef12007-07-19 18:02:32 +0000861 E = PreservedSet.end(); I != E; ++I) {
862 AnalysisID AID = *I;
Dan Gohman9450b0e2009-09-28 00:27:48 +0000863 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattnera782e752010-03-30 04:03:22 +0000864 TimeRegion PassTimer(getPassTimer(AP));
Devang Patel58e0ef12007-07-19 18:02:32 +0000865 AP->verifyAnalysis();
Dan Gohman9450b0e2009-09-28 00:27:48 +0000866 }
Devang Patel5b57e722008-07-01 17:44:24 +0000867 }
868}
869
Devang Patel844a3d12008-07-01 19:50:56 +0000870/// Remove Analysis not preserved by Pass P
Devang Patel58e0ef12007-07-19 18:02:32 +0000871void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +0000872 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
873 if (AnUsage->getPreservesAll())
Devang Patel04b4e052006-12-07 20:03:49 +0000874 return;
875
Devang Patel3b8a9062008-08-11 21:13:39 +0000876 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Michael Ilsemance522ee2013-02-26 01:31:59 +0000877 for (DenseMap<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel54e247d2006-12-12 23:07:44 +0000878 E = AvailableAnalysis.end(); I != E; ) {
Michael Ilsemance522ee2013-02-26 01:31:59 +0000879 DenseMap<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner5e664b82010-01-22 04:55:08 +0000880 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohman95df6192010-08-12 23:50:08 +0000881 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patele62f7502008-06-03 01:02:16 +0000882 PreservedSet.end()) {
Devang Patel14d65812006-11-11 01:24:55 +0000883 // Remove this analysis
Devang Patele62f7502008-06-03 01:02:16 +0000884 if (PassDebugging >= Details) {
885 Pass *S = Info->second;
David Greene170c48a2010-01-05 01:30:02 +0000886 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
887 dbgs() << S->getPassName() << "'\n";
Devang Patele62f7502008-06-03 01:02:16 +0000888 }
Dan Gohmane1877262008-11-06 21:57:17 +0000889 AvailableAnalysis.erase(Info);
Devang Patele62f7502008-06-03 01:02:16 +0000890 }
Devang Patel14d65812006-11-11 01:24:55 +0000891 }
Dan Gohman95df6192010-08-12 23:50:08 +0000892
Devang Patelfe613902007-03-06 01:55:46 +0000893 // Check inherited analysis also. If P is not preserving analysis
894 // provided by parent manager then remove it here.
895 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
896
897 if (!InheritedAnalysis[Index])
898 continue;
899
Michael Ilsemance522ee2013-02-26 01:31:59 +0000900 for (DenseMap<AnalysisID, Pass*>::iterator
Devang Patelfe613902007-03-06 01:55:46 +0000901 I = InheritedAnalysis[Index]->begin(),
902 E = InheritedAnalysis[Index]->end(); I != E; ) {
Michael Ilsemance522ee2013-02-26 01:31:59 +0000903 DenseMap<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner5e664b82010-01-22 04:55:08 +0000904 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohman95df6192010-08-12 23:50:08 +0000905 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter1f6ae812009-12-04 06:58:24 +0000906 PreservedSet.end()) {
Devang Patelfe613902007-03-06 01:55:46 +0000907 // Remove this analysis
Andreas Neustifter1f6ae812009-12-04 06:58:24 +0000908 if (PassDebugging >= Details) {
909 Pass *S = Info->second;
David Greene170c48a2010-01-05 01:30:02 +0000910 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
911 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter1f6ae812009-12-04 06:58:24 +0000912 }
Devang Pateld46825c2007-03-08 19:05:01 +0000913 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter1f6ae812009-12-04 06:58:24 +0000914 }
Devang Patelfe613902007-03-06 01:55:46 +0000915 }
916 }
Devang Patel889739c2006-11-07 22:35:17 +0000917}
918
Devang Pateldf1a10e2006-11-14 03:05:08 +0000919/// Remove analysis passes that are not used any longer
Daniel Dunbar2928c832009-11-06 10:58:06 +0000920void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel7f997612007-03-05 20:01:30 +0000921 enum PassDebuggingString DBG_STR) {
Devang Patelf9a60ae2006-12-08 00:37:52 +0000922
Devang Pateledbef382007-07-20 18:04:54 +0000923 SmallVector<Pass *, 12> DeadPasses;
Devang Patel0ed8df32007-04-16 20:27:05 +0000924
Devang Patel693941b2007-04-16 20:39:59 +0000925 // If this is a on the fly manager then it does not have TPM.
Devang Patel0ed8df32007-04-16 20:27:05 +0000926 if (!TPM)
927 return;
928
Devang Patelf9a60ae2006-12-08 00:37:52 +0000929 TPM->collectLastUses(DeadPasses, P);
930
Devang Patel8fb6a942008-06-06 17:50:36 +0000931 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene170c48a2010-01-05 01:30:02 +0000932 dbgs() << " -*- '" << P->getPassName();
933 dbgs() << "' is the last user of following pass instances.";
934 dbgs() << " Free these instances\n";
Evan Cheng7c9b6522008-06-04 09:13:31 +0000935 }
936
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000937 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman27a8fb82009-09-27 23:38:27 +0000938 E = DeadPasses.end(); I != E; ++I)
939 freePass(*I, Msg, DBG_STR);
940}
Devang Patel4eeea772006-12-13 23:50:44 +0000941
Daniel Dunbar2928c832009-11-06 10:58:06 +0000942void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman27a8fb82009-09-27 23:38:27 +0000943 enum PassDebuggingString DBG_STR) {
944 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel4eeea772006-12-13 23:50:44 +0000945
Dan Gohman27a8fb82009-09-27 23:38:27 +0000946 {
947 // If the pass crashes releasing memory, remember this.
948 PassManagerPrettyStackEntry X(P);
Chris Lattnera782e752010-03-30 04:03:22 +0000949 TimeRegion PassTimer(getPassTimer(P));
950
Dan Gohman27a8fb82009-09-27 23:38:27 +0000951 P->releaseMemory();
Dan Gohman27a8fb82009-09-27 23:38:27 +0000952 }
953
Owen Anderson90c579d2010-08-06 18:33:48 +0000954 AnalysisID PI = P->getPassID();
955 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman27a8fb82009-09-27 23:38:27 +0000956 // Remove the pass itself (if it is not already removed).
957 AvailableAnalysis.erase(PI);
958
959 // Remove all interfaces this pass implements, for which it is also
960 // listed as the available implementation.
Owen Anderson90c579d2010-08-06 18:33:48 +0000961 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson2dcacab2010-07-20 16:55:05 +0000962 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Michael Ilsemance522ee2013-02-26 01:31:59 +0000963 DenseMap<AnalysisID, Pass*>::iterator Pos =
Owen Anderson90c579d2010-08-06 18:33:48 +0000964 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman27a8fb82009-09-27 23:38:27 +0000965 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patel617fddf2008-10-06 20:36:36 +0000966 AvailableAnalysis.erase(Pos);
Devang Patel617fddf2008-10-06 20:36:36 +0000967 }
Devang Patelf9a60ae2006-12-08 00:37:52 +0000968 }
Devang Pateldf1a10e2006-11-14 03:05:08 +0000969}
970
Dan Gohman95df6192010-08-12 23:50:08 +0000971/// Add pass P into the PassVector. Update
Devang Patel893a5a62006-11-11 02:04:19 +0000972/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattnerf9574362009-03-06 05:53:14 +0000973void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Patel145e83d2006-12-08 23:53:00 +0000974 // This manager is going to manage pass P. Set up analysis resolver
975 // to connect them.
Devang Patelcde53d32007-01-05 22:47:07 +0000976 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel145e83d2006-12-08 23:53:00 +0000977 P->setResolver(AR);
978
Devang Patelcf5fb2b2007-03-05 22:57:49 +0000979 // If a FunctionPass F is the last user of ModulePass info M
980 // then the F's manager, not F, records itself as a last user of M.
Devang Pateledbef382007-07-20 18:04:54 +0000981 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelcf5fb2b2007-03-05 22:57:49 +0000982
Chris Lattnerf9574362009-03-06 05:53:14 +0000983 if (!ProcessAnalysis) {
984 // Add pass
985 PassVector.push_back(P);
986 return;
Devang Patel893a5a62006-11-11 02:04:19 +0000987 }
Devang Patele2533852006-11-11 01:51:02 +0000988
Chris Lattnerf9574362009-03-06 05:53:14 +0000989 // At the moment, this pass is the last user of all required passes.
990 SmallVector<Pass *, 12> LastUses;
991 SmallVector<Pass *, 8> RequiredPasses;
992 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
993
994 unsigned PDepth = this->getDepth();
995
Dan Gohman95df6192010-08-12 23:50:08 +0000996 collectRequiredAnalysis(RequiredPasses,
Chris Lattnerf9574362009-03-06 05:53:14 +0000997 ReqAnalysisNotAvailable, P);
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000998 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattnerf9574362009-03-06 05:53:14 +0000999 E = RequiredPasses.end(); I != E; ++I) {
1000 Pass *PRequired = *I;
1001 unsigned RDepth = 0;
1002
1003 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1004 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1005 RDepth = DM.getDepth();
1006
1007 if (PDepth == RDepth)
1008 LastUses.push_back(PRequired);
1009 else if (PDepth > RDepth) {
1010 // Let the parent claim responsibility of last use
1011 TransferLastUses.push_back(PRequired);
1012 // Keep track of higher level analysis used by this manager.
1013 HigherLevelAnalysis.push_back(PRequired);
Dan Gohman95df6192010-08-12 23:50:08 +00001014 } else
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001015 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattnerf9574362009-03-06 05:53:14 +00001016 }
1017
1018 // Set P as P's last user until someone starts using P.
1019 // However, if P is a Pass Manager then it does not need
1020 // to record its last user.
Chris Lattner3660eca2010-01-22 05:24:46 +00001021 if (P->getAsPMDataManager() == 0)
Chris Lattnerf9574362009-03-06 05:53:14 +00001022 LastUses.push_back(P);
1023 TPM->setLastUser(LastUses, P);
1024
1025 if (!TransferLastUses.empty()) {
Chris Lattner3660eca2010-01-22 05:24:46 +00001026 Pass *My_PM = getAsPass();
Chris Lattnerf9574362009-03-06 05:53:14 +00001027 TPM->setLastUser(TransferLastUses, My_PM);
1028 TransferLastUses.clear();
1029 }
1030
Dan Gohman3620ff92010-08-16 22:57:28 +00001031 // Now, take care of required analyses that are not available.
Dan Gohman9b0e47e2010-10-12 00:15:27 +00001032 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohman95df6192010-08-12 23:50:08 +00001033 I = ReqAnalysisNotAvailable.begin(),
Chris Lattnerf9574362009-03-06 05:53:14 +00001034 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Anderson90c579d2010-08-06 18:33:48 +00001035 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1036 Pass *AnalysisPass = PI->createPass();
Chris Lattnerf9574362009-03-06 05:53:14 +00001037 this->addLowerLevelRequiredPass(P, AnalysisPass);
1038 }
1039
1040 // Take a note of analysis required and made available by this pass.
1041 // Remove the analysis not preserved by this pass
1042 removeNotPreservedAnalysis(P);
1043 recordAvailableAnalysis(P);
1044
Devang Patele2533852006-11-11 01:51:02 +00001045 // Add pass
1046 PassVector.push_back(P);
Devang Patele2533852006-11-11 01:51:02 +00001047}
1048
Devang Patel569a6fd2007-04-16 20:12:57 +00001049
1050/// Populate RP with analysis pass that are required by
1051/// pass P and are available. Populate RP_NotAvail with analysis
1052/// pass that are required by pass P but are not available.
Dan Gohmanebb18342010-10-12 00:11:18 +00001053void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1054 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patel569a6fd2007-04-16 20:12:57 +00001055 Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +00001056 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1057 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohman95df6192010-08-12 23:50:08 +00001058 for (AnalysisUsage::VectorType::const_iterator
Chris Lattnerf9574362009-03-06 05:53:14 +00001059 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patel569a6fd2007-04-16 20:12:57 +00001060 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohman95df6192010-08-12 23:50:08 +00001061 RP.push_back(AnalysisPass);
Devang Patel569a6fd2007-04-16 20:12:57 +00001062 else
Chris Lattnerf9574362009-03-06 05:53:14 +00001063 RP_NotAvail.push_back(*I);
Devang Patelc17bbb62006-12-07 23:05:44 +00001064 }
Devang Patel27aaab22006-12-12 23:09:32 +00001065
Devang Patel3b8a9062008-08-11 21:13:39 +00001066 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnerfc65d382008-08-08 05:33:04 +00001067 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patel27aaab22006-12-12 23:09:32 +00001068 E = IDs.end(); I != E; ++I) {
Devang Patel569a6fd2007-04-16 20:12:57 +00001069 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohman95df6192010-08-12 23:50:08 +00001070 RP.push_back(AnalysisPass);
Devang Patel569a6fd2007-04-16 20:12:57 +00001071 else
Chris Lattnerf9574362009-03-06 05:53:14 +00001072 RP_NotAvail.push_back(*I);
Devang Patel27aaab22006-12-12 23:09:32 +00001073 }
Devang Patelc17bbb62006-12-07 23:05:44 +00001074}
1075
Devang Patel2f42ed62006-11-14 21:49:36 +00001076// All Required analyses should be available to the pass as it runs! Here
1077// we fill in the AnalysisImpls member of the pass so that it can
1078// successfully use the getAnalysis() method to retrieve the
1079// implementations it needs.
1080//
Devang Patel419f0e92006-12-07 18:36:24 +00001081void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +00001082 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1083
Chris Lattnerfc65d382008-08-08 05:33:04 +00001084 for (AnalysisUsage::VectorType::const_iterator
Devang Patel3b8a9062008-08-11 21:13:39 +00001085 I = AnUsage->getRequiredSet().begin(),
1086 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel69867b52006-12-08 22:30:11 +00001087 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel2f42ed62006-11-14 21:49:36 +00001088 if (Impl == 0)
Devang Patelf4bd76a2007-04-16 20:44:16 +00001089 // This may be analysis pass that is initialized on the fly.
1090 // If that is not the case then it will raise an assert when it is used.
1091 continue;
Devang Patelcde53d32007-01-05 22:47:07 +00001092 AnalysisResolver *AR = P->getResolver();
Chris Lattnerf9574362009-03-06 05:53:14 +00001093 assert(AR && "Analysis Resolver is not set");
Devang Patel298fead2006-12-09 01:11:34 +00001094 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel2f42ed62006-11-14 21:49:36 +00001095 }
1096}
1097
Devang Patel69867b52006-12-08 22:30:11 +00001098/// Find the pass that implements Analysis AID. If desired pass is not found
1099/// then return NULL.
1100Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1101
1102 // Check if AvailableAnalysis map has one entry.
Michael Ilsemance522ee2013-02-26 01:31:59 +00001103 DenseMap<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
Devang Patel69867b52006-12-08 22:30:11 +00001104
1105 if (I != AvailableAnalysis.end())
1106 return I->second;
1107
1108 // Search Parents through TopLevelManager
1109 if (SearchParent)
1110 return TPM->findAnalysisPass(AID);
Dan Gohman95df6192010-08-12 23:50:08 +00001111
Devang Patel5b640e72006-12-09 00:09:12 +00001112 return NULL;
Devang Patel69867b52006-12-08 22:30:11 +00001113}
1114
Devang Patela52035a2006-12-15 20:13:01 +00001115// Print list of passes that are last used by P.
1116void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1117
Devang Pateledbef382007-07-20 18:04:54 +00001118 SmallVector<Pass *, 12> LUses;
Devang Patel693941b2007-04-16 20:39:59 +00001119
1120 // If this is a on the fly manager then it does not have TPM.
1121 if (!TPM)
1122 return;
1123
Devang Patela52035a2006-12-15 20:13:01 +00001124 TPM->collectLastUses(LUses, P);
Dan Gohman95df6192010-08-12 23:50:08 +00001125
Dan Gohmanebb18342010-10-12 00:11:18 +00001126 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patela52035a2006-12-15 20:13:01 +00001127 E = LUses.end(); I != E; ++I) {
David Greene170c48a2010-01-05 01:30:02 +00001128 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohman8a757ae2010-08-19 01:29:07 +00001129 (*I)->dumpPassStructure(0);
Devang Patela52035a2006-12-15 20:13:01 +00001130 }
1131}
1132
1133void PMDataManager::dumpPassArguments() const {
Dan Gohmanebb18342010-10-12 00:11:18 +00001134 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patela52035a2006-12-15 20:13:01 +00001135 E = PassVector.end(); I != E; ++I) {
Chris Lattner3660eca2010-01-22 05:24:46 +00001136 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patela52035a2006-12-15 20:13:01 +00001137 PMD->dumpPassArguments();
1138 else
Owen Anderson90c579d2010-08-06 18:33:48 +00001139 if (const PassInfo *PI =
1140 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patela52035a2006-12-15 20:13:01 +00001141 if (!PI->isAnalysisGroup())
David Greene170c48a2010-01-05 01:30:02 +00001142 dbgs() << " -" << PI->getPassArgument();
Devang Patela52035a2006-12-15 20:13:01 +00001143 }
1144}
1145
Chris Lattner417efc82007-08-10 06:17:04 +00001146void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1147 enum PassDebuggingString S2,
Daniel Dunbar2928c832009-11-06 10:58:06 +00001148 StringRef Msg) {
Devang Patel26426942007-01-17 20:33:36 +00001149 if (PassDebugging < Executions)
Devang Patela52035a2006-12-15 20:13:01 +00001150 return;
David Greene170c48a2010-01-05 01:30:02 +00001151 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel7f997612007-03-05 20:01:30 +00001152 switch (S1) {
1153 case EXECUTION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001154 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001155 break;
1156 case MODIFICATION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001157 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001158 break;
1159 case FREEING_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001160 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001161 break;
1162 default:
1163 break;
1164 }
1165 switch (S2) {
1166 case ON_BASICBLOCK_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001167 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001168 break;
1169 case ON_FUNCTION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001170 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001171 break;
1172 case ON_MODULE_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001173 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001174 break;
Tobias Grosser65513602010-10-20 01:54:44 +00001175 case ON_REGION_MSG:
1176 dbgs() << "' on Region '" << Msg << "'...\n";
1177 break;
Devang Patel7f997612007-03-05 20:01:30 +00001178 case ON_LOOP_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001179 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001180 break;
1181 case ON_CG_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001182 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001183 break;
1184 default:
1185 break;
1186 }
Devang Patela52035a2006-12-15 20:13:01 +00001187}
1188
Chris Lattnerd6f16582009-03-06 06:45:05 +00001189void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001190 if (PassDebugging < Details)
1191 return;
Dan Gohman95df6192010-08-12 23:50:08 +00001192
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001193 AnalysisUsage analysisUsage;
1194 P->getAnalysisUsage(analysisUsage);
1195 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1196}
1197
Chris Lattnerd6f16582009-03-06 06:45:05 +00001198void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001199 if (PassDebugging < Details)
1200 return;
Dan Gohman95df6192010-08-12 23:50:08 +00001201
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001202 AnalysisUsage analysisUsage;
1203 P->getAnalysisUsage(analysisUsage);
1204 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1205}
1206
Daniel Dunbar2928c832009-11-06 10:58:06 +00001207void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattnerd6f16582009-03-06 06:45:05 +00001208 const AnalysisUsage::VectorType &Set) const {
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001209 assert(PassDebugging >= Details);
1210 if (Set.empty())
1211 return;
Roman Divacky59324292012-09-05 22:26:57 +00001212 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerd6f16582009-03-06 06:45:05 +00001213 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene170c48a2010-01-05 01:30:02 +00001214 if (i) dbgs() << ',';
Owen Anderson90c579d2010-08-06 18:33:48 +00001215 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trickc5d93bb2011-06-03 00:48:58 +00001216 if (!PInf) {
1217 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1218 // all drivers.
1219 dbgs() << " Uninitialized Pass";
1220 continue;
1221 }
Owen Anderson90c579d2010-08-06 18:33:48 +00001222 dbgs() << ' ' << PInf->getPassName();
Chris Lattnerd6f16582009-03-06 06:45:05 +00001223 }
David Greene170c48a2010-01-05 01:30:02 +00001224 dbgs() << '\n';
Devang Patela52035a2006-12-15 20:13:01 +00001225}
Devang Patelf3dc6d92006-12-08 23:28:54 +00001226
Devang Patel19fe8f92007-07-27 20:06:09 +00001227/// Add RequiredPass into list of lower level passes required by pass P.
1228/// RequiredPass is run on the fly by Pass Manager when P requests it
1229/// through getAnalysis interface.
1230/// This should be handled by specific pass manager.
1231void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1232 if (TPM) {
1233 TPM->dumpArguments();
1234 TPM->dumpPasses();
1235 }
Devang Patel1cf47cb2008-02-02 01:43:30 +00001236
Dan Gohman95df6192010-08-12 23:50:08 +00001237 // Module Level pass may required Function Level analysis info
1238 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1239 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel1cf47cb2008-02-02 01:43:30 +00001240 // module level pass is requiring lower level analysis info managed by
1241 // lower level pass manager.
1242
1243 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohman95df6192010-08-12 23:50:08 +00001244 // checks whether any lower level manager will be able to provide this
Devang Patel1cf47cb2008-02-02 01:43:30 +00001245 // analysis info on demand or not.
Devang Patelc0c33f52008-06-03 01:20:02 +00001246#ifndef NDEBUG
David Greene170c48a2010-01-05 01:30:02 +00001247 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1248 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelc0c33f52008-06-03 01:20:02 +00001249#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00001250 llvm_unreachable("Unable to schedule pass");
Devang Patel19fe8f92007-07-27 20:06:09 +00001251}
1252
Owen Anderson90c579d2010-08-06 18:33:48 +00001253Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topper50bee422012-02-05 22:14:15 +00001254 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmane407c1d2010-06-21 18:46:45 +00001255}
1256
Devang Patelab7752c2007-01-12 18:52:44 +00001257// Destructor
1258PMDataManager::~PMDataManager() {
Dan Gohmanebb18342010-10-12 00:11:18 +00001259 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patelab7752c2007-01-12 18:52:44 +00001260 E = PassVector.end(); I != E; ++I)
1261 delete *I;
Devang Patelab7752c2007-01-12 18:52:44 +00001262}
1263
Devang Patelf3dc6d92006-12-08 23:28:54 +00001264//===----------------------------------------------------------------------===//
1265// NOTE: Is this the right place to define this method ?
Duncan Sands1465d612009-01-28 13:14:17 +00001266// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1267Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patelf3dc6d92006-12-08 23:28:54 +00001268 return PM.findAnalysisPass(ID, dir);
1269}
1270
Dan Gohman95df6192010-08-12 23:50:08 +00001271Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel6b1df0e2007-04-16 20:56:24 +00001272 Function &F) {
1273 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1274}
1275
Devang Patel06e86562006-12-07 19:39:39 +00001276//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001277// BBPassManager implementation
Devang Patel55fd43f2006-11-07 21:31:57 +00001278
Dan Gohman95df6192010-08-12 23:50:08 +00001279/// Execute all of the passes scheduled for execution by invoking
1280/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel55fd43f2006-11-07 21:31:57 +00001281/// the function, and if so, return true.
Chris Lattnerd6f16582009-03-06 06:45:05 +00001282bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001283 if (F.isDeclaration())
Devang Patel1fbe2c92006-12-12 23:15:28 +00001284 return false;
1285
Devang Patel3b14fbe2006-12-08 01:38:28 +00001286 bool Changed = doInitialization(F);
Devang Patelc1d6e1f2006-11-14 01:23:29 +00001287
Devang Patel55fd43f2006-11-07 21:31:57 +00001288 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel1554c852006-12-16 00:56:26 +00001289 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1290 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001291 bool LocalChanged = false;
Devang Patel017b5d92006-12-14 00:25:06 +00001292
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001293 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001294 dumpRequiredSet(BP);
Devang Patel017b5d92006-12-14 00:25:06 +00001295
Devang Patel1554c852006-12-16 00:56:26 +00001296 initializeAnalysisImpl(BP);
Devang Patel693a74e2006-12-14 00:08:04 +00001297
Chris Lattnerd6f16582009-03-06 06:45:05 +00001298 {
1299 // If the pass crashes, remember this.
1300 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattnera782e752010-03-30 04:03:22 +00001301 TimeRegion PassTimer(getPassTimer(BP));
1302
Dan Gohman16b77212010-03-01 17:34:28 +00001303 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattnerd6f16582009-03-06 06:45:05 +00001304 }
Devang Patel693a74e2006-12-14 00:08:04 +00001305
Dan Gohman16b77212010-03-01 17:34:28 +00001306 Changed |= LocalChanged;
Dan Gohman95df6192010-08-12 23:50:08 +00001307 if (LocalChanged)
Dan Gohman97cf759b2008-01-29 12:09:55 +00001308 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001309 I->getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001310 dumpPreservedSet(BP);
Devang Patel693a74e2006-12-14 00:08:04 +00001311
Devang Patel58e0ef12007-07-19 18:02:32 +00001312 verifyPreservedAnalysis(BP);
Devang Patel1554c852006-12-16 00:56:26 +00001313 removeNotPreservedAnalysis(BP);
1314 recordAvailableAnalysis(BP);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001315 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel55fd43f2006-11-07 21:31:57 +00001316 }
Chris Lattnerfc23bc72007-08-10 06:22:25 +00001317
Bill Wendling47eb1ea2009-12-25 13:50:18 +00001318 return doFinalization(F) || Changed;
Devang Patel55fd43f2006-11-07 21:31:57 +00001319}
1320
Devang Patel964e45e2006-12-08 00:59:05 +00001321// Implement doInitialization and doFinalization
Duncan Sandse70a6832009-02-13 09:42:34 +00001322bool BBPassManager::doInitialization(Module &M) {
Devang Patel964e45e2006-12-08 00:59:05 +00001323 bool Changed = false;
1324
Chris Lattnerd6f16582009-03-06 06:45:05 +00001325 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1326 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel964e45e2006-12-08 00:59:05 +00001327
1328 return Changed;
1329}
1330
Duncan Sandse70a6832009-02-13 09:42:34 +00001331bool BBPassManager::doFinalization(Module &M) {
Devang Patel964e45e2006-12-08 00:59:05 +00001332 bool Changed = false;
1333
Pedro Artigasd1abec32012-12-05 17:12:22 +00001334 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001335 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel964e45e2006-12-08 00:59:05 +00001336
1337 return Changed;
1338}
1339
Duncan Sandse70a6832009-02-13 09:42:34 +00001340bool BBPassManager::doInitialization(Function &F) {
Devang Patel964e45e2006-12-08 00:59:05 +00001341 bool Changed = false;
1342
Devang Patel1554c852006-12-16 00:56:26 +00001343 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1344 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +00001345 Changed |= BP->doInitialization(F);
1346 }
1347
1348 return Changed;
1349}
1350
Duncan Sandse70a6832009-02-13 09:42:34 +00001351bool BBPassManager::doFinalization(Function &F) {
Devang Patel964e45e2006-12-08 00:59:05 +00001352 bool Changed = false;
1353
Devang Patel1554c852006-12-16 00:56:26 +00001354 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1355 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +00001356 Changed |= BP->doFinalization(F);
1357 }
1358
1359 return Changed;
1360}
1361
1362
Devang Patel06e86562006-12-07 19:39:39 +00001363//===----------------------------------------------------------------------===//
Devang Patel31626912006-12-13 02:36:01 +00001364// FunctionPassManager implementation
Devang Patel06e86562006-12-07 19:39:39 +00001365
Devang Patelc63592b2006-11-08 10:44:40 +00001366/// Create new Function pass manager
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001367FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001368 FPM = new FunctionPassManagerImpl();
Devang Pateldff33ef2006-12-12 22:02:16 +00001369 // FPM is the top level manager.
1370 FPM->setTopLevelManager(FPM);
Devang Patelb920bd82006-12-12 23:27:37 +00001371
Dan Gohman59ef0152008-03-13 02:08:36 +00001372 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patelb920bd82006-12-12 23:27:37 +00001373 FPM->setResolver(AR);
Devang Patelcc132cd2006-12-08 18:57:16 +00001374}
1375
Devang Patel31626912006-12-13 02:36:01 +00001376FunctionPassManager::~FunctionPassManager() {
Devang Patel37a6f792006-12-13 00:09:23 +00001377 delete FPM;
1378}
1379
Devang Patelc63592b2006-11-08 10:44:40 +00001380/// add - Add a pass to the queue of passes to run. This passes
1381/// ownership of the Pass to the PassManager. When the
1382/// PassManager_X is destroyed, the pass will be destroyed as well, so
1383/// there is no need to delete the pass. (TODO delete passes.)
1384/// This implies that all passes MUST be allocated with 'new'.
Dan Gohman95df6192010-08-12 23:50:08 +00001385void FunctionPassManager::add(Pass *P) {
Andrew Trick11e43292012-02-01 07:16:20 +00001386 FPM->add(P);
Devang Patelc63592b2006-11-08 10:44:40 +00001387}
1388
Devang Patel214ca232006-11-15 19:39:54 +00001389/// run - Execute all of the passes scheduled for execution. Keep
1390/// track of whether any of the passes modifies the function, and if
1391/// so, return true.
1392///
Devang Patel31626912006-12-13 02:36:01 +00001393bool FunctionPassManager::run(Function &F) {
Nick Lewyckyc6380882010-02-15 21:27:56 +00001394 if (F.isMaterializable()) {
1395 std::string errstr;
Chris Lattnerf88c8562010-04-07 22:41:29 +00001396 if (F.Materialize(&errstr))
Benjamin Kramer1bd73352010-04-08 10:44:28 +00001397 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel214ca232006-11-15 19:39:54 +00001398 }
Devang Patelc4756922006-12-08 22:57:48 +00001399 return FPM->run(F);
Devang Patel214ca232006-11-15 19:39:54 +00001400}
1401
1402
Devang Patel3799f972006-11-15 01:27:05 +00001403/// doInitialization - Run all of the initializers for the function passes.
1404///
Devang Patel31626912006-12-13 02:36:01 +00001405bool FunctionPassManager::doInitialization() {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001406 return FPM->doInitialization(*M);
Devang Patel3799f972006-11-15 01:27:05 +00001407}
1408
Dan Gohman209ee182007-07-30 14:51:13 +00001409/// doFinalization - Run all of the finalizers for the function passes.
Devang Patel3799f972006-11-15 01:27:05 +00001410///
Devang Patel31626912006-12-13 02:36:01 +00001411bool FunctionPassManager::doFinalization() {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001412 return FPM->doFinalization(*M);
Devang Patel3799f972006-11-15 01:27:05 +00001413}
1414
Devang Patel06e86562006-12-07 19:39:39 +00001415//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001416// FunctionPassManagerImpl implementation
1417//
Duncan Sandse70a6832009-02-13 09:42:34 +00001418bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001419 bool Changed = false;
1420
Dan Gohman9f9ca732009-11-23 16:24:18 +00001421 dumpArguments();
1422 dumpPasses();
1423
Pedro Artigasd1abec32012-12-05 17:12:22 +00001424 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1425 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1426 E = IPV.end(); I != E; ++I) {
1427 Changed |= (*I)->doInitialization(M);
1428 }
1429
Chris Lattnerd6f16582009-03-06 06:45:05 +00001430 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1431 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001432
1433 return Changed;
1434}
1435
Duncan Sandse70a6832009-02-13 09:42:34 +00001436bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001437 bool Changed = false;
1438
Pedro Artigasd1abec32012-12-05 17:12:22 +00001439 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001440 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001441
Pedro Artigasd1abec32012-12-05 17:12:22 +00001442 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1443 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1444 E = IPV.end(); I != E; ++I) {
1445 Changed |= (*I)->doFinalization(M);
1446 }
1447
Devang Patel5f4ddf52006-12-19 19:46:59 +00001448 return Changed;
1449}
1450
Devang Patel9dfa1672009-04-01 22:34:41 +00001451/// cleanup - After running all passes, clean up pass manager cache.
1452void FPPassManager::cleanup() {
1453 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1454 FunctionPass *FP = getContainedPass(Index);
1455 AnalysisResolver *AR = FP->getResolver();
1456 assert(AR && "Analysis Resolver is not set");
1457 AR->clearAnalysisImpls();
1458 }
1459}
1460
Torok Edwin1970a892009-06-29 18:49:09 +00001461void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1462 if (!wasRun)
1463 return;
1464 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1465 FPPassManager *FPPM = getContainedManager(Index);
1466 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1467 FPPM->getContainedPass(Index)->releaseMemory();
1468 }
1469 }
Torok Edwin6c839922009-06-29 21:05:10 +00001470 wasRun = false;
Torok Edwin1970a892009-06-29 18:49:09 +00001471}
1472
Devang Patel5f4ddf52006-12-19 19:46:59 +00001473// Execute all the passes managed by this top level manager.
1474// Return true if any function is modified by a pass.
1475bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001476 bool Changed = false;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001477 TimingInfo::createTheTimeInfo();
1478
Devang Patel1336a6b2006-12-21 00:16:50 +00001479 initializeAllAnalysisInfo();
Chris Lattnerd6f16582009-03-06 06:45:05 +00001480 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1481 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patel9dfa1672009-04-01 22:34:41 +00001482
1483 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1484 getContainedManager(Index)->cleanup();
1485
Torok Edwin1970a892009-06-29 18:49:09 +00001486 wasRun = true;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001487 return Changed;
1488}
1489
1490//===----------------------------------------------------------------------===//
1491// FPPassManager implementation
Devang Patel448d27c2006-11-07 21:49:50 +00001492
Devang Patel19974732007-05-03 01:11:54 +00001493char FPPassManager::ID = 0;
Devang Patelab7752c2007-01-12 18:52:44 +00001494/// Print passes managed by this manager
1495void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramer962bad72011-10-16 16:30:34 +00001496 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patelab7752c2007-01-12 18:52:44 +00001497 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1498 FunctionPass *FP = getContainedPass(Index);
Dan Gohman8a757ae2010-08-19 01:29:07 +00001499 FP->dumpPassStructure(Offset + 1);
Devang Patelab7752c2007-01-12 18:52:44 +00001500 dumpLastUses(FP, Offset+1);
1501 }
1502}
1503
1504
Dan Gohman95df6192010-08-12 23:50:08 +00001505/// Execute all of the passes scheduled for execution by invoking
1506/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel448d27c2006-11-07 21:49:50 +00001507/// the function, and if so, return true.
Devang Patel5f4ddf52006-12-19 19:46:59 +00001508bool FPPassManager::runOnFunction(Function &F) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001509 if (F.isDeclaration())
1510 return false;
Devang Patel214ca232006-11-15 19:39:54 +00001511
1512 bool Changed = false;
Devang Patel1fbe2c92006-12-12 23:15:28 +00001513
Devang Patelbed7e682008-03-20 01:09:53 +00001514 // Collect inherited analysis from Module level pass manager.
1515 populateInheritedAnalysis(TPM->activeStack);
Devang Patel1fbe2c92006-12-12 23:15:28 +00001516
Devang Patel1554c852006-12-16 00:56:26 +00001517 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1518 FunctionPass *FP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001519 bool LocalChanged = false;
Devang Patel1554c852006-12-16 00:56:26 +00001520
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001521 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001522 dumpRequiredSet(FP);
Devang Patel693a74e2006-12-14 00:08:04 +00001523
Devang Patel1554c852006-12-16 00:56:26 +00001524 initializeAnalysisImpl(FP);
Eric Christopher9e7e6092012-03-23 03:54:05 +00001525
Chris Lattnerd6f16582009-03-06 06:45:05 +00001526 {
1527 PassManagerPrettyStackEntry X(FP, F);
Chris Lattnera782e752010-03-30 04:03:22 +00001528 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattnerd6f16582009-03-06 06:45:05 +00001529
Dan Gohman16b77212010-03-01 17:34:28 +00001530 LocalChanged |= FP->runOnFunction(F);
Chris Lattnerd6f16582009-03-06 06:45:05 +00001531 }
Devang Patel693a74e2006-12-14 00:08:04 +00001532
Dan Gohman16b77212010-03-01 17:34:28 +00001533 Changed |= LocalChanged;
1534 if (LocalChanged)
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001535 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001536 dumpPreservedSet(FP);
Devang Patel693a74e2006-12-14 00:08:04 +00001537
Devang Patel58e0ef12007-07-19 18:02:32 +00001538 verifyPreservedAnalysis(FP);
Devang Patel1554c852006-12-16 00:56:26 +00001539 removeNotPreservedAnalysis(FP);
1540 recordAvailableAnalysis(FP);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001541 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel214ca232006-11-15 19:39:54 +00001542 }
1543 return Changed;
1544}
1545
Devang Patel5f4ddf52006-12-19 19:46:59 +00001546bool FPPassManager::runOnModule(Module &M) {
Pedro Artigas6eda0812012-11-29 17:47:05 +00001547 bool Changed = false;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001548
Dan Gohmand4271802010-05-11 20:30:00 +00001549 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendling7df4f962011-08-08 23:01:10 +00001550 Changed |= runOnFunction(*I);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001551
Pedro Artigas6eda0812012-11-29 17:47:05 +00001552 return Changed;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001553}
1554
Duncan Sandse70a6832009-02-13 09:42:34 +00001555bool FPPassManager::doInitialization(Module &M) {
Devang Patel3799f972006-11-15 01:27:05 +00001556 bool Changed = false;
1557
Chris Lattnerd6f16582009-03-06 06:45:05 +00001558 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1559 Changed |= getContainedPass(Index)->doInitialization(M);
Pedro Artigas6eda0812012-11-29 17:47:05 +00001560
Devang Patel3799f972006-11-15 01:27:05 +00001561 return Changed;
1562}
1563
Duncan Sandse70a6832009-02-13 09:42:34 +00001564bool FPPassManager::doFinalization(Module &M) {
Devang Patel3799f972006-11-15 01:27:05 +00001565 bool Changed = false;
Pedro Artigasd1abec32012-12-05 17:12:22 +00001566
1567 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001568 Changed |= getContainedPass(Index)->doFinalization(M);
Pedro Artigas6eda0812012-11-29 17:47:05 +00001569
Devang Patel3799f972006-11-15 01:27:05 +00001570 return Changed;
1571}
1572
Devang Patel06e86562006-12-07 19:39:39 +00001573//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001574// MPPassManager implementation
Devang Patel92c45ee2006-11-07 22:03:15 +00001575
Dan Gohman95df6192010-08-12 23:50:08 +00001576/// Execute all of the passes scheduled for execution by invoking
1577/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel92c45ee2006-11-07 22:03:15 +00001578/// the module, and if so, return true.
1579bool
Devang Patel5f4ddf52006-12-19 19:46:59 +00001580MPPassManager::runOnModule(Module &M) {
Devang Patel92c45ee2006-11-07 22:03:15 +00001581 bool Changed = false;
Devang Patelc1d6e1f2006-11-14 01:23:29 +00001582
Torok Edwin1970a892009-06-29 18:49:09 +00001583 // Initialize on-the-fly passes
1584 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1585 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1586 I != E; ++I) {
1587 FunctionPassManagerImpl *FPP = I->second;
1588 Changed |= FPP->doInitialization(M);
1589 }
1590
Pedro Artigas6eda0812012-11-29 17:47:05 +00001591 // Initialize module passes
1592 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1593 Changed |= getContainedPass(Index)->doInitialization(M);
1594
Devang Patel1554c852006-12-16 00:56:26 +00001595 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1596 ModulePass *MP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001597 bool LocalChanged = false;
Devang Patel1554c852006-12-16 00:56:26 +00001598
Benjamin Kramer69cee612009-12-08 13:07:38 +00001599 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001600 dumpRequiredSet(MP);
Devang Patel693a74e2006-12-14 00:08:04 +00001601
Devang Patel1554c852006-12-16 00:56:26 +00001602 initializeAnalysisImpl(MP);
Devang Patel8e58a1b2006-12-14 00:59:42 +00001603
Chris Lattnerd6f16582009-03-06 06:45:05 +00001604 {
1605 PassManagerPrettyStackEntry X(MP, M);
Chris Lattnera782e752010-03-30 04:03:22 +00001606 TimeRegion PassTimer(getPassTimer(MP));
1607
Dan Gohman16b77212010-03-01 17:34:28 +00001608 LocalChanged |= MP->runOnModule(M);
Chris Lattnerd6f16582009-03-06 06:45:05 +00001609 }
Devang Patel693a74e2006-12-14 00:08:04 +00001610
Dan Gohman16b77212010-03-01 17:34:28 +00001611 Changed |= LocalChanged;
1612 if (LocalChanged)
Dan Gohman97cf759b2008-01-29 12:09:55 +00001613 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramer69cee612009-12-08 13:07:38 +00001614 M.getModuleIdentifier());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001615 dumpPreservedSet(MP);
Dan Gohman95df6192010-08-12 23:50:08 +00001616
Devang Patel58e0ef12007-07-19 18:02:32 +00001617 verifyPreservedAnalysis(MP);
Devang Patel1554c852006-12-16 00:56:26 +00001618 removeNotPreservedAnalysis(MP);
1619 recordAvailableAnalysis(MP);
Benjamin Kramer69cee612009-12-08 13:07:38 +00001620 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel92c45ee2006-11-07 22:03:15 +00001621 }
Torok Edwin1970a892009-06-29 18:49:09 +00001622
Pedro Artigas6eda0812012-11-29 17:47:05 +00001623 // Finalize module passes
Pedro Artigasd1abec32012-12-05 17:12:22 +00001624 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigas6eda0812012-11-29 17:47:05 +00001625 Changed |= getContainedPass(Index)->doFinalization(M);
1626
Torok Edwin1970a892009-06-29 18:49:09 +00001627 // Finalize on-the-fly passes
1628 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1629 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1630 I != E; ++I) {
1631 FunctionPassManagerImpl *FPP = I->second;
1632 // We don't know when is the last time an on-the-fly pass is run,
1633 // so we need to releaseMemory / finalize here
1634 FPP->releaseMemoryOnTheFly();
1635 Changed |= FPP->doFinalization(M);
1636 }
Pedro Artigas6eda0812012-11-29 17:47:05 +00001637
Devang Patel92c45ee2006-11-07 22:03:15 +00001638 return Changed;
1639}
1640
Devang Patel569a6fd2007-04-16 20:12:57 +00001641/// Add RequiredPass into list of lower level passes required by pass P.
1642/// RequiredPass is run on the fly by Pass Manager when P requests it
1643/// through getAnalysis interface.
1644void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001645 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1646 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohman95df6192010-08-12 23:50:08 +00001647 assert((P->getPotentialPassManagerType() <
Chris Lattnerf9574362009-03-06 05:53:14 +00001648 RequiredPass->getPotentialPassManagerType()) &&
1649 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patel569a6fd2007-04-16 20:12:57 +00001650
Devang Pateldfa1ec32007-04-26 17:50:19 +00001651 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel0ed8df32007-04-16 20:27:05 +00001652 if (!FPP) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001653 FPP = new FunctionPassManagerImpl();
Devang Pateldfa1ec32007-04-26 17:50:19 +00001654 // FPP is the top level manager.
1655 FPP->setTopLevelManager(FPP);
1656
Devang Patel0ed8df32007-04-16 20:27:05 +00001657 OnTheFlyManagers[P] = FPP;
1658 }
Devang Pateldfa1ec32007-04-26 17:50:19 +00001659 FPP->add(RequiredPass);
Devang Patel0ed8df32007-04-16 20:27:05 +00001660
Devang Pateldfa1ec32007-04-26 17:50:19 +00001661 // Register P as the last user of RequiredPass.
Devang Patelc67d1842011-09-13 21:13:29 +00001662 if (RequiredPass) {
1663 SmallVector<Pass *, 1> LU;
1664 LU.push_back(RequiredPass);
1665 FPP->setLastUser(LU, P);
1666 }
Devang Patel569a6fd2007-04-16 20:12:57 +00001667}
Devang Patel0ed8df32007-04-16 20:27:05 +00001668
Dan Gohman95df6192010-08-12 23:50:08 +00001669/// Return function pass corresponding to PassInfo PI, that is
Devang Patel0ed8df32007-04-16 20:27:05 +00001670/// required by module pass MP. Instantiate analysis pass, by using
1671/// its runOnFunction() for function F.
Owen Anderson90c579d2010-08-06 18:33:48 +00001672Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Pateldfa1ec32007-04-26 17:50:19 +00001673 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattnerf9574362009-03-06 05:53:14 +00001674 assert(FPP && "Unable to find on the fly pass");
Dan Gohman95df6192010-08-12 23:50:08 +00001675
Torok Edwin1970a892009-06-29 18:49:09 +00001676 FPP->releaseMemoryOnTheFly();
Devang Pateldfa1ec32007-04-26 17:50:19 +00001677 FPP->run(F);
Chris Lattner77c95ed2010-01-22 05:37:10 +00001678 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel0ed8df32007-04-16 20:27:05 +00001679}
1680
1681
Devang Patel06e86562006-12-07 19:39:39 +00001682//===----------------------------------------------------------------------===//
1683// PassManagerImpl implementation
Owen Anderson40b6fdb2012-11-15 00:14:15 +00001684
Devang Patel37a6f792006-12-13 00:09:23 +00001685//
Devang Patelb30803b2006-11-07 22:23:34 +00001686/// run - Execute all of the passes scheduled for execution. Keep track of
1687/// whether any of the passes modifies the module, and if so, return true.
Devang Patel5f4ddf52006-12-19 19:46:59 +00001688bool PassManagerImpl::run(Module &M) {
Devang Patelb30803b2006-11-07 22:23:34 +00001689 bool Changed = false;
Devang Patel8e58a1b2006-12-14 00:59:42 +00001690 TimingInfo::createTheTimeInfo();
1691
Devang Patelc32cf542006-12-13 22:10:00 +00001692 dumpArguments();
Devang Patel5f4ddf52006-12-19 19:46:59 +00001693 dumpPasses();
Devang Patel45dc02d2006-12-13 20:03:48 +00001694
Pedro Artigasd1abec32012-12-05 17:12:22 +00001695 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1696 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1697 E = IPV.end(); I != E; ++I) {
1698 Changed |= (*I)->doInitialization(M);
1699 }
1700
Devang Patel1336a6b2006-12-21 00:16:50 +00001701 initializeAllAnalysisInfo();
Chris Lattnerd6f16582009-03-06 06:45:05 +00001702 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1703 Changed |= getContainedManager(Index)->runOnModule(M);
Pedro Artigasd1abec32012-12-05 17:12:22 +00001704
1705 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1706 E = IPV.end(); I != E; ++I) {
1707 Changed |= (*I)->doFinalization(M);
1708 }
1709
Devang Patelb30803b2006-11-07 22:23:34 +00001710 return Changed;
1711}
Devang Patel5a39b2e2006-11-08 10:29:57 +00001712
Devang Patel06e86562006-12-07 19:39:39 +00001713//===----------------------------------------------------------------------===//
1714// PassManager implementation
1715
Devang Patel5a39b2e2006-11-08 10:29:57 +00001716/// Create new pass manager
Devang Patel31626912006-12-13 02:36:01 +00001717PassManager::PassManager() {
Andrew Trick0e122d12011-08-29 17:07:00 +00001718 PM = new PassManagerImpl();
Devang Pateldff33ef2006-12-12 22:02:16 +00001719 // PM is the top level manager
1720 PM->setTopLevelManager(PM);
Devang Patel5a39b2e2006-11-08 10:29:57 +00001721}
1722
Devang Patel31626912006-12-13 02:36:01 +00001723PassManager::~PassManager() {
Devang Patel37a6f792006-12-13 00:09:23 +00001724 delete PM;
1725}
1726
Devang Patel5a39b2e2006-11-08 10:29:57 +00001727/// add - Add a pass to the queue of passes to run. This passes ownership of
1728/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1729/// will be destroyed as well, so there is no need to delete the pass. This
1730/// implies that all passes MUST be allocated with 'new'.
Chris Lattnerf9574362009-03-06 05:53:14 +00001731void PassManager::add(Pass *P) {
Andrew Trick11e43292012-02-01 07:16:20 +00001732 PM->add(P);
Devang Patel5a39b2e2006-11-08 10:29:57 +00001733}
1734
1735/// run - Execute all of the passes scheduled for execution. Keep track of
1736/// whether any of the passes modifies the module, and if so, return true.
Chris Lattnerf9574362009-03-06 05:53:14 +00001737bool PassManager::run(Module &M) {
Devang Patel5a39b2e2006-11-08 10:29:57 +00001738 return PM->run(M);
1739}
1740
Devang Patel8e58a1b2006-12-14 00:59:42 +00001741//===----------------------------------------------------------------------===//
Eli Bendersky16299652013-04-03 15:33:45 +00001742// TimingInfo implementation
1743
Devang Patel8e58a1b2006-12-14 00:59:42 +00001744bool llvm::TimePassesIsEnabled = false;
1745static cl::opt<bool,true>
1746EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1747 cl::desc("Time each pass, printing elapsed time for each on exit"));
1748
1749// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1750// a non null value (if the -time-passes option is enabled) or it leaves it
1751// null. It may be called multiple times.
1752void TimingInfo::createTheTimeInfo() {
1753 if (!TimePassesIsEnabled || TheTimeInfo) return;
1754
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001755 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patel8e58a1b2006-12-14 00:59:42 +00001756 // This guarantees that the object will be constructed before static globals,
1757 // thus it will be destroyed before them.
1758 static ManagedStatic<TimingInfo> TTI;
1759 TheTimeInfo = &*TTI;
1760}
1761
Devang Patelc874eb52007-01-29 23:10:37 +00001762/// If TimingInfo is enabled then start pass timer.
Chris Lattnera782e752010-03-30 04:03:22 +00001763Timer *llvm::getPassTimer(Pass *P) {
Dan Gohman95df6192010-08-12 23:50:08 +00001764 if (TheTimeInfo)
Chris Lattnera782e752010-03-30 04:03:22 +00001765 return TheTimeInfo->getPassTimer(P);
Dan Gohman5c12ada2009-09-28 00:07:05 +00001766 return 0;
Devang Patelc874eb52007-01-29 23:10:37 +00001767}
1768
Devang Patel09e6e432007-01-08 19:29:38 +00001769//===----------------------------------------------------------------------===//
1770// PMStack implementation
1771//
Devang Patel36bcb822007-01-11 22:15:30 +00001772
Devang Patel09e6e432007-01-08 19:29:38 +00001773// Pop Pass Manager from the stack and clear its analysis info.
1774void PMStack::pop() {
1775
1776 PMDataManager *Top = this->top();
1777 Top->initializeAnalysisInfo();
1778
1779 S.pop_back();
1780}
1781
1782// Push PM on the stack and set its top level manager.
Dan Gohmanc2f12ab2008-03-13 01:21:31 +00001783void PMStack::push(PMDataManager *PM) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001784 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick0e122d12011-08-29 17:07:00 +00001785 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel09e6e432007-01-08 19:29:38 +00001786
Chris Lattnerf9574362009-03-06 05:53:14 +00001787 if (!this->empty()) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001788 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1789 && "pushing bad pass manager to PMStack");
Chris Lattnerf9574362009-03-06 05:53:14 +00001790 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel09e6e432007-01-08 19:29:38 +00001791
Chris Lattnerf9574362009-03-06 05:53:14 +00001792 assert(TPM && "Unable to find top level manager");
Devang Patel97149732007-01-11 00:19:00 +00001793 TPM->addIndirectPassManager(PM);
1794 PM->setTopLevelManager(TPM);
Andrew Trick0e122d12011-08-29 17:07:00 +00001795 PM->setDepth(this->top()->getDepth()+1);
Craig Topper97fe3d92013-02-06 06:50:38 +00001796 } else {
Benjamin Kramer4a3d0a52011-08-29 18:14:15 +00001797 assert((PM->getPassManagerType() == PMT_ModulePassManager
1798 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick0e122d12011-08-29 17:07:00 +00001799 && "pushing bad pass manager to PMStack");
1800 PM->setDepth(1);
Devang Patel97149732007-01-11 00:19:00 +00001801 }
1802
Devang Patel97149732007-01-11 00:19:00 +00001803 S.push_back(PM);
1804}
1805
1806// Dump content of the pass manager stack.
Dan Gohman12376a82010-08-07 01:04:15 +00001807void PMStack::dump() const {
1808 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattnerf9574362009-03-06 05:53:14 +00001809 E = S.end(); I != E; ++I)
Benjamin Kramer3dedf7e2011-08-29 18:14:17 +00001810 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattnerf9574362009-03-06 05:53:14 +00001811
Devang Patel97149732007-01-11 00:19:00 +00001812 if (!S.empty())
Benjamin Kramer3dedf7e2011-08-29 18:14:17 +00001813 dbgs() << '\n';
Devang Patel09e6e432007-01-08 19:29:38 +00001814}
1815
Devang Patel09e6e432007-01-08 19:29:38 +00001816/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohman95df6192010-08-12 23:50:08 +00001817/// add self into that manager.
1818void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001819 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001820 // Find Module Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001821 while (!PMS.empty()) {
Devang Patel44b0d292007-01-17 21:19:23 +00001822 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1823 if (TopPMType == PreferredType)
1824 break; // We found desired pass manager
1825 else if (TopPMType > PMT_ModulePassManager)
Devang Patel09e6e432007-01-08 19:29:38 +00001826 PMS.pop(); // Pop children pass managers
Devang Patel6b9420e2007-01-11 19:59:06 +00001827 else
1828 break;
Devang Patel09e6e432007-01-08 19:29:38 +00001829 }
Devang Patelbd6dc7a2008-09-09 21:38:40 +00001830 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel44b0d292007-01-17 21:19:23 +00001831 PMS.top()->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001832}
1833
Devang Patel9d133e12007-01-16 21:43:18 +00001834/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001835/// in the PM Stack and add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001836void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001837 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001838
Andrew Trick11e43292012-02-01 07:16:20 +00001839 // Find Function Pass Manager
Chris Lattner77c95ed2010-01-22 05:37:10 +00001840 while (!PMS.empty()) {
Devang Patel6b9420e2007-01-11 19:59:06 +00001841 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1842 PMS.pop();
Devang Patel09e6e432007-01-08 19:29:38 +00001843 else
Dan Gohman95df6192010-08-12 23:50:08 +00001844 break;
Devang Patel9d133e12007-01-16 21:43:18 +00001845 }
Devang Patel9d133e12007-01-16 21:43:18 +00001846
Chris Lattner77c95ed2010-01-22 05:37:10 +00001847 // Create new Function Pass Manager if needed.
1848 FPPassManager *FPP;
1849 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1850 FPP = (FPPassManager *)PMS.top();
1851 } else {
Devang Patel9d133e12007-01-16 21:43:18 +00001852 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1853 PMDataManager *PMD = PMS.top();
1854
1855 // [1] Create new Function Pass Manager
Andrew Trick0e122d12011-08-29 17:07:00 +00001856 FPP = new FPPassManager();
Devang Patelbed7e682008-03-20 01:09:53 +00001857 FPP->populateInheritedAnalysis(PMS);
Devang Patel9d133e12007-01-16 21:43:18 +00001858
1859 // [2] Set up new manager's top level manager
1860 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1861 TPM->addIndirectPassManager(FPP);
1862
1863 // [3] Assign manager to manage this new manager. This may create
1864 // and push new managers into PMS
Devang Patel0938f742008-09-09 17:56:50 +00001865 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel9d133e12007-01-16 21:43:18 +00001866
1867 // [4] Push new manager into PMS
1868 PMS.push(FPP);
Devang Patel09e6e432007-01-08 19:29:38 +00001869 }
1870
Devang Patel9d133e12007-01-16 21:43:18 +00001871 // Assign FPP as the manager of this pass.
1872 FPP->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001873}
1874
Devang Patel9d133e12007-01-16 21:43:18 +00001875/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001876/// in the PM Stack and add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001877void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001878 PassManagerType PreferredType) {
Chris Lattner77c95ed2010-01-22 05:37:10 +00001879 BBPassManager *BBP;
Devang Patel09e6e432007-01-08 19:29:38 +00001880
Devang Patel97149732007-01-11 00:19:00 +00001881 // Basic Pass Manager is a leaf pass manager. It does not handle
1882 // any other pass manager.
Dan Gohman95df6192010-08-12 23:50:08 +00001883 if (!PMS.empty() &&
Chris Lattner77c95ed2010-01-22 05:37:10 +00001884 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1885 BBP = (BBPassManager *)PMS.top();
1886 } else {
1887 // If leaf manager is not Basic Block Pass manager then create new
1888 // basic Block Pass manager.
Devang Patel9d133e12007-01-16 21:43:18 +00001889 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1890 PMDataManager *PMD = PMS.top();
1891
1892 // [1] Create new Basic Block Manager
Andrew Trick0e122d12011-08-29 17:07:00 +00001893 BBP = new BBPassManager();
Devang Patel9d133e12007-01-16 21:43:18 +00001894
1895 // [2] Set up new manager's top level manager
1896 // Basic Block Pass Manager does not live by itself
1897 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1898 TPM->addIndirectPassManager(BBP);
1899
Devang Patel97149732007-01-11 00:19:00 +00001900 // [3] Assign manager to manage this new manager. This may create
1901 // and push new managers into PMS
David Greenee89f1c42010-05-10 20:24:27 +00001902 BBP->assignPassManager(PMS, PreferredType);
Devang Patel97149732007-01-11 00:19:00 +00001903
Devang Patel9d133e12007-01-16 21:43:18 +00001904 // [4] Push new manager into PMS
1905 PMS.push(BBP);
1906 }
Devang Patel09e6e432007-01-08 19:29:38 +00001907
Devang Patel9d133e12007-01-16 21:43:18 +00001908 // Assign BBP as the manager of this pass.
1909 BBP->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001910}
1911
Dan Gohman580b8992008-03-11 16:41:42 +00001912PassManagerBase::~PassManagerBase() {}