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

-O2 optimization error


The followin snippet of code generates incorrect
code using -O2 but correct when using -O1

Using builtin specs.
gcc version egcs-2.93.08 19990219 (gcc2 ss-980929 experimental)

ccmips -EL -mcpu=r4000 -mips3 -non_shared -G 0 -nostdinc -O2 -Wall
-DCPU=R4000 -DMIPSEL -c -o support.o support.c

-------------------------------------------
static unsigned short msign=0x7fff, mexp =0x7ff0  ;
static short prep1=54, gap=4, bias=1023           ;
static double novf=1.7E308, nunf=3.0E-308,zero=0.0;

double logb(x)
double x;
{

#if (_BYTE_ORDER == _LITTLE_ENDIAN)
        short *px=(short *) &x+3, k;
#else	/* little_endian */
        short *px=(short *) &x, k;
#endif	/* little_endian */

        if( (k= *px & mexp ) != mexp )
            if ( k != 0 )
                return ( (k>>gap) - bias );
            else if( x != zero)
                return ( -1022.0 );
            else
                return(-(1.0/zero));
        else if(x != x)
            return(x);
        else
            {*px &= msign; return(x);}
}
--------------------------------------------------------
Notice the a1 register is pulled of the stack immediadetly
but the argument fp12 isn't pushed onto the stack till 5 instructions
later.  In MIPS land the user does not push the args onto the stack
(unless there is too many for the available registers)

** Also the mexp value is being loaded into v0 and v1 (instructions
   1a8-1b4), when the same register could be used for both the and
   and the comparison.
   


0000000000001a4 <logb>:
 1a4:	97a50006 	lhu	$a1,6($sp)                     -----> pulled
off stack here
 1a8:	3c020000 	lui	$v0,0x0
 1ac:	94420002 	lhu	$v0,2($v0)
 1b0:	3c030000 	lui	$v1,0x0
 1b4:	94630002 	lhu	$v1,2($v1)
 1b8:	f7ac0000 	sdc1	$f12,0($sp)                    -----> but
not pushed till here
 1bc:	00a21024 	and	$v0,$a1,$v0
 1c0:	00021400 	sll	$v0,$v0,0x10
 1c4:	00022403 	sra	$a0,$v0,0x10
 1c8:	1083001d 	beq	$a0,$v1,240 <logb+0x9c>
 1cc:	46206006 	mov.d	$f0,$f12
 1d0:	1080000b 	beqz	$a0,200 <logb+0x5c>
 1d4:	00000000 	nop
 1d8:	3c020000 	lui	$v0,0x0
 1dc:	84420006 	lh	$v0,6($v0)
 1e0:	3c030000 	lui	$v1,0x0
 1e4:	84630008 	lh	$v1,8($v1)
 1e8:	00441007 	srav	$v0,$a0,$v0
 1ec:	00431023 	subu	$v0,$v0,$v1
 1f0:	44820000 	mtc1	$v0,$f0
 1f4:	00000000 	nop
 1f8:	03e00008 	jr	$ra
 1fc:	46800021 	cvt.d.w	$f0,$f0
 200:	3c010000 	lui	$at,0x0
 204:	d4210020 	ldc1	$f1,32($at)
 208:	46216032 	c.eq.d	$f12,$f1
 20c:	00000000 	nop
 210:	45010004 	bc1t	224 <logb+0x80>
 214:	00000000 	nop
 218:	3c010000 	lui	$at,0x0
 21c:	03e00008 	jr	$ra
 220:	d4200000 	ldc1	$f0,0($at)
 224:	3401ffc0 	li	$at,0xffc0
 228:	00010bbc 	dsll32	$at,$at,0xe
 22c:	44a10000 	dmtc1	$at,$f0
 230:	00000000 	nop
 234:	46210003 	div.d	$f0,$f0,$f1
 238:	03e00008 	jr	$ra
 23c:	46200007 	neg.d	$f0,$f0
 240:	46200032 	c.eq.d	$f0,$f0
 244:	00000000 	nop
 248:	45000006 	bc1f	264 <logb+0xc0>
 24c:	00000000 	nop
 250:	3c020000 	lui	$v0,0x0
 254:	94420000 	lhu	$v0,0($v0)
 258:	00a21024 	and	$v0,$a1,$v0
 25c:	03e00008 	jr	$ra
 260:	a7a20006 	sh	$v0,6($sp)
 264:	03e00008 	jr	$ra
 268:	00000000 	nop




_______________________________________________________________
Ken Faiczak                        PixStream Incorporated
ken@pixstream.com                  180 Columbia St. West
(519) 884-4196 x2234               Waterloo Ontario, Canada
fax:(519) 884-5949                 N2L 3L3


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