[llvm] [KnownFPClass] Improve known classes for `acos` (PR #214603)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 12 06:53:28 PDT 2026
https://github.com/ZERICO2005 updated https://github.com/llvm/llvm-project/pull/214603
>From 04c70180184ab700a9814bf04df2965a5a848eb8 Mon Sep 17 00:00:00 2001
From: zerico <zerico2005 at gmail.com>
Date: Thu, 6 Aug 2026 18:48:08 -0600
Subject: [PATCH] [KnownFPClass] Improve known classes for acos
acos can never return a subnormal value. Additionally, acos returns +0.0
only for an input of +1.0, so it cannot return zero when the input is
known not to be a positive normal value.
---
llvm/lib/Support/KnownFPClass.cpp | 13 ++++++++---
.../Transforms/Attributor/nofpclass-trig.ll | 22 ++++++++++++++-----
.../CodeGen/GlobalISel/KnownFPClassTest.cpp | 4 ++--
3 files changed, 29 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Support/KnownFPClass.cpp b/llvm/lib/Support/KnownFPClass.cpp
index 0d72e2317ef6f..875792052b8b7 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -659,9 +659,16 @@ KnownFPClass KnownFPClass::asin(const KnownFPClass &KnownSrc) {
KnownFPClass KnownFPClass::acos(const KnownFPClass &KnownSrc) {
KnownFPClass Known;
- // acos is bounded to [0, pi], never Inf or negative.
- Known.knownNot(fcInf);
- Known.knownNot(fcNegative);
+ // acos(x) is bounded to [0, pi] for -1 <= x <= 1, and is never negative,
+ // infinite, or subnormal. The smallest non-zero value occurs when x is
+ // close to 1.0, where acos(x) can be approximated by sqrt(2 * (1 - x)).
+ // Since sqrt cannot produce a subnormal result, we can conclude that
+ // acos(x) will also never produce a subnormal result.
+ Known.knownNot(fcNegative | fcInf | fcSubnormal);
+
+ // acos(x) == +0.0 iff x == +1.0
+ if (KnownSrc.isKnownNever(fcPosNormal))
+ Known.knownNot(fcZero);
if (KnownSrc.isKnownNever(fcSNan))
Known.knownNot(fcSNan);
diff --git a/llvm/test/Transforms/Attributor/nofpclass-trig.ll b/llvm/test/Transforms/Attributor/nofpclass-trig.ll
index f4eb5ffc3c71b..87ceaf4c54ba3 100644
--- a/llvm/test/Transforms/Attributor/nofpclass-trig.ll
+++ b/llvm/test/Transforms/Attributor/nofpclass-trig.ll
@@ -146,11 +146,11 @@ define float @ret_asin_nonan(float nofpclass(nan) %arg) {
ret float %call
}
-; acos is bounded to [0, pi], never Inf or negative.
+; acos is bounded to [0, pi], never infinite, negative, or subnormal.
define float @ret_acos(float %arg) {
-; CHECK-LABEL: define nofpclass(inf nzero nsub nnorm) float @ret_acos
+; CHECK-LABEL: define nofpclass(inf nzero sub nnorm) float @ret_acos
; CHECK-SAME: (float [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nzero nsub nnorm) float @llvm.acos.f32(float [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nzero sub nnorm) float @llvm.acos.f32(float [[ARG]]) #[[ATTR2]]
; CHECK-NEXT: ret float [[CALL]]
;
%call = call float @llvm.acos.f32(float %arg)
@@ -158,9 +158,21 @@ define float @ret_acos(float %arg) {
}
define float @ret_acos_nonan(float nofpclass(nan) %arg) {
-; CHECK-LABEL: define nofpclass(snan inf nzero nsub nnorm) float @ret_acos_nonan
+; CHECK-LABEL: define nofpclass(snan inf nzero sub nnorm) float @ret_acos_nonan
; CHECK-SAME: (float nofpclass(nan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(snan inf nzero nsub nnorm) float @llvm.acos.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(snan inf nzero sub nnorm) float @llvm.acos.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT: ret float [[CALL]]
+;
+ %call = call float @llvm.acos.f32(float %arg)
+ ret float %call
+}
+
+; acos(x) == +0.0 iff x == +1.0
+define float @ret_acos_no_pos_normal(
+ float nofpclass(pnorm) %arg) {
+; CHECK-LABEL: define nofpclass(inf zero sub nnorm) float @ret_acos_no_pos_normal
+; CHECK-SAME: (float nofpclass(pnorm) [[ARG:%.*]]) #[[ATTR1]] {
+; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf zero sub nnorm) float @llvm.acos.f32(float nofpclass(pnorm) [[ARG]]) #[[ATTR2]]
; CHECK-NEXT: ret float [[CALL]]
;
%call = call float @llvm.acos.f32(float %arg)
diff --git a/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp b/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
index 95fda0a19c19f..3f74405f026f4 100644
--- a/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
+++ b/llvm/unittests/CodeGen/GlobalISel/KnownFPClassTest.cpp
@@ -1522,7 +1522,7 @@ TEST_F(AArch64GISelMITest, TestFPClassFAsinPos) {
}
TEST_F(AArch64GISelMITest, TestFPClassFAcos) {
- // acos is bounded to [0, π]: never Inf, never negative.
+ // acos is bounded to [0, π]: never infinite, negative, or subnormal.
StringRef MIRString = R"(
%ptr:_(p0) = G_IMPLICIT_DEF
%val:_(s32) = G_LOAD %ptr(p0) :: (load (s32))
@@ -1537,7 +1537,7 @@ TEST_F(AArch64GISelMITest, TestFPClassFAcos) {
Register SrcReg = FinalCopy->getOperand(1).getReg();
GISelValueTracking Info(*MF);
KnownFPClass Known = Info.computeKnownFPClass(SrcReg);
- EXPECT_EQ(fcPosFinite | fcNan, Known.KnownFPClasses);
+ EXPECT_EQ(fcPosZero | fcPosNormal | fcNan, Known.KnownFPClasses);
EXPECT_EQ(std::nullopt, Known.SignBit);
}
More information about the llvm-commits
mailing list