public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
@ 2023-09-18  9:45 theodort at inf dot ethz.ch
  2023-09-18 10:41 ` [Bug tree-optimization/111456] " rguenth at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-09-18  9:45 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111456
           Summary: [14 Regression] Dead Code Elimination Regression since
                    r14-3719-gb34f3736356
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: theodort at inf dot ethz.ch
  Target Milestone: ---

https://godbolt.org/z/jqqP5baY8

Given the following code:

void foo(void);
static int i;
static int *j = &i;
static char l;
static void(a)(char) {}
static short(b)(short c, short d) { return c - d; }
static short(e)(short f, int g) {
    return f < 0 || g < 0 || g >= 32 ? f : f >> g;
}
static short(h)(short f, int g) { return g >= 2 ?: f >> g; }
static char k(char m, short n) {
    short o;
    int *p = &i;
    if (!(((m) >= 1) && ((m) <= 1))) {
        __builtin_unreachable();
    }
    o = e(i, i);
    if (h(1, o))
        ;
    else {
        m = 0;
        for (; m >= -20; m = b(m, 9))
            if (a(i), n) {
                if (*p)
                    ;
                else
                    foo();
                ;
            } else
                return l;
    }
    return i;
}
int main() { k(0 <= 0 > *j, i); }

gcc-trunk -O2 does not eliminate the call to foo:

main:
        movl    i(%rip), %eax
        testw   %ax, %ax
        js      .L6
        testl   %eax, %eax
        jne     .L6
.L9:
        xorl    %eax, %eax
        ret
        .p2align 4,,10
        .p2align 3
.L6:
        movswl  %ax, %edx
        cmpw    $1, %ax
        jg      .L9
        testl   %edx, %edx
        je      .L9
        pushq   %rbx
        movl    $3, %ebx
.L5:
        cmpl    $0, i(%rip)
        je      .L13
.L4:
        subb    $1, %bl
        jne     .L5
        xorl    %eax, %eax
        popq    %rbx
        ret
.L13:
        call    foo
        jmp     .L4

gcc-13.2.0 -O2 eliminates the call to foo:

main:
        xorl    %eax, %eax
        ret

Bisects to r14-3719-gb34f3736356

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
@ 2023-09-18 10:41 ` rguenth at gcc dot gnu.org
  2023-09-18 14:48 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-09-18 10:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |14.0

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
  2023-09-18 10:41 ` [Bug tree-optimization/111456] " rguenth at gcc dot gnu.org
@ 2023-09-18 14:48 ` pinskia at gcc dot gnu.org
  2023-09-18 23:34 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-18 14:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-09-18
             Status|UNCONFIRMED                 |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I noticed a few missed optimizations:
  <bb 2> [local count: 1073741824]:
  i.0_1 = i;
  _2 = (short int) i.0_1;
  _14 = _2 >= 0;
  _15 = i.0_1 == 0;
  _16 = _14 & _15;
  if (_16 != 0)
    goto <bb 7>; [50.00%]
  else
    goto <bb 3>; [50.00%]

  <bb 3> [local count: 805306368]:
  _21 = (int) _2;
  _20 = _21 == 0;
  _29 = _21 > 1;
  _30 = _20 | _29;
  if (_30 != 0)
    goto <bb 7>; [72.67%]
  else
    goto <bb 4>; [27.33%]

Here this should have been just 
  if (i.0_1 == 0)
    goto <bb 7>; [50.00%]
  else
    goto <bb 3>; [50.00%]

And that would fix the rest.

I suspect if we extend:
/* Convert (X == CST1) && (X OP2 CST2) to a known value
   based on CST1 OP2 CST2.  Similarly for (X != CST1).  */
/* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type.
   Similarly for (X != Y).  */

to handle a cast for the second part. This will be fixed ...
Will handle that this weekend or earlier.

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
  2023-09-18 10:41 ` [Bug tree-optimization/111456] " rguenth at gcc dot gnu.org
  2023-09-18 14:48 ` pinskia at gcc dot gnu.org
@ 2023-09-18 23:34 ` pinskia at gcc dot gnu.org
  2023-09-19  0:52 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-18 23:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Reduced testcase for the missed optimization before and after the patch:
```
_Bool f(int a)
{
        _Bool t = a == 0;
        short t1 = a;
        _Bool t2 = t1 >= 0;
        return t & t2;
}
```

This really should just reduce to `return a == 0;`

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
                   ` (2 preceding siblings ...)
  2023-09-18 23:34 ` pinskia at gcc dot gnu.org
@ 2023-09-19  0:52 ` pinskia at gcc dot gnu.org
  2023-09-19  2:46 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-19  0:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
