Fix a use of an invalidated iterator in the case where there are multiple
adjacent uses of a dead basic block from the same user. This fixes PR5596.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@89658 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp
index 9778278..d8c59b1 100644
--- a/lib/Transforms/Scalar/SCCP.cpp
+++ b/lib/Transforms/Scalar/SCCP.cpp
@@ -1871,8 +1871,12 @@
       BasicBlock *DeadBB = BlocksToErase[i];
       for (Value::use_iterator UI = DeadBB->use_begin(), UE = DeadBB->use_end();
            UI != UE; ) {
+        // Grab the user and then increment the iterator early, as the user
+        // will be deleted. Step past all adjacent uses from the same user.
+        Instruction *I = dyn_cast<Instruction>(*UI);
+        do { ++UI; } while (UI != UE && *UI == I);
+
         // Ignore blockaddress users; BasicBlock's dtor will handle them.
-        Instruction *I = dyn_cast<Instruction>(*UI++);
         if (!I) continue;
 
         bool Folded = ConstantFoldTerminator(I->getParent());