Fix PR3138: if we merge the entry block into another block, make sure to
authorChris Lattner <sabre@nondot.org>
Thu, 27 Nov 2008 19:25:19 +0000 (19:25 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 27 Nov 2008 19:25:19 +0000 (19:25 +0000)
move the other block back up into the entry position!

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

lib/Transforms/Scalar/JumpThreading.cpp
test/Transforms/JumpThreading/2008-11-27-EntryMunge.ll [new file with mode: 0644]

index ec35a891e05f9b3f124ee70253f84c78b567eb66..5de5fb3b776d18515f48d80b3cbcff29fe3f4ce8 100644 (file)
@@ -165,7 +165,13 @@ bool JumpThreading::ProcessBlock(BasicBlock *BB) {
   // predecessors of our predecessor block.
   if (BasicBlock *SinglePred = BB->getSinglePredecessor())
     if (SinglePred->getTerminator()->getNumSuccessors() == 1) {
+      // 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(BB);
+      
+      if (isEntry && BB != &BB->getParent()->getEntryBlock())
+        BB->moveBefore(&BB->getParent()->getEntryBlock());
       return true;
     }
   
diff --git a/test/Transforms/JumpThreading/2008-11-27-EntryMunge.ll b/test/Transforms/JumpThreading/2008-11-27-EntryMunge.ll
new file mode 100644 (file)
index 0000000..216dacb
--- /dev/null
@@ -0,0 +1,13 @@
+; RUN: llvm-as < %s  | opt -jump-threading -simplifycfg | llvm-dis | grep {ret i32 0}
+; PR3138
+
+define i32 @jt() {
+entry:
+       br i1 true, label %bb3, label %bb
+
+bb:             ; preds = %entry
+       unreachable
+
+bb3:            ; preds = %entry
+       ret i32 0
+}