Useless assembly
Sam Lauber
sam124@operamail.com
Sun Nov 28 21:40:00 GMT 2004
When I run GCC 3.4.3 on this code:
#include <stdio.h>
int main(void)
{
printf("Hello World!\n");
return 0;
}
it generates the assembly code (this is i686 assembly)
.file "test.c"
.section .rodata
.LC0:
.string "Hello World!\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $8, %esp
andl $-16, %esp
movl $0, %eax
addl $15, %eax
addl $15, %eax
shrl $4, %eax
sall $4, %eax
subl %eax, %esp
movl $.LC0, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.4.3"
By hand-optimizing the assembly, I made this:
.LC0:
.string "Hello World!\n"
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
movl $.LC0, (%esp)
call printf
leave
It worked the exact same. Clearly a lot was unnessecary. Removing "call printf" or "movl $.LC0, (%esp)" caused it not to print "Hello World!". Removing ".globl main" caused the linker to fail with an undefined reference to main. Removing anything else caused it to print Hello World and then generate a segmentation fault. Optimizing it at -O3 (which I'm sure includes SSA, DCE, and CCP) just made the compilation take longer and generate more assembly then the first time. If someone could just automate removing that code in cc1, it would probably mean a lot of optimization. Also, when I just compiled a "return 0;", I would expect that to just generate main and the instruction "ret" (or is it "retn?"). Instead, it generated a lot of extra instructions, then finally "ret". Clearly this is unsatisfactory. It makes cc1, as, and ld slower because they have to compile, assemble, and link more instructions. Each extra instruction makes a compilation time penalty of -3 and a runtime penalty of -1. At least that could be put onto DCE. If extra code gets generated by cc1, as and ld can't optimize it.
Samuel Lauber
--
_____________________________________________________________
Web-based SMS services available at http://www.operamail.com.
From your mailbox to local or overseas cell phones.
Powered by Outblaze
More information about the Gcc
mailing list