Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site umcp-cs.UUCP Path: utzoo!linus!philabs!cmcl2!seismo!umcp-cs!chris From: chris@umcp-cs.UUCP (Chris Torek) Newsgroups: net.bugs.4bsd Subject: missed optimizations in pcc Message-ID: <1213@umcp-cs.UUCP> Date: Sun, 18-Nov-84 07:24:33 EST Article-I.D.: umcp-cs.1213 Posted: Sun Nov 18 07:24:33 1984 Date-Received: Tue, 20-Nov-84 06:44:07 EST Distribution: net Organization: U of Maryland, Computer Science Dept., College Park, MD Lines: 71 Index: lib/pcc/optim.c 4.2 Fix Description: The machine independent expression optimizer doesn't handle "/=", "%", or "%=" by the constants 1 and 0. Repeat-By: Feed the following through /lib/ccom; look at the assembly: f () { register i,j; i = j / 1; /* nice, the division goes away */ i /= 1; /* but not this time! */ i = j % 1; /* result is always 0, but talk about doing things the hard way */ i %= 1; /* same */ } Fix: The following changes to optim.c make it like /= as well as /, and make it handle % 1 and %= 1 in a reasonable way. We also added checks for division and mod by zero (they used to just generate divisions by zero). RCS file: RCS/optim.c,v retrieving revision 1.1 diff -c1 -r1.1 optim.c *** /tmp/,RCSt1008671 Sun Nov 18 07:16:07 1984 --- optim.c Sun Nov 18 07:15:44 1984 *************** *** 142,143 case DIV: --- 142,157 ----- + case ASG MOD: + case MOD: + if( nncon( p->in.right ) ){ + if( p->in.right->tn.lval == 1 ){ + /* mod by one gives zero */ + p->in.op = p->in.op==MOD ? COMOP : ASSIGN; + p->in.right->tn.lval = 0; + break; + } + if( p->in.right->tn.lval == 0 ) uerror("mod by zero"); + } + break; + + case ASG DIV: case DIV: *************** *** 143,145 case DIV: ! if( nncon( p->in.right ) && p->in.right->tn.lval == 1 ) goto zapright; break; --- 157,162 ----- case DIV: ! if( nncon( p->in.right ) ){ ! if( p->in.right->tn.lval == 1 ) goto zapright; ! if( p->in.right->tn.lval == 0 ) uerror("division by zero"); ! } break; -- (This line accidently left nonblank.) In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (301) 454-7690 UUCP: {seismo,allegra,brl-bmd}!umcp-cs!chris CSNet: chris@umcp-cs ARPA: chris@maryland