[llvm] edf2e0e - [InstCombine] Folding `@llvm.ptrmask` with itself

Noah Goldstein via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 27 15:43:26 PDT 2023


Author: Noah Goldstein
Date: 2023-07-27T17:43:08-05:00
New Revision: edf2e0e075e8562ca6a57a3614fbb2b65077e375

URL: https://github.com/llvm/llvm-project/commit/edf2e0e075e8562ca6a57a3614fbb2b65077e375
DIFF: https://github.com/llvm/llvm-project/commit/edf2e0e075e8562ca6a57a3614fbb2b65077e375.diff

LOG: [InstCombine] Folding `@llvm.ptrmask` with itself

`@llvm.ptrmask` is basically just `and` with a `ptr` operand. This is
a trivial combine to do with `and` (many others could also be added).

Reviewed By: nikic

Differential Revision: https://reviews.llvm.org/D154006

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
    llvm/test/Transforms/InstCombine/consecutive-ptrmask.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index d3ec6a7aa667be..81f48c28ab3604 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1929,6 +1929,21 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
       return &CI;
     break;
   }
+  case Intrinsic::ptrmask: {
+    Value *InnerPtr, *InnerMask;
+    if (match(II->getArgOperand(0),
+              m_OneUse(m_Intrinsic<Intrinsic::ptrmask>(m_Value(InnerPtr),
+                                                       m_Value(InnerMask))))) {
+      if (II->getArgOperand(1)->getType() == InnerMask->getType()) {
+        Value *NewMask = Builder.CreateAnd(II->getArgOperand(1), InnerMask);
+        return replaceInstUsesWith(
+            *II,
+            Builder.CreateIntrinsic(InnerPtr->getType(), Intrinsic::ptrmask,
+                                    {InnerPtr, NewMask}));
+      }
+    }
+    break;
+  }
   case Intrinsic::uadd_with_overflow:
   case Intrinsic::sadd_with_overflow: {
     if (Instruction *I = foldIntrinsicWithOverflowCommon(II))

diff  --git a/llvm/test/Transforms/InstCombine/consecutive-ptrmask.ll b/llvm/test/Transforms/InstCombine/consecutive-ptrmask.ll
index 97d9bf2f25782e..904c758b99306f 100644
--- a/llvm/test/Transforms/InstCombine/consecutive-ptrmask.ll
+++ b/llvm/test/Transforms/InstCombine/consecutive-ptrmask.ll
@@ -7,8 +7,8 @@ declare void @use.ptr(ptr)
 define ptr @fold_2x(ptr %p, i64 %m0, i64 %m1) {
 ; CHECK-LABEL: define ptr @fold_2x
 ; CHECK-SAME: (ptr [[P:%.*]], i64 [[M0:%.*]], i64 [[M1:%.*]]) {
-; CHECK-NEXT:    [[P0:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 [[M0]])
-; CHECK-NEXT:    [[P1:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P0]], i64 [[M1]])
+; CHECK-NEXT:    [[TMP1:%.*]] = and i64 [[M1]], [[M0]]
+; CHECK-NEXT:    [[P1:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 [[TMP1]])
 ; CHECK-NEXT:    ret ptr [[P1]]
 ;
   %p0 = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 %m0)
@@ -19,8 +19,8 @@ define ptr @fold_2x(ptr %p, i64 %m0, i64 %m1) {
 define ptr @fold_2x_i32(ptr %p, i32 %m0, i32 %m1) {
 ; CHECK-LABEL: define ptr @fold_2x_i32
 ; CHECK-SAME: (ptr [[P:%.*]], i32 [[M0:%.*]], i32 [[M1:%.*]]) {
-; CHECK-NEXT:    [[P0:%.*]] = call ptr @llvm.ptrmask.p0.i32(ptr [[P]], i32 [[M0]])
-; CHECK-NEXT:    [[P1:%.*]] = call ptr @llvm.ptrmask.p0.i32(ptr [[P0]], i32 [[M1]])
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[M1]], [[M0]]
+; CHECK-NEXT:    [[P1:%.*]] = call ptr @llvm.ptrmask.p0.i32(ptr [[P]], i32 [[TMP1]])
 ; CHECK-NEXT:    ret ptr [[P1]]
 ;
   %p0 = call ptr @llvm.ptrmask.p0.i32(ptr %p, i32 %m0)


        


More information about the llvm-commits mailing list