Skip to content

Commit 248821e

Browse files
lei137Anthony Tran
authored andcommitted
[ConstantFold] Special case atan +/-0.0 (llvm#143962)
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
1 parent 0f859f6 commit 248821e

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

llvm/lib/Analysis/ConstantFolding.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,9 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
25532553
case Intrinsic::cosh:
25542554
return ConstantFoldFP(cosh, APF, Ty);
25552555
case Intrinsic::atan:
2556+
// Implement optional behavior from C's Annex F for +/-0.0.
2557+
if (U.isZero())
2558+
return ConstantFP::get(Ty->getContext(), U);
25562559
return ConstantFoldFP(atan, APF, Ty);
25572560
case Intrinsic::sqrt:
25582561
return ConstantFoldFP(sqrt, APF, Ty);
@@ -2606,6 +2609,9 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
26062609
break;
26072610
case LibFunc_atan:
26082611
case LibFunc_atanf:
2612+
// Implement optional behavior from C's Annex F for +/-0.0.
2613+
if (U.isZero())
2614+
return ConstantFP::get(Ty->getContext(), U);
26092615
if (TLI->has(Func))
26102616
return ConstantFoldFP(atan, APF, Ty);
26112617
break;

llvm/test/Transforms/InstSimplify/ConstProp/atan-intrinsic.ll

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt -S -passes=instsimplify < %s | FileCheck %s
3-
; XFAIL: target={{.*}}-aix{{.*}}
43

54
define double @test_atan_0() {
65
; CHECK-LABEL: define double @test_atan_0() {

llvm/test/Transforms/InstSimplify/ConstProp/calls.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,5 +202,17 @@ entry:
202202
ret float %0
203203
}
204204

205+
define float @test_atan_negzero() nounwind uwtable ssp {
206+
entry:
207+
; CHECK-LABEL: @test_atan_negzero(
208+
; CHECK: ret float -0.000000e+00
209+
;
210+
; FNOBUILTIN-LABEL: @test_atan_negzero(
211+
; FNOBUILTIN: ret float -0.000000e+00
212+
;
213+
%1 = call float @atanf(float -0.0)
214+
ret float %1
215+
}
216+
205217
declare double @llvm.pow.f64(double, double) nounwind readonly
206218
declare float @llvm.pow.f32(float, float) nounwind readonly

0 commit comments

Comments
 (0)