gcc optimizations

Matt Hiller hiller@redhat.com
Mon Jun 18 15:09:00 GMT 2001


Hello,

	To get a better idea of the exact methods that gcc is using to
optimize your code, I would recommend running gcc with -dap. This will
produce a number of debugging dumps each of which describe the internal
representation of your program that gcc has arrived at after each pass of
the compiler. It will also annotate your .s file with summaries of where
each machine instruction came from.

	Please consult the gcc manual for more details on this. In
particular, you'll want to familiarize yourself with gcc's Register
Transfer Language (RTL); the debugging dumps I describe are in RTL's
textual form.

-- 

Matt Hiller
GCC Engineer, Red Hat, Inc., Sunnyvale office
hiller@redhat.com

On Mon, 18 Jun 2001, Fuat Oezdemir wrote:

> 
> Hello,
> 
> I am an beginner with optimizing techniques from gcc. I have an example
> code  compiled with the gcc (v. egcs-2.91.57) with options 
> 
> -S 	generate assembler code
> and alternately
> -O0	no optimizing
> -O3	full optimizing.
> 
> My problem is, that I don't know which of the optimization options are the
> reason for the optimization. I tried the option -fverbose-asm of gcc. With
> this option the compiler includes, which optimization options are enabled,
> if I take the -O3 option. I tried it again with the enabled options
> without the -O3 option but in this case the compiler doesn't optimize.
> Is there anybody, who is be able to give an answer or an pointer to this
> problem.
> 
> Thank you very much in advance.
> 
> Fuat Özdemir
> 
> 
> The source code looks like this :
> 
> #include <stdio.h>
> 
> int main () {
> 
>  int a=0, b=0, zero=120;
> 
>    if(zero >= 50) {
> 
>      a=10;
> 
>    } else if (zero >= 100) { 		// unreachable !!
> 
>      b=20;
> 
>    }
> 
>    printf("a = %d   b = %d\n", a, b);
> 
>   return 1;
> }
> 
> 
> The else-if part is unreachable because zero is set to 120 and the first
> if-expression is always true.
> 
> The result of gcc with -O3 option is the following :
> 
> .file "test.c"
> gcc2_compiled.:
> ___gnu_compiled_c:
>    .def  ___main; .scl  2; .type 32;   .endef
> .text
> LC0:
>    .ascii "a = %d   b = %d\12\0"
>    .align 4
> .globl _main
>    .def  _main;   .scl  2; .type 32;   .endef
> _main:
>    pushl %ebp
>    movl %esp,%ebp
>    call ___main
>    pushl $0
>    pushl $10
>    pushl $LC0
>    call _printf
>    movl $1,%eax
>    movl %ebp,%esp
>    popl %ebp
>    ret
>    .def  _printf; .scl  2; .type 32;   .endef
> 
> 
> I am not sure about approach of every assembler command, but it looks like
> so, that the content of the main-function is optimized to one
> printf-function and the values of the variable 'a' to 10 and 'b' to 0. 
> 
>  
> --------------------------------------------------------------
> 
> E-Mail : fuat@cs.tu-berlin.de
>           
> 



More information about the Gcc-help mailing list