[llvm] goldsteinn/trunc nuw nsw icmp fold (PR #87935)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 7 12:10:58 PDT 2024


https://github.com/goldsteinn created https://github.com/llvm/llvm-project/pull/87935

- **[InstCombine] Add tests for folding `(icmp pred (trunc nuw/nsw X), C)`; NFC**
- **[InstCombine] Fold `(icmp pred (trunc nuw/nsw X), C)` -> `(icmp pred X, (zext/sext C))`**


>From 0cdff019bf7ff5f821cc299005f3258e026f504d Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sun, 7 Apr 2024 14:01:15 -0500
Subject: [PATCH 1/2] [InstCombine] Add tests for folding `(icmp pred (trunc
 nuw/nsw X), C)`; NFC

---
 .../test/Transforms/InstCombine/icmp-trunc.ll | 150 ++++++++++++++++++
 1 file changed, 150 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/icmp-trunc.ll b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
index b2de9dddb21947..8732825e937885 100644
--- a/llvm/test/Transforms/InstCombine/icmp-trunc.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
@@ -555,3 +555,153 @@ define i1 @shl1_trunc_sgt4(i32 %a) {
   %r = icmp sgt i16 %t, 4
   ret i1 %r
 }
+
+define i1 @eq_nuw(i32 %x) {
+; DL64-LABEL: @eq_nuw(
+; DL64-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
+; DL64-NEXT:    [[R:%.*]] = icmp eq i32 [[TMP1]], 123
+; DL64-NEXT:    ret i1 [[R]]
+;
+; DL8-LABEL: @eq_nuw(
+; DL8-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
+; DL8-NEXT:    [[R:%.*]] = icmp eq i8 [[T]], 123
+; DL8-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nuw i32 %x to i8
+  %r = icmp eq i8 %t, 123
+  ret i1 %r
+}
+
+define i1 @ult_nuw(i32 %x) {
+; CHECK-LABEL: @ult_nuw(
+; CHECK-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp ult i8 [[T]], 45
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nuw i32 %x to i8
+  %r = icmp ult i8 %t, 45
+  ret i1 %r
+}
+
+define i1 @ule_nuw(i32 %x) {
+; CHECK-LABEL: @ule_nuw(
+; CHECK-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp ult i8 [[T]], 46
+; CHECK-NEXT:    call void @use(i8 [[T]])
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nuw i32 %x to i8
+  %r = icmp ule i8 %t, 45
+  call void @use(i8 %t)
+  ret i1 %r
+}
+
+define i1 @ugt_nuw(i32 %x) {
+; CHECK-LABEL: @ugt_nuw(
+; CHECK-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp ugt i8 [[T]], 12
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nuw i32 %x to i8
+  %r = icmp ugt i8 %t, 12
+  ret i1 %r
+}
+
+define i1 @uge_nuw(i48 %x) {
+; CHECK-LABEL: @uge_nuw(
+; CHECK-NEXT:    [[T:%.*]] = trunc nuw i48 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp ugt i8 [[T]], 98
+; CHECK-NEXT:    call void @use(i8 [[T]])
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nuw i48 %x to i8
+  %r = icmp uge i8 %t, 99
+  call void @use(i8 %t)
+  ret i1 %r
+}
+
+define i1 @sgt_nuw_fail(i32 %x) {
+; CHECK-LABEL: @sgt_nuw_fail(
+; CHECK-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[T]], 12
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nuw i32 %x to i8
+  %r = icmp sgt i8 %t, 12
+  ret i1 %r
+}
+
+define i1 @ne_nsw(i32 %x) {
+; DL64-LABEL: @ne_nsw(
+; DL64-NEXT:    [[TMP1:%.*]] = and i32 [[X:%.*]], 255
+; DL64-NEXT:    [[R:%.*]] = icmp ne i32 [[TMP1]], 123
+; DL64-NEXT:    ret i1 [[R]]
+;
+; DL8-LABEL: @ne_nsw(
+; DL8-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
+; DL8-NEXT:    [[R:%.*]] = icmp ne i8 [[T]], 123
+; DL8-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nsw i32 %x to i8
+  %r = icmp ne i8 %t, 123
+  ret i1 %r
+}
+
+define i1 @slt_nsw(i32 %x) {
+; CHECK-LABEL: @slt_nsw(
+; CHECK-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[T]], 45
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nsw i32 %x to i8
+  %r = icmp slt i8 %t, 45
+  ret i1 %r
+}
+
+define i1 @sle_nsw(i32 %x) {
+; CHECK-LABEL: @sle_nsw(
+; CHECK-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[T]], 46
+; CHECK-NEXT:    call void @use(i8 [[T]])
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nsw i32 %x to i8
+  %r = icmp sle i8 %t, 45
+  call void @use(i8 %t)
+  ret i1 %r
+}
+
+define i1 @sgt_nsw(i32 %x) {
+; CHECK-LABEL: @sgt_nsw(
+; CHECK-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[T]], 12
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nsw i32 %x to i8
+  %r = icmp sgt i8 %t, 12
+  ret i1 %r
+}
+
+define i1 @sge_nsw(i48 %x) {
+; CHECK-LABEL: @sge_nsw(
+; CHECK-NEXT:    [[T:%.*]] = trunc nsw i48 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[T]], 98
+; CHECK-NEXT:    call void @use(i8 [[T]])
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nsw i48 %x to i8
+  %r = icmp sge i8 %t, 99
+  call void @use(i8 %t)
+  ret i1 %r
+}
+
+define i1 @ule_nsw_fail(i32 %x) {
+; CHECK-LABEL: @ule_nsw_fail(
+; CHECK-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
+; CHECK-NEXT:    [[R:%.*]] = icmp ult i8 [[T]], 46
+; CHECK-NEXT:    ret i1 [[R]]
+;
+  %t = trunc nsw i32 %x to i8
+  %r = icmp ule i8 %t, 45
+  ret i1 %r
+}

>From 42fc67c924dae62e70099f35dbdf3f839d06631c Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sun, 7 Apr 2024 14:01:29 -0500
Subject: [PATCH 2/2] [InstCombine] Fold `(icmp pred (trunc nuw/nsw X), C)` ->
 `(icmp pred X, (zext/sext C))`

This is valid as long as the sign of the wrap flag doesn't differ from
the sign of the `pred`.

Proofs: https://alive2.llvm.org/ce/z/mxWoFd

NB: The online Alive2 hasn't been updated with `trunc nuw/nsw`
support, so the proofs must be reproduced locally.
---
 .../InstCombine/InstCombineCompares.cpp       | 16 ++++++++++++
 .../test/Transforms/InstCombine/icmp-trunc.ll | 26 +++++++------------
 2 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index db302d7e526844..6a3f5ba7b0c1e7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -5705,6 +5705,22 @@ Instruction *InstCombinerImpl::foldICmpWithTrunc(ICmpInst &ICmp) {
   ICmpInst::Predicate Pred = ICmp.getPredicate();
   Value *Op0 = ICmp.getOperand(0), *Op1 = ICmp.getOperand(1);
 
+  // Match (icmp pred (trunc nuw/nsw X), C)
+  // Which we can convert to (icmp pred X, (sext/zext C))
+  // TODO: Maybe this makes sense as a general canonicalization?
+  if (match(Op1, m_ImmConstant())) {
+    if (auto *TI = dyn_cast<TruncInst>(Op0)) {
+      Value *ExtOp0 = TI->getOperand(0);
+      Value *ExtOp1 = nullptr;
+      if (!ICmp.isSigned() && TI->hasNoUnsignedWrap())
+        ExtOp1 = Builder.CreateZExt(Op1, ExtOp0->getType());
+      else if (!ICmp.isUnsigned() && TI->hasNoSignedWrap())
+        ExtOp1 = Builder.CreateSExt(Op1, ExtOp0->getType());
+      if (ExtOp1)
+        return new ICmpInst(Pred, ExtOp0, ExtOp1);
+    }
+  }
+
   // Try to canonicalize trunc + compare-to-constant into a mask + cmp.
   // The trunc masks high bits while the compare may effectively mask low bits.
   Value *X;
diff --git a/llvm/test/Transforms/InstCombine/icmp-trunc.ll b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
index 8732825e937885..13f83ac5db7798 100644
--- a/llvm/test/Transforms/InstCombine/icmp-trunc.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
@@ -563,8 +563,7 @@ define i1 @eq_nuw(i32 %x) {
 ; DL64-NEXT:    ret i1 [[R]]
 ;
 ; DL8-LABEL: @eq_nuw(
-; DL8-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
-; DL8-NEXT:    [[R:%.*]] = icmp eq i8 [[T]], 123
+; DL8-NEXT:    [[R:%.*]] = icmp eq i32 [[X:%.*]], 123
 ; DL8-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nuw i32 %x to i8
@@ -574,8 +573,7 @@ define i1 @eq_nuw(i32 %x) {
 
 define i1 @ult_nuw(i32 %x) {
 ; CHECK-LABEL: @ult_nuw(
-; CHECK-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = icmp ult i8 [[T]], 45
+; CHECK-NEXT:    [[R:%.*]] = icmp ult i32 [[X:%.*]], 45
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nuw i32 %x to i8
@@ -586,7 +584,7 @@ define i1 @ult_nuw(i32 %x) {
 define i1 @ule_nuw(i32 %x) {
 ; CHECK-LABEL: @ule_nuw(
 ; CHECK-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = icmp ult i8 [[T]], 46
+; CHECK-NEXT:    [[R:%.*]] = icmp ult i32 [[X]], 46
 ; CHECK-NEXT:    call void @use(i8 [[T]])
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
@@ -598,8 +596,7 @@ define i1 @ule_nuw(i32 %x) {
 
 define i1 @ugt_nuw(i32 %x) {
 ; CHECK-LABEL: @ugt_nuw(
-; CHECK-NEXT:    [[T:%.*]] = trunc nuw i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = icmp ugt i8 [[T]], 12
+; CHECK-NEXT:    [[R:%.*]] = icmp ugt i32 [[X:%.*]], 12
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nuw i32 %x to i8
@@ -610,7 +607,7 @@ define i1 @ugt_nuw(i32 %x) {
 define i1 @uge_nuw(i48 %x) {
 ; CHECK-LABEL: @uge_nuw(
 ; CHECK-NEXT:    [[T:%.*]] = trunc nuw i48 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = icmp ugt i8 [[T]], 98
+; CHECK-NEXT:    [[R:%.*]] = icmp ugt i48 [[X]], 98
 ; CHECK-NEXT:    call void @use(i8 [[T]])
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
@@ -638,8 +635,7 @@ define i1 @ne_nsw(i32 %x) {
 ; DL64-NEXT:    ret i1 [[R]]
 ;
 ; DL8-LABEL: @ne_nsw(
-; DL8-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
-; DL8-NEXT:    [[R:%.*]] = icmp ne i8 [[T]], 123
+; DL8-NEXT:    [[R:%.*]] = icmp ne i32 [[X:%.*]], 123
 ; DL8-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nsw i32 %x to i8
@@ -649,8 +645,7 @@ define i1 @ne_nsw(i32 %x) {
 
 define i1 @slt_nsw(i32 %x) {
 ; CHECK-LABEL: @slt_nsw(
-; CHECK-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[T]], 45
+; CHECK-NEXT:    [[R:%.*]] = icmp slt i32 [[X:%.*]], 45
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nsw i32 %x to i8
@@ -661,7 +656,7 @@ define i1 @slt_nsw(i32 %x) {
 define i1 @sle_nsw(i32 %x) {
 ; CHECK-LABEL: @sle_nsw(
 ; CHECK-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = icmp slt i8 [[T]], 46
+; CHECK-NEXT:    [[R:%.*]] = icmp slt i32 [[X]], 46
 ; CHECK-NEXT:    call void @use(i8 [[T]])
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
@@ -673,8 +668,7 @@ define i1 @sle_nsw(i32 %x) {
 
 define i1 @sgt_nsw(i32 %x) {
 ; CHECK-LABEL: @sgt_nsw(
-; CHECK-NEXT:    [[T:%.*]] = trunc nsw i32 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[T]], 12
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt i32 [[X:%.*]], 12
 ; CHECK-NEXT:    ret i1 [[R]]
 ;
   %t = trunc nsw i32 %x to i8
@@ -685,7 +679,7 @@ define i1 @sgt_nsw(i32 %x) {
 define i1 @sge_nsw(i48 %x) {
 ; CHECK-LABEL: @sge_nsw(
 ; CHECK-NEXT:    [[T:%.*]] = trunc nsw i48 [[X:%.*]] to i8
-; CHECK-NEXT:    [[R:%.*]] = icmp sgt i8 [[T]], 98
+; CHECK-NEXT:    [[R:%.*]] = icmp sgt i48 [[X]], 98
 ; CHECK-NEXT:    call void @use(i8 [[T]])
 ; CHECK-NEXT:    ret i1 [[R]]
 ;



More information about the llvm-commits mailing list