[llvm] ValueTracking: Propagate non-nan sources through fmul square (PR #176243)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 15 14:01:47 PST 2026


https://github.com/arsenm updated https://github.com/llvm/llvm-project/pull/176243

>From 6ffcf8ad1ed2fc2a23f3a6ce3a090b42039a79f3 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 15 Jan 2026 21:56:51 +0100
Subject: [PATCH 1/2] ValueTracking: Propagate non-nan sources through fmul
 square

https://alive2.llvm.org/ce/z/hbZUfc
---
 llvm/include/llvm/Support/KnownFPClass.h      |  1 +
 .../Transforms/Attributor/nofpclass-fmul.ll   | 30 +++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/llvm/include/llvm/Support/KnownFPClass.h b/llvm/include/llvm/Support/KnownFPClass.h
index 6c1a6a7e9b5c3..bf0b36e87892f 100644
--- a/llvm/include/llvm/Support/KnownFPClass.h
+++ b/llvm/include/llvm/Support/KnownFPClass.h
@@ -240,6 +240,7 @@ struct KnownFPClass {
 
     // X * X is always non-negative or a NaN.
     Known.knownNot(fcNegative);
+    Known.propagateNaN(Src);
     return Known;
   }
 
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fmul.ll b/llvm/test/Transforms/Attributor/nofpclass-fmul.ll
index 57c6f1de6abb2..e4ec90e0976e1 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fmul.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fmul.ll
@@ -44,6 +44,36 @@ define float @ret_mul_exponent_f32_22(float %arg0) {
   ret float %call
 }
 
+define float @ret_fmul_square_f32_src_no_nan(float noundef nofpclass(nan) %arg) {
+; CHECK-LABEL: define noundef nofpclass(ninf nzero nsub nnorm) float @ret_fmul_square_f32_src_no_nan(
+; CHECK-SAME: float noundef nofpclass(nan) [[ARG:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[FMUL:%.*]] = fmul float [[ARG]], [[ARG]]
+; CHECK-NEXT:    ret float [[FMUL]]
+;
+  %fmul = fmul float %arg, %arg
+  ret float %fmul
+}
+
+define float @ret_fmul_square_f32_src_no_snan(float noundef nofpclass(snan) %arg) {
+; CHECK-LABEL: define noundef nofpclass(ninf nzero nsub nnorm) float @ret_fmul_square_f32_src_no_snan(
+; CHECK-SAME: float noundef nofpclass(snan) [[ARG:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[FMUL:%.*]] = fmul float [[ARG]], [[ARG]]
+; CHECK-NEXT:    ret float [[FMUL]]
+;
+  %fmul = fmul float %arg, %arg
+  ret float %fmul
+}
+
+define float @ret_fmul_square_f32_src_no_qnan(float noundef nofpclass(qnan) %arg) {
+; CHECK-LABEL: define noundef nofpclass(ninf nzero nsub nnorm) float @ret_fmul_square_f32_src_no_qnan(
+; CHECK-SAME: float noundef nofpclass(qnan) [[ARG:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT:    [[FMUL:%.*]] = fmul float [[ARG]], [[ARG]]
+; CHECK-NEXT:    ret float [[FMUL]]
+;
+  %fmul = fmul float %arg, %arg
+  ret float %fmul
+}
+
 define float @ret_mul_exponent_f32_23(float %arg0) {
 ; CHECK-LABEL: define nofpclass(sub) float @ret_mul_exponent_f32_23(
 ; CHECK-SAME: float [[ARG0:%.*]]) #[[ATTR0]] {

>From d3e9e0510d10739268b45fa8431a648490cfdc64 Mon Sep 17 00:00:00 2001
From: Matt Arsenault <Matthew.Arsenault at amd.com>
Date: Thu, 15 Jan 2026 22:03:19 +0100
Subject: [PATCH 2/2] test-diff

---
 llvm/test/Transforms/Attributor/nofpclass-fmul.ll     | 4 ++--
 llvm/test/Transforms/Attributor/nofpclass-nan-fmul.ll | 2 +-
 llvm/test/Transforms/Attributor/nofpclass.ll          | 2 +-
 llvm/unittests/Analysis/ValueTrackingTest.cpp         | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/test/Transforms/Attributor/nofpclass-fmul.ll b/llvm/test/Transforms/Attributor/nofpclass-fmul.ll
index e4ec90e0976e1..edf8d196960d3 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fmul.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fmul.ll
@@ -45,7 +45,7 @@ define float @ret_mul_exponent_f32_22(float %arg0) {
 }
 
 define float @ret_fmul_square_f32_src_no_nan(float noundef nofpclass(nan) %arg) {
-; CHECK-LABEL: define noundef nofpclass(ninf nzero nsub nnorm) float @ret_fmul_square_f32_src_no_nan(
+; CHECK-LABEL: define noundef nofpclass(nan ninf nzero nsub nnorm) float @ret_fmul_square_f32_src_no_nan(
 ; CHECK-SAME: float noundef nofpclass(nan) [[ARG:%.*]]) #[[ATTR0]] {
 ; CHECK-NEXT:    [[FMUL:%.*]] = fmul float [[ARG]], [[ARG]]
 ; CHECK-NEXT:    ret float [[FMUL]]
@@ -55,7 +55,7 @@ define float @ret_fmul_square_f32_src_no_nan(float noundef nofpclass(nan) %arg)
 }
 
 define float @ret_fmul_square_f32_src_no_snan(float noundef nofpclass(snan) %arg) {
-; CHECK-LABEL: define noundef nofpclass(ninf nzero nsub nnorm) float @ret_fmul_square_f32_src_no_snan(
+; CHECK-LABEL: define noundef nofpclass(snan ninf nzero nsub nnorm) float @ret_fmul_square_f32_src_no_snan(
 ; CHECK-SAME: float noundef nofpclass(snan) [[ARG:%.*]]) #[[ATTR0]] {
 ; CHECK-NEXT:    [[FMUL:%.*]] = fmul float [[ARG]], [[ARG]]
 ; CHECK-NEXT:    ret float [[FMUL]]
diff --git a/llvm/test/Transforms/Attributor/nofpclass-nan-fmul.ll b/llvm/test/Transforms/Attributor/nofpclass-nan-fmul.ll
index 39db78794c41a..1312f06cde0a4 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-nan-fmul.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-nan-fmul.ll
@@ -175,7 +175,7 @@ define float @ret_fmul_square(float noundef %arg) #0 {
 }
 
 define float @ret_fmul_square_nnan(float noundef nofpclass(nan) %arg) #0 {
-; CHECK-LABEL: define noundef nofpclass(ninf nzero nsub nnorm) float @ret_fmul_square_nnan
+; CHECK-LABEL: define noundef nofpclass(nan ninf nzero nsub nnorm) float @ret_fmul_square_nnan
 ; CHECK-SAME: (float noundef nofpclass(nan) [[ARG:%.*]]) #[[ATTR0]] {
 ; CHECK-NEXT:    [[FMUL:%.*]] = fmul float [[ARG]], [[ARG]]
 ; CHECK-NEXT:    ret float [[FMUL]]
diff --git a/llvm/test/Transforms/Attributor/nofpclass.ll b/llvm/test/Transforms/Attributor/nofpclass.ll
index 066f9647bc9a5..d325ba001e65c 100644
--- a/llvm/test/Transforms/Attributor/nofpclass.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass.ll
@@ -1856,7 +1856,7 @@ define i32 @fptosi(float nofpclass(inf nan) %arg) {
 
 define float @fptrunc(double noundef nofpclass(inf nan) %arg) {
 ; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(none)
-; CHECK-LABEL: define noundef nofpclass(ninf nzero nsub nnorm) float @fptrunc
+; CHECK-LABEL: define noundef nofpclass(nan ninf nzero nsub nnorm) float @fptrunc
 ; CHECK-SAME: (double noundef nofpclass(nan inf) [[ARG:%.*]]) #[[ATTR3]] {
 ; CHECK-NEXT:    [[CAST:%.*]] = fptrunc double [[ARG]] to float
 ; CHECK-NEXT:    [[MUL:%.*]] = fmul float [[CAST]], [[CAST]]
diff --git a/llvm/unittests/Analysis/ValueTrackingTest.cpp b/llvm/unittests/Analysis/ValueTrackingTest.cpp
index 1707ed2c39264..5699480c7f375 100644
--- a/llvm/unittests/Analysis/ValueTrackingTest.cpp
+++ b/llvm/unittests/Analysis/ValueTrackingTest.cpp
@@ -1666,7 +1666,7 @@ TEST_F(ComputeKnownFPClassTest, FMul) {
   expectKnownFPClass(fcAllFlags, std::nullopt, A2);
   expectKnownFPClass(fcAllFlags, std::nullopt, A3);
   expectKnownFPClass(fcAllFlags, std::nullopt, A4);
-  expectKnownFPClass(fcPositive | fcNan, std::nullopt, A5);
+  expectKnownFPClass(fcPositive, false, A5);
 }
 
 TEST_F(ComputeKnownFPClassTest, FMulNoZero) {



More information about the llvm-commits mailing list