public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d
@ 2023-08-04 17:39 theodort at inf dot ethz.ch
  2023-08-04 17:58 ` [Bug tree-optimization/110903] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-08-04 17:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110903
           Summary: [14 Regression] Dead Code Elimination Regression since
                    r14-1597-g64d90d06d2d
           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/7of4jjM3K

Given the following code:

void foo(void);
static char b, c;
static short e, f;
static int g = 41317;
static int(a)(int h, int i) { return h + i; }
static int(d)(int h, int i) { return i ? h : 0; }
int main() {
    {
        char j;
        short k;
        for (; g >= 10; g = (short)g) {
            int l = 1, m = 0;
            j = 8 * k;
            k = j <= 0;
            f = c + 3;
            for (; c < 2; c = f) {
                char n = 4073709551615;
                if (!(((m) >= 0) && ((m) <= 0))) {
                    __builtin_unreachable();
                }
                if (g)
                    ;
                else {
                    if ((m = k, (b = a(d(l, k), e) && n) || l) < k) foo();
                    e = l = 0;
                }
            }
        }
    }
}

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

main:
        movl    g(%rip), %edi
        cmpl    $9, %edi
        jle     .L25
        pushq   %rbp
        movl    %edi, %ecx
        movl    $1, %ebp
        movl    $1, %esi
        pushq   %rbx
        movl    $1, %ebx
        subq    $8, %rsp
        movzbl  c(%rip), %edx
        movsbw  %dl, %ax
        addl    $3, %eax
        movw    %ax, f(%rip)
        cmpb    $1, %dl
        jg      .L12
        .p2align 4,,10
        .p2align 3
.L6:
        testl   %edi, %edi
        je      .L7
        movb    %al, c(%rip)
        movsbw  %al, %dx
        cmpb    $1, %al
        jle     .L6
.L9:
        movswl  %di, %ecx
        movl    %ecx, g(%rip)
        cmpl    $9, %ecx
        jle     .L17
        addl    $3, %edx
        movw    %dx, f(%rip)
.L12:
        movswl  %cx, %eax
        cmpw    $9, %cx
        jle     .L29
.L4:
        jmp     .L4
        .p2align 4,,10
        .p2align 3
.L7:
        movswl  e(%rip), %ecx
        movl    %ebx, %edx
        andl    %esi, %edx
        addl    %ecx, %edx
        orl     %esi, %edx
        jne     .L10
        testb   %bpl, %bpl
        jne     .L30
.L10:
        xorl    %edx, %edx
        movb    %al, c(%rip)
        movw    %dx, e(%rip)
        movsbw  %al, %dx
        cmpb    $1, %al
        jg      .L9
        xorl    %esi, %esi
        jmp     .L6
        .p2align 4,,10
        .p2align 3
.L30:
        call    foo
        movzwl  f(%rip), %eax
        movl    g(%rip), %edi
        jmp     .L10
.L29:
        movl    %eax, g(%rip)
.L17:
        addq    $8, %rsp
        xorl    %eax, %eax
        popq    %rbx
        popq    %rbp
        ret
.L25:
        xorl    %eax, %eax
        ret

gcc-13.2.0 -O3 eliminates the call to foo:

main:
        movl    g(%rip), %esi
        movl    %esi, %ecx
        cmpl    $9, %esi
        jle     .L14
        movzbl  c(%rip), %eax
        movsbw  %al, %dx
        addl    $3, %edx
        movw    %dx, f(%rip)
        cmpb    $1, %al
        jg      .L12
        xorl    %eax, %eax
        testb   %al, %al
        movl    %edx, %eax
        je      .L6
        cmpb    $1, %dl
        jg      .L22
.L7:
        jmp     .L7
        .p2align 4,,10
        .p2align 3
.L22:
        movb    %dl, c(%rip)
.L8:
        movswl  %si, %ecx
        movl    %ecx, g(%rip)
        cmpl    $9, %ecx
        jle     .L14
        addl    $3, %eax
        cbtw
        movw    %ax, f(%rip)
.L12:
        movswl  %cx, %eax
        cmpw    $9, %cx
        jle     .L23
.L4:
        jmp     .L4
        .p2align 4,,10
        .p2align 3
.L6:
        movb    %dl, c(%rip)
        cmpw    $1, %dx
        jg      .L8
        .p2align 4,,10
        .p2align 3
.L9:
        movl    g(%rip), %eax
        testl   %eax, %eax
        jne     .L9
        movw    $0, e(%rip)
        movb    %dl, c(%rip)
.L23:
        movl    %eax, g(%rip)
.L14:
        xorl    %eax, %eax
        ret

Bisects to r14-1597-g64d90d06d2d

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

* [Bug tree-optimization/110903] [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d
  2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
@ 2023-08-04 17:58 ` pinskia at gcc dot gnu.org
  2023-08-04 18:37 ` [Bug tree-optimization/110903] [12/13/14 Regression] Dead Code Elimination Regression pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-04 17:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note the original testcase has some obvious use of an uninitialized variable.
Anyways here is a fixed up testcase which does not have that uninitialized
variable and GCC 13 was able to optimize away the call to foo still:
```
void foo(void);
static signed char b, c;
static short e, f;
static int g = 41317;
static int(a)(int h, int i) { return h + i; }
static int(d)(int h, int i) { return i ? h : 0; }
short t = 10;
int main() {
    {
        signed char j;
        short k = t;
        for (; g >= 10; g = (short)g) {
            _Bool l = 1;
            int m = 0;
            j = 8 * k;
            k = j <= 0;
            f = c + 3;
            for (; c < 2; c = f) {
                signed char n = 4073709551615;
                if (!(((m) >= 0) && ((m) <= 0))) {
                    __builtin_unreachable();
                }
                if (g)
                    ;
                else {
                    if ((m = k, (b = a(d(l, k), e) && n) || l) < k) foo();
                    e = l = 0;
                }
            }
        }
    }
}
```

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

* [Bug tree-optimization/110903] [12/13/14 Regression] Dead Code Elimination Regression
  2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
  2023-08-04 17:58 ` [Bug tree-optimization/110903] " pinskia at gcc dot gnu.org
