Quick Search:

View

Revision:
Expand:  
Changeset: r-1-0-0:ragge:20110301174021

Diff

Diff from 1.36.2.1 to:

Annotations

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

Annotated File View

ragge
1.36.2.1
1 /*      $Id: optim.c,v 1.36.2.1 2011/03/01 17:40:21 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
ragge
1.21
46 /* remove left node */
47 static NODE *
48 zapleft(NODE *p)
49 {
50         NODE *q;
51
52         q = p->n_left;
53         nfree(p->n_right);
54         nfree(p);
55         return q;
56 }
57
ragge
1.2
58 /*
59  * fortran function arguments
60  */
ragge
1.9
61 static NODE *
ragge
1.2
62 fortarg(NODE *p)
63 {
ragge
1.3
64         ifp->n_op == CM ){
65                 p->n_left = fortargp->n_left );
66                 p->n_right = fortargp->n_right );
ragge
1.1
67                 return(p);
ragge
1.2
68         }
ragge
1.1
69
ragge
1.3
70         whileISPTR(p->n_type) ){
ragge
1.16
71                 p = buildtreeUMULpNIL );
ragge
1.2
72         }
ragge
1.1
73         returnoptim(p) );
ragge
1.2
74 }
ragge
1.1
75
76         /* mapping relationals when the sides are reversed */
77 short revrel[] ={ EQNEGEGTLELTUGEUGTULEULT };
ragge
1.2
78
79 /*
80  * local optimizations, most of which are probably
81  * machine independent
82  */
ragge
1.1
83 NODE *
ragge
1.2
84 optim(NODE *p)
85 {
ragge
1.35
86         struct attr *ap;
ragge
1.2
87         int oty;
ragge
1.8
88         NODE *sp, *q;
ragge
1.1
89         int i;
90         TWORD t;
91
ragge
1.29
92         t = BTYPE(p->n_type);
ragge
1.1
93         ifoflag ) return(p);
ragge
1.21
94
95         ty = coptype(p->n_op);
ragge
1.1
96         ifty == LTYPE ) return(p);
97
ragge
1.3
98         ifty == BITYPE ) p->n_right = optim(p->n_right);
99         p->n_left = optim(p->n_left);
ragge
1.1
100
101         /* collect constants */
ragge
1.21
102 again:  o = p->n_op;
ragge
1.1
103         switch(o){
104
105         case SCONV:
106         case PCONV:
107                 returnclocal(p) );
108
109         case FORTCALL:
ragge
1.3
110                 p->n_right = fortargp->n_right );
ragge
1.1
111                 break;
112
ragge
1.15
113         case ADDROF:
ragge
1.20
114                 if (LO(p) == TEMP)
115                         return p;
ragge
1.10
116                 ifLO(p) != NAME ) cerror"& error" );
117
118                 if( !andable(p->n_left) ) return(p);
ragge
1.1
119
120                 LO(p) = ICON;
121
122                 setuleft:
123                 /* paint over the type of the left hand side with the type of the top */
ragge
1.3
124                 p->n_left->n_type = p->n_type;
ragge
1.7
125                 p->n_left->n_df = p->n_df;
ragge
1.35
126                 p->n_left->n_ap = p->n_ap;
ragge
1.8
127                 q = p->n_left;
128                 nfree(p);
129                 return q;
ragge
1.1
130
ragge
1.16
131         case UMUL:
ragge
1.36
132                 if (LO(p) == ADDROF) {
133                         q = p->n_left->n_left;
134                         nfree(p->n_left);
135                         nfree(p);
136                         return q;
137                 }
ragge
1.1
138                 ifLO(p) != ICON ) break;
139                 LO(p) = NAME;
140                 goto setuleft;
141
ragge
1.21
142         case RS:
ragge
1.36.2.1
143                 if (LCON(p) && RCON(p) && conval(p->n_leftop->n_right))
144                         goto zapright;
145
ragge
1.35
146                 ap = attr_find(p->n_apATTR_BASETYP);
ragge
1.34
147
ragge
1.31
148                 if (LO(p) == RS && RCON(p->n_left) && RCON(p) &&
ragge
1.35
149                     (RV(p) + RV(p->n_left)) < ap->atypsz) {
ragge
1.21
150                         /* two right-shift  by constants */
151                         RV(p) += RV(p->n_left);
152                         p->n_left = zapleft(p->n_left);
ragge
1.23
153                 }
154 #if 0
155                   else if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
156                         RV(p) -= RV(p->n_left);
157                         if (RV(p) < 0)
158                                 o = p->n_op = LSRV(p) = -RV(p);
159                         p->n_left = zapleft(p->n_left);
160                 }
ragge
1.23
161 #endif
ragge
1.22
162                 if (RO(p) == ICON) {
163                         if (RV(p) < 0) {
164                                 RV(p) = -RV(p);
165                                 p->n_op = LS;
166                                 goto again;
167                         }
ragge
1.24
168 #ifdef notyet /* must check for side effects, --a >> 32; */
ragge
1.22
169                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue) &&
170                             ISUNSIGNED(p->n_type)) { /* ignore signed shifts */
171                                 /* too many shifts */
172                                 tfree(p->n_left);
173                                 nfree(p->n_right);
174                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
175                         } else
176 #endif
ragge
1.26
177                         /* avoid larger shifts than type size */
ragge
1.35
178                         if (RV(p) >= ap->atypsz) {
179                                 RV(p) = RV(p) % 
180                                     attr_find(p->n_apATTR_BASETYP)->atypsz;
ragge
1.26
181                                 werror("shift larger than type");
182                         }
ragge
1.24
183                         if (RV(p) == 0)
ragge
1.22
184                                 p = zapleft(p);
ragge
1.21
185                 }
186                 break;
187
188         case LS:
ragge
1.36.2.1
189                 if (LCON(p) && RCON(p) && conval(p->n_leftop->n_right))
190                         goto zapright;
191
ragge
1.35
192                 ap = attr_find(p->n_apATTR_BASETYP);
ragge
1.34
193
ragge
1.21
194                 if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
195                         /* two left-shift  by constants */
196                         RV(p) += RV(p->n_left);
197                         p->n_left = zapleft(p->n_left);
ragge
1.23
198                 }
199 #if 0
200                   else if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
201                         RV(p) -= RV(p->n_left);
202                         p->n_left = zapleft(p->n_left);
203                 }
ragge
1.23
204 #endif
ragge
1.22
205                 if (RO(p) == ICON) {
206                         if (RV(p) < 0) {
207                                 RV(p) = -RV(p);
208                                 p->n_op = RS;
209                                 goto again;
210                         }
ragge
1.24
211 #ifdef notyet /* must check for side effects */
ragge
1.22
212                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue)) {
213                                 /* too many shifts */
214                                 tfree(p->n_left);
215                                 nfree(p->n_right);
216                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
217                         } else
218 #endif
ragge
1.26
219                         /* avoid larger shifts than type size */
ragge
1.35
220                         if (RV(p) >= ap->atypsz) {
221                                 RV(p) = RV(p) %
222                                     attr_find(p->n_apATTR_BASETYP)->atypsz;
ragge
1.26
223                                 werror("shift larger than type");
224                         }
ragge
1.24
225                         if (RV(p) == 0)  
ragge
1.22
226                                 p = zapleft(p);
ragge
1.21
227                 }
228                 break;
229
ragge
1.1
230         case MINUS:
ragge
1.19
231                 if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
232                         /* link-time constants, but both are the same */
233                         /* solve it now by forgetting the symbols */
234                         p->n_left->n_sp = p->n_right->n_sp = NULL;
235                 }
ragge
1.3
236                 if( !nncon(p->n_right) ) break;
ragge
1.1
237                 RV(p) = -RV(p);
ragge
1.3
238                 o = p->n_op = PLUS;
ragge
1.1
239
240         case MUL:
241         case PLUS:
242         case AND:
243         case OR:
244         case ER:
245                 /* commutative ops; for now, just collect constants */
246                 /* someday, do it right */
ragge
1.14
247                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
248                         SWAPp->n_leftp->n_right );
ragge
1.1
249                 /* make ops tower to the left, not the right */
250                 ifRO(p) == o ){
251                         NODE *t1, *t2, *t3;
ragge
1.3
252                         t1 = p->n_left;
253                         sp = p->n_right;
254                         t2 = sp->n_left;
255                         t3 = sp->n_right;
ragge
1.1
256                         /* now, put together again */
ragge
1.3
257                         p->n_left = sp;
258                         sp->n_left = t1;
259                         sp->n_right = t2;
260                         p->n_right = t3;
ragge
1.1
261                         }
ragge
1.3
262                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
263                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
264                         zapleft:
ragge
1.8
265
266                         q = p->n_left->n_left;
267                         nfree(p->n_left->n_right);
268                         nfree(p->n_left);
269                         p->n_left = q;
ragge
1.1
270                 }
ragge
1.14
271                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
272                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
273                         goto zapleft;
274                         }
ragge
1.10
275                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
276                         zapright:
ragge
1.8
277                         nfree(p->n_right);
ragge
1.14
278                         q = makety(p->n_leftp->n_typep->n_qual,
ragge
1.35
279                             p->n_dfp->n_ap);
ragge
1.8
280                         nfree(p);
281                         return clocal(q);
ragge
1.10
282                         }
ragge
1.1
283
ragge
1.10
284                 /* change muls to shifts */
ragge
1.1
285
ragge
1.10
286                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
287                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
288                                 goto zapright;
289                                 }
ragge
1.10
290                         o = p->n_op = LS;
291                         p->n_right->n_type = INT;
292                         p->n_right->n_df = NULL;
293                         RV(p) = i;
ragge
1.1
294                         }
295
296                 /* change +'s of negative consts back to - */
ragge
1.3
297                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
298                         RV(p) = -RV(p);
ragge
1.3
299                         o = p->n_op = MINUS;
ragge
1.1
300                         }
ragge
1.28
301
302                 /* remove ops with RHS 0 */
303                 if ((o == PLUS || o == MINUS || o == OR || o == ER) &&
304                     nncon(p->n_right) && RV(p) == 0) {
305                         goto zapright;
306                 }
ragge
1.1
307                 break;
308
309         case DIV:
ragge
1.19
310                 ifnnconp->n_right ) && p->n_right->n_lval == 1 )
311                         goto zapright;
312                 if (LCON(p) && RCON(p) && conval(p->n_leftDIVp->n_right))
313                         goto zapright;
ragge
1.25
314                 if (RCON(p) && ISUNSIGNED(p->n_type) && (i=ispow2(RV(p))) > 0) {
315                         p->n_op = RS;
316                         RV(p) = i;
ragge
1.27
317                         q = p->n_right;
ragge
1.35
318                         if(tsize(q->n_typeq->n_dfq->n_ap) > SZINT)
319                                 p->n_right = makety(qINT00MKAP(INT));
ragge
1.27
320
ragge
1.25
321                         break;
322                 }
323                 break;
324
325         case MOD:
326                 if (RCON(p) && ISUNSIGNED(p->n_type) && ispow2(RV(p)) > 0) {
327                         p->n_op = AND;
328                         RV(p) = RV(p) -1;
329                         break;
330                 }
ragge
1.1
331                 break;
332
333         case EQ:
334         case NE:
335         case LT:
336         case LE:
337         case GT:
338         case GE:
339         case ULT:
340         case ULE:
341         case UGT:
342         case UGE:
343                 if( !LCON(p) ) break;
344
345                 /* exchange operands */
346
ragge
1.3
347                 sp = p->n_left;
348                 p->n_left = p->n_right;
349                 p->n_right = sp;
350                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
351                 break;
352
ragge
1.32
353 #ifdef notyet
354         case ASSIGN:
355                 /* Simple test to avoid two branches */
356                 if (RO(p) != NE)
357                         break;
358                 q = p->n_right;
359                 if (RCON(q) && RV(q) == 0 && LO(q) == AND &&
360                     RCON(q->n_left) && (i = ispow2(RV(q->n_left))) &&
361                     q->n_left->n_type == INT) {
362                         q->n_op = RS;
363                         RV(q) = i;
ragge
1.1
364                 }
ragge
1.32
365                 break;
366 #endif
367         }
ragge
1.1
368
369         return(p);
370         }
371
ragge
1.2
372 int
373 ispow2(CONSZ c)
374 {
375         int i;
ragge
1.1
376         ifc <= 0 || (c&(c-1)) ) return(-1);
377         fori=0c>1; ++ic >>= 1;
378         return(i);
ragge
1.2
379 }
ragge
1.10
380
381 int
382 nnconp ) NODE *p; {
383         /* is p a constant without a name */
384         returnp->n_op == ICON && p->n_sp == NULL );
385         }
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-09-21 04:04 +0200