public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
@ 2023-10-13 15:46 theodort at inf dot ethz.ch
  2023-10-13 15:48 ` [Bug tree-optimization/111799] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-10-13 15:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111799
           Summary: [14 Regression] Missed Dead Code Elimination since
                    r14-2365-g2e406f0753e
           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/bqcvMcbqn

Given the following code:

void foo(void);
static struct d {
    short e;
} g = {205}, *h = &g;
static int i;
static int *j, *a = &i;
static int **k = &j;
static int ****l;
static int *****m;
static char(n)(char b, char c) { return b + c; }
static char(o)(char b, char c) { return b * c; }
static short(p)(short f) {
    if (!(((f) >= 1) && ((f) <= 65459))) {
        __builtin_unreachable();
    }
    return 0;
}
static int *q(short);
static void s(struct d) { *k = q(i); }
static int *q(short ad) {
    int b = *a;
    ad = -21;
    for (; ad; ad = n(ad, 7)) p((ad ^ b && *a) <= *a);
    return *k;
}
int main() {
    i = 0;
    for (;; i = 1) {
        q(3);
        char r = o(126 | 1, g.e);
        p(r);
        s(*h);
        if (i) break;
        m = &l;
    }
    if (m)
        ;
    else
        foo();
    ;
}

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

main:
        movl    $2, %esi
        xorl    %ecx, %ecx
        xorl    %r9d, %r9d
        movl    $8, %edi
        movl    $0, i(%rip)
        .p2align 4,,10
        .p2align 3
.L10:
        movl    $-21, %eax
.L2:
        testl   %ecx, %ecx
        je      .L29
        leal    7(%rax), %edx
        movsbw  %dl, %r8w
        testb   %dl, %dl
        je      .L23
.L5:
        movl    $8, %eax
        cmpb    $1, %dl
        je      .L2
        movl    %r8d, %eax
        leal    7(%rax), %edx
        movsbw  %dl, %r8w
        testb   %dl, %dl
        jne     .L5
        .p2align 4,,10
        .p2align 3
.L23:
        movl    $-21, %eax
        addb    $7, %al
        je      .L30
.L9:
        movsbl  %al, %edx
        cmpl    %edx, %ecx
        cmove   %edi, %eax
        cbtw
        addb    $7, %al
        jne     .L9
.L30:
        cmpl    $1, %esi
        je      .L31
        movl    $1, %esi
        movl    $1, %ecx
        movl    $1, %r9d
        jmp     .L10
        .p2align 4,,10
        .p2align 3
.L29:
        testb   $1, %al
        je      .L4
        leal    7(%rax), %edx
        movsbw  %dl, %ax
        testb   %dl, %dl
        je      .L23
.L4:
        leal    14(%rax), %edx
        movsbw  %dl, %ax
        testb   %dl, %dl
        jne     .L4
        jmp     .L23
        .p2align 4,,10
        .p2align 3
.L31:
        testb   %r9b, %r9b
        je      .L11
        movq    $l, m(%rip)
        movl    %ecx, i(%rip)
.L25:
        xorl    %eax, %eax
        ret
.L11:
        cmpq    $0, m(%rip)
        jne     .L25
        pushq   %rax
        call    foo
        xorl    %eax, %eax
        popq    %rdx
        ret

gcc-13.2.0 -O2 eliminates the call to foo:

main:
        movq    $l, m(%rip)
        xorl    %eax, %eax
        movl    $1, i(%rip)
        ret

Bisects to r14-2365-g2e406f0753e

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

* [Bug tree-optimization/111799] [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
@ 2023-10-13 15:48 ` pinskia at gcc dot gnu.org
  2023-10-13 15:50 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-13 15:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |14.0
           Keywords|                            |missed-optimization

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