@ 2023-08-04 18:37 ` pinskia at gcc dot gnu.org
  2023-11-03 17:39 ` tkoenig at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-04 18:37 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-08-04
             Status|UNCONFIRMED                 |NEW
   Target Milestone|---                         |12.4
     Ever confirmed|0                           |1
           Keywords|                            |needs-bisection
            Summary|[14 Regression] Dead Code   |[12/13/14 Regression] Dead
                   |Elimination Regression      |Code Elimination Regression
                   |since r14-1597-g64d90d06d2d |

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. before r14-1597, there was a jump threading happening with respect
to:
  if (j_32 <= 0)
    goto <bb 9>; [50.00%]
  else
    goto <bb 8>; [50.00%]

  <bb 8> [local count: 238907556]:

  <bb 9> [local count: 477815112]:
  # iftmp.10_37 = PHI <_11(7), 0(8)>

But after, we change that into
iftmp.10_37 = _11 & (j_32 <= 0);

It just happens we depend on that due to:
  _43 = l_22 | _25;
  _39 = j_32 <= 0;
  _12 = ~_43;
  _44 = _12 & _39;


If we change the code to be:
```
void foo(void);
static signed char b, c;
static short e, f;
static int g = 41317;
static int(a)(int h, int i) { return h + i; }
static int(d)(int h, int i) { return i & h;}//i ? h : 0; }
short t = 10;
int main() {
    {
        signed char j;
        short k = t;
        for (; g >= 10; g = (short)g) {
            _Bool l = 1;
            int m = 0;
            j = 8 * k;
            k = j <= 0;
            f = c + 3;
            for (; c < 2; c = f) {
                signed char n = 4073709551615;
                if (!(((m) >= 0) && ((m) <= 0))) {
                    __builtin_unreachable();
                }
                if (g)
                    ;
                else {
                    if ((m = k, (b = a(d(l, k), e) && n) || l) < k) foo();
                    e = l = 0;
                }
            }
        }
    }
}
```

GCC 11 is able to remove the call to foo but GCC 12 cannot.
the IR for the part where the phiopt2 changes on the trunk is similar enough.

