Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20080206182300

Diff

Diff from 1.30 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/cc/ccom/optim.c

Annotated File View

ragge
1.30
1 /*      $Id: optim.c,v 1.30 2008/02/06 18:23:00 ragge Exp $     */
ragge
1.10
2 /*
3  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * Redistributions of source code and documentation must retain the above
10  * copyright notice, this list of conditions and the following disclaimer.
11  * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditionsand the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * All advertising materials mentioning features or use of this software
15  * must display the following acknowledgement:
16  *      This product includes software developed or owned by Caldera
17  *      International, Inc.
18  * Neither the name of Caldera International, Inc. nor the names of other
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27  * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
ragge
1.1
35
36 # include "pass1.h"
37
38 # define SWAP(p,q) {sp=p; p=q; q=sp;}
ragge
1.3
39 # define RCON(p) (p->n_right->n_op==ICON)
40 # define RO(p) p->n_right->n_op
41 # define RV(p) p->n_right->n_lval
42 # define LCON(p) (p->n_left->n_op==ICON)
43 # define LO(p) p->n_left->n_op
44 # define LV(p) p->n_left->n_lval
ragge
1.1
45
46 int oflag = 0;
47
ragge
1.21
48 /* remove left node */
49 static NODE *
50 zapleft(NODE *p)
51 {
52         NODE *q;
53
54         q = p->n_left;
55         nfree(p->n_right);
56         nfree(p);
57         return q;
58 }
59
ragge
1.2
60 /*
61  * fortran function arguments
62  */
ragge
1.9
63 static NODE *
ragge
1.2
64 fortarg(NODE *p)
65 {
ragge
1.3
66         ifp->n_op == CM ){
67                 p->n_left = fortargp->n_left );
68                 p->n_right = fortargp->n_right );
ragge
1.1
69                 return(p);
ragge
1.2
70         }
ragge
1.1
71
ragge
1.3
72         whileISPTR(p->n_type) ){
ragge
1.16
73                 p = buildtreeUMULpNIL );
ragge
1.2
74         }
ragge
1.1
75         returnoptim(p) );
ragge
1.2
76 }
ragge
1.1
77
78         /* mapping relationals when the sides are reversed */
79 short revrel[] ={ EQNEGEGTLELTUGEUGTULEULT };
ragge
1.2
80
81 /*
82  * local optimizations, most of which are probably
83  * machine independent
84  */
ragge
1.1
85 NODE *
ragge
1.2
86 optim(NODE *p)
87 {
88         int oty;
ragge
1.8
89         NODE *sp, *q;
ragge
1.1
90         int i;
91         TWORD t;
92
ragge
1.29
93         t = BTYPE(p->n_type);
ragge
1.1
94         ifoflag ) return(p);
ragge
1.21
95
96         ty = coptype(p->n_op);
ragge
1.1
97         ifty == LTYPE ) return(p);
98
ragge
1.3
99         ifty == BITYPE ) p->n_right = optim(p->n_right);
100         p->n_left = optim(p->n_left);
ragge
1.1
101
102         /* collect constants */
ragge
1.21
103 again:  o = p->n_op;
ragge
1.1
104         switch(o){
105
106         case SCONV:
107         case PCONV:
108                 returnclocal(p) );
109
110         case FORTCALL:
ragge
1.3
111                 p->n_right = fortargp->n_right );
ragge
1.1
112                 break;
113
ragge
1.15
114         case ADDROF:
ragge
1.20
115                 if (LO(p) == TEMP)
116                         return p;
ragge
1.10
117                 ifLO(p) != NAME ) cerror"& error" );
118
119                 if( !andable(p->n_left) ) return(p);
ragge
1.1
120
121                 LO(p) = ICON;
122
123                 setuleft:
124                 /* paint over the type of the left hand side with the type of the top */
ragge
1.3
125                 p->n_left->n_type = p->n_type;
ragge
1.7
126                 p->n_left->n_df = p->n_df;
ragge
1.5
127                 p->n_left->n_sue = p->n_sue;
ragge
1.8
128                 q = p->n_left;
129                 nfree(p);
130                 return q;
ragge
1.1
131
ragge
1.16
132         case UMUL:
ragge
1.1
133                 ifLO(p) != ICON ) break;
134                 LO(p) = NAME;
135                 goto setuleft;
136
ragge
1.21
137         case RS:
138                 if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
139                         /* two right-shift  by constants */
140                         RV(p) += RV(p->n_left);
141                         p->n_left = zapleft(p->n_left);
ragge
1.23
142                 }
143 #if 0
144                   else if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
145                         RV(p) -= RV(p->n_left);
146                         if (RV(p) < 0)
147                                 o = p->n_op = LSRV(p) = -RV(p);
148                         p->n_left = zapleft(p->n_left);
149                 }
ragge
1.23
150 #endif
ragge
1.22
151                 if (RO(p) == ICON) {
152                         if (RV(p) < 0) {
153                                 RV(p) = -RV(p);
154                                 p->n_op = LS;
155                                 goto again;
156                         }
ragge
1.24
157 #ifdef notyet /* must check for side effects, --a >> 32; */
ragge
1.22
158                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue) &&
159                             ISUNSIGNED(p->n_type)) { /* ignore signed shifts */
160                                 /* too many shifts */
161                                 tfree(p->n_left);
162                                 nfree(p->n_right);
163                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
164                         } else
165 #endif
ragge
1.26
166                         /* avoid larger shifts than type size */
167                         if (RV(p) >= p->n_sue->suesize) {
168                                 RV(p) = RV(p) % p->n_sue->suesize;
169                                 werror("shift larger than type");
170                         }
ragge
1.24
171                         if (RV(p) == 0)
ragge
1.22
172                                 p = zapleft(p);
ragge
1.21
173                 }
174                 break;
175
176         case LS:
177                 if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
178                         /* two left-shift  by constants */
179                         RV(p) += RV(p->n_left);
180                         p->n_left = zapleft(p->n_left);
ragge
1.23
181                 }
182 #if 0
183                   else if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
184                         RV(p) -= RV(p->n_left);
185                         p->n_left = zapleft(p->n_left);
186                 }
ragge
1.23
187 #endif
ragge
1.22
188                 if (RO(p) == ICON) {
189                         if (RV(p) < 0) {
190                                 RV(p) = -RV(p);
191                                 p->n_op = RS;
192                                 goto again;
193                         }
ragge
1.24
194 #ifdef notyet /* must check for side effects */
ragge
1.22
195                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue)) {
196                                 /* too many shifts */
197                                 tfree(p->n_left);
198                                 nfree(p->n_right);
199                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
200                         } else
201 #endif
ragge
1.26
202                         /* avoid larger shifts than type size */
203                         if (RV(p) >= p->n_sue->suesize) {
204                                 RV(p) = RV(p) % p->n_sue->suesize;
205                                 werror("shift larger than type");
206                         }
ragge
1.24
207                         if (RV(p) == 0)  
ragge
1.22
208                                 p = zapleft(p);
ragge
1.21
209                 }
210                 break;
211
ragge
1.1
212         case MINUS:
ragge
1.19
213                 if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
214                         /* link-time constants, but both are the same */
215                         /* solve it now by forgetting the symbols */
216                         p->n_left->n_sp = p->n_right->n_sp = NULL;
217                 }
ragge
1.3
218                 if( !nncon(p->n_right) ) break;
ragge
1.1
219                 RV(p) = -RV(p);
ragge
1.3
220                 o = p->n_op = PLUS;
ragge
1.1
221
222         case MUL:
223         case PLUS:
224         case AND:
225         case OR:
226         case ER:
227                 /* commutative ops; for now, just collect constants */
228                 /* someday, do it right */
ragge
1.14
229                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
230                         SWAPp->n_leftp->n_right );
ragge
1.1
231                 /* make ops tower to the left, not the right */
232                 ifRO(p) == o ){
233                         NODE *t1, *t2, *t3;
ragge
1.3
234                         t1 = p->n_left;
235                         sp = p->n_right;
236                         t2 = sp->n_left;
237                         t3 = sp->n_right;
ragge
1.1
238                         /* now, put together again */
ragge
1.3
239                         p->n_left = sp;
240                         sp->n_left = t1;
241                         sp->n_right = t2;
242                         p->n_right = t3;
ragge
1.1
243                         }
ragge
1.3
244                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
245                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
246                         zapleft:
ragge
1.8
247
248                         q = p->n_left->n_left;
249                         nfree(p->n_left->n_right);
250                         nfree(p->n_left);
251                         p->n_left = q;
ragge
1.1
252                 }
ragge
1.14
253                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
254                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
255                         goto zapleft;
256                         }
ragge
1.10
257                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
258                         zapright:
ragge
1.8
259                         nfree(p->n_right);
ragge
1.14
260                         q = makety(p->n_leftp->n_typep->n_qual,
261                             p->n_dfp->n_sue);
ragge
1.8
262                         nfree(p);
263                         return clocal(q);
ragge
1.10
264                         }
ragge
1.1
265
ragge
1.10
266                 /* change muls to shifts */
ragge
1.1
267
ragge
1.10
268                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
269                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
270                                 goto zapright;
271                                 }
ragge
1.10
272                         o = p->n_op = LS;
273                         p->n_right->n_type = INT;
274                         p->n_right->n_df = NULL;
275                         RV(p) = i;
ragge
1.1
276                         }
277
278                 /* change +'s of negative consts back to - */
ragge
1.3
279                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
280                         RV(p) = -RV(p);
ragge
1.3
281                         o = p->n_op = MINUS;
ragge
1.1
282                         }
ragge
1.28
283
284                 /* remove ops with RHS 0 */
285                 if ((o == PLUS || o == MINUS || o == OR || o == ER) &&
286                     nncon(p->n_right) && RV(p) == 0) {
287                         goto zapright;
288                 }
ragge
1.1
289                 break;
290
291         case DIV:
ragge
1.19
292                 ifnnconp->n_right ) && p->n_right->n_lval == 1 )
293                         goto zapright;
294                 if (LCON(p) && RCON(p) && conval(p->n_leftDIVp->n_right))
295                         goto zapright;
ragge
1.25
296                 if (RCON(p) && ISUNSIGNED(p->n_type) && (i=ispow2(RV(p))) > 0) {
297                         p->n_op = RS;
298                         RV(p) = i;
ragge
1.27
299                         q = p->n_right;
300                         if(tsize(q->n_typeq->n_dfq->n_sue) > SZINT)
301                                 p->n_right = makety(qINT00MKSUE(INT));
302
ragge
1.25
303                         break;
304                 }
305                 break;
306
307         case MOD:
308                 if (RCON(p) && ISUNSIGNED(p->n_type) && ispow2(RV(p)) > 0) {
309                         p->n_op = AND;
310                         RV(p) = RV(p) -1;
311                         break;
312                 }
ragge
1.1
313                 break;
314
315         case EQ:
316         case NE:
317         case LT:
318         case LE:
319         case GT:
320         case GE:
321         case ULT:
322         case ULE:
323         case UGT:
324         case UGE:
325                 if( !LCON(p) ) break;
326
327                 /* exchange operands */
328
ragge
1.3
329                 sp = p->n_left;
330                 p->n_left = p->n_right;
331                 p->n_right = sp;
332                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
333                 break;
334
335                 }
336
337         return(p);
338         }
339
ragge
1.2
340 int
341 ispow2(CONSZ c)
342 {
343         int i;
ragge
1.1
344         ifc <= 0 || (c&(c-1)) ) return(-1);
345         fori=0c>1; ++ic >>= 1;
346         return(i);
ragge
1.2
347 }
ragge
1.10
348
349 int
350 nnconp ) NODE *p; {
351         /* is p a constant without a name */
352         returnp->n_op == ICON && p->n_sp == NULL );
353         }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-10-31 16:03 +0100