GCC Unrolling Simple Loop Generates Rubbish Code?

Ian Lance Taylor ian@airs.com
Tue Jul 19 04:00:00 GMT 2005


Lei Ming <ming.lei@oracle.com> writes:

> This simple code:
> 
> int
> main (void)
> {
>   int s = 0;
>   int i;
>   for (i = 0; i < 100; i++)
>     s += i;
>   return 0;
> }
> 
> After compiled with "-O3 -funroll-all-loops",  will generate this piece of 
> assembly code for the loop:
> 
>         movl    $99, %eax
> .L6:
>         subl    $25, %eax        <==
>         jns     .L6
>         xorl    %eax, %eax
>         leave
>         ret

I tried compiling with current gcc mainline with -O3
-funroll-all-loops.  I got this:

   0:   55                      push   %ebp
   1:   31 c0                   xor    %eax,%eax
   3:   89 e5                   mov    %esp,%ebp
   5:   83 ec 08                sub    $0x8,%esp
   8:   83 e4 f0                and    $0xfffffff0,%esp
   b:   83 ec 10                sub    $0x10,%esp
   e:   c9                      leave
   f:   c3                      ret

So I think things are better now.

I also see the behaviour you report with gcc 3.2.3.  It does seem
pretty incomprehensible.  Since it seems to be fixed, I haven't
bothered to figure out why it used to behave like that.

Ian



More information about the Gcc-help mailing list