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

            Bug ID: 114161
           Summary: Missing Loop Optimization for Unexecuted Loop
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carnet at student dot ethz.ch
  Target Milestone: ---

In the following code the for-loop is never executed because 'a' is initialized
to zero. GCC -O3 and -Os compile to a loop while clang is able to optimize it
away.

https://godbolt.org/z/G7r4585o5

Source:
static int a = 0;
static char b;
static short(c)(short d, short e) { return d - e; }
int foo() {
    for (; a; a = c(a, 7))
        ;
    return a;
}

x86 -O3 Assembly
foo:
        movl    a(%rip), %eax
        testl   %eax, %eax
        je      .L2
        leal    -7(%rax), %edx
        testb   $1, %al
        je      .L3
        movswl  %dx, %eax
        testl   %eax, %eax
        je      .L13
.L3:
        subl    $14, %eax
        cwtl
        testl   %eax, %eax
        jne     .L3
.L13:
        movl    $0, a(%rip)
.L2:
        xorl    %eax, %eax
        ret

Reply via email to