I was wondering whether GCC can optimize sin(x) and cos(x) of the same angle in order to use the combined fsincos x86 instruction. The testcase is:
----------------8<-------------------- #include <math.h> double fn1(double x) { return sin(x) * cos(x); } double fn2(double x) { return __builtin_sin(x) * __builtin_cos(x); } ----------------8<-------------------- I compile it with: gcc -O3 -fomit-frame-pointer -S testcase.cpp The result turned out to be rather surprising: ----------------8<-------------------- .file "testcase.cpp" .text .p2align 4,,15 .globl __Z3fn1d .def __Z3fn1d; .scl 2; .type 32; .endef __Z3fn1d: subl $60, %esp fldl 64(%esp) fstl (%esp) fstpl 16(%esp) call _sin fstpl 40(%esp) fldl 16(%esp) fstpl (%esp) call _cos fmull 40(%esp) addl $60, %esp ret .p2align 4,,15 .globl __Z3fn2d .def __Z3fn2d; .scl 2; .type 32; .endef __Z3fn2d: subl $60, %esp fldl 64(%esp) fstl (%esp) fstpl 16(%esp) call _sin fstpl 40(%esp) fldl 16(%esp) fstpl (%esp) call _cos fmull 40(%esp) addl $60, %esp ret .def _sin; .scl 2; .type 32; .endef .def _cos; .scl 2; .type 32; .endef .def _sin; .scl 2; .type 32; .endef .def _cos; .scl 2; .type 32; .endef ----------------8<-------------------- As you can see: a) fsin and fcos are not combined into fsincos, because b) there are no fsin and fcos -- library calls have been emitted; c) the stack top manipulation (subl / addl $60, %esp) has no purpose. -- Summary: Several missing optimizations Product: gcc Version: 4.5.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: piotr dot wyderski at gmail dot com GCC build triplet: i686-pc-cygwin GCC host triplet: i686-pc-cygwin GCC target triplet: i686-pc-cygwin http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43599