Use DenseMap to keep track of last users.
[oota-llvm.git] / lib / VMCore / PassManager.cpp
index f98f2e16572000a324587c6dc4a6967cfd14f32a..ed0dde03a187ceacde5c0f4c56d5cab07b42f84b 100644 (file)
@@ -404,9 +404,11 @@ void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
 
     // If AP is the last user of other passes then make P last user of
     // such passes.
-    for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
+    for (DenseMap<Pass *, Pass *>::iterator LUI = LastUser.begin(),
            LUE = LastUser.end(); LUI != LUE; ++LUI) {
       if (LUI->second == AP)
+        // DenseMap iterator is not invalidated here because
+        // this is just updating exisitng entry.
         LastUser[LUI->first] = P;
     }
   }
@@ -414,11 +416,31 @@ void PMTopLevelManager::setLastUser(SmallVector<Pass *, 12> &AnalysisPasses,
 
 /// Collect passes whose last user is P
 void PMTopLevelManager::collectLastUses(SmallVector<Pass *, 12> &LastUses,
-                                            Pass *P) {
-   for (std::map<Pass *, Pass *>::iterator LUI = LastUser.begin(),
-          LUE = LastUser.end(); LUI != LUE; ++LUI)
-      if (LUI->second == P)
-        LastUses.push_back(LUI->first);
+                                        Pass *P) {
+  DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator DMI = 
+    InversedLastUser.find(P);
+  if (DMI == InversedLastUser.end())
+    return;
+
+  SmallPtrSet<Pass *, 8> &LU = DMI->second;
+  for (SmallPtrSet<Pass *, 8>::iterator I = LU.begin(),
+         E = LU.end(); I != E; ++I) {
+    LastUses.push_back(*I);
+  }
+
+}
+
+AnalysisUsage *PMTopLevelManager::findAnalysisUsage(Pass *P) {
+  AnalysisUsage *AnUsage = NULL;
+  DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.find(P);
+  if (DMI != AnUsageMap.end()) 
+    AnUsage = DMI->second;
+  else {
+    AnUsage = new AnalysisUsage();
+    P->getAnalysisUsage(*AnUsage);
+    AnUsageMap[P] = AnUsage;
+  }
+  return AnUsage;
 }
 
 /// Schedule pass P for execution. Make sure that passes required by
@@ -439,10 +461,10 @@ void PMTopLevelManager::schedulePass(Pass *P) {
       P->getPassInfo()->isAnalysis() && findAnalysisPass(P->getPassInfo()))
     return;
 
-  AnalysisUsage AnUsage;
-  P->getAnalysisUsage(AnUsage);
-  const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
-  for (std::vector<AnalysisID>::const_iterator I = RequiredSet.begin(),
+  AnalysisUsage *AnUsage = findAnalysisUsage(P);
+
+  const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
+  for (AnalysisUsage::VectorType::const_iterator I = RequiredSet.begin(),
          E = RequiredSet.end(); I != E; ++I) {
 
     Pass *AnalysisPass = findAnalysisPass(*I);
@@ -544,6 +566,19 @@ void PMTopLevelManager::initializeAllAnalysisInfo() {
   for (std::vector<PMDataManager *>::iterator I = IndirectPassManagers.begin(),
          E = IndirectPassManagers.end(); I != E; ++I)
     (*I)->initializeAnalysisInfo();
+
+  for(DenseMap<Pass *, Pass *>::iterator DMI = LastUser.begin(),
+        DME = LastUser.end(); DMI != DME; ++DMI) {
+    DenseMap<Pass *, SmallPtrSet<Pass *, 8> >::iterator InvDMI = 
+      InversedLastUser.find(DMI->second);
+    if (InvDMI != InversedLastUser.end()) {
+      SmallPtrSet<Pass *, 8> &L = InvDMI->second;
+      L.insert(DMI->first);
+    } else {
+      SmallPtrSet<Pass *, 8> L; L.insert(DMI->first);
+      InversedLastUser[DMI->second] = L;
+    }
+  }
 }
 
 /// Destructor
@@ -555,6 +590,13 @@ PMTopLevelManager::~PMTopLevelManager() {
   for (std::vector<ImmutablePass *>::iterator
          I = ImmutablePasses.begin(), E = ImmutablePasses.end(); I != E; ++I)
     delete *I;
+
+  for (DenseMap<Pass *, AnalysisUsage *>::iterator DMI = AnUsageMap.begin(),
+         DME = AnUsageMap.end(); DMI != DME; ++DMI) {
+    AnalysisUsage *AU = DMI->second;
+    delete AU;
+  }
+    
 }
 
 //===----------------------------------------------------------------------===//
@@ -578,13 +620,12 @@ void PMDataManager::recordAvailableAnalysis(Pass *P) {
 // passes managed by this manager
 bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
 
-  AnalysisUsage AnUsage;
-  P->getAnalysisUsage(AnUsage);
+  AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
   
-  if (AnUsage.getPreservesAll())
+  if (AnUsage->getPreservesAll())
     return true;
   
-  const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
+  const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
   for (std::vector<Pass *>::iterator I = HigherLevelAnalysis.begin(),
          E = HigherLevelAnalysis.end(); I  != E; ++I) {
     Pass *P1 = *I;
@@ -598,18 +639,20 @@ bool PMDataManager::preserveHigherLevelAnalysis(Pass *P) {
   return true;
 }
 
-/// verifyPreservedAnalysis -- Verify analysis presreved by pass P.
+/// verifyPreservedAnalysis -- Verify analysis preserved by pass P.
 void PMDataManager::verifyPreservedAnalysis(Pass *P) {
-  AnalysisUsage AnUsage;
-  P->getAnalysisUsage(AnUsage);
-  const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
+  // Don't do this unless assertions are enabled.
+#ifdef NDEBUG
+  return;
+#endif
+  AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
+  const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
 
   // Verify preserved analysis
-  for (std::vector<AnalysisID>::const_iterator I = PreservedSet.begin(),
+  for (AnalysisUsage::VectorType::const_iterator I = PreservedSet.begin(),
          E = PreservedSet.end(); I != E; ++I) {
     AnalysisID AID = *I;
-    Pass *AP = findAnalysisPass(AID, true);
-    if (AP)
+    if (Pass *AP = findAnalysisPass(AID, true))
       AP->verifyAnalysis();
   }
 }
@@ -628,10 +671,10 @@ void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
   OtherDT.getBase().recalculate(F);
   if (DT->compare(OtherDT)) {
     cerr << "Dominator Information for " << F.getNameStart() << "\n";
-    cerr << "Pass " << P.getPassName() << "\n";
+    cerr << "Pass '" << P.getPassName() << "'\n";
     cerr << "----- Valid -----\n";
     OtherDT.dump();
-    cerr << "----- InValid -----\n";
+    cerr << "----- Invalid -----\n";
     DT->dump();
     assert (0 && "Invalid dominator info");
   }
@@ -645,23 +688,22 @@ void PMDataManager::verifyDomInfo(Pass &P, Function &F) {
   OtherDF.calculate(*DT, DT->getNode(DTRoots[0]));
   if (DF->compare(OtherDF)) {
     cerr << "Dominator Information for " << F.getNameStart() << "\n";
-    cerr << "Pass " << P.getPassName() << "\n";
+    cerr << "Pass '" << P.getPassName() << "'\n";
     cerr << "----- Valid -----\n";
     OtherDF.dump();
-    cerr << "----- InValid -----\n";
+    cerr << "----- Invalid -----\n";
     DF->dump();
     assert (0 && "Invalid dominator info");
   }
 }
 
-/// Remove Analyss not preserved by Pass P
+/// Remove Analysis not preserved by Pass P
 void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
-  AnalysisUsage AnUsage;
-  P->getAnalysisUsage(AnUsage);
-  if (AnUsage.getPreservesAll())
+  AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
+  if (AnUsage->getPreservesAll())
     return;
 
-  const std::vector<AnalysisID> &PreservedSet = AnUsage.getPreservedSet();
+  const AnalysisUsage::VectorType &PreservedSet = AnUsage->getPreservedSet();
   for (std::map<AnalysisID, Pass*>::iterator I = AvailableAnalysis.begin(),
          E = AvailableAnalysis.end(); I != E; ) {
     std::map<AnalysisID, Pass*>::iterator Info = I++;
@@ -672,8 +714,8 @@ void PMDataManager::removeNotPreservedAnalysis(Pass *P) {
       AvailableAnalysis.erase(Info);
       if (PassDebugging >= Details) {
         Pass *S = Info->second;
-        cerr << " -- " <<  P->getPassName() << " is not preserving ";
-        cerr << S->getPassName() << "\n";
+        cerr << " -- '" <<  P->getPassName() << "' is not preserving '";
+        cerr << S->getPassName() << "'\n";
       }
     }
   }
@@ -711,8 +753,8 @@ void PMDataManager::removeDeadPasses(Pass *P, const char *Msg,
   TPM->collectLastUses(DeadPasses, P);
 
   if (PassDebugging >= Details && !DeadPasses.empty()) {
-    cerr << " -*- " <<  P->getPassName();
-    cerr << " is the last user of following pass instances.";
+    cerr << " -*- '" <<  P->getPassName();
+    cerr << "' is the last user of following pass instances.";
     cerr << " Free these instances\n";
   }
 
@@ -817,10 +859,9 @@ void PMDataManager::add(Pass *P,
 void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
                                        SmallVector<AnalysisID, 8> &RP_NotAvail,
                                             Pass *P) {
-  AnalysisUsage AnUsage;
-  P->getAnalysisUsage(AnUsage);
-  const std::vector<AnalysisID> &RequiredSet = AnUsage.getRequiredSet();
-  for (std::vector<AnalysisID>::const_iterator 
+  AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
+  const AnalysisUsage::VectorType &RequiredSet = AnUsage->getRequiredSet();
+  for (AnalysisUsage::VectorType::const_iterator 
          I = RequiredSet.begin(), E = RequiredSet.end();
        I != E; ++I) {
     AnalysisID AID = *I;
@@ -830,8 +871,8 @@ void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
       RP_NotAvail.push_back(AID);
   }
 
-  const std::vector<AnalysisID> &IDs = AnUsage.getRequiredTransitiveSet();
-  for (std::vector<AnalysisID>::const_iterator I = IDs.begin(),
+  const AnalysisUsage::VectorType &IDs = AnUsage->getRequiredTransitiveSet();
+  for (AnalysisUsage::VectorType::const_iterator I = IDs.begin(),
          E = IDs.end(); I != E; ++I) {
     AnalysisID AID = *I;
     if (Pass *AnalysisPass = findAnalysisPass(*I, true))
@@ -847,12 +888,11 @@ void PMDataManager::collectRequiredAnalysis(SmallVector<Pass *, 8>&RP,
 // implementations it needs.
 //
 void PMDataManager::initializeAnalysisImpl(Pass *P) {
-  AnalysisUsage AnUsage;
-  P->getAnalysisUsage(AnUsage);
-  for (std::vector<const PassInfo *>::const_iterator
-         I = AnUsage.getRequiredSet().begin(),
-         E = AnUsage.getRequiredSet().end(); I != E; ++I) {
+  AnalysisUsage *AnUsage = TPM->findAnalysisUsage(P);
+
+  for (AnalysisUsage::VectorType::const_iterator
+         I = AnUsage->getRequiredSet().begin(),
+         E = AnUsage->getRequiredSet().end(); I != E; ++I) {
     Pass *Impl = findAnalysisPass(*I, true);
     if (Impl == 0)
       // This may be analysis pass that is initialized on the fly.
@@ -951,17 +991,38 @@ void PMDataManager::dumpPassInfo(Pass *P, enum PassDebuggingString S1,
   }
 }
 
-void PMDataManager::dumpAnalysisSetInfo(const char *Msg, Pass *P,
-                                        const std::vector<AnalysisID> &Set) 
+void PMDataManager::dumpRequiredSet(const Pass *P)
   const {
-  if (PassDebugging >= Details && !Set.empty()) {
-    cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
-      for (unsigned i = 0; i != Set.size(); ++i) {
-        if (i) cerr << ",";
-        cerr << " " << Set[i]->getPassName();
-      }
-      cerr << "\n";
-  }
+  if (PassDebugging < Details)
+    return;
+    
+  AnalysisUsage analysisUsage;
+  P->getAnalysisUsage(analysisUsage);
+  dumpAnalysisUsage("Required", P, analysisUsage.getRequiredSet());
+}
+
+void PMDataManager::dumpPreservedSet(const Pass *P)
+  const {
+  if (PassDebugging < Details)
+    return;
+    
+  AnalysisUsage analysisUsage;
+  P->getAnalysisUsage(analysisUsage);
+  dumpAnalysisUsage("Preserved", P, analysisUsage.getPreservedSet());
+}
+
+void PMDataManager::dumpAnalysisUsage(const char *Msg, const Pass *P,
+                                        const AnalysisUsage::VectorType &Set)
+  const {
+  assert(PassDebugging >= Details);
+  if (Set.empty())
+    return;
+  cerr << (void*)P << std::string(getDepth()*2+3, ' ') << Msg << " Analyses:";
+    for (unsigned i = 0; i != Set.size(); ++i) {
+      if (i) cerr << ",";
+      cerr << " " << Set[i]->getPassName();
+    }
+    cerr << "\n";
 }
 
 /// Add RequiredPass into list of lower level passes required by pass P.
@@ -984,8 +1045,8 @@ void PMDataManager::addLowerLevelRequiredPass(Pass *P, Pass *RequiredPass) {
   // checks whether any lower level manager will be able to provide this 
   // analysis info on demand or not.
 #ifndef NDEBUG
-  cerr << "Unable to schedule " << RequiredPass->getPassName();
-  cerr << " required by " << P->getPassName() << "\n";
+  cerr << "Unable to schedule '" << RequiredPass->getPassName();
+  cerr << "' required by '" << P->getPassName() << "'\n";
 #endif
   assert (0 && "Unable to schedule pass");
 }
@@ -1028,11 +1089,9 @@ BBPassManager::runOnFunction(Function &F) {
   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
     for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
       BasicBlockPass *BP = getContainedPass(Index);
-      AnalysisUsage AnUsage;
-      BP->getAnalysisUsage(AnUsage);
 
       dumpPassInfo(BP, EXECUTION_MSG, ON_BASICBLOCK_MSG, I->getNameStart());
-      dumpAnalysisSetInfo("Required", BP, AnUsage.getRequiredSet());
+      dumpRequiredSet(BP);
 
       initializeAnalysisImpl(BP);
 
@@ -1043,7 +1102,7 @@ BBPassManager::runOnFunction(Function &F) {
       if (Changed) 
         dumpPassInfo(BP, MODIFICATION_MSG, ON_BASICBLOCK_MSG,
                      I->getNameStart());
-      dumpAnalysisSetInfo("Preserved", BP, AnUsage.getPreservedSet());
+      dumpPreservedSet(BP);
 
       verifyPreservedAnalysis(BP);
       removeNotPreservedAnalysis(BP);
@@ -1229,11 +1288,8 @@ bool FPPassManager::runOnFunction(Function &F) {
   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
     FunctionPass *FP = getContainedPass(Index);
 
-    AnalysisUsage AnUsage;
-    FP->getAnalysisUsage(AnUsage);
-
     dumpPassInfo(FP, EXECUTION_MSG, ON_FUNCTION_MSG, F.getNameStart());
-    dumpAnalysisSetInfo("Required", FP, AnUsage.getRequiredSet());
+    dumpRequiredSet(FP);
 
     initializeAnalysisImpl(FP);
 
@@ -1243,14 +1299,14 @@ bool FPPassManager::runOnFunction(Function &F) {
 
     if (Changed) 
       dumpPassInfo(FP, MODIFICATION_MSG, ON_FUNCTION_MSG, F.getNameStart());
-    dumpAnalysisSetInfo("Preserved", FP, AnUsage.getPreservedSet());
+    dumpPreservedSet(FP);
 
     verifyPreservedAnalysis(FP);
     removeNotPreservedAnalysis(FP);
     recordAvailableAnalysis(FP);
     removeDeadPasses(FP, F.getNameStart(), ON_FUNCTION_MSG);
 
-    // Verify dominator information if it is available and preserved.
+    // If dominator information is available then verify the info if requested.
     verifyDomInfo(*FP, F);
   }
   return Changed;
@@ -1301,12 +1357,9 @@ MPPassManager::runOnModule(Module &M) {
   for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) {
     ModulePass *MP = getContainedPass(Index);
 
-    AnalysisUsage AnUsage;
-    MP->getAnalysisUsage(AnUsage);
-
     dumpPassInfo(MP, EXECUTION_MSG, ON_MODULE_MSG,
                  M.getModuleIdentifier().c_str());
-    dumpAnalysisSetInfo("Required", MP, AnUsage.getRequiredSet());
+    dumpRequiredSet(MP);
 
     initializeAnalysisImpl(MP);
 
@@ -1317,8 +1370,8 @@ MPPassManager::runOnModule(Module &M) {
     if (Changed) 
       dumpPassInfo(MP, MODIFICATION_MSG, ON_MODULE_MSG,
                    M.getModuleIdentifier().c_str());
-    dumpAnalysisSetInfo("Preserved", MP, AnUsage.getPreservedSet());
-      
+    dumpPreservedSet(MP);
+    
     verifyPreservedAnalysis(MP);
     removeNotPreservedAnalysis(MP);
     recordAvailableAnalysis(MP);