[SelectionDAG] Allow sin/cos -> sincos optimization on GNU triples w/ just -fno-math-errno

Summary:
This change enables the sin(x) cos(x) -> sincos(x) optimization on GNU
target triples.  This optimization was being inhibited when -ffast-math
wasn't set because sincos in GLibC does not set errno, while sin and cos
do.  However, this optimization will only run if the attributes on the
sin/cos calls include readnone, which is how clang represents the fact
that it doesn't care about the errno values set by these functions (via
the -fno-math-errno flag).

Reviewers: hfinkel, bogner

Subscribers: mcrosier, javed.absar, llvm-commits, paul.redmond

Differential Revision: https://reviews.llvm.org/D32921

llvm-svn: 305204
This commit is contained in:
Geoff Berry 2017-06-12 17:15:41 +00:00
parent d9b77848f2
commit 06c9dc3d9c
6 changed files with 265 additions and 107 deletions

View File

@ -2192,19 +2192,6 @@ static bool isSinCosLibcallAvailable(SDNode *Node, const TargetLowering &TLI) {
return TLI.getLibcallName(LC) != nullptr;
}
/// Return true if sincos libcall is available and can be used to combine sin
/// and cos.
static bool canCombineSinCosLibcall(SDNode *Node, const TargetLowering &TLI,
const TargetMachine &TM) {
if (!isSinCosLibcallAvailable(Node, TLI))
return false;
// GNU sin/cos functions set errno while sincos does not. Therefore
// combining sin and cos is only safe if unsafe-fpmath is enabled.
if (TM.getTargetTriple().isGNUEnvironment() && !TM.Options.UnsafeFPMath)
return false;
return true;
}
/// Only issue sincos libcall if both sin and cos are needed.
static bool useSinCos(SDNode *Node) {
unsigned OtherOpcode = Node->getOpcode() == ISD::FSIN
@ -3247,7 +3234,7 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
// Turn fsin / fcos into ISD::FSINCOS node if there are a pair of fsin /
// fcos which share the same operand and both are used.
if ((TLI.isOperationLegalOrCustom(ISD::FSINCOS, VT) ||
canCombineSinCosLibcall(Node, TLI, TM))
isSinCosLibcallAvailable(Node, TLI))
&& useSinCos(Node)) {
SDVTList VTs = DAG.getVTList(VT, VT);
Tmp1 = DAG.getNode(ISD::FSINCOS, dl, VTs, Node->getOperand(0));

View File

@ -1,7 +1,9 @@
; RUN: llc < %s -mtriple=arm64-apple-ios7 | FileCheck %s --check-prefix CHECK-IOS
; RUN: llc < %s -mtriple=arm64-linux-gnu | FileCheck %s --check-prefix CHECK-LINUX
; Combine sin / cos into a single call.
; Combine sin / cos into a single call unless they may write errno (as
; captured by readnone attrbiute, controlled by clang -fmath-errno
; setting).
; rdar://12856873
define float @test1(float %x) nounwind {
@ -11,11 +13,26 @@ entry:
; CHECK-IOS: fadd s0, s0, s1
; CHECK-LINUX-LABEL: test1:
; CHECK-LINUX: bl sincosf
%call = tail call float @sinf(float %x) readnone
%call1 = tail call float @cosf(float %x) readnone
%add = fadd float %call, %call1
ret float %add
}
define float @test1_errno(float %x) nounwind {
entry:
; CHECK-IOS-LABEL: test1_errno:
; CHECK-IOS: bl _sinf
; CHECK-IOS: bl _cosf
; CHECK-LINUX-LABEL: test1_errno:
; CHECK-LINUX: bl sinf
; CHECK-LINUX: bl cosf
%call = tail call float @sinf(float %x) nounwind readnone
%call1 = tail call float @cosf(float %x) nounwind readnone
%call = tail call float @sinf(float %x)
%call1 = tail call float @cosf(float %x)
%add = fadd float %call, %call1
ret float %add
}
@ -27,16 +44,31 @@ entry:
; CHECK-IOS: fadd d0, d0, d1
; CHECK-LINUX-LABEL: test2:
; CHECK-LINUX: bl sin
; CHECK-LINUX: bl cos
; CHECK-LINUX: bl sincos
%call = tail call double @sin(double %x) nounwind readnone
%call1 = tail call double @cos(double %x) nounwind readnone
%call = tail call double @sin(double %x) readnone
%call1 = tail call double @cos(double %x) readnone
%add = fadd double %call, %call1
ret double %add
}
declare float @sinf(float) readonly
declare double @sin(double) readonly
declare float @cosf(float) readonly
declare double @cos(double) readonly
define double @test2_errno(double %x) nounwind {
entry:
; CHECK-IOS-LABEL: test2_errno:
; CHECK-IOS: bl _sin
; CHECK-IOS: bl _cos
; CHECK-LINUX-LABEL: test2_errno:
; CHECK-LINUX: bl sin
; CHECK-LINUX: bl cos
%call = tail call double @sin(double %x)
%call1 = tail call double @cos(double %x)
%add = fadd double %call, %call1
ret double %add
}
declare float @sinf(float)
declare double @sin(double)
declare float @cosf(float)
declare double @cos(double)

