Revert r70645 for now; it's causing a variety of regressions.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@70661 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Analysis/ScalarEvolution.cpp b/lib/Analysis/ScalarEvolution.cpp
index 48a2f52..a6d3954 100644
--- a/lib/Analysis/ScalarEvolution.cpp
+++ b/lib/Analysis/ScalarEvolution.cpp
@@ -1466,6 +1466,34 @@
 //            Basic SCEV Analysis and PHI Idiom Recognition Code
 //
 
+/// deleteValueFromRecords - This method should be called by the
+/// client before it removes an instruction from the program, to make sure
+/// that no dangling references are left around.
+void ScalarEvolution::deleteValueFromRecords(Value *V) {
+  SmallVector<Value *, 16> Worklist;
+
+  if (Scalars.erase(V)) {
+    if (PHINode *PN = dyn_cast<PHINode>(V))
+      ConstantEvolutionLoopExitValue.erase(PN);
+    Worklist.push_back(V);
+  }
+
+  while (!Worklist.empty()) {
+    Value *VV = Worklist.back();
+    Worklist.pop_back();
+
+    for (Instruction::use_iterator UI = VV->use_begin(), UE = VV->use_end();
+         UI != UE; ++UI) {
+      Instruction *Inst = cast<Instruction>(*UI);
+      if (Scalars.erase(Inst)) {
+        if (PHINode *PN = dyn_cast<PHINode>(VV))
+          ConstantEvolutionLoopExitValue.erase(PN);
+        Worklist.push_back(Inst);
+      }
+    }
+  }
+}
+
 /// isSCEVable - Test if values of the given type are analyzable within
 /// the SCEV framework. This primarily includes integer types, and it
 /// can optionally include pointer types if the ScalarEvolution class
@@ -1527,10 +1555,10 @@
 SCEVHandle ScalarEvolution::getSCEV(Value *V) {
   assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
 
-  std::map<SCEVCallbackVH, SCEVHandle>::iterator I = Scalars.find(V);
+  std::map<Value*, SCEVHandle>::iterator I = Scalars.find(V);
   if (I != Scalars.end()) return I->second;
   SCEVHandle S = createSCEV(V);
-  Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S));
+  Scalars.insert(std::make_pair(V, S));
   return S;
 }
 
@@ -1619,8 +1647,7 @@
 void ScalarEvolution::
 ReplaceSymbolicValueWithConcrete(Instruction *I, const SCEVHandle &SymName,
                                  const SCEVHandle &NewVal) {
-  std::map<SCEVCallbackVH, SCEVHandle>::iterator SI =
-    Scalars.find(SCEVCallbackVH(I, this));
+  std::map<Value*, SCEVHandle>::iterator SI = Scalars.find(I);
   if (SI == Scalars.end()) return;
 
   SCEVHandle NV =
@@ -1652,7 +1679,7 @@
         SCEVHandle SymbolicName = getUnknown(PN);
         assert(Scalars.find(PN) == Scalars.end() &&
                "PHI node already processed?");
-        Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
+        Scalars.insert(std::make_pair(PN, SymbolicName));
 
         // Using this symbolic name for the PHI, analyze the value coming around
         // the back-edge.
@@ -2104,7 +2131,7 @@
 void ScalarEvolution::forgetLoopPHIs(const Loop *L) {
   for (BasicBlock::iterator I = L->getHeader()->begin();
        PHINode *PN = dyn_cast<PHINode>(I); ++I)
-    Scalars.erase(PN);
+    deleteValueFromRecords(PN);
 }
 
 /// ComputeBackedgeTakenCount - Compute the number of times the backedge
@@ -3317,25 +3344,6 @@
 
 
 //===----------------------------------------------------------------------===//
-//                   SCEVCallbackVH Class Implementation
-//===----------------------------------------------------------------------===//
-
-void SCEVCallbackVH::deleted() {
-  assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!");
-  SE->Scalars.erase(getValPtr());
-  // this now dangles!
-}
-
-void SCEVCallbackVH::allUsesReplacedWith(Value *V) {
-  assert(SE && "SCEVCallbackVH called with a non-null ScalarEvolution!");
-  SE->Scalars.erase(getValPtr());
-  // this now dangles!
-}
-
-SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
-  : CallbackVH(V), SE(se) {}
-
-//===----------------------------------------------------------------------===//
 //                   ScalarEvolution Class Implementation
 //===----------------------------------------------------------------------===//