Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 1 | //===- PassManagerT.h - Container for Passes ---------------------*- C++ -*--=// |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 3 | // This file defines the PassManagerT class. This class is used to hold, |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 4 | // maintain, and optimize execution of Pass's. The PassManager class ensures |
| 5 | // that analysis results are available before a pass runs, and that Pass's are |
| 6 | // destroyed when the PassManager is destroyed. |
| 7 | // |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 8 | // The PassManagerT template is instantiated three times to do its job. The |
| 9 | // public PassManager class is a Pimpl around the PassManagerT<Module> interface |
| 10 | // to avoid having all of the PassManager clients being exposed to the |
| 11 | // implementation details herein. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Lattner | d0e9bd1 | 2002-04-28 20:42:50 +0000 | [diff] [blame] | 15 | #ifndef LLVM_PASSMANAGER_T_H |
| 16 | #define LLVM_PASSMANAGER_T_H |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 17 | |
| 18 | #include "llvm/Pass.h" |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 19 | #include "Support/CommandLine.h" |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Chris Lattner | 395c27a | 2002-07-31 18:04:17 +0000 | [diff] [blame] | 21 | #include <iostream> |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 22 | class Annotable; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 25 | // Pass debugging information. Often it is useful to find out what pass is |
| 26 | // running when a crash occurs in a utility. When this library is compiled with |
| 27 | // debugging on, a command line option (--debug-pass) is enabled that causes the |
| 28 | // pass name to be printed before it executes. |
| 29 | // |
| 30 | |
| 31 | // Different debug levels that can be enabled... |
| 32 | enum PassDebugLevel { |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 33 | None, Arguments, Structure, Executions, Details |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static cl::opt<enum PassDebugLevel> |
| 37 | PassDebugging("debug-pass", cl::Hidden, |
| 38 | cl::desc("Print PassManager debugging information"), |
| 39 | cl::values( |
| 40 | clEnumVal(None , "disable debug output"), |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 41 | clEnumVal(Arguments , "print pass arguments to pass to 'opt'"), |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 42 | clEnumVal(Structure , "print pass structure before run()"), |
| 43 | clEnumVal(Executions, "print pass name before it is executed"), |
| 44 | clEnumVal(Details , "print pass details when it is executed"), |
| 45 | 0)); |
| 46 | |
| 47 | //===----------------------------------------------------------------------===// |
Chris Lattner | 57d2ba3 | 2002-04-04 19:35:24 +0000 | [diff] [blame] | 48 | // PMDebug class - a set of debugging functions, that are not to be |
| 49 | // instantiated by the template. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 50 | // |
| 51 | struct PMDebug { |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 52 | static void PerformPassStartupStuff(Pass *P) { |
| 53 | // If debugging is enabled, print out argument information... |
| 54 | if (PassDebugging >= Arguments) { |
| 55 | std::cerr << "Pass Arguments: "; |
| 56 | PrintArgumentInformation(P); |
| 57 | std::cerr << "\n"; |
| 58 | |
| 59 | // Print the pass execution structure |
| 60 | if (PassDebugging >= Structure) |
| 61 | P->dumpPassStructure(); |
| 62 | } |
Chris Lattner | 198cf42 | 2002-07-30 16:27:02 +0000 | [diff] [blame] | 63 | } |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 64 | |
| 65 | static void PrintArgumentInformation(const Pass *P); |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 66 | static void PrintPassInformation(unsigned,const char*,Pass *, Annotable *); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 67 | static void PrintAnalysisSetInfo(unsigned,const char*,Pass *P, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 68 | const std::vector<AnalysisID> &); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 72 | //===----------------------------------------------------------------------===// |
| 73 | // TimingInfo Class - This class is used to calculate information about the |
| 74 | // amount of time each pass takes to execute. This only happens when |
| 75 | // -time-passes is enabled on the command line. |
| 76 | // |
| 77 | class TimingInfo { |
| 78 | std::map<Pass*, double> TimingData; |
| 79 | TimingInfo() {} // Private ctor, must use create member |
| 80 | public: |
| 81 | // Create method. If Timing is enabled, this creates and returns a new timing |
| 82 | // object, otherwise it returns null. |
| 83 | // |
| 84 | static TimingInfo *create(); |
| 85 | |
| 86 | // TimingDtor - Print out information about timing information |
| 87 | ~TimingInfo(); |
| 88 | |
| 89 | void passStarted(Pass *P); |
| 90 | void passEnded(Pass *P); |
| 91 | }; |
| 92 | |
| 93 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 94 | |
| 95 | //===----------------------------------------------------------------------===// |
| 96 | // Declare the PassManagerTraits which will be specialized... |
| 97 | // |
| 98 | template<class UnitType> class PassManagerTraits; // Do not define. |
| 99 | |
| 100 | |
| 101 | //===----------------------------------------------------------------------===// |
| 102 | // PassManagerT - Container object for passes. The PassManagerT destructor |
| 103 | // deletes all passes contained inside of the PassManagerT, so you shouldn't |
| 104 | // delete passes manually, and all passes should be dynamically allocated. |
| 105 | // |
| 106 | template<typename UnitType> |
| 107 | class PassManagerT : public PassManagerTraits<UnitType>,public AnalysisResolver{ |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 108 | typedef PassManagerTraits<UnitType> Traits; |
| 109 | typedef typename Traits::PassClass PassClass; |
| 110 | typedef typename Traits::SubPassClass SubPassClass; |
| 111 | typedef typename Traits::BatcherClass BatcherClass; |
| 112 | typedef typename Traits::ParentClass ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 113 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 114 | friend typename Traits::PassClass; |
| 115 | friend typename Traits::SubPassClass; |
| 116 | friend class Traits; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 117 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 118 | std::vector<PassClass*> Passes; // List of passes to run |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 119 | |
| 120 | // The parent of this pass manager... |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 121 | ParentClass * const Parent; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 122 | |
| 123 | // The current batcher if one is in use, or null |
| 124 | BatcherClass *Batcher; |
| 125 | |
| 126 | // CurrentAnalyses - As the passes are being run, this map contains the |
| 127 | // analyses that are available to the current pass for use. This is accessed |
| 128 | // through the getAnalysis() function in this class and in Pass. |
| 129 | // |
| 130 | std::map<AnalysisID, Pass*> CurrentAnalyses; |
| 131 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 132 | // LastUseOf - This map keeps track of the last usage in our pipeline of a |
| 133 | // particular pass. When executing passes, the memory for .first is free'd |
| 134 | // after .second is run. |
| 135 | // |
| 136 | std::map<Pass*, Pass*> LastUseOf; |
| 137 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 138 | public: |
| 139 | PassManagerT(ParentClass *Par = 0) : Parent(Par), Batcher(0) {} |
| 140 | ~PassManagerT() { |
| 141 | // Delete all of the contained passes... |
Chris Lattner | a82ee2d | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 142 | for (typename std::vector<PassClass*>::iterator |
| 143 | I = Passes.begin(), E = Passes.end(); I != E; ++I) |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 144 | delete *I; |
| 145 | } |
| 146 | |
| 147 | // run - Run all of the queued passes on the specified module in an optimal |
| 148 | // way. |
| 149 | virtual bool runOnUnit(UnitType *M) { |
| 150 | bool MadeChanges = false; |
| 151 | closeBatcher(); |
| 152 | CurrentAnalyses.clear(); |
| 153 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 154 | // LastUserOf - This contains the inverted LastUseOfMap... |
| 155 | std::map<Pass *, std::vector<Pass*> > LastUserOf; |
| 156 | for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(), |
| 157 | E = LastUseOf.end(); I != E; ++I) |
| 158 | LastUserOf[I->second].push_back(I->first); |
| 159 | |
| 160 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 161 | // Output debug information... |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 162 | if (Parent == 0) PMDebug::PerformPassStartupStuff(this); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 163 | |
| 164 | // Run all of the passes |
| 165 | for (unsigned i = 0, e = Passes.size(); i < e; ++i) { |
| 166 | PassClass *P = Passes[i]; |
| 167 | |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 168 | PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, |
| 169 | (Annotable*)M); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 170 | |
| 171 | // Get information about what analyses the pass uses... |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 172 | AnalysisUsage AnUsage; |
| 173 | P->getAnalysisUsage(AnUsage); |
| 174 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P, |
| 175 | AnUsage.getRequiredSet()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 176 | |
| 177 | #ifndef NDEBUG |
| 178 | // All Required analyses should be available to the pass as it runs! |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 179 | for (std::vector<AnalysisID>::const_iterator |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 180 | I = AnUsage.getRequiredSet().begin(), |
| 181 | E = AnUsage.getRequiredSet().end(); I != E; ++I) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 182 | assert(getAnalysisOrNullUp(*I) && "Analysis used but not available!"); |
| 183 | } |
| 184 | #endif |
| 185 | |
| 186 | // Run the sub pass! |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 187 | startPass(P); |
| 188 | bool Changed = runPass(P, M); |
| 189 | endPass(P); |
Chris Lattner | f2f31bd | 2002-02-01 04:53:36 +0000 | [diff] [blame] | 190 | MadeChanges |= Changed; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 191 | |
Chris Lattner | f2f31bd | 2002-02-01 04:53:36 +0000 | [diff] [blame] | 192 | if (Changed) |
| 193 | PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 194 | (Annotable*)M); |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 195 | PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P, |
| 196 | AnUsage.getPreservedSet()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 197 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 198 | |
| 199 | // Erase all analyses not in the preserved set... |
| 200 | if (!AnUsage.preservesAll()) { |
| 201 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 202 | for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(), |
| 203 | E = CurrentAnalyses.end(); I != E; ) |
| 204 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) != |
| 205 | PreservedSet.end()) |
| 206 | ++I; // This analysis is preserved, leave it in the available set... |
| 207 | else { |
| 208 | #if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER |
| 209 | I = CurrentAnalyses.erase(I); // Analysis not preserved! |
| 210 | #else |
| 211 | // GCC 2.95.3 STL doesn't have correct erase member! |
| 212 | CurrentAnalyses.erase(I); |
| 213 | I = CurrentAnalyses.begin(); |
| 214 | #endif |
| 215 | } |
| 216 | } |
| 217 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 218 | // Add the current pass to the set of passes that have been run, and are |
| 219 | // thus available to users. |
| 220 | // |
| 221 | if (const PassInfo *PI = P->getPassInfo()) |
| 222 | CurrentAnalyses[PI] = P; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 223 | |
| 224 | // Free memory for any passes that we are the last use of... |
| 225 | std::vector<Pass*> &DeadPass = LastUserOf[P]; |
| 226 | for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end(); |
| 227 | I != E; ++I) { |
| 228 | PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I, |
Chris Lattner | a454b5b | 2002-04-28 05:14:06 +0000 | [diff] [blame] | 229 | (Annotable*)M); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 230 | (*I)->releaseMemory(); |
| 231 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 232 | } |
| 233 | return MadeChanges; |
| 234 | } |
| 235 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 236 | // dumpPassStructure - Implement the -debug-passes=PassStructure option |
| 237 | virtual void dumpPassStructure(unsigned Offset = 0) { |
| 238 | std::cerr << std::string(Offset*2, ' ') << Traits::getPMName() |
| 239 | << " Pass Manager\n"; |
Chris Lattner | a82ee2d | 2002-07-24 22:08:53 +0000 | [diff] [blame] | 240 | for (typename std::vector<PassClass*>::iterator |
| 241 | I = Passes.begin(), E = Passes.end(); I != E; ++I) { |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 242 | PassClass *P = *I; |
| 243 | P->dumpPassStructure(Offset+1); |
| 244 | |
| 245 | // Loop through and see which classes are destroyed after this one... |
| 246 | for (std::map<Pass*, Pass*>::iterator I = LastUseOf.begin(), |
| 247 | E = LastUseOf.end(); I != E; ++I) { |
| 248 | if (P == I->second) { |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 249 | std::cerr << "--" << std::string(Offset*2, ' '); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 250 | I->first->dumpPassStructure(0); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 255 | |
| 256 | Pass *getAnalysisOrNullDown(AnalysisID ID) const { |
| 257 | std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID); |
| 258 | if (I == CurrentAnalyses.end()) { |
| 259 | if (Batcher) |
| 260 | return ((AnalysisResolver*)Batcher)->getAnalysisOrNullDown(ID); |
| 261 | return 0; |
| 262 | } |
| 263 | return I->second; |
| 264 | } |
| 265 | |
| 266 | Pass *getAnalysisOrNullUp(AnalysisID ID) const { |
| 267 | std::map<AnalysisID, Pass*>::const_iterator I = CurrentAnalyses.find(ID); |
| 268 | if (I == CurrentAnalyses.end()) { |
| 269 | if (Parent) |
| 270 | return Parent->getAnalysisOrNullUp(ID); |
| 271 | return 0; |
| 272 | } |
| 273 | return I->second; |
| 274 | } |
| 275 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 276 | // {start/end}Pass - Called when a pass is started, it just propogates |
| 277 | // information up to the top level PassManagerT object to tell it that a pass |
| 278 | // has started or ended. This is used to gather timing information about |
| 279 | // passes. |
| 280 | // |
| 281 | void startPass(Pass *P) { |
| 282 | if (Parent) Parent->startPass(P); |
| 283 | else PassStarted(P); |
| 284 | } |
| 285 | void endPass(Pass *P) { |
| 286 | if (Parent) Parent->endPass(P); |
| 287 | else PassEnded(P); |
| 288 | } |
| 289 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 290 | // markPassUsed - Inform higher level pass managers (and ourselves) |
| 291 | // that these analyses are being used by this pass. This is used to |
| 292 | // make sure that analyses are not free'd before we have to use |
| 293 | // them... |
| 294 | // |
| 295 | void markPassUsed(AnalysisID P, Pass *User) { |
| 296 | std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.find(P); |
| 297 | if (I != CurrentAnalyses.end()) { |
| 298 | LastUseOf[I->second] = User; // Local pass, extend the lifetime |
| 299 | } else { |
| 300 | // Pass not in current available set, must be a higher level pass |
| 301 | // available to us, propogate to parent pass manager... We tell the |
| 302 | // parent that we (the passmanager) are using the analysis so that it |
| 303 | // frees the analysis AFTER this pass manager runs. |
| 304 | // |
Chris Lattner | de421a7 | 2002-03-17 21:16:01 +0000 | [diff] [blame] | 305 | assert(Parent != 0 && "Pass available but not found! " |
| 306 | "Did your analysis pass 'Provide' itself?"); |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 307 | Parent->markPassUsed(P, this); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Return the number of parent PassManagers that exist |
| 312 | virtual unsigned getDepth() const { |
| 313 | if (Parent == 0) return 0; |
| 314 | return 1 + Parent->getDepth(); |
| 315 | } |
| 316 | |
Chris Lattner | 1e4867f | 2002-07-30 19:51:02 +0000 | [diff] [blame] | 317 | virtual unsigned getNumContainedPasses() const { return Passes.size(); } |
| 318 | virtual const Pass *getContainedPass(unsigned N) const { |
| 319 | assert(N < Passes.size() && "Pass number out of range!"); |
| 320 | return Passes[N]; |
| 321 | } |
| 322 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 323 | // add - Add a pass to the queue of passes to run. This passes ownership of |
| 324 | // the Pass to the PassManager. When the PassManager is destroyed, the pass |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 325 | // will be destroyed as well, so there is no need to delete the pass. This |
| 326 | // implies that all passes MUST be new'd. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 327 | // |
| 328 | void add(PassClass *P) { |
| 329 | // Get information about what analyses the pass uses... |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 330 | AnalysisUsage AnUsage; |
| 331 | P->getAnalysisUsage(AnUsage); |
| 332 | const std::vector<AnalysisID> &Required = AnUsage.getRequiredSet(); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 333 | |
| 334 | // Loop over all of the analyses used by this pass, |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 335 | for (std::vector<AnalysisID>::const_iterator I = Required.begin(), |
| 336 | E = Required.end(); I != E; ++I) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 337 | if (getAnalysisOrNullDown(*I) == 0) |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 338 | add((PassClass*)(*I)->createPass()); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | // Tell the pass to add itself to this PassManager... the way it does so |
| 342 | // depends on the class of the pass, and is critical to laying out passes in |
| 343 | // an optimal order.. |
| 344 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 345 | P->addToPassManager(this, AnUsage); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | private: |
| 349 | |
| 350 | // addPass - These functions are used to implement the subclass specific |
| 351 | // behaviors present in PassManager. Basically the add(Pass*) method ends up |
| 352 | // reflecting its behavior into a Pass::addToPassManager call. Subclasses of |
| 353 | // Pass override it specifically so that they can reflect the type |
| 354 | // information inherent in "this" back to the PassManager. |
| 355 | // |
| 356 | // For generic Pass subclasses (which are interprocedural passes), we simply |
| 357 | // 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] | 358 | // FunctionPass's that are present. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 359 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 360 | void addPass(PassClass *P, AnalysisUsage &AnUsage) { |
| 361 | const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet(); |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 362 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 363 | // FIXME: If this pass being added isn't killed by any of the passes in the |
| 364 | // batcher class then we can reorder to pass to execute before the batcher |
| 365 | // does, which will potentially allow us to batch more passes! |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 366 | // |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 367 | //const std::vector<AnalysisID> &ProvidedSet = AnUsage.getProvidedSet(); |
| 368 | if (Batcher /*&& ProvidedSet.empty()*/) |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 369 | closeBatcher(); // This pass cannot be batched! |
| 370 | |
| 371 | // Set the Resolver instance variable in the Pass so that it knows where to |
| 372 | // find this object... |
| 373 | // |
| 374 | setAnalysisResolver(P, this); |
| 375 | Passes.push_back(P); |
| 376 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 377 | // Inform higher level pass managers (and ourselves) that these analyses are |
| 378 | // being used by this pass. This is used to make sure that analyses are not |
| 379 | // free'd before we have to use them... |
| 380 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 381 | for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(), |
| 382 | E = RequiredSet.end(); I != E; ++I) |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 383 | markPassUsed(*I, P); // Mark *I as used by P |
| 384 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 385 | // Erase all analyses not in the preserved set... |
| 386 | if (!AnUsage.preservesAll()) { |
| 387 | const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet(); |
| 388 | for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(), |
| 389 | E = CurrentAnalyses.end(); I != E; ) |
| 390 | if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) != |
| 391 | PreservedSet.end()) |
| 392 | ++I; // This analysis is preserved, leave it in the available set... |
| 393 | else { |
| 394 | #if MAP_DOESNT_HAVE_BROKEN_ERASE_MEMBER |
| 395 | I = CurrentAnalyses.erase(I); // Analysis not preserved! |
| 396 | #else |
| 397 | CurrentAnalyses.erase(I);// GCC 2.95.3 STL doesn't have correct erase! |
| 398 | I = CurrentAnalyses.begin(); |
| 399 | #endif |
| 400 | } |
| 401 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 402 | |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 403 | // Add this pass to the currently available set... |
| 404 | if (const PassInfo *PI = P->getPassInfo()) |
| 405 | CurrentAnalyses[PI] = P; |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 406 | |
| 407 | // For now assume that our results are never used... |
| 408 | LastUseOf[P] = P; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 411 | // For FunctionPass subclasses, we must be sure to batch the FunctionPass's |
| 412 | // together in a BatcherClass object so that all of the analyses are run |
| 413 | // together a function at a time. |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 414 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 415 | void addPass(SubPassClass *MP, AnalysisUsage &AnUsage) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 416 | if (Batcher == 0) // If we don't have a batcher yet, make one now. |
| 417 | Batcher = new BatcherClass(this); |
Chris Lattner | 7350317 | 2002-07-29 21:03:38 +0000 | [diff] [blame] | 418 | // The Batcher will queue the passes up |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 419 | MP->addToPassManager(Batcher, AnUsage); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // closeBatcher - Terminate the batcher that is being worked on. |
| 423 | void closeBatcher() { |
| 424 | if (Batcher) { |
| 425 | Passes.push_back(Batcher); |
| 426 | Batcher = 0; |
| 427 | } |
| 428 | } |
| 429 | }; |
| 430 | |
| 431 | |
| 432 | |
| 433 | //===----------------------------------------------------------------------===// |
| 434 | // PassManagerTraits<BasicBlock> Specialization |
| 435 | // |
| 436 | // This pass manager is used to group together all of the BasicBlockPass's |
| 437 | // into a single unit. |
| 438 | // |
| 439 | template<> struct PassManagerTraits<BasicBlock> : public BasicBlockPass { |
| 440 | // PassClass - The type of passes tracked by this PassManager |
| 441 | typedef BasicBlockPass PassClass; |
| 442 | |
| 443 | // SubPassClass - The types of classes that should be collated together |
| 444 | // This is impossible to match, so BasicBlock instantiations of PassManagerT |
| 445 | // do not collate. |
| 446 | // |
| 447 | typedef PassManagerT<Module> SubPassClass; |
| 448 | |
| 449 | // BatcherClass - The type to use for collation of subtypes... This class is |
| 450 | // never instantiated for the PassManager<BasicBlock>, but it must be an |
| 451 | // instance of PassClass to typecheck. |
| 452 | // |
| 453 | typedef PassClass BatcherClass; |
| 454 | |
| 455 | // ParentClass - The type of the parent PassManager... |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 456 | typedef PassManagerT<Function> ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 458 | // PMType - The type of the passmanager that subclasses this class |
| 459 | typedef PassManagerT<BasicBlock> PMType; |
| 460 | |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 461 | // runPass - Specify how the pass should be run on the UnitType |
| 462 | static bool runPass(PassClass *P, BasicBlock *M) { |
| 463 | // todo, init and finalize |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 464 | return P->runOnBasicBlock(*M); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 465 | } |
| 466 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 467 | // Dummy implementation of PassStarted/PassEnded |
| 468 | static void PassStarted(Pass *P) {} |
| 469 | static void PassEnded(Pass *P) {} |
| 470 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 471 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 472 | // debugging. |
| 473 | const char *getPMName() const { return "BasicBlock"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 474 | virtual const char *getPassName() const { return "BasicBlock Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 475 | |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 476 | // Implement the BasicBlockPass interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 477 | virtual bool doInitialization(Module &M); |
| 478 | virtual bool runOnBasicBlock(BasicBlock &BB); |
| 479 | virtual bool doFinalization(Module &M); |
Chris Lattner | cb42906 | 2002-04-30 18:50:17 +0000 | [diff] [blame] | 480 | |
| 481 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 482 | AU.setPreservesAll(); |
| 483 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
| 486 | |
| 487 | |
| 488 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 489 | // PassManagerTraits<Function> Specialization |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 490 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 491 | // 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] | 492 | // into a single unit. |
| 493 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 494 | template<> struct PassManagerTraits<Function> : public FunctionPass { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 495 | // PassClass - The type of passes tracked by this PassManager |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 496 | typedef FunctionPass PassClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 497 | |
| 498 | // SubPassClass - The types of classes that should be collated together |
| 499 | typedef BasicBlockPass SubPassClass; |
| 500 | |
| 501 | // BatcherClass - The type to use for collation of subtypes... |
| 502 | typedef PassManagerT<BasicBlock> BatcherClass; |
| 503 | |
| 504 | // ParentClass - The type of the parent PassManager... |
| 505 | typedef PassManagerT<Module> ParentClass; |
| 506 | |
| 507 | // PMType - The type of the passmanager that subclasses this class |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 508 | typedef PassManagerT<Function> PMType; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 509 | |
| 510 | // runPass - Specify how the pass should be run on the UnitType |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 511 | static bool runPass(PassClass *P, Function *F) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 512 | return P->runOnFunction(*F); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 515 | // Dummy implementation of PassStarted/PassEnded |
| 516 | static void PassStarted(Pass *P) {} |
| 517 | static void PassEnded(Pass *P) {} |
| 518 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 519 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 520 | // debugging. |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 521 | const char *getPMName() const { return "Function"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 522 | virtual const char *getPassName() const { return "Function Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 523 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 524 | // Implement the FunctionPass interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 525 | virtual bool doInitialization(Module &M); |
| 526 | virtual bool runOnFunction(Function &F); |
| 527 | virtual bool doFinalization(Module &M); |
Chris Lattner | cb42906 | 2002-04-30 18:50:17 +0000 | [diff] [blame] | 528 | |
| 529 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 530 | AU.setPreservesAll(); |
| 531 | } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | |
| 535 | |
| 536 | //===----------------------------------------------------------------------===// |
| 537 | // PassManagerTraits<Module> Specialization |
| 538 | // |
| 539 | // This is the top level PassManager implementation that holds generic passes. |
| 540 | // |
| 541 | template<> struct PassManagerTraits<Module> : public Pass { |
| 542 | // PassClass - The type of passes tracked by this PassManager |
| 543 | typedef Pass PassClass; |
| 544 | |
| 545 | // SubPassClass - The types of classes that should be collated together |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 546 | typedef FunctionPass SubPassClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 547 | |
| 548 | // BatcherClass - The type to use for collation of subtypes... |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 549 | typedef PassManagerT<Function> BatcherClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 550 | |
| 551 | // ParentClass - The type of the parent PassManager... |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 552 | typedef AnalysisResolver ParentClass; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 553 | |
| 554 | // runPass - Specify how the pass should be run on the UnitType |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 555 | static bool runPass(PassClass *P, Module *M) { return P->run(*M); } |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 556 | |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 557 | // getPMName() - Return the name of the unit the PassManager operates on for |
| 558 | // debugging. |
| 559 | const char *getPMName() const { return "Module"; } |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 560 | virtual const char *getPassName() const { return "Module Pass Manager"; } |
Chris Lattner | ac3e060 | 2002-01-31 18:32:27 +0000 | [diff] [blame] | 561 | |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 562 | // TimingInformation - This data member maintains timing information for each |
| 563 | // of the passes that is executed. |
| 564 | // |
| 565 | TimingInfo *TimeInfo; |
| 566 | |
| 567 | // PassStarted/Ended - This callback is notified any time a pass is started |
| 568 | // or stops. This is used to collect timing information about the different |
| 569 | // passes being executed. |
| 570 | // |
| 571 | void PassStarted(Pass *P) { |
| 572 | if (TimeInfo) TimeInfo->passStarted(P); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 573 | } |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 574 | void PassEnded(Pass *P) { |
| 575 | if (TimeInfo) TimeInfo->passEnded(P); |
| 576 | } |
| 577 | |
| 578 | // run - Implement the PassManager interface... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 579 | bool run(Module &M) { |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 580 | TimeInfo = TimingInfo::create(); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 581 | bool Result = ((PassManagerT<Module>*)this)->runOnUnit(&M); |
Chris Lattner | e2eb99e | 2002-04-29 04:04:29 +0000 | [diff] [blame] | 582 | if (TimeInfo) { |
| 583 | delete TimeInfo; |
| 584 | TimeInfo = 0; |
| 585 | } |
| 586 | return Result; |
| 587 | } |
| 588 | |
| 589 | // PassManagerTraits constructor - Create a timing info object if the user |
| 590 | // specified timing info should be collected on the command line. |
| 591 | // |
| 592 | PassManagerTraits() : TimeInfo(0) {} |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 593 | }; |
| 594 | |
| 595 | |
| 596 | |
| 597 | //===----------------------------------------------------------------------===// |
| 598 | // PassManagerTraits Method Implementations |
| 599 | // |
| 600 | |
| 601 | // PassManagerTraits<BasicBlock> Implementations |
| 602 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 603 | inline bool PassManagerTraits<BasicBlock>::doInitialization(Module &M) { |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 604 | bool Changed = false; |
| 605 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 606 | ((PMType*)this)->Passes[i]->doInitialization(M); |
| 607 | return Changed; |
| 608 | } |
| 609 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 610 | inline bool PassManagerTraits<BasicBlock>::runOnBasicBlock(BasicBlock &BB) { |
| 611 | return ((PMType*)this)->runOnUnit(&BB); |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 612 | } |
| 613 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 614 | inline bool PassManagerTraits<BasicBlock>::doFinalization(Module &M) { |
Chris Lattner | 2eaac39 | 2002-01-31 00:40:44 +0000 | [diff] [blame] | 615 | bool Changed = false; |
| 616 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 617 | ((PMType*)this)->Passes[i]->doFinalization(M); |
| 618 | return Changed; |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | |
Chris Lattner | 4e8c487 | 2002-03-23 22:51:58 +0000 | [diff] [blame] | 622 | // PassManagerTraits<Function> Implementations |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 623 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 624 | inline bool PassManagerTraits<Function>::doInitialization(Module &M) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 625 | bool Changed = false; |
| 626 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 627 | ((PMType*)this)->Passes[i]->doInitialization(M); |
| 628 | return Changed; |
| 629 | } |
| 630 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 631 | inline bool PassManagerTraits<Function>::runOnFunction(Function &F) { |
| 632 | return ((PMType*)this)->runOnUnit(&F); |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 635 | inline bool PassManagerTraits<Function>::doFinalization(Module &M) { |
Chris Lattner | 67d2565 | 2002-01-30 23:20:39 +0000 | [diff] [blame] | 636 | bool Changed = false; |
| 637 | for (unsigned i = 0, e = ((PMType*)this)->Passes.size(); i != e; ++i) |
| 638 | ((PMType*)this)->Passes[i]->doFinalization(M); |
| 639 | return Changed; |
| 640 | } |
| 641 | |
| 642 | #endif |