[llvm-branch-commits] [llvm] ValueTracking: Improve sign bit handling for fdiv (PR #174652)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jan 6 13:27:36 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Matt Arsenault (arsenm)

<details>
<summary>Changes</summary>

This is the fdiv version of 5020e0ff1494137ff12b4ed7c8fa896f8088b17b for
fmul.

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


2 Files Affected:

- (modified) llvm/lib/Analysis/ValueTracking.cpp (+14-3) 
- (modified) llvm/test/Transforms/Attributor/nofpclass-fdiv.ll (+3-3) 


``````````diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index bdc2e71bdc680..4d5f2e8ac490c 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5812,8 +5812,9 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
                         fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
                         Depth + 1);
 
-    bool KnowSomethingUseful =
-        KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
+    bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
+                               KnownRHS.isKnownNever(fcNegative) ||
+                               KnownRHS.isKnownNever(fcPositive);
 
     if (KnowSomethingUseful || WantPositive) {
       const FPClassTest InterestedLHS =
@@ -5841,10 +5842,20 @@ void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
         Known.knownNot(fcNan);
       }
 
+      // xor sign bit.
       // X / -0.0 is -Inf (or NaN).
       // +X / +X is +X
-      if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
+      if ((KnownLHS.isKnownNever(fcNegative) &&
+           KnownRHS.isKnownNever(fcNegative)) ||
+          (KnownLHS.isKnownNever(fcPositive) &&
+           KnownRHS.isKnownNever(fcPositive)))
         Known.knownNot(fcNegative);
+
+      if ((KnownLHS.isKnownNever(fcPositive) &&
+           KnownRHS.isKnownNever(fcNegative)) ||
+          (KnownLHS.isKnownNever(fcNegative) &&
+           KnownRHS.isKnownNever(fcPositive)))
+        Known.knownNot(fcPositive);
     } else {
       // Inf REM x and x REM 0 produce NaN.
       if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fdiv.ll b/llvm/test/Transforms/Attributor/nofpclass-fdiv.ll
index 228be4ae748fa..8efed69d9d7c4 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fdiv.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fdiv.ll
@@ -909,7 +909,7 @@ define float @ret_fdiv_both_signs_positive_or_nan(float %lhs, float %rhs) {
 
 ; Result must be positive or nan
 define float @ret_fdiv_both_signs_negative_or_nan(float %lhs, float %rhs) {
-; CHECK-LABEL: define float @ret_fdiv_both_signs_negative_or_nan
+; CHECK-LABEL: define nofpclass(ninf nzero nsub nnorm) float @ret_fdiv_both_signs_negative_or_nan
 ; CHECK-SAME: (float [[LHS:%.*]], float [[RHS:%.*]]) #[[ATTR4]] {
 ; CHECK-NEXT:    [[LHS_FABS:%.*]] = call float @llvm.fabs.f32(float [[LHS]]) #[[ATTR6]]
 ; CHECK-NEXT:    [[RHS_FABS:%.*]] = call float @llvm.fabs.f32(float [[RHS]]) #[[ATTR6]]
@@ -928,7 +928,7 @@ define float @ret_fdiv_both_signs_negative_or_nan(float %lhs, float %rhs) {
 
 ; Result must be negative or nan
 define float @ret_fdiv_lhs_negative_rhs_positive(float %lhs, float %rhs) {
-; CHECK-LABEL: define float @ret_fdiv_lhs_negative_rhs_positive
+; CHECK-LABEL: define nofpclass(pinf pzero psub pnorm) float @ret_fdiv_lhs_negative_rhs_positive
 ; CHECK-SAME: (float [[LHS:%.*]], float [[RHS:%.*]]) #[[ATTR4]] {
 ; CHECK-NEXT:    [[LHS_FABS:%.*]] = call float @llvm.fabs.f32(float [[LHS]]) #[[ATTR6]]
 ; CHECK-NEXT:    [[RHS_FABS:%.*]] = call float @llvm.fabs.f32(float [[RHS]]) #[[ATTR6]]
@@ -945,7 +945,7 @@ define float @ret_fdiv_lhs_negative_rhs_positive(float %lhs, float %rhs) {
 
 ; Result must be negative or nan
 define float @ret_fdiv_rhs_negative_lhs_positive(float %lhs, float %rhs) {
-; CHECK-LABEL: define float @ret_fdiv_rhs_negative_lhs_positive
+; CHECK-LABEL: define nofpclass(pinf pzero psub pnorm) float @ret_fdiv_rhs_negative_lhs_positive
 ; CHECK-SAME: (float [[LHS:%.*]], float [[RHS:%.*]]) #[[ATTR4]] {
 ; CHECK-NEXT:    [[LHS_FABS:%.*]] = call float @llvm.fabs.f32(float [[LHS]]) #[[ATTR6]]
 ; CHECK-NEXT:    [[RHS_FABS:%.*]] = call float @llvm.fabs.f32(float [[RHS]]) #[[ATTR6]]

``````````

</details>


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


More information about the llvm-branch-commits mailing list