Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20060703150044

Diff

Diff from 1.25 to:

Annotations

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

Annotated File View

ragge
1.25
1 /*      $Id: optim.c,v 1.25 2006/07/03 15:00:44 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
168                         if (RV(p) == 0)
ragge
1.22
169                                 p = zapleft(p);
ragge
1.21
170                 }
171                 break;
172
173         case LS:
174                 if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
175                         /* two left-shift  by constants */
176                         RV(p) += RV(p->n_left);
177                         p->n_left = zapleft(p->n_left);
ragge
1.23
178                 }
179 #if 0
180                   else if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
ragge
1.21
181                         RV(p) -= RV(p->n_left);
182                         p->n_left = zapleft(p->n_left);
183                 }
ragge
1.23
184 #endif
ragge
1.22
185                 if (RO(p) == ICON) {
186                         if (RV(p) < 0) {
187                                 RV(p) = -RV(p);
188                                 p->n_op = RS;
189                                 goto again;
190                         }
ragge
1.24
191 #ifdef notyet /* must check for side effects */
ragge
1.22
192                         if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue)) {
193                                 /* too many shifts */
194                                 tfree(p->n_left);
195                                 nfree(p->n_right);
196                                 p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
ragge
1.24
197                         } else
198 #endif
199                         if (RV(p) == 0)  
ragge
1.22
200                                 p = zapleft(p);
ragge
1.21
201                 }
202                 break;
203
ragge
1.1
204         case MINUS:
ragge
1.19
205                 if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
206                         /* link-time constants, but both are the same */
207                         /* solve it now by forgetting the symbols */
208                         p->n_left->n_sp = p->n_right->n_sp = NULL;
209                 }
ragge
1.3
210                 if( !nncon(p->n_right) ) break;
ragge
1.1
211                 RV(p) = -RV(p);
ragge
1.3
212                 o = p->n_op = PLUS;
ragge
1.1
213
214         case MUL:
215         case PLUS:
216         case AND:
217         case OR:
218         case ER:
219                 /* commutative ops; for now, just collect constants */
220                 /* someday, do it right */
ragge
1.14
221                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
222                         SWAPp->n_leftp->n_right );
ragge
1.1
223                 /* make ops tower to the left, not the right */
224                 ifRO(p) == o ){
225                         NODE *t1, *t2, *t3;
ragge
1.3
226                         t1 = p->n_left;
227                         sp = p->n_right;
228                         t2 = sp->n_left;
229                         t3 = sp->n_right;
ragge
1.1
230                         /* now, put together again */
ragge
1.3
231                         p->n_left = sp;
232                         sp->n_left = t1;
233                         sp->n_right = t2;
234                         p->n_right = t3;
ragge
1.1
235                         }
ragge
1.3
236                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
237                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
238                         zapleft:
ragge
1.8
239
240                         q = p->n_left->n_left;
241                         nfree(p->n_left->n_right);
242                         nfree(p->n_left);
243                         p->n_left = q;
ragge
1.1
244                 }
ragge
1.14
245                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
246                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
247                         goto zapleft;
248                         }
ragge
1.10
249                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
250                         zapright:
ragge
1.8
251                         nfree(p->n_right);
ragge
1.14
252                         q = makety(p->n_leftp->n_typep->n_qual,
253                             p->n_dfp->n_sue);
ragge
1.8
254                         nfree(p);
255                         return clocal(q);
ragge
1.10
256                         }
ragge
1.1
257
ragge
1.10
258                 /* change muls to shifts */
ragge
1.1
259
ragge
1.10
260                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
261                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
262                                 goto zapright;
263                                 }
ragge
1.10
264                         o = p->n_op = LS;
265                         p->n_right->n_type = INT;
266                         p->n_right->n_df = NULL;
267                         RV(p) = i;
ragge
1.1
268                         }
269
270                 /* change +'s of negative consts back to - */
ragge
1.3
271                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
272                         RV(p) = -RV(p);
ragge
1.3
273                         o = p->n_op = MINUS;
ragge
1.1
274                         }
275                 break;
276
277         case DIV:
ragge
1.19
278                 ifnnconp->n_right ) && p->n_right->n_lval == 1 )
279                         goto zapright;
280                 if (LCON(p) && RCON(p) && conval(p->n_leftDIVp->n_right))
281                         goto zapright;
ragge
1.25
282                 if (RCON(p) && ISUNSIGNED(p->n_type) && (i=ispow2(RV(p))) > 0) {
283                         p->n_op = RS;
284                         RV(p) = i;
285                         break;
286                 }
287                 break;
288
289         case MOD:
290                 if (RCON(p) && ISUNSIGNED(p->n_type) && ispow2(RV(p)) > 0) {
291                         p->n_op = AND;
292                         RV(p) = RV(p) -1;
293                         break;
294                 }
ragge
1.1
295                 break;
296
297         case EQ:
298         case NE:
299         case LT:
300         case LE:
301         case GT:
302         case GE:
303         case ULT:
304         case ULE:
305         case UGT:
306         case UGE:
307                 if( !LCON(p) ) break;
308
309                 /* exchange operands */
310
ragge
1.3
311                 sp = p->n_left;
312                 p->n_left = p->n_right;
313                 p->n_right = sp;
314                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
315                 break;
316
317                 }
318
319         return(p);
320         }
321
ragge
1.2
322 int
323 ispow2(CONSZ c)
324 {
325         int i;
ragge
1.1
326         ifc <= 0 || (c&(c-1)) ) return(-1);
327         fori=0c>1; ++ic >>= 1;
328         return(i);
ragge
1.2
329 }
ragge
1.10
330
331 int
332 nnconp ) NODE *p; {
333         /* is p a constant without a name */
334         returnp->n_op == ICON && p->n_sp == NULL );
335         }
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-19 23:54 +0200