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

            Bug ID: 99357
           Summary: Missed Dead Code Elimination Opportunity
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: theodoridisgr at gmail dot com
  Target Milestone: ---

Compiling the following code with -O3 -S

static int a = 0;
extern void bar(void);
int main() {
    if (a)
        bar();
    a = 0;
    return 0;
}

results in the following asm:

main:
.LFB0:
        .cfi_startproc
        movl    a(%rip), %ecx
        testl   %ecx, %ecx
        jne     .L8
        movl    $0, a(%rip)
        xorl    %eax, %eax
        ret
.L8:
        pushq   %rax
        .cfi_def_cfa_offset 16
        call    bar
        xorl    %eax, %eax
        movl    $0, a(%rip)
        popq    %rdx
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc


Even though the branch calling bar is never executed, code for it is still
generated. If the a=0 assignment is removed, then the generated code is
simplified to:

main:
.LFB0:
        .cfi_startproc
        xorl    %eax, %eax
        ret
        .cfi_endproc

gcc -v:
Using built-in specs.
Target: x86_64-pc-linux-gnu
Configured with: ../gcc_source/configure
Thread model: posix
gcc version 11.0.1 20210302 (experimental) (GCC)
  • [Bug tree-optimization/99357] ... theodoridisgr at gmail dot com via Gcc-bugs

Reply via email to