Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20110530034055

Diff

Diff from 1.39 to:

Annotations

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

Annotated File View

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