Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20030604193605

Diff

Diff from 1.4 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;
82                 p->n_left->n_csiz = p->n_csiz;
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.3
132                         p->n_left = makety(p->n_leftp->n_typep->n_cdimp->n_csiz );
133                         p->n_op = FREE;
134                         returnclocalp->n_left ) );
ragge
1.1
135                         }
136                 /* FALL THROUGH */
137
138         case ASG MUL:
139                 /* change muls to adds or shifts */
140
141                 if( (o == MUL || o == ASG MUL) &&
ragge
1.3
142                     nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
ragge
1.1
143                         ifi == 0 ) /* multiplication by 1 */
144                                 goto zapright;
145                         /* c2 will turn 'i << 1' into 'i + i' for us */
146                         else {
ragge
1.3
147                                 p->n_op = (asgop(o) ? ASG LS : LS);
148                                 o = p->n_op;
149                                 p->n_right->n_type = p->n_right->n_csiz = INT;
ragge
1.1
150                                 RV(p) = i;
151                                 }
152                         }
153
154                 /* change +'s of negative consts back to - */
ragge
1.3
155                 ifo==PLUS && nncon(p->n_right) && RV(p)<0 ){
ragge
1.1
156                         RV(p) = -RV(p);
ragge
1.3
157                         o = p->n_op = MINUS;
ragge
1.1
158                         }
159                 /* FALL THROUGH */
160         case ASG AND:
161         case ASG PLUS:
162         case ASG MINUS:
163         case RS:
164         case LS:
165                 /* Operations with zero */
ragge
1.3
166                 ifnncon(p->n_right) && RV(p) == 0 ) {
ragge
1.1
167                         ifo == MUL || o == ASG MUL ||
168                             o == AND || o == ASG AND ) {
169                                 ifasgop(o) )
ragge
1.3
170                                         p->n_op = ASSIGN;
ragge
1.1
171                                 else ifoptype(LO(p)) == LTYPE ) {
ragge
1.3
172                                         p->n_op = FREE;
ragge
1.1
173                                         LO(p) = FREE;
ragge
1.3
174                                         p = p->n_right;
ragge
1.1
175                                         }
176                                 else
ragge
1.3
177                                         p->n_op = COMOP/* side effects */
ragge
1.1
178                                 }
179                         else ifo == PLUS || o == MINUS ||
180                                  o == ASG PLUS || o == ASG MINUS ||
181                                  o == OR || o == ER ||
182                                  o == LS || o == RS )
183                                 goto zapright;
184                         }
185                 ifo != LS && o != RS )
186                         break;
187                 /* FALL THROUGH */
188
189         case ASG RS:
190         case ASG LS:
ragge
1.3
191                 if( !ISUNSIGNED(p->n_left->n_type) )
ragge
1.1
192                         break;
ragge
1.3
193                 ifp->n_right->n_op == ICON &&
194                     p->n_right->n_lval < 0 ) {
ragge
1.1
195                         /*
196                          * Technically negative shifts are illegal
197                          * but we can support them, at least with
198                          * constants; you take potluck with variables.
199                          */
ragge
1.3
200                         p->n_right->n_lval = -p->n_right->n_lval;
ragge
1.1
201                         switcho ){
ragge
1.3
202                         case LS:        p->n_op = RSbreak;
203                         case ASG LS:    p->n_op = ASG RSbreak;
204                         case RS:        p->n_op = LSbreak;
205                         case ASG RS:    p->n_op = ASG LSbreak;
ragge
1.1
206                                 }
207                         }
208                 break;
209
210         case ASG DIV:
211         case DIV:
ragge
1.3
212                 ifnnconp->n_right ) ){
ragge
1.1
213                         ifRV(p) == 0 ) uerror("division by zero");
214                         else ifRV(p) == 1 ) goto zapright;
215                         /* Unsigned division by a power of two */
216                         else if( (i=ispow2(RV(p)))>=0 &&
ragge
1.3
217                                  (ISUNSIGNED(p->n_left->n_type) ||
218                                   ISUNSIGNED(p->n_right->n_type)) ){
219                                 p->n_op = (asgop(o) ? ASG RS : RS);
220                                 p->n_right->n_type = p->n_right->n_csiz = INT;
ragge
1.1
221                                 RV(p) = i;
222                                 }
223                         }
224                 break;
225
226         case ASG MOD:
227         case MOD:
ragge
1.3
228                 ifnncon(p->n_right) ){
ragge
1.1
229                         ifRV(p) == 0 ) uerror("modulus of zero");
230                         else ifRV(p) == 1 ){ /* mod by one gives zero */
231                                 RV(p) = 0;
232                                 ifasgop(o) )
ragge
1.3
233                                         p->n_op = ASSIGN;
ragge
1.1
234                                 else ifoptype(LO(p)) == LTYPE ) {
ragge
1.3
235                                         p->n_op = FREE;
ragge
1.1
236                                         LO(p) = FREE;
ragge
1.3
237                                         p = p->n_right;
ragge
1.1
238                                         }
239                                 else
ragge
1.3
240                                         p->n_op = COMOP/* side effects */
ragge
1.1
241                                 }
242                         else if ((i=ispow2(RV(p)))>=0 &&
ragge
1.3
243                                  (ISUNSIGNED(p->n_left->n_type) ||
244                                   ISUNSIGNED(p->n_right->n_type)) ){
ragge
1.1
245                                 /* Unsigned mod by a power of two */
ragge
1.3
246                                 p->n_op = (asgop(o) ? ASG AND : AND);
ragge
1.1
247                                 RV(p)--;
248                                 }
249                         }
250                 break;
251
252         case EQ:
253         case NE:
254         case LT:
255         case LE:
256         case GT:
257         case GE:
258         case ULT:
259         case ULE:
260         case UGT:
261         case UGE:
262                 if( !LCON(p) ) break;
263
264                 /* exchange operands */
265
ragge
1.3
266                 sp = p->n_left;
267                 p->n_left = p->n_right;
268                 p->n_right = sp;
269                 p->n_op = revrel[p->n_op - EQ ];
ragge
1.1
270                 break;
271
272                 }
273
274         return(p);
275         }
276
ragge
1.2
277 int
278 ispow2(CONSZ c)
279 {
280         int i;
ragge
1.1
281         ifc <= 0 || (c&(c-1)) ) return(-1);
282         fori=0c>1; ++ic >>= 1;
283         return(i);
ragge
1.2
284 }
FishEye: Open Source License registered to PCC.
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-12-21 00:44 +0100