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