[cfe-dev] clang does not use sincos with -O2 but gcc does

Dennis Luehring via cfe-dev cfe-dev at lists.llvm.org
Thu May 18 07:56:11 PDT 2017


>To make matters even more complex: on Mac the situation is the opposite

im on ubuntu 16.04 x64 only
gcc-5.4, gcc-6.2, clang 3.8.0 - they all should use the same stdlib
and i want to understand why gcc is using sincos (without -ffast-math) in
the 3 tested releases (5,6,7), and clang only when using -ffast-math
- this is not about platform differences

is it a missing clang optimization opportunity or is the gcc wrong in using sincos by default?

test.c
----
#include <math.h>
int main(int argc, char* argv[])
{
   double x = argv[0][0];
   return (int)sin(x)+cos(x);
}
----

gcc -S test.c -O2 -lm
test.s
----
	.file	"test.c"
	.section	.text.unlikely,"ax", at progbits
.LCOLDB0:
	.section	.text.startup,"ax", at progbits
.LHOTB0:
	.p2align 4,,15
	.globl	main
	.type	main, @function
main:
.LFB3:
	.cfi_startproc
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	(%rsi), %rax
	pxor	%xmm0, %xmm0
	leaq	8(%rsp), %rdi
	movq	%rsp, %rsi
	movsbl	(%rax), %eax
	cvtsi2sd	%eax, %xmm0
	call	sincos
	cvttsd2si	8(%rsp), %eax
	pxor	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	addsd	(%rsp), %xmm0
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	cvttsd2si	%xmm0, %eax
	ret
	.cfi_endproc
.LFE3:
	.size	main, .-main
	.section	.text.unlikely
.LCOLDE0:
	.section	.text.startup
.LHOTE0:
	.ident	"GCC: (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904"
	.section	.note.GNU-stack,"", at progbits
----

gcc-6 -S test.c -O2 -lm
test.s
----
	.file	"test.c"
	.section	.text.startup,"ax", at progbits
	.p2align 4,,15
	.globl	main
	.type	main, @function
main:
.LFB3:
	.cfi_startproc
	subq	$24, %rsp
	.cfi_def_cfa_offset 32
	movq	(%rsi), %rax
	pxor	%xmm0, %xmm0
	leaq	8(%rsp), %rdi
	movq	%rsp, %rsi
	movsbl	(%rax), %eax
	cvtsi2sd	%eax, %xmm0
	call	sincos
	cvttsd2si	8(%rsp), %eax
	pxor	%xmm0, %xmm0
	cvtsi2sd	%eax, %xmm0
	addsd	(%rsp), %xmm0
	addq	$24, %rsp
	.cfi_def_cfa_offset 8
	cvttsd2si	%xmm0, %eax
	ret
	.cfi_endproc
.LFE3:
	.size	main, .-main
	.ident	"GCC: (Ubuntu 6.2.0-3ubuntu11~16.04) 6.2.0 20160901"
	.section	.note.GNU-stack,"", at progbits

clang -S test.c -O2
test.s
----
	.text
	.file	"test.c"
	.globl	main
	.align	16, 0x90
	.type	main, at function
main:                                   # @main
	.cfi_startproc
# BB#0:
	subq	$24, %rsp
.Ltmp0:
	.cfi_def_cfa_offset 32
	movq	(%rsi), %rax
	movsbl	(%rax), %eax
	cvtsi2sdl	%eax, %xmm0
	movsd	%xmm0, 8(%rsp)          # 8-byte Spill
	callq	sin
	cvttsd2si	%xmm0, %eax
	xorps	%xmm0, %xmm0
	cvtsi2sdl	%eax, %xmm0
	movsd	%xmm0, 16(%rsp)         # 8-byte Spill
	movsd	8(%rsp), %xmm0          # 8-byte Reload
                                         # xmm0 = mem[0],zero
	callq	cos
	addsd	16(%rsp), %xmm0         # 8-byte Folded Reload
	cvttsd2si	%xmm0, %eax
	addq	$24, %rsp
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc


	.ident	"clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)"
	.section	".note.GNU-stack","", at progbits

clang -S test.c -O2 -ffast-math
test.s
----
	.text
	.file	"test.c"
	.globl	main
	.align	16, 0x90
	.type	main, at function
main:                                   # @main
	.cfi_startproc
# BB#0:
	subq	$24, %rsp
.Ltmp0:
	.cfi_def_cfa_offset 32
	movq	(%rsi), %rax
	movsbl	(%rax), %eax
	cvtsi2sdl	%eax, %xmm0
	leaq	16(%rsp), %rdi
	leaq	8(%rsp), %rsi
	callq	sincos
	cvttsd2si	16(%rsp), %eax
	xorps	%xmm0, %xmm0
	cvtsi2sdl	%eax, %xmm0
	addsd	8(%rsp), %xmm0
	cvttsd2si	%xmm0, %eax
	addq	$24, %rsp
	retq
.Lfunc_end0:
	.size	main, .Lfunc_end0-main
	.cfi_endproc


	.ident	"clang version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)"
	.section	".note.GNU-stack","", at progbits


Am 18.05.2017 um 16:20 schrieb René J.V. Bertin:
> On Thursday May 18 2017 14:55:03 Dennis Luehring wrote:
>
> >clang DOES use sincos if -ffast-math option is given
> >gcc use it even without -ffast-math
>
> To make matters even more complex: on Mac the situation is the opposite, that is clang always uses a sincos library function provided by Apple.
>
> >i don't think its the x87 opcode - because then LLVM would inline it - and not using a call
>
> That depends, LLVM cannot know how the sincos() function from libm is implemented.
>
> R.





More information about the cfe-dev mailing list