[llvm] [InstCombine] Fold fcmp into select (PR #86482)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 22 01:26:37 PDT 2024
https://github.com/dtcxzyw updated https://github.com/llvm/llvm-project/pull/86482
>From 97112e2f086ce19ae60dd0574b737b92ea52e173 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 25 Mar 2024 18:19:49 +0800
Subject: [PATCH 1/4] [InstCombine] Add pre-commit tests. NFC.
---
.../Transforms/InstCombine/fcmp-select.ll | 69 +++++++++++++++++++
1 file changed, 69 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/fcmp-select.ll b/llvm/test/Transforms/InstCombine/fcmp-select.ll
index f37c586845b12d..56c7ff0143921a 100644
--- a/llvm/test/Transforms/InstCombine/fcmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp-select.ll
@@ -148,3 +148,72 @@ define i1 @fcmp_ogt_select(i1 %cond, float %a, float %b) {
%res = fcmp ogt float %lhs, %rhs
ret i1 %res
}
+
+define i1 @test_fcmp_select_const_const(double %x) {
+; CHECK-LABEL: @test_fcmp_select_const_const(
+; CHECK-NEXT: [[CMP2:%.*]] = fcmp ord double [[X:%.*]], 0.000000e+00
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP2]], double 0xFFFFFFFFFFFFFFFF, double 0.000000e+00
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[SEL]], 0.000000e+00
+; CHECK-NEXT: ret i1 [[CMP1]]
+;
+ %cmp1 = fcmp ord double %x, 0.000000e+00
+ %sel = select i1 %cmp1, double 0xFFFFFFFFFFFFFFFF, double 0.000000e+00
+ %cmp2 = fcmp oeq double %sel, 0.000000e+00
+ ret i1 %cmp2
+}
+
+define i1 @test_fcmp_select_var_const(double %x, double %y) {
+; CHECK-LABEL: @test_fcmp_select_var_const(
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp ogt double [[X:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[Y:%.*]] = select i1 [[CMP1]], double [[Y1:%.*]], double 0.000000e+00
+; CHECK-NEXT: [[TMP1:%.*]] = fcmp olt double [[Y]], 0x3E80000000000000
+; CHECK-NEXT: ret i1 [[TMP1]]
+;
+ %cmp1 = fcmp ogt double %x, 0x3E80000000000000
+ %sel = select i1 %cmp1, double %y, double 0.000000e+00
+ %cmp2 = fcmp olt double %sel, 0x3E80000000000000
+ ret i1 %cmp2
+}
+
+define i1 @test_fcmp_select_var_const_fmf(double %x, double %y) {
+; CHECK-LABEL: @test_fcmp_select_var_const_fmf(
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp ogt double [[X:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], double [[Y:%.*]], double 0.000000e+00
+; CHECK-NEXT: [[CMP2:%.*]] = fcmp nnan olt double [[SEL]], 0x3E80000000000000
+; CHECK-NEXT: ret i1 [[CMP2]]
+;
+ %cmp1 = fcmp ogt double %x, 0x3E80000000000000
+ %sel = select i1 %cmp1, double %y, double 0.000000e+00
+ %cmp2 = fcmp nnan olt double %sel, 0x3E80000000000000
+ ret i1 %cmp2
+}
+
+define <2 x i1> @test_fcmp_select_const_const_vec(<2 x double> %x) {
+; CHECK-LABEL: @test_fcmp_select_const_const_vec(
+; CHECK-NEXT: [[CMP2:%.*]] = fcmp ord <2 x double> [[X:%.*]], zeroinitializer
+; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[CMP2]], <2 x double> <double 0xFFFFFFFFFFFFFFFF, double 0xFFFFFFFFFFFFFFFF>, <2 x double> zeroinitializer
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq <2 x double> [[SEL]], zeroinitializer
+; CHECK-NEXT: ret <2 x i1> [[CMP1]]
+;
+ %cmp1 = fcmp ord <2 x double> %x, zeroinitializer
+ %sel = select <2 x i1> %cmp1, <2 x double> <double 0xFFFFFFFFFFFFFFFF, double 0xFFFFFFFFFFFFFFFF>, <2 x double> zeroinitializer
+ %cmp2 = fcmp oeq <2 x double> %sel, zeroinitializer
+ ret <2 x i1> %cmp2
+}
+
+; Don't break clamp idioms
+
+define double @test_fcmp_select_clamp(double %x) {
+; CHECK-LABEL: @test_fcmp_select_clamp(
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp ogt double [[X:%.*]], 9.000000e-01
+; CHECK-NEXT: [[SEL1:%.*]] = select i1 [[CMP1]], double 9.000000e-01, double [[X]]
+; CHECK-NEXT: [[CMP2:%.*]] = fcmp olt double [[SEL1]], 5.000000e-01
+; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[CMP2]], double 5.000000e-01, double [[SEL1]]
+; CHECK-NEXT: ret double [[SEL2]]
+;
+ %cmp1 = fcmp ogt double %x, 9.000000e-01
+ %sel1 = select i1 %cmp1, double 9.000000e-01, double %x
+ %cmp2 = fcmp olt double %sel1, 5.000000e-01
+ %sel2 = select i1 %cmp2, double 5.000000e-01, double %sel1
+ ret double %sel2
+}
>From d17041affd0c67517e3dd406d9bf90acb286f4e0 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 25 Mar 2024 18:20:22 +0800
Subject: [PATCH 2/4] [InstCombine] Fold fcmp into select
---
.../InstCombine/InstCombineCompares.cpp | 2 ++
.../Transforms/InstCombine/fcmp-select.ll | 24 ++++++++-----------
.../Transforms/InstCombine/select-select.ll | 7 +++---
3 files changed, 15 insertions(+), 18 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index f0278f207549fa..c2062adcd5b7f7 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -8052,6 +8052,8 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
m_Select(m_Value(), m_Value(X), m_FNeg(m_Deferred(X)))) ||
match(LHSI, m_Select(m_Value(), m_FNeg(m_Value(X)), m_Deferred(X)))))
return replaceOperand(I, 0, X);
+ if (Instruction *NV = FoldOpIntoSelect(I, cast<SelectInst>(LHSI)))
+ return NV;
break;
case Instruction::PHI:
if (Instruction *NV = foldOpIntoPhi(I, cast<PHINode>(LHSI)))
diff --git a/llvm/test/Transforms/InstCombine/fcmp-select.ll b/llvm/test/Transforms/InstCombine/fcmp-select.ll
index 56c7ff0143921a..62fad8349d22e2 100644
--- a/llvm/test/Transforms/InstCombine/fcmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp-select.ll
@@ -151,9 +151,7 @@ define i1 @fcmp_ogt_select(i1 %cond, float %a, float %b) {
define i1 @test_fcmp_select_const_const(double %x) {
; CHECK-LABEL: @test_fcmp_select_const_const(
-; CHECK-NEXT: [[CMP2:%.*]] = fcmp ord double [[X:%.*]], 0.000000e+00
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP2]], double 0xFFFFFFFFFFFFFFFF, double 0.000000e+00
-; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[SEL]], 0.000000e+00
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp uno double [[X:%.*]], 0.000000e+00
; CHECK-NEXT: ret i1 [[CMP1]]
;
%cmp1 = fcmp ord double %x, 0.000000e+00
@@ -164,10 +162,10 @@ define i1 @test_fcmp_select_const_const(double %x) {
define i1 @test_fcmp_select_var_const(double %x, double %y) {
; CHECK-LABEL: @test_fcmp_select_var_const(
-; CHECK-NEXT: [[CMP1:%.*]] = fcmp ogt double [[X:%.*]], 0x3E80000000000000
-; CHECK-NEXT: [[Y:%.*]] = select i1 [[CMP1]], double [[Y1:%.*]], double 0.000000e+00
-; CHECK-NEXT: [[TMP1:%.*]] = fcmp olt double [[Y]], 0x3E80000000000000
-; CHECK-NEXT: ret i1 [[TMP1]]
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp ule double [[X:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[TMP1:%.*]] = fcmp olt double [[Y:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[CMP2:%.*]] = select i1 [[CMP1]], i1 true, i1 [[TMP1]]
+; CHECK-NEXT: ret i1 [[CMP2]]
;
%cmp1 = fcmp ogt double %x, 0x3E80000000000000
%sel = select i1 %cmp1, double %y, double 0.000000e+00
@@ -177,10 +175,10 @@ define i1 @test_fcmp_select_var_const(double %x, double %y) {
define i1 @test_fcmp_select_var_const_fmf(double %x, double %y) {
; CHECK-LABEL: @test_fcmp_select_var_const_fmf(
-; CHECK-NEXT: [[CMP1:%.*]] = fcmp ogt double [[X:%.*]], 0x3E80000000000000
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], double [[Y:%.*]], double 0.000000e+00
-; CHECK-NEXT: [[CMP2:%.*]] = fcmp nnan olt double [[SEL]], 0x3E80000000000000
-; CHECK-NEXT: ret i1 [[CMP2]]
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp ule double [[X:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[CMP2:%.*]] = fcmp nnan olt double [[SEL:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[CMP3:%.*]] = select i1 [[CMP1]], i1 true, i1 [[CMP2]]
+; CHECK-NEXT: ret i1 [[CMP3]]
;
%cmp1 = fcmp ogt double %x, 0x3E80000000000000
%sel = select i1 %cmp1, double %y, double 0.000000e+00
@@ -190,9 +188,7 @@ define i1 @test_fcmp_select_var_const_fmf(double %x, double %y) {
define <2 x i1> @test_fcmp_select_const_const_vec(<2 x double> %x) {
; CHECK-LABEL: @test_fcmp_select_const_const_vec(
-; CHECK-NEXT: [[CMP2:%.*]] = fcmp ord <2 x double> [[X:%.*]], zeroinitializer
-; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[CMP2]], <2 x double> <double 0xFFFFFFFFFFFFFFFF, double 0xFFFFFFFFFFFFFFFF>, <2 x double> zeroinitializer
-; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq <2 x double> [[SEL]], zeroinitializer
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp uno <2 x double> [[X:%.*]], zeroinitializer
; CHECK-NEXT: ret <2 x i1> [[CMP1]]
;
%cmp1 = fcmp ord <2 x double> %x, zeroinitializer
diff --git a/llvm/test/Transforms/InstCombine/select-select.ll b/llvm/test/Transforms/InstCombine/select-select.ll
index 785766136fb71b..84fe973093e327 100644
--- a/llvm/test/Transforms/InstCombine/select-select.ll
+++ b/llvm/test/Transforms/InstCombine/select-select.ll
@@ -18,11 +18,10 @@ define float @foo1(float %a) {
define float @foo2(float %a) {
; CHECK-LABEL: @foo2(
-; CHECK-NEXT: [[B:%.*]] = fcmp ogt float [[A:%.*]], 0.000000e+00
-; CHECK-NEXT: [[C:%.*]] = select i1 [[B]], float [[A]], float 0.000000e+00
+; CHECK-NEXT: [[B:%.*]] = fcmp ule float [[C:%.*]], 0.000000e+00
; CHECK-NEXT: [[D:%.*]] = fcmp olt float [[C]], 1.000000e+00
-; CHECK-NEXT: [[E:%.*]] = select i1 [[B]], float [[A]], float 0.000000e+00
-; CHECK-NEXT: [[F:%.*]] = select i1 [[D]], float [[E]], float 1.000000e+00
+; CHECK-NEXT: [[E:%.*]] = select i1 [[D]], float [[C]], float 1.000000e+00
+; CHECK-NEXT: [[F:%.*]] = select i1 [[B]], float 0.000000e+00, float [[E]]
; CHECK-NEXT: ret float [[F]]
;
%b = fcmp ogt float %a, 0.0
>From 09fd2677b684e32da657271155863afa31b0df12 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 22 Apr 2024 14:01:49 +0800
Subject: [PATCH 3/4] [InstCombine] Add additional tests. NFC.
---
.../Transforms/InstCombine/fcmp-select.ll | 37 +++++++++++++++++--
1 file changed, 34 insertions(+), 3 deletions(-)
diff --git a/llvm/test/Transforms/InstCombine/fcmp-select.ll b/llvm/test/Transforms/InstCombine/fcmp-select.ll
index 62fad8349d22e2..601a60ff80cbac 100644
--- a/llvm/test/Transforms/InstCombine/fcmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp-select.ll
@@ -2,6 +2,7 @@
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
declare void @use(i1)
+declare void @usef64(double)
; X == 42.0 ? X : 42.0 --> 42.0
@@ -176,9 +177,9 @@ define i1 @test_fcmp_select_var_const(double %x, double %y) {
define i1 @test_fcmp_select_var_const_fmf(double %x, double %y) {
; CHECK-LABEL: @test_fcmp_select_var_const_fmf(
; CHECK-NEXT: [[CMP1:%.*]] = fcmp ule double [[X:%.*]], 0x3E80000000000000
-; CHECK-NEXT: [[CMP2:%.*]] = fcmp nnan olt double [[SEL:%.*]], 0x3E80000000000000
-; CHECK-NEXT: [[CMP3:%.*]] = select i1 [[CMP1]], i1 true, i1 [[CMP2]]
-; CHECK-NEXT: ret i1 [[CMP3]]
+; CHECK-NEXT: [[TMP1:%.*]] = fcmp nnan olt double [[Y:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[CMP2:%.*]] = select i1 [[CMP1]], i1 true, i1 [[TMP1]]
+; CHECK-NEXT: ret i1 [[CMP2]]
;
%cmp1 = fcmp ogt double %x, 0x3E80000000000000
%sel = select i1 %cmp1, double %y, double 0.000000e+00
@@ -213,3 +214,33 @@ define double @test_fcmp_select_clamp(double %x) {
%sel2 = select i1 %cmp2, double 5.000000e-01, double %sel1
ret double %sel2
}
+
+; Don't break fmin/fmax idioms
+
+define double @test_fcmp_select_maxnum(double %x) {
+; CHECK-LABEL: @test_fcmp_select_maxnum(
+; CHECK-NEXT: [[SEL1:%.*]] = call nnan nsz double @llvm.maxnum.f64(double [[X:%.*]], double 1.000000e+00)
+; CHECK-NEXT: [[SEL2:%.*]] = call nnan nsz double @llvm.minnum.f64(double [[SEL1]], double 2.550000e+02)
+; CHECK-NEXT: ret double [[SEL2]]
+;
+ %cmp1 = fcmp ogt double %x, 1.0
+ %sel1 = select nnan nsz i1 %cmp1, double %x, double 1.0
+ %cmp2 = fcmp olt double %sel1, 255.0
+ %sel2 = select nnan nsz i1 %cmp2, double %sel1, double 255.0
+ ret double %sel2
+}
+
+define i1 @test_fcmp_select_const_const_multiuse(double %x) {
+; CHECK-LABEL: @test_fcmp_select_const_const_multiuse(
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp ord double [[X:%.*]], 0.000000e+00
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP1]], double 0xFFFFFFFFFFFFFFFF, double 0.000000e+00
+; CHECK-NEXT: call void @usef64(double [[SEL]])
+; CHECK-NEXT: [[CMP2:%.*]] = fcmp oeq double [[SEL]], 0.000000e+00
+; CHECK-NEXT: ret i1 [[CMP2]]
+;
+ %cmp1 = fcmp ord double %x, 0.000000e+00
+ %sel = select i1 %cmp1, double 0xFFFFFFFFFFFFFFFF, double 0.000000e+00
+ call void @usef64(double %sel)
+ %cmp2 = fcmp oeq double %sel, 0.000000e+00
+ ret i1 %cmp2
+}
>From faac6c48f2259d9473133d11f91fc725a54ba8a2 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Mon, 22 Apr 2024 16:17:22 +0800
Subject: [PATCH 4/4] [InstCombine] Add additional unordered tests. NFC.
---
.../Transforms/InstCombine/fcmp-select.ll | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/fcmp-select.ll b/llvm/test/Transforms/InstCombine/fcmp-select.ll
index 601a60ff80cbac..028de1ff8a99fa 100644
--- a/llvm/test/Transforms/InstCombine/fcmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp-select.ll
@@ -244,3 +244,27 @@ define i1 @test_fcmp_select_const_const_multiuse(double %x) {
%cmp2 = fcmp oeq double %sel, 0.000000e+00
ret i1 %cmp2
}
+
+define i1 @test_fcmp_select_const_const_unordered(double %x) {
+; CHECK-LABEL: @test_fcmp_select_const_const_unordered(
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp ord double [[X:%.*]], 0.000000e+00
+; CHECK-NEXT: ret i1 [[CMP1]]
+;
+ %cmp1 = fcmp uno double %x, 0.000000e+00
+ %sel = select i1 %cmp1, double 0xFFFFFFFFFFFFFFFF, double 0.000000e+00
+ %cmp2 = fcmp oeq double %sel, 0.000000e+00
+ ret i1 %cmp2
+}
+
+define i1 @test_fcmp_select_var_const_unordered(double %x, double %y) {
+; CHECK-LABEL: @test_fcmp_select_var_const_unordered(
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp ult double [[X:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[TMP1:%.*]] = fcmp ugt double [[Y:%.*]], 0x3E80000000000000
+; CHECK-NEXT: [[CMP2:%.*]] = select i1 [[CMP1]], i1 [[TMP1]], i1 false
+; CHECK-NEXT: ret i1 [[CMP2]]
+;
+ %cmp1 = fcmp ult double %x, 0x3E80000000000000
+ %sel = select i1 %cmp1, double %y, double 0.000000e+00
+ %cmp2 = fcmp ugt double %sel, 0x3E80000000000000
+ ret i1 %cmp2
+}
More information about the llvm-commits
mailing list