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