public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/96392] New: Optimize x+0.0 if x is an integer
@ 2020-07-30 19:08 hugo_musso_gualandi at hotmail dot com
  2020-07-30 20:34 ` [Bug tree-optimization/96392] " glisse at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: hugo_musso_gualandi at hotmail dot com @ 2020-07-30 19:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96392

            Bug ID: 96392
           Summary: Optimize x+0.0 if x is an integer
           Product: gcc
           Version: 10.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hugo_musso_gualandi at hotmail dot com
  Target Milestone: ---

One way to convert an integer to a floating point number in C is to multiply it
by 1.0. In this case, gcc is clever enough to optimize away the multiplication.

Another way is to add 0.0. However, in this case, GCC does not optimize away
the addition. 

Example C code:

    double times1(int x)
    {
        return x * 1.0;
    }

    double plus0(int x)
    {
        return x + 0.0;
    }

Output of objdump -d after compiling with gcc -O2 -c:

    0000000000000000 <times1>:
       0:       66 0f ef c0             pxor   %xmm0,%xmm0
       4:       f2 0f 2a c7             cvtsi2sd %edi,%xmm0
       8:       c3                      retq   
       9:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)

    0000000000000010 <plus0>:
      10:       66 0f ef c0             pxor   %xmm0,%xmm0
      14:       f2 0f 2a c7             cvtsi2sd %edi,%xmm0
      18:       f2 0f 58 05 00 00 00    addsd  0x0(%rip),%xmm0
      1f:       00 
      20:       c3                      retq   

I believe that the reason that GCC does not optimize x+0.0 is that it is
worried that x could be negative zero. However, promoting an integer to
floating point can never yield negative zero so it should be possible to
optimize in this particular case. (For the matter, Clang does optimize it.)

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

* [Bug tree-optimization/96392] Optimize x+0.0 if x is an integer
  2020-07-30 19:08 [Bug tree-optimization/96392] New: Optimize x+0.0 if x is an integer hugo_musso_gualandi at hotmail dot com
@ 2020-07-30 20:34 ` glisse at gcc dot gnu.org
  2021-06-11 16:18 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: glisse at gcc dot gnu.org @ 2020-07-30 20:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96392

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2020-07-30
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Severity|normal                      |enhancement

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

* [Bug tree-optimization/96392] Optimize x+0.0 if x is an integer
  2020-07-30 19:08 [Bug tree-optimization/96392] New: Optimize x+0.0 if x is an integer hugo_musso_gualandi at hotmail dot com
  2020-07-30 20:34 ` [Bug tree-optimization/96392] " glisse at gcc dot gnu.org
@ 2021-06-11 16:18 ` cvs-commit at gcc dot gnu.org
  2021-06-11 16:43 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-11 16:18 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96392

--- Comment #1 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:5b02ed4b87685c0f7c5da9b46cde3ce56fcfd457

