public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/109513] New: Missed Dead Code Elimination when using __builtin_unreachable
@ 2023-04-14 13:15 theodort at inf dot ethz.ch
  2023-04-17  7:11 ` [Bug tree-optimization/109513] " marxin at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-04-14 13:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109513
           Summary: Missed Dead Code Elimination when using
                    __builtin_unreachable
           Product: gcc
           Version: 13.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: ---

cat input.c

void foo(void);
void bar(void);

static char a, h;
static int b, c, d, k;
static int **e;
static int ***f = &e;
static short g;
static int *j, *l = &c;

static char m(int *i) {
  if (*i)
    return 0;
  for (; c;)
    ;
  if (i == &b)
    bar(); 
  return 1;
}
int main() {
  int *n = &b;
  for (; k; ++k)
    m(n);
  h = a + 10;
  if (h) {
    int *o = &d;
    for (; g; ++g) {
      *e = l;
      j = **f;
      o = j;
    }
    if (!(o == &c || o == &d))
      foo();
  }
}

In the above piece of code the calls to foo and bar are both dead

gcc-trunk at -O3 generates the following code:
main:
        subq    $8, %rsp
        movl    k(%rip), %ecx
        testl   %ecx, %ecx
        je      .L7
.L2:
        movl    c(%rip), %edx
        testl   %edx, %edx
        je      .L5
.L6:
        jmp     .L6
.L5:
        call    bar
        addl    $1, k(%rip)
        jne     .L2
.L7:
        cmpw    $0, g(%rip)
        je      .L4
        movq    e(%rip), %rax
        movq    $c, (%rax)
        xorl    %eax, %eax
        movw    %ax, g(%rip)
.L4:
        xorl    %eax, %eax
        addq    $8, %rsp
        ret

it eliminates the call to foo but not to bar. If I try to "help" the compiler
by replacing bar() with __builtin_unreachable(), it generates worse code and it
also misses eliminating the dead call to foo:

void foo(void);

static char a, h;
static int b, c, d, k;
static int **e;
static int ***f = &e;
static short g;
static int *j, *l = &c;

static char m(int *i) {
  if (*i)
    return 0;
  for (; c;)
    ;
  if (i == &b)
    __builtin_unreachable(); // <- the call to bar was here
  return 1;
}

int main() {
  int *n = &b;
  for (; k; ++k)
    m(n);
  h = a + 10;
  if (h) {
    int *o = &d;
    for (; g; ++g) {
      *e = l;
      j = **f;
      o = j;
    }
    if (!(o == &c || o == &d))
      foo();
  }
}

gcc-trunk -O3 output:

main:
        movl    k(%rip), %ecx
        testl   %ecx, %ecx
        je      .L22
.L2:
        jmp     .L2
.L22:
        movq    e(%rip), %rsi
        movzwl  g(%rip), %eax
        xorl    %ecx, %ecx
        movl    $d, %edx
        jmp     .L3
.L4:
        movq    $c, (%rsi)
        addl    $1, %eax
        movl    $1, %ecx
        movl    $c, %edx
.L3:
        testw   %ax, %ax
        jne     .L4
        testb   %cl, %cl
        je      .L5
        movw    $0, g(%rip)
.L5:
        cmpq    $d, %rdx
        je      .L18
        cmpq    $c, %rdx
        je      .L18
        pushq   %rax
        call    foo
        xorl    %eax, %eax
        popq    %rdx
        ret
.L18:
        xorl    %eax, %eax
        ret

This is also an old regression. gcc-7.4 at -O3 generates for the version with
__builtin_unreachable:

main:
        movl    k(%rip), %eax
        testl   %eax, %eax
        je      .L7
.L2:
        jmp     .L2
.L7:
        cmpw    $0, g(%rip)
        je      .L4
        movq    e(%rip), %rax
        movw    $0, g(%rip)
        movq    $c, (%rax)
.L4:
        xorl    %eax, %eax
        ret

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

* [Bug tree-optimization/109513] Missed Dead Code Elimination when using __builtin_unreachable
  2023-04-14 13:15 [Bug tree-optimization/109513] New: Missed Dead Code Elimination when using __builtin_unreachable theodort at inf dot ethz.ch
@ 2023-04-17  7:11 ` marxin at gcc dot gnu.org
  2023-04-17  7:20 ` pinskia at gcc dot gnu.org
  2023-04-17  7:31 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: marxin at gcc dot gnu.org @ 2023-04-17  7:11 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hubicka at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-04-17
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
The __builtin_unreachable missed optimization started with
r8-4670-gb00ff621f6eea4.

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

* [Bug tree-optimization/109513] Missed Dead Code Elimination when using __builtin_unreachable
  2023-04-14 13:15 [Bug tree-optimization/109513] New: Missed Dead Code Elimination when using __builtin_unreachable theodort at inf dot ethz.ch
  2023-04-17  7:11 ` [Bug tree-optimization/109513] " marxin at gcc dot gnu.org
@ 2023-04-17  7:20 ` pinskia at gcc dot gnu.org
  2023-04-17  7:31 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-17  7:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is interesting because the C++ front-end causes slightly different IR and
with __builtin_unreachable produces decent code at -O3 still.

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

* [Bug tree-optimization/109513] Missed Dead Code Elimination when using __builtin_unreachable
  2023-04-14 13:15 [Bug tree-optimization/109513] New: Missed Dead Code Elimination when using __builtin_unreachable theodort at inf dot ethz.ch
  2023-04-17  7:11 ` [Bug tree-optimization/109513] " marxin at gcc dot gnu.org
  2023-04-17  7:20 ` pinskia at gcc dot gnu.org
@ 2023-04-17  7:31 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-17  7:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> This is interesting because the C++ front-end causes slightly different IR
> and with __builtin_unreachable produces decent code at -O3 still.

The difference is due to -ffinite-loops .

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

end of thread, other threads:[~2023-04-17  7:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-14 13:15 [Bug tree-optimization/109513] New: Missed Dead Code Elimination when using __builtin_unreachable theodort at inf dot ethz.ch
2023-04-17  7:11 ` [Bug tree-optimization/109513] " marxin at gcc dot gnu.org
2023-04-17  7:20 ` pinskia at gcc dot gnu.org
2023-04-17  7:31 ` 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).