[PATCH] D156639: [InstCombine] Merge consecutive `llvm.ptrmask` with different mask types if a mask is constant.

Noah Goldstein via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 30 16:01:47 PDT 2023


goldstein.w.n created this revision.
goldstein.w.n added reviewers: nikic, RKSimon.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
goldstein.w.n requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We can `zext` / `trunc` a constant mask for free, so if one of the
masks if constant, we can proceed even if they are not the same type.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156639

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


Index: llvm/test/Transforms/InstCombine/consecutive-ptrmask.ll
===================================================================
--- llvm/test/Transforms/InstCombine/consecutive-ptrmask.ll
+++ llvm/test/Transforms/InstCombine/consecutive-ptrmask.ll
@@ -70,8 +70,8 @@
 define ptr @fold_2x_type_mismatch_const0(ptr %p, i32 %m1) {
 ; CHECK-LABEL: define ptr @fold_2x_type_mismatch_const0
 ; CHECK-SAME: (ptr [[P:%.*]], i32 [[M1:%.*]]) {
-; CHECK-NEXT:    [[P0:%.*]] = call align 128 ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 -128)
-; CHECK-NEXT:    [[P1:%.*]] = call align 128 ptr @llvm.ptrmask.p0.i32(ptr [[P0]], i32 [[M1]])
+; CHECK-NEXT:    [[TMP1:%.*]] = and i32 [[M1]], -128
+; CHECK-NEXT:    [[P1:%.*]] = call align 128 ptr @llvm.ptrmask.p0.i32(ptr [[P]], i32 [[TMP1]])
 ; CHECK-NEXT:    ret ptr [[P1]]
 ;
   %p0 = call ptr @llvm.ptrmask.p0.i64(ptr %p, i64 -128)
@@ -82,8 +82,8 @@
 define ptr @fold_2x_type_mismatch_const1(ptr %p, i64 %m0) {
 ; CHECK-LABEL: define ptr @fold_2x_type_mismatch_const1
 ; CHECK-SAME: (ptr [[P:%.*]], i64 [[M0:%.*]]) {
-; CHECK-NEXT:    [[P0:%.*]] = call ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 [[M0]])
-; CHECK-NEXT:    [[P1:%.*]] = call align 2 ptr @llvm.ptrmask.p0.i32(ptr [[P0]], i32 -2)
+; CHECK-NEXT:    [[TMP1:%.*]] = and i64 [[M0]], 4294967294
+; CHECK-NEXT:    [[P1:%.*]] = call align 2 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)
@@ -95,8 +95,7 @@
 define ptr @fold_2x_type_mismatch_const2(ptr %p) {
 ; CHECK-LABEL: define ptr @fold_2x_type_mismatch_const2
 ; CHECK-SAME: (ptr [[P:%.*]]) {
-; CHECK-NEXT:    [[P0:%.*]] = call align 4 ptr @llvm.ptrmask.p0.i32(ptr [[P]], i32 -4)
-; CHECK-NEXT:    [[P1:%.*]] = call align 32 ptr @llvm.ptrmask.p0.i64(ptr [[P0]], i64 4294967264)
+; CHECK-NEXT:    [[P1:%.*]] = call align 32 ptr @llvm.ptrmask.p0.i64(ptr [[P]], i64 4294967264)
 ; CHECK-NEXT:    ret ptr [[P1]]
 ;
   %p0 = call ptr @llvm.ptrmask.p0.i32(ptr %p, i32 -4)
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1950,10 +1950,30 @@
     //    -> (ptrmask p, (and A, B))
     if (match(Op0, m_OneUse(m_Intrinsic<Intrinsic::ptrmask>(
                        m_Value(InnerPtr), m_Value(InnerMask))))) {
+      // See if combining the two masks is free.
+      bool OkayToMerge = InnerMask->getType() == Op1->getType();
+      bool NeedsNew = false;
+      if (!OkayToMerge) {
+        if (match(InnerMask, m_ImmConstant())) {
+          InnerMask = Builder.CreateZExtOrTrunc(InnerMask, Op1->getType());
+          OkayToMerge = true;
+        } else if (match(Op1, m_ImmConstant())) {
+          Op1 = Builder.CreateZExtOrTrunc(Op1, InnerMask->getType());
+          OkayToMerge = true;
+          // Need to create a new one here, as the intrinsic id needs to change.
+          NeedsNew = true;
+        }
+      }
       if (InnerMask->getType() == Op1->getType()) {
         // TODO: If InnerMask == Op1, we could copy attributes from inner
         // callsite -> outer callsite.
         Value *NewMask = Builder.CreateAnd(Op1, InnerMask);
+        if (NeedsNew)
+          return replaceInstUsesWith(
+              *II,
+              Builder.CreateIntrinsic(InnerPtr->getType(), Intrinsic::ptrmask,
+                                      {InnerPtr, NewMask}));
+
         replaceOperand(CI, 0, InnerPtr);
         replaceOperand(CI, 1, NewMask);
         Changed = true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156639.545483.patch
Type: text/x-patch
Size: 3615 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230730/75098062/attachment.bin>


More information about the llvm-commits mailing list