[llvm] [ConstantFolding] Fix llvm.is.fpclass folding of x87 pseudo-NaN (PR #212011)

Aayush Mainali via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 25 03:59:39 PDT 2026


https://github.com/AayushMainali-Github created https://github.com/llvm/llvm-project/pull/212011

x87 unsupported encodings (pseudo-NaN, pseudo-infinity, unnormal) are classified as NaN by APFloat, but `TargetLowering::expandIS_FPCLASS` treats them as neither signaling nor quiet because the explicit integer/J-bit is clear. Constant folding used `isSignaling()` alone, so `llvm.is.fpclass` on a constant pseudo-NaN could fold `fcSNan` to true and disagree with `-O0` / codegen (e.g. `__builtin_issignaling` on `long double`).

Detect unsupported x87 NaNs via `hasExplicitIntegerBit` and a clear integer bit, then fold them as NaN but not SNaN/QNaN, matching `expandIS_FPCLASS`. Genuine f80 SNaN/QNaN with the integer bit set are unchanged.

Fixes #211205

Assisted-by: Cursor Agent

>From d688943f5d0a06bddfa879f42ccc7ba861764431 Mon Sep 17 00:00:00 2001
From: AayushMainali-Github <aayushmainali.codes at gmail.com>
Date: Sat, 25 Jul 2026 10:58:18 +0000
Subject: [PATCH] [ConstantFolding] Fix llvm.is.fpclass folding of x87
 pseudo-NaN

x87 unsupported encodings are NaNs in APFloat but neither signaling nor
quiet under expandIS_FPCLASS. Detect a clear explicit integer bit so
constant folding matches the runtime classification.

Fixes #211205

Assisted-by: Cursor Agent
---
 llvm/lib/Analysis/ConstantFolding.cpp         | 40 ++++++---
 .../test/Transforms/InstCombine/is_fpclass.ll | 89 +++++++++++++++++++
 2 files changed, 119 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 8901a5384e37e..b638216d442bb 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -3728,17 +3728,37 @@ static Constant *ConstantFoldIntrinsicCall2(Intrinsic::ID IntrinsicID, Type *Ty,
       }
       case Intrinsic::is_fpclass: {
         FPClassTest Mask = static_cast<FPClassTest>(Op2C->getZExtValue());
+        bool IsNaN = Op1V.isNaN();
+        bool IsSNan = false;
+        bool IsQNan = false;
+        if (IsNaN) {
+          // x87 unsupported encodings (pseudo-NaN/infinity, unnormal) are
+          // NaNs in APFloat, but expandIS_FPCLASS treats them as neither
+          // signaling nor quiet. A clear explicit integer bit identifies
+          // them so constant folding matches that runtime behavior.
+          const fltSemantics &Sem = Op1V.getSemantics();
+          bool IsUnsupportedX87NaN =
+              Sem.hasExplicitIntegerBit &&
+              !Op1V.bitcastToAPInt()[Sem.precision - 1];
+          if (!IsUnsupportedX87NaN) {
+            IsSNan = Op1V.isSignaling();
+            IsQNan = !IsSNan;
+          }
+        }
         bool Result =
-          ((Mask & fcSNan) && Op1V.isNaN() && Op1V.isSignaling()) ||
-          ((Mask & fcQNan) && Op1V.isNaN() && !Op1V.isSignaling()) ||
-          ((Mask & fcNegInf) && Op1V.isNegInfinity()) ||
-          ((Mask & fcNegNormal) && Op1V.isNormal() && Op1V.isNegative()) ||
-          ((Mask & fcNegSubnormal) && Op1V.isDenormal() && Op1V.isNegative()) ||
-          ((Mask & fcNegZero) && Op1V.isZero() && Op1V.isNegative()) ||
-          ((Mask & fcPosZero) && Op1V.isZero() && !Op1V.isNegative()) ||
-          ((Mask & fcPosSubnormal) && Op1V.isDenormal() && !Op1V.isNegative()) ||
-          ((Mask & fcPosNormal) && Op1V.isNormal() && !Op1V.isNegative()) ||
-          ((Mask & fcPosInf) && Op1V.isPosInfinity());
+            ((Mask & fcSNan) && IsSNan) || ((Mask & fcQNan) && IsQNan) ||
+            // Neither SNaN nor QNaN, but still a NaN (x87 unsupported).
+            ((Mask & fcNan) == fcNan && IsNaN && !IsSNan && !IsQNan) ||
+            ((Mask & fcNegInf) && Op1V.isNegInfinity()) ||
+            ((Mask & fcNegNormal) && Op1V.isNormal() && Op1V.isNegative()) ||
+            ((Mask & fcNegSubnormal) && Op1V.isDenormal() &&
+             Op1V.isNegative()) ||
+            ((Mask & fcNegZero) && Op1V.isZero() && Op1V.isNegative()) ||
+            ((Mask & fcPosZero) && Op1V.isZero() && !Op1V.isNegative()) ||
+            ((Mask & fcPosSubnormal) && Op1V.isDenormal() &&
+             !Op1V.isNegative()) ||
+            ((Mask & fcPosNormal) && Op1V.isNormal() && !Op1V.isNegative()) ||
+            ((Mask & fcPosInf) && Op1V.isPosInfinity());
         return ConstantInt::get(Ty, Result);
       }
       case Intrinsic::powi: {
diff --git a/llvm/test/Transforms/InstCombine/is_fpclass.ll b/llvm/test/Transforms/InstCombine/is_fpclass.ll
index 4f338f0df50e9..760b204e12224 100644
--- a/llvm/test/Transforms/InstCombine/is_fpclass.ll
+++ b/llvm/test/Transforms/InstCombine/is_fpclass.ll
@@ -3954,8 +3954,97 @@ entry:
   ret i1 %test
 }
 
+; --------------------------------------------------------------------
+; x87 pseudo-NaN / unsupported encodings (explicit integer bit clear)
+; --------------------------------------------------------------------
+
+; 0xK7FFF3FFFFFFFFFFFFFFF - exp all-ones, integer/J-bit = 0, payload != 0
+define i1 @test_constant_class_f80_pseudo_nan_snan() {
+; CHECK-LABEL: @test_constant_class_f80_pseudo_nan_snan(
+; CHECK-NEXT:    ret i1 false
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFF3FFFFFFFFFFFFFFF, i32 1)
+  ret i1 %val
+}
+
+define i1 @test_constant_class_f80_pseudo_nan_qnan() {
+; CHECK-LABEL: @test_constant_class_f80_pseudo_nan_qnan(
+; CHECK-NEXT:    ret i1 false
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFF3FFFFFFFFFFFFFFF, i32 2)
+  ret i1 %val
+}
+
+define i1 @test_constant_class_f80_pseudo_nan_nan() {
+; CHECK-LABEL: @test_constant_class_f80_pseudo_nan_nan(
+; CHECK-NEXT:    ret i1 true
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFF3FFFFFFFFFFFFFFF, i32 3)
+  ret i1 %val
+}
+
+; Quiet bit clear (would look like SNaN if the integer bit were ignored).
+define i1 @test_constant_class_f80_pseudo_nan_quiet_bit_clear_snan() {
+; CHECK-LABEL: @test_constant_class_f80_pseudo_nan_quiet_bit_clear_snan(
+; CHECK-NEXT:    ret i1 false
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFF1FFFFFFFFFFFFFFF, i32 1)
+  ret i1 %val
+}
+
+define i1 @test_constant_class_f80_pseudo_nan_quiet_bit_clear_nan() {
+; CHECK-LABEL: @test_constant_class_f80_pseudo_nan_quiet_bit_clear_nan(
+; CHECK-NEXT:    ret i1 true
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFF1FFFFFFFFFFFFFFF, i32 3)
+  ret i1 %val
+}
+
+; Pseudo-infinity: exp all-ones, integer bit 0, significand 0.
+define i1 @test_constant_class_f80_pseudo_inf_snan() {
+; CHECK-LABEL: @test_constant_class_f80_pseudo_inf_snan(
+; CHECK-NEXT:    ret i1 false
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFF0000000000000000, i32 1)
+  ret i1 %val
+}
+
+define i1 @test_constant_class_f80_pseudo_inf_nan() {
+; CHECK-LABEL: @test_constant_class_f80_pseudo_inf_nan(
+; CHECK-NEXT:    ret i1 true
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFF0000000000000000, i32 3)
+  ret i1 %val
+}
+
+; Genuine f80 SNaN / QNaN with the integer bit set must still fold correctly.
+define i1 @test_constant_class_f80_snan_snan() {
+; CHECK-LABEL: @test_constant_class_f80_snan_snan(
+; CHECK-NEXT:    ret i1 true
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFF8000000000000001, i32 1)
+  ret i1 %val
+}
+
+define i1 @test_constant_class_f80_qnan_qnan() {
+; CHECK-LABEL: @test_constant_class_f80_qnan_qnan(
+; CHECK-NEXT:    ret i1 true
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFFC000000000000000, i32 2)
+  ret i1 %val
+}
+
+define i1 @test_constant_class_f80_qnan_snan() {
+; CHECK-LABEL: @test_constant_class_f80_qnan_snan(
+; CHECK-NEXT:    ret i1 false
+;
+  %val = call i1 @llvm.is.fpclass.f80(x86_fp80 0xK7FFFC000000000000000, i32 1)
+  ret i1 %val
+}
+
 declare i1 @llvm.is.fpclass.f32(float, i32 immarg)
 declare i1 @llvm.is.fpclass.f64(double, i32 immarg)
+declare i1 @llvm.is.fpclass.f80(x86_fp80, i32 immarg)
 declare <2 x i1> @llvm.is.fpclass.v2f32(<2 x float>, i32 immarg)
 declare float @llvm.fabs.f32(float)
 declare <2 x float> @llvm.fabs.v2f32(<2 x float>)



More information about the llvm-commits mailing list