public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code
@ 2020-12-11 10:18 denis.campredon at gmail dot com
  2020-12-11 10:19 ` [Bug tree-optimization/98236] " denis.campredon at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: denis.campredon at gmail dot com @ 2020-12-11 10:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98236
           Summary: x plus/minus y cmp 0 produces unoptimal code
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: denis.campredon at gmail dot com
  Target Milestone: ---

Created attachment 49733
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49733&action=edit
input file

Compiling test.c on x86 with -O2 leads to unoptimal code generation.

f1 to f4 could be optimized to `add + set(g|ge|l|le)`
f5 to f8 could be optimized to `sud|cmp + set(g|ge|l|le)`


For f2, f4, f6 and f8 no pattern is recognized.


For f1, f3, f5 and f7, the optimizers are not aware that, at least on x86,
`add` and `sub` can set flags.

This can also be seen with the following function. gcc will produce `cmp + sub`
although only `sub` could be used

-----------
void foo();

int bar(int x, int y) {
    if (x - y)
        foo();
    return x - y;
}
-----------
produces
-----------
bar(int, int):
        push    rbp
        mov     ebp, esi
        push    rbx
        mov     ebx, edi
        sub     rsp, 8
        cmp     edi, esi
        je      .L2
        call    foo()
.L2:
        mov     eax, ebx
        add     rsp, 8
        sub     eax, ebp
        pop     rbx
        pop     rbp
        ret
-----------

expected

-----------
bar(int, int):
        push    rbx
        mov     ebx, edi
        sub     ebx, esi
        je      .L2
        call    foo()
.L2:
        mov     eax, ebx
        pop     rbx
        ret
-----------

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

* [Bug tree-optimization/98236] x plus/minus y cmp 0 produces unoptimal code
  2020-12-11 10:18 [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code denis.campredon at gmail dot com
@ 2020-12-11 10:19 ` denis.campredon at gmail dot com
  2020-12-11 10:39 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: denis.campredon at gmail dot com @ 2020-12-11 10:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from denis.campredon at gmail dot com ---
Created attachment 49734
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49734&action=edit
assemble generated

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

* [Bug tree-optimization/98236] x plus/minus y cmp 0 produces unoptimal code
  2020-12-11 10:18 [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code denis.campredon at gmail dot com
  2020-12-11 10:19 ` [Bug tree-optimization/98236] " denis.campredon at gmail dot com
@ 2020-12-11 10:39 ` rguenth at gcc dot gnu.org
  2020-12-11 10:40 ` [Bug middle-end/98236] " rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-12-11 10:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
This is likely because we expand from

int bar (int x, int y)
{
  int _6;

  <bb 2> [local count: 1073741824]:
  if (x_2(D) != y_3(D))
    goto <bb 3>; [48.88%]
  else
    goto <bb 4>; [51.12%]

  <bb 3> [local count: 524845000]:
  foo ();

  <bb 4> [local count: 1073741824]:
  _6 = x_2(D) - y_3(D);
  return _6;

which means we simplified the condition to x != y.  So related to PR91384

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

* [Bug middle-end/98236] x plus/minus y cmp 0 produces unoptimal code
  2020-12-11 10:18 [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code denis.campredon at gmail dot com
  2020-12-11 10:19 ` [Bug tree-optimization/98236] " denis.campredon at gmail dot com
  2020-12-11 10:39 ` rguenth at gcc dot gnu.org
@ 2020-12-11 10:40 ` rguenth at gcc dot gnu.org
  2021-08-01 18:14 ` pinskia at gcc dot gnu.org
  2023-06-13  5:56 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-12-11 10:40 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-12-11
     Ever confirmed|0                           |1

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

* [Bug middle-end/98236] x plus/minus y cmp 0 produces unoptimal code
  2020-12-11 10:18 [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code denis.campredon at gmail dot com
                   ` (2 preceding siblings ...)
  2020-12-11 10:40 ` [Bug middle-end/98236] " rguenth at gcc dot gnu.org
@ 2021-08-01 18:14 ` pinskia at gcc dot gnu.org
  2023-06-13  5:56 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-01 18:14 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
   Last reconfirmed|2020-12-11 00:00:00         |2021-8-1

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

* [Bug middle-end/98236] x plus/minus y cmp 0 produces unoptimal code
  2020-12-11 10:18 [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code denis.campredon at gmail dot com
                   ` (3 preceding siblings ...)
  2021-08-01 18:14 ` pinskia at gcc dot gnu.org
@ 2023-06-13  5:56 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-13  5:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note f4 right now produces the best code even though it is not add/setN.

        leal    (%rdi,%rsi), %eax
        shrl    $31, %eax
is better overall because it does not touch the flags register and all
especially if there is other code around it.

For f2, the biggest thing here is a cost model around doing the expansion as
not/shift or cmp/set.

f5, f6 and f7 are now caught in GCC 11+.

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

end of thread, other threads:[~2023-06-13  5:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-11 10:18 [Bug tree-optimization/98236] New: x plus/minus y cmp 0 produces unoptimal code denis.campredon at gmail dot com
2020-12-11 10:19 ` [Bug tree-optimization/98236] " denis.campredon at gmail dot com
2020-12-11 10:39 ` rguenth at gcc dot gnu.org
2020-12-11 10:40 ` [Bug middle-end/98236] " rguenth at gcc dot gnu.org
2021-08-01 18:14 ` pinskia at gcc dot gnu.org
2023-06-13  5:56 ` 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).