Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20060709130240

Diff

Diff from 1.26 to:

Annotations

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

Annotated File View

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