diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp index ec35a891e05..5de5fb3b776 100644 --- a/lib/Transforms/Scalar/JumpThreading.cpp +++ b/lib/Transforms/Scalar/JumpThreading.cpp @@ -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 index 00000000000..216dacb8d26 --- /dev/null +++ b/test/Transforms/JumpThreading/2008-11-27-EntryMunge.ll @@ -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 +}