Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050227083110

Diff

Diff from 1.21 to:

Annotations

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

Annotated File View

ragge
1.21
1 /*      $Id: optim.c,v 1.21 2005/02/27 08:31: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.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);
144                 } else if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
145                         RV(p) -= RV(p->n_left);
146                         if (RV(p) < 0)
147                                 o = p->n_op = LSRV(p) = -RV(p);
148                         p->n_left = zapleft(p->n_left);
149                 }
150                 if (RV(p) < 0) {
151                         RV(p) = -RV(p);
152                         p->n_op = LS;
153                         goto again;
154                 }
155                 if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue) &&
156                     ISUNSIGNED(p->n_type)) { /* ignore signed right shifts */
157                         /* too many shifts */
158                         tfree(p->n_left);
159                         nfree(p->n_right);
160                         p->n_op = ICONp->n_lval = 0p->n_sp = NULL;
161                 } else if (RV(p) == 0)
162                         p = zapleft(p);
163                 break;
164
165         case LS:
166                 if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
167                         /* two left-shift  by constants */
168                         RV(p) += RV(p->n_left);
169                         p->n_left = zapleft(p->n_left);
170                 } else if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
171                         RV(p) -= RV(p->n_left);
172                         p->n_left = zapleft(p->n_left);
173                 }
174                 if (RV(p) < 0) {
175                         RV(p) = -RV(p);
176                         p->n_op = RS;
177                         goto again;
178                 }
179                 if (RV(p) >= tsize(p->n_typep->n_dfp->n_sue)) {
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;
184                 } else if (RV(p) == 0)  
185                         p = zapleft(p);
186                 break;
187
ragge
1.1
188         case MINUS:
ragge
1.19
189                 if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
190                         /* link-time constants, but both are the same */
191                         /* solve it now by forgetting the symbols */
192                         p->n_left->n_sp = p->n_right->n_sp = NULL;
193                 }
ragge
1.3
194                 if( !nncon(p->n_right) ) break;
ragge
1.1
195                 RV(p) = -RV(p);
ragge
1.3
196                 o = p->n_op = PLUS;
ragge
1.1
197
198         case MUL:
199         case PLUS:
200         case AND:
201         case OR:
202         case ER:
203                 /* commutative ops; for now, just collect constants */
204                 /* someday, do it right */
ragge
1.14
205                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
206                         SWAPp->n_leftp->n_right );
ragge
1.1
207                 /* make ops tower to the left, not the right */
208                 ifRO(p) == o ){
209                         NODE *t1, *t2, *t3;
ragge
1.3
210                         t1 = p->n_left;
211                         sp = p->n_right;
212                         t2 = sp->n_left;
213                         t3 = sp->n_right;
ragge
1.1
214                         /* now, put together again */
ragge
1.3
215                         p->n_left = sp;
216                         sp->n_left = t1;
217                         sp->n_right = t2;
218                         p->n_right = t3;
ragge
1.1
219                         }
ragge
1.3
220                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
221                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
222                         zapleft:
ragge
1.8
223
224                         q = p->n_left->n_left;
225                         nfree(p->n_left->n_right);
226                         nfree(p->n_left);
227                         p->n_left = q;
ragge
1.1
228                 }
ragge
1.14
229                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
230                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
231                         goto zapleft;
232                         }
ragge
1.10
233                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
234                         zapright:
ragge
1.8
235                         nfree(p->n_right);
ragge
1.14
236                         q = makety(p->n_leftp->n_typep->n_qual,
237                             p->n_dfp->n_sue);
ragge
1.8
238                         nfree(p);
239                         return clocal(q);
ragge
1.10
240                         }
ragge
1.1
241
ragge
1.10
242                 /* change muls to shifts */
ragge
1.1
243
ragge
1.10
244                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
245                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
246                                 goto zapright;
247                                 }
ragge
1.10
248                         o = p->n_op = LS;
249                         p->n_right->n_type = INT;
250                         p->n_right->n_df = NULL;
251                         RV(p) = i;
ragge
1.1
252                         }
253
254                 /* change +'s of negative consts back to - */
ragge
1.3
255                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
256                         RV(p) = -RV(p);
ragge
1.3
257                         o = p->n_op = MINUS;
ragge
1.1
258                         }
259                 break;
260
261         case DIV:
ragge
1.19
262                 ifnnconp->n_right ) && p->n_right->n_lval == 1 )
263                         goto zapright;
264                 if (LCON(p) && RCON(p) && conval(p->n_leftDIVp->n_right))
265                         goto zapright;
ragge
1.1
266                 break;
267
268         case EQ:
269         case NE:
270         case LT:
271         case LE:
272         case GT:
273         case GE:
274         case ULT:
275         case ULE:
276         case UGT:
277         case UGE:
278                 if( !LCON(p) ) break;
279
280                 /* exchange operands */
281
ragge
1.3
282                 sp = p->n_left;
283                 p->n_left = p->n_right;
284                 p->n_right = sp;
285                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
286                 break;
287
288                 }
289
290         return(p);
291         }
292
ragge
1.2
293 int
294 ispow2(CONSZ c)
295 {
296         int i;
ragge
1.1
297         ifc <= 0 || (c&(c-1)) ) return(-1);
298         fori=0c>1; ++ic >>= 1;
299         return(i);
ragge
1.2
300 }
ragge
1.10
301
302 int
303 nnconp ) NODE *p; {
304         /* is p a constant without a name */
305         returnp->n_op == ICON && p->n_sp == NULL );
306         }
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 10:48 +0100