public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* GCC Unrolling Simple Loop Generates Rubbish Code?
@ 2005-07-19  2:51 Lei Ming
  2005-07-19  4:00 ` Ian Lance Taylor
  2005-07-19 16:14 ` jlh
  0 siblings, 2 replies; 3+ messages in thread
From: Lei Ming @ 2005-07-19  2:51 UTC (permalink / raw)
  To: gcc-help

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-07-19 16:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-19  2:51 GCC Unrolling Simple Loop Generates Rubbish Code? Lei Ming
2005-07-19  4:00 ` Ian Lance Taylor
2005-07-19 16:14 ` jlh

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).