commit r12-1393-g5b02ed4b87685c0f7c5da9b46cde3ce56fcfd457
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Fri Jun 11 17:15:38 2021 +0100

    [PATCH] PR tree-optimization/96392 Optimize x+0.0 if x is an integer

    The patch implements a missed optimization enhancement.  Under usual
    IEEE rules, x+0.0 can't be simplified to x when x might potentially
    be an IEEE minus zero (-0.0).  The current logic in the middle-end
    checks whether the type of x should honor signed zeros, but with this
    patch we introduce tree_expr_maybe_real_minus_zero_p that allows us
    to confirm that the value can't possibly be -0.0, for example, the result
    of a conversion from an integer type, or the result of fabs (or has a
    type that doesn't honor signed zero).

    Whilst modifying match.pd, I also converted some additional folding
    transformations from "testing the type" to "testing the value".

    2020-06-10  Roger Sayle  <roger@nextmovesoftware.com>

    gcc/ChangeLog
            PR tree-optimization/96392
            * fold-const.c (fold_real_zero_addition_p): Take both arguments
            of the addition or subtraction, not just the zero.  Use this
            other argument in tests for signaling NaNs and signed zeros.
            (tree_expr_maybe_real_minus_zero_p): New predicate.
            * fold-const.h (fold_real_zero_addition_p): Update prototype.
            (tree_expr_maybe_real_minus_zero_p): New function prototype.
            * match.pd: Update calls to fold_real_zero_addition_p.
            Replace HONOR_NANS with tree_expr_maybe_nan_p.
            Replace HONOR_SIGNED_ZEROS with tree_expr_maybe_real_minus_zero_p.
            Replace HONOR_SNANS with tree_expr_maybe_signaling_nan_p.
            * tree-ssa-reassoc.c (eliminate_using_constants): Update
            call to fold_real_zero_addition_p.

    gcc/testsuite/ChangeLog
            PR tree-optimization/96392
            * gcc.dg/pr96392.c: New test.

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

* [Bug tree-optimization/96392] Optimize x+0.0 if x is an integer
  2020-07-30 19:08 [Bug tree-optimization/96392] New: Optimize x+0.0 if x is an integer hugo_musso_gualandi at hotmail dot com
  2020-07-30 20:34 ` [Bug tree-optimization/96392] " glisse at gcc dot gnu.org
  2021-06-11 16:18 ` cvs-commit at gcc dot gnu.org
@ 2021-06-11 16:43 ` cvs-commit at gcc dot gnu.org
  2021-07-10  8:37 ` roger at nextmovesoftware dot com
  2022-02-09 14:24 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-06-11 16:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96392

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:a761829ae06a754f72ff957cbedb21f4d98fab70

commit r12-1394-ga761829ae06a754f72ff957cbedb21f4d98fab70
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Fri Jun 11 17:42:14 2021 +0100

    [PATCH] PR tree-optimization/96392 Optimize x+0.0 if x is an integer

    Doh! Wrong patch version.  Sorry for the inconvenience.

    2020-06-11  Roger Sayle  <roger@nextmovesoftware.com>

    gcc/ChangeLog
            PR tree-optimization/96392
            * fold-const.h (tree_expr_maybe_real_minus_zero_p): Fix prototype.

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

* [Bug tree-optimization/96392] Optimize x+0.0 if x is an integer
  2020-07-30 19:08 [Bug tree-optimization/96392] New: Optimize x+0.0 if x is an integer hugo_musso_gualandi at hotmail dot com
                   ` (2 preceding siblings ...)
  2021-06-11 16:43 ` cvs-commit at gcc dot gnu.org
@ 2021-07-10  8:37 ` roger at nextmovesoftware dot com
  2022-02-09 14:24 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: roger at nextmovesoftware dot com @ 2021-07-10  8:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96392

Roger Sayle <roger at nextmovesoftware dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |roger at nextmovesoftware dot com
         Resolution|---                         |FIXED
   Target Milestone|---                         |12.0
             Status|NEW                         |RESOLVED

--- Comment #3 from Roger Sayle <roger at nextmovesoftware dot com> ---
The optimization is now implemented in mainline GCC.

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

* [Bug tree-optimization/96392] Optimize x+0.0 if x is an integer
  2020-07-30 19:08 [Bug tree-optimization/96392] New: Optimize x+0.0 if x is an integer hugo_musso_gualandi at hotmail dot com
                   ` (3 preceding siblings ...)
  2021-07-10  8:37 ` roger at nextmovesoftware dot com
@ 2022-02-09 14:24 ` cvs-commit at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-02-09 14:24 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96392

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:2d3c477599b02b06e338acd5f5098ee7a3fe6176

commit r12-7130-g2d3c477599b02b06e338acd5f5098ee7a3fe6176
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Wed Feb 9 14:21:08 2022 +0000

    [PATCH] PR tree-optimization/104420: Fix checks for constant folding X*0.0

    This patch resolves PR tree-optimization/104420, which is a P1 regression
    where, as observed by Jakub Jelinek, the conditions for constant folding
    x*0.0 are incorrect (following my patch for PR tree-optimization/96392).
    The multiplication x*0.0 may yield a negative zero result, -0.0, if X is
    negative (not just if x may be negative zero).  Hence (without -ffast-math)
    (int)x*0.0 can't be optimized to 0.0, but (unsigned)x*0.0 can be constant
    folded.  This adds a bunch of test cases to confirm the desired behaviour,
    and removes an incorrect test from gcc.dg/pr96392.c which checked for the
    wrong behaviour.

    2022-02-09  Roger Sayle  <roger@nextmovesoftware.com>

    gcc/ChangeLog
            PR tree-optimization/104420
            * match.pd (mult @0 real_zerop): Tweak conditions for constant
            folding X*0.0 (or X*-0.0) to HONOR_SIGNED_ZEROS when appropriate.

    gcc/testsuite/ChangeLog
            PR tree-optimization/104420
            * gcc.dg/pr104420-1.c: New test case.
            * gcc.dg/pr104420-2.c: New test case.
            * gcc.dg/pr104420-3.c: New test case.
            * gcc.dg/pr104420-4.c: New test case.
            * gcc.dg/pr96392.c: Remove incorrect test.

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

end of thread, other threads:[~2022-02-09 14:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-30 19:08 [Bug tree-optimization/96392] New: Optimize x+0.0 if x is an integer hugo_musso_gualandi at hotmail dot com
2020-07-30 20:34 ` [Bug tree-optimization/96392] " glisse at gcc dot gnu.org
2021-06-11 16:18 ` cvs-commit at gcc dot gnu.org
2021-06-11 16:43 ` cvs-commit at gcc dot gnu.org
2021-07-10  8:37 ` roger at nextmovesoftware dot com
2022-02-09 14:24 ` cvs-commit 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).