Optimisation puzzle
Nate Denmark
ndenmark@googlemail.com
Tue Mar 13 15:21:00 GMT 2007
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
More information about the Gcc-help
mailing list