Firstly, having a BranchInst isn't exclusive with having an unwind_to.
authorNick Lewycky <nicholas@mxc.ca>
Sun, 9 Mar 2008 07:50:37 +0000 (07:50 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Sun, 9 Mar 2008 07:50:37 +0000 (07:50 +0000)
Secondly, we have to check whether the branch is actually pointing to the block
with the unwind in it. We could have gotten here because of the unwind_to alone.

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

lib/Transforms/Utils/SimplifyCFG.cpp
test/Transforms/SimplifyCFG/unwindto.ll

index 846c3bf9d9aa8f0dcb14873c322ca3d38abb256f..b9a9bc1c39357dbdeb6f5f1e114b3ff523b505d5 100644 (file)
@@ -1364,13 +1364,19 @@ bool llvm::SimplifyCFG(BasicBlock *BB) {
     SmallVector<BasicBlock*, 8> 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<BranchInst>(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<InvokeInst>(Pred->getTerminator())) {
+      } else if (InvokeInst *II = dyn_cast<InvokeInst>(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();
     }
index 28d9d1e6e392ae4fd7e6f1adad07e732b356970f..4e91c8301502ebcdc82c1c7e2269d18883fd18f1 100644 (file)
@@ -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
+}