[llvm] [ConstantFold] Special case atan +/-0.0 (PR #143962)
Lei Huang via llvm-commits
llvm-commits at lists.llvm.org
Thu Jun 19 12:31:58 PDT 2025
https://github.com/lei137 updated https://github.com/llvm/llvm-project/pull/143962
>From 1bb408adb38cf7316073df3cd71939a748e6626d Mon Sep 17 00:00:00 2001
From: Lei Huang <lei at ca.ibm.com>
Date: Wed, 11 Jun 2025 14:17:08 -0400
Subject: [PATCH 1/3] Revert "[PowerPC][AIX] xfail atan-intrinsic to unblock
bot (#143723)"
This reverts commit adfea33f0c412b8475b755a8d82c9961b785eb02.
---
llvm/test/Transforms/InstSimplify/ConstProp/atan-intrinsic.ll | 1 -
1 file changed, 1 deletion(-)
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/atan-intrinsic.ll b/llvm/test/Transforms/InstSimplify/ConstProp/atan-intrinsic.ll
index c5c17d65524c2..d824d6d35643d 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/atan-intrinsic.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/atan-intrinsic.ll
@@ -1,6 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
-; XFAIL: target={{.*}}-aix{{.*}}
define double @test_atan_0() {
; CHECK-LABEL: define double @test_atan_0() {
>From 139bf5bff27588f812f165c05a086581beb29dec Mon Sep 17 00:00:00 2001
From: Lei Huang <lei at ca.ibm.com>
Date: Thu, 12 Jun 2025 12:08:31 -0400
Subject: [PATCH 2/3] [ConstantFold] Special case atan +/-0.0
C's Annex F specifies that atan +/-0.0 returns the input value;
however, this behavior is optional and host C libraries may behave
differently. This change applies the Annex F behavior to constant
folding by LLVM.
Ref: https://pubs.opengroup.org/onlinepubs/9799919799/functions/atan.html
---
llvm/lib/Analysis/ConstantFolding.cpp | 3 +++
1 file changed, 3 insertions(+)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 139a0b81e299b..6a54c7355d70a 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2549,6 +2549,9 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
case Intrinsic::cosh:
return ConstantFoldFP(cosh, APF, Ty);
case Intrinsic::atan:
+ // Implement optional behavior from C's Annex F for +/-0.0.
+ if (U.isZero())
+ return ConstantFP::get(Ty->getContext(), U);
return ConstantFoldFP(atan, APF, Ty);
case Intrinsic::sqrt:
return ConstantFoldFP(sqrt, APF, Ty);
>From 0a64bc52fa42ac13659ec37b17447009f67ac7e8 Mon Sep 17 00:00:00 2001
From: Lei Huang <lei at ca.ibm.com>
Date: Thu, 19 Jun 2025 15:31:42 -0400
Subject: [PATCH 3/3] add test and handling for atan lib calls
---
llvm/lib/Analysis/ConstantFolding.cpp | 3 +++
llvm/test/Transforms/InstSimplify/ConstProp/calls.ll | 12 ++++++++++++
2 files changed, 15 insertions(+)
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 6a54c7355d70a..b7a1d75e408fa 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -2605,6 +2605,9 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
break;
case LibFunc_atan:
case LibFunc_atanf:
+ // Implement optional behavior from C's Annex F for +/-0.0.
+ if (U.isZero())
+ return ConstantFP::get(Ty->getContext(), U);
if (TLI->has(Func))
return ConstantFoldFP(atan, APF, Ty);
break;
diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/calls.ll b/llvm/test/Transforms/InstSimplify/ConstProp/calls.ll
index 61a30c781c0f4..26fb8c0d7a1c6 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/calls.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/calls.ll
@@ -202,5 +202,17 @@ entry:
ret float %0
}
+define float @test_atan_negzero() nounwind uwtable ssp {
+entry:
+; CHECK-LABEL: @test_atan_negzero(
+; CHECK: ret float -0.000000e+00
+;
+; FNOBUILTIN-LABEL: @test_atan_negzero(
+; FNOBUILTIN: ret float -0.000000e+00
+;
+ %1 = call float @atanf(float -0.0)
+ ret float %1
+}
+
declare double @llvm.pow.f64(double, double) nounwind readonly
declare float @llvm.pow.f32(float, float) nounwind readonly
More information about the llvm-commits
mailing list