Skip to content

[InstCombine] Use canIgnoreSignBitOfZero in spf->minmax fold #141914

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2025

Conversation

dtcxzyw
Copy link
Member

@dtcxzyw dtcxzyw commented May 29, 2025

Alive2: https://alive2.llvm.org/ce/z/dCZBB_
Fix remaining regressions caused by #141010.

@llvmbot
Copy link
Member

llvmbot commented May 29, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Yingwei Zheng (dtcxzyw)

Changes

Alive2: https://alive2.llvm.org/ce/z/dCZBB_
Fix remaining regressions caused by #141010.


Full diff: https://github.com/llvm/llvm-project/pull/141914.diff

3 Files Affected:

  • (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+4-1)
  • (modified) llvm/test/Transforms/InstCombine/minmax-fp.ll (+1-2)
  • (added) minmax ()
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 3882d4cb59e01..a791fc5db6698 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -3929,7 +3929,10 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
 
     // Canonicalize select of FP values where NaN and -0.0 are not valid as
     // minnum/maxnum intrinsics.
-    if (SIFPOp->hasNoNaNs() && SIFPOp->hasNoSignedZeros()) {
+    if (SIFPOp->hasNoNaNs() &&
+        (SIFPOp->hasNoSignedZeros() ||
+         (SIFPOp->hasOneUse() &&
+          canIgnoreSignBitOfZero(*SIFPOp->use_begin())))) {
       Value *X, *Y;
       if (match(&SI, m_OrdOrUnordFMax(m_Value(X), m_Value(Y)))) {
         Value *BinIntr =
diff --git a/llvm/test/Transforms/InstCombine/minmax-fp.ll b/llvm/test/Transforms/InstCombine/minmax-fp.ll
index a8470a20365e9..4ae6905404bbd 100644
--- a/llvm/test/Transforms/InstCombine/minmax-fp.ll
+++ b/llvm/test/Transforms/InstCombine/minmax-fp.ll
@@ -174,8 +174,7 @@ define i8 @t11(float %a, float %b) {
 ; Either operand could be NaN, but nnan modifier applied.
 define i8 @t12(float %a, float %b) {
 ; CHECK-LABEL: @t12(
-; CHECK-NEXT:    [[DOTINV:%.*]] = fcmp nnan oge float [[B:%.*]], [[A:%.*]]
-; CHECK-NEXT:    [[DOTV:%.*]] = select nnan i1 [[DOTINV]], float [[A]], float [[B]]
+; CHECK-NEXT:    [[DOTV:%.*]] = call nnan float @llvm.minnum.f32(float [[B:%.*]], float [[A:%.*]])
 ; CHECK-NEXT:    [[TMP1:%.*]] = fptosi float [[DOTV]] to i8
 ; CHECK-NEXT:    ret i8 [[TMP1]]
 ;
diff --git a/minmax b/minmax
new file mode 100644
index 0000000000000..e69de29bb2d1d

@dtcxzyw
Copy link
Member Author

dtcxzyw commented May 29, 2025

No real-world impact.

@arsenm arsenm added the floating-point Floating-point math label May 29, 2025
@dtcxzyw dtcxzyw merged commit 87fd352 into llvm:main May 30, 2025
12 checks passed
@dtcxzyw dtcxzyw deleted the perf/select-minmax-nsz branch May 30, 2025 06:18
dtcxzyw added a commit that referenced this pull request May 31, 2025
Previously,
3d6b539
propagates FMF from fcmp to avoid performance regressions. With the help
of #139861,
#141015, and
#141914, we can still convert
SPF into fabs/minnum/maxnum intrinsics even if some flags are missing.
This patch propagates FMF from select to address the long-standing
issue.

Closes #140994.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request May 31, 2025
…141010)

Previously,
llvm/llvm-project@3d6b539
propagates FMF from fcmp to avoid performance regressions. With the help
of llvm/llvm-project#139861,
llvm/llvm-project#141015, and
llvm/llvm-project#141914, we can still convert
SPF into fabs/minnum/maxnum intrinsics even if some flags are missing.
This patch propagates FMF from select to address the long-standing
issue.

Closes llvm/llvm-project#140994.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants