Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030707142340

Diff

Diff from 1.9 to:

Annotations

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

Annotated File View

ragge
1.2
1 #if 0
ragge
1.1
2 static char *sccsid ="@(#)optim.c       4.7 (Berkeley) 1/8/86";
ragge
1.2
3 #endif
ragge
1.1
4
5 # include "pass1.h"
6
7 # define SWAP(p,q) {sp=p; p=q; q=sp;}
ragge
1.3
8 # define RCON(p) (p->n_right->n_op==ICON)
9 # define RO(p) p->n_right->n_op
10 # define RV(p) p->n_right->n_lval
11 # define LCON(p) (p->n_left->n_op==ICON)
12 # define LO(p) p->n_left->n_op
13 # define LV(p) p->n_left->n_lval
ragge
1.1
14
15         /* is p a constant without a name */
ragge
1.4
16 # define nncon(p)       ((p)->n_op == ICON && (p)->n_sp == NULL)
ragge
1.1
17
18 int oflag = 0;
19
ragge
1.2
20 /*
21  * fortran function arguments
22  */
ragge
1.9
23 static NODE *
ragge
1.2
24 fortarg(NODE *p)
25 {
ragge
1.3
26         ifp->n_op == CM ){
27                 p->n_left = fortargp->n_left );
28                 p->n_right = fortargp->n_right );
ragge
1.1
29                 return(p);
ragge
1.2
30         }
ragge
1.1
31
ragge
1.3
32         whileISPTR(p->n_type) ){
ragge
1.1
33                 p = buildtreeUNARY MULpNIL );
ragge
1.2
34         }
ragge
1.1
35         returnoptim(p) );
ragge
1.2
36 }
ragge
1.1
37
38         /* mapping relationals when the sides are reversed */
39 short revrel[] ={ EQNEGEGTLELTUGEUGTULEULT };
ragge
1.2
40
41 /*
42  * local optimizations, most of which are probably
43  * machine independent
44  */
ragge
1.1
45 NODE *
ragge
1.2
46 optim(NODE *p)
47 {
48         int oty;
ragge
1.8
49         NODE *sp, *q;
ragge
1.1
50         int i;
51         TWORD t;
52
ragge
1.3
53         if( (t=BTYPE(p->n_type))==ENUMTY || t==MOETY ) econvert(p);
ragge
1.1
54         ifoflag ) return(p);
ragge
1.3
55         ty = optypeo=p->n_op);
ragge
1.1
56         ifty == LTYPE ) return(p);
57
ragge
1.3
58         ifty == BITYPE ) p->n_right = optim(p->n_right);
59         p->n_left = optim(p->n_left);
ragge
1.1
60
61         /* collect constants */
62
63         switch(o){
64
65         case SCONV:
66         case PCONV:
67                 returnclocal(p) );
68
69         case FORTCALL:
ragge
1.3
70                 p->n_right = fortargp->n_right );
ragge
1.1
71                 break;
72
73         case UNARY AND:
ragge
1.3
74                 ifLO(p) != NAME || !andable(p->n_left) ) return(p);
ragge
1.1
75
76                 LO(p) = ICON;
77
78                 setuleft:
79                 /* paint over the type of the left hand side with the type of the top */
ragge
1.3
80                 p->n_left->n_type = p->n_type;
ragge
1.7
81                 p->n_left->n_df = p->n_df;
ragge
1.5
82                 p->n_left->n_sue = p->n_sue;
ragge
1.8
83                 q = p->n_left;
84                 nfree(p);
85                 return q;
ragge
1.1
86
87         case UNARY MUL:
88                 ifLO(p) != ICON ) break;
89                 LO(p) = NAME;
90                 goto setuleft;
91
92         case MINUS:
ragge
1.3
93                 if( !nncon(p->n_right) ) break;
ragge
1.1
94                 RV(p) = -RV(p);
ragge
1.3
95                 o = p->n_op = PLUS;
ragge
1.1
96
97         case MUL:
98         case PLUS:
99         case AND:
100         case OR:
101         case ER:
102                 /* commutative ops; for now, just collect constants */
103                 /* someday, do it right */
ragge
1.3
104                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) ) SWAPp->n_leftp->n_right );
ragge
1.1
105                 /* make ops tower to the left, not the right */
106                 ifRO(p) == o ){
107                         NODE *t1, *t2, *t3;
ragge
1.3
108                         t1 = p->n_left;
109                         sp = p->n_right;
110                         t2 = sp->n_left;
111                         t3 = sp->n_right;
ragge
1.1
112                         /* now, put together again */
ragge
1.3
113                         p->n_left = sp;
114                         sp->n_left = t1;
115                         sp->n_right = t2;
116                         p->n_right = t3;
ragge
1.1
117                         }
ragge
1.3
118                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
119                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
120                         zapleft:
ragge
1.8
121
122                         q = p->n_left->n_left;
123                         nfree(p->n_left->n_right);
124                         nfree(p->n_left);
125                         p->n_left = q;
ragge
1.1
126                 }
ragge
1.3
127                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
128                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
129                         goto zapleft;
130                         }
131                 else ifLCON(p) && RCON(p) &&
ragge
1.3
132                          convalp->n_leftop->n_right ) ){
ragge
1.1
133                         zapright:
ragge
1.8
134                         nfree(p->n_right);
135                         q = makety(p->n_leftp->n_typep->n_dfp->n_sue);
136                         nfree(p);
137                         return clocal(q);
138                 }
ragge
1.1
139                 /* FALL THROUGH */
140
141         case ASG MUL:
142                 /* change muls to adds or shifts */
143
144                 if( (o == MUL || o == ASG MUL) &&
ragge
1.3
145                     nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
ragge
1.1
146                         ifi == 0 ) /* multiplication by 1 */
147                                 goto zapright;
148                         /* c2 will turn 'i << 1' into 'i + i' for us */
149                         else {
ragge
1.3
150                                 p->n_op = (asgop(o) ? ASG LS : LS);
151                                 o = p->n_op;
ragge
1.5
152                                 p->n_right->n_type = INT;
153                                 p->n_right->n_sue = MKSUE(INT);
ragge
1.1
154                                 RV(p) = i;
155                                 }
156                         }
157
158                 /* change +'s of negative consts back to - */
ragge
1.3
159                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
160                         RV(p) = -RV(p);
ragge
1.3
161                         o = p->n_op = MINUS;
ragge
1.1
162                         }
163                 /* FALL THROUGH */
164         case ASG AND:
165         case ASG PLUS:
166         case ASG MINUS:
167         case RS:
168         case LS:
169                 /* Operations with zero */
ragge
1.3
170                 ifnncon(p->n_right) && RV(p) == 0 ) {
ragge
1.1
171                         ifo == MUL || o == ASG MUL ||
172                             o == AND || o == ASG AND ) {
173                                 ifasgop(o) )
ragge
1.3
174                                         p->n_op = ASSIGN;
ragge
1.1
175                                 else ifoptype(LO(p)) == LTYPE ) {
ragge
1.8
176                                         q = p->n_right;
177                                         nfree(p);
178                                         nfree(p->n_left);
179                                         p = q;
180                                 } else
ragge
1.3
181                                         p->n_op = COMOP/* side effects */
ragge
1.8
182                         } else ifo == PLUS || o == MINUS ||
ragge
1.1
183                                  o == ASG PLUS || o == ASG MINUS ||
184                                  o == OR || o == ER ||
185                                  o == LS || o == RS )
186                                 goto zapright;
ragge
1.8
187                 }
ragge
1.1
188                 ifo != LS && o != RS )
189                         break;
190                 /* FALL THROUGH */
191
192         case ASG RS:
193         case ASG LS:
ragge
1.3
194                 if( !ISUNSIGNED(p->n_left->n_type) )
ragge
1.1
195                         break;
ragge
1.3
196                 ifp->n_right->n_op == ICON &&
197                     p->n_right->n_lval < 0 ) {
ragge
1.1
198                         /*
199                          * Technically negative shifts are illegal
200                          * but we can support them, at least with
201                          * constants; you take potluck with variables.
202                          */
ragge
1.3
203                         p->n_right->n_lval = -p->n_right->n_lval;
ragge
1.1
204                         switcho ){
ragge
1.3
205                         case LS:        p->n_op = RSbreak;
206                         case ASG LS:    p->n_op = ASG RSbreak;
207                         case RS:        p->n_op = LSbreak;
208                         case ASG RS:    p->n_op = ASG LSbreak;
ragge
1.1
209                                 }
210                         }
211                 break;
212
213         case ASG DIV:
214         case DIV:
ragge
1.3
215                 ifnnconp->n_right ) ){
ragge
1.1
216                         ifRV(p) == 0 ) uerror("division by zero");
217                         else ifRV(p) == 1 ) goto zapright;
218                         /* Unsigned division by a power of two */
219                         else if( (i=ispow2(RV(p)))>=0 &&
ragge
1.3
220                                  (ISUNSIGNED(p->n_left->n_type) ||
221                                   ISUNSIGNED(p->n_right->n_type)) ){
222                                 p->n_op = (asgop(o) ? ASG RS : RS);
ragge
1.5
223                                 p->n_right->n_type = INT;
ragge
1.6
224                                 p->n_right->n_sue = MKSUE(INT);
ragge
1.1
225                                 RV(p) = i;
226                                 }
227                         }
228                 break;
229
230         case ASG MOD:
231         case MOD:
ragge
1.3
232                 ifnncon(p->n_right) ){
ragge
1.1
233                         ifRV(p) == 0 ) uerror("modulus of zero");
234                         else ifRV(p) == 1 ){ /* mod by one gives zero */
235                                 RV(p) = 0;
236                                 ifasgop(o) )
ragge
1.3
237                                         p->n_op = ASSIGN;
ragge
1.1
238                                 else ifoptype(LO(p)) == LTYPE ) {
ragge
1.8
239                                         q = p->n_right;
240                                         nfree(p);
241                                         nfree(p->n_left);
242                                         p = q;
243                                 } else
ragge
1.3
244                                         p->n_op = COMOP/* side effects */
ragge
1.8
245                         } else if ((i=ispow2(RV(p)))>=0 &&
ragge
1.3
246                                  (ISUNSIGNED(p->n_left->n_type) ||
247                                   ISUNSIGNED(p->n_right->n_type)) ){
ragge
1.1
248                                 /* Unsigned mod by a power of two */
ragge
1.3
249                                 p->n_op = (asgop(o) ? ASG AND : AND);
ragge
1.1
250                                 RV(p)--;
251                         }
ragge
1.8
252                 }
ragge
1.1
253                 break;
254
255         case EQ:
256         case NE:
257         case LT:
258         case LE:
259         case GT:
260         case GE:
261         case ULT:
262         case ULE:
263         case UGT:
264         case UGE:
265                 if( !LCON(p) ) break;
266
267                 /* exchange operands */
268
ragge
1.3
269                 sp = p->n_left;
270                 p->n_left = p->n_right;
271                 p->n_right = sp;
272                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
273                 break;
274
275                 }
276
277         return(p);
278         }
279
ragge
1.2
280 int
281 ispow2(CONSZ c)
282 {
283         int i;
ragge
1.1
284         ifc <= 0 || (c&(c-1)) ) return(-1);
285         fori=0c>1; ++ic >>= 1;
286         return(i);
ragge
1.2
287 }
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-10-31 09:28 +0100