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