Index: llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp =================================================================== --- llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp +++ llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -373,22 +373,14 @@ if (!Later.Size.isPrecise() || !Earlier.Size.isPrecise()) { // In case no constant size is known, try to an IR values for the number // of bytes written and check if they match. - auto GetSizeFromInstr = [](const Instruction *I) -> Value * { - if (const IntrinsicInst *II = dyn_cast(I)) { - switch (II->getIntrinsicID()) { - default: - return nullptr; - case Intrinsic::memcpy: - case Intrinsic::memset: - return II->getArgOperand(2); - } - } - return nullptr; - }; - Value *LaterV = GetSizeFromInstr(LaterI); - Value *EarlierV = GetSizeFromInstr(EarlierI); - if (LaterV && LaterV == EarlierV && AA.isMustAlias(Earlier, Later)) - return OW_Complete; + const auto *LaterMemI = dyn_cast(LaterI); + const auto *EarlierMemI = dyn_cast(EarlierI); + if (LaterMemI && EarlierMemI) { + const Value *LaterV = LaterMemI->getLength(); + const Value *EarlierV = EarlierMemI->getLength(); + if (LaterV == EarlierV && AA.isMustAlias(Earlier, Later)) + return OW_Complete; + } // Masked stores have imprecise locations, but we can reason about them // to some extent. Index: llvm/test/Transforms/DeadStoreElimination/memory-intrinsics-sizes.ll =================================================================== --- llvm/test/Transforms/DeadStoreElimination/memory-intrinsics-sizes.ll +++ llvm/test/Transforms/DeadStoreElimination/memory-intrinsics-sizes.ll @@ -199,5 +199,135 @@ ret void } +define void @memmove_equal_size_values(i8* noalias %src, i8* noalias %dst, i64 %len) { +; CHECK-LABEL: @memmove_equal_size_values( +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST:%.*]], i8* [[SRC:%.*]], i64 [[LEN:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len, i1 false) + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len, i1 false) + ret void +} + +define void @memmove_different_size_values_1(i8* noalias %src, i8* noalias %dst, i64 %len.1, i64 %len.2) { +; CHECK-LABEL: @memmove_different_size_values_1( +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST:%.*]], i8* [[SRC:%.*]], i64 [[LEN_1:%.*]], i1 false) +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST]], i8* [[SRC]], i64 [[LEN_2:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len.1, i1 false) + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len.2, i1 false) + ret void +} + +define void @memmove_different_size_values_2(i8* noalias %src, i8* noalias %dst, i64 %len) { +; CHECK-LABEL: @memmove_different_size_values_2( +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST:%.*]], i8* [[SRC:%.*]], i64 [[LEN:%.*]], i1 false) +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST]], i8* [[SRC]], i64 100, i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len, i1 false) + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 100, i1 false) + ret void +} + +define void @memmove_different_size_values_3(i8* noalias %src, i8* noalias %dst, i64 %len) { +; CHECK-LABEL: @memmove_different_size_values_3( +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST:%.*]], i8* [[SRC:%.*]], i64 100, i1 false) +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST]], i8* [[SRC]], i64 [[LEN:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 100, i1 false) + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len, i1 false) + ret void +} + +define void @memset_and_memmove_equal_size_values(i8* noalias %src, i8* noalias %dst, i64 %len) { +; CHECK-LABEL: @memset_and_memmove_equal_size_values( +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST:%.*]], i8* [[SRC:%.*]], i64 [[LEN:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memset.p0i8.i64(i8* align 1 %dst, i8 0, i64 %len, i1 false) + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len, i1 false) + ret void +} + +define void @memset_and_memmove_different_size_values_1(i8* noalias %src, i8* noalias %dst, i64 %len.1, i64 %len.2) { +; CHECK-LABEL: @memset_and_memmove_different_size_values_1( +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[DST:%.*]], i8 0, i64 [[LEN_1:%.*]], i1 false) +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST]], i8* [[SRC:%.*]], i64 [[LEN_2:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memset.p0i8.i64(i8* align 1 %dst, i8 0, i64 %len.1, i1 false) + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len.2, i1 false) + ret void +} + +define void @memset_and_memmove_different_size_values_2(i8* noalias %src, i8* noalias %dst, i64 %len.1) { +; CHECK-LABEL: @memset_and_memmove_different_size_values_2( +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[DST:%.*]], i8 0, i64 [[LEN_1:%.*]], i1 false) +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST]], i8* [[SRC:%.*]], i64 100, i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memset.p0i8.i64(i8* align 1 %dst, i8 0, i64 %len.1, i1 false) + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 100, i1 false) + ret void +} + +define void @memset_and_memmove_different_size_values_3(i8* noalias %src, i8* noalias %dst, i64 %len.1) { +; CHECK-LABEL: @memset_and_memmove_different_size_values_3( +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[DST:%.*]], i8 0, i64 100, i1 false) +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST]], i8* [[SRC:%.*]], i64 [[LEN_1:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memset.p0i8.i64(i8* align 1 %dst, i8 0, i64 100, i1 false) + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len.1, i1 false) + ret void +} + +define void @memmove_and_memset_equal_size_values(i8* noalias %src, i8* noalias %dst, i64 %len) { +; CHECK-LABEL: @memmove_and_memset_equal_size_values( +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[DST:%.*]], i8 0, i64 [[LEN:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len, i1 false) + call void @llvm.memset.p0i8.i64(i8* align 1 %dst, i8 0, i64 %len, i1 false) + ret void +} + +define void @memmove_and_memset_different_size_values_1(i8* noalias %src, i8* noalias %dst, i64 %len.1, i64 %len.2) { +; CHECK-LABEL: @memmove_and_memset_different_size_values_1( +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST:%.*]], i8* [[SRC:%.*]], i64 [[LEN_1:%.*]], i1 false) +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[DST]], i8 0, i64 [[LEN_2:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len.1, i1 false) + call void @llvm.memset.p0i8.i64(i8* align 1 %dst, i8 0, i64 %len.2, i1 false) + ret void +} + +define void @memmove_and_memset_different_size_values_2(i8* noalias %src, i8* noalias %dst, i64 %len.1) { +; CHECK-LABEL: @memmove_and_memset_different_size_values_2( +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST:%.*]], i8* [[SRC:%.*]], i64 [[LEN_1:%.*]], i1 false) +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[DST]], i8 0, i64 100, i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len.1, i1 false) + call void @llvm.memset.p0i8.i64(i8* align 1 %dst, i8 0, i64 100, i1 false) + ret void +} + +define void @memmove_and_memset_different_size_values_3(i8* noalias %src, i8* noalias %dst, i64 %len.1) { +; CHECK-LABEL: @memmove_and_memset_different_size_values_3( +; CHECK-NEXT: call void @llvm.memmove.p0i8.p0i8.i64(i8* [[DST:%.*]], i8* [[SRC:%.*]], i64 100, i1 false) +; CHECK-NEXT: call void @llvm.memset.p0i8.i64(i8* align 1 [[DST]], i8 0, i64 [[LEN_1:%.*]], i1 false) +; CHECK-NEXT: ret void +; + call void @llvm.memmove.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 100, i1 false) + call void @llvm.memset.p0i8.i64(i8* align 1 %dst, i8 0, i64 %len.1, i1 false) + ret void +} + declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i1 immarg) declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i1) +declare void @llvm.memmove.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i1)