[llvm] [InstCombine] Fold zext(X) + C2 u< C -> X + trunc(C2) u< trunc(C) (PR #110511)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 30 06:46:51 PDT 2024
https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/110511
Motivating case from https://github.com/torvalds/linux/blob/9852d85ec9d492ebef56dc5f229416c925758edc/drivers/gpu/drm/drm_edid.c#L5238-L5240:
```
define i1 @src(i8 noundef %v13) {
entry:
%conv1 = zext i8 %v13 to i32
%add = add nsw i32 %conv1, -4
%cmp = icmp ult i32 %add, 3
%cmp4 = icmp slt i8 %v13, 4
%cond = select i1 %cmp4, i1 true, i1 %cmp
ret i1 %cond
}
define i1 @tgt(i8 noundef %v13) {
entry:
%cmp4 = icmp slt i8 %v13, 7
ret i1 %cmp4
}
```
Alive2: https://alive2.llvm.org/ce/z/o7VHta
>From 6445856bad110597206833f45be48476c5328869 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 30 Sep 2024 20:52:05 +0800
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests. NFC.
---
llvm/test/Transforms/InstCombine/icmp-add.ll | 96 ++++++++++++++++++++
1 file changed, 96 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/icmp-add.ll b/llvm/test/Transforms/InstCombine/icmp-add.ll
index 0c141d4b8e73aa..61d1b4a3b29264 100644
--- a/llvm/test/Transforms/InstCombine/icmp-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-add.ll
@@ -3183,3 +3183,99 @@ define i1 @icmp_of_ucmp_plus_const_with_const(i32 %x, i32 %y) {
%cmp2 = icmp ult i8 %add, 2
ret i1 %cmp2
}
+
+define i1 @zext_range_check_ult(i8 %x) {
+; CHECK-LABEL: @zext_range_check_ult(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[X:%.*]] to i32
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[CONV]], -4
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 3
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %conv = zext i8 %x to i32
+ %add = add i32 %conv, -4
+ %cmp = icmp ult i32 %add, 3
+ ret i1 %cmp
+}
+
+; TODO: should be canonicalized to (x - 4) u> 2
+define i1 @zext_range_check_ugt(i8 %x) {
+; CHECK-LABEL: @zext_range_check_ugt(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[X:%.*]] to i32
+; CHECK-NEXT: [[TMP0:%.*]] = add nsw i32 [[CONV]], -7
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP0]], -3
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %conv = zext i8 %x to i32
+ %add = add i32 %conv, -4
+ %cmp = icmp ugt i32 %add, 2
+ ret i1 %cmp
+}
+
+; TODO: should be canonicalized to (x - 4) u> 2
+define i1 @zext_range_check_ult_alter(i8 %x) {
+; CHECK-LABEL: @zext_range_check_ult_alter(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[X:%.*]] to i32
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[CONV]], -7
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], -3
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %conv = zext i8 %x to i32
+ %add = add i32 %conv, -7
+ %cmp = icmp ult i32 %add, -3
+ ret i1 %cmp
+}
+
+define i1 @zext_range_check_mergable(i8 %x) {
+; CHECK-LABEL: @zext_range_check_mergable(
+; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[X:%.*]] to i32
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[CONV]], -4
+; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i32 [[ADD]], 3
+; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 [[X]], 4
+; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP2]], i1 true, i1 [[CMP1]]
+; CHECK-NEXT: ret i1 [[COND]]
+;
+ %conv = zext i8 %x to i32
+ %add = add nsw i32 %conv, -4
+ %cmp1 = icmp ult i32 %add, 3
+ %cmp2 = icmp slt i8 %x, 4
+ %cond = select i1 %cmp2, i1 true, i1 %cmp1
+ ret i1 %cond
+}
+
+; Negative tests
+
+define i1 @sext_range_check_ult(i8 %x) {
+; CHECK-LABEL: @sext_range_check_ult(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CONV:%.*]] = sext i8 [[X:%.*]] to i32
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[CONV]], -4
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 3
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %conv = sext i8 %x to i32
+ %add = add i32 %conv, -4
+ %cmp = icmp ult i32 %add, 3
+ ret i1 %cmp
+}
+
+define i1 @zext_range_check_ult_illegal_type(i7 %x) {
+; CHECK-LABEL: @zext_range_check_ult_illegal_type(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CONV:%.*]] = zext i7 [[X:%.*]] to i32
+; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[CONV]], -4
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 3
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+entry:
+ %conv = zext i7 %x to i32
+ %add = add i32 %conv, -4
+ %cmp = icmp ult i32 %add, 3
+ ret i1 %cmp
+}
>From ad6af44f3988a5292c07776a776c430153f9cdd0 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 30 Sep 2024 21:31:02 +0800
Subject: [PATCH 2/2] [InstCombine] Fold `zext(X) + C2 u< C -> X + trunc(C2) u<
trunc(C)`
---
.../InstCombine/InstCombineCompares.cpp | 20 +++++++++++++++++++
llvm/test/Transforms/InstCombine/icmp-add.ll | 11 +++-------
2 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index e3f4925024e65c..aecb4f21b4d49f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -3165,6 +3165,26 @@ Instruction *InstCombinerImpl::foldICmpAddConstant(ICmpInst &Cmp,
Builder.CreateAdd(X, ConstantInt::get(Ty, *C2 - C - 1)),
ConstantInt::get(Ty, ~C));
+ // zext(V) + C2 <u C -> V + trunc(C2) <u trunc(C) iff C2 s<0 && C s>0
+ Value *V;
+ if (Pred == ICmpInst::ICMP_ULT && match(X, m_ZExt(m_Value(V)))) {
+ Type *NewCmpTy = V->getType();
+ unsigned NewCmpBW = NewCmpTy->getScalarSizeInBits();
+ if (shouldChangeType(Ty, NewCmpTy) &&
+ C2->getSignificantBits() <= NewCmpBW &&
+ C.getSignificantBits() <= NewCmpBW) {
+ APInt TruncatedOffset = C2->trunc(NewCmpBW);
+ APInt TruncatedRHS = C.trunc(NewCmpBW);
+ if (TruncatedOffset.isNegative() && TruncatedRHS.isNonNegative()) {
+ Value *TruncatedOffsetV = ConstantInt::get(NewCmpTy, TruncatedOffset);
+ Value *TruncatedRV = ConstantInt::get(NewCmpTy, TruncatedRHS);
+ return new ICmpInst(ICmpInst::ICMP_ULT,
+ Builder.CreateAdd(V, TruncatedOffsetV),
+ TruncatedRV);
+ }
+ }
+ }
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/icmp-add.ll b/llvm/test/Transforms/InstCombine/icmp-add.ll
index 61d1b4a3b29264..2239e48468ee04 100644
--- a/llvm/test/Transforms/InstCombine/icmp-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-add.ll
@@ -3187,9 +3187,8 @@ define i1 @icmp_of_ucmp_plus_const_with_const(i32 %x, i32 %y) {
define i1 @zext_range_check_ult(i8 %x) {
; CHECK-LABEL: @zext_range_check_ult(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[X:%.*]] to i32
-; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[CONV]], -4
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[ADD]], 3
+; CHECK-NEXT: [[TMP0:%.*]] = add i8 [[X:%.*]], -4
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[TMP0]], 3
; CHECK-NEXT: ret i1 [[CMP]]
;
entry:
@@ -3233,11 +3232,7 @@ entry:
define i1 @zext_range_check_mergable(i8 %x) {
; CHECK-LABEL: @zext_range_check_mergable(
-; CHECK-NEXT: [[CONV:%.*]] = zext i8 [[X:%.*]] to i32
-; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[CONV]], -4
-; CHECK-NEXT: [[CMP1:%.*]] = icmp ult i32 [[ADD]], 3
-; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i8 [[X]], 4
-; CHECK-NEXT: [[COND:%.*]] = select i1 [[CMP2]], i1 true, i1 [[CMP1]]
+; CHECK-NEXT: [[COND:%.*]] = icmp slt i8 [[X:%.*]], 7
; CHECK-NEXT: ret i1 [[COND]]
;
%conv = zext i8 %x to i32
More information about the llvm-commits
mailing list