blob: 4f7984e0889aaf5d59b94e178fd67c7273056484 [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";
629 }
630 else {
631 dbgs() << "\t" << "Error: Required pass not found! Possible causes:" << "\n";
632 dbgs() << "\t\t" << "- Pass misconfiguration (e.g.: missing macros)" << "\n";
633 dbgs() << "\t\t" << "- Corruption of the global PassRegistry" << "\n";
634 }
635 }
636 }
637
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000638 assert(PI && "Expected required passes to be initialized");
Owen Anderson90c579d2010-08-06 18:33:48 +0000639 AnalysisPass = PI->createPass();
Devang Patel488dc672008-08-14 23:07:48 +0000640 if (P->getPotentialPassManagerType () ==
641 AnalysisPass->getPotentialPassManagerType())
642 // Schedule analysis pass that is managed by the same pass manager.
643 schedulePass(AnalysisPass);
644 else if (P->getPotentialPassManagerType () >
645 AnalysisPass->getPotentialPassManagerType()) {
646 // Schedule analysis pass that is managed by a new manager.
647 schedulePass(AnalysisPass);
Dan Gohman3620ff92010-08-16 22:57:28 +0000648 // Recheck analysis passes to ensure that required analyses that
Devang Patel488dc672008-08-14 23:07:48 +0000649 // are already checked are still available.
650 checkAnalysis = true;
651 }
652 else
Dan Gohman95df6192010-08-12 23:50:08 +0000653 // Do not schedule this analysis. Lower level analsyis
Devang Patel488dc672008-08-14 23:07:48 +0000654 // passes are run on the fly.
655 delete AnalysisPass;
656 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000657 }
658 }
659
660 // Now all required passes are available.
Andrew Trick11e43292012-02-01 07:16:20 +0000661 if (ImmutablePass *IP = P->getAsImmutablePass()) {
662 // P is a immutable pass and it will be managed by this
663 // top level manager. Set up analysis resolver to connect them.
664 PMDataManager *DM = getAsPMDataManager();
665 AnalysisResolver *AR = new AnalysisResolver(*DM);
666 P->setResolver(AR);
667 DM->initializeAnalysisImpl(P);
668 addImmutablePass(IP);
669 DM->recordAvailableAnalysis(IP);
670 return;
671 }
672
673 if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) {
674 Pass *PP = P->createPrinterPass(
675 dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***");
676 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
677 }
678
679 // Add the requested pass to the best available pass manager.
680 P->assignPassManager(activeStack, getTopLevelPassManagerType());
681
682 if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) {
683 Pass *PP = P->createPrinterPass(
684 dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***");
685 PP->assignPassManager(activeStack, getTopLevelPassManagerType());
686 }
Devang Patel1b8d0152006-12-12 22:35:25 +0000687}
688
689/// Find the pass that implements Analysis AID. Search immutable
690/// passes and all pass managers. If desired pass is not found
691/// then return NULL.
692Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) {
693
Devang Pateld0fa16c2006-12-12 22:50:05 +0000694 // Check pass managers
Dan Gohmanebb18342010-10-12 00:11:18 +0000695 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Dan Gohman7c347302010-10-11 23:19:01 +0000696 E = PassManagers.end(); I != E; ++I)
697 if (Pass *P = (*I)->findAnalysisPass(AID, false))
698 return P;
Devang Pateld0fa16c2006-12-12 22:50:05 +0000699
700 // Check other pass managers
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000701 for (SmallVectorImpl<PMDataManager *>::iterator
Chris Lattnerf9574362009-03-06 05:53:14 +0000702 I = IndirectPassManagers.begin(),
Dan Gohman7c347302010-10-11 23:19:01 +0000703 E = IndirectPassManagers.end(); I != E; ++I)
704 if (Pass *P = (*I)->findAnalysisPass(AID, false))
705 return P;
Devang Pateld0fa16c2006-12-12 22:50:05 +0000706
Dan Gohman7c347302010-10-11 23:19:01 +0000707 // Check the immutable passes. Iterate in reverse order so that we find
708 // the most recently registered passes first.
709 for (SmallVector<ImmutablePass *, 8>::reverse_iterator I =
710 ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) {
Owen Anderson90c579d2010-08-06 18:33:48 +0000711 AnalysisID PI = (*I)->getPassID();
Devang Patel1b8d0152006-12-12 22:35:25 +0000712 if (PI == AID)
Dan Gohman7c347302010-10-11 23:19:01 +0000713 return *I;
Devang Patel1b8d0152006-12-12 22:35:25 +0000714
715 // If Pass not found then check the interfaces implemented by Immutable Pass
Dan Gohman7c347302010-10-11 23:19:01 +0000716 const PassInfo *PassInf =
717 PassRegistry::getPassRegistry()->getPassInfo(PI);
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000718 assert(PassInf && "Expected all immutable passes to be initialized");
Dan Gohman7c347302010-10-11 23:19:01 +0000719 const std::vector<const PassInfo*> &ImmPI =
720 PassInf->getInterfacesImplemented();
721 for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(),
722 EE = ImmPI.end(); II != EE; ++II) {
723 if ((*II)->getTypeInfo() == AID)
724 return *I;
Devang Patel1b8d0152006-12-12 22:35:25 +0000725 }
726 }
727
Dan Gohman7c347302010-10-11 23:19:01 +0000728 return 0;
Devang Patel1b8d0152006-12-12 22:35:25 +0000729}
730
Devang Patelebc09222006-12-12 23:34:33 +0000731// Print passes managed by this top level manager.
Devang Patela52035a2006-12-15 20:13:01 +0000732void PMTopLevelManager::dumpPasses() const {
Devang Patelebc09222006-12-12 23:34:33 +0000733
Devang Patel26426942007-01-17 20:33:36 +0000734 if (PassDebugging < Structure)
Devang Patel5f4ddf52006-12-19 19:46:59 +0000735 return;
736
Devang Patelebc09222006-12-12 23:34:33 +0000737 // Print out the immutable passes
738 for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) {
Dan Gohman8a757ae2010-08-19 01:29:07 +0000739 ImmutablePasses[i]->dumpPassStructure(0);
Devang Patelebc09222006-12-12 23:34:33 +0000740 }
Dan Gohman95df6192010-08-12 23:50:08 +0000741
Dan Gohman8a757ae2010-08-19 01:29:07 +0000742 // Every class that derives from PMDataManager also derives from Pass
743 // (sometimes indirectly), but there's no inheritance relationship
744 // between PMDataManager and Pass, so we have to getAsPass to get
745 // from a PMDataManager* to a Pass*.
Devang Patel78766ff2008-08-12 15:44:31 +0000746 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Devang Patelebc09222006-12-12 23:34:33 +0000747 E = PassManagers.end(); I != E; ++I)
Dan Gohman8a757ae2010-08-19 01:29:07 +0000748 (*I)->getAsPass()->dumpPassStructure(1);
Devang Patelebc09222006-12-12 23:34:33 +0000749}
750
Devang Patela52035a2006-12-15 20:13:01 +0000751void PMTopLevelManager::dumpArguments() const {
Devang Patelc32cf542006-12-13 22:10:00 +0000752
Devang Patel26426942007-01-17 20:33:36 +0000753 if (PassDebugging < Arguments)
Devang Patelc32cf542006-12-13 22:10:00 +0000754 return;
755
David Greene170c48a2010-01-05 01:30:02 +0000756 dbgs() << "Pass Arguments: ";
Dan Gohman67a84f12010-11-11 16:32:17 +0000757 for (SmallVector<ImmutablePass *, 8>::const_iterator I =
758 ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
759 if (const PassInfo *PI =
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000760 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) {
761 assert(PI && "Expected all immutable passes to be initialized");
Dan Gohman67a84f12010-11-11 16:32:17 +0000762 if (!PI->isAnalysisGroup())
763 dbgs() << " -" << PI->getPassArgument();
Andrew Trickc5d93bb2011-06-03 00:48:58 +0000764 }
Devang Patel78766ff2008-08-12 15:44:31 +0000765 for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(),
Chris Lattnerd6f16582009-03-06 06:45:05 +0000766 E = PassManagers.end(); I != E; ++I)
767 (*I)->dumpPassArguments();
David Greene170c48a2010-01-05 01:30:02 +0000768 dbgs() << "\n";
Devang Patelc32cf542006-12-13 22:10:00 +0000769}
770
Devang Patel1336a6b2006-12-21 00:16:50 +0000771void PMTopLevelManager::initializeAllAnalysisInfo() {
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000772 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Chris Lattnerd6f16582009-03-06 06:45:05 +0000773 E = PassManagers.end(); I != E; ++I)
774 (*I)->initializeAnalysisInfo();
Dan Gohman95df6192010-08-12 23:50:08 +0000775
Devang Patel1336a6b2006-12-21 00:16:50 +0000776 // Initailize other pass managers
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000777 for (SmallVectorImpl<PMDataManager *>::iterator
Dan Gohman95df6192010-08-12 23:50:08 +0000778 I = IndirectPassManagers.begin(), E = IndirectPassManagers.end();
779 I != E; ++I)
Devang Patel1336a6b2006-12-21 00:16:50 +0000780 (*I)->initializeAnalysisInfo();
Devang Patel721e59c2008-08-12 00:26:16 +0000781
Chris Lattnerf9574362009-03-06 05:53:14 +0000782 for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
Devang Patel721e59c2008-08-12 00:26:16 +0000783 DME = LastUser.end(); DMI != DME; ++DMI) {
Dan Gohman95df6192010-08-12 23:50:08 +0000784 DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI =
Devang Patel721e59c2008-08-12 00:26:16 +0000785 InversedLastUser.find(DMI->second);
786 if (InvDMI != InversedLastUser.end()) {
787 SmallPtrSet<Pass *, 8> &L = InvDMI->second;
788 L.insert(DMI->first);
789 } else {
790 SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
791 InversedLastUser[DMI->second] = L;
792 }
793 }
Devang Patel1336a6b2006-12-21 00:16:50 +0000794}
795
Devang Patelab7752c2007-01-12 18:52:44 +0000796/// Destructor
797PMTopLevelManager::~PMTopLevelManager() {
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000798 for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(),
Devang Patelab7752c2007-01-12 18:52:44 +0000799 E = PassManagers.end(); I != E; ++I)
800 delete *I;
Dan Gohman95df6192010-08-12 23:50:08 +0000801
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000802 for (SmallVectorImpl<ImmutablePass *>::iterator
Devang Patelab7752c2007-01-12 18:52:44 +0000803 I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
804 delete *I;
Devang Patel3b8a9062008-08-11 21:13:39 +0000805
806 for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
Chris Lattnerf9574362009-03-06 05:53:14 +0000807 DME = AnUsageMap.end(); DMI != DME; ++DMI)
808 delete DMI->second;
Devang Patelab7752c2007-01-12 18:52:44 +0000809}
810
Devang Patel1b8d0152006-12-12 22:35:25 +0000811//===----------------------------------------------------------------------===//
Devang Patel419f0e92006-12-07 18:36:24 +0000812// PMDataManager implementation
Devang Patel889739c2006-11-07 22:35:17 +0000813
Devang Patelb8526162006-11-11 01:10:19 +0000814/// Augement AvailableAnalysis by adding analysis made available by pass P.
Devang Patelf32b4dd2006-12-07 19:33:53 +0000815void PMDataManager::recordAvailableAnalysis(Pass *P) {
Owen Anderson90c579d2010-08-06 18:33:48 +0000816 AnalysisID PI = P->getPassID();
Dan Gohman95df6192010-08-12 23:50:08 +0000817
Chris Lattnerf9574362009-03-06 05:53:14 +0000818 AvailableAnalysis[PI] = P;
Dan Gohman95df6192010-08-12 23:50:08 +0000819
Dan Gohman9e2f6282010-08-12 23:46:28 +0000820 assert(!AvailableAnalysis.empty());
Devang Patelb8526162006-11-11 01:10:19 +0000821
Dan Gohman95df6192010-08-12 23:50:08 +0000822 // This pass is the current implementation of all of the interfaces it
823 // implements as well.
Owen Anderson90c579d2010-08-06 18:33:48 +0000824 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI);
825 if (PInf == 0) return;
826 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson2dcacab2010-07-20 16:55:05 +0000827 for (unsigned i = 0, e = II.size(); i != e; ++i)
Owen Anderson90c579d2010-08-06 18:33:48 +0000828 AvailableAnalysis[II[i]->getTypeInfo()] = P;
Devang Patelb8526162006-11-11 01:10:19 +0000829}
830
Devang Patel7b65dd92007-03-06 17:52:53 +0000831// Return true if P preserves high level analysis used by other
832// passes managed by this manager
833bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +0000834 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
Devang Patel3b8a9062008-08-11 21:13:39 +0000835 if (AnUsage->getPreservesAll())
Devang Patel7b65dd92007-03-06 17:52:53 +0000836 return true;
Dan Gohman95df6192010-08-12 23:50:08 +0000837
Devang Patel3b8a9062008-08-11 21:13:39 +0000838 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000839 for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(),
Devang Patel7b65dd92007-03-06 17:52:53 +0000840 E = HigherLevelAnalysis.end(); I != E; ++I) {
841 Pass *P1 = *I;
Chris Lattner5e664b82010-01-22 04:55:08 +0000842 if (P1->getAsImmutablePass() == 0 &&
Dan Gohman97cf759b2008-01-29 12:09:55 +0000843 std::find(PreservedSet.begin(), PreservedSet.end(),
Dan Gohman95df6192010-08-12 23:50:08 +0000844 P1->getPassID()) ==
Devang Pateld46825c2007-03-08 19:05:01 +0000845 PreservedSet.end())
846 return false;
Devang Patel7b65dd92007-03-06 17:52:53 +0000847 }
Dan Gohman95df6192010-08-12 23:50:08 +0000848
Devang Patel7b65dd92007-03-06 17:52:53 +0000849 return true;
850}
851
Chris Lattnere2c5ecd2008-08-07 07:34:50 +0000852/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
Devang Patel58e0ef12007-07-19 18:02:32 +0000853void PMDataManager::verifyPreservedAnalysis(Pass *P) {
Chris Lattnere2c5ecd2008-08-07 07:34:50 +0000854 // Don't do this unless assertions are enabled.
855#ifdef NDEBUG
856 return;
857#endif
Devang Patel3b8a9062008-08-11 21:13:39 +0000858 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
859 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patel889739c2006-11-07 22:35:17 +0000860
Devang Patel9750b5d2007-07-19 05:36:09 +0000861 // Verify preserved analysis
Chris Lattnerfc65d382008-08-08 05:33:04 +0000862 for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
Devang Patel58e0ef12007-07-19 18:02:32 +0000863 E = PreservedSet.end(); I != E; ++I) {
864 AnalysisID AID = *I;
Dan Gohman9450b0e2009-09-28 00:27:48 +0000865 if (Pass *AP = findAnalysisPass(AID, true)) {
Chris Lattnera782e752010-03-30 04:03:22 +0000866 TimeRegion PassTimer(getPassTimer(AP));
Devang Patel58e0ef12007-07-19 18:02:32 +0000867 AP->verifyAnalysis();
Dan Gohman9450b0e2009-09-28 00:27:48 +0000868 }
Devang Patel5b57e722008-07-01 17:44:24 +0000869 }
870}
871
Devang Patel844a3d12008-07-01 19:50:56 +0000872/// Remove Analysis not preserved by Pass P
Devang Patel58e0ef12007-07-19 18:02:32 +0000873void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +0000874 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
875 if (AnUsage->getPreservesAll())
Devang Patel04b4e052006-12-07 20:03:49 +0000876 return;
877
Devang Patel3b8a9062008-08-11 21:13:39 +0000878 const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
Devang Patelb899eed2006-11-14 01:59:59 +0000879 for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
Devang Patel54e247d2006-12-12 23:07:44 +0000880 E = AvailableAnalysis.end(); I != E; ) {
Devang Patel1a803862006-12-15 22:57:49 +0000881 std::map<AnalysisID, Pass*>::iterator Info = I++;
Chris Lattner5e664b82010-01-22 04:55:08 +0000882 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohman95df6192010-08-12 23:50:08 +0000883 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Devang Patele62f7502008-06-03 01:02:16 +0000884 PreservedSet.end()) {
Devang Patel14d65812006-11-11 01:24:55 +0000885 // Remove this analysis
Devang Patele62f7502008-06-03 01:02:16 +0000886 if (PassDebugging >= Details) {
887 Pass *S = Info->second;
David Greene170c48a2010-01-05 01:30:02 +0000888 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
889 dbgs() << S->getPassName() << "'\n";
Devang Patele62f7502008-06-03 01:02:16 +0000890 }
Dan Gohmane1877262008-11-06 21:57:17 +0000891 AvailableAnalysis.erase(Info);
Devang Patele62f7502008-06-03 01:02:16 +0000892 }
Devang Patel14d65812006-11-11 01:24:55 +0000893 }
Dan Gohman95df6192010-08-12 23:50:08 +0000894
Devang Patelfe613902007-03-06 01:55:46 +0000895 // Check inherited analysis also. If P is not preserving analysis
896 // provided by parent manager then remove it here.
897 for (unsigned Index = 0; Index < PMT_Last; ++Index) {
898
899 if (!InheritedAnalysis[Index])
900 continue;
901
Dan Gohman95df6192010-08-12 23:50:08 +0000902 for (std::map<AnalysisID, Pass*>::iterator
Devang Patelfe613902007-03-06 01:55:46 +0000903 I = InheritedAnalysis[Index]->begin(),
904 E = InheritedAnalysis[Index]->end(); I != E; ) {
905 std::map<AnalysisID, Pass *>::iterator Info = I++;
Chris Lattner5e664b82010-01-22 04:55:08 +0000906 if (Info->second->getAsImmutablePass() == 0 &&
Dan Gohman95df6192010-08-12 23:50:08 +0000907 std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) ==
Andreas Neustifter1f6ae812009-12-04 06:58:24 +0000908 PreservedSet.end()) {
Devang Patelfe613902007-03-06 01:55:46 +0000909 // Remove this analysis
Andreas Neustifter1f6ae812009-12-04 06:58:24 +0000910 if (PassDebugging >= Details) {
911 Pass *S = Info->second;
David Greene170c48a2010-01-05 01:30:02 +0000912 dbgs() << " -- '" << P->getPassName() << "' is not preserving '";
913 dbgs() << S->getPassName() << "'\n";
Andreas Neustifter1f6ae812009-12-04 06:58:24 +0000914 }
Devang Pateld46825c2007-03-08 19:05:01 +0000915 InheritedAnalysis[Index]->erase(Info);
Andreas Neustifter1f6ae812009-12-04 06:58:24 +0000916 }
Devang Patelfe613902007-03-06 01:55:46 +0000917 }
918 }
Devang Patel889739c2006-11-07 22:35:17 +0000919}
920
Devang Pateldf1a10e2006-11-14 03:05:08 +0000921/// Remove analysis passes that are not used any longer
Daniel Dunbar2928c832009-11-06 10:58:06 +0000922void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg,
Devang Patel7f997612007-03-05 20:01:30 +0000923 enum PassDebuggingString DBG_STR) {
Devang Patelf9a60ae2006-12-08 00:37:52 +0000924
Devang Pateledbef382007-07-20 18:04:54 +0000925 SmallVector<Pass *, 12> DeadPasses;
Devang Patel0ed8df32007-04-16 20:27:05 +0000926
Devang Patel693941b2007-04-16 20:39:59 +0000927 // If this is a on the fly manager then it does not have TPM.
Devang Patel0ed8df32007-04-16 20:27:05 +0000928 if (!TPM)
929 return;
930
Devang Patelf9a60ae2006-12-08 00:37:52 +0000931 TPM->collectLastUses(DeadPasses, P);
932
Devang Patel8fb6a942008-06-06 17:50:36 +0000933 if (PassDebugging >= Details && !DeadPasses.empty()) {
David Greene170c48a2010-01-05 01:30:02 +0000934 dbgs() << " -*- '" << P->getPassName();
935 dbgs() << "' is the last user of following pass instances.";
936 dbgs() << " Free these instances\n";
Evan Cheng7c9b6522008-06-04 09:13:31 +0000937 }
938
Dan Gohman9b0e47e2010-10-12 00:15:27 +0000939 for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(),
Dan Gohman27a8fb82009-09-27 23:38:27 +0000940 E = DeadPasses.end(); I != E; ++I)
941 freePass(*I, Msg, DBG_STR);
942}
Devang Patel4eeea772006-12-13 23:50:44 +0000943
Daniel Dunbar2928c832009-11-06 10:58:06 +0000944void PMDataManager::freePass(Pass *P, StringRef Msg,
Dan Gohman27a8fb82009-09-27 23:38:27 +0000945 enum PassDebuggingString DBG_STR) {
946 dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg);
Devang Patel4eeea772006-12-13 23:50:44 +0000947
Dan Gohman27a8fb82009-09-27 23:38:27 +0000948 {
949 // If the pass crashes releasing memory, remember this.
950 PassManagerPrettyStackEntry X(P);
Chris Lattnera782e752010-03-30 04:03:22 +0000951 TimeRegion PassTimer(getPassTimer(P));
952
Dan Gohman27a8fb82009-09-27 23:38:27 +0000953 P->releaseMemory();
Dan Gohman27a8fb82009-09-27 23:38:27 +0000954 }
955
Owen Anderson90c579d2010-08-06 18:33:48 +0000956 AnalysisID PI = P->getPassID();
957 if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) {
Dan Gohman27a8fb82009-09-27 23:38:27 +0000958 // Remove the pass itself (if it is not already removed).
959 AvailableAnalysis.erase(PI);
960
961 // Remove all interfaces this pass implements, for which it is also
962 // listed as the available implementation.
Owen Anderson90c579d2010-08-06 18:33:48 +0000963 const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented();
Owen Anderson2dcacab2010-07-20 16:55:05 +0000964 for (unsigned i = 0, e = II.size(); i != e; ++i) {
Devang Patel617fddf2008-10-06 20:36:36 +0000965 std::map<AnalysisID, Pass*>::iterator Pos =
Owen Anderson90c579d2010-08-06 18:33:48 +0000966 AvailableAnalysis.find(II[i]->getTypeInfo());
Dan Gohman27a8fb82009-09-27 23:38:27 +0000967 if (Pos != AvailableAnalysis.end() && Pos->second == P)
Devang Patel617fddf2008-10-06 20:36:36 +0000968 AvailableAnalysis.erase(Pos);
Devang Patel617fddf2008-10-06 20:36:36 +0000969 }
Devang Patelf9a60ae2006-12-08 00:37:52 +0000970 }
Devang Pateldf1a10e2006-11-14 03:05:08 +0000971}
972
Dan Gohman95df6192010-08-12 23:50:08 +0000973/// Add pass P into the PassVector. Update
Devang Patel893a5a62006-11-11 02:04:19 +0000974/// AvailableAnalysis appropriately if ProcessAnalysis is true.
Chris Lattnerf9574362009-03-06 05:53:14 +0000975void PMDataManager::add(Pass *P, bool ProcessAnalysis) {
Devang Patel145e83d2006-12-08 23:53:00 +0000976 // This manager is going to manage pass P. Set up analysis resolver
977 // to connect them.
Devang Patelcde53d32007-01-05 22:47:07 +0000978 AnalysisResolver *AR = new AnalysisResolver(*this);
Devang Patel145e83d2006-12-08 23:53:00 +0000979 P->setResolver(AR);
980
Devang Patelcf5fb2b2007-03-05 22:57:49 +0000981 // If a FunctionPass F is the last user of ModulePass info M
982 // then the F's manager, not F, records itself as a last user of M.
Devang Pateledbef382007-07-20 18:04:54 +0000983 SmallVector<Pass *, 12> TransferLastUses;
Devang Patelcf5fb2b2007-03-05 22:57:49 +0000984
Chris Lattnerf9574362009-03-06 05:53:14 +0000985 if (!ProcessAnalysis) {
986 // Add pass
987 PassVector.push_back(P);
988 return;
Devang Patel893a5a62006-11-11 02:04:19 +0000989 }
Devang Patele2533852006-11-11 01:51:02 +0000990
Chris Lattnerf9574362009-03-06 05:53:14 +0000991 // At the moment, this pass is the last user of all required passes.
992 SmallVector<Pass *, 12> LastUses;
993 SmallVector<Pass *, 8> RequiredPasses;
994 SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable;
995
996 unsigned PDepth = this->getDepth();
997
Dan Gohman95df6192010-08-12 23:50:08 +0000998 collectRequiredAnalysis(RequiredPasses,
Chris Lattnerf9574362009-03-06 05:53:14 +0000999 ReqAnalysisNotAvailable, P);
Dan Gohman9b0e47e2010-10-12 00:15:27 +00001000 for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(),
Chris Lattnerf9574362009-03-06 05:53:14 +00001001 E = RequiredPasses.end(); I != E; ++I) {
1002 Pass *PRequired = *I;
1003 unsigned RDepth = 0;
1004
1005 assert(PRequired->getResolver() && "Analysis Resolver is not set");
1006 PMDataManager &DM = PRequired->getResolver()->getPMDataManager();
1007 RDepth = DM.getDepth();
1008
1009 if (PDepth == RDepth)
1010 LastUses.push_back(PRequired);
1011 else if (PDepth > RDepth) {
1012 // Let the parent claim responsibility of last use
1013 TransferLastUses.push_back(PRequired);
1014 // Keep track of higher level analysis used by this manager.
1015 HigherLevelAnalysis.push_back(PRequired);
Dan Gohman95df6192010-08-12 23:50:08 +00001016 } else
Chris Lattner7a2bdde2011-04-15 05:18:47 +00001017 llvm_unreachable("Unable to accommodate Required Pass");
Chris Lattnerf9574362009-03-06 05:53:14 +00001018 }
1019
1020 // Set P as P's last user until someone starts using P.
1021 // However, if P is a Pass Manager then it does not need
1022 // to record its last user.
Chris Lattner3660eca2010-01-22 05:24:46 +00001023 if (P->getAsPMDataManager() == 0)
Chris Lattnerf9574362009-03-06 05:53:14 +00001024 LastUses.push_back(P);
1025 TPM->setLastUser(LastUses, P);
1026
1027 if (!TransferLastUses.empty()) {
Chris Lattner3660eca2010-01-22 05:24:46 +00001028 Pass *My_PM = getAsPass();
Chris Lattnerf9574362009-03-06 05:53:14 +00001029 TPM->setLastUser(TransferLastUses, My_PM);
1030 TransferLastUses.clear();
1031 }
1032
Dan Gohman3620ff92010-08-16 22:57:28 +00001033 // Now, take care of required analyses that are not available.
Dan Gohman9b0e47e2010-10-12 00:15:27 +00001034 for (SmallVectorImpl<AnalysisID>::iterator
Dan Gohman95df6192010-08-12 23:50:08 +00001035 I = ReqAnalysisNotAvailable.begin(),
Chris Lattnerf9574362009-03-06 05:53:14 +00001036 E = ReqAnalysisNotAvailable.end() ;I != E; ++I) {
Owen Anderson90c579d2010-08-06 18:33:48 +00001037 const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I);
1038 Pass *AnalysisPass = PI->createPass();
Chris Lattnerf9574362009-03-06 05:53:14 +00001039 this->addLowerLevelRequiredPass(P, AnalysisPass);
1040 }
1041
1042 // Take a note of analysis required and made available by this pass.
1043 // Remove the analysis not preserved by this pass
1044 removeNotPreservedAnalysis(P);
1045 recordAvailableAnalysis(P);
1046
Devang Patele2533852006-11-11 01:51:02 +00001047 // Add pass
1048 PassVector.push_back(P);
Devang Patele2533852006-11-11 01:51:02 +00001049}
1050
Devang Patel569a6fd2007-04-16 20:12:57 +00001051
1052/// Populate RP with analysis pass that are required by
1053/// pass P and are available. Populate RP_NotAvail with analysis
1054/// pass that are required by pass P but are not available.
Dan Gohmanebb18342010-10-12 00:11:18 +00001055void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP,
1056 SmallVectorImpl<AnalysisID> &RP_NotAvail,
Devang Patel569a6fd2007-04-16 20:12:57 +00001057 Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +00001058 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1059 const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
Dan Gohman95df6192010-08-12 23:50:08 +00001060 for (AnalysisUsage::VectorType::const_iterator
Chris Lattnerf9574362009-03-06 05:53:14 +00001061 I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) {
Devang Patel569a6fd2007-04-16 20:12:57 +00001062 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohman95df6192010-08-12 23:50:08 +00001063 RP.push_back(AnalysisPass);
Devang Patel569a6fd2007-04-16 20:12:57 +00001064 else
Chris Lattnerf9574362009-03-06 05:53:14 +00001065 RP_NotAvail.push_back(*I);
Devang Patelc17bbb62006-12-07 23:05:44 +00001066 }
Devang Patel27aaab22006-12-12 23:09:32 +00001067
Devang Patel3b8a9062008-08-11 21:13:39 +00001068 const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
Chris Lattnerfc65d382008-08-08 05:33:04 +00001069 for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
Devang Patel27aaab22006-12-12 23:09:32 +00001070 E = IDs.end(); I != E; ++I) {
Devang Patel569a6fd2007-04-16 20:12:57 +00001071 if (Pass *AnalysisPass = findAnalysisPass(*I, true))
Dan Gohman95df6192010-08-12 23:50:08 +00001072 RP.push_back(AnalysisPass);
Devang Patel569a6fd2007-04-16 20:12:57 +00001073 else
Chris Lattnerf9574362009-03-06 05:53:14 +00001074 RP_NotAvail.push_back(*I);
Devang Patel27aaab22006-12-12 23:09:32 +00001075 }
Devang Patelc17bbb62006-12-07 23:05:44 +00001076}
1077
Devang Patel2f42ed62006-11-14 21:49:36 +00001078// All Required analyses should be available to the pass as it runs! Here
1079// we fill in the AnalysisImpls member of the pass so that it can
1080// successfully use the getAnalysis() method to retrieve the
1081// implementations it needs.
1082//
Devang Patel419f0e92006-12-07 18:36:24 +00001083void PMDataManager::initializeAnalysisImpl(Pass *P) {
Devang Patel3b8a9062008-08-11 21:13:39 +00001084 AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
1085
Chris Lattnerfc65d382008-08-08 05:33:04 +00001086 for (AnalysisUsage::VectorType::const_iterator
Devang Patel3b8a9062008-08-11 21:13:39 +00001087 I = AnUsage->getRequiredSet().begin(),
1088 E = AnUsage->getRequiredSet().end(); I != E; ++I) {
Devang Patel69867b52006-12-08 22:30:11 +00001089 Pass *Impl = findAnalysisPass(*I, true);
Devang Patel2f42ed62006-11-14 21:49:36 +00001090 if (Impl == 0)
Devang Patelf4bd76a2007-04-16 20:44:16 +00001091 // This may be analysis pass that is initialized on the fly.
1092 // If that is not the case then it will raise an assert when it is used.
1093 continue;
Devang Patelcde53d32007-01-05 22:47:07 +00001094 AnalysisResolver *AR = P->getResolver();
Chris Lattnerf9574362009-03-06 05:53:14 +00001095 assert(AR && "Analysis Resolver is not set");
Devang Patel298fead2006-12-09 01:11:34 +00001096 AR->addAnalysisImplsPair(*I, Impl);
Devang Patel2f42ed62006-11-14 21:49:36 +00001097 }
1098}
1099
Devang Patel69867b52006-12-08 22:30:11 +00001100/// Find the pass that implements Analysis AID. If desired pass is not found
1101/// then return NULL.
1102Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) {
1103
1104 // Check if AvailableAnalysis map has one entry.
1105 std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID);
1106
1107 if (I != AvailableAnalysis.end())
1108 return I->second;
1109
1110 // Search Parents through TopLevelManager
1111 if (SearchParent)
1112 return TPM->findAnalysisPass(AID);
Dan Gohman95df6192010-08-12 23:50:08 +00001113
Devang Patel5b640e72006-12-09 00:09:12 +00001114 return NULL;
Devang Patel69867b52006-12-08 22:30:11 +00001115}
1116
Devang Patela52035a2006-12-15 20:13:01 +00001117// Print list of passes that are last used by P.
1118void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{
1119
Devang Pateledbef382007-07-20 18:04:54 +00001120 SmallVector<Pass *, 12> LUses;
Devang Patel693941b2007-04-16 20:39:59 +00001121
1122 // If this is a on the fly manager then it does not have TPM.
1123 if (!TPM)
1124 return;
1125
Devang Patela52035a2006-12-15 20:13:01 +00001126 TPM->collectLastUses(LUses, P);
Dan Gohman95df6192010-08-12 23:50:08 +00001127
Dan Gohmanebb18342010-10-12 00:11:18 +00001128 for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(),
Devang Patela52035a2006-12-15 20:13:01 +00001129 E = LUses.end(); I != E; ++I) {
David Greene170c48a2010-01-05 01:30:02 +00001130 llvm::dbgs() << "--" << std::string(Offset*2, ' ');
Dan Gohman8a757ae2010-08-19 01:29:07 +00001131 (*I)->dumpPassStructure(0);
Devang Patela52035a2006-12-15 20:13:01 +00001132 }
1133}
1134
1135void PMDataManager::dumpPassArguments() const {
Dan Gohmanebb18342010-10-12 00:11:18 +00001136 for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(),
Devang Patela52035a2006-12-15 20:13:01 +00001137 E = PassVector.end(); I != E; ++I) {
Chris Lattner3660eca2010-01-22 05:24:46 +00001138 if (PMDataManager *PMD = (*I)->getAsPMDataManager())
Devang Patela52035a2006-12-15 20:13:01 +00001139 PMD->dumpPassArguments();
1140 else
Owen Anderson90c579d2010-08-06 18:33:48 +00001141 if (const PassInfo *PI =
1142 PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID()))
Devang Patela52035a2006-12-15 20:13:01 +00001143 if (!PI->isAnalysisGroup())
David Greene170c48a2010-01-05 01:30:02 +00001144 dbgs() << " -" << PI->getPassArgument();
Devang Patela52035a2006-12-15 20:13:01 +00001145 }
1146}
1147
Chris Lattner417efc82007-08-10 06:17:04 +00001148void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
1149 enum PassDebuggingString S2,
Daniel Dunbar2928c832009-11-06 10:58:06 +00001150 StringRef Msg) {
Devang Patel26426942007-01-17 20:33:36 +00001151 if (PassDebugging < Executions)
Devang Patela52035a2006-12-15 20:13:01 +00001152 return;
David Greene170c48a2010-01-05 01:30:02 +00001153 dbgs() << (void*)this << std::string(getDepth()*2+1, ' ');
Devang Patel7f997612007-03-05 20:01:30 +00001154 switch (S1) {
1155 case EXECUTION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001156 dbgs() << "Executing Pass '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001157 break;
1158 case MODIFICATION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001159 dbgs() << "Made Modification '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001160 break;
1161 case FREEING_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001162 dbgs() << " Freeing Pass '" << P->getPassName();
Devang Patel7f997612007-03-05 20:01:30 +00001163 break;
1164 default:
1165 break;
1166 }
1167 switch (S2) {
1168 case ON_BASICBLOCK_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001169 dbgs() << "' on BasicBlock '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001170 break;
1171 case ON_FUNCTION_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001172 dbgs() << "' on Function '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001173 break;
1174 case ON_MODULE_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001175 dbgs() << "' on Module '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001176 break;
Tobias Grosser65513602010-10-20 01:54:44 +00001177 case ON_REGION_MSG:
1178 dbgs() << "' on Region '" << Msg << "'...\n";
1179 break;
Devang Patel7f997612007-03-05 20:01:30 +00001180 case ON_LOOP_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001181 dbgs() << "' on Loop '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001182 break;
1183 case ON_CG_MSG:
David Greene170c48a2010-01-05 01:30:02 +00001184 dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n";
Devang Patel7f997612007-03-05 20:01:30 +00001185 break;
1186 default:
1187 break;
1188 }
Devang Patela52035a2006-12-15 20:13:01 +00001189}
1190
Chris Lattnerd6f16582009-03-06 06:45:05 +00001191void PMDataManager::dumpRequiredSet(const Pass *P) const {
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001192 if (PassDebugging < Details)
1193 return;
Dan Gohman95df6192010-08-12 23:50:08 +00001194
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001195 AnalysisUsage analysisUsage;
1196 P->getAnalysisUsage(analysisUsage);
1197 dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
1198}
1199
Chris Lattnerd6f16582009-03-06 06:45:05 +00001200void PMDataManager::dumpPreservedSet(const Pass *P) const {
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001201 if (PassDebugging < Details)
1202 return;
Dan Gohman95df6192010-08-12 23:50:08 +00001203
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001204 AnalysisUsage analysisUsage;
1205 P->getAnalysisUsage(analysisUsage);
1206 dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
1207}
1208
Daniel Dunbar2928c832009-11-06 10:58:06 +00001209void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P,
Chris Lattnerd6f16582009-03-06 06:45:05 +00001210 const AnalysisUsage::VectorType &Set) const {
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001211 assert(PassDebugging >= Details);
1212 if (Set.empty())
1213 return;
Roman Divacky59324292012-09-05 22:26:57 +00001214 dbgs() << (const void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
Chris Lattnerd6f16582009-03-06 06:45:05 +00001215 for (unsigned i = 0; i != Set.size(); ++i) {
David Greene170c48a2010-01-05 01:30:02 +00001216 if (i) dbgs() << ',';
Owen Anderson90c579d2010-08-06 18:33:48 +00001217 const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]);
Andrew Trickc5d93bb2011-06-03 00:48:58 +00001218 if (!PInf) {
1219 // Some preserved passes, such as AliasAnalysis, may not be initialized by
1220 // all drivers.
1221 dbgs() << " Uninitialized Pass";
1222 continue;
1223 }
Owen Anderson90c579d2010-08-06 18:33:48 +00001224 dbgs() << ' ' << PInf->getPassName();
Chris Lattnerd6f16582009-03-06 06:45:05 +00001225 }
David Greene170c48a2010-01-05 01:30:02 +00001226 dbgs() << '\n';
Devang Patela52035a2006-12-15 20:13:01 +00001227}
Devang Patelf3dc6d92006-12-08 23:28:54 +00001228
Devang Patel19fe8f92007-07-27 20:06:09 +00001229/// Add RequiredPass into list of lower level passes required by pass P.
1230/// RequiredPass is run on the fly by Pass Manager when P requests it
1231/// through getAnalysis interface.
1232/// This should be handled by specific pass manager.
1233void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
1234 if (TPM) {
1235 TPM->dumpArguments();
1236 TPM->dumpPasses();
1237 }
Devang Patel1cf47cb2008-02-02 01:43:30 +00001238
Dan Gohman95df6192010-08-12 23:50:08 +00001239 // Module Level pass may required Function Level analysis info
1240 // (e.g. dominator info). Pass manager uses on the fly function pass manager
1241 // to provide this on demand. In that case, in Pass manager terminology,
Devang Patel1cf47cb2008-02-02 01:43:30 +00001242 // module level pass is requiring lower level analysis info managed by
1243 // lower level pass manager.
1244
1245 // When Pass manager is not able to order required analysis info, Pass manager
Dan Gohman95df6192010-08-12 23:50:08 +00001246 // checks whether any lower level manager will be able to provide this
Devang Patel1cf47cb2008-02-02 01:43:30 +00001247 // analysis info on demand or not.
Devang Patelc0c33f52008-06-03 01:20:02 +00001248#ifndef NDEBUG
David Greene170c48a2010-01-05 01:30:02 +00001249 dbgs() << "Unable to schedule '" << RequiredPass->getPassName();
1250 dbgs() << "' required by '" << P->getPassName() << "'\n";
Devang Patelc0c33f52008-06-03 01:20:02 +00001251#endif
Torok Edwinc23197a2009-07-14 16:55:14 +00001252 llvm_unreachable("Unable to schedule pass");
Devang Patel19fe8f92007-07-27 20:06:09 +00001253}
1254
Owen Anderson90c579d2010-08-06 18:33:48 +00001255Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) {
Craig Topper50bee422012-02-05 22:14:15 +00001256 llvm_unreachable("Unable to find on the fly pass");
Dan Gohmane407c1d2010-06-21 18:46:45 +00001257}
1258
Devang Patelab7752c2007-01-12 18:52:44 +00001259// Destructor
1260PMDataManager::~PMDataManager() {
Dan Gohmanebb18342010-10-12 00:11:18 +00001261 for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(),
Devang Patelab7752c2007-01-12 18:52:44 +00001262 E = PassVector.end(); I != E; ++I)
1263 delete *I;
Devang Patelab7752c2007-01-12 18:52:44 +00001264}
1265
Devang Patelf3dc6d92006-12-08 23:28:54 +00001266//===----------------------------------------------------------------------===//
1267// NOTE: Is this the right place to define this method ?
Duncan Sands1465d612009-01-28 13:14:17 +00001268// getAnalysisIfAvailable - Return analysis result or null if it doesn't exist.
1269Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const {
Devang Patelf3dc6d92006-12-08 23:28:54 +00001270 return PM.findAnalysisPass(ID, dir);
1271}
1272
Dan Gohman95df6192010-08-12 23:50:08 +00001273Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI,
Devang Patel6b1df0e2007-04-16 20:56:24 +00001274 Function &F) {
1275 return PM.getOnTheFlyPass(P, AnalysisPI, F);
1276}
1277
Devang Patel06e86562006-12-07 19:39:39 +00001278//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001279// BBPassManager implementation
Devang Patel55fd43f2006-11-07 21:31:57 +00001280
Dan Gohman95df6192010-08-12 23:50:08 +00001281/// Execute all of the passes scheduled for execution by invoking
1282/// runOnBasicBlock method. Keep track of whether any of the passes modifies
Devang Patel55fd43f2006-11-07 21:31:57 +00001283/// the function, and if so, return true.
Chris Lattnerd6f16582009-03-06 06:45:05 +00001284bool BBPassManager::runOnFunction(Function &F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +00001285 if (F.isDeclaration())
Devang Patel1fbe2c92006-12-12 23:15:28 +00001286 return false;
1287
Devang Patel3b14fbe2006-12-08 01:38:28 +00001288 bool Changed = doInitialization(F);
Devang Patelc1d6e1f2006-11-14 01:23:29 +00001289
Devang Patel55fd43f2006-11-07 21:31:57 +00001290 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
Devang Patel1554c852006-12-16 00:56:26 +00001291 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1292 BasicBlockPass *BP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001293 bool LocalChanged = false;
Devang Patel017b5d92006-12-14 00:25:06 +00001294
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001295 dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001296 dumpRequiredSet(BP);
Devang Patel017b5d92006-12-14 00:25:06 +00001297
Devang Patel1554c852006-12-16 00:56:26 +00001298 initializeAnalysisImpl(BP);
Devang Patel693a74e2006-12-14 00:08:04 +00001299
Chris Lattnerd6f16582009-03-06 06:45:05 +00001300 {
1301 // If the pass crashes, remember this.
1302 PassManagerPrettyStackEntry X(BP, *I);
Chris Lattnera782e752010-03-30 04:03:22 +00001303 TimeRegion PassTimer(getPassTimer(BP));
1304
Dan Gohman16b77212010-03-01 17:34:28 +00001305 LocalChanged |= BP->runOnBasicBlock(*I);
Chris Lattnerd6f16582009-03-06 06:45:05 +00001306 }
Devang Patel693a74e2006-12-14 00:08:04 +00001307
Dan Gohman16b77212010-03-01 17:34:28 +00001308 Changed |= LocalChanged;
Dan Gohman95df6192010-08-12 23:50:08 +00001309 if (LocalChanged)
Dan Gohman97cf759b2008-01-29 12:09:55 +00001310 dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001311 I->getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001312 dumpPreservedSet(BP);
Devang Patel693a74e2006-12-14 00:08:04 +00001313
Devang Patel58e0ef12007-07-19 18:02:32 +00001314 verifyPreservedAnalysis(BP);
Devang Patel1554c852006-12-16 00:56:26 +00001315 removeNotPreservedAnalysis(BP);
1316 recordAvailableAnalysis(BP);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001317 removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG);
Devang Patel55fd43f2006-11-07 21:31:57 +00001318 }
Chris Lattnerfc23bc72007-08-10 06:22:25 +00001319
Bill Wendling47eb1ea2009-12-25 13:50:18 +00001320 return doFinalization(F) || Changed;
Devang Patel55fd43f2006-11-07 21:31:57 +00001321}
1322
Devang Patel964e45e2006-12-08 00:59:05 +00001323// Implement doInitialization and doFinalization
Duncan Sandse70a6832009-02-13 09:42:34 +00001324bool BBPassManager::doInitialization(Module &M) {
Devang Patel964e45e2006-12-08 00:59:05 +00001325 bool Changed = false;
1326
Chris Lattnerd6f16582009-03-06 06:45:05 +00001327 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1328 Changed |= getContainedPass(Index)->doInitialization(M);
Devang Patel964e45e2006-12-08 00:59:05 +00001329
1330 return Changed;
1331}
1332
Duncan Sandse70a6832009-02-13 09:42:34 +00001333bool BBPassManager::doFinalization(Module &M) {
Devang Patel964e45e2006-12-08 00:59:05 +00001334 bool Changed = false;
1335
Pedro Artigasd1abec32012-12-05 17:12:22 +00001336 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001337 Changed |= getContainedPass(Index)->doFinalization(M);
Devang Patel964e45e2006-12-08 00:59:05 +00001338
1339 return Changed;
1340}
1341
Duncan Sandse70a6832009-02-13 09:42:34 +00001342bool BBPassManager::doInitialization(Function &F) {
Devang Patel964e45e2006-12-08 00:59:05 +00001343 bool Changed = false;
1344
Devang Patel1554c852006-12-16 00:56:26 +00001345 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1346 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +00001347 Changed |= BP->doInitialization(F);
1348 }
1349
1350 return Changed;
1351}
1352
Duncan Sandse70a6832009-02-13 09:42:34 +00001353bool BBPassManager::doFinalization(Function &F) {
Devang Patel964e45e2006-12-08 00:59:05 +00001354 bool Changed = false;
1355
Devang Patel1554c852006-12-16 00:56:26 +00001356 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1357 BasicBlockPass *BP = getContainedPass(Index);
Devang Patel964e45e2006-12-08 00:59:05 +00001358 Changed |= BP->doFinalization(F);
1359 }
1360
1361 return Changed;
1362}
1363
1364
Devang Patel06e86562006-12-07 19:39:39 +00001365//===----------------------------------------------------------------------===//
Devang Patel31626912006-12-13 02:36:01 +00001366// FunctionPassManager implementation
Devang Patel06e86562006-12-07 19:39:39 +00001367
Devang Patelc63592b2006-11-08 10:44:40 +00001368/// Create new Function pass manager
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001369FunctionPassManager::FunctionPassManager(Module *m) : M(m) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001370 FPM = new FunctionPassManagerImpl();
Devang Pateldff33ef2006-12-12 22:02:16 +00001371 // FPM is the top level manager.
1372 FPM->setTopLevelManager(FPM);
Devang Patelb920bd82006-12-12 23:27:37 +00001373
Dan Gohman59ef0152008-03-13 02:08:36 +00001374 AnalysisResolver *AR = new AnalysisResolver(*FPM);
Devang Patelb920bd82006-12-12 23:27:37 +00001375 FPM->setResolver(AR);
Devang Patelcc132cd2006-12-08 18:57:16 +00001376}
1377
Devang Patel31626912006-12-13 02:36:01 +00001378FunctionPassManager::~FunctionPassManager() {
Devang Patel37a6f792006-12-13 00:09:23 +00001379 delete FPM;
1380}
1381
Devang Patelc63592b2006-11-08 10:44:40 +00001382/// add - Add a pass to the queue of passes to run. This passes
1383/// ownership of the Pass to the PassManager. When the
1384/// PassManager_X is destroyed, the pass will be destroyed as well, so
1385/// there is no need to delete the pass. (TODO delete passes.)
1386/// This implies that all passes MUST be allocated with 'new'.
Dan Gohman95df6192010-08-12 23:50:08 +00001387void FunctionPassManager::add(Pass *P) {
Andrew Trick11e43292012-02-01 07:16:20 +00001388 FPM->add(P);
Devang Patelc63592b2006-11-08 10:44:40 +00001389}
1390
Devang Patel214ca232006-11-15 19:39:54 +00001391/// run - Execute all of the passes scheduled for execution. Keep
1392/// track of whether any of the passes modifies the function, and if
1393/// so, return true.
1394///
Devang Patel31626912006-12-13 02:36:01 +00001395bool FunctionPassManager::run(Function &F) {
Nick Lewyckyc6380882010-02-15 21:27:56 +00001396 if (F.isMaterializable()) {
1397 std::string errstr;
Chris Lattnerf88c8562010-04-07 22:41:29 +00001398 if (F.Materialize(&errstr))
Benjamin Kramer1bd73352010-04-08 10:44:28 +00001399 report_fatal_error("Error reading bitcode file: " + Twine(errstr));
Devang Patel214ca232006-11-15 19:39:54 +00001400 }
Devang Patelc4756922006-12-08 22:57:48 +00001401 return FPM->run(F);
Devang Patel214ca232006-11-15 19:39:54 +00001402}
1403
1404
Devang Patel3799f972006-11-15 01:27:05 +00001405/// doInitialization - Run all of the initializers for the function passes.
1406///
Devang Patel31626912006-12-13 02:36:01 +00001407bool FunctionPassManager::doInitialization() {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001408 return FPM->doInitialization(*M);
Devang Patel3799f972006-11-15 01:27:05 +00001409}
1410
Dan Gohman209ee182007-07-30 14:51:13 +00001411/// doFinalization - Run all of the finalizers for the function passes.
Devang Patel3799f972006-11-15 01:27:05 +00001412///
Devang Patel31626912006-12-13 02:36:01 +00001413bool FunctionPassManager::doFinalization() {
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001414 return FPM->doFinalization(*M);
Devang Patel3799f972006-11-15 01:27:05 +00001415}
1416
Devang Patel06e86562006-12-07 19:39:39 +00001417//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001418// FunctionPassManagerImpl implementation
1419//
Duncan Sandse70a6832009-02-13 09:42:34 +00001420bool FunctionPassManagerImpl::doInitialization(Module &M) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001421 bool Changed = false;
1422
Dan Gohman9f9ca732009-11-23 16:24:18 +00001423 dumpArguments();
1424 dumpPasses();
1425
Pedro Artigasd1abec32012-12-05 17:12:22 +00001426 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1427 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1428 E = IPV.end(); I != E; ++I) {
1429 Changed |= (*I)->doInitialization(M);
1430 }
1431
Chris Lattnerd6f16582009-03-06 06:45:05 +00001432 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1433 Changed |= getContainedManager(Index)->doInitialization(M);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001434
1435 return Changed;
1436}
1437
Duncan Sandse70a6832009-02-13 09:42:34 +00001438bool FunctionPassManagerImpl::doFinalization(Module &M) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001439 bool Changed = false;
1440
Pedro Artigasd1abec32012-12-05 17:12:22 +00001441 for (int Index = getNumContainedManagers() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001442 Changed |= getContainedManager(Index)->doFinalization(M);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001443
Pedro Artigasd1abec32012-12-05 17:12:22 +00001444 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1445 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1446 E = IPV.end(); I != E; ++I) {
1447 Changed |= (*I)->doFinalization(M);
1448 }
1449
Devang Patel5f4ddf52006-12-19 19:46:59 +00001450 return Changed;
1451}
1452
Devang Patel9dfa1672009-04-01 22:34:41 +00001453/// cleanup - After running all passes, clean up pass manager cache.
1454void FPPassManager::cleanup() {
1455 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1456 FunctionPass *FP = getContainedPass(Index);
1457 AnalysisResolver *AR = FP->getResolver();
1458 assert(AR && "Analysis Resolver is not set");
1459 AR->clearAnalysisImpls();
1460 }
1461}
1462
Torok Edwin1970a892009-06-29 18:49:09 +00001463void FunctionPassManagerImpl::releaseMemoryOnTheFly() {
1464 if (!wasRun)
1465 return;
1466 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) {
1467 FPPassManager *FPPM = getContainedManager(Index);
1468 for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) {
1469 FPPM->getContainedPass(Index)->releaseMemory();
1470 }
1471 }
Torok Edwin6c839922009-06-29 21:05:10 +00001472 wasRun = false;
Torok Edwin1970a892009-06-29 18:49:09 +00001473}
1474
Devang Patel5f4ddf52006-12-19 19:46:59 +00001475// Execute all the passes managed by this top level manager.
1476// Return true if any function is modified by a pass.
1477bool FunctionPassManagerImpl::run(Function &F) {
Devang Patel5f4ddf52006-12-19 19:46:59 +00001478 bool Changed = false;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001479 TimingInfo::createTheTimeInfo();
1480
Devang Patel1336a6b2006-12-21 00:16:50 +00001481 initializeAllAnalysisInfo();
Chris Lattnerd6f16582009-03-06 06:45:05 +00001482 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1483 Changed |= getContainedManager(Index)->runOnFunction(F);
Devang Patel9dfa1672009-04-01 22:34:41 +00001484
1485 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1486 getContainedManager(Index)->cleanup();
1487
Torok Edwin1970a892009-06-29 18:49:09 +00001488 wasRun = true;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001489 return Changed;
1490}
1491
1492//===----------------------------------------------------------------------===//
1493// FPPassManager implementation
Devang Patel448d27c2006-11-07 21:49:50 +00001494
Devang Patel19974732007-05-03 01:11:54 +00001495char FPPassManager::ID = 0;
Devang Patelab7752c2007-01-12 18:52:44 +00001496/// Print passes managed by this manager
1497void FPPassManager::dumpPassStructure(unsigned Offset) {
Benjamin Kramer962bad72011-10-16 16:30:34 +00001498 dbgs().indent(Offset*2) << "FunctionPass Manager\n";
Devang Patelab7752c2007-01-12 18:52:44 +00001499 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1500 FunctionPass *FP = getContainedPass(Index);
Dan Gohman8a757ae2010-08-19 01:29:07 +00001501 FP->dumpPassStructure(Offset + 1);
Devang Patelab7752c2007-01-12 18:52:44 +00001502 dumpLastUses(FP, Offset+1);
1503 }
1504}
1505
1506
Dan Gohman95df6192010-08-12 23:50:08 +00001507/// Execute all of the passes scheduled for execution by invoking
1508/// runOnFunction method. Keep track of whether any of the passes modifies
Devang Patel448d27c2006-11-07 21:49:50 +00001509/// the function, and if so, return true.
Devang Patel5f4ddf52006-12-19 19:46:59 +00001510bool FPPassManager::runOnFunction(Function &F) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001511 if (F.isDeclaration())
1512 return false;
Devang Patel214ca232006-11-15 19:39:54 +00001513
1514 bool Changed = false;
Devang Patel1fbe2c92006-12-12 23:15:28 +00001515
Devang Patelbed7e682008-03-20 01:09:53 +00001516 // Collect inherited analysis from Module level pass manager.
1517 populateInheritedAnalysis(TPM->activeStack);
Devang Patel1fbe2c92006-12-12 23:15:28 +00001518
Devang Patel1554c852006-12-16 00:56:26 +00001519 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1520 FunctionPass *FP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001521 bool LocalChanged = false;
Devang Patel1554c852006-12-16 00:56:26 +00001522
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001523 dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001524 dumpRequiredSet(FP);
Devang Patel693a74e2006-12-14 00:08:04 +00001525
Devang Patel1554c852006-12-16 00:56:26 +00001526 initializeAnalysisImpl(FP);
Eric Christopher9e7e6092012-03-23 03:54:05 +00001527
Chris Lattnerd6f16582009-03-06 06:45:05 +00001528 {
1529 PassManagerPrettyStackEntry X(FP, F);
Chris Lattnera782e752010-03-30 04:03:22 +00001530 TimeRegion PassTimer(getPassTimer(FP));
Chris Lattnerd6f16582009-03-06 06:45:05 +00001531
Dan Gohman16b77212010-03-01 17:34:28 +00001532 LocalChanged |= FP->runOnFunction(F);
Chris Lattnerd6f16582009-03-06 06:45:05 +00001533 }
Devang Patel693a74e2006-12-14 00:08:04 +00001534
Dan Gohman16b77212010-03-01 17:34:28 +00001535 Changed |= LocalChanged;
1536 if (LocalChanged)
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001537 dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001538 dumpPreservedSet(FP);
Devang Patel693a74e2006-12-14 00:08:04 +00001539
Devang Patel58e0ef12007-07-19 18:02:32 +00001540 verifyPreservedAnalysis(FP);
Devang Patel1554c852006-12-16 00:56:26 +00001541 removeNotPreservedAnalysis(FP);
1542 recordAvailableAnalysis(FP);
Daniel Dunbar93b67e42009-07-26 07:49:05 +00001543 removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG);
Devang Patel214ca232006-11-15 19:39:54 +00001544 }
1545 return Changed;
1546}
1547
Devang Patel5f4ddf52006-12-19 19:46:59 +00001548bool FPPassManager::runOnModule(Module &M) {
Pedro Artigas6eda0812012-11-29 17:47:05 +00001549 bool Changed = false;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001550
Dan Gohmand4271802010-05-11 20:30:00 +00001551 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Bill Wendling7df4f962011-08-08 23:01:10 +00001552 Changed |= runOnFunction(*I);
Devang Patel5f4ddf52006-12-19 19:46:59 +00001553
Pedro Artigas6eda0812012-11-29 17:47:05 +00001554 return Changed;
Devang Patel5f4ddf52006-12-19 19:46:59 +00001555}
1556
Duncan Sandse70a6832009-02-13 09:42:34 +00001557bool FPPassManager::doInitialization(Module &M) {
Devang Patel3799f972006-11-15 01:27:05 +00001558 bool Changed = false;
1559
Chris Lattnerd6f16582009-03-06 06:45:05 +00001560 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1561 Changed |= getContainedPass(Index)->doInitialization(M);
Pedro Artigas6eda0812012-11-29 17:47:05 +00001562
Devang Patel3799f972006-11-15 01:27:05 +00001563 return Changed;
1564}
1565
Duncan Sandse70a6832009-02-13 09:42:34 +00001566bool FPPassManager::doFinalization(Module &M) {
Devang Patel3799f972006-11-15 01:27:05 +00001567 bool Changed = false;
Pedro Artigasd1abec32012-12-05 17:12:22 +00001568
1569 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Chris Lattnerd6f16582009-03-06 06:45:05 +00001570 Changed |= getContainedPass(Index)->doFinalization(M);
Pedro Artigas6eda0812012-11-29 17:47:05 +00001571
Devang Patel3799f972006-11-15 01:27:05 +00001572 return Changed;
1573}
1574
Devang Patel06e86562006-12-07 19:39:39 +00001575//===----------------------------------------------------------------------===//
Devang Patel5f4ddf52006-12-19 19:46:59 +00001576// MPPassManager implementation
Devang Patel92c45ee2006-11-07 22:03:15 +00001577
Dan Gohman95df6192010-08-12 23:50:08 +00001578/// Execute all of the passes scheduled for execution by invoking
1579/// runOnModule method. Keep track of whether any of the passes modifies
Devang Patel92c45ee2006-11-07 22:03:15 +00001580/// the module, and if so, return true.
1581bool
Devang Patel5f4ddf52006-12-19 19:46:59 +00001582MPPassManager::runOnModule(Module &M) {
Devang Patel92c45ee2006-11-07 22:03:15 +00001583 bool Changed = false;
Devang Patelc1d6e1f2006-11-14 01:23:29 +00001584
Torok Edwin1970a892009-06-29 18:49:09 +00001585 // Initialize on-the-fly passes
1586 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1587 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1588 I != E; ++I) {
1589 FunctionPassManagerImpl *FPP = I->second;
1590 Changed |= FPP->doInitialization(M);
1591 }
1592
Pedro Artigas6eda0812012-11-29 17:47:05 +00001593 // Initialize module passes
1594 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index)
1595 Changed |= getContainedPass(Index)->doInitialization(M);
1596
Devang Patel1554c852006-12-16 00:56:26 +00001597 for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
1598 ModulePass *MP = getContainedPass(Index);
Dan Gohman16b77212010-03-01 17:34:28 +00001599 bool LocalChanged = false;
Devang Patel1554c852006-12-16 00:56:26 +00001600
Benjamin Kramer69cee612009-12-08 13:07:38 +00001601 dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001602 dumpRequiredSet(MP);
Devang Patel693a74e2006-12-14 00:08:04 +00001603
Devang Patel1554c852006-12-16 00:56:26 +00001604 initializeAnalysisImpl(MP);
Devang Patel8e58a1b2006-12-14 00:59:42 +00001605
Chris Lattnerd6f16582009-03-06 06:45:05 +00001606 {
1607 PassManagerPrettyStackEntry X(MP, M);
Chris Lattnera782e752010-03-30 04:03:22 +00001608 TimeRegion PassTimer(getPassTimer(MP));
1609
Dan Gohman16b77212010-03-01 17:34:28 +00001610 LocalChanged |= MP->runOnModule(M);
Chris Lattnerd6f16582009-03-06 06:45:05 +00001611 }
Devang Patel693a74e2006-12-14 00:08:04 +00001612
Dan Gohman16b77212010-03-01 17:34:28 +00001613 Changed |= LocalChanged;
1614 if (LocalChanged)
Dan Gohman97cf759b2008-01-29 12:09:55 +00001615 dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
Benjamin Kramer69cee612009-12-08 13:07:38 +00001616 M.getModuleIdentifier());
Chris Lattner0dabb7e2008-08-08 15:14:09 +00001617 dumpPreservedSet(MP);
Dan Gohman95df6192010-08-12 23:50:08 +00001618
Devang Patel58e0ef12007-07-19 18:02:32 +00001619 verifyPreservedAnalysis(MP);
Devang Patel1554c852006-12-16 00:56:26 +00001620 removeNotPreservedAnalysis(MP);
1621 recordAvailableAnalysis(MP);
Benjamin Kramer69cee612009-12-08 13:07:38 +00001622 removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG);
Devang Patel92c45ee2006-11-07 22:03:15 +00001623 }
Torok Edwin1970a892009-06-29 18:49:09 +00001624
Pedro Artigas6eda0812012-11-29 17:47:05 +00001625 // Finalize module passes
Pedro Artigasd1abec32012-12-05 17:12:22 +00001626 for (int Index = getNumContainedPasses() - 1; Index >= 0; --Index)
Pedro Artigas6eda0812012-11-29 17:47:05 +00001627 Changed |= getContainedPass(Index)->doFinalization(M);
1628
Torok Edwin1970a892009-06-29 18:49:09 +00001629 // Finalize on-the-fly passes
1630 for (std::map<Pass *, FunctionPassManagerImpl *>::iterator
1631 I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end();
1632 I != E; ++I) {
1633 FunctionPassManagerImpl *FPP = I->second;
1634 // We don't know when is the last time an on-the-fly pass is run,
1635 // so we need to releaseMemory / finalize here
1636 FPP->releaseMemoryOnTheFly();
1637 Changed |= FPP->doFinalization(M);
1638 }
Pedro Artigas6eda0812012-11-29 17:47:05 +00001639
Devang Patel92c45ee2006-11-07 22:03:15 +00001640 return Changed;
1641}
1642
Devang Patel569a6fd2007-04-16 20:12:57 +00001643/// Add RequiredPass into list of lower level passes required by pass P.
1644/// RequiredPass is run on the fly by Pass Manager when P requests it
1645/// through getAnalysis interface.
1646void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001647 assert(P->getPotentialPassManagerType() == PMT_ModulePassManager &&
1648 "Unable to handle Pass that requires lower level Analysis pass");
Dan Gohman95df6192010-08-12 23:50:08 +00001649 assert((P->getPotentialPassManagerType() <
Chris Lattnerf9574362009-03-06 05:53:14 +00001650 RequiredPass->getPotentialPassManagerType()) &&
1651 "Unable to handle Pass that requires lower level Analysis pass");
Devang Patel569a6fd2007-04-16 20:12:57 +00001652
Devang Pateldfa1ec32007-04-26 17:50:19 +00001653 FunctionPassManagerImpl *FPP = OnTheFlyManagers[P];
Devang Patel0ed8df32007-04-16 20:27:05 +00001654 if (!FPP) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001655 FPP = new FunctionPassManagerImpl();
Devang Pateldfa1ec32007-04-26 17:50:19 +00001656 // FPP is the top level manager.
1657 FPP->setTopLevelManager(FPP);
1658
Devang Patel0ed8df32007-04-16 20:27:05 +00001659 OnTheFlyManagers[P] = FPP;
1660 }
Devang Pateldfa1ec32007-04-26 17:50:19 +00001661 FPP->add(RequiredPass);
Devang Patel0ed8df32007-04-16 20:27:05 +00001662
Devang Pateldfa1ec32007-04-26 17:50:19 +00001663 // Register P as the last user of RequiredPass.
Devang Patelc67d1842011-09-13 21:13:29 +00001664 if (RequiredPass) {
1665 SmallVector<Pass *, 1> LU;
1666 LU.push_back(RequiredPass);
1667 FPP->setLastUser(LU, P);
1668 }
Devang Patel569a6fd2007-04-16 20:12:57 +00001669}
Devang Patel0ed8df32007-04-16 20:27:05 +00001670
Dan Gohman95df6192010-08-12 23:50:08 +00001671/// Return function pass corresponding to PassInfo PI, that is
Devang Patel0ed8df32007-04-16 20:27:05 +00001672/// required by module pass MP. Instantiate analysis pass, by using
1673/// its runOnFunction() for function F.
Owen Anderson90c579d2010-08-06 18:33:48 +00001674Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){
Devang Pateldfa1ec32007-04-26 17:50:19 +00001675 FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP];
Chris Lattnerf9574362009-03-06 05:53:14 +00001676 assert(FPP && "Unable to find on the fly pass");
Dan Gohman95df6192010-08-12 23:50:08 +00001677
Torok Edwin1970a892009-06-29 18:49:09 +00001678 FPP->releaseMemoryOnTheFly();
Devang Pateldfa1ec32007-04-26 17:50:19 +00001679 FPP->run(F);
Chris Lattner77c95ed2010-01-22 05:37:10 +00001680 return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI);
Devang Patel0ed8df32007-04-16 20:27:05 +00001681}
1682
1683
Devang Patel06e86562006-12-07 19:39:39 +00001684//===----------------------------------------------------------------------===//
1685// PassManagerImpl implementation
Owen Anderson40b6fdb2012-11-15 00:14:15 +00001686
Devang Patel37a6f792006-12-13 00:09:23 +00001687//
Devang Patelb30803b2006-11-07 22:23:34 +00001688/// run - Execute all of the passes scheduled for execution. Keep track of
1689/// whether any of the passes modifies the module, and if so, return true.
Devang Patel5f4ddf52006-12-19 19:46:59 +00001690bool PassManagerImpl::run(Module &M) {
Devang Patelb30803b2006-11-07 22:23:34 +00001691 bool Changed = false;
Devang Patel8e58a1b2006-12-14 00:59:42 +00001692 TimingInfo::createTheTimeInfo();
1693
Devang Patelc32cf542006-12-13 22:10:00 +00001694 dumpArguments();
Devang Patel5f4ddf52006-12-19 19:46:59 +00001695 dumpPasses();
Devang Patel45dc02d2006-12-13 20:03:48 +00001696
Pedro Artigasd1abec32012-12-05 17:12:22 +00001697 SmallVectorImpl<ImmutablePass *>& IPV = getImmutablePasses();
1698 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1699 E = IPV.end(); I != E; ++I) {
1700 Changed |= (*I)->doInitialization(M);
1701 }
1702
Devang Patel1336a6b2006-12-21 00:16:50 +00001703 initializeAllAnalysisInfo();
Chris Lattnerd6f16582009-03-06 06:45:05 +00001704 for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index)
1705 Changed |= getContainedManager(Index)->runOnModule(M);
Pedro Artigasd1abec32012-12-05 17:12:22 +00001706
1707 for (SmallVectorImpl<ImmutablePass *>::const_iterator I = IPV.begin(),
1708 E = IPV.end(); I != E; ++I) {
1709 Changed |= (*I)->doFinalization(M);
1710 }
1711
Devang Patelb30803b2006-11-07 22:23:34 +00001712 return Changed;
1713}
Devang Patel5a39b2e2006-11-08 10:29:57 +00001714
Devang Patel06e86562006-12-07 19:39:39 +00001715//===----------------------------------------------------------------------===//
1716// PassManager implementation
1717
Devang Patel5a39b2e2006-11-08 10:29:57 +00001718/// Create new pass manager
Devang Patel31626912006-12-13 02:36:01 +00001719PassManager::PassManager() {
Andrew Trick0e122d12011-08-29 17:07:00 +00001720 PM = new PassManagerImpl();
Devang Pateldff33ef2006-12-12 22:02:16 +00001721 // PM is the top level manager
1722 PM->setTopLevelManager(PM);
Devang Patel5a39b2e2006-11-08 10:29:57 +00001723}
1724
Devang Patel31626912006-12-13 02:36:01 +00001725PassManager::~PassManager() {
Devang Patel37a6f792006-12-13 00:09:23 +00001726 delete PM;
1727}
1728
Devang Patel5a39b2e2006-11-08 10:29:57 +00001729/// add - Add a pass to the queue of passes to run. This passes ownership of
1730/// the Pass to the PassManager. When the PassManager is destroyed, the pass
1731/// will be destroyed as well, so there is no need to delete the pass. This
1732/// implies that all passes MUST be allocated with 'new'.
Chris Lattnerf9574362009-03-06 05:53:14 +00001733void PassManager::add(Pass *P) {
Andrew Trick11e43292012-02-01 07:16:20 +00001734 PM->add(P);
Devang Patel5a39b2e2006-11-08 10:29:57 +00001735}
1736
1737/// run - Execute all of the passes scheduled for execution. Keep track of
1738/// whether any of the passes modifies the module, and if so, return true.
Chris Lattnerf9574362009-03-06 05:53:14 +00001739bool PassManager::run(Module &M) {
Devang Patel5a39b2e2006-11-08 10:29:57 +00001740 return PM->run(M);
1741}
1742
Devang Patel8e58a1b2006-12-14 00:59:42 +00001743//===----------------------------------------------------------------------===//
1744// TimingInfo Class - This class is used to calculate information about the
1745// amount of time each pass takes to execute. This only happens with
1746// -time-passes is enabled on the command line.
1747//
1748bool llvm::TimePassesIsEnabled = false;
1749static cl::opt<bool,true>
1750EnableTiming("time-passes", cl::location(TimePassesIsEnabled),
1751 cl::desc("Time each pass, printing elapsed time for each on exit"));
1752
1753// createTheTimeInfo - This method either initializes the TheTimeInfo pointer to
1754// a non null value (if the -time-passes option is enabled) or it leaves it
1755// null. It may be called multiple times.
1756void TimingInfo::createTheTimeInfo() {
1757 if (!TimePassesIsEnabled || TheTimeInfo) return;
1758
Sylvestre Ledru94c22712012-09-27 10:14:43 +00001759 // Constructed the first time this is called, iff -time-passes is enabled.
Devang Patel8e58a1b2006-12-14 00:59:42 +00001760 // This guarantees that the object will be constructed before static globals,
1761 // thus it will be destroyed before them.
1762 static ManagedStatic<TimingInfo> TTI;
1763 TheTimeInfo = &*TTI;
1764}
1765
Devang Patelc874eb52007-01-29 23:10:37 +00001766/// If TimingInfo is enabled then start pass timer.
Chris Lattnera782e752010-03-30 04:03:22 +00001767Timer *llvm::getPassTimer(Pass *P) {
Dan Gohman95df6192010-08-12 23:50:08 +00001768 if (TheTimeInfo)
Chris Lattnera782e752010-03-30 04:03:22 +00001769 return TheTimeInfo->getPassTimer(P);
Dan Gohman5c12ada2009-09-28 00:07:05 +00001770 return 0;
Devang Patelc874eb52007-01-29 23:10:37 +00001771}
1772
Devang Patel09e6e432007-01-08 19:29:38 +00001773//===----------------------------------------------------------------------===//
1774// PMStack implementation
1775//
Devang Patel36bcb822007-01-11 22:15:30 +00001776
Devang Patel09e6e432007-01-08 19:29:38 +00001777// Pop Pass Manager from the stack and clear its analysis info.
1778void PMStack::pop() {
1779
1780 PMDataManager *Top = this->top();
1781 Top->initializeAnalysisInfo();
1782
1783 S.pop_back();
1784}
1785
1786// Push PM on the stack and set its top level manager.
Dan Gohmanc2f12ab2008-03-13 01:21:31 +00001787void PMStack::push(PMDataManager *PM) {
Chris Lattnerf9574362009-03-06 05:53:14 +00001788 assert(PM && "Unable to push. Pass Manager expected");
Andrew Trick0e122d12011-08-29 17:07:00 +00001789 assert(PM->getDepth()==0 && "Pass Manager depth set too early");
Devang Patel09e6e432007-01-08 19:29:38 +00001790
Chris Lattnerf9574362009-03-06 05:53:14 +00001791 if (!this->empty()) {
Andrew Trick0e122d12011-08-29 17:07:00 +00001792 assert(PM->getPassManagerType() > this->top()->getPassManagerType()
1793 && "pushing bad pass manager to PMStack");
Chris Lattnerf9574362009-03-06 05:53:14 +00001794 PMTopLevelManager *TPM = this->top()->getTopLevelManager();
Devang Patel09e6e432007-01-08 19:29:38 +00001795
Chris Lattnerf9574362009-03-06 05:53:14 +00001796 assert(TPM && "Unable to find top level manager");
Devang Patel97149732007-01-11 00:19:00 +00001797 TPM->addIndirectPassManager(PM);
1798 PM->setTopLevelManager(TPM);
Andrew Trick0e122d12011-08-29 17:07:00 +00001799 PM->setDepth(this->top()->getDepth()+1);
1800 }
1801 else {
Benjamin Kramer4a3d0a52011-08-29 18:14:15 +00001802 assert((PM->getPassManagerType() == PMT_ModulePassManager
1803 || PM->getPassManagerType() == PMT_FunctionPassManager)
Andrew Trick0e122d12011-08-29 17:07:00 +00001804 && "pushing bad pass manager to PMStack");
1805 PM->setDepth(1);
Devang Patel97149732007-01-11 00:19:00 +00001806 }
1807
Devang Patel97149732007-01-11 00:19:00 +00001808 S.push_back(PM);
1809}
1810
1811// Dump content of the pass manager stack.
Dan Gohman12376a82010-08-07 01:04:15 +00001812void PMStack::dump() const {
1813 for (std::vector<PMDataManager *>::const_iterator I = S.begin(),
Chris Lattnerf9574362009-03-06 05:53:14 +00001814 E = S.end(); I != E; ++I)
Benjamin Kramer3dedf7e2011-08-29 18:14:17 +00001815 dbgs() << (*I)->getAsPass()->getPassName() << ' ';
Chris Lattnerf9574362009-03-06 05:53:14 +00001816
Devang Patel97149732007-01-11 00:19:00 +00001817 if (!S.empty())
Benjamin Kramer3dedf7e2011-08-29 18:14:17 +00001818 dbgs() << '\n';
Devang Patel09e6e432007-01-08 19:29:38 +00001819}
1820
Devang Patel09e6e432007-01-08 19:29:38 +00001821/// Find appropriate Module Pass Manager in the PM Stack and
Dan Gohman95df6192010-08-12 23:50:08 +00001822/// add self into that manager.
1823void ModulePass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001824 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001825 // Find Module Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001826 while (!PMS.empty()) {
Devang Patel44b0d292007-01-17 21:19:23 +00001827 PassManagerType TopPMType = PMS.top()->getPassManagerType();
1828 if (TopPMType == PreferredType)
1829 break; // We found desired pass manager
1830 else if (TopPMType > PMT_ModulePassManager)
Devang Patel09e6e432007-01-08 19:29:38 +00001831 PMS.pop(); // Pop children pass managers
Devang Patel6b9420e2007-01-11 19:59:06 +00001832 else
1833 break;
Devang Patel09e6e432007-01-08 19:29:38 +00001834 }
Devang Patelbd6dc7a2008-09-09 21:38:40 +00001835 assert(!PMS.empty() && "Unable to find appropriate Pass Manager");
Devang Patel44b0d292007-01-17 21:19:23 +00001836 PMS.top()->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001837}
1838
Devang Patel9d133e12007-01-16 21:43:18 +00001839/// Find appropriate Function Pass Manager or Call Graph Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001840/// in the PM Stack and add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001841void FunctionPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001842 PassManagerType PreferredType) {
Devang Patel09e6e432007-01-08 19:29:38 +00001843
Andrew Trick11e43292012-02-01 07:16:20 +00001844 // Find Function Pass Manager
Chris Lattner77c95ed2010-01-22 05:37:10 +00001845 while (!PMS.empty()) {
Devang Patel6b9420e2007-01-11 19:59:06 +00001846 if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager)
1847 PMS.pop();
Devang Patel09e6e432007-01-08 19:29:38 +00001848 else
Dan Gohman95df6192010-08-12 23:50:08 +00001849 break;
Devang Patel9d133e12007-01-16 21:43:18 +00001850 }
Devang Patel9d133e12007-01-16 21:43:18 +00001851
Chris Lattner77c95ed2010-01-22 05:37:10 +00001852 // Create new Function Pass Manager if needed.
1853 FPPassManager *FPP;
1854 if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) {
1855 FPP = (FPPassManager *)PMS.top();
1856 } else {
Devang Patel9d133e12007-01-16 21:43:18 +00001857 assert(!PMS.empty() && "Unable to create Function Pass Manager");
1858 PMDataManager *PMD = PMS.top();
1859
1860 // [1] Create new Function Pass Manager
Andrew Trick0e122d12011-08-29 17:07:00 +00001861 FPP = new FPPassManager();
Devang Patelbed7e682008-03-20 01:09:53 +00001862 FPP->populateInheritedAnalysis(PMS);
Devang Patel9d133e12007-01-16 21:43:18 +00001863
1864 // [2] Set up new manager's top level manager
1865 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1866 TPM->addIndirectPassManager(FPP);
1867
1868 // [3] Assign manager to manage this new manager. This may create
1869 // and push new managers into PMS
Devang Patel0938f742008-09-09 17:56:50 +00001870 FPP->assignPassManager(PMS, PMD->getPassManagerType());
Devang Patel9d133e12007-01-16 21:43:18 +00001871
1872 // [4] Push new manager into PMS
1873 PMS.push(FPP);
Devang Patel09e6e432007-01-08 19:29:38 +00001874 }
1875
Devang Patel9d133e12007-01-16 21:43:18 +00001876 // Assign FPP as the manager of this pass.
1877 FPP->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001878}
1879
Devang Patel9d133e12007-01-16 21:43:18 +00001880/// Find appropriate Basic Pass Manager or Call Graph Pass Manager
Dan Gohman95df6192010-08-12 23:50:08 +00001881/// in the PM Stack and add self into that manager.
Devang Patelbe1ffc62007-01-17 20:30:17 +00001882void BasicBlockPass::assignPassManager(PMStack &PMS,
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001883 PassManagerType PreferredType) {
Chris Lattner77c95ed2010-01-22 05:37:10 +00001884 BBPassManager *BBP;
Devang Patel09e6e432007-01-08 19:29:38 +00001885
Devang Patel97149732007-01-11 00:19:00 +00001886 // Basic Pass Manager is a leaf pass manager. It does not handle
1887 // any other pass manager.
Dan Gohman95df6192010-08-12 23:50:08 +00001888 if (!PMS.empty() &&
Chris Lattner77c95ed2010-01-22 05:37:10 +00001889 PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) {
1890 BBP = (BBPassManager *)PMS.top();
1891 } else {
1892 // If leaf manager is not Basic Block Pass manager then create new
1893 // basic Block Pass manager.
Devang Patel9d133e12007-01-16 21:43:18 +00001894 assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager");
1895 PMDataManager *PMD = PMS.top();
1896
1897 // [1] Create new Basic Block Manager
Andrew Trick0e122d12011-08-29 17:07:00 +00001898 BBP = new BBPassManager();
Devang Patel9d133e12007-01-16 21:43:18 +00001899
1900 // [2] Set up new manager's top level manager
1901 // Basic Block Pass Manager does not live by itself
1902 PMTopLevelManager *TPM = PMD->getTopLevelManager();
1903 TPM->addIndirectPassManager(BBP);
1904
Devang Patel97149732007-01-11 00:19:00 +00001905 // [3] Assign manager to manage this new manager. This may create
1906 // and push new managers into PMS
David Greenee89f1c42010-05-10 20:24:27 +00001907 BBP->assignPassManager(PMS, PreferredType);
Devang Patel97149732007-01-11 00:19:00 +00001908
Devang Patel9d133e12007-01-16 21:43:18 +00001909 // [4] Push new manager into PMS
1910 PMS.push(BBP);
1911 }
Devang Patel09e6e432007-01-08 19:29:38 +00001912
Devang Patel9d133e12007-01-16 21:43:18 +00001913 // Assign BBP as the manager of this pass.
1914 BBP->add(this);
Devang Patel09e6e432007-01-08 19:29:38 +00001915}
1916
Dan Gohman580b8992008-03-11 16:41:42 +00001917PassManagerBase::~PassManagerBase() {}