Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 1 | //===- Pass.cpp - LLVM Pass Infrastructure Implementation -----------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements the LLVM Pass infrastructure. It is primarily |
| 11 | // responsible with ensuring that passes are executed and batched together |
| 12 | // optimally. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 16 | #include "llvm/PassManager.h" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 17 | #include "PassManagerT.h" // PassManagerT implementation |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 19 | #include "llvm/ModuleProvider.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/STLExtras.h" |
| 21 | #include "llvm/Support/TypeInfo.h" |
Reid Spencer | 8baf8e2 | 2004-07-04 11:55:37 +0000 | [diff] [blame] | 22 | #include <iostream> |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 23 | #include <set> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 675e7a9 | 2002-08-21 23:51:51 +0000 | [diff] [blame] | 26 | // IncludeFile - Stub function used to help linking out. |
| 27 | IncludeFile::IncludeFile(void*) {} |
| 28 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // AnalysisID Class Implementation |
| 31 | // |
| 32 | |
Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 33 | // getCFGOnlyAnalyses - A wrapper around the CFGOnlyAnalyses which make it |
| 34 | // initializer order independent. |
| 35 | static std::vector<const PassInfo*> &getCFGOnlyAnalyses() { |
| 36 | static std::vector<const PassInfo*> CFGOnlyAnalyses; |
| 37 | return CFGOnlyAnalyses; |
| 38 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 39 | |
Chris Lattner | dd99f5b | 2003-10-12 18:52:12 +0000 | [diff] [blame] | 40 | void RegisterPassBase::setOnlyUsesCFG() { |
Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 41 | getCFGOnlyAnalyses().push_back(PIObj); |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | // AnalysisResolver Class Implementation |
| 46 | // |
| 47 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 48 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 49 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 50 | P->Resolver = AR; |
| 51 | } |
| 52 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 53 | //===----------------------------------------------------------------------===// |
| 54 | // AnalysisUsage Class Implementation |
| 55 | // |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 57 | // setPreservesCFG - This function should be called to by the pass, iff they do |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 58 | // not: |
| 59 | // |
| 60 | // 1. Add or remove basic blocks from the function |
| 61 | // 2. Modify terminator instructions in any way. |
| 62 | // |
| 63 | // This function annotates the AnalysisUsage info object to say that analyses |
| 64 | // that only depend on the CFG are preserved by this pass. |
| 65 | // |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 66 | void AnalysisUsage::setPreservesCFG() { |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 67 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 68 | // that only depend on the CFG (like dominators, loop info, etc...) |
| 69 | // |
| 70 | Preserved.insert(Preserved.end(), |
Chris Lattner | ab16a65 | 2003-10-13 05:33:01 +0000 | [diff] [blame] | 71 | getCFGOnlyAnalyses().begin(), getCFGOnlyAnalyses().end()); |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 75 | //===----------------------------------------------------------------------===// |
| 76 | // PassManager implementation - The PassManager class is a simple Pimpl class |
| 77 | // that wraps the PassManagerT template. |
| 78 | // |
| 79 | PassManager::PassManager() : PM(new PassManagerT<Module>()) {} |
| 80 | PassManager::~PassManager() { delete PM; } |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame^] | 81 | void PassManager::add(Pass *P) { |
| 82 | ModulePass *MP = dynamic_cast<ModulePass*>(P); |
| 83 | assert(MP && "Not a modulepass?"); |
| 84 | PM->add(MP); |
| 85 | } |
| 86 | bool PassManager::run(Module &M) { return PM->runOnModule(M); } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 87 | |
Brian Gaeke | b7a8388 | 2003-08-12 17:22:39 +0000 | [diff] [blame] | 88 | //===----------------------------------------------------------------------===// |
| 89 | // FunctionPassManager implementation - The FunctionPassManager class |
| 90 | // is a simple Pimpl class that wraps the PassManagerT template. It |
| 91 | // is like PassManager, but only deals in FunctionPasses. |
| 92 | // |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 93 | FunctionPassManager::FunctionPassManager(ModuleProvider *P) : |
| 94 | PM(new PassManagerT<Function>()), MP(P) {} |
Brian Gaeke | b7a8388 | 2003-08-12 17:22:39 +0000 | [diff] [blame] | 95 | FunctionPassManager::~FunctionPassManager() { delete PM; } |
| 96 | void FunctionPassManager::add(FunctionPass *P) { PM->add(P); } |
Brian Gaeke | 2cc4b9d | 2003-08-14 06:07:57 +0000 | [diff] [blame] | 97 | void FunctionPassManager::add(ImmutablePass *IP) { PM->add(IP); } |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 98 | bool FunctionPassManager::run(Function &F) { |
Reid Spencer | 6967cd5 | 2004-07-07 21:01:38 +0000 | [diff] [blame] | 99 | try { |
| 100 | MP->materializeFunction(&F); |
| 101 | } catch (std::string& errstr) { |
| 102 | std::cerr << "Error reading bytecode file: " << errstr << "\n"; |
| 103 | abort(); |
| 104 | } catch (...) { |
Misha Brukman | 4233155 | 2004-07-07 21:22:05 +0000 | [diff] [blame] | 105 | std::cerr << "Error reading bytecode file!\n"; |
Reid Spencer | 6967cd5 | 2004-07-07 21:01:38 +0000 | [diff] [blame] | 106 | abort(); |
| 107 | } |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame^] | 108 | return PM->run(F); |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 109 | } |
Brian Gaeke | b7a8388 | 2003-08-12 17:22:39 +0000 | [diff] [blame] | 110 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 111 | |
| 112 | //===----------------------------------------------------------------------===// |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 113 | // TimingInfo Class - This class is used to calculate information about the |
| 114 | // amount of time each pass takes to execute. This only happens with |
| 115 | // -time-passes is enabled on the command line. |
| 116 | // |
Reid Spencer | 2e4bfff | 2004-08-24 17:52:35 +0000 | [diff] [blame] | 117 | bool llvm::TimePassesIsEnabled = false; |
| 118 | static cl::opt<bool,true> |
| 119 | EnableTiming("time-passes", cl::location(TimePassesIsEnabled), |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 120 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 121 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 122 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer to |
| 123 | // a non null value (if the -time-passes option is enabled) or it leaves it |
| 124 | // null. It may be called multiple times. |
| 125 | void TimingInfo::createTheTimeInfo() { |
Reid Spencer | 2e4bfff | 2004-08-24 17:52:35 +0000 | [diff] [blame] | 126 | if (!TimePassesIsEnabled || TheTimeInfo) return; |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 127 | |
| 128 | // Constructed the first time this is called, iff -time-passes is enabled. |
| 129 | // This guarantees that the object will be constructed before static globals, |
| 130 | // thus it will be destroyed before them. |
| 131 | static TimingInfo TTI; |
| 132 | TheTimeInfo = &TTI; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 135 | void PMDebug::PrintArgumentInformation(const Pass *P) { |
| 136 | // Print out passes in pass manager... |
| 137 | if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) { |
| 138 | for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i) |
| 139 | PrintArgumentInformation(PM->getContainedPass(i)); |
| 140 | |
| 141 | } else { // Normal pass. Print argument information... |
| 142 | // Print out arguments for registered passes that are _optimizations_ |
| 143 | if (const PassInfo *PI = P->getPassInfo()) |
| 144 | if (PI->getPassType() & PassInfo::Optimization) |
| 145 | std::cerr << " -" << PI->getPassArgument(); |
| 146 | } |
| 147 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 148 | |
| 149 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
Chris Lattner | 43640d7 | 2004-02-29 22:37:04 +0000 | [diff] [blame] | 150 | Pass *P, Module *M) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 151 | if (PassDebugging >= Executions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 152 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 153 | << P->getPassName(); |
Chris Lattner | 43640d7 | 2004-02-29 22:37:04 +0000 | [diff] [blame] | 154 | if (M) std::cerr << "' on Module '" << M->getModuleIdentifier() << "'\n"; |
| 155 | std::cerr << "'...\n"; |
| 156 | } |
| 157 | } |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 158 | |
Chris Lattner | 43640d7 | 2004-02-29 22:37:04 +0000 | [diff] [blame] | 159 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
| 160 | Pass *P, Function *F) { |
| 161 | if (PassDebugging >= Executions) { |
| 162 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
| 163 | << P->getPassName(); |
| 164 | if (F) std::cerr << "' on Function '" << F->getName(); |
| 165 | std::cerr << "'...\n"; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
| 170 | Pass *P, BasicBlock *BB) { |
| 171 | if (PassDebugging >= Executions) { |
| 172 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
| 173 | << P->getPassName(); |
| 174 | if (BB) std::cerr << "' on BasicBlock '" << BB->getName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 175 | std::cerr << "'...\n"; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 180 | Pass *P, const std::vector<AnalysisID> &Set){ |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 181 | if (PassDebugging >= Details && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 182 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 183 | for (unsigned i = 0; i != Set.size(); ++i) { |
| 184 | if (i) std::cerr << ","; |
| 185 | std::cerr << " " << Set[i]->getPassName(); |
| 186 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 187 | std::cerr << "\n"; |
| 188 | } |
| 189 | } |
| 190 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 191 | //===----------------------------------------------------------------------===// |
| 192 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 193 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 194 | |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame^] | 195 | void ModulePass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 196 | PM->addPass(this, AU); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 9ad7757 | 2003-03-21 21:41:02 +0000 | [diff] [blame] | 199 | bool Pass::mustPreserveAnalysisID(const PassInfo *AnalysisID) const { |
| 200 | return Resolver->getAnalysisToUpdate(AnalysisID) != 0; |
| 201 | } |
| 202 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 203 | // dumpPassStructure - Implement the -debug-passes=Structure option |
| 204 | void Pass::dumpPassStructure(unsigned Offset) { |
| 205 | std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; |
| 206 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 207 | |
Brian Gaeke | 465a5cc | 2004-02-28 21:55:18 +0000 | [diff] [blame] | 208 | // getPassName - Use C++ RTTI to get a SOMEWHAT intelligible name for the pass. |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 209 | // |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 210 | const char *Pass::getPassName() const { |
| 211 | if (const PassInfo *PI = getPassInfo()) |
| 212 | return PI->getPassName(); |
| 213 | return typeid(*this).name(); |
| 214 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 215 | |
Misha Brukman | 56a8642 | 2003-10-14 21:38:42 +0000 | [diff] [blame] | 216 | // print - Print out the internal state of the pass. This is called by Analyze |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 217 | // to print out the contents of an analysis. Otherwise it is not necessary to |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 218 | // implement this method. |
| 219 | // |
| 220 | void Pass::print(std::ostream &O) const { |
| 221 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; |
| 222 | } |
| 223 | |
| 224 | // dump - call print(std::cerr); |
| 225 | void Pass::dump() const { |
| 226 | print(std::cerr, 0); |
| 227 | } |
| 228 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 229 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 230 | // ImmutablePass Implementation |
| 231 | // |
| 232 | void ImmutablePass::addToPassManager(PassManagerT<Module> *PM, |
| 233 | AnalysisUsage &AU) { |
| 234 | PM->addPass(this, AU); |
| 235 | } |
| 236 | |
| 237 | |
| 238 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 239 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 240 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 241 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 242 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 243 | // for every function in the module, then by finalizing. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 244 | // |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame^] | 245 | bool FunctionPass::runOnModule(Module &M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 246 | bool Changed = doInitialization(M); |
| 247 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 248 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 249 | if (!I->isExternal()) // Passes are not run on external functions! |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 250 | Changed |= runOnFunction(*I); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 251 | |
| 252 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 255 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 256 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 257 | bool FunctionPass::run(Function &F) { |
| 258 | if (F.isExternal()) return false;// Passes are not run on external functions! |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 259 | |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame^] | 260 | bool Changed = doInitialization(*F.getParent()); |
| 261 | Changed |= runOnFunction(F); |
| 262 | return Changed | doFinalization(*F.getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 263 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 264 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 265 | void FunctionPass::addToPassManager(PassManagerT<Module> *PM, |
| 266 | AnalysisUsage &AU) { |
| 267 | PM->addPass(this, AU); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 268 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 269 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 270 | void FunctionPass::addToPassManager(PassManagerT<Function> *PM, |
| 271 | AnalysisUsage &AU) { |
| 272 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | //===----------------------------------------------------------------------===// |
| 276 | // BasicBlockPass Implementation |
| 277 | // |
| 278 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 279 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 280 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 281 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 282 | bool BasicBlockPass::runOnFunction(Function &F) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 283 | bool Changed = doInitialization(F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 284 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 285 | Changed |= runOnBasicBlock(*I); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 286 | return Changed | doFinalization(F); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 290 | // finalize. |
| 291 | // |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame^] | 292 | bool BasicBlockPass::runPass(BasicBlock &BB) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 293 | Function &F = *BB.getParent(); |
| 294 | Module &M = *F.getParent(); |
Chris Lattner | 79e523d | 2004-09-20 04:47:19 +0000 | [diff] [blame^] | 295 | bool Changed = doInitialization(M); |
| 296 | Changed |= doInitialization(F); |
| 297 | Changed |= runOnBasicBlock(BB); |
| 298 | Changed |= doFinalization(F); |
| 299 | Changed |= doFinalization(M); |
| 300 | return Changed; |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 303 | void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 304 | AnalysisUsage &AU) { |
| 305 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 309 | AnalysisUsage &AU) { |
| 310 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 313 | |
| 314 | //===----------------------------------------------------------------------===// |
| 315 | // Pass Registration mechanism |
| 316 | // |
| 317 | static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0; |
| 318 | static std::vector<PassRegistrationListener*> *Listeners = 0; |
| 319 | |
| 320 | // getPassInfo - Return the PassInfo data structure that corresponds to this |
| 321 | // pass... |
| 322 | const PassInfo *Pass::getPassInfo() const { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 323 | if (PassInfoCache) return PassInfoCache; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 324 | return lookupPassInfo(typeid(*this)); |
| 325 | } |
| 326 | |
| 327 | const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 328 | if (PassInfoMap == 0) return 0; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 329 | std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI); |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 330 | return (I != PassInfoMap->end()) ? I->second : 0; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | void RegisterPassBase::registerPass(PassInfo *PI) { |
| 334 | if (PassInfoMap == 0) |
| 335 | PassInfoMap = new std::map<TypeInfo, PassInfo*>(); |
| 336 | |
| 337 | assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && |
| 338 | "Pass already registered!"); |
| 339 | PIObj = PI; |
| 340 | PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); |
| 341 | |
| 342 | // Notify any listeners... |
| 343 | if (Listeners) |
| 344 | for (std::vector<PassRegistrationListener*>::iterator |
| 345 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 346 | (*I)->passRegistered(PI); |
| 347 | } |
| 348 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 349 | void RegisterPassBase::unregisterPass(PassInfo *PI) { |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 350 | assert(PassInfoMap && "Pass registered but not in map!"); |
| 351 | std::map<TypeInfo, PassInfo*>::iterator I = |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 352 | PassInfoMap->find(PI->getTypeInfo()); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 353 | assert(I != PassInfoMap->end() && "Pass registered but not in map!"); |
| 354 | |
| 355 | // Remove pass from the map... |
| 356 | PassInfoMap->erase(I); |
| 357 | if (PassInfoMap->empty()) { |
| 358 | delete PassInfoMap; |
| 359 | PassInfoMap = 0; |
| 360 | } |
| 361 | |
| 362 | // Notify any listeners... |
| 363 | if (Listeners) |
| 364 | for (std::vector<PassRegistrationListener*>::iterator |
| 365 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 366 | (*I)->passUnregistered(PI); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 367 | |
| 368 | // Delete the PassInfo object itself... |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 369 | delete PI; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 372 | //===----------------------------------------------------------------------===// |
| 373 | // Analysis Group Implementation Code |
| 374 | //===----------------------------------------------------------------------===// |
| 375 | |
| 376 | struct AnalysisGroupInfo { |
| 377 | const PassInfo *DefaultImpl; |
| 378 | std::set<const PassInfo *> Implementations; |
| 379 | AnalysisGroupInfo() : DefaultImpl(0) {} |
| 380 | }; |
| 381 | |
| 382 | static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0; |
| 383 | |
| 384 | // RegisterAGBase implementation |
| 385 | // |
| 386 | RegisterAGBase::RegisterAGBase(const std::type_info &Interface, |
| 387 | const std::type_info *Pass, bool isDefault) |
| 388 | : ImplementationInfo(0), isDefaultImplementation(isDefault) { |
| 389 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 390 | InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface)); |
| 391 | if (InterfaceInfo == 0) { // First reference to Interface, add it now. |
| 392 | InterfaceInfo = // Create the new PassInfo for the interface... |
| 393 | new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0); |
| 394 | registerPass(InterfaceInfo); |
| 395 | PIObj = 0; |
| 396 | } |
| 397 | assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup && |
| 398 | "Trying to join an analysis group that is a normal pass!"); |
| 399 | |
| 400 | if (Pass) { |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 401 | ImplementationInfo = Pass::lookupPassInfo(*Pass); |
| 402 | assert(ImplementationInfo && |
| 403 | "Must register pass before adding to AnalysisGroup!"); |
| 404 | |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 405 | // Make sure we keep track of the fact that the implementation implements |
| 406 | // the interface. |
| 407 | PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo); |
| 408 | IIPI->addInterfaceImplemented(InterfaceInfo); |
| 409 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 410 | // Lazily allocate to avoid nasty initialization order dependencies |
| 411 | if (AnalysisGroupInfoMap == 0) |
| 412 | AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>(); |
| 413 | |
| 414 | AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo]; |
| 415 | assert(AGI.Implementations.count(ImplementationInfo) == 0 && |
| 416 | "Cannot add a pass to the same analysis group more than once!"); |
| 417 | AGI.Implementations.insert(ImplementationInfo); |
| 418 | if (isDefault) { |
| 419 | assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 && |
| 420 | "Default implementation for analysis group already specified!"); |
| 421 | assert(ImplementationInfo->getNormalCtor() && |
| 422 | "Cannot specify pass as default if it does not have a default ctor"); |
| 423 | AGI.DefaultImpl = ImplementationInfo; |
| 424 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void RegisterAGBase::setGroupName(const char *Name) { |
| 430 | assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!"); |
| 431 | InterfaceInfo->setPassName(Name); |
| 432 | } |
| 433 | |
| 434 | RegisterAGBase::~RegisterAGBase() { |
| 435 | if (ImplementationInfo) { |
| 436 | assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?"); |
| 437 | AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo]; |
| 438 | |
| 439 | assert(AGI.Implementations.count(ImplementationInfo) && |
| 440 | "Pass not a member of analysis group?"); |
| 441 | |
| 442 | if (AGI.DefaultImpl == ImplementationInfo) |
| 443 | AGI.DefaultImpl = 0; |
| 444 | |
| 445 | AGI.Implementations.erase(ImplementationInfo); |
| 446 | |
| 447 | // Last member of this analysis group? Unregister PassInfo, delete map entry |
| 448 | if (AGI.Implementations.empty()) { |
| 449 | assert(AGI.DefaultImpl == 0 && |
| 450 | "Default implementation didn't unregister?"); |
| 451 | AnalysisGroupInfoMap->erase(InterfaceInfo); |
| 452 | if (AnalysisGroupInfoMap->empty()) { // Delete map if empty |
| 453 | delete AnalysisGroupInfoMap; |
| 454 | AnalysisGroupInfoMap = 0; |
| 455 | } |
| 456 | |
| 457 | unregisterPass(InterfaceInfo); |
| 458 | } |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 463 | //===----------------------------------------------------------------------===// |
| 464 | // PassRegistrationListener implementation |
| 465 | // |
| 466 | |
| 467 | // PassRegistrationListener ctor - Add the current object to the list of |
| 468 | // PassRegistrationListeners... |
| 469 | PassRegistrationListener::PassRegistrationListener() { |
| 470 | if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); |
| 471 | Listeners->push_back(this); |
| 472 | } |
| 473 | |
| 474 | // dtor - Remove object from list of listeners... |
| 475 | PassRegistrationListener::~PassRegistrationListener() { |
| 476 | std::vector<PassRegistrationListener*>::iterator I = |
| 477 | std::find(Listeners->begin(), Listeners->end(), this); |
| 478 | assert(Listeners && I != Listeners->end() && |
| 479 | "PassRegistrationListener not registered!"); |
| 480 | Listeners->erase(I); |
| 481 | |
| 482 | if (Listeners->empty()) { |
| 483 | delete Listeners; |
| 484 | Listeners = 0; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | // enumeratePasses - Iterate over the registered passes, calling the |
| 489 | // passEnumerate callback on each PassInfo object. |
| 490 | // |
| 491 | void PassRegistrationListener::enumeratePasses() { |
| 492 | if (PassInfoMap) |
| 493 | for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(), |
| 494 | E = PassInfoMap->end(); I != E; ++I) |
| 495 | passEnumerate(I->second); |
| 496 | } |