public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/113680] New: Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0
@ 2024-01-31 12:30 Explorer09 at gmail dot com
  2024-01-31 12:47 ` [Bug middle-end/113680] " rguenth at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Explorer09 at gmail dot com @ 2024-01-31 12:30 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113680
           Summary: Missed optimization: Redundant cmp/test instructions
                    when comparing (x - y) > 0
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Explorer09 at gmail dot com
  Target Milestone: ---

Note: This issue is not limited to x86-64. I also tested it with ARM64 gcc in
Compiler Explorer (https://godbolt.org/) and it has the same "redundant cmp
instruction" problem.

This may be related to bug #3507 but I can't make sure it's the same bug. I
apologize if I reported a duplicate.

== Test code ==

```c
#include <stdio.h>

void func1(int x, int y) {
    int diff = x - y;
    if (diff > 0)
        putchar('>');
    if (diff < 0)
        putchar('<');
}

void func2(int x, int y) {
    if ((x - y) > 0)
        putchar('>');
    if ((x - y) < 0)
        putchar('<');
}

void func3(int x, int y) {
    if (x > y)
        putchar('>');
    if (x < y) {
        putchar('<');
}

void func4(int x, int y) {
    int diff = x - y;
    if (diff > 0)
        putchar('>');
    if (x < y) {
        putchar('<');
}
```

== Actual result ==

With x86-64 "gcc -Os" it generates the following.

In short, gcc can recognize func1() and func2() as completely identical, but
didn't recognize func1() and func2() can both optimize to func3().

func4() currently generates the worst assembly, but it might be another issue
to address (something messes up the register allocation algorithm).

```x86asm
func1:
        subl    %esi, %edi
        testl   %edi, %edi
        jle     .L2
        movl    $62, %edi
        jmp     .L4
.L2:
        je      .L1
        movl    $60, %edi
.L4:
        jmp     putchar
.L1:
        ret
func2:
        jmp     func1
func3:
        cmpl    %esi, %edi
        jle     .L8
        movl    $62, %edi
        jmp     .L10
.L8:
        jge     .L7
        movl    $60, %edi
.L10:
        jmp     putchar
.L7:
        ret
func4:
        pushq   %rbp
        movl    %edi, %ebp
        pushq   %rbx
        movl    %esi, %ebx
        pushq   %rcx
        cmpl    %esi, %edi
        jle     .L12
        movl    $62, %edi
        call    putchar
.L12:
        cmpl    %ebx, %ebp
        jge     .L11
        popq    %rdx
        movl    $60, %edi
        popq    %rbx
        popq    %rbp
        jmp     putchar
.L11:
        popq    %rax
        popq    %rbx
        popq    %rbp
        ret
```

== Expected result ==

func1(), func2(), func3() and func4() are all identical. With the func3() as
the example for the best assembly. (No redundant "test" instruction; the "sub"
instruction can simplify into a "cmp".)

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

* [Bug middle-end/113680] Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0
  2024-01-31 12:30 [Bug rtl-optimization/113680] New: Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0 Explorer09 at gmail dot com
@ 2024-01-31 12:47 ` rguenth at gcc dot gnu.org
  2024-01-31 12:53 ` Explorer09 at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-01-31 12:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|rtl-optimization            |middle-end
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-01-31
           Keywords|                            |easyhack,
                   |                            |missed-optimization
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
I don't think we have or had a (a - b) CMP 0 simplification pattern which
this seems to be about.  We have a +- CST CMP CST'.

Note the reverse, a < b ->  (a - b) < 0 isn't valid.

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

* [Bug middle-end/113680] Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0
  2024-01-31 12:30 [Bug rtl-optimization/113680] New: Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0 Explorer09 at gmail dot com
  2024-01-31 12:47 ` [Bug middle-end/113680] " rguenth at gcc dot gnu.org
@ 2024-01-31 12:53 ` Explorer09 at gmail dot com
  2024-01-31 15:23 ` Explorer09 at gmail dot com
  2024-03-03 16:56 ` kmatsui at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: Explorer09 at gmail dot com @ 2024-01-31 12:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Kang-Che Sung <Explorer09 at gmail dot com> ---
I forgot to mention that such optimization is unsafe for floating points
(actually I knew that when I write my code). `(a - b) < 0` optimization
shouldn't be performed with unsigned integers either. I request only
optimizations on signed integers.

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

* [Bug middle-end/113680] Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0
  2024-01-31 12:30 [Bug rtl-optimization/113680] New: Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0 Explorer09 at gmail dot com
  2024-01-31 12:47 ` [Bug middle-end/113680] " rguenth at gcc dot gnu.org
  2024-01-31 12:53 ` Explorer09 at gmail dot com
@ 2024-01-31 15:23 ` Explorer09 at gmail dot com
  2024-03-03 16:56 ` kmatsui at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: Explorer09 at gmail dot com @ 2024-01-31 15:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Kang-Che Sung <Explorer09 at gmail dot com> ---
Oops. I made a typo in the test code. func4() shouldn't have that redundant
brace.

The corrected example:

```
void func4(int x, int y) {
    int diff = x - y;
    if (diff > 0)
        putchar('>');
    if (x < y)
        putchar('<');
}
```

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

* [Bug middle-end/113680] Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0
  2024-01-31 12:30 [Bug rtl-optimization/113680] New: Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0 Explorer09 at gmail dot com
                   ` (2 preceding siblings ...)
  2024-01-31 15:23 ` Explorer09 at gmail dot com
@ 2024-03-03 16:56 ` kmatsui at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: kmatsui at gcc dot gnu.org @ 2024-03-03 16:56 UTC (permalink / raw)
  To: gcc-bugs

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

Ken Matsui <kmatsui at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |kmatsui at gcc dot gnu.org
                 CC|                            |kmatsui at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

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

end of thread, other threads:[~2024-03-03 16:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-31 12:30 [Bug rtl-optimization/113680] New: Missed optimization: Redundant cmp/test instructions when comparing (x - y) > 0 Explorer09 at gmail dot com
2024-01-31 12:47 ` [Bug middle-end/113680] " rguenth at gcc dot gnu.org
2024-01-31 12:53 ` Explorer09 at gmail dot com
2024-01-31 15:23 ` Explorer09 at gmail dot com
2024-03-03 16:56 ` kmatsui 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).