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