Skip to content

Conversation

@arsenm
Copy link
Contributor

@arsenm arsenm commented Nov 10, 2025

It's possible to determine the sign bit if the value is known
one of the positive/negative classes and not-nan.

Copy link
Contributor Author

arsenm commented Nov 10, 2025

@arsenm arsenm added floating-point Floating-point math llvm:SelectionDAG SelectionDAGISel as well labels Nov 10, 2025 — with Graphite App
@arsenm arsenm marked this pull request as ready for review November 10, 2025 07:59
@llvmbot
Copy link
Member

llvmbot commented Nov 10, 2025

@llvm/pr-subscribers-backend-amdgpu

@llvm/pr-subscribers-llvm-selectiondag

Author: Matt Arsenault (arsenm)

Changes

It's possible to determine the sign bit if the value is known
one of the positive/negative classes and not-nan.


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

2 Files Affected:

  • (modified) llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp (+21)
  • (modified) llvm/test/CodeGen/AMDGPU/compute-known-bits-nofpclass.ll (-2)
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index 80bbfea7fb83c..27dcd8a546d91 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -4121,6 +4121,27 @@ KnownBits SelectionDAG::computeKnownBits(SDValue Op, const APInt &DemandedElts,
     Known.One.clearLowBits(LogOfAlign);
     break;
   }
+  case ISD::AssertNoFPClass: {
+    Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
+
+    FPClassTest NoFPClass =
+        static_cast<FPClassTest>(Op.getConstantOperandVal(1));
+    const FPClassTest NegativeTestMask = fcNan | fcNegative;
+    if ((NoFPClass & NegativeTestMask) == NegativeTestMask) {
+      // Cannot be negative.
+      Known.Zero.setSignBit();
+      Known.One.clearSignBit();
+    }
+
+    const FPClassTest PositiveTestMask = fcNan | fcPositive;
+    if ((NoFPClass & PositiveTestMask) == PositiveTestMask) {
+      // Cannot be positive.
+      Known.Zero.clearSignBit();
+      Known.One.setSignBit();
+    }
+
+    break;
+  }
   case ISD::FGETSIGN:
     // All bits are zero except the low bit.
     Known.Zero.setBitsFrom(1);
diff --git a/llvm/test/CodeGen/AMDGPU/compute-known-bits-nofpclass.ll b/llvm/test/CodeGen/AMDGPU/compute-known-bits-nofpclass.ll
index d440d58246333..244c3f7c2a96a 100644
--- a/llvm/test/CodeGen/AMDGPU/compute-known-bits-nofpclass.ll
+++ b/llvm/test/CodeGen/AMDGPU/compute-known-bits-nofpclass.ll
@@ -5,7 +5,6 @@ define i32 @known_positive(float nofpclass(nan ninf nzero nsub nnorm) %signbit.z
 ; CHECK-LABEL: known_positive:
 ; CHECK:       ; %bb.0:
 ; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT:    v_and_b32_e32 v0, 0x7fffffff, v0
 ; CHECK-NEXT:    s_setpc_b64 s[30:31]
   %cast = bitcast float %signbit.zero to i32
   %and = and i32 %cast, 2147483647
@@ -27,7 +26,6 @@ define i32 @known_negative(float nofpclass(nan pinf pzero psub pnorm) %signbit.o
 ; CHECK-LABEL: known_negative:
 ; CHECK:       ; %bb.0:
 ; CHECK-NEXT:    s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
-; CHECK-NEXT:    v_or_b32_e32 v0, 0x80000000, v0
 ; CHECK-NEXT:    s_setpc_b64 s[30:31]
   %cast = bitcast float %signbit.one to i32
   %or = or i32 %cast, -2147483648

break;
}
case ISD::AssertNoFPClass: {
Known = computeKnownBits(Op.getOperand(0), DemandedElts, Depth + 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does SDAG's version of computeKnownBits also work for floating-point values?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly just through bitcasts to wrapped integer logic etc. But X86 does successfully use it for some of its FP node types

Base automatically changed from users/arsenm/amdgpu/add-baseline-test-compute-known-bits-assert-nofpclass to main November 10, 2025 15:56
It's possible to determine the sign bit if the value is known
one of the positive/negative classes and not-nan.
@arsenm arsenm force-pushed the users/arsenm/dag/computeKnownBits-assert-nofpclass branch from bb863b1 to ad35805 Compare November 10, 2025 16:00
@arsenm arsenm merged commit fa98fcd into main Nov 10, 2025
10 checks passed
@arsenm arsenm deleted the users/arsenm/dag/computeKnownBits-assert-nofpclass branch November 10, 2025 16:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend:AMDGPU floating-point Floating-point math llvm:SelectionDAG SelectionDAGISel as well

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants