GCC Unrolling Simple Loop Generates Rubbish Code?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Dear GCC Hackers,

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 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? :-)

The logic here seems confusing to me, but I don't know much about GCC's 
optimizing algorithms. Could someone kindly explain it to me?

I'm using GCC 3.2.3:

Reading specs from /usr/lib/gcc-lib/i386-asianux-linux/3.2.3/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man 
--infodir=/usr/share/info --enable-shared --enable-threads=posix 
--disable-checking --with-system-zlib --enable-__cxa_atexit 
--host=i386-asianux-linux
Thread model: posix
gcc version 3.2.3 20030502

I've asked other people to test this with their different GCC version, and all 
their GCC versions generate this kind of  code, with maybe different numbers 
than 25. Even GCC 4, it was said. I don't have GCC 4 currently.

Yesterday I asked this in #gcc on IRC, they suggested that this is a bug. But 
I think I should ask it here before sending a stupid fake bug report :-)

So what will you say?

Best Regards,
Lei Ming

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux