[llvm] [InstCombine] Fix FMF propagation in `foldFNegIntoConstant` (PR #191653)
Yingwei Zheng via llvm-commits
llvm-commits at lists.llvm.org
Sat Apr 11 11:24:54 PDT 2026
https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/191653
Counterexamples for ninf and nsz: https://alive2.llvm.org/ce/z/7-Je_K
Closes https://github.com/llvm/llvm-project/issues/189729.
>From 59392e53d28b075489b756f29ebcfdbbb57fead9 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 12 Apr 2026 01:44:26 +0800
Subject: [PATCH 1/2] [InstCombine] Add pre-commit tests. NFC.
---
llvm/test/Transforms/InstCombine/fneg.ll | 51 ++++++++++++++++++++++--
1 file changed, 47 insertions(+), 4 deletions(-)
diff --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index ed4d43eaea235..5d20dcba83826 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -115,7 +115,7 @@ define <4 x double> @fmul_fneg_vec(<4 x double> %x) {
define float @fdiv_fneg_fpmath(float %x, float %y) {
; CHECK-LABEL: @fdiv_fneg_fpmath(
; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = fdiv float [[NEG]], [[Y:%.*]], !fpmath
+; CHECK-NEXT: [[R:%.*]] = fdiv float [[NEG]], [[Y:%.*]], !fpmath [[META0:![0-9]+]]
; CHECK-NEXT: ret float [[R]]
;
%z = fdiv arcp float %x, %y, !fpmath !{float 2.5}
@@ -167,6 +167,49 @@ define float @fdiv_op1_constant_fneg_fmf(float %x) {
ret float %r
}
+; Don't propagate ninf if it is unavailable on fdiv
+
+define float @fdiv_op1_constant_fneg_ninf(float %x) {
+; CHECK-LABEL: @fdiv_op1_constant_fneg_ninf(
+; CHECK-NEXT: ret float poison
+;
+ %d = fdiv float %x, 0x7FF0000000000000
+ %r = fneg ninf float %d
+ ret float %r
+}
+
+define float @fdiv_ninf_op1_constant_fneg_ninf(float %x) {
+; CHECK-LABEL: @fdiv_ninf_op1_constant_fneg_ninf(
+; CHECK-NEXT: [[R:%.*]] = fdiv ninf float [[X:%.*]], 4.200000e+01
+; CHECK-NEXT: ret float [[R]]
+;
+ %d = fdiv ninf float %x, -42.0
+ %r = fneg ninf float %d
+ ret float %r
+}
+
+; Don't propagate nsz if it is unavailable on fdiv
+
+define float @fdiv_op1_constant_fneg_nsz(float %x) {
+; CHECK-LABEL: @fdiv_op1_constant_fneg_nsz(
+; CHECK-NEXT: [[R:%.*]] = fdiv nsz float [[X:%.*]], -0.000000e+00
+; CHECK-NEXT: ret float [[R]]
+;
+ %d = fdiv float %x, 0.0
+ %r = fneg nsz float %d
+ ret float %r
+}
+
+define float @fdiv_nsz_op1_constant_fneg_nsz(float %x) {
+; CHECK-LABEL: @fdiv_nsz_op1_constant_fneg_nsz(
+; CHECK-NEXT: [[R:%.*]] = fdiv nsz float [[X:%.*]], -0.000000e+00
+; CHECK-NEXT: ret float [[R]]
+;
+ %d = fdiv nsz float %x, 0.0
+ %r = fneg nsz float %d
+ ret float %r
+}
+
; Extra use prevents the fold. We don't want to replace the fneg with an fdiv.
define float @fdiv_op1_constant_fsub_extra_use(float %x) {
@@ -219,7 +262,7 @@ define <4 x double> @fdiv_op1_constant_fneg_vec(<4 x double> %x) {
define float @fdiv_op1_constant_fneg_fpmath(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fneg_fpmath(
-; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], 4.200000e+01, !fpmath
+; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], 4.200000e+01, !fpmath [[META0]]
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, -42.0, !fpmath !{float 2.5}
@@ -333,7 +376,7 @@ define float @fdiv_op0_constant_fneg_nnan(float %x) {
define float @fdiv_op0_constant_fneg_fpmath(float %x) {
; CHECK-LABEL: @fdiv_op0_constant_fneg_fpmath(
-; CHECK-NEXT: [[R:%.*]] = fdiv float -1.000000e+00, [[X:%.*]], !fpmath
+; CHECK-NEXT: [[R:%.*]] = fdiv float -1.000000e+00, [[X:%.*]], !fpmath [[META0]]
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float 1.0, %x, !fpmath !{float 2.5}
@@ -1014,7 +1057,7 @@ define float @fneg_ldexp_contract(float %x, i32 %n) {
define float @fneg_ldexp_metadata(float %x, i32 %n) {
; CHECK-LABEL: @fneg_ldexp_metadata(
; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[X:%.*]]
-; CHECK-NEXT: [[NEG:%.*]] = call float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]), !arst [[META0:![0-9]+]]
+; CHECK-NEXT: [[NEG:%.*]] = call float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]), !arst [[META1:![0-9]+]]
; CHECK-NEXT: ret float [[NEG]]
;
%ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n), !arst !0
>From 3f807377c89cb35c39a087db65ba0c6abe74c85d Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 12 Apr 2026 01:46:38 +0800
Subject: [PATCH 2/2] [InstCombine] Fix FMF propagation in
`foldFNegIntoConstant`
---
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp | 7 +++++++
llvm/test/Transforms/InstCombine/fneg.ll | 5 +++--
2 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 2f23976033923..635502f08b483 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -3015,6 +3015,13 @@ static Instruction *foldFNegIntoConstant(Instruction &I, const DataLayout &DL) {
if (match(FNegOp, m_FDiv(m_Value(X), m_Constant(C)))) {
if (Constant *NegC = ConstantFoldUnaryOpOperand(Instruction::FNeg, C, DL)) {
Instruction *FDiv = BinaryOperator::CreateFDivFMF(X, NegC, &I);
+
+ // Intersect 'nsz' and 'ninf' because those special value exceptions may
+ // not apply to the fdiv. Everything else propagates from the fneg.
+ FastMathFlags FMF = I.getFastMathFlags();
+ FastMathFlags OpFMF = FNegOp->getFastMathFlags();
+ FDiv->setHasNoSignedZeros(FMF.noSignedZeros() && OpFMF.noSignedZeros());
+ FDiv->setHasNoInfs(FMF.noInfs() && OpFMF.noInfs());
FDiv->copyMetadata(*FNegOp);
return FDiv;
}
diff --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index 5d20dcba83826..36852c1d2a684 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -171,7 +171,8 @@ define float @fdiv_op1_constant_fneg_fmf(float %x) {
define float @fdiv_op1_constant_fneg_ninf(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fneg_ninf(
-; CHECK-NEXT: ret float poison
+; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], 0xFFF0000000000000
+; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, 0x7FF0000000000000
%r = fneg ninf float %d
@@ -192,7 +193,7 @@ define float @fdiv_ninf_op1_constant_fneg_ninf(float %x) {
define float @fdiv_op1_constant_fneg_nsz(float %x) {
; CHECK-LABEL: @fdiv_op1_constant_fneg_nsz(
-; CHECK-NEXT: [[R:%.*]] = fdiv nsz float [[X:%.*]], -0.000000e+00
+; CHECK-NEXT: [[R:%.*]] = fdiv float [[X:%.*]], -0.000000e+00
; CHECK-NEXT: ret float [[R]]
;
%d = fdiv float %x, 0.0
More information about the llvm-commits
mailing list