diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 25baf1bf640..ec2c1af97ea 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -3424,6 +3424,11 @@ static bool removeEmptyCleanup(CleanupReturnInst *RI) { // This isn't an empty cleanup. return false; + // We cannot kill the pad if it has multiple uses. This typically arises + // from unreachable basic blocks. + if (!CPInst->hasOneUse()) + return false; + // Check that there are no other instructions except for benign intrinsics. BasicBlock::iterator I = CPInst->getIterator(), E = RI->getIterator(); while (++I != E) { diff --git a/test/Transforms/SimplifyCFG/empty-cleanuppad.ll b/test/Transforms/SimplifyCFG/empty-cleanuppad.ll index e83cbb50e58..9f657a81a05 100644 --- a/test/Transforms/SimplifyCFG/empty-cleanuppad.ll +++ b/test/Transforms/SimplifyCFG/empty-cleanuppad.ll @@ -434,6 +434,30 @@ try.cont: ret i32 0 } +; CHECK-LABEL: define void @f10( +define void @f10(i32 %V) personality i32 (...)* @__CxxFrameHandler3 { +entry: + invoke void @g() + to label %unreachable unwind label %cleanup +; CHECK: call void @g() +; CHECK-NEXT: unreachable + +unreachable: + unreachable + +cleanup: + %cp = cleanuppad within none [] + switch i32 %V, label %cleanupret1 [ + i32 0, label %cleanupret2 + ] + +cleanupret1: + cleanupret from %cp unwind to caller + +cleanupret2: + cleanupret from %cp unwind to caller +} + %struct.S = type { i8 } %struct.S2 = type { i8 } declare void @"\01??1S2@@QEAA@XZ"(%struct.S2*)