public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "theodort at inf dot ethz.ch" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/109513] New: Missed Dead Code Elimination when using __builtin_unreachable
Date: Fri, 14 Apr 2023 13:15:42 +0000	[thread overview]
Message-ID: <bug-109513-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             reply	other threads:[~2023-04-14 13:15 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-14 13:15 theodort at inf dot ethz.ch [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-109513-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).