* function.c (walk_fixup_memory_subreg): New parameter 'var'.
[official-gcc.git] / gcc / fold-const.c
blobb0d1db29237d4fcd2422ecb6f03963cf77332afb
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
10 version.
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
15 for more details.
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
20 02111-1307, USA. */
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
31 and force_fit_type.
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. */
45 #include "config.h"
46 #include "system.h"
47 #include "coretypes.h"
48 #include "tm.h"
49 #include "flags.h"
50 #include "tree.h"
51 #include "real.h"
52 #include "rtl.h"
53 #include "expr.h"
54 #include "tm_p.h"
55 #include "toplev.h"
56 #include "ggc.h"
57 #include "hashtab.h"
58 #include "langhooks.h"
59 #include "md5.h"
61 /* The following constants represent a bit based encoding of GCC's
62 comparison operators. This encoding simplifies transformations
63 on relational comparison operators, such as AND and OR. */
64 enum comparison_code {
65 COMPCODE_FALSE = 0,
66 COMPCODE_LT = 1,
67 COMPCODE_EQ = 2,
68 COMPCODE_LE = 3,
69 COMPCODE_GT = 4,
70 COMPCODE_LTGT = 5,
71 COMPCODE_GE = 6,
72 COMPCODE_ORD = 7,
73 COMPCODE_UNORD = 8,
74 COMPCODE_UNLT = 9,
75 COMPCODE_UNEQ = 10,
76 COMPCODE_UNLE = 11,
77 COMPCODE_UNGT = 12,
78 COMPCODE_NE = 13,
79 COMPCODE_UNGE = 14,
80 COMPCODE_TRUE = 15
83 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
84 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
85 static bool negate_mathfn_p (enum built_in_function);
86 static bool negate_expr_p (tree);
87 static tree negate_expr (tree);
88 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
89 static tree associate_trees (tree, tree, enum tree_code, tree);
90 static tree const_binop (enum tree_code, tree, tree, int);
91 static hashval_t size_htab_hash (const void *);
92 static int size_htab_eq (const void *, const void *);
93 static tree fold_convert_const (enum tree_code, tree, tree);
94 static enum tree_code invert_tree_comparison (enum tree_code, bool);
95 static enum tree_code swap_tree_comparison (enum tree_code);
96 static enum comparison_code comparison_to_compcode (enum tree_code);
97 static enum tree_code compcode_to_comparison (enum comparison_code);
98 static tree combine_comparisons (enum tree_code, enum tree_code,
99 enum tree_code, tree, tree, tree);
100 static int truth_value_p (enum tree_code);
101 static int operand_equal_for_comparison_p (tree, tree, tree);
102 static int twoval_comparison_p (tree, tree *, tree *, int *);
103 static tree eval_subst (tree, tree, tree, tree, tree);
104 static tree pedantic_omit_one_operand (tree, tree, tree);
105 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
106 static tree make_bit_field_ref (tree, tree, int, int, int);
107 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
108 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
109 enum machine_mode *, int *, int *,
110 tree *, tree *);
111 static int all_ones_mask_p (tree, int);
112 static tree sign_bit_p (tree, tree);
113 static int simple_operand_p (tree);
114 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
115 static tree make_range (tree, int *, tree *, tree *);
116 static tree build_range_check (tree, tree, int, tree, tree);
117 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
118 tree);
119 static tree fold_range_test (tree);
120 static tree unextend (tree, int, int, tree);
121 static tree fold_truthop (enum tree_code, tree, tree, tree);
122 static tree optimize_minmax_comparison (tree);
123 static tree extract_muldiv (tree, tree, enum tree_code, tree);
124 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
125 static int multiple_of_p (tree, tree, tree);
126 static tree constant_boolean_node (int, tree);
127 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree, tree,
128 tree, int);
129 static bool fold_real_zero_addition_p (tree, tree, int);
130 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
131 tree, tree, tree);
132 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
133 static tree fold_div_compare (enum tree_code, tree, tree, tree);
134 static bool reorder_operands_p (tree, tree);
135 static bool tree_swap_operands_p (tree, tree, bool);
137 static tree fold_negate_const (tree, tree);
138 static tree fold_abs_const (tree, tree);
139 static tree fold_relational_const (enum tree_code, tree, tree, tree);
140 static tree fold_relational_hi_lo (enum tree_code *, const tree, tree *, tree *);
142 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
143 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
144 and SUM1. Then this yields nonzero if overflow occurred during the
145 addition.
147 Overflow occurs if A and B have the same sign, but A and SUM differ in
148 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
149 sign. */
150 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
152 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
153 We do that by representing the two-word integer in 4 words, with only
154 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
155 number. The value of the word is LOWPART + HIGHPART * BASE. */
157 #define LOWPART(x) \
158 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
159 #define HIGHPART(x) \
160 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
161 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
163 /* Unpack a two-word integer into 4 words.
164 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
165 WORDS points to the array of HOST_WIDE_INTs. */
167 static void
168 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
170 words[0] = LOWPART (low);
171 words[1] = HIGHPART (low);
172 words[2] = LOWPART (hi);
173 words[3] = HIGHPART (hi);
176 /* Pack an array of 4 words into a two-word integer.
177 WORDS points to the array of words.
178 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
180 static void
181 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
182 HOST_WIDE_INT *hi)
184 *low = words[0] + words[1] * BASE;
185 *hi = words[2] + words[3] * BASE;
188 /* Make the integer constant T valid for its type by setting to 0 or 1 all
189 the bits in the constant that don't belong in the type.
191 Return 1 if a signed overflow occurs, 0 otherwise. If OVERFLOW is
192 nonzero, a signed overflow has already occurred in calculating T, so
193 propagate it. */
196 force_fit_type (tree t, int overflow)
198 unsigned HOST_WIDE_INT low;
199 HOST_WIDE_INT high;
200 unsigned int prec;
202 if (TREE_CODE (t) == REAL_CST)
204 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
205 Consider doing it via real_convert now. */
206 return overflow;
209 else if (TREE_CODE (t) != INTEGER_CST)
210 return overflow;
212 low = TREE_INT_CST_LOW (t);
213 high = TREE_INT_CST_HIGH (t);
215 if (POINTER_TYPE_P (TREE_TYPE (t))
216 || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
217 prec = POINTER_SIZE;
218 else
219 prec = TYPE_PRECISION (TREE_TYPE (t));
221 /* First clear all bits that are beyond the type's precision. */
223 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
225 else if (prec > HOST_BITS_PER_WIDE_INT)
226 TREE_INT_CST_HIGH (t)
227 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
228 else
230 TREE_INT_CST_HIGH (t) = 0;
231 if (prec < HOST_BITS_PER_WIDE_INT)
232 TREE_INT_CST_LOW (t) &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
235 /* Unsigned types do not suffer sign extension or overflow unless they
236 are a sizetype. */
237 if (TYPE_UNSIGNED (TREE_TYPE (t))
238 && ! (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
239 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
240 return overflow;
242 /* If the value's sign bit is set, extend the sign. */
243 if (prec != 2 * HOST_BITS_PER_WIDE_INT
244 && (prec > HOST_BITS_PER_WIDE_INT
245 ? 0 != (TREE_INT_CST_HIGH (t)
246 & ((HOST_WIDE_INT) 1
247 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
248 : 0 != (TREE_INT_CST_LOW (t)
249 & ((unsigned HOST_WIDE_INT) 1 << (prec - 1)))))
251 /* Value is negative:
252 set to 1 all the bits that are outside this type's precision. */
253 if (prec > HOST_BITS_PER_WIDE_INT)
254 TREE_INT_CST_HIGH (t)
255 |= ((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
256 else
258 TREE_INT_CST_HIGH (t) = -1;
259 if (prec < HOST_BITS_PER_WIDE_INT)
260 TREE_INT_CST_LOW (t) |= ((unsigned HOST_WIDE_INT) (-1) << prec);
264 /* Return nonzero if signed overflow occurred. */
265 return
266 ((overflow | (low ^ TREE_INT_CST_LOW (t)) | (high ^ TREE_INT_CST_HIGH (t)))
267 != 0);
270 /* Add two doubleword integers with doubleword result.
271 Each argument is given as two `HOST_WIDE_INT' pieces.
272 One argument is L1 and H1; the other, L2 and H2.
273 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
276 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
277 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
278 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
280 unsigned HOST_WIDE_INT l;
281 HOST_WIDE_INT h;
283 l = l1 + l2;
284 h = h1 + h2 + (l < l1);
286 *lv = l;
287 *hv = h;
288 return OVERFLOW_SUM_SIGN (h1, h2, h);
291 /* Negate a doubleword integer with doubleword result.
292 Return nonzero if the operation overflows, assuming it's signed.
293 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
294 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
297 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
298 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
300 if (l1 == 0)
302 *lv = 0;
303 *hv = - h1;
304 return (*hv & h1) < 0;
306 else
308 *lv = -l1;
309 *hv = ~h1;
310 return 0;
314 /* Multiply two doubleword integers with doubleword result.
315 Return nonzero if the operation overflows, assuming it's signed.
316 Each argument is given as two `HOST_WIDE_INT' pieces.
317 One argument is L1 and H1; the other, L2 and H2.
318 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
321 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
322 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
323 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
325 HOST_WIDE_INT arg1[4];
326 HOST_WIDE_INT arg2[4];
327 HOST_WIDE_INT prod[4 * 2];
328 unsigned HOST_WIDE_INT carry;
329 int i, j, k;
330 unsigned HOST_WIDE_INT toplow, neglow;
331 HOST_WIDE_INT tophigh, neghigh;
333 encode (arg1, l1, h1);
334 encode (arg2, l2, h2);
336 memset (prod, 0, sizeof prod);
338 for (i = 0; i < 4; i++)
340 carry = 0;
341 for (j = 0; j < 4; j++)
343 k = i + j;
344 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
345 carry += arg1[i] * arg2[j];
346 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
347 carry += prod[k];
348 prod[k] = LOWPART (carry);
349 carry = HIGHPART (carry);
351 prod[i + 4] = carry;
354 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
356 /* Check for overflow by calculating the top half of the answer in full;
357 it should agree with the low half's sign bit. */
358 decode (prod + 4, &toplow, &tophigh);
359 if (h1 < 0)
361 neg_double (l2, h2, &neglow, &neghigh);
362 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
364 if (h2 < 0)
366 neg_double (l1, h1, &neglow, &neghigh);
367 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
369 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
372 /* Shift the doubleword integer in L1, H1 left by COUNT places
373 keeping only PREC bits of result.
374 Shift right if COUNT is negative.
375 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
376 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
378 void
379 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
380 HOST_WIDE_INT count, unsigned int prec,
381 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
383 unsigned HOST_WIDE_INT signmask;
385 if (count < 0)
387 rshift_double (l1, h1, -count, prec, lv, hv, arith);
388 return;
391 if (SHIFT_COUNT_TRUNCATED)
392 count %= prec;
394 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
396 /* Shifting by the host word size is undefined according to the
397 ANSI standard, so we must handle this as a special case. */
398 *hv = 0;
399 *lv = 0;
401 else if (count >= HOST_BITS_PER_WIDE_INT)
403 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
404 *lv = 0;
406 else
408 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
409 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
410 *lv = l1 << count;
413 /* Sign extend all bits that are beyond the precision. */
415 signmask = -((prec > HOST_BITS_PER_WIDE_INT
416 ? ((unsigned HOST_WIDE_INT) *hv
417 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
418 : (*lv >> (prec - 1))) & 1);
420 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
422 else if (prec >= HOST_BITS_PER_WIDE_INT)
424 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
425 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
427 else
429 *hv = signmask;
430 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
431 *lv |= signmask << prec;
435 /* Shift the doubleword integer in L1, H1 right by COUNT places
436 keeping only PREC bits of result. COUNT must be positive.
437 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
438 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
440 void
441 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
442 HOST_WIDE_INT count, unsigned int prec,
443 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
444 int arith)
446 unsigned HOST_WIDE_INT signmask;
448 signmask = (arith
449 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
450 : 0);
452 if (SHIFT_COUNT_TRUNCATED)
453 count %= prec;
455 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
457 /* Shifting by the host word size is undefined according to the
458 ANSI standard, so we must handle this as a special case. */
459 *hv = 0;
460 *lv = 0;
462 else if (count >= HOST_BITS_PER_WIDE_INT)
464 *hv = 0;
465 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
467 else
469 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
470 *lv = ((l1 >> count)
471 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
474 /* Zero / sign extend all bits that are beyond the precision. */
476 if (count >= (HOST_WIDE_INT)prec)
478 *hv = signmask;
479 *lv = signmask;
481 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
483 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
485 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
486 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
488 else
490 *hv = signmask;
491 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
492 *lv |= signmask << (prec - count);
496 /* Rotate the doubleword integer in L1, H1 left by COUNT places
497 keeping only PREC bits of result.
498 Rotate right if COUNT is negative.
499 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
501 void
502 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
503 HOST_WIDE_INT count, unsigned int prec,
504 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
506 unsigned HOST_WIDE_INT s1l, s2l;
507 HOST_WIDE_INT s1h, s2h;
509 count %= prec;
510 if (count < 0)
511 count += prec;
513 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
514 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
515 *lv = s1l | s2l;
516 *hv = s1h | s2h;
519 /* Rotate the doubleword integer in L1, H1 left by COUNT places
520 keeping only PREC bits of result. COUNT must be positive.
521 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
523 void
524 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
525 HOST_WIDE_INT count, unsigned int prec,
526 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
528 unsigned HOST_WIDE_INT s1l, s2l;
529 HOST_WIDE_INT s1h, s2h;
531 count %= prec;
532 if (count < 0)
533 count += prec;
535 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
536 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
537 *lv = s1l | s2l;
538 *hv = s1h | s2h;
541 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
542 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
543 CODE is a tree code for a kind of division, one of
544 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
545 or EXACT_DIV_EXPR
546 It controls how the quotient is rounded to an integer.
547 Return nonzero if the operation overflows.
548 UNS nonzero says do unsigned division. */
551 div_and_round_double (enum tree_code code, int uns,
552 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
553 HOST_WIDE_INT hnum_orig,
554 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
555 HOST_WIDE_INT hden_orig,
556 unsigned HOST_WIDE_INT *lquo,
557 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
558 HOST_WIDE_INT *hrem)
560 int quo_neg = 0;
561 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
562 HOST_WIDE_INT den[4], quo[4];
563 int i, j;
564 unsigned HOST_WIDE_INT work;
565 unsigned HOST_WIDE_INT carry = 0;
566 unsigned HOST_WIDE_INT lnum = lnum_orig;
567 HOST_WIDE_INT hnum = hnum_orig;
568 unsigned HOST_WIDE_INT lden = lden_orig;
569 HOST_WIDE_INT hden = hden_orig;
570 int overflow = 0;
572 if (hden == 0 && lden == 0)
573 overflow = 1, lden = 1;
575 /* Calculate quotient sign and convert operands to unsigned. */
576 if (!uns)
578 if (hnum < 0)
580 quo_neg = ~ quo_neg;
581 /* (minimum integer) / (-1) is the only overflow case. */
582 if (neg_double (lnum, hnum, &lnum, &hnum)
583 && ((HOST_WIDE_INT) lden & hden) == -1)
584 overflow = 1;
586 if (hden < 0)
588 quo_neg = ~ quo_neg;
589 neg_double (lden, hden, &lden, &hden);
593 if (hnum == 0 && hden == 0)
594 { /* single precision */
595 *hquo = *hrem = 0;
596 /* This unsigned division rounds toward zero. */
597 *lquo = lnum / lden;
598 goto finish_up;
601 if (hnum == 0)
602 { /* trivial case: dividend < divisor */
603 /* hden != 0 already checked. */
604 *hquo = *lquo = 0;
605 *hrem = hnum;
606 *lrem = lnum;
607 goto finish_up;
610 memset (quo, 0, sizeof quo);
612 memset (num, 0, sizeof num); /* to zero 9th element */
613 memset (den, 0, sizeof den);
615 encode (num, lnum, hnum);
616 encode (den, lden, hden);
618 /* Special code for when the divisor < BASE. */
619 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
621 /* hnum != 0 already checked. */
622 for (i = 4 - 1; i >= 0; i--)
624 work = num[i] + carry * BASE;
625 quo[i] = work / lden;
626 carry = work % lden;
629 else
631 /* Full double precision division,
632 with thanks to Don Knuth's "Seminumerical Algorithms". */
633 int num_hi_sig, den_hi_sig;
634 unsigned HOST_WIDE_INT quo_est, scale;
636 /* Find the highest nonzero divisor digit. */
637 for (i = 4 - 1;; i--)
638 if (den[i] != 0)
640 den_hi_sig = i;
641 break;
644 /* Insure that the first digit of the divisor is at least BASE/2.
645 This is required by the quotient digit estimation algorithm. */
647 scale = BASE / (den[den_hi_sig] + 1);
648 if (scale > 1)
649 { /* scale divisor and dividend */
650 carry = 0;
651 for (i = 0; i <= 4 - 1; i++)
653 work = (num[i] * scale) + carry;
654 num[i] = LOWPART (work);
655 carry = HIGHPART (work);
658 num[4] = carry;
659 carry = 0;
660 for (i = 0; i <= 4 - 1; i++)
662 work = (den[i] * scale) + carry;
663 den[i] = LOWPART (work);
664 carry = HIGHPART (work);
665 if (den[i] != 0) den_hi_sig = i;
669 num_hi_sig = 4;
671 /* Main loop */
672 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
674 /* Guess the next quotient digit, quo_est, by dividing the first
675 two remaining dividend digits by the high order quotient digit.
676 quo_est is never low and is at most 2 high. */
677 unsigned HOST_WIDE_INT tmp;
679 num_hi_sig = i + den_hi_sig + 1;
680 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
681 if (num[num_hi_sig] != den[den_hi_sig])
682 quo_est = work / den[den_hi_sig];
683 else
684 quo_est = BASE - 1;
686 /* Refine quo_est so it's usually correct, and at most one high. */
687 tmp = work - quo_est * den[den_hi_sig];
688 if (tmp < BASE
689 && (den[den_hi_sig - 1] * quo_est
690 > (tmp * BASE + num[num_hi_sig - 2])))
691 quo_est--;
693 /* Try QUO_EST as the quotient digit, by multiplying the
694 divisor by QUO_EST and subtracting from the remaining dividend.
695 Keep in mind that QUO_EST is the I - 1st digit. */
697 carry = 0;
698 for (j = 0; j <= den_hi_sig; j++)
700 work = quo_est * den[j] + carry;
701 carry = HIGHPART (work);
702 work = num[i + j] - LOWPART (work);
703 num[i + j] = LOWPART (work);
704 carry += HIGHPART (work) != 0;
707 /* If quo_est was high by one, then num[i] went negative and
708 we need to correct things. */
709 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
711 quo_est--;
712 carry = 0; /* add divisor back in */
713 for (j = 0; j <= den_hi_sig; j++)
715 work = num[i + j] + den[j] + carry;
716 carry = HIGHPART (work);
717 num[i + j] = LOWPART (work);
720 num [num_hi_sig] += carry;
723 /* Store the quotient digit. */
724 quo[i] = quo_est;
728 decode (quo, lquo, hquo);
730 finish_up:
731 /* If result is negative, make it so. */
732 if (quo_neg)
733 neg_double (*lquo, *hquo, lquo, hquo);
735 /* Compute trial remainder: rem = num - (quo * den) */
736 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
737 neg_double (*lrem, *hrem, lrem, hrem);
738 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
740 switch (code)
742 case TRUNC_DIV_EXPR:
743 case TRUNC_MOD_EXPR: /* round toward zero */
744 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
745 return overflow;
747 case FLOOR_DIV_EXPR:
748 case FLOOR_MOD_EXPR: /* round toward negative infinity */
749 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
751 /* quo = quo - 1; */
752 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
753 lquo, hquo);
755 else
756 return overflow;
757 break;
759 case CEIL_DIV_EXPR:
760 case CEIL_MOD_EXPR: /* round toward positive infinity */
761 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
763 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
764 lquo, hquo);
766 else
767 return overflow;
768 break;
770 case ROUND_DIV_EXPR:
771 case ROUND_MOD_EXPR: /* round to closest integer */
773 unsigned HOST_WIDE_INT labs_rem = *lrem;
774 HOST_WIDE_INT habs_rem = *hrem;
775 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
776 HOST_WIDE_INT habs_den = hden, htwice;
778 /* Get absolute values. */
779 if (*hrem < 0)
780 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
781 if (hden < 0)
782 neg_double (lden, hden, &labs_den, &habs_den);
784 /* If (2 * abs (lrem) >= abs (lden)) */
785 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
786 labs_rem, habs_rem, &ltwice, &htwice);
788 if (((unsigned HOST_WIDE_INT) habs_den
789 < (unsigned HOST_WIDE_INT) htwice)
790 || (((unsigned HOST_WIDE_INT) habs_den
791 == (unsigned HOST_WIDE_INT) htwice)
792 && (labs_den < ltwice)))
794 if (*hquo < 0)
795 /* quo = quo - 1; */
796 add_double (*lquo, *hquo,
797 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
798 else
799 /* quo = quo + 1; */
800 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
801 lquo, hquo);
803 else
804 return overflow;
806 break;
808 default:
809 abort ();
812 /* Compute true remainder: rem = num - (quo * den) */
813 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
814 neg_double (*lrem, *hrem, lrem, hrem);
815 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
816 return overflow;
819 /* Return true if built-in mathematical function specified by CODE
820 preserves the sign of it argument, i.e. -f(x) == f(-x). */
822 static bool
823 negate_mathfn_p (enum built_in_function code)
825 switch (code)
827 case BUILT_IN_ASIN:
828 case BUILT_IN_ASINF:
829 case BUILT_IN_ASINL:
830 case BUILT_IN_ATAN:
831 case BUILT_IN_ATANF:
832 case BUILT_IN_ATANL:
833 case BUILT_IN_SIN:
834 case BUILT_IN_SINF:
835 case BUILT_IN_SINL:
836 case BUILT_IN_TAN:
837 case BUILT_IN_TANF:
838 case BUILT_IN_TANL:
839 return true;
841 default:
842 break;
844 return false;
847 /* Determine whether an expression T can be cheaply negated using
848 the function negate_expr. */
850 static bool
851 negate_expr_p (tree t)
853 unsigned HOST_WIDE_INT val;
854 unsigned int prec;
855 tree type;
857 if (t == 0)
858 return false;
860 type = TREE_TYPE (t);
862 STRIP_SIGN_NOPS (t);
863 switch (TREE_CODE (t))
865 case INTEGER_CST:
866 if (TYPE_UNSIGNED (type) || ! flag_trapv)
867 return true;
869 /* Check that -CST will not overflow type. */
870 prec = TYPE_PRECISION (type);
871 if (prec > HOST_BITS_PER_WIDE_INT)
873 if (TREE_INT_CST_LOW (t) != 0)
874 return true;
875 prec -= HOST_BITS_PER_WIDE_INT;
876 val = TREE_INT_CST_HIGH (t);
878 else
879 val = TREE_INT_CST_LOW (t);
880 if (prec < HOST_BITS_PER_WIDE_INT)
881 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
882 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
884 case REAL_CST:
885 case NEGATE_EXPR:
886 return true;
888 case COMPLEX_CST:
889 return negate_expr_p (TREE_REALPART (t))
890 && negate_expr_p (TREE_IMAGPART (t));
892 case PLUS_EXPR:
893 if (FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
894 return false;
895 /* -(A + B) -> (-B) - A. */
896 if (negate_expr_p (TREE_OPERAND (t, 1))
897 && reorder_operands_p (TREE_OPERAND (t, 0),
898 TREE_OPERAND (t, 1)))
899 return true;
900 /* -(A + B) -> (-A) - B. */
901 return negate_expr_p (TREE_OPERAND (t, 0));
903 case MINUS_EXPR:
904 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
905 return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
906 && reorder_operands_p (TREE_OPERAND (t, 0),
907 TREE_OPERAND (t, 1));
909 case MULT_EXPR:
910 if (TYPE_UNSIGNED (TREE_TYPE (t)))
911 break;
913 /* Fall through. */
915 case RDIV_EXPR:
916 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
917 return negate_expr_p (TREE_OPERAND (t, 1))
918 || negate_expr_p (TREE_OPERAND (t, 0));
919 break;
921 case NOP_EXPR:
922 /* Negate -((double)float) as (double)(-float). */
923 if (TREE_CODE (type) == REAL_TYPE)
925 tree tem = strip_float_extensions (t);
926 if (tem != t)
927 return negate_expr_p (tem);
929 break;
931 case CALL_EXPR:
932 /* Negate -f(x) as f(-x). */
933 if (negate_mathfn_p (builtin_mathfn_code (t)))
934 return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
935 break;
937 case RSHIFT_EXPR:
938 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
939 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
941 tree op1 = TREE_OPERAND (t, 1);
942 if (TREE_INT_CST_HIGH (op1) == 0
943 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
944 == TREE_INT_CST_LOW (op1))
945 return true;
947 break;
949 default:
950 break;
952 return false;
955 /* Given T, an expression, return the negation of T. Allow for T to be
956 null, in which case return null. */
958 static tree
959 negate_expr (tree t)
961 tree type;
962 tree tem;
964 if (t == 0)
965 return 0;
967 type = TREE_TYPE (t);
968 STRIP_SIGN_NOPS (t);
970 switch (TREE_CODE (t))
972 case INTEGER_CST:
973 tem = fold_negate_const (t, type);
974 if (! TREE_OVERFLOW (tem)
975 || TYPE_UNSIGNED (type)
976 || ! flag_trapv)
977 return tem;
978 break;
980 case REAL_CST:
981 tem = fold_negate_const (t, type);
982 /* Two's complement FP formats, such as c4x, may overflow. */
983 if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
984 return fold_convert (type, tem);
985 break;
987 case COMPLEX_CST:
989 tree rpart = negate_expr (TREE_REALPART (t));
990 tree ipart = negate_expr (TREE_IMAGPART (t));
992 if ((TREE_CODE (rpart) == REAL_CST
993 && TREE_CODE (ipart) == REAL_CST)
994 || (TREE_CODE (rpart) == INTEGER_CST
995 && TREE_CODE (ipart) == INTEGER_CST))
996 return build_complex (type, rpart, ipart);
998 break;
1000 case NEGATE_EXPR:
1001 return fold_convert (type, TREE_OPERAND (t, 0));
1003 case PLUS_EXPR:
1004 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1006 /* -(A + B) -> (-B) - A. */
1007 if (negate_expr_p (TREE_OPERAND (t, 1))
1008 && reorder_operands_p (TREE_OPERAND (t, 0),
1009 TREE_OPERAND (t, 1)))
1011 tem = negate_expr (TREE_OPERAND (t, 1));
1012 tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1013 tem, TREE_OPERAND (t, 0)));
1014 return fold_convert (type, tem);
1017 /* -(A + B) -> (-A) - B. */
1018 if (negate_expr_p (TREE_OPERAND (t, 0)))
1020 tem = negate_expr (TREE_OPERAND (t, 0));
1021 tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1022 tem, TREE_OPERAND (t, 1)));
1023 return fold_convert (type, tem);
1026 break;
1028 case MINUS_EXPR:
1029 /* - (A - B) -> B - A */
1030 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1031 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1032 return fold_convert (type,
1033 fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1034 TREE_OPERAND (t, 1),
1035 TREE_OPERAND (t, 0))));
1036 break;
1038 case MULT_EXPR:
1039 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1040 break;
1042 /* Fall through. */
1044 case RDIV_EXPR:
1045 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1047 tem = TREE_OPERAND (t, 1);
1048 if (negate_expr_p (tem))
1049 return fold_convert (type,
1050 fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1051 TREE_OPERAND (t, 0),
1052 negate_expr (tem))));
1053 tem = TREE_OPERAND (t, 0);
1054 if (negate_expr_p (tem))
1055 return fold_convert (type,
1056 fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1057 negate_expr (tem),
1058 TREE_OPERAND (t, 1))));
1060 break;
1062 case NOP_EXPR:
1063 /* Convert -((double)float) into (double)(-float). */
1064 if (TREE_CODE (type) == REAL_TYPE)
1066 tem = strip_float_extensions (t);
1067 if (tem != t && negate_expr_p (tem))
1068 return fold_convert (type, negate_expr (tem));
1070 break;
1072 case CALL_EXPR:
1073 /* Negate -f(x) as f(-x). */
1074 if (negate_mathfn_p (builtin_mathfn_code (t))
1075 && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1077 tree fndecl, arg, arglist;
1079 fndecl = get_callee_fndecl (t);
1080 arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1081 arglist = build_tree_list (NULL_TREE, arg);
1082 return build_function_call_expr (fndecl, arglist);
1084 break;
1086 case RSHIFT_EXPR:
1087 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1088 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1090 tree op1 = TREE_OPERAND (t, 1);
1091 if (TREE_INT_CST_HIGH (op1) == 0
1092 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1093 == TREE_INT_CST_LOW (op1))
1095 tree ntype = TYPE_UNSIGNED (type)
1096 ? lang_hooks.types.signed_type (type)
1097 : lang_hooks.types.unsigned_type (type);
1098 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1099 temp = fold (build2 (RSHIFT_EXPR, ntype, temp, op1));
1100 return fold_convert (type, temp);
1103 break;
1105 default:
1106 break;
1109 tem = fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1110 return fold_convert (type, tem);
1113 /* Split a tree IN into a constant, literal and variable parts that could be
1114 combined with CODE to make IN. "constant" means an expression with
1115 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1116 commutative arithmetic operation. Store the constant part into *CONP,
1117 the literal in *LITP and return the variable part. If a part isn't
1118 present, set it to null. If the tree does not decompose in this way,
1119 return the entire tree as the variable part and the other parts as null.
1121 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1122 case, we negate an operand that was subtracted. Except if it is a
1123 literal for which we use *MINUS_LITP instead.
1125 If NEGATE_P is true, we are negating all of IN, again except a literal
1126 for which we use *MINUS_LITP instead.
1128 If IN is itself a literal or constant, return it as appropriate.
1130 Note that we do not guarantee that any of the three values will be the
1131 same type as IN, but they will have the same signedness and mode. */
1133 static tree
1134 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1135 tree *minus_litp, int negate_p)
1137 tree var = 0;
1139 *conp = 0;
1140 *litp = 0;
1141 *minus_litp = 0;
1143 /* Strip any conversions that don't change the machine mode or signedness. */
1144 STRIP_SIGN_NOPS (in);
1146 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1147 *litp = in;
1148 else if (TREE_CODE (in) == code
1149 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1150 /* We can associate addition and subtraction together (even
1151 though the C standard doesn't say so) for integers because
1152 the value is not affected. For reals, the value might be
1153 affected, so we can't. */
1154 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1155 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1157 tree op0 = TREE_OPERAND (in, 0);
1158 tree op1 = TREE_OPERAND (in, 1);
1159 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1160 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1162 /* First see if either of the operands is a literal, then a constant. */
1163 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1164 *litp = op0, op0 = 0;
1165 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1166 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1168 if (op0 != 0 && TREE_CONSTANT (op0))
1169 *conp = op0, op0 = 0;
1170 else if (op1 != 0 && TREE_CONSTANT (op1))
1171 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1173 /* If we haven't dealt with either operand, this is not a case we can
1174 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1175 if (op0 != 0 && op1 != 0)
1176 var = in;
1177 else if (op0 != 0)
1178 var = op0;
1179 else
1180 var = op1, neg_var_p = neg1_p;
1182 /* Now do any needed negations. */
1183 if (neg_litp_p)
1184 *minus_litp = *litp, *litp = 0;
1185 if (neg_conp_p)
1186 *conp = negate_expr (*conp);
1187 if (neg_var_p)
1188 var = negate_expr (var);
1190 else if (TREE_CONSTANT (in))
1191 *conp = in;
1192 else
1193 var = in;
1195 if (negate_p)
1197 if (*litp)
1198 *minus_litp = *litp, *litp = 0;
1199 else if (*minus_litp)
1200 *litp = *minus_litp, *minus_litp = 0;
1201 *conp = negate_expr (*conp);
1202 var = negate_expr (var);
1205 return var;
1208 /* Re-associate trees split by the above function. T1 and T2 are either
1209 expressions to associate or null. Return the new expression, if any. If
1210 we build an operation, do it in TYPE and with CODE. */
1212 static tree
1213 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1215 if (t1 == 0)
1216 return t2;
1217 else if (t2 == 0)
1218 return t1;
1220 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1221 try to fold this since we will have infinite recursion. But do
1222 deal with any NEGATE_EXPRs. */
1223 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1224 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1226 if (code == PLUS_EXPR)
1228 if (TREE_CODE (t1) == NEGATE_EXPR)
1229 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1230 fold_convert (type, TREE_OPERAND (t1, 0)));
1231 else if (TREE_CODE (t2) == NEGATE_EXPR)
1232 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1233 fold_convert (type, TREE_OPERAND (t2, 0)));
1235 return build2 (code, type, fold_convert (type, t1),
1236 fold_convert (type, t2));
1239 return fold (build2 (code, type, fold_convert (type, t1),
1240 fold_convert (type, t2)));
1243 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1244 to produce a new constant.
1246 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1248 tree
1249 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1251 unsigned HOST_WIDE_INT int1l, int2l;
1252 HOST_WIDE_INT int1h, int2h;
1253 unsigned HOST_WIDE_INT low;
1254 HOST_WIDE_INT hi;
1255 unsigned HOST_WIDE_INT garbagel;
1256 HOST_WIDE_INT garbageh;
1257 tree t;
1258 tree type = TREE_TYPE (arg1);
1259 int uns = TYPE_UNSIGNED (type);
1260 int is_sizetype
1261 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1262 int overflow = 0;
1263 int no_overflow = 0;
1265 int1l = TREE_INT_CST_LOW (arg1);
1266 int1h = TREE_INT_CST_HIGH (arg1);
1267 int2l = TREE_INT_CST_LOW (arg2);
1268 int2h = TREE_INT_CST_HIGH (arg2);
1270 switch (code)
1272 case BIT_IOR_EXPR:
1273 low = int1l | int2l, hi = int1h | int2h;
1274 break;
1276 case BIT_XOR_EXPR:
1277 low = int1l ^ int2l, hi = int1h ^ int2h;
1278 break;
1280 case BIT_AND_EXPR:
1281 low = int1l & int2l, hi = int1h & int2h;
1282 break;
1284 case RSHIFT_EXPR:
1285 int2l = -int2l;
1286 case LSHIFT_EXPR:
1287 /* It's unclear from the C standard whether shifts can overflow.
1288 The following code ignores overflow; perhaps a C standard
1289 interpretation ruling is needed. */
1290 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1291 &low, &hi, !uns);
1292 no_overflow = 1;
1293 break;
1295 case RROTATE_EXPR:
1296 int2l = - int2l;
1297 case LROTATE_EXPR:
1298 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1299 &low, &hi);
1300 break;
1302 case PLUS_EXPR:
1303 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1304 break;
1306 case MINUS_EXPR:
1307 neg_double (int2l, int2h, &low, &hi);
1308 add_double (int1l, int1h, low, hi, &low, &hi);
1309 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1310 break;
1312 case MULT_EXPR:
1313 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1314 break;
1316 case TRUNC_DIV_EXPR:
1317 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1318 case EXACT_DIV_EXPR:
1319 /* This is a shortcut for a common special case. */
1320 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1321 && ! TREE_CONSTANT_OVERFLOW (arg1)
1322 && ! TREE_CONSTANT_OVERFLOW (arg2)
1323 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1325 if (code == CEIL_DIV_EXPR)
1326 int1l += int2l - 1;
1328 low = int1l / int2l, hi = 0;
1329 break;
1332 /* ... fall through ... */
1334 case ROUND_DIV_EXPR:
1335 if (int2h == 0 && int2l == 1)
1337 low = int1l, hi = int1h;
1338 break;
1340 if (int1l == int2l && int1h == int2h
1341 && ! (int1l == 0 && int1h == 0))
1343 low = 1, hi = 0;
1344 break;
1346 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1347 &low, &hi, &garbagel, &garbageh);
1348 break;
1350 case TRUNC_MOD_EXPR:
1351 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1352 /* This is a shortcut for a common special case. */
1353 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1354 && ! TREE_CONSTANT_OVERFLOW (arg1)
1355 && ! TREE_CONSTANT_OVERFLOW (arg2)
1356 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1358 if (code == CEIL_MOD_EXPR)
1359 int1l += int2l - 1;
1360 low = int1l % int2l, hi = 0;
1361 break;
1364 /* ... fall through ... */
1366 case ROUND_MOD_EXPR:
1367 overflow = div_and_round_double (code, uns,
1368 int1l, int1h, int2l, int2h,
1369 &garbagel, &garbageh, &low, &hi);
1370 break;
1372 case MIN_EXPR:
1373 case MAX_EXPR:
1374 if (uns)
1375 low = (((unsigned HOST_WIDE_INT) int1h
1376 < (unsigned HOST_WIDE_INT) int2h)
1377 || (((unsigned HOST_WIDE_INT) int1h
1378 == (unsigned HOST_WIDE_INT) int2h)
1379 && int1l < int2l));
1380 else
1381 low = (int1h < int2h
1382 || (int1h == int2h && int1l < int2l));
1384 if (low == (code == MIN_EXPR))
1385 low = int1l, hi = int1h;
1386 else
1387 low = int2l, hi = int2h;
1388 break;
1390 default:
1391 abort ();
1394 /* If this is for a sizetype, can be represented as one (signed)
1395 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1396 constants. */
1397 if (is_sizetype
1398 && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1399 || (hi == -1 && (HOST_WIDE_INT) low < 0))
1400 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1401 return size_int_type_wide (low, type);
1402 else
1404 t = build_int_2 (low, hi);
1405 TREE_TYPE (t) = TREE_TYPE (arg1);
1408 TREE_OVERFLOW (t)
1409 = ((notrunc
1410 ? (!uns || is_sizetype) && overflow
1411 : (force_fit_type (t, (!uns || is_sizetype) && overflow)
1412 && ! no_overflow))
1413 | TREE_OVERFLOW (arg1)
1414 | TREE_OVERFLOW (arg2));
1416 /* If we're doing a size calculation, unsigned arithmetic does overflow.
1417 So check if force_fit_type truncated the value. */
1418 if (is_sizetype
1419 && ! TREE_OVERFLOW (t)
1420 && (TREE_INT_CST_HIGH (t) != hi
1421 || TREE_INT_CST_LOW (t) != low))
1422 TREE_OVERFLOW (t) = 1;
1424 TREE_CONSTANT_OVERFLOW (t) = (TREE_OVERFLOW (t)
1425 | TREE_CONSTANT_OVERFLOW (arg1)
1426 | TREE_CONSTANT_OVERFLOW (arg2));
1427 return t;
1430 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1431 constant. We assume ARG1 and ARG2 have the same data type, or at least
1432 are the same kind of constant and the same machine mode.
1434 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1436 static tree
1437 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1439 STRIP_NOPS (arg1);
1440 STRIP_NOPS (arg2);
1442 if (TREE_CODE (arg1) == INTEGER_CST)
1443 return int_const_binop (code, arg1, arg2, notrunc);
1445 if (TREE_CODE (arg1) == REAL_CST)
1447 enum machine_mode mode;
1448 REAL_VALUE_TYPE d1;
1449 REAL_VALUE_TYPE d2;
1450 REAL_VALUE_TYPE value;
1451 tree t, type;
1453 d1 = TREE_REAL_CST (arg1);
1454 d2 = TREE_REAL_CST (arg2);
1456 type = TREE_TYPE (arg1);
1457 mode = TYPE_MODE (type);
1459 /* Don't perform operation if we honor signaling NaNs and
1460 either operand is a NaN. */
1461 if (HONOR_SNANS (mode)
1462 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1463 return NULL_TREE;
1465 /* Don't perform operation if it would raise a division
1466 by zero exception. */
1467 if (code == RDIV_EXPR
1468 && REAL_VALUES_EQUAL (d2, dconst0)
1469 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1470 return NULL_TREE;
1472 /* If either operand is a NaN, just return it. Otherwise, set up
1473 for floating-point trap; we return an overflow. */
1474 if (REAL_VALUE_ISNAN (d1))
1475 return arg1;
1476 else if (REAL_VALUE_ISNAN (d2))
1477 return arg2;
1479 REAL_ARITHMETIC (value, code, d1, d2);
1481 t = build_real (type, real_value_truncate (mode, value));
1483 TREE_OVERFLOW (t)
1484 = (force_fit_type (t, 0)
1485 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1486 TREE_CONSTANT_OVERFLOW (t)
1487 = TREE_OVERFLOW (t)
1488 | TREE_CONSTANT_OVERFLOW (arg1)
1489 | TREE_CONSTANT_OVERFLOW (arg2);
1490 return t;
1492 if (TREE_CODE (arg1) == COMPLEX_CST)
1494 tree type = TREE_TYPE (arg1);
1495 tree r1 = TREE_REALPART (arg1);
1496 tree i1 = TREE_IMAGPART (arg1);
1497 tree r2 = TREE_REALPART (arg2);
1498 tree i2 = TREE_IMAGPART (arg2);
1499 tree t;
1501 switch (code)
1503 case PLUS_EXPR:
1504 t = build_complex (type,
1505 const_binop (PLUS_EXPR, r1, r2, notrunc),
1506 const_binop (PLUS_EXPR, i1, i2, notrunc));
1507 break;
1509 case MINUS_EXPR:
1510 t = build_complex (type,
1511 const_binop (MINUS_EXPR, r1, r2, notrunc),
1512 const_binop (MINUS_EXPR, i1, i2, notrunc));
1513 break;
1515 case MULT_EXPR:
1516 t = build_complex (type,
1517 const_binop (MINUS_EXPR,
1518 const_binop (MULT_EXPR,
1519 r1, r2, notrunc),
1520 const_binop (MULT_EXPR,
1521 i1, i2, notrunc),
1522 notrunc),
1523 const_binop (PLUS_EXPR,
1524 const_binop (MULT_EXPR,
1525 r1, i2, notrunc),
1526 const_binop (MULT_EXPR,
1527 i1, r2, notrunc),
1528 notrunc));
1529 break;
1531 case RDIV_EXPR:
1533 tree magsquared
1534 = const_binop (PLUS_EXPR,
1535 const_binop (MULT_EXPR, r2, r2, notrunc),
1536 const_binop (MULT_EXPR, i2, i2, notrunc),
1537 notrunc);
1539 t = build_complex (type,
1540 const_binop
1541 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1542 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1543 const_binop (PLUS_EXPR,
1544 const_binop (MULT_EXPR, r1, r2,
1545 notrunc),
1546 const_binop (MULT_EXPR, i1, i2,
1547 notrunc),
1548 notrunc),
1549 magsquared, notrunc),
1550 const_binop
1551 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1552 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1553 const_binop (MINUS_EXPR,
1554 const_binop (MULT_EXPR, i1, r2,
1555 notrunc),
1556 const_binop (MULT_EXPR, r1, i2,
1557 notrunc),
1558 notrunc),
1559 magsquared, notrunc));
1561 break;
1563 default:
1564 abort ();
1566 return t;
1568 return 0;
1571 /* These are the hash table functions for the hash table of INTEGER_CST
1572 nodes of a sizetype. */
1574 /* Return the hash code code X, an INTEGER_CST. */
1576 static hashval_t
1577 size_htab_hash (const void *x)
1579 tree t = (tree) x;
1581 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1582 ^ htab_hash_pointer (TREE_TYPE (t))
1583 ^ (TREE_OVERFLOW (t) << 20));
1586 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1587 is the same as that given by *Y, which is the same. */
1589 static int
1590 size_htab_eq (const void *x, const void *y)
1592 tree xt = (tree) x;
1593 tree yt = (tree) y;
1595 return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1596 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1597 && TREE_TYPE (xt) == TREE_TYPE (yt)
1598 && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1601 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1602 bits are given by NUMBER and of the sizetype represented by KIND. */
1604 tree
1605 size_int_wide (HOST_WIDE_INT number, enum size_type_kind kind)
1607 return size_int_type_wide (number, sizetype_tab[(int) kind]);
1610 /* Likewise, but the desired type is specified explicitly. */
1612 static GTY (()) tree new_const;
1613 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
1614 htab_t size_htab;
1616 tree
1617 size_int_type_wide (HOST_WIDE_INT number, tree type)
1619 void **slot;
1621 if (size_htab == 0)
1623 size_htab = htab_create_ggc (1024, size_htab_hash, size_htab_eq, NULL);
1624 new_const = make_node (INTEGER_CST);
1627 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1628 hash table, we return the value from the hash table. Otherwise, we
1629 place that in the hash table and make a new node for the next time. */
1630 TREE_INT_CST_LOW (new_const) = number;
1631 TREE_INT_CST_HIGH (new_const) = number < 0 ? -1 : 0;
1632 TREE_TYPE (new_const) = type;
1633 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const)
1634 = force_fit_type (new_const, 0);
1636 slot = htab_find_slot (size_htab, new_const, INSERT);
1637 if (*slot == 0)
1639 tree t = new_const;
1641 *slot = new_const;
1642 new_const = make_node (INTEGER_CST);
1643 return t;
1645 else
1646 return (tree) *slot;
1649 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1650 is a tree code. The type of the result is taken from the operands.
1651 Both must be the same type integer type and it must be a size type.
1652 If the operands are constant, so is the result. */
1654 tree
1655 size_binop (enum tree_code code, tree arg0, tree arg1)
1657 tree type = TREE_TYPE (arg0);
1659 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1660 || type != TREE_TYPE (arg1))
1661 abort ();
1663 /* Handle the special case of two integer constants faster. */
1664 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1666 /* And some specific cases even faster than that. */
1667 if (code == PLUS_EXPR && integer_zerop (arg0))
1668 return arg1;
1669 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1670 && integer_zerop (arg1))
1671 return arg0;
1672 else if (code == MULT_EXPR && integer_onep (arg0))
1673 return arg1;
1675 /* Handle general case of two integer constants. */
1676 return int_const_binop (code, arg0, arg1, 0);
1679 if (arg0 == error_mark_node || arg1 == error_mark_node)
1680 return error_mark_node;
1682 return fold (build2 (code, type, arg0, arg1));
1685 /* Given two values, either both of sizetype or both of bitsizetype,
1686 compute the difference between the two values. Return the value
1687 in signed type corresponding to the type of the operands. */
1689 tree
1690 size_diffop (tree arg0, tree arg1)
1692 tree type = TREE_TYPE (arg0);
1693 tree ctype;
1695 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1696 || type != TREE_TYPE (arg1))
1697 abort ();
1699 /* If the type is already signed, just do the simple thing. */
1700 if (!TYPE_UNSIGNED (type))
1701 return size_binop (MINUS_EXPR, arg0, arg1);
1703 ctype = (type == bitsizetype || type == ubitsizetype
1704 ? sbitsizetype : ssizetype);
1706 /* If either operand is not a constant, do the conversions to the signed
1707 type and subtract. The hardware will do the right thing with any
1708 overflow in the subtraction. */
1709 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1710 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1711 fold_convert (ctype, arg1));
1713 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1714 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1715 overflow) and negate (which can't either). Special-case a result
1716 of zero while we're here. */
1717 if (tree_int_cst_equal (arg0, arg1))
1718 return fold_convert (ctype, integer_zero_node);
1719 else if (tree_int_cst_lt (arg1, arg0))
1720 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1721 else
1722 return size_binop (MINUS_EXPR, fold_convert (ctype, integer_zero_node),
1723 fold_convert (ctype, size_binop (MINUS_EXPR,
1724 arg1, arg0)));
1728 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1729 type TYPE. If no simplification can be done return NULL_TREE. */
1731 static tree
1732 fold_convert_const (enum tree_code code, tree type, tree arg1)
1734 int overflow = 0;
1735 tree t;
1737 if (TREE_TYPE (arg1) == type)
1738 return arg1;
1740 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1742 if (TREE_CODE (arg1) == INTEGER_CST)
1744 /* If we would build a constant wider than GCC supports,
1745 leave the conversion unfolded. */
1746 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1747 return NULL_TREE;
1749 /* If we are trying to make a sizetype for a small integer, use
1750 size_int to pick up cached types to reduce duplicate nodes. */
1751 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1752 && !TREE_CONSTANT_OVERFLOW (arg1)
1753 && compare_tree_int (arg1, 10000) < 0)
1754 return size_int_type_wide (TREE_INT_CST_LOW (arg1), type);
1756 /* Given an integer constant, make new constant with new type,
1757 appropriately sign-extended or truncated. */
1758 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1759 TREE_INT_CST_HIGH (arg1));
1760 TREE_TYPE (t) = type;
1761 /* Indicate an overflow if (1) ARG1 already overflowed,
1762 or (2) force_fit_type indicates an overflow.
1763 Tell force_fit_type that an overflow has already occurred
1764 if ARG1 is a too-large unsigned value and T is signed.
1765 But don't indicate an overflow if converting a pointer. */
1766 TREE_OVERFLOW (t)
1767 = ((force_fit_type (t,
1768 (TREE_INT_CST_HIGH (arg1) < 0
1769 && (TYPE_UNSIGNED (type)
1770 < TYPE_UNSIGNED (TREE_TYPE (arg1)))))
1771 && ! POINTER_TYPE_P (TREE_TYPE (arg1)))
1772 || TREE_OVERFLOW (arg1));
1773 TREE_CONSTANT_OVERFLOW (t)
1774 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1775 return t;
1777 else if (TREE_CODE (arg1) == REAL_CST)
1779 /* The following code implements the floating point to integer
1780 conversion rules required by the Java Language Specification,
1781 that IEEE NaNs are mapped to zero and values that overflow
1782 the target precision saturate, i.e. values greater than
1783 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1784 are mapped to INT_MIN. These semantics are allowed by the
1785 C and C++ standards that simply state that the behavior of
1786 FP-to-integer conversion is unspecified upon overflow. */
1788 HOST_WIDE_INT high, low;
1790 REAL_VALUE_TYPE r;
1791 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1793 switch (code)
1795 case FIX_TRUNC_EXPR:
1796 real_trunc (&r, VOIDmode, &x);
1797 break;
1799 case FIX_CEIL_EXPR:
1800 real_ceil (&r, VOIDmode, &x);
1801 break;
1803 case FIX_FLOOR_EXPR:
1804 real_floor (&r, VOIDmode, &x);
1805 break;
1807 case FIX_ROUND_EXPR:
1808 real_round (&r, VOIDmode, &x);
1809 break;
1811 default:
1812 abort ();
1815 /* If R is NaN, return zero and show we have an overflow. */
1816 if (REAL_VALUE_ISNAN (r))
1818 overflow = 1;
1819 high = 0;
1820 low = 0;
1823 /* See if R is less than the lower bound or greater than the
1824 upper bound. */
1826 if (! overflow)
1828 tree lt = TYPE_MIN_VALUE (type);
1829 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1830 if (REAL_VALUES_LESS (r, l))
1832 overflow = 1;
1833 high = TREE_INT_CST_HIGH (lt);
1834 low = TREE_INT_CST_LOW (lt);
1838 if (! overflow)
1840 tree ut = TYPE_MAX_VALUE (type);
1841 if (ut)
1843 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1844 if (REAL_VALUES_LESS (u, r))
1846 overflow = 1;
1847 high = TREE_INT_CST_HIGH (ut);
1848 low = TREE_INT_CST_LOW (ut);
1853 if (! overflow)
1854 REAL_VALUE_TO_INT (&low, &high, r);
1856 t = build_int_2 (low, high);
1857 TREE_TYPE (t) = type;
1858 TREE_OVERFLOW (t)
1859 = TREE_OVERFLOW (arg1) | force_fit_type (t, overflow);
1860 TREE_CONSTANT_OVERFLOW (t)
1861 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1862 return t;
1865 else if (TREE_CODE (type) == REAL_TYPE)
1867 if (TREE_CODE (arg1) == INTEGER_CST)
1868 return build_real_from_int_cst (type, arg1);
1869 if (TREE_CODE (arg1) == REAL_CST)
1871 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1873 /* We make a copy of ARG1 so that we don't modify an
1874 existing constant tree. */
1875 t = copy_node (arg1);
1876 TREE_TYPE (t) = type;
1877 return t;
1880 t = build_real (type,
1881 real_value_truncate (TYPE_MODE (type),
1882 TREE_REAL_CST (arg1)));
1884 TREE_OVERFLOW (t)
1885 = TREE_OVERFLOW (arg1) | force_fit_type (t, 0);
1886 TREE_CONSTANT_OVERFLOW (t)
1887 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1888 return t;
1891 return NULL_TREE;
1894 /* Convert expression ARG to type TYPE. Used by the middle-end for
1895 simple conversions in preference to calling the front-end's convert. */
1897 tree
1898 fold_convert (tree type, tree arg)
1900 tree orig = TREE_TYPE (arg);
1901 tree tem;
1903 if (type == orig)
1904 return arg;
1906 if (TREE_CODE (arg) == ERROR_MARK
1907 || TREE_CODE (type) == ERROR_MARK
1908 || TREE_CODE (orig) == ERROR_MARK)
1909 return error_mark_node;
1911 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
1912 return fold (build1 (NOP_EXPR, type, arg));
1914 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type))
1916 if (TREE_CODE (arg) == INTEGER_CST)
1918 tem = fold_convert_const (NOP_EXPR, type, arg);
1919 if (tem != NULL_TREE)
1920 return tem;
1922 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1923 return fold (build1 (NOP_EXPR, type, arg));
1924 if (TREE_CODE (orig) == COMPLEX_TYPE)
1926 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1927 return fold_convert (type, tem);
1929 if (TREE_CODE (orig) == VECTOR_TYPE
1930 && GET_MODE_SIZE (TYPE_MODE (type))
1931 == GET_MODE_SIZE (TYPE_MODE (orig)))
1932 return fold (build1 (NOP_EXPR, type, arg));
1934 else if (TREE_CODE (type) == REAL_TYPE)
1936 if (TREE_CODE (arg) == INTEGER_CST)
1938 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1939 if (tem != NULL_TREE)
1940 return tem;
1942 else if (TREE_CODE (arg) == REAL_CST)
1944 tem = fold_convert_const (NOP_EXPR, type, arg);
1945 if (tem != NULL_TREE)
1946 return tem;
1949 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1950 return fold (build1 (FLOAT_EXPR, type, arg));
1951 if (TREE_CODE (orig) == REAL_TYPE)
1952 return fold (build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
1953 type, arg));
1954 if (TREE_CODE (orig) == COMPLEX_TYPE)
1956 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1957 return fold_convert (type, tem);
1960 else if (TREE_CODE (type) == COMPLEX_TYPE)
1962 if (INTEGRAL_TYPE_P (orig)
1963 || POINTER_TYPE_P (orig)
1964 || TREE_CODE (orig) == REAL_TYPE)
1965 return build2 (COMPLEX_EXPR, type,
1966 fold_convert (TREE_TYPE (type), arg),
1967 fold_convert (TREE_TYPE (type), integer_zero_node));
1968 if (TREE_CODE (orig) == COMPLEX_TYPE)
1970 tree rpart, ipart;
1972 if (TREE_CODE (arg) == COMPLEX_EXPR)
1974 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
1975 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
1976 return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
1979 arg = save_expr (arg);
1980 rpart = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1981 ipart = fold (build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg));
1982 rpart = fold_convert (TREE_TYPE (type), rpart);
1983 ipart = fold_convert (TREE_TYPE (type), ipart);
1984 return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
1987 else if (TREE_CODE (type) == VECTOR_TYPE)
1989 if ((INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1990 && GET_MODE_SIZE (TYPE_MODE (type))
1991 == GET_MODE_SIZE (TYPE_MODE (orig)))
1992 return fold (build1 (NOP_EXPR, type, arg));
1993 if (TREE_CODE (orig) == VECTOR_TYPE
1994 && GET_MODE_SIZE (TYPE_MODE (type))
1995 == GET_MODE_SIZE (TYPE_MODE (orig)))
1996 return fold (build1 (NOP_EXPR, type, arg));
1998 else if (VOID_TYPE_P (type))
1999 return fold (build1 (CONVERT_EXPR, type, arg));
2000 abort ();
2003 /* Return an expr equal to X but certainly not valid as an lvalue. */
2005 tree
2006 non_lvalue (tree x)
2008 /* We only need to wrap lvalue tree codes. */
2009 switch (TREE_CODE (x))
2011 case VAR_DECL:
2012 case PARM_DECL:
2013 case RESULT_DECL:
2014 case LABEL_DECL:
2015 case FUNCTION_DECL:
2016 case SSA_NAME:
2018 case COMPONENT_REF:
2019 case INDIRECT_REF:
2020 case ARRAY_REF:
2021 case BIT_FIELD_REF:
2022 case BUFFER_REF:
2023 case ARRAY_RANGE_REF:
2024 case VTABLE_REF:
2026 case REALPART_EXPR:
2027 case IMAGPART_EXPR:
2028 case PREINCREMENT_EXPR:
2029 case PREDECREMENT_EXPR:
2030 case SAVE_EXPR:
2031 case UNSAVE_EXPR:
2032 case TRY_CATCH_EXPR:
2033 case WITH_CLEANUP_EXPR:
2034 case COMPOUND_EXPR:
2035 case MODIFY_EXPR:
2036 case TARGET_EXPR:
2037 case COND_EXPR:
2038 case BIND_EXPR:
2039 case MIN_EXPR:
2040 case MAX_EXPR:
2041 case RTL_EXPR:
2042 break;
2044 default:
2045 /* Assume the worst for front-end tree codes. */
2046 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2047 break;
2048 return x;
2050 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2053 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2054 Zero means allow extended lvalues. */
2056 int pedantic_lvalues;
2058 /* When pedantic, return an expr equal to X but certainly not valid as a
2059 pedantic lvalue. Otherwise, return X. */
2061 tree
2062 pedantic_non_lvalue (tree x)
2064 if (pedantic_lvalues)
2065 return non_lvalue (x);
2066 else
2067 return x;
2070 /* Given a tree comparison code, return the code that is the logical inverse
2071 of the given code. It is not safe to do this for floating-point
2072 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2073 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2075 static enum tree_code
2076 invert_tree_comparison (enum tree_code code, bool honor_nans)
2078 if (honor_nans && flag_trapping_math)
2079 return ERROR_MARK;
2081 switch (code)
2083 case EQ_EXPR:
2084 return NE_EXPR;
2085 case NE_EXPR:
2086 return EQ_EXPR;
2087 case GT_EXPR:
2088 return honor_nans ? UNLE_EXPR : LE_EXPR;
2089 case GE_EXPR:
2090 return honor_nans ? UNLT_EXPR : LT_EXPR;
2091 case LT_EXPR:
2092 return honor_nans ? UNGE_EXPR : GE_EXPR;
2093 case LE_EXPR:
2094 return honor_nans ? UNGT_EXPR : GT_EXPR;
2095 case LTGT_EXPR:
2096 return UNEQ_EXPR;
2097 case UNEQ_EXPR:
2098 return LTGT_EXPR;
2099 case UNGT_EXPR:
2100 return LE_EXPR;
2101 case UNGE_EXPR:
2102 return LT_EXPR;
2103 case UNLT_EXPR:
2104 return GE_EXPR;
2105 case UNLE_EXPR:
2106 return GT_EXPR;
2107 case ORDERED_EXPR:
2108 return UNORDERED_EXPR;
2109 case UNORDERED_EXPR:
2110 return ORDERED_EXPR;
2111 default:
2112 abort ();
2116 /* Similar, but return the comparison that results if the operands are
2117 swapped. This is safe for floating-point. */
2119 static enum tree_code
2120 swap_tree_comparison (enum tree_code code)
2122 switch (code)
2124 case EQ_EXPR:
2125 case NE_EXPR:
2126 return code;
2127 case GT_EXPR:
2128 return LT_EXPR;
2129 case GE_EXPR:
2130 return LE_EXPR;
2131 case LT_EXPR:
2132 return GT_EXPR;
2133 case LE_EXPR:
2134 return GE_EXPR;
2135 default:
2136 abort ();
2141 /* Convert a comparison tree code from an enum tree_code representation
2142 into a compcode bit-based encoding. This function is the inverse of
2143 compcode_to_comparison. */
2145 static enum comparison_code
2146 comparison_to_compcode (enum tree_code code)
2148 switch (code)
2150 case LT_EXPR:
2151 return COMPCODE_LT;
2152 case EQ_EXPR:
2153 return COMPCODE_EQ;
2154 case LE_EXPR:
2155 return COMPCODE_LE;
2156 case GT_EXPR:
2157 return COMPCODE_GT;
2158 case NE_EXPR:
2159 return COMPCODE_NE;
2160 case GE_EXPR:
2161 return COMPCODE_GE;
2162 case ORDERED_EXPR:
2163 return COMPCODE_ORD;
2164 case UNORDERED_EXPR:
2165 return COMPCODE_UNORD;
2166 case UNLT_EXPR:
2167 return COMPCODE_UNLT;
2168 case UNEQ_EXPR:
2169 return COMPCODE_UNEQ;
2170 case UNLE_EXPR:
2171 return COMPCODE_UNLE;
2172 case UNGT_EXPR:
2173 return COMPCODE_UNGT;
2174 case LTGT_EXPR:
2175 return COMPCODE_LTGT;
2176 case UNGE_EXPR:
2177 return COMPCODE_UNGE;
2178 default:
2179 abort ();
2183 /* Convert a compcode bit-based encoding of a comparison operator back
2184 to GCC's enum tree_code representation. This function is the
2185 inverse of comparison_to_compcode. */
2187 static enum tree_code
2188 compcode_to_comparison (enum comparison_code code)
2190 switch (code)
2192 case COMPCODE_LT:
2193 return LT_EXPR;
2194 case COMPCODE_EQ:
2195 return EQ_EXPR;
2196 case COMPCODE_LE:
2197 return LE_EXPR;
2198 case COMPCODE_GT:
2199 return GT_EXPR;
2200 case COMPCODE_NE:
2201 return NE_EXPR;
2202 case COMPCODE_GE:
2203 return GE_EXPR;
2204 case COMPCODE_ORD:
2205 return ORDERED_EXPR;
2206 case COMPCODE_UNORD:
2207 return UNORDERED_EXPR;
2208 case COMPCODE_UNLT:
2209 return UNLT_EXPR;
2210 case COMPCODE_UNEQ:
2211 return UNEQ_EXPR;
2212 case COMPCODE_UNLE:
2213 return UNLE_EXPR;
2214 case COMPCODE_UNGT:
2215 return UNGT_EXPR;
2216 case COMPCODE_LTGT:
2217 return LTGT_EXPR;
2218 case COMPCODE_UNGE:
2219 return UNGE_EXPR;
2220 default:
2221 abort ();
2225 /* Return a tree for the comparison which is the combination of
2226 doing the AND or OR (depending on CODE) of the two operations LCODE
2227 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2228 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2229 if this makes the transformation invalid. */
2231 tree
2232 combine_comparisons (enum tree_code code, enum tree_code lcode,
2233 enum tree_code rcode, tree truth_type,
2234 tree ll_arg, tree lr_arg)
2236 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2237 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2238 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2239 enum comparison_code compcode;
2241 switch (code)
2243 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2244 compcode = lcompcode & rcompcode;
2245 break;
2247 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2248 compcode = lcompcode | rcompcode;
2249 break;
2251 default:
2252 return NULL_TREE;
2255 if (!honor_nans)
2257 /* Eliminate unordered comparisons, as well as LTGT and ORD
2258 which are not used unless the mode has NaNs. */
2259 compcode &= ~COMPCODE_UNORD;
2260 if (compcode == COMPCODE_LTGT)
2261 compcode = COMPCODE_NE;
2262 else if (compcode == COMPCODE_ORD)
2263 compcode = COMPCODE_TRUE;
2265 else if (flag_trapping_math)
2267 /* Check that the original operation and the optimized ones will trap
2268 under the same condition. */
2269 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2270 && (lcompcode != COMPCODE_EQ)
2271 && (lcompcode != COMPCODE_ORD);
2272 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2273 && (rcompcode != COMPCODE_EQ)
2274 && (rcompcode != COMPCODE_ORD);
2275 bool trap = (compcode & COMPCODE_UNORD) == 0
2276 && (compcode != COMPCODE_EQ)
2277 && (compcode != COMPCODE_ORD);
2279 /* In a short-circuited boolean expression the LHS might be
2280 such that the RHS, if evaluated, will never trap. For
2281 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2282 if neither x nor y is NaN. (This is a mixed blessing: for
2283 example, the expression above will never trap, hence
2284 optimizing it to x < y would be invalid). */
2285 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2286 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2287 rtrap = false;
2289 /* If the comparison was short-circuited, and only the RHS
2290 trapped, we may now generate a spurious trap. */
2291 if (rtrap && !ltrap
2292 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2293 return NULL_TREE;
2295 /* If we changed the conditions that cause a trap, we lose. */
2296 if ((ltrap || rtrap) != trap)
2297 return NULL_TREE;
2300 if (compcode == COMPCODE_TRUE)
2301 return constant_boolean_node (true, truth_type);
2302 else if (compcode == COMPCODE_FALSE)
2303 return constant_boolean_node (false, truth_type);
2304 else
2305 return fold (build2 (compcode_to_comparison (compcode),
2306 truth_type, ll_arg, lr_arg));
2309 /* Return nonzero if CODE is a tree code that represents a truth value. */
2311 static int
2312 truth_value_p (enum tree_code code)
2314 return (TREE_CODE_CLASS (code) == '<'
2315 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2316 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2317 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2320 /* Return nonzero if two operands (typically of the same tree node)
2321 are necessarily equal. If either argument has side-effects this
2322 function returns zero. FLAGS modifies behavior as follows:
2324 If OEP_ONLY_CONST is set, only return nonzero for constants.
2325 This function tests whether the operands are indistinguishable;
2326 it does not test whether they are equal using C's == operation.
2327 The distinction is important for IEEE floating point, because
2328 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2329 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2331 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2332 even though it may hold multiple values during a function.
2333 This is because a GCC tree node guarantees that nothing else is
2334 executed between the evaluation of its "operands" (which may often
2335 be evaluated in arbitrary order). Hence if the operands themselves
2336 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2337 same value in each operand/subexpression. Hence a zero value for
2338 ONLY_CONST assumes isochronic (or instantaneous) tree equivalence.
2339 If comparing arbitrary expression trees, such as from different
2340 statements, ONLY_CONST must usually be nonzero.
2342 If OEP_PURE_SAME is set, then pure functions with identical arguments
2343 are considered the same. It is used when the caller has other ways
2344 to ensure that global memory is unchanged in between. */
2347 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2349 /* If either is ERROR_MARK, they aren't equal. */
2350 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2351 return 0;
2353 /* If both types don't have the same signedness, then we can't consider
2354 them equal. We must check this before the STRIP_NOPS calls
2355 because they may change the signedness of the arguments. */
2356 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2357 return 0;
2359 STRIP_NOPS (arg0);
2360 STRIP_NOPS (arg1);
2362 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2363 /* This is needed for conversions and for COMPONENT_REF.
2364 Might as well play it safe and always test this. */
2365 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2366 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2367 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2368 return 0;
2370 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2371 We don't care about side effects in that case because the SAVE_EXPR
2372 takes care of that for us. In all other cases, two expressions are
2373 equal if they have no side effects. If we have two identical
2374 expressions with side effects that should be treated the same due
2375 to the only side effects being identical SAVE_EXPR's, that will
2376 be detected in the recursive calls below. */
2377 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2378 && (TREE_CODE (arg0) == SAVE_EXPR
2379 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2380 return 1;
2382 /* Next handle constant cases, those for which we can return 1 even
2383 if ONLY_CONST is set. */
2384 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2385 switch (TREE_CODE (arg0))
2387 case INTEGER_CST:
2388 return (! TREE_CONSTANT_OVERFLOW (arg0)
2389 && ! TREE_CONSTANT_OVERFLOW (arg1)
2390 && tree_int_cst_equal (arg0, arg1));
2392 case REAL_CST:
2393 return (! TREE_CONSTANT_OVERFLOW (arg0)
2394 && ! TREE_CONSTANT_OVERFLOW (arg1)
2395 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2396 TREE_REAL_CST (arg1)));
2398 case VECTOR_CST:
2400 tree v1, v2;
2402 if (TREE_CONSTANT_OVERFLOW (arg0)
2403 || TREE_CONSTANT_OVERFLOW (arg1))
2404 return 0;
2406 v1 = TREE_VECTOR_CST_ELTS (arg0);
2407 v2 = TREE_VECTOR_CST_ELTS (arg1);
2408 while (v1 && v2)
2410 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2411 flags))
2412 return 0;
2413 v1 = TREE_CHAIN (v1);
2414 v2 = TREE_CHAIN (v2);
2417 return 1;
2420 case COMPLEX_CST:
2421 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2422 flags)
2423 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2424 flags));
2426 case STRING_CST:
2427 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2428 && ! memcmp (TREE_STRING_POINTER (arg0),
2429 TREE_STRING_POINTER (arg1),
2430 TREE_STRING_LENGTH (arg0)));
2432 case ADDR_EXPR:
2433 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2435 default:
2436 break;
2439 if (flags & OEP_ONLY_CONST)
2440 return 0;
2442 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2444 case '1':
2445 /* Two conversions are equal only if signedness and modes match. */
2446 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2447 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
2448 != TYPE_UNSIGNED (TREE_TYPE (arg1))))
2449 return 0;
2451 return operand_equal_p (TREE_OPERAND (arg0, 0),
2452 TREE_OPERAND (arg1, 0), flags);
2454 case '<':
2455 case '2':
2456 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0)
2457 && operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1),
2459 return 1;
2461 /* For commutative ops, allow the other order. */
2462 return (commutative_tree_code (TREE_CODE (arg0))
2463 && operand_equal_p (TREE_OPERAND (arg0, 0),
2464 TREE_OPERAND (arg1, 1), flags)
2465 && operand_equal_p (TREE_OPERAND (arg0, 1),
2466 TREE_OPERAND (arg1, 0), flags));
2468 case 'r':
2469 /* If either of the pointer (or reference) expressions we are
2470 dereferencing contain a side effect, these cannot be equal. */
2471 if (TREE_SIDE_EFFECTS (arg0)
2472 || TREE_SIDE_EFFECTS (arg1))
2473 return 0;
2475 switch (TREE_CODE (arg0))
2477 case INDIRECT_REF:
2478 return operand_equal_p (TREE_OPERAND (arg0, 0),
2479 TREE_OPERAND (arg1, 0), flags);
2481 case COMPONENT_REF:
2482 case ARRAY_REF:
2483 case ARRAY_RANGE_REF:
2484 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2485 TREE_OPERAND (arg1, 0), flags)
2486 && operand_equal_p (TREE_OPERAND (arg0, 1),
2487 TREE_OPERAND (arg1, 1), flags));
2489 case BIT_FIELD_REF:
2490 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2491 TREE_OPERAND (arg1, 0), flags)
2492 && operand_equal_p (TREE_OPERAND (arg0, 1),
2493 TREE_OPERAND (arg1, 1), flags)
2494 && operand_equal_p (TREE_OPERAND (arg0, 2),
2495 TREE_OPERAND (arg1, 2), flags));
2496 default:
2497 return 0;
2500 case 'e':
2501 switch (TREE_CODE (arg0))
2503 case ADDR_EXPR:
2504 case TRUTH_NOT_EXPR:
2505 return operand_equal_p (TREE_OPERAND (arg0, 0),
2506 TREE_OPERAND (arg1, 0), flags);
2508 case RTL_EXPR:
2509 return rtx_equal_p (RTL_EXPR_RTL (arg0), RTL_EXPR_RTL (arg1));
2511 case CALL_EXPR:
2512 /* If the CALL_EXPRs call different functions, then they
2513 clearly can not be equal. */
2514 if (! operand_equal_p (TREE_OPERAND (arg0, 0),
2515 TREE_OPERAND (arg1, 0), flags))
2516 return 0;
2519 unsigned int cef = call_expr_flags (arg0);
2520 if (flags & OEP_PURE_SAME)
2521 cef &= ECF_CONST | ECF_PURE;
2522 else
2523 cef &= ECF_CONST;
2524 if (!cef)
2525 return 0;
2528 /* Now see if all the arguments are the same. operand_equal_p
2529 does not handle TREE_LIST, so we walk the operands here
2530 feeding them to operand_equal_p. */
2531 arg0 = TREE_OPERAND (arg0, 1);
2532 arg1 = TREE_OPERAND (arg1, 1);
2533 while (arg0 && arg1)
2535 if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2536 flags))
2537 return 0;
2539 arg0 = TREE_CHAIN (arg0);
2540 arg1 = TREE_CHAIN (arg1);
2543 /* If we get here and both argument lists are exhausted
2544 then the CALL_EXPRs are equal. */
2545 return ! (arg0 || arg1);
2547 default:
2548 return 0;
2551 case 'd':
2552 /* Consider __builtin_sqrt equal to sqrt. */
2553 return (TREE_CODE (arg0) == FUNCTION_DECL
2554 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2555 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2556 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2558 default:
2559 return 0;
2563 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2564 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2566 When in doubt, return 0. */
2568 static int
2569 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2571 int unsignedp1, unsignedpo;
2572 tree primarg0, primarg1, primother;
2573 unsigned int correct_width;
2575 if (operand_equal_p (arg0, arg1, 0))
2576 return 1;
2578 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2579 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2580 return 0;
2582 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2583 and see if the inner values are the same. This removes any
2584 signedness comparison, which doesn't matter here. */
2585 primarg0 = arg0, primarg1 = arg1;
2586 STRIP_NOPS (primarg0);
2587 STRIP_NOPS (primarg1);
2588 if (operand_equal_p (primarg0, primarg1, 0))
2589 return 1;
2591 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2592 actual comparison operand, ARG0.
2594 First throw away any conversions to wider types
2595 already present in the operands. */
2597 primarg1 = get_narrower (arg1, &unsignedp1);
2598 primother = get_narrower (other, &unsignedpo);
2600 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2601 if (unsignedp1 == unsignedpo
2602 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2603 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2605 tree type = TREE_TYPE (arg0);
2607 /* Make sure shorter operand is extended the right way
2608 to match the longer operand. */
2609 primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2610 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2612 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2613 return 1;
2616 return 0;
2619 /* See if ARG is an expression that is either a comparison or is performing
2620 arithmetic on comparisons. The comparisons must only be comparing
2621 two different values, which will be stored in *CVAL1 and *CVAL2; if
2622 they are nonzero it means that some operands have already been found.
2623 No variables may be used anywhere else in the expression except in the
2624 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2625 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2627 If this is true, return 1. Otherwise, return zero. */
2629 static int
2630 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2632 enum tree_code code = TREE_CODE (arg);
2633 char class = TREE_CODE_CLASS (code);
2635 /* We can handle some of the 'e' cases here. */
2636 if (class == 'e' && code == TRUTH_NOT_EXPR)
2637 class = '1';
2638 else if (class == 'e'
2639 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2640 || code == COMPOUND_EXPR))
2641 class = '2';
2643 else if (class == 'e' && code == SAVE_EXPR && SAVE_EXPR_RTL (arg) == 0
2644 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2646 /* If we've already found a CVAL1 or CVAL2, this expression is
2647 two complex to handle. */
2648 if (*cval1 || *cval2)
2649 return 0;
2651 class = '1';
2652 *save_p = 1;
2655 switch (class)
2657 case '1':
2658 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2660 case '2':
2661 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2662 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2663 cval1, cval2, save_p));
2665 case 'c':
2666 return 1;
2668 case 'e':
2669 if (code == COND_EXPR)
2670 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2671 cval1, cval2, save_p)
2672 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2673 cval1, cval2, save_p)
2674 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2675 cval1, cval2, save_p));
2676 return 0;
2678 case '<':
2679 /* First see if we can handle the first operand, then the second. For
2680 the second operand, we know *CVAL1 can't be zero. It must be that
2681 one side of the comparison is each of the values; test for the
2682 case where this isn't true by failing if the two operands
2683 are the same. */
2685 if (operand_equal_p (TREE_OPERAND (arg, 0),
2686 TREE_OPERAND (arg, 1), 0))
2687 return 0;
2689 if (*cval1 == 0)
2690 *cval1 = TREE_OPERAND (arg, 0);
2691 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2693 else if (*cval2 == 0)
2694 *cval2 = TREE_OPERAND (arg, 0);
2695 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2697 else
2698 return 0;
2700 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2702 else if (*cval2 == 0)
2703 *cval2 = TREE_OPERAND (arg, 1);
2704 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2706 else
2707 return 0;
2709 return 1;
2711 default:
2712 return 0;
2716 /* ARG is a tree that is known to contain just arithmetic operations and
2717 comparisons. Evaluate the operations in the tree substituting NEW0 for
2718 any occurrence of OLD0 as an operand of a comparison and likewise for
2719 NEW1 and OLD1. */
2721 static tree
2722 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2724 tree type = TREE_TYPE (arg);
2725 enum tree_code code = TREE_CODE (arg);
2726 char class = TREE_CODE_CLASS (code);
2728 /* We can handle some of the 'e' cases here. */
2729 if (class == 'e' && code == TRUTH_NOT_EXPR)
2730 class = '1';
2731 else if (class == 'e'
2732 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2733 class = '2';
2735 switch (class)
2737 case '1':
2738 return fold (build1 (code, type,
2739 eval_subst (TREE_OPERAND (arg, 0),
2740 old0, new0, old1, new1)));
2742 case '2':
2743 return fold (build2 (code, type,
2744 eval_subst (TREE_OPERAND (arg, 0),
2745 old0, new0, old1, new1),
2746 eval_subst (TREE_OPERAND (arg, 1),
2747 old0, new0, old1, new1)));
2749 case 'e':
2750 switch (code)
2752 case SAVE_EXPR:
2753 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2755 case COMPOUND_EXPR:
2756 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2758 case COND_EXPR:
2759 return fold (build3 (code, type,
2760 eval_subst (TREE_OPERAND (arg, 0),
2761 old0, new0, old1, new1),
2762 eval_subst (TREE_OPERAND (arg, 1),
2763 old0, new0, old1, new1),
2764 eval_subst (TREE_OPERAND (arg, 2),
2765 old0, new0, old1, new1)));
2766 default:
2767 break;
2769 /* Fall through - ??? */
2771 case '<':
2773 tree arg0 = TREE_OPERAND (arg, 0);
2774 tree arg1 = TREE_OPERAND (arg, 1);
2776 /* We need to check both for exact equality and tree equality. The
2777 former will be true if the operand has a side-effect. In that
2778 case, we know the operand occurred exactly once. */
2780 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2781 arg0 = new0;
2782 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2783 arg0 = new1;
2785 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2786 arg1 = new0;
2787 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2788 arg1 = new1;
2790 return fold (build2 (code, type, arg0, arg1));
2793 default:
2794 return arg;
2798 /* Return a tree for the case when the result of an expression is RESULT
2799 converted to TYPE and OMITTED was previously an operand of the expression
2800 but is now not needed (e.g., we folded OMITTED * 0).
2802 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2803 the conversion of RESULT to TYPE. */
2805 tree
2806 omit_one_operand (tree type, tree result, tree omitted)
2808 tree t = fold_convert (type, result);
2810 if (TREE_SIDE_EFFECTS (omitted))
2811 return build2 (COMPOUND_EXPR, type, omitted, t);
2813 return non_lvalue (t);
2816 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2818 static tree
2819 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2821 tree t = fold_convert (type, result);
2823 if (TREE_SIDE_EFFECTS (omitted))
2824 return build2 (COMPOUND_EXPR, type, omitted, t);
2826 return pedantic_non_lvalue (t);
2829 /* Return a simplified tree node for the truth-negation of ARG. This
2830 never alters ARG itself. We assume that ARG is an operation that
2831 returns a truth value (0 or 1).
2833 FIXME: one would think we would fold the result, but it causes
2834 problems with the dominator optimizer. */
2835 tree
2836 invert_truthvalue (tree arg)
2838 tree type = TREE_TYPE (arg);
2839 enum tree_code code = TREE_CODE (arg);
2841 if (code == ERROR_MARK)
2842 return arg;
2844 /* If this is a comparison, we can simply invert it, except for
2845 floating-point non-equality comparisons, in which case we just
2846 enclose a TRUTH_NOT_EXPR around what we have. */
2848 if (TREE_CODE_CLASS (code) == '<')
2850 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
2851 if (FLOAT_TYPE_P (op_type)
2852 && flag_trapping_math
2853 && code != ORDERED_EXPR && code != UNORDERED_EXPR
2854 && code != NE_EXPR && code != EQ_EXPR)
2855 return build1 (TRUTH_NOT_EXPR, type, arg);
2856 else
2858 code = invert_tree_comparison (code,
2859 HONOR_NANS (TYPE_MODE (op_type)));
2860 if (code == ERROR_MARK)
2861 return build1 (TRUTH_NOT_EXPR, type, arg);
2862 else
2863 return build2 (code, type,
2864 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2868 switch (code)
2870 case INTEGER_CST:
2871 return fold_convert (type, build_int_2 (integer_zerop (arg), 0));
2873 case TRUTH_AND_EXPR:
2874 return build2 (TRUTH_OR_EXPR, type,
2875 invert_truthvalue (TREE_OPERAND (arg, 0)),
2876 invert_truthvalue (TREE_OPERAND (arg, 1)));
2878 case TRUTH_OR_EXPR:
2879 return build2 (TRUTH_AND_EXPR, type,
2880 invert_truthvalue (TREE_OPERAND (arg, 0)),
2881 invert_truthvalue (TREE_OPERAND (arg, 1)));
2883 case TRUTH_XOR_EXPR:
2884 /* Here we can invert either operand. We invert the first operand
2885 unless the second operand is a TRUTH_NOT_EXPR in which case our
2886 result is the XOR of the first operand with the inside of the
2887 negation of the second operand. */
2889 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2890 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2891 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2892 else
2893 return build2 (TRUTH_XOR_EXPR, type,
2894 invert_truthvalue (TREE_OPERAND (arg, 0)),
2895 TREE_OPERAND (arg, 1));
2897 case TRUTH_ANDIF_EXPR:
2898 return build2 (TRUTH_ORIF_EXPR, type,
2899 invert_truthvalue (TREE_OPERAND (arg, 0)),
2900 invert_truthvalue (TREE_OPERAND (arg, 1)));
2902 case TRUTH_ORIF_EXPR:
2903 return build2 (TRUTH_ANDIF_EXPR, type,
2904 invert_truthvalue (TREE_OPERAND (arg, 0)),
2905 invert_truthvalue (TREE_OPERAND (arg, 1)));
2907 case TRUTH_NOT_EXPR:
2908 return TREE_OPERAND (arg, 0);
2910 case COND_EXPR:
2911 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
2912 invert_truthvalue (TREE_OPERAND (arg, 1)),
2913 invert_truthvalue (TREE_OPERAND (arg, 2)));
2915 case COMPOUND_EXPR:
2916 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2917 invert_truthvalue (TREE_OPERAND (arg, 1)));
2919 case NON_LVALUE_EXPR:
2920 return invert_truthvalue (TREE_OPERAND (arg, 0));
2922 case NOP_EXPR:
2923 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2924 break;
2926 case CONVERT_EXPR:
2927 case FLOAT_EXPR:
2928 return build1 (TREE_CODE (arg), type,
2929 invert_truthvalue (TREE_OPERAND (arg, 0)));
2931 case BIT_AND_EXPR:
2932 if (!integer_onep (TREE_OPERAND (arg, 1)))
2933 break;
2934 return build2 (EQ_EXPR, type, arg,
2935 fold_convert (type, integer_zero_node));
2937 case SAVE_EXPR:
2938 return build1 (TRUTH_NOT_EXPR, type, arg);
2940 case CLEANUP_POINT_EXPR:
2941 return build1 (CLEANUP_POINT_EXPR, type,
2942 invert_truthvalue (TREE_OPERAND (arg, 0)));
2944 default:
2945 break;
2947 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
2948 abort ();
2949 return build1 (TRUTH_NOT_EXPR, type, arg);
2952 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
2953 operands are another bit-wise operation with a common input. If so,
2954 distribute the bit operations to save an operation and possibly two if
2955 constants are involved. For example, convert
2956 (A | B) & (A | C) into A | (B & C)
2957 Further simplification will occur if B and C are constants.
2959 If this optimization cannot be done, 0 will be returned. */
2961 static tree
2962 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
2964 tree common;
2965 tree left, right;
2967 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2968 || TREE_CODE (arg0) == code
2969 || (TREE_CODE (arg0) != BIT_AND_EXPR
2970 && TREE_CODE (arg0) != BIT_IOR_EXPR))
2971 return 0;
2973 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
2975 common = TREE_OPERAND (arg0, 0);
2976 left = TREE_OPERAND (arg0, 1);
2977 right = TREE_OPERAND (arg1, 1);
2979 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
2981 common = TREE_OPERAND (arg0, 0);
2982 left = TREE_OPERAND (arg0, 1);
2983 right = TREE_OPERAND (arg1, 0);
2985 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
2987 common = TREE_OPERAND (arg0, 1);
2988 left = TREE_OPERAND (arg0, 0);
2989 right = TREE_OPERAND (arg1, 1);
2991 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
2993 common = TREE_OPERAND (arg0, 1);
2994 left = TREE_OPERAND (arg0, 0);
2995 right = TREE_OPERAND (arg1, 0);
2997 else
2998 return 0;
3000 return fold (build2 (TREE_CODE (arg0), type, common,
3001 fold (build2 (code, type, left, right))));
3004 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3005 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3007 static tree
3008 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3009 int unsignedp)
3011 tree result = build3 (BIT_FIELD_REF, type, inner,
3012 size_int (bitsize), bitsize_int (bitpos));
3014 BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3016 return result;
3019 /* Optimize a bit-field compare.
3021 There are two cases: First is a compare against a constant and the
3022 second is a comparison of two items where the fields are at the same
3023 bit position relative to the start of a chunk (byte, halfword, word)
3024 large enough to contain it. In these cases we can avoid the shift
3025 implicit in bitfield extractions.
3027 For constants, we emit a compare of the shifted constant with the
3028 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3029 compared. For two fields at the same position, we do the ANDs with the
3030 similar mask and compare the result of the ANDs.
3032 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3033 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3034 are the left and right operands of the comparison, respectively.
3036 If the optimization described above can be done, we return the resulting
3037 tree. Otherwise we return zero. */
3039 static tree
3040 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3041 tree lhs, tree rhs)
3043 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3044 tree type = TREE_TYPE (lhs);
3045 tree signed_type, unsigned_type;
3046 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3047 enum machine_mode lmode, rmode, nmode;
3048 int lunsignedp, runsignedp;
3049 int lvolatilep = 0, rvolatilep = 0;
3050 tree linner, rinner = NULL_TREE;
3051 tree mask;
3052 tree offset;
3054 /* Get all the information about the extractions being done. If the bit size
3055 if the same as the size of the underlying object, we aren't doing an
3056 extraction at all and so can do nothing. We also don't want to
3057 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3058 then will no longer be able to replace it. */
3059 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3060 &lunsignedp, &lvolatilep);
3061 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3062 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3063 return 0;
3065 if (!const_p)
3067 /* If this is not a constant, we can only do something if bit positions,
3068 sizes, and signedness are the same. */
3069 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3070 &runsignedp, &rvolatilep);
3072 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3073 || lunsignedp != runsignedp || offset != 0
3074 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3075 return 0;
3078 /* See if we can find a mode to refer to this field. We should be able to,
3079 but fail if we can't. */
3080 nmode = get_best_mode (lbitsize, lbitpos,
3081 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3082 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3083 TYPE_ALIGN (TREE_TYPE (rinner))),
3084 word_mode, lvolatilep || rvolatilep);
3085 if (nmode == VOIDmode)
3086 return 0;
3088 /* Set signed and unsigned types of the precision of this mode for the
3089 shifts below. */
3090 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3091 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3093 /* Compute the bit position and size for the new reference and our offset
3094 within it. If the new reference is the same size as the original, we
3095 won't optimize anything, so return zero. */
3096 nbitsize = GET_MODE_BITSIZE (nmode);
3097 nbitpos = lbitpos & ~ (nbitsize - 1);
3098 lbitpos -= nbitpos;
3099 if (nbitsize == lbitsize)
3100 return 0;
3102 if (BYTES_BIG_ENDIAN)
3103 lbitpos = nbitsize - lbitsize - lbitpos;
3105 /* Make the mask to be used against the extracted field. */
3106 mask = build_int_2 (~0, ~0);
3107 TREE_TYPE (mask) = unsigned_type;
3108 force_fit_type (mask, 0);
3109 mask = fold_convert (unsigned_type, mask);
3110 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3111 mask = const_binop (RSHIFT_EXPR, mask,
3112 size_int (nbitsize - lbitsize - lbitpos), 0);
3114 if (! const_p)
3115 /* If not comparing with constant, just rework the comparison
3116 and return. */
3117 return build2 (code, compare_type,
3118 build2 (BIT_AND_EXPR, unsigned_type,
3119 make_bit_field_ref (linner, unsigned_type,
3120 nbitsize, nbitpos, 1),
3121 mask),
3122 build2 (BIT_AND_EXPR, unsigned_type,
3123 make_bit_field_ref (rinner, unsigned_type,
3124 nbitsize, nbitpos, 1),
3125 mask));
3127 /* Otherwise, we are handling the constant case. See if the constant is too
3128 big for the field. Warn and return a tree of for 0 (false) if so. We do
3129 this not only for its own sake, but to avoid having to test for this
3130 error case below. If we didn't, we might generate wrong code.
3132 For unsigned fields, the constant shifted right by the field length should
3133 be all zero. For signed fields, the high-order bits should agree with
3134 the sign bit. */
3136 if (lunsignedp)
3138 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3139 fold_convert (unsigned_type, rhs),
3140 size_int (lbitsize), 0)))
3142 warning ("comparison is always %d due to width of bit-field",
3143 code == NE_EXPR);
3144 return constant_boolean_node (code == NE_EXPR, compare_type);
3147 else
3149 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3150 size_int (lbitsize - 1), 0);
3151 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3153 warning ("comparison is always %d due to width of bit-field",
3154 code == NE_EXPR);
3155 return constant_boolean_node (code == NE_EXPR, compare_type);
3159 /* Single-bit compares should always be against zero. */
3160 if (lbitsize == 1 && ! integer_zerop (rhs))
3162 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3163 rhs = fold_convert (type, integer_zero_node);
3166 /* Make a new bitfield reference, shift the constant over the
3167 appropriate number of bits and mask it with the computed mask
3168 (in case this was a signed field). If we changed it, make a new one. */
3169 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3170 if (lvolatilep)
3172 TREE_SIDE_EFFECTS (lhs) = 1;
3173 TREE_THIS_VOLATILE (lhs) = 1;
3176 rhs = fold (const_binop (BIT_AND_EXPR,
3177 const_binop (LSHIFT_EXPR,
3178 fold_convert (unsigned_type, rhs),
3179 size_int (lbitpos), 0),
3180 mask, 0));
3182 return build2 (code, compare_type,
3183 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3184 rhs);
3187 /* Subroutine for fold_truthop: decode a field reference.
3189 If EXP is a comparison reference, we return the innermost reference.
3191 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3192 set to the starting bit number.
3194 If the innermost field can be completely contained in a mode-sized
3195 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3197 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3198 otherwise it is not changed.
3200 *PUNSIGNEDP is set to the signedness of the field.
3202 *PMASK is set to the mask used. This is either contained in a
3203 BIT_AND_EXPR or derived from the width of the field.
3205 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3207 Return 0 if this is not a component reference or is one that we can't
3208 do anything with. */
3210 static tree
3211 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3212 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3213 int *punsignedp, int *pvolatilep,
3214 tree *pmask, tree *pand_mask)
3216 tree outer_type = 0;
3217 tree and_mask = 0;
3218 tree mask, inner, offset;
3219 tree unsigned_type;
3220 unsigned int precision;
3222 /* All the optimizations using this function assume integer fields.
3223 There are problems with FP fields since the type_for_size call
3224 below can fail for, e.g., XFmode. */
3225 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3226 return 0;
3228 /* We are interested in the bare arrangement of bits, so strip everything
3229 that doesn't affect the machine mode. However, record the type of the
3230 outermost expression if it may matter below. */
3231 if (TREE_CODE (exp) == NOP_EXPR
3232 || TREE_CODE (exp) == CONVERT_EXPR
3233 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3234 outer_type = TREE_TYPE (exp);
3235 STRIP_NOPS (exp);
3237 if (TREE_CODE (exp) == BIT_AND_EXPR)
3239 and_mask = TREE_OPERAND (exp, 1);
3240 exp = TREE_OPERAND (exp, 0);
3241 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3242 if (TREE_CODE (and_mask) != INTEGER_CST)
3243 return 0;
3246 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3247 punsignedp, pvolatilep);
3248 if ((inner == exp && and_mask == 0)
3249 || *pbitsize < 0 || offset != 0
3250 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3251 return 0;
3253 /* If the number of bits in the reference is the same as the bitsize of
3254 the outer type, then the outer type gives the signedness. Otherwise
3255 (in case of a small bitfield) the signedness is unchanged. */
3256 if (outer_type && *pbitsize == tree_low_cst (TYPE_SIZE (outer_type), 1))
3257 *punsignedp = TYPE_UNSIGNED (outer_type);
3259 /* Compute the mask to access the bitfield. */
3260 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3261 precision = TYPE_PRECISION (unsigned_type);
3263 mask = build_int_2 (~0, ~0);
3264 TREE_TYPE (mask) = unsigned_type;
3265 force_fit_type (mask, 0);
3266 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3267 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3269 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3270 if (and_mask != 0)
3271 mask = fold (build2 (BIT_AND_EXPR, unsigned_type,
3272 fold_convert (unsigned_type, and_mask), mask));
3274 *pmask = mask;
3275 *pand_mask = and_mask;
3276 return inner;
3279 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3280 bit positions. */
3282 static int
3283 all_ones_mask_p (tree mask, int size)
3285 tree type = TREE_TYPE (mask);
3286 unsigned int precision = TYPE_PRECISION (type);
3287 tree tmask;
3289 tmask = build_int_2 (~0, ~0);
3290 TREE_TYPE (tmask) = lang_hooks.types.signed_type (type);
3291 force_fit_type (tmask, 0);
3292 return
3293 tree_int_cst_equal (mask,
3294 const_binop (RSHIFT_EXPR,
3295 const_binop (LSHIFT_EXPR, tmask,
3296 size_int (precision - size),
3298 size_int (precision - size), 0));
3301 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3302 represents the sign bit of EXP's type. If EXP represents a sign
3303 or zero extension, also test VAL against the unextended type.
3304 The return value is the (sub)expression whose sign bit is VAL,
3305 or NULL_TREE otherwise. */
3307 static tree
3308 sign_bit_p (tree exp, tree val)
3310 unsigned HOST_WIDE_INT mask_lo, lo;
3311 HOST_WIDE_INT mask_hi, hi;
3312 int width;
3313 tree t;
3315 /* Tree EXP must have an integral type. */
3316 t = TREE_TYPE (exp);
3317 if (! INTEGRAL_TYPE_P (t))
3318 return NULL_TREE;
3320 /* Tree VAL must be an integer constant. */
3321 if (TREE_CODE (val) != INTEGER_CST
3322 || TREE_CONSTANT_OVERFLOW (val))
3323 return NULL_TREE;
3325 width = TYPE_PRECISION (t);
3326 if (width > HOST_BITS_PER_WIDE_INT)
3328 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3329 lo = 0;
3331 mask_hi = ((unsigned HOST_WIDE_INT) -1
3332 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3333 mask_lo = -1;
3335 else
3337 hi = 0;
3338 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3340 mask_hi = 0;
3341 mask_lo = ((unsigned HOST_WIDE_INT) -1
3342 >> (HOST_BITS_PER_WIDE_INT - width));
3345 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3346 treat VAL as if it were unsigned. */
3347 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3348 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3349 return exp;
3351 /* Handle extension from a narrower type. */
3352 if (TREE_CODE (exp) == NOP_EXPR
3353 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3354 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3356 return NULL_TREE;
3359 /* Subroutine for fold_truthop: determine if an operand is simple enough
3360 to be evaluated unconditionally. */
3362 static int
3363 simple_operand_p (tree exp)
3365 /* Strip any conversions that don't change the machine mode. */
3366 while ((TREE_CODE (exp) == NOP_EXPR
3367 || TREE_CODE (exp) == CONVERT_EXPR)
3368 && (TYPE_MODE (TREE_TYPE (exp))
3369 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3370 exp = TREE_OPERAND (exp, 0);
3372 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3373 || (DECL_P (exp)
3374 && ! TREE_ADDRESSABLE (exp)
3375 && ! TREE_THIS_VOLATILE (exp)
3376 && ! DECL_NONLOCAL (exp)
3377 /* Don't regard global variables as simple. They may be
3378 allocated in ways unknown to the compiler (shared memory,
3379 #pragma weak, etc). */
3380 && ! TREE_PUBLIC (exp)
3381 && ! DECL_EXTERNAL (exp)
3382 /* Loading a static variable is unduly expensive, but global
3383 registers aren't expensive. */
3384 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3387 /* The following functions are subroutines to fold_range_test and allow it to
3388 try to change a logical combination of comparisons into a range test.
3390 For example, both
3391 X == 2 || X == 3 || X == 4 || X == 5
3393 X >= 2 && X <= 5
3394 are converted to
3395 (unsigned) (X - 2) <= 3
3397 We describe each set of comparisons as being either inside or outside
3398 a range, using a variable named like IN_P, and then describe the
3399 range with a lower and upper bound. If one of the bounds is omitted,
3400 it represents either the highest or lowest value of the type.
3402 In the comments below, we represent a range by two numbers in brackets
3403 preceded by a "+" to designate being inside that range, or a "-" to
3404 designate being outside that range, so the condition can be inverted by
3405 flipping the prefix. An omitted bound is represented by a "-". For
3406 example, "- [-, 10]" means being outside the range starting at the lowest
3407 possible value and ending at 10, in other words, being greater than 10.
3408 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3409 always false.
3411 We set up things so that the missing bounds are handled in a consistent
3412 manner so neither a missing bound nor "true" and "false" need to be
3413 handled using a special case. */
3415 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3416 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3417 and UPPER1_P are nonzero if the respective argument is an upper bound
3418 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3419 must be specified for a comparison. ARG1 will be converted to ARG0's
3420 type if both are specified. */
3422 static tree
3423 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3424 tree arg1, int upper1_p)
3426 tree tem;
3427 int result;
3428 int sgn0, sgn1;
3430 /* If neither arg represents infinity, do the normal operation.
3431 Else, if not a comparison, return infinity. Else handle the special
3432 comparison rules. Note that most of the cases below won't occur, but
3433 are handled for consistency. */
3435 if (arg0 != 0 && arg1 != 0)
3437 tem = fold (build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3438 arg0, fold_convert (TREE_TYPE (arg0), arg1)));
3439 STRIP_NOPS (tem);
3440 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3443 if (TREE_CODE_CLASS (code) != '<')
3444 return 0;
3446 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3447 for neither. In real maths, we cannot assume open ended ranges are
3448 the same. But, this is computer arithmetic, where numbers are finite.
3449 We can therefore make the transformation of any unbounded range with
3450 the value Z, Z being greater than any representable number. This permits
3451 us to treat unbounded ranges as equal. */
3452 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3453 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3454 switch (code)
3456 case EQ_EXPR:
3457 result = sgn0 == sgn1;
3458 break;
3459 case NE_EXPR:
3460 result = sgn0 != sgn1;
3461 break;
3462 case LT_EXPR:
3463 result = sgn0 < sgn1;
3464 break;
3465 case LE_EXPR:
3466 result = sgn0 <= sgn1;
3467 break;
3468 case GT_EXPR:
3469 result = sgn0 > sgn1;
3470 break;
3471 case GE_EXPR:
3472 result = sgn0 >= sgn1;
3473 break;
3474 default:
3475 abort ();
3478 return constant_boolean_node (result, type);
3481 /* Given EXP, a logical expression, set the range it is testing into
3482 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3483 actually being tested. *PLOW and *PHIGH will be made of the same type
3484 as the returned expression. If EXP is not a comparison, we will most
3485 likely not be returning a useful value and range. */
3487 static tree
3488 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3490 enum tree_code code;
3491 tree arg0 = NULL_TREE, arg1 = NULL_TREE, type = NULL_TREE;
3492 tree orig_type = NULL_TREE;
3493 int in_p, n_in_p;
3494 tree low, high, n_low, n_high;
3496 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3497 and see if we can refine the range. Some of the cases below may not
3498 happen, but it doesn't seem worth worrying about this. We "continue"
3499 the outer loop when we've changed something; otherwise we "break"
3500 the switch, which will "break" the while. */
3502 in_p = 0;
3503 low = high = fold_convert (TREE_TYPE (exp), integer_zero_node);
3505 while (1)
3507 code = TREE_CODE (exp);
3509 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3511 if (first_rtl_op (code) > 0)
3512 arg0 = TREE_OPERAND (exp, 0);
3513 if (TREE_CODE_CLASS (code) == '<'
3514 || TREE_CODE_CLASS (code) == '1'
3515 || TREE_CODE_CLASS (code) == '2')
3516 type = TREE_TYPE (arg0);
3517 if (TREE_CODE_CLASS (code) == '2'
3518 || TREE_CODE_CLASS (code) == '<'
3519 || (TREE_CODE_CLASS (code) == 'e'
3520 && TREE_CODE_LENGTH (code) > 1))
3521 arg1 = TREE_OPERAND (exp, 1);
3524 /* Set ORIG_TYPE as soon as TYPE is non-null so that we do not
3525 lose a cast by accident. */
3526 if (type != NULL_TREE && orig_type == NULL_TREE)
3527 orig_type = type;
3529 switch (code)
3531 case TRUTH_NOT_EXPR:
3532 in_p = ! in_p, exp = arg0;
3533 continue;
3535 case EQ_EXPR: case NE_EXPR:
3536 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3537 /* We can only do something if the range is testing for zero
3538 and if the second operand is an integer constant. Note that
3539 saying something is "in" the range we make is done by
3540 complementing IN_P since it will set in the initial case of
3541 being not equal to zero; "out" is leaving it alone. */
3542 if (low == 0 || high == 0
3543 || ! integer_zerop (low) || ! integer_zerop (high)
3544 || TREE_CODE (arg1) != INTEGER_CST)
3545 break;
3547 switch (code)
3549 case NE_EXPR: /* - [c, c] */
3550 low = high = arg1;
3551 break;
3552 case EQ_EXPR: /* + [c, c] */
3553 in_p = ! in_p, low = high = arg1;
3554 break;
3555 case GT_EXPR: /* - [-, c] */
3556 low = 0, high = arg1;
3557 break;
3558 case GE_EXPR: /* + [c, -] */
3559 in_p = ! in_p, low = arg1, high = 0;
3560 break;
3561 case LT_EXPR: /* - [c, -] */
3562 low = arg1, high = 0;
3563 break;
3564 case LE_EXPR: /* + [-, c] */
3565 in_p = ! in_p, low = 0, high = arg1;
3566 break;
3567 default:
3568 abort ();
3571 exp = arg0;
3573 /* If this is an unsigned comparison, we also know that EXP is
3574 greater than or equal to zero. We base the range tests we make
3575 on that fact, so we record it here so we can parse existing
3576 range tests. */
3577 if (TYPE_UNSIGNED (type) && (low == 0 || high == 0))
3579 if (! merge_ranges (&n_in_p, &n_low, &n_high, in_p, low, high,
3580 1, fold_convert (type, integer_zero_node),
3581 NULL_TREE))
3582 break;
3584 in_p = n_in_p, low = n_low, high = n_high;
3586 /* If the high bound is missing, but we have a nonzero low
3587 bound, reverse the range so it goes from zero to the low bound
3588 minus 1. */
3589 if (high == 0 && low && ! integer_zerop (low))
3591 in_p = ! in_p;
3592 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3593 integer_one_node, 0);
3594 low = fold_convert (type, integer_zero_node);
3597 continue;
3599 case NEGATE_EXPR:
3600 /* (-x) IN [a,b] -> x in [-b, -a] */
3601 n_low = range_binop (MINUS_EXPR, type,
3602 fold_convert (type, integer_zero_node),
3603 0, high, 1);
3604 n_high = range_binop (MINUS_EXPR, type,
3605 fold_convert (type, integer_zero_node),
3606 0, low, 0);
3607 low = n_low, high = n_high;
3608 exp = arg0;
3609 continue;
3611 case BIT_NOT_EXPR:
3612 /* ~ X -> -X - 1 */
3613 exp = build2 (MINUS_EXPR, type, negate_expr (arg0),
3614 fold_convert (type, integer_one_node));
3615 continue;
3617 case PLUS_EXPR: case MINUS_EXPR:
3618 if (TREE_CODE (arg1) != INTEGER_CST)
3619 break;
3621 /* If EXP is signed, any overflow in the computation is undefined,
3622 so we don't worry about it so long as our computations on
3623 the bounds don't overflow. For unsigned, overflow is defined
3624 and this is exactly the right thing. */
3625 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3626 type, low, 0, arg1, 0);
3627 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3628 type, high, 1, arg1, 0);
3629 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3630 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3631 break;
3633 /* Check for an unsigned range which has wrapped around the maximum
3634 value thus making n_high < n_low, and normalize it. */
3635 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3637 low = range_binop (PLUS_EXPR, type, n_high, 0,
3638 integer_one_node, 0);
3639 high = range_binop (MINUS_EXPR, type, n_low, 0,
3640 integer_one_node, 0);
3642 /* If the range is of the form +/- [ x+1, x ], we won't
3643 be able to normalize it. But then, it represents the
3644 whole range or the empty set, so make it
3645 +/- [ -, - ]. */
3646 if (tree_int_cst_equal (n_low, low)
3647 && tree_int_cst_equal (n_high, high))
3648 low = high = 0;
3649 else
3650 in_p = ! in_p;
3652 else
3653 low = n_low, high = n_high;
3655 exp = arg0;
3656 continue;
3658 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3659 if (TYPE_PRECISION (type) > TYPE_PRECISION (orig_type))
3660 break;
3662 if (! INTEGRAL_TYPE_P (type)
3663 || (low != 0 && ! int_fits_type_p (low, type))
3664 || (high != 0 && ! int_fits_type_p (high, type)))
3665 break;
3667 n_low = low, n_high = high;
3669 if (n_low != 0)
3670 n_low = fold_convert (type, n_low);
3672 if (n_high != 0)
3673 n_high = fold_convert (type, n_high);
3675 /* If we're converting from an unsigned to a signed type,
3676 we will be doing the comparison as unsigned. The tests above
3677 have already verified that LOW and HIGH are both positive.
3679 So we have to make sure that the original unsigned value will
3680 be interpreted as positive. */
3681 if (TYPE_UNSIGNED (type) && ! TYPE_UNSIGNED (TREE_TYPE (exp)))
3683 tree equiv_type = lang_hooks.types.type_for_mode
3684 (TYPE_MODE (type), 1);
3685 tree high_positive;
3687 /* A range without an upper bound is, naturally, unbounded.
3688 Since convert would have cropped a very large value, use
3689 the max value for the destination type. */
3690 high_positive
3691 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3692 : TYPE_MAX_VALUE (type);
3694 if (TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (exp)))
3695 high_positive = fold (build2 (RSHIFT_EXPR, type,
3696 fold_convert (type,
3697 high_positive),
3698 fold_convert (type,
3699 integer_one_node)));
3701 /* If the low bound is specified, "and" the range with the
3702 range for which the original unsigned value will be
3703 positive. */
3704 if (low != 0)
3706 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3707 1, n_low, n_high, 1,
3708 fold_convert (type, integer_zero_node),
3709 high_positive))
3710 break;
3712 in_p = (n_in_p == in_p);
3714 else
3716 /* Otherwise, "or" the range with the range of the input
3717 that will be interpreted as negative. */
3718 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3719 0, n_low, n_high, 1,
3720 fold_convert (type, integer_zero_node),
3721 high_positive))
3722 break;
3724 in_p = (in_p != n_in_p);
3728 exp = arg0;
3729 low = n_low, high = n_high;
3730 continue;
3732 default:
3733 break;
3736 break;
3739 /* If EXP is a constant, we can evaluate whether this is true or false. */
3740 if (TREE_CODE (exp) == INTEGER_CST)
3742 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3743 exp, 0, low, 0))
3744 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3745 exp, 1, high, 1)));
3746 low = high = 0;
3747 exp = 0;
3750 *pin_p = in_p, *plow = low, *phigh = high;
3751 return exp;
3754 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3755 type, TYPE, return an expression to test if EXP is in (or out of, depending
3756 on IN_P) the range. */
3758 static tree
3759 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3761 tree etype = TREE_TYPE (exp);
3762 tree value;
3764 if (! in_p
3765 && (0 != (value = build_range_check (type, exp, 1, low, high))))
3766 return invert_truthvalue (value);
3768 if (low == 0 && high == 0)
3769 return fold_convert (type, integer_one_node);
3771 if (low == 0)
3772 return fold (build2 (LE_EXPR, type, exp, high));
3774 if (high == 0)
3775 return fold (build2 (GE_EXPR, type, exp, low));
3777 if (operand_equal_p (low, high, 0))
3778 return fold (build2 (EQ_EXPR, type, exp, low));
3780 if (integer_zerop (low))
3782 if (! TYPE_UNSIGNED (etype))
3784 etype = lang_hooks.types.unsigned_type (etype);
3785 high = fold_convert (etype, high);
3786 exp = fold_convert (etype, exp);
3788 return build_range_check (type, exp, 1, 0, high);
3791 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
3792 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3794 unsigned HOST_WIDE_INT lo;
3795 HOST_WIDE_INT hi;
3796 int prec;
3798 prec = TYPE_PRECISION (etype);
3799 if (prec <= HOST_BITS_PER_WIDE_INT)
3801 hi = 0;
3802 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3804 else
3806 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3807 lo = (unsigned HOST_WIDE_INT) -1;
3810 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3812 if (TYPE_UNSIGNED (etype))
3814 etype = lang_hooks.types.signed_type (etype);
3815 exp = fold_convert (etype, exp);
3817 return fold (build2 (GT_EXPR, type, exp,
3818 fold_convert (etype, integer_zero_node)));
3822 if (0 != (value = const_binop (MINUS_EXPR, high, low, 0))
3823 && ! TREE_OVERFLOW (value))
3824 return build_range_check (type,
3825 fold (build2 (MINUS_EXPR, etype, exp, low)),
3826 1, fold_convert (etype, integer_zero_node),
3827 value);
3829 return 0;
3832 /* Given two ranges, see if we can merge them into one. Return 1 if we
3833 can, 0 if we can't. Set the output range into the specified parameters. */
3835 static int
3836 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
3837 tree high0, int in1_p, tree low1, tree high1)
3839 int no_overlap;
3840 int subset;
3841 int temp;
3842 tree tem;
3843 int in_p;
3844 tree low, high;
3845 int lowequal = ((low0 == 0 && low1 == 0)
3846 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3847 low0, 0, low1, 0)));
3848 int highequal = ((high0 == 0 && high1 == 0)
3849 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3850 high0, 1, high1, 1)));
3852 /* Make range 0 be the range that starts first, or ends last if they
3853 start at the same value. Swap them if it isn't. */
3854 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3855 low0, 0, low1, 0))
3856 || (lowequal
3857 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3858 high1, 1, high0, 1))))
3860 temp = in0_p, in0_p = in1_p, in1_p = temp;
3861 tem = low0, low0 = low1, low1 = tem;
3862 tem = high0, high0 = high1, high1 = tem;
3865 /* Now flag two cases, whether the ranges are disjoint or whether the
3866 second range is totally subsumed in the first. Note that the tests
3867 below are simplified by the ones above. */
3868 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3869 high0, 1, low1, 0));
3870 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3871 high1, 1, high0, 1));
3873 /* We now have four cases, depending on whether we are including or
3874 excluding the two ranges. */
3875 if (in0_p && in1_p)
3877 /* If they don't overlap, the result is false. If the second range
3878 is a subset it is the result. Otherwise, the range is from the start
3879 of the second to the end of the first. */
3880 if (no_overlap)
3881 in_p = 0, low = high = 0;
3882 else if (subset)
3883 in_p = 1, low = low1, high = high1;
3884 else
3885 in_p = 1, low = low1, high = high0;
3888 else if (in0_p && ! in1_p)
3890 /* If they don't overlap, the result is the first range. If they are
3891 equal, the result is false. If the second range is a subset of the
3892 first, and the ranges begin at the same place, we go from just after
3893 the end of the first range to the end of the second. If the second
3894 range is not a subset of the first, or if it is a subset and both
3895 ranges end at the same place, the range starts at the start of the
3896 first range and ends just before the second range.
3897 Otherwise, we can't describe this as a single range. */
3898 if (no_overlap)
3899 in_p = 1, low = low0, high = high0;
3900 else if (lowequal && highequal)
3901 in_p = 0, low = high = 0;
3902 else if (subset && lowequal)
3904 in_p = 1, high = high0;
3905 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
3906 integer_one_node, 0);
3908 else if (! subset || highequal)
3910 in_p = 1, low = low0;
3911 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
3912 integer_one_node, 0);
3914 else
3915 return 0;
3918 else if (! in0_p && in1_p)
3920 /* If they don't overlap, the result is the second range. If the second
3921 is a subset of the first, the result is false. Otherwise,
3922 the range starts just after the first range and ends at the
3923 end of the second. */
3924 if (no_overlap)
3925 in_p = 1, low = low1, high = high1;
3926 else if (subset || highequal)
3927 in_p = 0, low = high = 0;
3928 else
3930 in_p = 1, high = high1;
3931 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
3932 integer_one_node, 0);
3936 else
3938 /* The case where we are excluding both ranges. Here the complex case
3939 is if they don't overlap. In that case, the only time we have a
3940 range is if they are adjacent. If the second is a subset of the
3941 first, the result is the first. Otherwise, the range to exclude
3942 starts at the beginning of the first range and ends at the end of the
3943 second. */
3944 if (no_overlap)
3946 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
3947 range_binop (PLUS_EXPR, NULL_TREE,
3948 high0, 1,
3949 integer_one_node, 1),
3950 1, low1, 0)))
3951 in_p = 0, low = low0, high = high1;
3952 else
3953 return 0;
3955 else if (subset)
3956 in_p = 0, low = low0, high = high0;
3957 else
3958 in_p = 0, low = low0, high = high1;
3961 *pin_p = in_p, *plow = low, *phigh = high;
3962 return 1;
3965 #ifndef RANGE_TEST_NON_SHORT_CIRCUIT
3966 #define RANGE_TEST_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
3967 #endif
3969 /* EXP is some logical combination of boolean tests. See if we can
3970 merge it into some range test. Return the new tree if so. */
3972 static tree
3973 fold_range_test (tree exp)
3975 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
3976 || TREE_CODE (exp) == TRUTH_OR_EXPR);
3977 int in0_p, in1_p, in_p;
3978 tree low0, low1, low, high0, high1, high;
3979 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
3980 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
3981 tree tem;
3983 /* If this is an OR operation, invert both sides; we will invert
3984 again at the end. */
3985 if (or_op)
3986 in0_p = ! in0_p, in1_p = ! in1_p;
3988 /* If both expressions are the same, if we can merge the ranges, and we
3989 can build the range test, return it or it inverted. If one of the
3990 ranges is always true or always false, consider it to be the same
3991 expression as the other. */
3992 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
3993 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
3994 in1_p, low1, high1)
3995 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
3996 lhs != 0 ? lhs
3997 : rhs != 0 ? rhs : integer_zero_node,
3998 in_p, low, high))))
3999 return or_op ? invert_truthvalue (tem) : tem;
4001 /* On machines where the branch cost is expensive, if this is a
4002 short-circuited branch and the underlying object on both sides
4003 is the same, make a non-short-circuit operation. */
4004 else if (RANGE_TEST_NON_SHORT_CIRCUIT
4005 && lhs != 0 && rhs != 0
4006 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4007 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
4008 && operand_equal_p (lhs, rhs, 0))
4010 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4011 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4012 which cases we can't do this. */
4013 if (simple_operand_p (lhs))
4014 return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4015 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4016 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
4017 TREE_OPERAND (exp, 1));
4019 else if (lang_hooks.decls.global_bindings_p () == 0
4020 && ! CONTAINS_PLACEHOLDER_P (lhs))
4022 tree common = save_expr (lhs);
4024 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
4025 or_op ? ! in0_p : in0_p,
4026 low0, high0))
4027 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
4028 or_op ? ! in1_p : in1_p,
4029 low1, high1))))
4030 return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4031 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4032 TREE_TYPE (exp), lhs, rhs);
4036 return 0;
4039 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4040 bit value. Arrange things so the extra bits will be set to zero if and
4041 only if C is signed-extended to its full width. If MASK is nonzero,
4042 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4044 static tree
4045 unextend (tree c, int p, int unsignedp, tree mask)
4047 tree type = TREE_TYPE (c);
4048 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4049 tree temp;
4051 if (p == modesize || unsignedp)
4052 return c;
4054 /* We work by getting just the sign bit into the low-order bit, then
4055 into the high-order bit, then sign-extend. We then XOR that value
4056 with C. */
4057 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4058 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4060 /* We must use a signed type in order to get an arithmetic right shift.
4061 However, we must also avoid introducing accidental overflows, so that
4062 a subsequent call to integer_zerop will work. Hence we must
4063 do the type conversion here. At this point, the constant is either
4064 zero or one, and the conversion to a signed type can never overflow.
4065 We could get an overflow if this conversion is done anywhere else. */
4066 if (TYPE_UNSIGNED (type))
4067 temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4069 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4070 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4071 if (mask != 0)
4072 temp = const_binop (BIT_AND_EXPR, temp,
4073 fold_convert (TREE_TYPE (c), mask), 0);
4074 /* If necessary, convert the type back to match the type of C. */
4075 if (TYPE_UNSIGNED (type))
4076 temp = fold_convert (type, temp);
4078 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4081 /* Find ways of folding logical expressions of LHS and RHS:
4082 Try to merge two comparisons to the same innermost item.
4083 Look for range tests like "ch >= '0' && ch <= '9'".
4084 Look for combinations of simple terms on machines with expensive branches
4085 and evaluate the RHS unconditionally.
4087 For example, if we have p->a == 2 && p->b == 4 and we can make an
4088 object large enough to span both A and B, we can do this with a comparison
4089 against the object ANDed with the a mask.
4091 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4092 operations to do this with one comparison.
4094 We check for both normal comparisons and the BIT_AND_EXPRs made this by
4095 function and the one above.
4097 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
4098 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4100 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4101 two operands.
4103 We return the simplified tree or 0 if no optimization is possible. */
4105 static tree
4106 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4108 /* If this is the "or" of two comparisons, we can do something if
4109 the comparisons are NE_EXPR. If this is the "and", we can do something
4110 if the comparisons are EQ_EXPR. I.e.,
4111 (a->b == 2 && a->c == 4) can become (a->new == NEW).
4113 WANTED_CODE is this operation code. For single bit fields, we can
4114 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4115 comparison for one-bit fields. */
4117 enum tree_code wanted_code;
4118 enum tree_code lcode, rcode;
4119 tree ll_arg, lr_arg, rl_arg, rr_arg;
4120 tree ll_inner, lr_inner, rl_inner, rr_inner;
4121 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4122 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4123 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4124 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4125 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4126 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4127 enum machine_mode lnmode, rnmode;
4128 tree ll_mask, lr_mask, rl_mask, rr_mask;
4129 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4130 tree l_const, r_const;
4131 tree lntype, rntype, result;
4132 int first_bit, end_bit;
4133 int volatilep;
4135 /* Start by getting the comparison codes. Fail if anything is volatile.
4136 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4137 it were surrounded with a NE_EXPR. */
4139 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4140 return 0;
4142 lcode = TREE_CODE (lhs);
4143 rcode = TREE_CODE (rhs);
4145 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4147 lhs = build2 (NE_EXPR, truth_type, lhs, integer_zero_node);
4148 lcode = NE_EXPR;
4151 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4153 rhs = build2 (NE_EXPR, truth_type, rhs, integer_zero_node);
4154 rcode = NE_EXPR;
4157 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
4158 return 0;
4160 ll_arg = TREE_OPERAND (lhs, 0);
4161 lr_arg = TREE_OPERAND (lhs, 1);
4162 rl_arg = TREE_OPERAND (rhs, 0);
4163 rr_arg = TREE_OPERAND (rhs, 1);
4165 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
4166 if (simple_operand_p (ll_arg)
4167 && simple_operand_p (lr_arg))
4169 tree result;
4170 if (operand_equal_p (ll_arg, rl_arg, 0)
4171 && operand_equal_p (lr_arg, rr_arg, 0))
4173 result = combine_comparisons (code, lcode, rcode,
4174 truth_type, ll_arg, lr_arg);
4175 if (result)
4176 return result;
4178 else if (operand_equal_p (ll_arg, rr_arg, 0)
4179 && operand_equal_p (lr_arg, rl_arg, 0))
4181 result = combine_comparisons (code, lcode,
4182 swap_tree_comparison (rcode),
4183 truth_type, ll_arg, lr_arg);
4184 if (result)
4185 return result;
4189 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4190 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4192 /* If the RHS can be evaluated unconditionally and its operands are
4193 simple, it wins to evaluate the RHS unconditionally on machines
4194 with expensive branches. In this case, this isn't a comparison
4195 that can be merged. Avoid doing this if the RHS is a floating-point
4196 comparison since those can trap. */
4198 if (BRANCH_COST >= 2
4199 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4200 && simple_operand_p (rl_arg)
4201 && simple_operand_p (rr_arg))
4203 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
4204 if (code == TRUTH_OR_EXPR
4205 && lcode == NE_EXPR && integer_zerop (lr_arg)
4206 && rcode == NE_EXPR && integer_zerop (rr_arg)
4207 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4208 return build2 (NE_EXPR, truth_type,
4209 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4210 ll_arg, rl_arg),
4211 fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4213 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
4214 if (code == TRUTH_AND_EXPR
4215 && lcode == EQ_EXPR && integer_zerop (lr_arg)
4216 && rcode == EQ_EXPR && integer_zerop (rr_arg)
4217 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4218 return build2 (EQ_EXPR, truth_type,
4219 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4220 ll_arg, rl_arg),
4221 fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4223 return build2 (code, truth_type, lhs, rhs);
4226 /* See if the comparisons can be merged. Then get all the parameters for
4227 each side. */
4229 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4230 || (rcode != EQ_EXPR && rcode != NE_EXPR))
4231 return 0;
4233 volatilep = 0;
4234 ll_inner = decode_field_reference (ll_arg,
4235 &ll_bitsize, &ll_bitpos, &ll_mode,
4236 &ll_unsignedp, &volatilep, &ll_mask,
4237 &ll_and_mask);
4238 lr_inner = decode_field_reference (lr_arg,
4239 &lr_bitsize, &lr_bitpos, &lr_mode,
4240 &lr_unsignedp, &volatilep, &lr_mask,
4241 &lr_and_mask);
4242 rl_inner = decode_field_reference (rl_arg,
4243 &rl_bitsize, &rl_bitpos, &rl_mode,
4244 &rl_unsignedp, &volatilep, &rl_mask,
4245 &rl_and_mask);
4246 rr_inner = decode_field_reference (rr_arg,
4247 &rr_bitsize, &rr_bitpos, &rr_mode,
4248 &rr_unsignedp, &volatilep, &rr_mask,
4249 &rr_and_mask);
4251 /* It must be true that the inner operation on the lhs of each
4252 comparison must be the same if we are to be able to do anything.
4253 Then see if we have constants. If not, the same must be true for
4254 the rhs's. */
4255 if (volatilep || ll_inner == 0 || rl_inner == 0
4256 || ! operand_equal_p (ll_inner, rl_inner, 0))
4257 return 0;
4259 if (TREE_CODE (lr_arg) == INTEGER_CST
4260 && TREE_CODE (rr_arg) == INTEGER_CST)
4261 l_const = lr_arg, r_const = rr_arg;
4262 else if (lr_inner == 0 || rr_inner == 0
4263 || ! operand_equal_p (lr_inner, rr_inner, 0))
4264 return 0;
4265 else
4266 l_const = r_const = 0;
4268 /* If either comparison code is not correct for our logical operation,
4269 fail. However, we can convert a one-bit comparison against zero into
4270 the opposite comparison against that bit being set in the field. */
4272 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4273 if (lcode != wanted_code)
4275 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4277 /* Make the left operand unsigned, since we are only interested
4278 in the value of one bit. Otherwise we are doing the wrong
4279 thing below. */
4280 ll_unsignedp = 1;
4281 l_const = ll_mask;
4283 else
4284 return 0;
4287 /* This is analogous to the code for l_const above. */
4288 if (rcode != wanted_code)
4290 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4292 rl_unsignedp = 1;
4293 r_const = rl_mask;
4295 else
4296 return 0;
4299 /* After this point all optimizations will generate bit-field
4300 references, which we might not want. */
4301 if (! lang_hooks.can_use_bit_fields_p ())
4302 return 0;
4304 /* See if we can find a mode that contains both fields being compared on
4305 the left. If we can't, fail. Otherwise, update all constants and masks
4306 to be relative to a field of that size. */
4307 first_bit = MIN (ll_bitpos, rl_bitpos);
4308 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4309 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4310 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4311 volatilep);
4312 if (lnmode == VOIDmode)
4313 return 0;
4315 lnbitsize = GET_MODE_BITSIZE (lnmode);
4316 lnbitpos = first_bit & ~ (lnbitsize - 1);
4317 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
4318 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4320 if (BYTES_BIG_ENDIAN)
4322 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4323 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4326 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
4327 size_int (xll_bitpos), 0);
4328 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
4329 size_int (xrl_bitpos), 0);
4331 if (l_const)
4333 l_const = fold_convert (lntype, l_const);
4334 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4335 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4336 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4337 fold (build1 (BIT_NOT_EXPR,
4338 lntype, ll_mask)),
4339 0)))
4341 warning ("comparison is always %d", wanted_code == NE_EXPR);
4343 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4346 if (r_const)
4348 r_const = fold_convert (lntype, r_const);
4349 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4350 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4351 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4352 fold (build1 (BIT_NOT_EXPR,
4353 lntype, rl_mask)),
4354 0)))
4356 warning ("comparison is always %d", wanted_code == NE_EXPR);
4358 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4362 /* If the right sides are not constant, do the same for it. Also,
4363 disallow this optimization if a size or signedness mismatch occurs
4364 between the left and right sides. */
4365 if (l_const == 0)
4367 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4368 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4369 /* Make sure the two fields on the right
4370 correspond to the left without being swapped. */
4371 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4372 return 0;
4374 first_bit = MIN (lr_bitpos, rr_bitpos);
4375 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4376 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4377 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4378 volatilep);
4379 if (rnmode == VOIDmode)
4380 return 0;
4382 rnbitsize = GET_MODE_BITSIZE (rnmode);
4383 rnbitpos = first_bit & ~ (rnbitsize - 1);
4384 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
4385 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4387 if (BYTES_BIG_ENDIAN)
4389 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4390 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4393 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
4394 size_int (xlr_bitpos), 0);
4395 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
4396 size_int (xrr_bitpos), 0);
4398 /* Make a mask that corresponds to both fields being compared.
4399 Do this for both items being compared. If the operands are the
4400 same size and the bits being compared are in the same position
4401 then we can do this by masking both and comparing the masked
4402 results. */
4403 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4404 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4405 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4407 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4408 ll_unsignedp || rl_unsignedp);
4409 if (! all_ones_mask_p (ll_mask, lnbitsize))
4410 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
4412 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4413 lr_unsignedp || rr_unsignedp);
4414 if (! all_ones_mask_p (lr_mask, rnbitsize))
4415 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
4417 return build2 (wanted_code, truth_type, lhs, rhs);
4420 /* There is still another way we can do something: If both pairs of
4421 fields being compared are adjacent, we may be able to make a wider
4422 field containing them both.
4424 Note that we still must mask the lhs/rhs expressions. Furthermore,
4425 the mask must be shifted to account for the shift done by
4426 make_bit_field_ref. */
4427 if ((ll_bitsize + ll_bitpos == rl_bitpos
4428 && lr_bitsize + lr_bitpos == rr_bitpos)
4429 || (ll_bitpos == rl_bitpos + rl_bitsize
4430 && lr_bitpos == rr_bitpos + rr_bitsize))
4432 tree type;
4434 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4435 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4436 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4437 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4439 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4440 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4441 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4442 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4444 /* Convert to the smaller type before masking out unwanted bits. */
4445 type = lntype;
4446 if (lntype != rntype)
4448 if (lnbitsize > rnbitsize)
4450 lhs = fold_convert (rntype, lhs);
4451 ll_mask = fold_convert (rntype, ll_mask);
4452 type = rntype;
4454 else if (lnbitsize < rnbitsize)
4456 rhs = fold_convert (lntype, rhs);
4457 lr_mask = fold_convert (lntype, lr_mask);
4458 type = lntype;
4462 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4463 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
4465 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4466 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
4468 return build2 (wanted_code, truth_type, lhs, rhs);
4471 return 0;
4474 /* Handle the case of comparisons with constants. If there is something in
4475 common between the masks, those bits of the constants must be the same.
4476 If not, the condition is always false. Test for this to avoid generating
4477 incorrect code below. */
4478 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4479 if (! integer_zerop (result)
4480 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4481 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4483 if (wanted_code == NE_EXPR)
4485 warning ("`or' of unmatched not-equal tests is always 1");
4486 return constant_boolean_node (true, truth_type);
4488 else
4490 warning ("`and' of mutually exclusive equal-tests is always 0");
4491 return constant_boolean_node (false, truth_type);
4495 /* Construct the expression we will return. First get the component
4496 reference we will make. Unless the mask is all ones the width of
4497 that field, perform the mask operation. Then compare with the
4498 merged constant. */
4499 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4500 ll_unsignedp || rl_unsignedp);
4502 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4503 if (! all_ones_mask_p (ll_mask, lnbitsize))
4504 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
4506 return build2 (wanted_code, truth_type, result,
4507 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4510 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4511 constant. */
4513 static tree
4514 optimize_minmax_comparison (tree t)
4516 tree type = TREE_TYPE (t);
4517 tree arg0 = TREE_OPERAND (t, 0);
4518 enum tree_code op_code;
4519 tree comp_const = TREE_OPERAND (t, 1);
4520 tree minmax_const;
4521 int consts_equal, consts_lt;
4522 tree inner;
4524 STRIP_SIGN_NOPS (arg0);
4526 op_code = TREE_CODE (arg0);
4527 minmax_const = TREE_OPERAND (arg0, 1);
4528 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4529 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4530 inner = TREE_OPERAND (arg0, 0);
4532 /* If something does not permit us to optimize, return the original tree. */
4533 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4534 || TREE_CODE (comp_const) != INTEGER_CST
4535 || TREE_CONSTANT_OVERFLOW (comp_const)
4536 || TREE_CODE (minmax_const) != INTEGER_CST
4537 || TREE_CONSTANT_OVERFLOW (minmax_const))
4538 return t;
4540 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4541 and GT_EXPR, doing the rest with recursive calls using logical
4542 simplifications. */
4543 switch (TREE_CODE (t))
4545 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4546 return
4547 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4549 case GE_EXPR:
4550 return
4551 fold (build2 (TRUTH_ORIF_EXPR, type,
4552 optimize_minmax_comparison
4553 (build2 (EQ_EXPR, type, arg0, comp_const)),
4554 optimize_minmax_comparison
4555 (build2 (GT_EXPR, type, arg0, comp_const))));
4557 case EQ_EXPR:
4558 if (op_code == MAX_EXPR && consts_equal)
4559 /* MAX (X, 0) == 0 -> X <= 0 */
4560 return fold (build2 (LE_EXPR, type, inner, comp_const));
4562 else if (op_code == MAX_EXPR && consts_lt)
4563 /* MAX (X, 0) == 5 -> X == 5 */
4564 return fold (build2 (EQ_EXPR, type, inner, comp_const));
4566 else if (op_code == MAX_EXPR)
4567 /* MAX (X, 0) == -1 -> false */
4568 return omit_one_operand (type, integer_zero_node, inner);
4570 else if (consts_equal)
4571 /* MIN (X, 0) == 0 -> X >= 0 */
4572 return fold (build2 (GE_EXPR, type, inner, comp_const));
4574 else if (consts_lt)
4575 /* MIN (X, 0) == 5 -> false */
4576 return omit_one_operand (type, integer_zero_node, inner);
4578 else
4579 /* MIN (X, 0) == -1 -> X == -1 */
4580 return fold (build2 (EQ_EXPR, type, inner, comp_const));
4582 case GT_EXPR:
4583 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
4584 /* MAX (X, 0) > 0 -> X > 0
4585 MAX (X, 0) > 5 -> X > 5 */
4586 return fold (build2 (GT_EXPR, type, inner, comp_const));
4588 else if (op_code == MAX_EXPR)
4589 /* MAX (X, 0) > -1 -> true */
4590 return omit_one_operand (type, integer_one_node, inner);
4592 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
4593 /* MIN (X, 0) > 0 -> false
4594 MIN (X, 0) > 5 -> false */
4595 return omit_one_operand (type, integer_zero_node, inner);
4597 else
4598 /* MIN (X, 0) > -1 -> X > -1 */
4599 return fold (build2 (GT_EXPR, type, inner, comp_const));
4601 default:
4602 return t;
4606 /* T is an integer expression that is being multiplied, divided, or taken a
4607 modulus (CODE says which and what kind of divide or modulus) by a
4608 constant C. See if we can eliminate that operation by folding it with
4609 other operations already in T. WIDE_TYPE, if non-null, is a type that
4610 should be used for the computation if wider than our type.
4612 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
4613 (X * 2) + (Y * 4). We must, however, be assured that either the original
4614 expression would not overflow or that overflow is undefined for the type
4615 in the language in question.
4617 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
4618 the machine has a multiply-accumulate insn or that this is part of an
4619 addressing calculation.
4621 If we return a non-null expression, it is an equivalent form of the
4622 original computation, but need not be in the original type. */
4624 static tree
4625 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
4627 /* To avoid exponential search depth, refuse to allow recursion past
4628 three levels. Beyond that (1) it's highly unlikely that we'll find
4629 something interesting and (2) we've probably processed it before
4630 when we built the inner expression. */
4632 static int depth;
4633 tree ret;
4635 if (depth > 3)
4636 return NULL;
4638 depth++;
4639 ret = extract_muldiv_1 (t, c, code, wide_type);
4640 depth--;
4642 return ret;
4645 static tree
4646 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
4648 tree type = TREE_TYPE (t);
4649 enum tree_code tcode = TREE_CODE (t);
4650 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
4651 > GET_MODE_SIZE (TYPE_MODE (type)))
4652 ? wide_type : type);
4653 tree t1, t2;
4654 int same_p = tcode == code;
4655 tree op0 = NULL_TREE, op1 = NULL_TREE;
4657 /* Don't deal with constants of zero here; they confuse the code below. */
4658 if (integer_zerop (c))
4659 return NULL_TREE;
4661 if (TREE_CODE_CLASS (tcode) == '1')
4662 op0 = TREE_OPERAND (t, 0);
4664 if (TREE_CODE_CLASS (tcode) == '2')
4665 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
4667 /* Note that we need not handle conditional operations here since fold
4668 already handles those cases. So just do arithmetic here. */
4669 switch (tcode)
4671 case INTEGER_CST:
4672 /* For a constant, we can always simplify if we are a multiply
4673 or (for divide and modulus) if it is a multiple of our constant. */
4674 if (code == MULT_EXPR
4675 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
4676 return const_binop (code, fold_convert (ctype, t),
4677 fold_convert (ctype, c), 0);
4678 break;
4680 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
4681 /* If op0 is an expression ... */
4682 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
4683 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
4684 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
4685 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
4686 /* ... and is unsigned, and its type is smaller than ctype,
4687 then we cannot pass through as widening. */
4688 && ((TYPE_UNSIGNED (TREE_TYPE (op0))
4689 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
4690 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
4691 && (GET_MODE_SIZE (TYPE_MODE (ctype))
4692 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
4693 /* ... or its type is larger than ctype,
4694 then we cannot pass through this truncation. */
4695 || (GET_MODE_SIZE (TYPE_MODE (ctype))
4696 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
4697 /* ... or signedness changes for division or modulus,
4698 then we cannot pass through this conversion. */
4699 || (code != MULT_EXPR
4700 && (TYPE_UNSIGNED (ctype)
4701 != TYPE_UNSIGNED (TREE_TYPE (op0))))))
4702 break;
4704 /* Pass the constant down and see if we can make a simplification. If
4705 we can, replace this expression with the inner simplification for
4706 possible later conversion to our or some other type. */
4707 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
4708 && TREE_CODE (t2) == INTEGER_CST
4709 && ! TREE_CONSTANT_OVERFLOW (t2)
4710 && (0 != (t1 = extract_muldiv (op0, t2, code,
4711 code == MULT_EXPR
4712 ? ctype : NULL_TREE))))
4713 return t1;
4714 break;
4716 case NEGATE_EXPR: case ABS_EXPR:
4717 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4718 return fold (build1 (tcode, ctype, fold_convert (ctype, t1)));
4719 break;
4721 case MIN_EXPR: case MAX_EXPR:
4722 /* If widening the type changes the signedness, then we can't perform
4723 this optimization as that changes the result. */
4724 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
4725 break;
4727 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
4728 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
4729 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
4731 if (tree_int_cst_sgn (c) < 0)
4732 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
4734 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4735 fold_convert (ctype, t2)));
4737 break;
4739 case LSHIFT_EXPR: case RSHIFT_EXPR:
4740 /* If the second operand is constant, this is a multiplication
4741 or floor division, by a power of two, so we can treat it that
4742 way unless the multiplier or divisor overflows. */
4743 if (TREE_CODE (op1) == INTEGER_CST
4744 /* const_binop may not detect overflow correctly,
4745 so check for it explicitly here. */
4746 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
4747 && TREE_INT_CST_HIGH (op1) == 0
4748 && 0 != (t1 = fold_convert (ctype,
4749 const_binop (LSHIFT_EXPR,
4750 size_one_node,
4751 op1, 0)))
4752 && ! TREE_OVERFLOW (t1))
4753 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
4754 ? MULT_EXPR : FLOOR_DIV_EXPR,
4755 ctype, fold_convert (ctype, op0), t1),
4756 c, code, wide_type);
4757 break;
4759 case PLUS_EXPR: case MINUS_EXPR:
4760 /* See if we can eliminate the operation on both sides. If we can, we
4761 can return a new PLUS or MINUS. If we can't, the only remaining
4762 cases where we can do anything are if the second operand is a
4763 constant. */
4764 t1 = extract_muldiv (op0, c, code, wide_type);
4765 t2 = extract_muldiv (op1, c, code, wide_type);
4766 if (t1 != 0 && t2 != 0
4767 && (code == MULT_EXPR
4768 /* If not multiplication, we can only do this if both operands
4769 are divisible by c. */
4770 || (multiple_of_p (ctype, op0, c)
4771 && multiple_of_p (ctype, op1, c))))
4772 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4773 fold_convert (ctype, t2)));
4775 /* If this was a subtraction, negate OP1 and set it to be an addition.
4776 This simplifies the logic below. */
4777 if (tcode == MINUS_EXPR)
4778 tcode = PLUS_EXPR, op1 = negate_expr (op1);
4780 if (TREE_CODE (op1) != INTEGER_CST)
4781 break;
4783 /* If either OP1 or C are negative, this optimization is not safe for
4784 some of the division and remainder types while for others we need
4785 to change the code. */
4786 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
4788 if (code == CEIL_DIV_EXPR)
4789 code = FLOOR_DIV_EXPR;
4790 else if (code == FLOOR_DIV_EXPR)
4791 code = CEIL_DIV_EXPR;
4792 else if (code != MULT_EXPR
4793 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
4794 break;
4797 /* If it's a multiply or a division/modulus operation of a multiple
4798 of our constant, do the operation and verify it doesn't overflow. */
4799 if (code == MULT_EXPR
4800 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4802 op1 = const_binop (code, fold_convert (ctype, op1),
4803 fold_convert (ctype, c), 0);
4804 /* We allow the constant to overflow with wrapping semantics. */
4805 if (op1 == 0
4806 || (TREE_OVERFLOW (op1) && ! flag_wrapv))
4807 break;
4809 else
4810 break;
4812 /* If we have an unsigned type is not a sizetype, we cannot widen
4813 the operation since it will change the result if the original
4814 computation overflowed. */
4815 if (TYPE_UNSIGNED (ctype)
4816 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
4817 && ctype != type)
4818 break;
4820 /* If we were able to eliminate our operation from the first side,
4821 apply our operation to the second side and reform the PLUS. */
4822 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
4823 return fold (build2 (tcode, ctype, fold_convert (ctype, t1), op1));
4825 /* The last case is if we are a multiply. In that case, we can
4826 apply the distributive law to commute the multiply and addition
4827 if the multiplication of the constants doesn't overflow. */
4828 if (code == MULT_EXPR)
4829 return fold (build2 (tcode, ctype,
4830 fold (build2 (code, ctype,
4831 fold_convert (ctype, op0),
4832 fold_convert (ctype, c))),
4833 op1));
4835 break;
4837 case MULT_EXPR:
4838 /* We have a special case here if we are doing something like
4839 (C * 8) % 4 since we know that's zero. */
4840 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
4841 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
4842 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
4843 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4844 return omit_one_operand (type, integer_zero_node, op0);
4846 /* ... fall through ... */
4848 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
4849 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
4850 /* If we can extract our operation from the LHS, do so and return a
4851 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
4852 do something only if the second operand is a constant. */
4853 if (same_p
4854 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
4855 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
4856 fold_convert (ctype, op1)));
4857 else if (tcode == MULT_EXPR && code == MULT_EXPR
4858 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
4859 return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
4860 fold_convert (ctype, t1)));
4861 else if (TREE_CODE (op1) != INTEGER_CST)
4862 return 0;
4864 /* If these are the same operation types, we can associate them
4865 assuming no overflow. */
4866 if (tcode == code
4867 && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
4868 fold_convert (ctype, c), 0))
4869 && ! TREE_OVERFLOW (t1))
4870 return fold (build2 (tcode, ctype, fold_convert (ctype, op0), t1));
4872 /* If these operations "cancel" each other, we have the main
4873 optimizations of this pass, which occur when either constant is a
4874 multiple of the other, in which case we replace this with either an
4875 operation or CODE or TCODE.
4877 If we have an unsigned type that is not a sizetype, we cannot do
4878 this since it will change the result if the original computation
4879 overflowed. */
4880 if ((! TYPE_UNSIGNED (ctype)
4881 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
4882 && ! flag_wrapv
4883 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
4884 || (tcode == MULT_EXPR
4885 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
4886 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
4888 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
4889 return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
4890 fold_convert (ctype,
4891 const_binop (TRUNC_DIV_EXPR,
4892 op1, c, 0))));
4893 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
4894 return fold (build2 (code, ctype, fold_convert (ctype, op0),
4895 fold_convert (ctype,
4896 const_binop (TRUNC_DIV_EXPR,
4897 c, op1, 0))));
4899 break;
4901 default:
4902 break;
4905 return 0;
4908 /* Return a node which has the indicated constant VALUE (either 0 or
4909 1), and is of the indicated TYPE. */
4911 static tree
4912 constant_boolean_node (int value, tree type)
4914 if (type == integer_type_node)
4915 return value ? integer_one_node : integer_zero_node;
4916 else if (TREE_CODE (type) == BOOLEAN_TYPE)
4917 return lang_hooks.truthvalue_conversion (value ? integer_one_node
4918 : integer_zero_node);
4919 else
4921 tree t = build_int_2 (value, 0);
4923 TREE_TYPE (t) = type;
4924 return t;
4928 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
4929 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
4930 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
4931 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
4932 COND is the first argument to CODE; otherwise (as in the example
4933 given here), it is the second argument. TYPE is the type of the
4934 original expression. Return NULL_TREE if no simplification is
4935 possible. */
4937 static tree
4938 fold_binary_op_with_conditional_arg (enum tree_code code, tree type,
4939 tree cond, tree arg, int cond_first_p)
4941 tree test, true_value, false_value;
4942 tree lhs = NULL_TREE;
4943 tree rhs = NULL_TREE;
4945 /* This transformation is only worthwhile if we don't have to wrap
4946 arg in a SAVE_EXPR, and the operation can be simplified on atleast
4947 one of the branches once its pushed inside the COND_EXPR. */
4948 if (!TREE_CONSTANT (arg))
4949 return NULL_TREE;
4951 if (TREE_CODE (cond) == COND_EXPR)
4953 test = TREE_OPERAND (cond, 0);
4954 true_value = TREE_OPERAND (cond, 1);
4955 false_value = TREE_OPERAND (cond, 2);
4956 /* If this operand throws an expression, then it does not make
4957 sense to try to perform a logical or arithmetic operation
4958 involving it. */
4959 if (VOID_TYPE_P (TREE_TYPE (true_value)))
4960 lhs = true_value;
4961 if (VOID_TYPE_P (TREE_TYPE (false_value)))
4962 rhs = false_value;
4964 else
4966 tree testtype = TREE_TYPE (cond);
4967 test = cond;
4968 true_value = constant_boolean_node (true, testtype);
4969 false_value = constant_boolean_node (false, testtype);
4972 if (lhs == 0)
4973 lhs = fold (cond_first_p ? build2 (code, type, true_value, arg)
4974 : build2 (code, type, arg, true_value));
4975 if (rhs == 0)
4976 rhs = fold (cond_first_p ? build2 (code, type, false_value, arg)
4977 : build2 (code, type, arg, false_value));
4979 test = fold (build3 (COND_EXPR, type, test, lhs, rhs));
4980 return fold_convert (type, test);
4984 /* Subroutine of fold() that checks for the addition of +/- 0.0.
4986 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
4987 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
4988 ADDEND is the same as X.
4990 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
4991 and finite. The problematic cases are when X is zero, and its mode
4992 has signed zeros. In the case of rounding towards -infinity,
4993 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
4994 modes, X + 0 is not the same as X because -0 + 0 is 0. */
4996 static bool
4997 fold_real_zero_addition_p (tree type, tree addend, int negate)
4999 if (!real_zerop (addend))
5000 return false;
5002 /* Don't allow the fold with -fsignaling-nans. */
5003 if (HONOR_SNANS (TYPE_MODE (type)))
5004 return false;
5006 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
5007 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5008 return true;
5010 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
5011 if (TREE_CODE (addend) == REAL_CST
5012 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5013 negate = !negate;
5015 /* The mode has signed zeros, and we have to honor their sign.
5016 In this situation, there is only one case we can return true for.
5017 X - 0 is the same as X unless rounding towards -infinity is
5018 supported. */
5019 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5022 /* Subroutine of fold() that checks comparisons of built-in math
5023 functions against real constants.
5025 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5026 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
5027 is the type of the result and ARG0 and ARG1 are the operands of the
5028 comparison. ARG1 must be a TREE_REAL_CST.
5030 The function returns the constant folded tree if a simplification
5031 can be made, and NULL_TREE otherwise. */
5033 static tree
5034 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5035 tree type, tree arg0, tree arg1)
5037 REAL_VALUE_TYPE c;
5039 if (BUILTIN_SQRT_P (fcode))
5041 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5042 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5044 c = TREE_REAL_CST (arg1);
5045 if (REAL_VALUE_NEGATIVE (c))
5047 /* sqrt(x) < y is always false, if y is negative. */
5048 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5049 return omit_one_operand (type, integer_zero_node, arg);
5051 /* sqrt(x) > y is always true, if y is negative and we
5052 don't care about NaNs, i.e. negative values of x. */
5053 if (code == NE_EXPR || !HONOR_NANS (mode))
5054 return omit_one_operand (type, integer_one_node, arg);
5056 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5057 return fold (build2 (GE_EXPR, type, arg,
5058 build_real (TREE_TYPE (arg), dconst0)));
5060 else if (code == GT_EXPR || code == GE_EXPR)
5062 REAL_VALUE_TYPE c2;
5064 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5065 real_convert (&c2, mode, &c2);
5067 if (REAL_VALUE_ISINF (c2))
5069 /* sqrt(x) > y is x == +Inf, when y is very large. */
5070 if (HONOR_INFINITIES (mode))
5071 return fold (build2 (EQ_EXPR, type, arg,
5072 build_real (TREE_TYPE (arg), c2)));
5074 /* sqrt(x) > y is always false, when y is very large
5075 and we don't care about infinities. */
5076 return omit_one_operand (type, integer_zero_node, arg);
5079 /* sqrt(x) > c is the same as x > c*c. */
5080 return fold (build2 (code, type, arg,
5081 build_real (TREE_TYPE (arg), c2)));
5083 else if (code == LT_EXPR || code == LE_EXPR)
5085 REAL_VALUE_TYPE c2;
5087 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5088 real_convert (&c2, mode, &c2);
5090 if (REAL_VALUE_ISINF (c2))
5092 /* sqrt(x) < y is always true, when y is a very large
5093 value and we don't care about NaNs or Infinities. */
5094 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5095 return omit_one_operand (type, integer_one_node, arg);
5097 /* sqrt(x) < y is x != +Inf when y is very large and we
5098 don't care about NaNs. */
5099 if (! HONOR_NANS (mode))
5100 return fold (build2 (NE_EXPR, type, arg,
5101 build_real (TREE_TYPE (arg), c2)));
5103 /* sqrt(x) < y is x >= 0 when y is very large and we
5104 don't care about Infinities. */
5105 if (! HONOR_INFINITIES (mode))
5106 return fold (build2 (GE_EXPR, type, arg,
5107 build_real (TREE_TYPE (arg), dconst0)));
5109 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5110 if (lang_hooks.decls.global_bindings_p () != 0
5111 || CONTAINS_PLACEHOLDER_P (arg))
5112 return NULL_TREE;
5114 arg = save_expr (arg);
5115 return fold (build2 (TRUTH_ANDIF_EXPR, type,
5116 fold (build2 (GE_EXPR, type, arg,
5117 build_real (TREE_TYPE (arg),
5118 dconst0))),
5119 fold (build2 (NE_EXPR, type, arg,
5120 build_real (TREE_TYPE (arg),
5121 c2)))));
5124 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5125 if (! HONOR_NANS (mode))
5126 return fold (build2 (code, type, arg,
5127 build_real (TREE_TYPE (arg), c2)));
5129 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5130 if (lang_hooks.decls.global_bindings_p () == 0
5131 && ! CONTAINS_PLACEHOLDER_P (arg))
5133 arg = save_expr (arg);
5134 return fold (build2 (TRUTH_ANDIF_EXPR, type,
5135 fold (build2 (GE_EXPR, type, arg,
5136 build_real (TREE_TYPE (arg),
5137 dconst0))),
5138 fold (build2 (code, type, arg,
5139 build_real (TREE_TYPE (arg),
5140 c2)))));
5145 return NULL_TREE;
5148 /* Subroutine of fold() that optimizes comparisons against Infinities,
5149 either +Inf or -Inf.
5151 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5152 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5153 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5155 The function returns the constant folded tree if a simplification
5156 can be made, and NULL_TREE otherwise. */
5158 static tree
5159 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5161 enum machine_mode mode;
5162 REAL_VALUE_TYPE max;
5163 tree temp;
5164 bool neg;
5166 mode = TYPE_MODE (TREE_TYPE (arg0));
5168 /* For negative infinity swap the sense of the comparison. */
5169 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5170 if (neg)
5171 code = swap_tree_comparison (code);
5173 switch (code)
5175 case GT_EXPR:
5176 /* x > +Inf is always false, if with ignore sNANs. */
5177 if (HONOR_SNANS (mode))
5178 return NULL_TREE;
5179 return omit_one_operand (type, integer_zero_node, arg0);
5181 case LE_EXPR:
5182 /* x <= +Inf is always true, if we don't case about NaNs. */
5183 if (! HONOR_NANS (mode))
5184 return omit_one_operand (type, integer_one_node, arg0);
5186 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
5187 if (lang_hooks.decls.global_bindings_p () == 0
5188 && ! CONTAINS_PLACEHOLDER_P (arg0))
5190 arg0 = save_expr (arg0);
5191 return fold (build2 (EQ_EXPR, type, arg0, arg0));
5193 break;
5195 case EQ_EXPR:
5196 case GE_EXPR:
5197 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
5198 real_maxval (&max, neg, mode);
5199 return fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5200 arg0, build_real (TREE_TYPE (arg0), max)));
5202 case LT_EXPR:
5203 /* x < +Inf is always equal to x <= DBL_MAX. */
5204 real_maxval (&max, neg, mode);
5205 return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5206 arg0, build_real (TREE_TYPE (arg0), max)));
5208 case NE_EXPR:
5209 /* x != +Inf is always equal to !(x > DBL_MAX). */
5210 real_maxval (&max, neg, mode);
5211 if (! HONOR_NANS (mode))
5212 return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5213 arg0, build_real (TREE_TYPE (arg0), max)));
5214 temp = fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5215 arg0, build_real (TREE_TYPE (arg0), max)));
5216 return fold (build1 (TRUTH_NOT_EXPR, type, temp));
5218 default:
5219 break;
5222 return NULL_TREE;
5225 /* Subroutine of fold() that optimizes comparisons of a division by
5226 a nonzero integer constant against an integer constant, i.e.
5227 X/C1 op C2.
5229 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5230 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5231 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5233 The function returns the constant folded tree if a simplification
5234 can be made, and NULL_TREE otherwise. */
5236 static tree
5237 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5239 tree prod, tmp, hi, lo;
5240 tree arg00 = TREE_OPERAND (arg0, 0);
5241 tree arg01 = TREE_OPERAND (arg0, 1);
5242 unsigned HOST_WIDE_INT lpart;
5243 HOST_WIDE_INT hpart;
5244 int overflow;
5246 /* We have to do this the hard way to detect unsigned overflow.
5247 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
5248 overflow = mul_double (TREE_INT_CST_LOW (arg01),
5249 TREE_INT_CST_HIGH (arg01),
5250 TREE_INT_CST_LOW (arg1),
5251 TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
5252 prod = build_int_2 (lpart, hpart);
5253 TREE_TYPE (prod) = TREE_TYPE (arg00);
5254 TREE_OVERFLOW (prod) = force_fit_type (prod, overflow)
5255 || TREE_INT_CST_HIGH (prod) != hpart
5256 || TREE_INT_CST_LOW (prod) != lpart;
5257 TREE_CONSTANT_OVERFLOW (prod) = TREE_OVERFLOW (prod);
5259 if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
5261 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5262 lo = prod;
5264 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
5265 overflow = add_double (TREE_INT_CST_LOW (prod),
5266 TREE_INT_CST_HIGH (prod),
5267 TREE_INT_CST_LOW (tmp),
5268 TREE_INT_CST_HIGH (tmp),
5269 &lpart, &hpart);
5270 hi = build_int_2 (lpart, hpart);
5271 TREE_TYPE (hi) = TREE_TYPE (arg00);
5272 TREE_OVERFLOW (hi) = force_fit_type (hi, overflow)
5273 || TREE_INT_CST_HIGH (hi) != hpart
5274 || TREE_INT_CST_LOW (hi) != lpart
5275 || TREE_OVERFLOW (prod);
5276 TREE_CONSTANT_OVERFLOW (hi) = TREE_OVERFLOW (hi);
5278 else if (tree_int_cst_sgn (arg01) >= 0)
5280 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5281 switch (tree_int_cst_sgn (arg1))
5283 case -1:
5284 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5285 hi = prod;
5286 break;
5288 case 0:
5289 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
5290 hi = tmp;
5291 break;
5293 case 1:
5294 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5295 lo = prod;
5296 break;
5298 default:
5299 abort ();
5302 else
5304 tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
5305 switch (tree_int_cst_sgn (arg1))
5307 case -1:
5308 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5309 lo = prod;
5310 break;
5312 case 0:
5313 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
5314 lo = tmp;
5315 break;
5317 case 1:
5318 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5319 hi = prod;
5320 break;
5322 default:
5323 abort ();
5327 switch (code)
5329 case EQ_EXPR:
5330 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5331 return omit_one_operand (type, integer_zero_node, arg00);
5332 if (TREE_OVERFLOW (hi))
5333 return fold (build2 (GE_EXPR, type, arg00, lo));
5334 if (TREE_OVERFLOW (lo))
5335 return fold (build2 (LE_EXPR, type, arg00, hi));
5336 return build_range_check (type, arg00, 1, lo, hi);
5338 case NE_EXPR:
5339 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5340 return omit_one_operand (type, integer_one_node, arg00);
5341 if (TREE_OVERFLOW (hi))
5342 return fold (build2 (LT_EXPR, type, arg00, lo));
5343 if (TREE_OVERFLOW (lo))
5344 return fold (build2 (GT_EXPR, type, arg00, hi));
5345 return build_range_check (type, arg00, 0, lo, hi);
5347 case LT_EXPR:
5348 if (TREE_OVERFLOW (lo))
5349 return omit_one_operand (type, integer_zero_node, arg00);
5350 return fold (build2 (LT_EXPR, type, arg00, lo));
5352 case LE_EXPR:
5353 if (TREE_OVERFLOW (hi))
5354 return omit_one_operand (type, integer_one_node, arg00);
5355 return fold (build2 (LE_EXPR, type, arg00, hi));
5357 case GT_EXPR:
5358 if (TREE_OVERFLOW (hi))
5359 return omit_one_operand (type, integer_zero_node, arg00);
5360 return fold (build2 (GT_EXPR, type, arg00, hi));
5362 case GE_EXPR:
5363 if (TREE_OVERFLOW (lo))
5364 return omit_one_operand (type, integer_one_node, arg00);
5365 return fold (build2 (GE_EXPR, type, arg00, lo));
5367 default:
5368 break;
5371 return NULL_TREE;
5375 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5376 equality/inequality test, then return a simplified form of
5377 the test using shifts and logical operations. Otherwise return
5378 NULL. TYPE is the desired result type. */
5380 tree
5381 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
5382 tree result_type)
5384 /* If this is a TRUTH_NOT_EXPR, it may have a single bit test inside
5385 operand 0. */
5386 if (code == TRUTH_NOT_EXPR)
5388 code = TREE_CODE (arg0);
5389 if (code != NE_EXPR && code != EQ_EXPR)
5390 return NULL_TREE;
5392 /* Extract the arguments of the EQ/NE. */
5393 arg1 = TREE_OPERAND (arg0, 1);
5394 arg0 = TREE_OPERAND (arg0, 0);
5396 /* This requires us to invert the code. */
5397 code = (code == EQ_EXPR ? NE_EXPR : EQ_EXPR);
5400 /* If this is testing a single bit, we can optimize the test. */
5401 if ((code == NE_EXPR || code == EQ_EXPR)
5402 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
5403 && integer_pow2p (TREE_OPERAND (arg0, 1)))
5405 tree inner = TREE_OPERAND (arg0, 0);
5406 tree type = TREE_TYPE (arg0);
5407 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
5408 enum machine_mode operand_mode = TYPE_MODE (type);
5409 int ops_unsigned;
5410 tree signed_type, unsigned_type, intermediate_type;
5411 tree arg00;
5413 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5414 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5415 arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
5416 if (arg00 != NULL_TREE
5417 /* This is only a win if casting to a signed type is cheap,
5418 i.e. when arg00's type is not a partial mode. */
5419 && TYPE_PRECISION (TREE_TYPE (arg00))
5420 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
5422 tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
5423 return fold (build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
5424 result_type, fold_convert (stype, arg00),
5425 fold_convert (stype, integer_zero_node)));
5428 /* Otherwise we have (A & C) != 0 where C is a single bit,
5429 convert that into ((A >> C2) & 1). Where C2 = log2(C).
5430 Similarly for (A & C) == 0. */
5432 /* If INNER is a right shift of a constant and it plus BITNUM does
5433 not overflow, adjust BITNUM and INNER. */
5434 if (TREE_CODE (inner) == RSHIFT_EXPR
5435 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
5436 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
5437 && bitnum < TYPE_PRECISION (type)
5438 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
5439 bitnum - TYPE_PRECISION (type)))
5441 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
5442 inner = TREE_OPERAND (inner, 0);
5445 /* If we are going to be able to omit the AND below, we must do our
5446 operations as unsigned. If we must use the AND, we have a choice.
5447 Normally unsigned is faster, but for some machines signed is. */
5448 #ifdef LOAD_EXTEND_OP
5449 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND ? 0 : 1);
5450 #else
5451 ops_unsigned = 1;
5452 #endif
5454 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
5455 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
5456 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
5457 inner = fold_convert (intermediate_type, inner);
5459 if (bitnum != 0)
5460 inner = build2 (RSHIFT_EXPR, intermediate_type,
5461 inner, size_int (bitnum));
5463 if (code == EQ_EXPR)
5464 inner = build2 (BIT_XOR_EXPR, intermediate_type,
5465 inner, integer_one_node);
5467 /* Put the AND last so it can combine with more things. */
5468 inner = build2 (BIT_AND_EXPR, intermediate_type,
5469 inner, integer_one_node);
5471 /* Make sure to return the proper type. */
5472 inner = fold_convert (result_type, inner);
5474 return inner;
5476 return NULL_TREE;
5479 /* Check whether we are allowed to reorder operands arg0 and arg1,
5480 such that the evaluation of arg1 occurs before arg0. */
5482 static bool
5483 reorder_operands_p (tree arg0, tree arg1)
5485 if (! flag_evaluation_order)
5486 return true;
5487 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
5488 return true;
5489 return ! TREE_SIDE_EFFECTS (arg0)
5490 && ! TREE_SIDE_EFFECTS (arg1);
5493 /* Test whether it is preferable two swap two operands, ARG0 and
5494 ARG1, for example because ARG0 is an integer constant and ARG1
5495 isn't. If REORDER is true, only recommend swapping if we can
5496 evaluate the operands in reverse order. */
5498 static bool
5499 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
5501 STRIP_SIGN_NOPS (arg0);
5502 STRIP_SIGN_NOPS (arg1);
5504 if (TREE_CODE (arg1) == INTEGER_CST)
5505 return 0;
5506 if (TREE_CODE (arg0) == INTEGER_CST)
5507 return 1;
5509 if (TREE_CODE (arg1) == REAL_CST)
5510 return 0;
5511 if (TREE_CODE (arg0) == REAL_CST)
5512 return 1;
5514 if (TREE_CODE (arg1) == COMPLEX_CST)
5515 return 0;
5516 if (TREE_CODE (arg0) == COMPLEX_CST)
5517 return 1;
5519 if (TREE_CONSTANT (arg1))
5520 return 0;
5521 if (TREE_CONSTANT (arg0))
5522 return 1;
5524 if (optimize_size)
5525 return 0;
5527 if (reorder && flag_evaluation_order
5528 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5529 return 0;
5531 if (DECL_P (arg1))
5532 return 0;
5533 if (DECL_P (arg0))
5534 return 1;
5536 if (reorder && flag_evaluation_order
5537 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5538 return 0;
5540 if (DECL_P (arg1))
5541 return 0;
5542 if (DECL_P (arg0))
5543 return 1;
5545 return 0;
5548 /* Perform constant folding and related simplification of EXPR.
5549 The related simplifications include x*1 => x, x*0 => 0, etc.,
5550 and application of the associative law.
5551 NOP_EXPR conversions may be removed freely (as long as we
5552 are careful not to change the type of the overall expression).
5553 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
5554 but we can constant-fold them if they have constant operands. */
5556 #ifdef ENABLE_FOLD_CHECKING
5557 # define fold(x) fold_1 (x)
5558 static tree fold_1 (tree);
5559 static
5560 #endif
5561 tree
5562 fold (tree expr)
5564 const tree t = expr;
5565 const tree type = TREE_TYPE (expr);
5566 tree t1 = NULL_TREE;
5567 tree tem;
5568 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
5569 enum tree_code code = TREE_CODE (t);
5570 int kind = TREE_CODE_CLASS (code);
5572 /* WINS will be nonzero when the switch is done
5573 if all operands are constant. */
5574 int wins = 1;
5576 /* Don't try to process an RTL_EXPR since its operands aren't trees.
5577 Likewise for a SAVE_EXPR that's already been evaluated. */
5578 if (code == RTL_EXPR || (code == SAVE_EXPR && SAVE_EXPR_RTL (t) != 0))
5579 return t;
5581 /* Return right away if a constant. */
5582 if (kind == 'c')
5583 return t;
5585 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
5587 tree subop;
5589 /* Special case for conversion ops that can have fixed point args. */
5590 arg0 = TREE_OPERAND (t, 0);
5592 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
5593 if (arg0 != 0)
5594 STRIP_SIGN_NOPS (arg0);
5596 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
5597 subop = TREE_REALPART (arg0);
5598 else
5599 subop = arg0;
5601 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
5602 && TREE_CODE (subop) != REAL_CST)
5603 /* Note that TREE_CONSTANT isn't enough:
5604 static var addresses are constant but we can't
5605 do arithmetic on them. */
5606 wins = 0;
5608 else if (IS_EXPR_CODE_CLASS (kind))
5610 int len = first_rtl_op (code);
5611 int i;
5612 for (i = 0; i < len; i++)
5614 tree op = TREE_OPERAND (t, i);
5615 tree subop;
5617 if (op == 0)
5618 continue; /* Valid for CALL_EXPR, at least. */
5620 /* Strip any conversions that don't change the mode. This is
5621 safe for every expression, except for a comparison expression
5622 because its signedness is derived from its operands. So, in
5623 the latter case, only strip conversions that don't change the
5624 signedness.
5626 Note that this is done as an internal manipulation within the
5627 constant folder, in order to find the simplest representation
5628 of the arguments so that their form can be studied. In any
5629 cases, the appropriate type conversions should be put back in
5630 the tree that will get out of the constant folder. */
5631 if (kind == '<')
5632 STRIP_SIGN_NOPS (op);
5633 else
5634 STRIP_NOPS (op);
5636 if (TREE_CODE (op) == COMPLEX_CST)
5637 subop = TREE_REALPART (op);
5638 else
5639 subop = op;
5641 if (TREE_CODE (subop) != INTEGER_CST
5642 && TREE_CODE (subop) != REAL_CST)
5643 /* Note that TREE_CONSTANT isn't enough:
5644 static var addresses are constant but we can't
5645 do arithmetic on them. */
5646 wins = 0;
5648 if (i == 0)
5649 arg0 = op;
5650 else if (i == 1)
5651 arg1 = op;
5655 /* If this is a commutative operation, and ARG0 is a constant, move it
5656 to ARG1 to reduce the number of tests below. */
5657 if (commutative_tree_code (code)
5658 && tree_swap_operands_p (arg0, arg1, true))
5659 return fold (build2 (code, type, TREE_OPERAND (t, 1),
5660 TREE_OPERAND (t, 0)));
5662 /* Now WINS is set as described above,
5663 ARG0 is the first operand of EXPR,
5664 and ARG1 is the second operand (if it has more than one operand).
5666 First check for cases where an arithmetic operation is applied to a
5667 compound, conditional, or comparison operation. Push the arithmetic
5668 operation inside the compound or conditional to see if any folding
5669 can then be done. Convert comparison to conditional for this purpose.
5670 The also optimizes non-constant cases that used to be done in
5671 expand_expr.
5673 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
5674 one of the operands is a comparison and the other is a comparison, a
5675 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
5676 code below would make the expression more complex. Change it to a
5677 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
5678 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
5680 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
5681 || code == EQ_EXPR || code == NE_EXPR)
5682 && ((truth_value_p (TREE_CODE (arg0))
5683 && (truth_value_p (TREE_CODE (arg1))
5684 || (TREE_CODE (arg1) == BIT_AND_EXPR
5685 && integer_onep (TREE_OPERAND (arg1, 1)))))
5686 || (truth_value_p (TREE_CODE (arg1))
5687 && (truth_value_p (TREE_CODE (arg0))
5688 || (TREE_CODE (arg0) == BIT_AND_EXPR
5689 && integer_onep (TREE_OPERAND (arg0, 1)))))))
5691 tem = fold (build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
5692 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
5693 : TRUTH_XOR_EXPR,
5694 type, fold_convert (boolean_type_node, arg0),
5695 fold_convert (boolean_type_node, arg1)));
5697 if (code == EQ_EXPR)
5698 tem = invert_truthvalue (tem);
5700 return tem;
5703 if (TREE_CODE_CLASS (code) == '1')
5705 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5706 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5707 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
5708 else if (TREE_CODE (arg0) == COND_EXPR)
5710 tree arg01 = TREE_OPERAND (arg0, 1);
5711 tree arg02 = TREE_OPERAND (arg0, 2);
5712 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
5713 arg01 = fold (build1 (code, type, arg01));
5714 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
5715 arg02 = fold (build1 (code, type, arg02));
5716 tem = fold (build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
5717 arg01, arg02));
5719 /* If this was a conversion, and all we did was to move into
5720 inside the COND_EXPR, bring it back out. But leave it if
5721 it is a conversion from integer to integer and the
5722 result precision is no wider than a word since such a
5723 conversion is cheap and may be optimized away by combine,
5724 while it couldn't if it were outside the COND_EXPR. Then return
5725 so we don't get into an infinite recursion loop taking the
5726 conversion out and then back in. */
5728 if ((code == NOP_EXPR || code == CONVERT_EXPR
5729 || code == NON_LVALUE_EXPR)
5730 && TREE_CODE (tem) == COND_EXPR
5731 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
5732 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
5733 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
5734 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
5735 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
5736 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
5737 && ! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
5738 && (INTEGRAL_TYPE_P
5739 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
5740 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD))
5741 tem = build1 (code, type,
5742 build3 (COND_EXPR,
5743 TREE_TYPE (TREE_OPERAND
5744 (TREE_OPERAND (tem, 1), 0)),
5745 TREE_OPERAND (tem, 0),
5746 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
5747 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
5748 return tem;
5750 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5752 if (TREE_CODE (type) == BOOLEAN_TYPE)
5754 arg0 = copy_node (arg0);
5755 TREE_TYPE (arg0) = type;
5756 return arg0;
5758 else if (TREE_CODE (type) != INTEGER_TYPE)
5759 return fold (build3 (COND_EXPR, type, arg0,
5760 fold (build1 (code, type,
5761 integer_one_node)),
5762 fold (build1 (code, type,
5763 integer_zero_node))));
5766 else if (TREE_CODE_CLASS (code) == '<'
5767 && TREE_CODE (arg0) == COMPOUND_EXPR)
5768 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5769 fold (build2 (code, type, TREE_OPERAND (arg0, 1), arg1)));
5770 else if (TREE_CODE_CLASS (code) == '<'
5771 && TREE_CODE (arg1) == COMPOUND_EXPR)
5772 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5773 fold (build2 (code, type, arg0, TREE_OPERAND (arg1, 1))));
5774 else if (TREE_CODE_CLASS (code) == '2'
5775 || TREE_CODE_CLASS (code) == '<')
5777 if (TREE_CODE (arg0) == COMPOUND_EXPR)
5778 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
5779 fold (build2 (code, type, TREE_OPERAND (arg0, 1),
5780 arg1)));
5781 if (TREE_CODE (arg1) == COMPOUND_EXPR
5782 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
5783 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
5784 fold (build2 (code, type,
5785 arg0, TREE_OPERAND (arg1, 1))));
5787 if (TREE_CODE (arg0) == COND_EXPR
5788 || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
5790 tem = fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
5791 /*cond_first_p=*/1);
5792 if (tem != NULL_TREE)
5793 return tem;
5796 if (TREE_CODE (arg1) == COND_EXPR
5797 || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
5799 tem = fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
5800 /*cond_first_p=*/0);
5801 if (tem != NULL_TREE)
5802 return tem;
5806 switch (code)
5808 case CONST_DECL:
5809 return fold (DECL_INITIAL (t));
5811 case NOP_EXPR:
5812 case FLOAT_EXPR:
5813 case CONVERT_EXPR:
5814 case FIX_TRUNC_EXPR:
5815 case FIX_CEIL_EXPR:
5816 case FIX_FLOOR_EXPR:
5817 case FIX_ROUND_EXPR:
5818 if (TREE_TYPE (TREE_OPERAND (t, 0)) == type)
5819 return TREE_OPERAND (t, 0);
5821 /* Handle cases of two conversions in a row. */
5822 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
5823 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
5825 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5826 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
5827 int inside_int = INTEGRAL_TYPE_P (inside_type);
5828 int inside_ptr = POINTER_TYPE_P (inside_type);
5829 int inside_float = FLOAT_TYPE_P (inside_type);
5830 unsigned int inside_prec = TYPE_PRECISION (inside_type);
5831 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
5832 int inter_int = INTEGRAL_TYPE_P (inter_type);
5833 int inter_ptr = POINTER_TYPE_P (inter_type);
5834 int inter_float = FLOAT_TYPE_P (inter_type);
5835 unsigned int inter_prec = TYPE_PRECISION (inter_type);
5836 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
5837 int final_int = INTEGRAL_TYPE_P (type);
5838 int final_ptr = POINTER_TYPE_P (type);
5839 int final_float = FLOAT_TYPE_P (type);
5840 unsigned int final_prec = TYPE_PRECISION (type);
5841 int final_unsignedp = TYPE_UNSIGNED (type);
5843 /* In addition to the cases of two conversions in a row
5844 handled below, if we are converting something to its own
5845 type via an object of identical or wider precision, neither
5846 conversion is needed. */
5847 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
5848 && ((inter_int && final_int) || (inter_float && final_float))
5849 && inter_prec >= final_prec)
5850 return fold (build1 (code, type,
5851 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5853 /* Likewise, if the intermediate and final types are either both
5854 float or both integer, we don't need the middle conversion if
5855 it is wider than the final type and doesn't change the signedness
5856 (for integers). Avoid this if the final type is a pointer
5857 since then we sometimes need the inner conversion. Likewise if
5858 the outer has a precision not equal to the size of its mode. */
5859 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
5860 || (inter_float && inside_float))
5861 && inter_prec >= inside_prec
5862 && (inter_float || inter_unsignedp == inside_unsignedp)
5863 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
5864 && TYPE_MODE (type) == TYPE_MODE (inter_type))
5865 && ! final_ptr)
5866 return fold (build1 (code, type,
5867 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5869 /* If we have a sign-extension of a zero-extended value, we can
5870 replace that by a single zero-extension. */
5871 if (inside_int && inter_int && final_int
5872 && inside_prec < inter_prec && inter_prec < final_prec
5873 && inside_unsignedp && !inter_unsignedp)
5874 return fold (build1 (code, type,
5875 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5877 /* Two conversions in a row are not needed unless:
5878 - some conversion is floating-point (overstrict for now), or
5879 - the intermediate type is narrower than both initial and
5880 final, or
5881 - the intermediate type and innermost type differ in signedness,
5882 and the outermost type is wider than the intermediate, or
5883 - the initial type is a pointer type and the precisions of the
5884 intermediate and final types differ, or
5885 - the final type is a pointer type and the precisions of the
5886 initial and intermediate types differ. */
5887 if (! inside_float && ! inter_float && ! final_float
5888 && (inter_prec > inside_prec || inter_prec > final_prec)
5889 && ! (inside_int && inter_int
5890 && inter_unsignedp != inside_unsignedp
5891 && inter_prec < final_prec)
5892 && ((inter_unsignedp && inter_prec > inside_prec)
5893 == (final_unsignedp && final_prec > inter_prec))
5894 && ! (inside_ptr && inter_prec != final_prec)
5895 && ! (final_ptr && inside_prec != inter_prec)
5896 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
5897 && TYPE_MODE (type) == TYPE_MODE (inter_type))
5898 && ! final_ptr)
5899 return fold (build1 (code, type,
5900 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
5903 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
5904 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
5905 /* Detect assigning a bitfield. */
5906 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
5907 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
5909 /* Don't leave an assignment inside a conversion
5910 unless assigning a bitfield. */
5911 tree prev = TREE_OPERAND (t, 0);
5912 tem = copy_node (t);
5913 TREE_OPERAND (tem, 0) = TREE_OPERAND (prev, 1);
5914 /* First do the assignment, then return converted constant. */
5915 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), prev, fold (tem));
5916 TREE_NO_WARNING (tem) = 1;
5917 TREE_USED (tem) = 1;
5918 return tem;
5921 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
5922 constants (if x has signed type, the sign bit cannot be set
5923 in c). This folds extension into the BIT_AND_EXPR. */
5924 if (INTEGRAL_TYPE_P (type)
5925 && TREE_CODE (type) != BOOLEAN_TYPE
5926 && TREE_CODE (TREE_OPERAND (t, 0)) == BIT_AND_EXPR
5927 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST)
5929 tree and = TREE_OPERAND (t, 0);
5930 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
5931 int change = 0;
5933 if (TYPE_UNSIGNED (TREE_TYPE (and))
5934 || (TYPE_PRECISION (type)
5935 <= TYPE_PRECISION (TREE_TYPE (and))))
5936 change = 1;
5937 else if (TYPE_PRECISION (TREE_TYPE (and1))
5938 <= HOST_BITS_PER_WIDE_INT
5939 && host_integerp (and1, 1))
5941 unsigned HOST_WIDE_INT cst;
5943 cst = tree_low_cst (and1, 1);
5944 cst &= (HOST_WIDE_INT) -1
5945 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
5946 change = (cst == 0);
5947 #ifdef LOAD_EXTEND_OP
5948 if (change
5949 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
5950 == ZERO_EXTEND))
5952 tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
5953 and0 = fold_convert (uns, and0);
5954 and1 = fold_convert (uns, and1);
5956 #endif
5958 if (change)
5959 return fold (build2 (BIT_AND_EXPR, type,
5960 fold_convert (type, and0),
5961 fold_convert (type, and1)));
5964 /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
5965 T2 being pointers to types of the same size. */
5966 if (POINTER_TYPE_P (TREE_TYPE (t))
5967 && TREE_CODE_CLASS (TREE_CODE (arg0)) == '2'
5968 && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
5969 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
5971 tree arg00 = TREE_OPERAND (arg0, 0);
5972 tree t0 = TREE_TYPE (t);
5973 tree t1 = TREE_TYPE (arg00);
5974 tree tt0 = TREE_TYPE (t0);
5975 tree tt1 = TREE_TYPE (t1);
5976 tree s0 = TYPE_SIZE (tt0);
5977 tree s1 = TYPE_SIZE (tt1);
5979 if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
5980 return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
5981 TREE_OPERAND (arg0, 1));
5984 tem = fold_convert_const (code, type, arg0);
5985 return tem ? tem : t;
5987 case VIEW_CONVERT_EXPR:
5988 if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
5989 return build1 (VIEW_CONVERT_EXPR, type,
5990 TREE_OPERAND (TREE_OPERAND (t, 0), 0));
5991 return t;
5993 case COMPONENT_REF:
5994 if (TREE_CODE (arg0) == CONSTRUCTOR
5995 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
5997 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
5998 if (m)
5999 return TREE_VALUE (m);
6001 return t;
6003 case RANGE_EXPR:
6004 if (TREE_CONSTANT (t) != wins)
6006 tem = copy_node (t);
6007 TREE_CONSTANT (tem) = wins;
6008 TREE_INVARIANT (tem) = wins;
6009 return tem;
6011 return t;
6013 case NEGATE_EXPR:
6014 if (negate_expr_p (arg0))
6015 return fold_convert (type, negate_expr (arg0));
6016 return t;
6018 case ABS_EXPR:
6019 if (wins
6020 && (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST))
6021 return fold_abs_const (arg0, type);
6022 else if (TREE_CODE (arg0) == NEGATE_EXPR)
6023 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)));
6024 /* Convert fabs((double)float) into (double)fabsf(float). */
6025 else if (TREE_CODE (arg0) == NOP_EXPR
6026 && TREE_CODE (type) == REAL_TYPE)
6028 tree targ0 = strip_float_extensions (arg0);
6029 if (targ0 != arg0)
6030 return fold_convert (type, fold (build1 (ABS_EXPR,
6031 TREE_TYPE (targ0),
6032 targ0)));
6034 else if (tree_expr_nonnegative_p (arg0))
6035 return arg0;
6036 return t;
6038 case CONJ_EXPR:
6039 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6040 return fold_convert (type, arg0);
6041 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6042 return build2 (COMPLEX_EXPR, type,
6043 TREE_OPERAND (arg0, 0),
6044 negate_expr (TREE_OPERAND (arg0, 1)));
6045 else if (TREE_CODE (arg0) == COMPLEX_CST)
6046 return build_complex (type, TREE_REALPART (arg0),
6047 negate_expr (TREE_IMAGPART (arg0)));
6048 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6049 return fold (build2 (TREE_CODE (arg0), type,
6050 fold (build1 (CONJ_EXPR, type,
6051 TREE_OPERAND (arg0, 0))),
6052 fold (build1 (CONJ_EXPR, type,
6053 TREE_OPERAND (arg0, 1)))));
6054 else if (TREE_CODE (arg0) == CONJ_EXPR)
6055 return TREE_OPERAND (arg0, 0);
6056 return t;
6058 case BIT_NOT_EXPR:
6059 if (wins)
6061 tem = build_int_2 (~ TREE_INT_CST_LOW (arg0),
6062 ~ TREE_INT_CST_HIGH (arg0));
6063 TREE_TYPE (tem) = type;
6064 force_fit_type (tem, 0);
6065 TREE_OVERFLOW (tem) = TREE_OVERFLOW (arg0);
6066 TREE_CONSTANT_OVERFLOW (tem) = TREE_CONSTANT_OVERFLOW (arg0);
6067 return tem;
6069 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
6070 return TREE_OPERAND (arg0, 0);
6071 return t;
6073 case PLUS_EXPR:
6074 /* A + (-B) -> A - B */
6075 if (TREE_CODE (arg1) == NEGATE_EXPR)
6076 return fold (build2 (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6077 /* (-A) + B -> B - A */
6078 if (TREE_CODE (arg0) == NEGATE_EXPR
6079 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
6080 return fold (build2 (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
6081 if (! FLOAT_TYPE_P (type))
6083 if (integer_zerop (arg1))
6084 return non_lvalue (fold_convert (type, arg0));
6086 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
6087 with a constant, and the two constants have no bits in common,
6088 we should treat this as a BIT_IOR_EXPR since this may produce more
6089 simplifications. */
6090 if (TREE_CODE (arg0) == BIT_AND_EXPR
6091 && TREE_CODE (arg1) == BIT_AND_EXPR
6092 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6093 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6094 && integer_zerop (const_binop (BIT_AND_EXPR,
6095 TREE_OPERAND (arg0, 1),
6096 TREE_OPERAND (arg1, 1), 0)))
6098 code = BIT_IOR_EXPR;
6099 goto bit_ior;
6102 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
6103 (plus (plus (mult) (mult)) (foo)) so that we can
6104 take advantage of the factoring cases below. */
6105 if ((TREE_CODE (arg0) == PLUS_EXPR
6106 && TREE_CODE (arg1) == MULT_EXPR)
6107 || (TREE_CODE (arg1) == PLUS_EXPR
6108 && TREE_CODE (arg0) == MULT_EXPR))
6110 tree parg0, parg1, parg, marg;
6112 if (TREE_CODE (arg0) == PLUS_EXPR)
6113 parg = arg0, marg = arg1;
6114 else
6115 parg = arg1, marg = arg0;
6116 parg0 = TREE_OPERAND (parg, 0);
6117 parg1 = TREE_OPERAND (parg, 1);
6118 STRIP_NOPS (parg0);
6119 STRIP_NOPS (parg1);
6121 if (TREE_CODE (parg0) == MULT_EXPR
6122 && TREE_CODE (parg1) != MULT_EXPR)
6123 return fold (build2 (PLUS_EXPR, type,
6124 fold (build2 (PLUS_EXPR, type,
6125 fold_convert (type, parg0),
6126 fold_convert (type, marg))),
6127 fold_convert (type, parg1)));
6128 if (TREE_CODE (parg0) != MULT_EXPR
6129 && TREE_CODE (parg1) == MULT_EXPR)
6130 return fold (build2 (PLUS_EXPR, type,
6131 fold (build2 (PLUS_EXPR, type,
6132 fold_convert (type, parg1),
6133 fold_convert (type, marg))),
6134 fold_convert (type, parg0)));
6137 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
6139 tree arg00, arg01, arg10, arg11;
6140 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6142 /* (A * C) + (B * C) -> (A+B) * C.
6143 We are most concerned about the case where C is a constant,
6144 but other combinations show up during loop reduction. Since
6145 it is not difficult, try all four possibilities. */
6147 arg00 = TREE_OPERAND (arg0, 0);
6148 arg01 = TREE_OPERAND (arg0, 1);
6149 arg10 = TREE_OPERAND (arg1, 0);
6150 arg11 = TREE_OPERAND (arg1, 1);
6151 same = NULL_TREE;
6153 if (operand_equal_p (arg01, arg11, 0))
6154 same = arg01, alt0 = arg00, alt1 = arg10;
6155 else if (operand_equal_p (arg00, arg10, 0))
6156 same = arg00, alt0 = arg01, alt1 = arg11;
6157 else if (operand_equal_p (arg00, arg11, 0))
6158 same = arg00, alt0 = arg01, alt1 = arg10;
6159 else if (operand_equal_p (arg01, arg10, 0))
6160 same = arg01, alt0 = arg00, alt1 = arg11;
6162 /* No identical multiplicands; see if we can find a common
6163 power-of-two factor in non-power-of-two multiplies. This
6164 can help in multi-dimensional array access. */
6165 else if (TREE_CODE (arg01) == INTEGER_CST
6166 && TREE_CODE (arg11) == INTEGER_CST
6167 && TREE_INT_CST_HIGH (arg01) == 0
6168 && TREE_INT_CST_HIGH (arg11) == 0)
6170 HOST_WIDE_INT int01, int11, tmp;
6171 int01 = TREE_INT_CST_LOW (arg01);
6172 int11 = TREE_INT_CST_LOW (arg11);
6174 /* Move min of absolute values to int11. */
6175 if ((int01 >= 0 ? int01 : -int01)
6176 < (int11 >= 0 ? int11 : -int11))
6178 tmp = int01, int01 = int11, int11 = tmp;
6179 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6180 alt0 = arg01, arg01 = arg11, arg11 = alt0;
6183 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
6185 alt0 = fold (build2 (MULT_EXPR, type, arg00,
6186 build_int_2 (int01 / int11, 0)));
6187 alt1 = arg10;
6188 same = arg11;
6192 if (same)
6193 return fold (build2 (MULT_EXPR, type,
6194 fold (build2 (PLUS_EXPR, type,
6195 alt0, alt1)),
6196 same));
6199 else
6201 /* See if ARG1 is zero and X + ARG1 reduces to X. */
6202 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
6203 return non_lvalue (fold_convert (type, arg0));
6205 /* Likewise if the operands are reversed. */
6206 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6207 return non_lvalue (fold_convert (type, arg1));
6209 /* Convert x+x into x*2.0. */
6210 if (operand_equal_p (arg0, arg1, 0)
6211 && SCALAR_FLOAT_TYPE_P (type))
6212 return fold (build2 (MULT_EXPR, type, arg0,
6213 build_real (type, dconst2)));
6215 /* Convert x*c+x into x*(c+1). */
6216 if (flag_unsafe_math_optimizations
6217 && TREE_CODE (arg0) == MULT_EXPR
6218 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6219 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6220 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
6222 REAL_VALUE_TYPE c;
6224 c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6225 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6226 return fold (build2 (MULT_EXPR, type, arg1,
6227 build_real (type, c)));
6230 /* Convert x+x*c into x*(c+1). */
6231 if (flag_unsafe_math_optimizations
6232 && TREE_CODE (arg1) == MULT_EXPR
6233 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6234 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6235 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
6237 REAL_VALUE_TYPE c;
6239 c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6240 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6241 return fold (build2 (MULT_EXPR, type, arg0,
6242 build_real (type, c)));
6245 /* Convert x*c1+x*c2 into x*(c1+c2). */
6246 if (flag_unsafe_math_optimizations
6247 && TREE_CODE (arg0) == MULT_EXPR
6248 && TREE_CODE (arg1) == MULT_EXPR
6249 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6250 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6251 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6252 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6253 && operand_equal_p (TREE_OPERAND (arg0, 0),
6254 TREE_OPERAND (arg1, 0), 0))
6256 REAL_VALUE_TYPE c1, c2;
6258 c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6259 c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6260 real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
6261 return fold (build2 (MULT_EXPR, type,
6262 TREE_OPERAND (arg0, 0),
6263 build_real (type, c1)));
6265 /* Convert a + (b*c + d*e) into (a + b*c) + d*e */
6266 if (flag_unsafe_math_optimizations
6267 && TREE_CODE (arg1) == PLUS_EXPR
6268 && TREE_CODE (arg0) != MULT_EXPR)
6270 tree tree10 = TREE_OPERAND (arg1, 0);
6271 tree tree11 = TREE_OPERAND (arg1, 1);
6272 if (TREE_CODE (tree11) == MULT_EXPR
6273 && TREE_CODE (tree10) == MULT_EXPR)
6275 tree tree0;
6276 tree0 = fold (build2 (PLUS_EXPR, type, arg0, tree10));
6277 return fold (build2 (PLUS_EXPR, type, tree0, tree11));
6280 /* Convert (b*c + d*e) + a into b*c + (d*e +a) */
6281 if (flag_unsafe_math_optimizations
6282 && TREE_CODE (arg0) == PLUS_EXPR
6283 && TREE_CODE (arg1) != MULT_EXPR)
6285 tree tree00 = TREE_OPERAND (arg0, 0);
6286 tree tree01 = TREE_OPERAND (arg0, 1);
6287 if (TREE_CODE (tree01) == MULT_EXPR
6288 && TREE_CODE (tree00) == MULT_EXPR)
6290 tree tree0;
6291 tree0 = fold (build2 (PLUS_EXPR, type, tree01, arg1));
6292 return fold (build2 (PLUS_EXPR, type, tree00, tree0));
6297 bit_rotate:
6298 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
6299 is a rotate of A by C1 bits. */
6300 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
6301 is a rotate of A by B bits. */
6303 enum tree_code code0, code1;
6304 code0 = TREE_CODE (arg0);
6305 code1 = TREE_CODE (arg1);
6306 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
6307 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
6308 && operand_equal_p (TREE_OPERAND (arg0, 0),
6309 TREE_OPERAND (arg1, 0), 0)
6310 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6312 tree tree01, tree11;
6313 enum tree_code code01, code11;
6315 tree01 = TREE_OPERAND (arg0, 1);
6316 tree11 = TREE_OPERAND (arg1, 1);
6317 STRIP_NOPS (tree01);
6318 STRIP_NOPS (tree11);
6319 code01 = TREE_CODE (tree01);
6320 code11 = TREE_CODE (tree11);
6321 if (code01 == INTEGER_CST
6322 && code11 == INTEGER_CST
6323 && TREE_INT_CST_HIGH (tree01) == 0
6324 && TREE_INT_CST_HIGH (tree11) == 0
6325 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
6326 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
6327 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
6328 code0 == LSHIFT_EXPR ? tree01 : tree11);
6329 else if (code11 == MINUS_EXPR)
6331 tree tree110, tree111;
6332 tree110 = TREE_OPERAND (tree11, 0);
6333 tree111 = TREE_OPERAND (tree11, 1);
6334 STRIP_NOPS (tree110);
6335 STRIP_NOPS (tree111);
6336 if (TREE_CODE (tree110) == INTEGER_CST
6337 && 0 == compare_tree_int (tree110,
6338 TYPE_PRECISION
6339 (TREE_TYPE (TREE_OPERAND
6340 (arg0, 0))))
6341 && operand_equal_p (tree01, tree111, 0))
6342 return build2 ((code0 == LSHIFT_EXPR
6343 ? LROTATE_EXPR
6344 : RROTATE_EXPR),
6345 type, TREE_OPERAND (arg0, 0), tree01);
6347 else if (code01 == MINUS_EXPR)
6349 tree tree010, tree011;
6350 tree010 = TREE_OPERAND (tree01, 0);
6351 tree011 = TREE_OPERAND (tree01, 1);
6352 STRIP_NOPS (tree010);
6353 STRIP_NOPS (tree011);
6354 if (TREE_CODE (tree010) == INTEGER_CST
6355 && 0 == compare_tree_int (tree010,
6356 TYPE_PRECISION
6357 (TREE_TYPE (TREE_OPERAND
6358 (arg0, 0))))
6359 && operand_equal_p (tree11, tree011, 0))
6360 return build2 ((code0 != LSHIFT_EXPR
6361 ? LROTATE_EXPR
6362 : RROTATE_EXPR),
6363 type, TREE_OPERAND (arg0, 0), tree11);
6368 associate:
6369 /* In most languages, can't associate operations on floats through
6370 parentheses. Rather than remember where the parentheses were, we
6371 don't associate floats at all, unless the user has specified
6372 -funsafe-math-optimizations. */
6374 if (! wins
6375 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
6377 tree var0, con0, lit0, minus_lit0;
6378 tree var1, con1, lit1, minus_lit1;
6380 /* Split both trees into variables, constants, and literals. Then
6381 associate each group together, the constants with literals,
6382 then the result with variables. This increases the chances of
6383 literals being recombined later and of generating relocatable
6384 expressions for the sum of a constant and literal. */
6385 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
6386 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
6387 code == MINUS_EXPR);
6389 /* Only do something if we found more than two objects. Otherwise,
6390 nothing has changed and we risk infinite recursion. */
6391 if (2 < ((var0 != 0) + (var1 != 0)
6392 + (con0 != 0) + (con1 != 0)
6393 + (lit0 != 0) + (lit1 != 0)
6394 + (minus_lit0 != 0) + (minus_lit1 != 0)))
6396 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
6397 if (code == MINUS_EXPR)
6398 code = PLUS_EXPR;
6400 var0 = associate_trees (var0, var1, code, type);
6401 con0 = associate_trees (con0, con1, code, type);
6402 lit0 = associate_trees (lit0, lit1, code, type);
6403 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
6405 /* Preserve the MINUS_EXPR if the negative part of the literal is
6406 greater than the positive part. Otherwise, the multiplicative
6407 folding code (i.e extract_muldiv) may be fooled in case
6408 unsigned constants are subtracted, like in the following
6409 example: ((X*2 + 4) - 8U)/2. */
6410 if (minus_lit0 && lit0)
6412 if (TREE_CODE (lit0) == INTEGER_CST
6413 && TREE_CODE (minus_lit0) == INTEGER_CST
6414 && tree_int_cst_lt (lit0, minus_lit0))
6416 minus_lit0 = associate_trees (minus_lit0, lit0,
6417 MINUS_EXPR, type);
6418 lit0 = 0;
6420 else
6422 lit0 = associate_trees (lit0, minus_lit0,
6423 MINUS_EXPR, type);
6424 minus_lit0 = 0;
6427 if (minus_lit0)
6429 if (con0 == 0)
6430 return fold_convert (type,
6431 associate_trees (var0, minus_lit0,
6432 MINUS_EXPR, type));
6433 else
6435 con0 = associate_trees (con0, minus_lit0,
6436 MINUS_EXPR, type);
6437 return fold_convert (type,
6438 associate_trees (var0, con0,
6439 PLUS_EXPR, type));
6443 con0 = associate_trees (con0, lit0, code, type);
6444 return fold_convert (type, associate_trees (var0, con0,
6445 code, type));
6449 binary:
6450 if (wins)
6451 t1 = const_binop (code, arg0, arg1, 0);
6452 if (t1 != NULL_TREE)
6454 /* The return value should always have
6455 the same type as the original expression. */
6456 if (TREE_TYPE (t1) != type)
6457 t1 = fold_convert (type, t1);
6459 return t1;
6461 return t;
6463 case MINUS_EXPR:
6464 /* A - (-B) -> A + B */
6465 if (TREE_CODE (arg1) == NEGATE_EXPR)
6466 return fold (build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6467 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
6468 if (TREE_CODE (arg0) == NEGATE_EXPR
6469 && (FLOAT_TYPE_P (type)
6470 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
6471 && negate_expr_p (arg1)
6472 && reorder_operands_p (arg0, arg1))
6473 return fold (build2 (MINUS_EXPR, type, negate_expr (arg1),
6474 TREE_OPERAND (arg0, 0)));
6476 if (! FLOAT_TYPE_P (type))
6478 if (! wins && integer_zerop (arg0))
6479 return negate_expr (fold_convert (type, arg1));
6480 if (integer_zerop (arg1))
6481 return non_lvalue (fold_convert (type, arg0));
6483 /* Fold A - (A & B) into ~B & A. */
6484 if (!TREE_SIDE_EFFECTS (arg0)
6485 && TREE_CODE (arg1) == BIT_AND_EXPR)
6487 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
6488 return fold (build2 (BIT_AND_EXPR, type,
6489 fold (build1 (BIT_NOT_EXPR, type,
6490 TREE_OPERAND (arg1, 0))),
6491 arg0));
6492 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
6493 return fold (build2 (BIT_AND_EXPR, type,
6494 fold (build1 (BIT_NOT_EXPR, type,
6495 TREE_OPERAND (arg1, 1))),
6496 arg0));
6499 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
6500 any power of 2 minus 1. */
6501 if (TREE_CODE (arg0) == BIT_AND_EXPR
6502 && TREE_CODE (arg1) == BIT_AND_EXPR
6503 && operand_equal_p (TREE_OPERAND (arg0, 0),
6504 TREE_OPERAND (arg1, 0), 0))
6506 tree mask0 = TREE_OPERAND (arg0, 1);
6507 tree mask1 = TREE_OPERAND (arg1, 1);
6508 tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
6510 if (operand_equal_p (tem, mask1, 0))
6512 tem = fold (build2 (BIT_XOR_EXPR, type,
6513 TREE_OPERAND (arg0, 0), mask1));
6514 return fold (build2 (MINUS_EXPR, type, tem, mask1));
6519 /* See if ARG1 is zero and X - ARG1 reduces to X. */
6520 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
6521 return non_lvalue (fold_convert (type, arg0));
6523 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
6524 ARG0 is zero and X + ARG0 reduces to X, since that would mean
6525 (-ARG1 + ARG0) reduces to -ARG1. */
6526 else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6527 return negate_expr (fold_convert (type, arg1));
6529 /* Fold &x - &x. This can happen from &x.foo - &x.
6530 This is unsafe for certain floats even in non-IEEE formats.
6531 In IEEE, it is unsafe because it does wrong for NaNs.
6532 Also note that operand_equal_p is always false if an operand
6533 is volatile. */
6535 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
6536 && operand_equal_p (arg0, arg1, 0))
6537 return fold_convert (type, integer_zero_node);
6539 /* A - B -> A + (-B) if B is easily negatable. */
6540 if (!wins && negate_expr_p (arg1)
6541 && (FLOAT_TYPE_P (type)
6542 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
6543 return fold (build2 (PLUS_EXPR, type, arg0, negate_expr (arg1)));
6545 if (TREE_CODE (arg0) == MULT_EXPR
6546 && TREE_CODE (arg1) == MULT_EXPR
6547 && (INTEGRAL_TYPE_P (type) || flag_unsafe_math_optimizations))
6549 /* (A * C) - (B * C) -> (A-B) * C. */
6550 if (operand_equal_p (TREE_OPERAND (arg0, 1),
6551 TREE_OPERAND (arg1, 1), 0))
6552 return fold (build2 (MULT_EXPR, type,
6553 fold (build2 (MINUS_EXPR, type,
6554 TREE_OPERAND (arg0, 0),
6555 TREE_OPERAND (arg1, 0))),
6556 TREE_OPERAND (arg0, 1)));
6557 /* (A * C1) - (A * C2) -> A * (C1-C2). */
6558 if (operand_equal_p (TREE_OPERAND (arg0, 0),
6559 TREE_OPERAND (arg1, 0), 0))
6560 return fold (build2 (MULT_EXPR, type,
6561 TREE_OPERAND (arg0, 0),
6562 fold (build2 (MINUS_EXPR, type,
6563 TREE_OPERAND (arg0, 1),
6564 TREE_OPERAND (arg1, 1)))));
6567 goto associate;
6569 case MULT_EXPR:
6570 /* (-A) * (-B) -> A * B */
6571 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6572 return fold (build2 (MULT_EXPR, type,
6573 TREE_OPERAND (arg0, 0),
6574 negate_expr (arg1)));
6575 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6576 return fold (build2 (MULT_EXPR, type,
6577 negate_expr (arg0),
6578 TREE_OPERAND (arg1, 0)));
6580 if (! FLOAT_TYPE_P (type))
6582 if (integer_zerop (arg1))
6583 return omit_one_operand (type, arg1, arg0);
6584 if (integer_onep (arg1))
6585 return non_lvalue (fold_convert (type, arg0));
6587 /* (a * (1 << b)) is (a << b) */
6588 if (TREE_CODE (arg1) == LSHIFT_EXPR
6589 && integer_onep (TREE_OPERAND (arg1, 0)))
6590 return fold (build2 (LSHIFT_EXPR, type, arg0,
6591 TREE_OPERAND (arg1, 1)));
6592 if (TREE_CODE (arg0) == LSHIFT_EXPR
6593 && integer_onep (TREE_OPERAND (arg0, 0)))
6594 return fold (build2 (LSHIFT_EXPR, type, arg1,
6595 TREE_OPERAND (arg0, 1)));
6597 if (TREE_CODE (arg1) == INTEGER_CST
6598 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0),
6599 fold_convert (type, arg1),
6600 code, NULL_TREE)))
6601 return fold_convert (type, tem);
6604 else
6606 /* Maybe fold x * 0 to 0. The expressions aren't the same
6607 when x is NaN, since x * 0 is also NaN. Nor are they the
6608 same in modes with signed zeros, since multiplying a
6609 negative value by 0 gives -0, not +0. */
6610 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
6611 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
6612 && real_zerop (arg1))
6613 return omit_one_operand (type, arg1, arg0);
6614 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
6615 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6616 && real_onep (arg1))
6617 return non_lvalue (fold_convert (type, arg0));
6619 /* Transform x * -1.0 into -x. */
6620 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6621 && real_minus_onep (arg1))
6622 return fold_convert (type, negate_expr (arg0));
6624 /* Convert (C1/X)*C2 into (C1*C2)/X. */
6625 if (flag_unsafe_math_optimizations
6626 && TREE_CODE (arg0) == RDIV_EXPR
6627 && TREE_CODE (arg1) == REAL_CST
6628 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
6630 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
6631 arg1, 0);
6632 if (tem)
6633 return fold (build2 (RDIV_EXPR, type, tem,
6634 TREE_OPERAND (arg0, 1)));
6637 if (flag_unsafe_math_optimizations)
6639 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
6640 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
6642 /* Optimizations of root(...)*root(...). */
6643 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
6645 tree rootfn, arg, arglist;
6646 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6647 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6649 /* Optimize sqrt(x)*sqrt(x) as x. */
6650 if (BUILTIN_SQRT_P (fcode0)
6651 && operand_equal_p (arg00, arg10, 0)
6652 && ! HONOR_SNANS (TYPE_MODE (type)))
6653 return arg00;
6655 /* Optimize root(x)*root(y) as root(x*y). */
6656 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6657 arg = fold (build2 (MULT_EXPR, type, arg00, arg10));
6658 arglist = build_tree_list (NULL_TREE, arg);
6659 return build_function_call_expr (rootfn, arglist);
6662 /* Optimize expN(x)*expN(y) as expN(x+y). */
6663 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
6665 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6666 tree arg = build2 (PLUS_EXPR, type,
6667 TREE_VALUE (TREE_OPERAND (arg0, 1)),
6668 TREE_VALUE (TREE_OPERAND (arg1, 1)));
6669 tree arglist = build_tree_list (NULL_TREE, fold (arg));
6670 return build_function_call_expr (expfn, arglist);
6673 /* Optimizations of pow(...)*pow(...). */
6674 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
6675 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
6676 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
6678 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6679 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6680 1)));
6681 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6682 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6683 1)));
6685 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
6686 if (operand_equal_p (arg01, arg11, 0))
6688 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6689 tree arg = build2 (MULT_EXPR, type, arg00, arg10);
6690 tree arglist = tree_cons (NULL_TREE, fold (arg),
6691 build_tree_list (NULL_TREE,
6692 arg01));
6693 return build_function_call_expr (powfn, arglist);
6696 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
6697 if (operand_equal_p (arg00, arg10, 0))
6699 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6700 tree arg = fold (build2 (PLUS_EXPR, type, arg01, arg11));
6701 tree arglist = tree_cons (NULL_TREE, arg00,
6702 build_tree_list (NULL_TREE,
6703 arg));
6704 return build_function_call_expr (powfn, arglist);
6708 /* Optimize tan(x)*cos(x) as sin(x). */
6709 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
6710 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
6711 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
6712 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
6713 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
6714 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
6715 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
6716 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
6718 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
6720 if (sinfn != NULL_TREE)
6721 return build_function_call_expr (sinfn,
6722 TREE_OPERAND (arg0, 1));
6725 /* Optimize x*pow(x,c) as pow(x,c+1). */
6726 if (fcode1 == BUILT_IN_POW
6727 || fcode1 == BUILT_IN_POWF
6728 || fcode1 == BUILT_IN_POWL)
6730 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6731 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
6732 1)));
6733 if (TREE_CODE (arg11) == REAL_CST
6734 && ! TREE_CONSTANT_OVERFLOW (arg11)
6735 && operand_equal_p (arg0, arg10, 0))
6737 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6738 REAL_VALUE_TYPE c;
6739 tree arg, arglist;
6741 c = TREE_REAL_CST (arg11);
6742 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6743 arg = build_real (type, c);
6744 arglist = build_tree_list (NULL_TREE, arg);
6745 arglist = tree_cons (NULL_TREE, arg0, arglist);
6746 return build_function_call_expr (powfn, arglist);
6750 /* Optimize pow(x,c)*x as pow(x,c+1). */
6751 if (fcode0 == BUILT_IN_POW
6752 || fcode0 == BUILT_IN_POWF
6753 || fcode0 == BUILT_IN_POWL)
6755 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
6756 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
6757 1)));
6758 if (TREE_CODE (arg01) == REAL_CST
6759 && ! TREE_CONSTANT_OVERFLOW (arg01)
6760 && operand_equal_p (arg1, arg00, 0))
6762 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
6763 REAL_VALUE_TYPE c;
6764 tree arg, arglist;
6766 c = TREE_REAL_CST (arg01);
6767 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6768 arg = build_real (type, c);
6769 arglist = build_tree_list (NULL_TREE, arg);
6770 arglist = tree_cons (NULL_TREE, arg1, arglist);
6771 return build_function_call_expr (powfn, arglist);
6775 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
6776 if (! optimize_size
6777 && operand_equal_p (arg0, arg1, 0))
6779 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
6781 if (powfn)
6783 tree arg = build_real (type, dconst2);
6784 tree arglist = build_tree_list (NULL_TREE, arg);
6785 arglist = tree_cons (NULL_TREE, arg0, arglist);
6786 return build_function_call_expr (powfn, arglist);
6791 goto associate;
6793 case BIT_IOR_EXPR:
6794 bit_ior:
6795 if (integer_all_onesp (arg1))
6796 return omit_one_operand (type, arg1, arg0);
6797 if (integer_zerop (arg1))
6798 return non_lvalue (fold_convert (type, arg0));
6799 if (operand_equal_p (arg0, arg1, 0))
6800 return non_lvalue (fold_convert (type, arg0));
6801 t1 = distribute_bit_expr (code, type, arg0, arg1);
6802 if (t1 != NULL_TREE)
6803 return t1;
6805 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
6807 This results in more efficient code for machines without a NAND
6808 instruction. Combine will canonicalize to the first form
6809 which will allow use of NAND instructions provided by the
6810 backend if they exist. */
6811 if (TREE_CODE (arg0) == BIT_NOT_EXPR
6812 && TREE_CODE (arg1) == BIT_NOT_EXPR)
6814 return fold (build1 (BIT_NOT_EXPR, type,
6815 build2 (BIT_AND_EXPR, type,
6816 TREE_OPERAND (arg0, 0),
6817 TREE_OPERAND (arg1, 0))));
6820 /* See if this can be simplified into a rotate first. If that
6821 is unsuccessful continue in the association code. */
6822 goto bit_rotate;
6824 case BIT_XOR_EXPR:
6825 if (integer_zerop (arg1))
6826 return non_lvalue (fold_convert (type, arg0));
6827 if (integer_all_onesp (arg1))
6828 return fold (build1 (BIT_NOT_EXPR, type, arg0));
6829 if (operand_equal_p (arg0, arg1, 0))
6830 return omit_one_operand (type, integer_zero_node, arg0);
6832 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
6833 with a constant, and the two constants have no bits in common,
6834 we should treat this as a BIT_IOR_EXPR since this may produce more
6835 simplifications. */
6836 if (TREE_CODE (arg0) == BIT_AND_EXPR
6837 && TREE_CODE (arg1) == BIT_AND_EXPR
6838 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6839 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6840 && integer_zerop (const_binop (BIT_AND_EXPR,
6841 TREE_OPERAND (arg0, 1),
6842 TREE_OPERAND (arg1, 1), 0)))
6844 code = BIT_IOR_EXPR;
6845 goto bit_ior;
6848 /* See if this can be simplified into a rotate first. If that
6849 is unsuccessful continue in the association code. */
6850 goto bit_rotate;
6852 case BIT_AND_EXPR:
6853 if (integer_all_onesp (arg1))
6854 return non_lvalue (fold_convert (type, arg0));
6855 if (integer_zerop (arg1))
6856 return omit_one_operand (type, arg1, arg0);
6857 if (operand_equal_p (arg0, arg1, 0))
6858 return non_lvalue (fold_convert (type, arg0));
6859 t1 = distribute_bit_expr (code, type, arg0, arg1);
6860 if (t1 != NULL_TREE)
6861 return t1;
6862 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
6863 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
6864 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6866 unsigned int prec
6867 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
6869 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
6870 && (~TREE_INT_CST_LOW (arg1)
6871 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
6872 return fold_convert (type, TREE_OPERAND (arg0, 0));
6875 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
6877 This results in more efficient code for machines without a NOR
6878 instruction. Combine will canonicalize to the first form
6879 which will allow use of NOR instructions provided by the
6880 backend if they exist. */
6881 if (TREE_CODE (arg0) == BIT_NOT_EXPR
6882 && TREE_CODE (arg1) == BIT_NOT_EXPR)
6884 return fold (build1 (BIT_NOT_EXPR, type,
6885 build2 (BIT_IOR_EXPR, type,
6886 TREE_OPERAND (arg0, 0),
6887 TREE_OPERAND (arg1, 0))));
6890 goto associate;
6892 case RDIV_EXPR:
6893 /* Don't touch a floating-point divide by zero unless the mode
6894 of the constant can represent infinity. */
6895 if (TREE_CODE (arg1) == REAL_CST
6896 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
6897 && real_zerop (arg1))
6898 return t;
6900 /* (-A) / (-B) -> A / B */
6901 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
6902 return fold (build2 (RDIV_EXPR, type,
6903 TREE_OPERAND (arg0, 0),
6904 negate_expr (arg1)));
6905 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
6906 return fold (build2 (RDIV_EXPR, type,
6907 negate_expr (arg0),
6908 TREE_OPERAND (arg1, 0)));
6910 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
6911 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6912 && real_onep (arg1))
6913 return non_lvalue (fold_convert (type, arg0));
6915 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
6916 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
6917 && real_minus_onep (arg1))
6918 return non_lvalue (fold_convert (type, negate_expr (arg0)));
6920 /* If ARG1 is a constant, we can convert this to a multiply by the
6921 reciprocal. This does not have the same rounding properties,
6922 so only do this if -funsafe-math-optimizations. We can actually
6923 always safely do it if ARG1 is a power of two, but it's hard to
6924 tell if it is or not in a portable manner. */
6925 if (TREE_CODE (arg1) == REAL_CST)
6927 if (flag_unsafe_math_optimizations
6928 && 0 != (tem = const_binop (code, build_real (type, dconst1),
6929 arg1, 0)))
6930 return fold (build2 (MULT_EXPR, type, arg0, tem));
6931 /* Find the reciprocal if optimizing and the result is exact. */
6932 if (optimize)
6934 REAL_VALUE_TYPE r;
6935 r = TREE_REAL_CST (arg1);
6936 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
6938 tem = build_real (type, r);
6939 return fold (build2 (MULT_EXPR, type, arg0, tem));
6943 /* Convert A/B/C to A/(B*C). */
6944 if (flag_unsafe_math_optimizations
6945 && TREE_CODE (arg0) == RDIV_EXPR)
6946 return fold (build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
6947 fold (build2 (MULT_EXPR, type,
6948 TREE_OPERAND (arg0, 1), arg1))));
6950 /* Convert A/(B/C) to (A/B)*C. */
6951 if (flag_unsafe_math_optimizations
6952 && TREE_CODE (arg1) == RDIV_EXPR)
6953 return fold (build2 (MULT_EXPR, type,
6954 fold (build2 (RDIV_EXPR, type, arg0,
6955 TREE_OPERAND (arg1, 0))),
6956 TREE_OPERAND (arg1, 1)));
6958 /* Convert C1/(X*C2) into (C1/C2)/X. */
6959 if (flag_unsafe_math_optimizations
6960 && TREE_CODE (arg1) == MULT_EXPR
6961 && TREE_CODE (arg0) == REAL_CST
6962 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
6964 tree tem = const_binop (RDIV_EXPR, arg0,
6965 TREE_OPERAND (arg1, 1), 0);
6966 if (tem)
6967 return fold (build2 (RDIV_EXPR, type, tem,
6968 TREE_OPERAND (arg1, 0)));
6971 if (flag_unsafe_math_optimizations)
6973 enum built_in_function fcode = builtin_mathfn_code (arg1);
6974 /* Optimize x/expN(y) into x*expN(-y). */
6975 if (BUILTIN_EXPONENT_P (fcode))
6977 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6978 tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
6979 tree arglist = build_tree_list (NULL_TREE,
6980 fold_convert (type, arg));
6981 arg1 = build_function_call_expr (expfn, arglist);
6982 return fold (build2 (MULT_EXPR, type, arg0, arg1));
6985 /* Optimize x/pow(y,z) into x*pow(y,-z). */
6986 if (fcode == BUILT_IN_POW
6987 || fcode == BUILT_IN_POWF
6988 || fcode == BUILT_IN_POWL)
6990 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
6991 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
6992 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
6993 tree neg11 = fold_convert (type, negate_expr (arg11));
6994 tree arglist = tree_cons(NULL_TREE, arg10,
6995 build_tree_list (NULL_TREE, neg11));
6996 arg1 = build_function_call_expr (powfn, arglist);
6997 return fold (build2 (MULT_EXPR, type, arg0, arg1));
7001 if (flag_unsafe_math_optimizations)
7003 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
7004 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
7006 /* Optimize sin(x)/cos(x) as tan(x). */
7007 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
7008 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
7009 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
7010 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7011 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7013 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7015 if (tanfn != NULL_TREE)
7016 return build_function_call_expr (tanfn,
7017 TREE_OPERAND (arg0, 1));
7020 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
7021 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
7022 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
7023 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
7024 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7025 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7027 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7029 if (tanfn != NULL_TREE)
7031 tree tmp = TREE_OPERAND (arg0, 1);
7032 tmp = build_function_call_expr (tanfn, tmp);
7033 return fold (build2 (RDIV_EXPR, type,
7034 build_real (type, dconst1), tmp));
7038 /* Optimize pow(x,c)/x as pow(x,c-1). */
7039 if (fcode0 == BUILT_IN_POW
7040 || fcode0 == BUILT_IN_POWF
7041 || fcode0 == BUILT_IN_POWL)
7043 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7044 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7045 if (TREE_CODE (arg01) == REAL_CST
7046 && ! TREE_CONSTANT_OVERFLOW (arg01)
7047 && operand_equal_p (arg1, arg00, 0))
7049 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7050 REAL_VALUE_TYPE c;
7051 tree arg, arglist;
7053 c = TREE_REAL_CST (arg01);
7054 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
7055 arg = build_real (type, c);
7056 arglist = build_tree_list (NULL_TREE, arg);
7057 arglist = tree_cons (NULL_TREE, arg1, arglist);
7058 return build_function_call_expr (powfn, arglist);
7062 goto binary;
7064 case TRUNC_DIV_EXPR:
7065 case ROUND_DIV_EXPR:
7066 case FLOOR_DIV_EXPR:
7067 case CEIL_DIV_EXPR:
7068 case EXACT_DIV_EXPR:
7069 if (integer_onep (arg1))
7070 return non_lvalue (fold_convert (type, arg0));
7071 if (integer_zerop (arg1))
7072 return t;
7073 /* X / -1 is -X. */
7074 if (!TYPE_UNSIGNED (type)
7075 && TREE_CODE (arg1) == INTEGER_CST
7076 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7077 && TREE_INT_CST_HIGH (arg1) == -1)
7078 return fold_convert (type, negate_expr (arg0));
7080 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
7081 operation, EXACT_DIV_EXPR.
7083 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
7084 At one time others generated faster code, it's not clear if they do
7085 after the last round to changes to the DIV code in expmed.c. */
7086 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
7087 && multiple_of_p (type, arg0, arg1))
7088 return fold (build2 (EXACT_DIV_EXPR, type, arg0, arg1));
7090 if (TREE_CODE (arg1) == INTEGER_CST
7091 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7092 code, NULL_TREE)))
7093 return fold_convert (type, tem);
7095 goto binary;
7097 case CEIL_MOD_EXPR:
7098 case FLOOR_MOD_EXPR:
7099 case ROUND_MOD_EXPR:
7100 case TRUNC_MOD_EXPR:
7101 if (integer_onep (arg1))
7102 return omit_one_operand (type, integer_zero_node, arg0);
7103 if (integer_zerop (arg1))
7104 return t;
7105 /* X % -1 is zero. */
7106 if (!TYPE_UNSIGNED (type)
7107 && TREE_CODE (arg1) == INTEGER_CST
7108 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7109 && TREE_INT_CST_HIGH (arg1) == -1)
7110 return omit_one_operand (type, integer_zero_node, arg0);
7112 if (TREE_CODE (arg1) == INTEGER_CST
7113 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7114 code, NULL_TREE)))
7115 return fold_convert (type, tem);
7117 goto binary;
7119 case LROTATE_EXPR:
7120 case RROTATE_EXPR:
7121 if (integer_all_onesp (arg0))
7122 return omit_one_operand (type, arg0, arg1);
7123 goto shift;
7125 case RSHIFT_EXPR:
7126 /* Optimize -1 >> x for arithmetic right shifts. */
7127 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
7128 return omit_one_operand (type, arg0, arg1);
7129 /* ... fall through ... */
7131 case LSHIFT_EXPR:
7132 shift:
7133 if (integer_zerop (arg1))
7134 return non_lvalue (fold_convert (type, arg0));
7135 if (integer_zerop (arg0))
7136 return omit_one_operand (type, arg0, arg1);
7138 /* Since negative shift count is not well-defined,
7139 don't try to compute it in the compiler. */
7140 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
7141 return t;
7142 /* Rewrite an LROTATE_EXPR by a constant into an
7143 RROTATE_EXPR by a new constant. */
7144 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
7146 tree tem = build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0);
7147 tem = fold_convert (TREE_TYPE (arg1), tem);
7148 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
7149 return fold (build2 (RROTATE_EXPR, type, arg0, tem));
7152 /* If we have a rotate of a bit operation with the rotate count and
7153 the second operand of the bit operation both constant,
7154 permute the two operations. */
7155 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7156 && (TREE_CODE (arg0) == BIT_AND_EXPR
7157 || TREE_CODE (arg0) == BIT_IOR_EXPR
7158 || TREE_CODE (arg0) == BIT_XOR_EXPR)
7159 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7160 return fold (build2 (TREE_CODE (arg0), type,
7161 fold (build2 (code, type,
7162 TREE_OPERAND (arg0, 0), arg1)),
7163 fold (build2 (code, type,
7164 TREE_OPERAND (arg0, 1), arg1))));
7166 /* Two consecutive rotates adding up to the width of the mode can
7167 be ignored. */
7168 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7169 && TREE_CODE (arg0) == RROTATE_EXPR
7170 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7171 && TREE_INT_CST_HIGH (arg1) == 0
7172 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
7173 && ((TREE_INT_CST_LOW (arg1)
7174 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
7175 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
7176 return TREE_OPERAND (arg0, 0);
7178 goto binary;
7180 case MIN_EXPR:
7181 if (operand_equal_p (arg0, arg1, 0))
7182 return omit_one_operand (type, arg0, arg1);
7183 if (INTEGRAL_TYPE_P (type)
7184 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
7185 return omit_one_operand (type, arg1, arg0);
7186 goto associate;
7188 case MAX_EXPR:
7189 if (operand_equal_p (arg0, arg1, 0))
7190 return omit_one_operand (type, arg0, arg1);
7191 if (INTEGRAL_TYPE_P (type)
7192 && TYPE_MAX_VALUE (type)
7193 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
7194 return omit_one_operand (type, arg1, arg0);
7195 goto associate;
7197 case TRUTH_NOT_EXPR:
7198 /* The argument to invert_truthvalue must have Boolean type. */
7199 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7200 arg0 = fold_convert (boolean_type_node, arg0);
7202 /* Note that the operand of this must be an int
7203 and its values must be 0 or 1.
7204 ("true" is a fixed value perhaps depending on the language,
7205 but we don't handle values other than 1 correctly yet.) */
7206 tem = invert_truthvalue (arg0);
7207 /* Avoid infinite recursion. */
7208 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
7210 tem = fold_single_bit_test (code, arg0, arg1, type);
7211 if (tem)
7212 return tem;
7213 return t;
7215 return fold_convert (type, tem);
7217 case TRUTH_ANDIF_EXPR:
7218 /* Note that the operands of this must be ints
7219 and their values must be 0 or 1.
7220 ("true" is a fixed value perhaps depending on the language.) */
7221 /* If first arg is constant zero, return it. */
7222 if (integer_zerop (arg0))
7223 return fold_convert (type, arg0);
7224 case TRUTH_AND_EXPR:
7225 /* If either arg is constant true, drop it. */
7226 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7227 return non_lvalue (fold_convert (type, arg1));
7228 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
7229 /* Preserve sequence points. */
7230 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7231 return non_lvalue (fold_convert (type, arg0));
7232 /* If second arg is constant zero, result is zero, but first arg
7233 must be evaluated. */
7234 if (integer_zerop (arg1))
7235 return omit_one_operand (type, arg1, arg0);
7236 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
7237 case will be handled here. */
7238 if (integer_zerop (arg0))
7239 return omit_one_operand (type, arg0, arg1);
7241 truth_andor:
7242 /* We only do these simplifications if we are optimizing. */
7243 if (!optimize)
7244 return t;
7246 /* Check for things like (A || B) && (A || C). We can convert this
7247 to A || (B && C). Note that either operator can be any of the four
7248 truth and/or operations and the transformation will still be
7249 valid. Also note that we only care about order for the
7250 ANDIF and ORIF operators. If B contains side effects, this
7251 might change the truth-value of A. */
7252 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7253 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7254 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7255 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7256 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7257 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7259 tree a00 = TREE_OPERAND (arg0, 0);
7260 tree a01 = TREE_OPERAND (arg0, 1);
7261 tree a10 = TREE_OPERAND (arg1, 0);
7262 tree a11 = TREE_OPERAND (arg1, 1);
7263 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7264 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7265 && (code == TRUTH_AND_EXPR
7266 || code == TRUTH_OR_EXPR));
7268 if (operand_equal_p (a00, a10, 0))
7269 return fold (build2 (TREE_CODE (arg0), type, a00,
7270 fold (build2 (code, type, a01, a11))));
7271 else if (commutative && operand_equal_p (a00, a11, 0))
7272 return fold (build2 (TREE_CODE (arg0), type, a00,
7273 fold (build2 (code, type, a01, a10))));
7274 else if (commutative && operand_equal_p (a01, a10, 0))
7275 return fold (build2 (TREE_CODE (arg0), type, a01,
7276 fold (build2 (code, type, a00, a11))));
7278 /* This case if tricky because we must either have commutative
7279 operators or else A10 must not have side-effects. */
7281 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7282 && operand_equal_p (a01, a11, 0))
7283 return fold (build2 (TREE_CODE (arg0), type,
7284 fold (build2 (code, type, a00, a10)),
7285 a01));
7288 /* See if we can build a range comparison. */
7289 if (0 != (tem = fold_range_test (t)))
7290 return tem;
7292 /* Check for the possibility of merging component references. If our
7293 lhs is another similar operation, try to merge its rhs with our
7294 rhs. Then try to merge our lhs and rhs. */
7295 if (TREE_CODE (arg0) == code
7296 && 0 != (tem = fold_truthop (code, type,
7297 TREE_OPERAND (arg0, 1), arg1)))
7298 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7300 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
7301 return tem;
7303 return t;
7305 case TRUTH_ORIF_EXPR:
7306 /* Note that the operands of this must be ints
7307 and their values must be 0 or true.
7308 ("true" is a fixed value perhaps depending on the language.) */
7309 /* If first arg is constant true, return it. */
7310 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7311 return fold_convert (type, arg0);
7312 case TRUTH_OR_EXPR:
7313 /* If either arg is constant zero, drop it. */
7314 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
7315 return non_lvalue (fold_convert (type, arg1));
7316 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
7317 /* Preserve sequence points. */
7318 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7319 return non_lvalue (fold_convert (type, arg0));
7320 /* If second arg is constant true, result is true, but we must
7321 evaluate first arg. */
7322 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
7323 return omit_one_operand (type, arg1, arg0);
7324 /* Likewise for first arg, but note this only occurs here for
7325 TRUTH_OR_EXPR. */
7326 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7327 return omit_one_operand (type, arg0, arg1);
7328 goto truth_andor;
7330 case TRUTH_XOR_EXPR:
7331 /* If either arg is constant zero, drop it. */
7332 if (integer_zerop (arg0))
7333 return non_lvalue (fold_convert (type, arg1));
7334 if (integer_zerop (arg1))
7335 return non_lvalue (fold_convert (type, arg0));
7336 /* If either arg is constant true, this is a logical inversion. */
7337 if (integer_onep (arg0))
7338 return non_lvalue (fold_convert (type, invert_truthvalue (arg1)));
7339 if (integer_onep (arg1))
7340 return non_lvalue (fold_convert (type, invert_truthvalue (arg0)));
7341 /* Identical arguments cancel to zero. */
7342 if (operand_equal_p (arg0, arg1, 0))
7343 return omit_one_operand (type, integer_zero_node, arg0);
7344 return t;
7346 case EQ_EXPR:
7347 case NE_EXPR:
7348 case LT_EXPR:
7349 case GT_EXPR:
7350 case LE_EXPR:
7351 case GE_EXPR:
7352 /* If one arg is a real or integer constant, put it last. */
7353 if (tree_swap_operands_p (arg0, arg1, true))
7354 return fold (build2 (swap_tree_comparison (code), type, arg1, arg0));
7356 /* If this is an equality comparison of the address of a non-weak
7357 object against zero, then we know the result. */
7358 if ((code == EQ_EXPR || code == NE_EXPR)
7359 && TREE_CODE (arg0) == ADDR_EXPR
7360 && DECL_P (TREE_OPERAND (arg0, 0))
7361 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7362 && integer_zerop (arg1))
7363 return constant_boolean_node (code != EQ_EXPR, type);
7365 /* If this is an equality comparison of the address of two non-weak,
7366 unaliased symbols neither of which are extern (since we do not
7367 have access to attributes for externs), then we know the result. */
7368 if ((code == EQ_EXPR || code == NE_EXPR)
7369 && TREE_CODE (arg0) == ADDR_EXPR
7370 && DECL_P (TREE_OPERAND (arg0, 0))
7371 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7372 && ! lookup_attribute ("alias",
7373 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
7374 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
7375 && TREE_CODE (arg1) == ADDR_EXPR
7376 && DECL_P (TREE_OPERAND (arg1, 0))
7377 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
7378 && ! lookup_attribute ("alias",
7379 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
7380 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
7381 return constant_boolean_node (operand_equal_p (arg0, arg1, 0)
7382 ? code == EQ_EXPR : code != EQ_EXPR,
7383 type);
7385 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7387 tree targ0 = strip_float_extensions (arg0);
7388 tree targ1 = strip_float_extensions (arg1);
7389 tree newtype = TREE_TYPE (targ0);
7391 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7392 newtype = TREE_TYPE (targ1);
7394 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7395 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7396 return fold (build2 (code, type, fold_convert (newtype, targ0),
7397 fold_convert (newtype, targ1)));
7399 /* (-a) CMP (-b) -> b CMP a */
7400 if (TREE_CODE (arg0) == NEGATE_EXPR
7401 && TREE_CODE (arg1) == NEGATE_EXPR)
7402 return fold (build2 (code, type, TREE_OPERAND (arg1, 0),
7403 TREE_OPERAND (arg0, 0)));
7405 if (TREE_CODE (arg1) == REAL_CST)
7407 REAL_VALUE_TYPE cst;
7408 cst = TREE_REAL_CST (arg1);
7410 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7411 if (TREE_CODE (arg0) == NEGATE_EXPR)
7412 return
7413 fold (build2 (swap_tree_comparison (code), type,
7414 TREE_OPERAND (arg0, 0),
7415 build_real (TREE_TYPE (arg1),
7416 REAL_VALUE_NEGATE (cst))));
7418 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
7419 /* a CMP (-0) -> a CMP 0 */
7420 if (REAL_VALUE_MINUS_ZERO (cst))
7421 return fold (build2 (code, type, arg0,
7422 build_real (TREE_TYPE (arg1), dconst0)));
7424 /* x != NaN is always true, other ops are always false. */
7425 if (REAL_VALUE_ISNAN (cst)
7426 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
7428 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
7429 return omit_one_operand (type, tem, arg0);
7432 /* Fold comparisons against infinity. */
7433 if (REAL_VALUE_ISINF (cst))
7435 tem = fold_inf_compare (code, type, arg0, arg1);
7436 if (tem != NULL_TREE)
7437 return tem;
7441 /* If this is a comparison of a real constant with a PLUS_EXPR
7442 or a MINUS_EXPR of a real constant, we can convert it into a
7443 comparison with a revised real constant as long as no overflow
7444 occurs when unsafe_math_optimizations are enabled. */
7445 if (flag_unsafe_math_optimizations
7446 && TREE_CODE (arg1) == REAL_CST
7447 && (TREE_CODE (arg0) == PLUS_EXPR
7448 || TREE_CODE (arg0) == MINUS_EXPR)
7449 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7450 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7451 ? MINUS_EXPR : PLUS_EXPR,
7452 arg1, TREE_OPERAND (arg0, 1), 0))
7453 && ! TREE_CONSTANT_OVERFLOW (tem))
7454 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7456 /* Likewise, we can simplify a comparison of a real constant with
7457 a MINUS_EXPR whose first operand is also a real constant, i.e.
7458 (c1 - x) < c2 becomes x > c1-c2. */
7459 if (flag_unsafe_math_optimizations
7460 && TREE_CODE (arg1) == REAL_CST
7461 && TREE_CODE (arg0) == MINUS_EXPR
7462 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
7463 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
7464 arg1, 0))
7465 && ! TREE_CONSTANT_OVERFLOW (tem))
7466 return fold (build2 (swap_tree_comparison (code), type,
7467 TREE_OPERAND (arg0, 1), tem));
7469 /* Fold comparisons against built-in math functions. */
7470 if (TREE_CODE (arg1) == REAL_CST
7471 && flag_unsafe_math_optimizations
7472 && ! flag_errno_math)
7474 enum built_in_function fcode = builtin_mathfn_code (arg0);
7476 if (fcode != END_BUILTINS)
7478 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
7479 if (tem != NULL_TREE)
7480 return tem;
7485 /* Convert foo++ == CONST into ++foo == CONST + INCR. */
7486 if (TREE_CONSTANT (arg1)
7487 && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
7488 || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
7489 /* This optimization is invalid for ordered comparisons
7490 if CONST+INCR overflows or if foo+incr might overflow.
7491 This optimization is invalid for floating point due to rounding.
7492 For pointer types we assume overflow doesn't happen. */
7493 && (POINTER_TYPE_P (TREE_TYPE (arg0))
7494 || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
7495 && (code == EQ_EXPR || code == NE_EXPR))))
7497 tree varop, newconst;
7499 if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
7501 newconst = fold (build2 (PLUS_EXPR, TREE_TYPE (arg0),
7502 arg1, TREE_OPERAND (arg0, 1)));
7503 varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
7504 TREE_OPERAND (arg0, 0),
7505 TREE_OPERAND (arg0, 1));
7507 else
7509 newconst = fold (build2 (MINUS_EXPR, TREE_TYPE (arg0),
7510 arg1, TREE_OPERAND (arg0, 1)));
7511 varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
7512 TREE_OPERAND (arg0, 0),
7513 TREE_OPERAND (arg0, 1));
7517 /* If VAROP is a reference to a bitfield, we must mask
7518 the constant by the width of the field. */
7519 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
7520 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1)))
7522 tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
7523 int size = TREE_INT_CST_LOW (DECL_SIZE (fielddecl));
7524 tree folded_compare, shift;
7526 /* First check whether the comparison would come out
7527 always the same. If we don't do that we would
7528 change the meaning with the masking. */
7529 folded_compare = fold (build2 (code, type,
7530 TREE_OPERAND (varop, 0),
7531 arg1));
7532 if (integer_zerop (folded_compare)
7533 || integer_onep (folded_compare))
7534 return omit_one_operand (type, folded_compare, varop);
7536 shift = build_int_2 (TYPE_PRECISION (TREE_TYPE (varop)) - size,
7538 newconst = fold (build2 (LSHIFT_EXPR, TREE_TYPE (varop),
7539 newconst, shift));
7540 newconst = fold (build2 (RSHIFT_EXPR, TREE_TYPE (varop),
7541 newconst, shift));
7544 return fold (build2 (code, type, varop, newconst));
7547 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
7548 This transformation affects the cases which are handled in later
7549 optimizations involving comparisons with non-negative constants. */
7550 if (TREE_CODE (arg1) == INTEGER_CST
7551 && TREE_CODE (arg0) != INTEGER_CST
7552 && tree_int_cst_sgn (arg1) > 0)
7554 switch (code)
7556 case GE_EXPR:
7557 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7558 return fold (build2 (GT_EXPR, type, arg0, arg1));
7560 case LT_EXPR:
7561 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7562 return fold (build2 (LE_EXPR, type, arg0, arg1));
7564 default:
7565 break;
7569 /* Comparisons with the highest or lowest possible integer of
7570 the specified size will have known values.
7572 This is quite similar to fold_relational_hi_lo; however, my
7573 attempts to share the code have been nothing but trouble.
7574 I give up for now. */
7576 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
7578 if (TREE_CODE (arg1) == INTEGER_CST
7579 && ! TREE_CONSTANT_OVERFLOW (arg1)
7580 && width <= HOST_BITS_PER_WIDE_INT
7581 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
7582 || POINTER_TYPE_P (TREE_TYPE (arg1))))
7584 unsigned HOST_WIDE_INT signed_max;
7585 unsigned HOST_WIDE_INT max, min;
7587 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
7589 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
7591 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
7592 min = 0;
7594 else
7596 max = signed_max;
7597 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
7600 if (TREE_INT_CST_HIGH (arg1) == 0
7601 && TREE_INT_CST_LOW (arg1) == max)
7602 switch (code)
7604 case GT_EXPR:
7605 return omit_one_operand (type, integer_zero_node, arg0);
7607 case GE_EXPR:
7608 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7610 case LE_EXPR:
7611 return omit_one_operand (type, integer_one_node, arg0);
7613 case LT_EXPR:
7614 return fold (build2 (NE_EXPR, type, arg0, arg1));
7616 /* The GE_EXPR and LT_EXPR cases above are not normally
7617 reached because of previous transformations. */
7619 default:
7620 break;
7622 else if (TREE_INT_CST_HIGH (arg1) == 0
7623 && TREE_INT_CST_LOW (arg1) == max - 1)
7624 switch (code)
7626 case GT_EXPR:
7627 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7628 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7629 case LE_EXPR:
7630 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
7631 return fold (build2 (NE_EXPR, type, arg0, arg1));
7632 default:
7633 break;
7635 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7636 && TREE_INT_CST_LOW (arg1) == min)
7637 switch (code)
7639 case LT_EXPR:
7640 return omit_one_operand (type, integer_zero_node, arg0);
7642 case LE_EXPR:
7643 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7645 case GE_EXPR:
7646 return omit_one_operand (type, integer_one_node, arg0);
7648 case GT_EXPR:
7649 return fold (build2 (NE_EXPR, type, arg0, arg1));
7651 default:
7652 break;
7654 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
7655 && TREE_INT_CST_LOW (arg1) == min + 1)
7656 switch (code)
7658 case GE_EXPR:
7659 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7660 return fold (build2 (NE_EXPR, type, arg0, arg1));
7661 case LT_EXPR:
7662 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
7663 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7664 default:
7665 break;
7668 else if (!in_gimple_form
7669 && TREE_INT_CST_HIGH (arg1) == 0
7670 && TREE_INT_CST_LOW (arg1) == signed_max
7671 && TYPE_UNSIGNED (TREE_TYPE (arg1))
7672 /* signed_type does not work on pointer types. */
7673 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
7675 /* The following case also applies to X < signed_max+1
7676 and X >= signed_max+1 because previous transformations. */
7677 if (code == LE_EXPR || code == GT_EXPR)
7679 tree st0, st1;
7680 st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
7681 st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
7682 return fold
7683 (build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
7684 type, fold_convert (st0, arg0),
7685 fold_convert (st1, integer_zero_node)));
7691 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
7692 a MINUS_EXPR of a constant, we can convert it into a comparison with
7693 a revised constant as long as no overflow occurs. */
7694 if ((code == EQ_EXPR || code == NE_EXPR)
7695 && TREE_CODE (arg1) == INTEGER_CST
7696 && (TREE_CODE (arg0) == PLUS_EXPR
7697 || TREE_CODE (arg0) == MINUS_EXPR)
7698 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7699 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
7700 ? MINUS_EXPR : PLUS_EXPR,
7701 arg1, TREE_OPERAND (arg0, 1), 0))
7702 && ! TREE_CONSTANT_OVERFLOW (tem))
7703 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7705 /* Similarly for a NEGATE_EXPR. */
7706 else if ((code == EQ_EXPR || code == NE_EXPR)
7707 && TREE_CODE (arg0) == NEGATE_EXPR
7708 && TREE_CODE (arg1) == INTEGER_CST
7709 && 0 != (tem = negate_expr (arg1))
7710 && TREE_CODE (tem) == INTEGER_CST
7711 && ! TREE_CONSTANT_OVERFLOW (tem))
7712 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7714 /* If we have X - Y == 0, we can convert that to X == Y and similarly
7715 for !=. Don't do this for ordered comparisons due to overflow. */
7716 else if ((code == NE_EXPR || code == EQ_EXPR)
7717 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
7718 return fold (build2 (code, type,
7719 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
7721 /* If we are widening one operand of an integer comparison,
7722 see if the other operand is similarly being widened. Perhaps we
7723 can do the comparison in the narrower type. */
7724 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
7725 && TREE_CODE (arg0) == NOP_EXPR
7726 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
7727 && (code == EQ_EXPR || code == NE_EXPR
7728 || TYPE_UNSIGNED (TREE_TYPE (arg0))
7729 == TYPE_UNSIGNED (TREE_TYPE (tem)))
7730 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
7731 && (TREE_TYPE (t1) == TREE_TYPE (tem)
7732 || (TREE_CODE (t1) == INTEGER_CST
7733 && int_fits_type_p (t1, TREE_TYPE (tem)))))
7734 return fold (build2 (code, type, tem,
7735 fold_convert (TREE_TYPE (tem), t1)));
7737 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
7738 constant, we can simplify it. */
7739 else if (TREE_CODE (arg1) == INTEGER_CST
7740 && (TREE_CODE (arg0) == MIN_EXPR
7741 || TREE_CODE (arg0) == MAX_EXPR)
7742 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7743 return optimize_minmax_comparison (t);
7745 /* If we are comparing an ABS_EXPR with a constant, we can
7746 convert all the cases into explicit comparisons, but they may
7747 well not be faster than doing the ABS and one comparison.
7748 But ABS (X) <= C is a range comparison, which becomes a subtraction
7749 and a comparison, and is probably faster. */
7750 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7751 && TREE_CODE (arg0) == ABS_EXPR
7752 && ! TREE_SIDE_EFFECTS (arg0)
7753 && (0 != (tem = negate_expr (arg1)))
7754 && TREE_CODE (tem) == INTEGER_CST
7755 && ! TREE_CONSTANT_OVERFLOW (tem))
7756 return fold (build2 (TRUTH_ANDIF_EXPR, type,
7757 build2 (GE_EXPR, type,
7758 TREE_OPERAND (arg0, 0), tem),
7759 build2 (LE_EXPR, type,
7760 TREE_OPERAND (arg0, 0), arg1)));
7762 /* If this is an EQ or NE comparison with zero and ARG0 is
7763 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
7764 two operations, but the latter can be done in one less insn
7765 on machines that have only two-operand insns or on which a
7766 constant cannot be the first operand. */
7767 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
7768 && TREE_CODE (arg0) == BIT_AND_EXPR)
7770 tree arg00 = TREE_OPERAND (arg0, 0);
7771 tree arg01 = TREE_OPERAND (arg0, 1);
7772 if (TREE_CODE (arg00) == LSHIFT_EXPR
7773 && integer_onep (TREE_OPERAND (arg00, 0)))
7774 return
7775 fold (build2 (code, type,
7776 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7777 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
7778 arg01, TREE_OPERAND (arg00, 1)),
7779 fold_convert (TREE_TYPE (arg0),
7780 integer_one_node)),
7781 arg1));
7782 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
7783 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
7784 return
7785 fold (build2 (code, type,
7786 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7787 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
7788 arg00, TREE_OPERAND (arg01, 1)),
7789 fold_convert (TREE_TYPE (arg0),
7790 integer_one_node)),
7791 arg1));
7794 /* If this is an NE or EQ comparison of zero against the result of a
7795 signed MOD operation whose second operand is a power of 2, make
7796 the MOD operation unsigned since it is simpler and equivalent. */
7797 if ((code == NE_EXPR || code == EQ_EXPR)
7798 && integer_zerop (arg1)
7799 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
7800 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
7801 || TREE_CODE (arg0) == CEIL_MOD_EXPR
7802 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
7803 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
7804 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7806 tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
7807 tree newmod = build2 (TREE_CODE (arg0), newtype,
7808 fold_convert (newtype,
7809 TREE_OPERAND (arg0, 0)),
7810 fold_convert (newtype,
7811 TREE_OPERAND (arg0, 1)));
7813 return build2 (code, type, newmod, fold_convert (newtype, arg1));
7816 /* If this is an NE comparison of zero with an AND of one, remove the
7817 comparison since the AND will give the correct value. */
7818 if (code == NE_EXPR && integer_zerop (arg1)
7819 && TREE_CODE (arg0) == BIT_AND_EXPR
7820 && integer_onep (TREE_OPERAND (arg0, 1)))
7821 return fold_convert (type, arg0);
7823 /* If we have (A & C) == C where C is a power of 2, convert this into
7824 (A & C) != 0. Similarly for NE_EXPR. */
7825 if ((code == EQ_EXPR || code == NE_EXPR)
7826 && TREE_CODE (arg0) == BIT_AND_EXPR
7827 && integer_pow2p (TREE_OPERAND (arg0, 1))
7828 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
7829 return fold (build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
7830 arg0, integer_zero_node));
7832 /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
7833 2, then fold the expression into shifts and logical operations. */
7834 tem = fold_single_bit_test (code, arg0, arg1, type);
7835 if (tem)
7836 return tem;
7838 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
7839 Similarly for NE_EXPR. */
7840 if ((code == EQ_EXPR || code == NE_EXPR)
7841 && TREE_CODE (arg0) == BIT_AND_EXPR
7842 && TREE_CODE (arg1) == INTEGER_CST
7843 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7845 tree dandnotc
7846 = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7847 arg1, build1 (BIT_NOT_EXPR,
7848 TREE_TYPE (TREE_OPERAND (arg0, 1)),
7849 TREE_OPERAND (arg0, 1))));
7850 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7851 if (integer_nonzerop (dandnotc))
7852 return omit_one_operand (type, rslt, arg0);
7855 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
7856 Similarly for NE_EXPR. */
7857 if ((code == EQ_EXPR || code == NE_EXPR)
7858 && TREE_CODE (arg0) == BIT_IOR_EXPR
7859 && TREE_CODE (arg1) == INTEGER_CST
7860 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7862 tree candnotd
7863 = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
7864 TREE_OPERAND (arg0, 1),
7865 build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1)));
7866 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
7867 if (integer_nonzerop (candnotd))
7868 return omit_one_operand (type, rslt, arg0);
7871 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
7872 and similarly for >= into !=. */
7873 if ((code == LT_EXPR || code == GE_EXPR)
7874 && TYPE_UNSIGNED (TREE_TYPE (arg0))
7875 && TREE_CODE (arg1) == LSHIFT_EXPR
7876 && integer_onep (TREE_OPERAND (arg1, 0)))
7877 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7878 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7879 TREE_OPERAND (arg1, 1)),
7880 fold_convert (TREE_TYPE (arg0), integer_zero_node));
7882 else if ((code == LT_EXPR || code == GE_EXPR)
7883 && TYPE_UNSIGNED (TREE_TYPE (arg0))
7884 && (TREE_CODE (arg1) == NOP_EXPR
7885 || TREE_CODE (arg1) == CONVERT_EXPR)
7886 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
7887 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
7888 return
7889 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
7890 fold_convert (TREE_TYPE (arg0),
7891 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
7892 TREE_OPERAND (TREE_OPERAND (arg1, 0),
7893 1))),
7894 fold_convert (TREE_TYPE (arg0), integer_zero_node));
7896 /* Simplify comparison of something with itself. (For IEEE
7897 floating-point, we can only do some of these simplifications.) */
7898 if (operand_equal_p (arg0, arg1, 0))
7900 switch (code)
7902 case EQ_EXPR:
7903 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7904 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7905 return constant_boolean_node (1, type);
7906 break;
7908 case GE_EXPR:
7909 case LE_EXPR:
7910 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
7911 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7912 return constant_boolean_node (1, type);
7913 return fold (build2 (EQ_EXPR, type, arg0, arg1));
7915 case NE_EXPR:
7916 /* For NE, we can only do this simplification if integer
7917 or we don't honor IEEE floating point NaNs. */
7918 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
7919 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
7920 break;
7921 /* ... fall through ... */
7922 case GT_EXPR:
7923 case LT_EXPR:
7924 return constant_boolean_node (0, type);
7925 default:
7926 abort ();
7930 /* If we are comparing an expression that just has comparisons
7931 of two integer values, arithmetic expressions of those comparisons,
7932 and constants, we can simplify it. There are only three cases
7933 to check: the two values can either be equal, the first can be
7934 greater, or the second can be greater. Fold the expression for
7935 those three values. Since each value must be 0 or 1, we have
7936 eight possibilities, each of which corresponds to the constant 0
7937 or 1 or one of the six possible comparisons.
7939 This handles common cases like (a > b) == 0 but also handles
7940 expressions like ((x > y) - (y > x)) > 0, which supposedly
7941 occur in macroized code. */
7943 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
7945 tree cval1 = 0, cval2 = 0;
7946 int save_p = 0;
7948 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
7949 /* Don't handle degenerate cases here; they should already
7950 have been handled anyway. */
7951 && cval1 != 0 && cval2 != 0
7952 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
7953 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
7954 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
7955 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
7956 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
7957 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
7958 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
7960 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
7961 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
7963 /* We can't just pass T to eval_subst in case cval1 or cval2
7964 was the same as ARG1. */
7966 tree high_result
7967 = fold (build2 (code, type,
7968 eval_subst (arg0, cval1, maxval,
7969 cval2, minval),
7970 arg1));
7971 tree equal_result
7972 = fold (build2 (code, type,
7973 eval_subst (arg0, cval1, maxval,
7974 cval2, maxval),
7975 arg1));
7976 tree low_result
7977 = fold (build2 (code, type,
7978 eval_subst (arg0, cval1, minval,
7979 cval2, maxval),
7980 arg1));
7982 /* All three of these results should be 0 or 1. Confirm they
7983 are. Then use those values to select the proper code
7984 to use. */
7986 if ((integer_zerop (high_result)
7987 || integer_onep (high_result))
7988 && (integer_zerop (equal_result)
7989 || integer_onep (equal_result))
7990 && (integer_zerop (low_result)
7991 || integer_onep (low_result)))
7993 /* Make a 3-bit mask with the high-order bit being the
7994 value for `>', the next for '=', and the low for '<'. */
7995 switch ((integer_onep (high_result) * 4)
7996 + (integer_onep (equal_result) * 2)
7997 + integer_onep (low_result))
7999 case 0:
8000 /* Always false. */
8001 return omit_one_operand (type, integer_zero_node, arg0);
8002 case 1:
8003 code = LT_EXPR;
8004 break;
8005 case 2:
8006 code = EQ_EXPR;
8007 break;
8008 case 3:
8009 code = LE_EXPR;
8010 break;
8011 case 4:
8012 code = GT_EXPR;
8013 break;
8014 case 5:
8015 code = NE_EXPR;
8016 break;
8017 case 6:
8018 code = GE_EXPR;
8019 break;
8020 case 7:
8021 /* Always true. */
8022 return omit_one_operand (type, integer_one_node, arg0);
8025 tem = build2 (code, type, cval1, cval2);
8026 if (save_p)
8027 return save_expr (tem);
8028 else
8029 return fold (tem);
8034 /* If this is a comparison of a field, we may be able to simplify it. */
8035 if (((TREE_CODE (arg0) == COMPONENT_REF
8036 && lang_hooks.can_use_bit_fields_p ())
8037 || TREE_CODE (arg0) == BIT_FIELD_REF)
8038 && (code == EQ_EXPR || code == NE_EXPR)
8039 /* Handle the constant case even without -O
8040 to make sure the warnings are given. */
8041 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
8043 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
8044 if (t1)
8045 return t1;
8048 /* If this is a comparison of complex values and either or both sides
8049 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
8050 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
8051 This may prevent needless evaluations. */
8052 if ((code == EQ_EXPR || code == NE_EXPR)
8053 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
8054 && (TREE_CODE (arg0) == COMPLEX_EXPR
8055 || TREE_CODE (arg1) == COMPLEX_EXPR
8056 || TREE_CODE (arg0) == COMPLEX_CST
8057 || TREE_CODE (arg1) == COMPLEX_CST))
8059 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
8060 tree real0, imag0, real1, imag1;
8062 arg0 = save_expr (arg0);
8063 arg1 = save_expr (arg1);
8064 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
8065 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
8066 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
8067 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
8069 return fold (build2 ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
8070 : TRUTH_ORIF_EXPR),
8071 type,
8072 fold (build2 (code, type, real0, real1)),
8073 fold (build2 (code, type, imag0, imag1))));
8076 /* Optimize comparisons of strlen vs zero to a compare of the
8077 first character of the string vs zero. To wit,
8078 strlen(ptr) == 0 => *ptr == 0
8079 strlen(ptr) != 0 => *ptr != 0
8080 Other cases should reduce to one of these two (or a constant)
8081 due to the return value of strlen being unsigned. */
8082 if ((code == EQ_EXPR || code == NE_EXPR)
8083 && integer_zerop (arg1)
8084 && TREE_CODE (arg0) == CALL_EXPR)
8086 tree fndecl = get_callee_fndecl (arg0);
8087 tree arglist;
8089 if (fndecl
8090 && DECL_BUILT_IN (fndecl)
8091 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
8092 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
8093 && (arglist = TREE_OPERAND (arg0, 1))
8094 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
8095 && ! TREE_CHAIN (arglist))
8096 return fold (build2 (code, type,
8097 build1 (INDIRECT_REF, char_type_node,
8098 TREE_VALUE(arglist)),
8099 integer_zero_node));
8102 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8103 into a single range test. */
8104 if (TREE_CODE (arg0) == TRUNC_DIV_EXPR
8105 && TREE_CODE (arg1) == INTEGER_CST
8106 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8107 && !integer_zerop (TREE_OPERAND (arg0, 1))
8108 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8109 && !TREE_OVERFLOW (arg1))
8111 t1 = fold_div_compare (code, type, arg0, arg1);
8112 if (t1 != NULL_TREE)
8113 return t1;
8116 /* Both ARG0 and ARG1 are known to be constants at this point. */
8117 t1 = fold_relational_const (code, type, arg0, arg1);
8118 return (t1 == NULL_TREE ? t : t1);
8120 case COND_EXPR:
8121 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
8122 so all simple results must be passed through pedantic_non_lvalue. */
8123 if (TREE_CODE (arg0) == INTEGER_CST)
8125 tem = TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
8126 /* Only optimize constant conditions when the selected branch
8127 has the same type as the COND_EXPR. This avoids optimizing
8128 away "c ? x : throw", where the throw has a void type. */
8129 if (! VOID_TYPE_P (TREE_TYPE (tem))
8130 || VOID_TYPE_P (type))
8131 return pedantic_non_lvalue (tem);
8132 return t;
8134 if (operand_equal_p (arg1, TREE_OPERAND (t, 2), 0))
8135 return pedantic_omit_one_operand (type, arg1, arg0);
8137 /* If we have A op B ? A : C, we may be able to convert this to a
8138 simpler expression, depending on the operation and the values
8139 of B and C. Signed zeros prevent all of these transformations,
8140 for reasons given above each one. */
8142 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
8143 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
8144 arg1, TREE_OPERAND (arg0, 1))
8145 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
8147 tree arg2 = TREE_OPERAND (t, 2);
8148 enum tree_code comp_code = TREE_CODE (arg0);
8150 STRIP_NOPS (arg2);
8152 /* If we have A op 0 ? A : -A, consider applying the following
8153 transformations:
8155 A == 0? A : -A same as -A
8156 A != 0? A : -A same as A
8157 A >= 0? A : -A same as abs (A)
8158 A > 0? A : -A same as abs (A)
8159 A <= 0? A : -A same as -abs (A)
8160 A < 0? A : -A same as -abs (A)
8162 None of these transformations work for modes with signed
8163 zeros. If A is +/-0, the first two transformations will
8164 change the sign of the result (from +0 to -0, or vice
8165 versa). The last four will fix the sign of the result,
8166 even though the original expressions could be positive or
8167 negative, depending on the sign of A.
8169 Note that all these transformations are correct if A is
8170 NaN, since the two alternatives (A and -A) are also NaNs. */
8171 if ((FLOAT_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 1)))
8172 ? real_zerop (TREE_OPERAND (arg0, 1))
8173 : integer_zerop (TREE_OPERAND (arg0, 1)))
8174 && TREE_CODE (arg2) == NEGATE_EXPR
8175 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
8176 switch (comp_code)
8178 case EQ_EXPR:
8179 tem = fold_convert (TREE_TYPE (TREE_OPERAND (t, 1)), arg1);
8180 tem = fold_convert (type, negate_expr (tem));
8181 return pedantic_non_lvalue (tem);
8182 case NE_EXPR:
8183 return pedantic_non_lvalue (fold_convert (type, arg1));
8184 case GE_EXPR:
8185 case GT_EXPR:
8186 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8187 arg1 = fold_convert (lang_hooks.types.signed_type
8188 (TREE_TYPE (arg1)), arg1);
8189 arg1 = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
8190 return pedantic_non_lvalue (fold_convert (type, arg1));
8191 case LE_EXPR:
8192 case LT_EXPR:
8193 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8194 arg1 = fold_convert (lang_hooks.types.signed_type
8195 (TREE_TYPE (arg1)), arg1);
8196 arg1 = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
8197 arg1 = negate_expr (fold_convert (type, arg1));
8198 return pedantic_non_lvalue (arg1);
8199 default:
8200 abort ();
8203 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
8204 A == 0 ? A : 0 is always 0 unless A is -0. Note that
8205 both transformations are correct when A is NaN: A != 0
8206 is then true, and A == 0 is false. */
8208 if (integer_zerop (TREE_OPERAND (arg0, 1)) && integer_zerop (arg2))
8210 if (comp_code == NE_EXPR)
8211 return pedantic_non_lvalue (fold_convert (type, arg1));
8212 else if (comp_code == EQ_EXPR)
8213 return pedantic_non_lvalue (fold_convert (type, integer_zero_node));
8216 /* Try some transformations of A op B ? A : B.
8218 A == B? A : B same as B
8219 A != B? A : B same as A
8220 A >= B? A : B same as max (A, B)
8221 A > B? A : B same as max (B, A)
8222 A <= B? A : B same as min (A, B)
8223 A < B? A : B same as min (B, A)
8225 As above, these transformations don't work in the presence
8226 of signed zeros. For example, if A and B are zeros of
8227 opposite sign, the first two transformations will change
8228 the sign of the result. In the last four, the original
8229 expressions give different results for (A=+0, B=-0) and
8230 (A=-0, B=+0), but the transformed expressions do not.
8232 The first two transformations are correct if either A or B
8233 is a NaN. In the first transformation, the condition will
8234 be false, and B will indeed be chosen. In the case of the
8235 second transformation, the condition A != B will be true,
8236 and A will be chosen.
8238 The conversions to max() and min() are not correct if B is
8239 a number and A is not. The conditions in the original
8240 expressions will be false, so all four give B. The min()
8241 and max() versions would give a NaN instead. */
8242 if (operand_equal_for_comparison_p (TREE_OPERAND (arg0, 1),
8243 arg2, TREE_OPERAND (arg0, 0)))
8245 tree comp_op0 = TREE_OPERAND (arg0, 0);
8246 tree comp_op1 = TREE_OPERAND (arg0, 1);
8247 tree comp_type = TREE_TYPE (comp_op0);
8249 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
8250 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
8252 comp_type = type;
8253 comp_op0 = arg1;
8254 comp_op1 = arg2;
8257 switch (comp_code)
8259 case EQ_EXPR:
8260 return pedantic_non_lvalue (fold_convert (type, arg2));
8261 case NE_EXPR:
8262 return pedantic_non_lvalue (fold_convert (type, arg1));
8263 case LE_EXPR:
8264 case LT_EXPR:
8265 /* In C++ a ?: expression can be an lvalue, so put the
8266 operand which will be used if they are equal first
8267 so that we can convert this back to the
8268 corresponding COND_EXPR. */
8269 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8270 return pedantic_non_lvalue (fold_convert
8271 (type, fold (build2 (MIN_EXPR, comp_type,
8272 (comp_code == LE_EXPR
8273 ? comp_op0 : comp_op1),
8274 (comp_code == LE_EXPR
8275 ? comp_op1 : comp_op0)))));
8276 break;
8277 case GE_EXPR:
8278 case GT_EXPR:
8279 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
8280 return pedantic_non_lvalue (fold_convert
8281 (type, fold (build2 (MAX_EXPR, comp_type,
8282 (comp_code == GE_EXPR
8283 ? comp_op0 : comp_op1),
8284 (comp_code == GE_EXPR
8285 ? comp_op1 : comp_op0)))));
8286 break;
8287 default:
8288 abort ();
8292 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
8293 we might still be able to simplify this. For example,
8294 if C1 is one less or one more than C2, this might have started
8295 out as a MIN or MAX and been transformed by this function.
8296 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
8298 if (INTEGRAL_TYPE_P (type)
8299 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8300 && TREE_CODE (arg2) == INTEGER_CST)
8301 switch (comp_code)
8303 case EQ_EXPR:
8304 /* We can replace A with C1 in this case. */
8305 arg1 = fold_convert (type, TREE_OPERAND (arg0, 1));
8306 return fold (build3 (code, type, TREE_OPERAND (t, 0), arg1,
8307 TREE_OPERAND (t, 2)));
8309 case LT_EXPR:
8310 /* If C1 is C2 + 1, this is min(A, C2). */
8311 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
8312 OEP_ONLY_CONST)
8313 && operand_equal_p (TREE_OPERAND (arg0, 1),
8314 const_binop (PLUS_EXPR, arg2,
8315 integer_one_node, 0),
8316 OEP_ONLY_CONST))
8317 return pedantic_non_lvalue
8318 (fold (build2 (MIN_EXPR, type, arg1, arg2)));
8319 break;
8321 case LE_EXPR:
8322 /* If C1 is C2 - 1, this is min(A, C2). */
8323 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
8324 OEP_ONLY_CONST)
8325 && operand_equal_p (TREE_OPERAND (arg0, 1),
8326 const_binop (MINUS_EXPR, arg2,
8327 integer_one_node, 0),
8328 OEP_ONLY_CONST))
8329 return pedantic_non_lvalue
8330 (fold (build2 (MIN_EXPR, type, arg1, arg2)));
8331 break;
8333 case GT_EXPR:
8334 /* If C1 is C2 - 1, this is max(A, C2). */
8335 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
8336 OEP_ONLY_CONST)
8337 && operand_equal_p (TREE_OPERAND (arg0, 1),
8338 const_binop (MINUS_EXPR, arg2,
8339 integer_one_node, 0),
8340 OEP_ONLY_CONST))
8341 return pedantic_non_lvalue
8342 (fold (build2 (MAX_EXPR, type, arg1, arg2)));
8343 break;
8345 case GE_EXPR:
8346 /* If C1 is C2 + 1, this is max(A, C2). */
8347 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
8348 OEP_ONLY_CONST)
8349 && operand_equal_p (TREE_OPERAND (arg0, 1),
8350 const_binop (PLUS_EXPR, arg2,
8351 integer_one_node, 0),
8352 OEP_ONLY_CONST))
8353 return pedantic_non_lvalue
8354 (fold (build2 (MAX_EXPR, type, arg1, arg2)));
8355 break;
8356 case NE_EXPR:
8357 break;
8358 default:
8359 abort ();
8363 /* If the second operand is simpler than the third, swap them
8364 since that produces better jump optimization results. */
8365 if (tree_swap_operands_p (TREE_OPERAND (t, 1),
8366 TREE_OPERAND (t, 2), false))
8368 /* See if this can be inverted. If it can't, possibly because
8369 it was a floating-point inequality comparison, don't do
8370 anything. */
8371 tem = invert_truthvalue (arg0);
8373 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8374 return fold (build3 (code, type, tem,
8375 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)));
8378 /* Convert A ? 1 : 0 to simply A. */
8379 if (integer_onep (TREE_OPERAND (t, 1))
8380 && integer_zerop (TREE_OPERAND (t, 2))
8381 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
8382 call to fold will try to move the conversion inside
8383 a COND, which will recurse. In that case, the COND_EXPR
8384 is probably the best choice, so leave it alone. */
8385 && type == TREE_TYPE (arg0))
8386 return pedantic_non_lvalue (arg0);
8388 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
8389 over COND_EXPR in cases such as floating point comparisons. */
8390 if (integer_zerop (TREE_OPERAND (t, 1))
8391 && integer_onep (TREE_OPERAND (t, 2))
8392 && truth_value_p (TREE_CODE (arg0)))
8393 return pedantic_non_lvalue (fold_convert (type,
8394 invert_truthvalue (arg0)));
8396 /* Look for expressions of the form A & 2 ? 2 : 0. The result of this
8397 operation is simply A & 2. */
8399 if (integer_zerop (TREE_OPERAND (t, 2))
8400 && TREE_CODE (arg0) == NE_EXPR
8401 && integer_zerop (TREE_OPERAND (arg0, 1))
8402 && integer_pow2p (arg1)
8403 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
8404 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
8405 arg1, OEP_ONLY_CONST))
8406 return pedantic_non_lvalue (fold_convert (type,
8407 TREE_OPERAND (arg0, 0)));
8409 /* Convert A ? B : 0 into A && B if A and B are truth values. */
8410 if (integer_zerop (TREE_OPERAND (t, 2))
8411 && truth_value_p (TREE_CODE (arg0))
8412 && truth_value_p (TREE_CODE (arg1)))
8413 return pedantic_non_lvalue (fold (build2 (TRUTH_ANDIF_EXPR, type,
8414 arg0, arg1)));
8416 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
8417 if (integer_onep (TREE_OPERAND (t, 2))
8418 && truth_value_p (TREE_CODE (arg0))
8419 && truth_value_p (TREE_CODE (arg1)))
8421 /* Only perform transformation if ARG0 is easily inverted. */
8422 tem = invert_truthvalue (arg0);
8423 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8424 return pedantic_non_lvalue (fold (build2 (TRUTH_ORIF_EXPR, type,
8425 tem, arg1)));
8428 return t;
8430 case COMPOUND_EXPR:
8431 /* When pedantic, a compound expression can be neither an lvalue
8432 nor an integer constant expression. */
8433 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
8434 return t;
8435 /* Don't let (0, 0) be null pointer constant. */
8436 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
8437 : fold_convert (type, arg1);
8438 return pedantic_non_lvalue (tem);
8440 case COMPLEX_EXPR:
8441 if (wins)
8442 return build_complex (type, arg0, arg1);
8443 return t;
8445 case REALPART_EXPR:
8446 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8447 return t;
8448 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8449 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8450 TREE_OPERAND (arg0, 1));
8451 else if (TREE_CODE (arg0) == COMPLEX_CST)
8452 return TREE_REALPART (arg0);
8453 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8454 return fold (build2 (TREE_CODE (arg0), type,
8455 fold (build1 (REALPART_EXPR, type,
8456 TREE_OPERAND (arg0, 0))),
8457 fold (build1 (REALPART_EXPR, type,
8458 TREE_OPERAND (arg0, 1)))));
8459 return t;
8461 case IMAGPART_EXPR:
8462 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8463 return fold_convert (type, integer_zero_node);
8464 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8465 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8466 TREE_OPERAND (arg0, 0));
8467 else if (TREE_CODE (arg0) == COMPLEX_CST)
8468 return TREE_IMAGPART (arg0);
8469 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8470 return fold (build2 (TREE_CODE (arg0), type,
8471 fold (build1 (IMAGPART_EXPR, type,
8472 TREE_OPERAND (arg0, 0))),
8473 fold (build1 (IMAGPART_EXPR, type,
8474 TREE_OPERAND (arg0, 1)))));
8475 return t;
8477 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
8478 appropriate. */
8479 case CLEANUP_POINT_EXPR:
8480 if (! has_cleanups (arg0))
8481 return TREE_OPERAND (t, 0);
8484 enum tree_code code0 = TREE_CODE (arg0);
8485 int kind0 = TREE_CODE_CLASS (code0);
8486 tree arg00 = TREE_OPERAND (arg0, 0);
8487 tree arg01;
8489 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
8490 return fold (build1 (code0, type,
8491 fold (build1 (CLEANUP_POINT_EXPR,
8492 TREE_TYPE (arg00), arg00))));
8494 if (kind0 == '<' || kind0 == '2'
8495 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
8496 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
8497 || code0 == TRUTH_XOR_EXPR)
8499 arg01 = TREE_OPERAND (arg0, 1);
8501 if (TREE_CONSTANT (arg00)
8502 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
8503 && ! has_cleanups (arg00)))
8504 return fold (build2 (code0, type, arg00,
8505 fold (build1 (CLEANUP_POINT_EXPR,
8506 TREE_TYPE (arg01), arg01))));
8508 if (TREE_CONSTANT (arg01))
8509 return fold (build2 (code0, type,
8510 fold (build1 (CLEANUP_POINT_EXPR,
8511 TREE_TYPE (arg00), arg00)),
8512 arg01));
8515 return t;
8518 case CALL_EXPR:
8519 /* Check for a built-in function. */
8520 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
8521 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
8522 == FUNCTION_DECL)
8523 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
8525 tree tmp = fold_builtin (t);
8526 if (tmp)
8527 return tmp;
8529 return t;
8531 default:
8532 return t;
8533 } /* switch (code) */
8536 #ifdef ENABLE_FOLD_CHECKING
8537 #undef fold
8539 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
8540 static void fold_check_failed (tree, tree);
8541 void print_fold_checksum (tree);
8543 /* When --enable-checking=fold, compute a digest of expr before
8544 and after actual fold call to see if fold did not accidentally
8545 change original expr. */
8547 tree
8548 fold (tree expr)
8550 tree ret;
8551 struct md5_ctx ctx;
8552 unsigned char checksum_before[16], checksum_after[16];
8553 htab_t ht;
8555 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8556 md5_init_ctx (&ctx);
8557 fold_checksum_tree (expr, &ctx, ht);
8558 md5_finish_ctx (&ctx, checksum_before);
8559 htab_empty (ht);
8561 ret = fold_1 (expr);
8563 md5_init_ctx (&ctx);
8564 fold_checksum_tree (expr, &ctx, ht);
8565 md5_finish_ctx (&ctx, checksum_after);
8566 htab_delete (ht);
8568 if (memcmp (checksum_before, checksum_after, 16))
8569 fold_check_failed (expr, ret);
8571 return ret;
8574 void
8575 print_fold_checksum (tree expr)
8577 struct md5_ctx ctx;
8578 unsigned char checksum[16], cnt;
8579 htab_t ht;
8581 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
8582 md5_init_ctx (&ctx);
8583 fold_checksum_tree (expr, &ctx, ht);
8584 md5_finish_ctx (&ctx, checksum);
8585 htab_delete (ht);
8586 for (cnt = 0; cnt < 16; ++cnt)
8587 fprintf (stderr, "%02x", checksum[cnt]);
8588 putc ('\n', stderr);
8591 static void
8592 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
8594 internal_error ("fold check: original tree changed by fold");
8597 static void
8598 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
8600 void **slot;
8601 enum tree_code code;
8602 char buf[sizeof (struct tree_decl)];
8603 int i, len;
8605 if (sizeof (struct tree_exp) + 5 * sizeof (tree)
8606 > sizeof (struct tree_decl)
8607 || sizeof (struct tree_type) > sizeof (struct tree_decl))
8608 abort ();
8609 if (expr == NULL)
8610 return;
8611 slot = htab_find_slot (ht, expr, INSERT);
8612 if (*slot != NULL)
8613 return;
8614 *slot = expr;
8615 code = TREE_CODE (expr);
8616 if (code == SAVE_EXPR && SAVE_EXPR_NOPLACEHOLDER (expr))
8618 /* Allow SAVE_EXPR_NOPLACEHOLDER flag to be modified. */
8619 memcpy (buf, expr, tree_size (expr));
8620 expr = (tree) buf;
8621 SAVE_EXPR_NOPLACEHOLDER (expr) = 0;
8623 else if (TREE_CODE_CLASS (code) == 'd' && DECL_ASSEMBLER_NAME_SET_P (expr))
8625 /* Allow DECL_ASSEMBLER_NAME to be modified. */
8626 memcpy (buf, expr, tree_size (expr));
8627 expr = (tree) buf;
8628 SET_DECL_ASSEMBLER_NAME (expr, NULL);
8630 else if (TREE_CODE_CLASS (code) == 't'
8631 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)))
8633 /* Allow TYPE_POINTER_TO and TYPE_REFERENCE_TO to be modified. */
8634 memcpy (buf, expr, tree_size (expr));
8635 expr = (tree) buf;
8636 TYPE_POINTER_TO (expr) = NULL;
8637 TYPE_REFERENCE_TO (expr) = NULL;
8639 md5_process_bytes (expr, tree_size (expr), ctx);
8640 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
8641 if (TREE_CODE_CLASS (code) != 't' && TREE_CODE_CLASS (code) != 'd')
8642 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
8643 len = TREE_CODE_LENGTH (code);
8644 switch (TREE_CODE_CLASS (code))
8646 case 'c':
8647 switch (code)
8649 case STRING_CST:
8650 md5_process_bytes (TREE_STRING_POINTER (expr),
8651 TREE_STRING_LENGTH (expr), ctx);
8652 break;
8653 case COMPLEX_CST:
8654 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
8655 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
8656 break;
8657 case VECTOR_CST:
8658 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
8659 break;
8660 default:
8661 break;
8663 break;
8664 case 'x':
8665 switch (code)
8667 case TREE_LIST:
8668 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
8669 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
8670 break;
8671 case TREE_VEC:
8672 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
8673 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
8674 break;
8675 default:
8676 break;
8678 break;
8679 case 'e':
8680 switch (code)
8682 case SAVE_EXPR: len = 2; break;
8683 case GOTO_SUBROUTINE_EXPR: len = 0; break;
8684 case RTL_EXPR: len = 0; break;
8685 case WITH_CLEANUP_EXPR: len = 2; break;
8686 default: break;
8688 /* Fall through. */
8689 case 'r':
8690 case '<':
8691 case '1':
8692 case '2':
8693 case 's':
8694 for (i = 0; i < len; ++i)
8695 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
8696 break;
8697 case 'd':
8698 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
8699 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
8700 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
8701 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
8702 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
8703 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
8704 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
8705 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
8706 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
8707 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
8708 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
8709 break;
8710 case 't':
8711 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
8712 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
8713 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
8714 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
8715 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
8716 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
8717 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
8718 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
8719 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
8720 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
8721 break;
8722 default:
8723 break;
8727 #endif
8729 /* Perform constant folding and related simplification of initializer
8730 expression EXPR. This behaves identically to "fold" but ignores
8731 potential run-time traps and exceptions that fold must preserve. */
8733 tree
8734 fold_initializer (tree expr)
8736 int saved_signaling_nans = flag_signaling_nans;
8737 int saved_trapping_math = flag_trapping_math;
8738 int saved_trapv = flag_trapv;
8739 tree result;
8741 flag_signaling_nans = 0;
8742 flag_trapping_math = 0;
8743 flag_trapv = 0;
8745 result = fold (expr);
8747 flag_signaling_nans = saved_signaling_nans;
8748 flag_trapping_math = saved_trapping_math;
8749 flag_trapv = saved_trapv;
8751 return result;
8754 /* Determine if first argument is a multiple of second argument. Return 0 if
8755 it is not, or we cannot easily determined it to be.
8757 An example of the sort of thing we care about (at this point; this routine
8758 could surely be made more general, and expanded to do what the *_DIV_EXPR's
8759 fold cases do now) is discovering that
8761 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8763 is a multiple of
8765 SAVE_EXPR (J * 8)
8767 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
8769 This code also handles discovering that
8771 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
8773 is a multiple of 8 so we don't have to worry about dealing with a
8774 possible remainder.
8776 Note that we *look* inside a SAVE_EXPR only to determine how it was
8777 calculated; it is not safe for fold to do much of anything else with the
8778 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
8779 at run time. For example, the latter example above *cannot* be implemented
8780 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
8781 evaluation time of the original SAVE_EXPR is not necessarily the same at
8782 the time the new expression is evaluated. The only optimization of this
8783 sort that would be valid is changing
8785 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
8787 divided by 8 to
8789 SAVE_EXPR (I) * SAVE_EXPR (J)
8791 (where the same SAVE_EXPR (J) is used in the original and the
8792 transformed version). */
8794 static int
8795 multiple_of_p (tree type, tree top, tree bottom)
8797 if (operand_equal_p (top, bottom, 0))
8798 return 1;
8800 if (TREE_CODE (type) != INTEGER_TYPE)
8801 return 0;
8803 switch (TREE_CODE (top))
8805 case MULT_EXPR:
8806 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8807 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8809 case PLUS_EXPR:
8810 case MINUS_EXPR:
8811 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
8812 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
8814 case LSHIFT_EXPR:
8815 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
8817 tree op1, t1;
8819 op1 = TREE_OPERAND (top, 1);
8820 /* const_binop may not detect overflow correctly,
8821 so check for it explicitly here. */
8822 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
8823 > TREE_INT_CST_LOW (op1)
8824 && TREE_INT_CST_HIGH (op1) == 0
8825 && 0 != (t1 = fold_convert (type,
8826 const_binop (LSHIFT_EXPR,
8827 size_one_node,
8828 op1, 0)))
8829 && ! TREE_OVERFLOW (t1))
8830 return multiple_of_p (type, t1, bottom);
8832 return 0;
8834 case NOP_EXPR:
8835 /* Can't handle conversions from non-integral or wider integral type. */
8836 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
8837 || (TYPE_PRECISION (type)
8838 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
8839 return 0;
8841 /* .. fall through ... */
8843 case SAVE_EXPR:
8844 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
8846 case INTEGER_CST:
8847 if (TREE_CODE (bottom) != INTEGER_CST
8848 || (TYPE_UNSIGNED (type)
8849 && (tree_int_cst_sgn (top) < 0
8850 || tree_int_cst_sgn (bottom) < 0)))
8851 return 0;
8852 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
8853 top, bottom, 0));
8855 default:
8856 return 0;
8860 /* Return true if `t' is known to be non-negative. */
8863 tree_expr_nonnegative_p (tree t)
8865 switch (TREE_CODE (t))
8867 case ABS_EXPR:
8868 return 1;
8870 case INTEGER_CST:
8871 return tree_int_cst_sgn (t) >= 0;
8873 case REAL_CST:
8874 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
8876 case PLUS_EXPR:
8877 if (FLOAT_TYPE_P (TREE_TYPE (t)))
8878 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8879 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8881 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
8882 both unsigned and at least 2 bits shorter than the result. */
8883 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8884 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8885 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8887 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8888 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8889 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
8890 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
8892 unsigned int prec = MAX (TYPE_PRECISION (inner1),
8893 TYPE_PRECISION (inner2)) + 1;
8894 return prec < TYPE_PRECISION (TREE_TYPE (t));
8897 break;
8899 case MULT_EXPR:
8900 if (FLOAT_TYPE_P (TREE_TYPE (t)))
8902 /* x * x for floating point x is always non-negative. */
8903 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
8904 return 1;
8905 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8906 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8909 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
8910 both unsigned and their total bits is shorter than the result. */
8911 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
8912 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
8913 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
8915 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
8916 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
8917 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
8918 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
8919 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
8920 < TYPE_PRECISION (TREE_TYPE (t));
8922 return 0;
8924 case TRUNC_DIV_EXPR:
8925 case CEIL_DIV_EXPR:
8926 case FLOOR_DIV_EXPR:
8927 case ROUND_DIV_EXPR:
8928 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8929 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8931 case TRUNC_MOD_EXPR:
8932 case CEIL_MOD_EXPR:
8933 case FLOOR_MOD_EXPR:
8934 case ROUND_MOD_EXPR:
8935 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8937 case RDIV_EXPR:
8938 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8939 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8941 case BIT_AND_EXPR:
8942 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
8943 || tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8944 case BIT_IOR_EXPR:
8945 case BIT_XOR_EXPR:
8946 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8947 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8949 case NOP_EXPR:
8951 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
8952 tree outer_type = TREE_TYPE (t);
8954 if (TREE_CODE (outer_type) == REAL_TYPE)
8956 if (TREE_CODE (inner_type) == REAL_TYPE)
8957 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8958 if (TREE_CODE (inner_type) == INTEGER_TYPE)
8960 if (TYPE_UNSIGNED (inner_type))
8961 return 1;
8962 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8965 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
8967 if (TREE_CODE (inner_type) == REAL_TYPE)
8968 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
8969 if (TREE_CODE (inner_type) == INTEGER_TYPE)
8970 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
8971 && TYPE_UNSIGNED (inner_type);
8974 break;
8976 case COND_EXPR:
8977 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
8978 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
8979 case COMPOUND_EXPR:
8980 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8981 case MIN_EXPR:
8982 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8983 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8984 case MAX_EXPR:
8985 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
8986 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8987 case MODIFY_EXPR:
8988 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8989 case BIND_EXPR:
8990 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
8991 case SAVE_EXPR:
8992 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8993 case NON_LVALUE_EXPR:
8994 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8995 case FLOAT_EXPR:
8996 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
8997 case RTL_EXPR:
8998 return rtl_expr_nonnegative_p (RTL_EXPR_RTL (t));
9000 case CALL_EXPR:
9002 tree fndecl = get_callee_fndecl (t);
9003 tree arglist = TREE_OPERAND (t, 1);
9004 if (fndecl
9005 && DECL_BUILT_IN (fndecl)
9006 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD)
9007 switch (DECL_FUNCTION_CODE (fndecl))
9009 #define CASE_BUILTIN_F(BUILT_IN_FN) \
9010 case BUILT_IN_FN: case BUILT_IN_FN##F: case BUILT_IN_FN##L:
9011 #define CASE_BUILTIN_I(BUILT_IN_FN) \
9012 case BUILT_IN_FN: case BUILT_IN_FN##L: case BUILT_IN_FN##LL:
9014 CASE_BUILTIN_F (BUILT_IN_ACOS)
9015 CASE_BUILTIN_F (BUILT_IN_ACOSH)
9016 CASE_BUILTIN_F (BUILT_IN_CABS)
9017 CASE_BUILTIN_F (BUILT_IN_COSH)
9018 CASE_BUILTIN_F (BUILT_IN_ERFC)
9019 CASE_BUILTIN_F (BUILT_IN_EXP)
9020 CASE_BUILTIN_F (BUILT_IN_EXP10)
9021 CASE_BUILTIN_F (BUILT_IN_EXP2)
9022 CASE_BUILTIN_F (BUILT_IN_FABS)
9023 CASE_BUILTIN_F (BUILT_IN_FDIM)
9024 CASE_BUILTIN_F (BUILT_IN_FREXP)
9025 CASE_BUILTIN_F (BUILT_IN_HYPOT)
9026 CASE_BUILTIN_F (BUILT_IN_POW10)
9027 CASE_BUILTIN_F (BUILT_IN_SQRT)
9028 CASE_BUILTIN_I (BUILT_IN_FFS)
9029 CASE_BUILTIN_I (BUILT_IN_PARITY)
9030 CASE_BUILTIN_I (BUILT_IN_POPCOUNT)
9031 /* Always true. */
9032 return 1;
9034 CASE_BUILTIN_F (BUILT_IN_ASINH)
9035 CASE_BUILTIN_F (BUILT_IN_ATAN)
9036 CASE_BUILTIN_F (BUILT_IN_ATANH)
9037 CASE_BUILTIN_F (BUILT_IN_CBRT)
9038 CASE_BUILTIN_F (BUILT_IN_CEIL)
9039 CASE_BUILTIN_F (BUILT_IN_ERF)
9040 CASE_BUILTIN_F (BUILT_IN_EXPM1)
9041 CASE_BUILTIN_F (BUILT_IN_FLOOR)
9042 CASE_BUILTIN_F (BUILT_IN_FMOD)
9043 CASE_BUILTIN_F (BUILT_IN_LDEXP)
9044 CASE_BUILTIN_F (BUILT_IN_LLRINT)
9045 CASE_BUILTIN_F (BUILT_IN_LLROUND)
9046 CASE_BUILTIN_F (BUILT_IN_LRINT)
9047 CASE_BUILTIN_F (BUILT_IN_LROUND)
9048 CASE_BUILTIN_F (BUILT_IN_MODF)
9049 CASE_BUILTIN_F (BUILT_IN_NEARBYINT)
9050 CASE_BUILTIN_F (BUILT_IN_POW)
9051 CASE_BUILTIN_F (BUILT_IN_RINT)
9052 CASE_BUILTIN_F (BUILT_IN_ROUND)
9053 CASE_BUILTIN_F (BUILT_IN_SIGNBIT)
9054 CASE_BUILTIN_F (BUILT_IN_SINH)
9055 CASE_BUILTIN_F (BUILT_IN_TANH)
9056 CASE_BUILTIN_F (BUILT_IN_TRUNC)
9057 /* True if the 1st argument is nonnegative. */
9058 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
9060 CASE_BUILTIN_F(BUILT_IN_FMAX)
9061 /* True if the 1st OR 2nd arguments are nonnegative. */
9062 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9063 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9065 CASE_BUILTIN_F(BUILT_IN_FMIN)
9066 /* True if the 1st AND 2nd arguments are nonnegative. */
9067 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9068 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9070 CASE_BUILTIN_F(BUILT_IN_COPYSIGN)
9071 /* True if the 2nd argument is nonnegative. */
9072 return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9074 default:
9075 break;
9076 #undef CASE_BUILTIN_F
9077 #undef CASE_BUILTIN_I
9081 /* ... fall through ... */
9083 default:
9084 if (truth_value_p (TREE_CODE (t)))
9085 /* Truth values evaluate to 0 or 1, which is nonnegative. */
9086 return 1;
9089 /* We don't know sign of `t', so be conservative and return false. */
9090 return 0;
9093 /* Return true when T is an address and is known to be nonzero.
9094 For floating point we further ensure that T is not denormal.
9095 Similar logic is present in nonzero_address in rtlanal.h */
9097 static bool
9098 tree_expr_nonzero_p (tree t)
9100 tree type = TREE_TYPE (t);
9102 /* Doing something useful for floating point would need more work. */
9103 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9104 return false;
9106 switch (TREE_CODE (t))
9108 case ABS_EXPR:
9109 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9110 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9112 case INTEGER_CST:
9113 return !integer_zerop (t);
9115 case PLUS_EXPR:
9116 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9118 /* With the presence of negative values it is hard
9119 to say something. */
9120 if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9121 || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9122 return false;
9123 /* One of operands must be positive and the other non-negative. */
9124 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9125 || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9127 break;
9129 case MULT_EXPR:
9130 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9132 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9133 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9135 break;
9137 case NOP_EXPR:
9139 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
9140 tree outer_type = TREE_TYPE (t);
9142 return (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (outer_type)
9143 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
9145 break;
9147 case ADDR_EXPR:
9148 /* Weak declarations may link to NULL. */
9149 if (DECL_P (TREE_OPERAND (t, 0)))
9150 return !DECL_WEAK (TREE_OPERAND (t, 0));
9151 /* Constants and all other cases are never weak. */
9152 return true;
9154 case COND_EXPR:
9155 return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9156 && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
9158 case MIN_EXPR:
9159 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9160 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9162 case MAX_EXPR:
9163 if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
9165 /* When both operands are nonzero, then MAX must be too. */
9166 if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
9167 return true;
9169 /* MAX where operand 0 is positive is positive. */
9170 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9172 /* MAX where operand 1 is positive is positive. */
9173 else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9174 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9175 return true;
9176 break;
9178 case COMPOUND_EXPR:
9179 case MODIFY_EXPR:
9180 case BIND_EXPR:
9181 return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
9183 case SAVE_EXPR:
9184 case NON_LVALUE_EXPR:
9185 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9187 case BIT_IOR_EXPR:
9188 return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9189 || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9191 default:
9192 break;
9194 return false;
9197 /* Return true if `r' is known to be non-negative.
9198 Only handles constants at the moment. */
9201 rtl_expr_nonnegative_p (rtx r)
9203 switch (GET_CODE (r))
9205 case CONST_INT:
9206 return INTVAL (r) >= 0;
9208 case CONST_DOUBLE:
9209 if (GET_MODE (r) == VOIDmode)
9210 return CONST_DOUBLE_HIGH (r) >= 0;
9211 return 0;
9213 case CONST_VECTOR:
9215 int units, i;
9216 rtx elt;
9218 units = CONST_VECTOR_NUNITS (r);
9220 for (i = 0; i < units; ++i)
9222 elt = CONST_VECTOR_ELT (r, i);
9223 if (!rtl_expr_nonnegative_p (elt))
9224 return 0;
9227 return 1;
9230 case SYMBOL_REF:
9231 case LABEL_REF:
9232 /* These are always nonnegative. */
9233 return 1;
9235 default:
9236 return 0;
9241 /* See if we are applying CODE, a relational to the highest or lowest
9242 possible integer of TYPE. If so, then the result is a compile
9243 time constant. */
9245 static tree
9246 fold_relational_hi_lo (enum tree_code *code_p, const tree type, tree *op0_p,
9247 tree *op1_p)
9249 tree op0 = *op0_p;
9250 tree op1 = *op1_p;
9251 enum tree_code code = *code_p;
9252 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (op1)));
9254 if (TREE_CODE (op1) == INTEGER_CST
9255 && ! TREE_CONSTANT_OVERFLOW (op1)
9256 && width <= HOST_BITS_PER_WIDE_INT
9257 && (INTEGRAL_TYPE_P (TREE_TYPE (op1))
9258 || POINTER_TYPE_P (TREE_TYPE (op1))))
9260 unsigned HOST_WIDE_INT signed_max;
9261 unsigned HOST_WIDE_INT max, min;
9263 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
9265 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
9267 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
9268 min = 0;
9270 else
9272 max = signed_max;
9273 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9276 if (TREE_INT_CST_HIGH (op1) == 0
9277 && TREE_INT_CST_LOW (op1) == max)
9278 switch (code)
9280 case GT_EXPR:
9281 return omit_one_operand (type, integer_zero_node, op0);
9283 case GE_EXPR:
9284 *code_p = EQ_EXPR;
9285 break;
9286 case LE_EXPR:
9287 return omit_one_operand (type, integer_one_node, op0);
9289 case LT_EXPR:
9290 *code_p = NE_EXPR;
9291 break;
9293 /* The GE_EXPR and LT_EXPR cases above are not normally
9294 reached because of previous transformations. */
9296 default:
9297 break;
9299 else if (TREE_INT_CST_HIGH (op1) == 0
9300 && TREE_INT_CST_LOW (op1) == max - 1)
9301 switch (code)
9303 case GT_EXPR:
9304 *code_p = EQ_EXPR;
9305 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9306 break;
9307 case LE_EXPR:
9308 *code_p = NE_EXPR;
9309 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9310 break;
9311 default:
9312 break;
9314 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9315 && TREE_INT_CST_LOW (op1) == min)
9316 switch (code)
9318 case LT_EXPR:
9319 return omit_one_operand (type, integer_zero_node, op0);
9321 case LE_EXPR:
9322 *code_p = EQ_EXPR;
9323 break;
9325 case GE_EXPR:
9326 return omit_one_operand (type, integer_one_node, op0);
9328 case GT_EXPR:
9329 *code_p = NE_EXPR;
9330 break;
9332 default:
9333 break;
9335 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9336 && TREE_INT_CST_LOW (op1) == min + 1)
9337 switch (code)
9339 case GE_EXPR:
9340 *code_p = NE_EXPR;
9341 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9342 break;
9343 case LT_EXPR:
9344 *code_p = EQ_EXPR;
9345 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9346 break;
9347 default:
9348 break;
9351 else if (TREE_INT_CST_HIGH (op1) == 0
9352 && TREE_INT_CST_LOW (op1) == signed_max
9353 && TYPE_UNSIGNED (TREE_TYPE (op1))
9354 /* signed_type does not work on pointer types. */
9355 && INTEGRAL_TYPE_P (TREE_TYPE (op1)))
9357 /* The following case also applies to X < signed_max+1
9358 and X >= signed_max+1 because previous transformations. */
9359 if (code == LE_EXPR || code == GT_EXPR)
9361 tree st0, st1, exp, retval;
9362 st0 = lang_hooks.types.signed_type (TREE_TYPE (op0));
9363 st1 = lang_hooks.types.signed_type (TREE_TYPE (op1));
9365 exp = build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
9366 type,
9367 fold_convert (st0, op0),
9368 fold_convert (st1, integer_zero_node));
9370 retval
9371 = nondestructive_fold_binary_to_constant (TREE_CODE (exp),
9372 TREE_TYPE (exp),
9373 TREE_OPERAND (exp, 0),
9374 TREE_OPERAND (exp, 1));
9376 /* If we are in gimple form, then returning EXP would create
9377 non-gimple expressions. Clearing it is safe and insures
9378 we do not allow a non-gimple expression to escape. */
9379 if (in_gimple_form)
9380 exp = NULL;
9382 return (retval ? retval : exp);
9387 return NULL_TREE;
9391 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
9392 attempt to fold the expression to a constant without modifying TYPE,
9393 OP0 or OP1.
9395 If the expression could be simplified to a constant, then return
9396 the constant. If the expression would not be simplified to a
9397 constant, then return NULL_TREE.
9399 Note this is primarily designed to be called after gimplification
9400 of the tree structures and when at least one operand is a constant.
9401 As a result of those simplifying assumptions this routine is far
9402 simpler than the generic fold routine. */
9404 tree
9405 nondestructive_fold_binary_to_constant (enum tree_code code, tree type,
9406 tree op0, tree op1)
9408 int wins = 1;
9409 tree subop0;
9410 tree subop1;
9411 tree tem;
9413 /* If this is a commutative operation, and ARG0 is a constant, move it
9414 to ARG1 to reduce the number of tests below. */
9415 if (commutative_tree_code (code)
9416 && (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST))
9418 tem = op0;
9419 op0 = op1;
9420 op1 = tem;
9423 /* If either operand is a complex type, extract its real component. */
9424 if (TREE_CODE (op0) == COMPLEX_CST)
9425 subop0 = TREE_REALPART (op0);
9426 else
9427 subop0 = op0;
9429 if (TREE_CODE (op1) == COMPLEX_CST)
9430 subop1 = TREE_REALPART (op1);
9431 else
9432 subop1 = op1;
9434 /* Note if either argument is not a real or integer constant.
9435 With a few exceptions, simplification is limited to cases
9436 where both arguments are constants. */
9437 if ((TREE_CODE (subop0) != INTEGER_CST
9438 && TREE_CODE (subop0) != REAL_CST)
9439 || (TREE_CODE (subop1) != INTEGER_CST
9440 && TREE_CODE (subop1) != REAL_CST))
9441 wins = 0;
9443 switch (code)
9445 case PLUS_EXPR:
9446 /* (plus (address) (const_int)) is a constant. */
9447 if (TREE_CODE (op0) == PLUS_EXPR
9448 && TREE_CODE (op1) == INTEGER_CST
9449 && (TREE_CODE (TREE_OPERAND (op0, 0)) == ADDR_EXPR
9450 || (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
9451 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (op0, 0), 0))
9452 == ADDR_EXPR)))
9453 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9455 return build2 (PLUS_EXPR, type, TREE_OPERAND (op0, 0),
9456 const_binop (PLUS_EXPR, op1,
9457 TREE_OPERAND (op0, 1), 0));
9459 case BIT_XOR_EXPR:
9461 binary:
9462 if (!wins)
9463 return NULL_TREE;
9465 /* Both arguments are constants. Simplify. */
9466 tem = const_binop (code, op0, op1, 0);
9467 if (tem != NULL_TREE)
9469 /* The return value should always have the same type as
9470 the original expression. */
9471 if (TREE_TYPE (tem) != type)
9472 tem = fold_convert (type, tem);
9474 return tem;
9476 return NULL_TREE;
9478 case MINUS_EXPR:
9479 /* Fold &x - &x. This can happen from &x.foo - &x.
9480 This is unsafe for certain floats even in non-IEEE formats.
9481 In IEEE, it is unsafe because it does wrong for NaNs.
9482 Also note that operand_equal_p is always false if an
9483 operand is volatile. */
9484 if (! FLOAT_TYPE_P (type) && operand_equal_p (op0, op1, 0))
9485 return fold_convert (type, integer_zero_node);
9487 goto binary;
9489 case MULT_EXPR:
9490 case BIT_AND_EXPR:
9491 /* Special case multiplication or bitwise AND where one argument
9492 is zero. */
9493 if (! FLOAT_TYPE_P (type) && integer_zerop (op1))
9494 return omit_one_operand (type, op1, op0);
9495 else
9496 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (op0)))
9497 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
9498 && real_zerop (op1))
9499 return omit_one_operand (type, op1, op0);
9501 goto binary;
9503 case BIT_IOR_EXPR:
9504 /* Special case when we know the result will be all ones. */
9505 if (integer_all_onesp (op1))
9506 return omit_one_operand (type, op1, op0);
9508 goto binary;
9510 case TRUNC_DIV_EXPR:
9511 case ROUND_DIV_EXPR:
9512 case FLOOR_DIV_EXPR:
9513 case CEIL_DIV_EXPR:
9514 case EXACT_DIV_EXPR:
9515 case TRUNC_MOD_EXPR:
9516 case ROUND_MOD_EXPR:
9517 case FLOOR_MOD_EXPR:
9518 case CEIL_MOD_EXPR:
9519 case RDIV_EXPR:
9520 /* Division by zero is undefined. */
9521 if (integer_zerop (op1))
9522 return NULL_TREE;
9524 if (TREE_CODE (op1) == REAL_CST
9525 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (op1)))
9526 && real_zerop (op1))
9527 return NULL_TREE;
9529 goto binary;
9531 case MIN_EXPR:
9532 if (INTEGRAL_TYPE_P (type)
9533 && operand_equal_p (op1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
9534 return omit_one_operand (type, op1, op0);
9536 goto binary;
9538 case MAX_EXPR:
9539 if (INTEGRAL_TYPE_P (type)
9540 && TYPE_MAX_VALUE (type)
9541 && operand_equal_p (op1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
9542 return omit_one_operand (type, op1, op0);
9544 goto binary;
9546 case RSHIFT_EXPR:
9547 /* Optimize -1 >> x for arithmetic right shifts. */
9548 if (integer_all_onesp (op0) && ! TYPE_UNSIGNED (type))
9549 return omit_one_operand (type, op0, op1);
9550 /* ... fall through ... */
9552 case LSHIFT_EXPR:
9553 if (integer_zerop (op0))
9554 return omit_one_operand (type, op0, op1);
9556 /* Since negative shift count is not well-defined, don't
9557 try to compute it in the compiler. */
9558 if (TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sgn (op1) < 0)
9559 return NULL_TREE;
9561 goto binary;
9563 case LROTATE_EXPR:
9564 case RROTATE_EXPR:
9565 /* -1 rotated either direction by any amount is still -1. */
9566 if (integer_all_onesp (op0))
9567 return omit_one_operand (type, op0, op1);
9569 /* 0 rotated either direction by any amount is still zero. */
9570 if (integer_zerop (op0))
9571 return omit_one_operand (type, op0, op1);
9573 goto binary;
9575 case COMPLEX_EXPR:
9576 if (wins)
9577 return build_complex (type, op0, op1);
9578 return NULL_TREE;
9580 case LT_EXPR:
9581 case LE_EXPR:
9582 case GT_EXPR:
9583 case GE_EXPR:
9584 case EQ_EXPR:
9585 case NE_EXPR:
9586 /* If one arg is a real or integer constant, put it last. */
9587 if ((TREE_CODE (op0) == INTEGER_CST
9588 && TREE_CODE (op1) != INTEGER_CST)
9589 || (TREE_CODE (op0) == REAL_CST
9590 && TREE_CODE (op0) != REAL_CST))
9592 tree temp;
9594 temp = op0;
9595 op0 = op1;
9596 op1 = temp;
9597 code = swap_tree_comparison (code);
9600 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
9601 This transformation affects the cases which are handled in later
9602 optimizations involving comparisons with non-negative constants. */
9603 if (TREE_CODE (op1) == INTEGER_CST
9604 && TREE_CODE (op0) != INTEGER_CST
9605 && tree_int_cst_sgn (op1) > 0)
9607 switch (code)
9609 case GE_EXPR:
9610 code = GT_EXPR;
9611 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9612 break;
9614 case LT_EXPR:
9615 code = LE_EXPR;
9616 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9617 break;
9619 default:
9620 break;
9624 tem = fold_relational_hi_lo (&code, type, &op0, &op1);
9625 if (tem)
9626 return tem;
9628 if (!wins)
9629 return NULL_TREE;
9631 return fold_relational_const (code, type, op0, op1);
9633 case RANGE_EXPR:
9634 /* This could probably be handled. */
9635 return NULL_TREE;
9637 case TRUTH_AND_EXPR:
9638 /* If second arg is constant zero, result is zero, but first arg
9639 must be evaluated. */
9640 if (integer_zerop (op1))
9641 return omit_one_operand (type, op1, op0);
9642 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
9643 case will be handled here. */
9644 if (integer_zerop (op0))
9645 return omit_one_operand (type, op0, op1);
9646 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9647 return constant_boolean_node (true, type);
9648 return NULL_TREE;
9650 case TRUTH_OR_EXPR:
9651 /* If second arg is constant true, result is true, but we must
9652 evaluate first arg. */
9653 if (TREE_CODE (op1) == INTEGER_CST && ! integer_zerop (op1))
9654 return omit_one_operand (type, op1, op0);
9655 /* Likewise for first arg, but note this only occurs here for
9656 TRUTH_OR_EXPR. */
9657 if (TREE_CODE (op0) == INTEGER_CST && ! integer_zerop (op0))
9658 return omit_one_operand (type, op0, op1);
9659 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9660 return constant_boolean_node (false, type);
9661 return NULL_TREE;
9663 case TRUTH_XOR_EXPR:
9664 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9666 int x = ! integer_zerop (op0) ^ ! integer_zerop (op1);
9667 return constant_boolean_node (x, type);
9669 return NULL_TREE;
9671 default:
9672 return NULL_TREE;
9676 /* Given the components of a unary expression CODE, TYPE and OP0,
9677 attempt to fold the expression to a constant without modifying
9678 TYPE or OP0.
9680 If the expression could be simplified to a constant, then return
9681 the constant. If the expression would not be simplified to a
9682 constant, then return NULL_TREE.
9684 Note this is primarily designed to be called after gimplification
9685 of the tree structures and when op0 is a constant. As a result
9686 of those simplifying assumptions this routine is far simpler than
9687 the generic fold routine. */
9689 tree
9690 nondestructive_fold_unary_to_constant (enum tree_code code, tree type,
9691 tree op0)
9693 tree t;
9695 /* Make sure we have a suitable constant argument. */
9696 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
9698 tree subop;
9700 if (TREE_CODE (op0) == COMPLEX_CST)
9701 subop = TREE_REALPART (op0);
9702 else
9703 subop = op0;
9705 if (TREE_CODE (subop) != INTEGER_CST && TREE_CODE (subop) != REAL_CST)
9706 return NULL_TREE;
9709 switch (code)
9711 case NOP_EXPR:
9712 case FLOAT_EXPR:
9713 case CONVERT_EXPR:
9714 case FIX_TRUNC_EXPR:
9715 case FIX_FLOOR_EXPR:
9716 case FIX_CEIL_EXPR:
9717 return fold_convert_const (code, type, op0);
9719 case NEGATE_EXPR:
9720 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9721 return fold_negate_const (op0, type);
9722 else
9723 return NULL_TREE;
9725 case ABS_EXPR:
9726 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9727 return fold_abs_const (op0, type);
9728 else
9729 return NULL_TREE;
9731 case BIT_NOT_EXPR:
9732 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
9734 t = build_int_2 (~ TREE_INT_CST_LOW (op0), ~ TREE_INT_CST_HIGH (op0));
9735 TREE_TYPE (t) = type;
9736 force_fit_type (t, 0);
9737 TREE_OVERFLOW (t) = TREE_OVERFLOW (op0);
9738 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (op0);
9739 return t;
9741 else
9742 return NULL_TREE;
9744 case REALPART_EXPR:
9745 if (TREE_CODE (op0) == COMPLEX_CST)
9746 return TREE_REALPART (op0);
9747 else
9748 return NULL_TREE;
9750 case IMAGPART_EXPR:
9751 if (TREE_CODE (op0) == COMPLEX_CST)
9752 return TREE_IMAGPART (op0);
9753 else
9754 return NULL_TREE;
9756 case CONJ_EXPR:
9757 if (TREE_CODE (op0) == COMPLEX_CST
9758 && TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
9759 return build_complex (type, TREE_REALPART (op0),
9760 negate_expr (TREE_IMAGPART (op0)));
9761 return NULL_TREE;
9763 default:
9764 return NULL_TREE;
9768 /* If EXP represents referencing an element in a constant string
9769 (either via pointer arithmetic or array indexing), return the
9770 tree representing the value accessed, otherwise return NULL. */
9772 tree
9773 fold_read_from_constant_string (tree exp)
9775 if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
9777 tree exp1 = TREE_OPERAND (exp, 0);
9778 tree index;
9779 tree string;
9781 if (TREE_CODE (exp) == INDIRECT_REF)
9783 string = string_constant (exp1, &index);
9785 else
9787 tree domain = TYPE_DOMAIN (TREE_TYPE (exp1));
9788 tree low_bound = domain ? TYPE_MIN_VALUE (domain) : integer_zero_node;
9789 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
9791 /* Optimize the special-case of a zero lower bound.
9793 We convert the low_bound to sizetype to avoid some problems
9794 with constant folding. (E.g. suppose the lower bound is 1,
9795 and its mode is QI. Without the conversion,l (ARRAY
9796 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9797 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
9798 if (! integer_zerop (low_bound))
9799 index = size_diffop (index, fold_convert (sizetype, low_bound));
9801 string = exp1;
9804 if (string
9805 && TREE_CODE (string) == STRING_CST
9806 && TREE_CODE (index) == INTEGER_CST
9807 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
9808 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
9809 == MODE_INT)
9810 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
9811 return fold_convert (TREE_TYPE (exp),
9812 build_int_2 ((TREE_STRING_POINTER (string)
9813 [TREE_INT_CST_LOW (index)]), 0));
9815 return NULL;
9818 /* Return the tree for neg (ARG0) when ARG0 is known to be either
9819 an integer constant or real constant.
9821 TYPE is the type of the result. */
9823 static tree
9824 fold_negate_const (tree arg0, tree type)
9826 tree t = NULL_TREE;
9828 if (TREE_CODE (arg0) == INTEGER_CST)
9830 unsigned HOST_WIDE_INT low;
9831 HOST_WIDE_INT high;
9832 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
9833 TREE_INT_CST_HIGH (arg0),
9834 &low, &high);
9835 t = build_int_2 (low, high);
9836 TREE_TYPE (t) = type;
9837 TREE_OVERFLOW (t)
9838 = (TREE_OVERFLOW (arg0)
9839 | force_fit_type (t, overflow && !TYPE_UNSIGNED (type)));
9840 TREE_CONSTANT_OVERFLOW (t)
9841 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
9843 else if (TREE_CODE (arg0) == REAL_CST)
9844 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
9845 #ifdef ENABLE_CHECKING
9846 else
9847 abort ();
9848 #endif
9850 return t;
9853 /* Return the tree for abs (ARG0) when ARG0 is known to be either
9854 an integer constant or real constant.
9856 TYPE is the type of the result. */
9858 static tree
9859 fold_abs_const (tree arg0, tree type)
9861 tree t = NULL_TREE;
9863 if (TREE_CODE (arg0) == INTEGER_CST)
9865 /* If the value is unsigned, then the absolute value is
9866 the same as the ordinary value. */
9867 if (TYPE_UNSIGNED (type))
9868 return arg0;
9869 /* Similarly, if the value is non-negative. */
9870 else if (INT_CST_LT (integer_minus_one_node, arg0))
9871 return arg0;
9872 /* If the value is negative, then the absolute value is
9873 its negation. */
9874 else
9876 unsigned HOST_WIDE_INT low;
9877 HOST_WIDE_INT high;
9878 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
9879 TREE_INT_CST_HIGH (arg0),
9880 &low, &high);
9881 t = build_int_2 (low, high);
9882 TREE_TYPE (t) = type;
9883 TREE_OVERFLOW (t)
9884 = (TREE_OVERFLOW (arg0)
9885 | force_fit_type (t, overflow));
9886 TREE_CONSTANT_OVERFLOW (t)
9887 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg0);
9888 return t;
9891 else if (TREE_CODE (arg0) == REAL_CST)
9893 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
9894 return build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
9895 else
9896 return arg0;
9898 #ifdef ENABLE_CHECKING
9899 else
9900 abort ();
9901 #endif
9903 return t;
9906 /* Given CODE, a relational operator, the target type, TYPE and two
9907 constant operands OP0 and OP1, return the result of the
9908 relational operation. If the result is not a compile time
9909 constant, then return NULL_TREE. */
9911 static tree
9912 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
9914 tree tem;
9915 int invert;
9917 /* From here on, the only cases we handle are when the result is
9918 known to be a constant.
9920 To compute GT, swap the arguments and do LT.
9921 To compute GE, do LT and invert the result.
9922 To compute LE, swap the arguments, do LT and invert the result.
9923 To compute NE, do EQ and invert the result.
9925 Therefore, the code below must handle only EQ and LT. */
9927 if (code == LE_EXPR || code == GT_EXPR)
9929 tem = op0, op0 = op1, op1 = tem;
9930 code = swap_tree_comparison (code);
9933 /* Note that it is safe to invert for real values here because we
9934 will check below in the one case that it matters. */
9936 tem = NULL_TREE;
9937 invert = 0;
9938 if (code == NE_EXPR || code == GE_EXPR)
9940 invert = 1;
9941 code = invert_tree_comparison (code, false);
9944 /* Compute a result for LT or EQ if args permit;
9945 Otherwise return T. */
9946 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
9948 if (code == EQ_EXPR)
9949 tem = build_int_2 (tree_int_cst_equal (op0, op1), 0);
9950 else
9951 tem = build_int_2 ((TYPE_UNSIGNED (TREE_TYPE (op0))
9952 ? INT_CST_LT_UNSIGNED (op0, op1)
9953 : INT_CST_LT (op0, op1)),
9957 else if (code == EQ_EXPR && !TREE_SIDE_EFFECTS (op0)
9958 && integer_zerop (op1) && tree_expr_nonzero_p (op0))
9959 tem = build_int_2 (0, 0);
9961 /* Two real constants can be compared explicitly. */
9962 else if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
9964 /* If either operand is a NaN, the result is false with two
9965 exceptions: First, an NE_EXPR is true on NaNs, but that case
9966 is already handled correctly since we will be inverting the
9967 result for NE_EXPR. Second, if we had inverted a LE_EXPR
9968 or a GE_EXPR into a LT_EXPR, we must return true so that it
9969 will be inverted into false. */
9971 if (REAL_VALUE_ISNAN (TREE_REAL_CST (op0))
9972 || REAL_VALUE_ISNAN (TREE_REAL_CST (op1)))
9973 tem = build_int_2 (invert && code == LT_EXPR, 0);
9975 else if (code == EQ_EXPR)
9976 tem = build_int_2 (REAL_VALUES_EQUAL (TREE_REAL_CST (op0),
9977 TREE_REAL_CST (op1)),
9979 else
9980 tem = build_int_2 (REAL_VALUES_LESS (TREE_REAL_CST (op0),
9981 TREE_REAL_CST (op1)),
9985 if (tem == NULL_TREE)
9986 return NULL_TREE;
9988 if (invert)
9989 TREE_INT_CST_LOW (tem) ^= 1;
9991 TREE_TYPE (tem) = type;
9992 if (TREE_CODE (type) == BOOLEAN_TYPE)
9993 return lang_hooks.truthvalue_conversion (tem);
9994 return tem;
9997 #include "gt-fold-const.h"