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, 1999,
3 2000, 2001, 2002, 2003, 2004 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. */
47 #include "coretypes.h"
58 #include "langhooks.h"
61 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
62 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
63 static bool negate_mathfn_p (enum built_in_function);
64 static bool negate_expr_p (tree);
65 static tree negate_expr (tree);
66 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
67 static tree associate_trees (tree, tree, enum tree_code, tree);
68 static tree int_const_binop (enum tree_code, tree, tree, int);
69 static tree const_binop (enum tree_code, tree, tree, int);
70 static hashval_t size_htab_hash (const void *);
71 static int size_htab_eq (const void *, const void *);
72 static tree fold_convert_const (enum tree_code, tree, tree);
73 static enum tree_code invert_tree_comparison (enum tree_code);
74 static enum tree_code swap_tree_comparison (enum tree_code);
75 static int comparison_to_compcode (enum tree_code);
76 static enum tree_code compcode_to_comparison (int);
77 static int truth_value_p (enum tree_code);
78 static int operand_equal_for_comparison_p (tree, tree, tree);
79 static int twoval_comparison_p (tree, tree *, tree *, int *);
80 static tree eval_subst (tree, tree, tree, tree, tree);
81 static tree pedantic_omit_one_operand (tree, tree, tree);
82 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
83 static tree make_bit_field_ref (tree, tree, int, int, int);
84 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
85 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
86 enum machine_mode *, int *, int *,
88 static int all_ones_mask_p (tree, int);
89 static tree sign_bit_p (tree, tree);
90 static int simple_operand_p (tree);
91 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
92 static tree make_range (tree, int *, tree *, tree *);
93 static tree build_range_check (tree, tree, int, tree, tree);
94 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
96 static tree fold_range_test (tree);
97 static tree unextend (tree, int, int, tree);
98 static tree fold_truthop (enum tree_code, tree, tree, tree);
99 static tree optimize_minmax_comparison (tree);
100 static tree extract_muldiv (tree, tree, enum tree_code, tree);
101 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
102 static tree strip_compound_expr (tree, tree);
103 static int multiple_of_p (tree, tree, tree);
104 static tree constant_boolean_node (int, tree);
105 static int count_cond (tree, int);
106 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree, tree,
108 static bool fold_real_zero_addition_p (tree, tree, int);
109 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
111 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
112 static bool reorder_operands_p (tree, tree);
113 static bool tree_swap_operands_p (tree, tree, bool);
115 /* The following constants represent a bit based encoding of GCC's
116 comparison operators. This encoding simplifies transformations
117 on relational comparison operators, such as AND and OR. */
118 #define COMPCODE_FALSE 0
119 #define COMPCODE_LT 1
120 #define COMPCODE_EQ 2
121 #define COMPCODE_LE 3
122 #define COMPCODE_GT 4
123 #define COMPCODE_NE 5
124 #define COMPCODE_GE 6
125 #define COMPCODE_TRUE 7
127 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
128 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
129 and SUM1. Then this yields nonzero if overflow occurred during the
132 Overflow occurs if A and B have the same sign, but A and SUM differ in
133 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
135 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
137 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
138 We do that by representing the two-word integer in 4 words, with only
139 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
140 number. The value of the word is LOWPART + HIGHPART * BASE. */
143 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
144 #define HIGHPART(x) \
145 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
146 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
148 /* Unpack a two-word integer into 4 words.
149 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
150 WORDS points to the array of HOST_WIDE_INTs. */
153 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
155 words[0] = LOWPART (low);
156 words[1] = HIGHPART (low);
157 words[2] = LOWPART (hi);
158 words[3] = HIGHPART (hi);
161 /* Pack an array of 4 words into a two-word integer.
162 WORDS points to the array of words.
163 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
166 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
169 *low = words[0] + words[1] * BASE;
170 *hi = words[2] + words[3] * BASE;
173 /* Make the integer constant T valid for its type by setting to 0 or 1 all
174 the bits in the constant that don't belong in the type.
176 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
177 nonzero, a signed overflow has already occurred in calculating T, so
181 force_fit_type (tree t, int overflow)
183 unsigned HOST_WIDE_INT low;
187 if (TREE_CODE (t) == REAL_CST)
189 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
190 Consider doing it via real_convert now. */
194 else if (TREE_CODE (t) != INTEGER_CST)
197 low = TREE_INT_CST_LOW (t);
198 high = TREE_INT_CST_HIGH (t);
200 if (POINTER_TYPE_P (TREE_TYPE (t))
201 || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
204 prec = TYPE_PRECISION (TREE_TYPE (t));
206 /* First clear all bits that are beyond the type's precision. */
208 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
210 else if (prec > HOST_BITS_PER_WIDE_INT)
211 TREE_INT_CST_HIGH (t)
212 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
215 TREE_INT_CST_HIGH (t) = 0;
216 if (prec < HOST_BITS_PER_WIDE_INT)
217 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
220 /* Unsigned types do not suffer sign extension or overflow unless they
222 if (TREE_UNSIGNED (TREE_TYPE (t))
223 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
224 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
227 /* If the value's sign bit is set, extend the sign. */
228 if (prec != 2 * HOST_BITS_PER_WIDE_INT
229 && (prec > HOST_BITS_PER_WIDE_INT
230 ? 0 != (TREE_INT_CST_HIGH (t)
232 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
233 : 0 != (TREE_INT_CST_LOW (t)
234 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
236 /* Value is negative:
237 set to 1 all the bits that are outside this type's precision. */
238 if (prec > HOST_BITS_PER_WIDE_INT)
239 TREE_INT_CST_HIGH (t)
240 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
243 TREE_INT_CST_HIGH (t) = -1;
244 if (prec < HOST_BITS_PER_WIDE_INT)
245 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
249 /* Return nonzero if signed overflow occurred. */
251 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
255 /* Add two doubleword integers with doubleword result.
256 Each argument is given as two `HOST_WIDE_INT' pieces.
257 One argument is L1 and H1; the other, L2 and H2.
258 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
261 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
262 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
263 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
265 unsigned HOST_WIDE_INT l;
269 h = h1 + h2 + (l < l1);
273 return OVERFLOW_SUM_SIGN (h1, h2, h);
276 /* Negate a doubleword integer with doubleword result.
277 Return nonzero if the operation overflows, assuming it's signed.
278 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
279 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
282 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
283 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
289 return (*hv & h1) < 0;
299 /* Multiply two doubleword integers with doubleword result.
300 Return nonzero if the operation overflows, assuming it's signed.
301 Each argument is given as two `HOST_WIDE_INT' pieces.
302 One argument is L1 and H1; the other, L2 and H2.
303 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
306 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
307 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
308 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
310 HOST_WIDE_INT arg1[4];
311 HOST_WIDE_INT arg2[4];
312 HOST_WIDE_INT prod[4 * 2];
313 unsigned HOST_WIDE_INT carry;
315 unsigned HOST_WIDE_INT toplow, neglow;
316 HOST_WIDE_INT tophigh, neghigh;
318 encode (arg1, l1, h1);
319 encode (arg2, l2, h2);
321 memset (prod, 0, sizeof prod);
323 for (i = 0; i < 4; i++)
326 for (j = 0; j < 4; j++)
329 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
330 carry += arg1[i] * arg2[j];
331 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
333 prod[k] = LOWPART (carry);
334 carry = HIGHPART (carry);
339 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
341 /* Check for overflow by calculating the top half of the answer in full;
342 it should agree with the low half's sign bit. */
343 decode (prod + 4, &toplow, &tophigh);
346 neg_double (l2, h2, &neglow, &neghigh);
347 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
351 neg_double (l1, h1, &neglow, &neghigh);
352 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
354 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
357 /* Shift the doubleword integer in L1, H1 left by COUNT places
358 keeping only PREC bits of result.
359 Shift right if COUNT is negative.
360 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
361 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
364 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
365 HOST_WIDE_INT count, unsigned int prec,
366 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
368 unsigned HOST_WIDE_INT signmask;
372 rshift_double (l1, h1, -count, prec, lv, hv, arith);
376 #ifdef SHIFT_COUNT_TRUNCATED
377 if (SHIFT_COUNT_TRUNCATED)
381 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
383 /* Shifting by the host word size is undefined according to the
384 ANSI standard, so we must handle this as a special case. */
388 else if (count >= HOST_BITS_PER_WIDE_INT)
390 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
395 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
396 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
400 /* Sign extend all bits that are beyond the precision. */
402 signmask = -((prec > HOST_BITS_PER_WIDE_INT
403 ? ((unsigned HOST_WIDE_INT) *hv
404 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
405 : (*lv >> (prec - 1))) & 1);
407 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
409 else if (prec >= HOST_BITS_PER_WIDE_INT)
411 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
412 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
417 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
418 *lv |= signmask << prec;
422 /* Shift the doubleword integer in L1, H1 right by COUNT places
423 keeping only PREC bits of result. COUNT must be positive.
424 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
425 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
428 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
429 HOST_WIDE_INT count, unsigned int prec,
430 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
433 unsigned HOST_WIDE_INT signmask;
436 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
439 #ifdef SHIFT_COUNT_TRUNCATED
440 if (SHIFT_COUNT_TRUNCATED)
444 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
446 /* Shifting by the host word size is undefined according to the
447 ANSI standard, so we must handle this as a special case. */
451 else if (count >= HOST_BITS_PER_WIDE_INT)
454 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
458 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
460 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
463 /* Zero / sign extend all bits that are beyond the precision. */
465 if (count >= (HOST_WIDE_INT)prec)
470 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
472 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
474 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
475 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
480 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
481 *lv |= signmask << (prec - count);
485 /* Rotate the doubleword integer in L1, H1 left by COUNT places
486 keeping only PREC bits of result.
487 Rotate right if COUNT is negative.
488 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
491 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
492 HOST_WIDE_INT count, unsigned int prec,
493 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
495 unsigned HOST_WIDE_INT s1l, s2l;
496 HOST_WIDE_INT s1h, s2h;
502 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
503 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
508 /* Rotate the doubleword integer in L1, H1 left by COUNT places
509 keeping only PREC bits of result. COUNT must be positive.
510 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
513 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
514 HOST_WIDE_INT count, unsigned int prec,
515 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
517 unsigned HOST_WIDE_INT s1l, s2l;
518 HOST_WIDE_INT s1h, s2h;
524 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
525 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
530 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
531 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
532 CODE is a tree code for a kind of division, one of
533 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
535 It controls how the quotient is rounded to an integer.
536 Return nonzero if the operation overflows.
537 UNS nonzero says do unsigned division. */
540 div_and_round_double (enum tree_code code, int uns,
541 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
542 HOST_WIDE_INT hnum_orig,
543 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
544 HOST_WIDE_INT hden_orig,
545 unsigned HOST_WIDE_INT *lquo,
546 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
550 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
551 HOST_WIDE_INT den[4], quo[4];
553 unsigned HOST_WIDE_INT work;
554 unsigned HOST_WIDE_INT carry = 0;
555 unsigned HOST_WIDE_INT lnum = lnum_orig;
556 HOST_WIDE_INT hnum = hnum_orig;
557 unsigned HOST_WIDE_INT lden = lden_orig;
558 HOST_WIDE_INT hden = hden_orig;
561 if (hden == 0 && lden == 0)
562 overflow = 1, lden = 1;
564 /* Calculate quotient sign and convert operands to unsigned. */
570 /* (minimum integer) / (-1) is the only overflow case. */
571 if (neg_double (lnum, hnum, &lnum, &hnum)
572 && ((HOST_WIDE_INT) lden & hden) == -1)
578 neg_double (lden, hden, &lden, &hden);
582 if (hnum == 0 && hden == 0)
583 { /* single precision */
585 /* This unsigned division rounds toward zero. */
591 { /* trivial case: dividend < divisor */
592 /* hden != 0 already checked. */
599 memset (quo, 0, sizeof quo);
601 memset (num, 0, sizeof num); /* to zero 9th element */
602 memset (den, 0, sizeof den);
604 encode (num, lnum, hnum);
605 encode (den, lden, hden);
607 /* Special code for when the divisor < BASE. */
608 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
610 /* hnum != 0 already checked. */
611 for (i = 4 - 1; i >= 0; i--)
613 work = num[i] + carry * BASE;
614 quo[i] = work / lden;
620 /* Full double precision division,
621 with thanks to Don Knuth's "Seminumerical Algorithms". */
622 int num_hi_sig, den_hi_sig;
623 unsigned HOST_WIDE_INT quo_est, scale;
625 /* Find the highest nonzero divisor digit. */
626 for (i = 4 - 1;; i--)
633 /* Insure that the first digit of the divisor is at least BASE/2.
634 This is required by the quotient digit estimation algorithm. */
636 scale = BASE / (den[den_hi_sig] + 1);
638 { /* scale divisor and dividend */
640 for (i = 0; i <= 4 - 1; i++)
642 work = (num[i] * scale) + carry;
643 num[i] = LOWPART (work);
644 carry = HIGHPART (work);
649 for (i = 0; i <= 4 - 1; i++)
651 work = (den[i] * scale) + carry;
652 den[i] = LOWPART (work);
653 carry = HIGHPART (work);
654 if (den[i] != 0) den_hi_sig = i;
661 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
663 /* Guess the next quotient digit, quo_est, by dividing the first
664 two remaining dividend digits by the high order quotient digit.
665 quo_est is never low and is at most 2 high. */
666 unsigned HOST_WIDE_INT tmp;
668 num_hi_sig = i + den_hi_sig + 1;
669 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
670 if (num[num_hi_sig] != den[den_hi_sig])
671 quo_est = work / den[den_hi_sig];
675 /* Refine quo_est so it's usually correct, and at most one high. */
676 tmp = work - quo_est * den[den_hi_sig];
678 && (den[den_hi_sig - 1] * quo_est
679 > (tmp * BASE + num[num_hi_sig - 2])))
682 /* Try QUO_EST as the quotient digit, by multiplying the
683 divisor by QUO_EST and subtracting from the remaining dividend.
684 Keep in mind that QUO_EST is the I - 1st digit. */
687 for (j = 0; j <= den_hi_sig; j++)
689 work = quo_est * den[j] + carry;
690 carry = HIGHPART (work);
691 work = num[i + j] - LOWPART (work);
692 num[i + j] = LOWPART (work);
693 carry += HIGHPART (work) != 0;
696 /* If quo_est was high by one, then num[i] went negative and
697 we need to correct things. */
698 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
701 carry = 0; /* add divisor back in */
702 for (j = 0; j <= den_hi_sig; j++)
704 work = num[i + j] + den[j] + carry;
705 carry = HIGHPART (work);
706 num[i + j] = LOWPART (work);
709 num [num_hi_sig] += carry;
712 /* Store the quotient digit. */
717 decode (quo, lquo, hquo);
720 /* If result is negative, make it so. */
722 neg_double (*lquo, *hquo, lquo, hquo);
724 /* compute trial remainder: rem = num - (quo * den) */
725 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
726 neg_double (*lrem, *hrem, lrem, hrem);
727 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
732 case TRUNC_MOD_EXPR: /* round toward zero */
733 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
737 case FLOOR_MOD_EXPR: /* round toward negative infinity */
738 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
741 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
749 case CEIL_MOD_EXPR: /* round toward positive infinity */
750 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
752 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
760 case ROUND_MOD_EXPR: /* round to closest integer */
762 unsigned HOST_WIDE_INT labs_rem = *lrem;
763 HOST_WIDE_INT habs_rem = *hrem;
764 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
765 HOST_WIDE_INT habs_den = hden, htwice;
767 /* Get absolute values. */
769 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
771 neg_double (lden, hden, &labs_den, &habs_den);
773 /* If (2 * abs (lrem) >= abs (lden)) */
774 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
775 labs_rem, habs_rem, <wice, &htwice);
777 if (((unsigned HOST_WIDE_INT) habs_den
778 < (unsigned HOST_WIDE_INT) htwice)
779 || (((unsigned HOST_WIDE_INT) habs_den
780 == (unsigned HOST_WIDE_INT) htwice)
781 && (labs_den < ltwice)))
785 add_double (*lquo, *hquo,
786 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
789 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
801 /* Compute true remainder: rem = num - (quo * den) */
802 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
803 neg_double (*lrem, *hrem, lrem, hrem);
804 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
808 /* Return true if built-in mathematical function specified by CODE
809 preserves the sign of it argument, i.e. -f(x) == f(-x). */
812 negate_mathfn_p (enum built_in_function code)
837 /* Determine whether an expression T can be cheaply negated using
838 the function negate_expr. */
841 negate_expr_p (tree t)
843 unsigned HOST_WIDE_INT val;
850 type = TREE_TYPE (t);
853 switch (TREE_CODE (t))
856 if (TREE_UNSIGNED (type) || ! flag_trapv)
859 /* Check that -CST will not overflow type. */
860 prec = TYPE_PRECISION (type);
861 if (prec > HOST_BITS_PER_WIDE_INT)
863 if (TREE_INT_CST_LOW (t) != 0)
865 prec -= HOST_BITS_PER_WIDE_INT;
866 val = TREE_INT_CST_HIGH (t);
869 val = TREE_INT_CST_LOW (t);
870 if (prec < HOST_BITS_PER_WIDE_INT)
871 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
872 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
879 return negate_expr_p (TREE_REALPART (t))
880 && negate_expr_p (TREE_IMAGPART (t));
883 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
884 return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
885 && reorder_operands_p (TREE_OPERAND (t, 0),
886 TREE_OPERAND (t, 1));
889 if (TREE_UNSIGNED (TREE_TYPE (t)))
895 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
896 return negate_expr_p (TREE_OPERAND (t, 1))
897 || negate_expr_p (TREE_OPERAND (t, 0));
901 /* Negate -((double)float) as (double)(-float). */
902 if (TREE_CODE (type) == REAL_TYPE)
904 tree tem = strip_float_extensions (t);
906 return negate_expr_p (tem);
911 /* Negate -f(x) as f(-x). */
912 if (negate_mathfn_p (builtin_mathfn_code (t)))
913 return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
922 /* Given T, an expression, return the negation of T. Allow for T to be
923 null, in which case return null. */
934 type = TREE_TYPE (t);
937 switch (TREE_CODE (t))
941 unsigned HOST_WIDE_INT low;
943 int overflow = neg_double (TREE_INT_CST_LOW (t),
944 TREE_INT_CST_HIGH (t),
946 tem = build_int_2 (low, high);
947 TREE_TYPE (tem) = type;
950 | force_fit_type (tem, overflow && !TREE_UNSIGNED (type)));
951 TREE_CONSTANT_OVERFLOW (tem)
952 = TREE_OVERFLOW (tem) | TREE_CONSTANT_OVERFLOW (t);
954 if (! TREE_OVERFLOW (tem)
955 || TREE_UNSIGNED (type)
961 tem = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (t)));
962 /* Two's complement FP formats, such as c4x, may overflow. */
963 if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
964 return convert (type, tem);
969 tree rpart = negate_expr (TREE_REALPART (t));
970 tree ipart = negate_expr (TREE_IMAGPART (t));
972 if ((TREE_CODE (rpart) == REAL_CST
973 && TREE_CODE (ipart) == REAL_CST)
974 || (TREE_CODE (rpart) == INTEGER_CST
975 && TREE_CODE (ipart) == INTEGER_CST))
976 return build_complex (type, rpart, ipart);
981 return convert (type, TREE_OPERAND (t, 0));
984 /* - (A - B) -> B - A */
985 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
986 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
987 return convert (type,
988 fold (build (MINUS_EXPR, TREE_TYPE (t),
990 TREE_OPERAND (t, 0))));
994 if (TREE_UNSIGNED (TREE_TYPE (t)))
1000 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1002 tem = TREE_OPERAND (t, 1);
1003 if (negate_expr_p (tem))
1004 return convert (type,
1005 fold (build (TREE_CODE (t), TREE_TYPE (t),
1006 TREE_OPERAND (t, 0),
1007 negate_expr (tem))));
1008 tem = TREE_OPERAND (t, 0);
1009 if (negate_expr_p (tem))
1010 return convert (type,
1011 fold (build (TREE_CODE (t), TREE_TYPE (t),
1013 TREE_OPERAND (t, 1))));
1018 /* Convert -((double)float) into (double)(-float). */
1019 if (TREE_CODE (type) == REAL_TYPE)
1021 tem = strip_float_extensions (t);
1022 if (tem != t && negate_expr_p (tem))
1023 return convert (type, negate_expr (tem));
1028 /* Negate -f(x) as f(-x). */
1029 if (negate_mathfn_p (builtin_mathfn_code (t))
1030 && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1032 tree fndecl, arg, arglist;
1034 fndecl = get_callee_fndecl (t);
1035 arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1036 arglist = build_tree_list (NULL_TREE, arg);
1037 return build_function_call_expr (fndecl, arglist);
1045 return convert (type, fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t)));
1048 /* Split a tree IN into a constant, literal and variable parts that could be
1049 combined with CODE to make IN. "constant" means an expression with
1050 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1051 commutative arithmetic operation. Store the constant part into *CONP,
1052 the literal in *LITP and return the variable part. If a part isn't
1053 present, set it to null. If the tree does not decompose in this way,
1054 return the entire tree as the variable part and the other parts as null.
1056 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1057 case, we negate an operand that was subtracted. Except if it is a
1058 literal for which we use *MINUS_LITP instead.
1060 If NEGATE_P is true, we are negating all of IN, again except a literal
1061 for which we use *MINUS_LITP instead.
1063 If IN is itself a literal or constant, return it as appropriate.
1065 Note that we do not guarantee that any of the three values will be the
1066 same type as IN, but they will have the same signedness and mode. */
1069 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1070 tree *minus_litp, int negate_p)
1078 /* Strip any conversions that don't change the machine mode or signedness. */
1079 STRIP_SIGN_NOPS (in);
1081 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1083 else if (TREE_CODE (in) == code
1084 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1085 /* We can associate addition and subtraction together (even
1086 though the C standard doesn't say so) for integers because
1087 the value is not affected. For reals, the value might be
1088 affected, so we can't. */
1089 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1090 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1092 tree op0 = TREE_OPERAND (in, 0);
1093 tree op1 = TREE_OPERAND (in, 1);
1094 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1095 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1097 /* First see if either of the operands is a literal, then a constant. */
1098 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1099 *litp = op0, op0 = 0;
1100 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1101 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1103 if (op0 != 0 && TREE_CONSTANT (op0))
1104 *conp = op0, op0 = 0;
1105 else if (op1 != 0 && TREE_CONSTANT (op1))
1106 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1108 /* If we haven't dealt with either operand, this is not a case we can
1109 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1110 if (op0 != 0 && op1 != 0)
1115 var = op1, neg_var_p = neg1_p;
1117 /* Now do any needed negations. */
1119 *minus_litp = *litp, *litp = 0;
1121 *conp = negate_expr (*conp);
1123 var = negate_expr (var);
1125 else if (TREE_CONSTANT (in))
1133 *minus_litp = *litp, *litp = 0;
1134 else if (*minus_litp)
1135 *litp = *minus_litp, *minus_litp = 0;
1136 *conp = negate_expr (*conp);
1137 var = negate_expr (var);
1143 /* Re-associate trees split by the above function. T1 and T2 are either
1144 expressions to associate or null. Return the new expression, if any. If
1145 we build an operation, do it in TYPE and with CODE. */
1148 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1155 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1156 try to fold this since we will have infinite recursion. But do
1157 deal with any NEGATE_EXPRs. */
1158 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1159 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1161 if (code == PLUS_EXPR)
1163 if (TREE_CODE (t1) == NEGATE_EXPR)
1164 return build (MINUS_EXPR, type, convert (type, t2),
1165 convert (type, TREE_OPERAND (t1, 0)));
1166 else if (TREE_CODE (t2) == NEGATE_EXPR)
1167 return build (MINUS_EXPR, type, convert (type, t1),
1168 convert (type, TREE_OPERAND (t2, 0)));
1170 return build (code, type, convert (type, t1), convert (type, t2));
1173 return fold (build (code, type, convert (type, t1), convert (type, t2)));
1176 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1177 to produce a new constant.
1179 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1182 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1184 unsigned HOST_WIDE_INT int1l, int2l;
1185 HOST_WIDE_INT int1h, int2h;
1186 unsigned HOST_WIDE_INT low;
1188 unsigned HOST_WIDE_INT garbagel;
1189 HOST_WIDE_INT garbageh;
1191 tree type = TREE_TYPE (arg1);
1192 int uns = TREE_UNSIGNED (type);
1194 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1196 int no_overflow = 0;
1198 int1l = TREE_INT_CST_LOW (arg1);
1199 int1h = TREE_INT_CST_HIGH (arg1);
1200 int2l = TREE_INT_CST_LOW (arg2);
1201 int2h = TREE_INT_CST_HIGH (arg2);
1206 low = int1l | int2l, hi = int1h | int2h;
1210 low = int1l ^ int2l, hi = int1h ^ int2h;
1214 low = int1l & int2l, hi = int1h & int2h;
1220 /* It's unclear from the C standard whether shifts can overflow.
1221 The following code ignores overflow; perhaps a C standard
1222 interpretation ruling is needed. */
1223 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1231 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1236 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1240 neg_double (int2l, int2h, &low, &hi);
1241 add_double (int1l, int1h, low, hi, &low, &hi);
1242 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1246 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1249 case TRUNC_DIV_EXPR:
1250 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1251 case EXACT_DIV_EXPR:
1252 /* This is a shortcut for a common special case. */
1253 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1254 && ! TREE_CONSTANT_OVERFLOW (arg1)
1255 && ! TREE_CONSTANT_OVERFLOW (arg2)
1256 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1258 if (code == CEIL_DIV_EXPR)
1261 low = int1l / int2l, hi = 0;
1265 /* ... fall through ... */
1267 case ROUND_DIV_EXPR:
1268 if (int2h == 0 && int2l == 1)
1270 low = int1l, hi = int1h;
1273 if (int1l == int2l && int1h == int2h
1274 && ! (int1l == 0 && int1h == 0))
1279 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1280 &low, &hi, &garbagel, &garbageh);
1283 case TRUNC_MOD_EXPR:
1284 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1285 /* This is a shortcut for a common special case. */
1286 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1287 && ! TREE_CONSTANT_OVERFLOW (arg1)
1288 && ! TREE_CONSTANT_OVERFLOW (arg2)
1289 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1291 if (code == CEIL_MOD_EXPR)
1293 low = int1l % int2l, hi = 0;
1297 /* ... fall through ... */
1299 case ROUND_MOD_EXPR:
1300 overflow = div_and_round_double (code, uns,
1301 int1l, int1h, int2l, int2h,
1302 &garbagel, &garbageh, &low, &hi);
1308 low = (((unsigned HOST_WIDE_INT) int1h
1309 < (unsigned HOST_WIDE_INT) int2h)
1310 || (((unsigned HOST_WIDE_INT) int1h
1311 == (unsigned HOST_WIDE_INT) int2h)
1314 low = (int1h < int2h
1315 || (int1h == int2h && int1l < int2l));
1317 if (low == (code == MIN_EXPR))
1318 low = int1l, hi = int1h;
1320 low = int2l, hi = int2h;
1327 /* If this is for a sizetype, can be represented as one (signed)
1328 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1331 && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1332 || (hi == -1 && (HOST_WIDE_INT) low < 0))
1333 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1334 return size_int_type_wide (low, type);
1337 t = build_int_2 (low, hi);
1338 TREE_TYPE (t) = TREE_TYPE (arg1);
1343 ? (!uns || is_sizetype) && overflow
1344 : (force_fit_type (t, (!uns || is_sizetype) && overflow)
1346 | TREE_OVERFLOW (arg1)
1347 | TREE_OVERFLOW (arg2));
1349 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1350 So check if force_fit_type truncated the value. */
1352 && ! TREE_OVERFLOW (t)
1353 && (TREE_INT_CST_HIGH (t) != hi
1354 || TREE_INT_CST_LOW (t) != low))
1355 TREE_OVERFLOW (t) = 1;
1357 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1358 | TREE_CONSTANT_OVERFLOW (arg1)
1359 | TREE_CONSTANT_OVERFLOW (arg2));
1363 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1364 constant. We assume ARG1 and ARG2 have the same data type, or at least
1365 are the same kind of constant and the same machine mode.
1367 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1370 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1375 if (TREE_CODE (arg1) == INTEGER_CST)
1376 return int_const_binop (code, arg1, arg2, notrunc);
1378 if (TREE_CODE (arg1) == REAL_CST)
1380 enum machine_mode mode;
1383 REAL_VALUE_TYPE value;
1386 d1 = TREE_REAL_CST (arg1);
1387 d2 = TREE_REAL_CST (arg2);
1389 type = TREE_TYPE (arg1);
1390 mode = TYPE_MODE (type);
1392 /* Don't perform operation if we honor signaling NaNs and
1393 either operand is a NaN. */
1394 if (HONOR_SNANS (mode)
1395 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1398 /* Don't perform operation if it would raise a division
1399 by zero exception. */
1400 if (code == RDIV_EXPR
1401 && REAL_VALUES_EQUAL (d2, dconst0)
1402 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1405 /* If either operand is a NaN, just return it. Otherwise, set up
1406 for floating-point trap; we return an overflow. */
1407 if (REAL_VALUE_ISNAN (d1))
1409 else if (REAL_VALUE_ISNAN (d2))
1412 REAL_ARITHMETIC (value, code, d1, d2);
1414 t = build_real (type, real_value_truncate (mode, value));
1417 = (force_fit_type (t, 0)
1418 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1419 TREE_CONSTANT_OVERFLOW (t)
1421 | TREE_CONSTANT_OVERFLOW (arg1)
1422 | TREE_CONSTANT_OVERFLOW (arg2);
1425 if (TREE_CODE (arg1) == COMPLEX_CST)
1427 tree type = TREE_TYPE (arg1);
1428 tree r1 = TREE_REALPART (arg1);
1429 tree i1 = TREE_IMAGPART (arg1);
1430 tree r2 = TREE_REALPART (arg2);
1431 tree i2 = TREE_IMAGPART (arg2);
1437 t = build_complex (type,
1438 const_binop (PLUS_EXPR, r1, r2, notrunc),
1439 const_binop (PLUS_EXPR, i1, i2, notrunc));
1443 t = build_complex (type,
1444 const_binop (MINUS_EXPR, r1, r2, notrunc),
1445 const_binop (MINUS_EXPR, i1, i2, notrunc));
1449 t = build_complex (type,
1450 const_binop (MINUS_EXPR,
1451 const_binop (MULT_EXPR,
1453 const_binop (MULT_EXPR,
1456 const_binop (PLUS_EXPR,
1457 const_binop (MULT_EXPR,
1459 const_binop (MULT_EXPR,
1467 = const_binop (PLUS_EXPR,
1468 const_binop (MULT_EXPR, r2, r2, notrunc),
1469 const_binop (MULT_EXPR, i2, i2, notrunc),
1472 t = build_complex (type,
1474 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1475 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1476 const_binop (PLUS_EXPR,
1477 const_binop (MULT_EXPR, r1, r2,
1479 const_binop (MULT_EXPR, i1, i2,
1482 magsquared, notrunc),
1484 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1485 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1486 const_binop (MINUS_EXPR,
1487 const_binop (MULT_EXPR, i1, r2,
1489 const_binop (MULT_EXPR, r1, i2,
1492 magsquared, notrunc));
1504 /* These are the hash table functions for the hash table of INTEGER_CST
1505 nodes of a sizetype. */
1507 /* Return the hash code code X, an INTEGER_CST. */
1510 size_htab_hash (const void *x)
1514 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1515 ^ htab_hash_pointer (TREE_TYPE (t))
1516 ^ (TREE_OVERFLOW (t) << 20));
1519 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1520 is the same as that given by *Y, which is the same. */
1523 size_htab_eq (const void *x, const void *y)
1528 return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1529 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1530 && TREE_TYPE (xt) == TREE_TYPE (yt)
1531 && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1534 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1535 bits are given by NUMBER and of the sizetype represented by KIND. */
1538 size_int_wide (HOST_WIDE_INT number, enum size_type_kind kind)
1540 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1543 /* Likewise, but the desired type is specified explicitly. */
1545 static GTY (()) tree new_const;
1546 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
1550 size_int_type_wide (HOST_WIDE_INT number, tree type)
1556 size_htab = htab_create_ggc (1024, size_htab_hash, size_htab_eq, NULL);
1557 new_const = make_node (INTEGER_CST);
1560 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1561 hash table, we return the value from the hash table. Otherwise, we
1562 place that in the hash table and make a new node for the next time. */
1563 TREE_INT_CST_LOW (new_const) = number;
1564 TREE_INT_CST_HIGH (new_const) = number < 0 ? -1 : 0;
1565 TREE_TYPE (new_const) = type;
1566 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const)
1567 = force_fit_type (new_const, 0);
1569 slot = htab_find_slot (size_htab, new_const, INSERT);
1575 new_const = make_node (INTEGER_CST);
1579 return (tree) *slot;
1582 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1583 is a tree code. The type of the result is taken from the operands.
1584 Both must be the same type integer type and it must be a size type.
1585 If the operands are constant, so is the result. */
1588 size_binop (enum tree_code code, tree arg0, tree arg1)
1590 tree type = TREE_TYPE (arg0);
1592 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1593 || type != TREE_TYPE (arg1))
1596 /* Handle the special case of two integer constants faster. */
1597 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1599 /* And some specific cases even faster than that. */
1600 if (code == PLUS_EXPR && integer_zerop (arg0))
1602 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1603 && integer_zerop (arg1))
1605 else if (code == MULT_EXPR && integer_onep (arg0))
1608 /* Handle general case of two integer constants. */
1609 return int_const_binop (code, arg0, arg1, 0);
1612 if (arg0 == error_mark_node || arg1 == error_mark_node)
1613 return error_mark_node;
1615 return fold (build (code, type, arg0, arg1));
1618 /* Given two values, either both of sizetype or both of bitsizetype,
1619 compute the difference between the two values. Return the value
1620 in signed type corresponding to the type of the operands. */
1623 size_diffop (tree arg0, tree arg1)
1625 tree type = TREE_TYPE (arg0);
1628 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1629 || type != TREE_TYPE (arg1))
1632 /* If the type is already signed, just do the simple thing. */
1633 if (! TREE_UNSIGNED (type))
1634 return size_binop (MINUS_EXPR, arg0, arg1);
1636 ctype = (type == bitsizetype || type == ubitsizetype
1637 ? sbitsizetype : ssizetype);
1639 /* If either operand is not a constant, do the conversions to the signed
1640 type and subtract. The hardware will do the right thing with any
1641 overflow in the subtraction. */
1642 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1643 return size_binop (MINUS_EXPR, convert (ctype, arg0),
1644 convert (ctype, arg1));
1646 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1647 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1648 overflow) and negate (which can't either). Special-case a result
1649 of zero while we're here. */
1650 if (tree_int_cst_equal (arg0, arg1))
1651 return convert (ctype, integer_zero_node);
1652 else if (tree_int_cst_lt (arg1, arg0))
1653 return convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1655 return size_binop (MINUS_EXPR, convert (ctype, integer_zero_node),
1656 convert (ctype, size_binop (MINUS_EXPR, arg1, arg0)));
1660 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1661 type TYPE. If no simplification can be done return NULL_TREE. */
1664 fold_convert_const (enum tree_code code, tree type, tree arg1)
1669 if (TREE_TYPE (arg1) == type)
1672 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1674 if (TREE_CODE (arg1) == INTEGER_CST)
1676 /* If we would build a constant wider than GCC supports,
1677 leave the conversion unfolded. */
1678 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1681 /* If we are trying to make a sizetype for a small integer, use
1682 size_int to pick up cached types to reduce duplicate nodes. */
1683 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1684 && !TREE_CONSTANT_OVERFLOW (arg1)
1685 && compare_tree_int (arg1, 10000) < 0)
1686 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
1688 /* Given an integer constant, make new constant with new type,
1689 appropriately sign-extended or truncated. */
1690 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1691 TREE_INT_CST_HIGH (arg1));
1692 TREE_TYPE (t) = type;
1693 /* Indicate an overflow if (1) ARG1 already overflowed,
1694 or (2) force_fit_type indicates an overflow.
1695 Tell force_fit_type that an overflow has already occurred
1696 if ARG1 is a too-large unsigned value and T is signed.
1697 But don't indicate an overflow if converting a pointer. */
1699 = ((force_fit_type (t,
1700 (TREE_INT_CST_HIGH (arg1) < 0
1701 && (TREE_UNSIGNED (type)
1702 < TREE_UNSIGNED (TREE_TYPE (arg1)))))
1703 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
1704 || TREE_OVERFLOW (arg1));
1705 TREE_CONSTANT_OVERFLOW (t)
1706 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1709 else if (TREE_CODE (arg1) == REAL_CST)
1711 /* The following code implements the floating point to integer
1712 conversion rules required by the Java Language Specification,
1713 that IEEE NaNs are mapped to zero and values that overflow
1714 the target precision saturate, i.e. values greater than
1715 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1716 are mapped to INT_MIN. These semantics are allowed by the
1717 C and C++ standards that simply state that the behavior of
1718 FP-to-integer conversion is unspecified upon overflow. */
1720 HOST_WIDE_INT high, low;
1723 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1727 case FIX_TRUNC_EXPR:
1728 real_trunc (&r, VOIDmode, &x);
1732 real_ceil (&r, VOIDmode, &x);
1735 case FIX_FLOOR_EXPR:
1736 real_floor (&r, VOIDmode, &x);
1743 /* If R is NaN, return zero and show we have an overflow. */
1744 if (REAL_VALUE_ISNAN (r))
1751 /* See if R is less than the lower bound or greater than the
1756 tree lt = TYPE_MIN_VALUE (type);
1757 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1758 if (REAL_VALUES_LESS (r, l))
1761 high = TREE_INT_CST_HIGH (lt);
1762 low = TREE_INT_CST_LOW (lt);
1768 tree ut = TYPE_MAX_VALUE (type);
1771 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1772 if (REAL_VALUES_LESS (u, r))
1775 high = TREE_INT_CST_HIGH (ut);
1776 low = TREE_INT_CST_LOW (ut);
1782 REAL_VALUE_TO_INT (&low, &high, r);
1784 t = build_int_2 (low, high);
1785 TREE_TYPE (t) = type;
1787 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1788 TREE_CONSTANT_OVERFLOW (t)
1789 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1793 else if (TREE_CODE (type) == REAL_TYPE)
1795 if (TREE_CODE (arg1) == INTEGER_CST)
1796 return build_real_from_int_cst (type, arg1);
1797 if (TREE_CODE (arg1) == REAL_CST)
1799 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1801 /* We make a copy of ARG1 so that we don't modify an
1802 existing constant tree. */
1803 t = copy_node (arg1);
1804 TREE_TYPE (t) = type;
1808 t = build_real (type,
1809 real_value_truncate (TYPE_MODE (type),
1810 TREE_REAL_CST (arg1)));
1813 = TREE_OVERFLOW (arg1) | force_fit_type (t, 0);
1814 TREE_CONSTANT_OVERFLOW (t)
1815 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1822 /* Return an expr equal to X but certainly not valid as an lvalue. */
1829 /* These things are certainly not lvalues. */
1830 if (TREE_CODE (x) == NON_LVALUE_EXPR
1831 || TREE_CODE (x) == INTEGER_CST
1832 || TREE_CODE (x) == REAL_CST
1833 || TREE_CODE (x) == STRING_CST
1834 || TREE_CODE (x) == ADDR_EXPR)
1837 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
1838 TREE_CONSTANT (result) = TREE_CONSTANT (x);
1842 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
1843 Zero means allow extended lvalues. */
1845 int pedantic_lvalues;
1847 /* When pedantic, return an expr equal to X but certainly not valid as a
1848 pedantic lvalue. Otherwise, return X. */
1851 pedantic_non_lvalue (tree x)
1853 if (pedantic_lvalues)
1854 return non_lvalue (x);
1859 /* Given a tree comparison code, return the code that is the logical inverse
1860 of the given code. It is not safe to do this for floating-point
1861 comparisons, except for NE_EXPR and EQ_EXPR. */
1863 static enum tree_code
1864 invert_tree_comparison (enum tree_code code)
1885 /* Similar, but return the comparison that results if the operands are
1886 swapped. This is safe for floating-point. */
1888 static enum tree_code
1889 swap_tree_comparison (enum tree_code code)
1910 /* Convert a comparison tree code from an enum tree_code representation
1911 into a compcode bit-based encoding. This function is the inverse of
1912 compcode_to_comparison. */
1915 comparison_to_compcode (enum tree_code code)
1936 /* Convert a compcode bit-based encoding of a comparison operator back
1937 to GCC's enum tree_code representation. This function is the
1938 inverse of comparison_to_compcode. */
1940 static enum tree_code
1941 compcode_to_comparison (int code)
1962 /* Return nonzero if CODE is a tree code that represents a truth value. */
1965 truth_value_p (enum tree_code code)
1967 return (TREE_CODE_CLASS (code) == '<'
1968 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
1969 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
1970 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
1973 /* Return nonzero if two operands (typically of the same tree node)
1974 are necessarily equal. If either argument has side-effects this
1975 function returns zero.
1977 If ONLY_CONST is nonzero, only return nonzero for constants.
1978 This function tests whether the operands are indistinguishable;
1979 it does not test whether they are equal using C's == operation.
1980 The distinction is important for IEEE floating point, because
1981 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
1982 (2) two NaNs may be indistinguishable, but NaN!=NaN.
1984 If ONLY_CONST is zero, a VAR_DECL is considered equal to itself
1985 even though it may hold multiple values during a function.
1986 This is because a GCC tree node guarantees that nothing else is
1987 executed between the evaluation of its "operands" (which may often
1988 be evaluated in arbitrary order). Hence if the operands themselves
1989 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
1990 same value in each operand/subexpression. Hence a zero value for
1991 ONLY_CONST assumes isochronic (or instantaneous) tree equivalence.
1992 If comparing arbitrary expression trees, such as from different
1993 statements, ONLY_CONST must usually be nonzero. */
1996 operand_equal_p (tree arg0, tree arg1, int only_const)
2000 /* If both types don't have the same signedness, then we can't consider
2001 them equal. We must check this before the STRIP_NOPS calls
2002 because they may change the signedness of the arguments. */
2003 if (TREE_UNSIGNED (TREE_TYPE (arg0)) != TREE_UNSIGNED (TREE_TYPE (arg1)))
2009 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2010 /* This is needed for conversions and for COMPONENT_REF.
2011 Might as well play it safe and always test this. */
2012 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2013 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2014 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2017 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2018 We don't care about side effects in that case because the SAVE_EXPR
2019 takes care of that for us. In all other cases, two expressions are
2020 equal if they have no side effects. If we have two identical
2021 expressions with side effects that should be treated the same due
2022 to the only side effects being identical SAVE_EXPR's, that will
2023 be detected in the recursive calls below. */
2024 if (arg0 == arg1 && ! only_const
2025 && (TREE_CODE (arg0) == SAVE_EXPR
2026 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2029 /* Next handle constant cases, those for which we can return 1 even
2030 if ONLY_CONST is set. */
2031 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2032 switch (TREE_CODE (arg0))
2035 return (! TREE_CONSTANT_OVERFLOW (arg0)
2036 && ! TREE_CONSTANT_OVERFLOW (arg1)
2037 && tree_int_cst_equal (arg0, arg1));
2040 return (! TREE_CONSTANT_OVERFLOW (arg0)
2041 && ! TREE_CONSTANT_OVERFLOW (arg1)
2042 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2043 TREE_REAL_CST (arg1)));
2049 if (TREE_CONSTANT_OVERFLOW (arg0)
2050 || TREE_CONSTANT_OVERFLOW (arg1))
2053 v1 = TREE_VECTOR_CST_ELTS (arg0);
2054 v2 = TREE_VECTOR_CST_ELTS (arg1);
2057 if (!operand_equal_p (v1, v2, only_const))
2059 v1 = TREE_CHAIN (v1);
2060 v2 = TREE_CHAIN (v2);
2067 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2069 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2073 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2074 && ! memcmp (TREE_STRING_POINTER (arg0),
2075 TREE_STRING_POINTER (arg1),
2076 TREE_STRING_LENGTH (arg0)));
2079 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2088 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2091 /* Two conversions are equal only if signedness and modes match. */
2092 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2093 && (TREE_UNSIGNED (TREE_TYPE (arg0))
2094 != TREE_UNSIGNED (TREE_TYPE (arg1))))
2097 return operand_equal_p (TREE_OPERAND (arg0, 0),
2098 TREE_OPERAND (arg1, 0), 0);
2102 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2103 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2107 /* For commutative ops, allow the other order. */
2108 return ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MULT_EXPR
2109 || TREE_CODE (arg0) == MIN_EXPR || TREE_CODE (arg0) == MAX_EXPR
2110 || TREE_CODE (arg0) == BIT_IOR_EXPR
2111 || TREE_CODE (arg0) == BIT_XOR_EXPR
2112 || TREE_CODE (arg0) == BIT_AND_EXPR
2113 || TREE_CODE (arg0) == NE_EXPR || TREE_CODE (arg0) == EQ_EXPR)
2114 && operand_equal_p (TREE_OPERAND (arg0, 0),
2115 TREE_OPERAND (arg1, 1), 0)
2116 && operand_equal_p (TREE_OPERAND (arg0, 1),
2117 TREE_OPERAND (arg1, 0), 0));
2120 /* If either of the pointer (or reference) expressions we are
2121 dereferencing contain a side effect, these cannot be equal. */
2122 if (TREE_SIDE_EFFECTS (arg0)
2123 || TREE_SIDE_EFFECTS (arg1))
2126 switch (TREE_CODE (arg0))
2129 return operand_equal_p (TREE_OPERAND (arg0, 0),
2130 TREE_OPERAND (arg1, 0), 0);
2134 case ARRAY_RANGE_REF:
2135 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2136 TREE_OPERAND (arg1, 0), 0)
2137 && operand_equal_p (TREE_OPERAND (arg0, 1),
2138 TREE_OPERAND (arg1, 1), 0));
2141 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2142 TREE_OPERAND (arg1, 0), 0)
2143 && operand_equal_p (TREE_OPERAND (arg0, 1),
2144 TREE_OPERAND (arg1, 1), 0)
2145 && operand_equal_p (TREE_OPERAND (arg0, 2),
2146 TREE_OPERAND (arg1, 2), 0));
2152 switch (TREE_CODE (arg0))
2155 case TRUTH_NOT_EXPR:
2156 return operand_equal_p (TREE_OPERAND (arg0, 0),
2157 TREE_OPERAND (arg1, 0), 0);
2160 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2163 /* If the CALL_EXPRs call different functions, then they
2164 clearly can not be equal. */
2165 if (! operand_equal_p (TREE_OPERAND (arg0, 0),
2166 TREE_OPERAND (arg1, 0), 0))
2169 /* Only consider const functions equivalent. */
2170 fndecl = get_callee_fndecl (arg0);
2171 if (fndecl == NULL_TREE
2172 || ! (flags_from_decl_or_type (fndecl) & ECF_CONST))
2175 /* Now see if all the arguments are the same. operand_equal_p
2176 does not handle TREE_LIST, so we walk the operands here
2177 feeding them to operand_equal_p. */
2178 arg0 = TREE_OPERAND (arg0, 1);
2179 arg1 = TREE_OPERAND (arg1, 1);
2180 while (arg0 && arg1)
2182 if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1), 0))
2185 arg0 = TREE_CHAIN (arg0);
2186 arg1 = TREE_CHAIN (arg1);
2189 /* If we get here and both argument lists are exhausted
2190 then the CALL_EXPRs are equal. */
2191 return ! (arg0 || arg1);
2198 /* Consider __builtin_sqrt equal to sqrt. */
2199 return TREE_CODE (arg0) == FUNCTION_DECL
2200 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2201 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2202 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1);
2209 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2210 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2212 When in doubt, return 0. */
2215 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2217 int unsignedp1, unsignedpo;
2218 tree primarg0, primarg1, primother;
2219 unsigned int correct_width;
2221 if (operand_equal_p (arg0, arg1, 0))
2224 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2225 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2228 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2229 and see if the inner values are the same. This removes any
2230 signedness comparison, which doesn't matter here. */
2231 primarg0 = arg0, primarg1 = arg1;
2232 STRIP_NOPS (primarg0);
2233 STRIP_NOPS (primarg1);
2234 if (operand_equal_p (primarg0, primarg1, 0))
2237 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2238 actual comparison operand, ARG0.
2240 First throw away any conversions to wider types
2241 already present in the operands. */
2243 primarg1 = get_narrower (arg1, &unsignedp1);
2244 primother = get_narrower (other, &unsignedpo);
2246 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2247 if (unsignedp1 == unsignedpo
2248 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2249 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2251 tree type = TREE_TYPE (arg0);
2253 /* Make sure shorter operand is extended the right way
2254 to match the longer operand. */
2255 primarg1 = convert ((*lang_hooks.types.signed_or_unsigned_type)
2256 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2258 if (operand_equal_p (arg0, convert (type, primarg1), 0))
2265 /* See if ARG is an expression that is either a comparison or is performing
2266 arithmetic on comparisons. The comparisons must only be comparing
2267 two different values, which will be stored in *CVAL1 and *CVAL2; if
2268 they are nonzero it means that some operands have already been found.
2269 No variables may be used anywhere else in the expression except in the
2270 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2271 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2273 If this is true, return 1. Otherwise, return zero. */
2276 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2278 enum tree_code code = TREE_CODE (arg);
2279 char class = TREE_CODE_CLASS (code);
2281 /* We can handle some of the 'e' cases here. */
2282 if (class == 'e' && code == TRUTH_NOT_EXPR)
2284 else if (class == 'e'
2285 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2286 || code == COMPOUND_EXPR))
2289 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2290 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2292 /* If we've already found a CVAL1 or CVAL2, this expression is
2293 two complex to handle. */
2294 if (*cval1 || *cval2)
2304 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2307 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2308 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2309 cval1, cval2, save_p));
2315 if (code == COND_EXPR)
2316 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2317 cval1, cval2, save_p)
2318 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2319 cval1, cval2, save_p)
2320 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2321 cval1, cval2, save_p));
2325 /* First see if we can handle the first operand, then the second. For
2326 the second operand, we know *CVAL1 can't be zero. It must be that
2327 one side of the comparison is each of the values; test for the
2328 case where this isn't true by failing if the two operands
2331 if (operand_equal_p (TREE_OPERAND (arg, 0),
2332 TREE_OPERAND (arg, 1), 0))
2336 *cval1 = TREE_OPERAND (arg, 0);
2337 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2339 else if (*cval2 == 0)
2340 *cval2 = TREE_OPERAND (arg, 0);
2341 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2346 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2348 else if (*cval2 == 0)
2349 *cval2 = TREE_OPERAND (arg, 1);
2350 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2362 /* ARG is a tree that is known to contain just arithmetic operations and
2363 comparisons. Evaluate the operations in the tree substituting NEW0 for
2364 any occurrence of OLD0 as an operand of a comparison and likewise for
2368 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2370 tree type = TREE_TYPE (arg);
2371 enum tree_code code = TREE_CODE (arg);
2372 char class = TREE_CODE_CLASS (code);
2374 /* We can handle some of the 'e' cases here. */
2375 if (class == 'e' && code == TRUTH_NOT_EXPR)
2377 else if (class == 'e'
2378 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2384 return fold (build1 (code, type,
2385 eval_subst (TREE_OPERAND (arg, 0),
2386 old0, new0, old1, new1)));
2389 return fold (build (code, type,
2390 eval_subst (TREE_OPERAND (arg, 0),
2391 old0, new0, old1, new1),
2392 eval_subst (TREE_OPERAND (arg, 1),
2393 old0, new0, old1, new1)));
2399 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2402 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2405 return fold (build (code, type,
2406 eval_subst (TREE_OPERAND (arg, 0),
2407 old0, new0, old1, new1),
2408 eval_subst (TREE_OPERAND (arg, 1),
2409 old0, new0, old1, new1),
2410 eval_subst (TREE_OPERAND (arg, 2),
2411 old0, new0, old1, new1)));
2415 /* Fall through - ??? */
2419 tree arg0 = TREE_OPERAND (arg, 0);
2420 tree arg1 = TREE_OPERAND (arg, 1);
2422 /* We need to check both for exact equality and tree equality. The
2423 former will be true if the operand has a side-effect. In that
2424 case, we know the operand occurred exactly once. */
2426 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2428 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2431 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2433 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2436 return fold (build (code, type, arg0, arg1));
2444 /* Return a tree for the case when the result of an expression is RESULT
2445 converted to TYPE and OMITTED was previously an operand of the expression
2446 but is now not needed (e.g., we folded OMITTED * 0).
2448 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2449 the conversion of RESULT to TYPE. */
2452 omit_one_operand (tree type, tree result, tree omitted)
2454 tree t = convert (type, result);
2456 if (TREE_SIDE_EFFECTS (omitted))
2457 return build (COMPOUND_EXPR, type, omitted, t);
2459 return non_lvalue (t);
2462 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2465 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2467 tree t = convert (type, result);
2469 if (TREE_SIDE_EFFECTS (omitted))
2470 return build (COMPOUND_EXPR, type, omitted, t);
2472 return pedantic_non_lvalue (t);
2475 /* Return a simplified tree node for the truth-negation of ARG. This
2476 never alters ARG itself. We assume that ARG is an operation that
2477 returns a truth value (0 or 1). */
2480 invert_truthvalue (tree arg)
2482 tree type = TREE_TYPE (arg);
2483 enum tree_code code = TREE_CODE (arg);
2485 if (code == ERROR_MARK)
2488 /* If this is a comparison, we can simply invert it, except for
2489 floating-point non-equality comparisons, in which case we just
2490 enclose a TRUTH_NOT_EXPR around what we have. */
2492 if (TREE_CODE_CLASS (code) == '<')
2494 if (FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
2495 && !flag_unsafe_math_optimizations
2498 return build1 (TRUTH_NOT_EXPR, type, arg);
2500 return build (invert_tree_comparison (code), type,
2501 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2507 return convert (type, build_int_2 (integer_zerop (arg), 0));
2509 case TRUTH_AND_EXPR:
2510 return build (TRUTH_OR_EXPR, type,
2511 invert_truthvalue (TREE_OPERAND (arg, 0)),
2512 invert_truthvalue (TREE_OPERAND (arg, 1)));
2515 return build (TRUTH_AND_EXPR, type,
2516 invert_truthvalue (TREE_OPERAND (arg, 0)),
2517 invert_truthvalue (TREE_OPERAND (arg, 1)));
2519 case TRUTH_XOR_EXPR:
2520 /* Here we can invert either operand. We invert the first operand
2521 unless the second operand is a TRUTH_NOT_EXPR in which case our
2522 result is the XOR of the first operand with the inside of the
2523 negation of the second operand. */
2525 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2526 return build (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2527 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2529 return build (TRUTH_XOR_EXPR, type,
2530 invert_truthvalue (TREE_OPERAND (arg, 0)),
2531 TREE_OPERAND (arg, 1));
2533 case TRUTH_ANDIF_EXPR:
2534 return build (TRUTH_ORIF_EXPR, type,
2535 invert_truthvalue (TREE_OPERAND (arg, 0)),
2536 invert_truthvalue (TREE_OPERAND (arg, 1)));
2538 case TRUTH_ORIF_EXPR:
2539 return build (TRUTH_ANDIF_EXPR, type,
2540 invert_truthvalue (TREE_OPERAND (arg, 0)),
2541 invert_truthvalue (TREE_OPERAND (arg, 1)));
2543 case TRUTH_NOT_EXPR:
2544 return TREE_OPERAND (arg, 0);
2547 return build (COND_EXPR, type, TREE_OPERAND (arg, 0),
2548 invert_truthvalue (TREE_OPERAND (arg, 1)),
2549 invert_truthvalue (TREE_OPERAND (arg, 2)));
2552 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2553 invert_truthvalue (TREE_OPERAND (arg, 1)));
2555 case WITH_RECORD_EXPR:
2556 return build (WITH_RECORD_EXPR, type,
2557 invert_truthvalue (TREE_OPERAND (arg, 0)),
2558 TREE_OPERAND (arg, 1));
2560 case NON_LVALUE_EXPR:
2561 return invert_truthvalue (TREE_OPERAND (arg, 0));
2566 return build1 (TREE_CODE (arg), type,
2567 invert_truthvalue (TREE_OPERAND (arg, 0)));
2570 if (!integer_onep (TREE_OPERAND (arg, 1)))
2572 return build (EQ_EXPR, type, arg, convert (type, integer_zero_node));
2575 return build1 (TRUTH_NOT_EXPR, type, arg);
2577 case CLEANUP_POINT_EXPR:
2578 return build1 (CLEANUP_POINT_EXPR, type,
2579 invert_truthvalue (TREE_OPERAND (arg, 0)));
2584 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2586 return build1 (TRUTH_NOT_EXPR, type, arg);
2589 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2590 operands are another bit-wise operation with a common input. If so,
2591 distribute the bit operations to save an operation and possibly two if
2592 constants are involved. For example, convert
2593 (A | B) & (A | C) into A | (B & C)
2594 Further simplification will occur if B and C are constants.
2596 If this optimization cannot be done, 0 will be returned. */
2599 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
2604 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2605 || TREE_CODE (arg0) == code
2606 || (TREE_CODE (arg0) != BIT_AND_EXPR
2607 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2610 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2612 common = TREE_OPERAND (arg0, 0);
2613 left = TREE_OPERAND (arg0, 1);
2614 right = TREE_OPERAND (arg1, 1);
2616 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2618 common = TREE_OPERAND (arg0, 0);
2619 left = TREE_OPERAND (arg0, 1);
2620 right = TREE_OPERAND (arg1, 0);
2622 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2624 common = TREE_OPERAND (arg0, 1);
2625 left = TREE_OPERAND (arg0, 0);
2626 right = TREE_OPERAND (arg1, 1);
2628 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2630 common = TREE_OPERAND (arg0, 1);
2631 left = TREE_OPERAND (arg0, 0);
2632 right = TREE_OPERAND (arg1, 0);
2637 return fold (build (TREE_CODE (arg0), type, common,
2638 fold (build (code, type, left, right))));
2641 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
2642 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
2645 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
2648 tree result = build (BIT_FIELD_REF, type, inner,
2649 size_int (bitsize), bitsize_int (bitpos));
2651 TREE_UNSIGNED (result) = unsignedp;
2656 /* Optimize a bit-field compare.
2658 There are two cases: First is a compare against a constant and the
2659 second is a comparison of two items where the fields are at the same
2660 bit position relative to the start of a chunk (byte, halfword, word)
2661 large enough to contain it. In these cases we can avoid the shift
2662 implicit in bitfield extractions.
2664 For constants, we emit a compare of the shifted constant with the
2665 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
2666 compared. For two fields at the same position, we do the ANDs with the
2667 similar mask and compare the result of the ANDs.
2669 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
2670 COMPARE_TYPE is the type of the comparison, and LHS and RHS
2671 are the left and right operands of the comparison, respectively.
2673 If the optimization described above can be done, we return the resulting
2674 tree. Otherwise we return zero. */
2677 optimize_bit_field_compare (enum tree_code code, tree compare_type,
2680 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
2681 tree type = TREE_TYPE (lhs);
2682 tree signed_type, unsigned_type;
2683 int const_p = TREE_CODE (rhs) == INTEGER_CST;
2684 enum machine_mode lmode, rmode, nmode;
2685 int lunsignedp, runsignedp;
2686 int lvolatilep = 0, rvolatilep = 0;
2687 tree linner, rinner = NULL_TREE;
2691 /* Get all the information about the extractions being done. If the bit size
2692 if the same as the size of the underlying object, we aren't doing an
2693 extraction at all and so can do nothing. We also don't want to
2694 do anything if the inner expression is a PLACEHOLDER_EXPR since we
2695 then will no longer be able to replace it. */
2696 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
2697 &lunsignedp, &lvolatilep);
2698 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
2699 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
2704 /* If this is not a constant, we can only do something if bit positions,
2705 sizes, and signedness are the same. */
2706 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
2707 &runsignedp, &rvolatilep);
2709 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
2710 || lunsignedp != runsignedp || offset != 0
2711 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
2715 /* See if we can find a mode to refer to this field. We should be able to,
2716 but fail if we can't. */
2717 nmode = get_best_mode (lbitsize, lbitpos,
2718 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
2719 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
2720 TYPE_ALIGN (TREE_TYPE (rinner))),
2721 word_mode, lvolatilep || rvolatilep);
2722 if (nmode == VOIDmode)
2725 /* Set signed and unsigned types of the precision of this mode for the
2727 signed_type = (*lang_hooks.types.type_for_mode) (nmode, 0);
2728 unsigned_type = (*lang_hooks.types.type_for_mode) (nmode, 1);
2730 /* Compute the bit position and size for the new reference and our offset
2731 within it. If the new reference is the same size as the original, we
2732 won't optimize anything, so return zero. */
2733 nbitsize = GET_MODE_BITSIZE (nmode);
2734 nbitpos = lbitpos & ~ (nbitsize - 1);
2736 if (nbitsize == lbitsize)
2739 if (BYTES_BIG_ENDIAN)
2740 lbitpos = nbitsize - lbitsize - lbitpos;
2742 /* Make the mask to be used against the extracted field. */
2743 mask = build_int_2 (~0, ~0);
2744 TREE_TYPE (mask) = unsigned_type;
2745 force_fit_type (mask, 0);
2746 mask = convert (unsigned_type, mask);
2747 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
2748 mask = const_binop (RSHIFT_EXPR, mask,
2749 size_int (nbitsize - lbitsize - lbitpos), 0);
2752 /* If not comparing with constant, just rework the comparison
2754 return build (code, compare_type,
2755 build (BIT_AND_EXPR, unsigned_type,
2756 make_bit_field_ref (linner, unsigned_type,
2757 nbitsize, nbitpos, 1),
2759 build (BIT_AND_EXPR, unsigned_type,
2760 make_bit_field_ref (rinner, unsigned_type,
2761 nbitsize, nbitpos, 1),
2764 /* Otherwise, we are handling the constant case. See if the constant is too
2765 big for the field. Warn and return a tree of for 0 (false) if so. We do
2766 this not only for its own sake, but to avoid having to test for this
2767 error case below. If we didn't, we might generate wrong code.
2769 For unsigned fields, the constant shifted right by the field length should
2770 be all zero. For signed fields, the high-order bits should agree with
2775 if (! integer_zerop (const_binop (RSHIFT_EXPR,
2776 convert (unsigned_type, rhs),
2777 size_int (lbitsize), 0)))
2779 warning ("comparison is always %d due to width of bit-field",
2781 return convert (compare_type,
2783 ? integer_one_node : integer_zero_node));
2788 tree tem = const_binop (RSHIFT_EXPR, convert (signed_type, rhs),
2789 size_int (lbitsize - 1), 0);
2790 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
2792 warning ("comparison is always %d due to width of bit-field",
2794 return convert (compare_type,
2796 ? integer_one_node : integer_zero_node));
2800 /* Single-bit compares should always be against zero. */
2801 if (lbitsize == 1 && ! integer_zerop (rhs))
2803 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
2804 rhs = convert (type, integer_zero_node);
2807 /* Make a new bitfield reference, shift the constant over the
2808 appropriate number of bits and mask it with the computed mask
2809 (in case this was a signed field). If we changed it, make a new one. */
2810 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
2813 TREE_SIDE_EFFECTS (lhs) = 1;
2814 TREE_THIS_VOLATILE (lhs) = 1;
2817 rhs = fold (const_binop (BIT_AND_EXPR,
2818 const_binop (LSHIFT_EXPR,
2819 convert (unsigned_type, rhs),
2820 size_int (lbitpos), 0),
2823 return build (code, compare_type,
2824 build (BIT_AND_EXPR, unsigned_type, lhs, mask),
2828 /* Subroutine for fold_truthop: decode a field reference.
2830 If EXP is a comparison reference, we return the innermost reference.
2832 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
2833 set to the starting bit number.
2835 If the innermost field can be completely contained in a mode-sized
2836 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
2838 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
2839 otherwise it is not changed.
2841 *PUNSIGNEDP is set to the signedness of the field.
2843 *PMASK is set to the mask used. This is either contained in a
2844 BIT_AND_EXPR or derived from the width of the field.
2846 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
2848 Return 0 if this is not a component reference or is one that we can't
2849 do anything with. */
2852 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
2853 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
2854 int *punsignedp, int *pvolatilep,
2855 tree *pmask, tree *pand_mask)
2857 tree outer_type = 0;
2859 tree mask, inner, offset;
2861 unsigned int precision;
2863 /* All the optimizations using this function assume integer fields.
2864 There are problems with FP fields since the type_for_size call
2865 below can fail for, e.g., XFmode. */
2866 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
2869 /* We are interested in the bare arrangement of bits, so strip everything
2870 that doesn't affect the machine mode. However, record the type of the
2871 outermost expression if it may matter below. */
2872 if (TREE_CODE (exp) == NOP_EXPR
2873 || TREE_CODE (exp) == CONVERT_EXPR
2874 || TREE_CODE (exp) == NON_LVALUE_EXPR)
2875 outer_type = TREE_TYPE (exp);
2878 if (TREE_CODE (exp) == BIT_AND_EXPR)
2880 and_mask = TREE_OPERAND (exp, 1);
2881 exp = TREE_OPERAND (exp, 0);
2882 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
2883 if (TREE_CODE (and_mask) != INTEGER_CST)
2887 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
2888 punsignedp, pvolatilep);
2889 if ((inner == exp && and_mask == 0)
2890 || *pbitsize < 0 || offset != 0
2891 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
2894 /* If the number of bits in the reference is the same as the bitsize of
2895 the outer type, then the outer type gives the signedness. Otherwise
2896 (in case of a small bitfield) the signedness is unchanged. */
2897 if (outer_type && *pbitsize == tree_low_cst (TYPE_SIZE (outer_type), 1))
2898 *punsignedp = TREE_UNSIGNED (outer_type);
2900 /* Compute the mask to access the bitfield. */
2901 unsigned_type = (*lang_hooks.types.type_for_size) (*pbitsize, 1);
2902 precision = TYPE_PRECISION (unsigned_type);
2904 mask = build_int_2 (~0, ~0);
2905 TREE_TYPE (mask) = unsigned_type;
2906 force_fit_type (mask, 0);
2907 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2908 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
2910 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
2912 mask = fold (build (BIT_AND_EXPR, unsigned_type,
2913 convert (unsigned_type, and_mask), mask));
2916 *pand_mask = and_mask;
2920 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
2924 all_ones_mask_p (tree mask, int size)
2926 tree type = TREE_TYPE (mask);
2927 unsigned int precision = TYPE_PRECISION (type);
2930 tmask = build_int_2 (~0, ~0);
2931 TREE_TYPE (tmask) = (*lang_hooks.types.signed_type) (type);
2932 force_fit_type (tmask, 0);
2934 tree_int_cst_equal (mask,
2935 const_binop (RSHIFT_EXPR,
2936 const_binop (LSHIFT_EXPR, tmask,
2937 size_int (precision - size),
2939 size_int (precision - size), 0));
2942 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
2943 represents the sign bit of EXP's type. If EXP represents a sign
2944 or zero extension, also test VAL against the unextended type.
2945 The return value is the (sub)expression whose sign bit is VAL,
2946 or NULL_TREE otherwise. */
2949 sign_bit_p (tree exp, tree val)
2951 unsigned HOST_WIDE_INT mask_lo, lo;
2952 HOST_WIDE_INT mask_hi, hi;
2956 /* Tree EXP must have an integral type. */
2957 t = TREE_TYPE (exp);
2958 if (! INTEGRAL_TYPE_P (t))
2961 /* Tree VAL must be an integer constant. */
2962 if (TREE_CODE (val) != INTEGER_CST
2963 || TREE_CONSTANT_OVERFLOW (val))
2966 width = TYPE_PRECISION (t);
2967 if (width > HOST_BITS_PER_WIDE_INT)
2969 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
2972 mask_hi = ((unsigned HOST_WIDE_INT) -1
2973 >> (2 * HOST_BITS_PER_WIDE_INT - width));
2979 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
2982 mask_lo = ((unsigned HOST_WIDE_INT) -1
2983 >> (HOST_BITS_PER_WIDE_INT - width));
2986 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
2987 treat VAL as if it were unsigned. */
2988 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
2989 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
2992 /* Handle extension from a narrower type. */
2993 if (TREE_CODE (exp) == NOP_EXPR
2994 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
2995 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3000 /* Subroutine for fold_truthop: determine if an operand is simple enough
3001 to be evaluated unconditionally. */
3004 simple_operand_p (tree exp)
3006 /* Strip any conversions that don't change the machine mode. */
3007 while ((TREE_CODE (exp) == NOP_EXPR
3008 || TREE_CODE (exp) == CONVERT_EXPR)
3009 && (TYPE_MODE (TREE_TYPE (exp))
3010 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3011 exp = TREE_OPERAND (exp, 0);
3013 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3015 && ! TREE_ADDRESSABLE (exp)
3016 && ! TREE_THIS_VOLATILE (exp)
3017 && ! DECL_NONLOCAL (exp)
3018 /* Don't regard global variables as simple. They may be
3019 allocated in ways unknown to the compiler (shared memory,
3020 #pragma weak, etc). */
3021 && ! TREE_PUBLIC (exp)
3022 && ! DECL_EXTERNAL (exp)
3023 /* Loading a static variable is unduly expensive, but global
3024 registers aren't expensive. */
3025 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3028 /* The following functions are subroutines to fold_range_test and allow it to
3029 try to change a logical combination of comparisons into a range test.
3032 X == 2 || X == 3 || X == 4 || X == 5
3036 (unsigned) (X - 2) <= 3
3038 We describe each set of comparisons as being either inside or outside
3039 a range, using a variable named like IN_P, and then describe the
3040 range with a lower and upper bound. If one of the bounds is omitted,
3041 it represents either the highest or lowest value of the type.
3043 In the comments below, we represent a range by two numbers in brackets
3044 preceded by a "+" to designate being inside that range, or a "-" to
3045 designate being outside that range, so the condition can be inverted by
3046 flipping the prefix. An omitted bound is represented by a "-". For
3047 example, "- [-, 10]" means being outside the range starting at the lowest
3048 possible value and ending at 10, in other words, being greater than 10.
3049 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3052 We set up things so that the missing bounds are handled in a consistent
3053 manner so neither a missing bound nor "true" and "false" need to be
3054 handled using a special case. */
3056 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3057 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3058 and UPPER1_P are nonzero if the respective argument is an upper bound
3059 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3060 must be specified for a comparison. ARG1 will be converted to ARG0's
3061 type if both are specified. */
3064 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3065 tree arg1, int upper1_p)
3071 /* If neither arg represents infinity, do the normal operation.
3072 Else, if not a comparison, return infinity. Else handle the special
3073 comparison rules. Note that most of the cases below won't occur, but
3074 are handled for consistency. */
3076 if (arg0 != 0 && arg1 != 0)
3078 tem = fold (build (code, type != 0 ? type : TREE_TYPE (arg0),
3079 arg0, convert (TREE_TYPE (arg0), arg1)));
3081 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3084 if (TREE_CODE_CLASS (code) != '<')
3087 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3088 for neither. In real maths, we cannot assume open ended ranges are
3089 the same. But, this is computer arithmetic, where numbers are finite.
3090 We can therefore make the transformation of any unbounded range with
3091 the value Z, Z being greater than any representable number. This permits
3092 us to treat unbounded ranges as equal. */
3093 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3094 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3098 result = sgn0 == sgn1;
3101 result = sgn0 != sgn1;
3104 result = sgn0 < sgn1;
3107 result = sgn0 <= sgn1;
3110 result = sgn0 > sgn1;
3113 result = sgn0 >= sgn1;
3119 return convert (type, result ? integer_one_node : integer_zero_node);
3122 /* Given EXP, a logical expression, set the range it is testing into
3123 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3124 actually being tested. *PLOW and *PHIGH will be made of the same type
3125 as the returned expression. If EXP is not a comparison, we will most
3126 likely not be returning a useful value and range. */
3129 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3131 enum tree_code code;
3132 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3133 tree orig_type = NULL_TREE;
3135 tree low, high, n_low, n_high;
3137 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3138 and see if we can refine the range. Some of the cases below may not
3139 happen, but it doesn't seem worth worrying about this. We "continue"
3140 the outer loop when we've changed something; otherwise we "break"
3141 the switch, which will "break" the while. */
3143 in_p = 0, low = high = convert (TREE_TYPE (exp), integer_zero_node);
3147 code = TREE_CODE (exp);
3149 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3151 if (first_rtl_op (code) > 0)
3152 arg0 = TREE_OPERAND (exp, 0);
3153 if (TREE_CODE_CLASS (code) == '<'
3154 || TREE_CODE_CLASS (code) == '1'
3155 || TREE_CODE_CLASS (code) == '2')
3156 type = TREE_TYPE (arg0);
3157 if (TREE_CODE_CLASS (code) == '2'
3158 || TREE_CODE_CLASS (code) == '<'
3159 || (TREE_CODE_CLASS (code) == 'e'
3160 && TREE_CODE_LENGTH (code) > 1))
3161 arg1 = TREE_OPERAND (exp, 1);
3164 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3165 lose a cast by accident. */
3166 if (type != NULL_TREE && orig_type == NULL_TREE)
3171 case TRUTH_NOT_EXPR:
3172 in_p = ! in_p, exp = arg0;
3175 case EQ_EXPR: case NE_EXPR:
3176 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3177 /* We can only do something if the range is testing for zero
3178 and if the second operand is an integer constant. Note that
3179 saying something is "in" the range we make is done by
3180 complementing IN_P since it will set in the initial case of
3181 being not equal to zero; "out" is leaving it alone. */
3182 if (low == 0 || high == 0
3183 || ! integer_zerop (low) || ! integer_zerop (high)
3184 || TREE_CODE (arg1) != INTEGER_CST)
3189 case NE_EXPR: /* - [c, c] */
3192 case EQ_EXPR: /* + [c, c] */
3193 in_p = ! in_p, low = high = arg1;
3195 case GT_EXPR: /* - [-, c] */
3196 low = 0, high = arg1;
3198 case GE_EXPR: /* + [c, -] */
3199 in_p = ! in_p, low = arg1, high = 0;
3201 case LT_EXPR: /* - [c, -] */
3202 low = arg1, high = 0;
3204 case LE_EXPR: /* + [-, c] */
3205 in_p = ! in_p, low = 0, high = arg1;
3213 /* If this is an unsigned comparison, we also know that EXP is
3214 greater than or equal to zero. We base the range tests we make
3215 on that fact, so we record it here so we can parse existing
3217 if (TREE_UNSIGNED (type) && (low == 0 || high == 0))
3219 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3220 1, convert (type, integer_zero_node),
3224 in_p = n_in_p, low = n_low, high = n_high;
3226 /* If the high bound is missing, but we have a nonzero low
3227 bound, reverse the range so it goes from zero to the low bound
3229 if (high == 0 && low && ! integer_zerop (low))
3232 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3233 integer_one_node, 0);
3234 low = convert (type, integer_zero_node);
3240 /* (-x) IN [a,b] -> x in [-b, -a] */
3241 n_low = range_binop (MINUS_EXPR, type,
3242 convert (type, integer_zero_node), 0, high, 1);
3243 n_high = range_binop (MINUS_EXPR, type,
3244 convert (type, integer_zero_node), 0, low, 0);
3245 low = n_low, high = n_high;
3251 exp = build (MINUS_EXPR, type, negate_expr (arg0),
3252 convert (type, integer_one_node));
3255 case PLUS_EXPR: case MINUS_EXPR:
3256 if (TREE_CODE (arg1) != INTEGER_CST)
3259 /* If EXP is signed, any overflow in the computation is undefined,
3260 so we don't worry about it so long as our computations on
3261 the bounds don't overflow. For unsigned, overflow is defined
3262 and this is exactly the right thing. */
3263 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3264 type, low, 0, arg1, 0);
3265 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3266 type, high, 1, arg1, 0);
3267 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3268 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3271 /* Check for an unsigned range which has wrapped around the maximum
3272 value thus making n_high < n_low, and normalize it. */
3273 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3275 low = range_binop (PLUS_EXPR, type, n_high, 0,
3276 integer_one_node, 0);
3277 high = range_binop (MINUS_EXPR, type, n_low, 0,
3278 integer_one_node, 0);
3280 /* If the range is of the form +/- [ x+1, x ], we won't
3281 be able to normalize it. But then, it represents the
3282 whole range or the empty set, so make it
3284 if (tree_int_cst_equal (n_low, low)
3285 && tree_int_cst_equal (n_high, high))
3291 low = n_low, high = n_high;
3296 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3297 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3300 if (! INTEGRAL_TYPE_P (type)
3301 || (low != 0 && ! int_fits_type_p (low, type))
3302 || (high != 0 && ! int_fits_type_p (high, type)))
3305 n_low = low, n_high = high;
3308 n_low = convert (type, n_low);
3311 n_high = convert (type, n_high);
3313 /* If we're converting from an unsigned to a signed type,
3314 we will be doing the comparison as unsigned. The tests above
3315 have already verified that LOW and HIGH are both positive.
3317 So we have to make sure that the original unsigned value will
3318 be interpreted as positive. */
3319 if (TREE_UNSIGNED (type) && ! TREE_UNSIGNED (TREE_TYPE (exp)))
3321 tree equiv_type = (*lang_hooks.types.type_for_mode)
3322 (TYPE_MODE (type), 1);
3325 /* A range without an upper bound is, naturally, unbounded.
3326 Since convert would have cropped a very large value, use
3327 the max value for the destination type. */
3329 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3330 : TYPE_MAX_VALUE (type);
3332 if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (exp)))
3333 high_positive = fold (build (RSHIFT_EXPR, type,
3334 convert (type, high_positive),
3335 convert (type, integer_one_node)));
3337 /* If the low bound is specified, "and" the range with the
3338 range for which the original unsigned value will be
3342 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3344 1, convert (type, integer_zero_node),
3348 in_p = (n_in_p == in_p);
3352 /* Otherwise, "or" the range with the range of the input
3353 that will be interpreted as negative. */
3354 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3356 1, convert (type, integer_zero_node),
3360 in_p = (in_p != n_in_p);
3365 low = n_low, high = n_high;
3375 /* If EXP is a constant, we can evaluate whether this is true or false. */
3376 if (TREE_CODE (exp) == INTEGER_CST)
3378 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3380 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3386 *pin_p = in_p, *plow = low, *phigh = high;
3390 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3391 type, TYPE, return an expression to test if EXP is in (or out of, depending
3392 on IN_P) the range. */
3395 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3397 tree etype = TREE_TYPE (exp);
3401 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3402 return invert_truthvalue (value);
3404 if (low == 0 && high == 0)
3405 return convert (type, integer_one_node);
3408 return fold (build (LE_EXPR, type, exp, high));
3411 return fold (build (GE_EXPR, type, exp, low));
3413 if (operand_equal_p (low, high, 0))
3414 return fold (build (EQ_EXPR, type, exp, low));
3416 if (integer_zerop (low))
3418 if (! TREE_UNSIGNED (etype))
3420 etype = (*lang_hooks.types.unsigned_type) (etype);
3421 high = convert (etype, high);
3422 exp = convert (etype, exp);
3424 return build_range_check (type, exp, 1, 0, high);
3427 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
3428 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3430 unsigned HOST_WIDE_INT lo;
3434 prec = TYPE_PRECISION (etype);
3435 if (prec <= HOST_BITS_PER_WIDE_INT)
3438 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3442 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3443 lo = (unsigned HOST_WIDE_INT) -1;
3446 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3448 if (TREE_UNSIGNED (etype))
3450 etype = (*lang_hooks.types.signed_type) (etype);
3451 exp = convert (etype, exp);
3453 return fold (build (GT_EXPR, type, exp,
3454 convert (etype, integer_zero_node)));
3458 if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3459 && ! TREE_OVERFLOW (value))
3460 return build_range_check (type,
3461 fold (build (MINUS_EXPR, etype, exp, low)),
3462 1, convert (etype, integer_zero_node), value);
3467 /* Given two ranges, see if we can merge them into one. Return 1 if we
3468 can, 0 if we can't. Set the output range into the specified parameters. */
3471 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
3472 tree high0, int in1_p, tree low1, tree high1)
3480 int lowequal = ((low0 == 0 && low1 == 0)
3481 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3482 low0, 0, low1, 0)));
3483 int highequal = ((high0 == 0 && high1 == 0)
3484 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3485 high0, 1, high1, 1)));
3487 /* Make range 0 be the range that starts first, or ends last if they
3488 start at the same value. Swap them if it isn't. */
3489 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3492 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3493 high1, 1, high0, 1))))
3495 temp = in0_p, in0_p = in1_p, in1_p = temp;
3496 tem = low0, low0 = low1, low1 = tem;
3497 tem = high0, high0 = high1, high1 = tem;
3500 /* Now flag two cases, whether the ranges are disjoint or whether the
3501 second range is totally subsumed in the first. Note that the tests
3502 below are simplified by the ones above. */
3503 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3504 high0, 1, low1, 0));
3505 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3506 high1, 1, high0, 1));
3508 /* We now have four cases, depending on whether we are including or
3509 excluding the two ranges. */
3512 /* If they don't overlap, the result is false. If the second range
3513 is a subset it is the result. Otherwise, the range is from the start
3514 of the second to the end of the first. */
3516 in_p = 0, low = high = 0;
3518 in_p = 1, low = low1, high = high1;
3520 in_p = 1, low = low1, high = high0;
3523 else if (in0_p && ! in1_p)
3525 /* If they don't overlap, the result is the first range. If they are
3526 equal, the result is false. If the second range is a subset of the
3527 first, and the ranges begin at the same place, we go from just after
3528 the end of the first range to the end of the second. If the second
3529 range is not a subset of the first, or if it is a subset and both
3530 ranges end at the same place, the range starts at the start of the
3531 first range and ends just before the second range.
3532 Otherwise, we can't describe this as a single range. */
3534 in_p = 1, low = low0, high = high0;
3535 else if (lowequal && highequal)
3536 in_p = 0, low = high = 0;
3537 else if (subset && lowequal)
3539 in_p = 1, high = high0;
3540 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3541 integer_one_node, 0);
3543 else if (! subset || highequal)
3545 in_p = 1, low = low0;
3546 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3547 integer_one_node, 0);
3553 else if (! in0_p && in1_p)
3555 /* If they don't overlap, the result is the second range. If the second
3556 is a subset of the first, the result is false. Otherwise,
3557 the range starts just after the first range and ends at the
3558 end of the second. */
3560 in_p = 1, low = low1, high = high1;
3561 else if (subset || highequal)
3562 in_p = 0, low = high = 0;
3565 in_p = 1, high = high1;
3566 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3567 integer_one_node, 0);
3573 /* The case where we are excluding both ranges. Here the complex case
3574 is if they don't overlap. In that case, the only time we have a
3575 range is if they are adjacent. If the second is a subset of the
3576 first, the result is the first. Otherwise, the range to exclude
3577 starts at the beginning of the first range and ends at the end of the
3581 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3582 range_binop (PLUS_EXPR, NULL_TREE,
3584 integer_one_node, 1),
3586 in_p = 0, low = low0, high = high1;
3591 in_p = 0, low = low0, high = high0;
3593 in_p = 0, low = low0, high = high1;
3596 *pin_p = in_p, *plow = low, *phigh = high;
3600 #ifndef RANGE_TEST_NON_SHORT_CIRCUIT
3601 #define RANGE_TEST_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
3604 /* EXP is some logical combination of boolean tests. See if we can
3605 merge it into some range test. Return the new tree if so. */
3608 fold_range_test (tree exp)
3610 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3611 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3612 int in0_p, in1_p, in_p;
3613 tree low0, low1, low, high0, high1, high;
3614 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3615 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3618 /* If this is an OR operation, invert both sides; we will invert
3619 again at the end. */
3621 in0_p = ! in0_p, in1_p = ! in1_p;
3623 /* If both expressions are the same, if we can merge the ranges, and we
3624 can build the range test, return it or it inverted. If one of the
3625 ranges is always true or always false, consider it to be the same
3626 expression as the other. */
3627 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3628 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3630 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3632 : rhs != 0 ? rhs : integer_zero_node,
3634 return or_op ? invert_truthvalue (tem) : tem;
3636 /* On machines where the branch cost is expensive, if this is a
3637 short-circuited branch and the underlying object on both sides
3638 is the same, make a non-short-circuit operation. */
3639 else if (RANGE_TEST_NON_SHORT_CIRCUIT
3640 && lhs != 0 && rhs != 0
3641 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3642 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
3643 && operand_equal_p (lhs, rhs, 0))
3645 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
3646 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
3647 which cases we can't do this. */
3648 if (simple_operand_p (lhs))
3649 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3650 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3651 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
3652 TREE_OPERAND (exp, 1));
3654 else if ((*lang_hooks.decls.global_bindings_p) () == 0
3655 && ! CONTAINS_PLACEHOLDER_P (lhs))
3657 tree common = save_expr (lhs);
3659 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
3660 or_op ? ! in0_p : in0_p,
3662 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
3663 or_op ? ! in1_p : in1_p,
3665 return build (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
3666 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
3667 TREE_TYPE (exp), lhs, rhs);
3674 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
3675 bit value. Arrange things so the extra bits will be set to zero if and
3676 only if C is signed-extended to its full width. If MASK is nonzero,
3677 it is an INTEGER_CST that should be AND'ed with the extra bits. */
3680 unextend (tree c, int p, int unsignedp, tree mask)
3682 tree type = TREE_TYPE (c);
3683 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
3686 if (p == modesize || unsignedp)
3689 /* We work by getting just the sign bit into the low-order bit, then
3690 into the high-order bit, then sign-extend. We then XOR that value
3692 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
3693 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
3695 /* We must use a signed type in order to get an arithmetic right shift.
3696 However, we must also avoid introducing accidental overflows, so that
3697 a subsequent call to integer_zerop will work. Hence we must
3698 do the type conversion here. At this point, the constant is either
3699 zero or one, and the conversion to a signed type can never overflow.
3700 We could get an overflow if this conversion is done anywhere else. */
3701 if (TREE_UNSIGNED (type))
3702 temp = convert ((*lang_hooks.types.signed_type) (type), temp);
3704 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
3705 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
3707 temp = const_binop (BIT_AND_EXPR, temp, convert (TREE_TYPE (c), mask), 0);
3708 /* If necessary, convert the type back to match the type of C. */
3709 if (TREE_UNSIGNED (type))
3710 temp = convert (type, temp);
3712 return convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
3715 /* Find ways of folding logical expressions of LHS and RHS:
3716 Try to merge two comparisons to the same innermost item.
3717 Look for range tests like "ch >= '0' && ch <= '9'".
3718 Look for combinations of simple terms on machines with expensive branches
3719 and evaluate the RHS unconditionally.
3721 For example, if we have p->a == 2 && p->b == 4 and we can make an
3722 object large enough to span both A and B, we can do this with a comparison
3723 against the object ANDed with the a mask.
3725 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
3726 operations to do this with one comparison.
3728 We check for both normal comparisons and the BIT_AND_EXPRs made this by
3729 function and the one above.
3731 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
3732 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
3734 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
3737 We return the simplified tree or 0 if no optimization is possible. */
3740 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
3742 /* If this is the "or" of two comparisons, we can do something if
3743 the comparisons are NE_EXPR. If this is the "and", we can do something
3744 if the comparisons are EQ_EXPR. I.e.,
3745 (a->b == 2 && a->c == 4) can become (a->new == NEW).
3747 WANTED_CODE is this operation code. For single bit fields, we can
3748 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
3749 comparison for one-bit fields. */
3751 enum tree_code wanted_code;
3752 enum tree_code lcode, rcode;
3753 tree ll_arg, lr_arg, rl_arg, rr_arg;
3754 tree ll_inner, lr_inner, rl_inner, rr_inner;
3755 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
3756 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
3757 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
3758 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
3759 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
3760 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
3761 enum machine_mode lnmode, rnmode;
3762 tree ll_mask, lr_mask, rl_mask, rr_mask;
3763 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
3764 tree l_const, r_const;
3765 tree lntype, rntype, result;
3766 int first_bit, end_bit;
3769 /* Start by getting the comparison codes. Fail if anything is volatile.
3770 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
3771 it were surrounded with a NE_EXPR. */
3773 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
3776 lcode = TREE_CODE (lhs);
3777 rcode = TREE_CODE (rhs);
3779 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
3780 lcode = NE_EXPR, lhs = build (NE_EXPR, truth_type, lhs, integer_zero_node);
3782 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
3783 rcode = NE_EXPR, rhs = build (NE_EXPR, truth_type, rhs, integer_zero_node);
3785 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
3788 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
3789 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
3791 ll_arg = TREE_OPERAND (lhs, 0);
3792 lr_arg = TREE_OPERAND (lhs, 1);
3793 rl_arg = TREE_OPERAND (rhs, 0);
3794 rr_arg = TREE_OPERAND (rhs, 1);
3796 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
3797 if (simple_operand_p (ll_arg)
3798 && simple_operand_p (lr_arg)
3799 && !FLOAT_TYPE_P (TREE_TYPE (ll_arg)))
3803 if (operand_equal_p (ll_arg, rl_arg, 0)
3804 && operand_equal_p (lr_arg, rr_arg, 0))
3806 int lcompcode, rcompcode;
3808 lcompcode = comparison_to_compcode (lcode);
3809 rcompcode = comparison_to_compcode (rcode);
3810 compcode = (code == TRUTH_AND_EXPR)
3811 ? lcompcode & rcompcode
3812 : lcompcode | rcompcode;
3814 else if (operand_equal_p (ll_arg, rr_arg, 0)
3815 && operand_equal_p (lr_arg, rl_arg, 0))
3817 int lcompcode, rcompcode;
3819 rcode = swap_tree_comparison (rcode);
3820 lcompcode = comparison_to_compcode (lcode);
3821 rcompcode = comparison_to_compcode (rcode);
3822 compcode = (code == TRUTH_AND_EXPR)
3823 ? lcompcode & rcompcode
3824 : lcompcode | rcompcode;
3829 if (compcode == COMPCODE_TRUE)
3830 return convert (truth_type, integer_one_node);
3831 else if (compcode == COMPCODE_FALSE)
3832 return convert (truth_type, integer_zero_node);
3833 else if (compcode != -1)
3834 return build (compcode_to_comparison (compcode),
3835 truth_type, ll_arg, lr_arg);
3838 /* If the RHS can be evaluated unconditionally and its operands are
3839 simple, it wins to evaluate the RHS unconditionally on machines
3840 with expensive branches. In this case, this isn't a comparison
3841 that can be merged. Avoid doing this if the RHS is a floating-point
3842 comparison since those can trap. */
3844 if (BRANCH_COST >= 2
3845 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
3846 && simple_operand_p (rl_arg)
3847 && simple_operand_p (rr_arg))
3849 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
3850 if (code == TRUTH_OR_EXPR
3851 && lcode == NE_EXPR && integer_zerop (lr_arg)
3852 && rcode == NE_EXPR && integer_zerop (rr_arg)
3853 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
3854 return build (NE_EXPR, truth_type,
3855 build (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
3859 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
3860 if (code == TRUTH_AND_EXPR
3861 && lcode == EQ_EXPR && integer_zerop (lr_arg)
3862 && rcode == EQ_EXPR && integer_zerop (rr_arg)
3863 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
3864 return build (EQ_EXPR, truth_type,
3865 build (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
3869 return build (code, truth_type, lhs, rhs);
3872 /* See if the comparisons can be merged. Then get all the parameters for
3875 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
3876 || (rcode != EQ_EXPR && rcode != NE_EXPR))
3880 ll_inner = decode_field_reference (ll_arg,
3881 &ll_bitsize, &ll_bitpos, &ll_mode,
3882 &ll_unsignedp, &volatilep, &ll_mask,
3884 lr_inner = decode_field_reference (lr_arg,
3885 &lr_bitsize, &lr_bitpos, &lr_mode,
3886 &lr_unsignedp, &volatilep, &lr_mask,
3888 rl_inner = decode_field_reference (rl_arg,
3889 &rl_bitsize, &rl_bitpos, &rl_mode,
3890 &rl_unsignedp, &volatilep, &rl_mask,
3892 rr_inner = decode_field_reference (rr_arg,
3893 &rr_bitsize, &rr_bitpos, &rr_mode,
3894 &rr_unsignedp, &volatilep, &rr_mask,
3897 /* It must be true that the inner operation on the lhs of each
3898 comparison must be the same if we are to be able to do anything.
3899 Then see if we have constants. If not, the same must be true for
3901 if (volatilep || ll_inner == 0 || rl_inner == 0
3902 || ! operand_equal_p (ll_inner, rl_inner, 0))
3905 if (TREE_CODE (lr_arg) == INTEGER_CST
3906 && TREE_CODE (rr_arg) == INTEGER_CST)
3907 l_const = lr_arg, r_const = rr_arg;
3908 else if (lr_inner == 0 || rr_inner == 0
3909 || ! operand_equal_p (lr_inner, rr_inner, 0))
3912 l_const = r_const = 0;
3914 /* If either comparison code is not correct for our logical operation,
3915 fail. However, we can convert a one-bit comparison against zero into
3916 the opposite comparison against that bit being set in the field. */
3918 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
3919 if (lcode != wanted_code)
3921 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
3923 /* Make the left operand unsigned, since we are only interested
3924 in the value of one bit. Otherwise we are doing the wrong
3933 /* This is analogous to the code for l_const above. */
3934 if (rcode != wanted_code)
3936 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
3945 /* After this point all optimizations will generate bit-field
3946 references, which we might not want. */
3947 if (! (*lang_hooks.can_use_bit_fields_p) ())
3950 /* See if we can find a mode that contains both fields being compared on
3951 the left. If we can't, fail. Otherwise, update all constants and masks
3952 to be relative to a field of that size. */
3953 first_bit = MIN (ll_bitpos, rl_bitpos);
3954 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
3955 lnmode = get_best_mode (end_bit - first_bit, first_bit,
3956 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
3958 if (lnmode == VOIDmode)
3961 lnbitsize = GET_MODE_BITSIZE (lnmode);
3962 lnbitpos = first_bit & ~ (lnbitsize - 1);
3963 lntype = (*lang_hooks.types.type_for_size) (lnbitsize, 1);
3964 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
3966 if (BYTES_BIG_ENDIAN)
3968 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
3969 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
3972 ll_mask = const_binop (LSHIFT_EXPR, convert (lntype, ll_mask),
3973 size_int (xll_bitpos), 0);
3974 rl_mask = const_binop (LSHIFT_EXPR, convert (lntype, rl_mask),
3975 size_int (xrl_bitpos), 0);
3979 l_const = convert (lntype, l_const);
3980 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
3981 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
3982 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
3983 fold (build1 (BIT_NOT_EXPR,
3987 warning ("comparison is always %d", wanted_code == NE_EXPR);
3989 return convert (truth_type,
3990 wanted_code == NE_EXPR
3991 ? integer_one_node : integer_zero_node);
3996 r_const = convert (lntype, r_const);
3997 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
3998 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
3999 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4000 fold (build1 (BIT_NOT_EXPR,
4004 warning ("comparison is always %d", wanted_code == NE_EXPR);
4006 return convert (truth_type,
4007 wanted_code == NE_EXPR
4008 ? integer_one_node : integer_zero_node);
4012 /* If the right sides are not constant, do the same for it. Also,
4013 disallow this optimization if a size or signedness mismatch occurs
4014 between the left and right sides. */
4017 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4018 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4019 /* Make sure the two fields on the right
4020 correspond to the left without being swapped. */
4021 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4024 first_bit = MIN (lr_bitpos, rr_bitpos);
4025 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4026 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4027 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4029 if (rnmode == VOIDmode)
4032 rnbitsize = GET_MODE_BITSIZE (rnmode);
4033 rnbitpos = first_bit & ~ (rnbitsize - 1);
4034 rntype = (*lang_hooks.types.type_for_size) (rnbitsize, 1);
4035 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4037 if (BYTES_BIG_ENDIAN)
4039 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4040 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4043 lr_mask = const_binop (LSHIFT_EXPR, convert (rntype, lr_mask),
4044 size_int (xlr_bitpos), 0);
4045 rr_mask = const_binop (LSHIFT_EXPR, convert (rntype, rr_mask),
4046 size_int (xrr_bitpos), 0);
4048 /* Make a mask that corresponds to both fields being compared.
4049 Do this for both items being compared. If the operands are the
4050 same size and the bits being compared are in the same position
4051 then we can do this by masking both and comparing the masked
4053 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4054 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4055 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4057 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4058 ll_unsignedp || rl_unsignedp);
4059 if (! all_ones_mask_p (ll_mask, lnbitsize))
4060 lhs = build (BIT_AND_EXPR, lntype, lhs, ll_mask);
4062 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4063 lr_unsignedp || rr_unsignedp);
4064 if (! all_ones_mask_p (lr_mask, rnbitsize))
4065 rhs = build (BIT_AND_EXPR, rntype, rhs, lr_mask);
4067 return build (wanted_code, truth_type, lhs, rhs);
4070 /* There is still another way we can do something: If both pairs of
4071 fields being compared are adjacent, we may be able to make a wider
4072 field containing them both.
4074 Note that we still must mask the lhs/rhs expressions. Furthermore,
4075 the mask must be shifted to account for the shift done by
4076 make_bit_field_ref. */
4077 if ((ll_bitsize + ll_bitpos == rl_bitpos
4078 && lr_bitsize + lr_bitpos == rr_bitpos)
4079 || (ll_bitpos == rl_bitpos + rl_bitsize
4080 && lr_bitpos == rr_bitpos + rr_bitsize))
4084 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4085 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4086 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4087 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4089 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4090 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4091 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4092 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4094 /* Convert to the smaller type before masking out unwanted bits. */
4096 if (lntype != rntype)
4098 if (lnbitsize > rnbitsize)
4100 lhs = convert (rntype, lhs);
4101 ll_mask = convert (rntype, ll_mask);
4104 else if (lnbitsize < rnbitsize)
4106 rhs = convert (lntype, rhs);
4107 lr_mask = convert (lntype, lr_mask);
4112 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4113 lhs = build (BIT_AND_EXPR, type, lhs, ll_mask);
4115 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4116 rhs = build (BIT_AND_EXPR, type, rhs, lr_mask);
4118 return build (wanted_code, truth_type, lhs, rhs);
4124 /* Handle the case of comparisons with constants. If there is something in
4125 common between the masks, those bits of the constants must be the same.
4126 If not, the condition is always false. Test for this to avoid generating
4127 incorrect code below. */
4128 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4129 if (! integer_zerop (result)
4130 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4131 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4133 if (wanted_code == NE_EXPR)
4135 warning ("`or' of unmatched not-equal tests is always 1");
4136 return convert (truth_type, integer_one_node);
4140 warning ("`and' of mutually exclusive equal-tests is always 0");
4141 return convert (truth_type, integer_zero_node);
4145 /* Construct the expression we will return. First get the component
4146 reference we will make. Unless the mask is all ones the width of
4147 that field, perform the mask operation. Then compare with the
4149 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4150 ll_unsignedp || rl_unsignedp);
4152 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4153 if (! all_ones_mask_p (ll_mask, lnbitsize))
4154 result = build (BIT_AND_EXPR, lntype, result, ll_mask);
4156 return build (wanted_code, truth_type, result,
4157 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4160 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4164 optimize_minmax_comparison (tree t)
4166 tree type = TREE_TYPE (t);
4167 tree arg0 = TREE_OPERAND (t, 0);
4168 enum tree_code op_code;
4169 tree comp_const = TREE_OPERAND (t, 1);
4171 int consts_equal, consts_lt;
4174 STRIP_SIGN_NOPS (arg0);
4176 op_code = TREE_CODE (arg0);
4177 minmax_const = TREE_OPERAND (arg0, 1);
4178 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4179 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4180 inner = TREE_OPERAND (arg0, 0);
4182 /* If something does not permit us to optimize, return the original tree. */
4183 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4184 || TREE_CODE (comp_const) != INTEGER_CST
4185 || TREE_CONSTANT_OVERFLOW (comp_const)
4186 || TREE_CODE (minmax_const) != INTEGER_CST
4187 || TREE_CONSTANT_OVERFLOW (minmax_const))
4190 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4191 and GT_EXPR, doing the rest with recursive calls using logical
4193 switch (TREE_CODE (t))
4195 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4197 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4201 fold (build (TRUTH_ORIF_EXPR, type,
4202 optimize_minmax_comparison
4203 (build (EQ_EXPR, type, arg0, comp_const)),
4204 optimize_minmax_comparison
4205 (build (GT_EXPR, type, arg0, comp_const))));
4208 if (op_code == MAX_EXPR && consts_equal)
4209 /* MAX (X, 0) == 0 -> X <= 0 */
4210 return fold (build (LE_EXPR, type, inner, comp_const));
4212 else if (op_code == MAX_EXPR && consts_lt)
4213 /* MAX (X, 0) == 5 -> X == 5 */
4214 return fold (build (EQ_EXPR, type, inner, comp_const));
4216 else if (op_code == MAX_EXPR)
4217 /* MAX (X, 0) == -1 -> false */
4218 return omit_one_operand (type, integer_zero_node, inner);
4220 else if (consts_equal)
4221 /* MIN (X, 0) == 0 -> X >= 0 */
4222 return fold (build (GE_EXPR, type, inner, comp_const));
4225 /* MIN (X, 0) == 5 -> false */
4226 return omit_one_operand (type, integer_zero_node, inner);
4229 /* MIN (X, 0) == -1 -> X == -1 */
4230 return fold (build (EQ_EXPR, type, inner, comp_const));
4233 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4234 /* MAX (X, 0) > 0 -> X > 0
4235 MAX (X, 0) > 5 -> X > 5 */
4236 return fold (build (GT_EXPR, type, inner, comp_const));
4238 else if (op_code == MAX_EXPR)
4239 /* MAX (X, 0) > -1 -> true */
4240 return omit_one_operand (type, integer_one_node, inner);
4242 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4243 /* MIN (X, 0) > 0 -> false
4244 MIN (X, 0) > 5 -> false */
4245 return omit_one_operand (type, integer_zero_node, inner);
4248 /* MIN (X, 0) > -1 -> X > -1 */
4249 return fold (build (GT_EXPR, type, inner, comp_const));
4256 /* T is an integer expression that is being multiplied, divided, or taken a
4257 modulus (CODE says which and what kind of divide or modulus) by a
4258 constant C. See if we can eliminate that operation by folding it with
4259 other operations already in T. WIDE_TYPE, if non-null, is a type that
4260 should be used for the computation if wider than our type.
4262 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4263 (X * 2) + (Y * 4). We must, however, be assured that either the original
4264 expression would not overflow or that overflow is undefined for the type
4265 in the language in question.
4267 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4268 the machine has a multiply-accumulate insn or that this is part of an
4269 addressing calculation.
4271 If we return a non-null expression, it is an equivalent form of the
4272 original computation, but need not be in the original type. */
4275 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
4277 /* To avoid exponential search depth, refuse to allow recursion past
4278 three levels. Beyond that (1) it's highly unlikely that we'll find
4279 something interesting and (2) we've probably processed it before
4280 when we built the inner expression. */
4289 ret = extract_muldiv_1 (t, c, code, wide_type);
4296 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
4298 tree type = TREE_TYPE (t);
4299 enum tree_code tcode = TREE_CODE (t);
4300 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4301 > GET_MODE_SIZE (TYPE_MODE (type)))
4302 ? wide_type : type);
4304 int same_p = tcode == code;
4305 tree op0 = NULL_TREE, op1 = NULL_TREE;
4307 /* Don't deal with constants of zero here; they confuse the code below. */
4308 if (integer_zerop (c))
4311 if (TREE_CODE_CLASS (tcode) == '1')
4312 op0 = TREE_OPERAND (t, 0);
4314 if (TREE_CODE_CLASS (tcode) == '2')
4315 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4317 /* Note that we need not handle conditional operations here since fold
4318 already handles those cases. So just do arithmetic here. */
4322 /* For a constant, we can always simplify if we are a multiply
4323 or (for divide and modulus) if it is a multiple of our constant. */
4324 if (code == MULT_EXPR
4325 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4326 return const_binop (code, convert (ctype, t), convert (ctype, c), 0);
4329 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4330 /* If op0 is an expression ... */
4331 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4332 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4333 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4334 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4335 /* ... and is unsigned, and its type is smaller than ctype,
4336 then we cannot pass through as widening. */
4337 && ((TREE_UNSIGNED (TREE_TYPE (op0))
4338 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4339 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4340 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4341 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4342 /* ... or its type is larger than ctype,
4343 then we cannot pass through this truncation. */
4344 || (GET_MODE_SIZE (TYPE_MODE (ctype))
4345 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
4346 /* ... or signedness changes for division or modulus,
4347 then we cannot pass through this conversion. */
4348 || (code != MULT_EXPR
4349 && (TREE_UNSIGNED (ctype)
4350 != TREE_UNSIGNED (TREE_TYPE (op0))))))
4353 /* Pass the constant down and see if we can make a simplification. If
4354 we can, replace this expression with the inner simplification for
4355 possible later conversion to our or some other type. */
4356 if ((t2 = convert (TREE_TYPE (op0), c)) != 0
4357 && TREE_CODE (t2) == INTEGER_CST
4358 && ! TREE_CONSTANT_OVERFLOW (t2)
4359 && (0 != (t1 = extract_muldiv (op0, t2, code,
4361 ? ctype : NULL_TREE))))
4365 case NEGATE_EXPR: case ABS_EXPR:
4366 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4367 return fold (build1 (tcode, ctype, convert (ctype, t1)));
4370 case MIN_EXPR: case MAX_EXPR:
4371 /* If widening the type changes the signedness, then we can't perform
4372 this optimization as that changes the result. */
4373 if (TREE_UNSIGNED (ctype) != TREE_UNSIGNED (type))
4376 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4377 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4378 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4380 if (tree_int_cst_sgn (c) < 0)
4381 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4383 return fold (build (tcode, ctype, convert (ctype, t1),
4384 convert (ctype, t2)));
4388 case WITH_RECORD_EXPR:
4389 if ((t1 = extract_muldiv (TREE_OPERAND (t, 0), c, code, wide_type)) != 0)
4390 return build (WITH_RECORD_EXPR, TREE_TYPE (t1), t1,
4391 TREE_OPERAND (t, 1));
4394 case LSHIFT_EXPR: case RSHIFT_EXPR:
4395 /* If the second operand is constant, this is a multiplication
4396 or floor division, by a power of two, so we can treat it that
4397 way unless the multiplier or divisor overflows. */
4398 if (TREE_CODE (op1) == INTEGER_CST
4399 /* const_binop may not detect overflow correctly,
4400 so check for it explicitly here. */
4401 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4402 && TREE_INT_CST_HIGH (op1) == 0
4403 && 0 != (t1 = convert (ctype,
4404 const_binop (LSHIFT_EXPR, size_one_node,
4406 && ! TREE_OVERFLOW (t1))
4407 return extract_muldiv (build (tcode == LSHIFT_EXPR
4408 ? MULT_EXPR : FLOOR_DIV_EXPR,
4409 ctype, convert (ctype, op0), t1),
4410 c, code, wide_type);
4413 case PLUS_EXPR: case MINUS_EXPR:
4414 /* See if we can eliminate the operation on both sides. If we can, we
4415 can return a new PLUS or MINUS. If we can't, the only remaining
4416 cases where we can do anything are if the second operand is a
4418 t1 = extract_muldiv (op0, c, code, wide_type);
4419 t2 = extract_muldiv (op1, c, code, wide_type);
4420 if (t1 != 0 && t2 != 0
4421 && (code == MULT_EXPR
4422 /* If not multiplication, we can only do this if both operands
4423 are divisible by c. */
4424 || (multiple_of_p (ctype, op0, c)
4425 && multiple_of_p (ctype, op1, c))))
4426 return fold (build (tcode, ctype, convert (ctype, t1),
4427 convert (ctype, t2)));
4429 /* If this was a subtraction, negate OP1 and set it to be an addition.
4430 This simplifies the logic below. */
4431 if (tcode == MINUS_EXPR)
4432 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4434 if (TREE_CODE (op1) != INTEGER_CST)
4437 /* If either OP1 or C are negative, this optimization is not safe for
4438 some of the division and remainder types while for others we need
4439 to change the code. */
4440 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4442 if (code == CEIL_DIV_EXPR)
4443 code = FLOOR_DIV_EXPR;
4444 else if (code == FLOOR_DIV_EXPR)
4445 code = CEIL_DIV_EXPR;
4446 else if (code != MULT_EXPR
4447 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
4451 /* If it's a multiply or a division/modulus operation of a multiple
4452 of our constant, do the operation and verify it doesn't overflow. */
4453 if (code == MULT_EXPR
4454 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4456 op1 = const_binop (code, convert (ctype, op1),
4457 convert (ctype, c), 0);
4458 /* We allow the constant to overflow with wrapping semantics. */
4460 || (TREE_OVERFLOW (op1) && ! flag_wrapv))
4466 /* If we have an unsigned type is not a sizetype, we cannot widen
4467 the operation since it will change the result if the original
4468 computation overflowed. */
4469 if (TREE_UNSIGNED (ctype)
4470 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4474 /* If we were able to eliminate our operation from the first side,
4475 apply our operation to the second side and reform the PLUS. */
4476 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4477 return fold (build (tcode, ctype, convert (ctype, t1), op1));
4479 /* The last case is if we are a multiply. In that case, we can
4480 apply the distributive law to commute the multiply and addition
4481 if the multiplication of the constants doesn't overflow. */
4482 if (code == MULT_EXPR)
4483 return fold (build (tcode, ctype, fold (build (code, ctype,
4484 convert (ctype, op0),
4485 convert (ctype, c))),
4491 /* We have a special case here if we are doing something like
4492 (C * 8) % 4 since we know that's zero. */
4493 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4494 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4495 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4496 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4497 return omit_one_operand (type, integer_zero_node, op0);
4499 /* ... fall through ... */
4501 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4502 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4503 /* If we can extract our operation from the LHS, do so and return a
4504 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4505 do something only if the second operand is a constant. */
4507 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4508 return fold (build (tcode, ctype, convert (ctype, t1),
4509 convert (ctype, op1)));
4510 else if (tcode == MULT_EXPR && code == MULT_EXPR
4511 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4512 return fold (build (tcode, ctype, convert (ctype, op0),
4513 convert (ctype, t1)));
4514 else if (TREE_CODE (op1) != INTEGER_CST)
4517 /* If these are the same operation types, we can associate them
4518 assuming no overflow. */
4520 && 0 != (t1 = const_binop (MULT_EXPR, convert (ctype, op1),
4521 convert (ctype, c), 0))
4522 && ! TREE_OVERFLOW (t1))
4523 return fold (build (tcode, ctype, convert (ctype, op0), t1));
4525 /* If these operations "cancel" each other, we have the main
4526 optimizations of this pass, which occur when either constant is a
4527 multiple of the other, in which case we replace this with either an
4528 operation or CODE or TCODE.
4530 If we have an unsigned type that is not a sizetype, we cannot do
4531 this since it will change the result if the original computation
4533 if ((! TREE_UNSIGNED (ctype)
4534 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4536 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4537 || (tcode == MULT_EXPR
4538 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4539 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4541 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4542 return fold (build (tcode, ctype, convert (ctype, op0),
4544 const_binop (TRUNC_DIV_EXPR,
4546 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4547 return fold (build (code, ctype, convert (ctype, op0),
4549 const_binop (TRUNC_DIV_EXPR,
4561 /* If T contains a COMPOUND_EXPR which was inserted merely to evaluate
4562 S, a SAVE_EXPR, return the expression actually being evaluated. Note
4563 that we may sometimes modify the tree. */
4566 strip_compound_expr (tree t, tree s)
4568 enum tree_code code = TREE_CODE (t);
4570 /* See if this is the COMPOUND_EXPR we want to eliminate. */
4571 if (code == COMPOUND_EXPR && TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR
4572 && TREE_OPERAND (TREE_OPERAND (t, 0), 0) == s)
4573 return TREE_OPERAND (t, 1);
4575 /* See if this is a COND_EXPR or a simple arithmetic operator. We
4576 don't bother handling any other types. */
4577 else if (code == COND_EXPR)
4579 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4580 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4581 TREE_OPERAND (t, 2) = strip_compound_expr (TREE_OPERAND (t, 2), s);
4583 else if (TREE_CODE_CLASS (code) == '1')
4584 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4585 else if (TREE_CODE_CLASS (code) == '<'
4586 || TREE_CODE_CLASS (code) == '2')
4588 TREE_OPERAND (t, 0) = strip_compound_expr (TREE_OPERAND (t, 0), s);
4589 TREE_OPERAND (t, 1) = strip_compound_expr (TREE_OPERAND (t, 1), s);
4595 /* Return a node which has the indicated constant VALUE (either 0 or
4596 1), and is of the indicated TYPE. */
4599 constant_boolean_node (int value, tree type)
4601 if (type == integer_type_node)
4602 return value ? integer_one_node : integer_zero_node;
4603 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4604 return (*lang_hooks.truthvalue_conversion) (value ? integer_one_node :
4608 tree t = build_int_2 (value, 0);
4610 TREE_TYPE (t) = type;
4615 /* Utility function for the following routine, to see how complex a nesting of
4616 COND_EXPRs can be. EXPR is the expression and LIMIT is a count beyond which
4617 we don't care (to avoid spending too much time on complex expressions.). */
4620 count_cond (tree expr, int lim)
4624 if (TREE_CODE (expr) != COND_EXPR)
4629 ctrue = count_cond (TREE_OPERAND (expr, 1), lim - 1);
4630 cfalse = count_cond (TREE_OPERAND (expr, 2), lim - 1 - ctrue);
4631 return MIN (lim, 1 + ctrue + cfalse);
4634 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
4635 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4636 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4637 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
4638 COND is the first argument to CODE; otherwise (as in the example
4639 given here), it is the second argument. TYPE is the type of the
4640 original expression. */
4643 fold_binary_op_with_conditional_arg (enum tree_code code, tree type,
4644 tree cond, tree arg, int cond_first_p)
4646 tree test, true_value, false_value;
4647 tree lhs = NULL_TREE;
4648 tree rhs = NULL_TREE;
4649 /* In the end, we'll produce a COND_EXPR. Both arms of the
4650 conditional expression will be binary operations. The left-hand
4651 side of the expression to be executed if the condition is true
4652 will be pointed to by TRUE_LHS. Similarly, the right-hand side
4653 of the expression to be executed if the condition is true will be
4654 pointed to by TRUE_RHS. FALSE_LHS and FALSE_RHS are analogous --
4655 but apply to the expression to be executed if the conditional is
4661 /* These are the codes to use for the left-hand side and right-hand
4662 side of the COND_EXPR. Normally, they are the same as CODE. */
4663 enum tree_code lhs_code = code;
4664 enum tree_code rhs_code = code;
4665 /* And these are the types of the expressions. */
4666 tree lhs_type = type;
4667 tree rhs_type = type;
4672 true_rhs = false_rhs = &arg;
4673 true_lhs = &true_value;
4674 false_lhs = &false_value;
4678 true_lhs = false_lhs = &arg;
4679 true_rhs = &true_value;
4680 false_rhs = &false_value;
4683 if (TREE_CODE (cond) == COND_EXPR)
4685 test = TREE_OPERAND (cond, 0);
4686 true_value = TREE_OPERAND (cond, 1);
4687 false_value = TREE_OPERAND (cond, 2);
4688 /* If this operand throws an expression, then it does not make
4689 sense to try to perform a logical or arithmetic operation
4690 involving it. Instead of building `a + throw 3' for example,
4691 we simply build `a, throw 3'. */
4692 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4696 lhs_code = COMPOUND_EXPR;
4697 lhs_type = void_type_node;
4702 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4706 rhs_code = COMPOUND_EXPR;
4707 rhs_type = void_type_node;
4715 tree testtype = TREE_TYPE (cond);
4717 true_value = convert (testtype, integer_one_node);
4718 false_value = convert (testtype, integer_zero_node);
4721 /* If ARG is complex we want to make sure we only evaluate it once. Though
4722 this is only required if it is volatile, it might be more efficient even
4723 if it is not. However, if we succeed in folding one part to a constant,
4724 we do not need to make this SAVE_EXPR. Since we do this optimization
4725 primarily to see if we do end up with constant and this SAVE_EXPR
4726 interferes with later optimizations, suppressing it when we can is
4729 If we are not in a function, we can't make a SAVE_EXPR, so don't try to
4730 do so. Don't try to see if the result is a constant if an arm is a
4731 COND_EXPR since we get exponential behavior in that case. */
4733 if (saved_expr_p (arg))
4735 else if (lhs == 0 && rhs == 0
4736 && !TREE_CONSTANT (arg)
4737 && (*lang_hooks.decls.global_bindings_p) () == 0
4738 && ((TREE_CODE (arg) != VAR_DECL && TREE_CODE (arg) != PARM_DECL)
4739 || TREE_SIDE_EFFECTS (arg)))
4741 if (TREE_CODE (true_value) != COND_EXPR)
4742 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4744 if (TREE_CODE (false_value) != COND_EXPR)
4745 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4747 if ((lhs == 0 || ! TREE_CONSTANT (lhs))
4748 && (rhs == 0 || !TREE_CONSTANT (rhs)))
4750 arg = save_expr (arg);
4752 save = saved_expr_p (arg);
4757 lhs = fold (build (lhs_code, lhs_type, *true_lhs, *true_rhs));
4759 rhs = fold (build (rhs_code, rhs_type, *false_lhs, *false_rhs));
4761 test = fold (build (COND_EXPR, type, test, lhs, rhs));
4763 /* If ARG involves a SAVE_EXPR, we need to ensure it is evaluated
4764 ahead of the COND_EXPR we made. Otherwise we would have it only
4765 evaluated in one branch, with the other branch using the result
4766 but missing the evaluation code. Beware that the save_expr call
4767 above might not return a SAVE_EXPR, so testing the TREE_CODE
4768 of ARG is not enough to decide here. Â */
4770 return build (COMPOUND_EXPR, type,
4771 convert (void_type_node, arg),
4772 strip_compound_expr (test, arg));
4774 return convert (type, test);
4778 /* Subroutine of fold() that checks for the addition of +/- 0.0.
4780 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
4781 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
4782 ADDEND is the same as X.
4784 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
4785 and finite. The problematic cases are when X is zero, and its mode
4786 has signed zeros. In the case of rounding towards -infinity,
4787 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
4788 modes, X + 0 is not the same as X because -0 + 0 is 0. */
4791 fold_real_zero_addition_p (tree type, tree addend, int negate)
4793 if (!real_zerop (addend))
4796 /* Don't allow the fold with -fsignaling-nans. */
4797 if (HONOR_SNANS (TYPE_MODE (type)))
4800 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
4801 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
4804 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
4805 if (TREE_CODE (addend) == REAL_CST
4806 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
4809 /* The mode has signed zeros, and we have to honor their sign.
4810 In this situation, there is only one case we can return true for.
4811 X - 0 is the same as X unless rounding towards -infinity is
4813 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
4816 /* Subroutine of fold() that checks comparisons of built-in math
4817 functions against real constants.
4819 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
4820 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
4821 is the type of the result and ARG0 and ARG1 are the operands of the
4822 comparison. ARG1 must be a TREE_REAL_CST.
4824 The function returns the constant folded tree if a simplification
4825 can be made, and NULL_TREE otherwise. */
4828 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
4829 tree type, tree arg0, tree arg1)
4833 if (fcode == BUILT_IN_SQRT
4834 || fcode == BUILT_IN_SQRTF
4835 || fcode == BUILT_IN_SQRTL)
4837 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
4838 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
4840 c = TREE_REAL_CST (arg1);
4841 if (REAL_VALUE_NEGATIVE (c))
4843 /* sqrt(x) < y is always false, if y is negative. */
4844 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
4845 return omit_one_operand (type,
4846 convert (type, integer_zero_node),
4849 /* sqrt(x) > y is always true, if y is negative and we
4850 don't care about NaNs, i.e. negative values of x. */
4851 if (code == NE_EXPR || !HONOR_NANS (mode))
4852 return omit_one_operand (type,
4853 convert (type, integer_one_node),
4856 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
4857 return fold (build (GE_EXPR, type, arg,
4858 build_real (TREE_TYPE (arg), dconst0)));
4860 else if (code == GT_EXPR || code == GE_EXPR)
4864 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
4865 real_convert (&c2, mode, &c2);
4867 if (REAL_VALUE_ISINF (c2))
4869 /* sqrt(x) > y is x == +Inf, when y is very large. */
4870 if (HONOR_INFINITIES (mode))
4871 return fold (build (EQ_EXPR, type, arg,
4872 build_real (TREE_TYPE (arg), c2)));
4874 /* sqrt(x) > y is always false, when y is very large
4875 and we don't care about infinities. */
4876 return omit_one_operand (type,
4877 convert (type, integer_zero_node),
4881 /* sqrt(x) > c is the same as x > c*c. */
4882 return fold (build (code, type, arg,
4883 build_real (TREE_TYPE (arg), c2)));
4885 else if (code == LT_EXPR || code == LE_EXPR)
4889 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
4890 real_convert (&c2, mode, &c2);
4892 if (REAL_VALUE_ISINF (c2))
4894 /* sqrt(x) < y is always true, when y is a very large
4895 value and we don't care about NaNs or Infinities. */
4896 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
4897 return omit_one_operand (type,
4898 convert (type, integer_one_node),
4901 /* sqrt(x) < y is x != +Inf when y is very large and we
4902 don't care about NaNs. */
4903 if (! HONOR_NANS (mode))
4904 return fold (build (NE_EXPR, type, arg,
4905 build_real (TREE_TYPE (arg), c2)));
4907 /* sqrt(x) < y is x >= 0 when y is very large and we
4908 don't care about Infinities. */
4909 if (! HONOR_INFINITIES (mode))
4910 return fold (build (GE_EXPR, type, arg,
4911 build_real (TREE_TYPE (arg), dconst0)));
4913 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
4914 if ((*lang_hooks.decls.global_bindings_p) () != 0
4915 || CONTAINS_PLACEHOLDER_P (arg))
4918 arg = save_expr (arg);
4919 return fold (build (TRUTH_ANDIF_EXPR, type,
4920 fold (build (GE_EXPR, type, arg,
4921 build_real (TREE_TYPE (arg),
4923 fold (build (NE_EXPR, type, arg,
4924 build_real (TREE_TYPE (arg),
4928 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
4929 if (! HONOR_NANS (mode))
4930 return fold (build (code, type, arg,
4931 build_real (TREE_TYPE (arg), c2)));
4933 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
4934 if ((*lang_hooks.decls.global_bindings_p) () == 0
4935 && ! CONTAINS_PLACEHOLDER_P (arg))
4937 arg = save_expr (arg);
4938 return fold (build (TRUTH_ANDIF_EXPR, type,
4939 fold (build (GE_EXPR, type, arg,
4940 build_real (TREE_TYPE (arg),
4942 fold (build (code, type, arg,
4943 build_real (TREE_TYPE (arg),
4952 /* Subroutine of fold() that optimizes comparisons against Infinities,
4953 either +Inf or -Inf.
4955 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
4956 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
4957 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
4959 The function returns the constant folded tree if a simplification
4960 can be made, and NULL_TREE otherwise. */
4963 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
4965 enum machine_mode mode;
4966 REAL_VALUE_TYPE max;
4970 mode = TYPE_MODE (TREE_TYPE (arg0));
4972 /* For negative infinity swap the sense of the comparison. */
4973 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
4975 code = swap_tree_comparison (code);
4980 /* x > +Inf is always false, if with ignore sNANs. */
4981 if (HONOR_SNANS (mode))
4983 return omit_one_operand (type,
4984 convert (type, integer_zero_node),
4988 /* x <= +Inf is always true, if we don't case about NaNs. */
4989 if (! HONOR_NANS (mode))
4990 return omit_one_operand (type,
4991 convert (type, integer_one_node),
4994 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
4995 if ((*lang_hooks.decls.global_bindings_p) () == 0
4996 && ! CONTAINS_PLACEHOLDER_P (arg0))
4998 arg0 = save_expr (arg0);
4999 return fold (build (EQ_EXPR, type, arg0, arg0));
5005 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
5006 real_maxval (&max, neg, mode);
5007 return fold (build (neg ? LT_EXPR : GT_EXPR, type,
5008 arg0, build_real (TREE_TYPE (arg0), max)));
5011 /* x < +Inf is always equal to x <= DBL_MAX. */
5012 real_maxval (&max, neg, mode);
5013 return fold (build (neg ? GE_EXPR : LE_EXPR, type,
5014 arg0, build_real (TREE_TYPE (arg0), max)));
5017 /* x != +Inf is always equal to !(x > DBL_MAX). */
5018 real_maxval (&max, neg, mode);
5019 if (! HONOR_NANS (mode))
5020 return fold (build (neg ? GE_EXPR : LE_EXPR, type,
5021 arg0, build_real (TREE_TYPE (arg0), max)));
5022 temp = fold (build (neg ? LT_EXPR : GT_EXPR, type,
5023 arg0, build_real (TREE_TYPE (arg0), max)));
5024 return fold (build1 (TRUTH_NOT_EXPR, type, temp));
5033 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5034 equality/inequality test, then return a simplified form of
5035 the test using shifts and logical operations. Otherwise return
5036 NULL. TYPE is the desired result type. */
5039 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
5042 /* If this is a TRUTH_NOT_EXPR, it may have a single bit test inside
5044 if (code == TRUTH_NOT_EXPR)
5046 code = TREE_CODE (arg0);
5047 if (code != NE_EXPR && code != EQ_EXPR)
5050 /* Extract the arguments of the EQ/NE. */
5051 arg1 = TREE_OPERAND (arg0, 1);
5052 arg0 = TREE_OPERAND (arg0, 0);
5054 /* This requires us to invert the code. */
5055 code = (code == EQ_EXPR ? NE_EXPR : EQ_EXPR);
5058 /* If this is testing a single bit, we can optimize the test. */
5059 if ((code == NE_EXPR || code == EQ_EXPR)
5060 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
5061 && integer_pow2p (TREE_OPERAND (arg0, 1)))
5063 tree inner = TREE_OPERAND (arg0, 0);
5064 tree type = TREE_TYPE (arg0);
5065 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
5066 enum machine_mode operand_mode = TYPE_MODE (type);
5068 tree signed_type, unsigned_type, intermediate_type;
5071 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5072 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5073 arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
5074 if (arg00 != NULL_TREE)
5076 tree stype = (*lang_hooks.types.signed_type) (TREE_TYPE (arg00));
5077 return fold (build (code == EQ_EXPR ? GE_EXPR : LT_EXPR, result_type,
5078 convert (stype, arg00),
5079 convert (stype, integer_zero_node)));
5082 /* At this point, we know that arg0 is not testing the sign bit. */
5083 if (TYPE_PRECISION (type) - 1 == bitnum)
5086 /* Otherwise we have (A & C) != 0 where C is a single bit,
5087 convert that into ((A >> C2) & 1). Where C2 = log2(C).
5088 Similarly for (A & C) == 0. */
5090 /* If INNER is a right shift of a constant and it plus BITNUM does
5091 not overflow, adjust BITNUM and INNER. */
5092 if (TREE_CODE (inner) == RSHIFT_EXPR
5093 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
5094 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
5095 && bitnum < TYPE_PRECISION (type)
5096 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
5097 bitnum - TYPE_PRECISION (type)))
5099 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
5100 inner = TREE_OPERAND (inner, 0);
5103 /* If we are going to be able to omit the AND below, we must do our
5104 operations as unsigned. If we must use the AND, we have a choice.
5105 Normally unsigned is faster, but for some machines signed is. */
5106 #ifdef LOAD_EXTEND_OP
5107 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND ? 0 : 1);
5112 signed_type = (*lang_hooks.types.type_for_mode) (operand_mode, 0);
5113 unsigned_type = (*lang_hooks.types.type_for_mode) (operand_mode, 1);
5114 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
5115 inner = convert (intermediate_type, inner);
5118 inner = build (RSHIFT_EXPR, intermediate_type,
5119 inner, size_int (bitnum));
5121 if (code == EQ_EXPR)
5122 inner = build (BIT_XOR_EXPR, intermediate_type,
5123 inner, integer_one_node);
5125 /* Put the AND last so it can combine with more things. */
5126 inner = build (BIT_AND_EXPR, intermediate_type,
5127 inner, integer_one_node);
5129 /* Make sure to return the proper type. */
5130 inner = convert (result_type, inner);
5137 /* Check whether we are allowed to reorder operands arg0 and arg1,
5138 such that the evaluation of arg1 occurs before arg0. */
5141 reorder_operands_p (tree arg0, tree arg1)
5143 if (! flag_evaluation_order)
5145 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
5147 return ! TREE_SIDE_EFFECTS (arg0)
5148 && ! TREE_SIDE_EFFECTS (arg1);
5151 /* Test whether it is preferable two swap two operands, ARG0 and
5152 ARG1, for example because ARG0 is an integer constant and ARG1
5153 isn't. If REORDER is true, only recommend swapping if we can
5154 evaluate the operands in reverse order. */
5157 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
5159 STRIP_SIGN_NOPS (arg0);
5160 STRIP_SIGN_NOPS (arg1);
5162 if (TREE_CODE (arg1) == INTEGER_CST)
5164 if (TREE_CODE (arg0) == INTEGER_CST)
5167 if (TREE_CODE (arg1) == REAL_CST)
5169 if (TREE_CODE (arg0) == REAL_CST)
5172 if (TREE_CODE (arg1) == COMPLEX_CST)
5174 if (TREE_CODE (arg0) == COMPLEX_CST)
5177 if (TREE_CONSTANT (arg1))
5179 if (TREE_CONSTANT (arg0))
5185 if (reorder && flag_evaluation_order
5186 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5197 /* Perform constant folding and related simplification of EXPR.
5198 The related simplifications include x*1 => x, x*0 => 0, etc.,
5199 and application of the associative law.
5200 NOP_EXPR conversions may be removed freely (as long as we
5201 are careful not to change the C type of the overall expression)
5202 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
5203 but we can constant-fold them if they have constant operands. */
5205 #ifdef ENABLE_FOLD_CHECKING
5206 # define fold(x) fold_1 (x)
5207 static tree fold_1 (tree);
5213 tree t = expr, orig_t;
5214 tree t1 = NULL_TREE;
5216 tree type = TREE_TYPE (expr);
5217 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
5218 enum tree_code code = TREE_CODE (t);
5219 int kind = TREE_CODE_CLASS (code);
5221 /* WINS will be nonzero when the switch is done
5222 if all operands are constant. */
5225 /* Don't try to process an RTL_EXPR since its operands aren't trees.
5226 Likewise for a SAVE_EXPR that's already been evaluated. */
5227 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
5230 /* Return right away if a constant. */
5236 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
5240 /* Special case for conversion ops that can have fixed point args. */
5241 arg0 = TREE_OPERAND (t, 0);
5243 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
5245 STRIP_SIGN_NOPS (arg0);
5247 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
5248 subop = TREE_REALPART (arg0);
5252 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
5253 && TREE_CODE (subop) != REAL_CST)
5254 /* Note that TREE_CONSTANT isn't enough:
5255 static var addresses are constant but we can't
5256 do arithmetic on them. */
5259 else if (IS_EXPR_CODE_CLASS (kind))
5261 int len = first_rtl_op (code);
5263 for (i = 0; i < len; i++)
5265 tree op = TREE_OPERAND (t, i);
5269 continue; /* Valid for CALL_EXPR, at least. */
5271 if (kind == '<' || code == RSHIFT_EXPR)
5273 /* Signedness matters here. Perhaps we can refine this
5275 STRIP_SIGN_NOPS (op);
5278 /* Strip any conversions that don't change the mode. */
5281 if (TREE_CODE (op) == COMPLEX_CST)
5282 subop = TREE_REALPART (op);
5286 if (TREE_CODE (subop) != INTEGER_CST
5287 && TREE_CODE (subop) != REAL_CST)
5288 /* Note that TREE_CONSTANT isn't enough:
5289 static var addresses are constant but we can't
5290 do arithmetic on them. */
5300 /* If this is a commutative operation, and ARG0 is a constant, move it
5301 to ARG1 to reduce the number of tests below. */
5302 if ((code == PLUS_EXPR || code == MULT_EXPR || code == MIN_EXPR
5303 || code == MAX_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR
5304 || code == BIT_AND_EXPR)
5305 && tree_swap_operands_p (arg0, arg1, true))
5306 return fold (build (code, type, arg1, arg0));
5308 /* Now WINS is set as described above,
5309 ARG0 is the first operand of EXPR,
5310 and ARG1 is the second operand (if it has more than one operand).
5312 First check for cases where an arithmetic operation is applied to a
5313 compound, conditional, or comparison operation. Push the arithmetic
5314 operation inside the compound or conditional to see if any folding
5315 can then be done. Convert comparison to conditional for this purpose.
5316 The also optimizes non-constant cases that used to be done in
5319 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
5320 one of the operands is a comparison and the other is a comparison, a
5321 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
5322 code below would make the expression more complex. Change it to a
5323 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
5324 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
5326 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5327 || code == EQ_EXPR || code == NE_EXPR)
5328 && ((truth_value_p (TREE_CODE (arg0))
5329 && (truth_value_p (TREE_CODE (arg1))
5330 || (TREE_CODE (arg1) == BIT_AND_EXPR
5331 && integer_onep (TREE_OPERAND (arg1, 1)))))
5332 || (truth_value_p (TREE_CODE (arg1))
5333 && (truth_value_p (TREE_CODE (arg0))
5334 || (TREE_CODE (arg0) == BIT_AND_EXPR
5335 && integer_onep (TREE_OPERAND (arg0, 1)))))))
5337 t = fold (build (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5338 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5342 if (code == EQ_EXPR)
5343 t = invert_truthvalue (t);
5348 if (TREE_CODE_CLASS (code) == '1')
5350 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5351 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5352 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5353 else if (TREE_CODE (arg0) == COND_EXPR)
5355 tree arg01 = TREE_OPERAND (arg0, 1);
5356 tree arg02 = TREE_OPERAND (arg0, 2);
5357 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
5358 arg01 = fold (build1 (code, type, arg01));
5359 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
5360 arg02 = fold (build1 (code, type, arg02));
5361 t = fold (build (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5364 /* If this was a conversion, and all we did was to move into
5365 inside the COND_EXPR, bring it back out. But leave it if
5366 it is a conversion from integer to integer and the
5367 result precision is no wider than a word since such a
5368 conversion is cheap and may be optimized away by combine,
5369 while it couldn't if it were outside the COND_EXPR. Then return
5370 so we don't get into an infinite recursion loop taking the
5371 conversion out and then back in. */
5373 if ((code == NOP_EXPR || code == CONVERT_EXPR
5374 || code == NON_LVALUE_EXPR)
5375 && TREE_CODE (t) == COND_EXPR
5376 && TREE_CODE (TREE_OPERAND (t, 1)) == code
5377 && TREE_CODE (TREE_OPERAND (t, 2)) == code
5378 && ! VOID_TYPE_P (TREE_OPERAND (t, 1))
5379 && ! VOID_TYPE_P (TREE_OPERAND (t, 2))
5380 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))
5381 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 2), 0)))
5382 && ! (INTEGRAL_TYPE_P (TREE_TYPE (t))
5384 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0))))
5385 && TYPE_PRECISION (TREE_TYPE (t)) <= BITS_PER_WORD))
5386 t = build1 (code, type,
5388 TREE_TYPE (TREE_OPERAND
5389 (TREE_OPERAND (t, 1), 0)),
5390 TREE_OPERAND (t, 0),
5391 TREE_OPERAND (TREE_OPERAND (t, 1), 0),
5392 TREE_OPERAND (TREE_OPERAND (t, 2), 0)));
5395 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5396 return fold (build (COND_EXPR, type, arg0,
5397 fold (build1 (code, type, integer_one_node)),
5398 fold (build1 (code, type, integer_zero_node))));
5400 else if (TREE_CODE_CLASS (code) == '<'
5401 && TREE_CODE (arg0) == COMPOUND_EXPR)
5402 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5403 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5404 else if (TREE_CODE_CLASS (code) == '<'
5405 && TREE_CODE (arg1) == COMPOUND_EXPR)
5406 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5407 fold (build (code, type, arg0, TREE_OPERAND (arg1, 1))));
5408 else if (TREE_CODE_CLASS (code) == '2'
5409 || TREE_CODE_CLASS (code) == '<')
5411 if (TREE_CODE (arg1) == COMPOUND_EXPR
5412 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg1, 0))
5413 && ! TREE_SIDE_EFFECTS (arg0))
5414 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5415 fold (build (code, type,
5416 arg0, TREE_OPERAND (arg1, 1))));
5417 else if ((TREE_CODE (arg1) == COND_EXPR
5418 || (TREE_CODE_CLASS (TREE_CODE (arg1)) == '<'
5419 && TREE_CODE_CLASS (code) != '<'))
5420 && (TREE_CODE (arg0) != COND_EXPR
5421 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5422 && (! TREE_SIDE_EFFECTS (arg0)
5423 || ((*lang_hooks.decls.global_bindings_p) () == 0
5424 && ! CONTAINS_PLACEHOLDER_P (arg0))))
5426 fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5427 /*cond_first_p=*/0);
5428 else if (TREE_CODE (arg0) == COMPOUND_EXPR)
5429 return build (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5430 fold (build (code, type, TREE_OPERAND (arg0, 1), arg1)));
5431 else if ((TREE_CODE (arg0) == COND_EXPR
5432 || (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
5433 && TREE_CODE_CLASS (code) != '<'))
5434 && (TREE_CODE (arg1) != COND_EXPR
5435 || count_cond (arg0, 25) + count_cond (arg1, 25) <= 25)
5436 && (! TREE_SIDE_EFFECTS (arg1)
5437 || ((*lang_hooks.decls.global_bindings_p) () == 0
5438 && ! CONTAINS_PLACEHOLDER_P (arg1))))
5440 fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5441 /*cond_first_p=*/1);
5455 return fold (DECL_INITIAL (t));
5460 case FIX_TRUNC_EXPR:
5462 case FIX_FLOOR_EXPR:
5463 if (TREE_TYPE (TREE_OPERAND (t, 0)) == TREE_TYPE (t))
5464 return TREE_OPERAND (t, 0);
5466 /* Handle cases of two conversions in a row. */
5467 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5468 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5470 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5471 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5472 tree final_type = TREE_TYPE (t);
5473 int inside_int = INTEGRAL_TYPE_P (inside_type);
5474 int inside_ptr = POINTER_TYPE_P (inside_type);
5475 int inside_float = FLOAT_TYPE_P (inside_type);
5476 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5477 int inside_unsignedp = TREE_UNSIGNED (inside_type);
5478 int inter_int = INTEGRAL_TYPE_P (inter_type);
5479 int inter_ptr = POINTER_TYPE_P (inter_type);
5480 int inter_float = FLOAT_TYPE_P (inter_type);
5481 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5482 int inter_unsignedp = TREE_UNSIGNED (inter_type);
5483 int final_int = INTEGRAL_TYPE_P (final_type);
5484 int final_ptr = POINTER_TYPE_P (final_type);
5485 int final_float = FLOAT_TYPE_P (final_type);
5486 unsigned int final_prec = TYPE_PRECISION (final_type);
5487 int final_unsignedp = TREE_UNSIGNED (final_type);
5489 /* In addition to the cases of two conversions in a row
5490 handled below, if we are converting something to its own
5491 type via an object of identical or wider precision, neither
5492 conversion is needed. */
5493 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (final_type)
5494 && ((inter_int && final_int) || (inter_float && final_float))
5495 && inter_prec >= final_prec)
5496 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5498 /* Likewise, if the intermediate and final types are either both
5499 float or both integer, we don't need the middle conversion if
5500 it is wider than the final type and doesn't change the signedness
5501 (for integers). Avoid this if the final type is a pointer
5502 since then we sometimes need the inner conversion. Likewise if
5503 the outer has a precision not equal to the size of its mode. */
5504 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5505 || (inter_float && inside_float))
5506 && inter_prec >= inside_prec
5507 && (inter_float || inter_unsignedp == inside_unsignedp)
5508 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5509 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5511 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5513 /* If we have a sign-extension of a zero-extended value, we can
5514 replace that by a single zero-extension. */
5515 if (inside_int && inter_int && final_int
5516 && inside_prec < inter_prec && inter_prec < final_prec
5517 && inside_unsignedp && !inter_unsignedp)
5518 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5520 /* Two conversions in a row are not needed unless:
5521 - some conversion is floating-point (overstrict for now), or
5522 - the intermediate type is narrower than both initial and
5524 - the intermediate type and innermost type differ in signedness,
5525 and the outermost type is wider than the intermediate, or
5526 - the initial type is a pointer type and the precisions of the
5527 intermediate and final types differ, or
5528 - the final type is a pointer type and the precisions of the
5529 initial and intermediate types differ. */
5530 if (! inside_float && ! inter_float && ! final_float
5531 && (inter_prec > inside_prec || inter_prec > final_prec)
5532 && ! (inside_int && inter_int
5533 && inter_unsignedp != inside_unsignedp
5534 && inter_prec < final_prec)
5535 && ((inter_unsignedp && inter_prec > inside_prec)
5536 == (final_unsignedp && final_prec > inter_prec))
5537 && ! (inside_ptr && inter_prec != final_prec)
5538 && ! (final_ptr && inside_prec != inter_prec)
5539 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (final_type))
5540 && TYPE_MODE (final_type) == TYPE_MODE (inter_type))
5542 return convert (final_type, TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5545 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5546 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5547 /* Detect assigning a bitfield. */
5548 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5549 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5551 /* Don't leave an assignment inside a conversion
5552 unless assigning a bitfield. */
5553 tree prev = TREE_OPERAND (t, 0);
5556 TREE_OPERAND (t, 0) = TREE_OPERAND (prev, 1);
5557 /* First do the assignment, then return converted constant. */
5558 t = build (COMPOUND_EXPR, TREE_TYPE (t), prev, fold (t));
5563 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
5564 constants (if x has signed type, the sign bit cannot be set
5565 in c). This folds extension into the BIT_AND_EXPR. */
5566 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
5567 && TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE
5568 && TREE_CODE (TREE_OPERAND (t, 0)) == BIT_AND_EXPR
5569 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST)
5571 tree and = TREE_OPERAND (t, 0);
5572 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
5575 if (TREE_UNSIGNED (TREE_TYPE (and))
5576 || (TYPE_PRECISION (TREE_TYPE (t))
5577 <= TYPE_PRECISION (TREE_TYPE (and))))
5579 else if (TYPE_PRECISION (TREE_TYPE (and1))
5580 <= HOST_BITS_PER_WIDE_INT
5581 && host_integerp (and1, 1))
5583 unsigned HOST_WIDE_INT cst;
5585 cst = tree_low_cst (and1, 1);
5586 cst &= (HOST_WIDE_INT) -1
5587 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
5588 change = (cst == 0);
5589 #ifdef LOAD_EXTEND_OP
5591 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
5594 tree uns = (*lang_hooks.types.unsigned_type) (TREE_TYPE (and0));
5595 and0 = convert (uns, and0);
5596 and1 = convert (uns, and1);
5601 return fold (build (BIT_AND_EXPR, TREE_TYPE (t),
5602 convert (TREE_TYPE (t), and0),
5603 convert (TREE_TYPE (t), and1)));
5606 tem = fold_convert_const (code, TREE_TYPE (t), arg0);
5607 return tem ? tem : t;
5609 case VIEW_CONVERT_EXPR:
5610 if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
5611 return build1 (VIEW_CONVERT_EXPR, type,
5612 TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5616 if (TREE_CODE (arg0) == CONSTRUCTOR
5617 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
5619 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5626 if (TREE_CONSTANT (t) != wins)
5630 TREE_CONSTANT (t) = wins;
5635 if (negate_expr_p (arg0))
5636 return negate_expr (arg0);
5642 if (TREE_CODE (arg0) == INTEGER_CST)
5644 /* If the value is unsigned, then the absolute value is
5645 the same as the ordinary value. */
5646 if (TREE_UNSIGNED (type))
5648 /* Similarly, if the value is non-negative. */
5649 else if (INT_CST_LT (integer_minus_one_node, arg0))
5651 /* If the value is negative, then the absolute value is
5655 unsigned HOST_WIDE_INT low;
5657 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
5658 TREE_INT_CST_HIGH (arg0),
5660 t = build_int_2 (low, high);
5661 TREE_TYPE (t) = type;
5663 = (TREE_OVERFLOW (arg0)
5664 | force_fit_type (t, overflow));
5665 TREE_CONSTANT_OVERFLOW (t)
5666 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
5669 else if (TREE_CODE (arg0) == REAL_CST)
5671 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
5672 t = build_real (type,
5673 REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
5676 else if (TREE_CODE (arg0) == NEGATE_EXPR)
5677 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)));
5678 /* Convert fabs((double)float) into (double)fabsf(float). */
5679 else if (TREE_CODE (arg0) == NOP_EXPR
5680 && TREE_CODE (type) == REAL_TYPE)
5682 tree targ0 = strip_float_extensions (arg0);
5684 return convert (type, fold (build1 (ABS_EXPR, TREE_TYPE (targ0),
5687 else if (tree_expr_nonnegative_p (arg0))
5692 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
5693 return convert (type, arg0);
5694 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
5695 return build (COMPLEX_EXPR, type,
5696 TREE_OPERAND (arg0, 0),
5697 negate_expr (TREE_OPERAND (arg0, 1)));
5698 else if (TREE_CODE (arg0) == COMPLEX_CST)
5699 return build_complex (type, TREE_REALPART (arg0),
5700 negate_expr (TREE_IMAGPART (arg0)));
5701 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
5702 return fold (build (TREE_CODE (arg0), type,
5703 fold (build1 (CONJ_EXPR, type,
5704 TREE_OPERAND (arg0, 0))),
5705 fold (build1 (CONJ_EXPR,
5706 type, TREE_OPERAND (arg0, 1)))));
5707 else if (TREE_CODE (arg0) == CONJ_EXPR)
5708 return TREE_OPERAND (arg0, 0);
5714 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
5715 ~ TREE_INT_CST_HIGH (arg0));
5716 TREE_TYPE (t) = type;
5717 force_fit_type (t, 0);
5718 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg0);
5719 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (arg0);
5721 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
5722 return TREE_OPERAND (arg0, 0);
5726 /* A + (-B) -> A - B */
5727 if (TREE_CODE (arg1) == NEGATE_EXPR)
5728 return fold (build (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
5729 /* (-A) + B -> B - A */
5730 if (TREE_CODE (arg0) == NEGATE_EXPR)
5731 return fold (build (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
5732 else if (! FLOAT_TYPE_P (type))
5734 if (integer_zerop (arg1))
5735 return non_lvalue (convert (type, arg0));
5737 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
5738 with a constant, and the two constants have no bits in common,
5739 we should treat this as a BIT_IOR_EXPR since this may produce more
5741 if (TREE_CODE (arg0) == BIT_AND_EXPR
5742 && TREE_CODE (arg1) == BIT_AND_EXPR
5743 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
5744 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
5745 && integer_zerop (const_binop (BIT_AND_EXPR,
5746 TREE_OPERAND (arg0, 1),
5747 TREE_OPERAND (arg1, 1), 0)))
5749 code = BIT_IOR_EXPR;
5753 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
5754 (plus (plus (mult) (mult)) (foo)) so that we can
5755 take advantage of the factoring cases below. */
5756 if ((TREE_CODE (arg0) == PLUS_EXPR
5757 && TREE_CODE (arg1) == MULT_EXPR)
5758 || (TREE_CODE (arg1) == PLUS_EXPR
5759 && TREE_CODE (arg0) == MULT_EXPR))
5761 tree parg0, parg1, parg, marg;
5763 if (TREE_CODE (arg0) == PLUS_EXPR)
5764 parg = arg0, marg = arg1;
5766 parg = arg1, marg = arg0;
5767 parg0 = TREE_OPERAND (parg, 0);
5768 parg1 = TREE_OPERAND (parg, 1);
5772 if (TREE_CODE (parg0) == MULT_EXPR
5773 && TREE_CODE (parg1) != MULT_EXPR)
5774 return fold (build (PLUS_EXPR, type,
5775 fold (build (PLUS_EXPR, type,
5776 convert (type, parg0),
5777 convert (type, marg))),
5778 convert (type, parg1)));
5779 if (TREE_CODE (parg0) != MULT_EXPR
5780 && TREE_CODE (parg1) == MULT_EXPR)
5781 return fold (build (PLUS_EXPR, type,
5782 fold (build (PLUS_EXPR, type,
5783 convert (type, parg1),
5784 convert (type, marg))),
5785 convert (type, parg0)));
5788 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
5790 tree arg00, arg01, arg10, arg11;
5791 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
5793 /* (A * C) + (B * C) -> (A+B) * C.
5794 We are most concerned about the case where C is a constant,
5795 but other combinations show up during loop reduction. Since
5796 it is not difficult, try all four possibilities. */
5798 arg00 = TREE_OPERAND (arg0, 0);
5799 arg01 = TREE_OPERAND (arg0, 1);
5800 arg10 = TREE_OPERAND (arg1, 0);
5801 arg11 = TREE_OPERAND (arg1, 1);
5804 if (operand_equal_p (arg01, arg11, 0))
5805 same = arg01, alt0 = arg00, alt1 = arg10;
5806 else if (operand_equal_p (arg00, arg10, 0))
5807 same = arg00, alt0 = arg01, alt1 = arg11;
5808 else if (operand_equal_p (arg00, arg11, 0))
5809 same = arg00, alt0 = arg01, alt1 = arg10;
5810 else if (operand_equal_p (arg01, arg10, 0))
5811 same = arg01, alt0 = arg00, alt1 = arg11;
5813 /* No identical multiplicands; see if we can find a common
5814 power-of-two factor in non-power-of-two multiplies. This
5815 can help in multi-dimensional array access. */
5816 else if (TREE_CODE (arg01) == INTEGER_CST
5817 && TREE_CODE (arg11) == INTEGER_CST
5818 && TREE_INT_CST_HIGH (arg01) == 0
5819 && TREE_INT_CST_HIGH (arg11) == 0)
5821 HOST_WIDE_INT int01, int11, tmp;
5822 int01 = TREE_INT_CST_LOW (arg01);
5823 int11 = TREE_INT_CST_LOW (arg11);
5825 /* Move min of absolute values to int11. */
5826 if ((int01 >= 0 ? int01 : -int01)
5827 < (int11 >= 0 ? int11 : -int11))
5829 tmp = int01, int01 = int11, int11 = tmp;
5830 alt0 = arg00, arg00 = arg10, arg10 = alt0;
5831 alt0 = arg01, arg01 = arg11, arg11 = alt0;
5834 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
5836 alt0 = fold (build (MULT_EXPR, type, arg00,
5837 build_int_2 (int01 / int11, 0)));
5844 return fold (build (MULT_EXPR, type,
5845 fold (build (PLUS_EXPR, type, alt0, alt1)),
5851 /* See if ARG1 is zero and X + ARG1 reduces to X. */
5852 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
5853 return non_lvalue (convert (type, arg0));
5855 /* Likewise if the operands are reversed. */
5856 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
5857 return non_lvalue (convert (type, arg1));
5859 /* Convert x+x into x*2.0. */
5860 if (operand_equal_p (arg0, arg1, 0)
5861 && SCALAR_FLOAT_TYPE_P (type))
5862 return fold (build (MULT_EXPR, type, arg0,
5863 build_real (type, dconst2)));
5865 /* Convert x*c+x into x*(c+1). */
5866 if (flag_unsafe_math_optimizations
5867 && TREE_CODE (arg0) == MULT_EXPR
5868 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
5869 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
5870 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
5874 c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
5875 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
5876 return fold (build (MULT_EXPR, type, arg1,
5877 build_real (type, c)));
5880 /* Convert x+x*c into x*(c+1). */
5881 if (flag_unsafe_math_optimizations
5882 && TREE_CODE (arg1) == MULT_EXPR
5883 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
5884 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
5885 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
5889 c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
5890 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
5891 return fold (build (MULT_EXPR, type, arg0,
5892 build_real (type, c)));
5895 /* Convert x*c1+x*c2 into x*(c1+c2). */
5896 if (flag_unsafe_math_optimizations
5897 && TREE_CODE (arg0) == MULT_EXPR
5898 && TREE_CODE (arg1) == MULT_EXPR
5899 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
5900 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
5901 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
5902 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
5903 && operand_equal_p (TREE_OPERAND (arg0, 0),
5904 TREE_OPERAND (arg1, 0), 0))
5906 REAL_VALUE_TYPE c1, c2;
5908 c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
5909 c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
5910 real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
5911 return fold (build (MULT_EXPR, type,
5912 TREE_OPERAND (arg0, 0),
5913 build_real (type, c1)));
5918 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
5919 is a rotate of A by C1 bits. */
5920 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
5921 is a rotate of A by B bits. */
5923 enum tree_code code0, code1;
5924 code0 = TREE_CODE (arg0);
5925 code1 = TREE_CODE (arg1);
5926 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
5927 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
5928 && operand_equal_p (TREE_OPERAND (arg0, 0),
5929 TREE_OPERAND (arg1, 0), 0)
5930 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5932 tree tree01, tree11;
5933 enum tree_code code01, code11;
5935 tree01 = TREE_OPERAND (arg0, 1);
5936 tree11 = TREE_OPERAND (arg1, 1);
5937 STRIP_NOPS (tree01);
5938 STRIP_NOPS (tree11);
5939 code01 = TREE_CODE (tree01);
5940 code11 = TREE_CODE (tree11);
5941 if (code01 == INTEGER_CST
5942 && code11 == INTEGER_CST
5943 && TREE_INT_CST_HIGH (tree01) == 0
5944 && TREE_INT_CST_HIGH (tree11) == 0
5945 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
5946 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
5947 return build (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
5948 code0 == LSHIFT_EXPR ? tree01 : tree11);
5949 else if (code11 == MINUS_EXPR)
5951 tree tree110, tree111;
5952 tree110 = TREE_OPERAND (tree11, 0);
5953 tree111 = TREE_OPERAND (tree11, 1);
5954 STRIP_NOPS (tree110);
5955 STRIP_NOPS (tree111);
5956 if (TREE_CODE (tree110) == INTEGER_CST
5957 && 0 == compare_tree_int (tree110,
5959 (TREE_TYPE (TREE_OPERAND
5961 && operand_equal_p (tree01, tree111, 0))
5962 return build ((code0 == LSHIFT_EXPR
5965 type, TREE_OPERAND (arg0, 0), tree01);
5967 else if (code01 == MINUS_EXPR)
5969 tree tree010, tree011;
5970 tree010 = TREE_OPERAND (tree01, 0);
5971 tree011 = TREE_OPERAND (tree01, 1);
5972 STRIP_NOPS (tree010);
5973 STRIP_NOPS (tree011);
5974 if (TREE_CODE (tree010) == INTEGER_CST
5975 && 0 == compare_tree_int (tree010,
5977 (TREE_TYPE (TREE_OPERAND
5979 && operand_equal_p (tree11, tree011, 0))
5980 return build ((code0 != LSHIFT_EXPR
5983 type, TREE_OPERAND (arg0, 0), tree11);
5989 /* In most languages, can't associate operations on floats through
5990 parentheses. Rather than remember where the parentheses were, we
5991 don't associate floats at all, unless the user has specified
5992 -funsafe-math-optimizations. */
5995 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
5997 tree var0, con0, lit0, minus_lit0;
5998 tree var1, con1, lit1, minus_lit1;
6000 /* Split both trees into variables, constants, and literals. Then
6001 associate each group together, the constants with literals,
6002 then the result with variables. This increases the chances of
6003 literals being recombined later and of generating relocatable
6004 expressions for the sum of a constant and literal. */
6005 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
6006 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
6007 code == MINUS_EXPR);
6009 /* Only do something if we found more than two objects. Otherwise,
6010 nothing has changed and we risk infinite recursion. */
6011 if (2 < ((var0 != 0) + (var1 != 0)
6012 + (con0 != 0) + (con1 != 0)
6013 + (lit0 != 0) + (lit1 != 0)
6014 + (minus_lit0 != 0) + (minus_lit1 != 0)))
6016 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
6017 if (code == MINUS_EXPR)
6020 var0 = associate_trees (var0, var1, code, type);
6021 con0 = associate_trees (con0, con1, code, type);
6022 lit0 = associate_trees (lit0, lit1, code, type);
6023 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
6025 /* Preserve the MINUS_EXPR if the negative part of the literal is
6026 greater than the positive part. Otherwise, the multiplicative
6027 folding code (i.e extract_muldiv) may be fooled in case
6028 unsigned constants are subtracted, like in the following
6029 example: ((X*2 + 4) - 8U)/2. */
6030 if (minus_lit0 && lit0)
6032 if (TREE_CODE (lit0) == INTEGER_CST
6033 && TREE_CODE (minus_lit0) == INTEGER_CST
6034 && tree_int_cst_lt (lit0, minus_lit0))
6036 minus_lit0 = associate_trees (minus_lit0, lit0,
6042 lit0 = associate_trees (lit0, minus_lit0,
6050 return convert (type, associate_trees (var0, minus_lit0,
6054 con0 = associate_trees (con0, minus_lit0,
6056 return convert (type, associate_trees (var0, con0,
6061 con0 = associate_trees (con0, lit0, code, type);
6062 return convert (type, associate_trees (var0, con0, code, type));
6068 t1 = const_binop (code, arg0, arg1, 0);
6069 if (t1 != NULL_TREE)
6071 /* The return value should always have
6072 the same type as the original expression. */
6073 if (TREE_TYPE (t1) != TREE_TYPE (t))
6074 t1 = convert (TREE_TYPE (t), t1);
6081 /* A - (-B) -> A + B */
6082 if (TREE_CODE (arg1) == NEGATE_EXPR)
6083 return fold (build (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6084 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
6085 if (TREE_CODE (arg0) == NEGATE_EXPR
6086 && (FLOAT_TYPE_P (type)
6087 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
6088 && negate_expr_p (arg1)
6089 && reorder_operands_p (arg0, arg1))
6090 return fold (build (MINUS_EXPR, type, negate_expr (arg1),
6091 TREE_OPERAND (arg0, 0)));
6093 if (! FLOAT_TYPE_P (type))
6095 if (! wins && integer_zerop (arg0))
6096 return negate_expr (convert (type, arg1));
6097 if (integer_zerop (arg1))
6098 return non_lvalue (convert (type, arg0));
6100 /* (A * C) - (B * C) -> (A-B) * C. Since we are most concerned
6101 about the case where C is a constant, just try one of the
6102 four possibilities. */
6104 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR
6105 && operand_equal_p (TREE_OPERAND (arg0, 1),
6106 TREE_OPERAND (arg1, 1), 0))
6107 return fold (build (MULT_EXPR, type,
6108 fold (build (MINUS_EXPR, type,
6109 TREE_OPERAND (arg0, 0),
6110 TREE_OPERAND (arg1, 0))),
6111 TREE_OPERAND (arg0, 1)));
6113 /* Fold A - (A & B) into ~B & A. */
6114 if (!TREE_SIDE_EFFECTS (arg0)
6115 && TREE_CODE (arg1) == BIT_AND_EXPR)
6117 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
6118 return fold (build (BIT_AND_EXPR, type,
6119 fold (build1 (BIT_NOT_EXPR, type,
6120 TREE_OPERAND (arg1, 0))),
6122 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
6123 return fold (build (BIT_AND_EXPR, type,
6124 fold (build1 (BIT_NOT_EXPR, type,
6125 TREE_OPERAND (arg1, 1))),
6129 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
6130 any power of 2 minus 1. */
6131 if (TREE_CODE (arg0) == BIT_AND_EXPR
6132 && TREE_CODE (arg1) == BIT_AND_EXPR
6133 && operand_equal_p (TREE_OPERAND (arg0, 0),
6134 TREE_OPERAND (arg1, 0), 0))
6136 tree mask0 = TREE_OPERAND (arg0, 1);
6137 tree mask1 = TREE_OPERAND (arg1, 1);
6138 tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
6140 if (operand_equal_p (tem, mask1, 0))
6142 tem = fold (build (BIT_XOR_EXPR, type,
6143 TREE_OPERAND (arg0, 0), mask1));
6144 return fold (build (MINUS_EXPR, type, tem, mask1));
6149 /* See if ARG1 is zero and X - ARG1 reduces to X. */
6150 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
6151 return non_lvalue (convert (type, arg0));
6153 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
6154 ARG0 is zero and X + ARG0 reduces to X, since that would mean
6155 (-ARG1 + ARG0) reduces to -ARG1. */
6156 else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6157 return negate_expr (convert (type, arg1));
6159 /* Fold &x - &x. This can happen from &x.foo - &x.
6160 This is unsafe for certain floats even in non-IEEE formats.
6161 In IEEE, it is unsafe because it does wrong for NaNs.
6162 Also note that operand_equal_p is always false if an operand
6165 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
6166 && operand_equal_p (arg0, arg1, 0))
6167 return convert (type, integer_zero_node);
6172 /* (-A) * (-B) -> A * B */
6173 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6174 return fold (build (MULT_EXPR, type,
6175 TREE_OPERAND (arg0, 0),
6176 negate_expr (arg1)));
6177 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6178 return fold (build (MULT_EXPR, type,
6180 TREE_OPERAND (arg1, 0)));
6182 if (! FLOAT_TYPE_P (type))
6184 if (integer_zerop (arg1))
6185 return omit_one_operand (type, arg1, arg0);
6186 if (integer_onep (arg1))
6187 return non_lvalue (convert (type, arg0));
6189 /* (a * (1 << b)) is (a << b) */
6190 if (TREE_CODE (arg1) == LSHIFT_EXPR
6191 && integer_onep (TREE_OPERAND (arg1, 0)))
6192 return fold (build (LSHIFT_EXPR, type, arg0,
6193 TREE_OPERAND (arg1, 1)));
6194 if (TREE_CODE (arg0) == LSHIFT_EXPR
6195 && integer_onep (TREE_OPERAND (arg0, 0)))
6196 return fold (build (LSHIFT_EXPR, type, arg1,
6197 TREE_OPERAND (arg0, 1)));
6199 if (TREE_CODE (arg1) == INTEGER_CST
6200 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0),
6201 convert (type, arg1),
6203 return convert (type, tem);
6208 /* Maybe fold x * 0 to 0. The expressions aren't the same
6209 when x is NaN, since x * 0 is also NaN. Nor are they the
6210 same in modes with signed zeros, since multiplying a
6211 negative value by 0 gives -0, not +0. */
6212 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
6213 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
6214 && real_zerop (arg1))
6215 return omit_one_operand (type, arg1, arg0);
6216 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
6217 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6218 && real_onep (arg1))
6219 return non_lvalue (convert (type, arg0));
6221 /* Transform x * -1.0 into -x. */
6222 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6223 && real_minus_onep (arg1))
6224 return fold (build1 (NEGATE_EXPR, type, arg0));
6226 /* Convert (C1/X)*C2 into (C1*C2)/X. */
6227 if (flag_unsafe_math_optimizations
6228 && TREE_CODE (arg0) == RDIV_EXPR
6229 && TREE_CODE (arg1) == REAL_CST
6230 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
6232 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
6235 return fold (build (RDIV_EXPR, type, tem,
6236 TREE_OPERAND (arg0, 1)));
6239 if (flag_unsafe_math_optimizations)
6241 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
6242 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
6244 /* Optimizations of sqrt(...)*sqrt(...). */
6245 if ((fcode0 == BUILT_IN_SQRT && fcode1 == BUILT_IN_SQRT)
6246 || (fcode0 == BUILT_IN_SQRTF && fcode1 == BUILT_IN_SQRTF)
6247 || (fcode0 == BUILT_IN_SQRTL && fcode1 == BUILT_IN_SQRTL))
6249 tree sqrtfn, arg, arglist;
6250 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6251 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6253 /* Optimize sqrt(x)*sqrt(x) as x. */
6254 if (operand_equal_p (arg00, arg10, 0)
6255 && ! HONOR_SNANS (TYPE_MODE (type)))
6258 /* Optimize sqrt(x)*sqrt(y) as sqrt(x*y). */
6259 sqrtfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6260 arg = fold (build (MULT_EXPR, type, arg00, arg10));
6261 arglist = build_tree_list (NULL_TREE, arg);
6262 return build_function_call_expr (sqrtfn, arglist);
6265 /* Optimize expN(x)*expN(y) as expN(x+y). */
6266 if (fcode0 == fcode1
6267 && (fcode0 == BUILT_IN_EXP
6268 || fcode0 == BUILT_IN_EXPF
6269 || fcode0 == BUILT_IN_EXPL
6270 || fcode0 == BUILT_IN_EXP2
6271 || fcode0 == BUILT_IN_EXP2F
6272 || fcode0 == BUILT_IN_EXP2L
6273 || fcode0 == BUILT_IN_EXP10
6274 || fcode0 == BUILT_IN_EXP10F
6275 || fcode0 == BUILT_IN_EXP10L
6276 || fcode0 == BUILT_IN_POW10
6277 || fcode0 == BUILT_IN_POW10F
6278 || fcode0 == BUILT_IN_POW10L))
6280 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6281 tree arg = build (PLUS_EXPR, type,
6282 TREE_VALUE (TREE_OPERAND (arg0, 1)),
6283 TREE_VALUE (TREE_OPERAND (arg1, 1)));
6284 tree arglist = build_tree_list (NULL_TREE, fold (arg));
6285 return build_function_call_expr (expfn, arglist);
6288 /* Optimizations of pow(...)*pow(...). */
6289 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
6290 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
6291 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
6293 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6294 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6296 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6297 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6300 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
6301 if (operand_equal_p (arg01, arg11, 0))
6303 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6304 tree arg = build (MULT_EXPR, type, arg00, arg10);
6305 tree arglist = tree_cons (NULL_TREE, fold (arg),
6306 build_tree_list (NULL_TREE,
6308 return build_function_call_expr (powfn, arglist);
6311 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
6312 if (operand_equal_p (arg00, arg10, 0))
6314 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6315 tree arg = fold (build (PLUS_EXPR, type, arg01, arg11));
6316 tree arglist = tree_cons (NULL_TREE, arg00,
6317 build_tree_list (NULL_TREE,
6319 return build_function_call_expr (powfn, arglist);
6323 /* Optimize tan(x)*cos(x) as sin(x). */
6324 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
6325 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
6326 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
6327 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
6328 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
6329 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
6330 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
6331 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
6339 sinfn = implicit_built_in_decls[BUILT_IN_SIN];
6343 sinfn = implicit_built_in_decls[BUILT_IN_SINF];
6347 sinfn = implicit_built_in_decls[BUILT_IN_SINL];
6353 if (sinfn != NULL_TREE)
6354 return build_function_call_expr (sinfn,
6355 TREE_OPERAND (arg0, 1));
6358 /* Optimize x*pow(x,c) as pow(x,c+1). */
6359 if (fcode1 == BUILT_IN_POW
6360 || fcode1 == BUILT_IN_POWF
6361 || fcode1 == BUILT_IN_POWL)
6363 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6364 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6366 if (TREE_CODE (arg11) == REAL_CST
6367 && ! TREE_CONSTANT_OVERFLOW (arg11)
6368 && operand_equal_p (arg0, arg10, 0))
6370 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6374 c = TREE_REAL_CST (arg11);
6375 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6376 arg = build_real (type, c);
6377 arglist = build_tree_list (NULL_TREE, arg);
6378 arglist = tree_cons (NULL_TREE, arg0, arglist);
6379 return build_function_call_expr (powfn, arglist);
6383 /* Optimize pow(x,c)*x as pow(x,c+1). */
6384 if (fcode0 == BUILT_IN_POW
6385 || fcode0 == BUILT_IN_POWF
6386 || fcode0 == BUILT_IN_POWL)
6388 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6389 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6391 if (TREE_CODE (arg01) == REAL_CST
6392 && ! TREE_CONSTANT_OVERFLOW (arg01)
6393 && operand_equal_p (arg1, arg00, 0))
6395 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6399 c = TREE_REAL_CST (arg01);
6400 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6401 arg = build_real (type, c);
6402 arglist = build_tree_list (NULL_TREE, arg);
6403 arglist = tree_cons (NULL_TREE, arg1, arglist);
6404 return build_function_call_expr (powfn, arglist);
6408 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
6410 && operand_equal_p (arg0, arg1, 0))
6414 if (type == double_type_node)
6415 powfn = implicit_built_in_decls[BUILT_IN_POW];
6416 else if (type == float_type_node)
6417 powfn = implicit_built_in_decls[BUILT_IN_POWF];
6418 else if (type == long_double_type_node)
6419 powfn = implicit_built_in_decls[BUILT_IN_POWL];
6425 tree arg = build_real (type, dconst2);
6426 tree arglist = build_tree_list (NULL_TREE, arg);
6427 arglist = tree_cons (NULL_TREE, arg0, arglist);
6428 return build_function_call_expr (powfn, arglist);
6437 if (integer_all_onesp (arg1))
6438 return omit_one_operand (type, arg1, arg0);
6439 if (integer_zerop (arg1))
6440 return non_lvalue (convert (type, arg0));
6441 t1 = distribute_bit_expr (code, type, arg0, arg1);
6442 if (t1 != NULL_TREE)
6445 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
6447 This results in more efficient code for machines without a NAND
6448 instruction. Combine will canonicalize to the first form
6449 which will allow use of NAND instructions provided by the
6450 backend if they exist. */
6451 if (TREE_CODE (arg0) == BIT_NOT_EXPR
6452 && TREE_CODE (arg1) == BIT_NOT_EXPR)
6454 return fold (build1 (BIT_NOT_EXPR, type,
6455 build (BIT_AND_EXPR, type,
6456 TREE_OPERAND (arg0, 0),
6457 TREE_OPERAND (arg1, 0))));
6460 /* See if this can be simplified into a rotate first. If that
6461 is unsuccessful continue in the association code. */
6465 if (integer_zerop (arg1))
6466 return non_lvalue (convert (type, arg0));
6467 if (integer_all_onesp (arg1))
6468 return fold (build1 (BIT_NOT_EXPR, type, arg0));
6470 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
6471 with a constant, and the two constants have no bits in common,
6472 we should treat this as a BIT_IOR_EXPR since this may produce more
6474 if (TREE_CODE (arg0) == BIT_AND_EXPR
6475 && TREE_CODE (arg1) == BIT_AND_EXPR
6476 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6477 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6478 && integer_zerop (const_binop (BIT_AND_EXPR,
6479 TREE_OPERAND (arg0, 1),
6480 TREE_OPERAND (arg1, 1), 0)))
6482 code = BIT_IOR_EXPR;
6486 /* See if this can be simplified into a rotate first. If that
6487 is unsuccessful continue in the association code. */
6491 if (integer_all_onesp (arg1))
6492 return non_lvalue (convert (type, arg0));
6493 if (integer_zerop (arg1))
6494 return omit_one_operand (type, arg1, arg0);
6495 t1 = distribute_bit_expr (code, type, arg0, arg1);
6496 if (t1 != NULL_TREE)
6498 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
6499 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
6500 && TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6503 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
6505 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
6506 && (~TREE_INT_CST_LOW (arg1)
6507 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6508 return build1 (NOP_EXPR, type, TREE_OPERAND (arg0, 0));
6511 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
6513 This results in more efficient code for machines without a NOR
6514 instruction. Combine will canonicalize to the first form
6515 which will allow use of NOR instructions provided by the
6516 backend if they exist. */
6517 if (TREE_CODE (arg0) == BIT_NOT_EXPR
6518 && TREE_CODE (arg1) == BIT_NOT_EXPR)
6520 return fold (build1 (BIT_NOT_EXPR, type,
6521 build (BIT_IOR_EXPR, type,
6522 TREE_OPERAND (arg0, 0),
6523 TREE_OPERAND (arg1, 0))));
6529 /* Don't touch a floating-point divide by zero unless the mode
6530 of the constant can represent infinity. */
6531 if (TREE_CODE (arg1) == REAL_CST
6532 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
6533 && real_zerop (arg1))
6536 /* (-A) / (-B) -> A / B */
6537 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6538 return fold (build (RDIV_EXPR, type,
6539 TREE_OPERAND (arg0, 0),
6540 negate_expr (arg1)));
6541 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6542 return fold (build (RDIV_EXPR, type,
6544 TREE_OPERAND (arg1, 0)));
6546 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
6547 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6548 && real_onep (arg1))
6549 return non_lvalue (convert (type, arg0));
6551 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
6552 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6553 && real_minus_onep (arg1))
6554 return non_lvalue (convert (type, negate_expr (arg0)));
6556 /* If ARG1 is a constant, we can convert this to a multiply by the
6557 reciprocal. This does not have the same rounding properties,
6558 so only do this if -funsafe-math-optimizations. We can actually
6559 always safely do it if ARG1 is a power of two, but it's hard to
6560 tell if it is or not in a portable manner. */
6561 if (TREE_CODE (arg1) == REAL_CST)
6563 if (flag_unsafe_math_optimizations
6564 && 0 != (tem = const_binop (code, build_real (type, dconst1),
6566 return fold (build (MULT_EXPR, type, arg0, tem));
6567 /* Find the reciprocal if optimizing and the result is exact. */
6571 r = TREE_REAL_CST (arg1);
6572 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
6574 tem = build_real (type, r);
6575 return fold (build (MULT_EXPR, type, arg0, tem));
6579 /* Convert A/B/C to A/(B*C). */
6580 if (flag_unsafe_math_optimizations
6581 && TREE_CODE (arg0) == RDIV_EXPR)
6582 return fold (build (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6583 fold (build (MULT_EXPR, type,
6584 TREE_OPERAND (arg0, 1), arg1))));
6586 /* Convert A/(B/C) to (A/B)*C. */
6587 if (flag_unsafe_math_optimizations
6588 && TREE_CODE (arg1) == RDIV_EXPR)
6589 return fold (build (MULT_EXPR, type,
6590 fold (build (RDIV_EXPR, type, arg0,
6591 TREE_OPERAND (arg1, 0))),
6592 TREE_OPERAND (arg1, 1)));
6594 /* Convert C1/(X*C2) into (C1/C2)/X. */
6595 if (flag_unsafe_math_optimizations
6596 && TREE_CODE (arg1) == MULT_EXPR
6597 && TREE_CODE (arg0) == REAL_CST
6598 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
6600 tree tem = const_binop (RDIV_EXPR, arg0,
6601 TREE_OPERAND (arg1, 1), 0);
6603 return fold (build (RDIV_EXPR, type, tem,
6604 TREE_OPERAND (arg1, 0)));
6607 if (flag_unsafe_math_optimizations)
6609 enum built_in_function fcode = builtin_mathfn_code (arg1);
6610 /* Optimize x/expN(y) into x*expN(-y). */
6611 if (fcode == BUILT_IN_EXP
6612 || fcode == BUILT_IN_EXPF
6613 || fcode == BUILT_IN_EXPL
6614 || fcode == BUILT_IN_EXP2
6615 || fcode == BUILT_IN_EXP2F
6616 || fcode == BUILT_IN_EXP2L
6617 || fcode == BUILT_IN_EXP10
6618 || fcode == BUILT_IN_EXP10F
6619 || fcode == BUILT_IN_EXP10L
6620 || fcode == BUILT_IN_POW10
6621 || fcode == BUILT_IN_POW10F
6622 || fcode == BUILT_IN_POW10L)
6624 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6625 tree arg = build1 (NEGATE_EXPR, type,
6626 TREE_VALUE (TREE_OPERAND (arg1, 1)));
6627 tree arglist = build_tree_list (NULL_TREE, fold (arg));
6628 arg1 = build_function_call_expr (expfn, arglist);
6629 return fold (build (MULT_EXPR, type, arg0, arg1));
6632 /* Optimize x/pow(y,z) into x*pow(y,-z). */
6633 if (fcode == BUILT_IN_POW
6634 || fcode == BUILT_IN_POWF
6635 || fcode == BUILT_IN_POWL)
6637 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6638 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6639 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
6640 tree neg11 = fold (build1 (NEGATE_EXPR, type, arg11));
6641 tree arglist = tree_cons(NULL_TREE, arg10,
6642 build_tree_list (NULL_TREE, neg11));
6643 arg1 = build_function_call_expr (powfn, arglist);
6644 return fold (build (MULT_EXPR, type, arg0, arg1));
6648 if (flag_unsafe_math_optimizations)
6650 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
6651 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
6653 /* Optimize sin(x)/cos(x) as tan(x). */
6654 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
6655 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
6656 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
6657 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
6658 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
6662 if (fcode0 == BUILT_IN_SIN)
6663 tanfn = implicit_built_in_decls[BUILT_IN_TAN];
6664 else if (fcode0 == BUILT_IN_SINF)
6665 tanfn = implicit_built_in_decls[BUILT_IN_TANF];
6666 else if (fcode0 == BUILT_IN_SINL)
6667 tanfn = implicit_built_in_decls[BUILT_IN_TANL];
6671 if (tanfn != NULL_TREE)
6672 return build_function_call_expr (tanfn,
6673 TREE_OPERAND (arg0, 1));
6676 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
6677 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
6678 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
6679 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
6680 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
6681 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
6685 if (fcode0 == BUILT_IN_COS)
6686 tanfn = implicit_built_in_decls[BUILT_IN_TAN];
6687 else if (fcode0 == BUILT_IN_COSF)
6688 tanfn = implicit_built_in_decls[BUILT_IN_TANF];
6689 else if (fcode0 == BUILT_IN_COSL)
6690 tanfn = implicit_built_in_decls[BUILT_IN_TANL];
6694 if (tanfn != NULL_TREE)
6696 tree tmp = TREE_OPERAND (arg0, 1);
6697 tmp = build_function_call_expr (tanfn, tmp);
6698 return fold (build (RDIV_EXPR, type,
6699 build_real (type, dconst1),
6704 /* Optimize pow(x,c)/x as pow(x,c-1). */
6705 if (fcode0 == BUILT_IN_POW
6706 || fcode0 == BUILT_IN_POWF
6707 || fcode0 == BUILT_IN_POWL)
6709 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6710 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
6711 if (TREE_CODE (arg01) == REAL_CST
6712 && ! TREE_CONSTANT_OVERFLOW (arg01)
6713 && operand_equal_p (arg1, arg00, 0))
6715 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6719 c = TREE_REAL_CST (arg01);
6720 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
6721 arg = build_real (type, c);
6722 arglist = build_tree_list (NULL_TREE, arg);
6723 arglist = tree_cons (NULL_TREE, arg1, arglist);
6724 return build_function_call_expr (powfn, arglist);
6730 case TRUNC_DIV_EXPR:
6731 case ROUND_DIV_EXPR:
6732 case FLOOR_DIV_EXPR:
6734 case EXACT_DIV_EXPR:
6735 if (integer_onep (arg1))
6736 return non_lvalue (convert (type, arg0));
6737 if (integer_zerop (arg1))
6740 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
6741 operation, EXACT_DIV_EXPR.
6743 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
6744 At one time others generated faster code, it's not clear if they do
6745 after the last round to changes to the DIV code in expmed.c. */
6746 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
6747 && multiple_of_p (type, arg0, arg1))
6748 return fold (build (EXACT_DIV_EXPR, type, arg0, arg1));
6750 if (TREE_CODE (arg1) == INTEGER_CST
6751 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
6753 return convert (type, tem);
6758 case FLOOR_MOD_EXPR:
6759 case ROUND_MOD_EXPR:
6760 case TRUNC_MOD_EXPR:
6761 if (integer_onep (arg1))
6762 return omit_one_operand (type, integer_zero_node, arg0);
6763 if (integer_zerop (arg1))
6766 if (TREE_CODE (arg1) == INTEGER_CST
6767 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
6769 return convert (type, tem);
6775 if (integer_all_onesp (arg0))
6776 return omit_one_operand (type, arg0, arg1);
6780 /* Optimize -1 >> x for arithmetic right shifts. */
6781 if (integer_all_onesp (arg0) && ! TREE_UNSIGNED (type))
6782 return omit_one_operand (type, arg0, arg1);
6783 /* ... fall through ... */
6787 if (integer_zerop (arg1))
6788 return non_lvalue (convert (type, arg0));
6789 if (integer_zerop (arg0))
6790 return omit_one_operand (type, arg0, arg1);
6792 /* Since negative shift count is not well-defined,
6793 don't try to compute it in the compiler. */
6794 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
6796 /* Rewrite an LROTATE_EXPR by a constant into an
6797 RROTATE_EXPR by a new constant. */
6798 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
6800 tree tem = build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0);
6801 tem = convert (TREE_TYPE (arg1), tem);
6802 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
6803 return fold (build (RROTATE_EXPR, type, arg0, tem));
6806 /* If we have a rotate of a bit operation with the rotate count and
6807 the second operand of the bit operation both constant,
6808 permute the two operations. */
6809 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6810 && (TREE_CODE (arg0) == BIT_AND_EXPR
6811 || TREE_CODE (arg0) == BIT_IOR_EXPR
6812 || TREE_CODE (arg0) == BIT_XOR_EXPR)
6813 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
6814 return fold (build (TREE_CODE (arg0), type,
6815 fold (build (code, type,
6816 TREE_OPERAND (arg0, 0), arg1)),
6817 fold (build (code, type,
6818 TREE_OPERAND (arg0, 1), arg1))));
6820 /* Two consecutive rotates adding up to the width of the mode can
6822 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
6823 && TREE_CODE (arg0) == RROTATE_EXPR
6824 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6825 && TREE_INT_CST_HIGH (arg1) == 0
6826 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
6827 && ((TREE_INT_CST_LOW (arg1)
6828 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
6829 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
6830 return TREE_OPERAND (arg0, 0);
6835 if (operand_equal_p (arg0, arg1, 0))
6836 return omit_one_operand (type, arg0, arg1);
6837 if (INTEGRAL_TYPE_P (type)
6838 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), 1))
6839 return omit_one_operand (type, arg1, arg0);
6843 if (operand_equal_p (arg0, arg1, 0))
6844 return omit_one_operand (type, arg0, arg1);
6845 if (INTEGRAL_TYPE_P (type)
6846 && TYPE_MAX_VALUE (type)
6847 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), 1))
6848 return omit_one_operand (type, arg1, arg0);
6851 case TRUTH_NOT_EXPR:
6852 /* Note that the operand of this must be an int
6853 and its values must be 0 or 1.
6854 ("true" is a fixed value perhaps depending on the language,
6855 but we don't handle values other than 1 correctly yet.) */
6856 tem = invert_truthvalue (arg0);
6857 /* Avoid infinite recursion. */
6858 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
6860 tem = fold_single_bit_test (code, arg0, arg1, type);
6865 return convert (type, tem);
6867 case TRUTH_ANDIF_EXPR:
6868 /* Note that the operands of this must be ints
6869 and their values must be 0 or 1.
6870 ("true" is a fixed value perhaps depending on the language.) */
6871 /* If first arg is constant zero, return it. */
6872 if (integer_zerop (arg0))
6873 return convert (type, arg0);
6874 case TRUTH_AND_EXPR:
6875 /* If either arg is constant true, drop it. */
6876 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6877 return non_lvalue (convert (type, arg1));
6878 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
6879 /* Preserve sequence points. */
6880 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6881 return non_lvalue (convert (type, arg0));
6882 /* If second arg is constant zero, result is zero, but first arg
6883 must be evaluated. */
6884 if (integer_zerop (arg1))
6885 return omit_one_operand (type, arg1, arg0);
6886 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
6887 case will be handled here. */
6888 if (integer_zerop (arg0))
6889 return omit_one_operand (type, arg0, arg1);
6892 /* We only do these simplifications if we are optimizing. */
6896 /* Check for things like (A || B) && (A || C). We can convert this
6897 to A || (B && C). Note that either operator can be any of the four
6898 truth and/or operations and the transformation will still be
6899 valid. Also note that we only care about order for the
6900 ANDIF and ORIF operators. If B contains side effects, this
6901 might change the truth-value of A. */
6902 if (TREE_CODE (arg0) == TREE_CODE (arg1)
6903 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
6904 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
6905 || TREE_CODE (arg0) == TRUTH_AND_EXPR
6906 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
6907 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
6909 tree a00 = TREE_OPERAND (arg0, 0);
6910 tree a01 = TREE_OPERAND (arg0, 1);
6911 tree a10 = TREE_OPERAND (arg1, 0);
6912 tree a11 = TREE_OPERAND (arg1, 1);
6913 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
6914 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
6915 && (code == TRUTH_AND_EXPR
6916 || code == TRUTH_OR_EXPR));
6918 if (operand_equal_p (a00, a10, 0))
6919 return fold (build (TREE_CODE (arg0), type, a00,
6920 fold (build (code, type, a01, a11))));
6921 else if (commutative && operand_equal_p (a00, a11, 0))
6922 return fold (build (TREE_CODE (arg0), type, a00,
6923 fold (build (code, type, a01, a10))));
6924 else if (commutative && operand_equal_p (a01, a10, 0))
6925 return fold (build (TREE_CODE (arg0), type, a01,
6926 fold (build (code, type, a00, a11))));
6928 /* This case if tricky because we must either have commutative
6929 operators or else A10 must not have side-effects. */
6931 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
6932 && operand_equal_p (a01, a11, 0))
6933 return fold (build (TREE_CODE (arg0), type,
6934 fold (build (code, type, a00, a10)),
6938 /* See if we can build a range comparison. */
6939 if (0 != (tem = fold_range_test (t)))
6942 /* Check for the possibility of merging component references. If our
6943 lhs is another similar operation, try to merge its rhs with our
6944 rhs. Then try to merge our lhs and rhs. */
6945 if (TREE_CODE (arg0) == code
6946 && 0 != (tem = fold_truthop (code, type,
6947 TREE_OPERAND (arg0, 1), arg1)))
6948 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
6950 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
6955 case TRUTH_ORIF_EXPR:
6956 /* Note that the operands of this must be ints
6957 and their values must be 0 or true.
6958 ("true" is a fixed value perhaps depending on the language.) */
6959 /* If first arg is constant true, return it. */
6960 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6961 return convert (type, arg0);
6963 /* If either arg is constant zero, drop it. */
6964 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
6965 return non_lvalue (convert (type, arg1));
6966 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
6967 /* Preserve sequence points. */
6968 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
6969 return non_lvalue (convert (type, arg0));
6970 /* If second arg is constant true, result is true, but we must
6971 evaluate first arg. */
6972 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
6973 return omit_one_operand (type, arg1, arg0);
6974 /* Likewise for first arg, but note this only occurs here for
6976 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
6977 return omit_one_operand (type, arg0, arg1);
6980 case TRUTH_XOR_EXPR:
6981 /* If either arg is constant zero, drop it. */
6982 if (integer_zerop (arg0))
6983 return non_lvalue (convert (type, arg1));
6984 if (integer_zerop (arg1))
6985 return non_lvalue (convert (type, arg0));
6986 /* If either arg is constant true, this is a logical inversion. */
6987 if (integer_onep (arg0))
6988 return non_lvalue (convert (type, invert_truthvalue (arg1)));
6989 if (integer_onep (arg1))
6990 return non_lvalue (convert (type, invert_truthvalue (arg0)));
6999 /* If one arg is a real or integer constant, put it last. */
7000 if (tree_swap_operands_p (arg0, arg1, true))
7001 return fold (build (swap_tree_comparison (code), type, arg1, arg0));
7003 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7005 tree targ0 = strip_float_extensions (arg0);
7006 tree targ1 = strip_float_extensions (arg1);
7007 tree newtype = TREE_TYPE (targ0);
7009 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7010 newtype = TREE_TYPE (targ1);
7012 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7013 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7014 return fold (build (code, type, convert (newtype, targ0),
7015 convert (newtype, targ1)));
7017 /* (-a) CMP (-b) -> b CMP a */
7018 if (TREE_CODE (arg0) == NEGATE_EXPR
7019 && TREE_CODE (arg1) == NEGATE_EXPR)
7020 return fold (build (code, type, TREE_OPERAND (arg1, 0),
7021 TREE_OPERAND (arg0, 0)));
7023 if (TREE_CODE (arg1) == REAL_CST)
7025 REAL_VALUE_TYPE cst;
7026 cst = TREE_REAL_CST (arg1);
7028 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7029 if (TREE_CODE (arg0) == NEGATE_EXPR)
7031 fold (build (swap_tree_comparison (code), type,
7032 TREE_OPERAND (arg0, 0),
7033 build_real (TREE_TYPE (arg1),
7034 REAL_VALUE_NEGATE (cst))));
7036 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7037 /* a CMP (-0) -> a CMP 0 */
7038 if (REAL_VALUE_MINUS_ZERO (cst))
7039 return fold (build (code, type, arg0,
7040 build_real (TREE_TYPE (arg1), dconst0)));
7042 /* x != NaN is always true, other ops are always false. */
7043 if (REAL_VALUE_ISNAN (cst)
7044 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
7046 t = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
7047 return omit_one_operand (type, convert (type, t), arg0);
7050 /* Fold comparisons against infinity. */
7051 if (REAL_VALUE_ISINF (cst))
7053 tem = fold_inf_compare (code, type, arg0, arg1);
7054 if (tem != NULL_TREE)
7059 /* If this is a comparison of a real constant with a PLUS_EXPR
7060 or a MINUS_EXPR of a real constant, we can convert it into a
7061 comparison with a revised real constant as long as no overflow
7062 occurs when unsafe_math_optimizations are enabled. */
7063 if (flag_unsafe_math_optimizations
7064 && TREE_CODE (arg1) == REAL_CST
7065 && (TREE_CODE (arg0) == PLUS_EXPR
7066 || TREE_CODE (arg0) == MINUS_EXPR)
7067 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7068 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7069 ? MINUS_EXPR : PLUS_EXPR,
7070 arg1, TREE_OPERAND (arg0, 1), 0))
7071 && ! TREE_CONSTANT_OVERFLOW (tem))
7072 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
7074 /* Likewise, we can simplify a comparison of a real constant with
7075 a MINUS_EXPR whose first operand is also a real constant, i.e.
7076 (c1 - x) < c2 becomes x > c1-c2. */
7077 if (flag_unsafe_math_optimizations
7078 && TREE_CODE (arg1) == REAL_CST
7079 && TREE_CODE (arg0) == MINUS_EXPR
7080 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
7081 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
7083 && ! TREE_CONSTANT_OVERFLOW (tem))
7084 return fold (build (swap_tree_comparison (code), type,
7085 TREE_OPERAND (arg0, 1), tem));
7087 /* Fold comparisons against built-in math functions. */
7088 if (TREE_CODE (arg1) == REAL_CST
7089 && flag_unsafe_math_optimizations
7090 && ! flag_errno_math)
7092 enum built_in_function fcode = builtin_mathfn_code (arg0);
7094 if (fcode != END_BUILTINS)
7096 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
7097 if (tem != NULL_TREE)
7103 /* Convert foo++ == CONST into ++foo == CONST + INCR.
7104 First, see if one arg is constant; find the constant arg
7105 and the other one. */
7107 tree constop = 0, varop = NULL_TREE;
7108 int constopnum = -1;
7110 if (TREE_CONSTANT (arg1))
7111 constopnum = 1, constop = arg1, varop = arg0;
7112 if (TREE_CONSTANT (arg0))
7113 constopnum = 0, constop = arg0, varop = arg1;
7115 if (constop && TREE_CODE (varop) == POSTINCREMENT_EXPR)
7117 /* This optimization is invalid for ordered comparisons
7118 if CONST+INCR overflows or if foo+incr might overflow.
7119 This optimization is invalid for floating point due to rounding.
7120 For pointer types we assume overflow doesn't happen. */
7121 if (POINTER_TYPE_P (TREE_TYPE (varop))
7122 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
7123 && (code == EQ_EXPR || code == NE_EXPR)))
7126 = fold (build (PLUS_EXPR, TREE_TYPE (varop),
7127 constop, TREE_OPERAND (varop, 1)));
7129 /* Do not overwrite the current varop to be a preincrement,
7130 create a new node so that we won't confuse our caller who
7131 might create trees and throw them away, reusing the
7132 arguments that they passed to build. This shows up in
7133 the THEN or ELSE parts of ?: being postincrements. */
7134 varop = build (PREINCREMENT_EXPR, TREE_TYPE (varop),
7135 TREE_OPERAND (varop, 0),
7136 TREE_OPERAND (varop, 1));
7138 /* If VAROP is a reference to a bitfield, we must mask
7139 the constant by the width of the field. */
7140 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7141 && DECL_BIT_FIELD(TREE_OPERAND
7142 (TREE_OPERAND (varop, 0), 1)))
7145 = TREE_INT_CST_LOW (DECL_SIZE
7147 (TREE_OPERAND (varop, 0), 1)));
7148 tree mask, unsigned_type;
7149 unsigned int precision;
7150 tree folded_compare;
7152 /* First check whether the comparison would come out
7153 always the same. If we don't do that we would
7154 change the meaning with the masking. */
7155 if (constopnum == 0)
7156 folded_compare = fold (build (code, type, constop,
7157 TREE_OPERAND (varop, 0)));
7159 folded_compare = fold (build (code, type,
7160 TREE_OPERAND (varop, 0),
7162 if (integer_zerop (folded_compare)
7163 || integer_onep (folded_compare))
7164 return omit_one_operand (type, folded_compare, varop);
7166 unsigned_type = (*lang_hooks.types.type_for_size)(size, 1);
7167 precision = TYPE_PRECISION (unsigned_type);
7168 mask = build_int_2 (~0, ~0);
7169 TREE_TYPE (mask) = unsigned_type;
7170 force_fit_type (mask, 0);
7171 mask = const_binop (RSHIFT_EXPR, mask,
7172 size_int (precision - size), 0);
7173 newconst = fold (build (BIT_AND_EXPR,
7174 TREE_TYPE (varop), newconst,
7175 convert (TREE_TYPE (varop),
7179 t = build (code, type,
7180 (constopnum == 0) ? newconst : varop,
7181 (constopnum == 1) ? newconst : varop);
7185 else if (constop && TREE_CODE (varop) == POSTDECREMENT_EXPR)
7187 if (POINTER_TYPE_P (TREE_TYPE (varop))
7188 || (! FLOAT_TYPE_P (TREE_TYPE (varop))
7189 && (code == EQ_EXPR || code == NE_EXPR)))
7192 = fold (build (MINUS_EXPR, TREE_TYPE (varop),
7193 constop, TREE_OPERAND (varop, 1)));
7195 /* Do not overwrite the current varop to be a predecrement,
7196 create a new node so that we won't confuse our caller who
7197 might create trees and throw them away, reusing the
7198 arguments that they passed to build. This shows up in
7199 the THEN or ELSE parts of ?: being postdecrements. */
7200 varop = build (PREDECREMENT_EXPR, TREE_TYPE (varop),
7201 TREE_OPERAND (varop, 0),
7202 TREE_OPERAND (varop, 1));
7204 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7205 && DECL_BIT_FIELD(TREE_OPERAND
7206 (TREE_OPERAND (varop, 0), 1)))
7209 = TREE_INT_CST_LOW (DECL_SIZE
7211 (TREE_OPERAND (varop, 0), 1)));
7212 tree mask, unsigned_type;
7213 unsigned int precision;
7214 tree folded_compare;
7216 if (constopnum == 0)
7217 folded_compare = fold (build (code, type, constop,
7218 TREE_OPERAND (varop, 0)));
7220 folded_compare = fold (build (code, type,
7221 TREE_OPERAND (varop, 0),
7223 if (integer_zerop (folded_compare)
7224 || integer_onep (folded_compare))
7225 return omit_one_operand (type, folded_compare, varop);
7227 unsigned_type = (*lang_hooks.types.type_for_size)(size, 1);
7228 precision = TYPE_PRECISION (unsigned_type);
7229 mask = build_int_2 (~0, ~0);
7230 TREE_TYPE (mask) = TREE_TYPE (varop);
7231 force_fit_type (mask, 0);
7232 mask = const_binop (RSHIFT_EXPR, mask,
7233 size_int (precision - size), 0);
7234 newconst = fold (build (BIT_AND_EXPR,
7235 TREE_TYPE (varop), newconst,
7236 convert (TREE_TYPE (varop),
7240 t = build (code, type,
7241 (constopnum == 0) ? newconst : varop,
7242 (constopnum == 1) ? newconst : varop);
7248 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
7249 This transformation affects the cases which are handled in later
7250 optimizations involving comparisons with non-negative constants. */
7251 if (TREE_CODE (arg1) == INTEGER_CST
7252 && TREE_CODE (arg0) != INTEGER_CST
7253 && tree_int_cst_sgn (arg1) > 0)
7258 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7259 return fold (build (GT_EXPR, type, arg0, arg1));
7262 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7263 return fold (build (LE_EXPR, type, arg0, arg1));
7270 /* Comparisons with the highest or lowest possible integer of
7271 the specified size will have known values. */
7273 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
7275 if (TREE_CODE (arg1) == INTEGER_CST
7276 && ! TREE_CONSTANT_OVERFLOW (arg1)
7277 && width <= HOST_BITS_PER_WIDE_INT
7278 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
7279 || POINTER_TYPE_P (TREE_TYPE (arg1))))
7281 unsigned HOST_WIDE_INT signed_max;
7282 unsigned HOST_WIDE_INT max, min;
7284 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
7286 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7288 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
7294 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
7297 if (TREE_INT_CST_HIGH (arg1) == 0
7298 && TREE_INT_CST_LOW (arg1) == max)
7302 return omit_one_operand (type,
7303 convert (type, integer_zero_node),
7306 return fold (build (EQ_EXPR, type, arg0, arg1));
7309 return omit_one_operand (type,
7310 convert (type, integer_one_node),
7313 return fold (build (NE_EXPR, type, arg0, arg1));
7315 /* The GE_EXPR and LT_EXPR cases above are not normally
7316 reached because of previous transformations. */
7321 else if (TREE_INT_CST_HIGH (arg1) == 0
7322 && TREE_INT_CST_LOW (arg1) == max - 1)
7326 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7327 return fold (build (EQ_EXPR, type, arg0, arg1));
7329 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7330 return fold (build (NE_EXPR, type, arg0, arg1));
7334 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7335 && TREE_INT_CST_LOW (arg1) == min)
7339 return omit_one_operand (type,
7340 convert (type, integer_zero_node),
7343 return fold (build (EQ_EXPR, type, arg0, arg1));
7346 return omit_one_operand (type,
7347 convert (type, integer_one_node),
7350 return fold (build (NE_EXPR, type, arg0, arg1));
7355 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7356 && TREE_INT_CST_LOW (arg1) == min + 1)
7360 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7361 return fold (build (NE_EXPR, type, arg0, arg1));
7363 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7364 return fold (build (EQ_EXPR, type, arg0, arg1));
7369 else if (TREE_INT_CST_HIGH (arg1) == 0
7370 && TREE_INT_CST_LOW (arg1) == signed_max
7371 && TREE_UNSIGNED (TREE_TYPE (arg1))
7372 /* signed_type does not work on pointer types. */
7373 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
7375 /* The following case also applies to X < signed_max+1
7376 and X >= signed_max+1 because previous transformations. */
7377 if (code == LE_EXPR || code == GT_EXPR)
7380 st0 = (*lang_hooks.types.signed_type) (TREE_TYPE (arg0));
7381 st1 = (*lang_hooks.types.signed_type) (TREE_TYPE (arg1));
7383 (build (code == LE_EXPR ? GE_EXPR: LT_EXPR,
7384 type, convert (st0, arg0),
7385 convert (st1, integer_zero_node)));
7391 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
7392 a MINUS_EXPR of a constant, we can convert it into a comparison with
7393 a revised constant as long as no overflow occurs. */
7394 if ((code == EQ_EXPR || code == NE_EXPR)
7395 && TREE_CODE (arg1) == INTEGER_CST
7396 && (TREE_CODE (arg0) == PLUS_EXPR
7397 || TREE_CODE (arg0) == MINUS_EXPR)
7398 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7399 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7400 ? MINUS_EXPR : PLUS_EXPR,
7401 arg1, TREE_OPERAND (arg0, 1), 0))
7402 && ! TREE_CONSTANT_OVERFLOW (tem))
7403 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
7405 /* Similarly for a NEGATE_EXPR. */
7406 else if ((code == EQ_EXPR || code == NE_EXPR)
7407 && TREE_CODE (arg0) == NEGATE_EXPR
7408 && TREE_CODE (arg1) == INTEGER_CST
7409 && 0 != (tem = negate_expr (arg1))
7410 && TREE_CODE (tem) == INTEGER_CST
7411 && ! TREE_CONSTANT_OVERFLOW (tem))
7412 return fold (build (code, type, TREE_OPERAND (arg0, 0), tem));
7414 /* If we have X - Y == 0, we can convert that to X == Y and similarly
7415 for !=. Don't do this for ordered comparisons due to overflow. */
7416 else if ((code == NE_EXPR || code == EQ_EXPR)
7417 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
7418 return fold (build (code, type,
7419 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
7421 /* If we are widening one operand of an integer comparison,
7422 see if the other operand is similarly being widened. Perhaps we
7423 can do the comparison in the narrower type. */
7424 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
7425 && TREE_CODE (arg0) == NOP_EXPR
7426 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
7427 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
7428 && (TREE_TYPE (t1) == TREE_TYPE (tem)
7429 || (TREE_CODE (t1) == INTEGER_CST
7430 && int_fits_type_p (t1, TREE_TYPE (tem)))))
7431 return fold (build (code, type, tem, convert (TREE_TYPE (tem), t1)));
7433 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7434 constant, we can simplify it. */
7435 else if (TREE_CODE (arg1) == INTEGER_CST
7436 && (TREE_CODE (arg0) == MIN_EXPR
7437 || TREE_CODE (arg0) == MAX_EXPR)
7438 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7439 return optimize_minmax_comparison (t);
7441 /* If we are comparing an ABS_EXPR with a constant, we can
7442 convert all the cases into explicit comparisons, but they may
7443 well not be faster than doing the ABS and one comparison.
7444 But ABS (X) <= C is a range comparison, which becomes a subtraction
7445 and a comparison, and is probably faster. */
7446 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7447 && TREE_CODE (arg0) == ABS_EXPR
7448 && ! TREE_SIDE_EFFECTS (arg0)
7449 && (0 != (tem = negate_expr (arg1)))
7450 && TREE_CODE (tem) == INTEGER_CST
7451 && ! TREE_CONSTANT_OVERFLOW (tem))
7452 return fold (build (TRUTH_ANDIF_EXPR, type,
7453 build (GE_EXPR, type, TREE_OPERAND (arg0, 0), tem),
7454 build (LE_EXPR, type,
7455 TREE_OPERAND (arg0, 0), arg1)));
7457 /* If this is an EQ or NE comparison with zero and ARG0 is
7458 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
7459 two operations, but the latter can be done in one less insn
7460 on machines that have only two-operand insns or on which a
7461 constant cannot be the first operand. */
7462 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
7463 && TREE_CODE (arg0) == BIT_AND_EXPR)
7465 if (TREE_CODE (TREE_OPERAND (arg0, 0)) == LSHIFT_EXPR
7466 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 0), 0)))
7468 fold (build (code, type,
7469 build (BIT_AND_EXPR, TREE_TYPE (arg0),
7471 TREE_TYPE (TREE_OPERAND (arg0, 0)),
7472 TREE_OPERAND (arg0, 1),
7473 TREE_OPERAND (TREE_OPERAND (arg0, 0), 1)),
7474 convert (TREE_TYPE (arg0),
7477 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
7478 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
7480 fold (build (code, type,
7481 build (BIT_AND_EXPR, TREE_TYPE (arg0),
7483 TREE_TYPE (TREE_OPERAND (arg0, 1)),
7484 TREE_OPERAND (arg0, 0),
7485 TREE_OPERAND (TREE_OPERAND (arg0, 1), 1)),
7486 convert (TREE_TYPE (arg0),
7491 /* If this is an NE or EQ comparison of zero against the result of a
7492 signed MOD operation whose second operand is a power of 2, make
7493 the MOD operation unsigned since it is simpler and equivalent. */
7494 if ((code == NE_EXPR || code == EQ_EXPR)
7495 && integer_zerop (arg1)
7496 && ! TREE_UNSIGNED (TREE_TYPE (arg0))
7497 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
7498 || TREE_CODE (arg0) == CEIL_MOD_EXPR
7499 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
7500 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
7501 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7503 tree newtype = (*lang_hooks.types.unsigned_type) (TREE_TYPE (arg0));
7504 tree newmod = build (TREE_CODE (arg0), newtype,
7505 convert (newtype, TREE_OPERAND (arg0, 0)),
7506 convert (newtype, TREE_OPERAND (arg0, 1)));
7508 return build (code, type, newmod, convert (newtype, arg1));
7511 /* If this is an NE comparison of zero with an AND of one, remove the
7512 comparison since the AND will give the correct value. */
7513 if (code == NE_EXPR && integer_zerop (arg1)
7514 && TREE_CODE (arg0) == BIT_AND_EXPR
7515 && integer_onep (TREE_OPERAND (arg0, 1)))
7516 return convert (type, arg0);
7518 /* If we have (A & C) == C where C is a power of 2, convert this into
7519 (A & C) != 0. Similarly for NE_EXPR. */
7520 if ((code == EQ_EXPR || code == NE_EXPR)
7521 && TREE_CODE (arg0) == BIT_AND_EXPR
7522 && integer_pow2p (TREE_OPERAND (arg0, 1))
7523 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
7524 return fold (build (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
7525 arg0, integer_zero_node));
7527 /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
7528 2, then fold the expression into shifts and logical operations. */
7529 tem = fold_single_bit_test (code, arg0, arg1, type);
7533 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
7534 Similarly for NE_EXPR. */
7535 if ((code == EQ_EXPR || code == NE_EXPR)
7536 && TREE_CODE (arg0) == BIT_AND_EXPR
7537 && TREE_CODE (arg1) == INTEGER_CST
7538 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7541 = fold (build (BIT_AND_EXPR, TREE_TYPE (arg0),
7542 arg1, build1 (BIT_NOT_EXPR,
7543 TREE_TYPE (TREE_OPERAND (arg0, 1)),
7544 TREE_OPERAND (arg0, 1))));
7545 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7546 if (integer_nonzerop (dandnotc))
7547 return omit_one_operand (type, rslt, arg0);
7550 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7551 Similarly for NE_EXPR. */
7552 if ((code == EQ_EXPR || code == NE_EXPR)
7553 && TREE_CODE (arg0) == BIT_IOR_EXPR
7554 && TREE_CODE (arg1) == INTEGER_CST
7555 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7558 = fold (build (BIT_AND_EXPR, TREE_TYPE (arg0),
7559 TREE_OPERAND (arg0, 1),
7560 build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1)));
7561 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7562 if (integer_nonzerop (candnotd))
7563 return omit_one_operand (type, rslt, arg0);
7566 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
7567 and similarly for >= into !=. */
7568 if ((code == LT_EXPR || code == GE_EXPR)
7569 && TREE_UNSIGNED (TREE_TYPE (arg0))
7570 && TREE_CODE (arg1) == LSHIFT_EXPR
7571 && integer_onep (TREE_OPERAND (arg1, 0)))
7572 return build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7573 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7574 TREE_OPERAND (arg1, 1)),
7575 convert (TREE_TYPE (arg0), integer_zero_node));
7577 else if ((code == LT_EXPR || code == GE_EXPR)
7578 && TREE_UNSIGNED (TREE_TYPE (arg0))
7579 && (TREE_CODE (arg1) == NOP_EXPR
7580 || TREE_CODE (arg1) == CONVERT_EXPR)
7581 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
7582 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
7584 build (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7585 convert (TREE_TYPE (arg0),
7586 build (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7587 TREE_OPERAND (TREE_OPERAND (arg1, 0), 1))),
7588 convert (TREE_TYPE (arg0), integer_zero_node));
7590 /* Simplify comparison of something with itself. (For IEEE
7591 floating-point, we can only do some of these simplifications.) */
7592 if (operand_equal_p (arg0, arg1, 0))
7597 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7598 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7599 return constant_boolean_node (1, type);
7604 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7605 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7606 return constant_boolean_node (1, type);
7607 return fold (build (EQ_EXPR, type, arg0, arg1));
7610 /* For NE, we can only do this simplification if integer
7611 or we don't honor IEEE floating point NaNs. */
7612 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
7613 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7615 /* ... fall through ... */
7618 return constant_boolean_node (0, type);
7624 /* If we are comparing an expression that just has comparisons
7625 of two integer values, arithmetic expressions of those comparisons,
7626 and constants, we can simplify it. There are only three cases
7627 to check: the two values can either be equal, the first can be
7628 greater, or the second can be greater. Fold the expression for
7629 those three values. Since each value must be 0 or 1, we have
7630 eight possibilities, each of which corresponds to the constant 0
7631 or 1 or one of the six possible comparisons.
7633 This handles common cases like (a > b) == 0 but also handles
7634 expressions like ((x > y) - (y > x)) > 0, which supposedly
7635 occur in macroized code. */
7637 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
7639 tree cval1 = 0, cval2 = 0;
7642 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
7643 /* Don't handle degenerate cases here; they should already
7644 have been handled anyway. */
7645 && cval1 != 0 && cval2 != 0
7646 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
7647 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7648 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
7649 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
7650 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
7651 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
7652 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
7654 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
7655 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
7657 /* We can't just pass T to eval_subst in case cval1 or cval2
7658 was the same as ARG1. */
7661 = fold (build (code, type,
7662 eval_subst (arg0, cval1, maxval, cval2, minval),
7665 = fold (build (code, type,
7666 eval_subst (arg0, cval1, maxval, cval2, maxval),
7669 = fold (build (code, type,
7670 eval_subst (arg0, cval1, minval, cval2, maxval),
7673 /* All three of these results should be 0 or 1. Confirm they
7674 are. Then use those values to select the proper code
7677 if ((integer_zerop (high_result)
7678 || integer_onep (high_result))
7679 && (integer_zerop (equal_result)
7680 || integer_onep (equal_result))
7681 && (integer_zerop (low_result)
7682 || integer_onep (low_result)))
7684 /* Make a 3-bit mask with the high-order bit being the
7685 value for `>', the next for '=', and the low for '<'. */
7686 switch ((integer_onep (high_result) * 4)
7687 + (integer_onep (equal_result) * 2)
7688 + integer_onep (low_result))
7692 return omit_one_operand (type, integer_zero_node, arg0);
7713 return omit_one_operand (type, integer_one_node, arg0);
7716 t = build (code, type, cval1, cval2);
7718 return save_expr (t);
7725 /* If this is a comparison of a field, we may be able to simplify it. */
7726 if (((TREE_CODE (arg0) == COMPONENT_REF
7727 && (*lang_hooks.can_use_bit_fields_p) ())
7728 || TREE_CODE (arg0) == BIT_FIELD_REF)
7729 && (code == EQ_EXPR || code == NE_EXPR)
7730 /* Handle the constant case even without -O
7731 to make sure the warnings are given. */
7732 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
7734 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
7739 /* If this is a comparison of complex values and either or both sides
7740 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
7741 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
7742 This may prevent needless evaluations. */
7743 if ((code == EQ_EXPR || code == NE_EXPR)
7744 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
7745 && (TREE_CODE (arg0) == COMPLEX_EXPR
7746 || TREE_CODE (arg1) == COMPLEX_EXPR
7747 || TREE_CODE (arg0) == COMPLEX_CST
7748 || TREE_CODE (arg1) == COMPLEX_CST))
7750 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
7751 tree real0, imag0, real1, imag1;
7753 arg0 = save_expr (arg0);
7754 arg1 = save_expr (arg1);
7755 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
7756 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
7757 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
7758 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
7760 return fold (build ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
7763 fold (build (code, type, real0, real1)),
7764 fold (build (code, type, imag0, imag1))));
7767 /* Optimize comparisons of strlen vs zero to a compare of the
7768 first character of the string vs zero. To wit,
7769 strlen(ptr) == 0 => *ptr == 0
7770 strlen(ptr) != 0 => *ptr != 0
7771 Other cases should reduce to one of these two (or a constant)
7772 due to the return value of strlen being unsigned. */
7773 if ((code == EQ_EXPR || code == NE_EXPR)
7774 && integer_zerop (arg1)
7775 && TREE_CODE (arg0) == CALL_EXPR)
7777 tree fndecl = get_callee_fndecl (arg0);
7781 && DECL_BUILT_IN (fndecl)
7782 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
7783 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
7784 && (arglist = TREE_OPERAND (arg0, 1))
7785 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
7786 && ! TREE_CHAIN (arglist))
7787 return fold (build (code, type,
7788 build1 (INDIRECT_REF, char_type_node,
7789 TREE_VALUE(arglist)),
7790 integer_zero_node));
7793 /* From here on, the only cases we handle are when the result is
7794 known to be a constant.
7796 To compute GT, swap the arguments and do LT.
7797 To compute GE, do LT and invert the result.
7798 To compute LE, swap the arguments, do LT and invert the result.
7799 To compute NE, do EQ and invert the result.
7801 Therefore, the code below must handle only EQ and LT. */
7803 if (code == LE_EXPR || code == GT_EXPR)
7805 tem = arg0, arg0 = arg1, arg1 = tem;
7806 code = swap_tree_comparison (code);
7809 /* Note that it is safe to invert for real values here because we
7810 will check below in the one case that it matters. */
7814 if (code == NE_EXPR || code == GE_EXPR)
7817 code = invert_tree_comparison (code);
7820 /* Compute a result for LT or EQ if args permit;
7821 otherwise return T. */
7822 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
7824 if (code == EQ_EXPR)
7825 t1 = build_int_2 (tree_int_cst_equal (arg0, arg1), 0);
7827 t1 = build_int_2 ((TREE_UNSIGNED (TREE_TYPE (arg0))
7828 ? INT_CST_LT_UNSIGNED (arg0, arg1)
7829 : INT_CST_LT (arg0, arg1)),
7833 #if 0 /* This is no longer useful, but breaks some real code. */
7834 /* Assume a nonexplicit constant cannot equal an explicit one,
7835 since such code would be undefined anyway.
7836 Exception: on sysvr4, using #pragma weak,
7837 a label can come out as 0. */
7838 else if (TREE_CODE (arg1) == INTEGER_CST
7839 && !integer_zerop (arg1)
7840 && TREE_CONSTANT (arg0)
7841 && TREE_CODE (arg0) == ADDR_EXPR
7843 t1 = build_int_2 (0, 0);
7845 /* Two real constants can be compared explicitly. */
7846 else if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
7848 /* If either operand is a NaN, the result is false with two
7849 exceptions: First, an NE_EXPR is true on NaNs, but that case
7850 is already handled correctly since we will be inverting the
7851 result for NE_EXPR. Second, if we had inverted a LE_EXPR
7852 or a GE_EXPR into a LT_EXPR, we must return true so that it
7853 will be inverted into false. */
7855 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
7856 || REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
7857 t1 = build_int_2 (invert && code == LT_EXPR, 0);
7859 else if (code == EQ_EXPR)
7860 t1 = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (arg0),
7861 TREE_REAL_CST (arg1)),
7864 t1 = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (arg0),
7865 TREE_REAL_CST (arg1)),
7869 if (t1 == NULL_TREE)
7873 TREE_INT_CST_LOW (t1) ^= 1;
7875 TREE_TYPE (t1) = type;
7876 if (TREE_CODE (type) == BOOLEAN_TYPE)
7877 return (*lang_hooks.truthvalue_conversion) (t1);
7881 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
7882 so all simple results must be passed through pedantic_non_lvalue. */
7883 if (TREE_CODE (arg0) == INTEGER_CST)
7885 tem = TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
7886 /* Only optimize constant conditions when the selected branch
7887 has the same type as the COND_EXPR. This avoids optimizing
7888 away "c ? x : throw", where the throw has a void type. */
7889 if (! VOID_TYPE_P (TREE_TYPE (tem))
7890 || VOID_TYPE_P (TREE_TYPE (t)))
7891 return pedantic_non_lvalue (tem);
7894 if (operand_equal_p (arg1, TREE_OPERAND (expr, 2), 0))
7895 return pedantic_omit_one_operand (type, arg1, arg0);
7897 /* If we have A op B ? A : C, we may be able to convert this to a
7898 simpler expression, depending on the operation and the values
7899 of B and C. Signed zeros prevent all of these transformations,
7900 for reasons given above each one. */
7902 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
7903 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
7904 arg1, TREE_OPERAND (arg0, 1))
7905 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
7907 tree arg2 = TREE_OPERAND (t, 2);
7908 enum tree_code comp_code = TREE_CODE (arg0);
7912 /* If we have A op 0 ? A : -A, consider applying the following
7915 A == 0? A : -A same as -A
7916 A != 0? A : -A same as A
7917 A >= 0? A : -A same as abs (A)
7918 A > 0? A : -A same as abs (A)
7919 A <= 0? A : -A same as -abs (A)
7920 A < 0? A : -A same as -abs (A)
7922 None of these transformations work for modes with signed
7923 zeros. If A is +/-0, the first two transformations will
7924 change the sign of the result (from +0 to -0, or vice
7925 versa). The last four will fix the sign of the result,
7926 even though the original expressions could be positive or
7927 negative, depending on the sign of A.
7929 Note that all these transformations are correct if A is
7930 NaN, since the two alternatives (A and -A) are also NaNs. */
7931 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
7932 ? real_zerop (TREE_OPERAND (arg0, 1))
7933 : integer_zerop (TREE_OPERAND (arg0, 1)))
7934 && TREE_CODE (arg2) == NEGATE_EXPR
7935 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
7943 (convert (TREE_TYPE (TREE_OPERAND (t, 1)),
7946 return pedantic_non_lvalue (convert (type, arg1));
7949 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7950 arg1 = convert ((*lang_hooks.types.signed_type)
7951 (TREE_TYPE (arg1)), arg1);
7952 return pedantic_non_lvalue
7953 (convert (type, fold (build1 (ABS_EXPR,
7954 TREE_TYPE (arg1), arg1))));
7957 if (TREE_UNSIGNED (TREE_TYPE (arg1)))
7958 arg1 = convert ((lang_hooks.types.signed_type)
7959 (TREE_TYPE (arg1)), arg1);
7960 return pedantic_non_lvalue
7961 (negate_expr (convert (type,
7962 fold (build1 (ABS_EXPR,
7969 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
7970 A == 0 ? A : 0 is always 0 unless A is -0. Note that
7971 both transformations are correct when A is NaN: A != 0
7972 is then true, and A == 0 is false. */
7974 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
7976 if (comp_code == NE_EXPR)
7977 return pedantic_non_lvalue (convert (type, arg1));
7978 else if (comp_code == EQ_EXPR)
7979 return pedantic_non_lvalue (convert (type, integer_zero_node));
7982 /* Try some transformations of A op B ? A : B.
7984 A == B? A : B same as B
7985 A != B? A : B same as A
7986 A >= B? A : B same as max (A, B)
7987 A > B? A : B same as max (B, A)
7988 A <= B? A : B same as min (A, B)
7989 A < B? A : B same as min (B, A)
7991 As above, these transformations don't work in the presence
7992 of signed zeros. For example, if A and B are zeros of
7993 opposite sign, the first two transformations will change
7994 the sign of the result. In the last four, the original
7995 expressions give different results for (A=+0, B=-0) and
7996 (A=-0, B=+0), but the transformed expressions do not.
7998 The first two transformations are correct if either A or B
7999 is a NaN. In the first transformation, the condition will
8000 be false, and B will indeed be chosen. In the case of the
8001 second transformation, the condition A != B will be true,
8002 and A will be chosen.
8004 The conversions to max() and min() are not correct if B is
8005 a number and A is not. The conditions in the original
8006 expressions will be false, so all four give B. The min()
8007 and max() versions would give a NaN instead. */
8008 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
8009 arg2, TREE_OPERAND (arg0, 0)))
8011 tree comp_op0 = TREE_OPERAND (arg0, 0);
8012 tree comp_op1 = TREE_OPERAND (arg0, 1);
8013 tree comp_type = TREE_TYPE (comp_op0);
8015 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
8016 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
8026 return pedantic_non_lvalue (convert (type, arg2));
8028 return pedantic_non_lvalue (convert (type, arg1));
8031 /* In C++ a ?: expression can be an lvalue, so put the
8032 operand which will be used if they are equal first
8033 so that we can convert this back to the
8034 corresponding COND_EXPR. */
8035 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8036 return pedantic_non_lvalue
8037 (convert (type, fold (build (MIN_EXPR, comp_type,
8038 (comp_code == LE_EXPR
8039 ? comp_op0 : comp_op1),
8040 (comp_code == LE_EXPR
8041 ? comp_op1 : comp_op0)))));
8045 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8046 return pedantic_non_lvalue
8047 (convert (type, fold (build (MAX_EXPR, comp_type,
8048 (comp_code == GE_EXPR
8049 ? comp_op0 : comp_op1),
8050 (comp_code == GE_EXPR
8051 ? comp_op1 : comp_op0)))));
8058 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
8059 we might still be able to simplify this. For example,
8060 if C1 is one less or one more than C2, this might have started
8061 out as a MIN or MAX and been transformed by this function.
8062 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
8064 if (INTEGRAL_TYPE_P (type)
8065 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8066 && TREE_CODE (arg2) == INTEGER_CST)
8070 /* We can replace A with C1 in this case. */
8071 arg1 = convert (type, TREE_OPERAND (arg0, 1));
8072 return fold (build (code, type, TREE_OPERAND (t, 0), arg1,
8073 TREE_OPERAND (t, 2)));
8076 /* If C1 is C2 + 1, this is min(A, C2). */
8077 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
8078 && operand_equal_p (TREE_OPERAND (arg0, 1),
8079 const_binop (PLUS_EXPR, arg2,
8080 integer_one_node, 0), 1))
8081 return pedantic_non_lvalue
8082 (fold (build (MIN_EXPR, type, arg1, arg2)));
8086 /* If C1 is C2 - 1, this is min(A, C2). */
8087 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
8088 && operand_equal_p (TREE_OPERAND (arg0, 1),
8089 const_binop (MINUS_EXPR, arg2,
8090 integer_one_node, 0), 1))
8091 return pedantic_non_lvalue
8092 (fold (build (MIN_EXPR, type, arg1, arg2)));
8096 /* If C1 is C2 - 1, this is max(A, C2). */
8097 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type), 1)
8098 && operand_equal_p (TREE_OPERAND (arg0, 1),
8099 const_binop (MINUS_EXPR, arg2,
8100 integer_one_node, 0), 1))
8101 return pedantic_non_lvalue
8102 (fold (build (MAX_EXPR, type, arg1, arg2)));
8106 /* If C1 is C2 + 1, this is max(A, C2). */
8107 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type), 1)
8108 && operand_equal_p (TREE_OPERAND (arg0, 1),
8109 const_binop (PLUS_EXPR, arg2,
8110 integer_one_node, 0), 1))
8111 return pedantic_non_lvalue
8112 (fold (build (MAX_EXPR, type, arg1, arg2)));
8121 /* If the second operand is simpler than the third, swap them
8122 since that produces better jump optimization results. */
8123 if (tree_swap_operands_p (TREE_OPERAND (t, 1),
8124 TREE_OPERAND (t, 2), false))
8126 /* See if this can be inverted. If it can't, possibly because
8127 it was a floating-point inequality comparison, don't do
8129 tem = invert_truthvalue (arg0);
8131 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8132 return fold (build (code, type, tem,
8133 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)));
8136 /* Convert A ? 1 : 0 to simply A. */
8137 if (integer_onep (TREE_OPERAND (t, 1))
8138 && integer_zerop (TREE_OPERAND (t, 2))
8139 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
8140 call to fold will try to move the conversion inside
8141 a COND, which will recurse. In that case, the COND_EXPR
8142 is probably the best choice, so leave it alone. */
8143 && type == TREE_TYPE (arg0))
8144 return pedantic_non_lvalue (arg0);
8146 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
8147 over COND_EXPR in cases such as floating point comparisons. */
8148 if (integer_zerop (TREE_OPERAND (t, 1))
8149 && integer_onep (TREE_OPERAND (t, 2))
8150 && truth_value_p (TREE_CODE (arg0)))
8151 return pedantic_non_lvalue (convert (type,
8152 invert_truthvalue (arg0)));
8154 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
8155 operation is simply A & 2. */
8157 if (integer_zerop (TREE_OPERAND (t, 2))
8158 && TREE_CODE (arg0) == NE_EXPR
8159 && integer_zerop (TREE_OPERAND (arg0, 1))
8160 && integer_pow2p (arg1)
8161 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
8162 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
8164 return pedantic_non_lvalue (convert (type, TREE_OPERAND (arg0, 0)));
8166 /* Convert A ? B : 0 into A && B if A and B are truth values. */
8167 if (integer_zerop (TREE_OPERAND (t, 2))
8168 && truth_value_p (TREE_CODE (arg0))
8169 && truth_value_p (TREE_CODE (arg1)))
8170 return pedantic_non_lvalue (fold (build (TRUTH_ANDIF_EXPR, type,
8173 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
8174 if (integer_onep (TREE_OPERAND (t, 2))
8175 && truth_value_p (TREE_CODE (arg0))
8176 && truth_value_p (TREE_CODE (arg1)))
8178 /* Only perform transformation if ARG0 is easily inverted. */
8179 tem = invert_truthvalue (arg0);
8180 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8181 return pedantic_non_lvalue (fold (build (TRUTH_ORIF_EXPR, type,
8188 /* When pedantic, a compound expression can be neither an lvalue
8189 nor an integer constant expression. */
8190 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
8192 /* Don't let (0, 0) be null pointer constant. */
8193 if (integer_zerop (arg1))
8194 return pedantic_non_lvalue (build1 (NOP_EXPR, type, arg1));
8195 return pedantic_non_lvalue (convert (type, arg1));
8199 return build_complex (type, arg0, arg1);
8203 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8205 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8206 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8207 TREE_OPERAND (arg0, 1));
8208 else if (TREE_CODE (arg0) == COMPLEX_CST)
8209 return TREE_REALPART (arg0);
8210 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8211 return fold (build (TREE_CODE (arg0), type,
8212 fold (build1 (REALPART_EXPR, type,
8213 TREE_OPERAND (arg0, 0))),
8214 fold (build1 (REALPART_EXPR,
8215 type, TREE_OPERAND (arg0, 1)))));
8219 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8220 return convert (type, integer_zero_node);
8221 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8222 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8223 TREE_OPERAND (arg0, 0));
8224 else if (TREE_CODE (arg0) == COMPLEX_CST)
8225 return TREE_IMAGPART (arg0);
8226 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8227 return fold (build (TREE_CODE (arg0), type,
8228 fold (build1 (IMAGPART_EXPR, type,
8229 TREE_OPERAND (arg0, 0))),
8230 fold (build1 (IMAGPART_EXPR, type,
8231 TREE_OPERAND (arg0, 1)))));
8234 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
8236 case CLEANUP_POINT_EXPR:
8237 if (! has_cleanups (arg0))
8238 return TREE_OPERAND (t, 0);
8241 enum tree_code code0 = TREE_CODE (arg0);
8242 int kind0 = TREE_CODE_CLASS (code0);
8243 tree arg00 = TREE_OPERAND (arg0, 0);
8246 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
8247 return fold (build1 (code0, type,
8248 fold (build1 (CLEANUP_POINT_EXPR,
8249 TREE_TYPE (arg00), arg00))));
8251 if (kind0 == '<' || kind0 == '2'
8252 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
8253 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
8254 || code0 == TRUTH_XOR_EXPR)
8256 arg01 = TREE_OPERAND (arg0, 1);
8258 if (TREE_CONSTANT (arg00)
8259 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
8260 && ! has_cleanups (arg00)))
8261 return fold (build (code0, type, arg00,
8262 fold (build1 (CLEANUP_POINT_EXPR,
8263 TREE_TYPE (arg01), arg01))));
8265 if (TREE_CONSTANT (arg01))
8266 return fold (build (code0, type,
8267 fold (build1 (CLEANUP_POINT_EXPR,
8268 TREE_TYPE (arg00), arg00)),
8276 /* Check for a built-in function. */
8277 if (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR
8278 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (expr, 0), 0))
8280 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (expr, 0), 0)))
8282 tree tmp = fold_builtin (expr);
8290 } /* switch (code) */
8293 #ifdef ENABLE_FOLD_CHECKING
8296 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
8297 static void fold_check_failed (tree, tree);
8298 void print_fold_checksum (tree);
8300 /* When --enable-checking=fold, compute a digest of expr before
8301 and after actual fold call to see if fold did not accidentally
8302 change original expr. */
8309 unsigned char checksum_before[16], checksum_after[16];
8312 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8313 md5_init_ctx (&ctx);
8314 fold_checksum_tree (expr, &ctx, ht);
8315 md5_finish_ctx (&ctx, checksum_before);
8318 ret = fold_1 (expr);
8320 md5_init_ctx (&ctx);
8321 fold_checksum_tree (expr, &ctx, ht);
8322 md5_finish_ctx (&ctx, checksum_after);
8325 if (memcmp (checksum_before, checksum_after, 16))
8326 fold_check_failed (expr, ret);
8332 print_fold_checksum (tree expr)
8335 unsigned char checksum[16], cnt;
8338 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8339 md5_init_ctx (&ctx);
8340 fold_checksum_tree (expr, &ctx, ht);
8341 md5_finish_ctx (&ctx, checksum);
8343 for (cnt = 0; cnt < 16; ++cnt)
8344 fprintf (stderr, "%02x", checksum[cnt]);
8345 putc ('\n', stderr);
8349 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
8351 internal_error ("fold check: original tree changed by fold");
8355 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
8358 enum tree_code code;
8359 char buf[sizeof (struct tree_decl)];
8362 if (sizeof (struct tree_exp) + 5 * sizeof (tree)
8363 > sizeof (struct tree_decl)
8364 || sizeof (struct tree_type) > sizeof (struct tree_decl))
8368 slot = htab_find_slot (ht, expr, INSERT);
8372 code = TREE_CODE (expr);
8373 if (code == SAVE_EXPR && SAVE_EXPR_NOPLACEHOLDER (expr))
8375 /* Allow SAVE_EXPR_NOPLACEHOLDER flag to be modified. */
8376 memcpy (buf, expr, tree_size (expr));
8378 SAVE_EXPR_NOPLACEHOLDER (expr) = 0;
8380 else if (TREE_CODE_CLASS (code) == 'd' && DECL_ASSEMBLER_NAME_SET_P (expr))
8382 /* Allow DECL_ASSEMBLER_NAME to be modified. */
8383 memcpy (buf, expr, tree_size (expr));
8385 SET_DECL_ASSEMBLER_NAME (expr, NULL);
8387 else if (TREE_CODE_CLASS (code) == 't'
8388 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)))
8390 /* Allow TYPE_POINTER_TO and TYPE_REFERENCE_TO to be modified. */
8391 memcpy (buf, expr, tree_size (expr));
8393 TYPE_POINTER_TO (expr) = NULL;
8394 TYPE_REFERENCE_TO (expr) = NULL;
8396 md5_process_bytes (expr, tree_size (expr), ctx);
8397 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
8398 if (TREE_CODE_CLASS (code) != 't' && TREE_CODE_CLASS (code) != 'd')
8399 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
8400 len = TREE_CODE_LENGTH (code);
8401 switch (TREE_CODE_CLASS (code))
8407 md5_process_bytes (TREE_STRING_POINTER (expr),
8408 TREE_STRING_LENGTH (expr), ctx);
8411 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
8412 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
8415 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
8425 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
8426 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
8429 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
8430 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
8439 case SAVE_EXPR: len = 2; break;
8440 case GOTO_SUBROUTINE_EXPR: len = 0; break;
8441 case RTL_EXPR: len = 0; break;
8442 case WITH_CLEANUP_EXPR: len = 2; break;
8451 for (i = 0; i < len; ++i)
8452 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
8455 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
8456 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
8457 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
8458 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
8459 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
8460 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
8461 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
8462 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
8463 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
8464 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
8465 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
8468 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
8469 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
8470 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
8471 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
8472 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
8473 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
8474 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
8475 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
8476 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
8477 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
8486 /* Perform constant folding and related simplification of initializer
8487 expression EXPR. This behaves identically to "fold" but ignores
8488 potential run-time traps and exceptions that fold must preserve. */
8491 fold_initializer (tree expr)
8493 int saved_signaling_nans = flag_signaling_nans;
8494 int saved_trapping_math = flag_trapping_math;
8495 int saved_trapv = flag_trapv;
8498 flag_signaling_nans = 0;
8499 flag_trapping_math = 0;
8502 result = fold (expr);
8504 flag_signaling_nans = saved_signaling_nans;
8505 flag_trapping_math = saved_trapping_math;
8506 flag_trapv = saved_trapv;
8511 /* Determine if first argument is a multiple of second argument. Return 0 if
8512 it is not, or we cannot easily determined it to be.
8514 An example of the sort of thing we care about (at this point; this routine
8515 could surely be made more general, and expanded to do what the *_DIV_EXPR's
8516 fold cases do now) is discovering that
8518 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8524 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
8526 This code also handles discovering that
8528 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8530 is a multiple of 8 so we don't have to worry about dealing with a
8533 Note that we *look* inside a SAVE_EXPR only to determine how it was
8534 calculated; it is not safe for fold to do much of anything else with the
8535 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
8536 at run time. For example, the latter example above *cannot* be implemented
8537 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
8538 evaluation time of the original SAVE_EXPR is not necessarily the same at
8539 the time the new expression is evaluated. The only optimization of this
8540 sort that would be valid is changing
8542 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
8546 SAVE_EXPR (I) * SAVE_EXPR (J)
8548 (where the same SAVE_EXPR (J) is used in the original and the
8549 transformed version). */
8552 multiple_of_p (tree type, tree top, tree bottom)
8554 if (operand_equal_p (top, bottom, 0))
8557 if (TREE_CODE (type) != INTEGER_TYPE)
8560 switch (TREE_CODE (top))
8563 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8564 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8568 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8569 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8572 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
8576 op1 = TREE_OPERAND (top, 1);
8577 /* const_binop may not detect overflow correctly,
8578 so check for it explicitly here. */
8579 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
8580 > TREE_INT_CST_LOW (op1)
8581 && TREE_INT_CST_HIGH (op1) == 0
8582 && 0 != (t1 = convert (type,
8583 const_binop (LSHIFT_EXPR, size_one_node,
8585 && ! TREE_OVERFLOW (t1))
8586 return multiple_of_p (type, t1, bottom);
8591 /* Can't handle conversions from non-integral or wider integral type. */
8592 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
8593 || (TYPE_PRECISION (type)
8594 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
8597 /* .. fall through ... */
8600 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
8603 if (TREE_CODE (bottom) != INTEGER_CST
8604 || (TREE_UNSIGNED (type)
8605 && (tree_int_cst_sgn (top) < 0
8606 || tree_int_cst_sgn (bottom) < 0)))
8608 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
8616 /* Return true if `t' is known to be non-negative. */
8619 tree_expr_nonnegative_p (tree t)
8621 switch (TREE_CODE (t))
8627 return tree_int_cst_sgn (t) >= 0;
8630 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
8633 if (FLOAT_TYPE_P (TREE_TYPE (t)))
8634 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8635 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8637 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
8638 both unsigned and at least 2 bits shorter than the result. */
8639 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8640 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8641 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8643 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8644 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8645 if (TREE_CODE (inner1) == INTEGER_TYPE && TREE_UNSIGNED (inner1)
8646 && TREE_CODE (inner2) == INTEGER_TYPE && TREE_UNSIGNED (inner2))
8648 unsigned int prec = MAX (TYPE_PRECISION (inner1),
8649 TYPE_PRECISION (inner2)) + 1;
8650 return prec < TYPE_PRECISION (TREE_TYPE (t));
8656 if (FLOAT_TYPE_P (TREE_TYPE (t)))
8658 /* x * x for floating point x is always non-negative. */
8659 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
8661 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8662 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8665 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
8666 both unsigned and their total bits is shorter than the result. */
8667 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8668 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8669 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8671 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8672 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8673 if (TREE_CODE (inner1) == INTEGER_TYPE && TREE_UNSIGNED (inner1)
8674 && TREE_CODE (inner2) == INTEGER_TYPE && TREE_UNSIGNED (inner2))
8675 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
8676 < TYPE_PRECISION (TREE_TYPE (t));
8680 case TRUNC_DIV_EXPR:
8682 case FLOOR_DIV_EXPR:
8683 case ROUND_DIV_EXPR:
8684 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8685 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8687 case TRUNC_MOD_EXPR:
8689 case FLOOR_MOD_EXPR:
8690 case ROUND_MOD_EXPR:
8691 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8694 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8695 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8699 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
8700 tree outer_type = TREE_TYPE (t);
8702 if (TREE_CODE (outer_type) == REAL_TYPE)
8704 if (TREE_CODE (inner_type) == REAL_TYPE)
8705 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8706 if (TREE_CODE (inner_type) == INTEGER_TYPE)
8708 if (TREE_UNSIGNED (inner_type))
8710 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8713 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
8715 if (TREE_CODE (inner_type) == REAL_TYPE)
8716 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
8717 if (TREE_CODE (inner_type) == INTEGER_TYPE)
8718 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
8719 && TREE_UNSIGNED (inner_type);
8725 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
8726 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
8728 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8730 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8731 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8733 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8734 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8736 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8738 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8740 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8741 case NON_LVALUE_EXPR:
8742 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8744 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8746 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
8750 tree fndecl = get_callee_fndecl (t);
8751 tree arglist = TREE_OPERAND (t, 1);
8753 && DECL_BUILT_IN (fndecl)
8754 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD)
8755 switch (DECL_FUNCTION_CODE (fndecl))
8758 case BUILT_IN_CABSL:
8759 case BUILT_IN_CABSF:
8764 case BUILT_IN_EXP2F:
8765 case BUILT_IN_EXP2L:
8766 case BUILT_IN_EXP10:
8767 case BUILT_IN_EXP10F:
8768 case BUILT_IN_EXP10L:
8770 case BUILT_IN_FABSF:
8771 case BUILT_IN_FABSL:
8774 case BUILT_IN_FFSLL:
8775 case BUILT_IN_PARITY:
8776 case BUILT_IN_PARITYL:
8777 case BUILT_IN_PARITYLL:
8778 case BUILT_IN_POPCOUNT:
8779 case BUILT_IN_POPCOUNTL:
8780 case BUILT_IN_POPCOUNTLL:
8781 case BUILT_IN_POW10:
8782 case BUILT_IN_POW10F:
8783 case BUILT_IN_POW10L:
8785 case BUILT_IN_SQRTF:
8786 case BUILT_IN_SQRTL:
8790 case BUILT_IN_ATANF:
8791 case BUILT_IN_ATANL:
8793 case BUILT_IN_CEILF:
8794 case BUILT_IN_CEILL:
8795 case BUILT_IN_FLOOR:
8796 case BUILT_IN_FLOORF:
8797 case BUILT_IN_FLOORL:
8798 case BUILT_IN_NEARBYINT:
8799 case BUILT_IN_NEARBYINTF:
8800 case BUILT_IN_NEARBYINTL:
8801 case BUILT_IN_ROUND:
8802 case BUILT_IN_ROUNDF:
8803 case BUILT_IN_ROUNDL:
8804 case BUILT_IN_TRUNC:
8805 case BUILT_IN_TRUNCF:
8806 case BUILT_IN_TRUNCL:
8807 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
8812 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
8819 /* ... fall through ... */
8822 if (truth_value_p (TREE_CODE (t)))
8823 /* Truth values evaluate to 0 or 1, which is nonnegative. */
8827 /* We don't know sign of `t', so be conservative and return false. */
8831 /* Return true if `r' is known to be non-negative.
8832 Only handles constants at the moment. */
8835 rtl_expr_nonnegative_p (rtx r)
8837 switch (GET_CODE (r))
8840 return INTVAL (r) >= 0;
8843 if (GET_MODE (r) == VOIDmode)
8844 return CONST_DOUBLE_HIGH (r) >= 0;
8852 units = CONST_VECTOR_NUNITS (r);
8854 for (i = 0; i < units; ++i)
8856 elt = CONST_VECTOR_ELT (r, i);
8857 if (!rtl_expr_nonnegative_p (elt))
8866 /* These are always nonnegative. */
8874 #include "gt-fold-const.h"