Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20050302163716

Diff

Diff from 1.22 to:

Annotations

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

Annotated File View

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