Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20101009132210

Diff

Diff from 1.36 to:

Annotations

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

Annotated File View

ragge
1.36
1 /*      $Id: optim.c,v 1.36 2010/10/09 13:22:10 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.35
143                 ap = attr_find(p->n_apATTR_BASETYP);
ragge
1.34
144
ragge
1.31
145                 if (LO(p) == RS && RCON(p->n_left) && RCON(p) &&
ragge
1.35
146                     (RV(p) + RV(p->n_left)) < ap->atypsz) {
ragge
1.21
147                         /* two right-shift  by constants */
148                         RV(p) += RV(p->n_left);
149                         p->n_left = zapleft(p->n_left);
ragge
1.23
150                 }
151 #if 0
152                   else if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
153                         RV(p) -= RV(p->n_left);
154                         if (RV(p) < 0)
155                                 o = p->n_op = LSRV(p) = -RV(p);
156                         p->n_left = zapleft(p->n_left);
157                 }
ragge
1.23
158 #endif
ragge
1.22
159                 if (RO(p) == ICON) {
160                         if (RV(p) < 0) {
161                                 RV(p) = -RV(p);
162                                 p->n_op = LS;
163                                 goto again;
164                         }
ragge
1.24
165 #ifdef notyet /* must check for side effects, --a >> 32; */
ragge
1.22
166                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue) &&
167                             ISUNSIGNED(p->n_type)) { /* ignore signed shifts */
168                                 /* too many shifts */
169                                 tfree(p->n_left);
170                                 nfree(p->n_right);
171                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
172                         } else
173 #endif
ragge
1.26
174                         /* avoid larger shifts than type size */
ragge
1.35
175                         if (RV(p) >= ap->atypsz) {
176                                 RV(p) = RV(p) % 
177                                     attr_find(p->n_apATTR_BASETYP)->atypsz;
ragge
1.26
178                                 werror("shift larger than type");
179                         }
ragge
1.24
180                         if (RV(p) == 0)
ragge
1.22
181                                 p = zapleft(p);
ragge
1.21
182                 }
183                 break;
184
185         case LS:
ragge
1.35
186                 ap = attr_find(p->n_apATTR_BASETYP);
ragge
1.34
187
ragge
1.21
188                 if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
189                         /* two left-shift  by constants */
190                         RV(p) += RV(p->n_left);
191                         p->n_left = zapleft(p->n_left);
ragge
1.23
192                 }
193 #if 0
194                   else if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
195                         RV(p) -= RV(p->n_left);
196                         p->n_left = zapleft(p->n_left);
197                 }
ragge
1.23
198 #endif
ragge
1.22
199                 if (RO(p) == ICON) {
200                         if (RV(p) < 0) {
201                                 RV(p) = -RV(p);
202                                 p->n_op = RS;
203                                 goto again;
204                         }
ragge
1.24
205 #ifdef notyet /* must check for side effects */
ragge
1.22
206                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue)) {
207                                 /* too many shifts */
208                                 tfree(p->n_left);
209                                 nfree(p->n_right);
210                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
211                         } else
212 #endif
ragge
1.26
213                         /* avoid larger shifts than type size */
ragge
1.35
214                         if (RV(p) >= ap->atypsz) {
215                                 RV(p) = RV(p) %
216                                     attr_find(p->n_apATTR_BASETYP)->atypsz;
ragge
1.26
217                                 werror("shift larger than type");
218                         }
ragge
1.24
219                         if (RV(p) == 0)  
ragge
1.22
220                                 p = zapleft(p);
ragge
1.21
221                 }
222                 break;
223
ragge
1.1
224         case MINUS:
ragge
1.19
225                 if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
226                         /* link-time constants, but both are the same */
227                         /* solve it now by forgetting the symbols */
228                         p->n_left->n_sp = p->n_right->n_sp = NULL;
229                 }
ragge
1.3
230                 if( !nncon(p->n_right) ) break;
ragge
1.1
231                 RV(p) = -RV(p);
ragge
1.3
232                 o = p->n_op = PLUS;
ragge
1.1
233
234         case MUL:
235         case PLUS:
236         case AND:
237         case OR:
238         case ER:
239                 /* commutative ops; for now, just collect constants */
240                 /* someday, do it right */
ragge
1.14
241                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
242                         SWAPp->n_leftp->n_right );
ragge
1.1
243                 /* make ops tower to the left, not the right */
244                 ifRO(p) == o ){
245                         NODE *t1, *t2, *t3;
ragge
1.3
246                         t1 = p->n_left;
247                         sp = p->n_right;
248                         t2 = sp->n_left;
249                         t3 = sp->n_right;
ragge
1.1
250                         /* now, put together again */
ragge
1.3
251                         p->n_left = sp;
252                         sp->n_left = t1;
253                         sp->n_right = t2;
254                         p->n_right = t3;
ragge
1.1
255                         }
ragge
1.3
256                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
257                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
258                         zapleft:
ragge
1.8
259
260                         q = p->n_left->n_left;
261                         nfree(p->n_left->n_right);
262                         nfree(p->n_left);
263                         p->n_left = q;
ragge
1.1
264                 }
ragge
1.14
265                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
266                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
267                         goto zapleft;
268                         }
ragge
1.10
269                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
270                         zapright:
ragge
1.8
271                         nfree(p->n_right);
ragge
1.14
272                         q = makety(p->n_leftp->n_typep->n_qual,
ragge
1.35
273                             p->n_dfp->n_ap);
ragge
1.8
274                         nfree(p);
275                         return clocal(q);
ragge
1.10
276                         }
ragge
1.1
277
ragge
1.10
278                 /* change muls to shifts */
ragge
1.1
279
ragge
1.10
280                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
281                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
282                                 goto zapright;
283                                 }
ragge
1.10
284                         o = p->n_op = LS;
285                         p->n_right->n_type = INT;
286                         p->n_right->n_df = NULL;
287                         RV(p) = i;
ragge
1.1
288                         }
289
290                 /* change +'s of negative consts back to - */
ragge
1.3
291                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
292                         RV(p) = -RV(p);
ragge
1.3
293                         o = p->n_op = MINUS;
ragge
1.1
294                         }
ragge
1.28
295
296                 /* remove ops with RHS 0 */
297                 if ((o == PLUS || o == MINUS || o == OR || o == ER) &&
298                     nncon(p->n_right) && RV(p) == 0) {
299                         goto zapright;
300                 }
ragge
1.1
301                 break;
302
303         case DIV:
ragge
1.19
304                 ifnnconp->n_right ) && p->n_right->n_lval == 1 )
305                         goto zapright;
306                 if (LCON(p) && RCON(p) && conval(p->n_leftDIVp->n_right))
307                         goto zapright;
ragge
1.25
308                 if (RCON(p) && ISUNSIGNED(p->n_type) && (i=ispow2(RV(p))) > 0) {
309                         p->n_op = RS;
310                         RV(p) = i;
ragge
1.27
311                         q = p->n_right;
ragge
1.35
312                         if(tsize(q->n_typeq->n_dfq->n_ap) > SZINT)
313                                 p->n_right = makety(qINT00MKAP(INT));
ragge
1.27
314
ragge
1.25
315                         break;
316                 }
317                 break;
318
319         case MOD:
320                 if (RCON(p) && ISUNSIGNED(p->n_type) && ispow2(RV(p)) > 0) {
321                         p->n_op = AND;
322                         RV(p) = RV(p) -1;
323                         break;
324                 }
ragge
1.1
325                 break;
326
327         case EQ:
328         case NE:
329         case LT:
330         case LE:
331         case GT:
332         case GE:
333         case ULT:
334         case ULE:
335         case UGT:
336         case UGE:
337                 if( !LCON(p) ) break;
338
339                 /* exchange operands */
340
ragge
1.3
341                 sp = p->n_left;
342                 p->n_left = p->n_right;
343                 p->n_right = sp;
344                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
345                 break;
346
ragge
1.32
347 #ifdef notyet
348         case ASSIGN:
349                 /* Simple test to avoid two branches */
350                 if (RO(p) != NE)
351                         break;
352                 q = p->n_right;
353                 if (RCON(q) && RV(q) == 0 && LO(q) == AND &&
354                     RCON(q->n_left) && (i = ispow2(RV(q->n_left))) &&
355                     q->n_left->n_type == INT) {
356                         q->n_op = RS;
357                         RV(q) = i;
ragge
1.1
358                 }
ragge
1.32
359                 break;
360 #endif
361         }
ragge
1.1
362
363         return(p);
364         }
365
ragge
1.2
366 int
367 ispow2(CONSZ c)
368 {
369         int i;
ragge
1.1
370         ifc <= 0 || (c&(c-1)) ) return(-1);
371         fori=0c>1; ++ic >>= 1;
372         return(i);
ragge
1.2
373 }
ragge
1.10
374
375 int
376 nnconp ) NODE *p; {
377         /* is p a constant without a name */
378         returnp->n_op == ICON && p->n_sp == NULL );
379         }
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-08-21 14:05 +0200