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/109546] New: [13 Regression]  Missed Dead Code Elimination when using __builtin_unreachable
Date: Tue, 18 Apr 2023 15:07:29 +0000	[thread overview]
Message-ID: <bug-109546-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 109546
           Summary: [13 Regression]  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);
static int a, c;
static int *b = &a;
static int **d = &b;
void __assert_fail() __attribute__((__noreturn__));
int main() {
  int *e = *d;
  if (e == &a || e == &c);
  else {
  __assert_fail();
  }
  if (e == &a || e == &c);
  else
    foo();
}

both gcc 12 and trunk at -O3 generate the following code:

main:
        movq    b(%rip), %rax
        cmpq    $c, %rax
        je      .L2
        cmpq    $a, %rax
        jne     .L7
.L2:
        xorl    %eax, %eax
        ret
.L7:
        pushq   %rax
        xorl    %eax, %eax
        call    __assert_fail
b:
        .quad   a

the call to foo is eliminated but the call to __assert_fail is missed, even
though 
both if statement conditions are identical. 

If add a __builtin_unreachable before the call to __assert_fail the opposite
happens:

void foo(void);
static int a, c;
static int *b = &a;
static int **d = &b;
void assert_fail() __attribute__((__noreturn__));
int main() {
  int *e = *d;
  if (e == &a || e == &c);
  else {
    __builtin_unreachable(); 
  assert_fail();
  }
  if (e == &a || e == &c);
  else
    foo();
}

gcc-trunk -O3 generates:

main:
        movq    b(%rip), %rax
        cmpq    $c, %rax
        je      .L4
        cmpq    $a, %rax
        je      .L4
        pushq   %rax
        call    foo
        xorl    %eax, %eax
        popq    %rdx
        ret
.L4:
        xorl    %eax, %eax
        ret
b:
        .quad   a

the call to __assert_fail is now properly eliminated but now the call to foo is
missed. 

This is a regression as gcc-12 generates: 

main:
        xorl    %eax, %eax
        ret

             reply	other threads:[~2023-04-18 15:07 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-18 15:07 theodort at inf dot ethz.ch [this message]
2023-04-19  7:58 ` [Bug tree-optimization/109546] [13/14 " rguenth at gcc dot gnu.org
2023-04-20 13:03 ` [Bug tree-optimization/109546] [13/14 Regression] Missed Dead Code Elimination when using __builtin_unreachable since r13-3596-ge7310e24b1c0ca marxin at gcc dot gnu.org
2023-04-21 18:52 ` amacleod at redhat dot com
2023-04-21 18:55 ` amacleod at redhat dot com
2023-04-21 19:18 ` amacleod at redhat dot com
2023-04-21 20:34 ` cvs-commit at gcc dot gnu.org
2023-04-26  6:58 ` rguenth at gcc dot gnu.org
2023-07-27  9:25 ` rguenth at gcc dot gnu.org
2023-09-17  4:10 ` 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-109546-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).