Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110407185015

Diff

Diff from 1.38 to:

Annotations

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

Annotated File View

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