[llvm] IC: teach foldSelectOpOp about samesign (PR #122723)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 13 07:19:41 PST 2025
https://github.com/artagnon created https://github.com/llvm/llvm-project/pull/122723
Follow up on 4a0d53a (PatternMatch: migrate to CmpPredicate) to get rid of one of the FIXMEs it introduced by replacing a predicate comparison with CmpPredicate::getMatching.
>From 88a2db30a30232a1f91cd33d9c10206c414cf817 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Mon, 13 Jan 2025 14:51:08 +0000
Subject: [PATCH 1/2] IC: pre-commit tests for samesign-getMatching
---
.../test/Transforms/InstCombine/select-cmp.ll | 102 ++++++++++++++++++
1 file changed, 102 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/select-cmp.ll b/llvm/test/Transforms/InstCombine/select-cmp.ll
index f7505bd85f89eb..b0e4891bbcc9f4 100644
--- a/llvm/test/Transforms/InstCombine/select-cmp.ll
+++ b/llvm/test/Transforms/InstCombine/select-cmp.ll
@@ -23,6 +23,18 @@ define i1 @icmp_ne_common_op00(i1 %c, i6 %x, i6 %y, i6 %z) {
ret i1 %r
}
+define i1 @icmp_ne_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
+; CHECK-LABEL: @icmp_ne_samesign_common(
+; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ne i6 [[X:%.*]], [[R_V]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp1 = icmp samesign ne i6 %x, %y
+ %cmp2 = icmp ne i6 %x, %z
+ %r = select i1 %c, i1 %cmp1, i1 %cmp2
+ ret i1 %r
+}
+
define i1 @icmp_ne_common_op01(i1 %c, i3 %x, i3 %y, i3 %z) {
; CHECK-LABEL: @icmp_ne_common_op01(
; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i3 [[Y:%.*]], i3 [[Z:%.*]]
@@ -71,6 +83,18 @@ define i1 @icmp_eq_common_op00(i1 %c, i5 %x, i5 %y, i5 %z) {
ret i1 %r
}
+define i1 @icmp_eq_samesign_common(i1 %c, i5 %x, i5 %y, i5 %z) {
+; CHECK-LABEL: @icmp_eq_samesign_common(
+; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i5 [[Y:%.*]], i5 [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp eq i5 [[X:%.*]], [[R_V]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp1 = icmp eq i5 %x, %y
+ %cmp2 = icmp samesign eq i5 %x, %z
+ %r = select i1 %c, i1 %cmp1, i1 %cmp2
+ ret i1 %r
+}
+
define <5 x i1> @icmp_eq_common_op01(<5 x i1> %c, <5 x i7> %x, <5 x i7> %y, <5 x i7> %z) {
; CHECK-LABEL: @icmp_eq_common_op01(
; CHECK-NEXT: [[R_V:%.*]] = select <5 x i1> [[C:%.*]], <5 x i7> [[Y:%.*]], <5 x i7> [[Z:%.*]]
@@ -134,6 +158,19 @@ define i1 @icmp_slt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
ret i1 %r
}
+define i1 @icmp_slt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
+; CHECK-LABEL: @icmp_slt_samesign_common(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp samesign ult i6 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i6 [[X]], [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp1 = icmp samesign ult i6 %x, %y
+ %cmp2 = icmp slt i6 %x, %z
+ %r = select i1 %c, i1 %cmp1, i1 %cmp2
+ ret i1 %r
+}
+
define i1 @icmp_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_sgt_common(
; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -146,6 +183,19 @@ define i1 @icmp_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
ret i1 %r
}
+define i1 @icmp_sgt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
+; CHECK-LABEL: @icmp_sgt_samesign_common(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp samesign ugt i6 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i6 [[X]], [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp1 = icmp samesign ugt i6 %x, %y
+ %cmp2 = icmp sgt i6 %x, %z
+ %r = select i1 %c, i1 %cmp1, i1 %cmp2
+ ret i1 %r
+}
+
define i1 @icmp_sle_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_sle_common(
; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -158,6 +208,19 @@ define i1 @icmp_sle_common(i1 %c, i6 %x, i6 %y, i6 %z) {
ret i1 %r
}
+define i1 @icmp_sle_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
+; CHECK-LABEL: @icmp_sle_samesign_common(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sle i6 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp samesign ule i6 [[Z:%.*]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp1 = icmp sle i6 %y, %x
+ %cmp2 = icmp samesign ule i6 %z, %x
+ %r = select i1 %c, i1 %cmp1, i1 %cmp2
+ ret i1 %r
+}
+
define i1 @icmp_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_sge_common(
; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -170,6 +233,19 @@ define i1 @icmp_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
ret i1 %r
}
+define i1 @icmp_sge_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
+; CHECK-LABEL: @icmp_sge_samesign_common(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sge i6 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp samesign uge i6 [[Z:%.*]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp1 = icmp sge i6 %y, %x
+ %cmp2 = icmp samesign uge i6 %z, %x
+ %r = select i1 %c, i1 %cmp1, i1 %cmp2
+ ret i1 %r
+}
+
define i1 @icmp_slt_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_slt_sgt_common(
; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -182,6 +258,19 @@ define i1 @icmp_slt_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
ret i1 %r
}
+define i1 @icmp_slt_sgt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
+; CHECK-LABEL: @icmp_slt_sgt_samesign_common(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp samesign ult i6 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i6 [[Z:%.*]], [[X]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp1 = icmp samesign ult i6 %x, %y
+ %cmp2 = icmp sgt i6 %z, %x
+ %r = select i1 %c, i1 %cmp1, i1 %cmp2
+ ret i1 %r
+}
+
define i1 @icmp_sle_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_sle_sge_common(
; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
@@ -194,6 +283,19 @@ define i1 @icmp_sle_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
ret i1 %r
}
+define i1 @icmp_sle_sge_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
+; CHECK-LABEL: @icmp_sle_sge_samesign_common(
+; CHECK-NEXT: [[CMP1:%.*]] = icmp sle i6 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[CMP2:%.*]] = icmp samesign uge i6 [[X]], [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %cmp1 = icmp sle i6 %y, %x
+ %cmp2 = icmp samesign uge i6 %x, %z
+ %r = select i1 %c, i1 %cmp1, i1 %cmp2
+ ret i1 %r
+}
+
define i1 @icmp_ult_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_ult_common(
; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
>From 31c638d96e142ca352e3bc61675e9750131c4ac3 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <ramkumar.ramachandra at codasip.com>
Date: Mon, 13 Jan 2025 15:07:20 +0000
Subject: [PATCH 2/2] IC: teach foldSelectOpOp about samesign
Follow up on 4a0d53a (PatternMatch: migrate to CmpPredicate) to get rid
of one of the FIXMEs it introduced by replacing a predicate comparison
with CmpPredicate::getMatching.
---
.../InstCombine/InstCombineSelect.cpp | 8 ++---
.../test/Transforms/InstCombine/select-cmp.ll | 30 ++++++++-----------
2 files changed, 16 insertions(+), 22 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index f66a976ccb47fe..d5d9a829c3068a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -428,10 +428,10 @@ Instruction *InstCombinerImpl::foldSelectOpOp(SelectInst &SI, Instruction *TI,
CmpPredicate TPred, FPred;
if (match(TI, m_ICmp(TPred, m_Value(), m_Value())) &&
match(FI, m_ICmp(FPred, m_Value(), m_Value()))) {
- // FIXME: Use CmpPredicate::getMatching here.
- CmpInst::Predicate T = TPred, F = FPred;
- if (T == F || T == ICmpInst::getSwappedCmpPredicate(F)) {
- bool Swapped = T != F;
+ bool Swapped = ICmpInst::isRelational(FPred) &&
+ CmpPredicate::getMatching(
+ TPred, ICmpInst::getSwappedCmpPredicate(FPred));
+ if (CmpPredicate::getMatching(TPred, FPred) || Swapped) {
if (Value *MatchOp =
getCommonOp(TI, FI, ICmpInst::isEquality(TPred), Swapped)) {
Value *NewSel = Builder.CreateSelect(Cond, OtherOpT, OtherOpF,
diff --git a/llvm/test/Transforms/InstCombine/select-cmp.ll b/llvm/test/Transforms/InstCombine/select-cmp.ll
index b0e4891bbcc9f4..7e5d5821d9f6a7 100644
--- a/llvm/test/Transforms/InstCombine/select-cmp.ll
+++ b/llvm/test/Transforms/InstCombine/select-cmp.ll
@@ -160,9 +160,8 @@ define i1 @icmp_slt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
define i1 @icmp_slt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_slt_samesign_common(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp samesign ult i6 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[CMP2:%.*]] = icmp slt i6 [[X]], [[Z:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ult i6 [[X:%.*]], [[R_V]]
; CHECK-NEXT: ret i1 [[R]]
;
%cmp1 = icmp samesign ult i6 %x, %y
@@ -185,9 +184,8 @@ define i1 @icmp_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
define i1 @icmp_sgt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_sgt_samesign_common(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp samesign ugt i6 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i6 [[X]], [[Z:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ugt i6 [[X:%.*]], [[R_V]]
; CHECK-NEXT: ret i1 [[R]]
;
%cmp1 = icmp samesign ugt i6 %x, %y
@@ -210,9 +208,8 @@ define i1 @icmp_sle_common(i1 %c, i6 %x, i6 %y, i6 %z) {
define i1 @icmp_sle_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_sle_samesign_common(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp sle i6 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT: [[CMP2:%.*]] = icmp samesign ule i6 [[Z:%.*]], [[X]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp sge i6 [[X:%.*]], [[R_V]]
; CHECK-NEXT: ret i1 [[R]]
;
%cmp1 = icmp sle i6 %y, %x
@@ -235,9 +232,8 @@ define i1 @icmp_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
define i1 @icmp_sge_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_sge_samesign_common(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp sge i6 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT: [[CMP2:%.*]] = icmp samesign uge i6 [[Z:%.*]], [[X]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp sle i6 [[X:%.*]], [[R_V]]
; CHECK-NEXT: ret i1 [[R]]
;
%cmp1 = icmp sge i6 %y, %x
@@ -260,9 +256,8 @@ define i1 @icmp_slt_sgt_common(i1 %c, i6 %x, i6 %y, i6 %z) {
define i1 @icmp_slt_sgt_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_slt_sgt_samesign_common(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp samesign ult i6 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[CMP2:%.*]] = icmp sgt i6 [[Z:%.*]], [[X]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ult i6 [[X:%.*]], [[R_V]]
; CHECK-NEXT: ret i1 [[R]]
;
%cmp1 = icmp samesign ult i6 %x, %y
@@ -285,9 +280,8 @@ define i1 @icmp_sle_sge_common(i1 %c, i6 %x, i6 %y, i6 %z) {
define i1 @icmp_sle_sge_samesign_common(i1 %c, i6 %x, i6 %y, i6 %z) {
; CHECK-LABEL: @icmp_sle_sge_samesign_common(
-; CHECK-NEXT: [[CMP1:%.*]] = icmp sle i6 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT: [[CMP2:%.*]] = icmp samesign uge i6 [[X]], [[Z:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i1 [[CMP1]], i1 [[CMP2]]
+; CHECK-NEXT: [[R_V:%.*]] = select i1 [[C:%.*]], i6 [[Y:%.*]], i6 [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp sge i6 [[X:%.*]], [[R_V]]
; CHECK-NEXT: ret i1 [[R]]
;
%cmp1 = icmp sle i6 %y, %x
More information about the llvm-commits
mailing list