Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030912143253

Diff

Diff from 1.15 to:

Annotations

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

Annotated File View

ragge
1.15
1 /*      $Id: optim.c,v 1.15 2003/09/12 14:32:53 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.12
85         ty = coptype(o = 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
ragge
1.15
103         case ADDROF:
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.14
136                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
137                         SWAPp->n_leftp->n_right );
ragge
1.1
138                 /* make ops tower to the left, not the right */
139                 ifRO(p) == o ){
140                         NODE *t1, *t2, *t3;
ragge
1.3
141                         t1 = p->n_left;
142                         sp = p->n_right;
143                         t2 = sp->n_left;
144                         t3 = sp->n_right;
ragge
1.1
145                         /* now, put together again */
ragge
1.3
146                         p->n_left = sp;
147                         sp->n_left = t1;
148                         sp->n_right = t2;
149                         p->n_right = t3;
ragge
1.1
150                         }
ragge
1.3
151                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
152                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
153                         zapleft:
ragge
1.8
154
155                         q = p->n_left->n_left;
156                         nfree(p->n_left->n_right);
157                         nfree(p->n_left);
158                         p->n_left = q;
ragge
1.1
159                 }
ragge
1.14
160                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
161                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
162                         goto zapleft;
163                         }
ragge
1.10
164                 else ifLCON(p) && RCON(p) && convalp->n_leftop->n_right ) ){
ragge
1.1
165                         zapright:
ragge
1.8
166                         nfree(p->n_right);
ragge
1.14
167                         q = makety(p->n_leftp->n_typep->n_qual,
168                             p->n_dfp->n_sue);
ragge
1.8
169                         nfree(p);
170                         return clocal(q);
ragge
1.10
171                         }
ragge
1.1
172
ragge
1.10
173                 /* change muls to shifts */
ragge
1.1
174
ragge
1.10
175                 ifo == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
176                         ifi == 0 ) { /* multiplication by 1 */
ragge
1.1
177                                 goto zapright;
178                                 }
ragge
1.10
179                         o = p->n_op = LS;
180                         p->n_right->n_type = INT;
181                         p->n_right->n_df = NULL;
182                         RV(p) = i;
ragge
1.1
183                         }
184
185                 /* change +'s of negative consts back to - */
ragge
1.3
186                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
187                         RV(p) = -RV(p);
ragge
1.3
188                         o = p->n_op = MINUS;
ragge
1.1
189                         }
190                 break;
191
192         case DIV:
ragge
1.10
193                 ifnnconp->n_right ) && p->n_right->n_lval == 1 ) goto zapright;
ragge
1.1
194                 break;
195
196         case EQ:
197         case NE:
198         case LT:
199         case LE:
200         case GT:
201         case GE:
202         case ULT:
203         case ULE:
204         case UGT:
205         case UGE:
206                 if( !LCON(p) ) break;
207
208                 /* exchange operands */
209
ragge
1.3
210                 sp = p->n_left;
211                 p->n_left = p->n_right;
212                 p->n_right = sp;
213                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
214                 break;
215
216                 }
217
218         return(p);
219         }
220
ragge
1.2
221 int
222 ispow2(CONSZ c)
223 {
224         int i;
ragge
1.1
225         ifc <= 0 || (c&(c-1)) ) return(-1);
226         fori=0c>1; ++ic >>= 1;
227         return(i);
ragge
1.2
228 }
ragge
1.10
229
230 int
231 nnconp ) NODE *p; {
232         /* is p a constant without a name */
233         returnp->n_op == ICON && p->n_sp == NULL );
234         }
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-23 06:22 +0200