here is another missed optimization:
```
_Bool f2(int a, int b)
{
        _Bool t = a == b;
        unsigned t1 = a;
        unsigned t2 = b;
        _Bool b2 = t1 >= t2;
        return t & b2;
}
```
This should just be optimized to `return a == b;`.

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
                   ` (3 preceding siblings ...)
  2023-09-19  0:52 ` pinskia at gcc dot gnu.org
@ 2023-09-19  2:46 ` pinskia at gcc dot gnu.org
  2023-09-20 22:21 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-19  2:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55929
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55929&action=edit
Patch which I am testing

I still need to add testcases.

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
                   ` (4 preceding siblings ...)
  2023-09-19  2:46 ` pinskia at gcc dot gnu.org
@ 2023-09-20 22:21 ` pinskia at gcc dot gnu.org
  2023-09-21  2:44 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-20 22:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Just a quick note, --param=logical-op-non-short-circuit=0 also allows the
missed optimization to no longer to be missed.

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
                   ` (5 preceding siblings ...)
  2023-09-20 22:21 ` pinskia at gcc dot gnu.org
@ 2023-09-21  2:44 ` pinskia at gcc dot gnu.org
  2023-09-26 15:01 ` cvs-commit at gcc dot gnu.org
  2023-09-26 15:02 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-21  2:44 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2023-Septemb
                   |                            |er/631065.html
           Keywords|                            |patch

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Patch posted:
https://gcc.gnu.org/pipermail/gcc-patches/2023-September/631065.html

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
                   ` (6 preceding siblings ...)
  2023-09-21  2:44 ` pinskia at gcc dot gnu.org
@ 2023-09-26 15:01 ` cvs-commit at gcc dot gnu.org
  2023-09-26 15:02 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-09-26 15:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

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

commit r14-4280-gc3c6f30496d945b77dcb7f4ad8c3f8094f5a64a4
Author: Andrew Pinski <apinski@marvell.com>
Date:   Wed Sep 20 14:54:31 2023 -0700

    MATCH: Simplify `(A ==/!= B) &/| (((cast)A) CMP C)`

    This patch adds support to the pattern for `(A == B) &/| (A CMP C)`
    where the second A could be casted to a different type.
    Some were handled correctly if using seperate `if` statements
    but not if combined with BIT_AND/BIT_IOR.
    In the case of pr111456-1.c, the testcase would pass if
    `--param=logical-op-non-short-circuit=0` was used but now
    can be optimized always.

    OK? Bootstrapped and tested on x86_64-linux-gnu.

            PR tree-optimization/106164
            PR tree-optimization/111456

    gcc/ChangeLog:

            * match.pd (`(A ==/!= B) & (A CMP C)`):
            Support an optional cast on the second A.
            (`(A ==/!= B) | (A CMP C)`): Likewise.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/cmpbit-6.c: New test.
            * gcc.dg/tree-ssa/cmpbit-7.c: New test.
            * gcc.dg/tree-ssa/pr111456-1.c: New test.

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

* [Bug tree-optimization/111456] [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356
  2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
                   ` (7 preceding siblings ...)
  2023-09-26 15:01 ` cvs-commit at gcc dot gnu.org
@ 2023-09-26 15:02 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-26 15:02 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2023-09-26 15:02 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-18  9:45 [Bug tree-optimization/111456] New: [14 Regression] Dead Code Elimination Regression since r14-3719-gb34f3736356 theodort at inf dot ethz.ch
2023-09-18 10:41 ` [Bug tree-optimization/111456] " rguenth at gcc dot gnu.org
2023-09-18 14:48 ` pinskia at gcc dot gnu.org
2023-09-18 23:34 ` pinskia at gcc dot gnu.org
2023-09-19  0:52 ` pinskia at gcc dot gnu.org
2023-09-19  2:46 ` pinskia at gcc dot gnu.org
2023-09-20 22:21 ` pinskia at gcc dot gnu.org
2023-09-21  2:44 ` pinskia at gcc dot gnu.org
2023-09-26 15:01 ` cvs-commit at gcc dot gnu.org
2023-09-26 15:02 ` 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).