diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp --- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -1014,6 +1014,10 @@ if (CB->isLifetimeStartOrEnd()) return false; + // Invokes are terminators, so they cannot be removed + if (isa(CB)) + return false; + return CB->use_empty() && CB->willReturn() && CB->doesNotThrow(); } diff --git a/llvm/test/Transforms/DeadStoreElimination/nounwind-invoke.ll b/llvm/test/Transforms/DeadStoreElimination/nounwind-invoke.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/DeadStoreElimination/nounwind-invoke.ll @@ -0,0 +1,38 @@ +; RUN: opt -dse -S < %s | FileCheck %s + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +; Make sure invokes are not removed as dead stores. +; CHECK-LABEL: @test_nounwind_invoke +define void @test_nounwind_invoke() personality ptr @__gxx_personality_v0 { +bb: + %tmp = alloca i32, align 4 + ; 'foo' is 'argmemonly', meaning it can only write to memory pointed by %tmp. + ; And this def is killed by 'call @llvm.lifetime.end.p0' in bb1 without being + ; used elsewhere, becoming a dead store. But we shouldn't remove this because + ; invokes are terminators and thus cannot be removed. + invoke void @foo(ptr %tmp) + to label %bb1 unwind label %bb2 +; CHECK: bb: +; CHECK-NEXT: alloca +; CHECK-NEXT: invoke + +bb1: ; preds = %bb + call void @llvm.lifetime.end.p0(i64 4, ptr %tmp) + ret void + +bb2: ; preds = %bb + %tmp1 = landingpad { ptr, i32 } + cleanup + resume { ptr, i32 } %tmp1 +} + +; Function Attrs: argmemonly nocallback nofree nosync nounwind willreturn +declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #0 +; Function Attrs: argmemonly nounwind willreturn +declare void @foo(ptr) #1 +declare i32 @__gxx_personality_v0(...) + +attributes #0 = { argmemonly nocallback nofree nosync nounwind willreturn } +attributes #1 = { argmemonly nounwind willreturn }