[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-24 Thread Matt Arsenault via llvm-branch-commits

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

>From d03f5bc9b08ce48793d2ef52d5ffa104b7ca718b Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/4] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index 8641ab6f28e33..5b1513b084a25 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5623,6 +5623,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index 4b1d7367f103d..7a798dd521fb0 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -375,7 +375,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 2fd0dd81622f6..96d9a63fafe2f 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From df2aca2f49f1763fecd96a9858df129bde53fa35 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/4] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index 7a798dd521fb0..6d6be38b8f0a9 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -380,9 +380,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 96d9a63fafe2f..5a4c582bfd56d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-24 Thread Matt Arsenault via llvm-branch-commits

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

>From d03f5bc9b08ce48793d2ef52d5ffa104b7ca718b Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/4] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index 8641ab6f28e33..5b1513b084a25 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5623,6 +5623,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index 4b1d7367f103d..7a798dd521fb0 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -375,7 +375,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 2fd0dd81622f6..96d9a63fafe2f 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From df2aca2f49f1763fecd96a9858df129bde53fa35 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/4] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index 7a798dd521fb0..6d6be38b8f0a9 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -380,9 +380,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 96d9a63fafe2f..5a4c582bfd56d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-22 Thread Matt Arsenault via llvm-branch-commits

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

>From c71cd27a3f63f92812c9371d3ce216b4ad03389b Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/4] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index d1f620ac9eb3d..6b0abc99632bc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index dc7f6d3ca237d..a192f19e129f4 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 2fd0dd81622f6..96d9a63fafe2f 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From 2d180e7c6ec6766a8fd1017e26a39be5a607978b Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/4] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index a192f19e129f4..8a13c91780d50 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -362,9 +362,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 96d9a63fafe2f..5a4c582bfd56d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-22 Thread Matt Arsenault via llvm-branch-commits

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

>From c71cd27a3f63f92812c9371d3ce216b4ad03389b Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/4] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index d1f620ac9eb3d..6b0abc99632bc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index dc7f6d3ca237d..a192f19e129f4 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 2fd0dd81622f6..96d9a63fafe2f 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From 2d180e7c6ec6766a8fd1017e26a39be5a607978b Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/4] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index a192f19e129f4..8a13c91780d50 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -362,9 +362,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 96d9a63fafe2f..5a4c582bfd56d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-21 Thread Matt Arsenault via llvm-branch-commits

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

>From 9df35b3f1d0576cc9c1d3f64f8bf03572f96ce32 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/4] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index d1f620ac9eb3d..6b0abc99632bc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index dc7f6d3ca237d..a192f19e129f4 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 2fd0dd81622f6..96d9a63fafe2f 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From 9d5cb9fe4a4b4ee6016cba3c80422df2bfc874a6 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/4] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index a192f19e129f4..8a13c91780d50 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -362,9 +362,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 96d9a63fafe2f..5a4c582bfd56d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-21 Thread Matt Arsenault via llvm-branch-commits

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

>From 9df35b3f1d0576cc9c1d3f64f8bf03572f96ce32 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/4] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index d1f620ac9eb3d..6b0abc99632bc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index dc7f6d3ca237d..a192f19e129f4 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 2fd0dd81622f6..96d9a63fafe2f 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From 9d5cb9fe4a4b4ee6016cba3c80422df2bfc874a6 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/4] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index a192f19e129f4..8a13c91780d50 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -362,9 +362,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 96d9a63fafe2f..5a4c582bfd56d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-15 Thread Matt Arsenault via llvm-branch-commits

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

>From 6d01a14d16e596c6b4a626e970abedcd3a309561 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/4] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index d1f620ac9eb3d..6b0abc99632bc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index dc7f6d3ca237d..a192f19e129f4 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 2fd0dd81622f6..96d9a63fafe2f 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From 9f5259b5f079fd432ea8d8b6dcad5b1b322c8329 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/4] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index a192f19e129f4..8a13c91780d50 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -362,9 +362,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 96d9a63fafe2f..5a4c582bfd56d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-15 Thread Matt Arsenault via llvm-branch-commits

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

>From 550d190e3342f5029a184169db3cbf31a5008509 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/3] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index d1f620ac9eb3d..6b0abc99632bc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index dc7f6d3ca237d..a192f19e129f4 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index c4cdfd5e2b5a7..df42d9fb30058 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From 55835d0354adbd9f756ea626772728bee6e4f233 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/3] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index a192f19e129f4..8a13c91780d50 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -362,9 +362,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index df42d9fb30058..6d4ce9de7f90d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-15 Thread Matt Arsenault via llvm-branch-commits

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

