Bulk erasing instructions without RAUWing them is unsafe. Instead, break them
into a new BB that has no predecessors.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77433 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp
index 506ba1f..14212bc 100644
--- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp
+++ b/lib/Transforms/Scalar/SimplifyLibCalls.cpp
@@ -519,17 +519,19 @@
 
     TerminatorInst *OldTI = CI->getParent()->getTerminator();
     
-    // Create the return after the call.
-    ReturnInst *RI = B.CreateRet(CI->getOperand(1));
-
     // Drop all successor phi node entries.
     for (unsigned i = 0, e = OldTI->getNumSuccessors(); i != e; ++i)
       OldTI->getSuccessor(i)->removePredecessor(CI->getParent());
     
-    // Erase all instructions from after our return instruction until the end of
-    // the block.
-    BasicBlock::iterator FirstDead = RI; ++FirstDead;
-    CI->getParent()->getInstList().erase(FirstDead, CI->getParent()->end());
+    // Split the basic block after the call to exit.
+    BasicBlock::iterator FirstDead = CI; ++FirstDead;
+    CI->getParent()->splitBasicBlock(FirstDead);
+    B.SetInsertPoint(B.GetInsertBlock());
+
+    // Remove the branch that splitBB created and insert a return instead.
+    CI->getParent()->getTerminator()->eraseFromParent();
+    B.CreateRet(CI->getOperand(1));
+
     return CI;
   }
 };