Commit | Line | Data |
---|---|---|
afe46e70 C |
1 | /* Fold a constant sub-tree into a single node for C-compiler |
2 | Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc. | |
3 | ||
4 | This file is part of GNU CC. | |
5 | ||
6 | GNU CC is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GNU CC is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU CC; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | /*@@ Fix lossage on folding division of big integers. */ | |
21 | ||
22 | /*@@ This file should be rewritten to use an arbitrary precision | |
23 | @@ representation for "struct tree_int_cst" and "struct tree_real_cst". | |
24 | @@ Perhaps the routines could also be used for bc/dc, and made a lib. | |
25 | @@ The routines that translate from the ap rep should | |
26 | @@ warn if precision et. al. is lost. | |
27 | @@ This would also make life easier when this technology is used | |
28 | @@ for cross-compilers. */ | |
29 | ||
30 | ||
31 | /* The entry points in this file are fold, size_int and size_binop. | |
32 | ||
33 | fold takes a tree as argument and returns a simplified tree. | |
34 | ||
35 | size_binop takes a tree code for an arithmetic operation | |
36 | and two operands that are trees, and produces a tree for the | |
37 | result, assuming the type comes from `sizetype'. | |
38 | ||
39 | size_int takes an integer value, and creates a tree constant | |
40 | with type from `sizetype'. */ | |
41 | ||
42 | #include <stdio.h> | |
43 | #include <setjmp.h> | |
44 | #include "config.h" | |
45 | #include "flags.h" | |
46 | #include "tree.h" | |
47 | ||
48 | /* Handle floating overflow for `const_binop'. */ | |
49 | static jmp_buf float_error; | |
50 | ||
51 | int lshift_double (); | |
52 | void rshift_double (); | |
53 | void lrotate_double (); | |
54 | void rrotate_double (); | |
55 | static tree const_binop (); | |
56 | ||
57 | #ifndef BRANCH_COST | |
58 | #define BRANCH_COST 1 | |
59 | #endif | |
60 | ||
61 | /* Yield nonzero if a signed left shift of A by B bits overflows. */ | |
62 | #define left_shift_overflows(a, b) ((a) != ((a) << (b)) >> (b)) | |
63 | ||
64 | /* Yield nonzero if A and B have the same sign. */ | |
65 | #define same_sign(a, b) ((a) ^ (b) >= 0) | |
66 | ||
67 | /* Suppose A1 + B1 = SUM1, using 2's complement arithmetic ignoring overflow. | |
68 | Suppose A, B and SUM have the same respective signs as A1, B1, and SUM1. | |
69 | Then this yields nonzero if overflow occurred during the addition. | |
70 | Overflow occurs if A and B have the same sign, but A and SUM differ in sign. | |
71 | Use `^' to test whether signs differ, and `< 0' to isolate the sign. */ | |
72 | #define overflow_sum_sign(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0) | |
73 | \f | |
74 | /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic. | |
75 | We do that by representing the two-word integer as MAX_SHORTS shorts, | |
76 | with only 8 bits stored in each short, as a positive number. */ | |
77 | ||
78 | /* Unpack a two-word integer into MAX_SHORTS shorts. | |
79 | LOW and HI are the integer, as two `HOST_WIDE_INT' pieces. | |
80 | SHORTS points to the array of shorts. */ | |
81 | ||
82 | static void | |
83 | encode (shorts, low, hi) | |
84 | short *shorts; | |
85 | HOST_WIDE_INT low, hi; | |
86 | { | |
87 | register int i; | |
88 | ||
89 | for (i = 0; i < MAX_SHORTS / 2; i++) | |
90 | { | |
91 | shorts[i] = (low >> (i * 8)) & 0xff; | |
92 | shorts[i + MAX_SHORTS / 2] = (hi >> (i * 8) & 0xff); | |
93 | } | |
94 | } | |
95 | ||
96 | /* Pack an array of MAX_SHORTS shorts into a two-word integer. | |
97 | SHORTS points to the array of shorts. | |
98 | The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */ | |
99 | ||
100 | static void | |
101 | decode (shorts, low, hi) | |
102 | short *shorts; | |
103 | HOST_WIDE_INT *low, *hi; | |
104 | { | |
105 | register int i; | |
106 | HOST_WIDE_INT lv = 0, hv = 0; | |
107 | ||
108 | for (i = 0; i < MAX_SHORTS / 2; i++) | |
109 | { | |
110 | lv |= (HOST_WIDE_INT) shorts[i] << (i * 8); | |
111 | hv |= (HOST_WIDE_INT) shorts[i + MAX_SHORTS / 2] << (i * 8); | |
112 | } | |
113 | ||
114 | *low = lv, *hi = hv; | |
115 | } | |
116 | \f | |
117 | /* Make the integer constant T valid for its type | |
118 | by setting to 0 or 1 all the bits in the constant | |
119 | that don't belong in the type. */ | |
120 | ||
121 | static void | |
122 | force_fit_type (t) | |
123 | tree t; | |
124 | { | |
125 | register int prec = TYPE_PRECISION (TREE_TYPE (t)); | |
126 | ||
127 | if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE) | |
128 | prec = POINTER_SIZE; | |
129 | ||
130 | /* First clear all bits that are beyond the type's precision. */ | |
131 | ||
132 | if (prec == 2 * HOST_BITS_PER_WIDE_INT) | |
133 | ; | |
134 | else if (prec > HOST_BITS_PER_WIDE_INT) | |
135 | { | |
136 | TREE_INT_CST_HIGH (t) | |
137 | &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT)); | |
138 | } | |
139 | else | |
140 | { | |
141 | TREE_INT_CST_HIGH (t) = 0; | |
142 | if (prec < HOST_BITS_PER_WIDE_INT) | |
143 | TREE_INT_CST_LOW (t) &= ~((HOST_WIDE_INT) (-1) << prec); | |
144 | } | |
145 | ||
146 | /* If it's a signed type and value's sign bit is set, extend the sign. */ | |
147 | ||
148 | if (! TREE_UNSIGNED (TREE_TYPE (t)) | |
149 | && prec != 2 * HOST_BITS_PER_WIDE_INT | |
150 | && (prec > HOST_BITS_PER_WIDE_INT | |
151 | ? (TREE_INT_CST_HIGH (t) | |
152 | & ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1))) | |
153 | : TREE_INT_CST_LOW (t) & ((HOST_WIDE_INT) 1 << (prec - 1)))) | |
154 | { | |
155 | /* Value is negative: | |
156 | set to 1 all the bits that are outside this type's precision. */ | |
157 | if (prec > HOST_BITS_PER_WIDE_INT) | |
158 | { | |
159 | TREE_INT_CST_HIGH (t) | |
160 | |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT)); | |
161 | } | |
162 | else | |
163 | { | |
164 | TREE_INT_CST_HIGH (t) = -1; | |
165 | if (prec < HOST_BITS_PER_WIDE_INT) | |
166 | TREE_INT_CST_LOW (t) |= ((HOST_WIDE_INT) (-1) << prec); | |
167 | } | |
168 | } | |
169 | } | |
170 | \f | |
171 | /* Add two doubleword integers with doubleword result. | |
172 | Each argument is given as two `HOST_WIDE_INT' pieces. | |
173 | One argument is L1 and H1; the other, L2 and H2. | |
174 | The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. | |
175 | We use the 8-shorts representation internally. */ | |
176 | ||
177 | int | |
178 | add_double (l1, h1, l2, h2, lv, hv) | |
179 | HOST_WIDE_INT l1, h1, l2, h2; | |
180 | HOST_WIDE_INT *lv, *hv; | |
181 | { | |
182 | short arg1[MAX_SHORTS]; | |
183 | short arg2[MAX_SHORTS]; | |
184 | register int carry = 0; | |
185 | register int i; | |
186 | ||
187 | encode (arg1, l1, h1); | |
188 | encode (arg2, l2, h2); | |
189 | ||
190 | for (i = 0; i < MAX_SHORTS; i++) | |
191 | { | |
192 | carry += arg1[i] + arg2[i]; | |
193 | arg1[i] = carry & 0xff; | |
194 | carry >>= 8; | |
195 | } | |
196 | ||
197 | decode (arg1, lv, hv); | |
198 | return overflow_sum_sign (h1, h2, *hv); | |
199 | } | |
200 | ||
201 | /* Negate a doubleword integer with doubleword result. | |
202 | Return nonzero if the operation overflows, assuming it's signed. | |
203 | The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1. | |
204 | The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. | |
205 | We use the 8-shorts representation internally. */ | |
206 | ||
207 | int | |
208 | neg_double (l1, h1, lv, hv) | |
209 | HOST_WIDE_INT l1, h1; | |
210 | HOST_WIDE_INT *lv, *hv; | |
211 | { | |
212 | if (l1 == 0) | |
213 | { | |
214 | *lv = 0; | |
215 | *hv = - h1; | |
216 | return same_sign (h1, *hv); | |
217 | } | |
218 | else | |
219 | { | |
220 | *lv = - l1; | |
221 | *hv = ~ h1; | |
222 | return 0; | |
223 | } | |
224 | } | |
225 | \f | |
226 | /* Multiply two doubleword integers with doubleword result. | |
227 | Return nonzero if the operation overflows, assuming it's signed. | |
228 | Each argument is given as two `HOST_WIDE_INT' pieces. | |
229 | One argument is L1 and H1; the other, L2 and H2. | |
230 | The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. | |
231 | We use the 8-shorts representation internally. */ | |
232 | ||
233 | int | |
234 | mul_double (l1, h1, l2, h2, lv, hv) | |
235 | HOST_WIDE_INT l1, h1, l2, h2; | |
236 | HOST_WIDE_INT *lv, *hv; | |
237 | { | |
238 | short arg1[MAX_SHORTS]; | |
239 | short arg2[MAX_SHORTS]; | |
240 | short prod[MAX_SHORTS * 2]; | |
241 | register int carry = 0; | |
242 | register int i, j, k; | |
243 | HOST_WIDE_INT toplow, tophigh, neglow, neghigh; | |
244 | ||
245 | /* These cases are used extensively, arising from pointer combinations. */ | |
246 | if (h2 == 0) | |
247 | { | |
248 | if (l2 == 2) | |
249 | { | |
250 | int overflow = left_shift_overflows (h1, 1); | |
251 | unsigned HOST_WIDE_INT temp = l1 + l1; | |
252 | *hv = (h1 << 1) + (temp < l1); | |
253 | *lv = temp; | |
254 | return overflow; | |
255 | } | |
256 | if (l2 == 4) | |
257 | { | |
258 | int overflow = left_shift_overflows (h1, 2); | |
259 | unsigned HOST_WIDE_INT temp = l1 + l1; | |
260 | h1 = (h1 << 2) + ((temp < l1) << 1); | |
261 | l1 = temp; | |
262 | temp += temp; | |
263 | h1 += (temp < l1); | |
264 | *lv = temp; | |
265 | *hv = h1; | |
266 | return overflow; | |
267 | } | |
268 | if (l2 == 8) | |
269 | { | |
270 | int overflow = left_shift_overflows (h1, 3); | |
271 | unsigned HOST_WIDE_INT temp = l1 + l1; | |
272 | h1 = (h1 << 3) + ((temp < l1) << 2); | |
273 | l1 = temp; | |
274 | temp += temp; | |
275 | h1 += (temp < l1) << 1; | |
276 | l1 = temp; | |
277 | temp += temp; | |
278 | h1 += (temp < l1); | |
279 | *lv = temp; | |
280 | *hv = h1; | |
281 | return overflow; | |
282 | } | |
283 | } | |
284 | ||
285 | encode (arg1, l1, h1); | |
286 | encode (arg2, l2, h2); | |
287 | ||
288 | bzero (prod, sizeof prod); | |
289 | ||
290 | for (i = 0; i < MAX_SHORTS; i++) | |
291 | for (j = 0; j < MAX_SHORTS; j++) | |
292 | { | |
293 | k = i + j; | |
294 | carry = arg1[i] * arg2[j]; | |
295 | while (carry) | |
296 | { | |
297 | carry += prod[k]; | |
298 | prod[k] = carry & 0xff; | |
299 | carry >>= 8; | |
300 | k++; | |
301 | } | |
302 | } | |
303 | ||
304 | decode (prod, lv, hv); /* This ignores | |
305 | prod[MAX_SHORTS] -> prod[MAX_SHORTS*2-1] */ | |
306 | ||
307 | /* Check for overflow by calculating the top half of the answer in full; | |
308 | it should agree with the low half's sign bit. */ | |
309 | decode (prod+MAX_SHORTS, &toplow, &tophigh); | |
310 | if (h1 < 0) | |
311 | { | |
312 | neg_double (l2, h2, &neglow, &neghigh); | |
313 | add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh); | |
314 | } | |
315 | if (h2 < 0) | |
316 | { | |
317 | neg_double (l1, h1, &neglow, &neghigh); | |
318 | add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh); | |
319 | } | |
320 | return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0; | |
321 | } | |
322 | \f | |
323 | /* Shift the doubleword integer in L1, H1 left by COUNT places | |
324 | keeping only PREC bits of result. | |
325 | Shift right if COUNT is negative. | |
326 | ARITH nonzero specifies arithmetic shifting; otherwise use logical shift. | |
327 | Return nonzero if the arithmetic shift overflows, assuming it's signed. | |
328 | Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */ | |
329 | ||
330 | int | |
331 | lshift_double (l1, h1, count, prec, lv, hv, arith) | |
332 | HOST_WIDE_INT l1, h1; | |
333 | int count, prec; | |
334 | HOST_WIDE_INT *lv, *hv; | |
335 | int arith; | |
336 | { | |
337 | short arg1[MAX_SHORTS]; | |
338 | register int i; | |
339 | register int carry, overflow; | |
340 | ||
341 | if (count < 0) | |
342 | { | |
343 | rshift_double (l1, h1, - count, prec, lv, hv, arith); | |
344 | return 0; | |
345 | } | |
346 | ||
347 | encode (arg1, l1, h1); | |
348 | ||
349 | if (count > prec) | |
350 | count = prec; | |
351 | ||
352 | overflow = 0; | |
353 | while (count > 0) | |
354 | { | |
355 | carry = 0; | |
356 | for (i = 0; i < MAX_SHORTS; i++) | |
357 | { | |
358 | carry += arg1[i] << 1; | |
359 | arg1[i] = carry & 0xff; | |
360 | carry >>= 8; | |
361 | } | |
362 | count--; | |
363 | overflow |= carry ^ (arg1[7] >> 7); | |
364 | } | |
365 | ||
366 | decode (arg1, lv, hv); | |
367 | return overflow; | |
368 | } | |
369 | ||
370 | /* Shift the doubleword integer in L1, H1 right by COUNT places | |
371 | keeping only PREC bits of result. COUNT must be positive. | |
372 | ARITH nonzero specifies arithmetic shifting; otherwise use logical shift. | |
373 | Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */ | |
374 | ||
375 | void | |
376 | rshift_double (l1, h1, count, prec, lv, hv, arith) | |
377 | HOST_WIDE_INT l1, h1, count, prec; | |
378 | HOST_WIDE_INT *lv, *hv; | |
379 | int arith; | |
380 | { | |
381 | short arg1[MAX_SHORTS]; | |
382 | register int i; | |
383 | register int carry; | |
384 | ||
385 | encode (arg1, l1, h1); | |
386 | ||
387 | if (count > prec) | |
388 | count = prec; | |
389 | ||
390 | while (count > 0) | |
391 | { | |
392 | carry = arith && arg1[7] >> 7; | |
393 | for (i = MAX_SHORTS - 1; i >= 0; i--) | |
394 | { | |
395 | carry <<= 8; | |
396 | carry += arg1[i]; | |
397 | arg1[i] = (carry >> 1) & 0xff; | |
398 | } | |
399 | count--; | |
400 | } | |
401 | ||
402 | decode (arg1, lv, hv); | |
403 | } | |
404 | \f | |
405 | /* Rotate the doubldword integer in L1, H1 left by COUNT places | |
406 | keeping only PREC bits of result. | |
407 | Rotate right if COUNT is negative. | |
408 | Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */ | |
409 | ||
410 | void | |
411 | lrotate_double (l1, h1, count, prec, lv, hv) | |
412 | HOST_WIDE_INT l1, h1, count, prec; | |
413 | HOST_WIDE_INT *lv, *hv; | |
414 | { | |
415 | short arg1[MAX_SHORTS]; | |
416 | register int i; | |
417 | register int carry; | |
418 | ||
419 | if (count < 0) | |
420 | { | |
421 | rrotate_double (l1, h1, - count, prec, lv, hv); | |
422 | return; | |
423 | } | |
424 | ||
425 | encode (arg1, l1, h1); | |
426 | ||
427 | if (count > prec) | |
428 | count = prec; | |
429 | ||
430 | carry = arg1[MAX_SHORTS - 1] >> 7; | |
431 | while (count > 0) | |
432 | { | |
433 | for (i = 0; i < MAX_SHORTS; i++) | |
434 | { | |
435 | carry += arg1[i] << 1; | |
436 | arg1[i] = carry & 0xff; | |
437 | carry >>= 8; | |
438 | } | |
439 | count--; | |
440 | } | |
441 | ||
442 | decode (arg1, lv, hv); | |
443 | } | |
444 | ||
445 | /* Rotate the doubleword integer in L1, H1 left by COUNT places | |
446 | keeping only PREC bits of result. COUNT must be positive. | |
447 | Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */ | |
448 | ||
449 | void | |
450 | rrotate_double (l1, h1, count, prec, lv, hv) | |
451 | HOST_WIDE_INT l1, h1, count, prec; | |
452 | HOST_WIDE_INT *lv, *hv; | |
453 | { | |
454 | short arg1[MAX_SHORTS]; | |
455 | register int i; | |
456 | register int carry; | |
457 | ||
458 | encode (arg1, l1, h1); | |
459 | ||
460 | if (count > prec) | |
461 | count = prec; | |
462 | ||
463 | carry = arg1[0] & 1; | |
464 | while (count > 0) | |
465 | { | |
466 | for (i = MAX_SHORTS - 1; i >= 0; i--) | |
467 | { | |
468 | carry <<= 8; | |
469 | carry += arg1[i]; | |
470 | arg1[i] = (carry >> 1) & 0xff; | |
471 | } | |
472 | count--; | |
473 | } | |
474 | ||
475 | decode (arg1, lv, hv); | |
476 | } | |
477 | \f | |
478 | /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN | |
479 | for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM). | |
480 | CODE is a tree code for a kind of division, one of | |
481 | TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR | |
482 | or EXACT_DIV_EXPR | |
483 | It controls how the quotient is rounded to a integer. | |
484 | Return nonzero if the operation overflows. | |
485 | UNS nonzero says do unsigned division. */ | |
486 | ||
487 | static int | |
488 | div_and_round_double (code, uns, | |
489 | lnum_orig, hnum_orig, lden_orig, hden_orig, | |
490 | lquo, hquo, lrem, hrem) | |
491 | enum tree_code code; | |
492 | int uns; | |
493 | HOST_WIDE_INT lnum_orig, hnum_orig; /* num == numerator == dividend */ | |
494 | HOST_WIDE_INT lden_orig, hden_orig; /* den == denominator == divisor */ | |
495 | HOST_WIDE_INT *lquo, *hquo, *lrem, *hrem; | |
496 | { | |
497 | int quo_neg = 0; | |
498 | short num[MAX_SHORTS + 1]; /* extra element for scaling. */ | |
499 | short den[MAX_SHORTS], quo[MAX_SHORTS]; | |
500 | register int i, j, work; | |
501 | register int carry = 0; | |
502 | unsigned HOST_WIDE_INT lnum = lnum_orig; | |
503 | HOST_WIDE_INT hnum = hnum_orig; | |
504 | unsigned HOST_WIDE_INT lden = lden_orig; | |
505 | HOST_WIDE_INT hden = hden_orig; | |
506 | int overflow = 0; | |
507 | ||
508 | if ((hden == 0) && (lden == 0)) | |
509 | abort (); | |
510 | ||
511 | /* calculate quotient sign and convert operands to unsigned. */ | |
512 | if (!uns) | |
513 | { | |
514 | if (hnum < 0) | |
515 | { | |
516 | quo_neg = ~ quo_neg; | |
517 | /* (minimum integer) / (-1) is the only overflow case. */ | |
518 | if (neg_double (lnum, hnum, &lnum, &hnum) && (lden & hden) == -1) | |
519 | overflow = 1; | |
520 | } | |
521 | if (hden < 0) | |
522 | { | |
523 | quo_neg = ~ quo_neg; | |
524 | neg_double (lden, hden, &lden, &hden); | |
525 | } | |
526 | } | |
527 | ||
528 | if (hnum == 0 && hden == 0) | |
529 | { /* single precision */ | |
530 | *hquo = *hrem = 0; | |
531 | *lquo = lnum / lden; /* rounds toward zero since positive args */ | |
532 | goto finish_up; | |
533 | } | |
534 | ||
535 | if (hnum == 0) | |
536 | { /* trivial case: dividend < divisor */ | |
537 | /* hden != 0 already checked. */ | |
538 | *hquo = *lquo = 0; | |
539 | *hrem = hnum; | |
540 | *lrem = lnum; | |
541 | goto finish_up; | |
542 | } | |
543 | ||
544 | bzero (quo, sizeof quo); | |
545 | ||
546 | bzero (num, sizeof num); /* to zero 9th element */ | |
547 | bzero (den, sizeof den); | |
548 | ||
549 | encode (num, lnum, hnum); | |
550 | encode (den, lden, hden); | |
551 | ||
552 | /* This code requires more than just hden == 0. | |
553 | We also have to require that we don't need more than three bytes | |
554 | to hold CARRY. If we ever did need four bytes to hold it, we | |
555 | would lose part of it when computing WORK on the next round. */ | |
556 | if (hden == 0 && ((lden << 8) >> 8) == lden) | |
557 | { /* simpler algorithm */ | |
558 | /* hnum != 0 already checked. */ | |
559 | for (i = MAX_SHORTS - 1; i >= 0; i--) | |
560 | { | |
561 | work = num[i] + (carry << 8); | |
562 | quo[i] = work / lden; | |
563 | carry = work % lden; | |
564 | } | |
565 | } | |
566 | else { /* full double precision, | |
567 | with thanks to Don Knuth's | |
568 | "Seminumerical Algorithms". */ | |
569 | #define BASE 256 | |
570 | int quo_est, scale, num_hi_sig, den_hi_sig, quo_hi_sig; | |
571 | ||
572 | /* Find the highest non-zero divisor digit. */ | |
573 | for (i = MAX_SHORTS - 1; ; i--) | |
574 | if (den[i] != 0) { | |
575 | den_hi_sig = i; | |
576 | break; | |
577 | } | |
578 | for (i = MAX_SHORTS - 1; ; i--) | |
579 | if (num[i] != 0) { | |
580 | num_hi_sig = i; | |
581 | break; | |
582 | } | |
583 | quo_hi_sig = num_hi_sig - den_hi_sig + 1; | |
584 | ||
585 | /* Insure that the first digit of the divisor is at least BASE/2. | |
586 | This is required by the quotient digit estimation algorithm. */ | |
587 | ||
588 | scale = BASE / (den[den_hi_sig] + 1); | |
589 | if (scale > 1) { /* scale divisor and dividend */ | |
590 | carry = 0; | |
591 | for (i = 0; i <= MAX_SHORTS - 1; i++) { | |
592 | work = (num[i] * scale) + carry; | |
593 | num[i] = work & 0xff; | |
594 | carry = work >> 8; | |
595 | if (num[i] != 0) num_hi_sig = i; | |
596 | } | |
597 | carry = 0; | |
598 | for (i = 0; i <= MAX_SHORTS - 1; i++) { | |
599 | work = (den[i] * scale) + carry; | |
600 | den[i] = work & 0xff; | |
601 | carry = work >> 8; | |
602 | if (den[i] != 0) den_hi_sig = i; | |
603 | } | |
604 | } | |
605 | ||
606 | /* Main loop */ | |
607 | for (i = quo_hi_sig; i > 0; i--) { | |
608 | /* guess the next quotient digit, quo_est, by dividing the first | |
609 | two remaining dividend digits by the high order quotient digit. | |
610 | quo_est is never low and is at most 2 high. */ | |
611 | ||
612 | int num_hi; /* index of highest remaining dividend digit */ | |
613 | ||
614 | num_hi = i + den_hi_sig; | |
615 | ||
616 | work = (num[num_hi] * BASE) + (num_hi > 0 ? num[num_hi - 1] : 0); | |
617 | if (num[num_hi] != den[den_hi_sig]) { | |
618 | quo_est = work / den[den_hi_sig]; | |
619 | } | |
620 | else { | |
621 | quo_est = BASE - 1; | |
622 | } | |
623 | ||
624 | /* refine quo_est so it's usually correct, and at most one high. */ | |
625 | while ((den[den_hi_sig - 1] * quo_est) | |
626 | > (((work - (quo_est * den[den_hi_sig])) * BASE) | |
627 | + ((num_hi - 1) > 0 ? num[num_hi - 2] : 0))) | |
628 | quo_est--; | |
629 | ||
630 | /* Try QUO_EST as the quotient digit, by multiplying the | |
631 | divisor by QUO_EST and subtracting from the remaining dividend. | |
632 | Keep in mind that QUO_EST is the I - 1st digit. */ | |
633 | ||
634 | carry = 0; | |
635 | ||
636 | for (j = 0; j <= den_hi_sig; j++) | |
637 | { | |
638 | int digit; | |
639 | ||
640 | work = num[i + j - 1] - (quo_est * den[j]) + carry; | |
641 | digit = work & 0xff; | |
642 | carry = work >> 8; | |
643 | if (digit < 0) | |
644 | { | |
645 | digit += BASE; | |
646 | carry--; | |
647 | } | |
648 | num[i + j - 1] = digit; | |
649 | } | |
650 | ||
651 | /* if quo_est was high by one, then num[i] went negative and | |
652 | we need to correct things. */ | |
653 | ||
654 | if (num[num_hi] < 0) | |
655 | { | |
656 | quo_est--; | |
657 | carry = 0; /* add divisor back in */ | |
658 | for (j = 0; j <= den_hi_sig; j++) | |
659 | { | |
660 | work = num[i + j - 1] + den[j] + carry; | |
661 | if (work > BASE) | |
662 | { | |
663 | work -= BASE; | |
664 | carry = 1; | |
665 | } | |
666 | else | |
667 | { | |
668 | carry = 0; | |
669 | } | |
670 | num[i + j - 1] = work; | |
671 | } | |
672 | num [num_hi] += carry; | |
673 | } | |
674 | ||
675 | /* store the quotient digit. */ | |
676 | quo[i - 1] = quo_est; | |
677 | } | |
678 | } | |
679 | ||
680 | decode (quo, lquo, hquo); | |
681 | ||
682 | finish_up: | |
683 | /* if result is negative, make it so. */ | |
684 | if (quo_neg) | |
685 | neg_double (*lquo, *hquo, lquo, hquo); | |
686 | ||
687 | /* compute trial remainder: rem = num - (quo * den) */ | |
688 | mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem); | |
689 | neg_double (*lrem, *hrem, lrem, hrem); | |
690 | add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem); | |
691 | ||
692 | switch (code) | |
693 | { | |
694 | case TRUNC_DIV_EXPR: | |
695 | case TRUNC_MOD_EXPR: /* round toward zero */ | |
696 | case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */ | |
697 | return overflow; | |
698 | ||
699 | case FLOOR_DIV_EXPR: | |
700 | case FLOOR_MOD_EXPR: /* round toward negative infinity */ | |
701 | if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */ | |
702 | { | |
703 | /* quo = quo - 1; */ | |
704 | add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, | |
705 | lquo, hquo); | |
706 | } | |
707 | else return overflow; | |
708 | break; | |
709 | ||
710 | case CEIL_DIV_EXPR: | |
711 | case CEIL_MOD_EXPR: /* round toward positive infinity */ | |
712 | if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */ | |
713 | { | |
714 | add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0, | |
715 | lquo, hquo); | |
716 | } | |
717 | else return overflow; | |
718 | break; | |
719 | ||
720 | case ROUND_DIV_EXPR: | |
721 | case ROUND_MOD_EXPR: /* round to closest integer */ | |
722 | { | |
723 | HOST_WIDE_INT labs_rem = *lrem, habs_rem = *hrem; | |
724 | HOST_WIDE_INT labs_den = lden, habs_den = hden, ltwice, htwice; | |
725 | ||
726 | /* get absolute values */ | |
727 | if (*hrem < 0) neg_double (*lrem, *hrem, &labs_rem, &habs_rem); | |
728 | if (hden < 0) neg_double (lden, hden, &labs_den, &habs_den); | |
729 | ||
730 | /* if (2 * abs (lrem) >= abs (lden)) */ | |
731 | mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0, | |
732 | labs_rem, habs_rem, <wice, &htwice); | |
733 | if (((unsigned HOST_WIDE_INT) habs_den | |
734 | < (unsigned HOST_WIDE_INT) htwice) | |
735 | || (((unsigned HOST_WIDE_INT) habs_den | |
736 | == (unsigned HOST_WIDE_INT) htwice) | |
737 | && ((HOST_WIDE_INT unsigned) labs_den | |
738 | < (unsigned HOST_WIDE_INT) ltwice))) | |
739 | { | |
740 | if (*hquo < 0) | |
741 | /* quo = quo - 1; */ | |
742 | add_double (*lquo, *hquo, | |
743 | (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo); | |
744 | else | |
745 | /* quo = quo + 1; */ | |
746 | add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0, | |
747 | lquo, hquo); | |
748 | } | |
749 | else return overflow; | |
750 | } | |
751 | break; | |
752 | ||
753 | default: | |
754 | abort (); | |
755 | } | |
756 | ||
757 | /* compute true remainder: rem = num - (quo * den) */ | |
758 | mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem); | |
759 | neg_double (*lrem, *hrem, lrem, hrem); | |
760 | add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem); | |
761 | return overflow; | |
762 | } | |
763 | \f | |
764 | /* Effectively truncate a real value to represent | |
765 | the nearest possible value in a narrower mode. | |
766 | The result is actually represented in the same data type as the argument, | |
767 | but its value is usually different. */ | |
768 | ||
769 | REAL_VALUE_TYPE | |
770 | real_value_truncate (mode, arg) | |
771 | enum machine_mode mode; | |
772 | REAL_VALUE_TYPE arg; | |
773 | { | |
774 | #ifdef __STDC__ | |
775 | /* Make sure the value is actually stored in memory before we turn off | |
776 | the handler. */ | |
777 | volatile | |
778 | #endif | |
779 | REAL_VALUE_TYPE value; | |
780 | jmp_buf handler, old_handler; | |
781 | int handled; | |
782 | ||
783 | if (setjmp (handler)) | |
784 | { | |
785 | error ("floating overflow"); | |
786 | return dconst0; | |
787 | } | |
788 | handled = push_float_handler (handler, old_handler); | |
789 | value = REAL_VALUE_TRUNCATE (mode, arg); | |
790 | pop_float_handler (handled, old_handler); | |
791 | return value; | |
792 | } | |
793 | ||
794 | #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT | |
795 | ||
796 | /* Check for infinity in an IEEE double precision number. */ | |
797 | ||
798 | int | |
799 | target_isinf (x) | |
800 | REAL_VALUE_TYPE x; | |
801 | { | |
802 | /* The IEEE 64-bit double format. */ | |
803 | union { | |
804 | REAL_VALUE_TYPE d; | |
805 | struct { | |
806 | unsigned sign : 1; | |
807 | unsigned exponent : 11; | |
808 | unsigned mantissa1 : 20; | |
809 | unsigned mantissa2; | |
810 | } little_endian; | |
811 | struct { | |
812 | unsigned mantissa2; | |
813 | unsigned mantissa1 : 20; | |
814 | unsigned exponent : 11; | |
815 | unsigned sign : 1; | |
816 | } big_endian; | |
817 | } u; | |
818 | ||
819 | u.d = dconstm1; | |
820 | if (u.big_endian.sign == 1) | |
821 | { | |
822 | u.d = x; | |
823 | return (u.big_endian.exponent == 2047 | |
824 | && u.big_endian.mantissa1 == 0 | |
825 | && u.big_endian.mantissa2 == 0); | |
826 | } | |
827 | else | |
828 | { | |
829 | u.d = x; | |
830 | return (u.little_endian.exponent == 2047 | |
831 | && u.little_endian.mantissa1 == 0 | |
832 | && u.little_endian.mantissa2 == 0); | |
833 | } | |
834 | } | |
835 | ||
836 | /* Check whether an IEEE double precision number is a NaN. */ | |
837 | ||
838 | int | |
839 | target_isnan (x) | |
840 | REAL_VALUE_TYPE x; | |
841 | { | |
842 | /* The IEEE 64-bit double format. */ | |
843 | union { | |
844 | REAL_VALUE_TYPE d; | |
845 | struct { | |
846 | unsigned sign : 1; | |
847 | unsigned exponent : 11; | |
848 | unsigned mantissa1 : 20; | |
849 | unsigned mantissa2; | |
850 | } little_endian; | |
851 | struct { | |
852 | unsigned mantissa2; | |
853 | unsigned mantissa1 : 20; | |
854 | unsigned exponent : 11; | |
855 | unsigned sign : 1; | |
856 | } big_endian; | |
857 | } u; | |
858 | ||
859 | u.d = dconstm1; | |
860 | if (u.big_endian.sign == 1) | |
861 | { | |
862 | u.d = x; | |
863 | return (u.big_endian.exponent == 2047 | |
864 | && (u.big_endian.mantissa1 != 0 | |
865 | || u.big_endian.mantissa2 != 0)); | |
866 | } | |
867 | else | |
868 | { | |
869 | u.d = x; | |
870 | return (u.little_endian.exponent == 2047 | |
871 | && (u.little_endian.mantissa1 != 0 | |
872 | || u.little_endian.mantissa2 != 0)); | |
873 | } | |
874 | } | |
875 | ||
876 | /* Check for a negative IEEE double precision number. */ | |
877 | ||
878 | int | |
879 | target_negative (x) | |
880 | REAL_VALUE_TYPE x; | |
881 | { | |
882 | /* The IEEE 64-bit double format. */ | |
883 | union { | |
884 | REAL_VALUE_TYPE d; | |
885 | struct { | |
886 | unsigned sign : 1; | |
887 | unsigned exponent : 11; | |
888 | unsigned mantissa1 : 20; | |
889 | unsigned mantissa2; | |
890 | } little_endian; | |
891 | struct { | |
892 | unsigned mantissa2; | |
893 | unsigned mantissa1 : 20; | |
894 | unsigned exponent : 11; | |
895 | unsigned sign : 1; | |
896 | } big_endian; | |
897 | } u; | |
898 | ||
899 | u.d = dconstm1; | |
900 | if (u.big_endian.sign == 1) | |
901 | { | |
902 | u.d = x; | |
903 | return u.big_endian.sign; | |
904 | } | |
905 | else | |
906 | { | |
907 | u.d = x; | |
908 | return u.little_endian.sign; | |
909 | } | |
910 | } | |
911 | #else /* Target not IEEE */ | |
912 | ||
913 | /* Let's assume other float formats don't have infinity. | |
914 | (This can be overridden by redefining REAL_VALUE_ISINF.) */ | |
915 | ||
916 | target_isinf (x) | |
917 | REAL_VALUE_TYPE x; | |
918 | { | |
919 | return 0; | |
920 | } | |
921 | ||
922 | /* Let's assume other float formats don't have NaNs. | |
923 | (This can be overridden by redefining REAL_VALUE_ISNAN.) */ | |
924 | ||
925 | target_isnan (x) | |
926 | REAL_VALUE_TYPE x; | |
927 | { | |
928 | return 0; | |
929 | } | |
930 | ||
931 | /* Let's assume other float formats don't have minus zero. | |
932 | (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */ | |
933 | ||
934 | target_negative (x) | |
935 | REAL_VALUE_TYPE x; | |
936 | { | |
937 | return x < 0; | |
938 | } | |
939 | #endif /* Target not IEEE */ | |
940 | \f | |
941 | /* Split a tree IN into a constant and a variable part | |
942 | that could be combined with CODE to make IN. | |
943 | CODE must be a commutative arithmetic operation. | |
944 | Store the constant part into *CONP and the variable in &VARP. | |
945 | Return 1 if this was done; zero means the tree IN did not decompose | |
946 | this way. | |
947 | ||
948 | If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. | |
949 | Therefore, we must tell the caller whether the variable part | |
950 | was subtracted. We do this by storing 1 or -1 into *VARSIGNP. | |
951 | The value stored is the coefficient for the variable term. | |
952 | The constant term we return should always be added; | |
953 | we negate it if necessary. */ | |
954 | ||
955 | static int | |
956 | split_tree (in, code, varp, conp, varsignp) | |
957 | tree in; | |
958 | enum tree_code code; | |
959 | tree *varp, *conp; | |
960 | int *varsignp; | |
961 | { | |
962 | register tree outtype = TREE_TYPE (in); | |
963 | *varp = 0; | |
964 | *conp = 0; | |
965 | ||
966 | /* Strip any conversions that don't change the machine mode. */ | |
967 | while ((TREE_CODE (in) == NOP_EXPR | |
968 | || TREE_CODE (in) == CONVERT_EXPR) | |
969 | && (TYPE_MODE (TREE_TYPE (in)) | |
970 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (in, 0))))) | |
971 | in = TREE_OPERAND (in, 0); | |
972 | ||
973 | if (TREE_CODE (in) == code | |
974 | || (TREE_CODE (TREE_TYPE (in)) != REAL_TYPE | |
975 | /* We can associate addition and subtraction together | |
976 | (even though the C standard doesn't say so) | |
977 | for integers because the value is not affected. | |
978 | For reals, the value might be affected, so we can't. */ | |
979 | && | |
980 | ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR) | |
981 | || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR)))) | |
982 | { | |
983 | enum tree_code code = TREE_CODE (TREE_OPERAND (in, 0)); | |
984 | if (code == INTEGER_CST) | |
985 | { | |
986 | *conp = TREE_OPERAND (in, 0); | |
987 | *varp = TREE_OPERAND (in, 1); | |
988 | if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype) | |
989 | && TREE_TYPE (*varp) != outtype) | |
990 | *varp = convert (outtype, *varp); | |
991 | *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1; | |
992 | return 1; | |
993 | } | |
994 | if (TREE_CONSTANT (TREE_OPERAND (in, 1))) | |
995 | { | |
996 | *conp = TREE_OPERAND (in, 1); | |
997 | *varp = TREE_OPERAND (in, 0); | |
998 | *varsignp = 1; | |
999 | if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype) | |
1000 | && TREE_TYPE (*varp) != outtype) | |
1001 | *varp = convert (outtype, *varp); | |
1002 | if (TREE_CODE (in) == MINUS_EXPR) | |
1003 | { | |
1004 | /* If operation is subtraction and constant is second, | |
1005 | must negate it to get an additive constant. | |
1006 | And this cannot be done unless it is a manifest constant. | |
1007 | It could also be the address of a static variable. | |
1008 | We cannot negate that, so give up. */ | |
1009 | if (TREE_CODE (*conp) == INTEGER_CST) | |
1010 | /* Subtracting from integer_zero_node loses for long long. */ | |
1011 | *conp = fold (build1 (NEGATE_EXPR, TREE_TYPE (*conp), *conp)); | |
1012 | else | |
1013 | return 0; | |
1014 | } | |
1015 | return 1; | |
1016 | } | |
1017 | if (TREE_CONSTANT (TREE_OPERAND (in, 0))) | |
1018 | { | |
1019 | *conp = TREE_OPERAND (in, 0); | |
1020 | *varp = TREE_OPERAND (in, 1); | |
1021 | if (TYPE_MODE (TREE_TYPE (*varp)) != TYPE_MODE (outtype) | |
1022 | && TREE_TYPE (*varp) != outtype) | |
1023 | *varp = convert (outtype, *varp); | |
1024 | *varsignp = (TREE_CODE (in) == MINUS_EXPR) ? -1 : 1; | |
1025 | return 1; | |
1026 | } | |
1027 | } | |
1028 | return 0; | |
1029 | } | |
1030 | \f | |
1031 | /* Combine two constants NUM and ARG2 under operation CODE | |
1032 | to produce a new constant. | |
1033 | We assume ARG1 and ARG2 have the same data type, | |
1034 | or at least are the same kind of constant and the same machine mode. */ | |
1035 | ||
1036 | static tree | |
1037 | const_binop (code, arg1, arg2) | |
1038 | enum tree_code code; | |
1039 | register tree arg1, arg2; | |
1040 | { | |
1041 | if (TREE_CODE (arg1) == INTEGER_CST) | |
1042 | { | |
1043 | register HOST_WIDE_INT int1l = TREE_INT_CST_LOW (arg1); | |
1044 | register HOST_WIDE_INT int1h = TREE_INT_CST_HIGH (arg1); | |
1045 | HOST_WIDE_INT int2l = TREE_INT_CST_LOW (arg2); | |
1046 | HOST_WIDE_INT int2h = TREE_INT_CST_HIGH (arg2); | |
1047 | HOST_WIDE_INT low, hi; | |
1048 | HOST_WIDE_INT garbagel, garbageh; | |
1049 | register tree t; | |
1050 | int uns = TREE_UNSIGNED (TREE_TYPE (arg1)); | |
1051 | /* Propagate overflow flags from operands; also record new overflow. */ | |
1052 | int overflow | |
1053 | = TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2); | |
1054 | ||
1055 | switch (code) | |
1056 | { | |
1057 | case BIT_IOR_EXPR: | |
1058 | t = build_int_2 (int1l | int2l, int1h | int2h); | |
1059 | break; | |
1060 | ||
1061 | case BIT_XOR_EXPR: | |
1062 | t = build_int_2 (int1l ^ int2l, int1h ^ int2h); | |
1063 | break; | |
1064 | ||
1065 | case BIT_AND_EXPR: | |
1066 | t = build_int_2 (int1l & int2l, int1h & int2h); | |
1067 | break; | |
1068 | ||
1069 | case BIT_ANDTC_EXPR: | |
1070 | t = build_int_2 (int1l & ~int2l, int1h & ~int2h); | |
1071 | break; | |
1072 | ||
1073 | case RSHIFT_EXPR: | |
1074 | int2l = - int2l; | |
1075 | case LSHIFT_EXPR: | |
1076 | overflow = lshift_double (int1l, int1h, int2l, | |
1077 | TYPE_PRECISION (TREE_TYPE (arg1)), | |
1078 | &low, &hi, | |
1079 | !uns); | |
1080 | t = build_int_2 (low, hi); | |
1081 | break; | |
1082 | ||
1083 | case RROTATE_EXPR: | |
1084 | int2l = - int2l; | |
1085 | case LROTATE_EXPR: | |
1086 | lrotate_double (int1l, int1h, int2l, | |
1087 | TYPE_PRECISION (TREE_TYPE (arg1)), | |
1088 | &low, &hi); | |
1089 | t = build_int_2 (low, hi); | |
1090 | break; | |
1091 | ||
1092 | case PLUS_EXPR: | |
1093 | if (int1h == 0) | |
1094 | { | |
1095 | int2l += int1l; | |
1096 | if ((unsigned HOST_WIDE_INT) int2l < int1l) | |
1097 | { | |
1098 | hi = int2h++; | |
1099 | overflow = ! same_sign (hi, int2h); | |
1100 | } | |
1101 | t = build_int_2 (int2l, int2h); | |
1102 | break; | |
1103 | } | |
1104 | if (int2h == 0) | |
1105 | { | |
1106 | int1l += int2l; | |
1107 | if ((unsigned HOST_WIDE_INT) int1l < int2l) | |
1108 | { | |
1109 | hi = int1h++; | |
1110 | overflow = ! same_sign (hi, int1h); | |
1111 | } | |
1112 | t = build_int_2 (int1l, int1h); | |
1113 | break; | |
1114 | } | |
1115 | overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi); | |
1116 | t = build_int_2 (low, hi); | |
1117 | break; | |
1118 | ||
1119 | case MINUS_EXPR: | |
1120 | if (int2h == 0 && int2l == 0) | |
1121 | { | |
1122 | t = build_int_2 (int1l, int1h); | |
1123 | break; | |
1124 | } | |
1125 | neg_double (int2l, int2h, &low, &hi); | |
1126 | add_double (int1l, int1h, low, hi, &low, &hi); | |
1127 | overflow = overflow_sum_sign (hi, int2h, int1h); | |
1128 | t = build_int_2 (low, hi); | |
1129 | break; | |
1130 | ||
1131 | case MULT_EXPR: | |
1132 | /* Optimize simple cases. */ | |
1133 | if (int1h == 0) | |
1134 | { | |
1135 | unsigned HOST_WIDE_INT temp; | |
1136 | ||
1137 | switch (int1l) | |
1138 | { | |
1139 | case 0: | |
1140 | t = build_int_2 (0, 0); | |
1141 | goto got_it; | |
1142 | case 1: | |
1143 | t = build_int_2 (int2l, int2h); | |
1144 | goto got_it; | |
1145 | case 2: | |
1146 | overflow = left_shift_overflows (int2h, 1); | |
1147 | temp = int2l + int2l; | |
1148 | int2h = (int2h << 1) + (temp < int2l); | |
1149 | t = build_int_2 (temp, int2h); | |
1150 | goto got_it; | |
1151 | #if 0 /* This code can lose carries. */ | |
1152 | case 3: | |
1153 | temp = int2l + int2l + int2l; | |
1154 | int2h = int2h * 3 + (temp < int2l); | |
1155 | t = build_int_2 (temp, int2h); | |
1156 | goto got_it; | |
1157 | #endif | |
1158 | case 4: | |
1159 | overflow = left_shift_overflows (int2h, 2); | |
1160 | temp = int2l + int2l; | |
1161 | int2h = (int2h << 2) + ((temp < int2l) << 1); | |
1162 | int2l = temp; | |
1163 | temp += temp; | |
1164 | int2h += (temp < int2l); | |
1165 | t = build_int_2 (temp, int2h); | |
1166 | goto got_it; | |
1167 | case 8: | |
1168 | overflow = left_shift_overflows (int2h, 3); | |
1169 | temp = int2l + int2l; | |
1170 | int2h = (int2h << 3) + ((temp < int2l) << 2); | |
1171 | int2l = temp; | |
1172 | temp += temp; | |
1173 | int2h += (temp < int2l) << 1; | |
1174 | int2l = temp; | |
1175 | temp += temp; | |
1176 | int2h += (temp < int2l); | |
1177 | t = build_int_2 (temp, int2h); | |
1178 | goto got_it; | |
1179 | default: | |
1180 | break; | |
1181 | } | |
1182 | } | |
1183 | ||
1184 | if (int2h == 0) | |
1185 | { | |
1186 | if (int2l == 0) | |
1187 | { | |
1188 | t = build_int_2 (0, 0); | |
1189 | break; | |
1190 | } | |
1191 | if (int2l == 1) | |
1192 | { | |
1193 | t = build_int_2 (int1l, int1h); | |
1194 | break; | |
1195 | } | |
1196 | } | |
1197 | ||
1198 | overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi); | |
1199 | t = build_int_2 (low, hi); | |
1200 | break; | |
1201 | ||
1202 | case TRUNC_DIV_EXPR: | |
1203 | case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR: | |
1204 | case EXACT_DIV_EXPR: | |
1205 | /* This is a shortcut for a common special case. | |
1206 | It reduces the number of tree nodes generated | |
1207 | and saves time. */ | |
1208 | if (int2h == 0 && int2l > 0 | |
1209 | && TREE_TYPE (arg1) == sizetype | |
1210 | && int1h == 0 && int1l >= 0) | |
1211 | { | |
1212 | if (code == CEIL_DIV_EXPR) | |
1213 | int1l += int2l-1; | |
1214 | return size_int (int1l / int2l); | |
1215 | } | |
1216 | case ROUND_DIV_EXPR: | |
1217 | if (int2h == 0 && int2l == 1) | |
1218 | { | |
1219 | t = build_int_2 (int1l, int1h); | |
1220 | break; | |
1221 | } | |
1222 | if (int1l == int2l && int1h == int2h) | |
1223 | { | |
1224 | if ((int1l | int1h) == 0) | |
1225 | abort (); | |
1226 | t = build_int_2 (1, 0); | |
1227 | break; | |
1228 | } | |
1229 | overflow = div_and_round_double (code, uns, | |
1230 | int1l, int1h, int2l, int2h, | |
1231 | &low, &hi, &garbagel, &garbageh); | |
1232 | t = build_int_2 (low, hi); | |
1233 | break; | |
1234 | ||
1235 | case TRUNC_MOD_EXPR: case ROUND_MOD_EXPR: | |
1236 | case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR: | |
1237 | overflow = div_and_round_double (code, uns, | |
1238 | int1l, int1h, int2l, int2h, | |
1239 | &garbagel, &garbageh, &low, &hi); | |
1240 | t = build_int_2 (low, hi); | |
1241 | break; | |
1242 | ||
1243 | case MIN_EXPR: | |
1244 | case MAX_EXPR: | |
1245 | if (uns) | |
1246 | { | |
1247 | low = (((unsigned HOST_WIDE_INT) int1h | |
1248 | < (unsigned HOST_WIDE_INT) int2h) | |
1249 | || (((unsigned HOST_WIDE_INT) int1h | |
1250 | == (unsigned HOST_WIDE_INT) int2h) | |
1251 | && ((unsigned HOST_WIDE_INT) int1l | |
1252 | < (unsigned HOST_WIDE_INT) int2l))); | |
1253 | } | |
1254 | else | |
1255 | { | |
1256 | low = ((int1h < int2h) | |
1257 | || ((int1h == int2h) | |
1258 | && ((unsigned HOST_WIDE_INT) int1l | |
1259 | < (unsigned HOST_WIDE_INT) int2l))); | |
1260 | } | |
1261 | if (low == (code == MIN_EXPR)) | |
1262 | t = build_int_2 (int1l, int1h); | |
1263 | else | |
1264 | t = build_int_2 (int2l, int2h); | |
1265 | break; | |
1266 | ||
1267 | default: | |
1268 | abort (); | |
1269 | } | |
1270 | got_it: | |
1271 | TREE_TYPE (t) = TREE_TYPE (arg1); | |
1272 | force_fit_type (t); | |
1273 | TREE_CONSTANT_OVERFLOW (t) = overflow; | |
1274 | return t; | |
1275 | } | |
1276 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
1277 | if (TREE_CODE (arg1) == REAL_CST) | |
1278 | { | |
1279 | register REAL_VALUE_TYPE d1; | |
1280 | register REAL_VALUE_TYPE d2; | |
1281 | register REAL_VALUE_TYPE value; | |
1282 | tree t; | |
1283 | ||
1284 | d1 = TREE_REAL_CST (arg1); | |
1285 | d2 = TREE_REAL_CST (arg2); | |
1286 | if (setjmp (float_error)) | |
1287 | { | |
1288 | pedwarn ("floating overflow in constant expression"); | |
1289 | return build (code, TREE_TYPE (arg1), arg1, arg2); | |
1290 | } | |
1291 | set_float_handler (float_error); | |
1292 | ||
1293 | #ifdef REAL_ARITHMETIC | |
1294 | REAL_ARITHMETIC (value, code, d1, d2); | |
1295 | #else | |
1296 | switch (code) | |
1297 | { | |
1298 | case PLUS_EXPR: | |
1299 | value = d1 + d2; | |
1300 | break; | |
1301 | ||
1302 | case MINUS_EXPR: | |
1303 | value = d1 - d2; | |
1304 | break; | |
1305 | ||
1306 | case MULT_EXPR: | |
1307 | value = d1 * d2; | |
1308 | break; | |
1309 | ||
1310 | case RDIV_EXPR: | |
1311 | #ifndef REAL_INFINITY | |
1312 | if (d2 == 0) | |
1313 | abort (); | |
1314 | #endif | |
1315 | ||
1316 | value = d1 / d2; | |
1317 | break; | |
1318 | ||
1319 | case MIN_EXPR: | |
1320 | value = MIN (d1, d2); | |
1321 | break; | |
1322 | ||
1323 | case MAX_EXPR: | |
1324 | value = MAX (d1, d2); | |
1325 | break; | |
1326 | ||
1327 | default: | |
1328 | abort (); | |
1329 | } | |
1330 | #endif /* no REAL_ARITHMETIC */ | |
1331 | t = build_real (TREE_TYPE (arg1), | |
1332 | real_value_truncate (TYPE_MODE (TREE_TYPE (arg1)), value)); | |
1333 | set_float_handler (NULL_PTR); | |
1334 | return t; | |
1335 | } | |
1336 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
1337 | if (TREE_CODE (arg1) == COMPLEX_CST) | |
1338 | { | |
1339 | register tree r1 = TREE_REALPART (arg1); | |
1340 | register tree i1 = TREE_IMAGPART (arg1); | |
1341 | register tree r2 = TREE_REALPART (arg2); | |
1342 | register tree i2 = TREE_IMAGPART (arg2); | |
1343 | register tree t; | |
1344 | ||
1345 | switch (code) | |
1346 | { | |
1347 | case PLUS_EXPR: | |
1348 | t = build_complex (const_binop (PLUS_EXPR, r1, r2), | |
1349 | const_binop (PLUS_EXPR, i1, i2)); | |
1350 | break; | |
1351 | ||
1352 | case MINUS_EXPR: | |
1353 | t = build_complex (const_binop (MINUS_EXPR, r1, r2), | |
1354 | const_binop (MINUS_EXPR, i1, i2)); | |
1355 | break; | |
1356 | ||
1357 | case MULT_EXPR: | |
1358 | t = build_complex (const_binop (MINUS_EXPR, | |
1359 | const_binop (MULT_EXPR, r1, r2), | |
1360 | const_binop (MULT_EXPR, i1, i2)), | |
1361 | const_binop (PLUS_EXPR, | |
1362 | const_binop (MULT_EXPR, r1, i2), | |
1363 | const_binop (MULT_EXPR, i1, r2))); | |
1364 | break; | |
1365 | ||
1366 | case RDIV_EXPR: | |
1367 | { | |
1368 | register tree magsquared | |
1369 | = const_binop (PLUS_EXPR, | |
1370 | const_binop (MULT_EXPR, r2, r2), | |
1371 | const_binop (MULT_EXPR, i2, i2)); | |
1372 | t = build_complex (const_binop (RDIV_EXPR, | |
1373 | const_binop (PLUS_EXPR, | |
1374 | const_binop (MULT_EXPR, r1, r2), | |
1375 | const_binop (MULT_EXPR, i1, i2)), | |
1376 | magsquared), | |
1377 | const_binop (RDIV_EXPR, | |
1378 | const_binop (MINUS_EXPR, | |
1379 | const_binop (MULT_EXPR, i1, r2), | |
1380 | const_binop (MULT_EXPR, r1, i2)), | |
1381 | magsquared)); | |
1382 | } | |
1383 | break; | |
1384 | ||
1385 | default: | |
1386 | abort (); | |
1387 | } | |
1388 | TREE_TYPE (t) = TREE_TYPE (arg1); | |
1389 | return t; | |
1390 | } | |
1391 | return 0; | |
1392 | } | |
1393 | \f | |
1394 | /* Return an INTEGER_CST with value V and type from `sizetype'. */ | |
1395 | ||
1396 | tree | |
1397 | size_int (number) | |
1398 | unsigned int number; | |
1399 | { | |
1400 | register tree t; | |
1401 | /* Type-size nodes already made for small sizes. */ | |
1402 | static tree size_table[2*HOST_BITS_PER_WIDE_INT + 1]; | |
1403 | ||
1404 | if (number >= 0 && number < 2*HOST_BITS_PER_WIDE_INT + 1 | |
1405 | && size_table[number] != 0) | |
1406 | return size_table[number]; | |
1407 | if (number >= 0 && number < 2*HOST_BITS_PER_WIDE_INT + 1) | |
1408 | { | |
1409 | push_obstacks_nochange (); | |
1410 | /* Make this a permanent node. */ | |
1411 | end_temporary_allocation (); | |
1412 | t = build_int_2 (number, 0); | |
1413 | TREE_TYPE (t) = sizetype; | |
1414 | size_table[number] = t; | |
1415 | pop_obstacks (); | |
1416 | } | |
1417 | else | |
1418 | { | |
1419 | t = build_int_2 (number, 0); | |
1420 | TREE_TYPE (t) = sizetype; | |
1421 | } | |
1422 | return t; | |
1423 | } | |
1424 | ||
1425 | /* Combine operands OP1 and OP2 with arithmetic operation CODE. | |
1426 | CODE is a tree code. Data type is taken from `sizetype', | |
1427 | If the operands are constant, so is the result. */ | |
1428 | ||
1429 | tree | |
1430 | size_binop (code, arg0, arg1) | |
1431 | enum tree_code code; | |
1432 | tree arg0, arg1; | |
1433 | { | |
1434 | /* Handle the special case of two integer constants faster. */ | |
1435 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
1436 | { | |
1437 | /* And some specific cases even faster than that. */ | |
1438 | if (code == PLUS_EXPR | |
1439 | && TREE_INT_CST_LOW (arg0) == 0 | |
1440 | && TREE_INT_CST_HIGH (arg0) == 0) | |
1441 | return arg1; | |
1442 | if (code == MINUS_EXPR | |
1443 | && TREE_INT_CST_LOW (arg1) == 0 | |
1444 | && TREE_INT_CST_HIGH (arg1) == 0) | |
1445 | return arg0; | |
1446 | if (code == MULT_EXPR | |
1447 | && TREE_INT_CST_LOW (arg0) == 1 | |
1448 | && TREE_INT_CST_HIGH (arg0) == 0) | |
1449 | return arg1; | |
1450 | /* Handle general case of two integer constants. */ | |
1451 | return const_binop (code, arg0, arg1); | |
1452 | } | |
1453 | ||
1454 | if (arg0 == error_mark_node || arg1 == error_mark_node) | |
1455 | return error_mark_node; | |
1456 | ||
1457 | return fold (build (code, sizetype, arg0, arg1)); | |
1458 | } | |
1459 | \f | |
1460 | /* Given T, a tree representing type conversion of ARG1, a constant, | |
1461 | return a constant tree representing the result of conversion. */ | |
1462 | ||
1463 | static tree | |
1464 | fold_convert (t, arg1) | |
1465 | register tree t; | |
1466 | register tree arg1; | |
1467 | { | |
1468 | register tree type = TREE_TYPE (t); | |
1469 | ||
1470 | if (TREE_CODE (type) == POINTER_TYPE | |
1471 | || TREE_CODE (type) == INTEGER_TYPE | |
1472 | || TREE_CODE (type) == ENUMERAL_TYPE) | |
1473 | { | |
1474 | if (TREE_CODE (arg1) == INTEGER_CST) | |
1475 | { | |
1476 | /* Given an integer constant, make new constant with new type, | |
1477 | appropriately sign-extended or truncated. */ | |
1478 | t = build_int_2 (TREE_INT_CST_LOW (arg1), | |
1479 | TREE_INT_CST_HIGH (arg1)); | |
1480 | /* Carry forward overflow indication unless truncating. */ | |
1481 | if (TYPE_PRECISION (type) >= TYPE_PRECISION (TREE_TYPE (t))) | |
1482 | TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg1); | |
1483 | TREE_TYPE (t) = type; | |
1484 | force_fit_type (t); | |
1485 | } | |
1486 | #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
1487 | else if (TREE_CODE (arg1) == REAL_CST) | |
1488 | { | |
1489 | REAL_VALUE_TYPE | |
1490 | l = real_value_from_int_cst (TYPE_MIN_VALUE (type)), | |
1491 | x = TREE_REAL_CST (arg1), | |
1492 | u = real_value_from_int_cst (TYPE_MAX_VALUE (type)); | |
1493 | /* See if X will be in range after truncation towards 0. | |
1494 | To compensate for truncation, move the bounds away from 0, | |
1495 | but reject if X exactly equals the adjusted bounds. */ | |
1496 | #ifdef REAL_ARITHMETIC | |
1497 | REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1); | |
1498 | REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1); | |
1499 | #else | |
1500 | l--; | |
1501 | u++; | |
1502 | #endif | |
1503 | if (! (REAL_VALUES_LESS (l, x) && REAL_VALUES_LESS (x, u))) | |
1504 | { | |
1505 | pedwarn ("real constant out of range for integer conversion"); | |
1506 | return t; | |
1507 | } | |
1508 | #ifndef REAL_ARITHMETIC | |
1509 | { | |
1510 | REAL_VALUE_TYPE d; | |
1511 | HOST_WIDE_INT low, high; | |
1512 | HOST_WIDE_INT half_word | |
1513 | = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2); | |
1514 | ||
1515 | d = TREE_REAL_CST (arg1); | |
1516 | if (d < 0) | |
1517 | d = -d; | |
1518 | ||
1519 | high = (HOST_WIDE_INT) (d / half_word / half_word); | |
1520 | d -= (REAL_VALUE_TYPE) high * half_word * half_word; | |
1521 | if (d >= (REAL_VALUE_TYPE) half_word * half_word / 2) | |
1522 | { | |
1523 | low = d - (REAL_VALUE_TYPE) half_word * half_word / 2; | |
1524 | low |= (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1); | |
1525 | } | |
1526 | else | |
1527 | low = (HOST_WIDE_INT) d; | |
1528 | if (TREE_REAL_CST (arg1) < 0) | |
1529 | neg_double (low, high, &low, &high); | |
1530 | t = build_int_2 (low, high); | |
1531 | } | |
1532 | #else | |
1533 | { | |
1534 | HOST_WIDE_INT low, high; | |
1535 | REAL_VALUE_TO_INT (low, high, TREE_REAL_CST (arg1)); | |
1536 | t = build_int_2 (low, high); | |
1537 | } | |
1538 | #endif | |
1539 | TREE_TYPE (t) = type; | |
1540 | force_fit_type (t); | |
1541 | } | |
1542 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
1543 | TREE_TYPE (t) = type; | |
1544 | } | |
1545 | else if (TREE_CODE (type) == REAL_TYPE) | |
1546 | { | |
1547 | #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
1548 | if (TREE_CODE (arg1) == INTEGER_CST) | |
1549 | return build_real_from_int_cst (type, arg1); | |
1550 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
1551 | if (TREE_CODE (arg1) == REAL_CST) | |
1552 | { | |
1553 | if (setjmp (float_error)) | |
1554 | { | |
1555 | pedwarn ("floating overflow in constant expression"); | |
1556 | return t; | |
1557 | } | |
1558 | set_float_handler (float_error); | |
1559 | ||
1560 | t = build_real (type, real_value_truncate (TYPE_MODE (type), | |
1561 | TREE_REAL_CST (arg1))); | |
1562 | set_float_handler (NULL_PTR); | |
1563 | return t; | |
1564 | } | |
1565 | } | |
1566 | TREE_CONSTANT (t) = 1; | |
1567 | return t; | |
1568 | } | |
1569 | \f | |
1570 | /* Return an expr equal to X but certainly not valid as an lvalue. */ | |
1571 | ||
1572 | tree | |
1573 | non_lvalue (x) | |
1574 | tree x; | |
1575 | { | |
1576 | tree result; | |
1577 | ||
1578 | /* These things are certainly not lvalues. */ | |
1579 | if (TREE_CODE (x) == NON_LVALUE_EXPR | |
1580 | || TREE_CODE (x) == INTEGER_CST | |
1581 | || TREE_CODE (x) == REAL_CST | |
1582 | || TREE_CODE (x) == STRING_CST | |
1583 | || TREE_CODE (x) == ADDR_EXPR) | |
1584 | return x; | |
1585 | ||
1586 | result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x); | |
1587 | TREE_CONSTANT (result) = TREE_CONSTANT (x); | |
1588 | return result; | |
1589 | } | |
1590 | \f | |
1591 | /* Given a tree comparison code, return the code that is the logical inverse | |
1592 | of the given code. It is not safe to do this for floating-point | |
1593 | comparisons, except for NE_EXPR and EQ_EXPR. */ | |
1594 | ||
1595 | static enum tree_code | |
1596 | invert_tree_comparison (code) | |
1597 | enum tree_code code; | |
1598 | { | |
1599 | switch (code) | |
1600 | { | |
1601 | case EQ_EXPR: | |
1602 | return NE_EXPR; | |
1603 | case NE_EXPR: | |
1604 | return EQ_EXPR; | |
1605 | case GT_EXPR: | |
1606 | return LE_EXPR; | |
1607 | case GE_EXPR: | |
1608 | return LT_EXPR; | |
1609 | case LT_EXPR: | |
1610 | return GE_EXPR; | |
1611 | case LE_EXPR: | |
1612 | return GT_EXPR; | |
1613 | default: | |
1614 | abort (); | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | /* Similar, but return the comparison that results if the operands are | |
1619 | swapped. This is safe for floating-point. */ | |
1620 | ||
1621 | static enum tree_code | |
1622 | swap_tree_comparison (code) | |
1623 | enum tree_code code; | |
1624 | { | |
1625 | switch (code) | |
1626 | { | |
1627 | case EQ_EXPR: | |
1628 | case NE_EXPR: | |
1629 | return code; | |
1630 | case GT_EXPR: | |
1631 | return LT_EXPR; | |
1632 | case GE_EXPR: | |
1633 | return LE_EXPR; | |
1634 | case LT_EXPR: | |
1635 | return GT_EXPR; | |
1636 | case LE_EXPR: | |
1637 | return GE_EXPR; | |
1638 | default: | |
1639 | abort (); | |
1640 | } | |
1641 | } | |
1642 | \f | |
1643 | /* Return nonzero if two operands are necessarily equal. | |
1644 | If ONLY_CONST is non-zero, only return non-zero for constants. | |
1645 | This function tests whether the operands are indistinguishable; | |
1646 | it does not test whether they are equal using C's == operation. | |
1647 | The distinction is important for IEEE floating point, because | |
1648 | (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and | |
1649 | (2) two NaNs may be indistinguishable, but NaN!=NaN. */ | |
1650 | ||
1651 | int | |
1652 | operand_equal_p (arg0, arg1, only_const) | |
1653 | tree arg0, arg1; | |
1654 | int only_const; | |
1655 | { | |
1656 | /* If both types don't have the same signedness, then we can't consider | |
1657 | them equal. We must check this before the STRIP_NOPS calls | |
1658 | because they may change the signedness of the arguments. */ | |
1659 | if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1))) | |
1660 | return 0; | |
1661 | ||
1662 | STRIP_NOPS (arg0); | |
1663 | STRIP_NOPS (arg1); | |
1664 | ||
1665 | /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal. | |
1666 | We don't care about side effects in that case because the SAVE_EXPR | |
1667 | takes care of that for us. */ | |
1668 | if (TREE_CODE (arg0) == SAVE_EXPR && arg0 == arg1) | |
1669 | return ! only_const; | |
1670 | ||
1671 | if (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)) | |
1672 | return 0; | |
1673 | ||
1674 | if (TREE_CODE (arg0) == TREE_CODE (arg1) | |
1675 | && TREE_CODE (arg0) == ADDR_EXPR | |
1676 | && TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0)) | |
1677 | return 1; | |
1678 | ||
1679 | if (TREE_CODE (arg0) == TREE_CODE (arg1) | |
1680 | && TREE_CODE (arg0) == INTEGER_CST | |
1681 | && TREE_INT_CST_LOW (arg0) == TREE_INT_CST_LOW (arg1) | |
1682 | && TREE_INT_CST_HIGH (arg0) == TREE_INT_CST_HIGH (arg1)) | |
1683 | return 1; | |
1684 | ||
1685 | /* Detect when real constants are equal. */ | |
1686 | if (TREE_CODE (arg0) == TREE_CODE (arg1) | |
1687 | && TREE_CODE (arg0) == REAL_CST) | |
1688 | return !bcmp (&TREE_REAL_CST (arg0), &TREE_REAL_CST (arg1), | |
1689 | sizeof (REAL_VALUE_TYPE)); | |
1690 | ||
1691 | if (only_const) | |
1692 | return 0; | |
1693 | ||
1694 | if (arg0 == arg1) | |
1695 | return 1; | |
1696 | ||
1697 | if (TREE_CODE (arg0) != TREE_CODE (arg1)) | |
1698 | return 0; | |
1699 | /* This is needed for conversions and for COMPONENT_REF. | |
1700 | Might as well play it safe and always test this. */ | |
1701 | if (TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1))) | |
1702 | return 0; | |
1703 | ||
1704 | switch (TREE_CODE_CLASS (TREE_CODE (arg0))) | |
1705 | { | |
1706 | case '1': | |
1707 | /* Two conversions are equal only if signedness and modes match. */ | |
1708 | if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR) | |
1709 | && (TREE_UNSIGNED (TREE_TYPE (arg0)) | |
1710 | != TREE_UNSIGNED (TREE_TYPE (arg1)))) | |
1711 | return 0; | |
1712 | ||
1713 | return operand_equal_p (TREE_OPERAND (arg0, 0), | |
1714 | TREE_OPERAND (arg1, 0), 0); | |
1715 | ||
1716 | case '<': | |
1717 | case '2': | |
1718 | return (operand_equal_p (TREE_OPERAND (arg0, 0), | |
1719 | TREE_OPERAND (arg1, 0), 0) | |
1720 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
1721 | TREE_OPERAND (arg1, 1), 0)); | |
1722 | ||
1723 | case 'r': | |
1724 | switch (TREE_CODE (arg0)) | |
1725 | { | |
1726 | case INDIRECT_REF: | |
1727 | return operand_equal_p (TREE_OPERAND (arg0, 0), | |
1728 | TREE_OPERAND (arg1, 0), 0); | |
1729 | ||
1730 | case COMPONENT_REF: | |
1731 | case ARRAY_REF: | |
1732 | return (operand_equal_p (TREE_OPERAND (arg0, 0), | |
1733 | TREE_OPERAND (arg1, 0), 0) | |
1734 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
1735 | TREE_OPERAND (arg1, 1), 0)); | |
1736 | ||
1737 | case BIT_FIELD_REF: | |
1738 | return (operand_equal_p (TREE_OPERAND (arg0, 0), | |
1739 | TREE_OPERAND (arg1, 0), 0) | |
1740 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
1741 | TREE_OPERAND (arg1, 1), 0) | |
1742 | && operand_equal_p (TREE_OPERAND (arg0, 2), | |
1743 | TREE_OPERAND (arg1, 2), 0)); | |
1744 | } | |
1745 | break; | |
1746 | } | |
1747 | ||
1748 | return 0; | |
1749 | } | |
1750 | \f | |
1751 | /* Similar to operand_equal_p, but see if ARG0 might have been made by | |
1752 | shorten_compare from ARG1 when ARG1 was being compared with OTHER. | |
1753 | ||
1754 | When in doubt, return 0. */ | |
1755 | ||
1756 | static int | |
1757 | operand_equal_for_comparison_p (arg0, arg1, other) | |
1758 | tree arg0, arg1; | |
1759 | tree other; | |
1760 | { | |
1761 | int unsignedp1, unsignedpo; | |
1762 | tree primarg1, primother; | |
1763 | int correct_width; | |
1764 | ||
1765 | if (operand_equal_p (arg0, arg1, 0)) | |
1766 | return 1; | |
1767 | ||
1768 | if (TREE_CODE (TREE_TYPE (arg0)) != INTEGER_TYPE) | |
1769 | return 0; | |
1770 | ||
1771 | /* Duplicate what shorten_compare does to ARG1 and see if that gives the | |
1772 | actual comparison operand, ARG0. | |
1773 | ||
1774 | First throw away any conversions to wider types | |
1775 | already present in the operands. */ | |
1776 | ||
1777 | primarg1 = get_narrower (arg1, &unsignedp1); | |
1778 | primother = get_narrower (other, &unsignedpo); | |
1779 | ||
1780 | correct_width = TYPE_PRECISION (TREE_TYPE (arg1)); | |
1781 | if (unsignedp1 == unsignedpo | |
1782 | && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width | |
1783 | && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width) | |
1784 | { | |
1785 | tree type = TREE_TYPE (arg0); | |
1786 | ||
1787 | /* Make sure shorter operand is extended the right way | |
1788 | to match the longer operand. */ | |
1789 | primarg1 = convert (signed_or_unsigned_type (unsignedp1, | |
1790 | TREE_TYPE (primarg1)), | |
1791 | primarg1); | |
1792 | ||
1793 | if (operand_equal_p (arg0, convert (type, primarg1), 0)) | |
1794 | return 1; | |
1795 | } | |
1796 | ||
1797 | return 0; | |
1798 | } | |
1799 | \f | |
1800 | /* See if ARG is an expression that is either a comparison or is performing | |
1801 | arithmetic on comparisons. The comparisons must only be comparing | |
1802 | two different values, which will be stored in *CVAL1 and *CVAL2; if | |
1803 | they are non-zero it means that some operands have already been found. | |
1804 | No variables may be used anywhere else in the expression except in the | |
1805 | comparisons. | |
1806 | ||
1807 | If this is true, return 1. Otherwise, return zero. */ | |
1808 | ||
1809 | static int | |
1810 | twoval_comparison_p (arg, cval1, cval2) | |
1811 | tree arg; | |
1812 | tree *cval1, *cval2; | |
1813 | { | |
1814 | enum tree_code code = TREE_CODE (arg); | |
1815 | char class = TREE_CODE_CLASS (code); | |
1816 | ||
1817 | /* We can handle some of the 'e' cases here. */ | |
1818 | if (class == 'e' | |
1819 | && (code == TRUTH_NOT_EXPR | |
1820 | || (code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0))) | |
1821 | class = '1'; | |
1822 | else if (class == 'e' | |
1823 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR | |
1824 | || code == COMPOUND_EXPR)) | |
1825 | class = '2'; | |
1826 | ||
1827 | switch (class) | |
1828 | { | |
1829 | case '1': | |
1830 | return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2); | |
1831 | ||
1832 | case '2': | |
1833 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2) | |
1834 | && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2)); | |
1835 | ||
1836 | case 'c': | |
1837 | return 1; | |
1838 | ||
1839 | case 'e': | |
1840 | if (code == COND_EXPR) | |
1841 | return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2) | |
1842 | && twoval_comparison_p (TREE_OPERAND (arg, 1), cval1, cval2) | |
1843 | && twoval_comparison_p (TREE_OPERAND (arg, 2), | |
1844 | cval1, cval2)); | |
1845 | return 0; | |
1846 | ||
1847 | case '<': | |
1848 | /* First see if we can handle the first operand, then the second. For | |
1849 | the second operand, we know *CVAL1 can't be zero. It must be that | |
1850 | one side of the comparison is each of the values; test for the | |
1851 | case where this isn't true by failing if the two operands | |
1852 | are the same. */ | |
1853 | ||
1854 | if (operand_equal_p (TREE_OPERAND (arg, 0), | |
1855 | TREE_OPERAND (arg, 1), 0)) | |
1856 | return 0; | |
1857 | ||
1858 | if (*cval1 == 0) | |
1859 | *cval1 = TREE_OPERAND (arg, 0); | |
1860 | else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0)) | |
1861 | ; | |
1862 | else if (*cval2 == 0) | |
1863 | *cval2 = TREE_OPERAND (arg, 0); | |
1864 | else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0)) | |
1865 | ; | |
1866 | else | |
1867 | return 0; | |
1868 | ||
1869 | if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0)) | |
1870 | ; | |
1871 | else if (*cval2 == 0) | |
1872 | *cval2 = TREE_OPERAND (arg, 1); | |
1873 | else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0)) | |
1874 | ; | |
1875 | else | |
1876 | return 0; | |
1877 | ||
1878 | return 1; | |
1879 | } | |
1880 | ||
1881 | return 0; | |
1882 | } | |
1883 | \f | |
1884 | /* ARG is a tree that is known to contain just arithmetic operations and | |
1885 | comparisons. Evaluate the operations in the tree substituting NEW0 for | |
1886 | any occurrence of OLD0 as an operand of a comparison and likewise for | |
1887 | NEW1 and OLD1. */ | |
1888 | ||
1889 | static tree | |
1890 | eval_subst (arg, old0, new0, old1, new1) | |
1891 | tree arg; | |
1892 | tree old0, new0, old1, new1; | |
1893 | { | |
1894 | tree type = TREE_TYPE (arg); | |
1895 | enum tree_code code = TREE_CODE (arg); | |
1896 | char class = TREE_CODE_CLASS (code); | |
1897 | ||
1898 | /* We can handle some of the 'e' cases here. */ | |
1899 | if (class == 'e' && code == TRUTH_NOT_EXPR) | |
1900 | class = '1'; | |
1901 | else if (class == 'e' | |
1902 | && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR)) | |
1903 | class = '2'; | |
1904 | ||
1905 | switch (class) | |
1906 | { | |
1907 | case '1': | |
1908 | return fold (build1 (code, type, | |
1909 | eval_subst (TREE_OPERAND (arg, 0), | |
1910 | old0, new0, old1, new1))); | |
1911 | ||
1912 | case '2': | |
1913 | return fold (build (code, type, | |
1914 | eval_subst (TREE_OPERAND (arg, 0), | |
1915 | old0, new0, old1, new1), | |
1916 | eval_subst (TREE_OPERAND (arg, 1), | |
1917 | old0, new0, old1, new1))); | |
1918 | ||
1919 | case 'e': | |
1920 | switch (code) | |
1921 | { | |
1922 | case SAVE_EXPR: | |
1923 | return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1); | |
1924 | ||
1925 | case COMPOUND_EXPR: | |
1926 | return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1); | |
1927 | ||
1928 | case COND_EXPR: | |
1929 | return fold (build (code, type, | |
1930 | eval_subst (TREE_OPERAND (arg, 0), | |
1931 | old0, new0, old1, new1), | |
1932 | eval_subst (TREE_OPERAND (arg, 1), | |
1933 | old0, new0, old1, new1), | |
1934 | eval_subst (TREE_OPERAND (arg, 2), | |
1935 | old0, new0, old1, new1))); | |
1936 | } | |
1937 | ||
1938 | case '<': | |
1939 | { | |
1940 | tree arg0 = TREE_OPERAND (arg, 0); | |
1941 | tree arg1 = TREE_OPERAND (arg, 1); | |
1942 | ||
1943 | /* We need to check both for exact equality and tree equality. The | |
1944 | former will be true if the operand has a side-effect. In that | |
1945 | case, we know the operand occurred exactly once. */ | |
1946 | ||
1947 | if (arg0 == old0 || operand_equal_p (arg0, old0, 0)) | |
1948 | arg0 = new0; | |
1949 | else if (arg0 == old1 || operand_equal_p (arg0, old1, 0)) | |
1950 | arg0 = new1; | |
1951 | ||
1952 | if (arg1 == old0 || operand_equal_p (arg1, old0, 0)) | |
1953 | arg1 = new0; | |
1954 | else if (arg1 == old1 || operand_equal_p (arg1, old1, 0)) | |
1955 | arg1 = new1; | |
1956 | ||
1957 | return fold (build (code, type, arg0, arg1)); | |
1958 | } | |
1959 | } | |
1960 | ||
1961 | return arg; | |
1962 | } | |
1963 | \f | |
1964 | /* Return a tree for the case when the result of an expression is RESULT | |
1965 | converted to TYPE and OMITTED was previously an operand of the expression | |
1966 | but is now not needed (e.g., we folded OMITTED * 0). | |
1967 | ||
1968 | If OMITTED has side effects, we must evaluate it. Otherwise, just do | |
1969 | the conversion of RESULT to TYPE. */ | |
1970 | ||
1971 | static tree | |
1972 | omit_one_operand (type, result, omitted) | |
1973 | tree type, result, omitted; | |
1974 | { | |
1975 | tree t = convert (type, result); | |
1976 | ||
1977 | if (TREE_SIDE_EFFECTS (omitted)) | |
1978 | return build (COMPOUND_EXPR, type, omitted, t); | |
1979 | ||
1980 | return t; | |
1981 | } | |
1982 | \f | |
1983 | /* Return a simplified tree node for the truth-negation of ARG. This | |
1984 | never alters ARG itself. We assume that ARG is an operation that | |
1985 | returns a truth value (0 or 1). */ | |
1986 | ||
1987 | tree | |
1988 | invert_truthvalue (arg) | |
1989 | tree arg; | |
1990 | { | |
1991 | tree type = TREE_TYPE (arg); | |
1992 | enum tree_code code = TREE_CODE (arg); | |
1993 | ||
1994 | /* If this is a comparison, we can simply invert it, except for | |
1995 | floating-point non-equality comparisons, in which case we just | |
1996 | enclose a TRUTH_NOT_EXPR around what we have. */ | |
1997 | ||
1998 | if (TREE_CODE_CLASS (code) == '<') | |
1999 | { | |
2000 | if (TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))) == REAL_TYPE | |
2001 | && code != NE_EXPR && code != EQ_EXPR) | |
2002 | return build1 (TRUTH_NOT_EXPR, type, arg); | |
2003 | else | |
2004 | return build (invert_tree_comparison (code), type, | |
2005 | TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1)); | |
2006 | } | |
2007 | ||
2008 | switch (code) | |
2009 | { | |
2010 | case INTEGER_CST: | |
2011 | return convert (type, build_int_2 (TREE_INT_CST_LOW (arg) == 0 | |
2012 | && TREE_INT_CST_HIGH (arg) == 0, 0)); | |
2013 | ||
2014 | case TRUTH_AND_EXPR: | |
2015 | return build (TRUTH_OR_EXPR, type, | |
2016 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2017 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2018 | ||
2019 | case TRUTH_OR_EXPR: | |
2020 | return build (TRUTH_AND_EXPR, type, | |
2021 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2022 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2023 | ||
2024 | case TRUTH_ANDIF_EXPR: | |
2025 | return build (TRUTH_ORIF_EXPR, type, | |
2026 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2027 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2028 | ||
2029 | case TRUTH_ORIF_EXPR: | |
2030 | return build (TRUTH_ANDIF_EXPR, type, | |
2031 | invert_truthvalue (TREE_OPERAND (arg, 0)), | |
2032 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2033 | ||
2034 | case TRUTH_NOT_EXPR: | |
2035 | return TREE_OPERAND (arg, 0); | |
2036 | ||
2037 | case COND_EXPR: | |
2038 | return build (COND_EXPR, type, TREE_OPERAND (arg, 0), | |
2039 | invert_truthvalue (TREE_OPERAND (arg, 1)), | |
2040 | invert_truthvalue (TREE_OPERAND (arg, 2))); | |
2041 | ||
2042 | case COMPOUND_EXPR: | |
2043 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0), | |
2044 | invert_truthvalue (TREE_OPERAND (arg, 1))); | |
2045 | ||
2046 | case NON_LVALUE_EXPR: | |
2047 | return invert_truthvalue (TREE_OPERAND (arg, 0)); | |
2048 | ||
2049 | case NOP_EXPR: | |
2050 | case CONVERT_EXPR: | |
2051 | case FLOAT_EXPR: | |
2052 | return build1 (TREE_CODE (arg), type, | |
2053 | invert_truthvalue (TREE_OPERAND (arg, 0))); | |
2054 | ||
2055 | case BIT_AND_EXPR: | |
2056 | if (! integer_onep (TREE_OPERAND (arg, 1))) | |
2057 | abort (); | |
2058 | return build (EQ_EXPR, type, arg, convert (type, integer_zero_node)); | |
2059 | } | |
2060 | ||
2061 | abort (); | |
2062 | } | |
2063 | ||
2064 | /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both | |
2065 | operands are another bit-wise operation with a common input. If so, | |
2066 | distribute the bit operations to save an operation and possibly two if | |
2067 | constants are involved. For example, convert | |
2068 | (A | B) & (A | C) into A | (B & C) | |
2069 | Further simplification will occur if B and C are constants. | |
2070 | ||
2071 | If this optimization cannot be done, 0 will be returned. */ | |
2072 | ||
2073 | static tree | |
2074 | distribute_bit_expr (code, type, arg0, arg1) | |
2075 | enum tree_code code; | |
2076 | tree type; | |
2077 | tree arg0, arg1; | |
2078 | { | |
2079 | tree common; | |
2080 | tree left, right; | |
2081 | ||
2082 | if (TREE_CODE (arg0) != TREE_CODE (arg1) | |
2083 | || TREE_CODE (arg0) == code | |
2084 | || (TREE_CODE (arg0) != BIT_AND_EXPR | |
2085 | && TREE_CODE (arg0) != BIT_IOR_EXPR)) | |
2086 | return 0; | |
2087 | ||
2088 | if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)) | |
2089 | { | |
2090 | common = TREE_OPERAND (arg0, 0); | |
2091 | left = TREE_OPERAND (arg0, 1); | |
2092 | right = TREE_OPERAND (arg1, 1); | |
2093 | } | |
2094 | else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0)) | |
2095 | { | |
2096 | common = TREE_OPERAND (arg0, 0); | |
2097 | left = TREE_OPERAND (arg0, 1); | |
2098 | right = TREE_OPERAND (arg1, 0); | |
2099 | } | |
2100 | else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0)) | |
2101 | { | |
2102 | common = TREE_OPERAND (arg0, 1); | |
2103 | left = TREE_OPERAND (arg0, 0); | |
2104 | right = TREE_OPERAND (arg1, 1); | |
2105 | } | |
2106 | else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0)) | |
2107 | { | |
2108 | common = TREE_OPERAND (arg0, 1); | |
2109 | left = TREE_OPERAND (arg0, 0); | |
2110 | right = TREE_OPERAND (arg1, 0); | |
2111 | } | |
2112 | else | |
2113 | return 0; | |
2114 | ||
2115 | return fold (build (TREE_CODE (arg0), type, common, | |
2116 | fold (build (code, type, left, right)))); | |
2117 | } | |
2118 | \f | |
2119 | /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER | |
2120 | starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */ | |
2121 | ||
2122 | static tree | |
2123 | make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp) | |
2124 | tree inner; | |
2125 | tree type; | |
2126 | int bitsize, bitpos; | |
2127 | int unsignedp; | |
2128 | { | |
2129 | tree result = build (BIT_FIELD_REF, type, inner, | |
2130 | size_int (bitsize), size_int (bitpos)); | |
2131 | ||
2132 | TREE_UNSIGNED (result) = unsignedp; | |
2133 | ||
2134 | return result; | |
2135 | } | |
2136 | ||
2137 | /* Optimize a bit-field compare. | |
2138 | ||
2139 | There are two cases: First is a compare against a constant and the | |
2140 | second is a comparison of two items where the fields are at the same | |
2141 | bit position relative to the start of a chunk (byte, halfword, word) | |
2142 | large enough to contain it. In these cases we can avoid the shift | |
2143 | implicit in bitfield extractions. | |
2144 | ||
2145 | For constants, we emit a compare of the shifted constant with the | |
2146 | BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being | |
2147 | compared. For two fields at the same position, we do the ANDs with the | |
2148 | similar mask and compare the result of the ANDs. | |
2149 | ||
2150 | CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR. | |
2151 | COMPARE_TYPE is the type of the comparison, and LHS and RHS | |
2152 | are the left and right operands of the comparison, respectively. | |
2153 | ||
2154 | If the optimization described above can be done, we return the resulting | |
2155 | tree. Otherwise we return zero. */ | |
2156 | ||
2157 | static tree | |
2158 | optimize_bit_field_compare (code, compare_type, lhs, rhs) | |
2159 | enum tree_code code; | |
2160 | tree compare_type; | |
2161 | tree lhs, rhs; | |
2162 | { | |
2163 | int lbitpos, lbitsize, rbitpos, rbitsize; | |
2164 | int lnbitpos, lnbitsize, rnbitpos, rnbitsize; | |
2165 | tree type = TREE_TYPE (lhs); | |
2166 | tree signed_type, unsigned_type; | |
2167 | int const_p = TREE_CODE (rhs) == INTEGER_CST; | |
2168 | enum machine_mode lmode, rmode, lnmode, rnmode; | |
2169 | int lunsignedp, runsignedp; | |
2170 | int lvolatilep = 0, rvolatilep = 0; | |
2171 | tree linner, rinner; | |
2172 | tree mask; | |
2173 | tree offset; | |
2174 | ||
2175 | /* Get all the information about the extractions being done. If the bit size | |
2176 | if the same as the size of the underlying object, we aren't doing an | |
2177 | extraction at all and so can do nothing. */ | |
2178 | linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode, | |
2179 | &lunsignedp, &lvolatilep); | |
2180 | if (lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0 | |
2181 | || offset != 0) | |
2182 | return 0; | |
2183 | ||
2184 | if (!const_p) | |
2185 | { | |
2186 | /* If this is not a constant, we can only do something if bit positions, | |
2187 | sizes, and signedness are the same. */ | |
2188 | rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, | |
2189 | &rmode, &runsignedp, &rvolatilep); | |
2190 | ||
2191 | if (lbitpos != rbitpos || lbitsize != rbitsize | |
2192 | || lunsignedp != runsignedp || offset != 0) | |
2193 | return 0; | |
2194 | } | |
2195 | ||
2196 | /* See if we can find a mode to refer to this field. We should be able to, | |
2197 | but fail if we can't. */ | |
2198 | lnmode = get_best_mode (lbitsize, lbitpos, | |
2199 | TYPE_ALIGN (TREE_TYPE (linner)), word_mode, | |
2200 | lvolatilep); | |
2201 | if (lnmode == VOIDmode) | |
2202 | return 0; | |
2203 | ||
2204 | /* Set signed and unsigned types of the precision of this mode for the | |
2205 | shifts below. */ | |
2206 | signed_type = type_for_mode (lnmode, 0); | |
2207 | unsigned_type = type_for_mode (lnmode, 1); | |
2208 | ||
2209 | if (! const_p) | |
2210 | { | |
2211 | rnmode = get_best_mode (rbitsize, rbitpos, | |
2212 | TYPE_ALIGN (TREE_TYPE (rinner)), word_mode, | |
2213 | rvolatilep); | |
2214 | if (rnmode == VOIDmode) | |
2215 | return 0; | |
2216 | } | |
2217 | ||
2218 | /* Compute the bit position and size for the new reference and our offset | |
2219 | within it. If the new reference is the same size as the original, we | |
2220 | won't optimize anything, so return zero. */ | |
2221 | lnbitsize = GET_MODE_BITSIZE (lnmode); | |
2222 | lnbitpos = lbitpos & ~ (lnbitsize - 1); | |
2223 | lbitpos -= lnbitpos; | |
2224 | if (lnbitsize == lbitsize) | |
2225 | return 0; | |
2226 | ||
2227 | if (! const_p) | |
2228 | { | |
2229 | rnbitsize = GET_MODE_BITSIZE (rnmode); | |
2230 | rnbitpos = rbitpos & ~ (rnbitsize - 1); | |
2231 | rbitpos -= rnbitpos; | |
2232 | if (rnbitsize == rbitsize) | |
2233 | return 0; | |
2234 | } | |
2235 | ||
2236 | #if BYTES_BIG_ENDIAN | |
2237 | lbitpos = lnbitsize - lbitsize - lbitpos; | |
2238 | #endif | |
2239 | ||
2240 | /* Make the mask to be used against the extracted field. */ | |
2241 | mask = convert (unsigned_type, build_int_2 (~0, ~0)); | |
2242 | mask = const_binop (LSHIFT_EXPR, mask, size_int (lnbitsize - lbitsize)); | |
2243 | mask = const_binop (RSHIFT_EXPR, mask, | |
2244 | size_int (lnbitsize - lbitsize - lbitpos)); | |
2245 | ||
2246 | if (! const_p) | |
2247 | /* If not comparing with constant, just rework the comparison | |
2248 | and return. */ | |
2249 | return build (code, compare_type, | |
2250 | build (BIT_AND_EXPR, unsigned_type, | |
2251 | make_bit_field_ref (linner, unsigned_type, | |
2252 | lnbitsize, lnbitpos, 1), | |
2253 | mask), | |
2254 | build (BIT_AND_EXPR, unsigned_type, | |
2255 | make_bit_field_ref (rinner, unsigned_type, | |
2256 | rnbitsize, rnbitpos, 1), | |
2257 | mask)); | |
2258 | ||
2259 | /* Otherwise, we are handling the constant case. See if the constant is too | |
2260 | big for the field. Warn and return a tree of for 0 (false) if so. We do | |
2261 | this not only for its own sake, but to avoid having to test for this | |
2262 | error case below. If we didn't, we might generate wrong code. | |
2263 | ||
2264 | For unsigned fields, the constant shifted right by the field length should | |
2265 | be all zero. For signed fields, the high-order bits should agree with | |
2266 | the sign bit. */ | |
2267 | ||
2268 | if (lunsignedp) | |
2269 | { | |
2270 | if (! integer_zerop (const_binop (RSHIFT_EXPR, | |
2271 | convert (unsigned_type, rhs), | |
2272 | size_int (lbitsize)))) | |
2273 | { | |
2274 | warning ("comparison is always %s due to width of bitfield", | |
2275 | code == NE_EXPR ? "one" : "zero"); | |
2276 | return convert (compare_type, | |
2277 | (code == NE_EXPR | |
2278 | ? integer_one_node : integer_zero_node)); | |
2279 | } | |
2280 | } | |
2281 | else | |
2282 | { | |
2283 | tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs), | |
2284 | size_int (lbitsize - 1)); | |
2285 | if (! integer_zerop (tem) && ! integer_all_onesp (tem)) | |
2286 | { | |
2287 | warning ("comparison is always %s due to width of bitfield", | |
2288 | code == NE_EXPR ? "one" : "zero"); | |
2289 | return convert (compare_type, | |
2290 | (code == NE_EXPR | |
2291 | ? integer_one_node : integer_zero_node)); | |
2292 | } | |
2293 | } | |
2294 | ||
2295 | /* Single-bit compares should always be against zero. */ | |
2296 | if (lbitsize == 1 && ! integer_zerop (rhs)) | |
2297 | { | |
2298 | code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR; | |
2299 | rhs = convert (type, integer_zero_node); | |
2300 | } | |
2301 | ||
2302 | /* Make a new bitfield reference, shift the constant over the | |
2303 | appropriate number of bits and mask it with the computed mask | |
2304 | (in case this was a signed field). If we changed it, make a new one. */ | |
2305 | lhs = make_bit_field_ref (linner, unsigned_type, lnbitsize, lnbitpos, 1); | |
2306 | ||
2307 | rhs = fold (const_binop (BIT_AND_EXPR, | |
2308 | const_binop (LSHIFT_EXPR, | |
2309 | convert (unsigned_type, rhs), | |
2310 | size_int (lbitpos)), | |
2311 | mask)); | |
2312 | ||
2313 | return build (code, compare_type, | |
2314 | build (BIT_AND_EXPR, unsigned_type, lhs, mask), | |
2315 | rhs); | |
2316 | } | |
2317 | \f | |
2318 | /* Subroutine for fold_truthop: decode a field reference. | |
2319 | ||
2320 | If EXP is a comparison reference, we return the innermost reference. | |
2321 | ||
2322 | *PBITSIZE is set to the number of bits in the reference, *PBITPOS is | |
2323 | set to the starting bit number. | |
2324 | ||
2325 | If the innermost field can be completely contained in a mode-sized | |
2326 | unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode. | |
2327 | ||
2328 | *PVOLATILEP is set to 1 if the any expression encountered is volatile; | |
2329 | otherwise it is not changed. | |
2330 | ||
2331 | *PUNSIGNEDP is set to the signedness of the field. | |
2332 | ||
2333 | *PMASK is set to the mask used. This is either contained in a | |
2334 | BIT_AND_EXPR or derived from the width of the field. | |
2335 | ||
2336 | Return 0 if this is not a component reference or is one that we can't | |
2337 | do anything with. */ | |
2338 | ||
2339 | static tree | |
2340 | decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp, | |
2341 | pvolatilep, pmask) | |
2342 | tree exp; | |
2343 | int *pbitsize, *pbitpos; | |
2344 | enum machine_mode *pmode; | |
2345 | int *punsignedp, *pvolatilep; | |
2346 | tree *pmask; | |
2347 | { | |
2348 | tree mask = 0; | |
2349 | tree inner; | |
2350 | tree offset; | |
2351 | ||
2352 | STRIP_NOPS (exp); | |
2353 | ||
2354 | if (TREE_CODE (exp) == BIT_AND_EXPR) | |
2355 | { | |
2356 | mask = TREE_OPERAND (exp, 1); | |
2357 | exp = TREE_OPERAND (exp, 0); | |
2358 | STRIP_NOPS (exp); STRIP_NOPS (mask); | |
2359 | if (TREE_CODE (mask) != INTEGER_CST) | |
2360 | return 0; | |
2361 | } | |
2362 | ||
2363 | if (TREE_CODE (exp) != COMPONENT_REF && TREE_CODE (exp) != ARRAY_REF | |
2364 | && TREE_CODE (exp) != BIT_FIELD_REF) | |
2365 | return 0; | |
2366 | ||
2367 | inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode, | |
2368 | punsignedp, pvolatilep); | |
2369 | if (*pbitsize < 0 || offset != 0) | |
2370 | return 0; | |
2371 | ||
2372 | if (mask == 0) | |
2373 | { | |
2374 | tree unsigned_type = type_for_size (*pbitsize, 1); | |
2375 | int precision = TYPE_PRECISION (unsigned_type); | |
2376 | ||
2377 | mask = convert (unsigned_type, build_int_2 (~0, ~0)); | |
2378 | mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize)); | |
2379 | mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize)); | |
2380 | } | |
2381 | ||
2382 | *pmask = mask; | |
2383 | return inner; | |
2384 | } | |
2385 | ||
2386 | /* Return non-zero if MASK represents a mask of SIZE ones in the low-order | |
2387 | bit positions. */ | |
2388 | ||
2389 | static int | |
2390 | all_ones_mask_p (mask, size) | |
2391 | tree mask; | |
2392 | int size; | |
2393 | { | |
2394 | tree type = TREE_TYPE (mask); | |
2395 | int precision = TYPE_PRECISION (type); | |
2396 | ||
2397 | return | |
2398 | operand_equal_p (mask, | |
2399 | const_binop (RSHIFT_EXPR, | |
2400 | const_binop (LSHIFT_EXPR, | |
2401 | convert (signed_type (type), | |
2402 | build_int_2 (~0, ~0)), | |
2403 | size_int (precision - size)), | |
2404 | size_int (precision - size)), 0); | |
2405 | } | |
2406 | ||
2407 | /* Subroutine for fold_truthop: determine if an operand is simple enough | |
2408 | to be evaluated unconditionally. */ | |
2409 | ||
2410 | #ifdef __GNUC__ | |
2411 | __inline | |
2412 | #endif | |
2413 | static int | |
2414 | simple_operand_p (exp) | |
2415 | tree exp; | |
2416 | { | |
2417 | /* Strip any conversions that don't change the machine mode. */ | |
2418 | while ((TREE_CODE (exp) == NOP_EXPR | |
2419 | || TREE_CODE (exp) == CONVERT_EXPR) | |
2420 | && (TYPE_MODE (TREE_TYPE (exp)) | |
2421 | == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0))))) | |
2422 | exp = TREE_OPERAND (exp, 0); | |
2423 | ||
2424 | return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c' | |
2425 | || (TREE_CODE_CLASS (TREE_CODE (exp)) == 'd' | |
2426 | && ! TREE_ADDRESSABLE (exp) | |
2427 | && ! TREE_THIS_VOLATILE (exp) | |
2428 | && ! DECL_NONLOCAL (exp) | |
2429 | /* Don't regard global variables as simple. They may be | |
2430 | allocated in ways unknown to the compiler (shared memory, | |
2431 | #pragma weak, etc). */ | |
2432 | && ! TREE_PUBLIC (exp) | |
2433 | && ! DECL_EXTERNAL (exp) | |
2434 | /* Loading a static variable is unduly expensive, but global | |
2435 | registers aren't expensive. */ | |
2436 | && (! TREE_STATIC (exp) || DECL_REGISTER (exp)))); | |
2437 | } | |
2438 | \f | |
2439 | /* Subroutine for fold_truthop: try to optimize a range test. | |
2440 | ||
2441 | For example, "i >= 2 && i =< 9" can be done as "(unsigned) (i - 2) <= 7". | |
2442 | ||
2443 | JCODE is the logical combination of the two terms. It is TRUTH_AND_EXPR | |
2444 | (representing TRUTH_ANDIF_EXPR and TRUTH_AND_EXPR) or TRUTH_OR_EXPR | |
2445 | (representing TRUTH_ORIF_EXPR and TRUTH_OR_EXPR). TYPE is the type of | |
2446 | the result. | |
2447 | ||
2448 | VAR is the value being tested. LO_CODE and HI_CODE are the comparison | |
2449 | operators comparing VAR to LO_CST and HI_CST. LO_CST is known to be no | |
2450 | larger than HI_CST (they may be equal). | |
2451 | ||
2452 | We return the simplified tree or 0 if no optimization is possible. */ | |
2453 | ||
2454 | tree | |
2455 | range_test (jcode, type, lo_code, hi_code, var, lo_cst, hi_cst) | |
2456 | enum tree_code jcode, lo_code, hi_code; | |
2457 | tree type, var, lo_cst, hi_cst; | |
2458 | { | |
2459 | tree utype; | |
2460 | enum tree_code rcode; | |
2461 | ||
2462 | /* See if this is a range test and normalize the constant terms. */ | |
2463 | ||
2464 | if (jcode == TRUTH_AND_EXPR) | |
2465 | { | |
2466 | switch (lo_code) | |
2467 | { | |
2468 | case NE_EXPR: | |
2469 | /* See if we have VAR != CST && VAR != CST+1. */ | |
2470 | if (! (hi_code == NE_EXPR | |
2471 | && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1 | |
2472 | && tree_int_cst_equal (integer_one_node, | |
2473 | const_binop (MINUS_EXPR, | |
2474 | hi_cst, lo_cst)))) | |
2475 | return 0; | |
2476 | ||
2477 | rcode = GT_EXPR; | |
2478 | break; | |
2479 | ||
2480 | case GT_EXPR: | |
2481 | case GE_EXPR: | |
2482 | if (hi_code == LT_EXPR) | |
2483 | hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node); | |
2484 | else if (hi_code != LE_EXPR) | |
2485 | return 0; | |
2486 | ||
2487 | if (lo_code == GT_EXPR) | |
2488 | lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node); | |
2489 | ||
2490 | /* We now have VAR >= LO_CST && VAR <= HI_CST. */ | |
2491 | rcode = LE_EXPR; | |
2492 | break; | |
2493 | ||
2494 | default: | |
2495 | return 0; | |
2496 | } | |
2497 | } | |
2498 | else | |
2499 | { | |
2500 | switch (lo_code) | |
2501 | { | |
2502 | case EQ_EXPR: | |
2503 | /* See if we have VAR == CST || VAR == CST+1. */ | |
2504 | if (! (hi_code == EQ_EXPR | |
2505 | && TREE_INT_CST_LOW (hi_cst) - TREE_INT_CST_LOW (lo_cst) == 1 | |
2506 | && tree_int_cst_equal (integer_one_node, | |
2507 | const_binop (MINUS_EXPR, | |
2508 | hi_cst, lo_cst)))) | |
2509 | return 0; | |
2510 | ||
2511 | rcode = LE_EXPR; | |
2512 | break; | |
2513 | ||
2514 | case LE_EXPR: | |
2515 | case LT_EXPR: | |
2516 | if (hi_code == GE_EXPR) | |
2517 | hi_cst = const_binop (MINUS_EXPR, hi_cst, integer_one_node); | |
2518 | else if (hi_code != GT_EXPR) | |
2519 | return 0; | |
2520 | ||
2521 | if (lo_code == LE_EXPR) | |
2522 | lo_cst = const_binop (PLUS_EXPR, lo_cst, integer_one_node); | |
2523 | ||
2524 | /* We now have VAR < LO_CST || VAR > HI_CST. */ | |
2525 | rcode = GT_EXPR; | |
2526 | break; | |
2527 | ||
2528 | default: | |
2529 | return 0; | |
2530 | } | |
2531 | } | |
2532 | ||
2533 | /* When normalizing, it is possible to both increment the smaller constant | |
2534 | and decrement the larger constant. See if they are still ordered. */ | |
2535 | if (tree_int_cst_lt (hi_cst, lo_cst)) | |
2536 | return 0; | |
2537 | ||
2538 | /* Fail if VAR isn't an integer. */ | |
2539 | utype = TREE_TYPE (var); | |
2540 | if (TREE_CODE (utype) != INTEGER_TYPE | |
2541 | && TREE_CODE (utype) != ENUMERAL_TYPE) | |
2542 | return 0; | |
2543 | ||
2544 | /* The range test is invalid if subtracting the two constants results | |
2545 | in overflow. This can happen in traditional mode. */ | |
2546 | if (! int_fits_type_p (hi_cst, TREE_TYPE (var)) | |
2547 | || ! int_fits_type_p (lo_cst, TREE_TYPE (var))) | |
2548 | return 0; | |
2549 | ||
2550 | if (! TREE_UNSIGNED (utype)) | |
2551 | { | |
2552 | utype = unsigned_type (utype); | |
2553 | var = convert (utype, var); | |
2554 | lo_cst = convert (utype, lo_cst); | |
2555 | hi_cst = convert (utype, hi_cst); | |
2556 | } | |
2557 | ||
2558 | return fold (convert (type, | |
2559 | build (rcode, utype, | |
2560 | build (MINUS_EXPR, utype, var, lo_cst), | |
2561 | const_binop (MINUS_EXPR, hi_cst, lo_cst)))); | |
2562 | } | |
2563 | \f | |
2564 | /* Find ways of folding logical expressions of LHS and RHS: | |
2565 | Try to merge two comparisons to the same innermost item. | |
2566 | Look for range tests like "ch >= '0' && ch <= '9'". | |
2567 | Look for combinations of simple terms on machines with expensive branches | |
2568 | and evaluate the RHS unconditionally. | |
2569 | ||
2570 | For example, if we have p->a == 2 && p->b == 4 and we can make an | |
2571 | object large enough to span both A and B, we can do this with a comparison | |
2572 | against the object ANDed with the a mask. | |
2573 | ||
2574 | If we have p->a == q->a && p->b == q->b, we may be able to use bit masking | |
2575 | operations to do this with one comparison. | |
2576 | ||
2577 | We check for both normal comparisons and the BIT_AND_EXPRs made this by | |
2578 | function and the one above. | |
2579 | ||
2580 | CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR, | |
2581 | TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR. | |
2582 | ||
2583 | TRUTH_TYPE is the type of the logical operand and LHS and RHS are its | |
2584 | two operands. | |
2585 | ||
2586 | We return the simplified tree or 0 if no optimization is possible. */ | |
2587 | ||
2588 | static tree | |
2589 | fold_truthop (code, truth_type, lhs, rhs) | |
2590 | enum tree_code code; | |
2591 | tree truth_type, lhs, rhs; | |
2592 | { | |
2593 | /* If this is the "or" of two comparisons, we can do something if we | |
2594 | the comparisons are NE_EXPR. If this is the "and", we can do something | |
2595 | if the comparisons are EQ_EXPR. I.e., | |
2596 | (a->b == 2 && a->c == 4) can become (a->new == NEW). | |
2597 | ||
2598 | WANTED_CODE is this operation code. For single bit fields, we can | |
2599 | convert EQ_EXPR to NE_EXPR so we need not reject the "wrong" | |
2600 | comparison for one-bit fields. */ | |
2601 | ||
2602 | enum tree_code wanted_code; | |
2603 | enum tree_code lcode, rcode; | |
2604 | tree ll_arg, lr_arg, rl_arg, rr_arg; | |
2605 | tree ll_inner, lr_inner, rl_inner, rr_inner; | |
2606 | int ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos; | |
2607 | int rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos; | |
2608 | int xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos; | |
2609 | int lnbitsize, lnbitpos, rnbitsize, rnbitpos; | |
2610 | int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp; | |
2611 | enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode; | |
2612 | enum machine_mode lnmode, rnmode; | |
2613 | tree ll_mask, lr_mask, rl_mask, rr_mask; | |
2614 | tree l_const, r_const; | |
2615 | tree type, result; | |
2616 | int first_bit, end_bit; | |
2617 | int volatilep; | |
2618 | ||
2619 | /* Start by getting the comparison codes and seeing if this looks like | |
2620 | a range test. Fail if anything is volatile. */ | |
2621 | ||
2622 | if (TREE_SIDE_EFFECTS (lhs) | |
2623 | || TREE_SIDE_EFFECTS (rhs)) | |
2624 | return 0; | |
2625 | ||
2626 | lcode = TREE_CODE (lhs); | |
2627 | rcode = TREE_CODE (rhs); | |
2628 | ||
2629 | if (TREE_CODE_CLASS (lcode) != '<' | |
2630 | || TREE_CODE_CLASS (rcode) != '<') | |
2631 | return 0; | |
2632 | ||
2633 | code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR) | |
2634 | ? TRUTH_AND_EXPR : TRUTH_OR_EXPR); | |
2635 | ||
2636 | ll_arg = TREE_OPERAND (lhs, 0); | |
2637 | lr_arg = TREE_OPERAND (lhs, 1); | |
2638 | rl_arg = TREE_OPERAND (rhs, 0); | |
2639 | rr_arg = TREE_OPERAND (rhs, 1); | |
2640 | ||
2641 | if (TREE_CODE (lr_arg) == INTEGER_CST | |
2642 | && TREE_CODE (rr_arg) == INTEGER_CST | |
2643 | && operand_equal_p (ll_arg, rl_arg, 0)) | |
2644 | { | |
2645 | if (tree_int_cst_lt (lr_arg, rr_arg)) | |
2646 | result = range_test (code, truth_type, lcode, rcode, | |
2647 | ll_arg, lr_arg, rr_arg); | |
2648 | else | |
2649 | result = range_test (code, truth_type, rcode, lcode, | |
2650 | ll_arg, rr_arg, lr_arg); | |
2651 | ||
2652 | /* If this isn't a range test, it also isn't a comparison that | |
2653 | can be merged. However, it wins to evaluate the RHS unconditionally | |
2654 | on machines with expensive branches. */ | |
2655 | ||
2656 | if (result == 0 && BRANCH_COST >= 2) | |
2657 | { | |
2658 | if (TREE_CODE (ll_arg) != VAR_DECL | |
2659 | && TREE_CODE (ll_arg) != PARM_DECL) | |
2660 | { | |
2661 | /* Avoid evaluating the variable part twice. */ | |
2662 | ll_arg = save_expr (ll_arg); | |
2663 | lhs = build (lcode, TREE_TYPE (lhs), ll_arg, lr_arg); | |
2664 | rhs = build (rcode, TREE_TYPE (rhs), ll_arg, rr_arg); | |
2665 | } | |
2666 | return build (code, truth_type, lhs, rhs); | |
2667 | } | |
2668 | return result; | |
2669 | } | |
2670 | ||
2671 | /* If the RHS can be evaluated unconditionally and its operands are | |
2672 | simple, it wins to evaluate the RHS unconditionally on machines | |
2673 | with expensive branches. In this case, this isn't a comparison | |
2674 | that can be merged. */ | |
2675 | ||
2676 | /* @@ I'm not sure it wins on the m88110 to do this if the comparisons | |
2677 | are with zero (tmw). */ | |
2678 | ||
2679 | if (BRANCH_COST >= 2 | |
2680 | && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE | |
2681 | && simple_operand_p (rl_arg) | |
2682 | && simple_operand_p (rr_arg)) | |
2683 | return build (code, truth_type, lhs, rhs); | |
2684 | ||
2685 | /* See if the comparisons can be merged. Then get all the parameters for | |
2686 | each side. */ | |
2687 | ||
2688 | if ((lcode != EQ_EXPR && lcode != NE_EXPR) | |
2689 | || (rcode != EQ_EXPR && rcode != NE_EXPR)) | |
2690 | return 0; | |
2691 | ||
2692 | volatilep = 0; | |
2693 | ll_inner = decode_field_reference (ll_arg, | |
2694 | &ll_bitsize, &ll_bitpos, &ll_mode, | |
2695 | &ll_unsignedp, &volatilep, &ll_mask); | |
2696 | lr_inner = decode_field_reference (lr_arg, | |
2697 | &lr_bitsize, &lr_bitpos, &lr_mode, | |
2698 | &lr_unsignedp, &volatilep, &lr_mask); | |
2699 | rl_inner = decode_field_reference (rl_arg, | |
2700 | &rl_bitsize, &rl_bitpos, &rl_mode, | |
2701 | &rl_unsignedp, &volatilep, &rl_mask); | |
2702 | rr_inner = decode_field_reference (rr_arg, | |
2703 | &rr_bitsize, &rr_bitpos, &rr_mode, | |
2704 | &rr_unsignedp, &volatilep, &rr_mask); | |
2705 | ||
2706 | /* It must be true that the inner operation on the lhs of each | |
2707 | comparison must be the same if we are to be able to do anything. | |
2708 | Then see if we have constants. If not, the same must be true for | |
2709 | the rhs's. */ | |
2710 | if (volatilep || ll_inner == 0 || rl_inner == 0 | |
2711 | || ! operand_equal_p (ll_inner, rl_inner, 0)) | |
2712 | return 0; | |
2713 | ||
2714 | if (TREE_CODE (lr_arg) == INTEGER_CST | |
2715 | && TREE_CODE (rr_arg) == INTEGER_CST) | |
2716 | l_const = lr_arg, r_const = rr_arg; | |
2717 | else if (lr_inner == 0 || rr_inner == 0 | |
2718 | || ! operand_equal_p (lr_inner, rr_inner, 0)) | |
2719 | return 0; | |
2720 | else | |
2721 | l_const = r_const = 0; | |
2722 | ||
2723 | /* If either comparison code is not correct for our logical operation, | |
2724 | fail. However, we can convert a one-bit comparison against zero into | |
2725 | the opposite comparison against that bit being set in the field. */ | |
2726 | ||
2727 | wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR); | |
2728 | if (lcode != wanted_code) | |
2729 | { | |
2730 | if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask)) | |
2731 | l_const = ll_mask; | |
2732 | else | |
2733 | return 0; | |
2734 | } | |
2735 | ||
2736 | if (rcode != wanted_code) | |
2737 | { | |
2738 | if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask)) | |
2739 | r_const = rl_mask; | |
2740 | else | |
2741 | return 0; | |
2742 | } | |
2743 | ||
2744 | /* See if we can find a mode that contains both fields being compared on | |
2745 | the left. If we can't, fail. Otherwise, update all constants and masks | |
2746 | to be relative to a field of that size. */ | |
2747 | first_bit = MIN (ll_bitpos, rl_bitpos); | |
2748 | end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize); | |
2749 | lnmode = get_best_mode (end_bit - first_bit, first_bit, | |
2750 | TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode, | |
2751 | volatilep); | |
2752 | if (lnmode == VOIDmode) | |
2753 | return 0; | |
2754 | ||
2755 | lnbitsize = GET_MODE_BITSIZE (lnmode); | |
2756 | lnbitpos = first_bit & ~ (lnbitsize - 1); | |
2757 | type = type_for_size (lnbitsize, 1); | |
2758 | xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos; | |
2759 | ||
2760 | #if BYTES_BIG_ENDIAN | |
2761 | xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize; | |
2762 | xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize; | |
2763 | #endif | |
2764 | ||
2765 | ll_mask = const_binop (LSHIFT_EXPR, convert (type, ll_mask), | |
2766 | size_int (xll_bitpos)); | |
2767 | rl_mask = const_binop (LSHIFT_EXPR, convert (type, rl_mask), | |
2768 | size_int (xrl_bitpos)); | |
2769 | ||
2770 | /* Make sure the constants are interpreted as unsigned, so we | |
2771 | don't have sign bits outside the range of their type. */ | |
2772 | ||
2773 | if (l_const) | |
2774 | { | |
2775 | l_const = convert (unsigned_type (TREE_TYPE (l_const)), l_const); | |
2776 | l_const = const_binop (LSHIFT_EXPR, convert (type, l_const), | |
2777 | size_int (xll_bitpos)); | |
2778 | } | |
2779 | if (r_const) | |
2780 | { | |
2781 | r_const = convert (unsigned_type (TREE_TYPE (r_const)), r_const); | |
2782 | r_const = const_binop (LSHIFT_EXPR, convert (type, r_const), | |
2783 | size_int (xrl_bitpos)); | |
2784 | } | |
2785 | ||
2786 | /* If the right sides are not constant, do the same for it. Also, | |
2787 | disallow this optimization if a size or signedness mismatch occurs | |
2788 | between the left and right sides. */ | |
2789 | if (l_const == 0) | |
2790 | { | |
2791 | if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize | |
2792 | || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp | |
2793 | /* Make sure the two fields on the right | |
2794 | correspond to the left without being swapped. */ | |
2795 | || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos) | |
2796 | return 0; | |
2797 | ||
2798 | first_bit = MIN (lr_bitpos, rr_bitpos); | |
2799 | end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize); | |
2800 | rnmode = get_best_mode (end_bit - first_bit, first_bit, | |
2801 | TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode, | |
2802 | volatilep); | |
2803 | if (rnmode == VOIDmode) | |
2804 | return 0; | |
2805 | ||
2806 | rnbitsize = GET_MODE_BITSIZE (rnmode); | |
2807 | rnbitpos = first_bit & ~ (rnbitsize - 1); | |
2808 | xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos; | |
2809 | ||
2810 | #if BYTES_BIG_ENDIAN | |
2811 | xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize; | |
2812 | xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize; | |
2813 | #endif | |
2814 | ||
2815 | lr_mask = const_binop (LSHIFT_EXPR, convert (type, lr_mask), | |
2816 | size_int (xlr_bitpos)); | |
2817 | rr_mask = const_binop (LSHIFT_EXPR, convert (type, rr_mask), | |
2818 | size_int (xrr_bitpos)); | |
2819 | ||
2820 | /* Make a mask that corresponds to both fields being compared. | |
2821 | Do this for both items being compared. If the masks agree, | |
2822 | we can do this by masking both and comparing the masked | |
2823 | results. */ | |
2824 | ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask); | |
2825 | lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask); | |
2826 | if (operand_equal_p (ll_mask, lr_mask, 0) && lnbitsize == rnbitsize) | |
2827 | { | |
2828 | lhs = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos, | |
2829 | ll_unsignedp || rl_unsignedp); | |
2830 | rhs = make_bit_field_ref (lr_inner, type, rnbitsize, rnbitpos, | |
2831 | lr_unsignedp || rr_unsignedp); | |
2832 | if (! all_ones_mask_p (ll_mask, lnbitsize)) | |
2833 | { | |
2834 | lhs = build (BIT_AND_EXPR, type, lhs, ll_mask); | |
2835 | rhs = build (BIT_AND_EXPR, type, rhs, ll_mask); | |
2836 | } | |
2837 | return build (wanted_code, truth_type, lhs, rhs); | |
2838 | } | |
2839 | ||
2840 | /* There is still another way we can do something: If both pairs of | |
2841 | fields being compared are adjacent, we may be able to make a wider | |
2842 | field containing them both. */ | |
2843 | if ((ll_bitsize + ll_bitpos == rl_bitpos | |
2844 | && lr_bitsize + lr_bitpos == rr_bitpos) | |
2845 | || (ll_bitpos == rl_bitpos + rl_bitsize | |
2846 | && lr_bitpos == rr_bitpos + rr_bitsize)) | |
2847 | return build (wanted_code, truth_type, | |
2848 | make_bit_field_ref (ll_inner, type, | |
2849 | ll_bitsize + rl_bitsize, | |
2850 | MIN (ll_bitpos, rl_bitpos), | |
2851 | ll_unsignedp), | |
2852 | make_bit_field_ref (lr_inner, type, | |
2853 | lr_bitsize + rr_bitsize, | |
2854 | MIN (lr_bitpos, rr_bitpos), | |
2855 | lr_unsignedp)); | |
2856 | ||
2857 | return 0; | |
2858 | } | |
2859 | ||
2860 | /* Handle the case of comparisons with constants. If there is something in | |
2861 | common between the masks, those bits of the constants must be the same. | |
2862 | If not, the condition is always false. Test for this to avoid generating | |
2863 | incorrect code below. */ | |
2864 | result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask); | |
2865 | if (! integer_zerop (result) | |
2866 | && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const), | |
2867 | const_binop (BIT_AND_EXPR, result, r_const)) != 1) | |
2868 | { | |
2869 | if (wanted_code == NE_EXPR) | |
2870 | { | |
2871 | warning ("`or' of unmatched not-equal tests is always 1"); | |
2872 | return convert (truth_type, integer_one_node); | |
2873 | } | |
2874 | else | |
2875 | { | |
2876 | warning ("`and' of mutually exclusive equal-tests is always zero"); | |
2877 | return convert (truth_type, integer_zero_node); | |
2878 | } | |
2879 | } | |
2880 | ||
2881 | /* Construct the expression we will return. First get the component | |
2882 | reference we will make. Unless the mask is all ones the width of | |
2883 | that field, perform the mask operation. Then compare with the | |
2884 | merged constant. */ | |
2885 | result = make_bit_field_ref (ll_inner, type, lnbitsize, lnbitpos, | |
2886 | ll_unsignedp || rl_unsignedp); | |
2887 | ||
2888 | ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask); | |
2889 | if (! all_ones_mask_p (ll_mask, lnbitsize)) | |
2890 | result = build (BIT_AND_EXPR, type, result, ll_mask); | |
2891 | ||
2892 | return build (wanted_code, truth_type, result, | |
2893 | const_binop (BIT_IOR_EXPR, l_const, r_const)); | |
2894 | } | |
2895 | \f | |
2896 | /* Perform constant folding and related simplification of EXPR. | |
2897 | The related simplifications include x*1 => x, x*0 => 0, etc., | |
2898 | and application of the associative law. | |
2899 | NOP_EXPR conversions may be removed freely (as long as we | |
2900 | are careful not to change the C type of the overall expression) | |
2901 | We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR, | |
2902 | but we can constant-fold them if they have constant operands. */ | |
2903 | ||
2904 | tree | |
2905 | fold (expr) | |
2906 | tree expr; | |
2907 | { | |
2908 | register tree t = expr; | |
2909 | tree t1 = NULL_TREE; | |
2910 | tree tem; | |
2911 | tree type = TREE_TYPE (expr); | |
2912 | register tree arg0, arg1; | |
2913 | register enum tree_code code = TREE_CODE (t); | |
2914 | register int kind; | |
2915 | int invert; | |
2916 | ||
2917 | /* WINS will be nonzero when the switch is done | |
2918 | if all operands are constant. */ | |
2919 | ||
2920 | int wins = 1; | |
2921 | ||
2922 | /* Return right away if already constant. */ | |
2923 | if (TREE_CONSTANT (t)) | |
2924 | { | |
2925 | if (code == CONST_DECL) | |
2926 | return DECL_INITIAL (t); | |
2927 | return t; | |
2928 | } | |
2929 | ||
2930 | kind = TREE_CODE_CLASS (code); | |
2931 | if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR) | |
2932 | { | |
2933 | /* Special case for conversion ops that can have fixed point args. */ | |
2934 | arg0 = TREE_OPERAND (t, 0); | |
2935 | ||
2936 | /* Don't use STRIP_NOPS, because signedness of argument type matters. */ | |
2937 | if (arg0 != 0) | |
2938 | STRIP_TYPE_NOPS (arg0); | |
2939 | ||
2940 | if (arg0 != 0 && TREE_CODE (arg0) != INTEGER_CST | |
2941 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
2942 | && TREE_CODE (arg0) != REAL_CST | |
2943 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
2944 | ) | |
2945 | /* Note that TREE_CONSTANT isn't enough: | |
2946 | static var addresses are constant but we can't | |
2947 | do arithmetic on them. */ | |
2948 | wins = 0; | |
2949 | } | |
2950 | else if (kind == 'e' || kind == '<' | |
2951 | || kind == '1' || kind == '2' || kind == 'r') | |
2952 | { | |
2953 | register int len = tree_code_length[(int) code]; | |
2954 | register int i; | |
2955 | for (i = 0; i < len; i++) | |
2956 | { | |
2957 | tree op = TREE_OPERAND (t, i); | |
2958 | ||
2959 | if (op == 0) | |
2960 | continue; /* Valid for CALL_EXPR, at least. */ | |
2961 | ||
2962 | /* Strip any conversions that don't change the mode. */ | |
2963 | STRIP_NOPS (op); | |
2964 | ||
2965 | if (TREE_CODE (op) != INTEGER_CST | |
2966 | #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
2967 | && TREE_CODE (op) != REAL_CST | |
2968 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
2969 | ) | |
2970 | /* Note that TREE_CONSTANT isn't enough: | |
2971 | static var addresses are constant but we can't | |
2972 | do arithmetic on them. */ | |
2973 | wins = 0; | |
2974 | ||
2975 | if (i == 0) | |
2976 | arg0 = op; | |
2977 | else if (i == 1) | |
2978 | arg1 = op; | |
2979 | } | |
2980 | } | |
2981 | ||
2982 | /* If this is a commutative operation, and ARG0 is a constant, move it | |
2983 | to ARG1 to reduce the number of tests below. */ | |
2984 | if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR | |
2985 | || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR | |
2986 | || code == BIT_AND_EXPR) | |
2987 | && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)) | |
2988 | { | |
2989 | tem = arg0; arg0 = arg1; arg1 = tem; | |
2990 | ||
2991 | tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1); | |
2992 | TREE_OPERAND (t, 1) = tem; | |
2993 | } | |
2994 | ||
2995 | /* Now WINS is set as described above, | |
2996 | ARG0 is the first operand of EXPR, | |
2997 | and ARG1 is the second operand (if it has more than one operand). | |
2998 | ||
2999 | First check for cases where an arithmetic operation is applied to a | |
3000 | compound, conditional, or comparison operation. Push the arithmetic | |
3001 | operation inside the compound or conditional to see if any folding | |
3002 | can then be done. Convert comparison to conditional for this purpose. | |
3003 | The also optimizes non-constant cases that used to be done in | |
3004 | expand_expr. */ | |
3005 | if (TREE_CODE_CLASS (code) == '1') | |
3006 | { | |
3007 | if (TREE_CODE (arg0) == COMPOUND_EXPR) | |
3008 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), | |
3009 | fold (build1 (code, type, TREE_OPERAND (arg0, 1)))); | |
3010 | else if (TREE_CODE (arg0) == COND_EXPR) | |
3011 | { | |
3012 | t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0), | |
3013 | fold (build1 (code, type, TREE_OPERAND (arg0, 1))), | |
3014 | fold (build1 (code, type, TREE_OPERAND (arg0, 2))))); | |
3015 | ||
3016 | /* If this was a conversion, and all we did was to move into | |
3017 | inside the COND_EXPR, bring it back out. Then return so we | |
3018 | don't get into an infinite recursion loop taking the conversion | |
3019 | out and then back in. */ | |
3020 | ||
3021 | if ((code == NOP_EXPR || code == CONVERT_EXPR | |
3022 | || code == NON_LVALUE_EXPR) | |
3023 | && TREE_CODE (t) == COND_EXPR | |
3024 | && TREE_CODE (TREE_OPERAND (t, 1)) == code | |
3025 | && TREE_CODE (TREE_OPERAND (t, 2)) == code | |
3026 | && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)) | |
3027 | == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))) | |
3028 | t = build1 (code, type, | |
3029 | build (COND_EXPR, | |
3030 | TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0)), | |
3031 | TREE_OPERAND (t, 0), | |
3032 | TREE_OPERAND (TREE_OPERAND (t, 1), 0), | |
3033 | TREE_OPERAND (TREE_OPERAND (t, 2), 0))); | |
3034 | return t; | |
3035 | } | |
3036 | else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') | |
3037 | return fold (build (COND_EXPR, type, arg0, | |
3038 | fold (build1 (code, type, integer_one_node)), | |
3039 | fold (build1 (code, type, integer_zero_node)))); | |
3040 | } | |
3041 | else if (TREE_CODE_CLASS (code) == '2') | |
3042 | { | |
3043 | if (TREE_CODE (arg1) == COMPOUND_EXPR) | |
3044 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), | |
3045 | fold (build (code, type, arg0, TREE_OPERAND (arg1, 1)))); | |
3046 | else if (TREE_CODE (arg1) == COND_EXPR | |
3047 | || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<') | |
3048 | { | |
3049 | tree test, true_value, false_value; | |
3050 | ||
3051 | if (TREE_CODE (arg1) == COND_EXPR) | |
3052 | { | |
3053 | test = TREE_OPERAND (arg1, 0); | |
3054 | true_value = TREE_OPERAND (arg1, 1); | |
3055 | false_value = TREE_OPERAND (arg1, 2); | |
3056 | } | |
3057 | else | |
3058 | { | |
3059 | test = arg1; | |
3060 | true_value = integer_one_node; | |
3061 | false_value = integer_zero_node; | |
3062 | } | |
3063 | ||
3064 | if (TREE_CODE (arg0) != VAR_DECL && TREE_CODE (arg0) != PARM_DECL) | |
3065 | arg0 = save_expr (arg0); | |
3066 | test = fold (build (COND_EXPR, type, test, | |
3067 | fold (build (code, type, arg0, true_value)), | |
3068 | fold (build (code, type, arg0, false_value)))); | |
3069 | if (TREE_CODE (arg0) == SAVE_EXPR) | |
3070 | return build (COMPOUND_EXPR, type, | |
3071 | convert (void_type_node, arg0), test); | |
3072 | else | |
3073 | return convert (type, test); | |
3074 | } | |
3075 | ||
3076 | else if (TREE_CODE (arg0) == COMPOUND_EXPR) | |
3077 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), | |
3078 | fold (build (code, type, TREE_OPERAND (arg0, 1), arg1))); | |
3079 | else if (TREE_CODE (arg0) == COND_EXPR | |
3080 | || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<') | |
3081 | { | |
3082 | tree test, true_value, false_value; | |
3083 | ||
3084 | if (TREE_CODE (arg0) == COND_EXPR) | |
3085 | { | |
3086 | test = TREE_OPERAND (arg0, 0); | |
3087 | true_value = TREE_OPERAND (arg0, 1); | |
3088 | false_value = TREE_OPERAND (arg0, 2); | |
3089 | } | |
3090 | else | |
3091 | { | |
3092 | test = arg0; | |
3093 | true_value = integer_one_node; | |
3094 | false_value = integer_zero_node; | |
3095 | } | |
3096 | ||
3097 | if (TREE_CODE (arg1) != VAR_DECL && TREE_CODE (arg1) != PARM_DECL) | |
3098 | arg1 = save_expr (arg1); | |
3099 | test = fold (build (COND_EXPR, type, test, | |
3100 | fold (build (code, type, true_value, arg1)), | |
3101 | fold (build (code, type, false_value, arg1)))); | |
3102 | if (TREE_CODE (arg1) == SAVE_EXPR) | |
3103 | return build (COMPOUND_EXPR, type, | |
3104 | convert (void_type_node, arg1), test); | |
3105 | else | |
3106 | return convert (type, test); | |
3107 | } | |
3108 | } | |
3109 | else if (TREE_CODE_CLASS (code) == '<' | |
3110 | && TREE_CODE (arg0) == COMPOUND_EXPR) | |
3111 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), | |
3112 | fold (build (code, type, TREE_OPERAND (arg0, 1), arg1))); | |
3113 | else if (TREE_CODE_CLASS (code) == '<' | |
3114 | && TREE_CODE (arg1) == COMPOUND_EXPR) | |
3115 | return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), | |
3116 | fold (build (code, type, arg0, TREE_OPERAND (arg1, 1)))); | |
3117 | ||
3118 | switch (code) | |
3119 | { | |
3120 | case INTEGER_CST: | |
3121 | case REAL_CST: | |
3122 | case STRING_CST: | |
3123 | case COMPLEX_CST: | |
3124 | case CONSTRUCTOR: | |
3125 | return t; | |
3126 | ||
3127 | case CONST_DECL: | |
3128 | return fold (DECL_INITIAL (t)); | |
3129 | ||
3130 | case NOP_EXPR: | |
3131 | case FLOAT_EXPR: | |
3132 | case CONVERT_EXPR: | |
3133 | case FIX_TRUNC_EXPR: | |
3134 | /* Other kinds of FIX are not handled properly by fold_convert. */ | |
3135 | /* Two conversions in a row are not needed unless: | |
3136 | - the intermediate type is narrower than both initial and final, or | |
3137 | - the intermediate type and innermost type differ in signedness, | |
3138 | and the outermost type is wider than the intermediate, or | |
3139 | - the initial type is a pointer type and the precisions of the | |
3140 | intermediate and final types differ, or | |
3141 | - the final type is a pointer type and the precisions of the | |
3142 | initial and intermediate types differ. */ | |
3143 | if ((TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR | |
3144 | || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR) | |
3145 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))) | |
3146 | > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))) | |
3147 | || | |
3148 | TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))) | |
3149 | > TYPE_PRECISION (TREE_TYPE (t))) | |
3150 | && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))) | |
3151 | == INTEGER_TYPE) | |
3152 | && (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) | |
3153 | == INTEGER_TYPE) | |
3154 | && (TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0))) | |
3155 | != TREE_UNSIGNED (TREE_OPERAND (TREE_OPERAND (t, 0), 0))) | |
3156 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))) | |
3157 | < TYPE_PRECISION (TREE_TYPE (t)))) | |
3158 | && ((TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (t, 0))) | |
3159 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))) | |
3160 | > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))))) | |
3161 | == | |
3162 | (TREE_UNSIGNED (TREE_TYPE (t)) | |
3163 | && (TYPE_PRECISION (TREE_TYPE (t)) | |
3164 | > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))))) | |
3165 | && ! ((TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))) | |
3166 | == POINTER_TYPE) | |
3167 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0))) | |
3168 | != TYPE_PRECISION (TREE_TYPE (t)))) | |
3169 | && ! (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE | |
3170 | && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))) | |
3171 | != TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (t, 0)))))) | |
3172 | return convert (TREE_TYPE (t), TREE_OPERAND (TREE_OPERAND (t, 0), 0)); | |
3173 | ||
3174 | if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR | |
3175 | && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) | |
3176 | /* Detect assigning a bitfield. */ | |
3177 | && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF | |
3178 | && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1)))) | |
3179 | { | |
3180 | /* Don't leave an assignment inside a conversion | |
3181 | unless assigning a bitfield. */ | |
3182 | tree prev = TREE_OPERAND (t, 0); | |
3183 | TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1); | |
3184 | /* First do the assignment, then return converted constant. */ | |
3185 | t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t)); | |
3186 | TREE_USED (t) = 1; | |
3187 | return t; | |
3188 | } | |
3189 | if (!wins) | |
3190 | { | |
3191 | TREE_CONSTANT (t) = TREE_CONSTANT (arg0); | |
3192 | return t; | |
3193 | } | |
3194 | return fold_convert (t, arg0); | |
3195 | ||
3196 | #if 0 /* This loses on &"foo"[0]. */ | |
3197 | case ARRAY_REF: | |
3198 | { | |
3199 | int i; | |
3200 | ||
3201 | /* Fold an expression like: "foo"[2] */ | |
3202 | if (TREE_CODE (arg0) == STRING_CST | |
3203 | && TREE_CODE (arg1) == INTEGER_CST | |
3204 | && !TREE_INT_CST_HIGH (arg1) | |
3205 | && (i = TREE_INT_CST_LOW (arg1)) < TREE_STRING_LENGTH (arg0)) | |
3206 | { | |
3207 | t = build_int_2 (TREE_STRING_POINTER (arg0)[i], 0); | |
3208 | TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0)); | |
3209 | force_fit_type (t); | |
3210 | } | |
3211 | } | |
3212 | return t; | |
3213 | #endif /* 0 */ | |
3214 | ||
3215 | case RANGE_EXPR: | |
3216 | TREE_CONSTANT (t) = wins; | |
3217 | return t; | |
3218 | ||
3219 | case NEGATE_EXPR: | |
3220 | if (wins) | |
3221 | { | |
3222 | if (TREE_CODE (arg0) == INTEGER_CST) | |
3223 | { | |
3224 | HOST_WIDE_INT low, high; | |
3225 | int overflow = neg_double (TREE_INT_CST_LOW (arg0), | |
3226 | TREE_INT_CST_HIGH (arg0), | |
3227 | &low, &high); | |
3228 | t = build_int_2 (low, high); | |
3229 | TREE_CONSTANT_OVERFLOW (t) | |
3230 | = overflow | TREE_CONSTANT_OVERFLOW (arg0); | |
3231 | TREE_TYPE (t) = type; | |
3232 | force_fit_type (t); | |
3233 | } | |
3234 | else if (TREE_CODE (arg0) == REAL_CST) | |
3235 | t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0))); | |
3236 | TREE_TYPE (t) = type; | |
3237 | } | |
3238 | else if (TREE_CODE (arg0) == NEGATE_EXPR) | |
3239 | return TREE_OPERAND (arg0, 0); | |
3240 | ||
3241 | /* Convert - (a - b) to (b - a) for non-floating-point. */ | |
3242 | else if (TREE_CODE (arg0) == MINUS_EXPR && TREE_CODE (type) != REAL_TYPE) | |
3243 | return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1), | |
3244 | TREE_OPERAND (arg0, 0)); | |
3245 | ||
3246 | return t; | |
3247 | ||
3248 | case ABS_EXPR: | |
3249 | if (wins) | |
3250 | { | |
3251 | if (TREE_CODE (arg0) == INTEGER_CST) | |
3252 | { | |
3253 | if (! TREE_UNSIGNED (type) | |
3254 | && TREE_INT_CST_HIGH (arg0) < 0) | |
3255 | { | |
3256 | HOST_WIDE_INT low, high; | |
3257 | int overflow = neg_double (TREE_INT_CST_LOW (arg0), | |
3258 | TREE_INT_CST_HIGH (arg0), | |
3259 | &low, &high); | |
3260 | t = build_int_2 (low, high); | |
3261 | TREE_TYPE (t) = type; | |
3262 | force_fit_type (t, overflow); | |
3263 | } | |
3264 | } | |
3265 | else if (TREE_CODE (arg0) == REAL_CST) | |
3266 | { | |
3267 | if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0))) | |
3268 | t = build_real (type, | |
3269 | REAL_VALUE_NEGATE (TREE_REAL_CST (arg0))); | |
3270 | } | |
3271 | TREE_TYPE (t) = type; | |
3272 | } | |
3273 | else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR) | |
3274 | return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)); | |
3275 | return t; | |
3276 | ||
3277 | case BIT_NOT_EXPR: | |
3278 | if (wins) | |
3279 | { | |
3280 | if (TREE_CODE (arg0) == INTEGER_CST) | |
3281 | t = build_int_2 (~ TREE_INT_CST_LOW (arg0), | |
3282 | ~ TREE_INT_CST_HIGH (arg0)); | |
3283 | TREE_TYPE (t) = type; | |
3284 | force_fit_type (t); | |
3285 | TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0); | |
3286 | } | |
3287 | else if (TREE_CODE (arg0) == BIT_NOT_EXPR) | |
3288 | return TREE_OPERAND (arg0, 0); | |
3289 | return t; | |
3290 | ||
3291 | case PLUS_EXPR: | |
3292 | /* A + (-B) -> A - B */ | |
3293 | if (TREE_CODE (arg1) == NEGATE_EXPR) | |
3294 | return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0))); | |
3295 | else if (TREE_CODE (type) != REAL_TYPE) | |
3296 | { | |
3297 | if (integer_zerop (arg1)) | |
3298 | return non_lvalue (convert (type, arg0)); | |
3299 | ||
3300 | /* If we are adding two BIT_AND_EXPR's, both of which are and'ing | |
3301 | with a constant, and the two constants have no bits in common, | |
3302 | we should treat this as a BIT_IOR_EXPR since this may produce more | |
3303 | simplifications. */ | |
3304 | if (TREE_CODE (arg0) == BIT_AND_EXPR | |
3305 | && TREE_CODE (arg1) == BIT_AND_EXPR | |
3306 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
3307 | && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST | |
3308 | && integer_zerop (const_binop (BIT_AND_EXPR, | |
3309 | TREE_OPERAND (arg0, 1), | |
3310 | TREE_OPERAND (arg1, 1)))) | |
3311 | { | |
3312 | code = BIT_IOR_EXPR; | |
3313 | goto bit_ior; | |
3314 | } | |
3315 | } | |
3316 | /* In IEEE floating point, x+0 may not equal x. */ | |
3317 | else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
3318 | && real_zerop (arg1)) | |
3319 | return non_lvalue (convert (type, arg0)); | |
3320 | associate: | |
3321 | /* In most languages, can't associate operations on floats | |
3322 | through parentheses. Rather than remember where the parentheses | |
3323 | were, we don't associate floats at all. It shouldn't matter much. */ | |
3324 | if (TREE_CODE (type) == REAL_TYPE) | |
3325 | goto binary; | |
3326 | /* The varsign == -1 cases happen only for addition and subtraction. | |
3327 | It says that the arg that was split was really CON minus VAR. | |
3328 | The rest of the code applies to all associative operations. */ | |
3329 | if (!wins) | |
3330 | { | |
3331 | tree var, con; | |
3332 | int varsign; | |
3333 | ||
3334 | if (split_tree (arg0, code, &var, &con, &varsign)) | |
3335 | { | |
3336 | if (varsign == -1) | |
3337 | { | |
3338 | /* EXPR is (CON-VAR) +- ARG1. */ | |
3339 | /* If it is + and VAR==ARG1, return just CONST. */ | |
3340 | if (code == PLUS_EXPR && operand_equal_p (var, arg1, 0)) | |
3341 | return convert (TREE_TYPE (t), con); | |
3342 | ||
3343 | /* Otherwise return (CON +- ARG1) - VAR. */ | |
3344 | TREE_SET_CODE (t, MINUS_EXPR); | |
3345 | TREE_OPERAND (t, 1) = var; | |
3346 | TREE_OPERAND (t, 0) | |
3347 | = fold (build (code, TREE_TYPE (t), con, arg1)); | |
3348 | } | |
3349 | else | |
3350 | { | |
3351 | /* EXPR is (VAR+CON) +- ARG1. */ | |
3352 | /* If it is - and VAR==ARG1, return just CONST. */ | |
3353 | if (code == MINUS_EXPR && operand_equal_p (var, arg1, 0)) | |
3354 | return convert (TREE_TYPE (t), con); | |
3355 | ||
3356 | /* Otherwise return VAR +- (ARG1 +- CON). */ | |
3357 | TREE_OPERAND (t, 1) = tem | |
3358 | = fold (build (code, TREE_TYPE (t), arg1, con)); | |
3359 | TREE_OPERAND (t, 0) = var; | |
3360 | if (integer_zerop (tem) | |
3361 | && (code == PLUS_EXPR || code == MINUS_EXPR)) | |
3362 | return convert (type, var); | |
3363 | /* If we have x +/- (c - d) [c an explicit integer] | |
3364 | change it to x -/+ (d - c) since if d is relocatable | |
3365 | then the latter can be a single immediate insn | |
3366 | and the former cannot. */ | |
3367 | if (TREE_CODE (tem) == MINUS_EXPR | |
3368 | && TREE_CODE (TREE_OPERAND (tem, 0)) == INTEGER_CST) | |
3369 | { | |
3370 | tree tem1 = TREE_OPERAND (tem, 1); | |
3371 | TREE_OPERAND (tem, 1) = TREE_OPERAND (tem, 0); | |
3372 | TREE_OPERAND (tem, 0) = tem1; | |
3373 | TREE_SET_CODE (t, | |
3374 | (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR)); | |
3375 | } | |
3376 | } | |
3377 | return t; | |
3378 | } | |
3379 | ||
3380 | if (split_tree (arg1, code, &var, &con, &varsign)) | |
3381 | { | |
3382 | /* EXPR is ARG0 +- (CON +- VAR). */ | |
3383 | if (varsign == -1) | |
3384 | TREE_SET_CODE (t, | |
3385 | (code == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR)); | |
3386 | if (TREE_CODE (t) == MINUS_EXPR | |
3387 | && operand_equal_p (var, arg0, 0)) | |
3388 | { | |
3389 | /* If VAR and ARG0 cancel, return just CON or -CON. */ | |
3390 | if (code == PLUS_EXPR) | |
3391 | return convert (TREE_TYPE (t), con); | |
3392 | return fold (build1 (NEGATE_EXPR, TREE_TYPE (t), | |
3393 | convert (TREE_TYPE (t), con))); | |
3394 | } | |
3395 | TREE_OPERAND (t, 0) | |
3396 | = fold (build (code, TREE_TYPE (t), arg0, con)); | |
3397 | TREE_OPERAND (t, 1) = var; | |
3398 | if (integer_zerop (TREE_OPERAND (t, 0)) | |
3399 | && TREE_CODE (t) == PLUS_EXPR) | |
3400 | return convert (TREE_TYPE (t), var); | |
3401 | return t; | |
3402 | } | |
3403 | } | |
3404 | binary: | |
3405 | #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC) | |
3406 | if (TREE_CODE (arg1) == REAL_CST) | |
3407 | return t; | |
3408 | #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */ | |
3409 | if (wins) | |
3410 | t1 = const_binop (code, arg0, arg1); | |
3411 | if (t1 != NULL_TREE) | |
3412 | { | |
3413 | /* The return value should always have | |
3414 | the same type as the original expression. */ | |
3415 | TREE_TYPE (t1) = TREE_TYPE (t); | |
3416 | return t1; | |
3417 | } | |
3418 | return t; | |
3419 | ||
3420 | case MINUS_EXPR: | |
3421 | if (TREE_CODE (type) != REAL_TYPE) | |
3422 | { | |
3423 | if (! wins && integer_zerop (arg0)) | |
3424 | return build1 (NEGATE_EXPR, type, arg1); | |
3425 | if (integer_zerop (arg1)) | |
3426 | return non_lvalue (convert (type, arg0)); | |
3427 | } | |
3428 | /* Convert A - (-B) to A + B. */ | |
3429 | else if (TREE_CODE (arg1) == NEGATE_EXPR) | |
3430 | return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0))); | |
3431 | else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT) | |
3432 | { | |
3433 | /* Except with IEEE floating point, 0-x equals -x. */ | |
3434 | if (! wins && real_zerop (arg0)) | |
3435 | return build1 (NEGATE_EXPR, type, arg1); | |
3436 | /* Except with IEEE floating point, x-0 equals x. */ | |
3437 | if (real_zerop (arg1)) | |
3438 | return non_lvalue (convert (type, arg0)); | |
3439 | ||
3440 | /* Fold &x - &x. This can happen from &x.foo - &x. | |
3441 | This is unsafe for certain floats even in non-IEEE formats. | |
3442 | In IEEE, it is unsafe because it does wrong for NaNs. | |
3443 | Also note that operand_equal_p is always false if an operand | |
3444 | is volatile. */ | |
3445 | ||
3446 | if (operand_equal_p (arg0, arg1, | |
3447 | TREE_CODE (type) == REAL_TYPE)) | |
3448 | return convert (type, integer_zero_node); | |
3449 | } | |
3450 | goto associate; | |
3451 | ||
3452 | case MULT_EXPR: | |
3453 | if (TREE_CODE (type) != REAL_TYPE) | |
3454 | { | |
3455 | if (integer_zerop (arg1)) | |
3456 | return omit_one_operand (type, arg1, arg0); | |
3457 | if (integer_onep (arg1)) | |
3458 | return non_lvalue (convert (type, arg0)); | |
3459 | ||
3460 | /* (a * (1 << b)) is (a << b) */ | |
3461 | if (TREE_CODE (arg1) == LSHIFT_EXPR | |
3462 | && integer_onep (TREE_OPERAND (arg1, 0))) | |
3463 | return fold (build (LSHIFT_EXPR, type, arg0, | |
3464 | TREE_OPERAND (arg1, 1))); | |
3465 | if (TREE_CODE (arg0) == LSHIFT_EXPR | |
3466 | && integer_onep (TREE_OPERAND (arg0, 0))) | |
3467 | return fold (build (LSHIFT_EXPR, type, arg1, | |
3468 | TREE_OPERAND (arg0, 1))); | |
3469 | } | |
3470 | else | |
3471 | { | |
3472 | /* x*0 is 0, except for IEEE floating point. */ | |
3473 | if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
3474 | && real_zerop (arg1)) | |
3475 | return omit_one_operand (type, arg1, arg0); | |
3476 | /* In IEEE floating point, x*1 is not equivalent to x for snans. | |
3477 | However, ANSI says we can drop signals, | |
3478 | so we can do this anyway. */ | |
3479 | if (real_onep (arg1)) | |
3480 | return non_lvalue (convert (type, arg0)); | |
3481 | /* x*2 is x+x */ | |
3482 | if (! wins && real_twop (arg1)) | |
3483 | { | |
3484 | tree arg = save_expr (arg0); | |
3485 | return build (PLUS_EXPR, type, arg, arg); | |
3486 | } | |
3487 | } | |
3488 | goto associate; | |
3489 | ||
3490 | case BIT_IOR_EXPR: | |
3491 | bit_ior: | |
3492 | if (integer_all_onesp (arg1)) | |
3493 | return omit_one_operand (type, arg1, arg0); | |
3494 | if (integer_zerop (arg1)) | |
3495 | return non_lvalue (convert (type, arg0)); | |
3496 | t1 = distribute_bit_expr (code, type, arg0, arg1); | |
3497 | if (t1 != NULL_TREE) | |
3498 | return t1; | |
3499 | goto associate; | |
3500 | ||
3501 | case BIT_XOR_EXPR: | |
3502 | if (integer_zerop (arg1)) | |
3503 | return non_lvalue (convert (type, arg0)); | |
3504 | if (integer_all_onesp (arg1)) | |
3505 | return fold (build1 (BIT_NOT_EXPR, type, arg0)); | |
3506 | goto associate; | |
3507 | ||
3508 | case BIT_AND_EXPR: | |
3509 | bit_and: | |
3510 | if (integer_all_onesp (arg1)) | |
3511 | return non_lvalue (convert (type, arg0)); | |
3512 | if (integer_zerop (arg1)) | |
3513 | return omit_one_operand (type, arg1, arg0); | |
3514 | t1 = distribute_bit_expr (code, type, arg0, arg1); | |
3515 | if (t1 != NULL_TREE) | |
3516 | return t1; | |
3517 | /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */ | |
3518 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR | |
3519 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0)))) | |
3520 | { | |
3521 | int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0))); | |
3522 | if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT | |
3523 | && (~TREE_INT_CST_LOW (arg0) | |
3524 | & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0) | |
3525 | return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0)); | |
3526 | } | |
3527 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR | |
3528 | && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0)))) | |
3529 | { | |
3530 | int prec = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0))); | |
3531 | if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT | |
3532 | && (~TREE_INT_CST_LOW (arg1) | |
3533 | & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0) | |
3534 | return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0)); | |
3535 | } | |
3536 | goto associate; | |
3537 | ||
3538 | case BIT_ANDTC_EXPR: | |
3539 | if (integer_all_onesp (arg0)) | |
3540 | return non_lvalue (convert (type, arg1)); | |
3541 | if (integer_zerop (arg0)) | |
3542 | return omit_one_operand (type, arg0, arg1); | |
3543 | if (TREE_CODE (arg1) == INTEGER_CST) | |
3544 | { | |
3545 | arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1)); | |
3546 | code = BIT_AND_EXPR; | |
3547 | goto bit_and; | |
3548 | } | |
3549 | goto binary; | |
3550 | ||
3551 | case TRUNC_DIV_EXPR: | |
3552 | case ROUND_DIV_EXPR: | |
3553 | case FLOOR_DIV_EXPR: | |
3554 | case CEIL_DIV_EXPR: | |
3555 | case EXACT_DIV_EXPR: | |
3556 | case RDIV_EXPR: | |
3557 | if (integer_onep (arg1)) | |
3558 | return non_lvalue (convert (type, arg0)); | |
3559 | if (integer_zerop (arg1)) | |
3560 | return t; | |
3561 | ||
3562 | /* If we have ((a * C1) / C2) and C1 % C2 == 0, we can replace this with | |
3563 | (a * (C1/C2). Also look for when we have a SAVE_EXPR in | |
3564 | between. */ | |
3565 | if (TREE_CODE (arg1) == INTEGER_CST | |
3566 | && TREE_INT_CST_LOW (arg1) > 0 && TREE_INT_CST_HIGH (arg1) == 0 | |
3567 | && TREE_CODE (arg0) == MULT_EXPR | |
3568 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
3569 | && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) > 0 | |
3570 | && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0 | |
3571 | && 0 == (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) | |
3572 | % TREE_INT_CST_LOW (arg1))) | |
3573 | { | |
3574 | tree new_op | |
3575 | = build_int_2 (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) | |
3576 | / TREE_INT_CST_LOW (arg1), 0); | |
3577 | ||
3578 | TREE_TYPE (new_op) = type; | |
3579 | return build (MULT_EXPR, type, TREE_OPERAND (arg0, 0), new_op); | |
3580 | } | |
3581 | ||
3582 | else if (TREE_CODE (arg1) == INTEGER_CST | |
3583 | && TREE_INT_CST_LOW (arg1) > 0 && TREE_INT_CST_HIGH (arg1) == 0 | |
3584 | && TREE_CODE (arg0) == SAVE_EXPR | |
3585 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == MULT_EXPR | |
3586 | && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)) | |
3587 | == INTEGER_CST) | |
3588 | && (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)) | |
3589 | > 0) | |
3590 | && (TREE_INT_CST_HIGH (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)) | |
3591 | == 0) | |
3592 | && (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)) | |
3593 | % TREE_INT_CST_LOW (arg1)) == 0) | |
3594 | { | |
3595 | tree new_op | |
3596 | = build_int_2 (TREE_INT_CST_LOW (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)) | |
3597 | / TREE_INT_CST_LOW (arg1), 0); | |
3598 | ||
3599 | TREE_TYPE (new_op) = type; | |
3600 | return build (MULT_EXPR, type, | |
3601 | TREE_OPERAND (TREE_OPERAND (arg0, 0), 0), new_op); | |
3602 | } | |
3603 | ||
3604 | #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC) | |
3605 | #ifndef REAL_INFINITY | |
3606 | if (TREE_CODE (arg1) == REAL_CST | |
3607 | && real_zerop (arg1)) | |
3608 | return t; | |
3609 | #endif | |
3610 | #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */ | |
3611 | ||
3612 | goto binary; | |
3613 | ||
3614 | case CEIL_MOD_EXPR: | |
3615 | case FLOOR_MOD_EXPR: | |
3616 | case ROUND_MOD_EXPR: | |
3617 | case TRUNC_MOD_EXPR: | |
3618 | if (integer_onep (arg1)) | |
3619 | return omit_one_operand (type, integer_zero_node, arg0); | |
3620 | if (integer_zerop (arg1)) | |
3621 | return t; | |
3622 | goto binary; | |
3623 | ||
3624 | case LSHIFT_EXPR: | |
3625 | case RSHIFT_EXPR: | |
3626 | case LROTATE_EXPR: | |
3627 | case RROTATE_EXPR: | |
3628 | if (integer_zerop (arg1)) | |
3629 | return non_lvalue (convert (type, arg0)); | |
3630 | /* Since negative shift count is not well-defined, | |
3631 | don't try to compute it in the compiler. */ | |
3632 | if (tree_int_cst_lt (arg1, integer_zero_node)) | |
3633 | return t; | |
3634 | goto binary; | |
3635 | ||
3636 | case MIN_EXPR: | |
3637 | if (operand_equal_p (arg0, arg1, 0)) | |
3638 | return arg0; | |
3639 | if (TREE_CODE (type) == INTEGER_TYPE | |
3640 | && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1)) | |
3641 | return omit_one_operand (type, arg1, arg0); | |
3642 | goto associate; | |
3643 | ||
3644 | case MAX_EXPR: | |
3645 | if (operand_equal_p (arg0, arg1, 0)) | |
3646 | return arg0; | |
3647 | if (TREE_CODE (type) == INTEGER_TYPE | |
3648 | && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1)) | |
3649 | return omit_one_operand (type, arg1, arg0); | |
3650 | goto associate; | |
3651 | ||
3652 | case TRUTH_NOT_EXPR: | |
3653 | /* Note that the operand of this must be an int | |
3654 | and its values must be 0 or 1. | |
3655 | ("true" is a fixed value perhaps depending on the language, | |
3656 | but we don't handle values other than 1 correctly yet.) */ | |
3657 | return invert_truthvalue (arg0); | |
3658 | ||
3659 | case TRUTH_ANDIF_EXPR: | |
3660 | /* Note that the operands of this must be ints | |
3661 | and their values must be 0 or 1. | |
3662 | ("true" is a fixed value perhaps depending on the language.) */ | |
3663 | /* If first arg is constant zero, return it. */ | |
3664 | if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0)) | |
3665 | return arg0; | |
3666 | case TRUTH_AND_EXPR: | |
3667 | /* If either arg is constant true, drop it. */ | |
3668 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
3669 | return non_lvalue (arg1); | |
3670 | if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)) | |
3671 | return non_lvalue (arg0); | |
3672 | /* Both known to be zero => return zero. */ | |
3673 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
3674 | return arg0; | |
3675 | ||
3676 | truth_andor: | |
3677 | /* Check for the possibility of merging component references. If our | |
3678 | lhs is another similar operation, try to merge its rhs with our | |
3679 | rhs. Then try to merge our lhs and rhs. */ | |
3680 | if (optimize) | |
3681 | { | |
3682 | if (TREE_CODE (arg0) == code) | |
3683 | { | |
3684 | tem = fold_truthop (code, type, | |
3685 | TREE_OPERAND (arg0, 1), arg1); | |
3686 | if (tem) | |
3687 | return fold (build (code, type, TREE_OPERAND (arg0, 0), tem)); | |
3688 | } | |
3689 | ||
3690 | tem = fold_truthop (code, type, arg0, arg1); | |
3691 | if (tem) | |
3692 | return tem; | |
3693 | } | |
3694 | return t; | |
3695 | ||
3696 | case TRUTH_ORIF_EXPR: | |
3697 | /* Note that the operands of this must be ints | |
3698 | and their values must be 0 or true. | |
3699 | ("true" is a fixed value perhaps depending on the language.) */ | |
3700 | /* If first arg is constant true, return it. */ | |
3701 | if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0)) | |
3702 | return arg0; | |
3703 | case TRUTH_OR_EXPR: | |
3704 | /* If either arg is constant zero, drop it. */ | |
3705 | if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0)) | |
3706 | return non_lvalue (arg1); | |
3707 | if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)) | |
3708 | return non_lvalue (arg0); | |
3709 | /* Both known to be true => return true. */ | |
3710 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
3711 | return arg0; | |
3712 | goto truth_andor; | |
3713 | ||
3714 | case EQ_EXPR: | |
3715 | case NE_EXPR: | |
3716 | case LT_EXPR: | |
3717 | case GT_EXPR: | |
3718 | case LE_EXPR: | |
3719 | case GE_EXPR: | |
3720 | /* If one arg is a constant integer, put it last. */ | |
3721 | if (TREE_CODE (arg0) == INTEGER_CST | |
3722 | && TREE_CODE (arg1) != INTEGER_CST) | |
3723 | { | |
3724 | TREE_OPERAND (t, 0) = arg1; | |
3725 | TREE_OPERAND (t, 1) = arg0; | |
3726 | arg0 = TREE_OPERAND (t, 0); | |
3727 | arg1 = TREE_OPERAND (t, 1); | |
3728 | code = swap_tree_comparison (code); | |
3729 | TREE_SET_CODE (t, code); | |
3730 | } | |
3731 | ||
3732 | /* Convert foo++ == CONST into ++foo == CONST + INCR. | |
3733 | First, see if one arg is constant; find the constant arg | |
3734 | and the other one. */ | |
3735 | { | |
3736 | tree constop = 0, varop; | |
3737 | tree *constoploc; | |
3738 | ||
3739 | if (TREE_CONSTANT (arg1)) | |
3740 | constoploc = &TREE_OPERAND (t, 1), constop = arg1, varop = arg0; | |
3741 | if (TREE_CONSTANT (arg0)) | |
3742 | constoploc = &TREE_OPERAND (t, 0), constop = arg0, varop = arg1; | |
3743 | ||
3744 | if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR) | |
3745 | { | |
3746 | /* This optimization is invalid for ordered comparisons | |
3747 | if CONST+INCR overflows or if foo+incr might overflow. | |
3748 | This optimization is invalid for floating point due to rounding. | |
3749 | For pointer types we assume overflow doesn't happen. */ | |
3750 | if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE | |
3751 | || (TREE_CODE (TREE_TYPE (varop)) != REAL_TYPE | |
3752 | && (code == EQ_EXPR || code == NE_EXPR))) | |
3753 | { | |
3754 | tree newconst | |
3755 | = fold (build (PLUS_EXPR, TREE_TYPE (varop), | |
3756 | constop, TREE_OPERAND (varop, 1))); | |
3757 | TREE_SET_CODE (varop, PREINCREMENT_EXPR); | |
3758 | *constoploc = newconst; | |
3759 | return t; | |
3760 | } | |
3761 | } | |
3762 | else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR) | |
3763 | { | |
3764 | if (TREE_CODE (TREE_TYPE (varop)) == POINTER_TYPE | |
3765 | || (TREE_CODE (TREE_TYPE (varop)) != REAL_TYPE | |
3766 | && (code == EQ_EXPR || code == NE_EXPR))) | |
3767 | { | |
3768 | tree newconst | |
3769 | = fold (build (MINUS_EXPR, TREE_TYPE (varop), | |
3770 | constop, TREE_OPERAND (varop, 1))); | |
3771 | TREE_SET_CODE (varop, PREDECREMENT_EXPR); | |
3772 | *constoploc = newconst; | |
3773 | return t; | |
3774 | } | |
3775 | } | |
3776 | } | |
3777 | ||
3778 | /* Change X >= CST to X > (CST - 1) if CST is positive. */ | |
3779 | if (TREE_CODE (arg1) == INTEGER_CST | |
3780 | && TREE_CODE (arg0) != INTEGER_CST | |
3781 | && ! tree_int_cst_lt (arg1, integer_one_node)) | |
3782 | { | |
3783 | switch (TREE_CODE (t)) | |
3784 | { | |
3785 | case GE_EXPR: | |
3786 | code = GT_EXPR; | |
3787 | TREE_SET_CODE (t, code); | |
3788 | arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node); | |
3789 | TREE_OPERAND (t, 1) = arg1; | |
3790 | break; | |
3791 | ||
3792 | case LT_EXPR: | |
3793 | code = LE_EXPR; | |
3794 | TREE_SET_CODE (t, code); | |
3795 | arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node); | |
3796 | TREE_OPERAND (t, 1) = arg1; | |
3797 | } | |
3798 | } | |
3799 | ||
3800 | /* If this is an EQ or NE comparison with zero and ARG0 is | |
3801 | (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require | |
3802 | two operations, but the latter can be done in one less insn | |
3803 | one machine that have only two-operand insns or on which a | |
3804 | constant cannot be the first operand. */ | |
3805 | if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR) | |
3806 | && TREE_CODE (arg0) == BIT_AND_EXPR) | |
3807 | { | |
3808 | if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR | |
3809 | && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0))) | |
3810 | return | |
3811 | fold (build (code, type, | |
3812 | build (BIT_AND_EXPR, TREE_TYPE (arg0), | |
3813 | build (RSHIFT_EXPR, | |
3814 | TREE_TYPE (TREE_OPERAND (arg0, 0)), | |
3815 | TREE_OPERAND (arg0, 1), | |
3816 | TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)), | |
3817 | convert (TREE_TYPE (arg0), | |
3818 | integer_one_node)), | |
3819 | arg1)); | |
3820 | else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR | |
3821 | && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0))) | |
3822 | return | |
3823 | fold (build (code, type, | |
3824 | build (BIT_AND_EXPR, TREE_TYPE (arg0), | |
3825 | build (RSHIFT_EXPR, | |
3826 | TREE_TYPE (TREE_OPERAND (arg0, 1)), | |
3827 | TREE_OPERAND (arg0, 0), | |
3828 | TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)), | |
3829 | convert (TREE_TYPE (arg0), | |
3830 | integer_one_node)), | |
3831 | arg1)); | |
3832 | } | |
3833 | ||
3834 | /* If this is an NE comparison of zero with an AND of one, remove the | |
3835 | comparison since the AND will give the correct value. */ | |
3836 | if (code == NE_EXPR && integer_zerop (arg1) | |
3837 | && TREE_CODE (arg0) == BIT_AND_EXPR | |
3838 | && integer_onep (TREE_OPERAND (arg0, 1))) | |
3839 | return convert (type, arg0); | |
3840 | ||
3841 | /* If we have (A & C) == C where C is a power of 2, convert this into | |
3842 | (A & C) != 0. Similarly for NE_EXPR. */ | |
3843 | if ((code == EQ_EXPR || code == NE_EXPR) | |
3844 | && TREE_CODE (arg0) == BIT_AND_EXPR | |
3845 | && integer_pow2p (TREE_OPERAND (arg0, 1)) | |
3846 | && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)) | |
3847 | return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type, | |
3848 | arg0, integer_zero_node); | |
3849 | ||
3850 | /* Simplify comparison of something with itself. (For IEEE | |
3851 | floating-point, we can only do some of these simplifications.) */ | |
3852 | if (operand_equal_p (arg0, arg1, 0)) | |
3853 | { | |
3854 | switch (code) | |
3855 | { | |
3856 | case EQ_EXPR: | |
3857 | case GE_EXPR: | |
3858 | case LE_EXPR: | |
3859 | if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE) | |
3860 | { | |
3861 | t = build_int_2 (1, 0); | |
3862 | TREE_TYPE (t) = type; | |
3863 | return t; | |
3864 | } | |
3865 | code = EQ_EXPR; | |
3866 | TREE_SET_CODE (t, code); | |
3867 | break; | |
3868 | ||
3869 | case NE_EXPR: | |
3870 | /* For NE, we can only do this simplification if integer. */ | |
3871 | if (TREE_CODE (TREE_TYPE (arg0)) != INTEGER_TYPE) | |
3872 | break; | |
3873 | /* ... fall through ... */ | |
3874 | case GT_EXPR: | |
3875 | case LT_EXPR: | |
3876 | t = build_int_2 (0, 0); | |
3877 | TREE_TYPE (t) = type; | |
3878 | return t; | |
3879 | } | |
3880 | } | |
3881 | ||
3882 | /* An unsigned comparison against 0 can be simplified. */ | |
3883 | if (integer_zerop (arg1) | |
3884 | && (TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE | |
3885 | || TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE) | |
3886 | && TREE_UNSIGNED (TREE_TYPE (arg1))) | |
3887 | { | |
3888 | switch (TREE_CODE (t)) | |
3889 | { | |
3890 | case GT_EXPR: | |
3891 | code = NE_EXPR; | |
3892 | TREE_SET_CODE (t, NE_EXPR); | |
3893 | break; | |
3894 | case LE_EXPR: | |
3895 | code = EQ_EXPR; | |
3896 | TREE_SET_CODE (t, EQ_EXPR); | |
3897 | break; | |
3898 | case GE_EXPR: | |
3899 | return omit_one_operand (integer_type_node, | |
3900 | integer_one_node, arg0); | |
3901 | case LT_EXPR: | |
3902 | return omit_one_operand (integer_type_node, | |
3903 | integer_zero_node, arg0); | |
3904 | } | |
3905 | } | |
3906 | ||
3907 | /* If we are comparing an expression that just has comparisons | |
3908 | of two integer values, arithmetic expressions of those comparisons, | |
3909 | and constants, we can simplify it. There are only three cases | |
3910 | to check: the two values can either be equal, the first can be | |
3911 | greater, or the second can be greater. Fold the expression for | |
3912 | those three values. Since each value must be 0 or 1, we have | |
3913 | eight possibilities, each of which corresponds to the constant 0 | |
3914 | or 1 or one of the six possible comparisons. | |
3915 | ||
3916 | This handles common cases like (a > b) == 0 but also handles | |
3917 | expressions like ((x > y) - (y > x)) > 0, which supposedly | |
3918 | occur in macroized code. */ | |
3919 | ||
3920 | if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST) | |
3921 | { | |
3922 | tree cval1 = 0, cval2 = 0; | |
3923 | ||
3924 | if (twoval_comparison_p (arg0, &cval1, &cval2) | |
3925 | /* Don't handle degenerate cases here; they should already | |
3926 | have been handled anyway. */ | |
3927 | && cval1 != 0 && cval2 != 0 | |
3928 | && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2)) | |
3929 | && TREE_TYPE (cval1) == TREE_TYPE (cval2) | |
3930 | && TREE_CODE (TREE_TYPE (cval1)) == INTEGER_TYPE | |
3931 | && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)), | |
3932 | TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0)) | |
3933 | { | |
3934 | tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1)); | |
3935 | tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1)); | |
3936 | ||
3937 | /* We can't just pass T to eval_subst in case cval1 or cval2 | |
3938 | was the same as ARG1. */ | |
3939 | ||
3940 | tree high_result | |
3941 | = fold (build (code, type, | |
3942 | eval_subst (arg0, cval1, maxval, cval2, minval), | |
3943 | arg1)); | |
3944 | tree equal_result | |
3945 | = fold (build (code, type, | |
3946 | eval_subst (arg0, cval1, maxval, cval2, maxval), | |
3947 | arg1)); | |
3948 | tree low_result | |
3949 | = fold (build (code, type, | |
3950 | eval_subst (arg0, cval1, minval, cval2, maxval), | |
3951 | arg1)); | |
3952 | ||
3953 | /* All three of these results should be 0 or 1. Confirm they | |
3954 | are. Then use those values to select the proper code | |
3955 | to use. */ | |
3956 | ||
3957 | if ((integer_zerop (high_result) | |
3958 | || integer_onep (high_result)) | |
3959 | && (integer_zerop (equal_result) | |
3960 | || integer_onep (equal_result)) | |
3961 | && (integer_zerop (low_result) | |
3962 | || integer_onep (low_result))) | |
3963 | { | |
3964 | /* Make a 3-bit mask with the high-order bit being the | |
3965 | value for `>', the next for '=', and the low for '<'. */ | |
3966 | switch ((integer_onep (high_result) * 4) | |
3967 | + (integer_onep (equal_result) * 2) | |
3968 | + integer_onep (low_result)) | |
3969 | { | |
3970 | case 0: | |
3971 | /* Always false. */ | |
3972 | return omit_one_operand (type, integer_zero_node, arg0); | |
3973 | case 1: | |
3974 | code = LT_EXPR; | |
3975 | break; | |
3976 | case 2: | |
3977 | code = EQ_EXPR; | |
3978 | break; | |
3979 | case 3: | |
3980 | code = LE_EXPR; | |
3981 | break; | |
3982 | case 4: | |
3983 | code = GT_EXPR; | |
3984 | break; | |
3985 | case 5: | |
3986 | code = NE_EXPR; | |
3987 | break; | |
3988 | case 6: | |
3989 | code = GE_EXPR; | |
3990 | break; | |
3991 | case 7: | |
3992 | /* Always true. */ | |
3993 | return omit_one_operand (type, integer_one_node, arg0); | |
3994 | } | |
3995 | ||
3996 | return fold (build (code, type, cval1, cval2)); | |
3997 | } | |
3998 | } | |
3999 | } | |
4000 | ||
4001 | /* If this is a comparison of a field, we may be able to simplify it. */ | |
4002 | if ((TREE_CODE (arg0) == COMPONENT_REF | |
4003 | || TREE_CODE (arg0) == BIT_FIELD_REF) | |
4004 | && (code == EQ_EXPR || code == NE_EXPR) | |
4005 | /* Handle the constant case even without -O | |
4006 | to make sure the warnings are given. */ | |
4007 | && (optimize || TREE_CODE (arg1) == INTEGER_CST)) | |
4008 | { | |
4009 | t1 = optimize_bit_field_compare (code, type, arg0, arg1); | |
4010 | return t1 ? t1 : t; | |
4011 | } | |
4012 | ||
4013 | /* From here on, the only cases we handle are when the result is | |
4014 | known to be a constant. | |
4015 | ||
4016 | To compute GT, swap the arguments and do LT. | |
4017 | To compute GE, do LT and invert the result. | |
4018 | To compute LE, swap the arguments, do LT and invert the result. | |
4019 | To compute NE, do EQ and invert the result. | |
4020 | ||
4021 | Therefore, the code below must handle only EQ and LT. */ | |
4022 | ||
4023 | if (code == LE_EXPR || code == GT_EXPR) | |
4024 | { | |
4025 | tem = arg0, arg0 = arg1, arg1 = tem; | |
4026 | code = swap_tree_comparison (code); | |
4027 | } | |
4028 | ||
4029 | /* Note that it is safe to invert for real values here because we | |
4030 | will check below in the one case that it matters. */ | |
4031 | ||
4032 | invert = 0; | |
4033 | if (code == NE_EXPR || code == GE_EXPR) | |
4034 | { | |
4035 | invert = 1; | |
4036 | code = invert_tree_comparison (code); | |
4037 | } | |
4038 | ||
4039 | /* Compute a result for LT or EQ if args permit; | |
4040 | otherwise return T. */ | |
4041 | if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST) | |
4042 | { | |
4043 | if (code == EQ_EXPR) | |
4044 | t1 = build_int_2 ((TREE_INT_CST_LOW (arg0) | |
4045 | == TREE_INT_CST_LOW (arg1)) | |
4046 | && (TREE_INT_CST_HIGH (arg0) | |
4047 | == TREE_INT_CST_HIGH (arg1)), | |
4048 | 0); | |
4049 | else | |
4050 | t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0)) | |
4051 | ? INT_CST_LT_UNSIGNED (arg0, arg1) | |
4052 | : INT_CST_LT (arg0, arg1)), | |
4053 | 0); | |
4054 | } | |
4055 | ||
4056 | /* Assume a nonexplicit constant cannot equal an explicit one, | |
4057 | since such code would be undefined anyway. | |
4058 | Exception: on sysvr4, using #pragma weak, | |
4059 | a label can come out as 0. */ | |
4060 | else if (TREE_CODE (arg1) == INTEGER_CST | |
4061 | && !integer_zerop (arg1) | |
4062 | && TREE_CONSTANT (arg0) | |
4063 | && TREE_CODE (arg0) == ADDR_EXPR | |
4064 | && code == EQ_EXPR) | |
4065 | t1 = build_int_2 (0, 0); | |
4066 | ||
4067 | /* Two real constants can be compared explicitly. */ | |
4068 | else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST) | |
4069 | { | |
4070 | /* If either operand is a NaN, the result is false with two | |
4071 | exceptions: First, an NE_EXPR is true on NaNs, but that case | |
4072 | is already handled correctly since we will be inverting the | |
4073 | result for NE_EXPR. Second, if we had inverted a LE_EXPR | |
4074 | or a GE_EXPR into a LT_EXPR, we must return true so that it | |
4075 | will be inverted into false. */ | |
4076 | ||
4077 | if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0)) | |
4078 | || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))) | |
4079 | t1 = build_int_2 (invert && code == LT_EXPR, 0); | |
4080 | ||
4081 | else if (code == EQ_EXPR) | |
4082 | t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0), | |
4083 | TREE_REAL_CST (arg1)), | |
4084 | 0); | |
4085 | else | |
4086 | t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0), | |
4087 | TREE_REAL_CST (arg1)), | |
4088 | 0); | |
4089 | } | |
4090 | ||
4091 | if (t1 == NULL_TREE) | |
4092 | return t; | |
4093 | ||
4094 | if (invert) | |
4095 | TREE_INT_CST_LOW (t1) ^= 1; | |
4096 | ||
4097 | TREE_TYPE (t1) = type; | |
4098 | return t1; | |
4099 | ||
4100 | case COND_EXPR: | |
4101 | if (TREE_CODE (arg0) == INTEGER_CST) | |
4102 | return TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)); | |
4103 | else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0)) | |
4104 | return omit_one_operand (type, arg1, arg0); | |
4105 | ||
4106 | /* If the second operand is zero, invert the comparison and swap | |
4107 | the second and third operands. Likewise if the second operand | |
4108 | is constant and the third is not or if the third operand is | |
4109 | equivalent to the first operand of the comparison. */ | |
4110 | ||
4111 | if (integer_zerop (arg1) | |
4112 | || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2))) | |
4113 | || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<' | |
4114 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), | |
4115 | TREE_OPERAND (t, 2), | |
4116 | TREE_OPERAND (arg0, 1)))) | |
4117 | { | |
4118 | /* See if this can be inverted. If it can't, possibly because | |
4119 | it was a floating-point inequality comparison, don't do | |
4120 | anything. */ | |
4121 | tem = invert_truthvalue (arg0); | |
4122 | ||
4123 | if (TREE_CODE (tem) != TRUTH_NOT_EXPR) | |
4124 | { | |
4125 | arg0 = TREE_OPERAND (t, 0) = tem; | |
4126 | TREE_OPERAND (t, 1) = TREE_OPERAND (t, 2); | |
4127 | TREE_OPERAND (t, 2) = arg1; | |
4128 | arg1 = TREE_OPERAND (t, 1); | |
4129 | } | |
4130 | } | |
4131 | ||
4132 | /* If we have A op B ? A : C, we may be able to convert this to a | |
4133 | simpler expression, depending on the operation and the values | |
4134 | of B and C. IEEE floating point prevents this though, | |
4135 | because A or B might be -0.0 or a NaN. */ | |
4136 | ||
4137 | if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<' | |
4138 | && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT | |
4139 | || TREE_CODE (TREE_TYPE (TREE_OPERAND (arg0, 0))) != REAL_TYPE) | |
4140 | && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0), | |
4141 | arg1, TREE_OPERAND (arg0, 1))) | |
4142 | { | |
4143 | tree arg2 = TREE_OPERAND (t, 2); | |
4144 | enum tree_code comp_code = TREE_CODE (arg0); | |
4145 | ||
4146 | /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or abs (-A), | |
4147 | depending on the comparison operation. */ | |
4148 | if (integer_zerop (TREE_OPERAND (arg0, 1)) | |
4149 | && TREE_CODE (arg2) == NEGATE_EXPR | |
4150 | && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0)) | |
4151 | switch (comp_code) | |
4152 | { | |
4153 | case EQ_EXPR: | |
4154 | return fold (build1 (NEGATE_EXPR, type, arg1)); | |
4155 | case NE_EXPR: | |
4156 | return convert (type, arg1); | |
4157 | case GE_EXPR: | |
4158 | case GT_EXPR: | |
4159 | return fold (build1 (ABS_EXPR, type, arg1)); | |
4160 | case LE_EXPR: | |
4161 | case LT_EXPR: | |
4162 | return fold (build1 (NEGATE_EXPR, type, | |
4163 | fold (build1 (ABS_EXPR, type, arg1)))); | |
4164 | } | |
4165 | ||
4166 | /* If this is A != 0 ? A : 0, this is simply A. For ==, it is | |
4167 | always zero. */ | |
4168 | ||
4169 | if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2)) | |
4170 | { | |
4171 | if (comp_code == NE_EXPR) | |
4172 | return convert (type, arg1); | |
4173 | else if (comp_code == EQ_EXPR) | |
4174 | return convert (type, integer_zero_node); | |
4175 | } | |
4176 | ||
4177 | /* If this is A op B ? A : B, this is either A, B, min (A, B), | |
4178 | or max (A, B), depending on the operation. */ | |
4179 | ||
4180 | if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1), | |
4181 | arg2, TREE_OPERAND (arg0, 0))) | |
4182 | switch (comp_code) | |
4183 | { | |
4184 | case EQ_EXPR: | |
4185 | return convert (type, arg2); | |
4186 | case NE_EXPR: | |
4187 | return convert (type, arg1); | |
4188 | case LE_EXPR: | |
4189 | case LT_EXPR: | |
4190 | return fold (build (MIN_EXPR, type, arg1, arg2)); | |
4191 | case GE_EXPR: | |
4192 | case GT_EXPR: | |
4193 | return fold (build (MAX_EXPR, type, arg1, arg2)); | |
4194 | } | |
4195 | ||
4196 | /* If this is A op C1 ? A : C2 with C1 and C2 constant integers, | |
4197 | we might still be able to simplify this. For example, | |
4198 | if C1 is one less or one more than C2, this might have started | |
4199 | out as a MIN or MAX and been transformed by this function. | |
4200 | Only good for INTEGER_TYPE, because we need TYPE_MAX_VALUE. */ | |
4201 | ||
4202 | if (TREE_CODE (type) == INTEGER_TYPE | |
4203 | && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST | |
4204 | && TREE_CODE (arg2) == INTEGER_CST) | |
4205 | switch (comp_code) | |
4206 | { | |
4207 | case EQ_EXPR: | |
4208 | /* We can replace A with C1 in this case. */ | |
4209 | arg1 = TREE_OPERAND (t, 1) | |
4210 | = convert (type, TREE_OPERAND (arg0, 1)); | |
4211 | break; | |
4212 | ||
4213 | case LT_EXPR: | |
4214 | /* If C1 is C2 + 1, this is min(A, C2). */ | |
4215 | if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1) | |
4216 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
4217 | const_binop (PLUS_EXPR, arg2, | |
4218 | integer_one_node), 1)) | |
4219 | return fold (build (MIN_EXPR, type, arg1, arg2)); | |
4220 | break; | |
4221 | ||
4222 | case LE_EXPR: | |
4223 | /* If C1 is C2 - 1, this is min(A, C2). */ | |
4224 | if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1) | |
4225 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
4226 | const_binop (MINUS_EXPR, arg2, | |
4227 | integer_one_node), 1)) | |
4228 | return fold (build (MIN_EXPR, type, arg1, arg2)); | |
4229 | break; | |
4230 | ||
4231 | case GT_EXPR: | |
4232 | /* If C1 is C2 - 1, this is max(A, C2). */ | |
4233 | if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1) | |
4234 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
4235 | const_binop (MINUS_EXPR, arg2, | |
4236 | integer_one_node), 1)) | |
4237 | return fold (build (MAX_EXPR, type, arg1, arg2)); | |
4238 | break; | |
4239 | ||
4240 | case GE_EXPR: | |
4241 | /* If C1 is C2 + 1, this is max(A, C2). */ | |
4242 | if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1) | |
4243 | && operand_equal_p (TREE_OPERAND (arg0, 1), | |
4244 | const_binop (PLUS_EXPR, arg2, | |
4245 | integer_one_node), 1)) | |
4246 | return fold (build (MAX_EXPR, type, arg1, arg2)); | |
4247 | break; | |
4248 | } | |
4249 | } | |
4250 | ||
4251 | /* Convert A ? 1 : 0 to simply A. */ | |
4252 | if (integer_onep (TREE_OPERAND (t, 1)) | |
4253 | && integer_zerop (TREE_OPERAND (t, 2)) | |
4254 | /* If we try to convert TREE_OPERAND (t, 0) to our type, the | |
4255 | call to fold will try to move the conversion inside | |
4256 | a COND, which will recurse. In that case, the COND_EXPR | |
4257 | is probably the best choice, so leave it alone. */ | |
4258 | && type == TREE_TYPE (arg0)) | |
4259 | return arg0; | |
4260 | ||
4261 | ||
4262 | /* Look for expressions of the form A & 2 ? 2 : 0. The result of this | |
4263 | operation is simply A & 2. */ | |
4264 | ||
4265 | if (integer_zerop (TREE_OPERAND (t, 2)) | |
4266 | && TREE_CODE (arg0) == NE_EXPR | |
4267 | && integer_zerop (TREE_OPERAND (arg0, 1)) | |
4268 | && integer_pow2p (arg1) | |
4269 | && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR | |
4270 | && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1), | |
4271 | arg1, 1)) | |
4272 | return convert (type, TREE_OPERAND (arg0, 0)); | |
4273 | ||
4274 | return t; | |
4275 | ||
4276 | case COMPOUND_EXPR: | |
4277 | if (!TREE_SIDE_EFFECTS (arg0)) | |
4278 | return arg1; | |
4279 | return t; | |
4280 | ||
4281 | default: | |
4282 | return t; | |
4283 | } /* switch (code) */ | |
4284 | } |