[llvm] [ValueTracking] Treat fmul NaN sign bit as unknown to prevent incorrect fabs folding (PR #180339)
Nathiyaa Sengodan via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 19 00:26:10 PST 2026
https://github.com/Nathiyaa-Sengodan 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] [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
More information about the llvm-commits
mailing list