public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/106420] New: Missed optimization for comparisons
@ 2022-07-23  9:25 zero at smallinteger dot com
  2022-07-23  9:38 ` [Bug target/106420] " zero at smallinteger dot com
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: zero at smallinteger dot com @ 2022-07-23  9:25 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106420
           Summary: Missed optimization for comparisons
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: zero at smallinteger dot com
  Target Milestone: ---

Created attachment 53339
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53339&action=edit
Sample code

When comparing different variables to the same constants, in some cases the
compiler could first combine the variables and then do a single compare.  In
the sample given, two variables are compared against 7.  In the slow path, GCC
produces the following with -O2.

        cmp     edi, 7
        setg    al
        cmp     esi, 7
        setg    dl
        or      eax, edx
        movzx   eax, al
        ret

In the fast path, GCC produces this instead.

        or      edi, esi
        xor     eax, eax
        cmp     edi, 7
        setg    al
        ret

Although the expression a > 7 || b > 7 is the same as (a | b) > 7, the latter
is better because it results in fewer instructions.  A quick experiment shows
the latter also runs quite faster.

Verified with Godbolt for GCC trunk.  Clang, ICC, and MSVC latest versions also
miss this opportunity as per Godbolt.

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

* [Bug target/106420] Missed optimization for comparisons
  2022-07-23  9:25 [Bug target/106420] New: Missed optimization for comparisons zero at smallinteger dot com
@ 2022-07-23  9:38 ` zero at smallinteger dot com
  2022-07-25  2:12 ` [Bug tree-optimization/106420] " crazylht at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: zero at smallinteger dot com @ 2022-07-23  9:38 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from zero at smallinteger dot com ---
(it should be possible to massage the output further to use test and setne,
which ICC prefers to e.g. cmp and seta)

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

* [Bug tree-optimization/106420] Missed optimization for comparisons
  2022-07-23  9:25 [Bug target/106420] New: Missed optimization for comparisons zero at smallinteger dot com
  2022-07-23  9:38 ` [Bug target/106420] " zero at smallinteger dot com
@ 2022-07-25  2:12 ` crazylht at gmail dot com
  2022-07-25  2:23 ` pinskia at gcc dot gnu.org
  2022-07-25  2:35 ` zero at smallinteger dot com
  3 siblings, 0 replies; 5+ messages in thread
From: crazylht at gmail dot com @ 2022-07-25  2:12 UTC (permalink / raw)
  To: gcc-bugs

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

Hongtao.liu <crazylht at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |crazylht at gmail dot com

--- Comment #2 from Hongtao.liu <crazylht at gmail dot com> ---

> Although the expression a > 7 || b > 7 is the same as (a | b) > 7, the
Shoudn't the optimization be available for unsigned type only?

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

* [Bug tree-optimization/106420] Missed optimization for comparisons
  2022-07-23  9:25 [Bug target/106420] New: Missed optimization for comparisons zero at smallinteger dot com
  2022-07-23  9:38 ` [Bug target/106420] " zero at smallinteger dot com
  2022-07-25  2:12 ` [Bug tree-optimization/106420] " crazylht at gmail dot com
@ 2022-07-25  2:23 ` pinskia at gcc dot gnu.org
  2022-07-25  2:35 ` zero at smallinteger dot com
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-07-25  2:23 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This can only be done for unsigned types as if either one was negative, then
the check becames invalid.
It is already done since GCC 11 (most likely the patch which fixed PR 95731).

So this is invalid.

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

* [Bug tree-optimization/106420] Missed optimization for comparisons
  2022-07-23  9:25 [Bug target/106420] New: Missed optimization for comparisons zero at smallinteger dot com
                   ` (2 preceding siblings ...)
  2022-07-25  2:23 ` pinskia at gcc dot gnu.org
@ 2022-07-25  2:35 ` zero at smallinteger dot com
  3 siblings, 0 replies; 5+ messages in thread
From: zero at smallinteger dot com @ 2022-07-25  2:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from zero at smallinteger dot com ---
Sorry about that :/.

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

end of thread, other threads:[~2022-07-25  2:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-23  9:25 [Bug target/106420] New: Missed optimization for comparisons zero at smallinteger dot com
2022-07-23  9:38 ` [Bug target/106420] " zero at smallinteger dot com
2022-07-25  2:12 ` [Bug tree-optimization/106420] " crazylht at gmail dot com
2022-07-25  2:23 ` pinskia at gcc dot gnu.org
2022-07-25  2:35 ` zero at smallinteger dot com

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).