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

            Bug ID: 105618
           Summary: Missed loop body simplification by -O3 (trunk v.s.
                    10.3)
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: shaohua.li at inf dot ethz.ch
  Target Milestone: ---

For the following code, gcc truck -O3 fails to optimize away the loop body,
while gcc10.3 can. You can see from the assembly code that gcc10.3 figured out
that the variable c would be a constant after the loop while gcc-trunk didn't.

$cat a.c
static int b=4;
int c;
int main() {
  int e[5] = {1,1,1,1,1};
  for (; b >= 0; b--) {
    c = e[b];
  }
  return 0;
}
$
$gcc-trunk -O3 -S -o trunk.s a.c
$gcc-10-3  -O3 -S -o 10-3.s a.c
$wc trunk.s 10-3.s
  66  147 1005 trunk.s
  52  106  698 10-3.s
 118  253 1703 total
$
$cat 10-3.s
main:
.LFB0:
        .cfi_startproc
        endbr64
        movl    b(%rip), %eax
        testl   %eax, %eax
        js      .L2
        movl    $1, c(%rip)
        movl    $-1, b(%rip)
.L2:
        xorl    %eax, %eax
        ret
$
$cat trunk.s
main:
.LFB0:
        .cfi_startproc
        movdqa  .LC0(%rip), %xmm0
        movl    b(%rip), %eax
        movl    $1, -24(%rsp)
        movaps  %xmm0, -40(%rsp)
        testl   %eax, %eax
        js      .L2
        movslq  %eax, %rdx
        leal    -1(%rax), %ecx
        movl    -40(%rsp,%rdx,4), %edx
        je      .L3
        leal    -2(%rax), %edx
        cmpl    $1, %eax
        je      .L10
        leal    -3(%rax), %ecx
        cmpl    $2, %eax
        je      .L6
        leal    -4(%rax), %edx
        cmpl    $3, %eax
        je      .L10
.L6:
        movslq  %edx, %rdx
        movl    -40(%rsp,%rdx,4), %edx
.L3:
        movl    %edx, c(%rip)
        movl    $-1, b(%rip)
.L2:
        xorl    %eax, %eax
        ret
.L10:
        movslq  %ecx, %rcx
        movl    -40(%rsp,%rcx,4), %edx
        jmp     .L3
$

Reply via email to