Bug 35776 - Simple loop isn't optimized well
Summary: Simple loop isn't optimized well
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.2.1
: P3 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
Depends on:
Blocks:
 
Reported: 2008-03-31 17:03 UTC by Yuri
Modified: 2008-04-01 11:05 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Yuri 2008-03-31 17:03:25 UTC
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.
Comment 1 Richard Biener 2008-04-01 11:05:47 UTC
4.3 produces

.L5:
        call    f
        subl    $1, %ebx
        jne     .L5

thus fixed.