public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double.
@ 2013-01-28  2:58 mednafen at gmail dot com
  2013-01-28  7:23 ` [Bug tree-optimization/56125] " glisse at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: mednafen at gmail dot com @ 2013-01-28  2:58 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

             Bug #: 56125
           Summary: -O2 -ffast-math generates bad code when dividing a
                    double by the square of another double.
    Classification: Unclassified
           Product: gcc
           Version: 4.7.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: mednafen@gmail.com


Created attachment 29288
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29288
Test program.

XXX@willow:~$ gcc -O0 -o weird weird.c 
XXX@willow:~$ ./weird 
7.250000

XXX@willow:~$ gcc -O2 -o weird weird.c 
XXX@willow:~$ ./weird 
7.250000

XXX@willow:~$ gcc -ffast-math -O2 -o weird weird.c 
XXX@willow:~$ ./weird 
5.126524



Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.7.2-5'
--with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs
--enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.7 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object
--enable-plugin --enable-objc-gc --with-arch-32=i586 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Debian 4.7.2-5)


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/56125] -O2 -ffast-math generates bad code when dividing a double by the square of another double.
  2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
@ 2013-01-28  7:23 ` glisse at gcc dot gnu.org
  2013-01-28  7:30 ` [Bug tree-optimization/56125] [4.7/4.8 Regression] " glisse at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: glisse at gcc dot gnu.org @ 2013-01-28  7:23 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

Marc Glisse <glisse at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2013-01-28
          Component|c                           |tree-optimization
     Ever Confirmed|0                           |1

--- Comment #1 from Marc Glisse <glisse at gcc dot gnu.org> 2013-01-28 07:23:24 UTC ---
It is turned to x*pow(n,-2.) and the expansion of pow notices that 2*2 is an
integer and uses the code meant for 2.5...


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/56125] [4.7/4.8 Regression] -O2 -ffast-math generates bad code when dividing a double by the square of another double.
  2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
  2013-01-28  7:23 ` [Bug tree-optimization/56125] " glisse at gcc dot gnu.org
@ 2013-01-28  7:30 ` glisse at gcc dot gnu.org
  2013-01-28  9:15 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: glisse at gcc dot gnu.org @ 2013-01-28  7:30 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

Marc Glisse <glisse at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|-O2 -ffast-math generates   |[4.7/4.8 Regression] -O2
                   |bad code when dividing a    |-ffast-math generates bad
                   |double by the square of     |code when dividing a double
                   |another double.             |by the square of another
                   |                            |double.

--- Comment #2 from Marc Glisse <glisse at gcc dot gnu.org> 2013-01-28 07:30:05 UTC ---
4.6 generated a very suboptimal call to the libm pow function, but it didn't
have the completely wrong result.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/56125] [4.7/4.8 Regression] -O2 -ffast-math generates bad code when dividing a double by the square of another double.
  2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
  2013-01-28  7:23 ` [Bug tree-optimization/56125] " glisse at gcc dot gnu.org
  2013-01-28  7:30 ` [Bug tree-optimization/56125] [4.7/4.8 Regression] " glisse at gcc dot gnu.org
@ 2013-01-28  9:15 ` jakub at gcc dot gnu.org
  2013-01-28 10:27 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-01-28  9:15 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org
   Target Milestone|---                         |4.7.3

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-01-28 09:15:09 UTC ---
Regressed with http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174446
Looking into it.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/56125] [4.7/4.8 Regression] -O2 -ffast-math generates bad code when dividing a double by the square of another double.
  2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
                   ` (2 preceding siblings ...)
  2013-01-28  9:15 ` jakub at gcc dot gnu.org
@ 2013-01-28 10:27 ` jakub at gcc dot gnu.org
  2013-01-28 14:43 ` jakub at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-01-28 10:27 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|unassigned at gcc dot       |jakub at gcc dot gnu.org
                   |gnu.org                     |

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-01-28 10:27:19 UTC ---
Created attachment 29292
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=29292
gcc48-pr56125.patch

The bug is that the last two optimizations of pow (where 2c resp. 3c is a
non-zero integer) silently assume that the earlier optimizations already
handled the cases where already c (or 2c for the last optimization) is an
integer.
But that doesn't have to be the case, as shown by the testcase, the integer
optimization is guarded by c in [-1,2] or optimization for speed.

The && optimize_function_for_speed_p () (or should that be
optimize_insn_for_speed_p?, the pow folding is inconsistent in that, and I
don't see e.g. rtl_profile_for_bb being called during this pass to make it
accurate)
is up for discussions, say on x86_64
__attribute__((cold)) double
foo (double x, double n)
{
  double u = __builtin_pow (x, -1.5);
  return u;
}
with it we get smaller code:
        movsd   .LC0(%rip), %xmm1
        jmp     pow
compared to:
        sqrtsd  %xmm0, %xmm1
        mulsd   %xmm0, %xmm1
        movsd   .LC0(%rip), %xmm0
        divsd   %xmm1, %xmm0