So this is instead a regression from GCC 11.

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

* [Bug tree-optimization/110903] [12/13/14 Regression] Dead Code Elimination Regression
  2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
  2023-08-04 17:58 ` [Bug tree-optimization/110903] " pinskia at gcc dot gnu.org
  2023-08-04 18:37 ` [Bug tree-optimization/110903] [12/13/14 Regression] Dead Code Elimination Regression pinskia at gcc dot gnu.org
@ 2023-11-03 17:39 ` tkoenig at gcc dot gnu.org
  2023-11-03 17:51 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2023-11-03 17:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
The code from comment#2 and from comment#3 no longer calls foo
with current trunk, r14-5108-g751fc7bcdcdf25 .

Now, to see which commit fixed it...

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

* [Bug tree-optimization/110903] [12/13/14 Regression] Dead Code Elimination Regression
  2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
                   ` (2 preceding siblings ...)
  2023-11-03 17:39 ` tkoenig at gcc dot gnu.org
@ 2023-11-03 17:51 ` pinskia at gcc dot gnu.org
  2023-11-04 16:29 ` [Bug tree-optimization/110903] [12/13 " tkoenig at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-03 17:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Thomas Koenig from comment #3)
> The code from comment#2 and from comment#3 no longer calls foo
> with current trunk, r14-5108-g751fc7bcdcdf25 .
> 
> Now, to see which commit fixed it...

My bet is on r14-4089-gd45ddc2c04e471 .

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

* [Bug tree-optimization/110903] [12/13 Regression] Dead Code Elimination Regression
  2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
                   ` (3 preceding siblings ...)
  2023-11-03 17:51 ` pinskia at gcc dot gnu.org
@ 2023-11-04 16:29 ` tkoenig at gcc dot gnu.org
  2023-11-04 20:37 ` tkoenig at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2023-11-04 16:29 UTC (permalink / raw)
  To: gcc-bugs

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

Thomas Koenig <tkoenig at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[12/13/14 Regression] Dead  |[12/13 Regression] Dead
                   |Code Elimination Regression |Code Elimination Regression

--- Comment #5 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #4)
> (In reply to Thomas Koenig from comment #3)
> > The code from comment#2 and from comment#3 no longer calls foo
> > with current trunk, r14-5108-g751fc7bcdcdf25 .
> > 
> > Now, to see which commit fixed it...
> 
> My bet is on r14-4089-gd45ddc2c04e471 .

Weird thing is that I do see this on POWER (I often use gcc120 for
compiling because it is the beefiest machine I can lay my hands on).

However, this was actually fixed by r14-4141-gbf6b107e2a3423:

bf6b107e2a342319b3787ec960fc8014ef3aff91 is the first new commit
commit bf6b107e2a342319b3787ec960fc8014ef3aff91
Author: Andrew MacLeod <amacleod@redhat.com>
Date:   Wed Sep 13 11:52:15 2023 -0400

    New early __builtin_unreachable processing.

    in VRP passes before __builtin_unreachable MUST be removed, only remove it
    if all exports affected by the unreachable can have global values updated,
and
    do not involve loads from memory.

            PR tree-optimization/110080
            PR tree-optimization/110249
            gcc/
            * tree-vrp.cc (remove_unreachable::final_p): New.
            (remove_unreachable::maybe_register): Rename from
            maybe_register_block and call early or final routine.
            (fully_replaceable): New.
            (remove_unreachable::handle_early): New.
            (remove_unreachable::remove_and_update_globals): Remove
            non-final processing.
            (rvrp_folder::rvrp_folder): Add final flag to constructor.
            (rvrp_folder::post_fold_bb): Remove unreachable registration.
            (rvrp_folder::pre_fold_stmt): Move unreachable processing to here.
            (execute_ranger_vrp): Adjust some call parameters.

            gcc/testsuite/
            * g++.dg/pr110249.C: New.
            * gcc.dg/pr110080.c: New.
            * gcc.dg/pr93917.c: Adjust.

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

* [Bug tree-optimization/110903] [12/13 Regression] Dead Code Elimination Regression
  2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
                   ` (4 preceding siblings ...)
  2023-11-04 16:29 ` [Bug tree-optimization/110903] [12/13 " tkoenig at gcc dot gnu.org
@ 2023-11-04 20:37 ` tkoenig at gcc dot gnu.org
  2023-11-04 20:41 ` [Bug tree-optimization/110903] [12/13 Regression] Dead Code Elimination Regression since r12-4526-gd8edfadfc7a979 pinskia at gcc dot gnu.org
  2024-05-07  7:41 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2023-11-04 20:37 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
The original regression was caused by r12-4526-gd8edfadfc7a979 .

d8edfadfc7a9795b65177a50ce44fd348858e844 is the first bad commit
commit d8edfadfc7a9795b65177a50ce44fd348858e844
Author: Aldy Hernandez <aldyh@redhat.com>
Date:   Mon Oct 4 09:47:02 2021 +0200

    Disallow loop rotation and loop header crossing in jump threaders.

    There is a lot of fall-out from this patch, as there were many threading
    tests that assumed the restrictions introduced by this patch were valid.
    Some tests have merely shifted the threading to after loop
    optimizations, but others ended up with no threading opportunities at
    all.  Surprisingly some tests ended up with more total threads.  It was
    a crapshoot all around.

    On a postive note, there are 6 tests that no longer XFAIL, and one
    guality test which now passes.

    I felt a bit queasy about such a fundamental change wrt threading, so I
    ran it through my callgrind test harness (.ii files from a bootstrap).
    There was no change in overall compilation, DOM, or the VRP threaders.

    However, there was a slight increase of 1.63% in the backward threader.
    I'm pretty sure we could reduce this if we incorporated the restrictions
    into their profitability code.  This way we could stop the search when
    we ran into one of these restrictions.  Not sure it's worth it at this
    point.

    Tested on x86-64 Linux.

    Co-authored-by: Richard Biener <rguenther@suse.de>

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

* [Bug tree-optimization/110903] [12/13 Regression] Dead Code Elimination Regression since r12-4526-gd8edfadfc7a979
  2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
                   ` (5 preceding siblings ...)
  2023-11-04 20:37 ` tkoenig at gcc dot gnu.org
@ 2023-11-04 20:41 ` pinskia at gcc dot gnu.org
  2024-05-07  7:41 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-11-04 20:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed.  I doubt this missed optimization patch can be backported so closing as
