[PATCH, i386]: Fix PR target/17390

Uros Bizjak ubizjak@gmail.com
Wed Oct 19 12:18:00 GMT 2005


Hello!

Attached patch fixes enhancement PR target/17390: missing floating
point compare optimization.

As explained in PR by Jan Hubicka, non-fcomi i387 FP compare sequences
are expanded after reload, and they miss cse1 pass that would CSE some
redundant compares. Attached patch addresses this deficiency in
machine-reorg pass.

During machine-reorg pass, we find appropriate sequence and eliminate
redundant compares (and ax testing instruction, if flags are not
clobbered up to ax testing insn) in successor BBs.

The testcase from PR (this is in fact __sgn() from libm):

double test(double __x) {
  return __x == 0.0 ? 0.0 : (__x > 0.0 ? 1.0 : -1.0);

compiles with (-O2 -ffast-math -march=pentium -mtune=pentium) to:

test:
        fldl    4(%esp)
        ftst
        fnstsw  %ax
        testb   $64, %ah
        jne     .L10
        fstp    %st(0)              <<< deleted compare/fnstsw combination
        testb   $69, %ah
        jne     .L5
        fld1
        ret
        .p2align 4,,7
.L10:
        fstp    %st(0)
        fldz
        ret
.L5:
        flds    .LC2
        ret

This code is further optimized by adding -mtune=i686, where sahf is
used in place of testb. sahf is then CSE'd and following code is
produced:

test:
        fldl    4(%esp)
        ftst
        fnstsw  %ax
        sahf
        je      .L10
        fstp    %st(0)           <<< deleted comparison/fnstsw and sahf
        jbe     .L5
        fld1
        ret
        .p2align 4,,7
.L10:
        fstp    %st(0)
        fldz
        .p2align 4,,4
        ret
.L5:
        flds    .LC2
        .p2align 4,,2
        ret

Patch was bootstrapped on i686-pc-linux-gnu, regtested for c and c++.
Additionally, povray-3.6.1 was built with -march=i586 and -mtune=i686
or -mtune=i586. Benchmark test was ran and result were checked for
correctnes. Patched gcc removes some 30 - 35 redundant compares during
povray compilation.

:ADDPATCH target/i386:

2005-10-19  Uros Bizjak  <uros@kss-loka.si>

	PR target/17390
	* config/i386/i386.c (ix86_cse_i387_compares): New function.
	(ix86_reorg): Call ix86_cse_i387_compares for
	TARGET_80387 && !TARGET_CMOVE when !flag_trapping_math and
	optimize > 1.

	TARGET_80387 && !TARGET_CMOVE targets.

testsuite:

	PR target/17390
	* gcc.target/i386/pr17390-1.c: New test.
	* gcc.target/i386/pr17390-2.c: New test.


OK for 4.2 ? Perhaps also ok for 4.1 when it opens?

Uros
-------------- next part --------------
/* { dg-do compile } */
/* { dg-options "-march=i586 -mtune=i586 -O2 -ffast-math" } */
/* { dg-require-effective-target ilp32 } */

double test(double __x) {
  return __x == 0.0 ? 0.0 : (__x > 0.0 ? 1.0 : -1.0);
}

/* { dg-final { scan-assembler-times "fnstsw" 1 } } */
/* { dg-final { scan-assembler-times "testb" 2 } } */

-------------- next part --------------
/* { dg-do compile } */
/* { dg-options "-march=i586 -mtune=i686 -O2 -ffast-math" } */
/* { dg-require-effective-target ilp32 } */

double test(double __x) {
  return __x == 0.0 ? 0.0 : (__x > 0.0 ? 1.0 : -1.0);
}

/* { dg-final { scan-assembler-times "fnstsw" 1 } } */
/* { dg-final { scan-assembler-times "sahf" 1 } } */

-------------- next part --------------
A non-text attachment was scrubbed...
Name: pr17390.diff
Type: application/octet-stream
Size: 7235 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/gcc-patches/attachments/20051019/40c6dafe/attachment.obj>


More information about the Gcc-patches mailing list