public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Missing if optimization
@ 2004-09-09  5:24 Uros Bizjak
  2004-09-09  5:49 ` Andrew Pinski
  2004-09-09 22:15 ` James E Wilson
  0 siblings, 2 replies; 3+ messages in thread
From: Uros Bizjak @ 2004-09-09  5:24 UTC (permalink / raw)
  To: gcc

Hello!

The if optimization, which produces quite optimized code for integer 
case is missing for floatin-point compares.

This code:

int test(int a) {
        return a == 0 ? 0 : (a > 0 ? 1 : -1);
}

produces (with -O2  -fomit-frame-pointer):

test:
        xorl    %eax, %eax
        cmpl    $0, 4(%esp)
        je      .L4
        setg    %al
        movzbl  %al, %eax
        leal    -1(%eax,%eax), %eax
.L4:
        ret

However, fp compares:

double testf(double a) {
        return a == 0.0 ? 0.0 : (a > 0.0 ? 1.0 : -1.0);
}

Produce somehow unoptimized code (-O2 -ffast-math -fomit-frame-pointer):

testf:
        fldl    4(%esp)
        ftst
        fnstsw  %ax
        sahf
        jne     .L9
        fstp    %st(0)
        fldz
        ret
        .p2align 4,,7
.L9:
        ftst
        fnstsw  %ax
        fstp    %st(0)
        sahf
        ja      .L12
        flds    .LC1
        ret
        .p2align 4,,7
.L12:
        fld1
        .p2align 4,,2
        ret

There is no need for second fp compare, two conditional jumps after 
first fp compare would be enough.

Uros.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Missing if optimization
  2004-09-09  5:24 Missing if optimization Uros Bizjak
@ 2004-09-09  5:49 ` Andrew Pinski
  2004-09-09 22:15 ` James E Wilson
  1 sibling, 0 replies; 3+ messages in thread
From: Andrew Pinski @ 2004-09-09  5:49 UTC (permalink / raw)
  To: Uros Bizjak; +Cc: gcc


On Sep 8, 2004, at 10:19 PM, Uros Bizjak wrote:

> Hello!
>
> The if optimization, which produces quite optimized code for integer 
> case is missing for floatin-point compares.
>
> This code:
>
> int test(int a) {
>        return a == 0 ? 0 : (a > 0 ? 1 : -1);
> }
>
> produces (with -O2  -fomit-frame-pointer):
>
> test:
>        xorl    %eax, %eax
>        cmpl    $0, 4(%esp)
>        je      .L4
>        setg    %al
>        movzbl  %al, %eax
>        leal    -1(%eax,%eax), %eax
> .L4:
>        ret
>
> However, fp compares:
>
> double testf(double a) {
>        return a == 0.0 ? 0.0 : (a > 0.0 ? 1.0 : -1.0);
> }

On PPC we get only one compare:
(This is with a month old compiler):
_testf:
         lis r2,ha16(LC0)
         la r2,lo16(LC0)(r2)
         lfd f0,0(r2)
         fcmpu cr7,f1,f0 <-- the one and only compare
         beq- cr7,L4
         lis r2,ha16(LC1)
         la r2,lo16(LC1)(r2)
         lfd f0,0(r2)
         bgt- cr7,L8
L4:
         fmr f1,f0
         blr
L8:
         lis r2,ha16(LC2)
         la r2,lo16(LC2)(r2)
         lfd f0,0(r2)
         fmr f1,f0
         blr

Thanks,
Andrew Pinski

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Missing if optimization
  2004-09-09  5:24 Missing if optimization Uros Bizjak
  2004-09-09  5:49 ` Andrew Pinski
@ 2004-09-09 22:15 ` James E Wilson
  1 sibling, 0 replies; 3+ messages in thread
From: James E Wilson @ 2004-09-09 22:15 UTC (permalink / raw)
  To: Uros Bizjak; +Cc: gcc

Uros Bizjak wrote:
> The if optimization, which produces quite optimized code for integer 
> case is missing for floatin-point compares.

This is very target dependent, and you didn't mention the target.  From 
the assembly code, it appears to be x86.

Some targets have comparisons that can be used to optimize this 
testcase.  Some don't.

For those that do, whether it gets optimized depends on how the backend 
represents compares, which is done differently for different targets.

As for the optimization itself, there should be no problem there, as has 
already been pointed out we get this right for powerpc.

You might consider filing a bug report for the missed optimization if 
you haven't already.
-- 
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-09-09 22:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-09-09  5:24 Missing if optimization Uros Bizjak
2004-09-09  5:49 ` Andrew Pinski
2004-09-09 22:15 ` James E Wilson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).