>From 550d190e3342f5029a184169db3cbf31a5008509 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH 1/3] ValueTracking: Improve nan tracking for fma square
 special case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index d1f620ac9eb3d..6b0abc99632bc 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index dc7f6d3ca237d..a192f19e129f4 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd_impl(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd_impl(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  Known.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index c4cdfd5e2b5a7..df42d9fb30058 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call half @llvm.fma.f16(half noundef 
nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

>From 55835d0354adbd9f756ea626772728bee6e4f233 Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Thu, 15 Jan 2026 19:40:26 +0100
Subject: [PATCH 2/3] fix too conservative

---
 llvm/lib/Support/KnownFPClass.cpp| 4 +---
 llvm/test/Transforms/Attributor/nofpclass-fma.ll | 4 ++--
 2 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index a192f19e129f4..8a13c91780d50 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -362,9 +362,7 @@ KnownFPClass KnownFPClass::fma_square(const KnownFPClass 
&KnownSquared,
 
   // Since we know the squared input must be positive, the add of opposite sign
   // infinities nan hazard only applies for negative nan.
-  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
-  Known.isKnownNever(fcPosInf | fcNan) &&
-  KnownSquared.isKnownNeverLogicalZero(Mode))
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) && 
Squared.isKnownNever(fcNan))
 Known.knownNot(fcNan);
 
   return Known;
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index df42d9fb30058..6d4ce9de7f90d 100644
--- a/llvm/test/Transform

[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-14 Thread Matt Arsenault via llvm-branch-commits

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

>From 246522cde1987db996a2c313f827ff9bb21b967d Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH] ValueTracking: Improve nan tracking for fma square special
 case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index a99fca1de3118..3f16eda0b475a 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index e96d4b94ec07c..d9ff6da17a876 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  KnownSquared.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 84f89f3c463d9..e8e99584a001d 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define nofpclass(nzero) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan nzero) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nzero) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan nzero) half 
@llvm.fma.f16(half noundef nofpclass(nan inf zero) [[ARG0]], half noundef 
nofpclass(nan inf zero) [[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-14 Thread Matt Arsenault via llvm-branch-commits

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

>From 246522cde1987db996a2c313f827ff9bb21b967d Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH] ValueTracking: Improve nan tracking for fma square special
 case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index a99fca1de3118..3f16eda0b475a 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index e96d4b94ec07c..d9ff6da17a876 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  KnownSquared.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 84f89f3c463d9..e8e99584a001d 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define nofpclass(nzero) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan nzero) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nzero) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan nzero) half 
