+ for (unsigned i = 0, e = Passes.size(); i < e; ++i) {
+ PassClass *P = Passes[i];
+
+ PMDebug::PrintPassInformation(getDepth(), "Executing Pass", P, M);
+
+ // Get information about what analyses the pass uses...
+ AnalysisUsage AnUsage;
+ P->getAnalysisUsage(AnUsage);
+ PMDebug::PrintAnalysisSetInfo(getDepth(), "Required", P,
+ AnUsage.getRequiredSet());
+
+ initialiseAnalysisImpl(P, AnUsage);
+
+ // Run the sub pass!
+ if (TheTimeInfo) TheTimeInfo->passStarted(P);
+ bool Changed = runPass(P, M);
+ if (TheTimeInfo) TheTimeInfo->passEnded(P);
+ MadeChanges |= Changed;
+
+ // Check for memory leaks by the pass...
+ LeakDetector::checkForGarbage(std::string("after running pass '") +
+ P->getPassName() + "'");
+
+ if (Changed)
+ PMDebug::PrintPassInformation(getDepth()+1, "Made Modification", P, M);
+ PMDebug::PrintAnalysisSetInfo(getDepth(), "Preserved", P,
+ AnUsage.getPreservedSet());
+
+ // Erase all analyses not in the preserved set
+ removeNonPreservedAnalyses(AnUsage);
+
+ makeCurrentlyAvailable(P);
+
+ // free memory and remove dead passes from the CurrentAnalyses list...
+ removeDeadPasses(P, M, LastUserOf);
+ }
+
+ return MadeChanges;
+ }
+
+ // All Required analyses should be available to the pass as it runs! Here
+ // we fill in the AnalysisImpls member of the pass so that it can
+ // successfully use the getAnalysis() method to retrieve the
+ // implementations it needs.
+ //
+ inline void initialiseAnalysisImpl(PassClass *P, AnalysisUsage &AnUsage) {
+ P->AnalysisImpls.clear();
+ P->AnalysisImpls.reserve(AnUsage.getRequiredSet().size());
+
+ for (std::vector<const PassInfo *>::const_iterator
+ I = AnUsage.getRequiredSet().begin(),
+ E = AnUsage.getRequiredSet().end(); I != E; ++I) {
+ Pass *Impl = getAnalysisOrNullUp(*I);
+ if (Impl == 0) {
+ std::cerr << "Analysis '" << (*I)->getPassName()
+ << "' used but not available!";
+ assert(0 && "Analysis used but not available!");
+ } else if (PassDebugging == Details) {
+ if ((*I)->getPassName() != std::string(Impl->getPassName()))
+ std::cerr << " Interface '" << (*I)->getPassName()
+ << "' implemented by '" << Impl->getPassName() << "'\n";
+ }
+
+ P->AnalysisImpls.push_back(std::make_pair(*I, Impl));
+ }
+ }
+
+ inline void removeNonPreservedAnalyses(AnalysisUsage &AnUsage) {
+ if (!AnUsage.getPreservesAll()) {
+ const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
+ for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin(),
+ E = CurrentAnalyses.end(); I != E; )
+ if (std::find(PreservedSet.begin(), PreservedSet.end(), I->first) !=
+ PreservedSet.end())
+ ++I; // This analysis is preserved, leave it in the available set...
+ else {
+ if (!dynamic_cast<ImmutablePass*>(I->second)) {
+ std::map<AnalysisID, Pass*>::iterator J = I++;
+ CurrentAnalyses.erase(J); // Analysis not preserved!
+ } else {
+ ++I;
+ }
+ }
+ }
+ }
+
+ inline void removeDeadPasses(Pass* P, UnitType *M,
+ std::map<Pass *, std::vector<Pass*> > &LastUserOf) {
+ std::vector<Pass*> &DeadPass = LastUserOf[P];
+ for (std::vector<Pass*>::iterator I = DeadPass.begin(),E = DeadPass.end();
+ I != E; ++I) {
+ PMDebug::PrintPassInformation(getDepth()+1, "Freeing Pass", *I, M);
+ (*I)->releaseMemory();
+ }
+
+ for (std::map<AnalysisID, Pass*>::iterator I = CurrentAnalyses.begin();
+ I != CurrentAnalyses.end(); ) {
+ std::vector<Pass*>::iterator DPI = std::find(DeadPass.begin(),
+ DeadPass.end(), I->second);
+ if (DPI != DeadPass.end()) { // This pass is dead now... remove it
+ std::map<AnalysisID, Pass*>::iterator IDead = I++;
+ CurrentAnalyses.erase(IDead);
+ } else {
+ ++I; // Move on to the next element...
+ }
+ }
+ }
+
+ inline void makeCurrentlyAvailable(Pass* P) {
+ if (const PassInfo *PI = P->getPassInfo()) {
+ CurrentAnalyses[PI] = P;