[llvm] [KnownFPClass] Improve known classes for acos (PR #214603)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 6 17:56:39 PDT 2026
https://github.com/ZERICO2005 created https://github.com/llvm/llvm-project/pull/214603
Finishes the last remaining TODO from https://github.com/llvm/llvm-project/issues/211686
Improves the KnownFPClass for `acos` in two ways:
- `acos(x) == +0.0 iff x == +1.0`, so if `x` is not a positive normal then `acos(x)` cannot return zero.
- `acos(x)` can never return a subnormal value.
Proof that `acos(x)` never produces a subnormal result:
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.
>From 294d4eab01eff155dbf94bc5c02591012b1ed6e6 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 | 29 +++++++++++++++++--
3 files changed, 53 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Support/KnownFPClass.cpp b/llvm/lib/Support/KnownFPClass.cpp
index eccd83451a05f..fe00d8cf2cb93 100644
--- a/llvm/lib/Support/KnownFPClass.cpp
+++ b/llvm/lib/Support/KnownFPClass.cpp
@@ -656,9 +656,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);
// NaN propagates. acos(x) is also NaN for |x| > 1, so we cannot rule
// out NaN without knowing the source is in [-1, 1].
diff --git a/llvm/test/Transforms/Attributor/nofpclass-trig.ll b/llvm/test/Transforms/Attributor/nofpclass-trig.ll
index c832432d42e31..a5ee68d7192a6 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(inf nzero nsub nnorm) float @ret_acos_nonan
+; CHECK-LABEL: define nofpclass(inf nzero sub nnorm) float @ret_acos_nonan
; CHECK-SAME: (float nofpclass(nan) [[ARG:%.*]]) #[[ATTR1]] {
-; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(inf nzero nsub nnorm) float @llvm.acos.f32(float nofpclass(nan) [[ARG]]) #[[ATTR2]]
+; CHECK-NEXT: [[CALL:%.*]] = call nofpclass(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 9e3ed2f159b6f..a2f0967cfd678 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);
}
@@ -1559,7 +1559,30 @@ TEST_F(AArch64GISelMITest, TestFPClassFAcosPos) {
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);
+}
+
+TEST_F(AArch64GISelMITest, TestFPClassFAcosNeg) {
+ // For -1 <= x <= 0, acos returns a positive normal value.
+ // For x < -1, acos returns NaN.
+ StringRef MIRString = R"(
+ %ptr:_(p0) = G_IMPLICIT_DEF
+ %val:_(s32) = G_LOAD %ptr(p0) :: (load (s32))
+ %fabs:_(s32) = nnan ninf G_FABS %val
+ %fneg:_(s32) = G_FNEG %fabs
+ %facos:_(s32) = G_FACOS %fneg
+ %copy:_(s32) = COPY %facos
+)";
+ setUp(MIRString);
+ if (!TM)
+ GTEST_SKIP();
+ Register CopyReg = Copies[Copies.size() - 1];
+ MachineInstr *FinalCopy = MRI->getVRegDef(CopyReg);
+ Register SrcReg = FinalCopy->getOperand(1).getReg();
+ GISelValueTracking Info(*MF);
+ KnownFPClass Known = Info.computeKnownFPClass(SrcReg);
+ EXPECT_EQ(fcPosNormal | fcNan, Known.KnownFPClasses);
EXPECT_EQ(std::nullopt, Known.SignBit);
}
More information about the llvm-commits
mailing list