[llvm] [ValueTracking] Treat fmul NaN sign bit as unknown to prevent incorrect fabs folding (PR #180339)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 19 03:02:17 PST 2026
https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/180339
>From 0f2bf5006cc7c1b9b0e04bad3220362f18b1fda0 Mon Sep 17 00:00:00 2001
From: Nathiyaa Sengodan <s.nathiyaa at gmail.com>
Date: Sat, 7 Feb 2026 14:30:25 +0530
Subject: [PATCH 1/3] [ValueTracking] Do not remove fabs after fmul that may
produce NaN
Floating-point math operations such as fmul may produce NaN with a
non-deterministic sign bit. The llvm.fabs intrinsic canonicalizes the
sign bit of NaN, so removing fabs after fmul can change observable
behaviour.
Ensure the sign bit is treated as unknown when fmul may produce NaN to
prevent InstSimplify from incorrectly removing fabs.
---
llvm/lib/Support/KnownFPClass.cpp | 7 -----
.../InstSimplify/floating-point-arithmetic.ll | 27 +++++++++++++++++++
2 files changed, 27 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Support/KnownFPClass.cpp b/llvm/lib/Support/KnownFPClass.cpp
index 99d29119b8d86..080535a44c6c8 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -342,13 +342,6 @@ KnownFPClass KnownFPClass::fmul(const KnownFPClass &KnownLHS,
if (!KnownLHS.isKnownNeverNaN() || !KnownRHS.isKnownNeverNaN())
return Known;
- if (KnownLHS.SignBit && KnownRHS.SignBit) {
- if (*KnownLHS.SignBit == *KnownRHS.SignBit)
- Known.signBitMustBeZero();
- else
- Known.signBitMustBeOne();
- }
-
// If 0 * +/-inf produces NaN.
if ((KnownRHS.isKnownNeverInfinity() ||
KnownLHS.isKnownNeverLogicalZero(Mode)) &&
diff --git a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
index 820fff433e9e0..5c4c6cd034d34 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
@@ -1272,3 +1272,30 @@ entry:
%abs2 = call half @llvm.fabs.f16(half %sel)
ret half %abs2
}
+
+; fabs cannot be removed after fmul because fmul may produce NaN with unknown sign.
+define float @fabs_fmul_nan(float %x) {
+; CHECK-LABEL: @fabs_fmul_nan(
+; CHECK-NEXT: [[ABS:%.*]] = call nnan float @llvm.fabs.f32(float [[X:%.*]])
+; CHECK-NEXT: [[MUL:%.*]] = fmul float [[ABS]], 0x7FF0000000000000
+; CHECK-NEXT: [[ABS2:%.*]] = call float @llvm.fabs.f32(float [[MUL]])
+; CHECK-NEXT: ret float [[ABS2]]
+;
+ %abs = call nnan float @llvm.fabs.f32(float %x)
+ %mul = fmul float %abs, 0x7FF0000000000000
+ %abs2 = call float @llvm.fabs.f32(float %mul)
+ ret float %abs2
+}
+
+define <2 x float> @fabs_fmul_nan_vector(<2 x float> %x) {
+; CHECK-LABEL: @fabs_fmul_nan_vector(
+; CHECK-NEXT: [[ABS:%.*]] = call nnan <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
+; CHECK-NEXT: [[MUL:%.*]] = fmul <2 x float> [[ABS]], splat (float 0x7FF0000000000000)
+; CHECK-NEXT: [[ABS2:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[MUL]])
+; CHECK-NEXT: ret <2 x float> [[ABS2]]
+;
+ %abs = call nnan <2 x float> @llvm.fabs.v2f32(<2 x float> %x)
+ %mul = fmul <2 x float> %abs, <float 0x7FF0000000000000, float 0x7FF0000000000000>
+ %abs2 = call <2 x float> @llvm.fabs.v2f32(<2 x float> %mul)
+ ret <2 x float> %abs2
+}
\ No newline at end of file
>From c62a6ffcb9ff374d042cd2d73318cd81dd7fc002 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <arsenm2 at gmail.com>
Date: Thu, 19 Feb 2026 12:01:11 +0100
Subject: [PATCH 2/3] whitespace fix
---
llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
index 5c4c6cd034d34..3bb620d7a6e25 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
@@ -1298,4 +1298,4 @@ define <2 x float> @fabs_fmul_nan_vector(<2 x float> %x) {
%mul = fmul <2 x float> %abs, <float 0x7FF0000000000000, float 0x7FF0000000000000>
%abs2 = call <2 x float> @llvm.fabs.v2f32(<2 x float> %mul)
ret <2 x float> %abs2
-}
\ No newline at end of file
+}
>From 7c2a1bf3f42827f095e63a734cb672e2661942f7 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <arsenm2 at gmail.com>
Date: Thu, 19 Feb 2026 12:02:08 +0100
Subject: [PATCH 3/3] test cleanup
---
llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
index 3bb620d7a6e25..0312e8ed7d9ba 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
@@ -1295,7 +1295,7 @@ define <2 x float> @fabs_fmul_nan_vector(<2 x float> %x) {
; CHECK-NEXT: ret <2 x float> [[ABS2]]
;
%abs = call nnan <2 x float> @llvm.fabs.v2f32(<2 x float> %x)
- %mul = fmul <2 x float> %abs, <float 0x7FF0000000000000, float 0x7FF0000000000000>
+ %mul = fmul <2 x float> %abs, splat (float 0x7FF0000000000000)
%abs2 = call <2 x float> @llvm.fabs.v2f32(<2 x float> %mul)
ret <2 x float> %abs2
}
More information about the llvm-commits
mailing list