GCC Unrolling Simple Loop Generates Rubbish Code?

jlh jlh@gmx.ch
Tue Jul 19 16:14:00 GMT 2005

> I wonder what's the purpose of "subl $25, %eax"? If the compiler knows that
> the whole loop is useless, since we don't really use the result, why not just
> eliminate this loop and simply return zero? If it choose to execute the loop,
> why it is the number 25 instead of other number? Why not 42? :-)

GCC hasn't removed empty loops in the past and I think this topic has had long
discussions already.  It's probably not worth starting a new one, on whether
it shall or shall not do it.  But reading Ian's reply, it seems this is now
different on mainline.  Good thing.

GCC is able to do optimization of the style:
    for (i = 0; i < 100; i++) do_something();
    for (i = 0; i < 100; i += 2) { do_something(); do_something(); }
to reduce the overhead of the loop.  (There's a name for it, but I don't
remember.)  In this example, it always does 2 loop iterations at a time,
and only needs to 'loop' 50 times.  In your example, it makes 25 steps at
a time and therefore increments the iterator by 25 on each cycle.  42 won't
trivially work in the same way, because it is not a divisor of 100.

> I'm using GCC 3.2.3:

That's an old version.  I suggest you upgrade to 3.4.4 (or to 4.0.x if you
want) unless you have reasons not to do that.  I'm not even sure whether bug
reports for 3.2.x version and below are welcome anyway, given that's pretty old.

This is the output of 3.4.4 on my i686-pc-linux-gnu (-O3 -funroll-all-loops)

main:   pushl   %ebp
        xorl    %eax, %eax
        movl    %esp, %ebp
        subl    $8, %esp
        andl    $-16, %esp
        subl    $16, %esp
        .p2align 4,,7
.L5:    addl    $10, %eax
        cmpl    $99, %eax
        jle     .L5
        xorl    %eax, %eax

It takes steps of 10 here.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 256 bytes
Desc: OpenPGP digital signature
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20050719/22b5be7d/attachment-0001.sig>

More information about the Gcc-help mailing list