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