Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030609155556

Diff

Diff from 1.5 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.1
23 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.1
49         NODE *sp;
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;
81                 p->n_left->n_cdim = p->n_cdim;
ragge
1.5
82                 p->n_left->n_sue = p->n_sue;
ragge
1.3
83                 p->n_op = FREE;
84                 returnp->n_left );
ragge
1.1
85
86         case UNARY MUL:
87                 ifLO(p) != ICON ) break;
88                 LO(p) = NAME;
89                 goto setuleft;
90
91         case MINUS:
ragge
1.3
92                 if( !nncon(p->n_right) ) break;
ragge
1.1
93                 RV(p) = -RV(p);
ragge
1.3
94                 o = p->n_op = PLUS;
ragge
1.1
95
96         case MUL:
97         case PLUS:
98         case AND:
99         case OR:
100         case ER:
101                 /* commutative ops; for now, just collect constants */
102                 /* someday, do it right */
ragge
1.3
103                 ifnncon(p->n_left) || ( LCON(p) && !RCON(p) ) ) SWAPp->n_leftp->n_right );
ragge
1.1
104                 /* make ops tower to the left, not the right */
105                 ifRO(p) == o ){
106                         NODE *t1, *t2, *t3;
ragge
1.3
107                         t1 = p->n_left;
108                         sp = p->n_right;
109                         t2 = sp->n_left;
110                         t3 = sp->n_right;
ragge
1.1
111                         /* now, put together again */
ragge
1.3
112                         p->n_left = sp;
113                         sp->n_left = t1;
114                         sp->n_right = t2;
115                         p->n_right = t3;
ragge
1.1
116                         }
ragge
1.3
117                 if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
118                    conval(p->n_rightMINUSp->n_left->n_right)){
ragge
1.1
119                         zapleft:
ragge
1.3
120                         RO(p->n_left) = FREE;
ragge
1.1
121                         LO(p) = FREE;
ragge
1.3
122                         p->n_left = p->n_left->n_left;
ragge
1.1
123                 }
ragge
1.3
124                 ifRCON(p) && LO(p)==o && RCON(p->n_left) &&
125                     convalp->n_rightop->n_left->n_right ) ){
ragge
1.1
126                         goto zapleft;
127                         }
128                 else ifLCON(p) && RCON(p) &&
ragge
1.3
129                          convalp->n_leftop->n_right ) ){
ragge
1.1
130                         zapright:
131                         RO(p) = FREE;
ragge
1.5
132                         p->n_left = makety(p->n_leftp->n_type,
133                             p->n_cdimp->n_sue);
ragge
1.3
134                         p->n_op = FREE;
135                         returnclocalp->n_left ) );
ragge
1.1
136                         }
137                 /* FALL THROUGH */
138
139         case ASG MUL:
140                 /* change muls to adds or shifts */
141
142                 if( (o == MUL || o == ASG MUL) &&
ragge
1.3
143                     nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
ragge
1.1
144                         ifi == 0 ) /* multiplication by 1 */
145                                 goto zapright;
146                         /* c2 will turn 'i << 1' into 'i + i' for us */
147                         else {
ragge
1.3
148                                 p->n_op = (asgop(o) ? ASG LS : LS);
149                                 o = p->n_op;
ragge
1.5
150                                 p->n_right->n_type = INT;
151                                 p->n_right->n_sue = MKSUE(INT);
ragge
1.1
152                                 RV(p) = i;
153                                 }
154                         }
155
156                 /* change +'s of negative consts back to - */
ragge
1.3
157                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
158                         RV(p) = -RV(p);
ragge
1.3
159                         o = p->n_op = MINUS;
ragge
1.1
160                         }
161                 /* FALL THROUGH */
162         case ASG AND:
163         case ASG PLUS:
164         case ASG MINUS:
165         case RS:
166         case LS:
167                 /* Operations with zero */
ragge
1.3
168                 ifnncon(p->n_right) && RV(p) == 0 ) {
ragge
1.1
169                         ifo == MUL || o == ASG MUL ||
170                             o == AND || o == ASG AND ) {
171                                 ifasgop(o) )
ragge
1.3
172                                         p->n_op = ASSIGN;
ragge
1.1
173                                 else ifoptype(LO(p)) == LTYPE ) {
ragge
1.3
174                                         p->n_op = FREE;
ragge
1.1
175                                         LO(p) = FREE;
ragge
1.3
176                                         p = p->n_right;
ragge
1.1
177                                         }
178                                 else
ragge
1.3
179                                         p->n_op = COMOP/* side effects */
ragge
1.1
180                                 }
181                         else ifo == PLUS || o == MINUS ||
182                                  o == ASG PLUS || o == ASG MINUS ||
183                                  o == OR || o == ER ||
184                                  o == LS || o == RS )
185                                 goto zapright;
186                         }
187                 ifo != LS && o != RS )
188                         break;
189                 /* FALL THROUGH */
190
191         case ASG RS:
192         case ASG LS:
ragge
1.3
193                 if( !ISUNSIGNED(p->n_left->n_type) )
ragge
1.1
194                         break;
ragge
1.3
195                 ifp->n_right->n_op == ICON &&
196                     p->n_right->n_lval < 0 ) {
ragge
1.1
197                         /*
198                          * Technically negative shifts are illegal
199                          * but we can support them, at least with
200                          * constants; you take potluck with variables.
201                          */
ragge
1.3
202                         p->n_right->n_lval = -p->n_right->n_lval;
ragge
1.1
203                         switcho ){
ragge
1.3
204                         case LS:        p->n_op = RSbreak;
205                         case ASG LS:    p->n_op = ASG RSbreak;
206                         case RS:        p->n_op = LSbreak;
207                         case ASG RS:    p->n_op = ASG LSbreak;
ragge
1.1
208                                 }
209                         }
210                 break;
211
212         case ASG DIV:
213         case DIV:
ragge
1.3
214                 ifnnconp->n_right ) ){
ragge
1.1
215                         ifRV(p) == 0 ) uerror("division by zero");
216                         else ifRV(p) == 1 ) goto zapright;
217                         /* Unsigned division by a power of two */
218                         else if( (i=ispow2(RV(p)))>=0 &&
ragge
1.3
219                                  (ISUNSIGNED(p->n_left->n_type) ||
220                                   ISUNSIGNED(p->n_right->n_type)) ){
221                                 p->n_op = (asgop(o) ? ASG RS : RS);
ragge
1.5
222                                 p->n_right->n_type = INT;
223                                 p->n_right->n_sue = (struct suedef *)&dimtab[INT];
ragge
1.1
224                                 RV(p) = i;
225                                 }
226                         }
227                 break;
228
229         case ASG MOD:
230         case MOD:
ragge
1.3
231                 ifnncon(p->n_right) ){
ragge
1.1
232                         ifRV(p) == 0 ) uerror("modulus of zero");
233                         else ifRV(p) == 1 ){ /* mod by one gives zero */
234                                 RV(p) = 0;
235                                 ifasgop(o) )
ragge
1.3
236                                         p->n_op = ASSIGN;
ragge
1.1
237                                 else ifoptype(LO(p)) == LTYPE ) {
ragge
1.3
238                                         p->n_op = FREE;
ragge
1.1
239                                         LO(p) = FREE;
ragge
1.3
240                                         p = p->n_right;
ragge
1.1
241                                         }
242                                 else
ragge
1.3
243                                         p->n_op = COMOP/* side effects */
ragge
1.1
244                                 }
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                                 }
252                         }
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-08-23 15:24 +0200