gcc optimization??

Programming, for all ages and all languages.
Post Reply
NOTNULL

gcc optimization??

Post by NOTNULL »

Hello all,
I tried to write a simple program in C (prints "hello world") and disassembled the same using -S option in GCC. The output of the file seems to be so huge than just pushing the variables into the stack, calling the printf function and clearing the stack. Can any one explain me, why is this so? I have seen the output of some other version which seems to be so simple.

And also some other query. The program calls the "printf" function. But, the output code doesn't seems to have the printf function. ??? Does the printf function is called during the run-time? (some function related to OS). I am using the GCC version 3.2.

Any help greately appreciated.

Code: Select all

#include<stdio.h>

int main()
{
    printf("Hello World");
    return 0;
}

Code: Select all

 1         .file   "test.c"
 2         .section        .rodata
 3 .LC0:
 4         .string "Hello world"
 5         .text
 6         .align 2
 7 .globl main
 8         .type   main,@function
 9 main:
10         pushl   %ebp
11         movl    %esp, %ebp
12         subl    $8, %esp
13         andl    $-16, %esp
14         movl    $0, %eax
15         subl    %eax, %esp
16         subl    $12, %esp
17         pushl   $.LC0
18         call    printf
19         addl    $16, %esp
20         movl    $0, %eax
21         leave
22         ret
23 .Lfe1:
24         .size   main,.Lfe1-main
25         .ident  "GCC: (GNU) 3.2 20020903 (Red Hat Linux 8.0 3.2-7)"
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:gcc optimization??

Post by Candy »

Try compiling with -O1 or -O2, this removes most stupid code.

For the printf thing, it's in a library, probably (g)libc.
NOTNULL

Re:gcc optimization??

Post by NOTNULL »

gcc is capable of producing optimized code. Then, why it doesn't produce the optimized code directly without giving the -O1 and -O2 option? Why those damn instructions are produced in the unoptimized code?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:gcc optimization??

Post by Candy »

NOTNULL wrote: gcc is capable of producing optimized code. Then, why it doesn't produce the optimized code directly without giving the -O1 and -O2 option? Why those damn instructions are produced in the unoptimized code?
probably since just about everybody optimizes anyway, and this way each set of instructions has a direct relationship to a line in the input (line is not a strict line of code). Plus, it makes you immune to bugs in their optimizer :)
creichen

Re:gcc optimization??

Post by creichen »

Hi,
NOTNULL wrote: gcc is capable of producing optimized code. Then, why it doesn't produce the optimized code directly without giving the -O1 and -O2 option? Why those damn instructions are produced in the unoptimized code?

One additional reason for this is that all optimisations take a certain amount of time to be performed. In particular, SSA based optimisations require the AST to be mapped to an SSA representation first, which is known to be quite expensive. Also, recent analyses (albeit on the JIKES JIT compiler) seem to indicate that, for various reasons (mostly related to memory vs. CPU speed) modern CPUs hardly benefit from any instruction-level optimisations; the only practially relevant optimisations appear to be the ones that reduce memory access (particularly compile-time dynamic dispatch resolution).

As a developer, you might not always desire the full compile time associated with fully optimised compilations, particularly since languages like C do fairly little checking at compile time and thus require considerable run-time testing (i.e., frequent re-compiles until your program does something close to what it's supposed to do).

-- Christoph
Post Reply