View File

@ -1,8 +1,18 @@
; RUN: llc -mtriple=aarch64-linux-gnu -verify-machineinstrs -o - %s | FileCheck %s
define float @test_sincos_f32(float %f) {
; CHECK-LABEL: test_sincos_f32:
%sin = call float @sinf(float %f) readnone
%cos = call float @cosf(float %f) readnone
; CHECK: bl sincosf
%val = fadd float %sin, %cos
ret float %val
}
define float @test_sincos_f32_errno(float %f) {
; CHECK-LABEL: test_sincos_f32_errno:
%sin = call float @sinf(float %f)
%cos = call float @cosf(float %f)
; CHECK: bl sinf
; CHECK: bl cosf
%val = fadd float %sin, %cos
@ -10,26 +20,46 @@ define float @test_sincos_f32(float %f) {
}
define double @test_sincos_f64(double %f) {
; CHECK-LABEL: test_sincos_f64:
%sin = call double @sin(double %f) readnone
%cos = call double @cos(double %f) readnone
%val = fadd double %sin, %cos
; CHECK: bl sincos
ret double %val
}
define double @test_sincos_f64_errno(double %f) {
; CHECK-LABEL: test_sincos_f64_errno:
%sin = call double @sin(double %f)
%cos = call double @cos(double %f)
%val = fadd double %sin, %cos
; CHECK: bl sin
; CHECK: bl cos
ret double %val
}
define fp128 @test_sincos_f128(fp128 %f) {
; CHECK-LABEL: test_sincos_f128:
%sin = call fp128 @sinl(fp128 %f) readnone
%cos = call fp128 @cosl(fp128 %f) readnone
%val = fadd fp128 %sin, %cos
; CHECK: bl sincosl
ret fp128 %val
}
define fp128 @test_sincos_f128_errno(fp128 %f) {
; CHECK-LABEL: test_sincos_f128_errno:
%sin = call fp128 @sinl(fp128 %f)
%cos = call fp128 @cosl(fp128 %f)
%val = fadd fp128 %sin, %cos
; CHECK: bl sinl
; CHECK: bl cosl
ret fp128 %val
}
declare float @sinf(float) readonly
declare double @sin(double) readonly
declare fp128 @sinl(fp128) readonly
declare float @cosf(float) readonly
declare double @cos(double) readonly
declare fp128 @cosl(fp128) readonly
declare float @sinf(float)
declare double @sin(double)
declare fp128 @sinl(fp128)
declare float @cosf(float)
declare double @cos(double)
declare fp128 @cosl(fp128)

View File

@ -1,10 +1,12 @@
; RUN: llc < %s -mtriple=armv7-apple-ios6 -mcpu=cortex-a8 | FileCheck %s --check-prefix=NOOPT
; RUN: llc < %s -mtriple=armv7-apple-ios7 -mcpu=cortex-a8 | FileCheck %s --check-prefix=SINCOS
; RUN: llc < %s -mtriple=armv7-linux-gnu -mcpu=cortex-a8 | FileCheck %s --check-prefix=NOOPT-GNU
; RUN: llc < %s -mtriple=armv7-linux-gnu -mcpu=cortex-a8 | FileCheck %s --check-prefix=SINCOS-GNU
; RUN: llc < %s -mtriple=armv7-linux-gnueabi -mcpu=cortex-a8 \
; RUN: --enable-unsafe-fp-math | FileCheck %s --check-prefix=SINCOS-GNU
; Combine sin / cos into a single call.
; Combine sin / cos into a single call unless they may write errno (as
; captured by readnone attrbiute, controlled by clang -fmath-errno
; setting).
; rdar://12856873
define float @test1(float %x) nounwind {
@ -19,12 +21,28 @@ entry:
; NOOPT: bl _sinf
; NOOPT: bl _cosf
; NOOPT-GNU-LABEL: test1:
; NOOPT-GNU: bl sinf
; NOOPT-GNU: bl cosf
%call = tail call float @sinf(float %x) readnone
%call1 = tail call float @cosf(float %x) readnone
%add = fadd float %call, %call1
ret float %add
}
%call = tail call float @sinf(float %x) nounwind readnone
%call1 = tail call float @cosf(float %x) nounwind readnone
define float @test1_errno(float %x) nounwind {
entry:
; SINCOS-LABEL: test1_errno:
; SINCOS: bl _sinf
; SINCOS: bl _cosf
; SINCOS-GNU-LABEL: test1_errno:
; SINCOS-GNU: bl sinf
; SINCOS-GNU: bl cosf
; NOOPT-LABEL: test1_errno:
; NOOPT: bl _sinf
; NOOPT: bl _cosf
%call = tail call float @sinf(float %x)
%call1 = tail call float @cosf(float %x)
%add = fadd float %call, %call1
ret float %add
}
@ -41,16 +59,33 @@ entry:
; NOOPT: bl _sin
; NOOPT: bl _cos
; NOOPT-GNU-LABEL: test2:
; NOOPT-GNU: bl sin
; NOOPT-GNU: bl cos
%call = tail call double @sin(double %x) nounwind readnone
%call1 = tail call double @cos(double %x) nounwind readnone
%call = tail call double @sin(double %x) readnone
%call1 = tail call double @cos(double %x) readnone
%add = fadd double %call, %call1
ret double %add
}
declare float @sinf(float) readonly
declare double @sin(double) readonly
declare float @cosf(float) readonly
declare double @cos(double) readonly
define double @test2_errno(double %x) nounwind {
entry:
; SINCOS-LABEL: test2_errno:
; SINCOS: bl _sin
; SINCOS: bl _cos
; SINCOS-GNU-LABEL: test2_errno:
; SINCOS-GNU: bl sin
; SINCOS-GNU: bl cos
; NOOPT-LABEL: test2_errno:
; NOOPT: bl _sin
; NOOPT: bl _cos
%call = tail call double @sin(double %x)
%call1 = tail call double @cos(double %x)
%add = fadd double %call, %call1
ret double %add
}
declare float @sinf(float)
declare double @sin(double)
declare float @cosf(float)
declare double @cos(double)

