Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1 | //===- PassManager.cpp - LLVM Pass Infrastructure Implementation ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 10 | // This file implements the LLVM Pass Manager infrastructure. |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 15 | #include "llvm/PassManagers.h" |
Dan Gohman | a19631f | 2010-08-07 01:18:18 +0000 | [diff] [blame] | 16 | #include "llvm/PassManager.h" |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 17 | #include "llvm/Assembly/PrintModulePass.h" |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 18 | #include "llvm/Assembly/Writer.h" |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/CommandLine.h" |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Timer.h" |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PassNameParser.h" |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Mutex.h" |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 29 | #include <map> |
Dan Gohman | 8c43e41 | 2007-10-03 19:04:09 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 31 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 32 | // See PassManagers.h for Pass Manager infrastructure overview. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 33 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 34 | namespace 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 Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 43 | // Different debug levels that can be enabled... |
| 44 | enum PassDebugLevel { |
| 45 | None, Arguments, Structure, Executions, Details |
| 46 | }; |
| 47 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 48 | static cl::opt<enum PassDebugLevel> |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 49 | PassDebugging("debug-pass", cl::Hidden, |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 50 | cl::desc("Print PassManager debugging information"), |
| 51 | cl::values( |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 52 | 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 Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 57 | clEnumValEnd)); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 58 | |
| 59 | typedef llvm::cl::list<const llvm::PassInfo *, bool, PassNameParser> |
| 60 | PassOptionList; |
| 61 | |
| 62 | // Print IR out before/after specified passes. |
| 63 | static PassOptionList |
| 64 | PrintBefore("print-before", |
Eric Christopher | 787ba0b | 2011-03-09 19:46:51 +0000 | [diff] [blame] | 65 | llvm::cl::desc("Print IR before specified passes"), |
| 66 | cl::Hidden); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 67 | |
| 68 | static PassOptionList |
| 69 | PrintAfter("print-after", |
Eric Christopher | 787ba0b | 2011-03-09 19:46:51 +0000 | [diff] [blame] | 70 | llvm::cl::desc("Print IR after specified passes"), |
| 71 | cl::Hidden); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 72 | |
| 73 | static cl::opt<bool> |
| 74 | PrintBeforeAll("print-before-all", |
| 75 | llvm::cl::desc("Print IR before each pass"), |
| 76 | cl::init(false)); |
| 77 | static cl::opt<bool> |
| 78 | PrintAfterAll("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 Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 85 | static bool ShouldPrintBeforeOrAfterPass(const PassInfo *PI, |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 86 | PassOptionList &PassesToPrint) { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 87 | 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 Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 93 | } |
| 94 | return false; |
| 95 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 96 | |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 97 | /// This is a utility to check whether a pass should have IR dumped |
| 98 | /// before it. |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 99 | static bool ShouldPrintBeforePass(const PassInfo *PI) { |
| 100 | return PrintBeforeAll || ShouldPrintBeforeOrAfterPass(PI, PrintBefore); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /// This is a utility to check whether a pass should have IR dumped |
| 104 | /// after it. |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 105 | static bool ShouldPrintAfterPass(const PassInfo *PI) { |
| 106 | return PrintAfterAll || ShouldPrintBeforeOrAfterPass(PI, PrintAfter); |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 109 | } // End of llvm namespace |
| 110 | |
Chris Lattner | d4d966f | 2009-09-15 05:03:04 +0000 | [diff] [blame] | 111 | /// isPassDebuggingExecutionsOrMore - Return true if -debug-pass=Executions |
| 112 | /// or higher is specified. |
| 113 | bool PMDataManager::isPassDebuggingExecutionsOrMore() const { |
| 114 | return PassDebugging >= Executions; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 120 | void PassManagerPrettyStackEntry::print(raw_ostream &OS) const { |
| 121 | if (V == 0 && M == 0) |
| 122 | OS << "Releasing pass '"; |
| 123 | else |
| 124 | OS << "Running pass '"; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 126 | OS << P->getPassName() << "'"; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 128 | if (M) { |
| 129 | OS << " on module '" << M->getModuleIdentifier() << "'.\n"; |
| 130 | return; |
| 131 | } |
| 132 | if (V == 0) { |
| 133 | OS << '\n'; |
| 134 | return; |
| 135 | } |
| 136 | |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 137 | OS << " on "; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 138 | if (isa<Function>(V)) |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 139 | OS << "function"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 140 | else if (isa<BasicBlock>(V)) |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 141 | OS << "basic block"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 142 | else |
Dan Gohman | 79fc0e9 | 2009-03-10 18:47:59 +0000 | [diff] [blame] | 143 | OS << "value"; |
| 144 | |
| 145 | OS << " '"; |
| 146 | WriteAsOperand(OS, V, /*PrintTy=*/false, M); |
| 147 | OS << "'\n"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 151 | namespace { |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 152 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 153 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 154 | // BBPassManager |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 155 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 156 | /// BBPassManager manages BasicBlockPass. It batches all the |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 157 | /// pass together and sequence them to process one basic block before |
| 158 | /// processing next basic block. |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 159 | class BBPassManager : public PMDataManager, public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 160 | |
| 161 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 162 | static char ID; |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 163 | explicit BBPassManager() |
| 164 | : PMDataManager(), FunctionPass(ID) {} |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 165 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 166 | /// 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 Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 170 | /// Pass Manager itself does not invalidate any analysis info. |
| 171 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 172 | Info.setPreservesAll(); |
| 173 | } |
| 174 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 175 | bool doInitialization(Module &M); |
| 176 | bool doInitialization(Function &F); |
| 177 | bool doFinalization(Module &M); |
| 178 | bool doFinalization(Function &F); |
| 179 | |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 180 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 181 | virtual Pass *getAsPass() { return this; } |
| 182 | |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 183 | virtual const char *getPassName() const { |
Dan Gohman | 1e9860a | 2008-03-13 01:58:48 +0000 | [diff] [blame] | 184 | return "BasicBlock Pass Manager"; |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 187 | // Print passes managed by this manager |
| 188 | void dumpPassStructure(unsigned Offset) { |
Benjamin Kramer | 4dd515c | 2011-08-29 18:14:17 +0000 | [diff] [blame] | 189 | llvm::dbgs().indent(Offset*2) << "BasicBlockPass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 190 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 191 | BasicBlockPass *BP = getContainedPass(Index); |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 192 | BP->dumpPassStructure(Offset + 1); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 193 | dumpLastUses(BP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 196 | |
| 197 | BasicBlockPass *getContainedPass(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 198 | assert(N < PassVector.size() && "Pass number out of range!"); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 199 | BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); |
| 200 | return BP; |
| 201 | } |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 202 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 203 | virtual PassManagerType getPassManagerType() const { |
| 204 | return PMT_BasicBlockPassManager; |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 205 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 208 | char BBPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 209 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 210 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 211 | namespace llvm { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 212 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 213 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 214 | // FunctionPassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 215 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 216 | /// FunctionPassManagerImpl manages FPPassManagers |
| 217 | class FunctionPassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 218 | public PMDataManager, |
| 219 | public PMTopLevelManager { |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 220 | virtual void anchor(); |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 221 | private: |
| 222 | bool wasRun; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 223 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 224 | static char ID; |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 225 | explicit FunctionPassManagerImpl() : |
| 226 | Pass(PT_PassManager, ID), PMDataManager(), |
| 227 | PMTopLevelManager(new FPPassManager()), wasRun(false) {} |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 228 | |
| 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 Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 236 | |
| 237 | /// createPrinterPass - Get a function printer pass. |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 238 | Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { |
| 239 | return createPrintFunctionPass(Banner, &O); |
| 240 | } |
| 241 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 242 | // Prepare for running an on the fly pass, freeing memory if needed |
| 243 | // from a previous run. |
| 244 | void releaseMemoryOnTheFly(); |
| 245 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 246 | /// 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 Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 253 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 254 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 255 | /// |
| 256 | bool doFinalization(Module &M); |
| 257 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 259 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 260 | virtual Pass *getAsPass() { return this; } |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 261 | virtual PassManagerType getTopLevelPassManagerType() { |
| 262 | return PMT_FunctionPassManager; |
| 263 | } |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 264 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 265 | /// Pass Manager itself does not invalidate any analysis info. |
| 266 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 267 | Info.setPreservesAll(); |
| 268 | } |
| 269 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 270 | FPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 271 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 272 | FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); |
| 273 | return FP; |
| 274 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 275 | }; |
| 276 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 277 | void FunctionPassManagerImpl::anchor() {} |
| 278 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 279 | char FunctionPassManagerImpl::ID = 0; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 280 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 281 | //===----------------------------------------------------------------------===// |
| 282 | // MPPassManager |
| 283 | // |
| 284 | /// MPPassManager manages ModulePasses and function pass managers. |
Dan Gohman | dfdf2c0 | 2008-03-11 16:18:48 +0000 | [diff] [blame] | 285 | /// It batches all Module passes and function pass managers together and |
| 286 | /// sequences them to process one module. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 287 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 288 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 289 | static char ID; |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 290 | explicit MPPassManager() : |
| 291 | Pass(PT_PassManager, ID), PMDataManager() { } |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 292 | |
| 293 | // Delete on the fly managers. |
| 294 | virtual ~MPPassManager() { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 295 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 296 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 297 | I != E; ++I) { |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 298 | FunctionPassManagerImpl *FPP = I->second; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 299 | delete FPP; |
| 300 | } |
| 301 | } |
| 302 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 303 | /// createPrinterPass - Get a module printer pass. |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 304 | Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { |
| 305 | return createPrintModulePass(&O, false, Banner); |
| 306 | } |
| 307 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 308 | /// 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 Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 311 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 312 | /// Pass Manager itself does not invalidate any analysis info. |
| 313 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 314 | Info.setPreservesAll(); |
| 315 | } |
| 316 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 317 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 318 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 319 | /// through getAnalysis interface. |
| 320 | virtual void addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass); |
| 321 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 322 | /// Return function pass corresponding to PassInfo PI, that is |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 323 | /// required by module pass MP. Instantiate analysis pass, by using |
| 324 | /// its runOnFunction() for function F. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 325 | virtual Pass* getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 326 | |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 327 | virtual const char *getPassName() const { |
| 328 | return "Module Pass Manager"; |
| 329 | } |
| 330 | |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 331 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 332 | virtual Pass *getAsPass() { return this; } |
| 333 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 334 | // Print passes managed by this manager |
| 335 | void dumpPassStructure(unsigned Offset) { |
Benjamin Kramer | 4dd515c | 2011-08-29 18:14:17 +0000 | [diff] [blame] | 336 | llvm::dbgs().indent(Offset*2) << "ModulePass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 337 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 338 | ModulePass *MP = getContainedPass(Index); |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 339 | MP->dumpPassStructure(Offset + 1); |
Dan Gohman | 83ff184 | 2009-07-01 23:12:33 +0000 | [diff] [blame] | 340 | std::map<Pass *, FunctionPassManagerImpl *>::const_iterator I = |
| 341 | OnTheFlyManagers.find(MP); |
| 342 | if (I != OnTheFlyManagers.end()) |
| 343 | I->second->dumpPassStructure(Offset + 2); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 344 | dumpLastUses(MP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 345 | } |
| 346 | } |
| 347 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 348 | ModulePass *getContainedPass(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 349 | assert(N < PassVector.size() && "Pass number out of range!"); |
| 350 | return static_cast<ModulePass *>(PassVector[N]); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 353 | virtual PassManagerType getPassManagerType() const { |
| 354 | return PMT_ModulePassManager; |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 355 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 356 | |
| 357 | private: |
| 358 | /// Collection of on the fly FPPassManagers. These managers manage |
| 359 | /// function passes that are required by module passes. |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 360 | std::map<Pass *, FunctionPassManagerImpl *> OnTheFlyManagers; |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 361 | }; |
| 362 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 363 | char MPPassManager::ID = 0; |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 364 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 365 | // PassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 366 | // |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 367 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 368 | /// PassManagerImpl manages MPPassManagers |
| 369 | class PassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 370 | public PMDataManager, |
| 371 | public PMTopLevelManager { |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 372 | virtual void anchor(); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 373 | |
| 374 | public: |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 375 | static char ID; |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 376 | explicit PassManagerImpl() : |
| 377 | Pass(PT_PassManager, ID), PMDataManager(), |
| 378 | PMTopLevelManager(new MPPassManager()) {} |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 379 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 380 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 381 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 382 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 383 | /// implies that all passes MUST be allocated with 'new'. |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 384 | void add(Pass *P) { |
Devang Patel | df6c9ae | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 385 | schedulePass(P); |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 386 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 387 | |
| 388 | /// createPrinterPass - Get a module printer pass. |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 389 | Pass *createPrinterPass(raw_ostream &O, const std::string &Banner) const { |
| 390 | return createPrintModulePass(&O, false, Banner); |
| 391 | } |
| 392 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 393 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 394 | /// whether any of the passes modifies the module, and if so, return true. |
| 395 | bool run(Module &M); |
| 396 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 397 | /// Pass Manager itself does not invalidate any analysis info. |
| 398 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 399 | Info.setPreservesAll(); |
| 400 | } |
| 401 | |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 402 | virtual PMDataManager *getAsPMDataManager() { return this; } |
| 403 | virtual Pass *getAsPass() { return this; } |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 404 | virtual PassManagerType getTopLevelPassManagerType() { |
| 405 | return PMT_ModulePassManager; |
| 406 | } |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 407 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 408 | MPPassManager *getContainedManager(unsigned N) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 409 | assert(N < PassManagers.size() && "Pass number out of range!"); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 410 | MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); |
| 411 | return MP; |
| 412 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 413 | }; |
| 414 | |
David Blaikie | a379b181 | 2011-12-20 02:50:00 +0000 | [diff] [blame] | 415 | void PassManagerImpl::anchor() {} |
| 416 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 417 | char PassManagerImpl::ID = 0; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 418 | } // End of llvm namespace |
| 419 | |
| 420 | namespace { |
| 421 | |
| 422 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 423 | /// TimingInfo Class - This class is used to calculate information about the |
| 424 | /// amount of time each pass takes to execute. This only happens when |
| 425 | /// -time-passes is enabled on the command line. |
| 426 | /// |
Owen Anderson | 0dd39fd | 2009-06-17 21:28:54 +0000 | [diff] [blame] | 427 | |
Owen Anderson | 5a6960f | 2009-06-18 20:51:00 +0000 | [diff] [blame] | 428 | static ManagedStatic<sys::SmartMutex<true> > TimingInfoMutex; |
Owen Anderson | 0dd39fd | 2009-06-17 21:28:54 +0000 | [diff] [blame] | 429 | |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 430 | class TimingInfo { |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 431 | DenseMap<Pass*, Timer*> TimingData; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 432 | TimerGroup TG; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 433 | public: |
| 434 | // Use 'create' member to get this. |
| 435 | TimingInfo() : TG("... Pass execution timing report ...") {} |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 436 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 437 | // TimingDtor - Print out information about timing information |
| 438 | ~TimingInfo() { |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 439 | // Delete all of the timers, which accumulate their info into the |
| 440 | // TimerGroup. |
| 441 | for (DenseMap<Pass*, Timer*>::iterator I = TimingData.begin(), |
| 442 | E = TimingData.end(); I != E; ++I) |
| 443 | delete I->second; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 444 | // TimerGroup is deleted next, printing the report. |
| 445 | } |
| 446 | |
| 447 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer |
| 448 | // to a non null value (if the -time-passes option is enabled) or it leaves it |
| 449 | // null. It may be called multiple times. |
| 450 | static void createTheTimeInfo(); |
| 451 | |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 452 | /// getPassTimer - Return the timer for the specified pass if it exists. |
| 453 | Timer *getPassTimer(Pass *P) { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 454 | if (P->getAsPMDataManager()) |
Dan Gohman | 277e767 | 2009-09-28 00:07:05 +0000 | [diff] [blame] | 455 | return 0; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 456 | |
Owen Anderson | 5c96ef7 | 2009-07-07 18:33:04 +0000 | [diff] [blame] | 457 | sys::SmartScopedLock<true> Lock(*TimingInfoMutex); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 458 | Timer *&T = TimingData[P]; |
| 459 | if (T == 0) |
| 460 | T = new Timer(P->getPassName(), TG); |
Chris Lattner | ec8ef9b | 2010-03-30 03:57:00 +0000 | [diff] [blame] | 461 | return T; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 462 | } |
| 463 | }; |
| 464 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 465 | } // End of anon namespace |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 466 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 467 | static TimingInfo *TheTimeInfo; |
| 468 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 469 | //===----------------------------------------------------------------------===// |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 470 | // PMTopLevelManager implementation |
| 471 | |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 472 | /// Initialize top level manager. Create first pass manager. |
Dan Gohman | e85c619 | 2010-08-16 21:38:42 +0000 | [diff] [blame] | 473 | PMTopLevelManager::PMTopLevelManager(PMDataManager *PMDM) { |
| 474 | PMDM->setTopLevelManager(this); |
| 475 | addPassManager(PMDM); |
| 476 | activeStack.push(PMDM); |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 479 | /// Set pass P as the last user of the given analysis passes. |
Dan Gohman | c8da21b | 2010-10-12 00:12:29 +0000 | [diff] [blame] | 480 | void |
| 481 | PMTopLevelManager::setLastUser(const SmallVectorImpl<Pass *> &AnalysisPasses, |
| 482 | Pass *P) { |
Tobias Grosser | f07426b | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 483 | unsigned PDepth = 0; |
| 484 | if (P->getResolver()) |
| 485 | PDepth = P->getResolver()->getPMDataManager().getDepth(); |
| 486 | |
Dan Gohman | c8da21b | 2010-10-12 00:12:29 +0000 | [diff] [blame] | 487 | for (SmallVectorImpl<Pass *>::const_iterator I = AnalysisPasses.begin(), |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 488 | E = AnalysisPasses.end(); I != E; ++I) { |
| 489 | Pass *AP = *I; |
| 490 | LastUser[AP] = P; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 491 | |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 492 | if (P == AP) |
| 493 | continue; |
| 494 | |
Tobias Grosser | f07426b | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 495 | // Update the last users of passes that are required transitive by AP. |
| 496 | AnalysisUsage *AnUsage = findAnalysisUsage(AP); |
| 497 | const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); |
| 498 | SmallVector<Pass *, 12> LastUses; |
| 499 | SmallVector<Pass *, 12> LastPMUses; |
| 500 | for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(), |
| 501 | E = IDs.end(); I != E; ++I) { |
| 502 | Pass *AnalysisPass = findAnalysisPass(*I); |
| 503 | assert(AnalysisPass && "Expected analysis pass to exist."); |
| 504 | AnalysisResolver *AR = AnalysisPass->getResolver(); |
| 505 | assert(AR && "Expected analysis resolver to exist."); |
| 506 | unsigned APDepth = AR->getPMDataManager().getDepth(); |
| 507 | |
| 508 | if (PDepth == APDepth) |
| 509 | LastUses.push_back(AnalysisPass); |
| 510 | else if (PDepth > APDepth) |
| 511 | LastPMUses.push_back(AnalysisPass); |
| 512 | } |
| 513 | |
| 514 | setLastUser(LastUses, P); |
| 515 | |
| 516 | // If this pass has a corresponding pass manager, push higher level |
| 517 | // analysis to this pass manager. |
| 518 | if (P->getResolver()) |
| 519 | setLastUser(LastPMUses, P->getResolver()->getPMDataManager().getAsPass()); |
| 520 | |
| 521 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 522 | // If AP is the last user of other passes then make P last user of |
| 523 | // such passes. |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 524 | for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 525 | LUE = LastUser.end(); LUI != LUE; ++LUI) { |
| 526 | if (LUI->second == AP) |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 527 | // DenseMap iterator is not invalidated here because |
Tobias Grosser | f07426b | 2011-01-20 21:03:22 +0000 | [diff] [blame] | 528 | // this is just updating existing entries. |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 529 | LastUser[LUI->first] = P; |
| 530 | } |
| 531 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | /// Collect passes whose last user is P |
Dan Gohman | 7224bce | 2010-10-12 00:11:18 +0000 | [diff] [blame] | 535 | void PMTopLevelManager::collectLastUses(SmallVectorImpl<Pass *> &LastUses, |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 536 | Pass *P) { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 537 | DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI = |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 538 | InversedLastUser.find(P); |
| 539 | if (DMI == InversedLastUser.end()) |
| 540 | return; |
| 541 | |
| 542 | SmallPtrSet<Pass *, 8> &LU = DMI->second; |
| 543 | for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(), |
| 544 | E = LU.end(); I != E; ++I) { |
| 545 | LastUses.push_back(*I); |
| 546 | } |
| 547 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 550 | AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) { |
| 551 | AnalysisUsage *AnUsage = NULL; |
| 552 | DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 553 | if (DMI != AnUsageMap.end()) |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 554 | AnUsage = DMI->second; |
| 555 | else { |
| 556 | AnUsage = new AnalysisUsage(); |
| 557 | P->getAnalysisUsage(*AnUsage); |
| 558 | AnUsageMap[P] = AnUsage; |
| 559 | } |
| 560 | return AnUsage; |
| 561 | } |
| 562 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 563 | /// Schedule pass P for execution. Make sure that passes required by |
| 564 | /// P are run before P is run. Update analysis info maintained by |
| 565 | /// the manager. Remove dead passes. This is a recursive function. |
| 566 | void PMTopLevelManager::schedulePass(Pass *P) { |
| 567 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 568 | // TODO : Allocate function manager for this pass, other wise required set |
| 569 | // may be inserted into previous function manager |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 570 | |
Devang Patel | d74ede7 | 2007-03-06 01:06:16 +0000 | [diff] [blame] | 571 | // Give pass a chance to prepare the stage. |
| 572 | P->preparePassManager(activeStack); |
| 573 | |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 574 | // If P is an analysis pass and it is available then do not |
| 575 | // generate the analysis again. Stale analysis info should not be |
| 576 | // available at this point. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 577 | const PassInfo *PI = |
| 578 | PassRegistry::getPassRegistry()->getPassInfo(P->getPassID()); |
| 579 | if (PI && PI->isAnalysis() && findAnalysisPass(P->getPassID())) { |
Nuno Lopes | 0460bb2 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 580 | delete P; |
Devang Patel | af75ab8 | 2008-03-19 00:48:41 +0000 | [diff] [blame] | 581 | return; |
Nuno Lopes | 0460bb2 | 2008-11-04 23:03:58 +0000 | [diff] [blame] | 582 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 583 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 584 | AnalysisUsage *AnUsage = findAnalysisUsage(P); |
| 585 | |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 586 | bool checkAnalysis = true; |
| 587 | while (checkAnalysis) { |
| 588 | checkAnalysis = false; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 589 | |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 590 | const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); |
| 591 | for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(), |
| 592 | E = RequiredSet.end(); I != E; ++I) { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 593 | |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 594 | Pass *AnalysisPass = findAnalysisPass(*I); |
| 595 | if (!AnalysisPass) { |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 596 | const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I); |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 597 | assert(PI && "Expected required passes to be initialized"); |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 598 | AnalysisPass = PI->createPass(); |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 599 | if (P->getPotentialPassManagerType () == |
| 600 | AnalysisPass->getPotentialPassManagerType()) |
| 601 | // Schedule analysis pass that is managed by the same pass manager. |
| 602 | schedulePass(AnalysisPass); |
| 603 | else if (P->getPotentialPassManagerType () > |
| 604 | AnalysisPass->getPotentialPassManagerType()) { |
| 605 | // Schedule analysis pass that is managed by a new manager. |
| 606 | schedulePass(AnalysisPass); |
Dan Gohman | 6304db3 | 2010-08-16 22:57:28 +0000 | [diff] [blame] | 607 | // Recheck analysis passes to ensure that required analyses that |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 608 | // are already checked are still available. |
| 609 | checkAnalysis = true; |
| 610 | } |
| 611 | else |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 612 | // Do not schedule this analysis. Lower level analsyis |
Devang Patel | fdee703 | 2008-08-14 23:07:48 +0000 | [diff] [blame] | 613 | // passes are run on the fly. |
| 614 | delete AnalysisPass; |
| 615 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | // Now all required passes are available. |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 620 | if (ImmutablePass *IP = P->getAsImmutablePass()) { |
| 621 | // P is a immutable pass and it will be managed by this |
| 622 | // top level manager. Set up analysis resolver to connect them. |
| 623 | PMDataManager *DM = getAsPMDataManager(); |
| 624 | AnalysisResolver *AR = new AnalysisResolver(*DM); |
| 625 | P->setResolver(AR); |
| 626 | DM->initializeAnalysisImpl(P); |
| 627 | addImmutablePass(IP); |
| 628 | DM->recordAvailableAnalysis(IP); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | if (PI && !PI->isAnalysis() && ShouldPrintBeforePass(PI)) { |
| 633 | Pass *PP = P->createPrinterPass( |
| 634 | dbgs(), std::string("*** IR Dump Before ") + P->getPassName() + " ***"); |
| 635 | PP->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 636 | } |
| 637 | |
| 638 | // Add the requested pass to the best available pass manager. |
| 639 | P->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 640 | |
| 641 | if (PI && !PI->isAnalysis() && ShouldPrintAfterPass(PI)) { |
| 642 | Pass *PP = P->createPrinterPass( |
| 643 | dbgs(), std::string("*** IR Dump After ") + P->getPassName() + " ***"); |
| 644 | PP->assignPassManager(activeStack, getTopLevelPassManagerType()); |
| 645 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | /// Find the pass that implements Analysis AID. Search immutable |
| 649 | /// passes and all pass managers. If desired pass is not found |
| 650 | /// then return NULL. |
| 651 | Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { |
| 652 | |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 653 | // Check pass managers |
Dan Gohman | 7224bce | 2010-10-12 00:11:18 +0000 | [diff] [blame] | 654 | for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(), |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 655 | E = PassManagers.end(); I != E; ++I) |
| 656 | if (Pass *P = (*I)->findAnalysisPass(AID, false)) |
| 657 | return P; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 658 | |
| 659 | // Check other pass managers |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 660 | for (SmallVectorImpl<PMDataManager *>::iterator |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 661 | I = IndirectPassManagers.begin(), |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 662 | E = IndirectPassManagers.end(); I != E; ++I) |
| 663 | if (Pass *P = (*I)->findAnalysisPass(AID, false)) |
| 664 | return P; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 665 | |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 666 | // Check the immutable passes. Iterate in reverse order so that we find |
| 667 | // the most recently registered passes first. |
| 668 | for (SmallVector<ImmutablePass *, 8>::reverse_iterator I = |
| 669 | ImmutablePasses.rbegin(), E = ImmutablePasses.rend(); I != E; ++I) { |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 670 | AnalysisID PI = (*I)->getPassID(); |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 671 | if (PI == AID) |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 672 | return *I; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 673 | |
| 674 | // If Pass not found then check the interfaces implemented by Immutable Pass |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 675 | const PassInfo *PassInf = |
| 676 | PassRegistry::getPassRegistry()->getPassInfo(PI); |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 677 | assert(PassInf && "Expected all immutable passes to be initialized"); |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 678 | const std::vector<const PassInfo*> &ImmPI = |
| 679 | PassInf->getInterfacesImplemented(); |
| 680 | for (std::vector<const PassInfo*>::const_iterator II = ImmPI.begin(), |
| 681 | EE = ImmPI.end(); II != EE; ++II) { |
| 682 | if ((*II)->getTypeInfo() == AID) |
| 683 | return *I; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
Dan Gohman | 844dd0a | 2010-10-11 23:19:01 +0000 | [diff] [blame] | 687 | return 0; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 690 | // Print passes managed by this top level manager. |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 691 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 692 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 693 | if (PassDebugging < Structure) |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 694 | return; |
| 695 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 696 | // Print out the immutable passes |
| 697 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 698 | ImmutablePasses[i]->dumpPassStructure(0); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 699 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 700 | |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 701 | // Every class that derives from PMDataManager also derives from Pass |
| 702 | // (sometimes indirectly), but there's no inheritance relationship |
| 703 | // between PMDataManager and Pass, so we have to getAsPass to get |
| 704 | // from a PMDataManager* to a Pass*. |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 705 | for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(), |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 706 | E = PassManagers.end(); I != E; ++I) |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 707 | (*I)->getAsPass()->dumpPassStructure(1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 710 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 711 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 712 | if (PassDebugging < Arguments) |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 713 | return; |
| 714 | |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 715 | dbgs() << "Pass Arguments: "; |
Dan Gohman | f51d06bb | 2010-11-11 16:32:17 +0000 | [diff] [blame] | 716 | for (SmallVector<ImmutablePass *, 8>::const_iterator I = |
| 717 | ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 718 | if (const PassInfo *PI = |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 719 | PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) { |
| 720 | assert(PI && "Expected all immutable passes to be initialized"); |
Dan Gohman | f51d06bb | 2010-11-11 16:32:17 +0000 | [diff] [blame] | 721 | if (!PI->isAnalysisGroup()) |
| 722 | dbgs() << " -" << PI->getPassArgument(); |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 723 | } |
Devang Patel | 0d29ae0 | 2008-08-12 15:44:31 +0000 | [diff] [blame] | 724 | for (SmallVector<PMDataManager *, 8>::const_iterator I = PassManagers.begin(), |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 725 | E = PassManagers.end(); I != E; ++I) |
| 726 | (*I)->dumpPassArguments(); |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 727 | dbgs() << "\n"; |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 730 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 731 | for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(), |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 732 | E = PassManagers.end(); I != E; ++I) |
| 733 | (*I)->initializeAnalysisInfo(); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 734 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 735 | // Initailize other pass managers |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 736 | for (SmallVectorImpl<PMDataManager *>::iterator |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 737 | I = IndirectPassManagers.begin(), E = IndirectPassManagers.end(); |
| 738 | I != E; ++I) |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 739 | (*I)->initializeAnalysisInfo(); |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 740 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 741 | for (DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(), |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 742 | DME = LastUser.end(); DMI != DME; ++DMI) { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 743 | DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI = |
Devang Patel | c68a0b6 | 2008-08-12 00:26:16 +0000 | [diff] [blame] | 744 | InversedLastUser.find(DMI->second); |
| 745 | if (InvDMI != InversedLastUser.end()) { |
| 746 | SmallPtrSet<Pass *, 8> &L = InvDMI->second; |
| 747 | L.insert(DMI->first); |
| 748 | } else { |
| 749 | SmallPtrSet<Pass *, 8> L; L.insert(DMI->first); |
| 750 | InversedLastUser[DMI->second] = L; |
| 751 | } |
| 752 | } |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 755 | /// Destructor |
| 756 | PMTopLevelManager::~PMTopLevelManager() { |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 757 | for (SmallVectorImpl<PMDataManager *>::iterator I = PassManagers.begin(), |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 758 | E = PassManagers.end(); I != E; ++I) |
| 759 | delete *I; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 760 | |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 761 | for (SmallVectorImpl<ImmutablePass *>::iterator |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 762 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 763 | delete *I; |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 764 | |
| 765 | for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(), |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 766 | DME = AnUsageMap.end(); DMI != DME; ++DMI) |
| 767 | delete DMI->second; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 770 | //===----------------------------------------------------------------------===// |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 771 | // PMDataManager implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 772 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 773 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 774 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 775 | AnalysisID PI = P->getPassID(); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 776 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 777 | AvailableAnalysis[PI] = P; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 778 | |
Dan Gohman | b83d1b6 | 2010-08-12 23:46:28 +0000 | [diff] [blame] | 779 | assert(!AvailableAnalysis.empty()); |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 780 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 781 | // This pass is the current implementation of all of the interfaces it |
| 782 | // implements as well. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 783 | const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI); |
| 784 | if (PInf == 0) return; |
| 785 | const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); |
Owen Anderson | 3183ef1 | 2010-07-20 16:55:05 +0000 | [diff] [blame] | 786 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 787 | AvailableAnalysis[II[i]->getTypeInfo()] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 790 | // Return true if P preserves high level analysis used by other |
| 791 | // passes managed by this manager |
| 792 | bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 793 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 794 | if (AnUsage->getPreservesAll()) |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 795 | return true; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 796 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 797 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 798 | for (SmallVectorImpl<Pass *>::iterator I = HigherLevelAnalysis.begin(), |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 799 | E = HigherLevelAnalysis.end(); I != E; ++I) { |
| 800 | Pass *P1 = *I; |
Chris Lattner | 21889d7 | 2010-01-22 04:55:08 +0000 | [diff] [blame] | 801 | if (P1->getAsImmutablePass() == 0 && |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 802 | std::find(PreservedSet.begin(), PreservedSet.end(), |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 803 | P1->getPassID()) == |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 804 | PreservedSet.end()) |
| 805 | return false; |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 806 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 807 | |
Devang Patel | 9d9fc90 | 2007-03-06 17:52:53 +0000 | [diff] [blame] | 808 | return true; |
| 809 | } |
| 810 | |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 811 | /// verifyPreservedAnalysis -- Verify analysis preserved by pass P. |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 812 | void PMDataManager::verifyPreservedAnalysis(Pass *P) { |
Chris Lattner | 02eb94c | 2008-08-07 07:34:50 +0000 | [diff] [blame] | 813 | // Don't do this unless assertions are enabled. |
| 814 | #ifdef NDEBUG |
| 815 | return; |
| 816 | #endif |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 817 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 818 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 819 | |
Devang Patel | ef43253 | 2007-07-19 05:36:09 +0000 | [diff] [blame] | 820 | // Verify preserved analysis |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 821 | for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(), |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 822 | E = PreservedSet.end(); I != E; ++I) { |
| 823 | AnalysisID AID = *I; |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 824 | if (Pass *AP = findAnalysisPass(AID, true)) { |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 825 | TimeRegion PassTimer(getPassTimer(AP)); |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 826 | AP->verifyAnalysis(); |
Dan Gohman | 4dbb301 | 2009-09-28 00:27:48 +0000 | [diff] [blame] | 827 | } |
Devang Patel | 9dbe4d1 | 2008-07-01 17:44:24 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | |
Devang Patel | 67c79a4 | 2008-07-01 19:50:56 +0000 | [diff] [blame] | 831 | /// Remove Analysis not preserved by Pass P |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 832 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 833 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 834 | if (AnUsage->getPreservesAll()) |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 835 | return; |
| 836 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 837 | const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet(); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 838 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | be6bd55e | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 839 | E = AvailableAnalysis.end(); I != E; ) { |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 840 | std::map<AnalysisID, Pass*>::iterator Info = I++; |
Chris Lattner | 21889d7 | 2010-01-22 04:55:08 +0000 | [diff] [blame] | 841 | if (Info->second->getAsImmutablePass() == 0 && |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 842 | std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 843 | PreservedSet.end()) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 844 | // Remove this analysis |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 845 | if (PassDebugging >= Details) { |
| 846 | Pass *S = Info->second; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 847 | dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 848 | dbgs() << S->getPassName() << "'\n"; |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 849 | } |
Dan Gohman | 193e4c0 | 2008-11-06 21:57:17 +0000 | [diff] [blame] | 850 | AvailableAnalysis.erase(Info); |
Devang Patel | bb4720c | 2008-06-03 01:02:16 +0000 | [diff] [blame] | 851 | } |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 852 | } |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 853 | |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 854 | // Check inherited analysis also. If P is not preserving analysis |
| 855 | // provided by parent manager then remove it here. |
| 856 | for (unsigned Index = 0; Index < PMT_Last; ++Index) { |
| 857 | |
| 858 | if (!InheritedAnalysis[Index]) |
| 859 | continue; |
| 860 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 861 | for (std::map<AnalysisID, Pass*>::iterator |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 862 | I = InheritedAnalysis[Index]->begin(), |
| 863 | E = InheritedAnalysis[Index]->end(); I != E; ) { |
| 864 | std::map<AnalysisID, Pass *>::iterator Info = I++; |
Chris Lattner | 21889d7 | 2010-01-22 04:55:08 +0000 | [diff] [blame] | 865 | if (Info->second->getAsImmutablePass() == 0 && |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 866 | std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 867 | PreservedSet.end()) { |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 868 | // Remove this analysis |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 869 | if (PassDebugging >= Details) { |
| 870 | Pass *S = Info->second; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 871 | dbgs() << " -- '" << P->getPassName() << "' is not preserving '"; |
| 872 | dbgs() << S->getPassName() << "'\n"; |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 873 | } |
Devang Patel | 01919d2 | 2007-03-08 19:05:01 +0000 | [diff] [blame] | 874 | InheritedAnalysis[Index]->erase(Info); |
Andreas Neustifter | 4665141 | 2009-12-04 06:58:24 +0000 | [diff] [blame] | 875 | } |
Devang Patel | 42dd1e9 | 2007-03-06 01:55:46 +0000 | [diff] [blame] | 876 | } |
| 877 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 880 | /// Remove analysis passes that are not used any longer |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 881 | void PMDataManager::removeDeadPasses(Pass *P, StringRef Msg, |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 882 | enum PassDebuggingString DBG_STR) { |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 883 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 884 | SmallVector<Pass *, 12> DeadPasses; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 885 | |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 886 | // If this is a on the fly manager then it does not have TPM. |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 887 | if (!TPM) |
| 888 | return; |
| 889 | |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 890 | TPM->collectLastUses(DeadPasses, P); |
| 891 | |
Devang Patel | 656a917 | 2008-06-06 17:50:36 +0000 | [diff] [blame] | 892 | if (PassDebugging >= Details && !DeadPasses.empty()) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 893 | dbgs() << " -*- '" << P->getPassName(); |
| 894 | dbgs() << "' is the last user of following pass instances."; |
| 895 | dbgs() << " Free these instances\n"; |
Evan Cheng | 93af6ce | 2008-06-04 09:13:31 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 898 | for (SmallVectorImpl<Pass *>::iterator I = DeadPasses.begin(), |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 899 | E = DeadPasses.end(); I != E; ++I) |
| 900 | freePass(*I, Msg, DBG_STR); |
| 901 | } |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 902 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 903 | void PMDataManager::freePass(Pass *P, StringRef Msg, |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 904 | enum PassDebuggingString DBG_STR) { |
| 905 | dumpPassInfo(P, FREEING_MSG, DBG_STR, Msg); |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 906 | |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 907 | { |
| 908 | // If the pass crashes releasing memory, remember this. |
| 909 | PassManagerPrettyStackEntry X(P); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 910 | TimeRegion PassTimer(getPassTimer(P)); |
| 911 | |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 912 | P->releaseMemory(); |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 915 | AnalysisID PI = P->getPassID(); |
| 916 | if (const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(PI)) { |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 917 | // Remove the pass itself (if it is not already removed). |
| 918 | AvailableAnalysis.erase(PI); |
| 919 | |
| 920 | // Remove all interfaces this pass implements, for which it is also |
| 921 | // listed as the available implementation. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 922 | const std::vector<const PassInfo*> &II = PInf->getInterfacesImplemented(); |
Owen Anderson | 3183ef1 | 2010-07-20 16:55:05 +0000 | [diff] [blame] | 923 | for (unsigned i = 0, e = II.size(); i != e; ++i) { |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 924 | std::map<AnalysisID, Pass*>::iterator Pos = |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 925 | AvailableAnalysis.find(II[i]->getTypeInfo()); |
Dan Gohman | 5e8ba5d | 2009-09-27 23:38:27 +0000 | [diff] [blame] | 926 | if (Pos != AvailableAnalysis.end() && Pos->second == P) |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 927 | AvailableAnalysis.erase(Pos); |
Devang Patel | c3e3ca9 | 2008-10-06 20:36:36 +0000 | [diff] [blame] | 928 | } |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 929 | } |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 930 | } |
| 931 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 932 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 933 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 934 | void PMDataManager::add(Pass *P, bool ProcessAnalysis) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 935 | // This manager is going to manage pass P. Set up analysis resolver |
| 936 | // to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 937 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 938 | P->setResolver(AR); |
| 939 | |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 940 | // If a FunctionPass F is the last user of ModulePass info M |
| 941 | // then the F's manager, not F, records itself as a last user of M. |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 942 | SmallVector<Pass *, 12> TransferLastUses; |
Devang Patel | ec2b9a7 | 2007-03-05 22:57:49 +0000 | [diff] [blame] | 943 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 944 | if (!ProcessAnalysis) { |
| 945 | // Add pass |
| 946 | PassVector.push_back(P); |
| 947 | return; |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 948 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 949 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 950 | // At the moment, this pass is the last user of all required passes. |
| 951 | SmallVector<Pass *, 12> LastUses; |
| 952 | SmallVector<Pass *, 8> RequiredPasses; |
| 953 | SmallVector<AnalysisID, 8> ReqAnalysisNotAvailable; |
| 954 | |
| 955 | unsigned PDepth = this->getDepth(); |
| 956 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 957 | collectRequiredAnalysis(RequiredPasses, |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 958 | ReqAnalysisNotAvailable, P); |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 959 | for (SmallVectorImpl<Pass *>::iterator I = RequiredPasses.begin(), |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 960 | E = RequiredPasses.end(); I != E; ++I) { |
| 961 | Pass *PRequired = *I; |
| 962 | unsigned RDepth = 0; |
| 963 | |
| 964 | assert(PRequired->getResolver() && "Analysis Resolver is not set"); |
| 965 | PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); |
| 966 | RDepth = DM.getDepth(); |
| 967 | |
| 968 | if (PDepth == RDepth) |
| 969 | LastUses.push_back(PRequired); |
| 970 | else if (PDepth > RDepth) { |
| 971 | // Let the parent claim responsibility of last use |
| 972 | TransferLastUses.push_back(PRequired); |
| 973 | // Keep track of higher level analysis used by this manager. |
| 974 | HigherLevelAnalysis.push_back(PRequired); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 975 | } else |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 976 | llvm_unreachable("Unable to accommodate Required Pass"); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | // Set P as P's last user until someone starts using P. |
| 980 | // However, if P is a Pass Manager then it does not need |
| 981 | // to record its last user. |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 982 | if (P->getAsPMDataManager() == 0) |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 983 | LastUses.push_back(P); |
| 984 | TPM->setLastUser(LastUses, P); |
| 985 | |
| 986 | if (!TransferLastUses.empty()) { |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 987 | Pass *My_PM = getAsPass(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 988 | TPM->setLastUser(TransferLastUses, My_PM); |
| 989 | TransferLastUses.clear(); |
| 990 | } |
| 991 | |
Dan Gohman | 6304db3 | 2010-08-16 22:57:28 +0000 | [diff] [blame] | 992 | // Now, take care of required analyses that are not available. |
Dan Gohman | 060d5ba | 2010-10-12 00:15:27 +0000 | [diff] [blame] | 993 | for (SmallVectorImpl<AnalysisID>::iterator |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 994 | I = ReqAnalysisNotAvailable.begin(), |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 995 | E = ReqAnalysisNotAvailable.end() ;I != E; ++I) { |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 996 | const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(*I); |
| 997 | Pass *AnalysisPass = PI->createPass(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 998 | this->addLowerLevelRequiredPass(P, AnalysisPass); |
| 999 | } |
| 1000 | |
| 1001 | // Take a note of analysis required and made available by this pass. |
| 1002 | // Remove the analysis not preserved by this pass |
| 1003 | removeNotPreservedAnalysis(P); |
| 1004 | recordAvailableAnalysis(P); |
| 1005 | |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1006 | // Add pass |
| 1007 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1010 | |
| 1011 | /// Populate RP with analysis pass that are required by |
| 1012 | /// pass P and are available. Populate RP_NotAvail with analysis |
| 1013 | /// pass that are required by pass P but are not available. |
Dan Gohman | 7224bce | 2010-10-12 00:11:18 +0000 | [diff] [blame] | 1014 | void PMDataManager::collectRequiredAnalysis(SmallVectorImpl<Pass *> &RP, |
| 1015 | SmallVectorImpl<AnalysisID> &RP_NotAvail, |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1016 | Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 1017 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 1018 | const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet(); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1019 | for (AnalysisUsage::VectorType::const_iterator |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1020 | I = RequiredSet.begin(), E = RequiredSet.end(); I != E; ++I) { |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1021 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1022 | RP.push_back(AnalysisPass); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1023 | else |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1024 | RP_NotAvail.push_back(*I); |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 1025 | } |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 1026 | |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 1027 | const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet(); |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 1028 | for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(), |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 1029 | E = IDs.end(); I != E; ++I) { |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1030 | if (Pass *AnalysisPass = findAnalysisPass(*I, true)) |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1031 | RP.push_back(AnalysisPass); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1032 | else |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1033 | RP_NotAvail.push_back(*I); |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 1034 | } |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 1037 | // All Required analyses should be available to the pass as it runs! Here |
| 1038 | // we fill in the AnalysisImpls member of the pass so that it can |
| 1039 | // successfully use the getAnalysis() method to retrieve the |
| 1040 | // implementations it needs. |
| 1041 | // |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 1042 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 1043 | AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P); |
| 1044 | |
Chris Lattner | cbd160f | 2008-08-08 05:33:04 +0000 | [diff] [blame] | 1045 | for (AnalysisUsage::VectorType::const_iterator |
Devang Patel | ec9e1a60a | 2008-08-11 21:13:39 +0000 | [diff] [blame] | 1046 | I = AnUsage->getRequiredSet().begin(), |
| 1047 | E = AnUsage->getRequiredSet().end(); I != E; ++I) { |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1048 | Pass *Impl = findAnalysisPass(*I, true); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 1049 | if (Impl == 0) |
Devang Patel | 56a5c62 | 2007-04-16 20:44:16 +0000 | [diff] [blame] | 1050 | // This may be analysis pass that is initialized on the fly. |
| 1051 | // If that is not the case then it will raise an assert when it is used. |
| 1052 | continue; |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 1053 | AnalysisResolver *AR = P->getResolver(); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1054 | assert(AR && "Analysis Resolver is not set"); |
Devang Patel | 984698a | 2006-12-09 01:11:34 +0000 | [diff] [blame] | 1055 | AR->addAnalysisImplsPair(*I, Impl); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 1056 | } |
| 1057 | } |
| 1058 | |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1059 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 1060 | /// then return NULL. |
| 1061 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 1062 | |
| 1063 | // Check if AvailableAnalysis map has one entry. |
| 1064 | std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
| 1065 | |
| 1066 | if (I != AvailableAnalysis.end()) |
| 1067 | return I->second; |
| 1068 | |
| 1069 | // Search Parents through TopLevelManager |
| 1070 | if (SearchParent) |
| 1071 | return TPM->findAnalysisPass(AID); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1072 | |
Devang Patel | 9d759b8 | 2006-12-09 00:09:12 +0000 | [diff] [blame] | 1073 | return NULL; |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1076 | // Print list of passes that are last used by P. |
| 1077 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
| 1078 | |
Devang Patel | 8adae86 | 2007-07-20 18:04:54 +0000 | [diff] [blame] | 1079 | SmallVector<Pass *, 12> LUses; |
Devang Patel | 2ff4492 | 2007-04-16 20:39:59 +0000 | [diff] [blame] | 1080 | |
| 1081 | // If this is a on the fly manager then it does not have TPM. |
| 1082 | if (!TPM) |
| 1083 | return; |
| 1084 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1085 | TPM->collectLastUses(LUses, P); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1086 | |
Dan Gohman | 7224bce | 2010-10-12 00:11:18 +0000 | [diff] [blame] | 1087 | for (SmallVectorImpl<Pass *>::iterator I = LUses.begin(), |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1088 | E = LUses.end(); I != E; ++I) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1089 | llvm::dbgs() << "--" << std::string(Offset*2, ' '); |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 1090 | (*I)->dumpPassStructure(0); |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | void PMDataManager::dumpPassArguments() const { |
Dan Gohman | 7224bce | 2010-10-12 00:11:18 +0000 | [diff] [blame] | 1095 | for (SmallVectorImpl<Pass *>::const_iterator I = PassVector.begin(), |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1096 | E = PassVector.end(); I != E; ++I) { |
Chris Lattner | 2fa26e5 | 2010-01-22 05:24:46 +0000 | [diff] [blame] | 1097 | if (PMDataManager *PMD = (*I)->getAsPMDataManager()) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1098 | PMD->dumpPassArguments(); |
| 1099 | else |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1100 | if (const PassInfo *PI = |
| 1101 | PassRegistry::getPassRegistry()->getPassInfo((*I)->getPassID())) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1102 | if (!PI->isAnalysisGroup()) |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1103 | dbgs() << " -" << PI->getPassArgument(); |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Chris Lattner | dd6304f | 2007-08-10 06:17:04 +0000 | [diff] [blame] | 1107 | void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1, |
| 1108 | enum PassDebuggingString S2, |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1109 | StringRef Msg) { |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 1110 | if (PassDebugging < Executions) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1111 | return; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1112 | dbgs() << (void*)this << std::string(getDepth()*2+1, ' '); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1113 | switch (S1) { |
| 1114 | case EXECUTION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1115 | dbgs() << "Executing Pass '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1116 | break; |
| 1117 | case MODIFICATION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1118 | dbgs() << "Made Modification '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1119 | break; |
| 1120 | case FREEING_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1121 | dbgs() << " Freeing Pass '" << P->getPassName(); |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1122 | break; |
| 1123 | default: |
| 1124 | break; |
| 1125 | } |
| 1126 | switch (S2) { |
| 1127 | case ON_BASICBLOCK_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1128 | dbgs() << "' on BasicBlock '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1129 | break; |
| 1130 | case ON_FUNCTION_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1131 | dbgs() << "' on Function '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1132 | break; |
| 1133 | case ON_MODULE_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1134 | dbgs() << "' on Module '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1135 | break; |
Tobias Grosser | 23c8341 | 2010-10-20 01:54:44 +0000 | [diff] [blame] | 1136 | case ON_REGION_MSG: |
| 1137 | dbgs() << "' on Region '" << Msg << "'...\n"; |
| 1138 | break; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1139 | case ON_LOOP_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1140 | dbgs() << "' on Loop '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1141 | break; |
| 1142 | case ON_CG_MSG: |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1143 | dbgs() << "' on Call Graph Nodes '" << Msg << "'...\n"; |
Devang Patel | 003a559 | 2007-03-05 20:01:30 +0000 | [diff] [blame] | 1144 | break; |
| 1145 | default: |
| 1146 | break; |
| 1147 | } |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1150 | void PMDataManager::dumpRequiredSet(const Pass *P) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1151 | if (PassDebugging < Details) |
| 1152 | return; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1153 | |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1154 | AnalysisUsage analysisUsage; |
| 1155 | P->getAnalysisUsage(analysisUsage); |
| 1156 | dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet()); |
| 1157 | } |
| 1158 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1159 | void PMDataManager::dumpPreservedSet(const Pass *P) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1160 | if (PassDebugging < Details) |
| 1161 | return; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1162 | |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1163 | AnalysisUsage analysisUsage; |
| 1164 | P->getAnalysisUsage(analysisUsage); |
| 1165 | dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet()); |
| 1166 | } |
| 1167 | |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 1168 | void PMDataManager::dumpAnalysisUsage(StringRef Msg, const Pass *P, |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1169 | const AnalysisUsage::VectorType &Set) const { |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1170 | assert(PassDebugging >= Details); |
| 1171 | if (Set.empty()) |
| 1172 | return; |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1173 | dbgs() << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1174 | for (unsigned i = 0; i != Set.size(); ++i) { |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1175 | if (i) dbgs() << ','; |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1176 | const PassInfo *PInf = PassRegistry::getPassRegistry()->getPassInfo(Set[i]); |
Andrew Trick | 6bbaf13 | 2011-06-03 00:48:58 +0000 | [diff] [blame] | 1177 | if (!PInf) { |
| 1178 | // Some preserved passes, such as AliasAnalysis, may not be initialized by |
| 1179 | // all drivers. |
| 1180 | dbgs() << " Uninitialized Pass"; |
| 1181 | continue; |
| 1182 | } |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1183 | dbgs() << ' ' << PInf->getPassName(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1184 | } |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1185 | dbgs() << '\n'; |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 1186 | } |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1187 | |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1188 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1189 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1190 | /// through getAnalysis interface. |
| 1191 | /// This should be handled by specific pass manager. |
| 1192 | void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
| 1193 | if (TPM) { |
| 1194 | TPM->dumpArguments(); |
| 1195 | TPM->dumpPasses(); |
| 1196 | } |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1197 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1198 | // Module Level pass may required Function Level analysis info |
| 1199 | // (e.g. dominator info). Pass manager uses on the fly function pass manager |
| 1200 | // to provide this on demand. In that case, in Pass manager terminology, |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1201 | // module level pass is requiring lower level analysis info managed by |
| 1202 | // lower level pass manager. |
| 1203 | |
| 1204 | // When Pass manager is not able to order required analysis info, Pass manager |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1205 | // checks whether any lower level manager will be able to provide this |
Devang Patel | 8df7cc1 | 2008-02-02 01:43:30 +0000 | [diff] [blame] | 1206 | // analysis info on demand or not. |
Devang Patel | ab85d6b | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1207 | #ifndef NDEBUG |
David Greene | 994e1bb | 2010-01-05 01:30:02 +0000 | [diff] [blame] | 1208 | dbgs() << "Unable to schedule '" << RequiredPass->getPassName(); |
| 1209 | dbgs() << "' required by '" << P->getPassName() << "'\n"; |
Devang Patel | ab85d6b | 2008-06-03 01:20:02 +0000 | [diff] [blame] | 1210 | #endif |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1211 | llvm_unreachable("Unable to schedule pass"); |
Devang Patel | 004937b | 2007-07-27 20:06:09 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1214 | Pass *PMDataManager::getOnTheFlyPass(Pass *P, AnalysisID PI, Function &F) { |
Craig Topper | c514b54 | 2012-02-05 22:14:15 +0000 | [diff] [blame] | 1215 | llvm_unreachable("Unable to find on the fly pass"); |
Dan Gohman | ffdee30 | 2010-06-21 18:46:45 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1218 | // Destructor |
| 1219 | PMDataManager::~PMDataManager() { |
Dan Gohman | 7224bce | 2010-10-12 00:11:18 +0000 | [diff] [blame] | 1220 | for (SmallVectorImpl<Pass *>::iterator I = PassVector.begin(), |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1221 | E = PassVector.end(); I != E; ++I) |
| 1222 | delete *I; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1225 | //===----------------------------------------------------------------------===// |
| 1226 | // NOTE: Is this the right place to define this method ? |
Duncan Sands | 5a913d6 | 2009-01-28 13:14:17 +0000 | [diff] [blame] | 1227 | // getAnalysisIfAvailable - Return analysis result or null if it doesn't exist. |
| 1228 | Pass *AnalysisResolver::getAnalysisIfAvailable(AnalysisID ID, bool dir) const { |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 1229 | return PM.findAnalysisPass(ID, dir); |
| 1230 | } |
| 1231 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1232 | Pass *AnalysisResolver::findImplPass(Pass *P, AnalysisID AnalysisPI, |
Devang Patel | 9294281 | 2007-04-16 20:56:24 +0000 | [diff] [blame] | 1233 | Function &F) { |
| 1234 | return PM.getOnTheFlyPass(P, AnalysisPI, F); |
| 1235 | } |
| 1236 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1237 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1238 | // BBPassManager implementation |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1239 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1240 | /// Execute all of the passes scheduled for execution by invoking |
| 1241 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1242 | /// the function, and if so, return true. |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1243 | bool BBPassManager::runOnFunction(Function &F) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1244 | if (F.isDeclaration()) |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1245 | return false; |
| 1246 | |
Devang Patel | e958559 | 2006-12-08 01:38:28 +0000 | [diff] [blame] | 1247 | bool Changed = doInitialization(F); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1248 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1249 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1250 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1251 | BasicBlockPass *BP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1252 | bool LocalChanged = false; |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1253 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1254 | dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1255 | dumpRequiredSet(BP); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1256 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1257 | initializeAnalysisImpl(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1258 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1259 | { |
| 1260 | // If the pass crashes, remember this. |
| 1261 | PassManagerPrettyStackEntry X(BP, *I); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1262 | TimeRegion PassTimer(getPassTimer(BP)); |
| 1263 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1264 | LocalChanged |= BP->runOnBasicBlock(*I); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1265 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1266 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1267 | Changed |= LocalChanged; |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1268 | if (LocalChanged) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1269 | dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG, |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1270 | I->getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1271 | dumpPreservedSet(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1272 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1273 | verifyPreservedAnalysis(BP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1274 | removeNotPreservedAnalysis(BP); |
| 1275 | recordAvailableAnalysis(BP); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1276 | removeDeadPasses(BP, I->getName(), ON_BASICBLOCK_MSG); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1277 | } |
Chris Lattner | de2aa65 | 2007-08-10 06:22:25 +0000 | [diff] [blame] | 1278 | |
Bill Wendling | 6ce6d26 | 2009-12-25 13:50:18 +0000 | [diff] [blame] | 1279 | return doFinalization(F) || Changed; |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1282 | // Implement doInitialization and doFinalization |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1283 | bool BBPassManager::doInitialization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1284 | bool Changed = false; |
| 1285 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1286 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1287 | Changed |= getContainedPass(Index)->doInitialization(M); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1288 | |
| 1289 | return Changed; |
| 1290 | } |
| 1291 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1292 | bool BBPassManager::doFinalization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1293 | bool Changed = false; |
| 1294 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1295 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1296 | Changed |= getContainedPass(Index)->doFinalization(M); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1297 | |
| 1298 | return Changed; |
| 1299 | } |
| 1300 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1301 | bool BBPassManager::doInitialization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1302 | bool Changed = false; |
| 1303 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1304 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1305 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1306 | Changed |= BP->doInitialization(F); |
| 1307 | } |
| 1308 | |
| 1309 | return Changed; |
| 1310 | } |
| 1311 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1312 | bool BBPassManager::doFinalization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1313 | bool Changed = false; |
| 1314 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1315 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1316 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 1317 | Changed |= BP->doFinalization(F); |
| 1318 | } |
| 1319 | |
| 1320 | return Changed; |
| 1321 | } |
| 1322 | |
| 1323 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1324 | //===----------------------------------------------------------------------===// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1325 | // FunctionPassManager implementation |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1326 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1327 | /// Create new Function pass manager |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1328 | FunctionPassManager::FunctionPassManager(Module *m) : M(m) { |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1329 | FPM = new FunctionPassManagerImpl(); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1330 | // FPM is the top level manager. |
| 1331 | FPM->setTopLevelManager(FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1332 | |
Dan Gohman | 565df95 | 2008-03-13 02:08:36 +0000 | [diff] [blame] | 1333 | AnalysisResolver *AR = new AnalysisResolver(*FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 1334 | FPM->setResolver(AR); |
Devang Patel | 1f65368 | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1337 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1338 | delete FPM; |
| 1339 | } |
| 1340 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1341 | /// add - Add a pass to the queue of passes to run. This passes |
| 1342 | /// ownership of the Pass to the PassManager. When the |
| 1343 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 1344 | /// there is no need to delete the pass. (TODO delete passes.) |
| 1345 | /// This implies that all passes MUST be allocated with 'new'. |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1346 | void FunctionPassManager::add(Pass *P) { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1347 | FPM->add(P); |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1350 | /// run - Execute all of the passes scheduled for execution. Keep |
| 1351 | /// track of whether any of the passes modifies the function, and if |
| 1352 | /// so, return true. |
| 1353 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1354 | bool FunctionPassManager::run(Function &F) { |
Nick Lewycky | 94e168f | 2010-02-15 21:27:56 +0000 | [diff] [blame] | 1355 | if (F.isMaterializable()) { |
| 1356 | std::string errstr; |
Chris Lattner | b6166b3 | 2010-04-07 22:41:29 +0000 | [diff] [blame] | 1357 | if (F.Materialize(&errstr)) |
Benjamin Kramer | a676926 | 2010-04-08 10:44:28 +0000 | [diff] [blame] | 1358 | report_fatal_error("Error reading bitcode file: " + Twine(errstr)); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1359 | } |
Devang Patel | 272908d | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 1360 | return FPM->run(F); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1364 | /// doInitialization - Run all of the initializers for the function passes. |
| 1365 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1366 | bool FunctionPassManager::doInitialization() { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1367 | return FPM->doInitialization(*M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Dan Gohman | e6656eb | 2007-07-30 14:51:13 +0000 | [diff] [blame] | 1370 | /// doFinalization - Run all of the finalizers for the function passes. |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1371 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1372 | bool FunctionPassManager::doFinalization() { |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1373 | return FPM->doFinalization(*M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1376 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1377 | // FunctionPassManagerImpl implementation |
| 1378 | // |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1379 | bool FunctionPassManagerImpl::doInitialization(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1380 | bool Changed = false; |
| 1381 | |
Dan Gohman | 05ebc8f | 2009-11-23 16:24:18 +0000 | [diff] [blame] | 1382 | dumpArguments(); |
| 1383 | dumpPasses(); |
| 1384 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1385 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1386 | Changed |= getContainedManager(Index)->doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1387 | |
| 1388 | return Changed; |
| 1389 | } |
| 1390 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1391 | bool FunctionPassManagerImpl::doFinalization(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1392 | bool Changed = false; |
| 1393 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1394 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1395 | Changed |= getContainedManager(Index)->doFinalization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1396 | |
| 1397 | return Changed; |
| 1398 | } |
| 1399 | |
Devang Patel | ec9c58f | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1400 | /// cleanup - After running all passes, clean up pass manager cache. |
| 1401 | void FPPassManager::cleanup() { |
| 1402 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1403 | FunctionPass *FP = getContainedPass(Index); |
| 1404 | AnalysisResolver *AR = FP->getResolver(); |
| 1405 | assert(AR && "Analysis Resolver is not set"); |
| 1406 | AR->clearAnalysisImpls(); |
| 1407 | } |
| 1408 | } |
| 1409 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1410 | void FunctionPassManagerImpl::releaseMemoryOnTheFly() { |
| 1411 | if (!wasRun) |
| 1412 | return; |
| 1413 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1414 | FPPassManager *FPPM = getContainedManager(Index); |
| 1415 | for (unsigned Index = 0; Index < FPPM->getNumContainedPasses(); ++Index) { |
| 1416 | FPPM->getContainedPass(Index)->releaseMemory(); |
| 1417 | } |
| 1418 | } |
Torok Edwin | 896556e | 2009-06-29 21:05:10 +0000 | [diff] [blame] | 1419 | wasRun = false; |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1422 | // Execute all the passes managed by this top level manager. |
| 1423 | // Return true if any function is modified by a pass. |
| 1424 | bool FunctionPassManagerImpl::run(Function &F) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1425 | bool Changed = false; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1426 | TimingInfo::createTheTimeInfo(); |
| 1427 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1428 | initializeAllAnalysisInfo(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1429 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1430 | Changed |= getContainedManager(Index)->runOnFunction(F); |
Devang Patel | ec9c58f | 2009-04-01 22:34:41 +0000 | [diff] [blame] | 1431 | |
| 1432 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1433 | getContainedManager(Index)->cleanup(); |
| 1434 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1435 | wasRun = true; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1436 | return Changed; |
| 1437 | } |
| 1438 | |
| 1439 | //===----------------------------------------------------------------------===// |
| 1440 | // FPPassManager implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1441 | |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 1442 | char FPPassManager::ID = 0; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1443 | /// Print passes managed by this manager |
| 1444 | void FPPassManager::dumpPassStructure(unsigned Offset) { |
Benjamin Kramer | cc863b2 | 2011-10-16 16:30:34 +0000 | [diff] [blame] | 1445 | dbgs().indent(Offset*2) << "FunctionPass Manager\n"; |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1446 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1447 | FunctionPass *FP = getContainedPass(Index); |
Dan Gohman | f71c521 | 2010-08-19 01:29:07 +0000 | [diff] [blame] | 1448 | FP->dumpPassStructure(Offset + 1); |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 1449 | dumpLastUses(FP, Offset+1); |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1454 | /// Execute all of the passes scheduled for execution by invoking |
| 1455 | /// runOnFunction method. Keep track of whether any of the passes modifies |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 1456 | /// the function, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1457 | bool FPPassManager::runOnFunction(Function &F) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1458 | if (F.isDeclaration()) |
| 1459 | return false; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1460 | |
| 1461 | bool Changed = false; |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1462 | |
Devang Patel | cbbf291 | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1463 | // Collect inherited analysis from Module level pass manager. |
| 1464 | populateInheritedAnalysis(TPM->activeStack); |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 1465 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1466 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1467 | FunctionPass *FP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1468 | bool LocalChanged = false; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1469 | |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1470 | dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1471 | dumpRequiredSet(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1472 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1473 | initializeAnalysisImpl(FP); |
Eric Christopher | 3c0d516 | 2012-03-23 03:54:05 +0000 | [diff] [blame] | 1474 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1475 | { |
| 1476 | PassManagerPrettyStackEntry X(FP, F); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1477 | TimeRegion PassTimer(getPassTimer(FP)); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1478 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1479 | LocalChanged |= FP->runOnFunction(F); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1480 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1481 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1482 | Changed |= LocalChanged; |
| 1483 | if (LocalChanged) |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1484 | dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getName()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1485 | dumpPreservedSet(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1486 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1487 | verifyPreservedAnalysis(FP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1488 | removeNotPreservedAnalysis(FP); |
| 1489 | recordAvailableAnalysis(FP); |
Daniel Dunbar | 9813b0b | 2009-07-26 07:49:05 +0000 | [diff] [blame] | 1490 | removeDeadPasses(FP, F.getName(), ON_FUNCTION_MSG); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1491 | } |
| 1492 | return Changed; |
| 1493 | } |
| 1494 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1495 | bool FPPassManager::runOnModule(Module &M) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1496 | bool Changed = doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1497 | |
Dan Gohman | e7630be | 2010-05-11 20:30:00 +0000 | [diff] [blame] | 1498 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Bill Wendling | d12cec8 | 2011-08-08 23:01:10 +0000 | [diff] [blame] | 1499 | Changed |= runOnFunction(*I); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1500 | |
Bill Wendling | 6ce6d26 | 2009-12-25 13:50:18 +0000 | [diff] [blame] | 1501 | return doFinalization(M) || Changed; |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1504 | bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1505 | bool Changed = false; |
| 1506 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1507 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1508 | Changed |= getContainedPass(Index)->doInitialization(M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1509 | |
| 1510 | return Changed; |
| 1511 | } |
| 1512 | |
Duncan Sands | 5149560 | 2009-02-13 09:42:34 +0000 | [diff] [blame] | 1513 | bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1514 | bool Changed = false; |
| 1515 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1516 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) |
| 1517 | Changed |= getContainedPass(Index)->doFinalization(M); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1518 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1519 | return Changed; |
| 1520 | } |
| 1521 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1522 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1523 | // MPPassManager implementation |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1524 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1525 | /// Execute all of the passes scheduled for execution by invoking |
| 1526 | /// runOnModule method. Keep track of whether any of the passes modifies |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1527 | /// the module, and if so, return true. |
| 1528 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1529 | MPPassManager::runOnModule(Module &M) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1530 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1531 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1532 | // Initialize on-the-fly passes |
| 1533 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
| 1534 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 1535 | I != E; ++I) { |
| 1536 | FunctionPassManagerImpl *FPP = I->second; |
| 1537 | Changed |= FPP->doInitialization(M); |
| 1538 | } |
| 1539 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1540 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1541 | ModulePass *MP = getContainedPass(Index); |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1542 | bool LocalChanged = false; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1543 | |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1544 | dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG, M.getModuleIdentifier()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1545 | dumpRequiredSet(MP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1546 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1547 | initializeAnalysisImpl(MP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1548 | |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1549 | { |
| 1550 | PassManagerPrettyStackEntry X(MP, M); |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1551 | TimeRegion PassTimer(getPassTimer(MP)); |
| 1552 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1553 | LocalChanged |= MP->runOnModule(M); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1554 | } |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1555 | |
Dan Gohman | 74b189f | 2010-03-01 17:34:28 +0000 | [diff] [blame] | 1556 | Changed |= LocalChanged; |
| 1557 | if (LocalChanged) |
Dan Gohman | 929391a | 2008-01-29 12:09:55 +0000 | [diff] [blame] | 1558 | dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG, |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1559 | M.getModuleIdentifier()); |
Chris Lattner | 4c493d9 | 2008-08-08 15:14:09 +0000 | [diff] [blame] | 1560 | dumpPreservedSet(MP); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1561 | |
Devang Patel | a273d1c | 2007-07-19 18:02:32 +0000 | [diff] [blame] | 1562 | verifyPreservedAnalysis(MP); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1563 | removeNotPreservedAnalysis(MP); |
| 1564 | recordAvailableAnalysis(MP); |
Benjamin Kramer | dfcc285 | 2009-12-08 13:07:38 +0000 | [diff] [blame] | 1565 | removeDeadPasses(MP, M.getModuleIdentifier(), ON_MODULE_MSG); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1566 | } |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1567 | |
| 1568 | // Finalize on-the-fly passes |
| 1569 | for (std::map<Pass *, FunctionPassManagerImpl *>::iterator |
| 1570 | I = OnTheFlyManagers.begin(), E = OnTheFlyManagers.end(); |
| 1571 | I != E; ++I) { |
| 1572 | FunctionPassManagerImpl *FPP = I->second; |
| 1573 | // We don't know when is the last time an on-the-fly pass is run, |
| 1574 | // so we need to releaseMemory / finalize here |
| 1575 | FPP->releaseMemoryOnTheFly(); |
| 1576 | Changed |= FPP->doFinalization(M); |
| 1577 | } |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1578 | return Changed; |
| 1579 | } |
| 1580 | |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1581 | /// Add RequiredPass into list of lower level passes required by pass P. |
| 1582 | /// RequiredPass is run on the fly by Pass Manager when P requests it |
| 1583 | /// through getAnalysis interface. |
| 1584 | void MPPassManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1585 | assert(P->getPotentialPassManagerType() == PMT_ModulePassManager && |
| 1586 | "Unable to handle Pass that requires lower level Analysis pass"); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1587 | assert((P->getPotentialPassManagerType() < |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1588 | RequiredPass->getPotentialPassManagerType()) && |
| 1589 | "Unable to handle Pass that requires lower level Analysis pass"); |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1590 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1591 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[P]; |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1592 | if (!FPP) { |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1593 | FPP = new FunctionPassManagerImpl(); |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1594 | // FPP is the top level manager. |
| 1595 | FPP->setTopLevelManager(FPP); |
| 1596 | |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1597 | OnTheFlyManagers[P] = FPP; |
| 1598 | } |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1599 | FPP->add(RequiredPass); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1600 | |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1601 | // Register P as the last user of RequiredPass. |
Devang Patel | 6eb3a6b | 2011-09-13 21:13:29 +0000 | [diff] [blame] | 1602 | if (RequiredPass) { |
| 1603 | SmallVector<Pass *, 1> LU; |
| 1604 | LU.push_back(RequiredPass); |
| 1605 | FPP->setLastUser(LU, P); |
| 1606 | } |
Devang Patel | e64d305 | 2007-04-16 20:12:57 +0000 | [diff] [blame] | 1607 | } |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1608 | |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1609 | /// Return function pass corresponding to PassInfo PI, that is |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1610 | /// required by module pass MP. Instantiate analysis pass, by using |
| 1611 | /// its runOnFunction() for function F. |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 1612 | Pass* MPPassManager::getOnTheFlyPass(Pass *MP, AnalysisID PI, Function &F){ |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1613 | FunctionPassManagerImpl *FPP = OnTheFlyManagers[MP]; |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1614 | assert(FPP && "Unable to find on the fly pass"); |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1615 | |
Torok Edwin | 24c7835 | 2009-06-29 18:49:09 +0000 | [diff] [blame] | 1616 | FPP->releaseMemoryOnTheFly(); |
Devang Patel | 68f72b1 | 2007-04-26 17:50:19 +0000 | [diff] [blame] | 1617 | FPP->run(F); |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1618 | return ((PMTopLevelManager*)FPP)->findAnalysisPass(PI); |
Devang Patel | 69e9f6d | 2007-04-16 20:27:05 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1622 | //===----------------------------------------------------------------------===// |
| 1623 | // PassManagerImpl implementation |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1624 | // |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1625 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1626 | /// whether any of the passes modifies the module, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1627 | bool PassManagerImpl::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1628 | bool Changed = false; |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1629 | TimingInfo::createTheTimeInfo(); |
| 1630 | |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 1631 | dumpArguments(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1632 | dumpPasses(); |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1633 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1634 | initializeAllAnalysisInfo(); |
Chris Lattner | 4c1e954 | 2009-03-06 06:45:05 +0000 | [diff] [blame] | 1635 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) |
| 1636 | Changed |= getContainedManager(Index)->runOnModule(M); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1637 | return Changed; |
| 1638 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1639 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1640 | //===----------------------------------------------------------------------===// |
| 1641 | // PassManager implementation |
| 1642 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1643 | /// Create new pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1644 | PassManager::PassManager() { |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1645 | PM = new PassManagerImpl(); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1646 | // PM is the top level manager |
| 1647 | PM->setTopLevelManager(PM); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1650 | PassManager::~PassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1651 | delete PM; |
| 1652 | } |
| 1653 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1654 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 1655 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 1656 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 1657 | /// implies that all passes MUST be allocated with 'new'. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1658 | void PassManager::add(Pass *P) { |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1659 | PM->add(P); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1663 | /// whether any of the passes modifies the module, and if so, return true. |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1664 | bool PassManager::run(Module &M) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1665 | return PM->run(M); |
| 1666 | } |
| 1667 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1668 | //===----------------------------------------------------------------------===// |
| 1669 | // TimingInfo Class - This class is used to calculate information about the |
| 1670 | // amount of time each pass takes to execute. This only happens with |
| 1671 | // -time-passes is enabled on the command line. |
| 1672 | // |
| 1673 | bool llvm::TimePassesIsEnabled = false; |
| 1674 | static cl::opt<bool,true> |
| 1675 | EnableTiming("time-passes", cl::location(TimePassesIsEnabled), |
| 1676 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
| 1677 | |
| 1678 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 1679 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 1680 | // null. It may be called multiple times. |
| 1681 | void TimingInfo::createTheTimeInfo() { |
| 1682 | if (!TimePassesIsEnabled || TheTimeInfo) return; |
| 1683 | |
| 1684 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 1685 | // This guarantees that the object will be constructed before static globals, |
| 1686 | // thus it will be destroyed before them. |
| 1687 | static ManagedStatic<TimingInfo> TTI; |
| 1688 | TheTimeInfo = &*TTI; |
| 1689 | } |
| 1690 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1691 | /// If TimingInfo is enabled then start pass timer. |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1692 | Timer *llvm::getPassTimer(Pass *P) { |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1693 | if (TheTimeInfo) |
Chris Lattner | 707431c | 2010-03-30 04:03:22 +0000 | [diff] [blame] | 1694 | return TheTimeInfo->getPassTimer(P); |
Dan Gohman | 277e767 | 2009-09-28 00:07:05 +0000 | [diff] [blame] | 1695 | return 0; |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1698 | //===----------------------------------------------------------------------===// |
| 1699 | // PMStack implementation |
| 1700 | // |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 1701 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1702 | // Pop Pass Manager from the stack and clear its analysis info. |
| 1703 | void PMStack::pop() { |
| 1704 | |
| 1705 | PMDataManager *Top = this->top(); |
| 1706 | Top->initializeAnalysisInfo(); |
| 1707 | |
| 1708 | S.pop_back(); |
| 1709 | } |
| 1710 | |
| 1711 | // Push PM on the stack and set its top level manager. |
Dan Gohman | 11eecd6 | 2008-03-13 01:21:31 +0000 | [diff] [blame] | 1712 | void PMStack::push(PMDataManager *PM) { |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1713 | assert(PM && "Unable to push. Pass Manager expected"); |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1714 | assert(PM->getDepth()==0 && "Pass Manager depth set too early"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1715 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1716 | if (!this->empty()) { |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1717 | assert(PM->getPassManagerType() > this->top()->getPassManagerType() |
| 1718 | && "pushing bad pass manager to PMStack"); |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1719 | PMTopLevelManager *TPM = this->top()->getTopLevelManager(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1720 | |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1721 | assert(TPM && "Unable to find top level manager"); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1722 | TPM->addIndirectPassManager(PM); |
| 1723 | PM->setTopLevelManager(TPM); |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1724 | PM->setDepth(this->top()->getDepth()+1); |
| 1725 | } |
| 1726 | else { |
Benjamin Kramer | 6bb5b3c | 2011-08-29 18:14:15 +0000 | [diff] [blame] | 1727 | assert((PM->getPassManagerType() == PMT_ModulePassManager |
| 1728 | || PM->getPassManagerType() == PMT_FunctionPassManager) |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1729 | && "pushing bad pass manager to PMStack"); |
| 1730 | PM->setDepth(1); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1733 | S.push_back(PM); |
| 1734 | } |
| 1735 | |
| 1736 | // Dump content of the pass manager stack. |
Dan Gohman | 027ad43 | 2010-08-07 01:04:15 +0000 | [diff] [blame] | 1737 | void PMStack::dump() const { |
| 1738 | for (std::vector<PMDataManager *>::const_iterator I = S.begin(), |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1739 | E = S.end(); I != E; ++I) |
Benjamin Kramer | 4dd515c | 2011-08-29 18:14:17 +0000 | [diff] [blame] | 1740 | dbgs() << (*I)->getAsPass()->getPassName() << ' '; |
Chris Lattner | 6098736 | 2009-03-06 05:53:14 +0000 | [diff] [blame] | 1741 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1742 | if (!S.empty()) |
Benjamin Kramer | 4dd515c | 2011-08-29 18:14:17 +0000 | [diff] [blame] | 1743 | dbgs() << '\n'; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1744 | } |
| 1745 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1746 | /// Find appropriate Module Pass Manager in the PM Stack and |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1747 | /// add self into that manager. |
| 1748 | void ModulePass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1749 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1750 | // Find Module Pass Manager |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1751 | while (!PMS.empty()) { |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1752 | PassManagerType TopPMType = PMS.top()->getPassManagerType(); |
| 1753 | if (TopPMType == PreferredType) |
| 1754 | break; // We found desired pass manager |
| 1755 | else if (TopPMType > PMT_ModulePassManager) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1756 | PMS.pop(); // Pop children pass managers |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1757 | else |
| 1758 | break; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1759 | } |
Devang Patel | 18ff636 | 2008-09-09 21:38:40 +0000 | [diff] [blame] | 1760 | assert(!PMS.empty() && "Unable to find appropriate Pass Manager"); |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1761 | PMS.top()->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1764 | /// Find appropriate Function Pass Manager or Call Graph Pass Manager |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1765 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1766 | void FunctionPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1767 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1768 | |
Andrew Trick | cbc845f | 2012-02-01 07:16:20 +0000 | [diff] [blame] | 1769 | // Find Function Pass Manager |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1770 | while (!PMS.empty()) { |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1771 | if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) |
| 1772 | PMS.pop(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1773 | else |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1774 | break; |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1775 | } |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1776 | |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1777 | // Create new Function Pass Manager if needed. |
| 1778 | FPPassManager *FPP; |
| 1779 | if (PMS.top()->getPassManagerType() == PMT_FunctionPassManager) { |
| 1780 | FPP = (FPPassManager *)PMS.top(); |
| 1781 | } else { |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1782 | assert(!PMS.empty() && "Unable to create Function Pass Manager"); |
| 1783 | PMDataManager *PMD = PMS.top(); |
| 1784 | |
| 1785 | // [1] Create new Function Pass Manager |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1786 | FPP = new FPPassManager(); |
Devang Patel | cbbf291 | 2008-03-20 01:09:53 +0000 | [diff] [blame] | 1787 | FPP->populateInheritedAnalysis(PMS); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1788 | |
| 1789 | // [2] Set up new manager's top level manager |
| 1790 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1791 | TPM->addIndirectPassManager(FPP); |
| 1792 | |
| 1793 | // [3] Assign manager to manage this new manager. This may create |
| 1794 | // and push new managers into PMS |
Devang Patel | a328690 | 2008-09-09 17:56:50 +0000 | [diff] [blame] | 1795 | FPP->assignPassManager(PMS, PMD->getPassManagerType()); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1796 | |
| 1797 | // [4] Push new manager into PMS |
| 1798 | PMS.push(FPP); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1799 | } |
| 1800 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1801 | // Assign FPP as the manager of this pass. |
| 1802 | FPP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1805 | /// Find appropriate Basic Pass Manager or Call Graph Pass Manager |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1806 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1807 | void BasicBlockPass::assignPassManager(PMStack &PMS, |
Anton Korobeynikov | fb80151 | 2007-04-16 18:10:23 +0000 | [diff] [blame] | 1808 | PassManagerType PreferredType) { |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1809 | BBPassManager *BBP; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1810 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1811 | // Basic Pass Manager is a leaf pass manager. It does not handle |
| 1812 | // any other pass manager. |
Dan Gohman | de6188a | 2010-08-12 23:50:08 +0000 | [diff] [blame] | 1813 | if (!PMS.empty() && |
Chris Lattner | 9efd4fc | 2010-01-22 05:37:10 +0000 | [diff] [blame] | 1814 | PMS.top()->getPassManagerType() == PMT_BasicBlockPassManager) { |
| 1815 | BBP = (BBPassManager *)PMS.top(); |
| 1816 | } else { |
| 1817 | // If leaf manager is not Basic Block Pass manager then create new |
| 1818 | // basic Block Pass manager. |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1819 | assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); |
| 1820 | PMDataManager *PMD = PMS.top(); |
| 1821 | |
| 1822 | // [1] Create new Basic Block Manager |
Andrew Trick | 0896621 | 2011-08-29 17:07:00 +0000 | [diff] [blame] | 1823 | BBP = new BBPassManager(); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1824 | |
| 1825 | // [2] Set up new manager's top level manager |
| 1826 | // Basic Block Pass Manager does not live by itself |
| 1827 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1828 | TPM->addIndirectPassManager(BBP); |
| 1829 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1830 | // [3] Assign manager to manage this new manager. This may create |
| 1831 | // and push new managers into PMS |
David Greene | 103d4b4 | 2010-05-10 20:24:27 +0000 | [diff] [blame] | 1832 | BBP->assignPassManager(PMS, PreferredType); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1833 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1834 | // [4] Push new manager into PMS |
| 1835 | PMS.push(BBP); |
| 1836 | } |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1837 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1838 | // Assign BBP as the manager of this pass. |
| 1839 | BBP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1840 | } |
| 1841 | |
Dan Gohman | d3a20c9 | 2008-03-11 16:41:42 +0000 | [diff] [blame] | 1842 | PassManagerBase::~PassManagerBase() {} |