1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/* $OpenBSD: optim.c,v 1.3 2008/04/11 20:45:52 stefan Exp $ */
/*
* Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code and documentation must retain the above
* copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditionsand the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed or owned by Caldera
* International, Inc.
* Neither the name of Caldera International, Inc. nor the names of other
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
* INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
* FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
# include "pass1.h"
# define SWAP(p,q) {sp=p; p=q; q=sp;}
# define RCON(p) (p->n_right->n_op==ICON)
# define RO(p) p->n_right->n_op
# define RV(p) p->n_right->n_lval
# define LCON(p) (p->n_left->n_op==ICON)
# define LO(p) p->n_left->n_op
# define LV(p) p->n_left->n_lval
int oflag = 0;
/* remove left node */
static NODE *
zapleft(NODE *p)
{
NODE *q;
q = p->n_left;
nfree(p->n_right);
nfree(p);
return q;
}
/*
* fortran function arguments
*/
static NODE *
fortarg(NODE *p)
{
if( p->n_op == CM ){
p->n_left = fortarg( p->n_left );
p->n_right = fortarg( p->n_right );
return(p);
}
while( ISPTR(p->n_type) ){
p = buildtree( UMUL, p, NIL );
}
return( optim(p) );
}
/* mapping relationals when the sides are reversed */
short revrel[] ={ EQ, NE, GE, GT, LE, LT, UGE, UGT, ULE, ULT };
/*
* local optimizations, most of which are probably
* machine independent
*/
NODE *
optim(NODE *p)
{
int o, ty;
NODE *sp, *q;
int i;
TWORD t;
t = BTYPE(p->n_type);
if( oflag ) return(p);
ty = coptype(p->n_op);
if( ty == LTYPE ) return(p);
if( ty == BITYPE ) p->n_right = optim(p->n_right);
p->n_left = optim(p->n_left);
/* collect constants */
again: o = p->n_op;
switch(o){
case SCONV:
case PCONV:
return( clocal(p) );
case FORTCALL:
p->n_right = fortarg( p->n_right );
break;
case ADDROF:
if (LO(p) == TEMP)
return p;
if( LO(p) != NAME ) cerror( "& error" );
if( !andable(p->n_left) ) return(p);
LO(p) = ICON;
setuleft:
/* paint over the type of the left hand side with the type of the top */
p->n_left->n_type = p->n_type;
p->n_left->n_df = p->n_df;
p->n_left->n_sue = p->n_sue;
q = p->n_left;
nfree(p);
return q;
case UMUL:
if( LO(p) != ICON ) break;
LO(p) = NAME;
goto setuleft;
case RS:
if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
/* two right-shift by constants */
RV(p) += RV(p->n_left);
p->n_left = zapleft(p->n_left);
}
#if 0
else if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
RV(p) -= RV(p->n_left);
if (RV(p) < 0)
o = p->n_op = LS, RV(p) = -RV(p);
p->n_left = zapleft(p->n_left);
}
#endif
if (RO(p) == ICON) {
if (RV(p) < 0) {
RV(p) = -RV(p);
p->n_op = LS;
goto again;
}
#ifdef notyet /* must check for side effects, --a >> 32; */
if (RV(p) >= tsize(p->n_type, p->n_df, p->n_sue) &&
ISUNSIGNED(p->n_type)) { /* ignore signed shifts */
/* too many shifts */
tfree(p->n_left);
nfree(p->n_right);
p->n_op = ICON; p->n_lval = 0; p->n_sp = NULL;
} else
#endif
/* avoid larger shifts than type size */
if (RV(p) >= p->n_sue->suesize) {
RV(p) = RV(p) % p->n_sue->suesize;
werror("shift larger than type");
}
if (RV(p) == 0)
p = zapleft(p);
}
break;
case LS:
if (LO(p) == LS && RCON(p->n_left) && RCON(p)) {
/* two left-shift by constants */
RV(p) += RV(p->n_left);
p->n_left = zapleft(p->n_left);
}
#if 0
else if (LO(p) == RS && RCON(p->n_left) && RCON(p)) {
RV(p) -= RV(p->n_left);
p->n_left = zapleft(p->n_left);
}
#endif
if (RO(p) == ICON) {
if (RV(p) < 0) {
RV(p) = -RV(p);
p->n_op = RS;
goto again;
}
#ifdef notyet /* must check for side effects */
if (RV(p) >= tsize(p->n_type, p->n_df, p->n_sue)) {
/* too many shifts */
tfree(p->n_left);
nfree(p->n_right);
p->n_op = ICON; p->n_lval = 0; p->n_sp = NULL;
} else
#endif
/* avoid larger shifts than type size */
if (RV(p) >= p->n_sue->suesize) {
RV(p) = RV(p) % p->n_sue->suesize;
werror("shift larger than type");
}
if (RV(p) == 0)
p = zapleft(p);
}
break;
case MINUS:
if (LCON(p) && RCON(p) && p->n_left->n_sp == p->n_right->n_sp) {
/* link-time constants, but both are the same */
/* solve it now by forgetting the symbols */
p->n_left->n_sp = p->n_right->n_sp = NULL;
}
if( !nncon(p->n_right) ) break;
RV(p) = -RV(p);
o = p->n_op = PLUS;
case MUL:
case PLUS:
case AND:
case OR:
case ER:
/* commutative ops; for now, just collect constants */
/* someday, do it right */
if( nncon(p->n_left) || ( LCON(p) && !RCON(p) ) )
SWAP( p->n_left, p->n_right );
/* make ops tower to the left, not the right */
if( RO(p) == o ){
NODE *t1, *t2, *t3;
t1 = p->n_left;
sp = p->n_right;
t2 = sp->n_left;
t3 = sp->n_right;
/* now, put together again */
p->n_left = sp;
sp->n_left = t1;
sp->n_right = t2;
p->n_right = t3;
}
if(o == PLUS && LO(p) == MINUS && RCON(p) && RCON(p->n_left) &&
conval(p->n_right, MINUS, p->n_left->n_right)){
zapleft:
q = p->n_left->n_left;
nfree(p->n_left->n_right);
nfree(p->n_left);
p->n_left = q;
}
if( RCON(p) && LO(p)==o && RCON(p->n_left) &&
conval( p->n_right, o, p->n_left->n_right ) ){
goto zapleft;
}
else if( LCON(p) && RCON(p) && conval( p->n_left, o, p->n_right ) ){
zapright:
nfree(p->n_right);
q = makety(p->n_left, p->n_type, p->n_qual,
p->n_df, p->n_sue);
nfree(p);
return clocal(q);
}
/* change muls to shifts */
if( o == MUL && nncon(p->n_right) && (i=ispow2(RV(p)))>=0){
if( i == 0 ) { /* multiplication by 1 */
goto zapright;
}
o = p->n_op = LS;
p->n_right->n_type = INT;
p->n_right->n_df = NULL;
RV(p) = i;
}
/* change +'s of negative consts back to - */
if( o==PLUS && nncon(p->n_right) && RV(p)<0 ){
RV(p) = -RV(p);
o = p->n_op = MINUS;
}
/* remove ops with RHS 0 */
if ((o == PLUS || o == MINUS || o == OR || o == ER) &&
nncon(p->n_right) && RV(p) == 0) {
goto zapright;
}
break;
case DIV:
if( nncon( p->n_right ) && p->n_right->n_lval == 1 )
goto zapright;
if (LCON(p) && RCON(p) && conval(p->n_left, DIV, p->n_right))
goto zapright;
if (RCON(p) && ISUNSIGNED(p->n_type) && (i=ispow2(RV(p))) > 0) {
p->n_op = RS;
RV(p) = i;
q = p->n_right;
if(tsize(q->n_type, q->n_df, q->n_sue) > SZINT)
p->n_right = makety(q, INT, 0, 0, MKSUE(INT));
break;
}
break;
case MOD:
if (RCON(p) && ISUNSIGNED(p->n_type) && ispow2(RV(p)) > 0) {
p->n_op = AND;
RV(p) = RV(p) -1;
break;
}
break;
case EQ:
case NE:
case LT:
case LE:
case GT:
case GE:
case ULT:
case ULE:
case UGT:
case UGE:
if( !LCON(p) ) break;
/* exchange operands */
sp = p->n_left;
p->n_left = p->n_right;
p->n_right = sp;
p->n_op = revrel[p->n_op - EQ ];
break;
}
return(p);
}
int
ispow2(CONSZ c)
{
int i;
if( c <= 0 || (c&(c-1)) ) return(-1);
for( i=0; c>1; ++i) c >>= 1;
return(i);
}
int
nncon( p ) NODE *p; {
/* is p a constant without a name */
return( p->n_op == ICON && p->n_sp == NULL );
}
|