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