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 | // |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 5 | // This file was developed by Devang Patel and is distributed under |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
Jeff Cohen | b622c11 | 2007-03-05 00:00:42 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Devang Patel | a984459 | 2006-11-11 01:31:05 +0000 | [diff] [blame] | 23 | #include <vector> |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 24 | #include <map> |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 25 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 26 | // See PassManagers.h for Pass Manager infrastructure overview. |
Devang Patel | 6fea285 | 2006-12-07 18:23:30 +0000 | [diff] [blame] | 27 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 28 | namespace llvm { |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // Pass debugging information. Often it is useful to find out what pass is |
| 32 | // running when a crash occurs in a utility. When this library is compiled with |
| 33 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 34 | // pass name to be printed before it executes. |
| 35 | // |
| 36 | |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 37 | // Different debug levels that can be enabled... |
| 38 | enum PassDebugLevel { |
| 39 | None, Arguments, Structure, Executions, Details |
| 40 | }; |
| 41 | |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 42 | static cl::opt<enum PassDebugLevel> |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 43 | PassDebugging("debug-pass", cl::Hidden, |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 44 | cl::desc("Print PassManager debugging information"), |
| 45 | cl::values( |
Devang Patel | 03fb587 | 2006-12-13 21:13:31 +0000 | [diff] [blame] | 46 | clEnumVal(None , "disable debug output"), |
| 47 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
| 48 | clEnumVal(Structure , "print pass structure before run()"), |
| 49 | clEnumVal(Executions, "print pass name before it is executed"), |
| 50 | clEnumVal(Details , "print pass details when it is executed"), |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 51 | clEnumValEnd)); |
| 52 | } // End of llvm namespace |
| 53 | |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 54 | namespace { |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 55 | |
Devang Patel | f33f3eb | 2006-12-07 19:21:29 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 57 | // BBPassManager |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 58 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 59 | /// BBPassManager manages BasicBlockPass. It batches all the |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 60 | /// pass together and sequence them to process one basic block before |
| 61 | /// processing next basic block. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 62 | class VISIBILITY_HIDDEN BBPassManager : public PMDataManager, |
| 63 | public FunctionPass { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 64 | |
| 65 | public: |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 66 | BBPassManager(int Depth) : PMDataManager(Depth) { } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 67 | |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 68 | /// Execute all of the passes scheduled for execution. Keep track of |
| 69 | /// whether any of the passes modifies the function, and if so, return true. |
| 70 | bool runOnFunction(Function &F); |
| 71 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 72 | /// Pass Manager itself does not invalidate any analysis info. |
| 73 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 74 | Info.setPreservesAll(); |
| 75 | } |
| 76 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 77 | bool doInitialization(Module &M); |
| 78 | bool doInitialization(Function &F); |
| 79 | bool doFinalization(Module &M); |
| 80 | bool doFinalization(Function &F); |
| 81 | |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 82 | virtual const char *getPassName() const { |
| 83 | return "BasicBlock Pass Manager"; |
| 84 | } |
| 85 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 86 | // Print passes managed by this manager |
| 87 | void dumpPassStructure(unsigned Offset) { |
Devang Patel | ffca910 | 2006-12-15 19:39:30 +0000 | [diff] [blame] | 88 | llvm::cerr << std::string(Offset*2, ' ') << "BasicBlockPass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 89 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 90 | BasicBlockPass *BP = getContainedPass(Index); |
| 91 | BP->dumpPassStructure(Offset + 1); |
| 92 | dumpLastUses(BP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 95 | |
| 96 | BasicBlockPass *getContainedPass(unsigned N) { |
| 97 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 98 | BasicBlockPass *BP = static_cast<BasicBlockPass *>(PassVector[N]); |
| 99 | return BP; |
| 100 | } |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 101 | |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 102 | virtual PassManagerType getPassManagerType() const { |
Devang Patel | 3b3f899 | 2007-01-11 01:10:25 +0000 | [diff] [blame] | 103 | return PMT_BasicBlockPassManager; |
| 104 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 107 | } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 108 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 109 | namespace llvm { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 110 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 111 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 112 | // FunctionPassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 113 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 114 | /// FunctionPassManagerImpl manages FPPassManagers |
| 115 | class FunctionPassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 116 | public PMDataManager, |
| 117 | public PMTopLevelManager { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 118 | public: |
| 119 | |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 120 | FunctionPassManagerImpl(int Depth) : PMDataManager(Depth), |
| 121 | PMTopLevelManager(TLM_Function) { } |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 122 | |
| 123 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 124 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 125 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 126 | /// implies that all passes MUST be allocated with 'new'. |
| 127 | void add(Pass *P) { |
| 128 | schedulePass(P); |
| 129 | } |
| 130 | |
| 131 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 132 | /// whether any of the passes modifies the module, and if so, return true. |
| 133 | bool run(Function &F); |
| 134 | |
| 135 | /// doInitialization - Run all of the initializers for the function passes. |
| 136 | /// |
| 137 | bool doInitialization(Module &M); |
| 138 | |
| 139 | /// doFinalization - Run all of the initializers for the function passes. |
| 140 | /// |
| 141 | bool doFinalization(Module &M); |
| 142 | |
| 143 | /// Pass Manager itself does not invalidate any analysis info. |
| 144 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 145 | Info.setPreservesAll(); |
| 146 | } |
| 147 | |
| 148 | inline void addTopLevelPass(Pass *P) { |
| 149 | |
| 150 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
| 151 | |
| 152 | // P is a immutable pass and it will be managed by this |
| 153 | // top level manager. Set up analysis resolver to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 154 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 155 | P->setResolver(AR); |
| 156 | initializeAnalysisImpl(P); |
| 157 | addImmutablePass(IP); |
| 158 | recordAvailableAnalysis(IP); |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 159 | } else { |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 160 | P->assignPassManager(activeStack); |
Devang Patel | 4d06ace | 2007-02-07 19:37:53 +0000 | [diff] [blame] | 161 | activeStack.handleLastUserOverflow(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 162 | } |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 163 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | FPPassManager *getContainedManager(unsigned N) { |
| 167 | assert ( N < PassManagers.size() && "Pass number out of range!"); |
| 168 | FPPassManager *FP = static_cast<FPPassManager *>(PassManagers[N]); |
| 169 | return FP; |
| 170 | } |
| 171 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | //===----------------------------------------------------------------------===// |
| 175 | // MPPassManager |
| 176 | // |
| 177 | /// MPPassManager manages ModulePasses and function pass managers. |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 178 | /// It batches all Module passes passes and function pass managers together and |
| 179 | /// sequence them to process one module. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 180 | class MPPassManager : public Pass, public PMDataManager { |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 181 | |
| 182 | public: |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 183 | MPPassManager(int Depth) : PMDataManager(Depth) { } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 184 | |
| 185 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 186 | /// whether any of the passes modifies the module, and if so, return true. |
| 187 | bool runOnModule(Module &M); |
Devang Patel | ebba970 | 2006-11-13 22:40:09 +0000 | [diff] [blame] | 188 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 189 | /// Pass Manager itself does not invalidate any analysis info. |
| 190 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 191 | Info.setPreservesAll(); |
| 192 | } |
| 193 | |
Devang Patel | e3858e6 | 2007-02-01 22:08:25 +0000 | [diff] [blame] | 194 | virtual const char *getPassName() const { |
| 195 | return "Module Pass Manager"; |
| 196 | } |
| 197 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 198 | // Print passes managed by this manager |
| 199 | void dumpPassStructure(unsigned Offset) { |
| 200 | llvm::cerr << std::string(Offset*2, ' ') << "ModulePass Manager\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 201 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 202 | ModulePass *MP = getContainedPass(Index); |
| 203 | MP->dumpPassStructure(Offset + 1); |
| 204 | dumpLastUses(MP, Offset+1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 208 | ModulePass *getContainedPass(unsigned N) { |
| 209 | assert ( N < PassVector.size() && "Pass number out of range!"); |
| 210 | ModulePass *MP = static_cast<ModulePass *>(PassVector[N]); |
| 211 | return MP; |
| 212 | } |
| 213 | |
Devang Patel | 28349ab | 2007-02-27 15:00:39 +0000 | [diff] [blame] | 214 | virtual PassManagerType getPassManagerType() const { |
| 215 | return PMT_ModulePassManager; |
| 216 | } |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 217 | }; |
| 218 | |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 219 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 220 | // PassManagerImpl |
Devang Patel | 10c2ca6 | 2006-12-12 22:47:13 +0000 | [diff] [blame] | 221 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 222 | /// PassManagerImpl manages MPPassManagers |
| 223 | class PassManagerImpl : public Pass, |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 224 | public PMDataManager, |
| 225 | public PMTopLevelManager { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 226 | |
| 227 | public: |
| 228 | |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 229 | PassManagerImpl(int Depth) : PMDataManager(Depth), |
| 230 | PMTopLevelManager(TLM_Pass) { } |
Devang Patel | 4c36e6b | 2006-12-07 23:24:58 +0000 | [diff] [blame] | 231 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 232 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 233 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 234 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 235 | /// implies that all passes MUST be allocated with 'new'. |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 236 | void add(Pass *P) { |
Devang Patel | df6c9ae | 2006-12-08 22:34:02 +0000 | [diff] [blame] | 237 | schedulePass(P); |
Devang Patel | 31217af | 2006-12-07 21:32:57 +0000 | [diff] [blame] | 238 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 239 | |
| 240 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 241 | /// whether any of the passes modifies the module, and if so, return true. |
| 242 | bool run(Module &M); |
| 243 | |
Devang Patel | f9d96b9 | 2006-12-07 19:57:52 +0000 | [diff] [blame] | 244 | /// Pass Manager itself does not invalidate any analysis info. |
| 245 | void getAnalysisUsage(AnalysisUsage &Info) const { |
| 246 | Info.setPreservesAll(); |
| 247 | } |
| 248 | |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 249 | inline void addTopLevelPass(Pass *P) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 250 | |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 251 | if (ImmutablePass *IP = dynamic_cast<ImmutablePass *> (P)) { |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 252 | |
| 253 | // P is a immutable pass and it will be managed by this |
| 254 | // top level manager. Set up analysis resolver to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 255 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 256 | P->setResolver(AR); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 257 | initializeAnalysisImpl(P); |
Devang Patel | fa971cd | 2006-12-08 23:57:43 +0000 | [diff] [blame] | 258 | addImmutablePass(IP); |
Devang Patel | 9525754 | 2006-12-12 22:21:37 +0000 | [diff] [blame] | 259 | recordAvailableAnalysis(IP); |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 260 | } else { |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 261 | P->assignPassManager(activeStack); |
Devang Patel | 4d06ace | 2007-02-07 19:37:53 +0000 | [diff] [blame] | 262 | activeStack.handleLastUserOverflow(); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 263 | } |
Devang Patel | 0f08004 | 2007-01-12 17:23:48 +0000 | [diff] [blame] | 264 | |
Devang Patel | abcd1d3 | 2006-12-07 21:27:23 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 267 | MPPassManager *getContainedManager(unsigned N) { |
| 268 | assert ( N < PassManagers.size() && "Pass number out of range!"); |
| 269 | MPPassManager *MP = static_cast<MPPassManager *>(PassManagers[N]); |
| 270 | return MP; |
| 271 | } |
| 272 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 273 | }; |
| 274 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 275 | } // End of llvm namespace |
| 276 | |
| 277 | namespace { |
| 278 | |
| 279 | //===----------------------------------------------------------------------===// |
| 280 | // TimingInfo Class - This class is used to calculate information about the |
| 281 | // amount of time each pass takes to execute. This only happens when |
| 282 | // -time-passes is enabled on the command line. |
| 283 | // |
| 284 | |
| 285 | class VISIBILITY_HIDDEN TimingInfo { |
| 286 | std::map<Pass*, Timer> TimingData; |
| 287 | TimerGroup TG; |
| 288 | |
| 289 | public: |
| 290 | // Use 'create' member to get this. |
| 291 | TimingInfo() : TG("... Pass execution timing report ...") {} |
| 292 | |
| 293 | // TimingDtor - Print out information about timing information |
| 294 | ~TimingInfo() { |
| 295 | // Delete all of the timers... |
| 296 | TimingData.clear(); |
| 297 | // TimerGroup is deleted next, printing the report. |
| 298 | } |
| 299 | |
| 300 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer |
| 301 | // to a non null value (if the -time-passes option is enabled) or it leaves it |
| 302 | // null. It may be called multiple times. |
| 303 | static void createTheTimeInfo(); |
| 304 | |
| 305 | void passStarted(Pass *P) { |
| 306 | |
| 307 | if (dynamic_cast<PMDataManager *>(P)) |
| 308 | return; |
| 309 | |
| 310 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 311 | if (I == TimingData.end()) |
| 312 | I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first; |
| 313 | I->second.startTimer(); |
| 314 | } |
| 315 | void passEnded(Pass *P) { |
| 316 | |
| 317 | if (dynamic_cast<PMDataManager *>(P)) |
| 318 | return; |
| 319 | |
| 320 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 321 | assert (I != TimingData.end() && "passStarted/passEnded not nested right!"); |
| 322 | I->second.stopTimer(); |
| 323 | } |
| 324 | }; |
| 325 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 326 | static TimingInfo *TheTimeInfo; |
| 327 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 328 | } // End of anon namespace |
Devang Patel | ca58e35 | 2006-11-08 10:05:38 +0000 | [diff] [blame] | 329 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 330 | //===----------------------------------------------------------------------===// |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 331 | // PMTopLevelManager implementation |
| 332 | |
Devang Patel | 4268fc0 | 2007-01-16 02:00:38 +0000 | [diff] [blame] | 333 | /// Initialize top level manager. Create first pass manager. |
| 334 | PMTopLevelManager::PMTopLevelManager (enum TopLevelManagerType t) { |
| 335 | |
| 336 | if (t == TLM_Pass) { |
| 337 | MPPassManager *MPP = new MPPassManager(1); |
| 338 | MPP->setTopLevelManager(this); |
| 339 | addPassManager(MPP); |
| 340 | activeStack.push(MPP); |
| 341 | } |
| 342 | else if (t == TLM_Function) { |
| 343 | FPPassManager *FPP = new FPPassManager(1); |
| 344 | FPP->setTopLevelManager(this); |
| 345 | addPassManager(FPP); |
| 346 | activeStack.push(FPP); |
| 347 | } |
| 348 | } |
| 349 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 350 | /// Set pass P as the last user of the given analysis passes. |
| 351 | void PMTopLevelManager::setLastUser(std::vector<Pass *> &AnalysisPasses, |
| 352 | Pass *P) { |
| 353 | |
| 354 | for (std::vector<Pass *>::iterator I = AnalysisPasses.begin(), |
| 355 | E = AnalysisPasses.end(); I != E; ++I) { |
| 356 | Pass *AP = *I; |
| 357 | LastUser[AP] = P; |
| 358 | // If AP is the last user of other passes then make P last user of |
| 359 | // such passes. |
| 360 | for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
| 361 | LUE = LastUser.end(); LUI != LUE; ++LUI) { |
| 362 | if (LUI->second == AP) |
| 363 | LastUser[LUI->first] = P; |
| 364 | } |
| 365 | } |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | /// Collect passes whose last user is P |
| 369 | void PMTopLevelManager::collectLastUses(std::vector<Pass *> &LastUses, |
| 370 | Pass *P) { |
Chris Lattner | 9df8be4 | 2007-02-17 23:14:24 +0000 | [diff] [blame] | 371 | for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(), |
| 372 | LUE = LastUser.end(); LUI != LUE; ++LUI) |
| 373 | if (LUI->second == P) |
| 374 | LastUses.push_back(LUI->first); |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | /// Schedule pass P for execution. Make sure that passes required by |
| 378 | /// P are run before P is run. Update analysis info maintained by |
| 379 | /// the manager. Remove dead passes. This is a recursive function. |
| 380 | void PMTopLevelManager::schedulePass(Pass *P) { |
| 381 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 382 | // TODO : Allocate function manager for this pass, other wise required set |
| 383 | // may be inserted into previous function manager |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 384 | |
Devang Patel | 3f80696 | 2007-02-05 19:34:17 +0000 | [diff] [blame] | 385 | // If this Analysis is already requested by one of the previous pass |
| 386 | // and it is still available then do not insert new pass in the queue again. |
| 387 | if (findAnalysisPass(P->getPassInfo())) |
| 388 | return; |
| 389 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 390 | AnalysisUsage AnUsage; |
| 391 | P->getAnalysisUsage(AnUsage); |
| 392 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 393 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 394 | E = RequiredSet.end(); I != E; ++I) { |
| 395 | |
| 396 | Pass *AnalysisPass = findAnalysisPass(*I); |
| 397 | if (!AnalysisPass) { |
| 398 | // Schedule this analysis run first. |
| 399 | AnalysisPass = (*I)->createPass(); |
| 400 | schedulePass(AnalysisPass); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | // Now all required passes are available. |
| 405 | addTopLevelPass(P); |
| 406 | } |
| 407 | |
| 408 | /// Find the pass that implements Analysis AID. Search immutable |
| 409 | /// passes and all pass managers. If desired pass is not found |
| 410 | /// then return NULL. |
| 411 | Pass *PMTopLevelManager::findAnalysisPass(AnalysisID AID) { |
| 412 | |
| 413 | Pass *P = NULL; |
Devang Patel | cd6ba15 | 2006-12-12 22:50:05 +0000 | [diff] [blame] | 414 | // Check pass managers |
| 415 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 416 | E = PassManagers.end(); P == NULL && I != E; ++I) { |
| 417 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 418 | assert(PMD && "This is not a PassManager"); |
| 419 | P = PMD->findAnalysisPass(AID, false); |
| 420 | } |
| 421 | |
| 422 | // Check other pass managers |
| 423 | for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), |
| 424 | E = IndirectPassManagers.end(); P == NULL && I != E; ++I) |
| 425 | P = (*I)->findAnalysisPass(AID, false); |
| 426 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 427 | for (std::vector<ImmutablePass *>::iterator I = ImmutablePasses.begin(), |
| 428 | E = ImmutablePasses.end(); P == NULL && I != E; ++I) { |
| 429 | const PassInfo *PI = (*I)->getPassInfo(); |
| 430 | if (PI == AID) |
| 431 | P = *I; |
| 432 | |
| 433 | // If Pass not found then check the interfaces implemented by Immutable Pass |
| 434 | if (!P) { |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 435 | const std::vector<const PassInfo*> &ImmPI = PI->getInterfacesImplemented(); |
| 436 | if (std::find(ImmPI.begin(), ImmPI.end(), AID) != ImmPI.end()) |
| 437 | P = *I; |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 438 | } |
| 439 | } |
| 440 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 441 | return P; |
| 442 | } |
| 443 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 444 | // Print passes managed by this top level manager. |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 445 | void PMTopLevelManager::dumpPasses() const { |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 446 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 447 | if (PassDebugging < Structure) |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 448 | return; |
| 449 | |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 450 | // Print out the immutable passes |
| 451 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 452 | ImmutablePasses[i]->dumpPassStructure(0); |
| 453 | } |
| 454 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 455 | for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 456 | E = PassManagers.end(); I != E; ++I) |
| 457 | (*I)->dumpPassStructure(1); |
Devang Patel | eda5617 | 2006-12-12 23:34:33 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 460 | void PMTopLevelManager::dumpArguments() const { |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 461 | |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 462 | if (PassDebugging < Arguments) |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 463 | return; |
| 464 | |
| 465 | cerr << "Pass Arguments: "; |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 466 | for (std::vector<Pass *>::const_iterator I = PassManagers.begin(), |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 467 | E = PassManagers.end(); I != E; ++I) { |
| 468 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 469 | assert(PMD && "This is not a PassManager"); |
| 470 | PMD->dumpPassArguments(); |
| 471 | } |
| 472 | cerr << "\n"; |
| 473 | } |
| 474 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 475 | void PMTopLevelManager::initializeAllAnalysisInfo() { |
| 476 | |
| 477 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 478 | E = PassManagers.end(); I != E; ++I) { |
| 479 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I); |
| 480 | assert(PMD && "This is not a PassManager"); |
| 481 | PMD->initializeAnalysisInfo(); |
| 482 | } |
| 483 | |
| 484 | // Initailize other pass managers |
| 485 | for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(), |
| 486 | E = IndirectPassManagers.end(); I != E; ++I) |
| 487 | (*I)->initializeAnalysisInfo(); |
| 488 | } |
| 489 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 490 | /// Destructor |
| 491 | PMTopLevelManager::~PMTopLevelManager() { |
| 492 | for (std::vector<Pass *>::iterator I = PassManagers.begin(), |
| 493 | E = PassManagers.end(); I != E; ++I) |
| 494 | delete *I; |
| 495 | |
| 496 | for (std::vector<ImmutablePass *>::iterator |
| 497 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 498 | delete *I; |
| 499 | |
| 500 | PassManagers.clear(); |
| 501 | } |
| 502 | |
Devang Patel | afb1f362 | 2006-12-12 22:35:25 +0000 | [diff] [blame] | 503 | //===----------------------------------------------------------------------===// |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 504 | // PMDataManager implementation |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 505 | |
Devang Patel | d65e9e9 | 2006-11-08 01:31:28 +0000 | [diff] [blame] | 506 | /// Return true IFF pass P's required analysis set does not required new |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 507 | /// manager. |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 508 | bool PMDataManager::manageablePass(Pass *P) { |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 509 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 510 | // TODO |
| 511 | // If this pass is not preserving information that is required by a |
| 512 | // pass maintained by higher level pass manager then do not insert |
| 513 | // this pass into current manager. Use new manager. For example, |
| 514 | // For example, If FunctionPass F is not preserving ModulePass Info M1 |
| 515 | // that is used by another ModulePass M2 then do not insert F in |
| 516 | // current function pass manager. |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 517 | return true; |
| 518 | } |
| 519 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 520 | /// Augement AvailableAnalysis by adding analysis made available by pass P. |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 521 | void PMDataManager::recordAvailableAnalysis(Pass *P) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 522 | |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 523 | if (const PassInfo *PI = P->getPassInfo()) { |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 524 | AvailableAnalysis[PI] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 525 | |
Devang Patel | e9976aa | 2006-12-07 19:33:53 +0000 | [diff] [blame] | 526 | //This pass is the current implementation of all of the interfaces it |
| 527 | //implements as well. |
| 528 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 529 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 530 | AvailableAnalysis[II[i]] = P; |
Devang Patel | 643676c | 2006-11-11 01:10:19 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 534 | /// Remove Analyss not preserved by Pass P |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 535 | void PMDataManager::removeNotPreservedAnalysis(Pass *P) { |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 536 | AnalysisUsage AnUsage; |
| 537 | P->getAnalysisUsage(AnUsage); |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 538 | |
Devang Patel | 2e169c3 | 2006-12-07 20:03:49 +0000 | [diff] [blame] | 539 | if (AnUsage.getPreservesAll()) |
| 540 | return; |
| 541 | |
| 542 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
Devang Patel | f60b5d9 | 2006-11-14 01:59:59 +0000 | [diff] [blame] | 543 | for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(), |
Devang Patel | be6bd55e | 2006-12-12 23:07:44 +0000 | [diff] [blame] | 544 | E = AvailableAnalysis.end(); I != E; ) { |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 545 | std::map<AnalysisID, Pass*>::iterator Info = I++; |
| 546 | if (std::find(PreservedSet.begin(), PreservedSet.end(), Info->first) == |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 547 | PreservedSet.end()) { |
| 548 | // Remove this analysis |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 549 | if (!dynamic_cast<ImmutablePass*>(Info->second)) |
| 550 | AvailableAnalysis.erase(Info); |
| 551 | } |
Devang Patel | 349170f | 2006-11-11 01:24:55 +0000 | [diff] [blame] | 552 | } |
Devang Patel | f68a349 | 2006-11-07 22:35:17 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 555 | /// Remove analysis passes that are not used any longer |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 556 | void PMDataManager::removeDeadPasses(Pass *P, std::string &Msg) { |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 557 | |
| 558 | std::vector<Pass *> DeadPasses; |
| 559 | TPM->collectLastUses(DeadPasses, P); |
| 560 | |
| 561 | for (std::vector<Pass *>::iterator I = DeadPasses.begin(), |
| 562 | E = DeadPasses.end(); I != E; ++I) { |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 563 | |
| 564 | std::string Msg1 = " Freeing Pass '"; |
| 565 | dumpPassInfo(*I, Msg1, Msg); |
| 566 | |
Devang Patel | 7ebf09d | 2007-03-05 18:20:51 +0000 | [diff] [blame^] | 567 | if (TheTimeInfo) TheTimeInfo->passStarted(*I); |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 568 | (*I)->releaseMemory(); |
Devang Patel | 7ebf09d | 2007-03-05 18:20:51 +0000 | [diff] [blame^] | 569 | if (TheTimeInfo) TheTimeInfo->passEnded(*I); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 570 | |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 571 | std::map<AnalysisID, Pass*>::iterator Pos = |
| 572 | AvailableAnalysis.find((*I)->getPassInfo()); |
| 573 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 574 | // It is possible that pass is already removed from the AvailableAnalysis |
Devang Patel | 17ad096 | 2006-12-08 00:37:52 +0000 | [diff] [blame] | 575 | if (Pos != AvailableAnalysis.end()) |
| 576 | AvailableAnalysis.erase(Pos); |
| 577 | } |
Devang Patel | ca18926 | 2006-11-14 03:05:08 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Devang Patel | 8f677ce | 2006-12-07 18:47:25 +0000 | [diff] [blame] | 580 | /// Add pass P into the PassVector. Update |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 581 | /// AvailableAnalysis appropriately if ProcessAnalysis is true. |
Devang Patel | f85793d | 2007-01-12 20:07:16 +0000 | [diff] [blame] | 582 | void PMDataManager::add(Pass *P, |
Devang Patel | 6975b6e | 2007-01-15 23:06:56 +0000 | [diff] [blame] | 583 | bool ProcessAnalysis) { |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 584 | |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 585 | // This manager is going to manage pass P. Set up analysis resolver |
| 586 | // to connect them. |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 587 | AnalysisResolver *AR = new AnalysisResolver(*this); |
Devang Patel | d440cd9 | 2006-12-08 23:53:00 +0000 | [diff] [blame] | 588 | P->setResolver(AR); |
| 589 | |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 590 | if (ProcessAnalysis) { |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 591 | |
| 592 | // At the moment, this pass is the last user of all required passes. |
| 593 | std::vector<Pass *> LastUses; |
| 594 | std::vector<Pass *> RequiredPasses; |
| 595 | unsigned PDepth = this->getDepth(); |
| 596 | |
| 597 | collectRequiredAnalysisPasses(RequiredPasses, P); |
| 598 | for (std::vector<Pass *>::iterator I = RequiredPasses.begin(), |
| 599 | E = RequiredPasses.end(); I != E; ++I) { |
| 600 | Pass *PRequired = *I; |
| 601 | unsigned RDepth = 0; |
Devang Patel | 64619be | 2006-12-09 00:07:38 +0000 | [diff] [blame] | 602 | |
| 603 | PMDataManager &DM = PRequired->getResolver()->getPMDataManager(); |
| 604 | RDepth = DM.getDepth(); |
| 605 | |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 606 | if (PDepth == RDepth) |
| 607 | LastUses.push_back(PRequired); |
| 608 | else if (PDepth > RDepth) { |
| 609 | // Let the parent claim responsibility of last use |
Devang Patel | 832bc07 | 2006-12-15 00:08:26 +0000 | [diff] [blame] | 610 | TransferLastUses.push_back(PRequired); |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 611 | } else { |
| 612 | // Note : This feature is not yet implemented |
| 613 | assert (0 && |
| 614 | "Unable to handle Pass that requires lower level Analysis pass"); |
| 615 | } |
| 616 | } |
| 617 | |
Devang Patel | 39786a9 | 2007-01-15 20:31:54 +0000 | [diff] [blame] | 618 | // Set P as P's last user until someone starts using P. |
| 619 | // However, if P is a Pass Manager then it does not need |
| 620 | // to record its last user. |
Devang Patel | d85662f | 2007-01-16 22:38:10 +0000 | [diff] [blame] | 621 | if (!dynamic_cast<PMDataManager *>(P)) |
Devang Patel | 39786a9 | 2007-01-15 20:31:54 +0000 | [diff] [blame] | 622 | LastUses.push_back(P); |
Devang Patel | d85662f | 2007-01-16 22:38:10 +0000 | [diff] [blame] | 623 | TPM->setLastUser(LastUses, P); |
Devang Patel | bc03f13 | 2006-12-07 23:55:10 +0000 | [diff] [blame] | 624 | |
Devang Patel | 17bff0d | 2006-12-07 22:09:36 +0000 | [diff] [blame] | 625 | // Take a note of analysis required and made available by this pass. |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 626 | // Remove the analysis not preserved by this pass |
| 627 | removeNotPreservedAnalysis(P); |
Devang Patel | 17bff0d | 2006-12-07 22:09:36 +0000 | [diff] [blame] | 628 | recordAvailableAnalysis(P); |
Devang Patel | 90b05e0 | 2006-11-11 02:04:19 +0000 | [diff] [blame] | 629 | } |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 630 | |
| 631 | // Add pass |
| 632 | PassVector.push_back(P); |
Devang Patel | 8cad70d | 2006-11-11 01:51:02 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 635 | /// Populate RequiredPasses with the analysis pass that are required by |
| 636 | /// pass P. |
| 637 | void PMDataManager::collectRequiredAnalysisPasses(std::vector<Pass *> &RP, |
| 638 | Pass *P) { |
| 639 | AnalysisUsage AnUsage; |
| 640 | P->getAnalysisUsage(AnUsage); |
| 641 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
| 642 | for (std::vector<AnalysisID>::const_iterator |
| 643 | I = RequiredSet.begin(), E = RequiredSet.end(); |
| 644 | I != E; ++I) { |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 645 | Pass *AnalysisPass = findAnalysisPass(*I, true); |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 646 | assert (AnalysisPass && "Analysis pass is not available"); |
| 647 | RP.push_back(AnalysisPass); |
| 648 | } |
Devang Patel | f58183d | 2006-12-12 23:09:32 +0000 | [diff] [blame] | 649 | |
| 650 | const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet(); |
| 651 | for (std::vector<AnalysisID>::const_iterator I = IDs.begin(), |
| 652 | E = IDs.end(); I != E; ++I) { |
| 653 | Pass *AnalysisPass = findAnalysisPass(*I, true); |
| 654 | assert (AnalysisPass && "Analysis pass is not available"); |
| 655 | RP.push_back(AnalysisPass); |
| 656 | } |
Devang Patel | 1d6267c | 2006-12-07 23:05:44 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 659 | // All Required analyses should be available to the pass as it runs! Here |
| 660 | // we fill in the AnalysisImpls member of the pass so that it can |
| 661 | // successfully use the getAnalysis() method to retrieve the |
| 662 | // implementations it needs. |
| 663 | // |
Devang Patel | dbe4a1e | 2006-12-07 18:36:24 +0000 | [diff] [blame] | 664 | void PMDataManager::initializeAnalysisImpl(Pass *P) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 665 | AnalysisUsage AnUsage; |
| 666 | P->getAnalysisUsage(AnUsage); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 667 | |
| 668 | for (std::vector<const PassInfo *>::const_iterator |
| 669 | I = AnUsage.getRequiredSet().begin(), |
| 670 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 671 | Pass *Impl = findAnalysisPass(*I, true); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 672 | if (Impl == 0) |
| 673 | assert(0 && "Analysis used but not available!"); |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 674 | AnalysisResolver *AR = P->getResolver(); |
Devang Patel | 984698a | 2006-12-09 01:11:34 +0000 | [diff] [blame] | 675 | AR->addAnalysisImplsPair(*I, Impl); |
Devang Patel | 07f4f58 | 2006-11-14 21:49:36 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 679 | /// Find the pass that implements Analysis AID. If desired pass is not found |
| 680 | /// then return NULL. |
| 681 | Pass *PMDataManager::findAnalysisPass(AnalysisID AID, bool SearchParent) { |
| 682 | |
| 683 | // Check if AvailableAnalysis map has one entry. |
| 684 | std::map<AnalysisID, Pass*>::const_iterator I = AvailableAnalysis.find(AID); |
| 685 | |
| 686 | if (I != AvailableAnalysis.end()) |
| 687 | return I->second; |
| 688 | |
| 689 | // Search Parents through TopLevelManager |
| 690 | if (SearchParent) |
| 691 | return TPM->findAnalysisPass(AID); |
| 692 | |
Devang Patel | 9d759b8 | 2006-12-09 00:09:12 +0000 | [diff] [blame] | 693 | return NULL; |
Devang Patel | 640c5bb | 2006-12-08 22:30:11 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 696 | // Print list of passes that are last used by P. |
| 697 | void PMDataManager::dumpLastUses(Pass *P, unsigned Offset) const{ |
| 698 | |
| 699 | std::vector<Pass *> LUses; |
| 700 | |
| 701 | assert (TPM && "Top Level Manager is missing"); |
| 702 | TPM->collectLastUses(LUses, P); |
| 703 | |
| 704 | for (std::vector<Pass *>::iterator I = LUses.begin(), |
| 705 | E = LUses.end(); I != E; ++I) { |
| 706 | llvm::cerr << "--" << std::string(Offset*2, ' '); |
| 707 | (*I)->dumpPassStructure(0); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | void PMDataManager::dumpPassArguments() const { |
| 712 | for(std::vector<Pass *>::const_iterator I = PassVector.begin(), |
| 713 | E = PassVector.end(); I != E; ++I) { |
| 714 | if (PMDataManager *PMD = dynamic_cast<PMDataManager *>(*I)) |
| 715 | PMD->dumpPassArguments(); |
| 716 | else |
| 717 | if (const PassInfo *PI = (*I)->getPassInfo()) |
| 718 | if (!PI->isAnalysisGroup()) |
| 719 | cerr << " -" << PI->getPassArgument(); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | void PMDataManager:: dumpPassInfo(Pass *P, std::string &Msg1, |
| 724 | std::string &Msg2) const { |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 725 | if (PassDebugging < Executions) |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 726 | return; |
| 727 | cerr << (void*)this << std::string(getDepth()*2+1, ' '); |
| 728 | cerr << Msg1; |
| 729 | cerr << P->getPassName(); |
| 730 | cerr << Msg2; |
| 731 | } |
| 732 | |
| 733 | void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P, |
| 734 | const std::vector<AnalysisID> &Set) |
| 735 | const { |
Devang Patel | fd418432 | 2007-01-17 20:33:36 +0000 | [diff] [blame] | 736 | if (PassDebugging >= Details && !Set.empty()) { |
Devang Patel | 991aeba | 2006-12-15 20:13:01 +0000 | [diff] [blame] | 737 | cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:"; |
| 738 | for (unsigned i = 0; i != Set.size(); ++i) { |
| 739 | if (i) cerr << ","; |
| 740 | cerr << " " << Set[i]->getPassName(); |
| 741 | } |
| 742 | cerr << "\n"; |
| 743 | } |
| 744 | } |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 745 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 746 | // Destructor |
| 747 | PMDataManager::~PMDataManager() { |
| 748 | |
| 749 | for (std::vector<Pass *>::iterator I = PassVector.begin(), |
| 750 | E = PassVector.end(); I != E; ++I) |
| 751 | delete *I; |
| 752 | |
| 753 | PassVector.clear(); |
| 754 | } |
| 755 | |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 756 | //===----------------------------------------------------------------------===// |
| 757 | // NOTE: Is this the right place to define this method ? |
| 758 | // getAnalysisToUpdate - Return an analysis result or null if it doesn't exist |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 759 | Pass *AnalysisResolver::getAnalysisToUpdate(AnalysisID ID, bool dir) const { |
Devang Patel | 9bdf7d4 | 2006-12-08 23:28:54 +0000 | [diff] [blame] | 760 | return PM.findAnalysisPass(ID, dir); |
| 761 | } |
| 762 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 763 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 764 | // BBPassManager implementation |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 765 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 766 | /// Execute all of the passes scheduled for execution by invoking |
| 767 | /// runOnBasicBlock method. Keep track of whether any of the passes modifies |
| 768 | /// the function, and if so, return true. |
| 769 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 770 | BBPassManager::runOnFunction(Function &F) { |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 771 | |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 772 | if (F.isDeclaration()) |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 773 | return false; |
| 774 | |
Devang Patel | e958559 | 2006-12-08 01:38:28 +0000 | [diff] [blame] | 775 | bool Changed = doInitialization(F); |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 776 | |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 777 | std::string Msg1 = "Executing Pass '"; |
| 778 | std::string Msg3 = "' Made Modification '"; |
| 779 | |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 780 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 781 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 782 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 783 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 784 | BP->getAnalysisUsage(AnUsage); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 785 | |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 786 | std::string Msg2 = "' on BasicBlock '" + (*I).getName() + "'...\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 787 | dumpPassInfo(BP, Msg1, Msg2); |
| 788 | dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet()); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 789 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 790 | initializeAnalysisImpl(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 791 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 792 | if (TheTimeInfo) TheTimeInfo->passStarted(BP); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 793 | Changed |= BP->runOnBasicBlock(*I); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 794 | if (TheTimeInfo) TheTimeInfo->passEnded(BP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 795 | |
| 796 | if (Changed) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 797 | dumpPassInfo(BP, Msg3, Msg2); |
| 798 | dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 799 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 800 | removeNotPreservedAnalysis(BP); |
| 801 | recordAvailableAnalysis(BP); |
| 802 | removeDeadPasses(BP, Msg2); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 803 | } |
Devang Patel | 56d48ec | 2006-12-15 22:57:49 +0000 | [diff] [blame] | 804 | return Changed |= doFinalization(F); |
Devang Patel | 6e5a113 | 2006-11-07 21:31:57 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 807 | // Implement doInitialization and doFinalization |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 808 | inline bool BBPassManager::doInitialization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 809 | bool Changed = false; |
| 810 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 811 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 812 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 813 | Changed |= BP->doInitialization(M); |
| 814 | } |
| 815 | |
| 816 | return Changed; |
| 817 | } |
| 818 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 819 | inline bool BBPassManager::doFinalization(Module &M) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 820 | bool Changed = false; |
| 821 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 822 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 823 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 824 | Changed |= BP->doFinalization(M); |
| 825 | } |
| 826 | |
| 827 | return Changed; |
| 828 | } |
| 829 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 830 | inline bool BBPassManager::doInitialization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 831 | bool Changed = false; |
| 832 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 833 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 834 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 835 | Changed |= BP->doInitialization(F); |
| 836 | } |
| 837 | |
| 838 | return Changed; |
| 839 | } |
| 840 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 841 | inline bool BBPassManager::doFinalization(Function &F) { |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 842 | bool Changed = false; |
| 843 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 844 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 845 | BasicBlockPass *BP = getContainedPass(Index); |
Devang Patel | 475c453 | 2006-12-08 00:59:05 +0000 | [diff] [blame] | 846 | Changed |= BP->doFinalization(F); |
| 847 | } |
| 848 | |
| 849 | return Changed; |
| 850 | } |
| 851 | |
| 852 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 853 | //===----------------------------------------------------------------------===// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 854 | // FunctionPassManager implementation |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 855 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 856 | /// Create new Function pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 857 | FunctionPassManager::FunctionPassManager(ModuleProvider *P) { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 858 | FPM = new FunctionPassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 859 | // FPM is the top level manager. |
| 860 | FPM->setTopLevelManager(FPM); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 861 | |
| 862 | PMDataManager *PMD = dynamic_cast<PMDataManager *>(FPM); |
Devang Patel | b66334b | 2007-01-05 22:47:07 +0000 | [diff] [blame] | 863 | AnalysisResolver *AR = new AnalysisResolver(*PMD); |
Devang Patel | 1036b65 | 2006-12-12 23:27:37 +0000 | [diff] [blame] | 864 | FPM->setResolver(AR); |
| 865 | |
Devang Patel | 1f65368 | 2006-12-08 18:57:16 +0000 | [diff] [blame] | 866 | MP = P; |
| 867 | } |
| 868 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 869 | FunctionPassManager::~FunctionPassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 870 | delete FPM; |
| 871 | } |
| 872 | |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 873 | /// add - Add a pass to the queue of passes to run. This passes |
| 874 | /// ownership of the Pass to the PassManager. When the |
| 875 | /// PassManager_X is destroyed, the pass will be destroyed as well, so |
| 876 | /// there is no need to delete the pass. (TODO delete passes.) |
| 877 | /// This implies that all passes MUST be allocated with 'new'. |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 878 | void FunctionPassManager::add(Pass *P) { |
Devang Patel | 4e12f86 | 2006-11-08 10:44:40 +0000 | [diff] [blame] | 879 | FPM->add(P); |
| 880 | } |
| 881 | |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 882 | /// run - Execute all of the passes scheduled for execution. Keep |
| 883 | /// track of whether any of the passes modifies the function, and if |
| 884 | /// so, return true. |
| 885 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 886 | bool FunctionPassManager::run(Function &F) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 887 | std::string errstr; |
| 888 | if (MP->materializeFunction(&F, &errstr)) { |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 889 | cerr << "Error reading bytecode file: " << errstr << "\n"; |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 890 | abort(); |
| 891 | } |
Devang Patel | 272908d | 2006-12-08 22:57:48 +0000 | [diff] [blame] | 892 | return FPM->run(F); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 896 | /// doInitialization - Run all of the initializers for the function passes. |
| 897 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 898 | bool FunctionPassManager::doInitialization() { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 899 | return FPM->doInitialization(*MP->getModule()); |
| 900 | } |
| 901 | |
| 902 | /// doFinalization - Run all of the initializers for the function passes. |
| 903 | /// |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 904 | bool FunctionPassManager::doFinalization() { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 905 | return FPM->doFinalization(*MP->getModule()); |
| 906 | } |
| 907 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 908 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 909 | // FunctionPassManagerImpl implementation |
| 910 | // |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 911 | inline bool FunctionPassManagerImpl::doInitialization(Module &M) { |
| 912 | bool Changed = false; |
| 913 | |
| 914 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 915 | FPPassManager *FP = getContainedManager(Index); |
| 916 | Changed |= FP->doInitialization(M); |
| 917 | } |
| 918 | |
| 919 | return Changed; |
| 920 | } |
| 921 | |
| 922 | inline bool FunctionPassManagerImpl::doFinalization(Module &M) { |
| 923 | bool Changed = false; |
| 924 | |
| 925 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 926 | FPPassManager *FP = getContainedManager(Index); |
| 927 | Changed |= FP->doFinalization(M); |
| 928 | } |
| 929 | |
| 930 | return Changed; |
| 931 | } |
| 932 | |
| 933 | // Execute all the passes managed by this top level manager. |
| 934 | // Return true if any function is modified by a pass. |
| 935 | bool FunctionPassManagerImpl::run(Function &F) { |
| 936 | |
| 937 | bool Changed = false; |
| 938 | |
| 939 | TimingInfo::createTheTimeInfo(); |
| 940 | |
| 941 | dumpArguments(); |
| 942 | dumpPasses(); |
| 943 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 944 | initializeAllAnalysisInfo(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 945 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 946 | FPPassManager *FP = getContainedManager(Index); |
| 947 | Changed |= FP->runOnFunction(F); |
| 948 | } |
| 949 | return Changed; |
| 950 | } |
| 951 | |
| 952 | //===----------------------------------------------------------------------===// |
| 953 | // FPPassManager implementation |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 954 | |
Devang Patel | e759955 | 2007-01-12 18:52:44 +0000 | [diff] [blame] | 955 | /// Print passes managed by this manager |
| 956 | void FPPassManager::dumpPassStructure(unsigned Offset) { |
| 957 | llvm::cerr << std::string(Offset*2, ' ') << "FunctionPass Manager\n"; |
| 958 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 959 | FunctionPass *FP = getContainedPass(Index); |
| 960 | FP->dumpPassStructure(Offset + 1); |
| 961 | dumpLastUses(FP, Offset+1); |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | |
Devang Patel | 0c2012f | 2006-11-07 21:49:50 +0000 | [diff] [blame] | 966 | /// Execute all of the passes scheduled for execution by invoking |
| 967 | /// runOnFunction method. Keep track of whether any of the passes modifies |
| 968 | /// the function, and if so, return true. |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 969 | bool FPPassManager::runOnFunction(Function &F) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 970 | |
| 971 | bool Changed = false; |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 972 | |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 973 | if (F.isDeclaration()) |
Devang Patel | 745a696 | 2006-12-12 23:15:28 +0000 | [diff] [blame] | 974 | return false; |
| 975 | |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 976 | std::string Msg1 = "Executing Pass '"; |
| 977 | std::string Msg3 = "' Made Modification '"; |
| 978 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 979 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 980 | FunctionPass *FP = getContainedPass(Index); |
| 981 | |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 982 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 983 | FP->getAnalysisUsage(AnUsage); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 984 | |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 985 | std::string Msg2 = "' on Function '" + F.getName() + "'...\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 986 | dumpPassInfo(FP, Msg1, Msg2); |
| 987 | dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 988 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 989 | initializeAnalysisImpl(FP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 990 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 991 | if (TheTimeInfo) TheTimeInfo->passStarted(FP); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 992 | Changed |= FP->runOnFunction(F); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 993 | if (TheTimeInfo) TheTimeInfo->passEnded(FP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 994 | |
| 995 | if (Changed) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 996 | dumpPassInfo(FP, Msg3, Msg2); |
| 997 | dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 998 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 999 | removeNotPreservedAnalysis(FP); |
| 1000 | recordAvailableAnalysis(FP); |
| 1001 | removeDeadPasses(FP, Msg2); |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1002 | } |
| 1003 | return Changed; |
| 1004 | } |
| 1005 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1006 | bool FPPassManager::runOnModule(Module &M) { |
Devang Patel | 9f3083e | 2006-11-15 19:39:54 +0000 | [diff] [blame] | 1007 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1008 | bool Changed = doInitialization(M); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1009 | |
| 1010 | for(Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 1011 | this->runOnFunction(*I); |
| 1012 | |
| 1013 | return Changed |= doFinalization(M); |
| 1014 | } |
| 1015 | |
| 1016 | inline bool FPPassManager::doInitialization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1017 | bool Changed = false; |
| 1018 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1019 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1020 | FunctionPass *FP = getContainedPass(Index); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1021 | Changed |= FP->doInitialization(M); |
| 1022 | } |
| 1023 | |
| 1024 | return Changed; |
| 1025 | } |
| 1026 | |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1027 | inline bool FPPassManager::doFinalization(Module &M) { |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1028 | bool Changed = false; |
| 1029 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1030 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1031 | FunctionPass *FP = getContainedPass(Index); |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1032 | Changed |= FP->doFinalization(M); |
| 1033 | } |
| 1034 | |
Devang Patel | ff631ae | 2006-11-15 01:27:05 +0000 | [diff] [blame] | 1035 | return Changed; |
| 1036 | } |
| 1037 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1038 | //===----------------------------------------------------------------------===// |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1039 | // MPPassManager implementation |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1040 | |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1041 | /// Execute all of the passes scheduled for execution by invoking |
| 1042 | /// runOnModule method. Keep track of whether any of the passes modifies |
| 1043 | /// the module, and if so, return true. |
| 1044 | bool |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1045 | MPPassManager::runOnModule(Module &M) { |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1046 | bool Changed = false; |
Devang Patel | 050ec72 | 2006-11-14 01:23:29 +0000 | [diff] [blame] | 1047 | |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1048 | std::string Msg1 = "Executing Pass '"; |
| 1049 | std::string Msg3 = "' Made Modification '"; |
| 1050 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1051 | for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) { |
| 1052 | ModulePass *MP = getContainedPass(Index); |
| 1053 | |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1054 | AnalysisUsage AnUsage; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1055 | MP->getAnalysisUsage(AnUsage); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1056 | |
Devang Patel | 200d305 | 2006-12-13 23:50:44 +0000 | [diff] [blame] | 1057 | std::string Msg2 = "' on Module '" + M.getModuleIdentifier() + "'...\n"; |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1058 | dumpPassInfo(MP, Msg1, Msg2); |
| 1059 | dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet()); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1060 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1061 | initializeAnalysisImpl(MP); |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1062 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1063 | if (TheTimeInfo) TheTimeInfo->passStarted(MP); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1064 | Changed |= MP->runOnModule(M); |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1065 | if (TheTimeInfo) TheTimeInfo->passEnded(MP); |
Devang Patel | 93a197c | 2006-12-14 00:08:04 +0000 | [diff] [blame] | 1066 | |
| 1067 | if (Changed) |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1068 | dumpPassInfo(MP, Msg3, Msg2); |
| 1069 | dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet()); |
Devang Patel | f6d1d21 | 2006-12-14 00:25:06 +0000 | [diff] [blame] | 1070 | |
Devang Patel | abfbe3b | 2006-12-16 00:56:26 +0000 | [diff] [blame] | 1071 | removeNotPreservedAnalysis(MP); |
| 1072 | recordAvailableAnalysis(MP); |
| 1073 | removeDeadPasses(MP, Msg2); |
Devang Patel | 05e1a97 | 2006-11-07 22:03:15 +0000 | [diff] [blame] | 1074 | } |
| 1075 | return Changed; |
| 1076 | } |
| 1077 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1078 | //===----------------------------------------------------------------------===// |
| 1079 | // PassManagerImpl implementation |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1080 | // |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1081 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1082 | /// 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] | 1083 | bool PassManagerImpl::run(Module &M) { |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1084 | |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1085 | bool Changed = false; |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1086 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1087 | TimingInfo::createTheTimeInfo(); |
| 1088 | |
Devang Patel | cfd70c4 | 2006-12-13 22:10:00 +0000 | [diff] [blame] | 1089 | dumpArguments(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1090 | dumpPasses(); |
Devang Patel | f1567a5 | 2006-12-13 20:03:48 +0000 | [diff] [blame] | 1091 | |
Devang Patel | e306840 | 2006-12-21 00:16:50 +0000 | [diff] [blame] | 1092 | initializeAllAnalysisInfo(); |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1093 | for (unsigned Index = 0; Index < getNumContainedManagers(); ++Index) { |
| 1094 | MPPassManager *MP = getContainedManager(Index); |
Devang Patel | 5bbeb49 | 2006-12-08 22:47:25 +0000 | [diff] [blame] | 1095 | Changed |= MP->runOnModule(M); |
Devang Patel | c290c8a | 2006-11-07 22:23:34 +0000 | [diff] [blame] | 1096 | } |
| 1097 | return Changed; |
| 1098 | } |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1099 | |
Devang Patel | a1514cb | 2006-12-07 19:39:39 +0000 | [diff] [blame] | 1100 | //===----------------------------------------------------------------------===// |
| 1101 | // PassManager implementation |
| 1102 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1103 | /// Create new pass manager |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1104 | PassManager::PassManager() { |
Devang Patel | 67d6a5e | 2006-12-19 19:46:59 +0000 | [diff] [blame] | 1105 | PM = new PassManagerImpl(0); |
Devang Patel | 9c6290c | 2006-12-12 22:02:16 +0000 | [diff] [blame] | 1106 | // PM is the top level manager |
| 1107 | PM->setTopLevelManager(PM); |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1110 | PassManager::~PassManager() { |
Devang Patel | ab97cf4 | 2006-12-13 00:09:23 +0000 | [diff] [blame] | 1111 | delete PM; |
| 1112 | } |
| 1113 | |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1114 | /// add - Add a pass to the queue of passes to run. This passes ownership of |
| 1115 | /// the Pass to the PassManager. When the PassManager is destroyed, the pass |
| 1116 | /// will be destroyed as well, so there is no need to delete the pass. This |
| 1117 | /// implies that all passes MUST be allocated with 'new'. |
| 1118 | void |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1119 | PassManager::add(Pass *P) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1120 | PM->add(P); |
| 1121 | } |
| 1122 | |
| 1123 | /// run - Execute all of the passes scheduled for execution. Keep track of |
| 1124 | /// whether any of the passes modifies the module, and if so, return true. |
| 1125 | bool |
Devang Patel | b67904d | 2006-12-13 02:36:01 +0000 | [diff] [blame] | 1126 | PassManager::run(Module &M) { |
Devang Patel | 376fefa | 2006-11-08 10:29:57 +0000 | [diff] [blame] | 1127 | return PM->run(M); |
| 1128 | } |
| 1129 | |
Devang Patel | b8817b9 | 2006-12-14 00:59:42 +0000 | [diff] [blame] | 1130 | //===----------------------------------------------------------------------===// |
| 1131 | // TimingInfo Class - This class is used to calculate information about the |
| 1132 | // amount of time each pass takes to execute. This only happens with |
| 1133 | // -time-passes is enabled on the command line. |
| 1134 | // |
| 1135 | bool llvm::TimePassesIsEnabled = false; |
| 1136 | static cl::opt<bool,true> |
| 1137 | EnableTiming("time-passes", cl::location(TimePassesIsEnabled), |
| 1138 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
| 1139 | |
| 1140 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 1141 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 1142 | // null. It may be called multiple times. |
| 1143 | void TimingInfo::createTheTimeInfo() { |
| 1144 | if (!TimePassesIsEnabled || TheTimeInfo) return; |
| 1145 | |
| 1146 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 1147 | // This guarantees that the object will be constructed before static globals, |
| 1148 | // thus it will be destroyed before them. |
| 1149 | static ManagedStatic<TimingInfo> TTI; |
| 1150 | TheTimeInfo = &*TTI; |
| 1151 | } |
| 1152 | |
Devang Patel | 1c3633e | 2007-01-29 23:10:37 +0000 | [diff] [blame] | 1153 | /// If TimingInfo is enabled then start pass timer. |
| 1154 | void StartPassTimer(Pass *P) { |
| 1155 | if (TheTimeInfo) |
| 1156 | TheTimeInfo->passStarted(P); |
| 1157 | } |
| 1158 | |
| 1159 | /// If TimingInfo is enabled then stop pass timer. |
| 1160 | void StopPassTimer(Pass *P) { |
| 1161 | if (TheTimeInfo) |
| 1162 | TheTimeInfo->passEnded(P); |
| 1163 | } |
| 1164 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1165 | //===----------------------------------------------------------------------===// |
| 1166 | // PMStack implementation |
| 1167 | // |
Devang Patel | ad98d23 | 2007-01-11 22:15:30 +0000 | [diff] [blame] | 1168 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1169 | // Pop Pass Manager from the stack and clear its analysis info. |
| 1170 | void PMStack::pop() { |
| 1171 | |
| 1172 | PMDataManager *Top = this->top(); |
| 1173 | Top->initializeAnalysisInfo(); |
| 1174 | |
| 1175 | S.pop_back(); |
| 1176 | } |
| 1177 | |
| 1178 | // Push PM on the stack and set its top level manager. |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1179 | void PMStack::push(Pass *P) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1180 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1181 | PMDataManager *Top = NULL; |
| 1182 | PMDataManager *PM = dynamic_cast<PMDataManager *>(P); |
| 1183 | assert (PM && "Unable to push. Pass Manager expected"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1184 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1185 | if (this->empty()) { |
| 1186 | Top = PM; |
| 1187 | } |
| 1188 | else { |
| 1189 | Top = this->top(); |
| 1190 | PMTopLevelManager *TPM = Top->getTopLevelManager(); |
| 1191 | |
| 1192 | assert (TPM && "Unable to find top level manager"); |
| 1193 | TPM->addIndirectPassManager(PM); |
| 1194 | PM->setTopLevelManager(TPM); |
| 1195 | } |
| 1196 | |
| 1197 | AnalysisResolver *AR = new AnalysisResolver(*Top); |
| 1198 | P->setResolver(AR); |
| 1199 | |
| 1200 | S.push_back(PM); |
| 1201 | } |
| 1202 | |
| 1203 | // Dump content of the pass manager stack. |
| 1204 | void PMStack::dump() { |
| 1205 | for(std::deque<PMDataManager *>::iterator I = S.begin(), |
| 1206 | E = S.end(); I != E; ++I) { |
| 1207 | Pass *P = dynamic_cast<Pass *>(*I); |
| 1208 | printf ("%s ", P->getPassName()); |
| 1209 | } |
| 1210 | if (!S.empty()) |
| 1211 | printf ("\n"); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | // Walk Pass Manager stack and set LastUse markers if any |
| 1215 | // manager is transfering this priviledge to its parent manager |
| 1216 | void PMStack::handleLastUserOverflow() { |
| 1217 | |
| 1218 | for(PMStack::iterator I = this->begin(), E = this->end(); I != E;) { |
| 1219 | |
| 1220 | PMDataManager *Child = *I++; |
| 1221 | if (I != E) { |
| 1222 | PMDataManager *Parent = *I++; |
| 1223 | PMTopLevelManager *TPM = Parent->getTopLevelManager(); |
| 1224 | std::vector<Pass *> &TLU = Child->getTransferredLastUses(); |
| 1225 | if (!TLU.empty()) { |
| 1226 | Pass *P = dynamic_cast<Pass *>(Parent); |
| 1227 | TPM->setLastUser(TLU, P); |
Devang Patel | 4d06ace | 2007-02-07 19:37:53 +0000 | [diff] [blame] | 1228 | TLU.clear(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /// Find appropriate Module Pass Manager in the PM Stack and |
| 1235 | /// add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1236 | void ModulePass::assignPassManager(PMStack &PMS, |
| 1237 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1238 | |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1239 | // Find Module Pass Manager |
| 1240 | while(!PMS.empty()) { |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1241 | PassManagerType TopPMType = PMS.top()->getPassManagerType(); |
| 1242 | if (TopPMType == PreferredType) |
| 1243 | break; // We found desired pass manager |
| 1244 | else if (TopPMType > PMT_ModulePassManager) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1245 | PMS.pop(); // Pop children pass managers |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1246 | else |
| 1247 | break; |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
Devang Patel | 23f8aa9 | 2007-01-17 21:19:23 +0000 | [diff] [blame] | 1250 | PMS.top()->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1251 | } |
| 1252 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1253 | /// Find appropriate Function Pass Manager or Call Graph Pass Manager |
| 1254 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1255 | void FunctionPass::assignPassManager(PMStack &PMS, |
| 1256 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1257 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1258 | // Find Module Pass Manager (TODO : Or Call Graph Pass Manager) |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1259 | while(!PMS.empty()) { |
Devang Patel | ac99eca | 2007-01-11 19:59:06 +0000 | [diff] [blame] | 1260 | if (PMS.top()->getPassManagerType() > PMT_FunctionPassManager) |
| 1261 | PMS.pop(); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1262 | else |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1263 | break; |
| 1264 | } |
| 1265 | FPPassManager *FPP = dynamic_cast<FPPassManager *>(PMS.top()); |
| 1266 | |
| 1267 | // Create new Function Pass Manager |
| 1268 | if (!FPP) { |
| 1269 | assert(!PMS.empty() && "Unable to create Function Pass Manager"); |
| 1270 | PMDataManager *PMD = PMS.top(); |
| 1271 | |
| 1272 | // [1] Create new Function Pass Manager |
| 1273 | FPP = new FPPassManager(PMD->getDepth() + 1); |
| 1274 | |
| 1275 | // [2] Set up new manager's top level manager |
| 1276 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1277 | TPM->addIndirectPassManager(FPP); |
| 1278 | |
| 1279 | // [3] Assign manager to manage this new manager. This may create |
| 1280 | // and push new managers into PMS |
| 1281 | Pass *P = dynamic_cast<Pass *>(FPP); |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1282 | |
| 1283 | // If Call Graph Pass Manager is active then use it to manage |
| 1284 | // this new Function Pass manager. |
| 1285 | if (PMD->getPassManagerType() == PMT_CallGraphPassManager) |
| 1286 | P->assignPassManager(PMS, PMT_CallGraphPassManager); |
| 1287 | else |
| 1288 | P->assignPassManager(PMS); |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1289 | |
| 1290 | // [4] Push new manager into PMS |
| 1291 | PMS.push(FPP); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1294 | // Assign FPP as the manager of this pass. |
| 1295 | FPP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1296 | } |
| 1297 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1298 | /// Find appropriate Basic Pass Manager or Call Graph Pass Manager |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1299 | /// in the PM Stack and add self into that manager. |
Devang Patel | dffca63 | 2007-01-17 20:30:17 +0000 | [diff] [blame] | 1300 | void BasicBlockPass::assignPassManager(PMStack &PMS, |
| 1301 | PassManagerType PreferredType) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1302 | |
| 1303 | BBPassManager *BBP = NULL; |
| 1304 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1305 | // Basic Pass Manager is a leaf pass manager. It does not handle |
| 1306 | // any other pass manager. |
| 1307 | if (!PMS.empty()) { |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1308 | BBP = dynamic_cast<BBPassManager *>(PMS.top()); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1311 | // If leaf manager is not Basic Block Pass manager then create new |
| 1312 | // basic Block Pass manager. |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1313 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1314 | if (!BBP) { |
| 1315 | assert(!PMS.empty() && "Unable to create BasicBlock Pass Manager"); |
| 1316 | PMDataManager *PMD = PMS.top(); |
| 1317 | |
| 1318 | // [1] Create new Basic Block Manager |
| 1319 | BBP = new BBPassManager(PMD->getDepth() + 1); |
| 1320 | |
| 1321 | // [2] Set up new manager's top level manager |
| 1322 | // Basic Block Pass Manager does not live by itself |
| 1323 | PMTopLevelManager *TPM = PMD->getTopLevelManager(); |
| 1324 | TPM->addIndirectPassManager(BBP); |
| 1325 | |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1326 | // [3] Assign manager to manage this new manager. This may create |
| 1327 | // and push new managers into PMS |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1328 | Pass *P = dynamic_cast<Pass *>(BBP); |
| 1329 | P->assignPassManager(PMS); |
Devang Patel | 15701b5 | 2007-01-11 00:19:00 +0000 | [diff] [blame] | 1330 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1331 | // [4] Push new manager into PMS |
| 1332 | PMS.push(BBP); |
| 1333 | } |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1334 | |
Devang Patel | 3312f75 | 2007-01-16 21:43:18 +0000 | [diff] [blame] | 1335 | // Assign BBP as the manager of this pass. |
| 1336 | BBP->add(this); |
Devang Patel | 1c56a63 | 2007-01-08 19:29:38 +0000 | [diff] [blame] | 1337 | } |
| 1338 | |
Devang Patel | c6b5a55 | 2007-01-05 20:16:23 +0000 | [diff] [blame] | 1339 | |