defensive patch: if CGP is merging a block with the entry block, make sure
authorChris Lattner <sabre@nondot.org>
Thu, 27 Nov 2008 19:29:14 +0000 (19:29 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 27 Nov 2008 19:29:14 +0000 (19:29 +0000)
it ends up being the entry block.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60180 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/CodeGenPrepare.cpp

index caf60c8c7af04ef5c18071484b241d09607c6471..9bf39911b6c697f80a2059766b225f506fd9f2a8 100644 (file)
@@ -204,8 +204,15 @@ void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
 
   // If the destination block has a single pred, then this is a trivial edge,
   // just collapse it.
-  if (DestBB->getSinglePredecessor()) {
+  if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
+    // Remember if SinglePred was the entry block of the function.  If so, we
+    // will need to move BB back to the entry position.
+    bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
     MergeBasicBlockIntoOnlyPred(DestBB);
+
+    if (isEntry && BB != &BB->getParent()->getEntryBlock())
+      BB->moveBefore(&BB->getParent()->getEntryBlock());
+    
     DOUT << "AFTER:\n" << *DestBB << "\n\n\n";
     return;
   }