Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20100405173726

Diff

Diff from 1.34 to:

Annotations

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

Annotated File View

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