diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 139a0b81e299b..b7a1d75e408fa 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); @@ -2602,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/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() { 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