[llvm] [InstCombine] Relax guard against FP min/max in select fold (PR #143144)
Acthink Yang via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 6 09:00:48 PDT 2025
https://github.com/Acthink-Yang updated https://github.com/llvm/llvm-project/pull/143144
>From 76ec6929ccfcb12455f08c4dd352792f42fbf969 Mon Sep 17 00:00:00 2001
From: QiYue <yangzhh at mail.ustc.edu.cn>
Date: Wed, 4 Jun 2025 19:27:58 +0800
Subject: [PATCH 1/3] [InstCombine] Relax guard against FP min/max in select
fold
FCmp's equality and commutativity predicates do not work with min/max semantics
Closes #142711
---
.../Transforms/InstCombine/InstructionCombining.cpp | 3 ++-
llvm/test/Transforms/InstCombine/fcmp-select.ll | 11 +++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 439a86d951a83..c85abd9b26d61 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1729,7 +1729,8 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
if (auto *CI = dyn_cast<FCmpInst>(SI->getCondition())) {
if (CI->hasOneUse()) {
Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
- if ((TV == Op0 && FV == Op1) || (FV == Op0 && TV == Op1))
+ if (((TV == Op0 && FV == Op1) || (FV == Op0 && TV == Op1)) &&
+ !CI->isEquality() && !CI->isCommutative())
return nullptr;
}
}
diff --git a/llvm/test/Transforms/InstCombine/fcmp-select.ll b/llvm/test/Transforms/InstCombine/fcmp-select.ll
index 408bc1cdc268f..bf08fbfc27302 100644
--- a/llvm/test/Transforms/InstCombine/fcmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp-select.ll
@@ -268,3 +268,14 @@ define i1 @test_fcmp_select_var_const_unordered(double %x, double %y) {
%cmp2 = fcmp ugt double %sel, 0x3E80000000000000
ret i1 %cmp2
}
+
+define i1 @test_fcmp_ord_select_fcmp_oeq_var_const(double %x) {
+; CHECK-LABEL: @test_fcmp_ord_select_fcmp_oeq_var_const(
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[X:%.*]], 0.000000e+00
+; CHECK-NEXT: ret i1 [[CMP1]]
+;
+ %cmp1 = fcmp ord double %x, 0.000000e+00
+ %sel = select i1 %cmp1, double %x, double 0.000000e+00
+ %cmp2 = fcmp oeq double %sel, 1.000000e+00
+ ret i1 %cmp2
+}
>From a4fb76cdc1b7f45b2dc533b34cb7c34df081cfbf Mon Sep 17 00:00:00 2001
From: Acthink Yang <yangzhh at mail.ustc.edu.cn>
Date: Fri, 6 Jun 2025 22:32:02 +0800
Subject: [PATCH 2/3] remove isEquality
Co-authored-by: Yingwei Zheng <dtcxzyw at qq.com>
---
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index c85abd9b26d61..e261807bbc035 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1730,7 +1730,7 @@ Instruction *InstCombinerImpl::FoldOpIntoSelect(Instruction &Op, SelectInst *SI,
if (CI->hasOneUse()) {
Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
if (((TV == Op0 && FV == Op1) || (FV == Op0 && TV == Op1)) &&
- !CI->isEquality() && !CI->isCommutative())
+ !CI->isCommutative())
return nullptr;
}
}
>From 8f2cdcdb37afd9dbf9f093dc4ea24953e4c8bcae Mon Sep 17 00:00:00 2001
From: Acthink Yang <yangzhh at mail.ustc.edu.cn>
Date: Sat, 7 Jun 2025 00:00:38 +0800
Subject: [PATCH 3/3] Update llvm/test/Transforms/InstCombine/fcmp-select.ll
Co-authored-by: Yingwei Zheng <dtcxzyw at qq.com>
---
llvm/test/Transforms/InstCombine/fcmp-select.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/Transforms/InstCombine/fcmp-select.ll b/llvm/test/Transforms/InstCombine/fcmp-select.ll
index bf08fbfc27302..053b233cb5f04 100644
--- a/llvm/test/Transforms/InstCombine/fcmp-select.ll
+++ b/llvm/test/Transforms/InstCombine/fcmp-select.ll
@@ -271,7 +271,7 @@ define i1 @test_fcmp_select_var_const_unordered(double %x, double %y) {
define i1 @test_fcmp_ord_select_fcmp_oeq_var_const(double %x) {
; CHECK-LABEL: @test_fcmp_ord_select_fcmp_oeq_var_const(
-; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[X:%.*]], 0.000000e+00
+; CHECK-NEXT: [[CMP1:%.*]] = fcmp oeq double [[X:%.*]], 1.000000e+00
; CHECK-NEXT: ret i1 [[CMP1]]
;
%cmp1 = fcmp ord double %x, 0.000000e+00
More information about the llvm-commits
mailing list