This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
| Other format: | [Raw text] | |
I have a question about the optimiser in GCC. Take this bit of code:From man:puts I see that it is declared "int puts(const char *)". This means that puts does not promise to leave its argument unchanged. Therefore the caller must push the argument anew before each call. If it had been declared "int puts(const char * const)" instead, the push should be moved outside the loop. Unfortunately this does not seem to work. I tried with the following program:
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.
and built it with "gcc -std=c99 -Os -Wall -Wextra -Werror -S":
.L2:
subl $12, %esp
incl %ebx
pushl $77
call q
addl $16, %esp
cmpl $10, %ebx
jne .L2and q.ads: procedure Q(N : in Natural);
and built with "gnatgcc -Os -Wall -Wextra -Wextra -Werror -S f.adb":
.L5:
pushl $77
.LCFI3:
call _ada_q
popl %eax
decl %ebx
jns .L5| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |