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