[llvm] c68b560 - [DSE] Handle memmove with equal non-const sizes
Dávid Bolvanský via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 10 08:52:31 PST 2021
Author: Dávid Bolvanský
Date: 2021-03-10T17:52:00+01:00
New Revision: c68b560be381e831fd72c6b8b8909427e4e2ff36
URL: https://github.com/llvm/llvm-project/commit/c68b560be381e831fd72c6b8b8909427e4e2ff36
DIFF: https://github.com/llvm/llvm-project/commit/c68b560be381e831fd72c6b8b8909427e4e2ff36.diff
LOG: [DSE] Handle memmove with equal non-const sizes
Follow up for fhahn's D98284. Also fixes a case from PR47644.
Reviewed By: fhahn
Differential Revision: https://reviews.llvm.org/D98346
Added:
Modified:
llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
llvm/test/Transforms/DeadStoreElimination/memory-intrinsics-sizes.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
index 61cc4e3b06a3..9cf9760c074e 100644
--- a/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
@@ -373,22 +373,14 @@ isOverwrite(const Instruction *LaterI, const Instruction *EarlierI,
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<IntrinsicInst>(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<MemIntrinsic>(LaterI);
+ const auto *EarlierMemI = dyn_cast<MemIntrinsic>(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.
diff --git a/llvm/test/Transforms/DeadStoreElimination/memory-intrinsics-sizes.ll b/llvm/test/Transforms/DeadStoreElimination/memory-intrinsics-sizes.ll
index 412ae6569154..944a08d56ae3 100644
--- a/llvm/test/Transforms/DeadStoreElimination/memory-intrinsics-sizes.ll
+++ b/llvm/test/Transforms/DeadStoreElimination/memory-intrinsics-sizes.ll
@@ -199,5 +199,155 @@ define void @memcpy_and_memset_
diff erent_size_values_3(i8* noalias %src, i8* noa
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_
diff erent_size_values_1(i8* noalias %src, i8* noalias %dst, i64 %len.1, i64 %len.2) {
+; CHECK-LABEL: @memmove_
diff erent_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_
diff erent_size_values_2(i8* noalias %src, i8* noalias %dst, i64 %len) {
+; CHECK-LABEL: @memmove_
diff erent_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_
diff erent_size_values_3(i8* noalias %src, i8* noalias %dst, i64 %len) {
+; CHECK-LABEL: @memmove_
diff erent_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_
diff erent_size_values_1(i8* noalias %src, i8* noalias %dst, i64 %len.1, i64 %len.2) {
+; CHECK-LABEL: @memset_and_memmove_
diff erent_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_
diff erent_size_values_2(i8* noalias %src, i8* noalias %dst, i64 %len.1) {
+; CHECK-LABEL: @memset_and_memmove_
diff erent_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_
diff erent_size_values_3(i8* noalias %src, i8* noalias %dst, i64 %len.1) {
+; CHECK-LABEL: @memset_and_memmove_
diff erent_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_
diff erent_size_values_1(i8* noalias %src, i8* noalias %dst, i64 %len.1, i64 %len.2) {
+; CHECK-LABEL: @memmove_and_memset_
diff erent_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_
diff erent_size_values_2(i8* noalias %src, i8* noalias %dst, i64 %len.1) {
+; CHECK-LABEL: @memmove_and_memset_
diff erent_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_
diff erent_size_values_3(i8* noalias %src, i8* noalias %dst, i64 %len.1) {
+; CHECK-LABEL: @memmove_and_memset_
diff erent_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
+}
+
+define void @memmove_and_memcpy_equal_size_values(i8* noalias %src, i8* noalias %dst, i64 %len) {
+; CHECK-LABEL: @memmove_and_memcpy_equal_size_values(
+; CHECK-NEXT: call void @llvm.memcpy.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.memcpy.p0i8.p0i8.i64(i8* %dst, i8* %src, i64 %len, i1 false)
+ ret void
+}
+
+define void @memcpy_and_memmove_equal_size_values(i8* noalias %src, i8* noalias %dst, i64 %len) {
+; CHECK-LABEL: @memcpy_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.memcpy.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
+}
+
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)
More information about the llvm-commits
mailing list