From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,ae6d7754224e7cfb X-Google-Attributes: gidfee84,public X-Google-Thread: 1164f8,ae6d7754224e7cfb X-Google-Attributes: gid1164f8,public From: Gerald Feldman Subject: Re: horrible "cc -O" bug (DU 3.2c) Date: 1997/05/10 Message-ID: <3374892D.5E93@highway1.com>#1/1 X-Deja-AN: 240585666 References: <5kvqsu$18d$1@news.iastate.edu> Organization: nntp Needham Reply-To: gaf@highway1.com Newsgroups: comp.unix.osf.osf1,comp.sys.dec John Hascall wrote: > > Mark Bartelt wrote: > }Someone just called my attention to a really disturbing > }bug in DEC's C compiler. It seems that if a program is > }compiled with "-O", then evaluation of "expr1 && expr2" > }is botched: Even when expr1 evaluates to 0, expr2 gets > }evaluated anyway! > } > }The implications of this bug are rather staggering; for > }example, what if expr2 has side-effects? Gack. > } > }The bug seems to appear only when "-O" is used. Here's > }a short example program. It generates a floating-point > }exception when compiled with "-O". (It shouldn't, since > }the expression to the right of the "&&" should never be > }evaluated.) > > Gack! Here's the code w/o and with -O: > > # 6 if ( flag && 1/x > y ) x = 0; > > ldl $1, 16($sp) ldl $1, 48($sp) > beq $1, $32 ldit $f10, 1.0000000000000000e+00 > ldit $f10, 1.0000000000000000e+00 divt $f10, $f17, $f11 > ldt $f11, 24($sp) ldt $f12, 64($sp) > divt $f10, $f11, $f12 cmptlt $f12, $f11, $f13 > ldt $f13, 32($sp) cpys $f17, $f17, $f14 > cmptlt $f13, $f12, $f14 fcmovne $f13, $f31, $f14 > fbeq $f14, $32 stq $1, 32($sp) > stt $f31, 24($sp) ldt $f15, 32($sp) > $32: cpys $f14, $f14, $f0 > fcmoveq $f15, $f17, $f0 > > Obviously they are trying to use conditional > moves to avoid branches - but this is really > wrong... Interesting. BTW: The cc command in V3.2c is the old MIPS based compiler that has been retired. The cc -migrate is the newer DECC compiler. I am not sure thnat this is really a bug. As long as the evaluation does not cause any side effects. Remember that on the Alpha chip, 2 or 4 instaructions can be executed at the same time. (eg. dual or quad issue). even with the branch code, the float instructions will have been prefetched, and may be executed before the branch completes. The code is only wrong if it were to cause a floating exception when flag is zero, or if the cost of evaluating argument 2 is higher than the conditional branch. -- +----------------------------------------------+ | Jerry Feldman | | Boston Computer Solutions and Consulting | | http://gaf.ne.highway1.com/ | +----------------------------------------------+