Perform an intelligent splice of the predecessor with the single successor.

If the predecessor's being spliced into a landing pad, then we need the PHIs to
come first and the rest of the predecessor's code to come *after* the landing
pad instruction.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@193035 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Utils/Local.cpp b/lib/Transforms/Utils/Local.cpp
index 82b8da3..78217c8 100644
--- a/lib/Transforms/Utils/Local.cpp
+++ b/lib/Transforms/Utils/Local.cpp
@@ -503,7 +503,19 @@
 
   // Splice all the instructions from PredBB to DestBB.
   PredBB->getTerminator()->eraseFromParent();
-  DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList());
+
+  // First splice over the PHI nodes.
+  BasicBlock::iterator PI = PredBB->begin();
+  while (isa<PHINode>(PI))
+    ++PI;
+
+  if (PI != PredBB->begin())
+    DestBB->getInstList().splice(DestBB->begin(), PredBB->getInstList(),
+                                 PredBB->begin(), PI);
+
+  // Now splice over the rest of the instructions.
+  DestBB->getInstList().splice(DestBB->getFirstInsertionPt(),
+                               PredBB->getInstList(), PI, PredBB->end());
 
   if (P) {
     DominatorTree *DT = P->getAnalysisIfAvailable<DominatorTree>();
@@ -513,6 +525,7 @@
       DT->eraseNode(PredBB);
     }
   }
+
   // Nuke BB.
   PredBB->eraseFromParent();
 }