This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Optimisation puzzle
- From: "Nate Denmark" <ndenmark at googlemail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 13 Mar 2007 14:51:11 +0000
- Subject: Optimisation puzzle
Hello all,
I have a question about the optimiser in GCC. Take this bit of code:
for(x = 0; x != 10; x++)
puts("Hello");
When compiled with full optimisations (-O3, -fexpensive-optimizations
etc.) it generates the following loop in assembly:
.L2:
incl %ebx
movl $.LC0, (%esp)
call puts
cmpl $10, %ebx
jne .L2
.LC0 points to the "Hello" string. I'm wondering why GCC puts that
'movl' line inside the loop, so that it's executed each time, when it
could go before the loop? As I understand it, 'puts' shouldn't do
anything to the stack, and the return value is passed back in eax, so
I'm not sure why it's doing the 'movl' each time. If I force loop
unrolling the same thing happens -- the 'movl' each iteration.
That said, I don't know much about compiler design so I'm probably
missing something here!
Thanks,
Nate