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