Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110707075106

Diff

Diff from 1.43 to:

Annotations

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

Annotated File View

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