Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20100811140844

Diff

Diff from 1.35 to:

Annotations

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

Annotated File View

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