[llvm] [InstCombine] Fold `(icmp pred (trunc nuw/nsw X), C)` -> `(icmp pred X, (zext/sext C))` (PR #87935)
via llvm-commits
llvm-commits at lists.llvm.org
Thu May 2 13:00:21 PDT 2024
https://github.com/goldsteinn updated https://github.com/llvm/llvm-project/pull/87935
>From e8de7276a77a092c361a491d1f23095e1ef625cb 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 | 164 ++++++++++++++++++
1 file changed, 164 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/icmp-trunc.ll b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
index b2de9dddb21947..25ae21cfa1f04f 100644
--- a/llvm/test/Transforms/InstCombine/icmp-trunc.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
@@ -555,3 +555,167 @@ 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 <2 x i1> @uge_nsw(<2 x i32> %x) {
+; CHECK-LABEL: @uge_nsw(
+; CHECK-NEXT: [[T:%.*]] = trunc nsw <2 x i32> [[X:%.*]] to <2 x i8>
+; CHECK-NEXT: [[R:%.*]] = icmp ugt <2 x i8> [[T]], <i8 44, i8 44>
+; CHECK-NEXT: ret <2 x i1> [[R]]
+;
+ %t = trunc nsw <2 x i32> %x to <2 x i8>
+ %r = icmp uge <2 x i8> %t, <i8 45, i8 45>
+ ret <2 x i1> %r
+}
+
+
+define <2 x i1> @uge_nsw_non_splat(<2 x i32> %x) {
+; CHECK-LABEL: @uge_nsw_non_splat(
+; CHECK-NEXT: [[T:%.*]] = trunc nsw <2 x i32> [[X:%.*]] to <2 x i8>
+; CHECK-NEXT: [[R:%.*]] = icmp ugt <2 x i8> [[T]], <i8 44, i8 45>
+; CHECK-NEXT: ret <2 x i1> [[R]]
+;
+ %t = trunc nsw <2 x i32> %x to <2 x i8>
+ %r = icmp uge <2 x i8> %t, <i8 45, i8 46>
+ ret <2 x i1> %r
+}
+
>From 3f3b660f56b523ac9197f0f82b85ef145b95d2a6 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/35NsrR
NB: The online Alive2 hasn't been updated with `trunc nuw/nsw`
support, so the proofs must be reproduced locally.
---
.../InstCombine/InstCombineCompares.cpp | 15 ++++--
.../Transforms/InstCombine/cmp-intrinsic.ll | 4 +-
.../test/Transforms/InstCombine/icmp-trunc.ll | 47 ++++++-------------
.../test/Transforms/InstCombine/known-bits.ll | 3 +-
4 files changed, 29 insertions(+), 40 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index f66883de8dd583..e48969c341a3b7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -1409,6 +1409,17 @@ Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
const APInt &C) {
ICmpInst::Predicate Pred = Cmp.getPredicate();
Value *X = Trunc->getOperand(0);
+ Type *SrcTy = X->getType();
+ unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
+ SrcBits = SrcTy->getScalarSizeInBits();
+
+ // Match (icmp pred (trunc nuw/nsw X), C)
+ // Which we can convert to (icmp pred X, (sext/zext C))
+ if (!Cmp.isSigned() && Trunc->hasNoUnsignedWrap())
+ return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.zext(SrcBits)));
+ if (Trunc->hasNoSignedWrap())
+ return new ICmpInst(Pred, X, ConstantInt::get(SrcTy, C.sext(SrcBits)));
+
if (C.isOne() && C.getBitWidth() > 1) {
// icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
Value *V = nullptr;
@@ -1417,10 +1428,6 @@ Instruction *InstCombinerImpl::foldICmpTruncConstant(ICmpInst &Cmp,
ConstantInt::get(V->getType(), 1));
}
- Type *SrcTy = X->getType();
- unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
- SrcBits = SrcTy->getScalarSizeInBits();
-
// TODO: Handle any shifted constant by subtracting trailing zeros.
// TODO: Handle non-equality predicates.
Value *Y;
diff --git a/llvm/test/Transforms/InstCombine/cmp-intrinsic.ll b/llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
index 9a9f359fa80b4a..097a33fadd90c3 100644
--- a/llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
+++ b/llvm/test/Transforms/InstCombine/cmp-intrinsic.ll
@@ -620,7 +620,7 @@ define i1 @trunc_cttz_false_ult_other_i32_i6_extra_use(i32 %x) {
; CHECK-NEXT: [[TZ:%.*]] = tail call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 false)
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i32 [[TZ]] to i6
; CHECK-NEXT: call void @use6(i6 [[TRUNC]])
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i6 [[TRUNC]], 7
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TZ]], 7
; CHECK-NEXT: ret i1 [[CMP]]
;
%tz = tail call i32 @llvm.cttz.i32(i32 %x, i1 false)
@@ -722,7 +722,7 @@ define i1 @trunc_ctlz_false_ugt_other_i32_i6_extra_use(i32 %x) {
; CHECK-NEXT: [[LZ:%.*]] = tail call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 [[X:%.*]], i1 false)
; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i32 [[LZ]] to i6
; CHECK-NEXT: call void @use6(i6 [[TRUNC]])
-; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i6 [[TRUNC]], 4
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X]], 134217728
; CHECK-NEXT: ret i1 [[CMP]]
;
%lz = tail call i32 @llvm.ctlz.i32(i32 %x, i1 false)
diff --git a/llvm/test/Transforms/InstCombine/icmp-trunc.ll b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
index 25ae21cfa1f04f..00f2cd4387f47e 100644
--- a/llvm/test/Transforms/InstCombine/icmp-trunc.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-trunc.ll
@@ -557,15 +557,9 @@ define i1 @shl1_trunc_sgt4(i32 %a) {
}
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]]
+; CHECK-LABEL: @eq_nuw(
+; CHECK-NEXT: [[R:%.*]] = icmp eq i32 [[X:%.*]], 123
+; CHECK-NEXT: ret i1 [[R]]
;
%t = trunc nuw i32 %x to i8
%r = icmp eq i8 %t, 123
@@ -574,8 +568,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 +579,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 +591,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 +602,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]]
;
@@ -632,15 +624,9 @@ define i1 @sgt_nuw_fail(i32 %x) {
}
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]]
+; CHECK-LABEL: @ne_nsw(
+; CHECK-NEXT: [[R:%.*]] = icmp ne i32 [[X:%.*]], 123
+; CHECK-NEXT: ret i1 [[R]]
;
%t = trunc nsw i32 %x to i8
%r = icmp ne i8 %t, 123
@@ -649,8 +635,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 +646,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 +658,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 +669,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]]
;
@@ -698,8 +682,7 @@ define i1 @sge_nsw(i48 %x) {
define <2 x i1> @uge_nsw(<2 x i32> %x) {
; CHECK-LABEL: @uge_nsw(
-; CHECK-NEXT: [[T:%.*]] = trunc nsw <2 x i32> [[X:%.*]] to <2 x i8>
-; CHECK-NEXT: [[R:%.*]] = icmp ugt <2 x i8> [[T]], <i8 44, i8 44>
+; CHECK-NEXT: [[R:%.*]] = icmp ugt <2 x i32> [[X:%.*]], <i32 44, i32 44>
; CHECK-NEXT: ret <2 x i1> [[R]]
;
%t = trunc nsw <2 x i32> %x to <2 x i8>
diff --git a/llvm/test/Transforms/InstCombine/known-bits.ll b/llvm/test/Transforms/InstCombine/known-bits.ll
index 8b4249b2c25a92..4f9cb3a5b1aeaf 100644
--- a/llvm/test/Transforms/InstCombine/known-bits.ll
+++ b/llvm/test/Transforms/InstCombine/known-bits.ll
@@ -455,8 +455,7 @@ define i64 @test_icmp_trunc5(i64 %n) {
; CHECK-LABEL: @test_icmp_trunc5(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[SHR:%.*]] = ashr i64 [[N:%.*]], 47
-; CHECK-NEXT: [[CONV1:%.*]] = trunc nsw i64 [[SHR]] to i32
-; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[CONV1]], -13
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i64 [[SHR]], -13
; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.then:
; CHECK-NEXT: [[TMP0:%.*]] = and i64 [[SHR]], 15
More information about the llvm-commits
mailing list