Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20120510154040

Diff

Diff from 1.53 to:

Annotations

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

Annotated File View

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