without it, 7 bytes shorter.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/56125] [4.7/4.8 Regression] -O2 -ffast-math generates bad code when dividing a double by the square of another double.
  2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
                   ` (3 preceding siblings ...)
  2013-01-28 10:27 ` jakub at gcc dot gnu.org
@ 2013-01-28 14:43 ` jakub at gcc dot gnu.org
  2013-01-28 15:07 ` [Bug tree-optimization/56125] [4.7 " jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-01-28 14:43 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-01-28 14:43:07 UTC ---
Author: jakub
Date: Mon Jan 28 14:43:03 2013
New Revision: 195507

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195507
Log:
    PR tree-optimization/56125
    * tree-ssa-math-opts.c (gimple_expand_builtin_pow): Don't optimize
    pow(x,c) into sqrt(x) * powi(x, n/2) or
    1.0 / (sqrt(x) * powi(x, abs(n/2))) if c is an integer or when
    optimizing for size.
    Don't optimize pow(x,c) into powi(x, n/3) * powi(cbrt(x), n%3) or
    1.0 / (powi(x, abs(n)/3) * powi(cbrt(x), abs(n)%3)) if 2c is an
    integer.

    * gcc.dg/pr56125.c: New test.

Added:
    trunk/gcc/testsuite/gcc.dg/pr56125.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-ssa-math-opts.c


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/56125] [4.7 Regression] -O2 -ffast-math generates bad code when dividing a double by the square of another double.
  2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
                   ` (4 preceding siblings ...)
  2013-01-28 14:43 ` jakub at gcc dot gnu.org
@ 2013-01-28 15:07 ` jakub at gcc dot gnu.org
  2013-02-01 14:18 ` jakub at gcc dot gnu.org
  2013-02-01 14:39 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-01-28 15:07 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[4.7/4.8 Regression] -O2    |[4.7 Regression] -O2
                   |-ffast-math generates bad   |-ffast-math generates bad
                   |code when dividing a double |code when dividing a double
                   |by the square of another    |by the square of another
                   |double.                     |double.

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-01-28 15:07:24 UTC ---
Fixed on the trunk so far.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/56125] [4.7 Regression] -O2 -ffast-math generates bad code when dividing a double by the square of another double.
  2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
                   ` (5 preceding siblings ...)
  2013-01-28 15:07 ` [Bug tree-optimization/56125] [4.7 " jakub at gcc dot gnu.org
@ 2013-02-01 14:18 ` jakub at gcc dot gnu.org
  2013-02-01 14:39 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-01 14:18 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-01 14:17:58 UTC ---
Author: jakub
Date: Fri Feb  1 14:17:50 2013
New Revision: 195664

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=195664
Log:
    Backported from mainline
    2013-01-28  Jakub Jelinek  <jakub@redhat.com>

    PR tree-optimization/56125
    * tree-ssa-math-opts.c (gimple_expand_builtin_pow): Don't optimize
    pow(x,c) into sqrt(x) * powi(x, n/2) or
    1.0 / (sqrt(x) * powi(x, abs(n/2))) if c is an integer or when
    optimizing for size.
    Don't optimize pow(x,c) into powi(x, n/3) * powi(cbrt(x), n%3) or
    1.0 / (powi(x, abs(n)/3) * powi(cbrt(x), abs(n)%3)) if 2c is an
    integer.

    * gcc.dg/pr56125.c: New test.

Added:
    branches/gcc-4_7-branch/gcc/testsuite/gcc.dg/pr56125.c
Modified:
    branches/gcc-4_7-branch/gcc/ChangeLog
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_7-branch/gcc/tree-ssa-math-opts.c


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug tree-optimization/56125] [4.7 Regression] -O2 -ffast-math generates bad code when dividing a double by the square of another double.
  2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
                   ` (6 preceding siblings ...)
  2013-02-01 14:18 ` jakub at gcc dot gnu.org
@ 2013-02-01 14:39 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-01 14:39 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56125

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-01 14:38:51 UTC ---
Fixed.


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2013-02-01 14:39 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-01-28  2:58 [Bug c/56125] New: -O2 -ffast-math generates bad code when dividing a double by the square of another double mednafen at gmail dot com
2013-01-28  7:23 ` [Bug tree-optimization/56125] " glisse at gcc dot gnu.org
2013-01-28  7:30 ` [Bug tree-optimization/56125] [4.7/4.8 Regression] " glisse at gcc dot gnu.org
2013-01-28  9:15 ` jakub at gcc dot gnu.org
2013-01-28 10:27 ` jakub at gcc dot gnu.org
2013-01-28 14:43 ` jakub at gcc dot gnu.org
2013-01-28 15:07 ` [Bug tree-optimization/56125] [4.7 " jakub at gcc dot gnu.org
2013-02-01 14:18 ` jakub at gcc dot gnu.org
2013-02-01 14:39 ` jakub at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).