@llvm.fma.f16(half noundef nofpclass(nan inf zero) [[ARG0]], half noundef 
nofpclass(nan inf zero) [[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-14 Thread via llvm-branch-commits

llvmbot wrote:



@llvm/pr-subscribers-llvm-adt

@llvm/pr-subscribers-llvm-support

Author: Matt Arsenault (arsenm)


Changes

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.

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


3 Files Affected:

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


``diff
diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index a99fca1de3118..3f16eda0b475a 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index e96d4b94ec07c..d9ff6da17a876 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  KnownSquared.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 84f89f3c463d9..e8e99584a001d 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define nofpclass(nzero) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan nzero) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nzero) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan nzero) half 
@llvm.fma.f16(half noundef nofpclass(nan inf zero) [[ARG0]], half noundef 
nofpclass(nan inf zero) [[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

``




https://github.com/llvm/llvm-project/pull/175999
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-14 Thread Matt Arsenault via llvm-branch-commits

arsenm wrote:

> [!WARNING]
> This pull request is not mergeable via GitHub because a downstack PR is 
> open. Once all requirements are satisfied, merge this PR as a stack  href="https://app.graphite.com/github/pr/llvm/llvm-project/175999?utm_source=stack-comment-downstack-mergeability-warning";
>  >on Graphite.
> https://graphite.dev/docs/merge-pull-requests";>Learn more

* **#175999** https://app.graphite.com/github/pr/llvm/llvm-project/175999?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/> 👈 https://app.graphite.com/github/pr/llvm/llvm-project/175999?utm_source=stack-comment-view-in-graphite";
 target="_blank">(View in Graphite)
* **#175614** https://app.graphite.com/github/pr/llvm/llvm-project/175614?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>: 1 other dependent PR 
([#175615](https://github.com/llvm/llvm-project/pull/175615) https://app.graphite.com/github/pr/llvm/llvm-project/175615?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>)
* **#175613** https://app.graphite.com/github/pr/llvm/llvm-project/175613?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#174853** https://app.graphite.com/github/pr/llvm/llvm-project/174853?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* **#174852** https://app.graphite.com/github/pr/llvm/llvm-project/174852?utm_source=stack-comment-icon";
 target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" 
width="10px" height="10px"/>
* `main`




This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn 
more about https://stacking.dev/?utm_source=stack-comment";>stacking.


https://github.com/llvm/llvm-project/pull/175999
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-14 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm ready_for_review 
https://github.com/llvm/llvm-project/pull/175999
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] ValueTracking: Improve nan tracking for fma square special case (PR #175999)

2026-01-14 Thread Matt Arsenault via llvm-branch-commits

https://github.com/arsenm created 
https://github.com/llvm/llvm-project/pull/175999

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.

>From f980e41db3cbd2ae5121c18dd9e1c75fae7e49ae Mon Sep 17 00:00:00 2001
From: Matt Arsenault 
Date: Mon, 12 Jan 2026 14:28:25 +0100
Subject: [PATCH] ValueTracking: Improve nan tracking for fma square special
 case

In the square multiply case, we can infer if the add of opposite
sign infinities can occur.
---
 llvm/lib/Analysis/ValueTracking.cpp  |  4 
 llvm/lib/Support/KnownFPClass.cpp| 12 +++-
 llvm/test/Transforms/Attributor/nofpclass-fma.ll |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/ValueTracking.cpp 
b/llvm/lib/Analysis/ValueTracking.cpp
index a99fca1de3118..3f16eda0b475a 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -5661,6 +5661,10 @@ void computeKnownFPClass(const Value *V, const APInt 
&DemandedElts,
   Q, Depth + 1);
 }
 
+// TODO: Improve accuracy in unfused FMA pattern. We can prove an 
additional
+// not-nan if the addend is known-not negative infinity if the multiply is
+// known-not infinity.
+
 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
 Q, Depth + 1);
 
diff --git a/llvm/lib/Support/KnownFPClass.cpp 
b/llvm/lib/Support/KnownFPClass.cpp
index e96d4b94ec07c..d9ff6da17a876 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -357,7 +357,17 @@ KnownFPClass KnownFPClass::fma(const KnownFPClass 
&KnownLHS,
 KnownFPClass KnownFPClass::fma_square(const KnownFPClass &KnownSquared,
   const KnownFPClass &KnownAddend,
   DenormalMode Mode) {
-  return fadd(square(KnownSquared, Mode), KnownAddend, Mode);
+  KnownFPClass Squared = square(KnownSquared, Mode);
+  KnownFPClass Known = fadd(Squared, KnownAddend, Mode);
+
+  // Since we know the squared input must be positive, the add of opposite sign
+  // infinities nan hazard only applies for negative nan.
+  if (KnownAddend.isKnownNever(fcNegInf | fcNan) &&
+  KnownSquared.isKnownNever(fcPosInf | fcNan) &&
+  KnownSquared.isKnownNeverLogicalZero(Mode))
+Known.knownNot(fcNan);
+
+  return Known;
 }
 
 KnownFPClass KnownFPClass::exp(const KnownFPClass &KnownSrc) {
diff --git a/llvm/test/Transforms/Attributor/nofpclass-fma.ll 
b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
index 84f89f3c463d9..e8e99584a001d 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-fma.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-fma.ll
@@ -454,9 +454,9 @@ define half 
@ret_fma_square__no_nan_no_inf__no_nan_no_ninf(half noundef nofpclas
 }
 
 define half @ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf(half 
noundef nofpclass(nan inf zero) %arg0, half nofpclass(nan ninf) %arg1) {
-; CHECK-LABEL: define nofpclass(nzero) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
+; CHECK-LABEL: define nofpclass(nan nzero) half 
@ret_fma_square__no_nan_no_inf_no_zero__no_nan_no_ninf
 ; CHECK-SAME: (half noundef nofpclass(nan inf zero) [[ARG0:%.*]], half 
nofpclass(nan ninf) [[ARG1:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nzero) half @llvm.fma.f16(half 
noundef nofpclass(nan inf zero) [[ARG0]], half noundef nofpclass(nan inf zero) 
[[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
+; CHECK-NEXT:[[CALL:%.*]] = call nofpclass(nan nzero) half 
@llvm.fma.f16(half noundef nofpclass(nan inf zero) [[ARG0]], half noundef 
nofpclass(nan inf zero) [[ARG0]], half nofpclass(nan ninf) [[ARG1]]) #[[ATTR2]]
 ; CHECK-NEXT:ret half [[CALL]]
 ;
   %call = call half @llvm.fma.f16(half %arg0, half %arg0, half %arg1)

___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits