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