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