Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030707145715

Diff

Diff from 1.11 to:

Annotations

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

Annotated File View

ragge
1.11
1 /*      $Id: optim.c,v 1.11 2003/07/07 14:57:15 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.2
50 /*
51  * fortran function arguments
52  */
ragge
1.9
53 static NODE *
ragge
1.2
54 fortarg(NODE *p)
55 {
ragge
1.3
56         ifp->n_op == CM ){
57                 p->n_left = fortargp->n_left );
58                 p->n_right = fortargp->n_right );
ragge
1.1
59                 return(p);
ragge
1.2
60         }
ragge
1.1
61
ragge
1.3
62         whileISPTR(p->n_type) ){
ragge
1.1
63                 p = buildtreeUNARY MULpNIL );
ragge
1.2
64         }
ragge
1.1
65         returnoptim(p) );
ragge
1.2
66 }
ragge
1.1
67
68         /* mapping relationals when the sides are reversed */
69 short revrel[] ={ EQNEGEGTLELTUGEUGTULEULT };
ragge
1.2
70
71 /*
72  * local optimizations, most of which are probably
73  * machine independent
74  */
ragge
1.1
75 NODE *
ragge
1.2
76 optim(NODE *p)
77 {
78         int oty;
ragge
1.8
79         NODE *sp, *q;
ragge
1.1
80         int i;
81         TWORD t;
82
ragge
1.3
83         if( (t=BTYPE(p->n_type))==ENUMTY || t==MOETY ) econvert(p);
ragge
1.1
84         ifoflag ) return(p);
ragge
1.3
85         ty = optypeo=p->n_op);
ragge
1.1
86         ifty == LTYPE ) return(p);
87
ragge
1.3
88         ifty == BITYPE ) p->n_right = optim(p->n_right);
89         p->n_left = optim(p->n_left);
ragge
1.1
90
91         /* collect constants */
92
93         switch(o){
94
95         case SCONV:
96         case PCONV:
97                 returnclocal(p) );
98
99         case FORTCALL:
ragge
1.3
100                 p->n_right = fortargp->n_right );
ragge
1.1
101                 break;
102
103         case UNARY AND:
ragge
1.10
104                 ifLO(p) != NAME ) cerror"& error" );
105
106                 if( !andable(p->n_left) ) return(p);
ragge
1.1
107
108                 LO(p) = ICON;
109
110                 setuleft:
111                 /* paint over the type of the left hand side with the type of the top */
ragge
1.3
112                 p->n_left->n_type = p->n_type;
ragge
1.7
113                 p->n_left->n_df = p->n_df;
ragge
1.5
114                 p->n_left->n_sue = p->n_sue;
ragge
1.8
115                 q = p->n_left;
116                 nfree(p);
117                 return q;
ragge
1.1
118
119         case UNARY MUL:
120                 ifLO(p) != ICON ) break;
121                 LO(p) = NAME;
122                 goto setuleft;
123
124         case MINUS:
ragge
1.3
125                 if( !nncon(p->n_right) ) break;
ragge
1.1
126                 RV(p) = -RV(p);
ragge
1.3
127                 o = p->n_op = PLUS;
ragge
1.1
128
129         case MUL:
130         case PLUS:
131         case AND:
132         case OR:
133         case ER:
134                 /* commutative ops; for now, just collect constants */
135                 /* someday, do it right */
ragge
1.3
136                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) ) SWAPp->n_leftp->n_right );
ragge
1.1
137                 /* make ops tower to the left, not the right */
138                 ifRO(p) == o ){
139                         NODE *t1, *t2, *t3;
ragge
1.3
140                         t1 = p->n_left;
141                         sp = p->n_right;
142                         t2 = sp->n_left;
143                         t3 = sp->n_right;
ragge
1.1
144                         /* now, put together again */
ragge
1.3
145                         p->n_left = sp;
146                         sp->n_left = t1;
147                         sp->n_right = t2;
148                         p->n_right = t3;
ragge
1.1
149                         }
ragge
1.3
150                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
151                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
152                         zapleft:
ragge
1.8
153
154                         q = p->n_left->n_left;
155                         nfree(p->n_left->n_right);
156                         nfree(p->n_left);
157                         p->n_left = q;
ragge
1.1
158                 }
ragge
1.10
159                 ifRCON(p) && LO(p)==o && RCON(p->n_left) && convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
160                         goto zapleft;
161                         }
ragge
1.10
162                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
163                         zapright:
ragge
1.8
164                         nfree(p->n_right);
165                         q = makety(p->n_leftp->n_typep->n_dfp->n_sue);
166                         nfree(p);
167                         return clocal(q);
ragge
1.10
168                         }
ragge
1.1
169
ragge
1.10
170                 /* change muls to shifts */
ragge
1.1
171
ragge
1.10
172                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
173                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
174                                 goto zapright;
175                                 }
ragge
1.10
176                         o = p->n_op = LS;
177                         p->n_right->n_type = INT;
178                         p->n_right->n_df = NULL;
179                         RV(p) = i;
ragge
1.1
180                         }
181
182                 /* change +'s of negative consts back to - */
ragge
1.3
183                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
184                         RV(p) = -RV(p);
ragge
1.3
185                         o = p->n_op = MINUS;
ragge
1.1
186                         }
187                 break;
188
189         case DIV:
ragge
1.10
190                 ifnnconp->n_right ) && p->n_right->n_lval == 1 ) goto zapright;
ragge
1.1
191                 break;
192
193         case EQ:
194         case NE:
195         case LT:
196         case LE:
197         case GT:
198         case GE:
199         case ULT:
200         case ULE:
201         case UGT:
202         case UGE:
203                 if( !LCON(p) ) break;
204
205                 /* exchange operands */
206
ragge
1.3
207                 sp = p->n_left;
208                 p->n_left = p->n_right;
209                 p->n_right = sp;
210                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
211                 break;
212
213                 }
214
215         return(p);
216         }
217
ragge
1.2
218 int
219 ispow2(CONSZ c)
220 {
221         int i;
ragge
1.1
222         ifc <= 0 || (c&(c-1)) ) return(-1);
223         fori=0c>1; ++ic >>= 1;
224         return(i);
ragge
1.2
225 }
ragge
1.10
226
227 int
228 nnconp ) NODE *p; {
229         /* is p a constant without a name */
230         returnp->n_op == ICON && p->n_sp == NULL );
231         }
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-21 12:06 +0200