fixed.  Maybe it would be good to have a testcase committed for both cases
though.

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

* [Bug tree-optimization/110903] [12/13 Regression] Dead Code Elimination Regression since r12-4526-gd8edfadfc7a979
  2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
                   ` (6 preceding siblings ...)
  2023-11-04 20:41 ` [Bug tree-optimization/110903] [12/13 Regression] Dead Code Elimination Regression since r12-4526-gd8edfadfc7a979 pinskia at gcc dot gnu.org
@ 2024-05-07  7:41 ` rguenth at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-07  7:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 14.1 is being released, retargeting bugs to GCC 14.2.

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

end of thread, other threads:[~2024-05-07  7:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-04 17:39 [Bug tree-optimization/110903] New: [14 Regression] Dead Code Elimination Regression since r14-1597-g64d90d06d2d theodort at inf dot ethz.ch
2023-08-04 17:58 ` [Bug tree-optimization/110903] " pinskia at gcc dot gnu.org
2023-08-04 18:37 ` [Bug tree-optimization/110903] [12/13/14 Regression] Dead Code Elimination Regression pinskia at gcc dot gnu.org
2023-11-03 17:39 ` tkoenig at gcc dot gnu.org
2023-11-03 17:51 ` pinskia at gcc dot gnu.org
2023-11-04 16:29 ` [Bug tree-optimization/110903] [12/13 " tkoenig at gcc dot gnu.org
2023-11-04 20:37 ` tkoenig at gcc dot gnu.org
2023-11-04 20:41 ` [Bug tree-optimization/110903] [12/13 Regression] Dead Code Elimination Regression since r12-4526-gd8edfadfc7a979 pinskia at gcc dot gnu.org
2024-05-07  7:41 ` rguenth 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).