TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Jasen Betts
from: Tulipant Gergely
date: 2003-06-03 18:14:38
subject: gcc -O

Jasen Betts -> Tulipant Gergely >>gcc -O<< {at} 27 May 03  21:58:40

 JB> FCBBX: Citizen David advise status of preparation of the device

  Whazzat?

 TG>> Can y'all tell me how to figure out exactly what optimizations the
 TG>> different -O[1-3] options switch on?

  Use the source, Luke? :\

 TG>> I used gcc-2.96-113.i386.rpm with the below l'art pour l'art code (O.c).

 TG>> long add (long a, void *ptr, int type) {
 TG>> if (type == 0)
 TG>> return a + *(int *) ptr;
 TG>> else
 TG>> return a + *(long *) ptr;
 TG>> }

 JB> first off I'll point out that in that environment  int  and  long  are the
 JB> same type so the  if(type==0) is meaningless,

  Yeah. That's the whole point of the experiment: testing code elimination.

 JB> O2 and O3 manage to merge the two branches of the  if into a single block
 JB> but for some reason test the the value of type but ignore the result.

 JB> it looks like a bug to me
 JB> ~~~~~~~~~~~~~~~~~~~~~~~~~

  Yeah. I've read warnings about the RedHat-tampered gcc - which they call
2.96 - while compiling different sources. :( Could y'all test with some
other version?

 TG>> It does make sense helping the compiler as Ulrich Drepper recommends:

 TG>> if (sizeof(int) == sizeof(long) || type == 0)

 TG>> This way -O2 makes as good a translation as -O1. :)

 JB> but O0 is worsened....

  That's normal. -O0 produces horrible code (for the sake of flawless
tracing?), still it generates some advanced code, e.g. integer division by
constant is compiled into multiplication.
  I don't care much about -O0.

 JB> It may be better to do a #if using something from values.h
 JB> but you may loose POSIX compatibility that way.

  Why would it be better?


  Here's another oddity:

#include 
int main(void) {
  puts("foo");
  return 0;
}

gcc -O3 -S -o - test.c 
    .file	"test.c"
    .version	"01.01"
gcc2_compiled.:
    .section	.rodata.str1.1,"aMS",{at}progbits,1
.LC0:
    .string	"foo"
.text
    .align 4
.globl main
    .type	 main,{at}function
main:
    pushl	%ebp
    movl	%esp, %ebp
    subl	$20, %esp
    pushl	$.LC0
    call	puts
    addl	$16, %esp
    xorl	%eax, %eax
    leave
    ret
.Lfe1:
    .size	 main,.Lfe1-main
    .ident	"GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.3 2.96-113)"

  That "addl $16, %esp" is pretty useless before
"leave", and -O2 does eliminate it. Again, more is less.

--- SkyReader/Linux v1.01a-7
* Origin: COMM Port OS/2 juge.com 204.89.247.1 (281) 980-9671 (1:106/2000)
SEEN-BY: 633/267 270
@PATH: 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.