Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20081027211320

Diff

Diff from 1.32 to:

Annotations

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

Annotated File View

ragge
1.32
1 /*      $Id: optim.c,v 1.32 2008/10/27 21:13:20 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
46 int oflag = 0;
47
ragge
1.21
48 /* remove left node */
49 static NODE *
50 zapleft(NODE *p)
51 {
52         NODE *q;
53
54         q = p->n_left;
55         nfree(p->n_right);
56         nfree(p);
57         return q;
58 }
59
ragge
1.2
60 /*
61  * fortran function arguments
62  */
ragge
1.9
63 static NODE *
ragge
1.2
64 fortarg(NODE *p)
65 {
ragge
1.3
66         ifp->n_op == CM ){
67                 p->n_left = fortargp->n_left );
68                 p->n_right = fortargp->n_right );
ragge
1.1
69                 return(p);
ragge
1.2
70         }
ragge
1.1
71
ragge
1.3
72         whileISPTR(p->n_type) ){
ragge
1.16
73                 p = buildtreeUMULpNIL );
ragge
1.2
74         }
ragge
1.1
75         returnoptim(p) );
ragge
1.2
76 }
ragge
1.1
77
78         /* mapping relationals when the sides are reversed */
79 short revrel[] ={ EQNEGEGTLELTUGEUGTULEULT };
ragge
1.2
80
81 /*
82  * local optimizations, most of which are probably
83  * machine independent
84  */
ragge
1.1
85 NODE *
ragge
1.2
86 optim(NODE *p)
87 {
88         int oty;
ragge
1.8
89         NODE *sp, *q;
ragge
1.1
90         int i;
91         TWORD t;
92
ragge
1.29
93         t = BTYPE(p->n_type);
ragge
1.1
94         ifoflag ) return(p);
ragge
1.21
95
96         ty = coptype(p->n_op);
ragge
1.1
97         ifty == LTYPE ) return(p);
98
ragge
1.3
99         ifty == BITYPE ) p->n_right = optim(p->n_right);
100         p->n_left = optim(p->n_left);
ragge
1.1
101
102         /* collect constants */
ragge
1.21
103 again:  o = p->n_op;
ragge
1.1
104         switch(o){
105
106         case SCONV:
107         case PCONV:
108                 returnclocal(p) );
109
110         case FORTCALL:
ragge
1.3
111                 p->n_right = fortargp->n_right );
ragge
1.1
112                 break;
113
ragge
1.15
114         case ADDROF:
ragge
1.20
115                 if (LO(p) == TEMP)
116                         return p;
ragge
1.10
117                 ifLO(p) != NAME ) cerror"& error" );
118
119                 if( !andable(p->n_left) ) return(p);
ragge
1.1
120
121                 LO(p) = ICON;
122
123                 setuleft:
124                 /* paint over the type of the left hand side with the type of the top */
ragge
1.3
125                 p->n_left->n_type = p->n_type;
ragge
1.7
126                 p->n_left->n_df = p->n_df;
ragge
1.5
127                 p->n_left->n_sue = p->n_sue;
ragge
1.8
128                 q = p->n_left;
129                 nfree(p);
130                 return q;
ragge
1.1
131
ragge
1.16
132         case UMUL:
ragge
1.1
133                 ifLO(p) != ICON ) break;
134                 LO(p) = NAME;
135                 goto setuleft;
136
ragge
1.21
137         case RS:
ragge
1.31
138                 if (LO(p) == RS && RCON(p->n_left) && RCON(p) &&
139                     (RV(p) + RV(p->n_left)) < p->n_sue->suesize) {
ragge
1.21
140                         /* two right-shift  by constants */
141                         RV(p) += RV(p->n_left);
142                         p->n_left = zapleft(p->n_left);
ragge
1.23
143                 }
144 #if 0
145                   else if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
146                         RV(p) -= RV(p->n_left);
147                         if (RV(p) < 0)
148                                 o = p->n_op = LSRV(p) = -RV(p);
149                         p->n_left = zapleft(p->n_left);
150                 }
ragge
1.23
151 #endif
ragge
1.22
152                 if (RO(p) == ICON) {
153                         if (RV(p) < 0) {
154                                 RV(p) = -RV(p);
155                                 p->n_op = LS;
156                                 goto again;
157                         }
ragge
1.24
158 #ifdef notyet /* must check for side effects, --a >> 32; */
ragge
1.22
159                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue) &&
160                             ISUNSIGNED(p->n_type)) { /* ignore signed shifts */
161                                 /* too many shifts */
162                                 tfree(p->n_left);
163                                 nfree(p->n_right);
164                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
165                         } else
166 #endif
ragge
1.26
167                         /* avoid larger shifts than type size */
168                         if (RV(p) >= p->n_sue->suesize) {
169                                 RV(p) = RV(p) % p->n_sue->suesize;
170                                 werror("shift larger than type");
171                         }
ragge
1.24
172                         if (RV(p) == 0)
ragge
1.22
173                                 p = zapleft(p);
ragge
1.21
174                 }
175                 break;
176
177         case LS:
178                 if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
179                         /* two left-shift  by constants */
180                         RV(p) += RV(p->n_left);
181                         p->n_left = zapleft(p->n_left);
ragge
1.23
182                 }
183 #if 0
184                   else if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
185                         RV(p) -= RV(p->n_left);
186                         p->n_left = zapleft(p->n_left);
187                 }
ragge
1.23
188 #endif
ragge
1.22
189                 if (RO(p) == ICON) {
190                         if (RV(p) < 0) {
191                                 RV(p) = -RV(p);
192                                 p->n_op = RS;
193                                 goto again;
194                         }
ragge
1.24
195 #ifdef notyet /* must check for side effects */
ragge
1.22
196                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue)) {
197                                 /* too many shifts */
198                                 tfree(p->n_left);
199                                 nfree(p->n_right);
200                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
201                         } else
202 #endif
ragge
1.26
203                         /* avoid larger shifts than type size */
204                         if (RV(p) >= p->n_sue->suesize) {
205                                 RV(p) = RV(p) % p->n_sue->suesize;
206                                 werror("shift larger than type");
207                         }
ragge
1.24
208                         if (RV(p) == 0)  
ragge
1.22
209                                 p = zapleft(p);
ragge
1.21
210                 }
211                 break;
212
ragge
1.1
213         case MINUS:
ragge
1.19
214                 if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
215                         /* link-time constants, but both are the same */
216                         /* solve it now by forgetting the symbols */
217                         p->n_left->n_sp = p->n_right->n_sp = NULL;
218                 }
ragge
1.3
219                 if( !nncon(p->n_right) ) break;
ragge
1.1
220                 RV(p) = -RV(p);
ragge
1.3
221                 o = p->n_op = PLUS;
ragge
1.1
222
223         case MUL:
224         case PLUS:
225         case AND:
226         case OR:
227         case ER:
228                 /* commutative ops; for now, just collect constants */
229                 /* someday, do it right */
ragge
1.14
230                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
231                         SWAPp->n_leftp->n_right );
ragge
1.1
232                 /* make ops tower to the left, not the right */
233                 ifRO(p) == o ){
234                         NODE *t1, *t2, *t3;
ragge
1.3
235                         t1 = p->n_left;
236                         sp = p->n_right;
237                         t2 = sp->n_left;
238                         t3 = sp->n_right;
ragge
1.1
239                         /* now, put together again */
ragge
1.3
240                         p->n_left = sp;
241                         sp->n_left = t1;
242                         sp->n_right = t2;
243                         p->n_right = t3;
ragge
1.1
244                         }
ragge
1.3
245                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
246                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
247                         zapleft:
ragge
1.8
248
249                         q = p->n_left->n_left;
250                         nfree(p->n_left->n_right);
251                         nfree(p->n_left);
252                         p->n_left = q;
ragge
1.1
253                 }
ragge
1.14
254                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
255                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
256                         goto zapleft;
257                         }
ragge
1.10
258                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
259                         zapright:
ragge
1.8
260                         nfree(p->n_right);
ragge
1.14
261                         q = makety(p->n_leftp->n_typep->n_qual,
262                             p->n_dfp->n_sue);
ragge
1.8
263                         nfree(p);
264                         return clocal(q);
ragge
1.10
265                         }
ragge
1.1
266
ragge
1.10
267                 /* change muls to shifts */
ragge
1.1
268
ragge
1.10
269                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
270                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
271                                 goto zapright;
272                                 }
ragge
1.10
273                         o = p->n_op = LS;
274                         p->n_right->n_type = INT;
275                         p->n_right->n_df = NULL;
276                         RV(p) = i;
ragge
1.1
277                         }
278
279                 /* change +'s of negative consts back to - */
ragge
1.3
280                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
281                         RV(p) = -RV(p);
ragge
1.3
282                         o = p->n_op = MINUS;
ragge
1.1
283                         }
ragge
1.28
284
285                 /* remove ops with RHS 0 */
286                 if ((o == PLUS || o == MINUS || o == OR || o == ER) &&
287                     nncon(p->n_right) && RV(p) == 0) {
288                         goto zapright;
289                 }
ragge
1.1
290                 break;
291
292         case DIV:
ragge
1.19
293                 ifnnconp->n_right ) && p->n_right->n_lval == 1 )
294                         goto zapright;
295                 if (LCON(p) && RCON(p) && conval(p->n_leftDIVp->n_right))
296                         goto zapright;
ragge
1.25
297                 if (RCON(p) && ISUNSIGNED(p->n_type) && (i=ispow2(RV(p))) > 0) {
298                         p->n_op = RS;
299                         RV(p) = i;
ragge
1.27
300                         q = p->n_right;
301                         if(tsize(q->n_typeq->n_dfq->n_sue) > SZINT)
302                                 p->n_right = makety(qINT00MKSUE(INT));
303
ragge
1.25
304                         break;
305                 }
306                 break;
307
308         case MOD:
309                 if (RCON(p) && ISUNSIGNED(p->n_type) && ispow2(RV(p)) > 0) {
310                         p->n_op = AND;
311                         RV(p) = RV(p) -1;
312                         break;
313                 }
ragge
1.1
314                 break;
315
316         case EQ:
317         case NE:
318         case LT:
319         case LE:
320         case GT:
321         case GE:
322         case ULT:
323         case ULE:
324         case UGT:
325         case UGE:
326                 if( !LCON(p) ) break;
327
328                 /* exchange operands */
329
ragge
1.3
330                 sp = p->n_left;
331                 p->n_left = p->n_right;
332                 p->n_right = sp;
333                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
334                 break;
335
ragge
1.32
336 #ifdef notyet
337         case ASSIGN:
338                 /* Simple test to avoid two branches */
339                 if (RO(p) != NE)
340                         break;
341                 q = p->n_right;
342                 if (RCON(q) && RV(q) == 0 && LO(q) == AND &&
343                     RCON(q->n_left) && (i = ispow2(RV(q->n_left))) &&
344                     q->n_left->n_type == INT) {
345                         q->n_op = RS;
346                         RV(q) = i;
ragge
1.1
347                 }
ragge
1.32
348                 break;
349 #endif
350         }
ragge
1.1
351
352         return(p);
353         }
354
ragge
1.2
355 int
356 ispow2(CONSZ c)
357 {
358         int i;
ragge
1.1
359         ifc <= 0 || (c&(c-1)) ) return(-1);
360         fori=0c>1; ++ic >>= 1;
361         return(i);
ragge
1.2
362 }
ragge
1.10
363
364 int
365 nnconp ) NODE *p; {
366         /* is p a constant without a name */
367         returnp->n_op == ICON && p->n_sp == NULL );
368         }
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-07-10 05:59 +0200