diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 846c3bf9d9a..b9a9bc1c393 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -1364,13 +1364,19 @@ bool llvm::SimplifyCFG(BasicBlock *BB) { SmallVector Preds(pred_begin(BB), pred_end(BB)); while (!Preds.empty()) { BasicBlock *Pred = Preds.back(); + + if (Pred->getUnwindDest() == BB) { + Pred->setUnwindDest(NULL); + Changed = true; + } + if (BranchInst *BI = dyn_cast(Pred->getTerminator())) { - if (BI->isUnconditional()) { + if (BI->isUnconditional() && BI->getSuccessor(0) == BB) { Pred->getInstList().pop_back(); // nuke uncond branch new UnwindInst(Pred); // Use unwind. Changed = true; } - } else if (InvokeInst *II = dyn_cast(Pred->getTerminator())) { + } else if (InvokeInst *II = dyn_cast(Pred->getTerminator())) if (II->getUnwindDest() == BB) { // Insert a new branch instruction before the invoke, because this // is now a fall through... @@ -1388,9 +1394,6 @@ bool llvm::SimplifyCFG(BasicBlock *BB) { delete II; Changed = true; } - } else if (Pred->getUnwindDest() == BB) { - Pred->setUnwindDest(NULL); - } Preds.pop_back(); } diff --git a/test/Transforms/SimplifyCFG/unwindto.ll b/test/Transforms/SimplifyCFG/unwindto.ll index 28d9d1e6e39..4e91c830150 100644 --- a/test/Transforms/SimplifyCFG/unwindto.ll +++ b/test/Transforms/SimplifyCFG/unwindto.ll @@ -41,3 +41,21 @@ entry: unwind_to %cleanup cleanup: unwind } + +define i32 @f4() { +entry: unwind_to %cleanup + call void @g(i32 0) + br label %cleanup +cleanup: + unwind +} + +define i32 @f5() { +entry: unwind_to %cleanup + call void @g(i32 0) + br label %other +other: + ret i32 0 +cleanup: + unwind +}