Chris Lattner | c7bc3bb | 2003-10-13 05:34:24 +0000 | [diff] [blame] | 1 | //===- PassManagerT.h - Container for Passes --------------------*- C++ -*-===// |
John Criswell | 29265fe | 2003-10-21 15:17:13 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 10 | // This file defines the PassManagerT class. This class is used to hold, |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 11 | // maintain, and optimize execution of Pass's. The PassManager class ensures |
| 12 | // that analysis results are available before a pass runs, and that Pass's are |
| 13 | // destroyed when the PassManager is destroyed. |
| 14 | // |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 15 | // The PassManagerT template is instantiated three times to do its job. The |
| 16 | // public PassManager class is a Pimpl around the PassManagerT<Module> interface |
| 17 | // to avoid having all of the PassManager clients being exposed to the |
| 18 | // implementation details herein. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 22 | #ifndef LLVM_PASSMANAGER_T_H |
| 23 | #define LLVM_PASSMANAGER_T_H |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 24 | |
| 25 | #include "llvm/Pass.h" |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 26 | #include "Support/CommandLine.h" |
Chris Lattner | ed6504b | 2002-09-08 19:00:07 +0000 | [diff] [blame] | 27 | #include "Support/LeakDetector.h" |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 28 | #include "Support/Timer.h" |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 29 | #include <algorithm> |
Chris Lattner | 395c27a | 2002-07-31 18:04:17 +0000 | [diff] [blame] | 30 | #include <iostream> |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 31 | class Annotable; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 34 | // Pass debugging information. Often it is useful to find out what pass is |
| 35 | // running when a crash occurs in a utility. When this library is compiled with |
| 36 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 37 | // pass name to be printed before it executes. |
| 38 | // |
| 39 | |
| 40 | // Different debug levels that can be enabled... |
| 41 | enum PassDebugLevel { |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 42 | None, Arguments, Structure, Executions, Details |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static cl::opt<enum PassDebugLevel> |
| 46 | PassDebugging("debug-pass", cl::Hidden, |
| 47 | cl::desc("Print PassManager debugging information"), |
| 48 | cl::values( |
| 49 | clEnumVal(None , "disable debug output"), |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 50 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 51 | clEnumVal(Structure , "print pass structure before run()"), |
| 52 | clEnumVal(Executions, "print pass name before it is executed"), |
| 53 | clEnumVal(Details , "print pass details when it is executed"), |
| 54 | 0)); |
| 55 | |
| 56 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57d2ba3 | 2002-04-04 19:35:24 +0000 | [diff] [blame] | 57 | // PMDebug class - a set of debugging functions, that are not to be |
| 58 | // instantiated by the template. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 59 | // |
| 60 | struct PMDebug { |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 61 | static void PerformPassStartupStuff(Pass *P) { |
| 62 | // If debugging is enabled, print out argument information... |
| 63 | if (PassDebugging >= Arguments) { |
| 64 | std::cerr << "Pass Arguments: "; |
| 65 | PrintArgumentInformation(P); |
| 66 | std::cerr << "\n"; |
| 67 | |
| 68 | // Print the pass execution structure |
| 69 | if (PassDebugging >= Structure) |
| 70 | P->dumpPassStructure(); |
| 71 | } |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 72 | } |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 73 | |
| 74 | static void PrintArgumentInformation(const Pass *P); |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 75 | static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 76 | static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 77 | const std::vector<AnalysisID> &); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
| 80 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 81 | //===----------------------------------------------------------------------===// |
| 82 | // TimingInfo Class - This class is used to calculate information about the |
| 83 | // amount of time each pass takes to execute. This only happens when |
| 84 | // -time-passes is enabled on the command line. |
| 85 | // |
Chris Lattner | 6a33d6f | 2002-08-01 19:33:09 +0000 | [diff] [blame] | 86 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 87 | class TimingInfo { |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 88 | std::map<Pass*, Timer> TimingData; |
| 89 | TimerGroup TG; |
| 90 | |
| 91 | // Private ctor, must use 'create' member |
| 92 | TimingInfo() : TG("... Pass execution timing report ...") {} |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 93 | public: |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 94 | // TimingDtor - Print out information about timing information |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 95 | ~TimingInfo() { |
| 96 | // Delete all of the timers... |
| 97 | TimingData.clear(); |
| 98 | // TimerGroup is deleted next, printing the report. |
| 99 | } |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 100 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 101 | // createTheTimeInfo - This method either initializes the TheTimeInfo pointer |
| 102 | // to a non null value (if the -time-passes option is enabled) or it leaves it |
| 103 | // null. It may be called multiple times. |
| 104 | static void createTheTimeInfo(); |
| 105 | |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 106 | void passStarted(Pass *P) { |
Chris Lattner | d5fc902 | 2002-10-01 20:08:11 +0000 | [diff] [blame] | 107 | if (dynamic_cast<AnalysisResolver*>(P)) return; |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 108 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 109 | if (I == TimingData.end()) |
Chris Lattner | 52db271 | 2002-10-01 20:12:06 +0000 | [diff] [blame] | 110 | I=TimingData.insert(std::make_pair(P, Timer(P->getPassName(), TG))).first; |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 111 | I->second.startTimer(); |
| 112 | } |
| 113 | void passEnded(Pass *P) { |
Chris Lattner | d5fc902 | 2002-10-01 20:08:11 +0000 | [diff] [blame] | 114 | if (dynamic_cast<AnalysisResolver*>(P)) return; |
Chris Lattner | af751b8 | 2002-10-01 19:54:07 +0000 | [diff] [blame] | 115 | std::map<Pass*, Timer>::iterator I = TimingData.find(P); |
| 116 | assert (I != TimingData.end() && "passStarted/passEnded not nested right!"); |
| 117 | I->second.stopTimer(); |
| 118 | } |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 121 | static TimingInfo *TheTimeInfo; |
| 122 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 123 | //===----------------------------------------------------------------------===// |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 124 | // Declare the PassManagerTraits which will be specialized... |
| 125 | // |
| 126 | template<class UnitType> class PassManagerTraits; // Do not define. |
| 127 | |
| 128 | |
| 129 | //===----------------------------------------------------------------------===// |
| 130 | // PassManagerT - Container object for passes. The PassManagerT destructor |
| 131 | // deletes all passes contained inside of the PassManagerT, so you shouldn't |
| 132 | // delete passes manually, and all passes should be dynamically allocated. |
| 133 | // |
| 134 | template<typename UnitType> |
| 135 | class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{ |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 136 | typedef PassManagerTraits<UnitType> Traits; |
| 137 | typedef typename Traits::PassClass PassClass; |
| 138 | typedef typename Traits::SubPassClass SubPassClass; |
| 139 | typedef typename Traits::BatcherClass BatcherClass; |
| 140 | typedef typename Traits::ParentClass ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 4c9cd82 | 2003-06-23 19:16:20 +0000 | [diff] [blame] | 142 | friend class PassManagerTraits<UnitType>::PassClass; |
| 143 | friend class PassManagerTraits<UnitType>::SubPassClass; |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 144 | friend class Traits; |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 145 | friend class ImmutablePass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 146 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 147 | std::vector<PassClass*> Passes; // List of passes to run |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 148 | std::vector<ImmutablePass*> ImmutablePasses; // List of immutable passes |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 149 | |
| 150 | // The parent of this pass manager... |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 151 | ParentClass * const Parent; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 152 | |
| 153 | // The current batcher if one is in use, or null |
| 154 | BatcherClass *Batcher; |
| 155 | |
| 156 | // CurrentAnalyses - As the passes are being run, this map contains the |
| 157 | // analyses that are available to the current pass for use. This is accessed |
| 158 | // through the getAnalysis() function in this class and in Pass. |
| 159 | // |
| 160 | std::map<AnalysisID, Pass*> CurrentAnalyses; |
| 161 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 162 | // LastUseOf - This map keeps track of the last usage in our pipeline of a |
| 163 | // particular pass. When executing passes, the memory for .first is free'd |
| 164 | // after .second is run. |
| 165 | // |
| 166 | std::map<Pass*, Pass*> LastUseOf; |
| 167 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 168 | public: |
| 169 | PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {} |
| 170 | ~PassManagerT() { |
| 171 | // Delete all of the contained passes... |
Chris Lattner | a82ee2d | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 172 | for (typename std::vector<PassClass*>::iterator |
| 173 | I = Passes.begin(), E = Passes.end(); I != E; ++I) |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 174 | delete *I; |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 175 | |
| 176 | for (std::vector<ImmutablePass*>::iterator |
| 177 | I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I) |
| 178 | delete *I; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | // run - Run all of the queued passes on the specified module in an optimal |
| 182 | // way. |
| 183 | virtual bool runOnUnit(UnitType *M) { |
| 184 | bool MadeChanges = false; |
| 185 | closeBatcher(); |
| 186 | CurrentAnalyses.clear(); |
| 187 | |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 188 | TimingInfo::createTheTimeInfo(); |
| 189 | |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 190 | // Add any immutable passes to the CurrentAnalyses set... |
Chris Lattner | 480b37d | 2002-09-25 22:26:52 +0000 | [diff] [blame] | 191 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 192 | ImmutablePass *IPass = ImmutablePasses[i]; |
| 193 | if (const PassInfo *PI = IPass->getPassInfo()) { |
| 194 | CurrentAnalyses[PI] = IPass; |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 195 | |
| 196 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 197 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
Chris Lattner | 480b37d | 2002-09-25 22:26:52 +0000 | [diff] [blame] | 198 | CurrentAnalyses[II[i]] = IPass; |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | 480b37d | 2002-09-25 22:26:52 +0000 | [diff] [blame] | 200 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 201 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 202 | // LastUserOf - This contains the inverted LastUseOfMap... |
| 203 | std::map<Pass *, std::vector<Pass*> > LastUserOf; |
| 204 | for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(), |
| 205 | E = LastUseOf.end(); I != E; ++I) |
| 206 | LastUserOf[I->second].push_back(I->first); |
| 207 | |
| 208 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 209 | // Output debug information... |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 210 | if (Parent == 0) PMDebug::PerformPassStartupStuff(this); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 211 | |
| 212 | // Run all of the passes |
| 213 | for (unsigned i = 0, e = Passes.size(); i < e; ++i) { |
| 214 | PassClass *P = Passes[i]; |
| 215 | |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 216 | PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, |
| 217 | (Annotable*)M); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 218 | |
| 219 | // Get information about what analyses the pass uses... |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 220 | AnalysisUsage AnUsage; |
| 221 | P->getAnalysisUsage(AnUsage); |
| 222 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P, |
| 223 | AnUsage.getRequiredSet()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 224 | |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 225 | // All Required analyses should be available to the pass as it runs! Here |
| 226 | // we fill in the AnalysisImpls member of the pass so that it can |
| 227 | // successfully use the getAnalysis() method to retrieve the |
| 228 | // implementations it needs. |
| 229 | // |
| 230 | P->AnalysisImpls.clear(); |
| 231 | P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size()); |
| 232 | for (std::vector<const PassInfo *>::const_iterator |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 233 | I = AnUsage.getRequiredSet().begin(), |
| 234 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 235 | Pass *Impl = getAnalysisOrNullUp(*I); |
| 236 | if (Impl == 0) { |
| 237 | std::cerr << "Analysis '" << (*I)->getPassName() |
| 238 | << "' used but not available!"; |
| 239 | assert(0 && "Analysis used but not available!"); |
| 240 | } else if (PassDebugging == Details) { |
| 241 | if ((*I)->getPassName() != std::string(Impl->getPassName())) |
| 242 | std::cerr << " Interface '" << (*I)->getPassName() |
| 243 | << "' implemented by '" << Impl->getPassName() << "'\n"; |
| 244 | } |
| 245 | P->AnalysisImpls.push_back(std::make_pair(*I, Impl)); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 246 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 247 | |
| 248 | // Run the sub pass! |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 249 | if (TheTimeInfo) TheTimeInfo->passStarted(P); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 250 | bool Changed = runPass(P, M); |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 251 | if (TheTimeInfo) TheTimeInfo->passEnded(P); |
Chris Lattner | f2f31bd | 2002-02-01 04:53:36 +0000 | [diff] [blame] | 252 | MadeChanges |= Changed; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 253 | |
Chris Lattner | ed6504b | 2002-09-08 19:00:07 +0000 | [diff] [blame] | 254 | // Check for memory leaks by the pass... |
| 255 | LeakDetector::checkForGarbage(std::string("after running pass '") + |
| 256 | P->getPassName() + "'"); |
| 257 | |
Chris Lattner | f2f31bd | 2002-02-01 04:53:36 +0000 | [diff] [blame] | 258 | if (Changed) |
| 259 | PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 260 | (Annotable*)M); |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 261 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P, |
| 262 | AnUsage.getPreservedSet()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 263 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 264 | |
| 265 | // Erase all analyses not in the preserved set... |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 266 | if (!AnUsage.getPreservesAll()) { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 267 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 268 | for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(), |
| 269 | E = CurrentAnalyses.end(); I != E; ) |
| 270 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) != |
| 271 | PreservedSet.end()) |
| 272 | ++I; // This analysis is preserved, leave it in the available set... |
| 273 | else { |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 274 | if (!dynamic_cast<ImmutablePass*>(I->second)) { |
Chris Lattner | ade85ec | 2003-02-14 05:34:36 +0000 | [diff] [blame] | 275 | std::map<AnalysisID, Pass*>::iterator J = I++; |
| 276 | CurrentAnalyses.erase(J); // Analysis not preserved! |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 277 | } else { |
| 278 | ++I; |
| 279 | } |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 283 | // Add the current pass to the set of passes that have been run, and are |
| 284 | // thus available to users. |
| 285 | // |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 286 | if (const PassInfo *PI = P->getPassInfo()) { |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 287 | CurrentAnalyses[PI] = P; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 289 | // This pass is the current implementation of all of the interfaces it |
| 290 | // implements as well. |
| 291 | // |
| 292 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 293 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 294 | CurrentAnalyses[II[i]] = P; |
| 295 | } |
| 296 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 297 | // Free memory for any passes that we are the last use of... |
| 298 | std::vector<Pass*> &DeadPass = LastUserOf[P]; |
| 299 | for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end(); |
| 300 | I != E; ++I) { |
| 301 | PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 302 | (Annotable*)M); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 303 | (*I)->releaseMemory(); |
| 304 | } |
Chris Lattner | 6b6f540 | 2002-09-29 22:50:22 +0000 | [diff] [blame] | 305 | |
| 306 | // Make sure to remove dead passes from the CurrentAnalyses list... |
| 307 | for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(); |
| 308 | I != CurrentAnalyses.end(); ) { |
| 309 | std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(), |
| 310 | DeadPass.end(), I->second); |
| 311 | if (DPI != DeadPass.end()) { // This pass is dead now... remove it |
| 312 | std::map<AnalysisID, Pass*>::iterator IDead = I++; |
| 313 | CurrentAnalyses.erase(IDead); |
| 314 | } else { |
| 315 | ++I; // Move on to the next element... |
| 316 | } |
| 317 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 318 | } |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 320 | return MadeChanges; |
| 321 | } |
| 322 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 323 | // dumpPassStructure - Implement the -debug-passes=PassStructure option |
| 324 | virtual void dumpPassStructure(unsigned Offset = 0) { |
Chris Lattner | 480b37d | 2002-09-25 22:26:52 +0000 | [diff] [blame] | 325 | // Print out the immutable passes... |
| 326 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) |
| 327 | ImmutablePasses[i]->dumpPassStructure(0); |
| 328 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 329 | std::cerr << std::string(Offset*2, ' ') << Traits::getPMName() |
| 330 | << " Pass Manager\n"; |
Chris Lattner | a82ee2d | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 331 | for (typename std::vector<PassClass*>::iterator |
| 332 | I = Passes.begin(), E = Passes.end(); I != E; ++I) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 333 | PassClass *P = *I; |
| 334 | P->dumpPassStructure(Offset+1); |
| 335 | |
| 336 | // Loop through and see which classes are destroyed after this one... |
| 337 | for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(), |
| 338 | E = LastUseOf.end(); I != E; ++I) { |
| 339 | if (P == I->second) { |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 340 | std::cerr << "--" << std::string(Offset*2, ' '); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 341 | I->first->dumpPassStructure(0); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 346 | |
Chris Lattner | 6043042 | 2003-04-24 20:07:38 +0000 | [diff] [blame] | 347 | Pass *getImmutablePassOrNull(const PassInfo *ID) const { |
| 348 | for (unsigned i = 0, e = ImmutablePasses.size(); i != e; ++i) { |
| 349 | const PassInfo *IPID = ImmutablePasses[i]->getPassInfo(); |
| 350 | if (IPID == ID) |
| 351 | return ImmutablePasses[i]; |
| 352 | |
| 353 | // This pass is the current implementation of all of the interfaces it |
| 354 | // implements as well. |
| 355 | // |
| 356 | const std::vector<const PassInfo*> &II = |
| 357 | IPID->getInterfacesImplemented(); |
| 358 | for (unsigned j = 0, e = II.size(); j != e; ++j) |
| 359 | if (II[j] == ID) return ImmutablePasses[i]; |
| 360 | } |
| 361 | return 0; |
| 362 | } |
| 363 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 364 | Pass *getAnalysisOrNullDown(const PassInfo *ID) const { |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 365 | std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 366 | |
| 367 | if (I != CurrentAnalyses.end()) |
| 368 | return I->second; // Found it. |
| 369 | |
Chris Lattner | 6043042 | 2003-04-24 20:07:38 +0000 | [diff] [blame] | 370 | if (Pass *P = getImmutablePassOrNull(ID)) |
| 371 | return P; |
| 372 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 373 | if (Batcher) |
| 374 | return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID); |
| 375 | return 0; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 378 | Pass *getAnalysisOrNullUp(const PassInfo *ID) const { |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 379 | std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 380 | if (I != CurrentAnalyses.end()) |
| 381 | return I->second; // Found it. |
| 382 | |
| 383 | if (Parent) // Try scanning... |
| 384 | return Parent->getAnalysisOrNullUp(ID); |
Chris Lattner | 6043042 | 2003-04-24 20:07:38 +0000 | [diff] [blame] | 385 | else if (!ImmutablePasses.empty()) |
| 386 | return getImmutablePassOrNull(ID); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 387 | return 0; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // markPassUsed - Inform higher level pass managers (and ourselves) |
| 391 | // that these analyses are being used by this pass. This is used to |
| 392 | // make sure that analyses are not free'd before we have to use |
| 393 | // them... |
| 394 | // |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 395 | void markPassUsed(const PassInfo *P, Pass *User) { |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 396 | std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(P); |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 397 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 398 | if (I != CurrentAnalyses.end()) { |
| 399 | LastUseOf[I->second] = User; // Local pass, extend the lifetime |
| 400 | } else { |
| 401 | // Pass not in current available set, must be a higher level pass |
Misha Brukman | 632df28 | 2002-10-29 23:06:16 +0000 | [diff] [blame] | 402 | // available to us, propagate to parent pass manager... We tell the |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 403 | // parent that we (the passmanager) are using the analysis so that it |
| 404 | // frees the analysis AFTER this pass manager runs. |
| 405 | // |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 406 | if (Parent) { |
| 407 | Parent->markPassUsed(P, this); |
| 408 | } else { |
Chris Lattner | 6043042 | 2003-04-24 20:07:38 +0000 | [diff] [blame] | 409 | assert(getAnalysisOrNullUp(P) && |
| 410 | dynamic_cast<ImmutablePass*>(getAnalysisOrNullUp(P)) && |
| 411 | "Pass available but not found! " |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 412 | "Perhaps this is a module pass requiring a function pass?"); |
| 413 | } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 414 | } |
| 415 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 416 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 417 | // Return the number of parent PassManagers that exist |
| 418 | virtual unsigned getDepth() const { |
| 419 | if (Parent == 0) return 0; |
| 420 | return 1 + Parent->getDepth(); |
| 421 | } |
| 422 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 423 | virtual unsigned getNumContainedPasses() const { return Passes.size(); } |
| 424 | virtual const Pass *getContainedPass(unsigned N) const { |
| 425 | assert(N < Passes.size() && "Pass number out of range!"); |
| 426 | return Passes[N]; |
| 427 | } |
| 428 | |
Chris Lattner | 6e041bd | 2002-08-21 22:17:09 +0000 | [diff] [blame] | 429 | // add - Add a pass to the queue of passes to run. This gives ownership of |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 430 | // the Pass to the PassManager. When the PassManager is destroyed, the pass |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 431 | // will be destroyed as well, so there is no need to delete the pass. This |
| 432 | // implies that all passes MUST be new'd. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 433 | // |
| 434 | void add(PassClass *P) { |
| 435 | // Get information about what analyses the pass uses... |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 436 | AnalysisUsage AnUsage; |
| 437 | P->getAnalysisUsage(AnUsage); |
| 438 | const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet(); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 439 | |
| 440 | // Loop over all of the analyses used by this pass, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 441 | for (std::vector<AnalysisID>::const_iterator I = Required.begin(), |
| 442 | E = Required.end(); I != E; ++I) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 443 | if (getAnalysisOrNullDown(*I) == 0) |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 444 | add((PassClass*)(*I)->createPass()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // Tell the pass to add itself to this PassManager... the way it does so |
| 448 | // depends on the class of the pass, and is critical to laying out passes in |
| 449 | // an optimal order.. |
| 450 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 451 | P->addToPassManager(this, AnUsage); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Brian Gaeke | 2cc4b9d | 2003-08-14 06:07:57 +0000 | [diff] [blame] | 454 | // add - H4x0r an ImmutablePass into a PassManager that might not be |
| 455 | // expecting one. |
| 456 | // |
| 457 | void add(ImmutablePass *P) { |
| 458 | // Get information about what analyses the pass uses... |
| 459 | AnalysisUsage AnUsage; |
| 460 | P->getAnalysisUsage(AnUsage); |
| 461 | const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet(); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 462 | |
Brian Gaeke | 2cc4b9d | 2003-08-14 06:07:57 +0000 | [diff] [blame] | 463 | // Loop over all of the analyses used by this pass, |
| 464 | for (std::vector<AnalysisID>::const_iterator I = Required.begin(), |
| 465 | E = Required.end(); I != E; ++I) { |
| 466 | if (getAnalysisOrNullDown(*I) == 0) |
| 467 | add((PassClass*)(*I)->createPass()); |
| 468 | } |
| 469 | |
| 470 | // Add the ImmutablePass to this PassManager. |
| 471 | addPass(P, AnUsage); |
| 472 | } |
| 473 | |
| 474 | private: |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 475 | // addPass - These functions are used to implement the subclass specific |
| 476 | // behaviors present in PassManager. Basically the add(Pass*) method ends up |
| 477 | // reflecting its behavior into a Pass::addToPassManager call. Subclasses of |
| 478 | // Pass override it specifically so that they can reflect the type |
| 479 | // information inherent in "this" back to the PassManager. |
| 480 | // |
| 481 | // For generic Pass subclasses (which are interprocedural passes), we simply |
| 482 | // add the pass to the end of the pass list and terminate any accumulation of |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 483 | // FunctionPass's that are present. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 484 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 485 | void addPass(PassClass *P, AnalysisUsage &AnUsage) { |
| 486 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 487 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 488 | // FIXME: If this pass being added isn't killed by any of the passes in the |
| 489 | // batcher class then we can reorder to pass to execute before the batcher |
| 490 | // does, which will potentially allow us to batch more passes! |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 491 | // |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 492 | //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet(); |
| 493 | if (Batcher /*&& ProvidedSet.empty()*/) |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 494 | closeBatcher(); // This pass cannot be batched! |
| 495 | |
| 496 | // Set the Resolver instance variable in the Pass so that it knows where to |
| 497 | // find this object... |
| 498 | // |
| 499 | setAnalysisResolver(P, this); |
| 500 | Passes.push_back(P); |
| 501 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 502 | // Inform higher level pass managers (and ourselves) that these analyses are |
| 503 | // being used by this pass. This is used to make sure that analyses are not |
| 504 | // free'd before we have to use them... |
| 505 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 506 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 507 | E = RequiredSet.end(); I != E; ++I) |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 508 | markPassUsed(*I, P); // Mark *I as used by P |
| 509 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 510 | // Erase all analyses not in the preserved set... |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 511 | if (!AnUsage.getPreservesAll()) { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 512 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 513 | for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(), |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 514 | E = CurrentAnalyses.end(); I != E; ) { |
| 515 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) == |
| 516 | PreservedSet.end()) { // Analysis not preserved! |
| 517 | CurrentAnalyses.erase(I); // Remove from available analyses |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 518 | I = CurrentAnalyses.begin(); |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 519 | } else { |
| 520 | ++I; |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 521 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 522 | } |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 523 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 524 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 525 | // Add this pass to the currently available set... |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 526 | if (const PassInfo *PI = P->getPassInfo()) { |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 527 | CurrentAnalyses[PI] = P; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 528 | |
Chris Lattner | 216edd4 | 2002-08-30 20:25:01 +0000 | [diff] [blame] | 529 | // This pass is the current implementation of all of the interfaces it |
| 530 | // implements as well. |
| 531 | // |
| 532 | const std::vector<const PassInfo*> &II = PI->getInterfacesImplemented(); |
| 533 | for (unsigned i = 0, e = II.size(); i != e; ++i) |
| 534 | CurrentAnalyses[II[i]] = P; |
| 535 | } |
| 536 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 537 | // For now assume that our results are never used... |
| 538 | LastUseOf[P] = P; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 541 | // For FunctionPass subclasses, we must be sure to batch the FunctionPass's |
| 542 | // together in a BatcherClass object so that all of the analyses are run |
| 543 | // together a function at a time. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 544 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 545 | void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 546 | if (Batcher == 0) // If we don't have a batcher yet, make one now. |
| 547 | Batcher = new BatcherClass(this); |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 548 | // The Batcher will queue the passes up |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 549 | MP->addToPassManager(Batcher, AnUsage); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // closeBatcher - Terminate the batcher that is being worked on. |
| 553 | void closeBatcher() { |
| 554 | if (Batcher) { |
| 555 | Passes.push_back(Batcher); |
| 556 | Batcher = 0; |
| 557 | } |
| 558 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 559 | |
| 560 | public: |
| 561 | // When an ImmutablePass is added, it gets added to the top level pass |
| 562 | // manager. |
| 563 | void addPass(ImmutablePass *IP, AnalysisUsage &AU) { |
| 564 | if (Parent) { // Make sure this request goes to the top level passmanager... |
| 565 | Parent->addPass(IP, AU); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | // Set the Resolver instance variable in the Pass so that it knows where to |
| 570 | // find this object... |
| 571 | // |
| 572 | setAnalysisResolver(IP, this); |
| 573 | ImmutablePasses.push_back(IP); |
Chris Lattner | d4ed5c6 | 2003-02-26 19:10:57 +0000 | [diff] [blame] | 574 | |
| 575 | // All Required analyses should be available to the pass as it initializes! |
| 576 | // Here we fill in the AnalysisImpls member of the pass so that it can |
| 577 | // successfully use the getAnalysis() method to retrieve the implementations |
| 578 | // it needs. |
| 579 | // |
| 580 | IP->AnalysisImpls.clear(); |
| 581 | IP->AnalysisImpls.reserve(AU.getRequiredSet().size()); |
| 582 | for (std::vector<const PassInfo *>::const_iterator |
| 583 | I = AU.getRequiredSet().begin(), |
| 584 | E = AU.getRequiredSet().end(); I != E; ++I) { |
| 585 | Pass *Impl = getAnalysisOrNullUp(*I); |
| 586 | if (Impl == 0) { |
| 587 | std::cerr << "Analysis '" << (*I)->getPassName() |
| 588 | << "' used but not available!"; |
| 589 | assert(0 && "Analysis used but not available!"); |
| 590 | } else if (PassDebugging == Details) { |
| 591 | if ((*I)->getPassName() != std::string(Impl->getPassName())) |
| 592 | std::cerr << " Interface '" << (*I)->getPassName() |
| 593 | << "' implemented by '" << Impl->getPassName() << "'\n"; |
| 594 | } |
| 595 | IP->AnalysisImpls.push_back(std::make_pair(*I, Impl)); |
| 596 | } |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 597 | |
Chris Lattner | d4ed5c6 | 2003-02-26 19:10:57 +0000 | [diff] [blame] | 598 | // Initialize the immutable pass... |
| 599 | IP->initializePass(); |
Chris Lattner | ee0788d | 2002-09-25 21:59:11 +0000 | [diff] [blame] | 600 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 601 | }; |
| 602 | |
| 603 | |
| 604 | |
| 605 | //===----------------------------------------------------------------------===// |
| 606 | // PassManagerTraits<BasicBlock> Specialization |
| 607 | // |
| 608 | // This pass manager is used to group together all of the BasicBlockPass's |
| 609 | // into a single unit. |
| 610 | // |
| 611 | template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass { |
| 612 | // PassClass - The type of passes tracked by this PassManager |
| 613 | typedef BasicBlockPass PassClass; |
| 614 | |
| 615 | // SubPassClass - The types of classes that should be collated together |
| 616 | // This is impossible to match, so BasicBlock instantiations of PassManagerT |
| 617 | // do not collate. |
| 618 | // |
| 619 | typedef PassManagerT<Module> SubPassClass; |
| 620 | |
| 621 | // BatcherClass - The type to use for collation of subtypes... This class is |
| 622 | // never instantiated for the PassManager<BasicBlock>, but it must be an |
| 623 | // instance of PassClass to typecheck. |
| 624 | // |
| 625 | typedef PassClass BatcherClass; |
| 626 | |
| 627 | // ParentClass - The type of the parent PassManager... |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 628 | typedef PassManagerT<Function> ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 629 | |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 630 | // PMType - The type of the passmanager that subclasses this class |
| 631 | typedef PassManagerT<BasicBlock> PMType; |
| 632 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 633 | // runPass - Specify how the pass should be run on the UnitType |
| 634 | static bool runPass(PassClass *P, BasicBlock *M) { |
| 635 | // todo, init and finalize |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 636 | return P->runOnBasicBlock(*M); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 639 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 640 | // debugging. |
| 641 | const char *getPMName() const { return "BasicBlock"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 642 | virtual const char *getPassName() const { return "BasicBlock Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 644 | // Implement the BasicBlockPass interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 645 | virtual bool doInitialization(Module &M); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 646 | virtual bool doInitialization(Function &F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 647 | virtual bool runOnBasicBlock(BasicBlock &BB); |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 648 | virtual bool doFinalization(Function &F); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 649 | virtual bool doFinalization(Module &M); |
Chris Lattner | cb42906 | 2002-04-30 18:50:17 +0000 | [diff] [blame] | 650 | |
| 651 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 652 | AU.setPreservesAll(); |
| 653 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 654 | }; |
| 655 | |
| 656 | |
| 657 | |
| 658 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 659 | // PassManagerTraits<Function> Specialization |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 660 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 661 | // This pass manager is used to group together all of the FunctionPass's |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 662 | // into a single unit. |
| 663 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 664 | template<> struct PassManagerTraits<Function> : public FunctionPass { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 665 | // PassClass - The type of passes tracked by this PassManager |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 666 | typedef FunctionPass PassClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 667 | |
| 668 | // SubPassClass - The types of classes that should be collated together |
| 669 | typedef BasicBlockPass SubPassClass; |
| 670 | |
| 671 | // BatcherClass - The type to use for collation of subtypes... |
| 672 | typedef PassManagerT<BasicBlock> BatcherClass; |
| 673 | |
| 674 | // ParentClass - The type of the parent PassManager... |
| 675 | typedef PassManagerT<Module> ParentClass; |
| 676 | |
| 677 | // PMType - The type of the passmanager that subclasses this class |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 678 | typedef PassManagerT<Function> PMType; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 679 | |
| 680 | // runPass - Specify how the pass should be run on the UnitType |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 681 | static bool runPass(PassClass *P, Function *F) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 682 | return P->runOnFunction(*F); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 685 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 686 | // debugging. |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 687 | const char *getPMName() const { return "Function"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 688 | virtual const char *getPassName() const { return "Function Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 689 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 690 | // Implement the FunctionPass interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 691 | virtual bool doInitialization(Module &M); |
| 692 | virtual bool runOnFunction(Function &F); |
| 693 | virtual bool doFinalization(Module &M); |
Chris Lattner | cb42906 | 2002-04-30 18:50:17 +0000 | [diff] [blame] | 694 | |
| 695 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 696 | AU.setPreservesAll(); |
| 697 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 698 | }; |
| 699 | |
| 700 | |
| 701 | |
| 702 | //===----------------------------------------------------------------------===// |
| 703 | // PassManagerTraits<Module> Specialization |
| 704 | // |
| 705 | // This is the top level PassManager implementation that holds generic passes. |
| 706 | // |
| 707 | template<> struct PassManagerTraits<Module> : public Pass { |
| 708 | // PassClass - The type of passes tracked by this PassManager |
| 709 | typedef Pass PassClass; |
| 710 | |
| 711 | // SubPassClass - The types of classes that should be collated together |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 712 | typedef FunctionPass SubPassClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 713 | |
| 714 | // BatcherClass - The type to use for collation of subtypes... |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 715 | typedef PassManagerT<Function> BatcherClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 716 | |
| 717 | // ParentClass - The type of the parent PassManager... |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 718 | typedef AnalysisResolver ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 719 | |
| 720 | // runPass - Specify how the pass should be run on the UnitType |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 721 | static bool runPass(PassClass *P, Module *M) { return P->run(*M); } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 722 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 723 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 724 | // debugging. |
| 725 | const char *getPMName() const { return "Module"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 726 | virtual const char *getPassName() const { return "Module Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 727 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 728 | // run - Implement the PassManager interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 729 | bool run(Module &M) { |
Chris Lattner | e7e0944 | 2003-08-14 05:20:28 +0000 | [diff] [blame] | 730 | return ((PassManagerT<Module>*)this)->runOnUnit(&M); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 731 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 732 | }; |
| 733 | |
| 734 | |
| 735 | |
| 736 | //===----------------------------------------------------------------------===// |
| 737 | // PassManagerTraits Method Implementations |
| 738 | // |
| 739 | |
| 740 | // PassManagerTraits<BasicBlock> Implementations |
| 741 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 742 | inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) { |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 743 | bool Changed = false; |
| 744 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 745 | ((PMType*)this)->Passes[i]->doInitialization(M); |
| 746 | return Changed; |
| 747 | } |
| 748 | |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 749 | inline bool PassManagerTraits<BasicBlock>::doInitialization(Function &F) { |
| 750 | bool Changed = false; |
| 751 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 752 | ((PMType*)this)->Passes[i]->doInitialization(F); |
| 753 | return Changed; |
| 754 | } |
| 755 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 756 | inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) { |
| 757 | return ((PMType*)this)->runOnUnit(&BB); |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Chris Lattner | bae3c67 | 2002-09-12 17:06:40 +0000 | [diff] [blame] | 760 | inline bool PassManagerTraits<BasicBlock>::doFinalization(Function &F) { |
| 761 | bool Changed = false; |
| 762 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 763 | ((PMType*)this)->Passes[i]->doFinalization(F); |
| 764 | return Changed; |
| 765 | } |
| 766 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 767 | inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) { |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 768 | bool Changed = false; |
| 769 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 770 | ((PMType*)this)->Passes[i]->doFinalization(M); |
| 771 | return Changed; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 775 | // PassManagerTraits<Function> Implementations |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 776 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 777 | inline bool PassManagerTraits<Function>::doInitialization(Module &M) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 778 | bool Changed = false; |
| 779 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 780 | ((PMType*)this)->Passes[i]->doInitialization(M); |
| 781 | return Changed; |
| 782 | } |
| 783 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 784 | inline bool PassManagerTraits<Function>::runOnFunction(Function &F) { |
| 785 | return ((PMType*)this)->runOnUnit(&F); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 786 | } |
| 787 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 788 | inline bool PassManagerTraits<Function>::doFinalization(Module &M) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 789 | bool Changed = false; |
| 790 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 791 | ((PMType*)this)->Passes[i]->doFinalization(M); |
| 792 | return Changed; |
| 793 | } |
| 794 | |
| 795 | #endif |