I have a C code:
void f();
void i(unsigned n) {
  while (n-- > 0) {
    f();
  }
}

Which when compiled with -O3 on i586 produces the assembly:
i:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %esi
        movl    8(%ebp), %esi
        pushl   %ebx
        testl   %esi, %esi
        je      .L5
        xorl    %ebx, %ebx
        .p2align 4,,7
.L4:
        addl    $1, %ebx
        call    f
        cmpl    %esi, %ebx
        jne     .L4
.L5:
        popl    %ebx
        popl    %esi
        popl    %ebp
        ret

And obviously shorter assembly with one less instruction in the loop is:
i:
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %esi
        movl    8(%ebp), %esi
        testl   %esi, %esi
        je      .L5
        .p2align 4,,7
.L4:
        call    f
        dec     %esi
        jl      .L4
.L5:
        popl    %esi
        popl    %ebp
        ret

This is a very short and basic loop occurring in programs many times.
That's why this should be optimized very well.


-- 
           Summary: Simple loop isn't optimized well
           Product: gcc
           Version: 4.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: yuri at tsoft dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=35776

Reply via email to