[llvm] [ValueTracking] Improve nofpclass inference for nsz fadd (PR #186315)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 12 22:01:30 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Yunbo Ni (cardigan1008)

<details>
<summary>Changes</summary>

Fixes #<!-- -->185920 

This patch improves `nofpclass` inference in ValueTracking for floating-point arithmetic with `nsz`.

When `nsz` is present, distinctions between +0.0 and -0.0 are not semantically observable, so `nofpclass` reasoning should avoid deriving sign-sensitive zero properties from such operations. 

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


2 Files Affected:

- (modified) llvm/lib/Analysis/ValueTracking.cpp (+4-1) 
- (modified) llvm/test/Transforms/Attributor/nofpclass.ll (+11) 


``````````diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 0672dc889b640..25a8a8166d3af 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5588,7 +5588,10 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
       DenormalMode Mode =
           F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
 
-      if (Self && Opc == Instruction::FAdd) {
+      const FPMathOperator *FPop = cast<FPMathOperator>(Op);
+      bool HasNSZ = FPop->hasNoSignedZeros();
+
+      if (!HasNSZ && Self && Opc == Instruction::FAdd) {
         Known = KnownFPClass::fadd_self(KnownLHS, Mode);
       } else {
         // RHS is canonically cheaper to compute. Skip inspecting the LHS if
diff --git a/llvm/test/Transforms/Attributor/nofpclass.ll b/llvm/test/Transforms/Attributor/nofpclass.ll
index 507d0f3ff98f3..3946c9f88e41d 100644
--- a/llvm/test/Transforms/Attributor/nofpclass.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass.ll
@@ -3326,6 +3326,17 @@ define float @fadd_double_known_negative_nonsub_dynamic(float noundef nofpclass(
   ret float %add
 }
 
+define float @fadd_double_known_negative_zero_nsz(float noundef nofpclass(ninf pzero sub nnorm) %arg) {
+; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
+; CHECK-LABEL: define noundef nofpclass(ninf nsub nnorm) float @fadd_double_known_negative_zero_nsz
+; CHECK-SAME: (float noundef nofpclass(ninf pzero sub nnorm) [[ARG:%.*]]) #[[ATTR3]] {
+; CHECK-NEXT:    [[ADD:%.*]] = fadd nsz float [[ARG]], [[ARG]]
+; CHECK-NEXT:    ret float [[ADD]]
+;
+  %add = fadd nsz float %arg, %arg
+  ret float %add
+}
+
 define float @fsub_self(float noundef %arg) {
 ; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
 ; CHECK-LABEL: define noundef float @fsub_self

``````````

</details>


https://github.com/llvm/llvm-project/pull/186315


More information about the llvm-commits mailing list