Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:mickey:20100327234611

Diff

Diff from 1.33 to:

Annotations

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

Annotated File View

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