[llvm] [InstCombine] Convert mem intrinsic with null into a noop (PR #100388)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 24 07:52:10 PDT 2024


https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/100388

>From a61d61371b818b9fa0976a7d1fa0d32aab7c7208 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 24 Jul 2024 22:08:30 +0800
Subject: [PATCH 1/3] [InstCombine] Add pre-commit tests. NFC.

---
 .../Transforms/InstCombine/mem-intrinsics.ll  | 73 +++++++++++++++++++
 1 file changed, 73 insertions(+)
 create mode 100644 llvm/test/Transforms/InstCombine/mem-intrinsics.ll

diff --git a/llvm/test/Transforms/InstCombine/mem-intrinsics.ll b/llvm/test/Transforms/InstCombine/mem-intrinsics.ll
new file mode 100644
index 0000000000000..73619f81115f0
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/mem-intrinsics.ll
@@ -0,0 +1,73 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
+; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+
+define void @memset_null(i64 %len) {
+; CHECK-LABEL: define void @memset_null(
+; CHECK-SAME: i64 [[LEN:%.*]]) {
+; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 4294967296 null, i8 0, i64 [[LEN]], i1 false)
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.memset.p0.i64(ptr null, i8 0, i64 %len, i1 false)
+  ret void
+}
+
+define void @memset_null_ub() {
+; CHECK-LABEL: define void @memset_null_ub() {
+; CHECK-NEXT:    store i64 poison, ptr null, align 4294967296
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.memset.p0.i64(ptr null, i8 0, i64 8, i1 false)
+  ret void
+}
+
+define void @memcpy_null_src(ptr %dst, i64 %len) {
+; CHECK-LABEL: define void @memcpy_null_src(
+; CHECK-SAME: ptr [[DST:%.*]], i64 [[LEN:%.*]]) {
+; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr align 1 [[DST]], ptr align 4294967296 null, i64 [[LEN]], i1 false)
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.memcpy.p0.i64(ptr %dst, ptr null, i64 %len, i1 false)
+  ret void
+}
+
+define void @memmove_null_src(ptr %dst, i64 %len) {
+; CHECK-LABEL: define void @memmove_null_src(
+; CHECK-SAME: ptr [[DST:%.*]], i64 [[LEN:%.*]]) {
+; CHECK-NEXT:    call void @llvm.memmove.p0.p0.i64(ptr align 1 [[DST]], ptr align 4294967296 null, i64 [[LEN]], i1 false)
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.memmove.p0.i64(ptr %dst, ptr null, i64 %len, i1 false)
+  ret void
+}
+
+define void @memset_element_atomic(i64 %len) {
+; CHECK-LABEL: define void @memset_element_atomic(
+; CHECK-SAME: i64 [[LEN:%.*]]) {
+; CHECK-NEXT:    call void @llvm.memset.element.unordered.atomic.p0.i64(ptr align 4294967296 null, i8 0, i64 [[LEN]], i32 1)
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.memset.element.unordered.atomic.p0.i64(ptr align 1 null, i8 0, i64 %len, i32 1)
+  ret void
+}
+
+; negative tests
+
+define void @memset_null_volatile(i64 %len) {
+; CHECK-LABEL: define void @memset_null_volatile(
+; CHECK-SAME: i64 [[LEN:%.*]]) {
+; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr null, i8 0, i64 [[LEN]], i1 true)
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.memset.p0.i64(ptr null, i8 0, i64 %len, i1 true)
+  ret void
+}
+
+define void @memset_null_is_defined(i64 %len) null_pointer_is_valid {
+; CHECK-LABEL: define void @memset_null_is_defined(
+; CHECK-SAME: i64 [[LEN:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 4294967296 null, i8 0, i64 [[LEN]], i1 false)
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.memset.p0.i64(ptr null, i8 0, i64 %len, i1 false)
+  ret void
+}

>From 598cf9261511f0fcabe659697f9c9aec0d522544 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 24 Jul 2024 22:19:03 +0800
Subject: [PATCH 2/3] [InstCombine] Convert mem intrinsic with null into a noop

---
 llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp |  9 +++++++++
 llvm/test/Transforms/InstCombine/mem-intrinsics.ll   | 12 ++++++++----
 llvm/test/Transforms/InstCombine/mempcpy.ll          |  2 +-
 3 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 3502bbbbeae25..aab915fd97902 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1575,16 +1575,25 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         return eraseInstFromFunction(CI);
     }
 
+    bool SrcIsNull = false;
     // If we can determine a pointer alignment that is bigger than currently
     // set, update the alignment.
     if (auto *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
       if (Instruction *I = SimplifyAnyMemTransfer(MTI))
         return I;
+      SrcIsNull = isa<ConstantPointerNull>(MTI->getRawSource());
     } else if (auto *MSI = dyn_cast<AnyMemSetInst>(MI)) {
       if (Instruction *I = SimplifyAnyMemSet(MSI))
         return I;
     }
 
+    // If src/dest is null, this memory intrinsic must be a noop.
+    if (!NullPointerIsDefined(MI->getFunction()) &&
+        (SrcIsNull || isa<ConstantPointerNull>(MI->getRawDest()))) {
+      Builder.CreateAssumption(Builder.CreateIsNull(MI->getLength()));
+      return eraseInstFromFunction(CI);
+    }
+
     if (Changed) return II;
   }
 
diff --git a/llvm/test/Transforms/InstCombine/mem-intrinsics.ll b/llvm/test/Transforms/InstCombine/mem-intrinsics.ll
index 73619f81115f0..5448d3c20fb12 100644
--- a/llvm/test/Transforms/InstCombine/mem-intrinsics.ll
+++ b/llvm/test/Transforms/InstCombine/mem-intrinsics.ll
@@ -4,7 +4,8 @@
 define void @memset_null(i64 %len) {
 ; CHECK-LABEL: define void @memset_null(
 ; CHECK-SAME: i64 [[LEN:%.*]]) {
-; CHECK-NEXT:    call void @llvm.memset.p0.i64(ptr align 4294967296 null, i8 0, i64 [[LEN]], i1 false)
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[LEN]], 0
+; CHECK-NEXT:    call void @llvm.assume(i1 [[TMP1]])
 ; CHECK-NEXT:    ret void
 ;
   call void @llvm.memset.p0.i64(ptr null, i8 0, i64 %len, i1 false)
@@ -23,7 +24,8 @@ define void @memset_null_ub() {
 define void @memcpy_null_src(ptr %dst, i64 %len) {
 ; CHECK-LABEL: define void @memcpy_null_src(
 ; CHECK-SAME: ptr [[DST:%.*]], i64 [[LEN:%.*]]) {
-; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr align 1 [[DST]], ptr align 4294967296 null, i64 [[LEN]], i1 false)
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[LEN]], 0
+; CHECK-NEXT:    call void @llvm.assume(i1 [[TMP1]])
 ; CHECK-NEXT:    ret void
 ;
   call void @llvm.memcpy.p0.i64(ptr %dst, ptr null, i64 %len, i1 false)
@@ -33,7 +35,8 @@ define void @memcpy_null_src(ptr %dst, i64 %len) {
 define void @memmove_null_src(ptr %dst, i64 %len) {
 ; CHECK-LABEL: define void @memmove_null_src(
 ; CHECK-SAME: ptr [[DST:%.*]], i64 [[LEN:%.*]]) {
-; CHECK-NEXT:    call void @llvm.memmove.p0.p0.i64(ptr align 1 [[DST]], ptr align 4294967296 null, i64 [[LEN]], i1 false)
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[LEN]], 0
+; CHECK-NEXT:    call void @llvm.assume(i1 [[TMP1]])
 ; CHECK-NEXT:    ret void
 ;
   call void @llvm.memmove.p0.i64(ptr %dst, ptr null, i64 %len, i1 false)
@@ -43,7 +46,8 @@ define void @memmove_null_src(ptr %dst, i64 %len) {
 define void @memset_element_atomic(i64 %len) {
 ; CHECK-LABEL: define void @memset_element_atomic(
 ; CHECK-SAME: i64 [[LEN:%.*]]) {
-; CHECK-NEXT:    call void @llvm.memset.element.unordered.atomic.p0.i64(ptr align 4294967296 null, i8 0, i64 [[LEN]], i32 1)
+; CHECK-NEXT:    [[TMP1:%.*]] = icmp eq i64 [[LEN]], 0
+; CHECK-NEXT:    call void @llvm.assume(i1 [[TMP1]])
 ; CHECK-NEXT:    ret void
 ;
   call void @llvm.memset.element.unordered.atomic.p0.i64(ptr align 1 null, i8 0, i64 %len, i32 1)
diff --git a/llvm/test/Transforms/InstCombine/mempcpy.ll b/llvm/test/Transforms/InstCombine/mempcpy.ll
index c996758a919d9..82c34f8a864ce 100644
--- a/llvm/test/Transforms/InstCombine/mempcpy.ll
+++ b/llvm/test/Transforms/InstCombine/mempcpy.ll
@@ -55,7 +55,7 @@ define ptr @memcpy_big_const_n(ptr %d, ptr nocapture readonly %s) {
 
 define i32 @PR48810() {
 ; CHECK-LABEL: @PR48810(
-; CHECK-NEXT:    call void @llvm.memcpy.p0.p0.i64(ptr align 1 undef, ptr align 4294967296 null, i64 undef, i1 false)
+; CHECK-NEXT:    store i1 true, ptr poison, align 1
 ; CHECK-NEXT:    ret i32 undef
 ;
   %r = call dereferenceable(1) ptr @mempcpy(ptr undef, ptr null, i64 undef)

>From 5dd05ae6edff8dfceb2ea6963c2f0deccb05a831 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Wed, 24 Jul 2024 22:50:39 +0800
Subject: [PATCH 3/3] [InstCombine] Address review comment.

---
 .../lib/Transforms/InstCombine/InstCombineCalls.cpp | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index aab915fd97902..69abe356c332d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1575,21 +1575,26 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
         return eraseInstFromFunction(CI);
     }
 
-    bool SrcIsNull = false;
+    auto IsPointerUndefined = [MI](Value *Ptr) {
+      return isa<ConstantPointerNull>(Ptr) &&
+             !NullPointerIsDefined(
+                 MI->getFunction(),
+                 cast<PointerType>(Ptr->getType())->getAddressSpace());
+    };
+    bool SrcIsUndefined = false;
     // If we can determine a pointer alignment that is bigger than currently
     // set, update the alignment.
     if (auto *MTI = dyn_cast<AnyMemTransferInst>(MI)) {
       if (Instruction *I = SimplifyAnyMemTransfer(MTI))
         return I;
-      SrcIsNull = isa<ConstantPointerNull>(MTI->getRawSource());
+      SrcIsUndefined = IsPointerUndefined(MTI->getRawSource());
     } else if (auto *MSI = dyn_cast<AnyMemSetInst>(MI)) {
       if (Instruction *I = SimplifyAnyMemSet(MSI))
         return I;
     }
 
     // If src/dest is null, this memory intrinsic must be a noop.
-    if (!NullPointerIsDefined(MI->getFunction()) &&
-        (SrcIsNull || isa<ConstantPointerNull>(MI->getRawDest()))) {
+    if (SrcIsUndefined || IsPointerUndefined(MI->getRawDest())) {
       Builder.CreateAssumption(Builder.CreateIsNull(MI->getLength()));
       return eraseInstFromFunction(CI);
     }



More information about the llvm-commits mailing list