llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.09k stars 12k forks source link

Dead Code Elimination Regression at -O3 (trunk vs 13.0.0) #51922

Open llvmbot opened 2 years ago

llvmbot commented 2 years ago
Bugzilla Link 52580
Version trunk
OS Linux
Reporter LLVM Bugzilla Contributor
CC @anton-afanasyev

Extended Description

cat case.c
void foo(void);

static int b;
static int *e = &b;
static char a(char f) { return f < 0 ? f : 0; }
static char c(unsigned char f, int g) { return f >> g; }

int main() {
  int h = *e;
  *e = a(h);
  if (c(h, 1)){
    if (h)
      *e = 0;
    else
      foo();
  }
  c(0, 0);
}

trunk cannot eliminate the call to foo but 13.0.0 can:

clang-13.0.0 -O3 -S -o /dev/stdout case.c
main:                                   # @main
    .cfi_startproc
# %bb.0:
    xorl    %eax, %eax
    retq
.Lfunc_end0:
    .size   main, .Lfunc_end0-main
    .cfi_endproc
                                        # -- End function
clang-trunk -O3 -S -o /dev/stdout case.c
main:                                   # @main
    .cfi_startproc
# %bb.0:
    pushq   %rax
    .cfi_def_cfa_offset 16
    movl    b(%rip), %eax
    xorl    %ecx, %ecx
    testb   %al, %al
    movsbl  %al, %edx
    cmovsl  %edx, %ecx
    movl    %ecx, b(%rip)
    cmpb    $2, %dl
    jb  .LBB0_4
# %bb.1:
    testl   %eax, %eax
    je  .LBB0_3
# %bb.2:
    movl    $0, b(%rip)
.LBB0_4:
    xorl    %eax, %eax
    popq    %rcx
    .cfi_def_cfa_offset 8
    retq
.LBB0_3:
    .cfi_def_cfa_offset 16
    callq   foo
    xorl    %eax, %eax
    popq    %rcx
    .cfi_def_cfa_offset 8
    retq
.Lfunc_end0:
clang-trunk -v
clang version 14.0.0 (https://github.com/llvm/llvm-project dc9b5550b2238d65bcaa937c412d51802d819aeb)
Target: x86_64-unknown-linux-gnu
Thread model: posix

Started with https://github.com/llvm/llvm-project/commit/cfb6dfcbd13b417f83d8adca69b60ba4254b4233

llvmbot commented 2 years ago

@llvm/issue-subscribers-clang-codegen

fhahn commented 2 years ago

It looks like this is still reproducible on current main. @anton-afanasyev is this an expected fallout of cfb6dfc?

anton-afanasyev commented 2 years ago

@fhahn Thanks for pointing this out! No, it is not expected. Assigned to myself.