public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/52802] New: Equality rewrites pessimizes code
@ 2012-03-31  0:35 gcc at magfr dot user.lysator.liu.se
  2012-04-02  9:16 ` [Bug c/52802] " rguenth at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gcc at magfr dot user.lysator.liu.se @ 2012-03-31  0:35 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 52802
           Summary: Equality rewrites pessimizes code
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: gcc@magfr.user.lysator.liu.se


I have a function (in foo.c)

unsigned f(unsigned a, unsigned b)
{
        if (a < 8)
                return a;
        else if (a == 8)
                return b;
        else
                return 4711;
}

that GCC compiles to (x86_64, -Os)

        cmpl    $7, %edi        #, a
        movl    %edi, %eax      # a, a
        jbe     .L2     #,
        cmpl    $8, %edi        #, a
        movl    $4711, %eax     #, tmp63
        cmove   %esi, %eax      # b,, a
.L2:
        ret

Here it is confusing to see that it starts by doing a "cmp $7, %edi" and then
goes on to do a "cmp $8, %edi" since I never said anything but 8, so the big
question is why there are two compares.

The reason seems to be premature optimization. The C parser tries to minimize
the value of all comparision values so that -fdump-tree-original shows

;; Function f (null)
;; enabled by -tree-original


{
  if (a <= 7)
    {
      return a;
    }
  else
    {
      if (a == 8)
        {
          return b;
        }
      else
        {
          return 4711;
        }
    }
}

and there the 7 have magically appeared.


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

* [Bug c/52802] Equality rewrites pessimizes code
  2012-03-31  0:35 [Bug c/52802] New: Equality rewrites pessimizes code gcc at magfr dot user.lysator.liu.se
@ 2012-04-02  9:16 ` rguenth at gcc dot gnu.org
  2012-04-02  9:43 ` mikpe at it dot uu.se
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-04-02  9:16 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
   Last reconfirmed|                            |2012-04-02
     Ever Confirmed|0                           |1

--- Comment #1 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-04-02 09:16:20 UTC ---
How would you do with a single compare?!


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

* [Bug c/52802] Equality rewrites pessimizes code
  2012-03-31  0:35 [Bug c/52802] New: Equality rewrites pessimizes code gcc at magfr dot user.lysator.liu.se
  2012-04-02  9:16 ` [Bug c/52802] " rguenth at gcc dot gnu.org
@ 2012-04-02  9:43 ` mikpe at it dot uu.se
  2012-04-02 10:29 ` [Bug target/52802] " rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: mikpe at it dot uu.se @ 2012-04-02  9:43 UTC (permalink / raw)
  To: gcc-bugs

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

Mikael Pettersson <mikpe at it dot uu.se> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mikpe at it dot uu.se

--- Comment #2 from Mikael Pettersson <mikpe at it dot uu.se> 2012-04-02 09:43:03 UTC ---
On x86, mov and jcc don't clobber the flags, so the first cmpl should compare
with 8 not 7, the jbe should be adjusted to jb, and then the second cmpl is
redundant as the flags from the first cmpl are still available.


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

* [Bug target/52802] Equality rewrites pessimizes code
  2012-03-31  0:35 [Bug c/52802] New: Equality rewrites pessimizes code gcc at magfr dot user.lysator.liu.se
  2012-04-02  9:16 ` [Bug c/52802] " rguenth at gcc dot gnu.org
  2012-04-02  9:43 ` mikpe at it dot uu.se
@ 2012-04-02 10:29 ` rguenth at gcc dot gnu.org
  2021-08-02 18:28 ` [Bug rtl-optimization/52802] " pinskia at gcc dot gnu.org
  2023-08-05 21:42 ` [Bug middle-end/52802] " pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2012-04-02 10:29 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Guenther <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Target|                            |x86_64-*-*, i?86-*-*
             Status|WAITING                     |NEW
          Component|c                           |target

--- Comment #3 from Richard Guenther <rguenth at gcc dot gnu.org> 2012-04-02 10:28:58 UTC ---
Confirmed.  On the tree level we canonicalize things which is good, if
you change the source to what the canonicalization looks like you get the
same problem.

Target issue.


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

* [Bug rtl-optimization/52802] Equality rewrites pessimizes code
  2012-03-31  0:35 [Bug c/52802] New: Equality rewrites pessimizes code gcc at magfr dot user.lysator.liu.se
                   ` (2 preceding siblings ...)
  2012-04-02 10:29 ` [Bug target/52802] " rguenth at gcc dot gnu.org
@ 2021-08-02 18:28 ` pinskia at gcc dot gnu.org
  2023-08-05 21:42 ` [Bug middle-end/52802] " pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-02 18:28 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
          Component|target                      |rtl-optimization
   Last reconfirmed|2012-04-02 00:00:00         |2021-8-2

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
We miss this also for aarch64:
        cmp     w0, 7
        bls     .L1
        cmp     w0, 8
        mov     w0, 4711
        csel    w0, w1, w0, eq
.L1:
        ret

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

* [Bug middle-end/52802] Equality rewrites pessimizes code
  2012-03-31  0:35 [Bug c/52802] New: Equality rewrites pessimizes code gcc at magfr dot user.lysator.liu.se
                   ` (3 preceding siblings ...)
  2021-08-02 18:28 ` [Bug rtl-optimization/52802] " pinskia at gcc dot gnu.org
@ 2023-08-05 21:42 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-05 21:42 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pinskia at gcc dot gnu.org
          Component|rtl-optimization            |middle-end

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I wonder if we could change `a <= 7` back to `a < 8` in isel if we notice we do
another comparison in either branch against `a` and `8`.

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

end of thread, other threads:[~2023-08-05 21:43 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-31  0:35 [Bug c/52802] New: Equality rewrites pessimizes code gcc at magfr dot user.lysator.liu.se
2012-04-02  9:16 ` [Bug c/52802] " rguenth at gcc dot gnu.org
2012-04-02  9:43 ` mikpe at it dot uu.se
2012-04-02 10:29 ` [Bug target/52802] " rguenth at gcc dot gnu.org
2021-08-02 18:28 ` [Bug rtl-optimization/52802] " pinskia at gcc dot gnu.org
2023-08-05 21:42 ` [Bug middle-end/52802] " pinskia 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).