[llvm] [InstCombine][NFC] Add test for existing fold (PR #181555)
Tirthankar Mazumder via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 15 10:21:02 PST 2026
https://github.com/wermos updated https://github.com/llvm/llvm-project/pull/181555
>From 1fbfc02c29e49352ad10c7bedb882b4d77a6d914 Mon Sep 17 00:00:00 2001
From: Tirthankar Mazumder <tmazumder.github at gmail.com>
Date: Sun, 15 Feb 2026 21:07:29 +0530
Subject: [PATCH 1/2] Add test for a fold that is happening, but isn't
specifically tested for.
---
llvm/test/Transforms/InstCombine/icmp-add.ll | 22 ++++++++++++++++++++
1 file changed, 22 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/icmp-add.ll b/llvm/test/Transforms/InstCombine/icmp-add.ll
index 85d01b1786cc9..170ceb9f8ff08 100644
--- a/llvm/test/Transforms/InstCombine/icmp-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-add.ll
@@ -3579,3 +3579,25 @@ define i1 @icmp_ult_add_lshr_neg_no_nuw(i32 %arg0) {
%v2 = icmp ult i32 %v1, 256
ret i1 %v2
}
+
+define i1 @fold-icmp-sum-of-extended-i1(i16 %0, i16 %1, i16 %2, i16 %3) {
+; CHECK-LABEL: @fold-icmp-sum-of-extended-i1(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i16 [[TMP0:%.*]], [[TMP2:%.*]]
+; CHECK-NEXT: [[_0:%.*]] = icmp ult i16 [[TMP1:%.*]], [[TMP3:%.*]]
+; CHECK-NEXT: [[TMP5:%.*]] = icmp slt i16 [[TMP0]], [[TMP2]]
+; CHECK-NEXT: [[_0_0:%.*]] = select i1 [[TMP4]], i1 [[_0]], i1 [[TMP5]]
+; CHECK-NEXT: ret i1 [[_0_0]]
+;
+entry:
+ %lhs = icmp sgt i16 %0, %2
+ %rhs = icmp slt i16 %0, %2
+ %self1 = zext i1 %lhs to i8
+ %rhs2.neg = sext i1 %rhs to i8
+ %diff = add nsw i8 %rhs2.neg, %self1
+ %4 = icmp eq i8 %diff, 0
+ %_0 = icmp ult i16 %1, %3
+ %5 = icmp slt i8 %diff, 0
+ %_0.0 = select i1 %4, i1 %_0, i1 %5
+ ret i1 %_0.0
+}
>From 8a92d12fbe78d87ebd66f54aac0bddf13152431e Mon Sep 17 00:00:00 2001
From: Tirthankar Mazumder <tmazumder.github at gmail.com>
Date: Sun, 15 Feb 2026 23:50:51 +0530
Subject: [PATCH 2/2] Renamed the IR variables to have more descriptive names
and added some explanatory comments to the IR.
---
llvm/test/Transforms/InstCombine/icmp-add.ll | 33 ++++++++++----------
1 file changed, 17 insertions(+), 16 deletions(-)
diff --git a/llvm/test/Transforms/InstCombine/icmp-add.ll b/llvm/test/Transforms/InstCombine/icmp-add.ll
index 170ceb9f8ff08..1b66a50c26e59 100644
--- a/llvm/test/Transforms/InstCombine/icmp-add.ll
+++ b/llvm/test/Transforms/InstCombine/icmp-add.ll
@@ -3580,24 +3580,25 @@ define i1 @icmp_ult_add_lshr_neg_no_nuw(i32 %arg0) {
ret i1 %v2
}
-define i1 @fold-icmp-sum-of-extended-i1(i16 %0, i16 %1, i16 %2, i16 %3) {
+define i1 @fold-icmp-sum-of-extended-i1(i16 %v0, i16 %v1, i16 %v2, i16 %v3) {
; CHECK-LABEL: @fold-icmp-sum-of-extended-i1(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i16 [[TMP0:%.*]], [[TMP2:%.*]]
-; CHECK-NEXT: [[_0:%.*]] = icmp ult i16 [[TMP1:%.*]], [[TMP3:%.*]]
-; CHECK-NEXT: [[TMP5:%.*]] = icmp slt i16 [[TMP0]], [[TMP2]]
-; CHECK-NEXT: [[_0_0:%.*]] = select i1 [[TMP4]], i1 [[_0]], i1 [[TMP5]]
-; CHECK-NEXT: ret i1 [[_0_0]]
+; CHECK-NEXT: [[EQ:%.*]] = icmp eq i16 [[V0:%.*]], [[V2:%.*]]
+; CHECK-NEXT: [[V1_LT_V3:%.*]] = icmp ult i16 [[V1:%.*]], [[V3:%.*]]
+; CHECK-NEXT: [[LESS_THAN:%.*]] = icmp slt i16 [[V0]], [[V2]]
+; CHECK-NEXT: [[RESULT:%.*]] = select i1 [[EQ]], i1 [[V1_LT_V3]], i1 [[LESS_THAN]]
+; CHECK-NEXT: ret i1 [[RESULT]]
;
entry:
- %lhs = icmp sgt i16 %0, %2
- %rhs = icmp slt i16 %0, %2
- %self1 = zext i1 %lhs to i8
- %rhs2.neg = sext i1 %rhs to i8
- %diff = add nsw i8 %rhs2.neg, %self1
- %4 = icmp eq i8 %diff, 0
- %_0 = icmp ult i16 %1, %3
- %5 = icmp slt i8 %diff, 0
- %_0.0 = select i1 %4, i1 %_0, i1 %5
- ret i1 %_0.0
+ ; Determines if (v0, v1) is lexicographically less than (v2, v3)
+ %is_gt = icmp sgt i16 %v0, %v2
+ %is_lt = icmp slt i16 %v0, %v2
+ %is_gt_i8 = zext i1 %is_gt to i8 ; 1 if v0 > v2, 0 otherwise
+ %is_lt_i8_neg = sext i1 %is_lt to i8 ; -1 if v0 < v2, 0 otherwise
+ %cmp3 = add nsw i8 %is_lt_i8_neg, %is_gt_i8 ; result of the three-way comparison of v0 and v2
+ %eq = icmp eq i8 %cmp3, 0
+ %v1_lt_v3 = icmp ult i16 %v1, %v3
+ %less_than = icmp slt i8 %cmp3, 0
+ %result = select i1 %eq, i1 %v1_lt_v3, i1 %less_than
+ ret i1 %result
}
More information about the llvm-commits
mailing list