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