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 via email to