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