* [Bug tree-optimization/111799] [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
  2023-10-13 15:48 ` [Bug tree-optimization/111799] " pinskia at gcc dot gnu.org
@ 2023-10-13 15:50 ` pinskia at gcc dot gnu.org
  2023-10-13 15:52 ` 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-10-13 15:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Right now I get an ICE while doing -fdump-tree-ccp1-details even:during GIMPLE
pass: ccp
dump file: /app/output.cpp.034t.ccp1
<source>: In function 'q':
<source>:42:1: internal compiler error: Segmentation fault
   42 | }
      | ^
0x22fdf0e internal_error(char const*, ...)
        ???:0
0x14e8779 print_hex(generic_wide_int<wide_int_ref_storage<false, true> >
const&, char*)
        ???:0
0x14e8beb print_hex(generic_wide_int<wide_int_ref_storage<false, true> >
const&, _IO_FILE*)
        ???:0
g++: internal compiler error: Segmentation fault signal terminated program cc1
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
See <https://gcc.gnu.org/bugs/> for instructions.
Compiler returned: 4

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

* [Bug tree-optimization/111799] [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
  2023-10-13 15:48 ` [Bug tree-optimization/111799] " pinskia at gcc dot gnu.org
  2023-10-13 15:50 ` pinskia at gcc dot gnu.org
@ 2023-10-13 15:52 ` pinskia at gcc dot gnu.org
  2023-10-16  4:08 ` 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-10-13 15:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #1)
> Right now I get an ICE while doing -fdump-tree-ccp1-details even:during

Filed PR 111800 for that ...

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

* [Bug tree-optimization/111799] [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
                   ` (2 preceding siblings ...)
  2023-10-13 15:52 ` pinskia at gcc dot gnu.org
@ 2023-10-16  4:08 ` pinskia at gcc dot gnu.org
  2023-10-17 11:08 ` theodort at inf dot ethz.ch
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-16  4:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm, I think we reach __builtin_unreachable always and that is confusing
everything.

If we inline s early on (via using the always_inline attribute). main turns
into just __builtin_unreachable .

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

* [Bug tree-optimization/111799] [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
                   ` (3 preceding siblings ...)
  2023-10-16  4:08 ` pinskia at gcc dot gnu.org
@ 2023-10-17 11:08 ` theodort at inf dot ethz.ch
  2023-10-18 23:55 ` [Bug middle-end/111799] " pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-10-17 11:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Theodoros Theodoridis <theodort at inf dot ethz.ch> ---
Oops, there was a bug in my reduction, here's the fixed code:

https://godbolt.org/z/shxffzs8E

void foo(void);
typedef unsigned short uint16_t;
static int b;
static int ****c;
static int *f = &b;
static int *****ad;
static char(a)(char g, char h) { return g + h; }
static char(d)(char g, char h) { return g * h; }
static void(e)(uint16_t g) {
    if (!(((g) >= 1) && ((g) <= 65459))) {
        __builtin_unreachable();
    }
}
int main() {
    b = 0;
    for (;; b = 1) {
        char i = d(126 | 1, 205);
        e(i);
        short j;
        int k = *f;
        j = -21;
        for (; j; j = a(j, 7)) e((j ^ k && *f) <= *f);
        if (b) break;
        ad = &c;
    }
    if (ad)
        ;
    else
        foo();
    ;
}



trunk -O2:

main:
        movl    $0, b(%rip)
        movl    $2, %esi
        xorl    %ecx, %ecx
        xorl    %r8d, %r8d
.L7:
        movl    $-21, %eax
.L2:
        testl   %ecx, %ecx
        je      .L26
        leal    7(%rax), %edx
        movsbw  %dl, %di
        testb   %dl, %dl
        je      .L6
.L5:
        movl    $8, %eax
        cmpb    $1, %dl
        je      .L2
        movl    %edi, %eax
        leal    7(%rax), %edx
        movsbw  %dl, %di
        testb   %dl, %dl
        jne     .L5
.L6:
        cmpl    $1, %esi
        je      .L27
.L12:
        movl    $1, %esi
        movl    $1, %ecx
        movl    $1, %r8d
        jmp     .L7
.L26:
        testb   $1, %al
        je      .L4
        leal    7(%rax), %edx
        movsbw  %dl, %ax
        testb   %dl, %dl
        je      .L6
.L4:
        leal    14(%rax), %edx
        movsbw  %dl, %ax
        testb   %dl, %dl
        jne     .L4
        cmpl    $1, %esi
        jne     .L12
.L27:
        testb   %r8b, %r8b
        je      .L28
        movq    $c, ad(%rip)
        movl    %ecx, b(%rip)
.L23:
        xorl    %eax, %eax
        ret
.L28:
        cmpq    $0, ad(%rip)
        jne     .L23
        pushq   %rax
        call    foo
        xorl    %eax, %eax
        popq    %rdx
        ret


13.2 -O2:

main:
        movq    $c, ad(%rip)
        xorl    %eax, %eax
        movl    $1, b(%rip)
        ret


the bisection remains the same: r14-2365-g2e406f0753e

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

* [Bug middle-end/111799] [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
                   ` (4 preceding siblings ...)
  2023-10-17 11:08 ` theodort at inf dot ethz.ch
@ 2023-10-18 23:55 ` pinskia at gcc dot gnu.org
  2024-03-07 21:04 ` law at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-10-18 23:55 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
          Component|tree-optimization           |middle-end
     Ever confirmed|0                           |1
           Keywords|                            |TREE
   Last reconfirmed|                            |2023-10-18

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Theodoros Theodoridis from comment #4)
> Oops, there was a bug in my reduction, here's the fixed code:
> 
> https://godbolt.org/z/shxffzs8E
> 
> void foo(void);
> typedef unsigned short uint16_t;
> static int b;
> static int ****c;
> static int *f = &b;
> static int *****ad;
> static char(a)(char g, char h) { return g + h; }
> static char(d)(char g, char h) { return g * h; }
> static void(e)(uint16_t g) {
>     if (!(((g) >= 1) && ((g) <= 65459))) {
>         __builtin_unreachable();
>     }
> }
> int main() {
>     b = 0;
>     for (;; b = 1) {
>         char i = d(126 | 1, 205);
>         e(i);
>         short j;
>         int k = *f;
>         j = -21;
>         for (; j; j = a(j, 7)) e((j ^ k && *f) <= *f);
>         if (b) break;
>         ad = &c;
>     }
>     if (ad)
>         ;
>     else
>         foo();
>     ;
> }


Confirmed with this testcase but what is interesting is that the optimization
of calling foo away does NOT happen at the gimple level but at the RTL level
and just happens on x86_64 but NOT on aarch64.

on aarch64 for GCC 13, we even have:
        mov     w0, 1
        str     wzr, [x1, #:lo12:.LANCHOR0]
        cbz     w0, .L13

Which is obvious should have been removed ...

Anyways this is a missed jump threading that should have happened at the gimple
level. I have not looked into why it is not done.

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

* [Bug middle-end/111799] [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
                   ` (5 preceding siblings ...)
  2023-10-18 23:55 ` [Bug middle-end/111799] " pinskia at gcc dot gnu.org
@ 2024-03-07 21:04 ` law at gcc dot gnu.org
  2024-03-09  5:43 ` law at gcc dot gnu.org
  2024-05-07  7:42 ` [Bug middle-end/111799] [14/15 " rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-07 21:04 UTC (permalink / raw)
  To: gcc-bugs

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

Jeffrey A. Law <law at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2
                 CC|                            |law at gcc dot gnu.org

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

* [Bug middle-end/111799] [14 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
                   ` (6 preceding siblings ...)
  2024-03-07 21:04 ` law at gcc dot gnu.org
@ 2024-03-09  5:43 ` law at gcc dot gnu.org
  2024-05-07  7:42 ` [Bug middle-end/111799] [14/15 " rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: law at gcc dot gnu.org @ 2024-03-09  5:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jeffrey A. Law <law at gcc dot gnu.org> ---
I'd hazard a guess we need to first eliminate the ad = &c assignment.  That
should in turn allow us to realize ad is unchanging.

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

* [Bug middle-end/111799] [14/15 Regression] Missed Dead Code Elimination since  r14-2365-g2e406f0753e
  2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
                   ` (7 preceding siblings ...)
  2024-03-09  5:43 ` law at gcc dot gnu.org
@ 2024-05-07  7:42 ` rguenth at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-05-07  7:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #7 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] 10+ messages in thread

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

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-13 15:46 [Bug tree-optimization/111799] New: [14 Regression] Missed Dead Code Elimination since r14-2365-g2e406f0753e theodort at inf dot ethz.ch
2023-10-13 15:48 ` [Bug tree-optimization/111799] " pinskia at gcc dot gnu.org
2023-10-13 15:50 ` pinskia at gcc dot gnu.org
2023-10-13 15:52 ` pinskia at gcc dot gnu.org
2023-10-16  4:08 ` pinskia at gcc dot gnu.org
2023-10-17 11:08 ` theodort at inf dot ethz.ch
2023-10-18 23:55 ` [Bug middle-end/111799] " pinskia at gcc dot gnu.org
2024-03-07 21:04 ` law at gcc dot gnu.org
2024-03-09  5:43 ` law at gcc dot gnu.org
2024-05-07  7:42 ` [Bug middle-end/111799] [14/15 " 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).