Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110714170310

Diff

Diff from 1.44 to:

Annotations

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

Annotated File View

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