Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 1 | //===- Pass.cpp - LLVM Pass Infrastructure Impementation ------------------===// |
| 2 | // |
| 3 | // This file implements the LLVM Pass infrastructure. It is primarily |
| 4 | // responsible with ensuring that passes are executed and batched together |
| 5 | // optimally. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 9 | #include "llvm/PassManager.h" |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 10 | #include "PassManagerT.h" // PassManagerT implementation |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 11 | #include "llvm/Module.h" |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 12 | #include "Support/STLExtras.h" |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 13 | #include "Support/TypeInfo.h" |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 14 | #include <stdio.h> |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 15 | #include <sys/resource.h> |
Chris Lattner | 548002f | 2002-09-13 14:47:12 +0000 | [diff] [blame] | 16 | #include <sys/time.h> |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 17 | #include <sys/unistd.h> |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 18 | #include <set> |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 675e7a9 | 2002-08-21 23:51:51 +0000 | [diff] [blame] | 20 | // IncludeFile - Stub function used to help linking out. |
| 21 | IncludeFile::IncludeFile(void*) {} |
| 22 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 23 | //===----------------------------------------------------------------------===// |
| 24 | // AnalysisID Class Implementation |
| 25 | // |
| 26 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 27 | static std::vector<const PassInfo*> CFGOnlyAnalyses; |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 29 | void RegisterPassBase::setPreservesCFG() { |
| 30 | CFGOnlyAnalyses.push_back(PIObj); |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | //===----------------------------------------------------------------------===// |
| 34 | // AnalysisResolver Class Implementation |
| 35 | // |
| 36 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 37 | void AnalysisResolver::setAnalysisResolver(Pass *P, AnalysisResolver *AR) { |
| 38 | assert(P->Resolver == 0 && "Pass already in a PassManager!"); |
| 39 | P->Resolver = AR; |
| 40 | } |
| 41 | |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // AnalysisUsage Class Implementation |
| 44 | // |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 45 | |
| 46 | // preservesCFG - This function should be called to by the pass, iff they do |
| 47 | // not: |
| 48 | // |
| 49 | // 1. Add or remove basic blocks from the function |
| 50 | // 2. Modify terminator instructions in any way. |
| 51 | // |
| 52 | // This function annotates the AnalysisUsage info object to say that analyses |
| 53 | // that only depend on the CFG are preserved by this pass. |
| 54 | // |
| 55 | void AnalysisUsage::preservesCFG() { |
Chris Lattner | 7e0dbe6 | 2002-05-06 19:31:52 +0000 | [diff] [blame] | 56 | // Since this transformation doesn't modify the CFG, it preserves all analyses |
| 57 | // that only depend on the CFG (like dominators, loop info, etc...) |
| 58 | // |
| 59 | Preserved.insert(Preserved.end(), |
| 60 | CFGOnlyAnalyses.begin(), CFGOnlyAnalyses.end()); |
Chris Lattner | ee2ff5d | 2002-04-28 21:25:41 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 64 | //===----------------------------------------------------------------------===// |
| 65 | // PassManager implementation - The PassManager class is a simple Pimpl class |
| 66 | // that wraps the PassManagerT template. |
| 67 | // |
| 68 | PassManager::PassManager() : PM(new PassManagerT<Module>()) {} |
| 69 | PassManager::~PassManager() { delete PM; } |
| 70 | void PassManager::add(Pass *P) { PM->add(P); } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 71 | bool PassManager::run(Module &M) { return PM->run(M); } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 37c8667 | 2002-04-28 20:46:05 +0000 | [diff] [blame] | 73 | |
| 74 | //===----------------------------------------------------------------------===// |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 75 | // TimingInfo Class - This class is used to calculate information about the |
| 76 | // amount of time each pass takes to execute. This only happens with |
| 77 | // -time-passes is enabled on the command line. |
| 78 | // |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 79 | static cl::opt<bool> |
| 80 | EnableTiming("time-passes", |
| 81 | cl::desc("Time each pass, printing elapsed time for each on exit")); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 83 | static TimeRecord getTimeRecord() { |
| 84 | static unsigned long PageSize = 0; |
| 85 | |
| 86 | if (PageSize == 0) { |
| 87 | #ifdef _SC_PAGE_SIZE |
| 88 | PageSize = sysconf(_SC_PAGE_SIZE); |
| 89 | #else |
| 90 | #ifdef _SC_PAGESIZE |
| 91 | PageSize = sysconf(_SC_PAGESIZE); |
| 92 | #else |
| 93 | PageSize = getpagesize(); |
| 94 | #endif |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | struct rusage RU; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 99 | struct timeval T; |
| 100 | gettimeofday(&T, 0); |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 101 | if (getrusage(RUSAGE_SELF, &RU)) { |
| 102 | perror("getrusage call failed: -time-passes info incorrect!"); |
| 103 | } |
| 104 | |
| 105 | TimeRecord Result; |
| 106 | Result.Elapsed = T.tv_sec + T.tv_usec/1000000.0; |
| 107 | Result.UserTime = RU.ru_utime.tv_sec + RU.ru_utime.tv_usec/1000000.0; |
| 108 | Result.SystemTime = RU.ru_stime.tv_sec + RU.ru_stime.tv_usec/1000000.0; |
| 109 | Result.MaxRSS = RU.ru_maxrss*PageSize; |
| 110 | |
| 111 | return Result; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | e821d78 | 2002-08-20 18:47:53 +0000 | [diff] [blame] | 114 | bool TimeRecord::operator<(const TimeRecord &TR) const { |
| 115 | // Primary sort key is User+System time |
| 116 | if (UserTime+SystemTime < TR.UserTime+TR.SystemTime) |
| 117 | return true; |
| 118 | if (UserTime+SystemTime > TR.UserTime+TR.SystemTime) |
| 119 | return false; |
| 120 | |
| 121 | // Secondary sort key is Wall Time |
| 122 | return Elapsed < TR.Elapsed; |
| 123 | } |
| 124 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 125 | void TimeRecord::passStart(const TimeRecord &T) { |
| 126 | Elapsed -= T.Elapsed; |
| 127 | UserTime -= T.UserTime; |
| 128 | SystemTime -= T.SystemTime; |
| 129 | RSSTemp = T.MaxRSS; |
| 130 | } |
| 131 | |
| 132 | void TimeRecord::passEnd(const TimeRecord &T) { |
| 133 | Elapsed += T.Elapsed; |
| 134 | UserTime += T.UserTime; |
| 135 | SystemTime += T.SystemTime; |
| 136 | RSSTemp = T.MaxRSS - RSSTemp; |
| 137 | MaxRSS = std::max(MaxRSS, RSSTemp); |
| 138 | } |
| 139 | |
Chris Lattner | 5ec216b | 2002-08-19 15:43:33 +0000 | [diff] [blame] | 140 | static void printVal(double Val, double Total) { |
| 141 | if (Total < 1e-7) // Avoid dividing by zero... |
Chris Lattner | ca5afe7 | 2002-08-19 20:42:12 +0000 | [diff] [blame] | 142 | fprintf(stderr, " ----- "); |
Chris Lattner | 5ec216b | 2002-08-19 15:43:33 +0000 | [diff] [blame] | 143 | else |
| 144 | fprintf(stderr, " %7.4f (%5.1f%%)", Val, Val*100/Total); |
| 145 | } |
| 146 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 147 | void TimeRecord::print(const char *PassName, const TimeRecord &Total) const { |
Chris Lattner | 5ec216b | 2002-08-19 15:43:33 +0000 | [diff] [blame] | 148 | printVal(UserTime, Total.UserTime); |
| 149 | printVal(SystemTime, Total.SystemTime); |
| 150 | printVal(UserTime+SystemTime, Total.UserTime+Total.SystemTime); |
| 151 | printVal(Elapsed, Total.Elapsed); |
| 152 | |
| 153 | fprintf(stderr, " "); |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 154 | |
| 155 | if (Total.MaxRSS) |
| 156 | std::cerr << MaxRSS << "\t"; |
| 157 | std::cerr << PassName << "\n"; |
| 158 | } |
| 159 | |
| 160 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 161 | // Create method. If Timing is enabled, this creates and returns a new timing |
| 162 | // object, otherwise it returns null. |
| 163 | // |
| 164 | TimingInfo *TimingInfo::create() { |
| 165 | return EnableTiming ? new TimingInfo() : 0; |
| 166 | } |
| 167 | |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 168 | void TimingInfo::passStarted(Pass *P) { |
| 169 | TimingData[P].passStart(getTimeRecord()); |
| 170 | } |
| 171 | void TimingInfo::passEnded(Pass *P) { |
| 172 | TimingData[P].passEnd(getTimeRecord()); |
| 173 | } |
| 174 | void TimeRecord::sum(const TimeRecord &TR) { |
| 175 | Elapsed += TR.Elapsed; |
| 176 | UserTime += TR.UserTime; |
| 177 | SystemTime += TR.SystemTime; |
| 178 | MaxRSS += TR.MaxRSS; |
| 179 | } |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 180 | |
| 181 | // TimingDtor - Print out information about timing information |
| 182 | TimingInfo::~TimingInfo() { |
| 183 | // Iterate over all of the data, converting it into the dual of the data map, |
| 184 | // so that the data is sorted by amount of time taken, instead of pointer. |
| 185 | // |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 186 | std::vector<std::pair<TimeRecord, Pass*> > Data; |
| 187 | TimeRecord Total; |
| 188 | for (std::map<Pass*, TimeRecord>::iterator I = TimingData.begin(), |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 189 | E = TimingData.end(); I != E; ++I) |
| 190 | // Throw out results for "grouping" pass managers... |
| 191 | if (!dynamic_cast<AnalysisResolver*>(I->first)) { |
| 192 | Data.push_back(std::make_pair(I->second, I->first)); |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 193 | Total.sum(I->second); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Sort the data by time as the primary key, in reverse order... |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 197 | std::sort(Data.begin(), Data.end(), |
| 198 | std::greater<std::pair<TimeRecord, Pass*> >()); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 199 | |
| 200 | // Print out timing header... |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 201 | std::cerr << std::string(79, '=') << "\n" |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 202 | << " ... Pass execution timing report ...\n" |
| 203 | << std::string(79, '=') << "\n Total Execution Time: " |
| 204 | << (Total.UserTime+Total.SystemTime) << " seconds (" |
| 205 | << Total.Elapsed << " wall clock)\n\n ---User Time--- " |
| 206 | << "--System Time-- --User+System-- ---Wall Time---"; |
| 207 | |
| 208 | if (Total.MaxRSS) |
| 209 | std::cerr << " ---Mem---"; |
| 210 | std::cerr << " --- Pass Name ---\n"; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 211 | |
| 212 | // Loop through all of the timing data, printing it out... |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 213 | for (unsigned i = 0, e = Data.size(); i != e; ++i) |
| 214 | Data[i].first.print(Data[i].second->getPassName(), Total); |
| 215 | |
| 216 | Total.print("TOTAL", Total); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 220 | void PMDebug::PrintArgumentInformation(const Pass *P) { |
| 221 | // Print out passes in pass manager... |
| 222 | if (const AnalysisResolver *PM = dynamic_cast<const AnalysisResolver*>(P)) { |
| 223 | for (unsigned i = 0, e = PM->getNumContainedPasses(); i != e; ++i) |
| 224 | PrintArgumentInformation(PM->getContainedPass(i)); |
| 225 | |
| 226 | } else { // Normal pass. Print argument information... |
| 227 | // Print out arguments for registered passes that are _optimizations_ |
| 228 | if (const PassInfo *PI = P->getPassInfo()) |
| 229 | if (PI->getPassType() & PassInfo::Optimization) |
| 230 | std::cerr << " -" << PI->getPassArgument(); |
| 231 | } |
| 232 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 233 | |
| 234 | void PMDebug::PrintPassInformation(unsigned Depth, const char *Action, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 235 | Pass *P, Annotable *V) { |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 236 | if (PassDebugging >= Executions) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 237 | std::cerr << (void*)P << std::string(Depth*2+1, ' ') << Action << " '" |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 238 | << P->getPassName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 239 | if (V) { |
| 240 | std::cerr << "' on "; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 241 | |
| 242 | if (dynamic_cast<Module*>(V)) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 243 | std::cerr << "Module\n"; return; |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 244 | } else if (Function *F = dynamic_cast<Function*>(V)) |
| 245 | std::cerr << "Function '" << F->getName(); |
| 246 | else if (BasicBlock *BB = dynamic_cast<BasicBlock*>(V)) |
| 247 | std::cerr << "BasicBlock '" << BB->getName(); |
| 248 | else if (Value *Val = dynamic_cast<Value*>(V)) |
| 249 | std::cerr << typeid(*Val).name() << " '" << Val->getName(); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 250 | } |
| 251 | std::cerr << "'...\n"; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void PMDebug::PrintAnalysisSetInfo(unsigned Depth, const char *Msg, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 256 | Pass *P, const std::vector<AnalysisID> &Set){ |
Chris Lattner | f5cad15 | 2002-07-22 02:10:13 +0000 | [diff] [blame] | 257 | if (PassDebugging >= Details && !Set.empty()) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 258 | std::cerr << (void*)P << std::string(Depth*2+3, ' ') << Msg << " Analyses:"; |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 259 | for (unsigned i = 0; i != Set.size(); ++i) { |
| 260 | if (i) std::cerr << ","; |
| 261 | std::cerr << " " << Set[i]->getPassName(); |
| 262 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 263 | std::cerr << "\n"; |
| 264 | } |
| 265 | } |
| 266 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 267 | //===----------------------------------------------------------------------===// |
| 268 | // Pass Implementation |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 269 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 270 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 271 | void Pass::addToPassManager(PassManagerT<Module> *PM, AnalysisUsage &AU) { |
| 272 | PM->addPass(this, AU); |
Chris Lattner | 654b5bc | 2002-01-22 00:17:48 +0000 | [diff] [blame] | 273 | } |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 275 | // dumpPassStructure - Implement the -debug-passes=Structure option |
| 276 | void Pass::dumpPassStructure(unsigned Offset) { |
| 277 | std::cerr << std::string(Offset*2, ' ') << getPassName() << "\n"; |
| 278 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 279 | |
| 280 | // getPassName - Use C++ RTTI to get a SOMEWHAT intelligable name for the pass. |
| 281 | // |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 282 | const char *Pass::getPassName() const { |
| 283 | if (const PassInfo *PI = getPassInfo()) |
| 284 | return PI->getPassName(); |
| 285 | return typeid(*this).name(); |
| 286 | } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 288 | // print - Print out the internal state of the pass. This is called by Analyse |
| 289 | // to print out the contents of an analysis. Otherwise it is not neccesary to |
| 290 | // implement this method. |
| 291 | // |
| 292 | void Pass::print(std::ostream &O) const { |
| 293 | O << "Pass::print not implemented for pass: '" << getPassName() << "'!\n"; |
| 294 | } |
| 295 | |
| 296 | // dump - call print(std::cerr); |
| 297 | void Pass::dump() const { |
| 298 | print(std::cerr, 0); |
| 299 | } |
| 300 | |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 301 | //===----------------------------------------------------------------------===// |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame^] | 302 | // ImmutablePass Implementation |
| 303 | // |
| 304 | void ImmutablePass::addToPassManager(PassManagerT<Module> *PM, |
| 305 | AnalysisUsage &AU) { |
| 306 | PM->addPass(this, AU); |
| 307 | } |
| 308 | |
| 309 | |
| 310 | //===----------------------------------------------------------------------===// |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 311 | // FunctionPass Implementation |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 312 | // |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 313 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 314 | // run - On a module, we run this pass by initializing, runOnFunction'ing once |
| 315 | // for every function in the module, then by finalizing. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 316 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 317 | bool FunctionPass::run(Module &M) { |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 318 | bool Changed = doInitialization(M); |
| 319 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 320 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 321 | if (!I->isExternal()) // Passes are not run on external functions! |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 322 | Changed |= runOnFunction(*I); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 323 | |
| 324 | return Changed | doFinalization(M); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 327 | // run - On a function, we simply initialize, run the function, then finalize. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 328 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 329 | bool FunctionPass::run(Function &F) { |
| 330 | if (F.isExternal()) return false;// Passes are not run on external functions! |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 332 | return doInitialization(*F.getParent()) | runOnFunction(F) |
| 333 | | doFinalization(*F.getParent()); |
Chris Lattner | 26e4f89 | 2002-01-21 07:37:31 +0000 | [diff] [blame] | 334 | } |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 335 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 336 | void FunctionPass::addToPassManager(PassManagerT<Module> *PM, |
| 337 | AnalysisUsage &AU) { |
| 338 | PM->addPass(this, AU); |
Chris Lattner | d013ba9 | 2002-01-23 05:49:41 +0000 | [diff] [blame] | 339 | } |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 340 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 341 | void FunctionPass::addToPassManager(PassManagerT<Function> *PM, |
| 342 | AnalysisUsage &AU) { |
| 343 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | //===----------------------------------------------------------------------===// |
| 347 | // BasicBlockPass Implementation |
| 348 | // |
| 349 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 350 | // To run this pass on a function, we simply call runOnBasicBlock once for each |
| 351 | // function. |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 352 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 353 | bool BasicBlockPass::runOnFunction(Function &F) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 354 | bool Changed = doInitialization(F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 355 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 356 | Changed |= runOnBasicBlock(*I); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 357 | return Changed | doFinalization(F); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | // To run directly on the basic block, we initialize, runOnBasicBlock, then |
| 361 | // finalize. |
| 362 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 363 | bool BasicBlockPass::run(BasicBlock &BB) { |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 364 | Function &F = *BB.getParent(); |
| 365 | Module &M = *F.getParent(); |
| 366 | return doInitialization(M) | doInitialization(F) | runOnBasicBlock(BB) | |
| 367 | doFinalization(F) | doFinalization(M); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Chris Lattner | 57698e2 | 2002-03-26 18:01:55 +0000 | [diff] [blame] | 370 | void BasicBlockPass::addToPassManager(PassManagerT<Function> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 371 | AnalysisUsage &AU) { |
| 372 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void BasicBlockPass::addToPassManager(PassManagerT<BasicBlock> *PM, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 376 | AnalysisUsage &AU) { |
| 377 | PM->addPass(this, AU); |
Chris Lattner | cdd09c2 | 2002-01-31 00:45:31 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 380 | |
| 381 | //===----------------------------------------------------------------------===// |
| 382 | // Pass Registration mechanism |
| 383 | // |
| 384 | static std::map<TypeInfo, PassInfo*> *PassInfoMap = 0; |
| 385 | static std::vector<PassRegistrationListener*> *Listeners = 0; |
| 386 | |
| 387 | // getPassInfo - Return the PassInfo data structure that corresponds to this |
| 388 | // pass... |
| 389 | const PassInfo *Pass::getPassInfo() const { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 390 | if (PassInfoCache) return PassInfoCache; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 391 | return lookupPassInfo(typeid(*this)); |
| 392 | } |
| 393 | |
| 394 | const PassInfo *Pass::lookupPassInfo(const std::type_info &TI) { |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 395 | if (PassInfoMap == 0) return 0; |
Chris Lattner | 4b16963 | 2002-08-21 17:08:37 +0000 | [diff] [blame] | 396 | std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->find(TI); |
Chris Lattner | 071577d | 2002-07-29 21:02:31 +0000 | [diff] [blame] | 397 | return (I != PassInfoMap->end()) ? I->second : 0; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void RegisterPassBase::registerPass(PassInfo *PI) { |
| 401 | if (PassInfoMap == 0) |
| 402 | PassInfoMap = new std::map<TypeInfo, PassInfo*>(); |
| 403 | |
| 404 | assert(PassInfoMap->find(PI->getTypeInfo()) == PassInfoMap->end() && |
| 405 | "Pass already registered!"); |
| 406 | PIObj = PI; |
| 407 | PassInfoMap->insert(std::make_pair(TypeInfo(PI->getTypeInfo()), PI)); |
| 408 | |
| 409 | // Notify any listeners... |
| 410 | if (Listeners) |
| 411 | for (std::vector<PassRegistrationListener*>::iterator |
| 412 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
| 413 | (*I)->passRegistered(PI); |
| 414 | } |
| 415 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 416 | void RegisterPassBase::unregisterPass(PassInfo *PI) { |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 417 | assert(PassInfoMap && "Pass registered but not in map!"); |
| 418 | std::map<TypeInfo, PassInfo*>::iterator I = |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 419 | PassInfoMap->find(PI->getTypeInfo()); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 420 | assert(I != PassInfoMap->end() && "Pass registered but not in map!"); |
| 421 | |
| 422 | // Remove pass from the map... |
| 423 | PassInfoMap->erase(I); |
| 424 | if (PassInfoMap->empty()) { |
| 425 | delete PassInfoMap; |
| 426 | PassInfoMap = 0; |
| 427 | } |
| 428 | |
| 429 | // Notify any listeners... |
| 430 | if (Listeners) |
| 431 | for (std::vector<PassRegistrationListener*>::iterator |
| 432 | I = Listeners->begin(), E = Listeners->end(); I != E; ++I) |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 433 | (*I)->passUnregistered(PI); |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 434 | |
| 435 | // Delete the PassInfo object itself... |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 436 | delete PI; |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 439 | //===----------------------------------------------------------------------===// |
| 440 | // Analysis Group Implementation Code |
| 441 | //===----------------------------------------------------------------------===// |
| 442 | |
| 443 | struct AnalysisGroupInfo { |
| 444 | const PassInfo *DefaultImpl; |
| 445 | std::set<const PassInfo *> Implementations; |
| 446 | AnalysisGroupInfo() : DefaultImpl(0) {} |
| 447 | }; |
| 448 | |
| 449 | static std::map<const PassInfo *, AnalysisGroupInfo> *AnalysisGroupInfoMap = 0; |
| 450 | |
| 451 | // RegisterAGBase implementation |
| 452 | // |
| 453 | RegisterAGBase::RegisterAGBase(const std::type_info &Interface, |
| 454 | const std::type_info *Pass, bool isDefault) |
| 455 | : ImplementationInfo(0), isDefaultImplementation(isDefault) { |
| 456 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 457 | InterfaceInfo = const_cast<PassInfo*>(Pass::lookupPassInfo(Interface)); |
| 458 | if (InterfaceInfo == 0) { // First reference to Interface, add it now. |
| 459 | InterfaceInfo = // Create the new PassInfo for the interface... |
| 460 | new PassInfo("", "", Interface, PassInfo::AnalysisGroup, 0, 0); |
| 461 | registerPass(InterfaceInfo); |
| 462 | PIObj = 0; |
| 463 | } |
| 464 | assert(InterfaceInfo->getPassType() == PassInfo::AnalysisGroup && |
| 465 | "Trying to join an analysis group that is a normal pass!"); |
| 466 | |
| 467 | if (Pass) { |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 468 | ImplementationInfo = Pass::lookupPassInfo(*Pass); |
| 469 | assert(ImplementationInfo && |
| 470 | "Must register pass before adding to AnalysisGroup!"); |
| 471 | |
Chris Lattner | b3708e2 | 2002-08-30 20:23:45 +0000 | [diff] [blame] | 472 | // Make sure we keep track of the fact that the implementation implements |
| 473 | // the interface. |
| 474 | PassInfo *IIPI = const_cast<PassInfo*>(ImplementationInfo); |
| 475 | IIPI->addInterfaceImplemented(InterfaceInfo); |
| 476 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 477 | // Lazily allocate to avoid nasty initialization order dependencies |
| 478 | if (AnalysisGroupInfoMap == 0) |
| 479 | AnalysisGroupInfoMap = new std::map<const PassInfo *,AnalysisGroupInfo>(); |
| 480 | |
| 481 | AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo]; |
| 482 | assert(AGI.Implementations.count(ImplementationInfo) == 0 && |
| 483 | "Cannot add a pass to the same analysis group more than once!"); |
| 484 | AGI.Implementations.insert(ImplementationInfo); |
| 485 | if (isDefault) { |
| 486 | assert(AGI.DefaultImpl == 0 && InterfaceInfo->getNormalCtor() == 0 && |
| 487 | "Default implementation for analysis group already specified!"); |
| 488 | assert(ImplementationInfo->getNormalCtor() && |
| 489 | "Cannot specify pass as default if it does not have a default ctor"); |
| 490 | AGI.DefaultImpl = ImplementationInfo; |
| 491 | InterfaceInfo->setNormalCtor(ImplementationInfo->getNormalCtor()); |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void RegisterAGBase::setGroupName(const char *Name) { |
| 497 | assert(InterfaceInfo->getPassName()[0] == 0 && "Interface Name already set!"); |
| 498 | InterfaceInfo->setPassName(Name); |
| 499 | } |
| 500 | |
| 501 | RegisterAGBase::~RegisterAGBase() { |
| 502 | if (ImplementationInfo) { |
| 503 | assert(AnalysisGroupInfoMap && "Inserted into map, but map doesn't exist?"); |
| 504 | AnalysisGroupInfo &AGI = (*AnalysisGroupInfoMap)[InterfaceInfo]; |
| 505 | |
| 506 | assert(AGI.Implementations.count(ImplementationInfo) && |
| 507 | "Pass not a member of analysis group?"); |
| 508 | |
| 509 | if (AGI.DefaultImpl == ImplementationInfo) |
| 510 | AGI.DefaultImpl = 0; |
| 511 | |
| 512 | AGI.Implementations.erase(ImplementationInfo); |
| 513 | |
| 514 | // Last member of this analysis group? Unregister PassInfo, delete map entry |
| 515 | if (AGI.Implementations.empty()) { |
| 516 | assert(AGI.DefaultImpl == 0 && |
| 517 | "Default implementation didn't unregister?"); |
| 518 | AnalysisGroupInfoMap->erase(InterfaceInfo); |
| 519 | if (AnalysisGroupInfoMap->empty()) { // Delete map if empty |
| 520 | delete AnalysisGroupInfoMap; |
| 521 | AnalysisGroupInfoMap = 0; |
| 522 | } |
| 523 | |
| 524 | unregisterPass(InterfaceInfo); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | |
Chris Lattner | 37d3c95 | 2002-07-23 18:08:00 +0000 | [diff] [blame] | 530 | //===----------------------------------------------------------------------===// |
| 531 | // PassRegistrationListener implementation |
| 532 | // |
| 533 | |
| 534 | // PassRegistrationListener ctor - Add the current object to the list of |
| 535 | // PassRegistrationListeners... |
| 536 | PassRegistrationListener::PassRegistrationListener() { |
| 537 | if (!Listeners) Listeners = new std::vector<PassRegistrationListener*>(); |
| 538 | Listeners->push_back(this); |
| 539 | } |
| 540 | |
| 541 | // dtor - Remove object from list of listeners... |
| 542 | PassRegistrationListener::~PassRegistrationListener() { |
| 543 | std::vector<PassRegistrationListener*>::iterator I = |
| 544 | std::find(Listeners->begin(), Listeners->end(), this); |
| 545 | assert(Listeners && I != Listeners->end() && |
| 546 | "PassRegistrationListener not registered!"); |
| 547 | Listeners->erase(I); |
| 548 | |
| 549 | if (Listeners->empty()) { |
| 550 | delete Listeners; |
| 551 | Listeners = 0; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | // enumeratePasses - Iterate over the registered passes, calling the |
| 556 | // passEnumerate callback on each PassInfo object. |
| 557 | // |
| 558 | void PassRegistrationListener::enumeratePasses() { |
| 559 | if (PassInfoMap) |
| 560 | for (std::map<TypeInfo, PassInfo*>::iterator I = PassInfoMap->begin(), |
| 561 | E = PassInfoMap->end(); I != E; ++I) |
| 562 | passEnumerate(I->second); |
| 563 | } |