This is the mail archive of the gcc@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]

Strange optimization results


Using gcc version 2.96 19991001 (experimental) on i486-pc-linux-gnu :

On the following small piece of code, I was astonished that the
evaluation of the function at compile time depends on the target
processor selected.

Here is the source code:

int f1 (double a)
{
  if (a > 0.0) return 0;
  else return 1;
}

int f2()
{
  return f1(1.0);
}

When I compale with -march=pentiumpro, the compiler optimizes f2 to a
constant function returning 0. For lower target processors, f2 will
contain an inlined version of f1. Why is GCC unable to optimize f2
independently of the taget processor ?

Here is the resulting assembler code:
Using gcc -O3 -ffast-math -S test3.c -march=pentium :
 .file "test3.c"
 .version "01.01"
gcc2_compiled.:
.text
 .align 16
.globl f1
 .type  f1,@function
f1:
 pushl %ebp
 movl %esp, %ebp
 fldz
 fldl 8(%ebp)
 fcompp
 fnstsw %ax
 testb $1, %ah
 je .L3
 movl $1, %eax
 jmp .L11
 .p2align 4,,7
.L3:
 movl $0, %eax
.L11:
 movl %ebp, %esp
 popl %ebp
 ret
.Lfe1:
 .size  f1,.Lfe1-f1
 .align 16
.globl f2
 .type  f2,@function
f2:
 fld1
 fldz
 fxch %st(1)
 pushl %ebp
 fcompp
 fnstsw %ax
 movl %esp, %ebp
 testb $1, %ah
 movl %ebp, %esp
 setne %al
 andl $255, %eax
 popl %ebp
 ret
.Lfe2:
 .size  f2,.Lfe2-f2
 .ident "GCC: (GNU) 2.96 19991001 (experimental)"

Using :
gcc -O3 -ffast-math -S test3.c -march=pentiumpro

 .file "test3.c"
 .version "01.01"
gcc2_compiled.:
.text
 .align 16
.globl f1
 .type  f1,@function
f1:
 pushl %ebp
 movl %esp, %ebp
 fldz
 fldl 8(%ebp)
 fcomip %st(1), %st
 fstp %st(0)
 ja .L3
 movl $1, %eax
 jmp .L11
 .p2align 4,,7
.L3:
 movl $0, %eax
.L11:
 movl %ebp, %esp
 popl %ebp
 ret
.Lfe1:
 .size  f1,.Lfe1-f1
 .align 16
.globl f2
 .type  f2,@function
f2:
 pushl %ebp
 movl %esp, %ebp
 movl %ebp, %esp
 popl %ebp
 movl $0, %eax
 ret
.Lfe2:
 .size  f2,.Lfe2-f2
 .ident "GCC: (GNU) 2.96 19991001 (experimental)"





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]