View File

@ -1,6 +1,6 @@
; Test that combined sin/cos library call is emitted when appropriate
; RUN: llc < %s -mtriple=s390x-linux-gnu | FileCheck %s --check-prefix=CHECK-NOOPT
; RUN: llc < %s -mtriple=s390x-linux-gnu | FileCheck %s --check-prefix=CHECK-OPT
; RUN: llc < %s -mtriple=s390x-linux-gnu -enable-unsafe-fp-math | FileCheck %s --check-prefix=CHECK-OPT
define float @f1(float %x) {
@ -8,10 +8,18 @@ define float @f1(float %x) {
; CHECK-OPT: brasl %r14, sincosf@PLT
; CHECK-OPT: le %f0, 164(%r15)
; CHECK-OPT: aeb %f0, 160(%r15)
%tmp1 = call float @sinf(float %x) readnone
%tmp2 = call float @cosf(float %x) readnone
%add = fadd float %tmp1, %tmp2
ret float %add
}
; CHECK-NOOPT-LABEL: f1:
; CHECK-NOOPT: brasl %r14, sinf@PLT
; CHECK-NOOPT: brasl %r14, cosf@PLT
define float @f1_errno(float %x) {
; CHECK-OPT-LABEL: f1_errno:
; CHECK-OPT: brasl %r14, sinf@PLT
; CHECK-OPT: ler %f9, %f0
; CHECK-OPT: brasl %r14, cosf@PLT
; CHECK-OPT: aebr %f0, %f9
%tmp1 = call float @sinf(float %x)
%tmp2 = call float @cosf(float %x)
%add = fadd float %tmp1, %tmp2
@ -23,10 +31,18 @@ define double @f2(double %x) {
; CHECK-OPT: brasl %r14, sincos@PLT
; CHECK-OPT: ld %f0, 168(%r15)
; CHECK-OPT: adb %f0, 160(%r15)
%tmp1 = call double @sin(double %x) readnone
%tmp2 = call double @cos(double %x) readnone
%add = fadd double %tmp1, %tmp2
ret double %add
}
; CHECK-NOOPT-LABEL: f2:
; CHECK-NOOPT: brasl %r14, sin@PLT
; CHECK-NOOPT: brasl %r14, cos@PLT
define double @f2_errno(double %x) {
; CHECK-OPT-LABEL: f2_errno:
; CHECK-OPT: brasl %r14, sin@PLT
; CHECK-OPT: ldr %f9, %f0
; CHECK-OPT: brasl %r14, cos@PLT
; CHECK-OPT: adbr %f0, %f9
%tmp1 = call double @sin(double %x)
%tmp2 = call double @cos(double %x)
%add = fadd double %tmp1, %tmp2
@ -37,20 +53,27 @@ define fp128 @f3(fp128 %x) {
; CHECK-OPT-LABEL: f3:
; CHECK-OPT: brasl %r14, sincosl@PLT
; CHECK-OPT: axbr
%tmp1 = call fp128 @sinl(fp128 %x) readnone
%tmp2 = call fp128 @cosl(fp128 %x) readnone
%add = fadd fp128 %tmp1, %tmp2
ret fp128 %add
}
; CHECK-NOOPT-LABEL: f3:
; CHECK-NOOPT: brasl %r14, sinl@PLT
; CHECK-NOOPT: brasl %r14, cosl@PLT
define fp128 @f3_errno(fp128 %x) {
; CHECK-OPT-LABEL: f3_errno:
; CHECK-OPT: brasl %r14, sinl@PLT
; CHECK-OPT: brasl %r14, cosl@PLT
; CHECK-OPT: axbr
%tmp1 = call fp128 @sinl(fp128 %x)
%tmp2 = call fp128 @cosl(fp128 %x)
%add = fadd fp128 %tmp1, %tmp2
ret fp128 %add
}
declare float @sinf(float) readonly
declare double @sin(double) readonly
declare fp128 @sinl(fp128) readonly
declare float @cosf(float) readonly
declare double @cos(double) readonly
declare fp128 @cosl(fp128) readonly
declare float @sinf(float)
declare double @sin(double)
declare fp128 @sinl(fp128)
declare float @cosf(float)
declare double @cos(double)
declare fp128 @cosl(fp128)

View File

@ -1,10 +1,12 @@
; RUN: llc < %s -mtriple=x86_64-apple-macosx10.9.0 -mcpu=core2 | FileCheck %s --check-prefix=OSX_SINCOS
; RUN: llc < %s -mtriple=x86_64-apple-macosx10.8.0 -mcpu=core2 | FileCheck %s --check-prefix=OSX_NOOPT
; RUN: llc < %s -mtriple=x86_64-pc-linux-gnu -mcpu=core2 | FileCheck %s --check-prefix=GNU_NOOPT
; RUN: llc < %s -mtriple=x86_64-pc-linux-gnu -mcpu=core2 -enable-unsafe-fp-math | FileCheck %s --check-prefix=GNU_SINCOS
; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 -mcpu=core2 -enable-unsafe-fp-math | FileCheck %s --check-prefix=GNUX32_SINCOS
; RUN: llc < %s -mtriple=x86_64-pc-linux-gnu -mcpu=core2 | FileCheck %s --check-prefix=GNU_SINCOS
; RUN: llc < %s -mtriple=x86_64-pc-linux-gnu -mcpu=core2 -enable-unsafe-fp-math | FileCheck %s --check-prefix=GNU_SINCOS_FASTMATH
; RUN: llc < %s -mtriple=x86_64-pc-linux-gnux32 -mcpu=core2 -enable-unsafe-fp-math | FileCheck %s --check-prefix=GNU_SINCOS_FASTMATH
; Combine sin / cos into a single call.
; Combine sin / cos into a single call unless they may write errno (as
; captured by readnone attrbiute, controlled by clang -fmath-errno
; setting).
; rdar://13087969
; rdar://13599493
@ -15,25 +17,44 @@ entry:
; GNU_SINCOS: movss 4(%rsp), %xmm0
; GNU_SINCOS: addss (%rsp), %xmm0
; GNUX32_SINCOS-LABEL: test1:
; GNUX32_SINCOS: callq sincosf
; GNUX32_SINCOS: movss 4(%esp), %xmm0
; GNUX32_SINCOS: addss (%esp), %xmm0
; GNU_NOOPT: test1
; GNU_NOOPT: callq sinf
; GNU_NOOPT: callq cosf
; GNU_SINCOS_FASTMATH-LABEL: test1:
; GNU_SINCOS_FASTMATH: callq sincosf
; GNU_SINCOS_FASTMATH: movss 4(%{{[re]}}sp), %xmm0
; GNU_SINCOS_FASTMATH: addss (%{{[re]}}sp), %xmm0
; OSX_SINCOS-LABEL: test1:
; OSX_SINCOS: callq ___sincosf_stret
; OSX_SINCOS: movshdup {{.*}} xmm1 = xmm0[1,1,3,3]
; OSX_SINCOS: addss %xmm1, %xmm0
; OSX_NOOPT: test1
; OSX_NOOPT-LABEL: test1:
; OSX_NOOPT: callq _sinf
; OSX_NOOPT: callq _cosf
%call = tail call float @sinf(float %x) nounwind readnone
%call1 = tail call float @cosf(float %x) nounwind readnone
%call = tail call float @sinf(float %x) readnone
%call1 = tail call float @cosf(float %x) readnone
%add = fadd float %call, %call1
ret float %add
}
define float @test1_errno(float %x) nounwind {
entry:
; GNU_SINCOS-LABEL: test1_errno:
; GNU_SINCOS: callq sinf
; GNU_SINCOS: callq cosf
; GNU_SINCOS_FASTMATH-LABEL: test1_errno:
; GNU_SINCOS_FASTMATH: callq sinf
; GNU_SINCOS_FASTMATH: callq cosf
; OSX_SINCOS-LABEL: test1_errno:
; OSX_SINCOS: callq _sinf
; OSX_SINCOS: callq _cosf
; OSX_NOOPT-LABEL: test1_errno:
; OSX_NOOPT: callq _sinf
; OSX_NOOPT: callq _cosf
%call = tail call float @sinf(float %x)
%call1 = tail call float @cosf(float %x)
%add = fadd float %call, %call1
ret float %add
}
@ -45,24 +66,43 @@ entry:
; GNU_SINCOS: movsd 16(%rsp), %xmm0
; GNU_SINCOS: addsd 8(%rsp), %xmm0
; GNUX32_SINCOS-LABEL: test2:
; GNUX32_SINCOS: callq sincos
; GNUX32_SINCOS: movsd 16(%esp), %xmm0
; GNUX32_SINCOS: addsd 8(%esp), %xmm0
; GNU_NOOPT: test2:
; GNU_NOOPT: callq sin
; GNU_NOOPT: callq cos
; GNU_SINCOS_FASTMATH-LABEL: test2:
; GNU_SINCOS_FASTMATH: callq sincos
; GNU_SINCOS_FASTMATH: movsd 16(%{{[re]}}sp), %xmm0
; GNU_SINCOS_FASTMATH: addsd 8(%{{[re]}}sp), %xmm0
; OSX_SINCOS-LABEL: test2:
; OSX_SINCOS: callq ___sincos_stret
; OSX_SINCOS: addsd %xmm1, %xmm0
; OSX_NOOPT: test2
; OSX_NOOPT-LABEL: test2:
; OSX_NOOPT: callq _sin
; OSX_NOOPT: callq _cos
%call = tail call double @sin(double %x) nounwind readnone
%call1 = tail call double @cos(double %x) nounwind readnone
%call = tail call double @sin(double %x) readnone
%call1 = tail call double @cos(double %x) readnone
%add = fadd double %call, %call1
ret double %add
}
define double @test2_errno(double %x) nounwind {
entry:
; GNU_SINCOS-LABEL: test2_errno:
; GNU_SINCOS: callq sin
; GNU_SINCOS: callq cos
; GNU_SINCOS_FASTMATH-LABEL: test2_errno:
; GNU_SINCOS_FASTMATH: callq sin
; GNU_SINCOS_FASTMATH: callq cos
; OSX_SINCOS-LABEL: test2_errno:
; OSX_SINCOS: callq _sin
; OSX_SINCOS: callq _cos
; OSX_NOOPT-LABEL: test2_errno:
; OSX_NOOPT: callq _sin
; OSX_NOOPT: callq _cos
%call = tail call double @sin(double %x)
%call1 = tail call double @cos(double %x)
%add = fadd double %call, %call1
ret double %add
}
@ -70,29 +110,40 @@ entry:
define x86_fp80 @test3(x86_fp80 %x) nounwind {
entry:
; GNU_SINCOS-LABEL: test3:
; GNU_SINCOS: callq sinl
; GNU_SINCOS: callq cosl
; GNU_SINCOS: ret
; GNU_SINCOS: callq sincosl
; GNU_SINCOS: fldt 16(%rsp)
; GNU_SINCOS: fldt 32(%rsp)
; GNU_SINCOS: faddp %st(1)
; GNUX32_SINCOS-LABEL: test3:
; GNUX32_SINCOS: callq sinl
; GNUX32_SINCOS: callq cosl
; GNUX32_SINCOS: ret
; GNU_NOOPT: test3:
; GNU_NOOPT: callq sinl
; GNU_NOOPT: callq cosl
%call = tail call x86_fp80 @sinl(x86_fp80 %x) nounwind
%call1 = tail call x86_fp80 @cosl(x86_fp80 %x) nounwind
; GNU_SINCOS_FASTMATH-LABEL: test3:
; GNU_SINCOS_FASTMATH: fsin
; GNU_SINCOS_FASTMATH: fcos
; GNU_SINCOS_FASTMATH: faddp %st(1)
; GNU_SINCOS_FASTMATH: ret
%call = tail call x86_fp80 @sinl(x86_fp80 %x) readnone
%call1 = tail call x86_fp80 @cosl(x86_fp80 %x) readnone
%add = fadd x86_fp80 %call, %call1
ret x86_fp80 %add
}
declare float @sinf(float) readonly
declare double @sin(double) readonly
declare float @cosf(float) readonly
declare double @cos(double) readonly
define x86_fp80 @test3_errno(x86_fp80 %x) nounwind {
entry:
; GNU_SINCOS-LABEL: test3_errno:
; GNU_SINCOS: callq sinl
; GNU_SINCOS: callq cosl
; GNU_SINCOS_FASTMATH-LABEL: test3_errno:
; GNU_SINCOS_FASTMATH: callq sinl
; GNU_SINCOS_FASTMATH: callq cosl
%call = tail call x86_fp80 @sinl(x86_fp80 %x)
%call1 = tail call x86_fp80 @cosl(x86_fp80 %x)
%add = fadd x86_fp80 %call, %call1
ret x86_fp80 %add
}
declare float @sinf(float)
declare double @sin(double)
declare float @cosf(float)
declare double @cos(double)
declare x86_fp80 @sinl(x86_fp80)
declare x86_fp80 @cosl(x86_fp80)