1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 /*@@ This file should be rewritten to use an arbitrary precision
23 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25 @@ The routines that translate from the ap rep should
26 @@ warn if precision et. al. is lost.
27 @@ This would also make life easier when this technology is used
28 @@ for cross-compilers. */
30 /* The entry points in this file are fold, size_int_wide, size_binop
33 fold takes a tree as argument and returns a simplified tree.
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'.
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'.
42 force_fit_type takes a constant and prior overflow indicator, and
43 forces the value to fit the type. It returns an overflow indicator. */
56 static void encode PARAMS ((HOST_WIDE_INT *,
57 unsigned HOST_WIDE_INT,
59 static void decode PARAMS ((HOST_WIDE_INT *,
60 unsigned HOST_WIDE_INT *,
62 #ifndef REAL_ARITHMETIC
63 static void exact_real_inverse_1 PARAMS ((PTR));
65 static tree negate_expr PARAMS ((tree));
66 static tree split_tree PARAMS ((tree, enum tree_code, tree *, tree *,
68 static tree associate_trees PARAMS ((tree, tree, enum tree_code, tree));
69 static tree int_const_binop PARAMS ((enum tree_code, tree, tree, int));
70 static void const_binop_1 PARAMS ((PTR));
71 static tree const_binop PARAMS ((enum tree_code, tree, tree, int));
72 static hashval_t size_htab_hash PARAMS ((const void *));
73 static int size_htab_eq PARAMS ((const void *, const void *));
74 static void fold_convert_1 PARAMS ((PTR));
75 static tree fold_convert PARAMS ((tree, tree));
76 static enum tree_code invert_tree_comparison PARAMS ((enum tree_code));
77 static enum tree_code swap_tree_comparison PARAMS ((enum tree_code));
78 static int truth_value_p PARAMS ((enum tree_code));
79 static int operand_equal_for_comparison_p PARAMS ((tree, tree, tree));
80 static int twoval_comparison_p PARAMS ((tree, tree *, tree *, int *));
81 static tree eval_subst PARAMS ((tree, tree, tree, tree, tree));
82 static tree omit_one_operand PARAMS ((tree, tree, tree));
83 static tree pedantic_omit_one_operand PARAMS ((tree, tree, tree));
84 static tree distribute_bit_expr PARAMS ((enum tree_code, tree, tree, tree));
85 static tree make_bit_field_ref PARAMS ((tree, tree, int, int, int));
86 static tree optimize_bit_field_compare PARAMS ((enum tree_code, tree,
88 static tree decode_field_reference PARAMS ((tree, HOST_WIDE_INT *,
90 enum machine_mode *, int *,
91 int *, tree *, tree *));
92 static int all_ones_mask_p PARAMS ((tree, int));
93 static int simple_operand_p PARAMS ((tree));
94 static tree range_binop PARAMS ((enum tree_code, tree, tree, int,
96 static tree make_range PARAMS ((tree, int *, tree *, tree *));
97 static tree build_range_check PARAMS ((tree, tree, int, tree, tree));
98 static int merge_ranges PARAMS ((int *, tree *, tree *, int, tree, tree,
100 static tree fold_range_test PARAMS ((tree));
101 static tree unextend PARAMS ((tree, int, int, tree));
102 static tree fold_truthop PARAMS ((enum tree_code, tree, tree, tree));
103 static tree optimize_minmax_comparison PARAMS ((tree));
104 static tree extract_muldiv PARAMS ((tree, tree, enum tree_code, tree));
105 static tree strip_compound_expr PARAMS ((tree, tree));
106 static int multiple_of_p PARAMS ((tree, tree, tree));
107 static tree constant_boolean_node PARAMS ((int, tree));
108 static int count_cond PARAMS ((tree, int));
109 static tree fold_binary_op_with_conditional_arg
110 PARAMS ((enum tree_code, tree, tree, tree, int));
112 #if defined(HOST_EBCDIC)
113 /* bit 8 is significant in EBCDIC */
114 #define CHARMASK 0xff
116 #define CHARMASK 0x7f
119 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
120 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
121 and SUM1. Then this yields nonzero if overflow occurred during the
124 Overflow occurs if A and B have the same sign, but A and SUM differ in
125 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
127 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
129 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
130 We do that by representing the two-word integer in 4 words, with only
131 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
132 number. The value of the word is LOWPART + HIGHPART * BASE. */
135 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
136 #define HIGHPART(x) \
137 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
138 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
140 /* Unpack a two-word integer into 4 words.
141 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
142 WORDS points to the array of HOST_WIDE_INTs. */
145 encode (words, low, hi)
146 HOST_WIDE_INT *words;
147 unsigned HOST_WIDE_INT low;
150 words[0] = LOWPART (low);
151 words[1] = HIGHPART (low);
152 words[2] = LOWPART (hi);
153 words[3] = HIGHPART (hi);
156 /* Pack an array of 4 words into a two-word integer.
157 WORDS points to the array of words.
158 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
161 decode (words, low, hi)
162 HOST_WIDE_INT *words;
163 unsigned HOST_WIDE_INT *low;
166 *low = words[0] + words[1] * BASE;
167 *hi = words[2] + words[3] * BASE;
170 /* Make the integer constant T valid for its type by setting to 0 or 1 all
171 the bits in the constant that don't belong in the type.
173 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
174 nonzero, a signed overflow has already occurred in calculating T, so
177 Make the real constant T valid for its type by calling CHECK_FLOAT_VALUE,
181 force_fit_type (t, overflow)
185 unsigned HOST_WIDE_INT low;
189 if (TREE_CODE (t) == REAL_CST)
191 #ifdef CHECK_FLOAT_VALUE
192 CHECK_FLOAT_VALUE (TYPE_MODE (TREE_TYPE (t)), TREE_REAL_CST (t),
198 else if (TREE_CODE (t) != INTEGER_CST)
201 low = TREE_INT_CST_LOW (t);
202 high = TREE_INT_CST_HIGH (t);
204 if (POINTER_TYPE_P (TREE_TYPE (t)))
207 prec = TYPE_PRECISION (TREE_TYPE (t));
209 /* First clear all bits that are beyond the type's precision. */
211 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
213 else if (prec > HOST_BITS_PER_WIDE_INT)
214 TREE_INT_CST_HIGH (t)
215 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
218 TREE_INT_CST_HIGH (t) = 0;
219 if (prec < HOST_BITS_PER_WIDE_INT)
220 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
223 /* Unsigned types do not suffer sign extension or overflow unless they
225 if (TREE_UNSIGNED (TREE_TYPE (t))
226 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
227 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
230 /* If the value's sign bit is set, extend the sign. */
231 if (prec != 2 * HOST_BITS_PER_WIDE_INT
232 && (prec > HOST_BITS_PER_WIDE_INT
233 ? 0 != (TREE_INT_CST_HIGH (t)
235 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
236 : 0 != (TREE_INT_CST_LOW (t)
237 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
239 /* Value is negative:
240 set to 1 all the bits that are outside this type's precision. */
241 if (prec > HOST_BITS_PER_WIDE_INT)
242 TREE_INT_CST_HIGH (t)
243 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
246 TREE_INT_CST_HIGH (t) = -1;
247 if (prec < HOST_BITS_PER_WIDE_INT)
248 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
252 /* Return nonzero if signed overflow occurred. */
254 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
258 /* Add two doubleword integers with doubleword result.
259 Each argument is given as two `HOST_WIDE_INT' pieces.
260 One argument is L1 and H1; the other, L2 and H2.
261 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
264 add_double (l1, h1, l2, h2, lv, hv)
265 unsigned HOST_WIDE_INT l1, l2;
266 HOST_WIDE_INT h1, h2;
267 unsigned HOST_WIDE_INT *lv;
270 unsigned HOST_WIDE_INT l;
274 h = h1 + h2 + (l < l1);
278 return OVERFLOW_SUM_SIGN (h1, h2, h);
281 /* Negate a doubleword integer with doubleword result.
282 Return nonzero if the operation overflows, assuming it's signed.
283 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
284 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
287 neg_double (l1, h1, lv, hv)
288 unsigned HOST_WIDE_INT l1;
290 unsigned HOST_WIDE_INT *lv;
297 return (*hv & h1) < 0;
307 /* Multiply two doubleword integers with doubleword result.
308 Return nonzero if the operation overflows, assuming it's signed.
309 Each argument is given as two `HOST_WIDE_INT' pieces.
310 One argument is L1 and H1; the other, L2 and H2.
311 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
314 mul_double (l1, h1, l2, h2, lv, hv)
315 unsigned HOST_WIDE_INT l1, l2;
316 HOST_WIDE_INT h1, h2;
317 unsigned HOST_WIDE_INT *lv;
320 HOST_WIDE_INT arg1[4];
321 HOST_WIDE_INT arg2[4];
322 HOST_WIDE_INT prod[4 * 2];
323 unsigned HOST_WIDE_INT carry;
325 unsigned HOST_WIDE_INT toplow, neglow;
326 HOST_WIDE_INT tophigh, neghigh;
328 encode (arg1, l1, h1);
329 encode (arg2, l2, h2);
331 memset ((char *) prod, 0, sizeof prod);
333 for (i = 0; i < 4; i++)
336 for (j = 0; j < 4; j++)
339 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
340 carry += arg1[i] * arg2[j];
341 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
343 prod[k] = LOWPART (carry);
344 carry = HIGHPART (carry);
349 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
351 /* Check for overflow by calculating the top half of the answer in full;
352 it should agree with the low half's sign bit. */
353 decode (prod + 4, &toplow, &tophigh);
356 neg_double (l2, h2, &neglow, &neghigh);
357 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
361 neg_double (l1, h1, &neglow, &neghigh);
362 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
364 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
367 /* Shift the doubleword integer in L1, H1 left by COUNT places
368 keeping only PREC bits of result.
369 Shift right if COUNT is negative.
370 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
371 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
374 lshift_double (l1, h1, count, prec, lv, hv, arith)
375 unsigned HOST_WIDE_INT l1;
376 HOST_WIDE_INT h1, count;
378 unsigned HOST_WIDE_INT *lv;
382 unsigned HOST_WIDE_INT signmask;
386 rshift_double (l1, h1, -count, prec, lv, hv, arith);
390 #ifdef SHIFT_COUNT_TRUNCATED
391 if (SHIFT_COUNT_TRUNCATED)
395 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
397 /* Shifting by the host word size is undefined according to the
398 ANSI standard, so we must handle this as a special case. */
402 else if (count >= HOST_BITS_PER_WIDE_INT)
404 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
409 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
410 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
414 /* Sign extend all bits that are beyond the precision. */
416 signmask = -((prec > HOST_BITS_PER_WIDE_INT
417 ? (*hv >> (prec - HOST_BITS_PER_WIDE_INT - 1))
418 : (*lv >> (prec - 1))) & 1);
420 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
422 else if (prec >= HOST_BITS_PER_WIDE_INT)
424 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
425 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
430 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
431 *lv |= signmask << prec;
435 /* Shift the doubleword integer in L1, H1 right by COUNT places
436 keeping only PREC bits of result. COUNT must be positive.
437 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
438 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
441 rshift_double (l1, h1, count, prec, lv, hv, arith)
442 unsigned HOST_WIDE_INT l1;
443 HOST_WIDE_INT h1, count;
445 unsigned HOST_WIDE_INT *lv;
449 unsigned HOST_WIDE_INT signmask;
452 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
455 #ifdef SHIFT_COUNT_TRUNCATED
456 if (SHIFT_COUNT_TRUNCATED)
460 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
462 /* Shifting by the host word size is undefined according to the
463 ANSI standard, so we must handle this as a special case. */
467 else if (count >= HOST_BITS_PER_WIDE_INT)
470 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
474 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
476 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
479 /* Zero / sign extend all bits that are beyond the precision. */
481 if (count >= (HOST_WIDE_INT)prec)
486 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
488 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
490 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
491 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
496 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
497 *lv |= signmask << (prec - count);
501 /* Rotate the doubleword integer in L1, H1 left by COUNT places
502 keeping only PREC bits of result.
503 Rotate right if COUNT is negative.
504 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
507 lrotate_double (l1, h1, count, prec, lv, hv)
508 unsigned HOST_WIDE_INT l1;
509 HOST_WIDE_INT h1, count;
511 unsigned HOST_WIDE_INT *lv;
514 unsigned HOST_WIDE_INT s1l, s2l;
515 HOST_WIDE_INT s1h, s2h;
521 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
522 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
527 /* Rotate the doubleword integer in L1, H1 left by COUNT places
528 keeping only PREC bits of result. COUNT must be positive.
529 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
532 rrotate_double (l1, h1, count, prec, lv, hv)
533 unsigned HOST_WIDE_INT l1;
534 HOST_WIDE_INT h1, count;
536 unsigned HOST_WIDE_INT *lv;
539 unsigned HOST_WIDE_INT s1l, s2l;
540 HOST_WIDE_INT s1h, s2h;
546 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
547 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
552 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
553 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
554 CODE is a tree code for a kind of division, one of
555 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
557 It controls how the quotient is rounded to an integer.
558 Return nonzero if the operation overflows.
559 UNS nonzero says do unsigned division. */
562 div_and_round_double (code, uns,
563 lnum_orig, hnum_orig, lden_orig, hden_orig,
564 lquo, hquo, lrem, hrem)
567 unsigned HOST_WIDE_INT lnum_orig; /* num == numerator == dividend */
568 HOST_WIDE_INT hnum_orig;
569 unsigned HOST_WIDE_INT lden_orig; /* den == denominator == divisor */
570 HOST_WIDE_INT hden_orig;
571 unsigned HOST_WIDE_INT *lquo, *lrem;
572 HOST_WIDE_INT *hquo, *hrem;
575 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
576 HOST_WIDE_INT den[4], quo[4];
578 unsigned HOST_WIDE_INT work;
579 unsigned HOST_WIDE_INT carry = 0;
580 unsigned HOST_WIDE_INT lnum = lnum_orig;
581 HOST_WIDE_INT hnum = hnum_orig;
582 unsigned HOST_WIDE_INT lden = lden_orig;
583 HOST_WIDE_INT hden = hden_orig;
586 if (hden == 0 && lden == 0)
587 overflow = 1, lden = 1;
589 /* calculate quotient sign and convert operands to unsigned. */
595 /* (minimum integer) / (-1) is the only overflow case. */
596 if (neg_double (lnum, hnum, &lnum, &hnum)
597 && ((HOST_WIDE_INT) lden & hden) == -1)
603 neg_double (lden, hden, &lden, &hden);
607 if (hnum == 0 && hden == 0)
608 { /* single precision */
610 /* This unsigned division rounds toward zero. */
616 { /* trivial case: dividend < divisor */
617 /* hden != 0 already checked. */
624 memset ((char *) quo, 0, sizeof quo);
626 memset ((char *) num, 0, sizeof num); /* to zero 9th element */
627 memset ((char *) den, 0, sizeof den);
629 encode (num, lnum, hnum);
630 encode (den, lden, hden);
632 /* Special code for when the divisor < BASE. */
633 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
635 /* hnum != 0 already checked. */
636 for (i = 4 - 1; i >= 0; i--)
638 work = num[i] + carry * BASE;
639 quo[i] = work / lden;
645 /* Full double precision division,
646 with thanks to Don Knuth's "Seminumerical Algorithms". */
647 int num_hi_sig, den_hi_sig;
648 unsigned HOST_WIDE_INT quo_est, scale;
650 /* Find the highest non-zero divisor digit. */
651 for (i = 4 - 1;; i--)
658 /* Insure that the first digit of the divisor is at least BASE/2.
659 This is required by the quotient digit estimation algorithm. */
661 scale = BASE / (den[den_hi_sig] + 1);
663 { /* scale divisor and dividend */
665 for (i = 0; i <= 4 - 1; i++)
667 work = (num[i] * scale) + carry;
668 num[i] = LOWPART (work);
669 carry = HIGHPART (work);
674 for (i = 0; i <= 4 - 1; i++)
676 work = (den[i] * scale) + carry;
677 den[i] = LOWPART (work);
678 carry = HIGHPART (work);
679 if (den[i] != 0) den_hi_sig = i;
686 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
688 /* Guess the next quotient digit, quo_est, by dividing the first
689 two remaining dividend digits by the high order quotient digit.
690 quo_est is never low and is at most 2 high. */
691 unsigned HOST_WIDE_INT tmp;
693 num_hi_sig = i + den_hi_sig + 1;
694 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
695 if (num[num_hi_sig] != den[den_hi_sig])
696 quo_est = work / den[den_hi_sig];
700 /* Refine quo_est so it's usually correct, and at most one high. */
701 tmp = work - quo_est * den[den_hi_sig];
703 && (den[den_hi_sig - 1] * quo_est
704 > (tmp * BASE + num[num_hi_sig - 2])))
707 /* Try QUO_EST as the quotient digit, by multiplying the
708 divisor by QUO_EST and subtracting from the remaining dividend.
709 Keep in mind that QUO_EST is the I - 1st digit. */
712 for (j = 0; j <= den_hi_sig; j++)
714 work = quo_est * den[j] + carry;
715 carry = HIGHPART (work);
716 work = num[i + j] - LOWPART (work);
717 num[i + j] = LOWPART (work);
718 carry += HIGHPART (work) != 0;
721 /* If quo_est was high by one, then num[i] went negative and
722 we need to correct things. */
723 if (num[num_hi_sig] < carry)
726 carry = 0; /* add divisor back in */
727 for (j = 0; j <= den_hi_sig; j++)
729 work = num[i + j] + den[j] + carry;
730 carry = HIGHPART (work);
731 num[i + j] = LOWPART (work);
734 num [num_hi_sig] += carry;
737 /* Store the quotient digit. */
742 decode (quo, lquo, hquo);
745 /* if result is negative, make it so. */
747 neg_double (*lquo, *hquo, lquo, hquo);
749 /* compute trial remainder: rem = num - (quo * den) */
750 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
751 neg_double (*lrem, *hrem, lrem, hrem);
752 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
757 case TRUNC_MOD_EXPR: /* round toward zero */
758 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
762 case FLOOR_MOD_EXPR: /* round toward negative infinity */
763 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
766 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
774 case CEIL_MOD_EXPR: /* round toward positive infinity */
775 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
777 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
785 case ROUND_MOD_EXPR: /* round to closest integer */
787 unsigned HOST_WIDE_INT labs_rem = *lrem;
788 HOST_WIDE_INT habs_rem = *hrem;
789 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
790 HOST_WIDE_INT habs_den = hden, htwice;
792 /* Get absolute values */
794 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
796 neg_double (lden, hden, &labs_den, &habs_den);
798 /* If (2 * abs (lrem) >= abs (lden)) */
799 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
800 labs_rem, habs_rem, <wice, &htwice);
802 if (((unsigned HOST_WIDE_INT) habs_den
803 < (unsigned HOST_WIDE_INT) htwice)
804 || (((unsigned HOST_WIDE_INT) habs_den
805 == (unsigned HOST_WIDE_INT) htwice)
806 && (labs_den < ltwice)))
810 add_double (*lquo, *hquo,
811 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
814 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
826 /* compute true remainder: rem = num - (quo * den) */
827 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
828 neg_double (*lrem, *hrem, lrem, hrem);
829 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
833 #ifndef REAL_ARITHMETIC
834 /* Effectively truncate a real value to represent the nearest possible value
835 in a narrower mode. The result is actually represented in the same data
836 type as the argument, but its value is usually different.
838 A trap may occur during the FP operations and it is the responsibility
839 of the calling function to have a handler established. */
842 real_value_truncate (mode, arg)
843 enum machine_mode mode;
846 return REAL_VALUE_TRUNCATE (mode, arg);
849 #if TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
851 /* Check for infinity in an IEEE double precision number. */
857 /* The IEEE 64-bit double format. */
862 unsigned exponent : 11;
863 unsigned mantissa1 : 20;
864 unsigned mantissa2 : 32;
867 unsigned mantissa2 : 32;
868 unsigned mantissa1 : 20;
869 unsigned exponent : 11;
875 if (u.big_endian.sign == 1)
878 return (u.big_endian.exponent == 2047
879 && u.big_endian.mantissa1 == 0
880 && u.big_endian.mantissa2 == 0);
885 return (u.little_endian.exponent == 2047
886 && u.little_endian.mantissa1 == 0
887 && u.little_endian.mantissa2 == 0);
891 /* Check whether an IEEE double precision number is a NaN. */
897 /* The IEEE 64-bit double format. */
902 unsigned exponent : 11;
903 unsigned mantissa1 : 20;
904 unsigned mantissa2 : 32;
907 unsigned mantissa2 : 32;
908 unsigned mantissa1 : 20;
909 unsigned exponent : 11;
915 if (u.big_endian.sign == 1)
918 return (u.big_endian.exponent == 2047
919 && (u.big_endian.mantissa1 != 0
920 || u.big_endian.mantissa2 != 0));
925 return (u.little_endian.exponent == 2047
926 && (u.little_endian.mantissa1 != 0
927 || u.little_endian.mantissa2 != 0));
931 /* Check for a negative IEEE double precision number. */
937 /* The IEEE 64-bit double format. */
942 unsigned exponent : 11;
943 unsigned mantissa1 : 20;
944 unsigned mantissa2 : 32;
947 unsigned mantissa2 : 32;
948 unsigned mantissa1 : 20;
949 unsigned exponent : 11;
955 if (u.big_endian.sign == 1)
958 return u.big_endian.sign;
963 return u.little_endian.sign;
966 #else /* Target not IEEE */
968 /* Let's assume other float formats don't have infinity.
969 (This can be overridden by redefining REAL_VALUE_ISINF.) */
973 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
978 /* Let's assume other float formats don't have NaNs.
979 (This can be overridden by redefining REAL_VALUE_ISNAN.) */
983 REAL_VALUE_TYPE x ATTRIBUTE_UNUSED;
988 /* Let's assume other float formats don't have minus zero.
989 (This can be overridden by redefining REAL_VALUE_NEGATIVE.) */
997 #endif /* Target not IEEE */
999 /* Try to change R into its exact multiplicative inverse in machine mode
1000 MODE. Return nonzero function value if successful. */
1001 struct exact_real_inverse_args
1004 enum machine_mode mode;
1009 exact_real_inverse_1 (p)
1012 struct exact_real_inverse_args *args =
1013 (struct exact_real_inverse_args *) p;
1015 enum machine_mode mode = args->mode;
1016 REAL_VALUE_TYPE *r = args->r;
1021 unsigned short i[4];
1024 #ifdef CHECK_FLOAT_VALUE
1028 /* Set array index to the less significant bits in the unions, depending
1029 on the endian-ness of the host doubles. */
1030 #if HOST_FLOAT_FORMAT == VAX_FLOAT_FORMAT \
1031 || HOST_FLOAT_FORMAT == IBM_FLOAT_FORMAT
1034 # define K (2 * HOST_FLOAT_WORDS_BIG_ENDIAN)
1037 /* Domain check the argument. */
1042 #ifdef REAL_INFINITY
1043 if (REAL_VALUE_ISINF (x.d) || REAL_VALUE_ISNAN (x.d))
1047 /* Compute the reciprocal and check for numerical exactness.
1048 It is unnecessary to check all the significand bits to determine
1049 whether X is a power of 2. If X is not, then it is impossible for
1050 the bottom half significand of both X and 1/X to be all zero bits.
1051 Hence we ignore the data structure of the top half and examine only
1052 the low order bits of the two significands. */
1054 if (x.i[K] != 0 || x.i[K + 1] != 0 || t.i[K] != 0 || t.i[K + 1] != 0)
1057 /* Truncate to the required mode and range-check the result. */
1058 y.d = REAL_VALUE_TRUNCATE (mode, t.d);
1059 #ifdef CHECK_FLOAT_VALUE
1061 if (CHECK_FLOAT_VALUE (mode, y.d, i))
1065 /* Fail if truncation changed the value. */
1066 if (y.d != t.d || y.d == 0.0)
1069 #ifdef REAL_INFINITY
1070 if (REAL_VALUE_ISINF (y.d) || REAL_VALUE_ISNAN (y.d))
1074 /* Output the reciprocal and return success flag. */
1088 exact_real_inverse (mode, r)
1089 enum machine_mode mode;
1092 struct exact_real_inverse_args args;
1094 /* Disable if insufficient information on the data structure. */
1095 #if HOST_FLOAT_FORMAT == UNKNOWN_FLOAT_FORMAT
1099 /* Usually disable if bounds checks are not reliable. */
1100 if ((HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT) && !flag_pretend_float)
1106 if (do_float_handler (exact_real_inverse_1, (PTR) &args))
1107 return args.success;
1111 /* Convert C99 hexadecimal floating point string constant S. Return
1112 real value type in mode MODE. This function uses the host computer's
1113 floating point arithmetic when there is no REAL_ARITHMETIC. */
1116 real_hex_to_f (s, mode)
1118 enum machine_mode mode;
1122 unsigned HOST_WIDE_INT low, high;
1123 int shcount, nrmcount, k;
1124 int sign, expsign, isfloat;
1125 int lost = 0;/* Nonzero low order bits shifted out and discarded. */
1126 int frexpon = 0; /* Bits after the decimal point. */
1127 int expon = 0; /* Value of exponent. */
1128 int decpt = 0; /* How many decimal points. */
1129 int gotp = 0; /* How many P's. */
1136 while (*p == ' ' || *p == '\t')
1139 /* Sign, if any, comes first. */
1147 /* The string is supposed to start with 0x or 0X . */
1151 if (*p == 'x' || *p == 'X')
1165 while ((c = *p) != '\0')
1169 k = hex_value (c & CHARMASK);
1171 if ((high & 0xf0000000) == 0)
1173 high = (high << 4) + ((low >> 28) & 15);
1174 low = (low << 4) + k;
1181 /* Record nonzero lost bits. */
1194 else if (c == 'p' || c == 'P')
1198 /* Sign of exponent. */
1205 /* Value of exponent.
1206 The exponent field is a decimal integer. */
1207 while (ISDIGIT (*p))
1209 k = (*p++ & CHARMASK) - '0';
1210 expon = 10 * expon + k;
1214 /* F suffix is ambiguous in the significand part
1215 so it must appear after the decimal exponent field. */
1216 if (*p == 'f' || *p == 'F')
1224 else if (c == 'l' || c == 'L')
1233 /* Abort if last character read was not legitimate. */
1235 if ((c != '\0' && c != ' ' && c != '\n' && c != '\r') || (decpt > 1))
1238 /* There must be either one decimal point or one p. */
1239 if (decpt == 0 && gotp == 0)
1243 if (high == 0 && low == 0)
1255 /* Leave a high guard bit for carry-out. */
1256 if ((high & 0x80000000) != 0)
1259 low = (low >> 1) | (high << 31);
1264 if ((high & 0xffff8000) == 0)
1266 high = (high << 16) + ((low >> 16) & 0xffff);
1271 while ((high & 0xc0000000) == 0)
1273 high = (high << 1) + ((low >> 31) & 1);
1278 if (isfloat || GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1280 /* Keep 24 bits precision, bits 0x7fffff80.
1281 Rounding bit is 0x40. */
1282 lost = lost | low | (high & 0x3f);
1286 if ((high & 0x80) || lost)
1293 /* We need real.c to do long double formats, so here default
1294 to double precision. */
1295 #if HOST_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
1297 Keep 53 bits precision, bits 0x7fffffff fffffc00.
1298 Rounding bit is low word 0x200. */
1299 lost = lost | (low & 0x1ff);
1302 if ((low & 0x400) || lost)
1304 low = (low + 0x200) & 0xfffffc00;
1311 /* Assume it's a VAX with 56-bit significand,
1312 bits 0x7fffffff ffffff80. */
1313 lost = lost | (low & 0x7f);
1316 if ((low & 0x80) || lost)
1318 low = (low + 0x40) & 0xffffff80;
1328 ip = REAL_VALUE_LDEXP (ip, 32) + (double) low;
1329 /* Apply shifts and exponent value as power of 2. */
1330 ip = REAL_VALUE_LDEXP (ip, expon - (nrmcount + frexpon));
1337 #endif /* no REAL_ARITHMETIC */
1339 /* Given T, an expression, return the negation of T. Allow for T to be
1340 null, in which case return null. */
1352 type = TREE_TYPE (t);
1353 STRIP_SIGN_NOPS (t);
1355 switch (TREE_CODE (t))
1359 if (! TREE_UNSIGNED (type)
1360 && 0 != (tem = fold (build1 (NEGATE_EXPR, type, t)))
1361 && ! TREE_OVERFLOW (tem))
1366 return convert (type, TREE_OPERAND (t, 0));
1369 /* - (A - B) -> B - A */
1370 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1371 return convert (type,
1372 fold (build (MINUS_EXPR, TREE_TYPE (t),
1373 TREE_OPERAND (t, 1),
1374 TREE_OPERAND (t, 0))));
1381 return convert (type, fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t)));
1384 /* Split a tree IN into a constant, literal and variable parts that could be
1385 combined with CODE to make IN. "constant" means an expression with
1386 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1387 commutative arithmetic operation. Store the constant part into *CONP,
1388 the literal in *LITP and return the variable part. If a part isn't
1389 present, set it to null. If the tree does not decompose in this way,
1390 return the entire tree as the variable part and the other parts as null.
1392 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1393 case, we negate an operand that was subtracted. Except if it is a
1394 literal for which we use *MINUS_LITP instead.
1396 If NEGATE_P is true, we are negating all of IN, again except a literal
1397 for which we use *MINUS_LITP instead.
1399 If IN is itself a literal or constant, return it as appropriate.
1401 Note that we do not guarantee that any of the three values will be the
1402 same type as IN, but they will have the same signedness and mode. */
1405 split_tree (in, code, conp, litp, minus_litp, negate_p)
1407 enum tree_code code;
1408 tree *conp, *litp, *minus_litp;
1417 /* Strip any conversions that don't change the machine mode or signedness. */
1418 STRIP_SIGN_NOPS (in);
1420 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1422 else if (TREE_CODE (in) == code
1423 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1424 /* We can associate addition and subtraction together (even
1425 though the C standard doesn't say so) for integers because
1426 the value is not affected. For reals, the value might be
1427 affected, so we can't. */
1428 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1429 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1431 tree op0 = TREE_OPERAND (in, 0);
1432 tree op1 = TREE_OPERAND (in, 1);
1433 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1434 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1436 /* First see if either of the operands is a literal, then a constant. */
1437 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1438 *litp = op0, op0 = 0;
1439 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1440 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1442 if (op0 != 0 && TREE_CONSTANT (op0))
1443 *conp = op0, op0 = 0;
1444 else if (op1 != 0 && TREE_CONSTANT (op1))
1445 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1447 /* If we haven't dealt with either operand, this is not a case we can
1448 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1449 if (op0 != 0 && op1 != 0)
1454 var = op1, neg_var_p = neg1_p;
1456 /* Now do any needed negations. */
1458 *minus_litp = *litp, *litp = 0;
1460 *conp = negate_expr (*conp);
1462 var = negate_expr (var);
1464 else if (TREE_CONSTANT (in))
1472 *minus_litp = *litp, *litp = 0;
1473 else if (*minus_litp)
1474 *litp = *minus_litp, *minus_litp = 0;
1475 *conp = negate_expr (*conp);
1476 var = negate_expr (var);
1482 /* Re-associate trees split by the above function. T1 and T2 are either
1483 expressions to associate or null. Return the new expression, if any. If
1484 we build an operation, do it in TYPE and with CODE. */
1487 associate_trees (t1, t2, code, type)
1489 enum tree_code code;
1497 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1498 try to fold this since we will have infinite recursion. But do
1499 deal with any NEGATE_EXPRs. */
1500 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1501 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1503 if (TREE_CODE (t1) == NEGATE_EXPR)
1504 return build (MINUS_EXPR, type, convert (type, t2),
1505 convert (type, TREE_OPERAND (t1, 0)));
1506 else if (TREE_CODE (t2) == NEGATE_EXPR)
1507 return build (MINUS_EXPR, type, convert (type, t1),
1508 convert (type, TREE_OPERAND (t2, 0)));
1510 return build (code, type, convert (type, t1), convert (type, t2));
1513 return fold (build (code, type, convert (type, t1), convert (type, t2)));
1516 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1517 to produce a new constant.
1519 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1522 int_const_binop (code, arg1, arg2, notrunc)
1523 enum tree_code code;
1527 unsigned HOST_WIDE_INT int1l, int2l;
1528 HOST_WIDE_INT int1h, int2h;
1529 unsigned HOST_WIDE_INT low;
1531 unsigned HOST_WIDE_INT garbagel;
1532 HOST_WIDE_INT garbageh;
1534 tree type = TREE_TYPE (arg1);
1535 int uns = TREE_UNSIGNED (type);
1537 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1539 int no_overflow = 0;
1541 int1l = TREE_INT_CST_LOW (arg1);
1542 int1h = TREE_INT_CST_HIGH (arg1);
1543 int2l = TREE_INT_CST_LOW (arg2);
1544 int2h = TREE_INT_CST_HIGH (arg2);
1549 low = int1l | int2l, hi = int1h | int2h;
1553 low = int1l ^ int2l, hi = int1h ^ int2h;
1557 low = int1l & int2l, hi = int1h & int2h;
1560 case BIT_ANDTC_EXPR:
1561 low = int1l & ~int2l, hi = int1h & ~int2h;
1567 /* It's unclear from the C standard whether shifts can overflow.
1568 The following code ignores overflow; perhaps a C standard
1569 interpretation ruling is needed. */
1570 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1578 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1583 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1587 neg_double (int2l, int2h, &low, &hi);
1588 add_double (int1l, int1h, low, hi, &low, &hi);
1589 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1593 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1596 case TRUNC_DIV_EXPR:
1597 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1598 case EXACT_DIV_EXPR:
1599 /* This is a shortcut for a common special case. */
1600 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1601 && ! TREE_CONSTANT_OVERFLOW (arg1)
1602 && ! TREE_CONSTANT_OVERFLOW (arg2)
1603 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1605 if (code == CEIL_DIV_EXPR)
1608 low = int1l / int2l, hi = 0;
1612 /* ... fall through ... */
1614 case ROUND_DIV_EXPR:
1615 if (int2h == 0 && int2l == 1)
1617 low = int1l, hi = int1h;
1620 if (int1l == int2l && int1h == int2h
1621 && ! (int1l == 0 && int1h == 0))
1626 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1627 &low, &hi, &garbagel, &garbageh);
1630 case TRUNC_MOD_EXPR:
1631 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1632 /* This is a shortcut for a common special case. */
1633 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1634 && ! TREE_CONSTANT_OVERFLOW (arg1)
1635 && ! TREE_CONSTANT_OVERFLOW (arg2)
1636 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1638 if (code == CEIL_MOD_EXPR)
1640 low = int1l % int2l, hi = 0;
1644 /* ... fall through ... */
1646 case ROUND_MOD_EXPR:
1647 overflow = div_and_round_double (code, uns,
1648 int1l, int1h, int2l, int2h,
1649 &garbagel, &garbageh, &low, &hi);
1655 low = (((unsigned HOST_WIDE_INT) int1h
1656 < (unsigned HOST_WIDE_INT) int2h)
1657 || (((unsigned HOST_WIDE_INT) int1h
1658 == (unsigned HOST_WIDE_INT) int2h)
1661 low = (int1h < int2h
1662 || (int1h == int2h && int1l < int2l));
1664 if (low == (code == MIN_EXPR))
1665 low = int1l, hi = int1h;
1667 low = int2l, hi = int2h;
1674 /* If this is for a sizetype, can be represented as one (signed)
1675 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1678 && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1679 || (hi == -1 && (HOST_WIDE_INT) low < 0))
1680 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1681 return size_int_type_wide (low, type);
1684 t = build_int_2 (low, hi);
1685 TREE_TYPE (t) = TREE_TYPE (arg1);
1690 ? (!uns || is_sizetype) && overflow
1691 : (force_fit_type (t, (!uns || is_sizetype) && overflow)
1693 | TREE_OVERFLOW (arg1)
1694 | TREE_OVERFLOW (arg2));
1696 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1697 So check if force_fit_type truncated the value. */
1699 && ! TREE_OVERFLOW (t)
1700 && (TREE_INT_CST_HIGH (t) != hi
1701 || TREE_INT_CST_LOW (t) != low))
1702 TREE_OVERFLOW (t) = 1;
1704 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1705 | TREE_CONSTANT_OVERFLOW (arg1)
1706 | TREE_CONSTANT_OVERFLOW (arg2));
1710 /* Define input and output argument for const_binop_1. */
1713 enum tree_code code; /* Input: tree code for operation. */
1714 tree type; /* Input: tree type for operation. */
1715 REAL_VALUE_TYPE d1, d2; /* Input: floating point operands. */
1716 tree t; /* Output: constant for result. */
1719 /* Do the real arithmetic for const_binop while protected by a
1720 float overflow handler. */
1723 const_binop_1 (data)
1726 struct cb_args *args = (struct cb_args *) data;
1727 REAL_VALUE_TYPE value;
1729 #ifdef REAL_ARITHMETIC
1730 REAL_ARITHMETIC (value, args->code, args->d1, args->d2);
1735 value = args->d1 + args->d2;
1739 value = args->d1 - args->d2;
1743 value = args->d1 * args->d2;
1747 #ifndef REAL_INFINITY
1752 value = args->d1 / args->d2;
1756 value = MIN (args->d1, args->d2);
1760 value = MAX (args->d1, args->d2);
1766 #endif /* no REAL_ARITHMETIC */
1769 = build_real (args->type,
1770 real_value_truncate (TYPE_MODE (args->type), value));
1773 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1774 constant. We assume ARG1 and ARG2 have the same data type, or at least
1775 are the same kind of constant and the same machine mode.
1777 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1780 const_binop (code, arg1, arg2, notrunc)
1781 enum tree_code code;
1788 if (TREE_CODE (arg1) == INTEGER_CST)
1789 return int_const_binop (code, arg1, arg2, notrunc);
1791 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1792 if (TREE_CODE (arg1) == REAL_CST)
1798 struct cb_args args;
1800 d1 = TREE_REAL_CST (arg1);
1801 d2 = TREE_REAL_CST (arg2);
1803 /* If either operand is a NaN, just return it. Otherwise, set up
1804 for floating-point trap; we return an overflow. */
1805 if (REAL_VALUE_ISNAN (d1))
1807 else if (REAL_VALUE_ISNAN (d2))
1810 /* Setup input for const_binop_1() */
1811 args.type = TREE_TYPE (arg1);
1816 if (do_float_handler (const_binop_1, (PTR) &args))
1817 /* Receive output from const_binop_1. */
1821 /* We got an exception from const_binop_1. */
1822 t = copy_node (arg1);
1827 = (force_fit_type (t, overflow)
1828 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1829 TREE_CONSTANT_OVERFLOW (t)
1831 | TREE_CONSTANT_OVERFLOW (arg1)
1832 | TREE_CONSTANT_OVERFLOW (arg2);
1835 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1836 if (TREE_CODE (arg1) == COMPLEX_CST)
1838 tree type = TREE_TYPE (arg1);
1839 tree r1 = TREE_REALPART (arg1);
1840 tree i1 = TREE_IMAGPART (arg1);
1841 tree r2 = TREE_REALPART (arg2);
1842 tree i2 = TREE_IMAGPART (arg2);
1848 t = build_complex (type,
1849 const_binop (PLUS_EXPR, r1, r2, notrunc),
1850 const_binop (PLUS_EXPR, i1, i2, notrunc));
1854 t = build_complex (type,
1855 const_binop (MINUS_EXPR, r1, r2, notrunc),
1856 const_binop (MINUS_EXPR, i1, i2, notrunc));
1860 t = build_complex (type,
1861 const_binop (MINUS_EXPR,
1862 const_binop (MULT_EXPR,
1864 const_binop (MULT_EXPR,
1867 const_binop (PLUS_EXPR,
1868 const_binop (MULT_EXPR,
1870 const_binop (MULT_EXPR,
1878 = const_binop (PLUS_EXPR,
1879 const_binop (MULT_EXPR, r2, r2, notrunc),
1880 const_binop (MULT_EXPR, i2, i2, notrunc),
1883 t = build_complex (type,
1885 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1886 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1887 const_binop (PLUS_EXPR,
1888 const_binop (MULT_EXPR, r1, r2,
1890 const_binop (MULT_EXPR, i1, i2,
1893 magsquared, notrunc),
1895 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1896 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1897 const_binop (MINUS_EXPR,
1898 const_binop (MULT_EXPR, i1, r2,
1900 const_binop (MULT_EXPR, r1, i2,
1903 magsquared, notrunc));
1915 /* These are the hash table functions for the hash table of INTEGER_CST
1916 nodes of a sizetype. */
1918 /* Return the hash code code X, an INTEGER_CST. */
1926 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1927 ^ (hashval_t) ((long) TREE_TYPE (t) >> 3)
1928 ^ (TREE_OVERFLOW (t) << 20));
1931 /* Return non-zero if the value represented by *X (an INTEGER_CST tree node)
1932 is the same as that given by *Y, which is the same. */
1942 return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1943 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1944 && TREE_TYPE (xt) == TREE_TYPE (yt)
1945 && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1948 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1949 bits are given by NUMBER and of the sizetype represented by KIND. */
1952 size_int_wide (number, kind)
1953 HOST_WIDE_INT number;
1954 enum size_type_kind kind;
1956 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1959 /* Likewise, but the desired type is specified explicitly. */
1962 size_int_type_wide (number, type)
1963 HOST_WIDE_INT number;
1966 static htab_t size_htab = 0;
1967 static tree new_const = 0;
1972 size_htab = htab_create (1024, size_htab_hash, size_htab_eq, NULL);
1973 ggc_add_deletable_htab (size_htab, NULL, NULL);
1974 new_const = make_node (INTEGER_CST);
1975 ggc_add_tree_root (&new_const, 1);
1978 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1979 hash table, we return the value from the hash table. Otherwise, we
1980 place that in the hash table and make a new node for the next time. */
1981 TREE_INT_CST_LOW (new_const) = number;
1982 TREE_INT_CST_HIGH (new_const) = number < 0 ? -1 : 0;
1983 TREE_TYPE (new_const) = type;
1984 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const)
1985 = force_fit_type (new_const, 0);
1987 slot = htab_find_slot (size_htab, new_const, INSERT);
1992 *slot = (PTR) new_const;
1993 new_const = make_node (INTEGER_CST);
1997 return (tree) *slot;
2000 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2001 is a tree code. The type of the result is taken from the operands.
2002 Both must be the same type integer type and it must be a size type.
2003 If the operands are constant, so is the result. */
2006 size_binop (code, arg0, arg1)
2007 enum tree_code code;
2010 tree type = TREE_TYPE (arg0);
2012 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
2013 || type != TREE_TYPE (arg1))
2016 /* Handle the special case of two integer constants faster. */
2017 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2019 /* And some specific cases even faster than that. */
2020 if (code == PLUS_EXPR && integer_zerop (arg0))
2022 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
2023 && integer_zerop (arg1))
2025 else if (code == MULT_EXPR && integer_onep (arg0))
2028 /* Handle general case of two integer constants. */
2029 return int_const_binop (code, arg0, arg1, 0);
2032 if (arg0 == error_mark_node || arg1 == error_mark_node)
2033 return error_mark_node;
2035 return fold (build (code, type, arg0, arg1));
2038 /* Given two values, either both of sizetype or both of bitsizetype,
2039 compute the difference between the two values. Return the value
2040 in signed type corresponding to the type of the operands. */
2043 size_diffop (arg0, arg1)
2046 tree type = TREE_TYPE (arg0);
2049 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
2050 || type != TREE_TYPE (arg1))
2053 /* If the type is already signed, just do the simple thing. */
2054 if (! TREE_UNSIGNED (type))
2055 return size_binop (MINUS_EXPR, arg0, arg1);
2057 ctype = (type == bitsizetype || type == ubitsizetype
2058 ? sbitsizetype : ssizetype);
2060 /* If either operand is not a constant, do the conversions to the signed
2061 type and subtract. The hardware will do the right thing with any
2062 overflow in the subtraction. */
2063 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2064 return size_binop (MINUS_EXPR, convert (ctype, arg0),
2065 convert (ctype, arg1));
2067 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2068 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2069 overflow) and negate (which can't either). Special-case a result
2070 of zero while we're here. */
2071 if (tree_int_cst_equal (arg0, arg1))
2072 return convert (ctype, integer_zero_node);
2073 else if (tree_int_cst_lt (arg1, arg0))
2074 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2076 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
2077 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
2080 /* This structure is used to communicate arguments to fold_convert_1. */
2083 tree arg1; /* Input: value to convert. */
2084 tree type; /* Input: type to convert value to. */
2085 tree t; /* Output: result of conversion. */
2088 /* Function to convert floating-point constants, protected by floating
2089 point exception handler. */
2092 fold_convert_1 (data)
2095 struct fc_args *args = (struct fc_args *) data;
2097 args->t = build_real (args->type,
2098 real_value_truncate (TYPE_MODE (args->type),
2099 TREE_REAL_CST (args->arg1)));
2102 /* Given T, a tree representing type conversion of ARG1, a constant,
2103 return a constant tree representing the result of conversion. */
2106 fold_convert (t, arg1)
2110 tree type = TREE_TYPE (t);
2113 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2115 if (TREE_CODE (arg1) == INTEGER_CST)
2117 /* If we would build a constant wider than GCC supports,
2118 leave the conversion unfolded. */
2119 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
2122 /* If we are trying to make a sizetype for a small integer, use
2123 size_int to pick up cached types to reduce duplicate nodes. */
2124 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
2125 && !TREE_CONSTANT_OVERFLOW (arg1)
2126 && compare_tree_int (arg1, 10000) < 0)
2127 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
2129 /* Given an integer constant, make new constant with new type,
2130 appropriately sign-extended or truncated. */
2131 t = build_int_2 (TREE_INT_CST_LOW (arg1),
2132 TREE_INT_CST_HIGH (arg1));
2133 TREE_TYPE (t) = type;
2134 /* Indicate an overflow if (1) ARG1 already overflowed,
2135 or (2) force_fit_type indicates an overflow.
2136 Tell force_fit_type that an overflow has already occurred
2137 if ARG1 is a too-large unsigned value and T is signed.
2138 But don't indicate an overflow if converting a pointer. */
2140 = ((force_fit_type (t,
2141 (TREE_INT_CST_HIGH (arg1) < 0
2142 && (TREE_UNSIGNED (type)
2143 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
2144 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
2145 || TREE_OVERFLOW (arg1));
2146 TREE_CONSTANT_OVERFLOW (t)
2147 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2149 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2150 else if (TREE_CODE (arg1) == REAL_CST)
2152 /* Don't initialize these, use assignments.
2153 Initialized local aggregates don't work on old compilers. */
2157 tree type1 = TREE_TYPE (arg1);
2160 x = TREE_REAL_CST (arg1);
2161 l = real_value_from_int_cst (type1, TYPE_MIN_VALUE (type));
2163 no_upper_bound = (TYPE_MAX_VALUE (type) == NULL);
2164 if (!no_upper_bound)
2165 u = real_value_from_int_cst (type1, TYPE_MAX_VALUE (type));
2167 /* See if X will be in range after truncation towards 0.
2168 To compensate for truncation, move the bounds away from 0,
2169 but reject if X exactly equals the adjusted bounds. */
2170 #ifdef REAL_ARITHMETIC
2171 REAL_ARITHMETIC (l, MINUS_EXPR, l, dconst1);
2172 if (!no_upper_bound)
2173 REAL_ARITHMETIC (u, PLUS_EXPR, u, dconst1);
2176 if (!no_upper_bound)
2179 /* If X is a NaN, use zero instead and show we have an overflow.
2180 Otherwise, range check. */
2181 if (REAL_VALUE_ISNAN (x))
2182 overflow = 1, x = dconst0;
2183 else if (! (REAL_VALUES_LESS (l, x)
2185 && REAL_VALUES_LESS (x, u)))
2188 #ifndef REAL_ARITHMETIC
2190 HOST_WIDE_INT low, high;
2191 HOST_WIDE_INT half_word
2192 = (HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2);
2197 high = (HOST_WIDE_INT) (x / half_word / half_word);
2198 x -= (REAL_VALUE_TYPE) high * half_word * half_word;
2199 if (x >= (REAL_VALUE_TYPE) half_word * half_word / 2)
2201 low = x - (REAL_VALUE_TYPE) half_word * half_word / 2;
2202 low |= (HOST_WIDE_INT) -1 << (HOST_BITS_PER_WIDE_INT - 1);
2205 low = (HOST_WIDE_INT) x;
2206 if (TREE_REAL_CST (arg1) < 0)
2207 neg_double (low, high, &low, &high);
2208 t = build_int_2 (low, high);
2212 HOST_WIDE_INT low, high;
2213 REAL_VALUE_TO_INT (&low, &high, x);
2214 t = build_int_2 (low, high);
2217 TREE_TYPE (t) = type;
2219 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2220 TREE_CONSTANT_OVERFLOW (t)
2221 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2223 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2224 TREE_TYPE (t) = type;
2226 else if (TREE_CODE (type) == REAL_TYPE)
2228 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
2229 if (TREE_CODE (arg1) == INTEGER_CST)
2230 return build_real_from_int_cst (type, arg1);
2231 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
2232 if (TREE_CODE (arg1) == REAL_CST)
2234 struct fc_args args;
2236 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
2239 TREE_TYPE (arg1) = type;
2243 /* Setup input for fold_convert_1() */
2247 if (do_float_handler (fold_convert_1, (PTR) &args))
2249 /* Receive output from fold_convert_1() */
2254 /* We got an exception from fold_convert_1() */
2256 t = copy_node (arg1);
2260 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
2261 TREE_CONSTANT_OVERFLOW (t)
2262 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2266 TREE_CONSTANT (t) = 1;
2270 /* Return an expr equal to X but certainly not valid as an lvalue. */
2278 /* These things are certainly not lvalues. */
2279 if (TREE_CODE (x) == NON_LVALUE_EXPR
2280 || TREE_CODE (x) == INTEGER_CST
2281 || TREE_CODE (x) == REAL_CST
2282 || TREE_CODE (x) == STRING_CST
2283 || TREE_CODE (x) == ADDR_EXPR)
2286 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2287 TREE_CONSTANT (result) = TREE_CONSTANT (x);
2291 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2292 Zero means allow extended lvalues. */
2294 int pedantic_lvalues;
2296 /* When pedantic, return an expr equal to X but certainly not valid as a
2297 pedantic lvalue. Otherwise, return X. */
2300 pedantic_non_lvalue (x)
2303 if (pedantic_lvalues)
2304 return non_lvalue (x);
2309 /* Given a tree comparison code, return the code that is the logical inverse
2310 of the given code. It is not safe to do this for floating-point
2311 comparisons, except for NE_EXPR and EQ_EXPR. */
2313 static enum tree_code
2314 invert_tree_comparison (code)
2315 enum tree_code code;
2336 /* Similar, but return the comparison that results if the operands are
2337 swapped. This is safe for floating-point. */
2339 static enum tree_code
2340 swap_tree_comparison (code)
2341 enum tree_code code;
2361 /* Return nonzero if CODE is a tree code that represents a truth value. */
2364 truth_value_p (code)
2365 enum tree_code code;
2367 return (TREE_CODE_CLASS (code) == '<'
2368 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2369 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2370 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2373 /* Return nonzero if two operands are necessarily equal.
2374 If ONLY_CONST is non-zero, only return non-zero for constants.
2375 This function tests whether the operands are indistinguishable;
2376 it does not test whether they are equal using C's == operation.
2377 The distinction is important for IEEE floating point, because
2378 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2379 (2) two NaNs may be indistinguishable, but NaN!=NaN. */
2382 operand_equal_p (arg0, arg1, only_const)
2386 /* If both types don't have the same signedness, then we can't consider
2387 them equal. We must check this before the STRIP_NOPS calls
2388 because they may change the signedness of the arguments. */
2389 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2395 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2396 /* This is needed for conversions and for COMPONENT_REF.
2397 Might as well play it safe and always test this. */
2398 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2399 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2400 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2403 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2404 We don't care about side effects in that case because the SAVE_EXPR
2405 takes care of that for us. In all other cases, two expressions are
2406 equal if they have no side effects. If we have two identical
2407 expressions with side effects that should be treated the same due
2408 to the only side effects being identical SAVE_EXPR's, that will
2409 be detected in the recursive calls below. */
2410 if (arg0 == arg1 && ! only_const
2411 && (TREE_CODE (arg0) == SAVE_EXPR
2412 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2415 /* Next handle constant cases, those for which we can return 1 even
2416 if ONLY_CONST is set. */
2417 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2418 switch (TREE_CODE (arg0))
2421 return (! TREE_CONSTANT_OVERFLOW (arg0)
2422 && ! TREE_CONSTANT_OVERFLOW (arg1)
2423 && tree_int_cst_equal (arg0, arg1));
2426 return (! TREE_CONSTANT_OVERFLOW (arg0)
2427 && ! TREE_CONSTANT_OVERFLOW (arg1)
2428 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2429 TREE_REAL_CST (arg1)));
2435 if (TREE_CONSTANT_OVERFLOW (arg0)
2436 || TREE_CONSTANT_OVERFLOW (arg1))
2439 v1 = TREE_VECTOR_CST_ELTS (arg0);
2440 v2 = TREE_VECTOR_CST_ELTS (arg1);
2443 if (!operand_equal_p (v1, v2, only_const))
2445 v1 = TREE_CHAIN (v1);
2446 v2 = TREE_CHAIN (v2);
2453 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2455 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2459 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2460 && ! memcmp (TREE_STRING_POINTER (arg0),
2461 TREE_STRING_POINTER (arg1),
2462 TREE_STRING_LENGTH (arg0)));
2465 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2474 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2477 /* Two conversions are equal only if signedness and modes match. */
2478 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2479 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2480 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2483 return operand_equal_p (TREE_OPERAND (arg0, 0),
2484 TREE_OPERAND (arg1, 0), 0);
2488 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2489 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2493 /* For commutative ops, allow the other order. */
2494 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2495 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2496 || TREE_CODE (arg0) == BIT_IOR_EXPR
2497 || TREE_CODE (arg0) == BIT_XOR_EXPR
2498 || TREE_CODE (arg0) == BIT_AND_EXPR
2499 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2500 && operand_equal_p (TREE_OPERAND (arg0, 0),
2501 TREE_OPERAND (arg1, 1), 0)
2502 && operand_equal_p (TREE_OPERAND (arg0, 1),
2503 TREE_OPERAND (arg1, 0), 0));
2506 /* If either of the pointer (or reference) expressions we are dereferencing
2507 contain a side effect, these cannot be equal. */
2508 if (TREE_SIDE_EFFECTS (arg0)
2509 || TREE_SIDE_EFFECTS (arg1))
2512 switch (TREE_CODE (arg0))
2515 return operand_equal_p (TREE_OPERAND (arg0, 0),
2516 TREE_OPERAND (arg1, 0), 0);
2520 case ARRAY_RANGE_REF:
2521 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2522 TREE_OPERAND (arg1, 0), 0)
2523 && operand_equal_p (TREE_OPERAND (arg0, 1),
2524 TREE_OPERAND (arg1, 1), 0));
2527 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2528 TREE_OPERAND (arg1, 0), 0)
2529 && operand_equal_p (TREE_OPERAND (arg0, 1),
2530 TREE_OPERAND (arg1, 1), 0)
2531 && operand_equal_p (TREE_OPERAND (arg0, 2),
2532 TREE_OPERAND (arg1, 2), 0));
2538 if (TREE_CODE (arg0) == RTL_EXPR)
2539 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2547 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2548 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2550 When in doubt, return 0. */
2553 operand_equal_for_comparison_p (arg0, arg1, other)
2557 int unsignedp1, unsignedpo;
2558 tree primarg0, primarg1, primother;
2559 unsigned int correct_width;
2561 if (operand_equal_p (arg0, arg1, 0))
2564 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2565 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2568 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2569 and see if the inner values are the same. This removes any
2570 signedness comparison, which doesn't matter here. */
2571 primarg0 = arg0, primarg1 = arg1;
2572 STRIP_NOPS (primarg0);
2573 STRIP_NOPS (primarg1);
2574 if (operand_equal_p (primarg0, primarg1, 0))
2577 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2578 actual comparison operand, ARG0.
2580 First throw away any conversions to wider types
2581 already present in the operands. */
2583 primarg1 = get_narrower (arg1, &unsignedp1);
2584 primother = get_narrower (other, &unsignedpo);
2586 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2587 if (unsignedp1 == unsignedpo
2588 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2589 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2591 tree type = TREE_TYPE (arg0);
2593 /* Make sure shorter operand is extended the right way
2594 to match the longer operand. */
2595 primarg1 = convert (signed_or_unsigned_type (unsignedp1,
2596 TREE_TYPE (primarg1)),
2599 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2606 /* See if ARG is an expression that is either a comparison or is performing
2607 arithmetic on comparisons. The comparisons must only be comparing
2608 two different values, which will be stored in *CVAL1 and *CVAL2; if
2609 they are non-zero it means that some operands have already been found.
2610 No variables may be used anywhere else in the expression except in the
2611 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2612 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2614 If this is true, return 1. Otherwise, return zero. */
2617 twoval_comparison_p (arg, cval1, cval2, save_p)
2619 tree *cval1, *cval2;
2622 enum tree_code code = TREE_CODE (arg);
2623 char class = TREE_CODE_CLASS (code);
2625 /* We can handle some of the 'e' cases here. */
2626 if (class == 'e' && code == TRUTH_NOT_EXPR)
2628 else if (class == 'e'
2629 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2630 || code == COMPOUND_EXPR))
2633 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2634 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2636 /* If we've already found a CVAL1 or CVAL2, this expression is
2637 two complex to handle. */
2638 if (*cval1 || *cval2)
2648 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2651 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2652 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2653 cval1, cval2, save_p));
2659 if (code == COND_EXPR)
2660 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2661 cval1, cval2, save_p)
2662 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2663 cval1, cval2, save_p)
2664 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2665 cval1, cval2, save_p));
2669 /* First see if we can handle the first operand, then the second. For
2670 the second operand, we know *CVAL1 can't be zero. It must be that
2671 one side of the comparison is each of the values; test for the
2672 case where this isn't true by failing if the two operands
2675 if (operand_equal_p (TREE_OPERAND (arg, 0),
2676 TREE_OPERAND (arg, 1), 0))
2680 *cval1 = TREE_OPERAND (arg, 0);
2681 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2683 else if (*cval2 == 0)
2684 *cval2 = TREE_OPERAND (arg, 0);
2685 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2690 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2692 else if (*cval2 == 0)
2693 *cval2 = TREE_OPERAND (arg, 1);
2694 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2706 /* ARG is a tree that is known to contain just arithmetic operations and
2707 comparisons. Evaluate the operations in the tree substituting NEW0 for
2708 any occurrence of OLD0 as an operand of a comparison and likewise for
2712 eval_subst (arg, old0, new0, old1, new1)
2714 tree old0, new0, old1, new1;
2716 tree type = TREE_TYPE (arg);
2717 enum tree_code code = TREE_CODE (arg);
2718 char class = TREE_CODE_CLASS (code);
2720 /* We can handle some of the 'e' cases here. */
2721 if (class == 'e' && code == TRUTH_NOT_EXPR)
2723 else if (class == 'e'
2724 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2730 return fold (build1 (code, type,
2731 eval_subst (TREE_OPERAND (arg, 0),
2732 old0, new0, old1, new1)));
2735 return fold (build (code, type,
2736 eval_subst (TREE_OPERAND (arg, 0),
2737 old0, new0, old1, new1),
2738 eval_subst (TREE_OPERAND (arg, 1),
2739 old0, new0, old1, new1)));
2745 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2748 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2751 return fold (build (code, type,
2752 eval_subst (TREE_OPERAND (arg, 0),
2753 old0, new0, old1, new1),
2754 eval_subst (TREE_OPERAND (arg, 1),
2755 old0, new0, old1, new1),
2756 eval_subst (TREE_OPERAND (arg, 2),
2757 old0, new0, old1, new1)));
2761 /* fall through - ??? */
2765 tree arg0 = TREE_OPERAND (arg, 0);
2766 tree arg1 = TREE_OPERAND (arg, 1);
2768 /* We need to check both for exact equality and tree equality. The
2769 former will be true if the operand has a side-effect. In that
2770 case, we know the operand occurred exactly once. */
2772 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2774 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2777 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2779 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2782 return fold (build (code, type, arg0, arg1));
2790 /* Return a tree for the case when the result of an expression is RESULT
2791 converted to TYPE and OMITTED was previously an operand of the expression
2792 but is now not needed (e.g., we folded OMITTED * 0).
2794 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2795 the conversion of RESULT to TYPE. */
2798 omit_one_operand (type, result, omitted)
2799 tree type, result, omitted;
2801 tree t = convert (type, result);
2803 if (TREE_SIDE_EFFECTS (omitted))
2804 return build (COMPOUND_EXPR, type, omitted, t);
2806 return non_lvalue (t);
2809 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2812 pedantic_omit_one_operand (type, result, omitted)
2813 tree type, result, omitted;
2815 tree t = convert (type, result);
2817 if (TREE_SIDE_EFFECTS (omitted))
2818 return build (COMPOUND_EXPR, type, omitted, t);
2820 return pedantic_non_lvalue (t);
2823 /* Return a simplified tree node for the truth-negation of ARG. This
2824 never alters ARG itself. We assume that ARG is an operation that
2825 returns a truth value (0 or 1). */
2828 invert_truthvalue (arg)
2831 tree type = TREE_TYPE (arg);
2832 enum tree_code code = TREE_CODE (arg);
2834 if (code == ERROR_MARK)
2837 /* If this is a comparison, we can simply invert it, except for
2838 floating-point non-equality comparisons, in which case we just
2839 enclose a TRUTH_NOT_EXPR around what we have. */
2841 if (TREE_CODE_CLASS (code) == '<')
2843 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2844 && !flag_unsafe_math_optimizations
2847 return build1 (TRUTH_NOT_EXPR, type, arg);
2849 return build (invert_tree_comparison (code), type,
2850 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2856 return convert (type, build_int_2 (integer_zerop (arg), 0));
2858 case TRUTH_AND_EXPR:
2859 return build (TRUTH_OR_EXPR, type,
2860 invert_truthvalue (TREE_OPERAND (arg, 0)),
2861 invert_truthvalue (TREE_OPERAND (arg, 1)));
2864 return build (TRUTH_AND_EXPR, type,
2865 invert_truthvalue (TREE_OPERAND (arg, 0)),
2866 invert_truthvalue (TREE_OPERAND (arg, 1)));
2868 case TRUTH_XOR_EXPR:
2869 /* Here we can invert either operand. We invert the first operand
2870 unless the second operand is a TRUTH_NOT_EXPR in which case our
2871 result is the XOR of the first operand with the inside of the
2872 negation of the second operand. */
2874 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2875 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2876 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2878 return build (TRUTH_XOR_EXPR, type,
2879 invert_truthvalue (TREE_OPERAND (arg, 0)),
2880 TREE_OPERAND (arg, 1));
2882 case TRUTH_ANDIF_EXPR:
2883 return build (TRUTH_ORIF_EXPR, type,
2884 invert_truthvalue (TREE_OPERAND (arg, 0)),
2885 invert_truthvalue (TREE_OPERAND (arg, 1)));
2887 case TRUTH_ORIF_EXPR:
2888 return build (TRUTH_ANDIF_EXPR, type,
2889 invert_truthvalue (TREE_OPERAND (arg, 0)),
2890 invert_truthvalue (TREE_OPERAND (arg, 1)));
2892 case TRUTH_NOT_EXPR:
2893 return TREE_OPERAND (arg, 0);
2896 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2897 invert_truthvalue (TREE_OPERAND (arg, 1)),
2898 invert_truthvalue (TREE_OPERAND (arg, 2)));
2901 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2902 invert_truthvalue (TREE_OPERAND (arg, 1)));
2904 case WITH_RECORD_EXPR:
2905 return build (WITH_RECORD_EXPR, type,
2906 invert_truthvalue (TREE_OPERAND (arg, 0)),
2907 TREE_OPERAND (arg, 1));
2909 case NON_LVALUE_EXPR:
2910 return invert_truthvalue (TREE_OPERAND (arg, 0));
2915 return build1 (TREE_CODE (arg), type,
2916 invert_truthvalue (TREE_OPERAND (arg, 0)));
2919 if (!integer_onep (TREE_OPERAND (arg, 1)))
2921 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2924 return build1 (TRUTH_NOT_EXPR, type, arg);
2926 case CLEANUP_POINT_EXPR:
2927 return build1 (CLEANUP_POINT_EXPR, type,
2928 invert_truthvalue (TREE_OPERAND (arg, 0)));
2933 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2935 return build1 (TRUTH_NOT_EXPR, type, arg);
2938 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2939 operands are another bit-wise operation with a common input. If so,
2940 distribute the bit operations to save an operation and possibly two if
2941 constants are involved. For example, convert
2942 (A | B) & (A | C) into A | (B & C)
2943 Further simplification will occur if B and C are constants.
2945 If this optimization cannot be done, 0 will be returned. */
2948 distribute_bit_expr (code, type, arg0, arg1)
2949 enum tree_code code;
2956 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2957 || TREE_CODE (arg0) == code
2958 || (TREE_CODE (arg0) != BIT_AND_EXPR
2959 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2962 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2964 common = TREE_OPERAND (arg0, 0);
2965 left = TREE_OPERAND (arg0, 1);
2966 right = TREE_OPERAND (arg1, 1);
2968 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2970 common = TREE_OPERAND (arg0, 0);
2971 left = TREE_OPERAND (arg0, 1);
2972 right = TREE_OPERAND (arg1, 0);
2974 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2976 common = TREE_OPERAND (arg0, 1);
2977 left = TREE_OPERAND (arg0, 0);
2978 right = TREE_OPERAND (arg1, 1);
2980 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2982 common = TREE_OPERAND (arg0, 1);
2983 left = TREE_OPERAND (arg0, 0);
2984 right = TREE_OPERAND (arg1, 0);
2989 return fold (build (TREE_CODE (arg0), type, common,
2990 fold (build (code, type, left, right))));
2993 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2994 starting at BITPOS. The field is unsigned if UNSIGNEDP is non-zero. */
2997 make_bit_field_ref (inner, type, bitsize, bitpos, unsignedp)
3000 int bitsize, bitpos;
3003 tree result = build (BIT_FIELD_REF, type, inner,
3004 size_int (bitsize), bitsize_int (bitpos));
3006 TREE_UNSIGNED (result) = unsignedp;
3011 /* Optimize a bit-field compare.
3013 There are two cases: First is a compare against a constant and the
3014 second is a comparison of two items where the fields are at the same
3015 bit position relative to the start of a chunk (byte, halfword, word)
3016 large enough to contain it. In these cases we can avoid the shift
3017 implicit in bitfield extractions.
3019 For constants, we emit a compare of the shifted constant with the
3020 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3021 compared. For two fields at the same position, we do the ANDs with the
3022 similar mask and compare the result of the ANDs.
3024 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3025 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3026 are the left and right operands of the comparison, respectively.
3028 If the optimization described above can be done, we return the resulting
3029 tree. Otherwise we return zero. */
3032 optimize_bit_field_compare (code, compare_type, lhs, rhs)
3033 enum tree_code code;
3037 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3038 tree type = TREE_TYPE (lhs);
3039 tree signed_type, unsigned_type;
3040 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3041 enum machine_mode lmode, rmode, nmode;
3042 int lunsignedp, runsignedp;
3043 int lvolatilep = 0, rvolatilep = 0;
3044 tree linner, rinner = NULL_TREE;
3048 /* Get all the information about the extractions being done. If the bit size
3049 if the same as the size of the underlying object, we aren't doing an
3050 extraction at all and so can do nothing. We also don't want to
3051 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3052 then will no longer be able to replace it. */
3053 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3054 &lunsignedp, &lvolatilep);
3055 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3056 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3061 /* If this is not a constant, we can only do something if bit positions,
3062 sizes, and signedness are the same. */
3063 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3064 &runsignedp, &rvolatilep);
3066 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3067 || lunsignedp != runsignedp || offset != 0
3068 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3072 /* See if we can find a mode to refer to this field. We should be able to,
3073 but fail if we can't. */
3074 nmode = get_best_mode (lbitsize, lbitpos,
3075 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3076 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3077 TYPE_ALIGN (TREE_TYPE (rinner))),
3078 word_mode, lvolatilep || rvolatilep);
3079 if (nmode == VOIDmode)
3082 /* Set signed and unsigned types of the precision of this mode for the
3084 signed_type = type_for_mode (nmode, 0);
3085 unsigned_type = type_for_mode (nmode, 1);
3087 /* Compute the bit position and size for the new reference and our offset
3088 within it. If the new reference is the same size as the original, we
3089 won't optimize anything, so return zero. */
3090 nbitsize = GET_MODE_BITSIZE (nmode);
3091 nbitpos = lbitpos & ~ (nbitsize - 1);
3093 if (nbitsize == lbitsize)
3096 if (BYTES_BIG_ENDIAN)
3097 lbitpos = nbitsize - lbitsize - lbitpos;
3099 /* Make the mask to be used against the extracted field. */
3100 mask = build_int_2 (~0, ~0);
3101 TREE_TYPE (mask) = unsigned_type;
3102 force_fit_type (mask, 0);
3103 mask = convert (unsigned_type, mask);
3104 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3105 mask = const_binop (RSHIFT_EXPR, mask,
3106 size_int (nbitsize - lbitsize - lbitpos), 0);
3109 /* If not comparing with constant, just rework the comparison
3111 return build (code, compare_type,
3112 build (BIT_AND_EXPR, unsigned_type,
3113 make_bit_field_ref (linner, unsigned_type,
3114 nbitsize, nbitpos, 1),
3116 build (BIT_AND_EXPR, unsigned_type,
3117 make_bit_field_ref (rinner, unsigned_type,
3118 nbitsize, nbitpos, 1),
3121 /* Otherwise, we are handling the constant case. See if the constant is too
3122 big for the field. Warn and return a tree of for 0 (false) if so. We do
3123 this not only for its own sake, but to avoid having to test for this
3124 error case below. If we didn't, we might generate wrong code.
3126 For unsigned fields, the constant shifted right by the field length should
3127 be all zero. For signed fields, the high-order bits should agree with
3132 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3133 convert (unsigned_type, rhs),
3134 size_int (lbitsize), 0)))
3136 warning ("comparison is always %d due to width of bit-field",
3138 return convert (compare_type,
3140 ? integer_one_node : integer_zero_node));
3145 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
3146 size_int (lbitsize - 1), 0);
3147 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3149 warning ("comparison is always %d due to width of bit-field",
3151 return convert (compare_type,
3153 ? integer_one_node : integer_zero_node));
3157 /* Single-bit compares should always be against zero. */
3158 if (lbitsize == 1 && ! integer_zerop (rhs))
3160 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3161 rhs = convert (type, integer_zero_node);
3164 /* Make a new bitfield reference, shift the constant over the
3165 appropriate number of bits and mask it with the computed mask
3166 (in case this was a signed field). If we changed it, make a new one. */
3167 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3170 TREE_SIDE_EFFECTS (lhs) = 1;
3171 TREE_THIS_VOLATILE (lhs) = 1;
3174 rhs = fold (const_binop (BIT_AND_EXPR,
3175 const_binop (LSHIFT_EXPR,
3176 convert (unsigned_type, rhs),
3177 size_int (lbitpos), 0),
3180 return build (code, compare_type,
3181 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
3185 /* Subroutine for fold_truthop: decode a field reference.
3187 If EXP is a comparison reference, we return the innermost reference.
3189 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3190 set to the starting bit number.
3192 If the innermost field can be completely contained in a mode-sized
3193 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3195 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3196 otherwise it is not changed.
3198 *PUNSIGNEDP is set to the signedness of the field.
3200 *PMASK is set to the mask used. This is either contained in a
3201 BIT_AND_EXPR or derived from the width of the field.
3203 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3205 Return 0 if this is not a component reference or is one that we can't
3206 do anything with. */
3209 decode_field_reference (exp, pbitsize, pbitpos, pmode, punsignedp,
3210 pvolatilep, pmask, pand_mask)
3212 HOST_WIDE_INT *pbitsize, *pbitpos;
3213 enum machine_mode *pmode;
3214 int *punsignedp, *pvolatilep;
3219 tree mask, inner, offset;
3221 unsigned int precision;
3223 /* All the optimizations using this function assume integer fields.
3224 There are problems with FP fields since the type_for_size call
3225 below can fail for, e.g., XFmode. */
3226 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3231 if (TREE_CODE (exp) == BIT_AND_EXPR)
3233 and_mask = TREE_OPERAND (exp, 1);
3234 exp = TREE_OPERAND (exp, 0);
3235 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3236 if (TREE_CODE (and_mask) != INTEGER_CST)
3240 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3241 punsignedp, pvolatilep);
3242 if ((inner == exp && and_mask == 0)
3243 || *pbitsize < 0 || offset != 0
3244 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3247 /* Compute the mask to access the bitfield. */
3248 unsigned_type = type_for_size (*pbitsize, 1);
3249 precision = TYPE_PRECISION (unsigned_type);
3251 mask = build_int_2 (~0, ~0);
3252 TREE_TYPE (mask) = unsigned_type;
3253 force_fit_type (mask, 0);
3254 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3255 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3257 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3259 mask = fold (build (BIT_AND_EXPR, unsigned_type,
3260 convert (unsigned_type, and_mask), mask));
3263 *pand_mask = and_mask;
3267 /* Return non-zero if MASK represents a mask of SIZE ones in the low-order
3271 all_ones_mask_p (mask, size)
3275 tree type = TREE_TYPE (mask);
3276 unsigned int precision = TYPE_PRECISION (type);
3279 tmask = build_int_2 (~0, ~0);
3280 TREE_TYPE (tmask) = signed_type (type);
3281 force_fit_type (tmask, 0);
3283 tree_int_cst_equal (mask,
3284 const_binop (RSHIFT_EXPR,
3285 const_binop (LSHIFT_EXPR, tmask,
3286 size_int (precision - size),
3288 size_int (precision - size), 0));
3291 /* Subroutine for fold_truthop: determine if an operand is simple enough
3292 to be evaluated unconditionally. */
3295 simple_operand_p (exp)
3298 /* Strip any conversions that don't change the machine mode. */
3299 while ((TREE_CODE (exp) == NOP_EXPR
3300 || TREE_CODE (exp) == CONVERT_EXPR)
3301 && (TYPE_MODE (TREE_TYPE (exp))
3302 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3303 exp = TREE_OPERAND (exp, 0);
3305 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3307 && ! TREE_ADDRESSABLE (exp)
3308 && ! TREE_THIS_VOLATILE (exp)
3309 && ! DECL_NONLOCAL (exp)
3310 /* Don't regard global variables as simple. They may be
3311 allocated in ways unknown to the compiler (shared memory,
3312 #pragma weak, etc). */
3313 && ! TREE_PUBLIC (exp)
3314 && ! DECL_EXTERNAL (exp)
3315 /* Loading a static variable is unduly expensive, but global
3316 registers aren't expensive. */
3317 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3320 /* The following functions are subroutines to fold_range_test and allow it to
3321 try to change a logical combination of comparisons into a range test.
3324 X == 2 || X == 3 || X == 4 || X == 5
3328 (unsigned) (X - 2) <= 3
3330 We describe each set of comparisons as being either inside or outside
3331 a range, using a variable named like IN_P, and then describe the
3332 range with a lower and upper bound. If one of the bounds is omitted,
3333 it represents either the highest or lowest value of the type.
3335 In the comments below, we represent a range by two numbers in brackets
3336 preceded by a "+" to designate being inside that range, or a "-" to
3337 designate being outside that range, so the condition can be inverted by
3338 flipping the prefix. An omitted bound is represented by a "-". For
3339 example, "- [-, 10]" means being outside the range starting at the lowest
3340 possible value and ending at 10, in other words, being greater than 10.
3341 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3344 We set up things so that the missing bounds are handled in a consistent
3345 manner so neither a missing bound nor "true" and "false" need to be
3346 handled using a special case. */
3348 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3349 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3350 and UPPER1_P are nonzero if the respective argument is an upper bound
3351 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3352 must be specified for a comparison. ARG1 will be converted to ARG0's
3353 type if both are specified. */
3356 range_binop (code, type, arg0, upper0_p, arg1, upper1_p)
3357 enum tree_code code;
3360 int upper0_p, upper1_p;
3366 /* If neither arg represents infinity, do the normal operation.
3367 Else, if not a comparison, return infinity. Else handle the special
3368 comparison rules. Note that most of the cases below won't occur, but
3369 are handled for consistency. */
3371 if (arg0 != 0 && arg1 != 0)
3373 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3374 arg0, convert (TREE_TYPE (arg0), arg1)));
3376 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3379 if (TREE_CODE_CLASS (code) != '<')
3382 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3383 for neither. In real maths, we cannot assume open ended ranges are
3384 the same. But, this is computer arithmetic, where numbers are finite.
3385 We can therefore make the transformation of any unbounded range with
3386 the value Z, Z being greater than any representable number. This permits
3387 us to treat unbounded ranges as equal. */
3388 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3389 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3393 result = sgn0 == sgn1;
3396 result = sgn0 != sgn1;
3399 result = sgn0 < sgn1;
3402 result = sgn0 <= sgn1;
3405 result = sgn0 > sgn1;
3408 result = sgn0 >= sgn1;
3414 return convert (type, result ? integer_one_node : integer_zero_node);
3417 /* Given EXP, a logical expression, set the range it is testing into
3418 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3419 actually being tested. *PLOW and *PHIGH will be made of the same type
3420 as the returned expression. If EXP is not a comparison, we will most
3421 likely not be returning a useful value and range. */
3424 make_range (exp, pin_p, plow, phigh)
3429 enum tree_code code;
3430 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3431 tree orig_type = NULL_TREE;
3433 tree low, high, n_low, n_high;
3435 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3436 and see if we can refine the range. Some of the cases below may not
3437 happen, but it doesn't seem worth worrying about this. We "continue"
3438 the outer loop when we've changed something; otherwise we "break"
3439 the switch, which will "break" the while. */
3441 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3445 code = TREE_CODE (exp);
3447 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3449 arg0 = TREE_OPERAND (exp, 0);
3450 if (TREE_CODE_CLASS (code) == '<'
3451 || TREE_CODE_CLASS (code) == '1'
3452 || TREE_CODE_CLASS (code) == '2')
3453 type = TREE_TYPE (arg0);
3454 if (TREE_CODE_CLASS (code) == '2'
3455 || TREE_CODE_CLASS (code) == '<'
3456 || (TREE_CODE_CLASS (code) == 'e'
3457 && TREE_CODE_LENGTH (code) > 1))
3458 arg1 = TREE_OPERAND (exp, 1);
3461 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3462 lose a cast by accident. */
3463 if (type != NULL_TREE && orig_type == NULL_TREE)
3468 case TRUTH_NOT_EXPR:
3469 in_p = ! in_p, exp = arg0;
3472 case EQ_EXPR: case NE_EXPR:
3473 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3474 /* We can only do something if the range is testing for zero
3475 and if the second operand is an integer constant. Note that
3476 saying something is "in" the range we make is done by
3477 complementing IN_P since it will set in the initial case of
3478 being not equal to zero; "out" is leaving it alone. */
3479 if (low == 0 || high == 0
3480 || ! integer_zerop (low) || ! integer_zerop (high)
3481 || TREE_CODE (arg1) != INTEGER_CST)
3486 case NE_EXPR: /* - [c, c] */
3489 case EQ_EXPR: /* + [c, c] */
3490 in_p = ! in_p, low = high = arg1;
3492 case GT_EXPR: /* - [-, c] */
3493 low = 0, high = arg1;
3495 case GE_EXPR: /* + [c, -] */
3496 in_p = ! in_p, low = arg1, high = 0;
3498 case LT_EXPR: /* - [c, -] */
3499 low = arg1, high = 0;
3501 case LE_EXPR: /* + [-, c] */
3502 in_p = ! in_p, low = 0, high = arg1;
3510 /* If this is an unsigned comparison, we also know that EXP is
3511 greater than or equal to zero. We base the range tests we make
3512 on that fact, so we record it here so we can parse existing
3514 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3516 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3517 1, convert (type, integer_zero_node),
3521 in_p = n_in_p, low = n_low, high = n_high;
3523 /* If the high bound is missing, but we
3524 have a low bound, reverse the range so
3525 it goes from zero to the low bound minus 1. */
3526 if (high == 0 && low)
3529 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3530 integer_one_node, 0);
3531 low = convert (type, integer_zero_node);
3537 /* (-x) IN [a,b] -> x in [-b, -a] */
3538 n_low = range_binop (MINUS_EXPR, type,
3539 convert (type, integer_zero_node), 0, high, 1);
3540 n_high = range_binop (MINUS_EXPR, type,
3541 convert (type, integer_zero_node), 0, low, 0);
3542 low = n_low, high = n_high;
3548 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3549 convert (type, integer_one_node));
3552 case PLUS_EXPR: case MINUS_EXPR:
3553 if (TREE_CODE (arg1) != INTEGER_CST)
3556 /* If EXP is signed, any overflow in the computation is undefined,
3557 so we don't worry about it so long as our computations on
3558 the bounds don't overflow. For unsigned, overflow is defined
3559 and this is exactly the right thing. */
3560 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3561 type, low, 0, arg1, 0);
3562 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3563 type, high, 1, arg1, 0);
3564 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3565 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3568 /* Check for an unsigned range which has wrapped around the maximum
3569 value thus making n_high < n_low, and normalize it. */
3570 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3572 low = range_binop (PLUS_EXPR, type, n_high, 0,
3573 integer_one_node, 0);
3574 high = range_binop (MINUS_EXPR, type, n_low, 0,
3575 integer_one_node, 0);
3577 /* If the range is of the form +/- [ x+1, x ], we won't
3578 be able to normalize it. But then, it represents the
3579 whole range or the empty set, so make it
3581 if (tree_int_cst_equal (n_low, low)
3582 && tree_int_cst_equal (n_high, high))
3588 low = n_low, high = n_high;
3593 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3594 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3597 if (! INTEGRAL_TYPE_P (type)
3598 || (low != 0 && ! int_fits_type_p (low, type))
3599 || (high != 0 && ! int_fits_type_p (high, type)))
3602 n_low = low, n_high = high;
3605 n_low = convert (type, n_low);
3608 n_high = convert (type, n_high);
3610 /* If we're converting from an unsigned to a signed type,
3611 we will be doing the comparison as unsigned. The tests above
3612 have already verified that LOW and HIGH are both positive.
3614 So we have to make sure that the original unsigned value will
3615 be interpreted as positive. */
3616 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3618 tree equiv_type = type_for_mode (TYPE_MODE (type), 1);
3621 /* A range without an upper bound is, naturally, unbounded.
3622 Since convert would have cropped a very large value, use
3623 the max value for the destination type. */
3625 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3626 : TYPE_MAX_VALUE (type);
3628 high_positive = fold (build (RSHIFT_EXPR, type,
3629 convert (type, high_positive),
3630 convert (type, integer_one_node)));
3632 /* If the low bound is specified, "and" the range with the
3633 range for which the original unsigned value will be
3637 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3639 1, convert (type, integer_zero_node),
3643 in_p = (n_in_p == in_p);
3647 /* Otherwise, "or" the range with the range of the input
3648 that will be interpreted as negative. */
3649 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3651 1, convert (type, integer_zero_node),
3655 in_p = (in_p != n_in_p);
3660 low = n_low, high = n_high;
3670 /* If EXP is a constant, we can evaluate whether this is true or false. */
3671 if (TREE_CODE (exp) == INTEGER_CST)
3673 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3675 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3681 *pin_p = in_p, *plow = low, *phigh = high;
3685 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3686 type, TYPE, return an expression to test if EXP is in (or out of, depending
3687 on IN_P) the range. */
3690 build_range_check (type, exp, in_p, low, high)
3696 tree etype = TREE_TYPE (exp);
3700 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3701 return invert_truthvalue (value);
3703 else if (low == 0 && high == 0)
3704 return convert (type, integer_one_node);
3707 return fold (build (LE_EXPR, type, exp, high));
3710 return fold (build (GE_EXPR, type, exp, low));
3712 else if (operand_equal_p (low, high, 0))
3713 return fold (build (EQ_EXPR, type, exp, low));
3715 else if (TREE_UNSIGNED (etype) && integer_zerop (low))
3716 return build_range_check (type, exp, 1, 0, high);
3718 else if (integer_zerop (low))
3720 utype = unsigned_type (etype);
3721 return build_range_check (type, convert (utype, exp), 1, 0,
3722 convert (utype, high));
3725 else if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3726 && ! TREE_OVERFLOW (value))
3727 return build_range_check (type,
3728 fold (build (MINUS_EXPR, etype, exp, low)),
3729 1, convert (etype, integer_zero_node), value);
3734 /* Given two ranges, see if we can merge them into one. Return 1 if we
3735 can, 0 if we can't. Set the output range into the specified parameters. */
3738 merge_ranges (pin_p, plow, phigh, in0_p, low0, high0, in1_p, low1, high1)
3742 tree low0, high0, low1, high1;
3750 int lowequal = ((low0 == 0 && low1 == 0)
3751 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3752 low0, 0, low1, 0)));
3753 int highequal = ((high0 == 0 && high1 == 0)
3754 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3755 high0, 1, high1, 1)));
3757 /* Make range 0 be the range that starts first, or ends last if they
3758 start at the same value. Swap them if it isn't. */
3759 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3762 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3763 high1, 1, high0, 1))))
3765 temp = in0_p, in0_p = in1_p, in1_p = temp;
3766 tem = low0, low0 = low1, low1 = tem;
3767 tem = high0, high0 = high1, high1 = tem;
3770 /* Now flag two cases, whether the ranges are disjoint or whether the
3771 second range is totally subsumed in the first. Note that the tests
3772 below are simplified by the ones above. */
3773 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3774 high0, 1, low1, 0));
3775 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3776 high1, 1, high0, 1));
3778 /* We now have four cases, depending on whether we are including or
3779 excluding the two ranges. */
3782 /* If they don't overlap, the result is false. If the second range
3783 is a subset it is the result. Otherwise, the range is from the start
3784 of the second to the end of the first. */
3786 in_p = 0, low = high = 0;
3788 in_p = 1, low = low1, high = high1;
3790 in_p = 1, low = low1, high = high0;
3793 else if (in0_p && ! in1_p)
3795 /* If they don't overlap, the result is the first range. If they are
3796 equal, the result is false. If the second range is a subset of the
3797 first, and the ranges begin at the same place, we go from just after
3798 the end of the first range to the end of the second. If the second
3799 range is not a subset of the first, or if it is a subset and both
3800 ranges end at the same place, the range starts at the start of the
3801 first range and ends just before the second range.
3802 Otherwise, we can't describe this as a single range. */
3804 in_p = 1, low = low0, high = high0;
3805 else if (lowequal && highequal)
3806 in_p = 0, low = high = 0;
3807 else if (subset && lowequal)
3809 in_p = 1, high = high0;
3810 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3811 integer_one_node, 0);
3813 else if (! subset || highequal)
3815 in_p = 1, low = low0;
3816 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3817 integer_one_node, 0);
3823 else if (! in0_p && in1_p)
3825 /* If they don't overlap, the result is the second range. If the second
3826 is a subset of the first, the result is false. Otherwise,
3827 the range starts just after the first range and ends at the
3828 end of the second. */
3830 in_p = 1, low = low1, high = high1;
3831 else if (subset || highequal)
3832 in_p = 0, low = high = 0;
3835 in_p = 1, high = high1;
3836 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3837 integer_one_node, 0);
3843 /* The case where we are excluding both ranges. Here the complex case
3844 is if they don't overlap. In that case, the only time we have a
3845 range is if they are adjacent. If the second is a subset of the
3846 first, the result is the first. Otherwise, the range to exclude
3847 starts at the beginning of the first range and ends at the end of the
3851 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3852 range_binop (PLUS_EXPR, NULL_TREE,
3854 integer_one_node, 1),
3856 in_p = 0, low = low0, high = high1;
3861 in_p = 0, low = low0, high = high0;
3863 in_p = 0, low = low0, high = high1;
3866 *pin_p = in_p, *plow = low, *phigh = high;
3870 /* EXP is some logical combination of boolean tests. See if we can
3871 merge it into some range test. Return the new tree if so. */
3874 fold_range_test (exp)
3877 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3878 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3879 int in0_p, in1_p, in_p;
3880 tree low0, low1, low, high0, high1, high;
3881 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3882 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3885 /* If this is an OR operation, invert both sides; we will invert
3886 again at the end. */
3888 in0_p = ! in0_p, in1_p = ! in1_p;
3890 /* If both expressions are the same, if we can merge the ranges, and we
3891 can build the range test, return it or it inverted. If one of the
3892 ranges is always true or always false, consider it to be the same
3893 expression as the other. */
3894 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3895 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3897 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3899 : rhs != 0 ? rhs : integer_zero_node,
3901 return or_op ? invert_truthvalue (tem) : tem;
3903 /* On machines where the branch cost is expensive, if this is a
3904 short-circuited branch and the underlying object on both sides
3905 is the same, make a non-short-circuit operation. */
3906 else if (BRANCH_COST >= 2
3907 && lhs != 0 && rhs != 0
3908 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3909 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3910 && operand_equal_p (lhs, rhs, 0))
3912 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3913 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3914 which cases we can't do this. */
3915 if (simple_operand_p (lhs))
3916 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3917 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3918 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3919 TREE_OPERAND (exp, 1));
3921 else if (global_bindings_p () == 0
3922 && ! contains_placeholder_p (lhs))
3924 tree common = save_expr (lhs);
3926 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3927 or_op ? ! in0_p : in0_p,
3929 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3930 or_op ? ! in1_p : in1_p,
3932 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3933 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3934 TREE_TYPE (exp), lhs, rhs);
3941 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3942 bit value. Arrange things so the extra bits will be set to zero if and
3943 only if C is signed-extended to its full width. If MASK is nonzero,
3944 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3947 unextend (c, p, unsignedp, mask)
3953 tree type = TREE_TYPE (c);
3954 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3957 if (p == modesize || unsignedp)
3960 /* We work by getting just the sign bit into the low-order bit, then
3961 into the high-order bit, then sign-extend. We then XOR that value
3963 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3964 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3966 /* We must use a signed type in order to get an arithmetic right shift.
3967 However, we must also avoid introducing accidental overflows, so that
3968 a subsequent call to integer_zerop will work. Hence we must
3969 do the type conversion here. At this point, the constant is either
3970 zero or one, and the conversion to a signed type can never overflow.
3971 We could get an overflow if this conversion is done anywhere else. */
3972 if (TREE_UNSIGNED (type))
3973 temp = convert (signed_type (type), temp);
3975 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3976 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3978 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3979 /* If necessary, convert the type back to match the type of C. */
3980 if (TREE_UNSIGNED (type))
3981 temp = convert (type, temp);
3983 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3986 /* Find ways of folding logical expressions of LHS and RHS:
3987 Try to merge two comparisons to the same innermost item.
3988 Look for range tests like "ch >= '0' && ch <= '9'".
3989 Look for combinations of simple terms on machines with expensive branches
3990 and evaluate the RHS unconditionally.
3992 For example, if we have p->a == 2 && p->b == 4 and we can make an
3993 object large enough to span both A and B, we can do this with a comparison
3994 against the object ANDed with the a mask.
3996 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3997 operations to do this with one comparison.
3999 We check for both normal comparisons and the BIT_AND_EXPRs made this by
4000 function and the one above.
4002 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
4003 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4005 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4008 We return the simplified tree or 0 if no optimization is possible. */
4011 fold_truthop (code, truth_type, lhs, rhs)
4012 enum tree_code code;
4013 tree truth_type, lhs, rhs;
4015 /* If this is the "or" of two comparisons, we can do something if
4016 the comparisons are NE_EXPR. If this is the "and", we can do something
4017 if the comparisons are EQ_EXPR. I.e.,
4018 (a->b == 2 && a->c == 4) can become (a->new == NEW).
4020 WANTED_CODE is this operation code. For single bit fields, we can
4021 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4022 comparison for one-bit fields. */
4024 enum tree_code wanted_code;
4025 enum tree_code lcode, rcode;
4026 tree ll_arg, lr_arg, rl_arg, rr_arg;
4027 tree ll_inner, lr_inner, rl_inner, rr_inner;
4028 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4029 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4030 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4031 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4032 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4033 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4034 enum machine_mode lnmode, rnmode;
4035 tree ll_mask, lr_mask, rl_mask, rr_mask;
4036 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4037 tree l_const, r_const;
4038 tree lntype, rntype, result;
4039 int first_bit, end_bit;
4042 /* Start by getting the comparison codes. Fail if anything is volatile.
4043 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4044 it were surrounded with a NE_EXPR. */
4046 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4049 lcode = TREE_CODE (lhs);
4050 rcode = TREE_CODE (rhs);
4052 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4053 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
4055 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4056 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
4058 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
4061 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4062 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4064 ll_arg = TREE_OPERAND (lhs, 0);
4065 lr_arg = TREE_OPERAND (lhs, 1);
4066 rl_arg = TREE_OPERAND (rhs, 0);
4067 rr_arg = TREE_OPERAND (rhs, 1);
4069 /* If the RHS can be evaluated unconditionally and its operands are
4070 simple, it wins to evaluate the RHS unconditionally on machines
4071 with expensive branches. In this case, this isn't a comparison
4072 that can be merged. Avoid doing this if the RHS is a floating-point
4073 comparison since those can trap. */
4075 if (BRANCH_COST >= 2
4076 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4077 && simple_operand_p (rl_arg)
4078 && simple_operand_p (rr_arg))
4079 return build (code, truth_type, lhs, rhs);
4081 /* See if the comparisons can be merged. Then get all the parameters for
4084 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4085 || (rcode != EQ_EXPR && rcode != NE_EXPR))
4089 ll_inner = decode_field_reference (ll_arg,
4090 &ll_bitsize, &ll_bitpos, &ll_mode,
4091 &ll_unsignedp, &volatilep, &ll_mask,
4093 lr_inner = decode_field_reference (lr_arg,
4094 &lr_bitsize, &lr_bitpos, &lr_mode,
4095 &lr_unsignedp, &volatilep, &lr_mask,
4097 rl_inner = decode_field_reference (rl_arg,
4098 &rl_bitsize, &rl_bitpos, &rl_mode,
4099 &rl_unsignedp, &volatilep, &rl_mask,
4101 rr_inner = decode_field_reference (rr_arg,
4102 &rr_bitsize, &rr_bitpos, &rr_mode,
4103 &rr_unsignedp, &volatilep, &rr_mask,
4106 /* It must be true that the inner operation on the lhs of each
4107 comparison must be the same if we are to be able to do anything.
4108 Then see if we have constants. If not, the same must be true for
4110 if (volatilep || ll_inner == 0 || rl_inner == 0
4111 || ! operand_equal_p (ll_inner, rl_inner, 0))
4114 if (TREE_CODE (lr_arg) == INTEGER_CST
4115 && TREE_CODE (rr_arg) == INTEGER_CST)
4116 l_const = lr_arg, r_const = rr_arg;
4117 else if (lr_inner == 0 || rr_inner == 0
4118 || ! operand_equal_p (lr_inner, rr_inner, 0))
4121 l_const = r_const = 0;
4123 /* If either comparison code is not correct for our logical operation,
4124 fail. However, we can convert a one-bit comparison against zero into
4125 the opposite comparison against that bit being set in the field. */
4127 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4128 if (lcode != wanted_code)
4130 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4132 /* Make the left operand unsigned, since we are only interested
4133 in the value of one bit. Otherwise we are doing the wrong
4142 /* This is analogous to the code for l_const above. */
4143 if (rcode != wanted_code)
4145 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4154 /* See if we can find a mode that contains both fields being compared on
4155 the left. If we can't, fail. Otherwise, update all constants and masks
4156 to be relative to a field of that size. */
4157 first_bit = MIN (ll_bitpos, rl_bitpos);
4158 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4159 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4160 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4162 if (lnmode == VOIDmode)
4165 lnbitsize = GET_MODE_BITSIZE (lnmode);
4166 lnbitpos = first_bit & ~ (lnbitsize - 1);
4167 lntype = type_for_size (lnbitsize, 1);
4168 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4170 if (BYTES_BIG_ENDIAN)
4172 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4173 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4176 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
4177 size_int (xll_bitpos), 0);
4178 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
4179 size_int (xrl_bitpos), 0);
4183 l_const = convert (lntype, l_const);
4184 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4185 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4186 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4187 fold (build1 (BIT_NOT_EXPR,
4191 warning ("comparison is always %d", wanted_code == NE_EXPR);
4193 return convert (truth_type,
4194 wanted_code == NE_EXPR
4195 ? integer_one_node : integer_zero_node);
4200 r_const = convert (lntype, r_const);
4201 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4202 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4203 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4204 fold (build1 (BIT_NOT_EXPR,
4208 warning ("comparison is always %d", wanted_code == NE_EXPR);
4210 return convert (truth_type,
4211 wanted_code == NE_EXPR
4212 ? integer_one_node : integer_zero_node);
4216 /* If the right sides are not constant, do the same for it. Also,
4217 disallow this optimization if a size or signedness mismatch occurs
4218 between the left and right sides. */
4221 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4222 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4223 /* Make sure the two fields on the right
4224 correspond to the left without being swapped. */
4225 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4228 first_bit = MIN (lr_bitpos, rr_bitpos);
4229 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4230 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4231 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4233 if (rnmode == VOIDmode)
4236 rnbitsize = GET_MODE_BITSIZE (rnmode);
4237 rnbitpos = first_bit & ~ (rnbitsize - 1);
4238 rntype = type_for_size (rnbitsize, 1);
4239 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4241 if (BYTES_BIG_ENDIAN)
4243 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4244 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4247 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
4248 size_int (xlr_bitpos), 0);
4249 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
4250 size_int (xrr_bitpos), 0);
4252 /* Make a mask that corresponds to both fields being compared.
4253 Do this for both items being compared. If the operands are the
4254 same size and the bits being compared are in the same position
4255 then we can do this by masking both and comparing the masked
4257 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4258 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4259 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4261 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4262 ll_unsignedp || rl_unsignedp);
4263 if (! all_ones_mask_p (ll_mask, lnbitsize))
4264 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4266 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4267 lr_unsignedp || rr_unsignedp);
4268 if (! all_ones_mask_p (lr_mask, rnbitsize))
4269 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4271 return build (wanted_code, truth_type, lhs, rhs);
4274 /* There is still another way we can do something: If both pairs of
4275 fields being compared are adjacent, we may be able to make a wider
4276 field containing them both.
4278 Note that we still must mask the lhs/rhs expressions. Furthermore,
4279 the mask must be shifted to account for the shift done by
4280 make_bit_field_ref. */
4281 if ((ll_bitsize + ll_bitpos == rl_bitpos
4282 && lr_bitsize + lr_bitpos == rr_bitpos)
4283 || (ll_bitpos == rl_bitpos + rl_bitsize
4284 && lr_bitpos == rr_bitpos + rr_bitsize))
4288 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4289 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4290 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4291 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4293 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4294 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4295 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4296 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4298 /* Convert to the smaller type before masking out unwanted bits. */
4300 if (lntype != rntype)
4302 if (lnbitsize > rnbitsize)
4304 lhs = convert (rntype, lhs);
4305 ll_mask = convert (rntype, ll_mask);
4308 else if (lnbitsize < rnbitsize)
4310 rhs = convert (lntype, rhs);
4311 lr_mask = convert (lntype, lr_mask);
4316 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4317 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4319 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4320 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4322 return build (wanted_code, truth_type, lhs, rhs);
4328 /* Handle the case of comparisons with constants. If there is something in
4329 common between the masks, those bits of the constants must be the same.
4330 If not, the condition is always false. Test for this to avoid generating
4331 incorrect code below. */
4332 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4333 if (! integer_zerop (result)
4334 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4335 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4337 if (wanted_code == NE_EXPR)
4339 warning ("`or' of unmatched not-equal tests is always 1");
4340 return convert (truth_type, integer_one_node);
4344 warning ("`and' of mutually exclusive equal-tests is always 0");
4345 return convert (truth_type, integer_zero_node);
4349 /* Construct the expression we will return. First get the component
4350 reference we will make. Unless the mask is all ones the width of
4351 that field, perform the mask operation. Then compare with the
4353 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4354 ll_unsignedp || rl_unsignedp);
4356 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4357 if (! all_ones_mask_p (ll_mask, lnbitsize))
4358 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
4360 return build (wanted_code, truth_type, result,
4361 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4364 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4368 optimize_minmax_comparison (t)
4371 tree type = TREE_TYPE (t);
4372 tree arg0 = TREE_OPERAND (t, 0);
4373 enum tree_code op_code;
4374 tree comp_const = TREE_OPERAND (t, 1);
4376 int consts_equal, consts_lt;
4379 STRIP_SIGN_NOPS (arg0);
4381 op_code = TREE_CODE (arg0);
4382 minmax_const = TREE_OPERAND (arg0, 1);
4383 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4384 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4385 inner = TREE_OPERAND (arg0, 0);
4387 /* If something does not permit us to optimize, return the original tree. */
4388 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4389 || TREE_CODE (comp_const) != INTEGER_CST
4390 || TREE_CONSTANT_OVERFLOW (comp_const)
4391 || TREE_CODE (minmax_const) != INTEGER_CST
4392 || TREE_CONSTANT_OVERFLOW (minmax_const))
4395 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4396 and GT_EXPR, doing the rest with recursive calls using logical
4398 switch (TREE_CODE (t))
4400 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4402 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4406 fold (build (TRUTH_ORIF_EXPR, type,
4407 optimize_minmax_comparison
4408 (build (EQ_EXPR, type, arg0, comp_const)),
4409 optimize_minmax_comparison
4410 (build (GT_EXPR, type, arg0, comp_const))));
4413 if (op_code == MAX_EXPR && consts_equal)
4414 /* MAX (X, 0) == 0 -> X <= 0 */
4415 return fold (build (LE_EXPR, type, inner, comp_const));
4417 else if (op_code == MAX_EXPR && consts_lt)
4418 /* MAX (X, 0) == 5 -> X == 5 */
4419 return fold (build (EQ_EXPR, type, inner, comp_const));
4421 else if (op_code == MAX_EXPR)
4422 /* MAX (X, 0) == -1 -> false */
4423 return omit_one_operand (type, integer_zero_node, inner);
4425 else if (consts_equal)
4426 /* MIN (X, 0) == 0 -> X >= 0 */
4427 return fold (build (GE_EXPR, type, inner, comp_const));
4430 /* MIN (X, 0) == 5 -> false */
4431 return omit_one_operand (type, integer_zero_node, inner);
4434 /* MIN (X, 0) == -1 -> X == -1 */
4435 return fold (build (EQ_EXPR, type, inner, comp_const));
4438 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4439 /* MAX (X, 0) > 0 -> X > 0
4440 MAX (X, 0) > 5 -> X > 5 */
4441 return fold (build (GT_EXPR, type, inner, comp_const));
4443 else if (op_code == MAX_EXPR)
4444 /* MAX (X, 0) > -1 -> true */
4445 return omit_one_operand (type, integer_one_node, inner);
4447 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4448 /* MIN (X, 0) > 0 -> false
4449 MIN (X, 0) > 5 -> false */
4450 return omit_one_operand (type, integer_zero_node, inner);
4453 /* MIN (X, 0) > -1 -> X > -1 */
4454 return fold (build (GT_EXPR, type, inner, comp_const));
4461 /* T is an integer expression that is being multiplied, divided, or taken a
4462 modulus (CODE says which and what kind of divide or modulus) by a
4463 constant C. See if we can eliminate that operation by folding it with
4464 other operations already in T. WIDE_TYPE, if non-null, is a type that
4465 should be used for the computation if wider than our type.
4467 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4468 (X * 2) + (Y * 4). We must, however, be assured that either the original
4469 expression would not overflow or that overflow is undefined for the type
4470 in the language in question.
4472 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4473 the machine has a multiply-accumulate insn or that this is part of an
4474 addressing calculation.
4476 If we return a non-null expression, it is an equivalent form of the
4477 original computation, but need not be in the original type. */
4480 extract_muldiv (t, c, code, wide_type)
4483 enum tree_code code;
4486 tree type = TREE_TYPE (t);
4487 enum tree_code tcode = TREE_CODE (t);
4488 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4489 > GET_MODE_SIZE (TYPE_MODE (type)))
4490 ? wide_type : type);
4492 int same_p = tcode == code;
4493 tree op0 = NULL_TREE, op1 = NULL_TREE;
4495 /* Don't deal with constants of zero here; they confuse the code below. */
4496 if (integer_zerop (c))
4499 if (TREE_CODE_CLASS (tcode) == '1')
4500 op0 = TREE_OPERAND (t, 0);
4502 if (TREE_CODE_CLASS (tcode) == '2')
4503 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4505 /* Note that we need not handle conditional operations here since fold
4506 already handles those cases. So just do arithmetic here. */
4510 /* For a constant, we can always simplify if we are a multiply
4511 or (for divide and modulus) if it is a multiple of our constant. */
4512 if (code == MULT_EXPR
4513 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4514 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4517 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4518 /* If op0 is an expression, and is unsigned, and the type is
4519 smaller than ctype, then we cannot widen the expression. */
4520 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4521 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4522 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4523 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4524 && TREE_UNSIGNED (TREE_TYPE (op0))
4525 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4526 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4527 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4528 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4531 /* Pass the constant down and see if we can make a simplification. If
4532 we can, replace this expression with the inner simplification for
4533 possible later conversion to our or some other type. */
4534 if (0 != (t1 = extract_muldiv (op0, convert (TREE_TYPE (op0), c), code,
4535 code == MULT_EXPR ? ctype : NULL_TREE)))
4539 case NEGATE_EXPR: case ABS_EXPR:
4540 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4541 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4544 case MIN_EXPR: case MAX_EXPR:
4545 /* If widening the type changes the signedness, then we can't perform
4546 this optimization as that changes the result. */
4547 if (TREE_UNSIGNED (ctype) != TREE_UNSIGNED (type))
4550 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4551 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4552 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4554 if (tree_int_cst_sgn (c) < 0)
4555 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4557 return fold (build (tcode, ctype, convert (ctype, t1),
4558 convert (ctype, t2)));
4562 case WITH_RECORD_EXPR:
4563 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4564 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4565 TREE_OPERAND (t, 1));
4569 /* If this has not been evaluated and the operand has no side effects,
4570 we can see if we can do something inside it and make a new one.
4571 Note that this test is overly conservative since we can do this
4572 if the only reason it had side effects is that it was another
4573 similar SAVE_EXPR, but that isn't worth bothering with. */
4574 if (SAVE_EXPR_RTL (t) == 0 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0))
4575 && 0 != (t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code,
4578 t1 = save_expr (t1);
4579 if (SAVE_EXPR_PERSISTENT_P (t) && TREE_CODE (t1) == SAVE_EXPR)
4580 SAVE_EXPR_PERSISTENT_P (t1) = 1;
4581 if (is_pending_size (t))
4582 put_pending_size (t1);
4587 case LSHIFT_EXPR: case RSHIFT_EXPR:
4588 /* If the second operand is constant, this is a multiplication
4589 or floor division, by a power of two, so we can treat it that
4590 way unless the multiplier or divisor overflows. */
4591 if (TREE_CODE (op1) == INTEGER_CST
4592 /* const_binop may not detect overflow correctly,
4593 so check for it explicitly here. */
4594 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4595 && TREE_INT_CST_HIGH (op1) == 0
4596 && 0 != (t1 = convert (ctype,
4597 const_binop (LSHIFT_EXPR, size_one_node,
4599 && ! TREE_OVERFLOW (t1))
4600 return extract_muldiv (build (tcode == LSHIFT_EXPR
4601 ? MULT_EXPR : FLOOR_DIV_EXPR,
4602 ctype, convert (ctype, op0), t1),
4603 c, code, wide_type);
4606 case PLUS_EXPR: case MINUS_EXPR:
4607 /* See if we can eliminate the operation on both sides. If we can, we
4608 can return a new PLUS or MINUS. If we can't, the only remaining
4609 cases where we can do anything are if the second operand is a
4611 t1 = extract_muldiv (op0, c, code, wide_type);
4612 t2 = extract_muldiv (op1, c, code, wide_type);
4613 if (t1 != 0 && t2 != 0
4614 && (code == MULT_EXPR
4615 /* If not multiplication, we can only do this if either operand
4616 is divisible by c. */
4617 || multiple_of_p (ctype, op0, c)
4618 || multiple_of_p (ctype, op1, c)))
4619 return fold (build (tcode, ctype, convert (ctype, t1),
4620 convert (ctype, t2)));
4622 /* If this was a subtraction, negate OP1 and set it to be an addition.
4623 This simplifies the logic below. */
4624 if (tcode == MINUS_EXPR)
4625 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4627 if (TREE_CODE (op1) != INTEGER_CST)
4630 /* If either OP1 or C are negative, this optimization is not safe for
4631 some of the division and remainder types while for others we need
4632 to change the code. */
4633 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4635 if (code == CEIL_DIV_EXPR)
4636 code = FLOOR_DIV_EXPR;
4637 else if (code == FLOOR_DIV_EXPR)
4638 code = CEIL_DIV_EXPR;
4639 else if (code != MULT_EXPR
4640 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
4644 /* If it's a multiply or a division/modulus operation of a multiple
4645 of our constant, do the operation and verify it doesn't overflow. */
4646 if (code == MULT_EXPR
4647 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4649 op1 = const_binop (code, convert (ctype, op1), convert (ctype, c), 0);
4650 if (op1 == 0 || TREE_OVERFLOW (op1))
4656 /* If we have an unsigned type is not a sizetype, we cannot widen
4657 the operation since it will change the result if the original
4658 computation overflowed. */
4659 if (TREE_UNSIGNED (ctype)
4660 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4664 /* If we were able to eliminate our operation from the first side,
4665 apply our operation to the second side and reform the PLUS. */
4666 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4667 return fold (build (tcode, ctype, convert (ctype, t1), op1));
4669 /* The last case is if we are a multiply. In that case, we can
4670 apply the distributive law to commute the multiply and addition
4671 if the multiplication of the constants doesn't overflow. */
4672 if (code == MULT_EXPR)
4673 return fold (build (tcode, ctype, fold (build (code, ctype,
4674 convert (ctype, op0),
4675 convert (ctype, c))),
4681 /* We have a special case here if we are doing something like
4682 (C * 8) % 4 since we know that's zero. */
4683 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4684 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4685 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4686 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4687 return omit_one_operand (type, integer_zero_node, op0);
4689 /* ... fall through ... */
4691 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4692 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4693 /* If we can extract our operation from the LHS, do so and return a
4694 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4695 do something only if the second operand is a constant. */
4697 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4698 return fold (build (tcode, ctype, convert (ctype, t1),
4699 convert (ctype, op1)));
4700 else if (tcode == MULT_EXPR && code == MULT_EXPR
4701 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4702 return fold (build (tcode, ctype, convert (ctype, op0),
4703 convert (ctype, t1)));
4704 else if (TREE_CODE (op1) != INTEGER_CST)
4707 /* If these are the same operation types, we can associate them
4708 assuming no overflow. */
4710 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4711 convert (ctype, c), 0))
4712 && ! TREE_OVERFLOW (t1))
4713 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4715 /* If these operations "cancel" each other, we have the main
4716 optimizations of this pass, which occur when either constant is a
4717 multiple of the other, in which case we replace this with either an
4718 operation or CODE or TCODE.
4720 If we have an unsigned type that is not a sizetype, we cannot do
4721 this since it will change the result if the original computation
4723 if ((! TREE_UNSIGNED (ctype)
4724 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4725 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4726 || (tcode == MULT_EXPR
4727 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4728 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4730 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4731 return fold (build (tcode, ctype, convert (ctype, op0),
4733 const_binop (TRUNC_DIV_EXPR,
4735 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4736 return fold (build (code, ctype, convert (ctype, op0),
4738 const_binop (TRUNC_DIV_EXPR,
4750 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4751 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4752 that we may sometimes modify the tree. */
4755 strip_compound_expr (t, s)
4759 enum tree_code code = TREE_CODE (t);
4761 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4762 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4763 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4764 return TREE_OPERAND (t, 1);
4766 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4767 don't bother handling any other types. */
4768 else if (code == COND_EXPR)
4770 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4771 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4772 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4774 else if (TREE_CODE_CLASS (code) == '1')
4775 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4776 else if (TREE_CODE_CLASS (code) == '<'
4777 || TREE_CODE_CLASS (code) == '2')
4779 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4780 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4786 /* Return a node which has the indicated constant VALUE (either 0 or
4787 1), and is of the indicated TYPE. */
4790 constant_boolean_node (value, type)
4794 if (type == integer_type_node)
4795 return value ? integer_one_node : integer_zero_node;
4796 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4797 return truthvalue_conversion (value ? integer_one_node :
4801 tree t = build_int_2 (value, 0);
4803 TREE_TYPE (t) = type;
4808 /* Utility function for the following routine, to see how complex a nesting of
4809 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4810 we don't care (to avoid spending too much time on complex expressions.). */
4813 count_cond (expr, lim)
4819 if (TREE_CODE (expr) != COND_EXPR)
4824 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4825 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4826 return MIN (lim, 1 + ctrue + cfalse);
4829 /* Transform `a + (b ? x : y)' into `x ? (a + b) : (a + y)'.
4830 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4831 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4832 expression, and ARG to `a'. If COND_FIRST_P is non-zero, then the
4833 COND is the first argument to CODE; otherwise (as in the example
4834 given here), it is the second argument. TYPE is the type of the
4835 original expression. */
4838 fold_binary_op_with_conditional_arg (code, type, cond, arg, cond_first_p)
4839 enum tree_code code;
4845 tree test, true_value, false_value;
4846 tree lhs = NULL_TREE;
4847 tree rhs = NULL_TREE;
4848 /* In the end, we'll produce a COND_EXPR. Both arms of the
4849 conditional expression will be binary operations. The left-hand
4850 side of the expression to be executed if the condition is true
4851 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4852 of the expression to be executed if the condition is true will be
4853 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analogous --
4854 but apply to the expression to be executed if the conditional is
4860 /* These are the codes to use for the left-hand side and right-hand
4861 side of the COND_EXPR. Normally, they are the same as CODE. */
4862 enum tree_code lhs_code = code;
4863 enum tree_code rhs_code = code;
4864 /* And these are the types of the expressions. */
4865 tree lhs_type = type;
4866 tree rhs_type = type;
4870 true_rhs = false_rhs = &arg;
4871 true_lhs = &true_value;
4872 false_lhs = &false_value;
4876 true_lhs = false_lhs = &arg;
4877 true_rhs = &true_value;
4878 false_rhs = &false_value;
4881 if (TREE_CODE (cond) == COND_EXPR)
4883 test = TREE_OPERAND (cond, 0);
4884 true_value = TREE_OPERAND (cond, 1);
4885 false_value = TREE_OPERAND (cond, 2);
4886 /* If this operand throws an expression, then it does not make
4887 sense to try to perform a logical or arithmetic operation
4888 involving it. Instead of building `a + throw 3' for example,
4889 we simply build `a, throw 3'. */
4890 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4892 lhs_code = COMPOUND_EXPR;
4894 lhs_type = void_type_node;
4896 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4898 rhs_code = COMPOUND_EXPR;
4900 rhs_type = void_type_node;
4905 tree testtype = TREE_TYPE (cond);
4907 true_value = convert (testtype, integer_one_node);
4908 false_value = convert (testtype, integer_zero_node);
4911 /* If ARG is complex we want to make sure we only evaluate
4912 it once. Though this is only required if it is volatile, it
4913 might be more efficient even if it is not. However, if we
4914 succeed in folding one part to a constant, we do not need
4915 to make this SAVE_EXPR. Since we do this optimization
4916 primarily to see if we do end up with constant and this
4917 SAVE_EXPR interferes with later optimizations, suppressing
4918 it when we can is important.
4920 If we are not in a function, we can't make a SAVE_EXPR, so don't
4921 try to do so. Don't try to see if the result is a constant
4922 if an arm is a COND_EXPR since we get exponential behavior
4925 if (TREE_CODE (arg) != SAVE_EXPR && ! TREE_CONSTANT (arg)
4926 && global_bindings_p () == 0
4927 && ((TREE_CODE (arg) != VAR_DECL
4928 && TREE_CODE (arg) != PARM_DECL)
4929 || TREE_SIDE_EFFECTS (arg)))
4931 if (TREE_CODE (true_value) != COND_EXPR)
4932 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4934 if (TREE_CODE (false_value) != COND_EXPR)
4935 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4937 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4938 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4939 arg = save_expr (arg), lhs = rhs = 0;
4943 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4945 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4947 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4949 if (TREE_CODE (arg) == SAVE_EXPR)
4950 return build (COMPOUND_EXPR, type,
4951 convert (void_type_node, arg),
4952 strip_compound_expr (test, arg));
4954 return convert (type, test);
4958 /* Perform constant folding and related simplification of EXPR.
4959 The related simplifications include x*1 => x, x*0 => 0, etc.,
4960 and application of the associative law.
4961 NOP_EXPR conversions may be removed freely (as long as we
4962 are careful not to change the C type of the overall expression)
4963 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
4964 but we can constant-fold them if they have constant operands. */
4971 tree t1 = NULL_TREE;
4973 tree type = TREE_TYPE (expr);
4974 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4975 enum tree_code code = TREE_CODE (t);
4976 int kind = TREE_CODE_CLASS (code);
4978 /* WINS will be nonzero when the switch is done
4979 if all operands are constant. */
4982 /* Don't try to process an RTL_EXPR since its operands aren't trees.
4983 Likewise for a SAVE_EXPR that's already been evaluated. */
4984 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
4987 /* Return right away if a constant. */
4991 #ifdef MAX_INTEGER_COMPUTATION_MODE
4992 check_max_integer_computation_mode (expr);
4995 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
4999 /* Special case for conversion ops that can have fixed point args. */
5000 arg0 = TREE_OPERAND (t, 0);
5002 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
5004 STRIP_SIGN_NOPS (arg0);
5006 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
5007 subop = TREE_REALPART (arg0);
5011 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
5012 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5013 && TREE_CODE (subop) != REAL_CST
5014 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5016 /* Note that TREE_CONSTANT isn't enough:
5017 static var addresses are constant but we can't
5018 do arithmetic on them. */
5021 else if (IS_EXPR_CODE_CLASS (kind) || kind == 'r')
5023 int len = first_rtl_op (code);
5025 for (i = 0; i < len; i++)
5027 tree op = TREE_OPERAND (t, i);
5031 continue; /* Valid for CALL_EXPR, at least. */
5033 if (kind == '<' || code == RSHIFT_EXPR)
5035 /* Signedness matters here. Perhaps we can refine this
5037 STRIP_SIGN_NOPS (op);
5040 /* Strip any conversions that don't change the mode. */
5043 if (TREE_CODE (op) == COMPLEX_CST)
5044 subop = TREE_REALPART (op);
5048 if (TREE_CODE (subop) != INTEGER_CST
5049 #if ! defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5050 && TREE_CODE (subop) != REAL_CST
5051 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
5053 /* Note that TREE_CONSTANT isn't enough:
5054 static var addresses are constant but we can't
5055 do arithmetic on them. */
5065 /* If this is a commutative operation, and ARG0 is a constant, move it
5066 to ARG1 to reduce the number of tests below. */
5067 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
5068 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
5069 || code == BIT_AND_EXPR)
5070 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
5072 tem = arg0; arg0 = arg1; arg1 = tem;
5074 tem = TREE_OPERAND (t, 0); TREE_OPERAND (t, 0) = TREE_OPERAND (t, 1);
5075 TREE_OPERAND (t, 1) = tem;
5078 /* Now WINS is set as described above,
5079 ARG0 is the first operand of EXPR,
5080 and ARG1 is the second operand (if it has more than one operand).
5082 First check for cases where an arithmetic operation is applied to a
5083 compound, conditional, or comparison operation. Push the arithmetic
5084 operation inside the compound or conditional to see if any folding
5085 can then be done. Convert comparison to conditional for this purpose.
5086 The also optimizes non-constant cases that used to be done in
5089 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
5090 one of the operands is a comparison and the other is a comparison, a
5091 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
5092 code below would make the expression more complex. Change it to a
5093 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
5094 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
5096 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5097 || code == EQ_EXPR || code == NE_EXPR)
5098 && ((truth_value_p (TREE_CODE (arg0))
5099 && (truth_value_p (TREE_CODE (arg1))
5100 || (TREE_CODE (arg1) == BIT_AND_EXPR
5101 && integer_onep (TREE_OPERAND (arg1, 1)))))
5102 || (truth_value_p (TREE_CODE (arg1))
5103 && (truth_value_p (TREE_CODE (arg0))
5104 || (TREE_CODE (arg0) == BIT_AND_EXPR
5105 && integer_onep (TREE_OPERAND (arg0, 1)))))))
5107 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5108 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5112 if (code == EQ_EXPR)
5113 t = invert_truthvalue (t);
5118 if (TREE_CODE_CLASS (code) == '1')
5120 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5121 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5122 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5123 else if (TREE_CODE (arg0) == COND_EXPR)
5125 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5126 fold (build1 (code, type, TREE_OPERAND (arg0, 1))),
5127 fold (build1 (code, type, TREE_OPERAND (arg0, 2)))));
5129 /* If this was a conversion, and all we did was to move into
5130 inside the COND_EXPR, bring it back out. But leave it if
5131 it is a conversion from integer to integer and the
5132 result precision is no wider than a word since such a
5133 conversion is cheap and may be optimized away by combine,
5134 while it couldn't if it were outside the COND_EXPR. Then return
5135 so we don't get into an infinite recursion loop taking the
5136 conversion out and then back in. */
5138 if ((code == NOP_EXPR || code == CONVERT_EXPR
5139 || code == NON_LVALUE_EXPR)
5140 && TREE_CODE (t) == COND_EXPR
5141 && TREE_CODE (TREE_OPERAND (t, 1)) == code
5142 && TREE_CODE (TREE_OPERAND (t, 2)) == code
5143 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
5144 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
5145 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
5147 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
5148 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
5149 t = build1 (code, type,
5151 TREE_TYPE (TREE_OPERAND
5152 (TREE_OPERAND (t, 1), 0)),
5153 TREE_OPERAND (t, 0),
5154 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
5155 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
5158 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5159 return fold (build (COND_EXPR, type, arg0,
5160 fold (build1 (code, type, integer_one_node)),
5161 fold (build1 (code, type, integer_zero_node))));
5163 else if (TREE_CODE_CLASS (code) == '2'
5164 || TREE_CODE_CLASS (code) == '<')
5166 if (TREE_CODE (arg1) == COMPOUND_EXPR)
5167 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5168 fold (build (code, type,
5169 arg0, TREE_OPERAND (arg1, 1))));
5170 else if ((TREE_CODE (arg1) == COND_EXPR
5171 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
5172 && TREE_CODE_CLASS (code) != '<'))
5173 && (TREE_CODE (arg0) != COND_EXPR
5174 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5175 && (! TREE_SIDE_EFFECTS (arg0)
5176 || (global_bindings_p () == 0
5177 && ! contains_placeholder_p (arg0))))
5179 fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5180 /*cond_first_p=*/0);
5181 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
5182 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5183 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5184 else if ((TREE_CODE (arg0) == COND_EXPR
5185 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5186 && TREE_CODE_CLASS (code) != '<'))
5187 && (TREE_CODE (arg1) != COND_EXPR
5188 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5189 && (! TREE_SIDE_EFFECTS (arg1)
5190 || (global_bindings_p () == 0
5191 && ! contains_placeholder_p (arg1))))
5193 fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5194 /*cond_first_p=*/1);
5196 else if (TREE_CODE_CLASS (code) == '<'
5197 && TREE_CODE (arg0) == COMPOUND_EXPR)
5198 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5199 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5200 else if (TREE_CODE_CLASS (code) == '<'
5201 && TREE_CODE (arg1) == COMPOUND_EXPR)
5202 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5203 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5216 return fold (DECL_INITIAL (t));
5221 case FIX_TRUNC_EXPR:
5222 /* Other kinds of FIX are not handled properly by fold_convert. */
5224 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5225 return TREE_OPERAND (t, 0);
5227 /* Handle cases of two conversions in a row. */
5228 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5229 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5231 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5232 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5233 tree final_type = TREE_TYPE (t);
5234 int inside_int = INTEGRAL_TYPE_P (inside_type);
5235 int inside_ptr = POINTER_TYPE_P (inside_type);
5236 int inside_float = FLOAT_TYPE_P (inside_type);
5237 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5238 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5239 int inter_int = INTEGRAL_TYPE_P (inter_type);
5240 int inter_ptr = POINTER_TYPE_P (inter_type);
5241 int inter_float = FLOAT_TYPE_P (inter_type);
5242 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5243 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5244 int final_int = INTEGRAL_TYPE_P (final_type);
5245 int final_ptr = POINTER_TYPE_P (final_type);
5246 int final_float = FLOAT_TYPE_P (final_type);
5247 unsigned int final_prec = TYPE_PRECISION (final_type);
5248 int final_unsignedp = TREE_UNSIGNED (final_type);
5250 /* In addition to the cases of two conversions in a row
5251 handled below, if we are converting something to its own
5252 type via an object of identical or wider precision, neither
5253 conversion is needed. */
5254 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (final_type)
5255 && ((inter_int && final_int) || (inter_float && final_float))
5256 && inter_prec >= final_prec)
5257 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5259 /* Likewise, if the intermediate and final types are either both
5260 float or both integer, we don't need the middle conversion if
5261 it is wider than the final type and doesn't change the signedness
5262 (for integers). Avoid this if the final type is a pointer
5263 since then we sometimes need the inner conversion. Likewise if
5264 the outer has a precision not equal to the size of its mode. */
5265 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5266 || (inter_float && inside_float))
5267 && inter_prec >= inside_prec
5268 && (inter_float || inter_unsignedp == inside_unsignedp)
5269 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5270 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5272 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5274 /* If we have a sign-extension of a zero-extended value, we can
5275 replace that by a single zero-extension. */
5276 if (inside_int && inter_int && final_int
5277 && inside_prec < inter_prec && inter_prec < final_prec
5278 && inside_unsignedp && !inter_unsignedp)
5279 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5281 /* Two conversions in a row are not needed unless:
5282 - some conversion is floating-point (overstrict for now), or
5283 - the intermediate type is narrower than both initial and
5285 - the intermediate type and innermost type differ in signedness,
5286 and the outermost type is wider than the intermediate, or
5287 - the initial type is a pointer type and the precisions of the
5288 intermediate and final types differ, or
5289 - the final type is a pointer type and the precisions of the
5290 initial and intermediate types differ. */
5291 if (! inside_float && ! inter_float && ! final_float
5292 && (inter_prec > inside_prec || inter_prec > final_prec)
5293 && ! (inside_int && inter_int
5294 && inter_unsignedp != inside_unsignedp
5295 && inter_prec < final_prec)
5296 && ((inter_unsignedp && inter_prec > inside_prec)
5297 == (final_unsignedp && final_prec > inter_prec))
5298 && ! (inside_ptr && inter_prec != final_prec)
5299 && ! (final_ptr && inside_prec != inter_prec)
5300 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5301 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5303 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5306 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5307 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5308 /* Detect assigning a bitfield. */
5309 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5310 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5312 /* Don't leave an assignment inside a conversion
5313 unless assigning a bitfield. */
5314 tree prev = TREE_OPERAND (t, 0);
5315 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5316 /* First do the assignment, then return converted constant. */
5317 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5323 TREE_CONSTANT (t) = TREE_CONSTANT (arg0);
5326 return fold_convert (t, arg0);
5328 case VIEW_CONVERT_EXPR:
5329 if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
5330 return build1 (VIEW_CONVERT_EXPR, type,
5331 TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5334 #if 0 /* This loses on &"foo"[0]. */
5339 /* Fold an expression like: "foo"[2] */
5340 if (TREE_CODE (arg0) == STRING_CST
5341 && TREE_CODE (arg1) == INTEGER_CST
5342 && compare_tree_int (arg1, TREE_STRING_LENGTH (arg0)) < 0)
5344 t = build_int_2 (TREE_STRING_POINTER (arg0)[TREE_INT_CST_LOW (arg))], 0);
5345 TREE_TYPE (t) = TREE_TYPE (TREE_TYPE (arg0));
5346 force_fit_type (t, 0);
5353 if (TREE_CODE (arg0) == CONSTRUCTOR)
5355 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5362 TREE_CONSTANT (t) = wins;
5368 if (TREE_CODE (arg0) == INTEGER_CST)
5370 unsigned HOST_WIDE_INT low;
5372 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5373 TREE_INT_CST_HIGH (arg0),
5375 t = build_int_2 (low, high);
5376 TREE_TYPE (t) = type;
5378 = (TREE_OVERFLOW (arg0)
5379 | force_fit_type (t, overflow && !TREE_UNSIGNED (type)));
5380 TREE_CONSTANT_OVERFLOW (t)
5381 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5383 else if (TREE_CODE (arg0) == REAL_CST)
5384 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5386 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5387 return TREE_OPERAND (arg0, 0);
5389 /* Convert - (a - b) to (b - a) for non-floating-point. */
5390 else if (TREE_CODE (arg0) == MINUS_EXPR
5391 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
5392 return build (MINUS_EXPR, type, TREE_OPERAND (arg0, 1),
5393 TREE_OPERAND (arg0, 0));
5400 if (TREE_CODE (arg0) == INTEGER_CST)
5402 /* If the value is unsigned, then the absolute value is
5403 the same as the ordinary value. */
5404 if (TREE_UNSIGNED (type))
5406 /* Similarly, if the value is non-negative. */
5407 else if (INT_CST_LT (integer_minus_one_node, arg0))
5409 /* If the value is negative, then the absolute value is
5413 unsigned HOST_WIDE_INT low;
5415 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5416 TREE_INT_CST_HIGH (arg0),
5418 t = build_int_2 (low, high);
5419 TREE_TYPE (t) = type;
5421 = (TREE_OVERFLOW (arg0)
5422 | force_fit_type (t, overflow));
5423 TREE_CONSTANT_OVERFLOW (t)
5424 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5427 else if (TREE_CODE (arg0) == REAL_CST)
5429 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
5430 t = build_real (type,
5431 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5434 else if (TREE_CODE (arg0) == ABS_EXPR || TREE_CODE (arg0) == NEGATE_EXPR)
5435 return build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
5439 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5440 return convert (type, arg0);
5441 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5442 return build (COMPLEX_EXPR, type,
5443 TREE_OPERAND (arg0, 0),
5444 negate_expr (TREE_OPERAND (arg0, 1)));
5445 else if (TREE_CODE (arg0) == COMPLEX_CST)
5446 return build_complex (type, TREE_REALPART (arg0),
5447 negate_expr (TREE_IMAGPART (arg0)));
5448 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5449 return fold (build (TREE_CODE (arg0), type,
5450 fold (build1 (CONJ_EXPR, type,
5451 TREE_OPERAND (arg0, 0))),
5452 fold (build1 (CONJ_EXPR,
5453 type, TREE_OPERAND (arg0, 1)))));
5454 else if (TREE_CODE (arg0) == CONJ_EXPR)
5455 return TREE_OPERAND (arg0, 0);
5461 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5462 ~ TREE_INT_CST_HIGH (arg0));
5463 TREE_TYPE (t) = type;
5464 force_fit_type (t, 0);
5465 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
5466 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
5468 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5469 return TREE_OPERAND (arg0, 0);
5473 /* A + (-B) -> A - B */
5474 if (TREE_CODE (arg1) == NEGATE_EXPR)
5475 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5476 /* (-A) + B -> B - A */
5477 if (TREE_CODE (arg0) == NEGATE_EXPR)
5478 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
5479 else if (! FLOAT_TYPE_P (type))
5481 if (integer_zerop (arg1))
5482 return non_lvalue (convert (type, arg0));
5484 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5485 with a constant, and the two constants have no bits in common,
5486 we should treat this as a BIT_IOR_EXPR since this may produce more
5488 if (TREE_CODE (arg0) == BIT_AND_EXPR
5489 && TREE_CODE (arg1) == BIT_AND_EXPR
5490 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5491 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5492 && integer_zerop (const_binop (BIT_AND_EXPR,
5493 TREE_OPERAND (arg0, 1),
5494 TREE_OPERAND (arg1, 1), 0)))
5496 code = BIT_IOR_EXPR;
5500 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5501 (plus (plus (mult) (mult)) (foo)) so that we can
5502 take advantage of the factoring cases below. */
5503 if ((TREE_CODE (arg0) == PLUS_EXPR
5504 && TREE_CODE (arg1) == MULT_EXPR)
5505 || (TREE_CODE (arg1) == PLUS_EXPR
5506 && TREE_CODE (arg0) == MULT_EXPR))
5508 tree parg0, parg1, parg, marg;
5510 if (TREE_CODE (arg0) == PLUS_EXPR)
5511 parg = arg0, marg = arg1;
5513 parg = arg1, marg = arg0;
5514 parg0 = TREE_OPERAND (parg, 0);
5515 parg1 = TREE_OPERAND (parg, 1);
5519 if (TREE_CODE (parg0) == MULT_EXPR
5520 && TREE_CODE (parg1) != MULT_EXPR)
5521 return fold (build (PLUS_EXPR, type,
5522 fold (build (PLUS_EXPR, type, parg0, marg)),
5524 if (TREE_CODE (parg0) != MULT_EXPR
5525 && TREE_CODE (parg1) == MULT_EXPR)
5526 return fold (build (PLUS_EXPR, type,
5527 fold (build (PLUS_EXPR, type, parg1, marg)),
5531 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5533 tree arg00, arg01, arg10, arg11;
5534 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
5536 /* (A * C) + (B * C) -> (A+B) * C.
5537 We are most concerned about the case where C is a constant,
5538 but other combinations show up during loop reduction. Since
5539 it is not difficult, try all four possibilities. */
5541 arg00 = TREE_OPERAND (arg0, 0);
5542 arg01 = TREE_OPERAND (arg0, 1);
5543 arg10 = TREE_OPERAND (arg1, 0);
5544 arg11 = TREE_OPERAND (arg1, 1);
5547 if (operand_equal_p (arg01, arg11, 0))
5548 same = arg01, alt0 = arg00, alt1 = arg10;
5549 else if (operand_equal_p (arg00, arg10, 0))
5550 same = arg00, alt0 = arg01, alt1 = arg11;
5551 else if (operand_equal_p (arg00, arg11, 0))
5552 same = arg00, alt0 = arg01, alt1 = arg10;
5553 else if (operand_equal_p (arg01, arg10, 0))
5554 same = arg01, alt0 = arg00, alt1 = arg11;
5556 /* No identical multiplicands; see if we can find a common
5557 power-of-two factor in non-power-of-two multiplies. This
5558 can help in multi-dimensional array access. */
5559 else if (TREE_CODE (arg01) == INTEGER_CST
5560 && TREE_CODE (arg11) == INTEGER_CST
5561 && TREE_INT_CST_HIGH (arg01) == 0
5562 && TREE_INT_CST_HIGH (arg11) == 0)
5564 HOST_WIDE_INT int01, int11, tmp;
5565 int01 = TREE_INT_CST_LOW (arg01);
5566 int11 = TREE_INT_CST_LOW (arg11);
5568 /* Move min of absolute values to int11. */
5569 if ((int01 >= 0 ? int01 : -int01)
5570 < (int11 >= 0 ? int11 : -int11))
5572 tmp = int01, int01 = int11, int11 = tmp;
5573 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5574 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5577 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5579 alt0 = fold (build (MULT_EXPR, type, arg00,
5580 build_int_2 (int01 / int11, 0)));
5587 return fold (build (MULT_EXPR, type,
5588 fold (build (PLUS_EXPR, type, alt0, alt1)),
5592 /* In IEEE floating point, x+0 may not equal x. */
5593 else if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5594 || flag_unsafe_math_optimizations)
5595 && real_zerop (arg1))
5596 return non_lvalue (convert (type, arg0));
5597 /* x+(-0) equals x, even for IEEE. */
5598 else if (TREE_CODE (arg1) == REAL_CST
5599 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
5600 return non_lvalue (convert (type, arg0));
5603 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5604 is a rotate of A by C1 bits. */
5605 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5606 is a rotate of A by B bits. */
5608 enum tree_code code0, code1;
5609 code0 = TREE_CODE (arg0);
5610 code1 = TREE_CODE (arg1);
5611 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5612 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
5613 && operand_equal_p (TREE_OPERAND (arg0, 0),
5614 TREE_OPERAND (arg1, 0), 0)
5615 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5617 tree tree01, tree11;
5618 enum tree_code code01, code11;
5620 tree01 = TREE_OPERAND (arg0, 1);
5621 tree11 = TREE_OPERAND (arg1, 1);
5622 STRIP_NOPS (tree01);
5623 STRIP_NOPS (tree11);
5624 code01 = TREE_CODE (tree01);
5625 code11 = TREE_CODE (tree11);
5626 if (code01 == INTEGER_CST
5627 && code11 == INTEGER_CST
5628 && TREE_INT_CST_HIGH (tree01) == 0
5629 && TREE_INT_CST_HIGH (tree11) == 0
5630 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5631 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
5632 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
5633 code0 == LSHIFT_EXPR ? tree01 : tree11);
5634 else if (code11 == MINUS_EXPR)
5636 tree tree110, tree111;
5637 tree110 = TREE_OPERAND (tree11, 0);
5638 tree111 = TREE_OPERAND (tree11, 1);
5639 STRIP_NOPS (tree110);
5640 STRIP_NOPS (tree111);
5641 if (TREE_CODE (tree110) == INTEGER_CST
5642 && 0 == compare_tree_int (tree110,
5644 (TREE_TYPE (TREE_OPERAND
5646 && operand_equal_p (tree01, tree111, 0))
5647 return build ((code0 == LSHIFT_EXPR
5650 type, TREE_OPERAND (arg0, 0), tree01);
5652 else if (code01 == MINUS_EXPR)
5654 tree tree010, tree011;
5655 tree010 = TREE_OPERAND (tree01, 0);
5656 tree011 = TREE_OPERAND (tree01, 1);
5657 STRIP_NOPS (tree010);
5658 STRIP_NOPS (tree011);
5659 if (TREE_CODE (tree010) == INTEGER_CST
5660 && 0 == compare_tree_int (tree010,
5662 (TREE_TYPE (TREE_OPERAND
5664 && operand_equal_p (tree11, tree011, 0))
5665 return build ((code0 != LSHIFT_EXPR
5668 type, TREE_OPERAND (arg0, 0), tree11);
5674 /* In most languages, can't associate operations on floats through
5675 parentheses. Rather than remember where the parentheses were, we
5676 don't associate floats at all. It shouldn't matter much. However,
5677 associating multiplications is only very slightly inaccurate, so do
5678 that if -funsafe-math-optimizations is specified. */
5681 && (! FLOAT_TYPE_P (type)
5682 || (flag_unsafe_math_optimizations && code == MULT_EXPR)))
5684 tree var0, con0, lit0, minus_lit0;
5685 tree var1, con1, lit1, minus_lit1;
5687 /* Split both trees into variables, constants, and literals. Then
5688 associate each group together, the constants with literals,
5689 then the result with variables. This increases the chances of
5690 literals being recombined later and of generating relocatable
5691 expressions for the sum of a constant and literal. */
5692 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
5693 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
5694 code == MINUS_EXPR);
5696 /* Only do something if we found more than two objects. Otherwise,
5697 nothing has changed and we risk infinite recursion. */
5698 if (2 < ((var0 != 0) + (var1 != 0)
5699 + (con0 != 0) + (con1 != 0)
5700 + (lit0 != 0) + (lit1 != 0)
5701 + (minus_lit0 != 0) + (minus_lit1 != 0)))
5703 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
5704 if (code == MINUS_EXPR)
5707 var0 = associate_trees (var0, var1, code, type);
5708 con0 = associate_trees (con0, con1, code, type);
5709 lit0 = associate_trees (lit0, lit1, code, type);
5710 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
5712 /* Preserve the MINUS_EXPR if the negative part of the literal is
5713 greater than the positive part. Otherwise, the multiplicative
5714 folding code (i.e extract_muldiv) may be fooled in case
5715 unsigned constants are substracted, like in the following
5716 example: ((X*2 + 4) - 8U)/2. */
5717 if (minus_lit0 && lit0)
5719 if (tree_int_cst_lt (lit0, minus_lit0))
5721 minus_lit0 = associate_trees (minus_lit0, lit0,
5727 lit0 = associate_trees (lit0, minus_lit0,
5735 return convert (type, associate_trees (var0, minus_lit0,
5739 con0 = associate_trees (con0, minus_lit0,
5741 return convert (type, associate_trees (var0, con0,
5746 con0 = associate_trees (con0, lit0, code, type);
5747 return convert (type, associate_trees (var0, con0, code, type));
5752 #if defined (REAL_IS_NOT_DOUBLE) && ! defined (REAL_ARITHMETIC)
5753 if (TREE_CODE (arg1) == REAL_CST)
5755 #endif /* REAL_IS_NOT_DOUBLE, and no REAL_ARITHMETIC */
5757 t1 = const_binop (code, arg0, arg1, 0);
5758 if (t1 != NULL_TREE)
5760 /* The return value should always have
5761 the same type as the original expression. */
5762 if (TREE_TYPE (t1) != TREE_TYPE (t))
5763 t1 = convert (TREE_TYPE (t), t1);
5770 /* A - (-B) -> A + B */
5771 if (TREE_CODE (arg1) == NEGATE_EXPR)
5772 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5773 /* (-A) - CST -> (-CST) - A for floating point (what about ints ?) */
5774 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
5776 fold (build (MINUS_EXPR, type,
5777 build_real (TREE_TYPE (arg1),
5778 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1))),
5779 TREE_OPERAND (arg0, 0)));
5781 if (! FLOAT_TYPE_P (type))
5783 if (! wins && integer_zerop (arg0))
5784 return negate_expr (convert (type, arg1));
5785 if (integer_zerop (arg1))
5786 return non_lvalue (convert (type, arg0));
5788 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
5789 about the case where C is a constant, just try one of the
5790 four possibilities. */
5792 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
5793 && operand_equal_p (TREE_OPERAND (arg0, 1),
5794 TREE_OPERAND (arg1, 1), 0))
5795 return fold (build (MULT_EXPR, type,
5796 fold (build (MINUS_EXPR, type,
5797 TREE_OPERAND (arg0, 0),
5798 TREE_OPERAND (arg1, 0))),
5799 TREE_OPERAND (arg0, 1)));
5802 else if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5803 || flag_unsafe_math_optimizations)
5805 /* Except with IEEE floating point, 0-x equals -x. */
5806 if (! wins && real_zerop (arg0))
5807 return negate_expr (convert (type, arg1));
5808 /* Except with IEEE floating point, x-0 equals x. */
5809 if (real_zerop (arg1))
5810 return non_lvalue (convert (type, arg0));
5813 /* Fold &x - &x. This can happen from &x.foo - &x.
5814 This is unsafe for certain floats even in non-IEEE formats.
5815 In IEEE, it is unsafe because it does wrong for NaNs.
5816 Also note that operand_equal_p is always false if an operand
5819 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
5820 && operand_equal_p (arg0, arg1, 0))
5821 return convert (type, integer_zero_node);
5826 /* (-A) * (-B) -> A * B */
5827 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
5828 return fold (build (MULT_EXPR, type, TREE_OPERAND (arg0, 0),
5829 TREE_OPERAND (arg1, 0)));
5831 if (! FLOAT_TYPE_P (type))
5833 if (integer_zerop (arg1))
5834 return omit_one_operand (type, arg1, arg0);
5835 if (integer_onep (arg1))
5836 return non_lvalue (convert (type, arg0));
5838 /* (a * (1 << b)) is (a << b) */
5839 if (TREE_CODE (arg1) == LSHIFT_EXPR
5840 && integer_onep (TREE_OPERAND (arg1, 0)))
5841 return fold (build (LSHIFT_EXPR, type, arg0,
5842 TREE_OPERAND (arg1, 1)));
5843 if (TREE_CODE (arg0) == LSHIFT_EXPR
5844 && integer_onep (TREE_OPERAND (arg0, 0)))
5845 return fold (build (LSHIFT_EXPR, type, arg1,
5846 TREE_OPERAND (arg0, 1)));
5848 if (TREE_CODE (arg1) == INTEGER_CST
5849 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
5851 return convert (type, tem);
5856 /* x*0 is 0, except for IEEE floating point. */
5857 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
5858 || flag_unsafe_math_optimizations)
5859 && real_zerop (arg1))
5860 return omit_one_operand (type, arg1, arg0);
5861 /* In IEEE floating point, x*1 is not equivalent to x for snans.
5862 However, ANSI says we can drop signals,
5863 so we can do this anyway. */
5864 if (real_onep (arg1))
5865 return non_lvalue (convert (type, arg0));
5867 if (! wins && real_twop (arg1) && global_bindings_p () == 0
5868 && ! contains_placeholder_p (arg0))
5870 tree arg = save_expr (arg0);
5871 return build (PLUS_EXPR, type, arg, arg);
5878 if (integer_all_onesp (arg1))
5879 return omit_one_operand (type, arg1, arg0);
5880 if (integer_zerop (arg1))
5881 return non_lvalue (convert (type, arg0));
5882 t1 = distribute_bit_expr (code, type, arg0, arg1);
5883 if (t1 != NULL_TREE)
5886 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
5888 This results in more efficient code for machines without a NAND
5889 instruction. Combine will canonicalize to the first form
5890 which will allow use of NAND instructions provided by the
5891 backend if they exist. */
5892 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5893 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5895 return fold (build1 (BIT_NOT_EXPR, type,
5896 build (BIT_AND_EXPR, type,
5897 TREE_OPERAND (arg0, 0),
5898 TREE_OPERAND (arg1, 0))));
5901 /* See if this can be simplified into a rotate first. If that
5902 is unsuccessful continue in the association code. */
5906 if (integer_zerop (arg1))
5907 return non_lvalue (convert (type, arg0));
5908 if (integer_all_onesp (arg1))
5909 return fold (build1 (BIT_NOT_EXPR, type, arg0));
5911 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
5912 with a constant, and the two constants have no bits in common,
5913 we should treat this as a BIT_IOR_EXPR since this may produce more
5915 if (TREE_CODE (arg0) == BIT_AND_EXPR
5916 && TREE_CODE (arg1) == BIT_AND_EXPR
5917 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5918 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5919 && integer_zerop (const_binop (BIT_AND_EXPR,
5920 TREE_OPERAND (arg0, 1),
5921 TREE_OPERAND (arg1, 1), 0)))
5923 code = BIT_IOR_EXPR;
5927 /* See if this can be simplified into a rotate first. If that
5928 is unsuccessful continue in the association code. */
5933 if (integer_all_onesp (arg1))
5934 return non_lvalue (convert (type, arg0));
5935 if (integer_zerop (arg1))
5936 return omit_one_operand (type, arg1, arg0);
5937 t1 = distribute_bit_expr (code, type, arg0, arg1);
5938 if (t1 != NULL_TREE)
5940 /* Simplify ((int)c & 0x377) into (int)c, if c is unsigned char. */
5941 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == NOP_EXPR
5942 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg1, 0))))
5945 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg1, 0)));
5947 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5948 && (~TREE_INT_CST_LOW (arg0)
5949 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5950 return build1 (NOP_EXPR, type, TREE_OPERAND (arg1, 0));
5952 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
5953 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5956 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
5958 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
5959 && (~TREE_INT_CST_LOW (arg1)
5960 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
5961 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
5964 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
5966 This results in more efficient code for machines without a NOR
5967 instruction. Combine will canonicalize to the first form
5968 which will allow use of NOR instructions provided by the
5969 backend if they exist. */
5970 if (TREE_CODE (arg0) == BIT_NOT_EXPR
5971 && TREE_CODE (arg1) == BIT_NOT_EXPR)
5973 return fold (build1 (BIT_NOT_EXPR, type,
5974 build (BIT_IOR_EXPR, type,
5975 TREE_OPERAND (arg0, 0),
5976 TREE_OPERAND (arg1, 0))));
5981 case BIT_ANDTC_EXPR:
5982 if (integer_all_onesp (arg0))
5983 return non_lvalue (convert (type, arg1));
5984 if (integer_zerop (arg0))
5985 return omit_one_operand (type, arg0, arg1);
5986 if (TREE_CODE (arg1) == INTEGER_CST)
5988 arg1 = fold (build1 (BIT_NOT_EXPR, type, arg1));
5989 code = BIT_AND_EXPR;
5995 /* In most cases, do nothing with a divide by zero. */
5996 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
5997 #ifndef REAL_INFINITY
5998 if (TREE_CODE (arg1) == REAL_CST && real_zerop (arg1))
6001 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
6003 /* (-A) / (-B) -> A / B */
6004 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == NEGATE_EXPR)
6005 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6006 TREE_OPERAND (arg1, 0)));
6008 /* In IEEE floating point, x/1 is not equivalent to x for snans.
6009 However, ANSI says we can drop signals, so we can do this anyway. */
6010 if (real_onep (arg1))
6011 return non_lvalue (convert (type, arg0));
6013 /* If ARG1 is a constant, we can convert this to a multiply by the
6014 reciprocal. This does not have the same rounding properties,
6015 so only do this if -funsafe-math-optimizations. We can actually
6016 always safely do it if ARG1 is a power of two, but it's hard to
6017 tell if it is or not in a portable manner. */
6018 if (TREE_CODE (arg1) == REAL_CST)
6020 if (flag_unsafe_math_optimizations
6021 && 0 != (tem = const_binop (code, build_real (type, dconst1),
6023 return fold (build (MULT_EXPR, type, arg0, tem));
6024 /* Find the reciprocal if optimizing and the result is exact. */
6028 r = TREE_REAL_CST (arg1);
6029 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
6031 tem = build_real (type, r);
6032 return fold (build (MULT_EXPR, type, arg0, tem));
6036 /* Convert A/B/C to A/(B*C). */
6037 if (flag_unsafe_math_optimizations
6038 && TREE_CODE (arg0) == RDIV_EXPR)
6040 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6041 build (MULT_EXPR, type, TREE_OPERAND (arg0, 1),
6044 /* Convert A/(B/C) to (A/B)*C. */
6045 if (flag_unsafe_math_optimizations
6046 && TREE_CODE (arg1) == RDIV_EXPR)
6048 return fold (build (MULT_EXPR, type,
6049 build (RDIV_EXPR, type, arg0,
6050 TREE_OPERAND (arg1, 0)),
6051 TREE_OPERAND (arg1, 1)));
6055 case TRUNC_DIV_EXPR:
6056 case ROUND_DIV_EXPR:
6057 case FLOOR_DIV_EXPR:
6059 case EXACT_DIV_EXPR:
6060 if (integer_onep (arg1))
6061 return non_lvalue (convert (type, arg0));
6062 if (integer_zerop (arg1))
6065 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
6066 operation, EXACT_DIV_EXPR.
6068 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
6069 At one time others generated faster code, it's not clear if they do
6070 after the last round to changes to the DIV code in expmed.c. */
6071 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
6072 && multiple_of_p (type, arg0, arg1))
6073 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
6075 if (TREE_CODE (arg1) == INTEGER_CST
6076 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
6078 return convert (type, tem);
6083 case FLOOR_MOD_EXPR:
6084 case ROUND_MOD_EXPR:
6085 case TRUNC_MOD_EXPR:
6086 if (integer_onep (arg1))
6087 return omit_one_operand (type, integer_zero_node, arg0);
6088 if (integer_zerop (arg1))
6091 if (TREE_CODE (arg1) == INTEGER_CST
6092 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
6094 return convert (type, tem);
6102 if (integer_zerop (arg1))
6103 return non_lvalue (convert (type, arg0));
6104 /* Since negative shift count is not well-defined,
6105 don't try to compute it in the compiler. */
6106 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
6108 /* Rewrite an LROTATE_EXPR by a constant into an
6109 RROTATE_EXPR by a new constant. */
6110 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
6112 TREE_SET_CODE (t, RROTATE_EXPR);
6113 code = RROTATE_EXPR;
6114 TREE_OPERAND (t, 1) = arg1
6117 convert (TREE_TYPE (arg1),
6118 build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0)),
6120 if (tree_int_cst_sgn (arg1) < 0)
6124 /* If we have a rotate of a bit operation with the rotate count and
6125 the second operand of the bit operation both constant,
6126 permute the two operations. */
6127 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6128 && (TREE_CODE (arg0) == BIT_AND_EXPR
6129 || TREE_CODE (arg0) == BIT_ANDTC_EXPR
6130 || TREE_CODE (arg0) == BIT_IOR_EXPR
6131 || TREE_CODE (arg0) == BIT_XOR_EXPR)
6132 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6133 return fold (build (TREE_CODE (arg0), type,
6134 fold (build (code, type,
6135 TREE_OPERAND (arg0, 0), arg1)),
6136 fold (build (code, type,
6137 TREE_OPERAND (arg0, 1), arg1))));
6139 /* Two consecutive rotates adding up to the width of the mode can
6141 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6142 && TREE_CODE (arg0) == RROTATE_EXPR
6143 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6144 && TREE_INT_CST_HIGH (arg1) == 0
6145 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
6146 && ((TREE_INT_CST_LOW (arg1)
6147 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
6148 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
6149 return TREE_OPERAND (arg0, 0);
6154 if (operand_equal_p (arg0, arg1, 0))
6155 return omit_one_operand (type, arg0, arg1);
6156 if (INTEGRAL_TYPE_P (type)
6157 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
6158 return omit_one_operand (type, arg1, arg0);
6162 if (operand_equal_p (arg0, arg1, 0))
6163 return omit_one_operand (type, arg0, arg1);
6164 if (INTEGRAL_TYPE_P (type)
6165 && TYPE_MAX_VALUE (type)
6166 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
6167 return omit_one_operand (type, arg1, arg0);
6170 case TRUTH_NOT_EXPR:
6171 /* Note that the operand of this must be an int
6172 and its values must be 0 or 1.
6173 ("true" is a fixed value perhaps depending on the language,
6174 but we don't handle values other than 1 correctly yet.) */
6175 tem = invert_truthvalue (arg0);
6176 /* Avoid infinite recursion. */
6177 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
6179 return convert (type, tem);
6181 case TRUTH_ANDIF_EXPR:
6182 /* Note that the operands of this must be ints
6183 and their values must be 0 or 1.
6184 ("true" is a fixed value perhaps depending on the language.) */
6185 /* If first arg is constant zero, return it. */
6186 if (integer_zerop (arg0))
6187 return convert (type, arg0);
6188 case TRUTH_AND_EXPR:
6189 /* If either arg is constant true, drop it. */
6190 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6191 return non_lvalue (convert (type, arg1));
6192 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
6193 /* Preserve sequence points. */
6194 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6195 return non_lvalue (convert (type, arg0));
6196 /* If second arg is constant zero, result is zero, but first arg
6197 must be evaluated. */
6198 if (integer_zerop (arg1))
6199 return omit_one_operand (type, arg1, arg0);
6200 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6201 case will be handled here. */
6202 if (integer_zerop (arg0))
6203 return omit_one_operand (type, arg0, arg1);
6206 /* We only do these simplifications if we are optimizing. */
6210 /* Check for things like (A || B) && (A || C). We can convert this
6211 to A || (B && C). Note that either operator can be any of the four
6212 truth and/or operations and the transformation will still be
6213 valid. Also note that we only care about order for the
6214 ANDIF and ORIF operators. If B contains side effects, this
6215 might change the truth-value of A. */
6216 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6217 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
6218 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
6219 || TREE_CODE (arg0) == TRUTH_AND_EXPR
6220 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
6221 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
6223 tree a00 = TREE_OPERAND (arg0, 0);
6224 tree a01 = TREE_OPERAND (arg0, 1);
6225 tree a10 = TREE_OPERAND (arg1, 0);
6226 tree a11 = TREE_OPERAND (arg1, 1);
6227 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
6228 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
6229 && (code == TRUTH_AND_EXPR
6230 || code == TRUTH_OR_EXPR));
6232 if (operand_equal_p (a00, a10, 0))
6233 return fold (build (TREE_CODE (arg0), type, a00,
6234 fold (build (code, type, a01, a11))));
6235 else if (commutative && operand_equal_p (a00, a11, 0))
6236 return fold (build (TREE_CODE (arg0), type, a00,
6237 fold (build (code, type, a01, a10))));
6238 else if (commutative && operand_equal_p (a01, a10, 0))
6239 return fold (build (TREE_CODE (arg0), type, a01,
6240 fold (build (code, type, a00, a11))));
6242 /* This case if tricky because we must either have commutative
6243 operators or else A10 must not have side-effects. */
6245 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
6246 && operand_equal_p (a01, a11, 0))
6247 return fold (build (TREE_CODE (arg0), type,
6248 fold (build (code, type, a00, a10)),
6252 /* See if we can build a range comparison. */
6253 if (0 != (tem = fold_range_test (t)))
6256 /* Check for the possibility of merging component references. If our
6257 lhs is another similar operation, try to merge its rhs with our
6258 rhs. Then try to merge our lhs and rhs. */
6259 if (TREE_CODE (arg0) == code
6260 && 0 != (tem = fold_truthop (code, type,
6261 TREE_OPERAND (arg0, 1), arg1)))
6262 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6264 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
6269 case TRUTH_ORIF_EXPR:
6270 /* Note that the operands of this must be ints
6271 and their values must be 0 or true.
6272 ("true" is a fixed value perhaps depending on the language.) */
6273 /* If first arg is constant true, return it. */
6274 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6275 return convert (type, arg0);
6277 /* If either arg is constant zero, drop it. */
6278 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
6279 return non_lvalue (convert (type, arg1));
6280 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
6281 /* Preserve sequence points. */
6282 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6283 return non_lvalue (convert (type, arg0));
6284 /* If second arg is constant true, result is true, but we must
6285 evaluate first arg. */
6286 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6287 return omit_one_operand (type, arg1, arg0);
6288 /* Likewise for first arg, but note this only occurs here for
6290 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6291 return omit_one_operand (type, arg0, arg1);
6294 case TRUTH_XOR_EXPR:
6295 /* If either arg is constant zero, drop it. */
6296 if (integer_zerop (arg0))
6297 return non_lvalue (convert (type, arg1));
6298 if (integer_zerop (arg1))
6299 return non_lvalue (convert (type, arg0));
6300 /* If either arg is constant true, this is a logical inversion. */
6301 if (integer_onep (arg0))
6302 return non_lvalue (convert (type, invert_truthvalue (arg1)));
6303 if (integer_onep (arg1))
6304 return non_lvalue (convert (type, invert_truthvalue (arg0)));
6313 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6315 /* (-a) CMP (-b) -> b CMP a */
6316 if (TREE_CODE (arg0) == NEGATE_EXPR
6317 && TREE_CODE (arg1) == NEGATE_EXPR)
6318 return fold (build (code, type, TREE_OPERAND (arg1, 0),
6319 TREE_OPERAND (arg0, 0)));
6320 /* (-a) CMP CST -> a swap(CMP) (-CST) */
6321 if (TREE_CODE (arg0) == NEGATE_EXPR && TREE_CODE (arg1) == REAL_CST)
6324 (swap_tree_comparison (code), type,
6325 TREE_OPERAND (arg0, 0),
6326 build_real (TREE_TYPE (arg1),
6327 REAL_VALUE_NEGATE (TREE_REAL_CST (arg1)))));
6328 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
6329 /* a CMP (-0) -> a CMP 0 */
6330 if (TREE_CODE (arg1) == REAL_CST
6331 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1)))
6332 return fold (build (code, type, arg0,
6333 build_real (TREE_TYPE (arg1), dconst0)));
6336 /* If one arg is a constant integer, put it last. */
6337 if (TREE_CODE (arg0) == INTEGER_CST
6338 && TREE_CODE (arg1) != INTEGER_CST)
6340 TREE_OPERAND (t, 0) = arg1;
6341 TREE_OPERAND (t, 1) = arg0;
6342 arg0 = TREE_OPERAND (t, 0);
6343 arg1 = TREE_OPERAND (t, 1);
6344 code = swap_tree_comparison (code);
6345 TREE_SET_CODE (t, code);
6348 /* Convert foo++ == CONST into ++foo == CONST + INCR.
6349 First, see if one arg is constant; find the constant arg
6350 and the other one. */
6352 tree constop = 0, varop = NULL_TREE;
6353 int constopnum = -1;
6355 if (TREE_CONSTANT (arg1))
6356 constopnum = 1, constop = arg1, varop = arg0;
6357 if (TREE_CONSTANT (arg0))
6358 constopnum = 0, constop = arg0, varop = arg1;
6360 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
6362 /* This optimization is invalid for ordered comparisons
6363 if CONST+INCR overflows or if foo+incr might overflow.
6364 This optimization is invalid for floating point due to rounding.
6365 For pointer types we assume overflow doesn't happen. */
6366 if (POINTER_TYPE_P (TREE_TYPE (varop))
6367 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6368 && (code == EQ_EXPR || code == NE_EXPR)))
6371 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
6372 constop, TREE_OPERAND (varop, 1)));
6374 /* Do not overwrite the current varop to be a preincrement,
6375 create a new node so that we won't confuse our caller who
6376 might create trees and throw them away, reusing the
6377 arguments that they passed to build. This shows up in
6378 the THEN or ELSE parts of ?: being postincrements. */
6379 varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop),
6380 TREE_OPERAND (varop, 0),
6381 TREE_OPERAND (varop, 1));
6383 /* If VAROP is a reference to a bitfield, we must mask
6384 the constant by the width of the field. */
6385 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6386 && DECL_BIT_FIELD(TREE_OPERAND
6387 (TREE_OPERAND (varop, 0), 1)))
6390 = TREE_INT_CST_LOW (DECL_SIZE
6392 (TREE_OPERAND (varop, 0), 1)));
6393 tree mask, unsigned_type;
6394 unsigned int precision;
6395 tree folded_compare;
6397 /* First check whether the comparison would come out
6398 always the same. If we don't do that we would
6399 change the meaning with the masking. */
6400 if (constopnum == 0)
6401 folded_compare = fold (build (code, type, constop,
6402 TREE_OPERAND (varop, 0)));
6404 folded_compare = fold (build (code, type,
6405 TREE_OPERAND (varop, 0),
6407 if (integer_zerop (folded_compare)
6408 || integer_onep (folded_compare))
6409 return omit_one_operand (type, folded_compare, varop);
6411 unsigned_type = type_for_size (size, 1);
6412 precision = TYPE_PRECISION (unsigned_type);
6413 mask = build_int_2 (~0, ~0);
6414 TREE_TYPE (mask) = unsigned_type;
6415 force_fit_type (mask, 0);
6416 mask = const_binop (RSHIFT_EXPR, mask,
6417 size_int (precision - size), 0);
6418 newconst = fold (build (BIT_AND_EXPR,
6419 TREE_TYPE (varop), newconst,
6420 convert (TREE_TYPE (varop),
6424 t = build (code, type,
6425 (constopnum == 0) ? newconst : varop,
6426 (constopnum == 1) ? newconst : varop);
6430 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
6432 if (POINTER_TYPE_P (TREE_TYPE (varop))
6433 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
6434 && (code == EQ_EXPR || code == NE_EXPR)))
6437 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
6438 constop, TREE_OPERAND (varop, 1)));
6440 /* Do not overwrite the current varop to be a predecrement,
6441 create a new node so that we won't confuse our caller who
6442 might create trees and throw them away, reusing the
6443 arguments that they passed to build. This shows up in
6444 the THEN or ELSE parts of ?: being postdecrements. */
6445 varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop),
6446 TREE_OPERAND (varop, 0),
6447 TREE_OPERAND (varop, 1));
6449 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
6450 && DECL_BIT_FIELD(TREE_OPERAND
6451 (TREE_OPERAND (varop, 0), 1)))
6454 = TREE_INT_CST_LOW (DECL_SIZE
6456 (TREE_OPERAND (varop, 0), 1)));
6457 tree mask, unsigned_type;
6458 unsigned int precision;
6459 tree folded_compare;
6461 if (constopnum == 0)
6462 folded_compare = fold (build (code, type, constop,
6463 TREE_OPERAND (varop, 0)));
6465 folded_compare = fold (build (code, type,
6466 TREE_OPERAND (varop, 0),
6468 if (integer_zerop (folded_compare)
6469 || integer_onep (folded_compare))
6470 return omit_one_operand (type, folded_compare, varop);
6472 unsigned_type = type_for_size (size, 1);
6473 precision = TYPE_PRECISION (unsigned_type);
6474 mask = build_int_2 (~0, ~0);
6475 TREE_TYPE (mask) = TREE_TYPE (varop);
6476 force_fit_type (mask, 0);
6477 mask = const_binop (RSHIFT_EXPR, mask,
6478 size_int (precision - size), 0);
6479 newconst = fold (build (BIT_AND_EXPR,
6480 TREE_TYPE (varop), newconst,
6481 convert (TREE_TYPE (varop),
6485 t = build (code, type,
6486 (constopnum == 0) ? newconst : varop,
6487 (constopnum == 1) ? newconst : varop);
6493 /* Change X >= CST to X > (CST - 1) if CST is positive. */
6494 if (TREE_CODE (arg1) == INTEGER_CST
6495 && TREE_CODE (arg0) != INTEGER_CST
6496 && tree_int_cst_sgn (arg1) > 0)
6498 switch (TREE_CODE (t))
6502 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6503 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6508 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
6509 t = build (code, type, TREE_OPERAND (t, 0), arg1);
6517 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
6518 a MINUS_EXPR of a constant, we can convert it into a comparison with
6519 a revised constant as long as no overflow occurs. */
6520 if ((code == EQ_EXPR || code == NE_EXPR)
6521 && TREE_CODE (arg1) == INTEGER_CST
6522 && (TREE_CODE (arg0) == PLUS_EXPR
6523 || TREE_CODE (arg0) == MINUS_EXPR)
6524 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6525 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
6526 ? MINUS_EXPR : PLUS_EXPR,
6527 arg1, TREE_OPERAND (arg0, 1), 0))
6528 && ! TREE_CONSTANT_OVERFLOW (tem))
6529 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6531 /* Similarly for a NEGATE_EXPR. */
6532 else if ((code == EQ_EXPR || code == NE_EXPR)
6533 && TREE_CODE (arg0) == NEGATE_EXPR
6534 && TREE_CODE (arg1) == INTEGER_CST
6535 && 0 != (tem = negate_expr (arg1))
6536 && TREE_CODE (tem) == INTEGER_CST
6537 && ! TREE_CONSTANT_OVERFLOW (tem))
6538 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6540 /* If we have X - Y == 0, we can convert that to X == Y and similarly
6541 for !=. Don't do this for ordered comparisons due to overflow. */
6542 else if ((code == NE_EXPR || code == EQ_EXPR)
6543 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
6544 return fold (build (code, type,
6545 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
6547 /* If we are widening one operand of an integer comparison,
6548 see if the other operand is similarly being widened. Perhaps we
6549 can do the comparison in the narrower type. */
6550 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
6551 && TREE_CODE (arg0) == NOP_EXPR
6552 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
6553 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
6554 && (TREE_TYPE (t1) == TREE_TYPE (tem)
6555 || (TREE_CODE (t1) == INTEGER_CST
6556 && int_fits_type_p (t1, TREE_TYPE (tem)))))
6557 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
6559 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
6560 constant, we can simplify it. */
6561 else if (TREE_CODE (arg1) == INTEGER_CST
6562 && (TREE_CODE (arg0) == MIN_EXPR
6563 || TREE_CODE (arg0) == MAX_EXPR)
6564 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6565 return optimize_minmax_comparison (t);
6567 /* If we are comparing an ABS_EXPR with a constant, we can
6568 convert all the cases into explicit comparisons, but they may
6569 well not be faster than doing the ABS and one comparison.
6570 But ABS (X) <= C is a range comparison, which becomes a subtraction
6571 and a comparison, and is probably faster. */
6572 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6573 && TREE_CODE (arg0) == ABS_EXPR
6574 && ! TREE_SIDE_EFFECTS (arg0)
6575 && (0 != (tem = negate_expr (arg1)))
6576 && TREE_CODE (tem) == INTEGER_CST
6577 && ! TREE_CONSTANT_OVERFLOW (tem))
6578 return fold (build (TRUTH_ANDIF_EXPR, type,
6579 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
6580 build (LE_EXPR, type,
6581 TREE_OPERAND (arg0, 0), arg1)));
6583 /* If this is an EQ or NE comparison with zero and ARG0 is
6584 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
6585 two operations, but the latter can be done in one less insn
6586 on machines that have only two-operand insns or on which a
6587 constant cannot be the first operand. */
6588 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
6589 && TREE_CODE (arg0) == BIT_AND_EXPR)
6591 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
6592 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
6594 fold (build (code, type,
6595 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6597 TREE_TYPE (TREE_OPERAND (arg0, 0)),
6598 TREE_OPERAND (arg0, 1),
6599 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
6600 convert (TREE_TYPE (arg0),
6603 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
6604 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
6606 fold (build (code, type,
6607 build (BIT_AND_EXPR, TREE_TYPE (arg0),
6609 TREE_TYPE (TREE_OPERAND (arg0, 1)),
6610 TREE_OPERAND (arg0, 0),
6611 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
6612 convert (TREE_TYPE (arg0),
6617 /* If this is an NE or EQ comparison of zero against the result of a
6618 signed MOD operation whose second operand is a power of 2, make
6619 the MOD operation unsigned since it is simpler and equivalent. */
6620 if ((code == NE_EXPR || code == EQ_EXPR)
6621 && integer_zerop (arg1)
6622 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
6623 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
6624 || TREE_CODE (arg0) == CEIL_MOD_EXPR
6625 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
6626 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
6627 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6629 tree newtype = unsigned_type (TREE_TYPE (arg0));
6630 tree newmod = build (TREE_CODE (arg0), newtype,
6631 convert (newtype, TREE_OPERAND (arg0, 0)),
6632 convert (newtype, TREE_OPERAND (arg0, 1)));
6634 return build (code, type, newmod, convert (newtype, arg1));
6637 /* If this is an NE comparison of zero with an AND of one, remove the
6638 comparison since the AND will give the correct value. */
6639 if (code == NE_EXPR && integer_zerop (arg1)
6640 && TREE_CODE (arg0) == BIT_AND_EXPR
6641 && integer_onep (TREE_OPERAND (arg0, 1)))
6642 return convert (type, arg0);
6644 /* If we have (A & C) == C where C is a power of 2, convert this into
6645 (A & C) != 0. Similarly for NE_EXPR. */
6646 if ((code == EQ_EXPR || code == NE_EXPR)
6647 && TREE_CODE (arg0) == BIT_AND_EXPR
6648 && integer_pow2p (TREE_OPERAND (arg0, 1))
6649 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
6650 return build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
6651 arg0, integer_zero_node);
6653 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
6654 and similarly for >= into !=. */
6655 if ((code == LT_EXPR || code == GE_EXPR)
6656 && TREE_UNSIGNED (TREE_TYPE (arg0))
6657 && TREE_CODE (arg1) == LSHIFT_EXPR
6658 && integer_onep (TREE_OPERAND (arg1, 0)))
6659 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6660 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6661 TREE_OPERAND (arg1, 1)),
6662 convert (TREE_TYPE (arg0), integer_zero_node));
6664 else if ((code == LT_EXPR || code == GE_EXPR)
6665 && TREE_UNSIGNED (TREE_TYPE (arg0))
6666 && (TREE_CODE (arg1) == NOP_EXPR
6667 || TREE_CODE (arg1) == CONVERT_EXPR)
6668 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
6669 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
6671 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
6672 convert (TREE_TYPE (arg0),
6673 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
6674 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
6675 convert (TREE_TYPE (arg0), integer_zero_node));
6677 /* Simplify comparison of something with itself. (For IEEE
6678 floating-point, we can only do some of these simplifications.) */
6679 if (operand_equal_p (arg0, arg1, 0))
6686 if (! FLOAT_TYPE_P (TREE_TYPE (arg0)))
6687 return constant_boolean_node (1, type);
6689 TREE_SET_CODE (t, code);
6693 /* For NE, we can only do this simplification if integer. */
6694 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
6696 /* ... fall through ... */
6699 return constant_boolean_node (0, type);
6705 /* An unsigned comparison against 0 can be simplified. */
6706 if (integer_zerop (arg1)
6707 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6708 || POINTER_TYPE_P (TREE_TYPE (arg1)))
6709 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6711 switch (TREE_CODE (t))
6715 TREE_SET_CODE (t, NE_EXPR);
6719 TREE_SET_CODE (t, EQ_EXPR);
6722 return omit_one_operand (type,
6723 convert (type, integer_one_node),
6726 return omit_one_operand (type,
6727 convert (type, integer_zero_node),
6734 /* Comparisons with the highest or lowest possible integer of
6735 the specified size will have known values and an unsigned
6736 <= 0x7fffffff can be simplified. */
6738 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
6740 if (TREE_CODE (arg1) == INTEGER_CST
6741 && ! TREE_CONSTANT_OVERFLOW (arg1)
6742 && width <= HOST_BITS_PER_WIDE_INT
6743 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
6744 || POINTER_TYPE_P (TREE_TYPE (arg1))))
6746 if (TREE_INT_CST_HIGH (arg1) == 0
6747 && (TREE_INT_CST_LOW (arg1)
6748 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6749 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6750 switch (TREE_CODE (t))
6753 return omit_one_operand (type,
6754 convert (type, integer_zero_node),
6757 TREE_SET_CODE (t, EQ_EXPR);
6761 return omit_one_operand (type,
6762 convert (type, integer_one_node),
6765 TREE_SET_CODE (t, NE_EXPR);
6772 else if (TREE_INT_CST_HIGH (arg1) == -1
6773 && (TREE_INT_CST_LOW (arg1)
6774 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)))
6775 && ! TREE_UNSIGNED (TREE_TYPE (arg1)))
6776 switch (TREE_CODE (t))
6779 return omit_one_operand (type,
6780 convert (type, integer_zero_node),
6783 TREE_SET_CODE (t, EQ_EXPR);
6787 return omit_one_operand (type,
6788 convert (type, integer_one_node),
6791 TREE_SET_CODE (t, NE_EXPR);
6798 else if (TREE_INT_CST_HIGH (arg1) == 0
6799 && (TREE_INT_CST_LOW (arg1)
6800 == ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1)
6801 && TREE_UNSIGNED (TREE_TYPE (arg1))
6802 /* signed_type does not work on pointer types. */
6803 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
6804 switch (TREE_CODE (t))
6807 return fold (build (GE_EXPR, type,
6808 convert (signed_type (TREE_TYPE (arg0)),
6810 convert (signed_type (TREE_TYPE (arg1)),
6811 integer_zero_node)));
6813 return fold (build (LT_EXPR, type,
6814 convert (signed_type (TREE_TYPE (arg0)),
6816 convert (signed_type (TREE_TYPE (arg1)),
6817 integer_zero_node)));
6823 else if (TREE_INT_CST_HIGH (arg1) == 0
6824 && (TREE_INT_CST_LOW (arg1)
6825 == ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1)
6826 && TREE_UNSIGNED (TREE_TYPE (arg1)))
6827 switch (TREE_CODE (t))
6830 return omit_one_operand (type,
6831 convert (type, integer_zero_node),
6834 TREE_SET_CODE (t, EQ_EXPR);
6838 return omit_one_operand (type,
6839 convert (type, integer_one_node),
6842 TREE_SET_CODE (t, NE_EXPR);
6851 /* If we are comparing an expression that just has comparisons
6852 of two integer values, arithmetic expressions of those comparisons,
6853 and constants, we can simplify it. There are only three cases
6854 to check: the two values can either be equal, the first can be
6855 greater, or the second can be greater. Fold the expression for
6856 those three values. Since each value must be 0 or 1, we have
6857 eight possibilities, each of which corresponds to the constant 0
6858 or 1 or one of the six possible comparisons.
6860 This handles common cases like (a > b) == 0 but also handles
6861 expressions like ((x > y) - (y > x)) > 0, which supposedly
6862 occur in macroized code. */
6864 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
6866 tree cval1 = 0, cval2 = 0;
6869 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
6870 /* Don't handle degenerate cases here; they should already
6871 have been handled anyway. */
6872 && cval1 != 0 && cval2 != 0
6873 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
6874 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
6875 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
6876 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
6877 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
6878 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
6879 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
6881 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
6882 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
6884 /* We can't just pass T to eval_subst in case cval1 or cval2
6885 was the same as ARG1. */
6888 = fold (build (code, type,
6889 eval_subst (arg0, cval1, maxval, cval2, minval),
6892 = fold (build (code, type,
6893 eval_subst (arg0, cval1, maxval, cval2, maxval),
6896 = fold (build (code, type,
6897 eval_subst (arg0, cval1, minval, cval2, maxval),
6900 /* All three of these results should be 0 or 1. Confirm they
6901 are. Then use those values to select the proper code
6904 if ((integer_zerop (high_result)
6905 || integer_onep (high_result))
6906 && (integer_zerop (equal_result)
6907 || integer_onep (equal_result))
6908 && (integer_zerop (low_result)
6909 || integer_onep (low_result)))
6911 /* Make a 3-bit mask with the high-order bit being the
6912 value for `>', the next for '=', and the low for '<'. */
6913 switch ((integer_onep (high_result) * 4)
6914 + (integer_onep (equal_result) * 2)
6915 + integer_onep (low_result))
6919 return omit_one_operand (type, integer_zero_node, arg0);
6940 return omit_one_operand (type, integer_one_node, arg0);
6943 t = build (code, type, cval1, cval2);
6945 return save_expr (t);
6952 /* If this is a comparison of a field, we may be able to simplify it. */
6953 if ((TREE_CODE (arg0) == COMPONENT_REF
6954 || TREE_CODE (arg0) == BIT_FIELD_REF)
6955 && (code == EQ_EXPR || code == NE_EXPR)
6956 /* Handle the constant case even without -O
6957 to make sure the warnings are given. */
6958 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
6960 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
6964 /* If this is a comparison of complex values and either or both sides
6965 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
6966 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
6967 This may prevent needless evaluations. */
6968 if ((code == EQ_EXPR || code == NE_EXPR)
6969 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
6970 && (TREE_CODE (arg0) == COMPLEX_EXPR
6971 || TREE_CODE (arg1) == COMPLEX_EXPR
6972 || TREE_CODE (arg0) == COMPLEX_CST
6973 || TREE_CODE (arg1) == COMPLEX_CST))
6975 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
6976 tree real0, imag0, real1, imag1;
6978 arg0 = save_expr (arg0);
6979 arg1 = save_expr (arg1);
6980 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
6981 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
6982 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
6983 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
6985 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
6988 fold (build (code, type, real0, real1)),
6989 fold (build (code, type, imag0, imag1))));
6992 /* Optimize comparisons of strlen vs zero to a compare of the
6993 first character of the string vs zero. To wit,
6994 strlen(ptr) == 0 => *ptr == 0
6995 strlen(ptr) != 0 => *ptr != 0
6996 Other cases should reduce to one of these two (or a constant)
6997 due to the return value of strlen being unsigned. */
6998 if ((code == EQ_EXPR || code == NE_EXPR)
6999 && integer_zerop (arg1)
7000 && TREE_CODE (arg0) == CALL_EXPR
7001 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ADDR_EXPR)
7003 tree fndecl = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7006 if (TREE_CODE (fndecl) == FUNCTION_DECL
7007 && DECL_BUILT_IN (fndecl)
7008 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
7009 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
7010 && (arglist = TREE_OPERAND (arg0, 1))
7011 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
7012 && ! TREE_CHAIN (arglist))
7013 return fold (build (code, type,
7014 build1 (INDIRECT_REF, char_type_node,
7015 TREE_VALUE(arglist)),
7016 integer_zero_node));
7019 /* From here on, the only cases we handle are when the result is
7020 known to be a constant.
7022 To compute GT, swap the arguments and do LT.
7023 To compute GE, do LT and invert the result.
7024 To compute LE, swap the arguments, do LT and invert the result.
7025 To compute NE, do EQ and invert the result.
7027 Therefore, the code below must handle only EQ and LT. */
7029 if (code == LE_EXPR || code == GT_EXPR)
7031 tem = arg0, arg0 = arg1, arg1 = tem;
7032 code = swap_tree_comparison (code);
7035 /* Note that it is safe to invert for real values here because we
7036 will check below in the one case that it matters. */
7040 if (code == NE_EXPR || code == GE_EXPR)
7043 code = invert_tree_comparison (code);
7046 /* Compute a result for LT or EQ if args permit;
7047 otherwise return T. */
7048 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
7050 if (code == EQ_EXPR)
7051 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
7053 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
7054 ? INT_CST_LT_UNSIGNED (arg0, arg1)
7055 : INT_CST_LT (arg0, arg1)),
7059 #if 0 /* This is no longer useful, but breaks some real code. */
7060 /* Assume a nonexplicit constant cannot equal an explicit one,
7061 since such code would be undefined anyway.
7062 Exception: on sysvr4, using #pragma weak,
7063 a label can come out as 0. */
7064 else if (TREE_CODE (arg1) == INTEGER_CST
7065 && !integer_zerop (arg1)
7066 && TREE_CONSTANT (arg0)
7067 && TREE_CODE (arg0) == ADDR_EXPR
7069 t1 = build_int_2 (0, 0);
7071 /* Two real constants can be compared explicitly. */
7072 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
7074 /* If either operand is a NaN, the result is false with two
7075 exceptions: First, an NE_EXPR is true on NaNs, but that case
7076 is already handled correctly since we will be inverting the
7077 result for NE_EXPR. Second, if we had inverted a LE_EXPR
7078 or a GE_EXPR into a LT_EXPR, we must return true so that it
7079 will be inverted into false. */
7081 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
7082 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
7083 t1 = build_int_2 (invert && code == LT_EXPR, 0);
7085 else if (code == EQ_EXPR)
7086 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
7087 TREE_REAL_CST (arg1)),
7090 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
7091 TREE_REAL_CST (arg1)),
7095 if (t1 == NULL_TREE)
7099 TREE_INT_CST_LOW (t1) ^= 1;
7101 TREE_TYPE (t1) = type;
7102 if (TREE_CODE (type) == BOOLEAN_TYPE)
7103 return truthvalue_conversion (t1);
7107 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
7108 so all simple results must be passed through pedantic_non_lvalue. */
7109 if (TREE_CODE (arg0) == INTEGER_CST)
7110 return pedantic_non_lvalue
7111 (TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1)));
7112 else if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
7113 return pedantic_omit_one_operand (type, arg1, arg0);
7115 /* If the second operand is zero, invert the comparison and swap
7116 the second and third operands. Likewise if the second operand
7117 is constant and the third is not or if the third operand is
7118 equivalent to the first operand of the comparison. */
7120 if (integer_zerop (arg1)
7121 || (TREE_CONSTANT (arg1) && ! TREE_CONSTANT (TREE_OPERAND (t, 2)))
7122 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
7123 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
7124 TREE_OPERAND (t, 2),
7125 TREE_OPERAND (arg0, 1))))
7127 /* See if this can be inverted. If it can't, possibly because
7128 it was a floating-point inequality comparison, don't do
7130 tem = invert_truthvalue (arg0);
7132 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7134 t = build (code, type, tem,
7135 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7137 /* arg1 should be the first argument of the new T. */
7138 arg1 = TREE_OPERAND (t, 1);
7143 /* If we have A op B ? A : C, we may be able to convert this to a
7144 simpler expression, depending on the operation and the values
7145 of B and C. IEEE floating point prevents this though,
7146 because A or B might be -0.0 or a NaN. */
7148 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
7149 && (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
7150 || ! FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0)))
7151 || flag_unsafe_math_optimizations)
7152 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
7153 arg1, TREE_OPERAND (arg0, 1)))
7155 tree arg2 = TREE_OPERAND (t, 2);
7156 enum tree_code comp_code = TREE_CODE (arg0);
7160 /* If we have A op 0 ? A : -A, this is A, -A, abs (A), or -abs (A),
7161 depending on the comparison operation. */
7162 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
7163 ? real_zerop (TREE_OPERAND (arg0, 1))
7164 : integer_zerop (TREE_OPERAND (arg0, 1)))
7165 && TREE_CODE (arg2) == NEGATE_EXPR
7166 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
7174 (convert (TREE_TYPE (TREE_OPERAND (t, 1)),
7178 return pedantic_non_lvalue (convert (type, arg1));
7181 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7182 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
7183 return pedantic_non_lvalue
7184 (convert (type, fold (build1 (ABS_EXPR,
7185 TREE_TYPE (arg1), arg1))));
7188 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7189 arg1 = convert (signed_type (TREE_TYPE (arg1)), arg1);
7190 return pedantic_non_lvalue
7191 (negate_expr (convert (type,
7192 fold (build1 (ABS_EXPR,
7199 /* If this is A != 0 ? A : 0, this is simply A. For ==, it is
7202 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
7204 if (comp_code == NE_EXPR)
7205 return pedantic_non_lvalue (convert (type, arg1));
7206 else if (comp_code == EQ_EXPR)
7207 return pedantic_non_lvalue (convert (type, integer_zero_node));
7210 /* If this is A op B ? A : B, this is either A, B, min (A, B),
7211 or max (A, B), depending on the operation. */
7213 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
7214 arg2, TREE_OPERAND (arg0, 0)))
7216 tree comp_op0 = TREE_OPERAND (arg0, 0);
7217 tree comp_op1 = TREE_OPERAND (arg0, 1);
7218 tree comp_type = TREE_TYPE (comp_op0);
7220 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
7221 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
7227 return pedantic_non_lvalue (convert (type, arg2));
7229 return pedantic_non_lvalue (convert (type, arg1));
7232 /* In C++ a ?: expression can be an lvalue, so put the
7233 operand which will be used if they are equal first
7234 so that we can convert this back to the
7235 corresponding COND_EXPR. */
7236 return pedantic_non_lvalue
7237 (convert (type, fold (build (MIN_EXPR, comp_type,
7238 (comp_code == LE_EXPR
7239 ? comp_op0 : comp_op1),
7240 (comp_code == LE_EXPR
7241 ? comp_op1 : comp_op0)))));
7245 return pedantic_non_lvalue
7246 (convert (type, fold (build (MAX_EXPR, comp_type,
7247 (comp_code == GE_EXPR
7248 ? comp_op0 : comp_op1),
7249 (comp_code == GE_EXPR
7250 ? comp_op1 : comp_op0)))));
7257 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
7258 we might still be able to simplify this. For example,
7259 if C1 is one less or one more than C2, this might have started
7260 out as a MIN or MAX and been transformed by this function.
7261 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
7263 if (INTEGRAL_TYPE_P (type)
7264 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7265 && TREE_CODE (arg2) == INTEGER_CST)
7269 /* We can replace A with C1 in this case. */
7270 arg1 = convert (type, TREE_OPERAND (arg0, 1));
7271 t = build (code, type, TREE_OPERAND (t, 0), arg1,
7272 TREE_OPERAND (t, 2));
7276 /* If C1 is C2 + 1, this is min(A, C2). */
7277 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7278 && operand_equal_p (TREE_OPERAND (arg0, 1),
7279 const_binop (PLUS_EXPR, arg2,
7280 integer_one_node, 0), 1))
7281 return pedantic_non_lvalue
7282 (fold (build (MIN_EXPR, type, arg1, arg2)));
7286 /* If C1 is C2 - 1, this is min(A, C2). */
7287 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7288 && operand_equal_p (TREE_OPERAND (arg0, 1),
7289 const_binop (MINUS_EXPR, arg2,
7290 integer_one_node, 0), 1))
7291 return pedantic_non_lvalue
7292 (fold (build (MIN_EXPR, type, arg1, arg2)));
7296 /* If C1 is C2 - 1, this is max(A, C2). */
7297 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
7298 && operand_equal_p (TREE_OPERAND (arg0, 1),
7299 const_binop (MINUS_EXPR, arg2,
7300 integer_one_node, 0), 1))
7301 return pedantic_non_lvalue
7302 (fold (build (MAX_EXPR, type, arg1, arg2)));
7306 /* If C1 is C2 + 1, this is max(A, C2). */
7307 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
7308 && operand_equal_p (TREE_OPERAND (arg0, 1),
7309 const_binop (PLUS_EXPR, arg2,
7310 integer_one_node, 0), 1))
7311 return pedantic_non_lvalue
7312 (fold (build (MAX_EXPR, type, arg1, arg2)));
7321 /* If the second operand is simpler than the third, swap them
7322 since that produces better jump optimization results. */
7323 if ((TREE_CONSTANT (arg1) || DECL_P (arg1)
7324 || TREE_CODE (arg1) == SAVE_EXPR)
7325 && ! (TREE_CONSTANT (TREE_OPERAND (t, 2))
7326 || DECL_P (TREE_OPERAND (t, 2))
7327 || TREE_CODE (TREE_OPERAND (t, 2)) == SAVE_EXPR))
7329 /* See if this can be inverted. If it can't, possibly because
7330 it was a floating-point inequality comparison, don't do
7332 tem = invert_truthvalue (arg0);
7334 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
7336 t = build (code, type, tem,
7337 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1));
7339 /* arg1 should be the first argument of the new T. */
7340 arg1 = TREE_OPERAND (t, 1);
7345 /* Convert A ? 1 : 0 to simply A. */
7346 if (integer_onep (TREE_OPERAND (t, 1))
7347 && integer_zerop (TREE_OPERAND (t, 2))
7348 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
7349 call to fold will try to move the conversion inside
7350 a COND, which will recurse. In that case, the COND_EXPR
7351 is probably the best choice, so leave it alone. */
7352 && type == TREE_TYPE (arg0))
7353 return pedantic_non_lvalue (arg0);
7355 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
7356 operation is simply A & 2. */
7358 if (integer_zerop (TREE_OPERAND (t, 2))
7359 && TREE_CODE (arg0) == NE_EXPR
7360 && integer_zerop (TREE_OPERAND (arg0, 1))
7361 && integer_pow2p (arg1)
7362 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
7363 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
7365 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
7370 /* When pedantic, a compound expression can be neither an lvalue
7371 nor an integer constant expression. */
7372 if (TREE_SIDE_EFFECTS (arg0) || pedantic)
7374 /* Don't let (0, 0) be null pointer constant. */
7375 if (integer_zerop (arg1))
7376 return build1 (NOP_EXPR, type, arg1);
7377 return convert (type, arg1);
7381 return build_complex (type, arg0, arg1);
7385 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7387 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7388 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
7389 TREE_OPERAND (arg0, 1));
7390 else if (TREE_CODE (arg0) == COMPLEX_CST)
7391 return TREE_REALPART (arg0);
7392 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7393 return fold (build (TREE_CODE (arg0), type,
7394 fold (build1 (REALPART_EXPR, type,
7395 TREE_OPERAND (arg0, 0))),
7396 fold (build1 (REALPART_EXPR,
7397 type, TREE_OPERAND (arg0, 1)))));
7401 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7402 return convert (type, integer_zero_node);
7403 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7404 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7405 TREE_OPERAND (arg0, 0));
7406 else if (TREE_CODE (arg0) == COMPLEX_CST)
7407 return TREE_IMAGPART (arg0);
7408 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7409 return fold (build (TREE_CODE (arg0), type,
7410 fold (build1 (IMAGPART_EXPR, type,
7411 TREE_OPERAND (arg0, 0))),
7412 fold (build1 (IMAGPART_EXPR, type,
7413 TREE_OPERAND (arg0, 1)))));
7416 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
7418 case CLEANUP_POINT_EXPR:
7419 if (! has_cleanups (arg0))
7420 return TREE_OPERAND (t, 0);
7423 enum tree_code code0 = TREE_CODE (arg0);
7424 int kind0 = TREE_CODE_CLASS (code0);
7425 tree arg00 = TREE_OPERAND (arg0, 0);
7428 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
7429 return fold (build1 (code0, type,
7430 fold (build1 (CLEANUP_POINT_EXPR,
7431 TREE_TYPE (arg00), arg00))));
7433 if (kind0 == '<' || kind0 == '2'
7434 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
7435 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
7436 || code0 == TRUTH_XOR_EXPR)
7438 arg01 = TREE_OPERAND (arg0, 1);
7440 if (TREE_CONSTANT (arg00)
7441 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
7442 && ! has_cleanups (arg00)))
7443 return fold (build (code0, type, arg00,
7444 fold (build1 (CLEANUP_POINT_EXPR,
7445 TREE_TYPE (arg01), arg01))));
7447 if (TREE_CONSTANT (arg01))
7448 return fold (build (code0, type,
7449 fold (build1 (CLEANUP_POINT_EXPR,
7450 TREE_TYPE (arg00), arg00)),
7458 /* Check for a built-in function. */
7459 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
7460 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))
7462 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
7464 tree tmp = fold_builtin (expr);
7472 } /* switch (code) */
7475 /* Determine if first argument is a multiple of second argument. Return 0 if
7476 it is not, or we cannot easily determined it to be.
7478 An example of the sort of thing we care about (at this point; this routine
7479 could surely be made more general, and expanded to do what the *_DIV_EXPR's
7480 fold cases do now) is discovering that
7482 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7488 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
7490 This code also handles discovering that
7492 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
7494 is a multiple of 8 so we don't have to worry about dealing with a
7497 Note that we *look* inside a SAVE_EXPR only to determine how it was
7498 calculated; it is not safe for fold to do much of anything else with the
7499 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
7500 at run time. For example, the latter example above *cannot* be implemented
7501 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
7502 evaluation time of the original SAVE_EXPR is not necessarily the same at
7503 the time the new expression is evaluated. The only optimization of this
7504 sort that would be valid is changing
7506 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
7510 SAVE_EXPR (I) * SAVE_EXPR (J)
7512 (where the same SAVE_EXPR (J) is used in the original and the
7513 transformed version). */
7516 multiple_of_p (type, top, bottom)
7521 if (operand_equal_p (top, bottom, 0))
7524 if (TREE_CODE (type) != INTEGER_TYPE)
7527 switch (TREE_CODE (top))
7530 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7531 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7535 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
7536 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
7539 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
7543 op1 = TREE_OPERAND (top, 1);
7544 /* const_binop may not detect overflow correctly,
7545 so check for it explicitly here. */
7546 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
7547 > TREE_INT_CST_LOW (op1)
7548 && TREE_INT_CST_HIGH (op1) == 0
7549 && 0 != (t1 = convert (type,
7550 const_binop (LSHIFT_EXPR, size_one_node,
7552 && ! TREE_OVERFLOW (t1))
7553 return multiple_of_p (type, t1, bottom);
7558 /* Can't handle conversions from non-integral or wider integral type. */
7559 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
7560 || (TYPE_PRECISION (type)
7561 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
7564 /* .. fall through ... */
7567 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
7570 if (TREE_CODE (bottom) != INTEGER_CST
7571 || (TREE_UNSIGNED (type)
7572 && (tree_int_cst_sgn (top) < 0
7573 || tree_int_cst_sgn (bottom) < 0)))
7575 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
7583 /* Return true if `t' is known to be non-negative. */
7586 tree_expr_nonnegative_p (t)
7589 switch (TREE_CODE (t))
7595 return tree_int_cst_sgn (t) >= 0;
7596 case TRUNC_DIV_EXPR:
7598 case FLOOR_DIV_EXPR:
7599 case ROUND_DIV_EXPR:
7600 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7601 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7602 case TRUNC_MOD_EXPR:
7604 case FLOOR_MOD_EXPR:
7605 case ROUND_MOD_EXPR:
7606 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7608 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
7609 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
7611 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7613 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7614 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7616 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
7617 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7619 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7621 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
7623 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7624 case NON_LVALUE_EXPR:
7625 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
7627 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
7630 if (truth_value_p (TREE_CODE (t)))
7631 /* Truth values evaluate to 0 or 1, which is nonnegative. */
7634 /* We don't know sign of `t', so be conservative and return false. */
7639 /* Return true if `r' is known to be non-negative.
7640 Only handles constants at the moment. */
7643 rtl_expr_nonnegative_p (r)
7646 switch (GET_CODE (r))
7649 return INTVAL (r) >= 0;
7652 if (GET_MODE (r) == VOIDmode)
7653 return CONST_DOUBLE_HIGH (r) >= 0;
7661 units = CONST_VECTOR_NUNITS (r);
7663 for (i = 0; i < units; ++i)
7665 elt = CONST_VECTOR_ELT (r, i);
7666 if (!rtl_expr_nonnegative_p (elt))
7675 /* These are always nonnegative. */